• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "variableHasEnclosingScope.h"
17 #include "variableHasScope.h"
18 #include "ast_verifier/helpers.h"
19 
20 namespace ark::es2panda::compiler::ast_verifier {
21 
operator ()(const ir::AstNode * ast)22 [[nodiscard]] CheckResult VariableHasEnclosingScope::operator()(const ir::AstNode *ast)
23 {
24     if (!ast->IsIdentifier()) {
25         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
26     }
27     const auto *var = TryGetLocalScopeVariable(ast->AsIdentifier());
28     if (var == nullptr) {
29         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
30     }
31     const auto scope = var->GetScope();
32     if (scope == nullptr) {
33         // already checked
34         return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
35     }
36     const auto encloseScope = scope->EnclosingVariableScope();
37     if (encloseScope == nullptr) {
38         AddCheckMessage("NO_ENCLOSING_VAR_SCOPE", *ast);
39         return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
40     }
41     const auto node = scope->Node();
42     auto result = std::make_tuple(CheckDecision::CORRECT, CheckAction::CONTINUE);
43     if (!IsContainedIn(ast, node)) {
44         if (CheckCatchClause(ast, node)) {
45             return {CheckDecision::CORRECT, CheckAction::CONTINUE};
46         }
47 
48         if (CheckAstExceptions(ast)) {
49             return {CheckDecision::CORRECT, CheckAction::CONTINUE};
50         }
51 
52         result = {CheckDecision::INCORRECT, CheckAction::CONTINUE};
53         AddCheckMessage("VARIABLE_NOT_ENCLOSE_SCOPE", *ast);
54     }
55 
56     if (!IsContainedIn<varbinder::Scope>(scope, encloseScope)) {
57         result = {CheckDecision::INCORRECT, CheckAction::CONTINUE};
58         AddCheckMessage("VARIABLE_NOT_ENCLOSE_SCOPE", *ast);
59     }
60 
61     return result;
62 }
63 
CheckCatchClause(const ir::AstNode * ast,const ir::AstNode * node) const64 bool VariableHasEnclosingScope::CheckCatchClause(const ir::AstNode *ast, const ir::AstNode *node) const
65 {
66     if (node == nullptr) {
67         return false;
68     }
69 
70     // Check that ast node is contained within node parent for Catch Clause:
71     // Catch Clause {
72     //      Catch Body {
73     //          AST that we need to check
74     //      }
75     //      Param (Scope Node) {
76     //      }
77     // }
78     if (node->Parent() != nullptr && node->Parent()->IsCatchClause()) {
79         return IsContainedIn(ast, node->Parent());
80     }
81 
82     return false;
83 }
84 
CheckAstExceptions(const ir::AstNode * ast) const85 bool VariableHasEnclosingScope::CheckAstExceptions(const ir::AstNode *ast) const
86 {
87     // Labels are attached to loop scopes,
88     // however label identifier is outside of loop.
89     // Example:
90     //
91     // loop: for (let i = 0; i < 10; i++) {
92     // }
93     return (ast->Parent()->IsLabelledStatement());
94 }
95 
96 }  // namespace ark::es2panda::compiler::ast_verifier
97