13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2021-2024 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#ifndef ES2PANDA_COMPILER_CHECKER_ETS_FUNCTION_HELPERS_H
173af6ab5fSopenharmony_ci#define ES2PANDA_COMPILER_CHECKER_ETS_FUNCTION_HELPERS_H
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ci#include "checker/ETSchecker.h"
203af6ab5fSopenharmony_ci#include "checker/ets/typeRelationContext.h"
213af6ab5fSopenharmony_ci#include "checker/types/ets/etsObjectType.h"
223af6ab5fSopenharmony_ci#include "checker/types/type.h"
233af6ab5fSopenharmony_ci#include "checker/types/typeFlag.h"
243af6ab5fSopenharmony_ci#include "ir/astNode.h"
253af6ab5fSopenharmony_ci#include "ir/typeNode.h"
263af6ab5fSopenharmony_ci#include "ir/base/catchClause.h"
273af6ab5fSopenharmony_ci#include "ir/base/classDefinition.h"
283af6ab5fSopenharmony_ci#include "ir/base/classProperty.h"
293af6ab5fSopenharmony_ci#include "ir/base/methodDefinition.h"
303af6ab5fSopenharmony_ci#include "ir/base/scriptFunction.h"
313af6ab5fSopenharmony_ci#include "ir/base/spreadElement.h"
323af6ab5fSopenharmony_ci#include "ir/expressions/arrowFunctionExpression.h"
333af6ab5fSopenharmony_ci#include "ir/expressions/callExpression.h"
343af6ab5fSopenharmony_ci#include "ir/expressions/functionExpression.h"
353af6ab5fSopenharmony_ci#include "ir/expressions/memberExpression.h"
363af6ab5fSopenharmony_ci#include "ir/statements/blockStatement.h"
373af6ab5fSopenharmony_ci#include "ir/statements/doWhileStatement.h"
383af6ab5fSopenharmony_ci#include "ir/statements/expressionStatement.h"
393af6ab5fSopenharmony_ci#include "ir/statements/forInStatement.h"
403af6ab5fSopenharmony_ci#include "ir/statements/forOfStatement.h"
413af6ab5fSopenharmony_ci#include "ir/statements/forUpdateStatement.h"
423af6ab5fSopenharmony_ci#include "ir/statements/switchStatement.h"
433af6ab5fSopenharmony_ci#include "ir/statements/whileStatement.h"
443af6ab5fSopenharmony_ci#include "ir/ts/tsTypeParameterInstantiation.h"
453af6ab5fSopenharmony_ci#include "parser/program/program.h"
463af6ab5fSopenharmony_ci#include "utils/arena_containers.h"
473af6ab5fSopenharmony_ci#include "util/helpers.h"
483af6ab5fSopenharmony_ci#include "util/language.h"
493af6ab5fSopenharmony_ci#include "varbinder/declaration.h"
503af6ab5fSopenharmony_ci#include "varbinder/ETSBinder.h"
513af6ab5fSopenharmony_ci#include "varbinder/scope.h"
523af6ab5fSopenharmony_ci#include "varbinder/varbinder.h"
533af6ab5fSopenharmony_ci#include "varbinder/variable.h"
543af6ab5fSopenharmony_ci#include "varbinder/variableFlags.h"
553af6ab5fSopenharmony_ci
563af6ab5fSopenharmony_cinamespace ark::es2panda::checker {
573af6ab5fSopenharmony_ci
583af6ab5fSopenharmony_cistatic Type *MaybeBoxedType(ETSChecker *checker, Type *type, ir::Expression *expr)
593af6ab5fSopenharmony_ci{
603af6ab5fSopenharmony_ci    if (!type->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) {
613af6ab5fSopenharmony_ci        return type;
623af6ab5fSopenharmony_ci    }
633af6ab5fSopenharmony_ci    auto *relation = checker->Relation();
643af6ab5fSopenharmony_ci    auto *oldNode = relation->GetNode();
653af6ab5fSopenharmony_ci    relation->SetNode(expr);
663af6ab5fSopenharmony_ci    auto *res = checker->PrimitiveTypeAsETSBuiltinType(type);
673af6ab5fSopenharmony_ci    relation->SetNode(oldNode);
683af6ab5fSopenharmony_ci    return res;
693af6ab5fSopenharmony_ci}
703af6ab5fSopenharmony_ci
713af6ab5fSopenharmony_cistatic void InferUntilFail(Signature const *const signature, const ArenaVector<ir::Expression *> &arguments,
723af6ab5fSopenharmony_ci                           ETSChecker *checker, Substitution *substitution)
733af6ab5fSopenharmony_ci{
743af6ab5fSopenharmony_ci    auto *sigInfo = signature->GetSignatureInfo();
753af6ab5fSopenharmony_ci    auto &sigParams = signature->GetSignatureInfo()->typeParams;
763af6ab5fSopenharmony_ci    ArenaVector<bool> inferStatus(checker->Allocator()->Adapter());
773af6ab5fSopenharmony_ci    inferStatus.assign(arguments.size(), false);
783af6ab5fSopenharmony_ci    bool anyChange = true;
793af6ab5fSopenharmony_ci    size_t lastSubsititutionSize = 0;
803af6ab5fSopenharmony_ci
813af6ab5fSopenharmony_ci    // some ets lib files require type infer from arg index 0,1,... , not fit to build graph
823af6ab5fSopenharmony_ci    while (anyChange && substitution->size() < sigParams.size()) {
833af6ab5fSopenharmony_ci        anyChange = false;
843af6ab5fSopenharmony_ci        for (size_t ix = 0; ix < arguments.size(); ++ix) {
853af6ab5fSopenharmony_ci            if (inferStatus[ix]) {
863af6ab5fSopenharmony_ci                continue;
873af6ab5fSopenharmony_ci            }
883af6ab5fSopenharmony_ci
893af6ab5fSopenharmony_ci            auto *arg = arguments[ix];
903af6ab5fSopenharmony_ci            if (arg->IsObjectExpression()) {
913af6ab5fSopenharmony_ci                continue;
923af6ab5fSopenharmony_ci            }
933af6ab5fSopenharmony_ci
943af6ab5fSopenharmony_ci            auto *const argType = arg->IsSpreadElement()
953af6ab5fSopenharmony_ci                                      ? MaybeBoxedType(checker, arg->AsSpreadElement()->Argument()->Check(checker),
963af6ab5fSopenharmony_ci                                                       arg->AsSpreadElement()->Argument())
973af6ab5fSopenharmony_ci                                      : MaybeBoxedType(checker, arg->Check(checker), arg);
983af6ab5fSopenharmony_ci            auto *const paramType = (ix < signature->MinArgCount()) ? sigInfo->params[ix]->TsType()
993af6ab5fSopenharmony_ci                                    : sigInfo->restVar != nullptr   ? sigInfo->restVar->TsType()
1003af6ab5fSopenharmony_ci                                                                    : nullptr;
1013af6ab5fSopenharmony_ci
1023af6ab5fSopenharmony_ci            if (paramType == nullptr) {
1033af6ab5fSopenharmony_ci                continue;
1043af6ab5fSopenharmony_ci            }
1053af6ab5fSopenharmony_ci
1063af6ab5fSopenharmony_ci            if (checker->EnhanceSubstitutionForType(sigInfo->typeParams, paramType, argType, substitution)) {
1073af6ab5fSopenharmony_ci                inferStatus[ix] = true;
1083af6ab5fSopenharmony_ci            }
1093af6ab5fSopenharmony_ci            if (lastSubsititutionSize != substitution->size()) {
1103af6ab5fSopenharmony_ci                lastSubsititutionSize = substitution->size();
1113af6ab5fSopenharmony_ci                anyChange = true;
1123af6ab5fSopenharmony_ci            }
1133af6ab5fSopenharmony_ci        }
1143af6ab5fSopenharmony_ci    }
1153af6ab5fSopenharmony_ci}
1163af6ab5fSopenharmony_ci
1173af6ab5fSopenharmony_cistatic const Substitution *BuildImplicitSubstitutionForArguments(ETSChecker *checker, Signature *signature,
1183af6ab5fSopenharmony_ci                                                                 const ArenaVector<ir::Expression *> &arguments)
1193af6ab5fSopenharmony_ci{
1203af6ab5fSopenharmony_ci    Substitution *substitution = checker->NewSubstitution();
1213af6ab5fSopenharmony_ci    auto *sigInfo = signature->GetSignatureInfo();
1223af6ab5fSopenharmony_ci    auto &sigParams = signature->GetSignatureInfo()->typeParams;
1233af6ab5fSopenharmony_ci
1243af6ab5fSopenharmony_ci    InferUntilFail(signature, arguments, checker, substitution);
1253af6ab5fSopenharmony_ci
1263af6ab5fSopenharmony_ci    if (substitution->size() != sigParams.size()) {
1273af6ab5fSopenharmony_ci        for (const auto typeParam : sigParams) {
1283af6ab5fSopenharmony_ci            auto newTypeParam = typeParam->AsETSTypeParameter();
1293af6ab5fSopenharmony_ci            if (auto it = substitution->find(newTypeParam); it != substitution->cend()) {
1303af6ab5fSopenharmony_ci                continue;
1313af6ab5fSopenharmony_ci            }
1323af6ab5fSopenharmony_ci            if (newTypeParam->GetDefaultType() == nullptr) {
1333af6ab5fSopenharmony_ci                return nullptr;
1343af6ab5fSopenharmony_ci            }
1353af6ab5fSopenharmony_ci            auto dflt = newTypeParam->GetDefaultType()->Substitute(checker->Relation(), substitution);
1363af6ab5fSopenharmony_ci            if (!checker->EnhanceSubstitutionForType(sigInfo->typeParams, newTypeParam, dflt, substitution)) {
1373af6ab5fSopenharmony_ci                return nullptr;
1383af6ab5fSopenharmony_ci            }
1393af6ab5fSopenharmony_ci        }
1403af6ab5fSopenharmony_ci
1413af6ab5fSopenharmony_ci        if (substitution->size() != sigParams.size() &&
1423af6ab5fSopenharmony_ci            (signature->Function()->ReturnTypeAnnotation() == nullptr ||
1433af6ab5fSopenharmony_ci             !checker->EnhanceSubstitutionForType(sigInfo->typeParams,
1443af6ab5fSopenharmony_ci                                                  signature->Function()->ReturnTypeAnnotation()->TsType(),
1453af6ab5fSopenharmony_ci                                                  signature->ReturnType(), substitution))) {
1463af6ab5fSopenharmony_ci            return nullptr;
1473af6ab5fSopenharmony_ci        }
1483af6ab5fSopenharmony_ci    }
1493af6ab5fSopenharmony_ci
1503af6ab5fSopenharmony_ci    return substitution;
1513af6ab5fSopenharmony_ci}
1523af6ab5fSopenharmony_ci
1533af6ab5fSopenharmony_cistatic const Substitution *BuildExplicitSubstitutionForArguments(ETSChecker *checker, Signature *signature,
1543af6ab5fSopenharmony_ci                                                                 const ArenaVector<ir::TypeNode *> &params,
1553af6ab5fSopenharmony_ci                                                                 const lexer::SourcePosition &pos,
1563af6ab5fSopenharmony_ci                                                                 TypeRelationFlag flags)
1573af6ab5fSopenharmony_ci{
1583af6ab5fSopenharmony_ci    auto &sigParams = signature->GetSignatureInfo()->typeParams;
1593af6ab5fSopenharmony_ci    auto *substitution = checker->NewSubstitution();
1603af6ab5fSopenharmony_ci    auto *constraintsSubstitution = checker->NewSubstitution();
1613af6ab5fSopenharmony_ci    ArenaVector<Type *> instArgs {checker->Allocator()->Adapter()};
1623af6ab5fSopenharmony_ci
1633af6ab5fSopenharmony_ci    for (size_t ix = 0; ix < params.size(); ++ix) {
1643af6ab5fSopenharmony_ci        instArgs.push_back(MaybeBoxedType(checker, params[ix]->GetType(checker), params[ix]));
1653af6ab5fSopenharmony_ci        if (ix < sigParams.size()) {
1663af6ab5fSopenharmony_ci            ETSChecker::EmplaceSubstituted(constraintsSubstitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
1673af6ab5fSopenharmony_ci        }
1683af6ab5fSopenharmony_ci    }
1693af6ab5fSopenharmony_ci    for (size_t ix = instArgs.size(); ix < sigParams.size(); ++ix) {
1703af6ab5fSopenharmony_ci        auto *dflt = sigParams[ix]->AsETSTypeParameter()->GetDefaultType();
1713af6ab5fSopenharmony_ci        if (dflt == nullptr) {
1723af6ab5fSopenharmony_ci            break;
1733af6ab5fSopenharmony_ci        }
1743af6ab5fSopenharmony_ci
1753af6ab5fSopenharmony_ci        dflt = dflt->Substitute(checker->Relation(), constraintsSubstitution);
1763af6ab5fSopenharmony_ci        instArgs.push_back(dflt);
1773af6ab5fSopenharmony_ci        ETSChecker::EmplaceSubstituted(constraintsSubstitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
1783af6ab5fSopenharmony_ci    }
1793af6ab5fSopenharmony_ci    if (sigParams.size() != instArgs.size()) {
1803af6ab5fSopenharmony_ci        if ((flags & TypeRelationFlag::NO_THROW) != 0) {
1813af6ab5fSopenharmony_ci            return nullptr;
1823af6ab5fSopenharmony_ci        }
1833af6ab5fSopenharmony_ci        checker->LogTypeError({"Expected ", sigParams.size(), " type arguments, got ", instArgs.size(), " ."}, pos);
1843af6ab5fSopenharmony_ci        return nullptr;
1853af6ab5fSopenharmony_ci    }
1863af6ab5fSopenharmony_ci
1873af6ab5fSopenharmony_ci    for (size_t ix = 0; ix < sigParams.size(); ix++) {
1883af6ab5fSopenharmony_ci        if (!checker->IsCompatibleTypeArgument(sigParams[ix]->AsETSTypeParameter(), instArgs[ix],
1893af6ab5fSopenharmony_ci                                               constraintsSubstitution)) {
1903af6ab5fSopenharmony_ci            return nullptr;
1913af6ab5fSopenharmony_ci        }
1923af6ab5fSopenharmony_ci        ETSChecker::EmplaceSubstituted(substitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
1933af6ab5fSopenharmony_ci    }
1943af6ab5fSopenharmony_ci    return substitution;
1953af6ab5fSopenharmony_ci}
1963af6ab5fSopenharmony_ci
1973af6ab5fSopenharmony_cistatic Signature *MaybeSubstituteTypeParameters(ETSChecker *checker, Signature *signature,
1983af6ab5fSopenharmony_ci                                                const ir::TSTypeParameterInstantiation *typeArguments,
1993af6ab5fSopenharmony_ci                                                const ArenaVector<ir::Expression *> &arguments,
2003af6ab5fSopenharmony_ci                                                const lexer::SourcePosition &pos, TypeRelationFlag flags)
2013af6ab5fSopenharmony_ci{
2023af6ab5fSopenharmony_ci    if (typeArguments == nullptr && signature->GetSignatureInfo()->typeParams.empty()) {
2033af6ab5fSopenharmony_ci        return signature;
2043af6ab5fSopenharmony_ci    }
2053af6ab5fSopenharmony_ci
2063af6ab5fSopenharmony_ci    const Substitution *substitution =
2073af6ab5fSopenharmony_ci        (typeArguments != nullptr)
2083af6ab5fSopenharmony_ci            ? BuildExplicitSubstitutionForArguments(checker, signature, typeArguments->Params(), pos, flags)
2093af6ab5fSopenharmony_ci            : BuildImplicitSubstitutionForArguments(checker, signature, arguments);
2103af6ab5fSopenharmony_ci
2113af6ab5fSopenharmony_ci    return (substitution == nullptr) ? nullptr : signature->Substitute(checker->Relation(), substitution);
2123af6ab5fSopenharmony_ci}
2133af6ab5fSopenharmony_ci
2143af6ab5fSopenharmony_cistatic bool CheckInterfaceOverride(ETSChecker *const checker, ETSObjectType *const interface,
2153af6ab5fSopenharmony_ci                                   Signature *const signature)
2163af6ab5fSopenharmony_ci{
2173af6ab5fSopenharmony_ci    bool isOverriding = checker->CheckOverride(signature, interface);
2183af6ab5fSopenharmony_ci
2193af6ab5fSopenharmony_ci    for (auto *const superInterface : interface->Interfaces()) {
2203af6ab5fSopenharmony_ci        isOverriding |= CheckInterfaceOverride(checker, superInterface, signature);
2213af6ab5fSopenharmony_ci    }
2223af6ab5fSopenharmony_ci
2233af6ab5fSopenharmony_ci    return isOverriding;
2243af6ab5fSopenharmony_ci}
2253af6ab5fSopenharmony_ci
2263af6ab5fSopenharmony_cistatic varbinder::Scope *NodeScope(ir::AstNode *ast)
2273af6ab5fSopenharmony_ci{
2283af6ab5fSopenharmony_ci    if (ast->IsBlockStatement()) {
2293af6ab5fSopenharmony_ci        return ast->AsBlockStatement()->Scope();
2303af6ab5fSopenharmony_ci    }
2313af6ab5fSopenharmony_ci    if (ast->IsDoWhileStatement()) {
2323af6ab5fSopenharmony_ci        return ast->AsDoWhileStatement()->Scope();
2333af6ab5fSopenharmony_ci    }
2343af6ab5fSopenharmony_ci    if (ast->IsForInStatement()) {
2353af6ab5fSopenharmony_ci        return ast->AsForInStatement()->Scope();
2363af6ab5fSopenharmony_ci    }
2373af6ab5fSopenharmony_ci    if (ast->IsForOfStatement()) {
2383af6ab5fSopenharmony_ci        return ast->AsForOfStatement()->Scope();
2393af6ab5fSopenharmony_ci    }
2403af6ab5fSopenharmony_ci    if (ast->IsForUpdateStatement()) {
2413af6ab5fSopenharmony_ci        return ast->AsForUpdateStatement()->Scope();
2423af6ab5fSopenharmony_ci    }
2433af6ab5fSopenharmony_ci    if (ast->IsSwitchStatement()) {
2443af6ab5fSopenharmony_ci        return ast->AsSwitchStatement()->Scope();
2453af6ab5fSopenharmony_ci    }
2463af6ab5fSopenharmony_ci    if (ast->IsWhileStatement()) {
2473af6ab5fSopenharmony_ci        return ast->AsWhileStatement()->Scope();
2483af6ab5fSopenharmony_ci    }
2493af6ab5fSopenharmony_ci    if (ast->IsCatchClause()) {
2503af6ab5fSopenharmony_ci        return ast->AsCatchClause()->Scope();
2513af6ab5fSopenharmony_ci    }
2523af6ab5fSopenharmony_ci    if (ast->IsClassDefinition()) {
2533af6ab5fSopenharmony_ci        return ast->AsClassDefinition()->Scope();
2543af6ab5fSopenharmony_ci    }
2553af6ab5fSopenharmony_ci    if (ast->IsScriptFunction()) {
2563af6ab5fSopenharmony_ci        return ast->AsScriptFunction()->Scope()->ParamScope();
2573af6ab5fSopenharmony_ci    }
2583af6ab5fSopenharmony_ci    return nullptr;
2593af6ab5fSopenharmony_ci}
2603af6ab5fSopenharmony_ci
2613af6ab5fSopenharmony_ci}  // namespace ark::es2panda::checker
2623af6ab5fSopenharmony_ci
2633af6ab5fSopenharmony_ci#endif
264