13af6ab5fSopenharmony_ci/**
23af6ab5fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "callExpression.h"
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci#include <util/helpers.h>
193af6ab5fSopenharmony_ci#include <compiler/core/pandagen.h>
203af6ab5fSopenharmony_ci#include <compiler/core/regScope.h>
213af6ab5fSopenharmony_ci#include <typescript/checker.h>
223af6ab5fSopenharmony_ci#include <typescript/types/objectType.h>
233af6ab5fSopenharmony_ci#include <typescript/types/signature.h>
243af6ab5fSopenharmony_ci#include <typescript/types/type.h>
253af6ab5fSopenharmony_ci#include <ir/astDump.h>
263af6ab5fSopenharmony_ci#include <ir/base/classDefinition.h>
273af6ab5fSopenharmony_ci#include <ir/base/scriptFunction.h>
283af6ab5fSopenharmony_ci#include <ir/base/spreadElement.h>
293af6ab5fSopenharmony_ci#include <ir/expressions/chainExpression.h>
303af6ab5fSopenharmony_ci#include <ir/expressions/memberExpression.h>
313af6ab5fSopenharmony_ci#include <ir/ts/tsAsExpression.h>
323af6ab5fSopenharmony_ci#include <ir/ts/tsNonNullExpression.h>
333af6ab5fSopenharmony_ci#include <ir/ts/tsTypeAssertion.h>
343af6ab5fSopenharmony_ci#include <ir/ts/tsTypeParameterInstantiation.h>
353af6ab5fSopenharmony_ci
363af6ab5fSopenharmony_cinamespace panda::es2panda::ir {
373af6ab5fSopenharmony_ci
383af6ab5fSopenharmony_civoid CallExpression::Iterate(const NodeTraverser &cb) const
393af6ab5fSopenharmony_ci{
403af6ab5fSopenharmony_ci    cb(callee_);
413af6ab5fSopenharmony_ci
423af6ab5fSopenharmony_ci    if (typeParams_) {
433af6ab5fSopenharmony_ci        cb(typeParams_);
443af6ab5fSopenharmony_ci    }
453af6ab5fSopenharmony_ci
463af6ab5fSopenharmony_ci    for (auto *it : arguments_) {
473af6ab5fSopenharmony_ci        cb(it);
483af6ab5fSopenharmony_ci    }
493af6ab5fSopenharmony_ci}
503af6ab5fSopenharmony_ci
513af6ab5fSopenharmony_civoid CallExpression::Dump(ir::AstDumper *dumper) const
523af6ab5fSopenharmony_ci{
533af6ab5fSopenharmony_ci    dumper->Add({{"type", "CallExpression"},
543af6ab5fSopenharmony_ci                 {"callee", callee_},
553af6ab5fSopenharmony_ci                 {"arguments", arguments_},
563af6ab5fSopenharmony_ci                 {"optional", optional_},
573af6ab5fSopenharmony_ci                 {"typeParameters", AstDumper::Optional(typeParams_)}});
583af6ab5fSopenharmony_ci}
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_cicompiler::VReg CallExpression::CreateSpreadArguments(compiler::PandaGen *pg) const
613af6ab5fSopenharmony_ci{
623af6ab5fSopenharmony_ci    compiler::VReg argsObj = pg->AllocReg();
633af6ab5fSopenharmony_ci    pg->CreateArray(this, arguments_, argsObj);
643af6ab5fSopenharmony_ci
653af6ab5fSopenharmony_ci    return argsObj;
663af6ab5fSopenharmony_ci}
673af6ab5fSopenharmony_ci
683af6ab5fSopenharmony_civoid CallExpression::CompileSuperCallWithSpreadArgs(compiler::PandaGen *pg) const
693af6ab5fSopenharmony_ci{
703af6ab5fSopenharmony_ci    compiler::RegScope paramScope(pg);
713af6ab5fSopenharmony_ci    const ir::ScriptFunction *constructorFunc = util::Helpers::GetContainingConstructor(this);
723af6ab5fSopenharmony_ci    CHECK_NOT_NULL(constructorFunc);
733af6ab5fSopenharmony_ci    // For super call in default constructor.
743af6ab5fSopenharmony_ci    if (constructorFunc->HasFlag(ir::ScriptFunctionFlags::GENERATED_CONSTRUCTOR)) {
753af6ab5fSopenharmony_ci        // Use callruntime.supercallforwardallargs to optimize super call in default constructor since api13.
763af6ab5fSopenharmony_ci        if (pg->Binder()->Program()->TargetApiVersion() >= util::Helpers::SUPER_CALL_OPT_MIN_SUPPORTED_API_VERSION) {
773af6ab5fSopenharmony_ci            compiler::VReg funcObj = pg->AllocReg();
783af6ab5fSopenharmony_ci            pg->GetFunctionObject(this);
793af6ab5fSopenharmony_ci            pg->StoreAccumulator(this, funcObj);
803af6ab5fSopenharmony_ci            pg->SuperCallForwardAllArgs(this, funcObj);
813af6ab5fSopenharmony_ci        } else {
823af6ab5fSopenharmony_ci            compiler::VReg argsObj = pg->AllocReg();
833af6ab5fSopenharmony_ci            arguments_[0]->AsSpreadElement()->Argument()->Compile(pg);
843af6ab5fSopenharmony_ci            pg->StoreAccumulator(this, argsObj);
853af6ab5fSopenharmony_ci            pg->GetFunctionObject(this);
863af6ab5fSopenharmony_ci            pg->SuperCallSpread(this, argsObj);
873af6ab5fSopenharmony_ci        }
883af6ab5fSopenharmony_ci    } else {
893af6ab5fSopenharmony_ci        compiler::VReg argsObj = CreateSpreadArguments(pg);
903af6ab5fSopenharmony_ci        pg->GetFunctionObject(this);
913af6ab5fSopenharmony_ci        pg->SuperCallSpread(this, argsObj);
923af6ab5fSopenharmony_ci    }
933af6ab5fSopenharmony_ci}
943af6ab5fSopenharmony_ci
953af6ab5fSopenharmony_civoid CallExpression::CompileSuperCall(compiler::PandaGen *pg, bool containsSpread) const
963af6ab5fSopenharmony_ci{
973af6ab5fSopenharmony_ci    if (containsSpread) {
983af6ab5fSopenharmony_ci        CompileSuperCallWithSpreadArgs(pg);
993af6ab5fSopenharmony_ci    } else {
1003af6ab5fSopenharmony_ci        compiler::RegScope paramScope(pg);
1013af6ab5fSopenharmony_ci        compiler::VReg argStart {};
1023af6ab5fSopenharmony_ci
1033af6ab5fSopenharmony_ci        if (arguments_.empty()) {
1043af6ab5fSopenharmony_ci            argStart = pg->AllocReg();
1053af6ab5fSopenharmony_ci            pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
1063af6ab5fSopenharmony_ci            pg->StoreAccumulator(this, argStart);
1073af6ab5fSopenharmony_ci        } else {
1083af6ab5fSopenharmony_ci            argStart = pg->NextReg();
1093af6ab5fSopenharmony_ci        }
1103af6ab5fSopenharmony_ci
1113af6ab5fSopenharmony_ci        for (const auto *it : arguments_) {
1123af6ab5fSopenharmony_ci            compiler::VReg arg = pg->AllocReg();
1133af6ab5fSopenharmony_ci            it->Compile(pg);
1143af6ab5fSopenharmony_ci            pg->StoreAccumulator(it, arg);
1153af6ab5fSopenharmony_ci        }
1163af6ab5fSopenharmony_ci
1173af6ab5fSopenharmony_ci        pg->SuperCall(this, argStart, arguments_.size());
1183af6ab5fSopenharmony_ci    }
1193af6ab5fSopenharmony_ci
1203af6ab5fSopenharmony_ci    compiler::VReg newThis = pg->AllocReg();
1213af6ab5fSopenharmony_ci    pg->StoreAccumulator(this, newThis);
1223af6ab5fSopenharmony_ci
1233af6ab5fSopenharmony_ci    pg->GetThis(this);
1243af6ab5fSopenharmony_ci    pg->ThrowIfSuperNotCorrectCall(this, 1);
1253af6ab5fSopenharmony_ci
1263af6ab5fSopenharmony_ci    pg->LoadAccumulator(this, newThis);
1273af6ab5fSopenharmony_ci    pg->SetThis(this);
1283af6ab5fSopenharmony_ci
1293af6ab5fSopenharmony_ci    const auto *classDef = util::Helpers::GetClassDefiniton(util::Helpers::GetContainingConstructor(this));
1303af6ab5fSopenharmony_ci    if (classDef->NeedInstanceInitializer()) {
1313af6ab5fSopenharmony_ci        auto thisReg = pg->AllocReg();
1323af6ab5fSopenharmony_ci        pg->MoveVreg(this, thisReg, newThis);
1333af6ab5fSopenharmony_ci
1343af6ab5fSopenharmony_ci        auto [level, slot] = pg->Scope()->Find(classDef->InstanceInitializer()->Key());
1353af6ab5fSopenharmony_ci        pg->LoadLexicalVar(this, level, slot);
1363af6ab5fSopenharmony_ci
1373af6ab5fSopenharmony_ci        pg->CallInit(this, thisReg);
1383af6ab5fSopenharmony_ci    }
1393af6ab5fSopenharmony_ci}
1403af6ab5fSopenharmony_ci
1413af6ab5fSopenharmony_civoid CallExpression::Compile(compiler::PandaGen *pg) const
1423af6ab5fSopenharmony_ci{
1433af6ab5fSopenharmony_ci    const ir::Expression *realCallee = callee_;
1443af6ab5fSopenharmony_ci    while (realCallee->IsTSNonNullExpression() || realCallee->IsTSAsExpression() || realCallee->IsTSTypeAssertion()) {
1453af6ab5fSopenharmony_ci        if (realCallee->IsTSNonNullExpression()) {
1463af6ab5fSopenharmony_ci            realCallee = realCallee->AsTSNonNullExpression()->Expr();
1473af6ab5fSopenharmony_ci        } else if (realCallee->IsTSAsExpression()) {
1483af6ab5fSopenharmony_ci            realCallee = realCallee->AsTSAsExpression()->Expr();
1493af6ab5fSopenharmony_ci        } else if (realCallee->IsTSTypeAssertion()) {
1503af6ab5fSopenharmony_ci            realCallee = realCallee->AsTSTypeAssertion()->GetExpression();
1513af6ab5fSopenharmony_ci        }
1523af6ab5fSopenharmony_ci    }
1533af6ab5fSopenharmony_ci
1543af6ab5fSopenharmony_ci    if (realCallee->IsCallExpression() || realCallee->IsNewExpression()) {
1553af6ab5fSopenharmony_ci        if (pg->TryCompileFunctionCallOrNewExpression(realCallee)) {
1563af6ab5fSopenharmony_ci            return;
1573af6ab5fSopenharmony_ci        }
1583af6ab5fSopenharmony_ci    }
1593af6ab5fSopenharmony_ci
1603af6ab5fSopenharmony_ci    compiler::RegScope rs(pg);
1613af6ab5fSopenharmony_ci    bool containsSpread = util::Helpers::ContainSpreadElement(arguments_);
1623af6ab5fSopenharmony_ci
1633af6ab5fSopenharmony_ci    if (callee_->IsSuperExpression()) {
1643af6ab5fSopenharmony_ci        CompileSuperCall(pg, containsSpread);
1653af6ab5fSopenharmony_ci        return;
1663af6ab5fSopenharmony_ci    }
1673af6ab5fSopenharmony_ci
1683af6ab5fSopenharmony_ci    compiler::VReg callee = pg->AllocReg();
1693af6ab5fSopenharmony_ci    bool hasThis = false;
1703af6ab5fSopenharmony_ci    compiler::VReg thisReg {};
1713af6ab5fSopenharmony_ci
1723af6ab5fSopenharmony_ci    if (realCallee->IsMemberExpression()) {
1733af6ab5fSopenharmony_ci        hasThis = true;
1743af6ab5fSopenharmony_ci        thisReg = pg->AllocReg();
1753af6ab5fSopenharmony_ci
1763af6ab5fSopenharmony_ci        compiler::RegScope mrs(pg);
1773af6ab5fSopenharmony_ci        realCallee->AsMemberExpression()->Compile(pg, thisReg);
1783af6ab5fSopenharmony_ci    } else if (realCallee->IsChainExpression()) {
1793af6ab5fSopenharmony_ci        hasThis = realCallee->AsChainExpression()->GetExpression()->IsMemberExpression();
1803af6ab5fSopenharmony_ci        if (hasThis) {
1813af6ab5fSopenharmony_ci            // Guaranteed by implementation in callThis, thisVReg is always the next register of callee.
1823af6ab5fSopenharmony_ci            thisVReg_ = callee + 1;
1833af6ab5fSopenharmony_ci        }
1843af6ab5fSopenharmony_ci        realCallee->AsChainExpression()->Compile(pg);
1853af6ab5fSopenharmony_ci    } else {
1863af6ab5fSopenharmony_ci        realCallee->Compile(pg);
1873af6ab5fSopenharmony_ci    }
1883af6ab5fSopenharmony_ci
1893af6ab5fSopenharmony_ci    pg->StoreAccumulator(this, callee);
1903af6ab5fSopenharmony_ci    pg->GetOptionalChain()->CheckNullish(optional_, callee);
1913af6ab5fSopenharmony_ci
1923af6ab5fSopenharmony_ci    if (containsSpread) {
1933af6ab5fSopenharmony_ci        if (!hasThis) {
1943af6ab5fSopenharmony_ci            thisReg = pg->AllocReg();
1953af6ab5fSopenharmony_ci            pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
1963af6ab5fSopenharmony_ci            pg->StoreAccumulator(this, thisReg);
1973af6ab5fSopenharmony_ci        }
1983af6ab5fSopenharmony_ci
1993af6ab5fSopenharmony_ci        compiler::VReg argsObj = CreateSpreadArguments(pg);
2003af6ab5fSopenharmony_ci        pg->CallSpread(this, callee, thisReg, argsObj);
2013af6ab5fSopenharmony_ci        return;
2023af6ab5fSopenharmony_ci    }
2033af6ab5fSopenharmony_ci
2043af6ab5fSopenharmony_ci    for (const auto *it : arguments_) {
2053af6ab5fSopenharmony_ci        it->Compile(pg);
2063af6ab5fSopenharmony_ci        compiler::VReg arg = pg->AllocReg();
2073af6ab5fSopenharmony_ci        pg->StoreAccumulator(it, arg);
2083af6ab5fSopenharmony_ci    }
2093af6ab5fSopenharmony_ci
2103af6ab5fSopenharmony_ci    if (hasThis) {
2113af6ab5fSopenharmony_ci        pg->CallThis(this, callee, static_cast<int64_t>(arguments_.size() + 1));
2123af6ab5fSopenharmony_ci        return;
2133af6ab5fSopenharmony_ci    }
2143af6ab5fSopenharmony_ci
2153af6ab5fSopenharmony_ci    pg->Call(this, callee, arguments_.size());
2163af6ab5fSopenharmony_ci}
2173af6ab5fSopenharmony_ci
2183af6ab5fSopenharmony_cichecker::Type *CallExpression::Check(checker::Checker *checker) const
2193af6ab5fSopenharmony_ci{
2203af6ab5fSopenharmony_ci    checker::Type *calleeType = callee_->Check(checker);
2213af6ab5fSopenharmony_ci
2223af6ab5fSopenharmony_ci    // TODO(aszilagyi): handle optional chain
2233af6ab5fSopenharmony_ci    if (calleeType->IsObjectType()) {
2243af6ab5fSopenharmony_ci        checker::ObjectType *calleeObj = calleeType->AsObjectType();
2253af6ab5fSopenharmony_ci        return checker->resolveCallOrNewExpression(calleeObj->CallSignatures(), arguments_, Start());
2263af6ab5fSopenharmony_ci    }
2273af6ab5fSopenharmony_ci
2283af6ab5fSopenharmony_ci    checker->ThrowTypeError("This expression is not callable.", Start());
2293af6ab5fSopenharmony_ci    return nullptr;
2303af6ab5fSopenharmony_ci}
2313af6ab5fSopenharmony_ci
2323af6ab5fSopenharmony_civoid CallExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
2333af6ab5fSopenharmony_ci{
2343af6ab5fSopenharmony_ci    callee_ = std::get<ir::AstNode *>(cb(callee_))->AsExpression();
2353af6ab5fSopenharmony_ci
2363af6ab5fSopenharmony_ci    if (typeParams_) {
2373af6ab5fSopenharmony_ci        typeParams_ = std::get<ir::AstNode *>(cb(typeParams_))->AsTSTypeParameterInstantiation();
2383af6ab5fSopenharmony_ci    }
2393af6ab5fSopenharmony_ci
2403af6ab5fSopenharmony_ci    for (auto iter = arguments_.begin(); iter != arguments_.end(); iter++) {
2413af6ab5fSopenharmony_ci        *iter = std::get<ir::AstNode *>(cb(*iter))->AsExpression();
2423af6ab5fSopenharmony_ci    }
2433af6ab5fSopenharmony_ci}
2443af6ab5fSopenharmony_ci
2453af6ab5fSopenharmony_ci}  // namespace panda::es2panda::ir
246