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 "forLoopCorrectlyInitialized.h"
17 #include "ir/statements/forInStatement.h"
18 #include "ir/statements/forOfStatement.h"
19 #include "ir/statements/forUpdateStatement.h"
20
21 namespace ark::es2panda::compiler::ast_verifier {
22
operator ()(const ir::AstNode * ast)23 [[nodiscard]] CheckResult ForLoopCorrectlyInitialized::operator()(const ir::AstNode *ast)
24 {
25 if (ast->IsForInStatement()) {
26 return HandleForInStatement(ast);
27 }
28
29 if (ast->IsForOfStatement()) {
30 return HandleForOfStatement(ast);
31 }
32
33 if (ast->IsForUpdateStatement()) {
34 return HandleForUpdateStatement(ast);
35 }
36 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
37 }
38
HandleForInStatement(const ir::AstNode * ast)39 [[nodiscard]] CheckResult ForLoopCorrectlyInitialized::HandleForInStatement(const ir::AstNode *ast)
40 {
41 auto const *left = ast->AsForInStatement()->Left();
42 if (left == nullptr) {
43 AddCheckMessage("NULL FOR-IN-LEFT", *ast);
44 return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
45 }
46
47 if (!left->IsIdentifier() && !left->IsVariableDeclaration()) {
48 AddCheckMessage("INCORRECT FOR-IN-LEFT", *ast);
49 return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
50 }
51
52 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
53 }
54
HandleForOfStatement(const ir::AstNode * ast)55 [[nodiscard]] CheckResult ForLoopCorrectlyInitialized::HandleForOfStatement(const ir::AstNode *ast)
56 {
57 auto const *left = ast->AsForOfStatement()->Left();
58 if (left == nullptr) {
59 AddCheckMessage("NULL FOR-OF-LEFT", *ast);
60 return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
61 }
62
63 if (!left->IsIdentifier() && !left->IsVariableDeclaration()) {
64 AddCheckMessage("INCORRECT FOR-OF-LEFT", *ast);
65 return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
66 }
67
68 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
69 }
70
HandleForUpdateStatement(const ir::AstNode * ast)71 [[nodiscard]] CheckResult ForLoopCorrectlyInitialized::HandleForUpdateStatement(const ir::AstNode *ast)
72 {
73 auto const *test = ast->AsForUpdateStatement()->Test();
74 if (test == nullptr) {
75 auto const *body = ast->AsForUpdateStatement()->Body();
76 if (body == nullptr) {
77 AddCheckMessage("NULL FOR-TEST AND FOR-BODY", *ast);
78 return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
79 }
80
81 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
82 }
83
84 if (!test->IsExpression()) {
85 AddCheckMessage("NULL FOR VAR", *ast);
86 return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
87 }
88
89 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
90 }
91
92 } // namespace ark::es2panda::compiler::ast_verifier
93