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 "checkScopeDeclaration.h"
173af6ab5fSopenharmony_ci#include "ir/expressions/identifier.h"
183af6ab5fSopenharmony_ci#include "ir/base/classDefinition.h"
193af6ab5fSopenharmony_ci#include "varbinder/scope.h"
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_cinamespace ark::es2panda::compiler::ast_verifier {
223af6ab5fSopenharmony_ci
233af6ab5fSopenharmony_ciCheckResult CheckScopeDeclaration::operator()(CheckContext &ctx, const ir::AstNode *ast) const
243af6ab5fSopenharmony_ci{
253af6ab5fSopenharmony_ci    if (!ast->IsIdentifier()) {
263af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
273af6ab5fSopenharmony_ci    }
283af6ab5fSopenharmony_ci
293af6ab5fSopenharmony_ci    auto const *const id = ast->AsIdentifier();
303af6ab5fSopenharmony_ci    auto const *const var = id->Variable();
313af6ab5fSopenharmony_ci    if (var == nullptr) {
323af6ab5fSopenharmony_ci        // This is checked in IdentifierHasVariable
333af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
343af6ab5fSopenharmony_ci    }
353af6ab5fSopenharmony_ci
363af6ab5fSopenharmony_ci    auto const *const scope = var->GetScope();
373af6ab5fSopenharmony_ci    if (scope == nullptr) {
383af6ab5fSopenharmony_ci        // This is checked in VariableHasScope
393af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
403af6ab5fSopenharmony_ci    }
413af6ab5fSopenharmony_ci
423af6ab5fSopenharmony_ci    return CheckScope(ctx, scope);
433af6ab5fSopenharmony_ci}
443af6ab5fSopenharmony_ci
453af6ab5fSopenharmony_ciCheckResult CheckScopeDeclaration::CheckScope(CheckContext &ctx, varbinder::Scope const *const scope) const
463af6ab5fSopenharmony_ci{
473af6ab5fSopenharmony_ci    auto const *const node = scope->Node();
483af6ab5fSopenharmony_ci    if (node == nullptr) {
493af6ab5fSopenharmony_ci        // This is checked in VariableHasEnclosingScope
503af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
513af6ab5fSopenharmony_ci    }
523af6ab5fSopenharmony_ci
533af6ab5fSopenharmony_ci    if (!node->IsScopeBearer()) {
543af6ab5fSopenharmony_ci        ctx.AddCheckMessage("NODE IS NOT SCOPE BEARER", *node, node->Start());
553af6ab5fSopenharmony_ci        return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
563af6ab5fSopenharmony_ci    }
573af6ab5fSopenharmony_ci
583af6ab5fSopenharmony_ci    if (node->Scope() == scope) {
593af6ab5fSopenharmony_ci        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
603af6ab5fSopenharmony_ci    }
613af6ab5fSopenharmony_ci
623af6ab5fSopenharmony_ci    if (node->Scope()->IsFunctionScope()) {
633af6ab5fSopenharmony_ci        auto const *const functionScope = node->Scope()->AsFunctionScope();
643af6ab5fSopenharmony_ci        if (functionScope->ParamScope() == scope) {
653af6ab5fSopenharmony_ci            return {CheckDecision::CORRECT, CheckAction::CONTINUE};
663af6ab5fSopenharmony_ci        }
673af6ab5fSopenharmony_ci    }
683af6ab5fSopenharmony_ci
693af6ab5fSopenharmony_ci    // NOTE(kkonkuznetsov): cannot check with IsLoopScope
703af6ab5fSopenharmony_ci    // Loop Scope has type Local, using flag instead
713af6ab5fSopenharmony_ci    if (node->Scope()->HasFlag(varbinder::ScopeFlags::LOOP_SCOPE)) {
723af6ab5fSopenharmony_ci        // NOTE(kkonkuznetsov): cannot use AsLoopScope here due to assert failure with IsLoopScope
733af6ab5fSopenharmony_ci        auto *const loopScope = reinterpret_cast<varbinder::LoopScope *>(node->Scope());
743af6ab5fSopenharmony_ci        if (loopScope->DeclScope() == scope) {
753af6ab5fSopenharmony_ci            return {CheckDecision::CORRECT, CheckAction::CONTINUE};
763af6ab5fSopenharmony_ci        }
773af6ab5fSopenharmony_ci    }
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_ci    if (node->Scope()->IsCatchScope()) {
803af6ab5fSopenharmony_ci        auto *const catchScope = node->Scope()->AsCatchScope();
813af6ab5fSopenharmony_ci        if (catchScope->ParamScope() == scope) {
823af6ab5fSopenharmony_ci            return {CheckDecision::CORRECT, CheckAction::CONTINUE};
833af6ab5fSopenharmony_ci        }
843af6ab5fSopenharmony_ci    }
853af6ab5fSopenharmony_ci
863af6ab5fSopenharmony_ci    ctx.AddCheckMessage("SCOPE IS NOT CONSISTENT WITH ITS DECLARING NODE", *node, node->Start());
873af6ab5fSopenharmony_ci    return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
883af6ab5fSopenharmony_ci}
893af6ab5fSopenharmony_ci
903af6ab5fSopenharmony_ci}  // namespace ark::es2panda::compiler::ast_verifier
91