• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 
22 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)23 void CallExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
24 {
25     if (auto *transformedNode = cb(callee_); callee_ != transformedNode) {
26         callee_->SetTransformedNode(transformationName, transformedNode);
27         callee_ = transformedNode->AsExpression();
28     }
29 
30     if (typeParams_ != nullptr) {
31         if (auto *transformedNode = cb(typeParams_); typeParams_ != transformedNode) {
32             typeParams_->SetTransformedNode(transformationName, transformedNode);
33             typeParams_ = transformedNode->AsTSTypeParameterInstantiation();
34         }
35     }
36 
37     for (auto *&it : VectorIterationGuard(arguments_)) {
38         if (auto *transformedNode = cb(it); it != transformedNode) {
39             it->SetTransformedNode(transformationName, transformedNode);
40             it = transformedNode->AsExpression();
41         }
42     }
43 
44     if (trailingLambdaInfo_.block != nullptr) {
45         if (auto *transformedNode = cb(trailingLambdaInfo_.block); trailingLambdaInfo_.block != transformedNode) {
46             trailingLambdaInfo_.block->SetTransformedNode(transformationName, transformedNode);
47             trailingLambdaInfo_.block = transformedNode->AsBlockStatement();
48         }
49     }
50 }
51 
Iterate(const NodeTraverser & cb) const52 void CallExpression::Iterate(const NodeTraverser &cb) const
53 {
54     cb(callee_);
55 
56     if (typeParams_ != nullptr) {
57         cb(typeParams_);
58     }
59 
60     for (auto *it : VectorIterationGuard(arguments_)) {
61         cb(it);
62     }
63 
64     if (trailingLambdaInfo_.block != nullptr) {
65         cb(trailingLambdaInfo_.block);
66     }
67 }
68 
Dump(ir::AstDumper * dumper) const69 void CallExpression::Dump(ir::AstDumper *dumper) const
70 {
71     dumper->Add({{"type", "CallExpression"},
72                  {"callee", callee_},
73                  {"arguments", arguments_},
74                  {"optional", IsOptional()},
75                  {"typeParameters", AstDumper::Optional(typeParams_)}});
76     // #22953: trailing block is not handled
77 }
78 
Dump(ir::SrcDumper * dumper) const79 void CallExpression::Dump(ir::SrcDumper *dumper) const
80 {
81     ES2PANDA_ASSERT(callee_);
82     callee_->Dump(dumper);
83     if (IsOptional()) {
84         dumper->Add("?.");
85     }
86 
87     if (typeParams_ != nullptr) {
88         typeParams_->Dump(dumper);
89     }
90 
91     dumper->Add("(");
92     for (auto arg : arguments_) {
93         arg->Dump(dumper);
94         if (arg != arguments_.back()) {
95             dumper->Add(", ");
96         }
97     }
98     dumper->Add(")");
99     if (trailingLambdaInfo_.block != nullptr) {
100         if (trailingLambdaInfo_.isBlockInNewLine) {
101             dumper->Endl();
102         }
103         trailingLambdaInfo_.block->Dump(dumper);
104     }
105 }
106 
Compile(compiler::PandaGen * pg) const107 void CallExpression::Compile(compiler::PandaGen *pg) const
108 {
109     pg->GetAstCompiler()->Compile(this);
110 }
111 
Compile(compiler::ETSGen * etsg) const112 void CallExpression::Compile(compiler::ETSGen *etsg) const
113 {
114     etsg->GetAstCompiler()->Compile(this);
115 }
116 
Check(checker::TSChecker * checker)117 checker::Type *CallExpression::Check(checker::TSChecker *checker)
118 {
119     return checker->GetAnalyzer()->Check(this);
120 }
121 
Check(checker::ETSChecker * checker)122 checker::VerifiedType CallExpression::Check(checker::ETSChecker *checker)
123 {
124     return {this, checker->GetAnalyzer()->Check(this)};
125 }
126 
CallExpression(CallExpression const & other,ArenaAllocator * const allocator)127 CallExpression::CallExpression(CallExpression const &other, ArenaAllocator *const allocator)
128     : MaybeOptionalExpression(static_cast<MaybeOptionalExpression const &>(other)),
129       arguments_(allocator->Adapter()),
130       signature_(other.signature_),
131       trailingComma_(other.trailingComma_),
132       trailingLambdaInfo_({other.trailingLambdaInfo_.block, other.trailingLambdaInfo_.isBlockInNewLine,
133                            other.trailingLambdaInfo_.isTrailingCall})
134 {
135     callee_ = other.callee_->Clone(allocator, this)->AsExpression();
136     typeParams_ = other.typeParams_ != nullptr ? other.typeParams_->Clone(allocator, this) : nullptr;
137 
138     for (auto *const argument : other.arguments_) {
139         arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression());
140     }
141 
142     trailingLambdaInfo_.block = other.trailingLambdaInfo_.block != nullptr
143                                     ? other.trailingLambdaInfo_.block->Clone(allocator, this)->AsBlockStatement()
144                                     : nullptr;
145 }
146 
Clone(ArenaAllocator * const allocator,AstNode * const parent)147 CallExpression *CallExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
148 {
149     auto *const clone = allocator->New<CallExpression>(*this, allocator);
150     ES2PANDA_ASSERT(clone != nullptr);
151     if (parent != nullptr) {
152         clone->SetParent(parent);
153     }
154 
155     clone->SetRange(Range());
156     return clone;
157 }
158 
SetTypeParams(TSTypeParameterInstantiation * typeParams)159 void CallExpression::SetTypeParams(TSTypeParameterInstantiation *typeParams) noexcept
160 {
161     typeParams_ = typeParams;
162     if (typeParams_ != nullptr) {
163         typeParams_->SetParent(this);
164     }
165 }
166 
SetTrailingBlock(ir::BlockStatement * const block)167 void CallExpression::SetTrailingBlock(ir::BlockStatement *const block) noexcept
168 {
169     trailingLambdaInfo_.block = block;
170     if (trailingLambdaInfo_.block != nullptr) {
171         trailingLambdaInfo_.block->SetParent(this);
172     }
173 }
174 
IsExtensionAccessorCall()175 bool CallExpression::IsExtensionAccessorCall()
176 {
177     return (Signature() != nullptr) && (Signature()->Function()->IsExtensionAccessor());
178 }
179 
180 }  // namespace ark::es2panda::ir
181