1 /** 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ES2PANDA_PARSER_INCLUDE_AST_METHOD_DEFINITION_H 17 #define ES2PANDA_PARSER_INCLUDE_AST_METHOD_DEFINITION_H 18 19 #include <ir/statement.h> 20 21 namespace panda::es2panda::compiler { 22 class PandaGen; 23 } // namespace panda::es2panda::compiler 24 25 namespace panda::es2panda::checker { 26 class Checker; 27 class Type; 28 } // namespace panda::es2panda::checker 29 30 namespace panda::es2panda::ir { 31 32 class Expression; 33 class FunctionExpression; 34 35 enum class MethodDefinitionKind { CONSTRUCTOR, METHOD, GET, SET }; 36 37 struct ParamDecorators { 38 size_t paramIndex; 39 ArenaVector<Decorator *> decorators; 40 }; 41 42 class MethodDefinition : public Statement { 43 public: MethodDefinition(MethodDefinitionKind kind,Expression * key,FunctionExpression * value,ModifierFlags modifiers,ArenaAllocator * allocator,ArenaVector<Decorator * > && decorators,ArenaVector<ParamDecorators> && paramDecorators,bool isComputed)44 explicit MethodDefinition(MethodDefinitionKind kind, Expression *key, FunctionExpression *value, 45 ModifierFlags modifiers, ArenaAllocator *allocator, ArenaVector<Decorator *> &&decorators, 46 ArenaVector<ParamDecorators> &¶mDecorators, bool isComputed) 47 : Statement(AstNodeType::METHOD_DEFINITION), 48 kind_(kind), 49 key_(key), 50 value_(value), 51 modifiers_(modifiers), 52 overloads_(allocator->Adapter()), 53 decorators_(std::move(decorators)), 54 paramDecorators_(std::move(paramDecorators)), 55 isComputed_(isComputed) 56 { 57 } 58 Kind()59 MethodDefinitionKind Kind() const 60 { 61 return kind_; 62 } 63 Modifiers()64 ModifierFlags Modifiers() const 65 { 66 return modifiers_; 67 } 68 Key()69 const Expression *Key() const 70 { 71 return key_; 72 } 73 Key()74 Expression *Key() 75 { 76 return key_; 77 } 78 SetKey(Expression * key)79 void SetKey(Expression *key) 80 { 81 key_ = key; 82 } 83 Value()84 const FunctionExpression *Value() const 85 { 86 return value_; 87 } 88 Computed()89 bool Computed() const 90 { 91 return isComputed_; 92 } 93 IsAbstract()94 bool IsAbstract() const 95 { 96 return (modifiers_ & ModifierFlags::ABSTRACT) != 0; 97 } 98 IsStatic()99 bool IsStatic() const 100 { 101 return (modifiers_ & ModifierFlags::STATIC) != 0; 102 } 103 IsAccessor()104 bool IsAccessor() const 105 { 106 return (kind_ == MethodDefinitionKind::GET) || (kind_ == MethodDefinitionKind::SET); 107 } 108 IsOptional()109 bool IsOptional() const 110 { 111 return (modifiers_ & ModifierFlags::OPTIONAL) != 0; 112 } 113 Overloads()114 const ArenaVector<MethodDefinition *> &Overloads() const 115 { 116 return overloads_; 117 } 118 Decorators()119 const ArenaVector<Decorator *> &Decorators() const 120 { 121 return decorators_; 122 } 123 GetParamDecorators()124 const ArenaVector<ParamDecorators> &GetParamDecorators() const 125 { 126 return paramDecorators_; 127 } 128 HasParamDecorators()129 bool HasParamDecorators() const 130 { 131 return !paramDecorators_.empty(); 132 } 133 HasDecorators()134 bool HasDecorators() const 135 { 136 return !decorators_.empty(); 137 } 138 SetOverloads(ArenaVector<MethodDefinition * > && overloads)139 void SetOverloads(ArenaVector<MethodDefinition *> &&overloads) 140 { 141 overloads_ = std::move(overloads); 142 } 143 AddOverload(MethodDefinition * overload)144 void AddOverload(MethodDefinition *overload) 145 { 146 overloads_.push_back(overload); 147 } 148 149 const ScriptFunction *Function() const; 150 151 ScriptFunction *Function(); 152 153 void Iterate(const NodeTraverser &cb) const override; 154 void Dump(ir::AstDumper *dumper) const override; 155 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 156 checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override; 157 void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override; 158 159 private: 160 MethodDefinitionKind kind_; 161 Expression *key_; 162 FunctionExpression *value_; 163 ModifierFlags modifiers_; 164 ArenaVector<MethodDefinition *> overloads_; 165 ArenaVector<Decorator *> decorators_; 166 ArenaVector<ParamDecorators> paramDecorators_; 167 bool isComputed_; 168 }; 169 170 } // namespace panda::es2panda::ir 171 172 #endif 173