13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 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#include "helpers.h"
173af6ab5fSopenharmony_ci#include "identifierHasVariable.h"
183af6ab5fSopenharmony_ci#include "ir/base/scriptFunction.h"
193af6ab5fSopenharmony_ci#include "ir/expressions/memberExpression.h"
203af6ab5fSopenharmony_ci#include "ir/ts/tsEnumDeclaration.h"
213af6ab5fSopenharmony_ci#include "ir/typeNode.h"
223af6ab5fSopenharmony_ci
233af6ab5fSopenharmony_cinamespace ark::es2panda::compiler::ast_verifier {
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_ciCheckResult IdentifierHasVariable::operator()(CheckContext &ctx, const ir::AstNode *ast)
263af6ab5fSopenharmony_ci{
273af6ab5fSopenharmony_ci    if (!ast->IsIdentifier()) {
283af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
293af6ab5fSopenharmony_ci    }
303af6ab5fSopenharmony_ci
313af6ab5fSopenharmony_ci    if (ast->AsIdentifier()->Variable() != nullptr) {
323af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
333af6ab5fSopenharmony_ci    }
343af6ab5fSopenharmony_ci
353af6ab5fSopenharmony_ci    const auto *id = ast->AsIdentifier();
363af6ab5fSopenharmony_ci    if (CheckAstExceptions(id)) {
373af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
383af6ab5fSopenharmony_ci    }
393af6ab5fSopenharmony_ci
403af6ab5fSopenharmony_ci    // Another function with exceptions to reduce function size
413af6ab5fSopenharmony_ci    if (CheckMoreAstExceptions(id)) {
423af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
433af6ab5fSopenharmony_ci    }
443af6ab5fSopenharmony_ci
453af6ab5fSopenharmony_ci    ctx.AddCheckMessage("NULL_VARIABLE", *id, id->Start());
463af6ab5fSopenharmony_ci    return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
473af6ab5fSopenharmony_ci}
483af6ab5fSopenharmony_ci
493af6ab5fSopenharmony_cibool IdentifierHasVariable::CheckMoreAstExceptions(const ir::Identifier *ast) const
503af6ab5fSopenharmony_ci{
513af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): skip async functions
523af6ab5fSopenharmony_ci    auto parent = ast->Parent();
533af6ab5fSopenharmony_ci    while (parent != nullptr) {
543af6ab5fSopenharmony_ci        if (parent->IsScriptFunction()) {
553af6ab5fSopenharmony_ci            auto script = parent->AsScriptFunction();
563af6ab5fSopenharmony_ci            if (script->IsAsyncFunc()) {
573af6ab5fSopenharmony_ci                return true;
583af6ab5fSopenharmony_ci            }
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_ci            break;
613af6ab5fSopenharmony_ci        }
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_ci        parent = parent->Parent();
643af6ab5fSopenharmony_ci    }
653af6ab5fSopenharmony_ci
663af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): skip reexport declarations
673af6ab5fSopenharmony_ci    if (ast->Parent() != nullptr && ast->Parent()->Parent() != nullptr) {
683af6ab5fSopenharmony_ci        parent = ast->Parent()->Parent();
693af6ab5fSopenharmony_ci        if (parent->IsETSReExportDeclaration()) {
703af6ab5fSopenharmony_ci            return true;
713af6ab5fSopenharmony_ci        }
723af6ab5fSopenharmony_ci    }
733af6ab5fSopenharmony_ci
743af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): object expressions
753af6ab5fSopenharmony_ci    parent = ast->Parent();
763af6ab5fSopenharmony_ci    while (parent != nullptr) {
773af6ab5fSopenharmony_ci        if (parent->IsObjectExpression()) {
783af6ab5fSopenharmony_ci            return true;
793af6ab5fSopenharmony_ci        }
803af6ab5fSopenharmony_ci
813af6ab5fSopenharmony_ci        parent = parent->Parent();
823af6ab5fSopenharmony_ci    }
833af6ab5fSopenharmony_ci
843af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): some identifiers have empty names
853af6ab5fSopenharmony_ci    if (ast->Name().Empty()) {
863af6ab5fSopenharmony_ci        return true;
873af6ab5fSopenharmony_ci    }
883af6ab5fSopenharmony_ci
893af6ab5fSopenharmony_ci    // NOTE(mmartin): find a better solution to handle utility type resolution
903af6ab5fSopenharmony_ci    if (ast->Name().Is(Signatures::PARTIAL_TYPE_NAME) || ast->Name().Is(Signatures::REQUIRED_TYPE_NAME) ||
913af6ab5fSopenharmony_ci        ast->Name().Is(Signatures::READONLY_TYPE_NAME)) {
923af6ab5fSopenharmony_ci        return true;
933af6ab5fSopenharmony_ci    }
943af6ab5fSopenharmony_ci
953af6ab5fSopenharmony_ci    return false;
963af6ab5fSopenharmony_ci}
973af6ab5fSopenharmony_ci
983af6ab5fSopenharmony_cibool IdentifierHasVariable::CheckAstExceptions(const ir::Identifier *ast) const
993af6ab5fSopenharmony_ci{
1003af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): skip enums
1013af6ab5fSopenharmony_ci    if (ast->Parent()->IsMemberExpression() &&
1023af6ab5fSopenharmony_ci        (ast->Parent()->AsMemberExpression()->Object()->TsType() == nullptr ||
1033af6ab5fSopenharmony_ci         ast->Parent()->AsMemberExpression()->Object()->TsType()->IsETSEnumType())) {
1043af6ab5fSopenharmony_ci        return true;
1053af6ab5fSopenharmony_ci    }
1063af6ab5fSopenharmony_ci
1073af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): skip length property
1083af6ab5fSopenharmony_ci    if (ast->Parent()->IsMemberExpression() && ast->Name().Is("length")) {
1093af6ab5fSopenharmony_ci        return true;
1103af6ab5fSopenharmony_ci    }
1113af6ab5fSopenharmony_ci
1123af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): skip anonymous class id
1133af6ab5fSopenharmony_ci    if (ast->Parent()->Parent() != nullptr && ast->Parent()->Parent()->IsETSNewClassInstanceExpression()) {
1143af6ab5fSopenharmony_ci        return true;
1153af6ab5fSopenharmony_ci    }
1163af6ab5fSopenharmony_ci
1173af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): skip package declarations
1183af6ab5fSopenharmony_ci    auto parent = ast->Parent();
1193af6ab5fSopenharmony_ci    while (parent != nullptr) {
1203af6ab5fSopenharmony_ci        if (parent->IsETSPackageDeclaration()) {
1213af6ab5fSopenharmony_ci            return true;
1223af6ab5fSopenharmony_ci        }
1233af6ab5fSopenharmony_ci
1243af6ab5fSopenharmony_ci        parent = parent->Parent();
1253af6ab5fSopenharmony_ci    }
1263af6ab5fSopenharmony_ci
1273af6ab5fSopenharmony_ci    return false;
1283af6ab5fSopenharmony_ci}
1293af6ab5fSopenharmony_ci
1303af6ab5fSopenharmony_ci}  // namespace ark::es2panda::compiler::ast_verifier
131