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 class MethodDefinition : public Statement { 38 public: MethodDefinition(MethodDefinitionKind kind,Expression * key,FunctionExpression * value,ModifierFlags modifiers,ArenaAllocator * allocator,ArenaVector<Decorator * > && decorators,bool isComputed)39 explicit MethodDefinition(MethodDefinitionKind kind, Expression *key, FunctionExpression *value, 40 ModifierFlags modifiers, ArenaAllocator *allocator, ArenaVector<Decorator *> &&decorators, 41 bool isComputed) 42 : Statement(AstNodeType::METHOD_DEFINITION), 43 kind_(kind), 44 key_(key), 45 value_(value), 46 modifiers_(modifiers), 47 overloads_(allocator->Adapter()), 48 decorators_(std::move(decorators)), 49 isComputed_(isComputed) 50 { 51 } 52 Kind()53 MethodDefinitionKind Kind() const 54 { 55 return kind_; 56 } 57 Modifiers()58 ModifierFlags Modifiers() const 59 { 60 return modifiers_; 61 } 62 Key()63 const Expression *Key() const 64 { 65 return key_; 66 } 67 Value()68 const FunctionExpression *Value() const 69 { 70 return value_; 71 } 72 Computed()73 bool Computed() const 74 { 75 return isComputed_; 76 } 77 IsStatic()78 bool IsStatic() const 79 { 80 return (modifiers_ & ModifierFlags::STATIC) != 0; 81 } 82 IsAccessor()83 bool IsAccessor() const 84 { 85 return (kind_ == MethodDefinitionKind::GET) || (kind_ == MethodDefinitionKind::SET); 86 } 87 IsOptional()88 bool IsOptional() const 89 { 90 return (modifiers_ & ModifierFlags::OPTIONAL) != 0; 91 } 92 Overloads()93 const ArenaVector<MethodDefinition *> &Overloads() const 94 { 95 return overloads_; 96 } 97 Decorators()98 const ArenaVector<Decorator *> &Decorators() const 99 { 100 return decorators_; 101 } 102 SetOverloads(ArenaVector<MethodDefinition * > && overloads)103 void SetOverloads(ArenaVector<MethodDefinition *> &&overloads) 104 { 105 overloads_ = std::move(overloads); 106 } 107 AddOverload(MethodDefinition * overload)108 void AddOverload(MethodDefinition *overload) 109 { 110 overloads_.push_back(overload); 111 } 112 113 const ScriptFunction *Function() const; 114 115 void Iterate(const NodeTraverser &cb) const override; 116 void Dump(ir::AstDumper *dumper) const override; 117 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 118 checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override; 119 void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override; 120 121 private: 122 MethodDefinitionKind kind_; 123 Expression *key_; 124 FunctionExpression *value_; 125 ModifierFlags modifiers_; 126 ArenaVector<MethodDefinition *> overloads_; 127 ArenaVector<Decorator *> decorators_; 128 bool isComputed_; 129 }; 130 131 } // namespace panda::es2panda::ir 132 133 #endif 134