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 "modifierAccessValid.h"
17 #include "helpers.h"
18 #include "varbinder/variableFlags.h"
19 #include "ir/expressions/memberExpression.h"
20 #include "ir/expressions/callExpression.h"
21 
22 namespace ark::es2panda::compiler::ast_verifier {
23 
operator ()(CheckContext &ctx, const ir::AstNode *ast)24 [[nodiscard]] CheckResult ModifierAccessValid::operator()(CheckContext &ctx, const ir::AstNode *ast)
25 {
26     if (auto [decision, action] = HandleMethodExpression(ctx, ast); decision == CheckDecision::INCORRECT) {
27         return {decision, action};
28     }
29     if (auto [decision, action] = HandleCallExpression(ctx, ast); decision == CheckDecision::INCORRECT) {
30         return {decision, action};
31     }
32     return {CheckDecision::CORRECT, CheckAction::CONTINUE};
33 }
34 
HandleMethodExpression(CheckContext &ctx, const ir::AstNode *ast)35 CheckResult ModifierAccessValid::HandleMethodExpression(CheckContext &ctx, const ir::AstNode *ast)
36 {
37     if (!ast->IsMemberExpression()) {
38         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
39     }
40     const auto *propVar = ast->AsMemberExpression()->PropVar();
41     if (propVar != nullptr && propVar->HasFlag(varbinder::VariableFlags::PROPERTY) &&
42         !ValidateVariableAccess(propVar, ast->AsMemberExpression())) {
43         ctx.AddCheckMessage("PROPERTY_NOT_VISIBLE_HERE", *ast, ast->Start());
44         return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
45     }
46     return {CheckDecision::CORRECT, CheckAction::CONTINUE};
47 }
48 
HandleCallExpression(CheckContext &ctx, const ir::AstNode *ast)49 CheckResult ModifierAccessValid::HandleCallExpression(CheckContext &ctx, const ir::AstNode *ast)
50 {
51     if (!ast->IsCallExpression()) {
52         return {CheckDecision::CORRECT, CheckAction::CONTINUE};
53     }
54     const auto *callExpr = ast->AsCallExpression();
55     const auto *callee = callExpr->Callee();
56     if (callee != nullptr && callee->IsMemberExpression()) {
57         const auto *calleeMember = callee->AsMemberExpression();
58         const auto *propVarCallee = calleeMember->PropVar();
59         if (propVarCallee != nullptr && propVarCallee->HasFlag(varbinder::VariableFlags::METHOD) &&
60             !ValidateMethodAccess(calleeMember, ast->AsCallExpression())) {
61             ctx.AddCheckMessage("PROPERTY_NOT_VISIBLE_HERE", *callee, callee->Start());
62             return {CheckDecision::INCORRECT, CheckAction::CONTINUE};
63         }
64     }
65     return {CheckDecision::CORRECT, CheckAction::CONTINUE};
66 }
67 
68 }  // namespace ark::es2panda::compiler::ast_verifier
69