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/base/classDefinition.h>
27 #include <ir/base/scriptFunction.h>
28 #include <ir/base/spreadElement.h>
29 #include <ir/expressions/chainExpression.h>
30 #include <ir/expressions/memberExpression.h>
31 #include <ir/ts/tsAsExpression.h>
32 #include <ir/ts/tsNonNullExpression.h>
33 #include <ir/ts/tsTypeAssertion.h>
34 #include <ir/ts/tsTypeParameterInstantiation.h>
35
36 namespace panda::es2panda::ir {
37
Iterate(const NodeTraverser & cb) const38 void CallExpression::Iterate(const NodeTraverser &cb) const
39 {
40 cb(callee_);
41
42 if (typeParams_) {
43 cb(typeParams_);
44 }
45
46 for (auto *it : arguments_) {
47 cb(it);
48 }
49 }
50
Dump(ir::AstDumper * dumper) const51 void CallExpression::Dump(ir::AstDumper *dumper) const
52 {
53 dumper->Add({{"type", "CallExpression"},
54 {"callee", callee_},
55 {"arguments", arguments_},
56 {"optional", optional_},
57 {"typeParameters", AstDumper::Optional(typeParams_)}});
58 }
59
CreateSpreadArguments(compiler::PandaGen * pg) const60 compiler::VReg CallExpression::CreateSpreadArguments(compiler::PandaGen *pg) const
61 {
62 compiler::VReg argsObj = pg->AllocReg();
63 pg->CreateArray(this, arguments_, argsObj);
64
65 return argsObj;
66 }
67
CompileSuperCall(compiler::PandaGen * pg,bool containsSpread) const68 void CallExpression::CompileSuperCall(compiler::PandaGen *pg, bool containsSpread) const
69 {
70 if (containsSpread) {
71 compiler::RegScope paramScope(pg);
72 compiler::VReg argsObj {};
73 // arguments_ is only ...args
74 const ir::ScriptFunction *constructorFunc = util::Helpers::GetContainingConstructor(this);
75 CHECK_NOT_NULL(constructorFunc);
76 if (constructorFunc->HasFlag(ir::ScriptFunctionFlags::GENERATED_CONSTRUCTOR)) {
77 argsObj = pg->AllocReg();
78 arguments_[0]->AsSpreadElement()->Argument()->Compile(pg);
79 pg->StoreAccumulator(this, argsObj);
80 } else {
81 argsObj = CreateSpreadArguments(pg);
82 }
83
84 pg->GetFunctionObject(this);
85 pg->SuperCallSpread(this, argsObj);
86 } else {
87 compiler::RegScope paramScope(pg);
88 compiler::VReg argStart {};
89
90 if (arguments_.empty()) {
91 argStart = pg->AllocReg();
92 pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
93 pg->StoreAccumulator(this, argStart);
94 } else {
95 argStart = pg->NextReg();
96 }
97
98 for (const auto *it : arguments_) {
99 compiler::VReg arg = pg->AllocReg();
100 it->Compile(pg);
101 pg->StoreAccumulator(it, arg);
102 }
103
104 pg->SuperCall(this, argStart, arguments_.size());
105 }
106
107 compiler::VReg newThis = pg->AllocReg();
108 pg->StoreAccumulator(this, newThis);
109
110 pg->GetThis(this);
111 pg->ThrowIfSuperNotCorrectCall(this, 1);
112
113 pg->LoadAccumulator(this, newThis);
114 pg->SetThis(this);
115
116 const auto *classDef = util::Helpers::GetClassDefiniton(util::Helpers::GetContainingConstructor(this));
117 if (classDef->NeedInstanceInitializer()) {
118 auto thisReg = pg->AllocReg();
119 pg->MoveVreg(this, thisReg, newThis);
120
121 auto [level, slot] = pg->Scope()->Find(classDef->InstanceInitializer()->Key());
122 pg->LoadLexicalVar(this, level, slot);
123
124 pg->CallInit(this, thisReg);
125 }
126 }
127
Compile(compiler::PandaGen * pg) const128 void CallExpression::Compile(compiler::PandaGen *pg) const
129 {
130 const ir::Expression *realCallee = callee_;
131 while (realCallee->IsTSNonNullExpression() || realCallee->IsTSAsExpression() || realCallee->IsTSTypeAssertion()) {
132 if (realCallee->IsTSNonNullExpression()) {
133 realCallee = realCallee->AsTSNonNullExpression()->Expr();
134 } else if (realCallee->IsTSAsExpression()) {
135 realCallee = realCallee->AsTSAsExpression()->Expr();
136 } else if (realCallee->IsTSTypeAssertion()) {
137 realCallee = realCallee->AsTSTypeAssertion()->GetExpression();
138 }
139 }
140
141 if (realCallee->IsCallExpression() || realCallee->IsNewExpression()) {
142 if (pg->TryCompileFunctionCallOrNewExpression(realCallee)) {
143 return;
144 }
145 }
146
147 compiler::RegScope rs(pg);
148 bool containsSpread = util::Helpers::ContainSpreadElement(arguments_);
149
150 if (callee_->IsSuperExpression()) {
151 CompileSuperCall(pg, containsSpread);
152 return;
153 }
154
155 compiler::VReg callee = pg->AllocReg();
156 bool hasThis = false;
157 compiler::VReg thisReg {};
158
159 if (realCallee->IsMemberExpression()) {
160 hasThis = true;
161 thisReg = pg->AllocReg();
162
163 compiler::RegScope mrs(pg);
164 realCallee->AsMemberExpression()->Compile(pg, thisReg);
165 } else if (realCallee->IsChainExpression()) {
166 hasThis = realCallee->AsChainExpression()->GetExpression()->IsMemberExpression();
167 if (hasThis) {
168 // Guaranteed by implementation in callThis, thisVReg is always the next register of callee.
169 thisVReg_ = callee + 1;
170 }
171 realCallee->AsChainExpression()->Compile(pg);
172 } else {
173 realCallee->Compile(pg);
174 }
175
176 pg->StoreAccumulator(this, callee);
177 pg->GetOptionalChain()->CheckNullish(optional_, callee);
178
179 if (containsSpread) {
180 if (!hasThis) {
181 thisReg = pg->AllocReg();
182 pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
183 pg->StoreAccumulator(this, thisReg);
184 }
185
186 compiler::VReg argsObj = CreateSpreadArguments(pg);
187 pg->CallSpread(this, callee, thisReg, argsObj);
188 return;
189 }
190
191 for (const auto *it : arguments_) {
192 it->Compile(pg);
193 compiler::VReg arg = pg->AllocReg();
194 pg->StoreAccumulator(it, arg);
195 }
196
197 if (hasThis) {
198 pg->CallThis(this, callee, static_cast<int64_t>(arguments_.size() + 1));
199 return;
200 }
201
202 pg->Call(this, callee, arguments_.size());
203 }
204
Check(checker::Checker * checker) const205 checker::Type *CallExpression::Check(checker::Checker *checker) const
206 {
207 checker::Type *calleeType = callee_->Check(checker);
208
209 // TODO(aszilagyi): handle optional chain
210 if (calleeType->IsObjectType()) {
211 checker::ObjectType *calleeObj = calleeType->AsObjectType();
212 return checker->resolveCallOrNewExpression(calleeObj->CallSignatures(), arguments_, Start());
213 }
214
215 checker->ThrowTypeError("This expression is not callable.", Start());
216 return nullptr;
217 }
218
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)219 void CallExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
220 {
221 callee_ = std::get<ir::AstNode *>(cb(callee_))->AsExpression();
222
223 if (typeParams_) {
224 typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterInstantiation();
225 }
226
227 for (auto iter = arguments_.begin(); iter != arguments_.end(); iter++) {
228 *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
229 }
230 }
231
232 } // namespace panda::es2panda::ir
233