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 "arithmeticOperationValid.h"
17 #include "ir/expressions/binaryExpression.h"
18 #include "ir/base/classDefinition.h"
19 #include "ir/ts/tsInterfaceDeclaration.h"
20 #include "ir/ts/tsEnumDeclaration.h"
21 #include "ir/ts/tsInterfaceBody.h"
22 #include "ast_verifier/helpers.h"
23
24 namespace ark::es2panda::compiler::ast_verifier {
25
operator ()(const ir::AstNode * ast)26 [[nodiscard]] CheckResult ArithmeticOperationValid::operator()(const ir::AstNode *ast)
27 {
28 if (auto [decision, action] = CheckCompound(ast); action == CheckAction::SKIP_SUBTREE) {
29 return {decision, action};
30 }
31 if (!ast->IsBinaryExpression() || !ast->AsBinaryExpression()->IsArithmetic()) {
32 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
33 }
34 if ((ast->AsBinaryExpression()->OperatorType() == lexer::TokenType::PUNCTUATOR_PLUS ||
35 ast->AsBinaryExpression()->OperatorType() == lexer::TokenType::PUNCTUATOR_PLUS_EQUAL) &&
36 (IsStringType(ast->AsBinaryExpression()->Left()) || IsStringType(ast->AsBinaryExpression()->Right()))) {
37 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
38 }
39 auto result = std::make_tuple(CheckDecision::CORRECT, CheckAction::CONTINUE);
40 bool isBitwise = ast->AsBinaryExpression()->IsBitwise();
41 ast->Iterate([this, &result, &isBitwise](ir::AstNode *child) {
42 if (!IsValidTypeForBinaryOp(child, isBitwise)) {
43 AddCheckMessage("Not a numeric type", *child);
44 result = {CheckDecision::INCORRECT, CheckAction::CONTINUE};
45 }
46 });
47 return result;
48 }
49
CheckCompound(const ir::AstNode * ast)50 CheckResult ArithmeticOperationValid::CheckCompound(const ir::AstNode *ast)
51 {
52 if (ast->IsTSInterfaceDeclaration()) {
53 for (const auto &member : ast->AsTSInterfaceDeclaration()->Body()->Body()) {
54 [[maybe_unused]] auto _ = (*this)(member);
55 }
56 return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
57 }
58 if (ast->IsTSEnumDeclaration()) {
59 for (const auto &member : ast->AsTSEnumDeclaration()->Members()) {
60 [[maybe_unused]] auto _ = (*this)(member);
61 }
62 return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
63 }
64 if (ast->IsClassDefinition()) {
65 for (const auto &member : ast->AsClassDefinition()->Body()) {
66 [[maybe_unused]] auto _ = (*this)(member);
67 }
68 return {CheckDecision::CORRECT, CheckAction::SKIP_SUBTREE};
69 }
70 return {CheckDecision::CORRECT, CheckAction::CONTINUE};
71 }
72
73 } // namespace ark::es2panda::compiler::ast_verifier
74