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 #include "callExpression.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23
24 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)25 void CallExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
26 {
27 if (auto *transformedNode = cb(callee_); callee_ != transformedNode) {
28 callee_->SetTransformedNode(transformationName, transformedNode);
29 callee_ = transformedNode->AsExpression();
30 }
31
32 if (typeParams_ != nullptr) {
33 if (auto *transformedNode = cb(typeParams_); typeParams_ != transformedNode) {
34 typeParams_->SetTransformedNode(transformationName, transformedNode);
35 typeParams_ = transformedNode->AsTSTypeParameterInstantiation();
36 }
37 }
38
39 for (auto *&it : arguments_) {
40 if (auto *transformedNode = cb(it); it != transformedNode) {
41 it->SetTransformedNode(transformationName, transformedNode);
42 it = transformedNode->AsExpression();
43 }
44 }
45 }
46
Iterate(const NodeTraverser & cb) const47 void CallExpression::Iterate(const NodeTraverser &cb) const
48 {
49 cb(callee_);
50
51 if (typeParams_ != nullptr) {
52 cb(typeParams_);
53 }
54
55 for (auto *it : arguments_) {
56 cb(it);
57 }
58
59 if (trailingBlock_ != nullptr) {
60 cb(trailingBlock_);
61 }
62 }
63
Dump(ir::AstDumper * dumper) const64 void CallExpression::Dump(ir::AstDumper *dumper) const
65 {
66 dumper->Add({{"type", "CallExpression"},
67 {"callee", callee_},
68 {"arguments", arguments_},
69 {"optional", IsOptional()},
70 {"typeParameters", AstDumper::Optional(typeParams_)}});
71 }
72
Dump(ir::SrcDumper * dumper) const73 void CallExpression::Dump(ir::SrcDumper *dumper) const
74 {
75 ASSERT(callee_);
76 callee_->Dump(dumper);
77 if (IsOptional()) {
78 dumper->Add("?.");
79 }
80
81 if (typeParams_ != nullptr) {
82 typeParams_->Dump(dumper);
83 }
84
85 dumper->Add("(");
86 for (auto arg : arguments_) {
87 arg->Dump(dumper);
88 if (arg != arguments_.back()) {
89 dumper->Add(", ");
90 }
91 }
92 dumper->Add(")");
93 }
94
Compile(compiler::PandaGen * pg) const95 void CallExpression::Compile(compiler::PandaGen *pg) const
96 {
97 pg->GetAstCompiler()->Compile(this);
98 }
99
Compile(compiler::ETSGen * etsg) const100 void CallExpression::Compile(compiler::ETSGen *etsg) const
101 {
102 etsg->GetAstCompiler()->Compile(this);
103 }
104
Check(checker::TSChecker * checker)105 checker::Type *CallExpression::Check(checker::TSChecker *checker)
106 {
107 return checker->GetAnalyzer()->Check(this);
108 }
109
IsETSConstructorCall() const110 bool CallExpression::IsETSConstructorCall() const
111 {
112 return callee_->IsThisExpression() || callee_->IsSuperExpression();
113 }
114
Check(checker::ETSChecker * checker)115 checker::Type *CallExpression::Check(checker::ETSChecker *checker)
116 {
117 return checker->GetAnalyzer()->Check(this);
118 }
119
CallExpression(CallExpression const & other,ArenaAllocator * const allocator)120 CallExpression::CallExpression(CallExpression const &other, ArenaAllocator *const allocator)
121 : MaybeOptionalExpression(static_cast<MaybeOptionalExpression const &>(other)),
122 arguments_(allocator->Adapter()),
123 signature_(other.signature_),
124 trailingComma_(other.trailingComma_),
125 isTrailingBlockInNewLine_(other.isTrailingBlockInNewLine_)
126 {
127 callee_ = other.callee_->Clone(allocator, this)->AsExpression();
128 typeParams_ = other.typeParams_ != nullptr ? other.typeParams_->Clone(allocator, this) : nullptr;
129
130 for (auto *const argument : other.arguments_) {
131 arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression());
132 }
133
134 trailingBlock_ =
135 other.trailingBlock_ != nullptr ? other.trailingBlock_->Clone(allocator, this)->AsBlockStatement() : nullptr;
136 }
137
Clone(ArenaAllocator * const allocator,AstNode * const parent)138 CallExpression *CallExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
139 {
140 if (auto *const clone = allocator->New<CallExpression>(*this, allocator); clone != nullptr) {
141 if (parent != nullptr) {
142 clone->SetParent(parent);
143 }
144
145 clone->SetRange(Range());
146 return clone;
147 }
148
149 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
150 }
151
SetTypeParams(TSTypeParameterInstantiation * typeParams)152 void CallExpression::SetTypeParams(TSTypeParameterInstantiation *typeParams) noexcept
153 {
154 typeParams_ = typeParams;
155 if (typeParams_ != nullptr) {
156 typeParams_->SetParent(this);
157 }
158 }
159
SetTrailingBlock(ir::BlockStatement * const block)160 void CallExpression::SetTrailingBlock(ir::BlockStatement *const block) noexcept
161 {
162 trailingBlock_ = block;
163 if (trailingBlock_ != nullptr) {
164 trailingBlock_->SetParent(this);
165 }
166 }
167 } // namespace ark::es2panda::ir
168