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 "variableHasEnclosingScope.h"
173af6ab5fSopenharmony_ci#include "variableHasScope.h"
183af6ab5fSopenharmony_ci#include "helpers.h"
193af6ab5fSopenharmony_ci#include "ir/expressions/functionExpression.h"
203af6ab5fSopenharmony_ci#include "ir/base/scriptFunction.h"
213af6ab5fSopenharmony_ci#include "ir/expressions/identifier.h"
223af6ab5fSopenharmony_ci#include "ir/typeNode.h"
233af6ab5fSopenharmony_ci
243af6ab5fSopenharmony_cinamespace ark::es2panda::compiler::ast_verifier {
253af6ab5fSopenharmony_ci
263af6ab5fSopenharmony_ci[[nodiscard]] CheckResult VariableHasEnclosingScope::operator()(CheckContext &ctx, const ir::AstNode *ast)
273af6ab5fSopenharmony_ci{
283af6ab5fSopenharmony_ci    const auto maybeVar = VariableHasScope::GetLocalScopeVariable(allocator_, ctx, ast);
293af6ab5fSopenharmony_ci    if (!maybeVar) {
303af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
313af6ab5fSopenharmony_ci    }
323af6ab5fSopenharmony_ci    const auto var = *maybeVar;
333af6ab5fSopenharmony_ci    const auto scope = var->GetScope();
343af6ab5fSopenharmony_ci    if (scope == nullptr) {
353af6ab5fSopenharmony_ci        // already checked
363af6ab5fSopenharmony_ci        return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
373af6ab5fSopenharmony_ci    }
383af6ab5fSopenharmony_ci    const auto encloseScope = scope->EnclosingVariableScope();
393af6ab5fSopenharmony_ci    if (encloseScope == nullptr) {
403af6ab5fSopenharmony_ci        ctx.AddCheckMessage("NO_ENCLOSING_VAR_SCOPE", *ast, ast->Start());
413af6ab5fSopenharmony_ci        return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
423af6ab5fSopenharmony_ci    }
433af6ab5fSopenharmony_ci    const auto node = scope->Node();
443af6ab5fSopenharmony_ci    auto result = std::make_tuple(CheckDecision::CORRECT, CheckAction::CONTINUE);
453af6ab5fSopenharmony_ci    if (!IsContainedIn(ast, node)) {
463af6ab5fSopenharmony_ci        if (CheckCatchClause(ast, node)) {
473af6ab5fSopenharmony_ci            return {CheckDecision::CORRECT, CheckAction::CONTINUE};
483af6ab5fSopenharmony_ci        }
493af6ab5fSopenharmony_ci
503af6ab5fSopenharmony_ci        if (CheckAstExceptions(ast)) {
513af6ab5fSopenharmony_ci            return {CheckDecision::CORRECT, CheckAction::CONTINUE};
523af6ab5fSopenharmony_ci        }
533af6ab5fSopenharmony_ci
543af6ab5fSopenharmony_ci        result = {CheckDecision::INCORRECT, CheckAction::CONTINUE};
553af6ab5fSopenharmony_ci        ctx.AddCheckMessage("VARIABLE_NOT_ENCLOSE_SCOPE", *ast, ast->Start());
563af6ab5fSopenharmony_ci    }
573af6ab5fSopenharmony_ci
583af6ab5fSopenharmony_ci    if (!IsContainedIn<varbinder::Scope>(scope, encloseScope)) {
593af6ab5fSopenharmony_ci        result = {CheckDecision::INCORRECT, CheckAction::CONTINUE};
603af6ab5fSopenharmony_ci        ctx.AddCheckMessage("VARIABLE_NOT_ENCLOSE_SCOPE", *ast, ast->Start());
613af6ab5fSopenharmony_ci    }
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_ci    return result;
643af6ab5fSopenharmony_ci}
653af6ab5fSopenharmony_ci
663af6ab5fSopenharmony_cibool VariableHasEnclosingScope::CheckCatchClause(const ir::AstNode *ast, const ir::AstNode *node) const
673af6ab5fSopenharmony_ci{
683af6ab5fSopenharmony_ci    if (node == nullptr) {
693af6ab5fSopenharmony_ci        return false;
703af6ab5fSopenharmony_ci    }
713af6ab5fSopenharmony_ci
723af6ab5fSopenharmony_ci    // Check that ast node is contained within node parent for Catch Clause:
733af6ab5fSopenharmony_ci    // Catch Clause {
743af6ab5fSopenharmony_ci    //      Catch Body {
753af6ab5fSopenharmony_ci    //          AST that we need to check
763af6ab5fSopenharmony_ci    //      }
773af6ab5fSopenharmony_ci    //      Param (Scope Node) {
783af6ab5fSopenharmony_ci    //      }
793af6ab5fSopenharmony_ci    // }
803af6ab5fSopenharmony_ci    if (node->Parent() != nullptr && node->Parent()->IsCatchClause()) {
813af6ab5fSopenharmony_ci        return IsContainedIn(ast, node->Parent());
823af6ab5fSopenharmony_ci    }
833af6ab5fSopenharmony_ci
843af6ab5fSopenharmony_ci    return false;
853af6ab5fSopenharmony_ci}
863af6ab5fSopenharmony_ci
873af6ab5fSopenharmony_cibool VariableHasEnclosingScope::CheckAstExceptions(const ir::AstNode *ast) const
883af6ab5fSopenharmony_ci{
893af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): skip parameter expression inside arrow function expression
903af6ab5fSopenharmony_ci    auto parent = ast->Parent();
913af6ab5fSopenharmony_ci    while (parent != nullptr) {
923af6ab5fSopenharmony_ci        if (parent->IsETSParameterExpression()) {
933af6ab5fSopenharmony_ci            return true;
943af6ab5fSopenharmony_ci        }
953af6ab5fSopenharmony_ci
963af6ab5fSopenharmony_ci        parent = parent->Parent();
973af6ab5fSopenharmony_ci    }
983af6ab5fSopenharmony_ci
993af6ab5fSopenharmony_ci    // Labels are attached to loop scopes,
1003af6ab5fSopenharmony_ci    // however label identifier is outside of loop.
1013af6ab5fSopenharmony_ci    // Example:
1023af6ab5fSopenharmony_ci    //
1033af6ab5fSopenharmony_ci    // loop: for (let i = 0; i < 10; i++) {
1043af6ab5fSopenharmony_ci    // }
1053af6ab5fSopenharmony_ci    return (ast->Parent()->IsLabelledStatement());
1063af6ab5fSopenharmony_ci}
1073af6ab5fSopenharmony_ci
1083af6ab5fSopenharmony_ci}  // namespace ark::es2panda::compiler::ast_verifier
109