1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "checkScopeDeclaration.h"
17#include "ir/expressions/identifier.h"
18#include "ir/base/classDefinition.h"
19#include "varbinder/scope.h"
20
21namespace ark::es2panda::compiler::ast_verifier {
22
23CheckResult CheckScopeDeclaration::operator()(CheckContext &ctx, const ir::AstNode *ast) const
24{
25    if (!ast->IsIdentifier()) {
26        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
27    }
28
29    auto const *const id = ast->AsIdentifier();
30    auto const *const var = id->Variable();
31    if (var == nullptr) {
32        // This is checked in IdentifierHasVariable
33        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
34    }
35
36    auto const *const scope = var->GetScope();
37    if (scope == nullptr) {
38        // This is checked in VariableHasScope
39        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
40    }
41
42    return CheckScope(ctx, scope);
43}
44
45CheckResult CheckScopeDeclaration::CheckScope(CheckContext &ctx, varbinder::Scope const *const scope) const
46{
47    auto const *const node = scope->Node();
48    if (node == nullptr) {
49        // This is checked in VariableHasEnclosingScope
50        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
51    }
52
53    if (!node->IsScopeBearer()) {
54        ctx.AddCheckMessage("NODE IS NOT SCOPE BEARER", *node, node->Start());
55        return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
56    }
57
58    if (node->Scope() == scope) {
59        return {CheckDecision::CORRECT, CheckAction::CONTINUE};
60    }
61
62    if (node->Scope()->IsFunctionScope()) {
63        auto const *const functionScope = node->Scope()->AsFunctionScope();
64        if (functionScope->ParamScope() == scope) {
65            return {CheckDecision::CORRECT, CheckAction::CONTINUE};
66        }
67    }
68
69    // NOTE(kkonkuznetsov): cannot check with IsLoopScope
70    // Loop Scope has type Local, using flag instead
71    if (node->Scope()->HasFlag(varbinder::ScopeFlags::LOOP_SCOPE)) {
72        // NOTE(kkonkuznetsov): cannot use AsLoopScope here due to assert failure with IsLoopScope
73        auto *const loopScope = reinterpret_cast<varbinder::LoopScope *>(node->Scope());
74        if (loopScope->DeclScope() == scope) {
75            return {CheckDecision::CORRECT, CheckAction::CONTINUE};
76        }
77    }
78
79    if (node->Scope()->IsCatchScope()) {
80        auto *const catchScope = node->Scope()->AsCatchScope();
81        if (catchScope->ParamScope() == scope) {
82            return {CheckDecision::CORRECT, CheckAction::CONTINUE};
83        }
84    }
85
86    ctx.AddCheckMessage("SCOPE IS NOT CONSISTENT WITH ITS DECLARING NODE", *node, node->Start());
87    return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
88}
89
90}  // namespace ark::es2panda::compiler::ast_verifier
91