1 /** 2 * Copyright (c) 2024 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_UTIL_INCLUDE_CLASS_DEFINITION_BUILDER 17 #define ES2PANDA_UTIL_INCLUDE_CLASS_DEFINITION_BUILDER 18 19 #include "ir/base/classDefinition.h" 20 #include "mem/arena_allocator.h" 21 #include "astBuilder.h" 22 23 namespace ark::es2panda::ir { 24 25 class ClassDefinitionBuilder : public AstBuilder { 26 public: ClassDefinitionBuilder(ark::ArenaAllocator * allocator)27 explicit ClassDefinitionBuilder(ark::ArenaAllocator *allocator) 28 : AstBuilder(allocator), body_(Allocator()->Adapter()), implements_(Allocator()->Adapter()) 29 { 30 } 31 SetIdentifier(util::StringView id)32 ClassDefinitionBuilder &SetIdentifier(util::StringView id) 33 { 34 ident_ = AllocNode<ir::Identifier>(id, Allocator()); 35 return *this; 36 } 37 SetConstructor(MethodDefinition * ctor)38 ClassDefinitionBuilder &SetConstructor(MethodDefinition *ctor) 39 { 40 ctor_ = ctor; 41 return *this; 42 } 43 SetSuperClass(Expression * superClass)44 ClassDefinitionBuilder &SetSuperClass(Expression *superClass) 45 { 46 superClass_ = superClass; 47 return *this; 48 } 49 AddProperty(AstNode * property)50 ClassDefinitionBuilder &AddProperty(AstNode *property) 51 { 52 body_.push_back(property); 53 return *this; 54 } 55 SetParent(AstNode * const parent)56 ClassDefinitionBuilder &SetParent(AstNode *const parent) 57 { 58 parent_ = parent; 59 return *this; 60 } 61 SetTSTypeParameterDeclaration(TSTypeParameterDeclaration * typeParams)62 ClassDefinitionBuilder &SetTSTypeParameterDeclaration(TSTypeParameterDeclaration *typeParams) 63 { 64 typeParams_ = typeParams; 65 return *this; 66 } 67 SetTSTypeParameterInstantiation(TSTypeParameterInstantiation * superTypeParams)68 ClassDefinitionBuilder &SetTSTypeParameterInstantiation(TSTypeParameterInstantiation *superTypeParams) 69 { 70 superTypeParams_ = superTypeParams; 71 return *this; 72 } 73 SetImplements(ArenaVector<TSClassImplements * > implements)74 ClassDefinitionBuilder &SetImplements(ArenaVector<TSClassImplements *> implements) 75 { 76 implements_ = std::move(implements); 77 return *this; 78 } 79 AddImplements(TSClassImplements * implement)80 ClassDefinitionBuilder &AddImplements(TSClassImplements *implement) 81 { 82 implements_.push_back(implement); 83 return *this; 84 } 85 Build()86 ClassDefinition *Build() 87 { 88 auto classDef = AllocNode<ClassDefinition>(util::StringView(), ident_, typeParams_, superTypeParams_, 89 std::move(implements_), ctor_, superClass_, std::move(body_), 90 ir::ClassDefinitionModifiers::CLASS_DECL, ir::ModifierFlags::NONE, 91 Language(Language::Id::ETS)); 92 classDef->SetParent(parent_); 93 return classDef; 94 } 95 96 private: 97 util::StringView privateId_ {}; 98 Identifier *ident_ {}; 99 MethodDefinition *ctor_ {}; 100 Expression *superClass_ {}; 101 ArenaVector<AstNode *> body_; 102 TSTypeParameterDeclaration *typeParams_ {}; 103 TSTypeParameterInstantiation *superTypeParams_ {}; 104 ArenaVector<TSClassImplements *> implements_; 105 AstNode *parent_ {}; 106 }; 107 108 } // namespace ark::es2panda::ir 109 #endif // ES2PANDA_UTIL_INCLUDE_CLASS_DEFINITION_BUILDER