1 /** 2 * Copyright (c) 2021 - 2023 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/base/classElement.h" 20 21 namespace panda::es2panda::checker { 22 class ETSAnalyzer; 23 } // namespace panda::es2panda::checker 24 25 namespace panda::es2panda::ir { 26 27 class Expression; 28 class ScriptFunction; 29 30 enum class MethodDefinitionKind { NONE, CONSTRUCTOR, METHOD, EXTENSION_METHOD, GET, SET }; 31 32 class MethodDefinition : public ClassElement { 33 public: 34 MethodDefinition() = delete; 35 ~MethodDefinition() override = default; 36 37 NO_COPY_SEMANTIC(MethodDefinition); 38 NO_MOVE_SEMANTIC(MethodDefinition); 39 40 using OverloadsT = ArenaVector<MethodDefinition *>; 41 MethodDefinition(MethodDefinitionKind const kind,Expression * const key,Expression * const value,ModifierFlags const modifiers,ArenaAllocator * const allocator,bool const isComputed)42 explicit MethodDefinition(MethodDefinitionKind const kind, Expression *const key, Expression *const value, 43 ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const isComputed) 44 : ClassElement(AstNodeType::METHOD_DEFINITION, key, value, modifiers, allocator, isComputed), 45 kind_(kind), 46 overloads_(allocator->Adapter()) 47 { 48 } 49 50 // NOTE (csabahurton): these friend relationships can be removed once there are getters for private fields 51 friend class checker::ETSAnalyzer; 52 Kind()53 MethodDefinitionKind Kind() const 54 { 55 return kind_; 56 } 57 IsConstructor()58 [[nodiscard]] bool IsConstructor() const noexcept 59 { 60 return kind_ == MethodDefinitionKind::CONSTRUCTOR; 61 } 62 IsExtensionMethod()63 [[nodiscard]] bool IsExtensionMethod() const noexcept 64 { 65 return kind_ == MethodDefinitionKind::EXTENSION_METHOD; 66 } 67 Key()68 Expression const *Key() const 69 { 70 return key_; 71 } 72 Value()73 Expression const *Value() const 74 { 75 return value_; 76 } 77 Overloads()78 [[nodiscard]] const OverloadsT &Overloads() const noexcept 79 { 80 return overloads_; 81 } 82 SetOverloads(OverloadsT && overloads)83 void SetOverloads(OverloadsT &&overloads) 84 { 85 overloads_ = std::move(overloads); 86 } 87 ClearOverloads()88 void ClearOverloads() 89 { 90 overloads_.clear(); 91 } 92 AddOverload(MethodDefinition * const overload)93 void AddOverload(MethodDefinition *const overload) 94 { 95 overloads_.emplace_back(overload); 96 } 97 HasOverload(MethodDefinition * overload)98 [[nodiscard]] bool HasOverload(MethodDefinition *overload) noexcept 99 { 100 return std::find(overloads_.begin(), overloads_.end(), overload) != overloads_.end(); 101 } 102 103 ScriptFunction *Function(); 104 const ScriptFunction *Function() const; 105 PrivateFieldKind ToPrivateFieldKind(bool isStatic) const override; 106 107 // NOLINTNEXTLINE(google-default-arguments) 108 [[nodiscard]] MethodDefinition *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; 109 110 void TransformChildren(const NodeTransformer &cb) override; 111 void Iterate(const NodeTraverser &cb) const override; 112 113 void Dump(ir::AstDumper *dumper) const override; 114 void Dump(ir::SrcDumper *dumper) const override; 115 void Compile(compiler::PandaGen *pg) const override; 116 void Compile(compiler::ETSGen *etsg) const override; 117 checker::Type *Check(checker::TSChecker *checker) override; 118 checker::Type *Check(checker::ETSChecker *checker) override; 119 Accept(ASTVisitorT * v)120 void Accept(ASTVisitorT *v) override 121 { 122 v->Accept(this); 123 } 124 125 private: 126 MethodDefinitionKind kind_; 127 OverloadsT overloads_; 128 }; 129 } // namespace panda::es2panda::ir 130 131 #endif 132