1 /**
2 * Copyright (c) 2021 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 <util/helpers.h>
19 #include <compiler/core/pandagen.h>
20 #include <compiler/core/regScope.h>
21 #include <typescript/checker.h>
22 #include <typescript/types/objectType.h>
23 #include <typescript/types/signature.h>
24 #include <typescript/types/type.h>
25 #include <ir/astDump.h>
26 #include <ir/expressions/chainExpression.h>
27 #include <ir/expressions/memberExpression.h>
28 #include <ir/ts/tsAsExpression.h>
29 #include <ir/ts/tsNonNullExpression.h>
30 #include <ir/ts/tsTypeAssertion.h>
31 #include <ir/ts/tsTypeParameterInstantiation.h>
32
33 namespace panda::es2panda::ir {
34
Iterate(const NodeTraverser & cb) const35 void CallExpression::Iterate(const NodeTraverser &cb) const
36 {
37 cb(callee_);
38
39 if (typeParams_) {
40 cb(typeParams_);
41 }
42
43 for (auto *it : arguments_) {
44 cb(it);
45 }
46 }
47
Dump(ir::AstDumper * dumper) const48 void CallExpression::Dump(ir::AstDumper *dumper) const
49 {
50 dumper->Add({{"type", "CallExpression"},
51 {"callee", callee_},
52 {"arguments", arguments_},
53 {"optional", optional_},
54 {"typeParameters", AstDumper::Optional(typeParams_)}});
55 }
56
CreateSpreadArguments(compiler::PandaGen * pg) const57 compiler::VReg CallExpression::CreateSpreadArguments(compiler::PandaGen *pg) const
58 {
59 compiler::VReg argsObj = pg->AllocReg();
60 pg->CreateArray(this, arguments_, argsObj);
61
62 return argsObj;
63 }
64
Compile(compiler::PandaGen * pg) const65 void CallExpression::Compile(compiler::PandaGen *pg) const
66 {
67 ir::Expression *realCallee = callee_;
68 while (realCallee->IsTSNonNullExpression() || realCallee->IsTSAsExpression() || realCallee->IsTSTypeAssertion()) {
69 if (realCallee->IsTSNonNullExpression()) {
70 realCallee = realCallee->AsTSNonNullExpression()->Expr();
71 } else if (realCallee->IsTSAsExpression()) {
72 realCallee = realCallee->AsTSAsExpression()->Expr();
73 } else if (realCallee->IsTSTypeAssertion()) {
74 realCallee = realCallee->AsTSTypeAssertion()->GetExpression();
75 }
76 }
77
78 if (realCallee->IsCallExpression() || realCallee->IsNewExpression()) {
79 if (pg->TryCompileFunctionCallOrNewExpression(realCallee)) {
80 return;
81 }
82 }
83
84 compiler::RegScope rs(pg);
85 bool containsSpread = util::Helpers::ContainSpreadElement(arguments_);
86
87 if (callee_->IsSuperExpression()) {
88 if (containsSpread) {
89 compiler::RegScope paramScope(pg);
90 compiler::VReg argsObj = CreateSpreadArguments(pg);
91
92 pg->GetFunctionObject(this);
93 pg->SuperCallSpread(this, argsObj);
94 } else {
95 compiler::RegScope paramScope(pg);
96 compiler::VReg argStart {};
97
98 if (arguments_.empty()) {
99 argStart = pg->AllocReg();
100 pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
101 pg->StoreAccumulator(this, argStart);
102 } else {
103 argStart = pg->NextReg();
104 }
105
106 for (const auto *it : arguments_) {
107 compiler::VReg arg = pg->AllocReg();
108 it->Compile(pg);
109 pg->StoreAccumulator(it, arg);
110 }
111
112 pg->SuperCall(this, argStart, arguments_.size());
113 }
114
115 compiler::VReg newThis = pg->AllocReg();
116 pg->StoreAccumulator(this, newThis);
117
118 pg->GetThis(this);
119 pg->ThrowIfSuperNotCorrectCall(this, 1);
120
121 pg->LoadAccumulator(this, newThis);
122 pg->SetThis(this);
123 return;
124 }
125
126 compiler::VReg callee = pg->AllocReg();
127 bool hasThis = false;
128 compiler::VReg thisReg {};
129
130 if (realCallee->IsMemberExpression()) {
131 hasThis = true;
132 thisReg = pg->AllocReg();
133
134 compiler::RegScope mrs(pg);
135 realCallee->AsMemberExpression()->Compile(pg, thisReg);
136 } else if (realCallee->IsChainExpression()) {
137 hasThis = true;
138 realCallee->AsChainExpression()->Compile(pg);
139 } else {
140 realCallee->Compile(pg);
141 }
142
143 pg->StoreAccumulator(this, callee);
144 pg->GetOptionalChain()->CheckNullish(optional_, callee);
145
146 if (containsSpread) {
147 if (!hasThis) {
148 thisReg = pg->AllocReg();
149 pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
150 pg->StoreAccumulator(this, thisReg);
151 }
152
153 compiler::VReg argsObj = CreateSpreadArguments(pg);
154 pg->CallSpread(this, callee, thisReg, argsObj);
155 return;
156 }
157
158 for (const auto *it : arguments_) {
159 it->Compile(pg);
160 compiler::VReg arg = pg->AllocReg();
161 pg->StoreAccumulator(it, arg);
162 }
163
164 if (hasThis) {
165 pg->CallThis(this, callee, static_cast<int64_t>(arguments_.size() + 1));
166 return;
167 }
168
169 pg->Call(this, callee, arguments_.size());
170 }
171
Check(checker::Checker * checker) const172 checker::Type *CallExpression::Check(checker::Checker *checker) const
173 {
174 checker::Type *calleeType = callee_->Check(checker);
175
176 // TODO(aszilagyi): handle optional chain
177 if (calleeType->IsObjectType()) {
178 checker::ObjectType *calleeObj = calleeType->AsObjectType();
179 return checker->resolveCallOrNewExpression(calleeObj->CallSignatures(), arguments_, Start());
180 }
181
182 checker->ThrowTypeError("This expression is not callable.", Start());
183 return nullptr;
184 }
185
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)186 void CallExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
187 {
188 callee_ = std::get<ir::AstNode *>(cb(callee_))->AsExpression();
189
190 if (typeParams_) {
191 typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterInstantiation();
192 }
193
194 for (auto iter = arguments_.begin(); iter != arguments_.end(); iter++) {
195 *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
196 }
197 }
198
199 } // namespace panda::es2panda::ir
200