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