• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<ClassDefinition> {
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(ir::Identifier * id)32     ClassDefinitionBuilder &SetIdentifier(ir::Identifier *id)
33     {
34         ident_ = id;
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 
SetTSTypeParameterDeclaration(TSTypeParameterDeclaration * typeParams)56     ClassDefinitionBuilder &SetTSTypeParameterDeclaration(TSTypeParameterDeclaration *typeParams)
57     {
58         typeParams_ = typeParams;
59         return *this;
60     }
61 
SetTSTypeParameterInstantiation(TSTypeParameterInstantiation * superTypeParams)62     ClassDefinitionBuilder &SetTSTypeParameterInstantiation(TSTypeParameterInstantiation *superTypeParams)
63     {
64         superTypeParams_ = superTypeParams;
65         return *this;
66     }
67 
SetImplements(ArenaVector<TSClassImplements * > implements)68     ClassDefinitionBuilder &SetImplements(ArenaVector<TSClassImplements *> implements)
69     {
70         implements_ = std::move(implements);
71         return *this;
72     }
73 
AddImplements(TSClassImplements * implement)74     ClassDefinitionBuilder &AddImplements(TSClassImplements *implement)
75     {
76         implements_.push_back(implement);
77         return *this;
78     }
79 
Build()80     ClassDefinition *Build()
81     {
82         auto node = AllocNode(util::StringView(), ident_, typeParams_, superTypeParams_, std::move(implements_), ctor_,
83                               superClass_, std::move(body_), ir::ClassDefinitionModifiers::CLASS_DECL,
84                               ir::ModifierFlags::NONE, Language(Language::Id::ETS));
85         return node;
86     }
87 
88 private:
89     util::StringView privateId_ {};
90     Identifier *ident_ {};
91     MethodDefinition *ctor_ {};
92     Expression *superClass_ {};
93     ArenaVector<AstNode *> body_;
94     TSTypeParameterDeclaration *typeParams_ {};
95     TSTypeParameterInstantiation *superTypeParams_ {};
96     ArenaVector<TSClassImplements *> implements_;
97 };
98 
99 }  // namespace ark::es2panda::ir
100 #endif  // ES2PANDA_UTIL_INCLUDE_CLASS_DEFINITION_BUILDER