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 "variableHasScope.h"
193af6ab5fSopenharmony_ci#include "ir/base/scriptFunction.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 VariableHasScope::operator()(CheckContext &ctx, const ir::AstNode *ast)
263af6ab5fSopenharmony_ci{
273af6ab5fSopenharmony_ci    if (!ast->IsIdentifier()) {
283af6ab5fSopenharmony_ci        // Check invariant of Identifier only
293af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
303af6ab5fSopenharmony_ci    }
313af6ab5fSopenharmony_ci
323af6ab5fSopenharmony_ci    // we will check invariant for only local variables of identifiers
333af6ab5fSopenharmony_ci    if (const auto maybeVar = GetLocalScopeVariable(allocator_, ctx, ast); maybeVar.has_value()) {
343af6ab5fSopenharmony_ci        const auto var = *maybeVar;
353af6ab5fSopenharmony_ci        const auto scope = var->GetScope();
363af6ab5fSopenharmony_ci        if (scope == nullptr) {
373af6ab5fSopenharmony_ci            ctx.AddCheckMessage("NULL_SCOPE_LOCAL_VAR", *ast, ast->Start());
383af6ab5fSopenharmony_ci            return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
393af6ab5fSopenharmony_ci        }
403af6ab5fSopenharmony_ci
413af6ab5fSopenharmony_ci        auto result = std::make_tuple(CheckDecision::CORRECT, CheckAction::CONTINUE);
423af6ab5fSopenharmony_ci        if (!ScopeEncloseVariable(ctx, var)) {
433af6ab5fSopenharmony_ci            result = {CheckDecision::INCORRECT, CheckAction::CONTINUE};
443af6ab5fSopenharmony_ci        }
453af6ab5fSopenharmony_ci
463af6ab5fSopenharmony_ci        return result;
473af6ab5fSopenharmony_ci    }
483af6ab5fSopenharmony_ci
493af6ab5fSopenharmony_ci    return {CheckDecision::CORRECT, CheckAction::CONTINUE};
503af6ab5fSopenharmony_ci}
513af6ab5fSopenharmony_ci
523af6ab5fSopenharmony_cistd::optional<varbinder::LocalVariable *> VariableHasScope::GetLocalScopeVariable(ArenaAllocator &allocator,
533af6ab5fSopenharmony_ci                                                                                  CheckContext &ctx,
543af6ab5fSopenharmony_ci                                                                                  const ir::AstNode *ast)
553af6ab5fSopenharmony_ci{
563af6ab5fSopenharmony_ci    if (!ast->IsIdentifier()) {
573af6ab5fSopenharmony_ci        return std::nullopt;
583af6ab5fSopenharmony_ci    }
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_ci    auto invariantHasVariable = IdentifierHasVariable {allocator};
613af6ab5fSopenharmony_ci    const auto variable = ast->AsIdentifier()->Variable();
623af6ab5fSopenharmony_ci    const auto [decision, action] = invariantHasVariable(ctx, ast);
633af6ab5fSopenharmony_ci
643af6ab5fSopenharmony_ci    if (variable == nullptr) {
653af6ab5fSopenharmony_ci        // NOTE(kkonkuznetsov): variable should not be null
663af6ab5fSopenharmony_ci        // but currently some identifiers do not have variables,
673af6ab5fSopenharmony_ci        // see exceptions in IdentifierHasVariable check
683af6ab5fSopenharmony_ci        return std::nullopt;
693af6ab5fSopenharmony_ci    }
703af6ab5fSopenharmony_ci
713af6ab5fSopenharmony_ci    if (decision == CheckDecision::CORRECT && variable->IsLocalVariable()) {
723af6ab5fSopenharmony_ci        const auto localVar = variable->AsLocalVariable();
733af6ab5fSopenharmony_ci        if (localVar->HasFlag(varbinder::VariableFlags::LOCAL)) {
743af6ab5fSopenharmony_ci            return localVar;
753af6ab5fSopenharmony_ci        }
763af6ab5fSopenharmony_ci    }
773af6ab5fSopenharmony_ci
783af6ab5fSopenharmony_ci    return std::nullopt;
793af6ab5fSopenharmony_ci}
803af6ab5fSopenharmony_ci
813af6ab5fSopenharmony_cibool VariableHasScope::ScopeEncloseVariable(CheckContext &ctx, const varbinder::LocalVariable *var)
823af6ab5fSopenharmony_ci{
833af6ab5fSopenharmony_ci    ASSERT(var);
843af6ab5fSopenharmony_ci
853af6ab5fSopenharmony_ci    const auto scope = var->GetScope();
863af6ab5fSopenharmony_ci    if (scope == nullptr || var->Declaration() == nullptr) {
873af6ab5fSopenharmony_ci        return true;
883af6ab5fSopenharmony_ci    }
893af6ab5fSopenharmony_ci
903af6ab5fSopenharmony_ci    const auto node = var->Declaration()->Node();
913af6ab5fSopenharmony_ci    if (node == nullptr) {
923af6ab5fSopenharmony_ci        return true;
933af6ab5fSopenharmony_ci    }
943af6ab5fSopenharmony_ci
953af6ab5fSopenharmony_ci    const auto varStart = node->Start();
963af6ab5fSopenharmony_ci    bool isOk = true;
973af6ab5fSopenharmony_ci    if (scope->Bindings().count(var->Name()) == 0) {
983af6ab5fSopenharmony_ci        ctx.AddCheckMessage("SCOPE_DO_NOT_ENCLOSE_LOCAL_VAR", *node, varStart);
993af6ab5fSopenharmony_ci        isOk = false;
1003af6ab5fSopenharmony_ci    }
1013af6ab5fSopenharmony_ci
1023af6ab5fSopenharmony_ci    const auto scopeNode = scope->Node();
1033af6ab5fSopenharmony_ci    const auto varNode = node;
1043af6ab5fSopenharmony_ci    bool skip = CheckAstExceptions(varNode);
1053af6ab5fSopenharmony_ci
1063af6ab5fSopenharmony_ci    if (!IsContainedIn(varNode, scopeNode) || scopeNode == nullptr) {
1073af6ab5fSopenharmony_ci        if (!skip) {
1083af6ab5fSopenharmony_ci            ctx.AddCheckMessage("SCOPE_NODE_DONT_DOMINATE_VAR_NODE", *node, varStart);
1093af6ab5fSopenharmony_ci            isOk = false;
1103af6ab5fSopenharmony_ci        }
1113af6ab5fSopenharmony_ci    }
1123af6ab5fSopenharmony_ci
1133af6ab5fSopenharmony_ci    const auto &decls = scope->Decls();
1143af6ab5fSopenharmony_ci    const auto declDominate = std::count(decls.begin(), decls.end(), var->Declaration());
1153af6ab5fSopenharmony_ci    if (declDominate == 0) {
1163af6ab5fSopenharmony_ci        if (!skip) {
1173af6ab5fSopenharmony_ci            ctx.AddCheckMessage("SCOPE_DECL_DONT_DOMINATE_VAR_DECL", *node, varStart);
1183af6ab5fSopenharmony_ci            isOk = false;
1193af6ab5fSopenharmony_ci        }
1203af6ab5fSopenharmony_ci    }
1213af6ab5fSopenharmony_ci
1223af6ab5fSopenharmony_ci    return isOk;
1233af6ab5fSopenharmony_ci}
1243af6ab5fSopenharmony_ci
1253af6ab5fSopenharmony_cibool VariableHasScope::CheckAstExceptions(const ir::AstNode *ast)
1263af6ab5fSopenharmony_ci{
1273af6ab5fSopenharmony_ci    // Labels are attached to loop scopes,
1283af6ab5fSopenharmony_ci    // however label identifier is outside of loop.
1293af6ab5fSopenharmony_ci    // Example:
1303af6ab5fSopenharmony_ci    //
1313af6ab5fSopenharmony_ci    // ```
1323af6ab5fSopenharmony_ci    // loop: for (let i = 0; i < 10; i++) {
1333af6ab5fSopenharmony_ci    // }
1343af6ab5fSopenharmony_ci    // ```
1353af6ab5fSopenharmony_ci    return ast->IsLabelledStatement();
1363af6ab5fSopenharmony_ci}
1373af6ab5fSopenharmony_ci
1383af6ab5fSopenharmony_ci}  // namespace ark::es2panda::compiler::ast_verifier
139