• 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_EVALUATE_METHOD_BUILDER_H
17 #define ES2PANDA_EVALUATE_METHOD_BUILDER_H
18 
19 #include "util/ustring.h"
20 #include "ir/astNodeFlags.h"
21 #include "generated/signatures.h"
22 #include "libpandabase/utils/arena_containers.h"
23 
24 namespace ark::panda_file {
25 class ClassDataAccessor;
26 }  // namespace ark::panda_file
27 
28 namespace ark::panda_file {
29 class MethodDataAccessor;
30 }  // namespace ark::panda_file
31 
32 namespace ark::es2panda::ir {
33 class AstNode;
34 class Expression;
35 class TypeNode;
36 class ExpressionStatement;
37 class BlockStatement;
38 class MethodDefinition;
39 class FunctionExpression;
40 class Identifier;
41 class Statement;
42 }  // namespace ark::es2panda::ir
43 
44 namespace ark::es2panda::checker {
45 class ETSChecker;
46 }  // namespace ark::es2panda::checker
47 
48 namespace ark::es2panda::evaluate {
49 
50 class MethodBuilder {
51 public:
52     explicit MethodBuilder(checker::ETSChecker *checker, panda_file::MethodDataAccessor &mda,
53                            ir::ModifierFlags classModifierFlags);
54 
55     ir::AstNode *Build() &&;
56 
IsConstructor()57     [[nodiscard]] bool IsConstructor() const
58     {
59         return methodName_ == compiler::Signatures::CTOR;
60     }
61 
IsStaticConstructor()62     [[nodiscard]] bool IsStaticConstructor() const
63     {
64         return methodName_ == compiler::Signatures::CCTOR;
65     }
66 
IsAbstractMethod()67     [[nodiscard]] bool IsAbstractMethod() const
68     {
69         return (modifierFlags_ & ir::ModifierFlags::ABSTRACT) != 0;
70     }
71 
72 private:
73     void CollectParametersAndReturnType();
74     ir::ExpressionStatement *CreateSuperConstructorExpressionCall();
75     ir::BlockStatement *CreateBody(ArenaVector<ir::Statement *> statements);
76 
77     template <bool IS_STATIC>
78     ir::AstNode *CreateIrConstructor(ir::Identifier *id, ir::BlockStatement *body);
79 
80     ir::MethodDefinition *CreateIrMethod(ir::Identifier *id, ir::BlockStatement *body);
81     ir::FunctionExpression *CreateFunctionExpression(ir::Identifier *id, ir::BlockStatement *body,
82                                                      ir::ScriptFunctionFlags scriptFuncFlags);
83 
84 private:
85     checker::ETSChecker *checker_ {nullptr};
86 
87     panda_file::MethodDataAccessor &mda_;
88 
89     util::StringView methodName_ {};
90     ir::ModifierFlags modifierFlags_ {ir::ModifierFlags::NONE};
91     ArenaVector<ir::Expression *> params_;
92     ir::TypeNode *returnType_ {nullptr};
93 
94     // Modifier flags of the class that owns the method that we want to build.
95     ir::ModifierFlags classModifierFlags_ {ir::ModifierFlags::NONE};
96 };
97 
98 }  // namespace ark::es2panda::evaluate
99 
100 #endif  // ES2PANDA_EVALUATE_METHOD_BUILDER_H
101