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
24namespace ark::panda_file {
25class ClassDataAccessor;
26}  // namespace ark::panda_file
27
28namespace ark::panda_file {
29class MethodDataAccessor;
30}  // namespace ark::panda_file
31
32namespace ark::es2panda::ir {
33class AstNode;
34class Expression;
35class TypeNode;
36class ExpressionStatement;
37class BlockStatement;
38class MethodDefinition;
39class FunctionExpression;
40class Identifier;
41class Statement;
42}  // namespace ark::es2panda::ir
43
44namespace ark::es2panda::checker {
45class ETSChecker;
46}  // namespace ark::es2panda::checker
47
48namespace ark::es2panda::evaluate {
49
50class MethodBuilder {
51public:
52    explicit MethodBuilder(checker::ETSChecker *checker, panda_file::MethodDataAccessor &mda,
53                           ir::ModifierFlags classModifierFlags);
54
55    ir::AstNode *Build() &&;
56
57    [[nodiscard]] bool IsConstructor() const
58    {
59        return methodName_ == compiler::Signatures::CTOR;
60    }
61
62    [[nodiscard]] bool IsStaticConstructor() const
63    {
64        return methodName_ == compiler::Signatures::CCTOR;
65    }
66
67    [[nodiscard]] bool IsAbstractMethod() const
68    {
69        return (modifierFlags_ & ir::ModifierFlags::ABSTRACT) != 0;
70    }
71
72private:
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
84private:
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