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<Annotation *> &&annotations, ArenaVector<ParamDecorators> &&paramDecorators, bool isComputed)44     explicit MethodDefinition(MethodDefinitionKind kind, Expression *key, FunctionExpression *value,
45                               ModifierFlags modifiers, ArenaAllocator *allocator, ArenaVector<Decorator *> &&decorators,
46                               ArenaVector<Annotation *> &&annotations, ArenaVector<ParamDecorators> &&paramDecorators,
47                               bool isComputed)
48         : Statement(AstNodeType::METHOD_DEFINITION),
49           kind_(kind),
50           key_(key),
51           value_(value),
52           modifiers_(modifiers),
53           overloads_(allocator->Adapter()),
54           decorators_(std::move(decorators)),
55           annotations_(std::move(annotations)),
56           paramDecorators_(std::move(paramDecorators)),
57           isComputed_(isComputed)
58     {
59     }
60 
Kind() const61     MethodDefinitionKind Kind() const
62     {
63         return kind_;
64     }
65 
Modifiers() const66     ModifierFlags Modifiers() const
67     {
68         return modifiers_;
69     }
70 
Key() const71     const Expression *Key() const
72     {
73         return key_;
74     }
75 
Key()76     Expression *Key()
77     {
78         return key_;
79     }
80 
SetKey(Expression *key)81     void SetKey(Expression *key)
82     {
83         key_ = key;
84     }
85 
Value() const86     const FunctionExpression *Value() const
87     {
88         return value_;
89     }
90 
Computed() const91     bool Computed() const
92     {
93         return isComputed_;
94     }
95 
IsAbstract() const96     bool IsAbstract() const
97     {
98         return (modifiers_ & ModifierFlags::ABSTRACT) != 0;
99     }
100 
IsStatic() const101     bool IsStatic() const
102     {
103         return (modifiers_ & ModifierFlags::STATIC) != 0;
104     }
105 
IsAccessor() const106     bool IsAccessor() const
107     {
108         return (kind_ == MethodDefinitionKind::GET) || (kind_ == MethodDefinitionKind::SET);
109     }
110 
IsOptional() const111     bool IsOptional() const
112     {
113         return (modifiers_ & ModifierFlags::OPTIONAL) != 0;
114     }
115 
IsPrivate() const116     bool IsPrivate() const
117     {
118         return key_->IsPrivateIdentifier();
119     }
120 
Overloads() const121     const ArenaVector<MethodDefinition *> &Overloads() const
122     {
123         return overloads_;
124     }
125 
Decorators() const126     const ArenaVector<Decorator *> &Decorators() const
127     {
128         return decorators_;
129     }
130 
Annotations() const131     const ArenaVector<Annotation *> &Annotations() const
132     {
133         return annotations_;
134     }
135 
SetAnnotations(ArenaVector<Annotation *> &&annotations)136     void SetAnnotations(ArenaVector<Annotation *> &&annotations)
137     {
138         annotations_ = std::move(annotations);
139     }
140 
GetParamDecorators() const141     const ArenaVector<ParamDecorators> &GetParamDecorators() const
142     {
143         return paramDecorators_;
144     }
145 
HasParamDecorators() const146     bool HasParamDecorators() const
147     {
148         return !paramDecorators_.empty();
149     }
150 
HasDecorators() const151     bool HasDecorators() const
152     {
153         return !decorators_.empty();
154     }
155 
SetOverloads(ArenaVector<MethodDefinition *> &&overloads)156     void SetOverloads(ArenaVector<MethodDefinition *> &&overloads)
157     {
158         overloads_ = std::move(overloads);
159     }
160 
AddOverload(MethodDefinition *overload)161     void AddOverload(MethodDefinition *overload)
162     {
163         overloads_.push_back(overload);
164     }
165 
SetKeyReg(compiler::VReg keyReg)166     void SetKeyReg(compiler::VReg keyReg)
167     {
168         keyReg_ = keyReg;
169     }
170 
KeyReg() const171     compiler::VReg KeyReg() const
172     {
173         return keyReg_;
174     }
175 
176     const ScriptFunction *Function() const;
177 
178     ScriptFunction *Function();
179 
180     void Iterate(const NodeTraverser &cb) const override;
181     void Dump(ir::AstDumper *dumper) const override;
182     void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
183     checker::Type *Check([[maybe_unused]] checker::Checker *checker) const override;
184     void UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) override;
185 
186 private:
187     MethodDefinitionKind kind_;
188     Expression *key_;
189     compiler::VReg keyReg_ {0};
190     FunctionExpression *value_;
191     ModifierFlags modifiers_;
192     ArenaVector<MethodDefinition *> overloads_;
193     ArenaVector<Decorator *> decorators_;
194     ArenaVector<Annotation *> annotations_;
195     ArenaVector<ParamDecorators> paramDecorators_;
196     bool isComputed_;
197 };
198 
199 }  // namespace panda::es2panda::ir
200 
201 #endif
202