1 /*
2  * Copyright (c) 2021-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_IR_ETS_NEW_CLASS_INSTANCE_EXPRESSION_H
17 #define ES2PANDA_IR_ETS_NEW_CLASS_INSTANCE_EXPRESSION_H
18 
19 #include "compiler/core/vReg.h"
20 #include "ir/expression.h"
21 
22 namespace ark::es2panda::checker {
23 class ETSAnalyzer;
24 class Signature;
25 }  // namespace ark::es2panda::checker
26 
27 namespace ark::es2panda::compiler {
28 class ETSCompiler;
29 }  // namespace ark::es2panda::compiler
30 
31 namespace ark::es2panda::ir {
32 
33 class ClassDefinition;
34 
35 class ETSNewClassInstanceExpression : public Expression {
36 public:
37     ETSNewClassInstanceExpression() = delete;
38     ~ETSNewClassInstanceExpression() override = default;
39 
40     NO_COPY_SEMANTIC(ETSNewClassInstanceExpression);
41     NO_MOVE_SEMANTIC(ETSNewClassInstanceExpression);
42 
ETSNewClassInstanceExpression(ir::Expression *const typeReference, ArenaVector<ir::Expression *> &&arguments, ir::ClassDefinition *const classDefinition)43     explicit ETSNewClassInstanceExpression(ir::Expression *const typeReference,
44                                            ArenaVector<ir::Expression *> &&arguments,
45                                            ir::ClassDefinition *const classDefinition)
46         : Expression(AstNodeType::ETS_NEW_CLASS_INSTANCE_EXPRESSION),
47           typeReference_(typeReference),
48           arguments_(std::move(arguments)),
49           classDef_(classDefinition)
50     {
51     }
52     // NOTE (csabahurton): these friend relationships can be removed once there are getters for private fields
53     friend class checker::ETSAnalyzer;
54     friend class compiler::ETSCompiler;
55 
56     explicit ETSNewClassInstanceExpression(ETSNewClassInstanceExpression const &other, ArenaAllocator *allocator);
57 
58     [[nodiscard]] ir::ClassDefinition *ClassDefinition() noexcept
59     {
60         return classDef_;
61     }
62 
63     [[nodiscard]] const ir::ClassDefinition *ClassDefinition() const noexcept
64     {
65         return classDef_;
66     }
67 
68     [[nodiscard]] ir::Expression *GetTypeRef() const noexcept
69     {
70         return typeReference_;
71     }
72 
73     [[nodiscard]] const ArenaVector<ir::Expression *> &GetArguments() const noexcept
74     {
75         return arguments_;
76     }
77 
78     [[nodiscard]] checker::Signature *GetSignature() const noexcept
79     {
80         return signature_;
81     }
82 
83     void SetSignature(checker::Signature *const signature) noexcept
84     {
85         signature_ = signature;
86     }
87 
AddToArgumentsFront(ir::Expression *expr)88     void AddToArgumentsFront(ir::Expression *expr)
89     {
90         arguments_.insert(arguments_.begin(), expr);
91     }
92 
93     [[nodiscard]] ETSNewClassInstanceExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override;
94 
95     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
96     void Iterate(const NodeTraverser &cb) const override;
97 
98     void Dump(ir::AstDumper *dumper) const override;
99     void Dump(ir::SrcDumper *dumper) const override;
100     void Compile(compiler::PandaGen *pg) const override;
101     void Compile(compiler::ETSGen *etsg) const override;
102     checker::Type *Check(checker::TSChecker *checker) override;
103     checker::Type *Check(checker::ETSChecker *checker) override;
104 
105     void Accept(ASTVisitorT *v) override
106     {
107         v->Accept(this);
108     }
109 
110 private:
111     ir::Expression *typeReference_;
112     ArenaVector<ir::Expression *> arguments_;
113     ir::ClassDefinition *classDef_;
114     checker::Signature *signature_ {};
115 };
116 }  // namespace ark::es2panda::ir
117 
118 #endif
119