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 #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 panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)25 void CallExpression::TransformChildren(const NodeTransformer &cb)
26 {
27 callee_ = cb(callee_)->AsExpression();
28
29 if (typeParams_ != nullptr) {
30 typeParams_ = cb(typeParams_)->AsTSTypeParameterInstantiation();
31 }
32
33 for (auto *&it : arguments_) {
34 it = cb(it)->AsExpression();
35 }
36 }
37
Iterate(const NodeTraverser & cb) const38 void CallExpression::Iterate(const NodeTraverser &cb) const
39 {
40 cb(callee_);
41
42 if (typeParams_ != nullptr) {
43 cb(typeParams_);
44 }
45
46 for (auto *it : arguments_) {
47 cb(it);
48 }
49
50 if (trailingBlock_ != nullptr) {
51 cb(trailingBlock_);
52 }
53 }
54
Dump(ir::AstDumper * dumper) const55 void CallExpression::Dump(ir::AstDumper *dumper) const
56 {
57 dumper->Add({{"type", "CallExpression"},
58 {"callee", callee_},
59 {"arguments", arguments_},
60 {"optional", IsOptional()},
61 {"typeParameters", AstDumper::Optional(typeParams_)}});
62 }
63
Dump(ir::SrcDumper * dumper) const64 void CallExpression::Dump(ir::SrcDumper *dumper) const
65 {
66 ASSERT(callee_);
67 callee_->Dump(dumper);
68 dumper->Add("(");
69 for (auto arg : arguments_) {
70 arg->Dump(dumper);
71 if (arg != arguments_.back()) {
72 dumper->Add(", ");
73 }
74 }
75 dumper->Add(")");
76 }
77
Compile(compiler::PandaGen * pg) const78 void CallExpression::Compile(compiler::PandaGen *pg) const
79 {
80 pg->GetAstCompiler()->Compile(this);
81 }
82
Compile(compiler::ETSGen * etsg) const83 void CallExpression::Compile(compiler::ETSGen *etsg) const
84 {
85 etsg->GetAstCompiler()->Compile(this);
86 }
87
Check(checker::TSChecker * checker)88 checker::Type *CallExpression::Check(checker::TSChecker *checker)
89 {
90 return checker->GetAnalyzer()->Check(this);
91 }
92
IsETSConstructorCall() const93 bool CallExpression::IsETSConstructorCall() const
94 {
95 return callee_->IsThisExpression() || callee_->IsSuperExpression();
96 }
97
Check(checker::ETSChecker * checker)98 checker::Type *CallExpression::Check(checker::ETSChecker *checker)
99 {
100 return checker->GetAnalyzer()->Check(this);
101 }
102
CallExpression(CallExpression const & other,ArenaAllocator * const allocator)103 CallExpression::CallExpression(CallExpression const &other, ArenaAllocator *const allocator)
104 : MaybeOptionalExpression(static_cast<MaybeOptionalExpression const &>(other)),
105 arguments_(allocator->Adapter()),
106 signature_(other.signature_),
107 trailingComma_(other.trailingComma_),
108 isTrailingBlockInNewLine_(other.isTrailingBlockInNewLine_)
109 {
110 callee_ = other.callee_->Clone(allocator, this)->AsExpression();
111 typeParams_ = other.typeParams_->Clone(allocator, this);
112
113 for (auto *const argument : other.arguments_) {
114 arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression());
115 }
116
117 if (other.trailingBlock_ != nullptr) {
118 trailingBlock_ = other.trailingBlock_->Clone(allocator, this)->AsBlockStatement();
119 }
120 }
121
122 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)123 CallExpression *CallExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
124 {
125 if (auto *const clone = allocator->New<CallExpression>(*this, allocator); clone != nullptr) {
126 if (parent != nullptr) {
127 clone->SetParent(parent);
128 }
129 return clone;
130 }
131
132 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
133 }
134 } // namespace panda::es2panda::ir
135