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