13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2021 - 2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#ifndef ES2PANDA_COMPILER_ENUM_PRE_CHECK_LOWERING_H 173af6ab5fSopenharmony_ci#define ES2PANDA_COMPILER_ENUM_PRE_CHECK_LOWERING_H 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include "compiler/lowering/phase.h" 203af6ab5fSopenharmony_ci#include "checker/types/ets/etsEnumType.h" 213af6ab5fSopenharmony_ci 223af6ab5fSopenharmony_cinamespace ark::es2panda::compiler { 233af6ab5fSopenharmony_ci 243af6ab5fSopenharmony_ciclass EnumLoweringPhase : public Phase { 253af6ab5fSopenharmony_cipublic: 263af6ab5fSopenharmony_ci EnumLoweringPhase() noexcept = default; 273af6ab5fSopenharmony_ci std::string_view Name() const override 283af6ab5fSopenharmony_ci { 293af6ab5fSopenharmony_ci return "EnumLoweringPhase"; 303af6ab5fSopenharmony_ci } 313af6ab5fSopenharmony_ci bool Perform(public_lib::Context *ctx, parser::Program *program) override; 323af6ab5fSopenharmony_ci static util::UString GetEnumClassName(checker::ETSChecker *checker, const ir::TSEnumDeclaration *const enumDecl); 333af6ab5fSopenharmony_ci checker::ETSChecker *Checker() 343af6ab5fSopenharmony_ci { 353af6ab5fSopenharmony_ci return checker_; 363af6ab5fSopenharmony_ci } 373af6ab5fSopenharmony_ci 383af6ab5fSopenharmony_ci varbinder::ETSBinder *Varbinder() 393af6ab5fSopenharmony_ci { 403af6ab5fSopenharmony_ci return varbinder_; 413af6ab5fSopenharmony_ci } 423af6ab5fSopenharmony_ci 433af6ab5fSopenharmony_ciprivate: 443af6ab5fSopenharmony_ci struct FunctionInfo { 453af6ab5fSopenharmony_ci varbinder::FunctionParamScope *paramScope; 463af6ab5fSopenharmony_ci ArenaVector<ir::Expression *> &¶ms; 473af6ab5fSopenharmony_ci ArenaVector<ir::Statement *> &&body; 483af6ab5fSopenharmony_ci ir::TypeNode *returnTypeAnnotation; 493af6ab5fSopenharmony_ci const ir::TSEnumDeclaration *enumDecl; 503af6ab5fSopenharmony_ci ir::ModifierFlags flags; 513af6ab5fSopenharmony_ci }; 523af6ab5fSopenharmony_ci 533af6ab5fSopenharmony_ci [[nodiscard]] ir::ScriptFunction *MakeFunction(FunctionInfo &&functionInfo); 543af6ab5fSopenharmony_ci ir::ClassDefinition *CreateClass(ir::TSEnumDeclaration *const enumDecl); 553af6ab5fSopenharmony_ci ir::ClassProperty *CreateOrdinalField(ir::ClassDefinition *const enumClass); 563af6ab5fSopenharmony_ci void CreateCCtorForEnumClass(ir::ClassDefinition *const enumClass); 573af6ab5fSopenharmony_ci void CreateCtorForEnumClass(ir::ClassDefinition *const enumClass); 583af6ab5fSopenharmony_ci 593af6ab5fSopenharmony_ci void CreateEnumIntClassFromEnumDeclaration(ir::TSEnumDeclaration *const enumDecl); 603af6ab5fSopenharmony_ci void CreateEnumStringClassFromEnumDeclaration(ir::TSEnumDeclaration *const enumDecl); 613af6ab5fSopenharmony_ci static void AppendParentNames(util::UString &qualifiedName, const ir::AstNode *const node); 623af6ab5fSopenharmony_ci template <typename ElementMaker> 633af6ab5fSopenharmony_ci [[nodiscard]] ir::Identifier *MakeArray(const ir::TSEnumDeclaration *const enumDecl, ir::ClassDefinition *enumClass, 643af6ab5fSopenharmony_ci const util::StringView &name, ir::TypeNode *const typeAnnotation, 653af6ab5fSopenharmony_ci ElementMaker &&elementMaker); 663af6ab5fSopenharmony_ci 673af6ab5fSopenharmony_ci ir::Identifier *CreateEnumNamesArray(const ir::TSEnumDeclaration *const enumDecl, ir::ClassDefinition *enumClass); 683af6ab5fSopenharmony_ci ir::Identifier *CreateEnumValuesArray(const ir::TSEnumDeclaration *const enumDecl, ir::ClassDefinition *enumClass); 693af6ab5fSopenharmony_ci ir::Identifier *CreateEnumStringValuesArray(const ir::TSEnumDeclaration *const enumDecl, 703af6ab5fSopenharmony_ci ir::ClassDefinition *enumClass); 713af6ab5fSopenharmony_ci ir::Identifier *CreateEnumItemsArray(const ir::TSEnumDeclaration *const enumDecl, ir::ClassDefinition *enumClass); 723af6ab5fSopenharmony_ci ir::Identifier *CreateBoxedEnumItemsArray(const ir::TSEnumDeclaration *const enumDecl, 733af6ab5fSopenharmony_ci ir::ClassDefinition *enumClass); 743af6ab5fSopenharmony_ci 753af6ab5fSopenharmony_ci void CreateEnumFromIntMethod(ir::TSEnumDeclaration const *const enumDecl, ir::ClassDefinition *const enumClass, 763af6ab5fSopenharmony_ci ir::Identifier *const arrayIdent, const util::StringView &methodName, 773af6ab5fSopenharmony_ci const util::StringView &returnTypeName); 783af6ab5fSopenharmony_ci void CreateEnumToStringMethod(ir::TSEnumDeclaration const *const enumDecl, ir::ClassDefinition *const enumClass, 793af6ab5fSopenharmony_ci ir::Identifier *const stringValuesArrayIdent); 803af6ab5fSopenharmony_ci void CreateEnumValueOfMethod(ir::TSEnumDeclaration const *const enumDecl, ir::ClassDefinition *const enumClass, 813af6ab5fSopenharmony_ci ir::Identifier *const valuesArrayIdent); 823af6ab5fSopenharmony_ci void CreateEnumGetNameMethod(ir::TSEnumDeclaration const *const enumDecl, ir::ClassDefinition *const enumClass, 833af6ab5fSopenharmony_ci ir::Identifier *const namesArrayIdent); 843af6ab5fSopenharmony_ci void CreateEnumGetValueOfMethod(ir::TSEnumDeclaration const *const enumDecl, ir::ClassDefinition *const enumClass, 853af6ab5fSopenharmony_ci ir::Identifier *const namesArrayIdent); 863af6ab5fSopenharmony_ci void CreateEnumValuesMethod(ir::TSEnumDeclaration const *const enumDecl, ir::ClassDefinition *const enumClass, 873af6ab5fSopenharmony_ci ir::Identifier *const itemsArrayIdent); 883af6ab5fSopenharmony_ci void CreateUnboxingMethod(ir::TSEnumDeclaration const *const enumDecl, ir::ClassDefinition *const enumClass, 893af6ab5fSopenharmony_ci ir::Identifier *const itemsArrayIdent); 903af6ab5fSopenharmony_ci 913af6ab5fSopenharmony_ci ArenaAllocator *Allocator(); 923af6ab5fSopenharmony_ci 933af6ab5fSopenharmony_ciprivate: 943af6ab5fSopenharmony_ci checker::ETSChecker *checker_ {nullptr}; 953af6ab5fSopenharmony_ci parser::Program *program_ {nullptr}; 963af6ab5fSopenharmony_ci varbinder::ETSBinder *varbinder_ {nullptr}; 973af6ab5fSopenharmony_ci}; 983af6ab5fSopenharmony_ci 993af6ab5fSopenharmony_ci} // namespace ark::es2panda::compiler 1003af6ab5fSopenharmony_ci 1013af6ab5fSopenharmony_ci#endif // ES2PANDA_COMPILER_ENUM_PRE_CHECK_LOWERING_H 102