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 #ifndef ES2PANDA_COMPILER_LOWERING_CONSTANT_EXPRESSION_LOWERING_H 17 #define ES2PANDA_COMPILER_LOWERING_CONSTANT_EXPRESSION_LOWERING_H 18 19 #include "compiler/lowering/phase.h" 20 21 namespace ark::es2panda::compiler { 22 23 enum class TypeRank { 24 // Keep this order 25 CHAR, 26 INT32, 27 INT64, 28 FLOAT, 29 DOUBLE 30 }; 31 32 class ConstantExpressionLowering : public PhaseForDeclarations { 33 public: Name()34 std::string_view Name() const override 35 { 36 return "ConstantExpressionLowering"; 37 } 38 39 bool PerformForModule(public_lib::Context *ctx, parser::Program *program) override; 40 41 private: 42 void LogError(const diagnostic::DiagnosticKind &diagnostic, const util::DiagnosticMessageParams &diagnosticParams, 43 const lexer::SourcePosition &pos) const; 44 45 ir::AstNode *FoldTernaryConstant(ir::ConditionalExpression *cond); 46 47 template <typename InputType> 48 bool PerformRelationOperator(InputType left, InputType right, lexer::TokenType opType); 49 50 bool HandleRelationOperator(ir::Literal *left, ir::Literal *right, lexer::TokenType opType); 51 52 bool HandleBitwiseLogicalOperator(ir::Literal *left, ir::Literal *right, lexer::TokenType opType); 53 54 ir::AstNode *HandleLogicalOperator(ir::BinaryExpression *expr, lexer::TokenType opType); 55 56 ir::AstNode *FoldBinaryBooleanConstant(ir::BinaryExpression *expr); 57 58 template <typename IntegerType> 59 IntegerType PerformBitwiseArithmetic(IntegerType left, IntegerType right, lexer::TokenType operationType); 60 61 template <typename TargetType> 62 lexer::Number HandleBitwiseOperator(TargetType leftNum, TargetType rightNum, lexer::TokenType operationType, 63 TypeRank targetRank); 64 65 template <typename TargetType> 66 TargetType HandleArithmeticOperation(TargetType leftNum, TargetType rightNum, ir::BinaryExpression *expr); 67 68 template <typename InputType> 69 ir::AstNode *FoldBinaryNumericConstantHelper(ir::BinaryExpression *expr, TypeRank targetRank); 70 71 ir::AstNode *FoldBinaryNumericConstant(ir::BinaryExpression *expr); 72 73 ir::AstNode *FoldBinaryStringConstant(ir::BinaryExpression *expr); 74 75 ir::AstNode *FoldBinaryConstant(ir::BinaryExpression *expr); 76 77 template <typename InputType> 78 lexer::Number HandleBitwiseNegate(InputType value, TypeRank rank); 79 80 template <typename InputType> 81 ir::AstNode *FoldUnaryNumericConstantHelper(ir::UnaryExpression *unary, ir::Literal *node, TypeRank rank); 82 83 ir::AstNode *FoldUnaryNumericConstant(ir::UnaryExpression *unary); 84 85 ir::AstNode *FoldUnaryBooleanConstant(ir::UnaryExpression *unary); 86 87 ir::AstNode *FoldUnaryConstant(ir::UnaryExpression *unary); 88 89 ir::AstNode *TryFoldTSAsExpressionForString(ir::TSAsExpression *expr); 90 91 ir::AstNode *FoldTSAsExpressionToChar(ir::TSAsExpression *expr); 92 93 ir::AstNode *FoldTSAsExpressionToBoolean(ir::TSAsExpression *expr); 94 95 ir::AstNode *FoldTSAsExpression(ir::TSAsExpression *expr); 96 97 ir::AstNode *FoldMultilineString(ir::TemplateLiteral *expr); 98 99 ir::AstNode *FoldConstant(ir::AstNode *constantNode); 100 101 varbinder::Variable *FindIdentifier(ir::Identifier *ident); 102 103 ir::AstNode *UnfoldConstIdentifier(ir::AstNode *node, ir::AstNode *originNode); 104 105 ir::AstNode *UnFoldEnumMemberExpression(ir::AstNode *constantNode); 106 107 ir::AstNode *FindNameInEnumMember(ArenaVector<ir::AstNode *> *members, util::StringView targetName); 108 109 ir::AstNode *FindAndReplaceEnumMember(ir::AstNode *expr, ir::AstNode *constantNode); 110 111 ir::AstNode *UnfoldConstIdentifiers(ir::AstNode *constantNode); 112 113 void IsInitByConstant(ir::AstNode *node); 114 void TryFoldInitializerOfPackage(ir::ClassDefinition *globalClass); 115 116 public_lib::Context *context_ {nullptr}; 117 parser::Program *program_ {nullptr}; 118 varbinder::ETSBinder *varbinder_ {nullptr}; 119 }; 120 121 } // namespace ark::es2panda::compiler 122 123 #endif // ES2PANDA_COMPILER_LOWERING_CONSTANT_EXPRESSION_LOWERING_H 124