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 #include <ir/expression.h> 21 22 namespace panda::es2panda::compiler { 23 class PandaGen; 24 } // namespace panda::es2panda::compiler 25 26 namespace panda::es2panda::checker { 27 class Checker; 28 class Type; 29 } // namespace panda::es2panda::checker 30 31 namespace panda::es2panda::ir { 32 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 IsPrivate()114 bool IsPrivate() const 115 { 116 return key_->IsPrivateIdentifier(); 117 } 118 Overloads()119 const ArenaVector<MethodDefinition *> &Overloads() const 120 { 121 return overloads_; 122 } 123 Decorators()124 const ArenaVector<Decorator *> &Decorators() const 125 { 126 return decorators_; 127 } 128 GetParamDecorators()129 const ArenaVector<ParamDecorators> &GetParamDecorators() const 130 { 131 return paramDecorators_; 132 } 133 HasParamDecorators()134 bool HasParamDecorators() const 135 { 136 return !paramDecorators_.empty(); 137 } 138 HasDecorators()139 bool HasDecorators() const 140 { 141 return !decorators_.empty(); 142 } 143 SetOverloads(ArenaVector<MethodDefinition * > && overloads)144 void SetOverloads(ArenaVector<MethodDefinition *> &&overloads) 145 { 146 overloads_ = std::move(overloads); 147 } 148 AddOverload(MethodDefinition * overload)149 void AddOverload(MethodDefinition *overload) 150 { 151 overloads_.push_back(overload); 152 } 153 SetKeyReg(compiler::VReg keyReg)154 void SetKeyReg(compiler::VReg keyReg) 155 { 156 keyReg_ = keyReg; 157 } 158 KeyReg()159 compiler::VReg KeyReg() const 160 { 161 return keyReg_; 162 } 163 164 const ScriptFunction *Function() const; 165 166 ScriptFunction *Function(); 167 168 void Iterate(const NodeTraverser &cb) const override; 169 void Dump(ir::AstDumper *dumper) const override; 170 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 171 checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override; 172 void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override; 173 174 private: 175 MethodDefinitionKind kind_; 176 Expression *key_; 177 compiler::VReg keyReg_ {0}; 178 FunctionExpression *value_; 179 ModifierFlags modifiers_; 180 ArenaVector<MethodDefinition *> overloads_; 181 ArenaVector<Decorator *> decorators_; 182 ArenaVector<ParamDecorators> paramDecorators_; 183 bool isComputed_; 184 }; 185 186 } // namespace panda::es2panda::ir 187 188 #endif 189