1 /* 2 * Copyright (c) 2021-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_ENUM_POST_CHECK_LOWERING_H 17 #define ES2PANDA_COMPILER_ENUM_POST_CHECK_LOWERING_H 18 19 #include "compiler/lowering/phase.h" 20 #include "ir/ts/tsAsExpression.h" 21 22 namespace ark::es2panda::compiler { 23 24 enum class EnumCastType { 25 NONE, 26 CAST_TO_STRING, 27 CAST_TO_INT, 28 CAST_TO_INT_ENUM, 29 CAST_TO_STRING_ENUM, 30 }; 31 32 class EnumPostCheckLoweringPhase : public PhaseForBodies { 33 public: 34 EnumPostCheckLoweringPhase() noexcept = default; Name()35 std::string_view Name() const override 36 { 37 return "EnumPostCheckLoweringPhase"; 38 } 39 bool PerformForModule(public_lib::Context *ctx, parser::Program *program) override; 40 41 private: 42 ir::Statement *CreateStatement(const std::string &src, ir::Expression *ident, ir::Expression *init); 43 void CreateStatementForUnionConstituentType(EnumCastType castType, ir::Identifier *ident, checker::Type *type, 44 ir::TSAsExpression *tsAsExpr, ArenaVector<ir::Statement *> &statements); 45 ir::SwitchStatement *GenerateGetOrdinalCallForSwitch(ir::SwitchStatement *const node); 46 ir::AstNode *GenerateEnumCasting(ir::TSAsExpression *node, EnumCastType castType); 47 ir::AstNode *GenerateValueOfCall(ir::AstNode *const node); 48 ir::Expression *GenerateFromValueCall(ir::Expression *const node, util::StringView name); 49 ir::Expression *HandleEnumTypeCasting(checker::Type *type, ir::Expression *expr, ir::TSAsExpression *tsAsExpr); 50 ir::Expression *HandleUnionTypeForCalls(checker::ETSUnionType *unionType, ir::Expression *expr, 51 ir::TSAsExpression *tsAsExpr, EnumCastType castType); 52 53 public_lib::Context *context_ {nullptr}; 54 parser::ETSParser *parser_ {nullptr}; 55 checker::ETSChecker *checker_ {nullptr}; 56 varbinder::ETSBinder *varbinder_ {nullptr}; 57 }; 58 59 } // namespace ark::es2panda::compiler 60 61 #endif // ES2PANDA_COMPILER_ENUM_POST_CHECK_LOWERING_H 62