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