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