• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 - 2023 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_EXPRESSION_CALL_EXPRESSION_H
17 #define ES2PANDA_IR_EXPRESSION_CALL_EXPRESSION_H
18 
19 #include "varbinder/variable.h"
20 #include "checker/types/ets/etsFunctionType.h"
21 #include "ir/expression.h"
22 
23 namespace panda::es2panda::checker {
24 class ETSAnalyzer;
25 class TSAnalyzer;
26 class Signature;
27 }  // namespace panda::es2panda::checker
28 
29 namespace panda::es2panda::compiler {
30 class JSCompiler;
31 class ETSCompiler;
32 }  // namespace panda::es2panda::compiler
33 
34 namespace panda::es2panda::ir {
35 class TSTypeParameterInstantiation;
36 
37 class CallExpression : public MaybeOptionalExpression {
38 public:
39     CallExpression() = delete;
40     ~CallExpression() override = default;
41 
42     NO_COPY_SEMANTIC(CallExpression);
43     NO_MOVE_SEMANTIC(CallExpression);
44 
45     explicit CallExpression(Expression *const callee, ArenaVector<Expression *> &&arguments,
46                             TSTypeParameterInstantiation *const typeParams, bool const optional,
47                             bool const trailingComma = false)
MaybeOptionalExpression(AstNodeType::CALL_EXPRESSION,optional)48         : MaybeOptionalExpression(AstNodeType::CALL_EXPRESSION, optional),
49           callee_(callee),
50           arguments_(std::move(arguments)),
51           typeParams_(typeParams),
52           trailingComma_(trailingComma)
53     {
54     }
55 
56     explicit CallExpression(CallExpression const &other, ArenaAllocator *allocator);
57 
58     // NOTE (csabahurton): these friend relationships can be removed once there are getters for private fields
59     friend class checker::TSAnalyzer;
60     friend class checker::ETSAnalyzer;
61     friend class compiler::JSCompiler;
62     friend class compiler::ETSCompiler;
63 
Callee()64     const Expression *Callee() const
65     {
66         return callee_;
67     }
68 
Callee()69     [[nodiscard]] Expression *Callee() noexcept
70     {
71         return callee_;
72     }
73 
SetCallee(Expression * callee)74     void SetCallee(Expression *callee) noexcept
75     {
76         callee_ = callee;
77     }
78 
TypeParams()79     [[nodiscard]] const TSTypeParameterInstantiation *TypeParams() const noexcept
80     {
81         return typeParams_;
82     }
83 
TypeParams()84     [[nodiscard]] TSTypeParameterInstantiation *TypeParams() noexcept
85     {
86         return typeParams_;
87     }
88 
Arguments()89     [[nodiscard]] const ArenaVector<Expression *> &Arguments() const noexcept
90     {
91         return arguments_;
92     }
93 
Arguments()94     [[nodiscard]] ArenaVector<Expression *> &Arguments() noexcept
95     {
96         return arguments_;
97     }
98 
HasTrailingComma()99     [[nodiscard]] bool HasTrailingComma() const noexcept
100     {
101         return trailingComma_;
102     }
103 
Signature()104     [[nodiscard]] checker::Signature *Signature() noexcept
105     {
106         return signature_;
107     }
108 
Signature()109     [[nodiscard]] checker::Signature *Signature() const noexcept
110     {
111         return signature_;
112     }
113 
SetSignature(checker::Signature * const signature)114     void SetSignature(checker::Signature *const signature) noexcept
115     {
116         signature_ = signature;
117     }
118 
SetTypeParams(TSTypeParameterInstantiation * const typeParams)119     void SetTypeParams(TSTypeParameterInstantiation *const typeParams) noexcept
120     {
121         typeParams_ = typeParams;
122     }
123 
UncheckedType()124     [[nodiscard]] checker::Type *UncheckedType() const noexcept
125     {
126         return uncheckedType_;
127     }
128 
SetUncheckedType(checker::Type * type)129     void SetUncheckedType(checker::Type *type) noexcept
130     {
131         uncheckedType_ = type;
132     }
133 
SetTrailingBlock(ir::BlockStatement * const block)134     void SetTrailingBlock(ir::BlockStatement *const block) noexcept
135     {
136         trailingBlock_ = block;
137     }
138 
TrailingBlock()139     [[nodiscard]] ir::BlockStatement *TrailingBlock() const noexcept
140     {
141         return trailingBlock_;
142     }
143 
SetIsTrailingBlockInNewLine(bool const isNewLine)144     void SetIsTrailingBlockInNewLine(bool const isNewLine) noexcept
145     {
146         isTrailingBlockInNewLine_ = isNewLine;
147     }
148 
IsTrailingBlockInNewLine()149     [[nodiscard]] bool IsTrailingBlockInNewLine() const noexcept
150     {
151         return isTrailingBlockInNewLine_;
152     }
153 
154     // NOLINTNEXTLINE(google-default-arguments)
155     [[nodiscard]] CallExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override;
156 
157     void TransformChildren(const NodeTransformer &cb) override;
158     void Iterate(const NodeTraverser &cb) const override;
159 
160     void Dump(ir::AstDumper *dumper) const override;
161     void Dump(ir::SrcDumper *dumper) const override;
162     void Compile(compiler::PandaGen *pg) const override;
163     void Compile(compiler::ETSGen *etsg) const override;
164     checker::Type *Check(checker::TSChecker *checker) override;
165     checker::Type *Check(checker::ETSChecker *checker) override;
166 
Accept(ASTVisitorT * v)167     void Accept(ASTVisitorT *v) override
168     {
169         v->Accept(this);
170     }
171 
172 protected:
173     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
174     Expression *callee_;
175     ArenaVector<Expression *> arguments_;
176     TSTypeParameterInstantiation *typeParams_;
177     checker::Signature *signature_ {};
178     bool trailingComma_;
179     // for trailing lambda feature in ets
180     ir::BlockStatement *trailingBlock_ {};
181     bool isTrailingBlockInNewLine_ {false};
182     checker::Type *uncheckedType_ {};
183     // NOLINTEND(misc-non-private-member-variables-in-classes)
184 
185 private:
186     bool IsETSConstructorCall() const;
187     checker::Type *InitAnonymousLambdaCallee(checker::ETSChecker *checker, Expression *callee,
188                                              checker::Type *calleeType);
189 };
190 }  // namespace panda::es2panda::ir
191 
192 #endif
193