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_IR_TS_ENUM_DECLARATION_H 17 #define ES2PANDA_IR_TS_ENUM_DECLARATION_H 18 19 #include "ir/statement.h" 20 #include "ir/statements/annotationUsage.h" 21 #include "varbinder/scope.h" 22 23 namespace ark::es2panda::varbinder { 24 class EnumVariable; 25 } // namespace ark::es2panda::varbinder 26 27 namespace ark::es2panda::ir { 28 class Identifier; 29 class TSEnumMember; 30 31 class TSEnumDeclaration : public TypedStatement { 32 public: 33 // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init) 34 struct ConstructorFlags { 35 bool isConst; 36 bool isStatic = false; 37 bool isDeclare = false; 38 }; 39 // NOLINTEND(cppcoreguidelines-pro-type-member-init) 40 TSEnumDeclaration(ArenaAllocator * allocator,Identifier * key,ArenaVector<AstNode * > && members,ConstructorFlags && flags)41 explicit TSEnumDeclaration(ArenaAllocator *allocator, Identifier *key, ArenaVector<AstNode *> &&members, 42 ConstructorFlags &&flags) 43 : TypedStatement(AstNodeType::TS_ENUM_DECLARATION), 44 decorators_(allocator->Adapter()), 45 key_(key), 46 members_(std::move(members)), 47 isConst_(flags.isConst) 48 { 49 if (flags.isStatic) { 50 AddModifier(ModifierFlags::STATIC); 51 } 52 if (flags.isDeclare) { 53 AddModifier(ModifierFlags::DECLARE); 54 } 55 } 56 IsScopeBearer()57 [[nodiscard]] bool IsScopeBearer() const noexcept override 58 { 59 return true; 60 } 61 Scope()62 [[nodiscard]] varbinder::LocalScope *Scope() const noexcept override 63 { 64 return scope_; 65 } 66 SetScope(varbinder::LocalScope * scope)67 void SetScope(varbinder::LocalScope *scope) 68 { 69 ES2PANDA_ASSERT(scope_ == nullptr); 70 scope_ = scope; 71 } 72 ClearScope()73 void ClearScope() noexcept override 74 { 75 scope_ = nullptr; 76 } 77 Key()78 const Identifier *Key() const 79 { 80 return key_; 81 } 82 Key()83 Identifier *Key() 84 { 85 return key_; 86 } 87 Members()88 const ArenaVector<AstNode *> &Members() const 89 { 90 return members_; 91 } 92 InternalName()93 const util::StringView &InternalName() const 94 { 95 return internalName_; 96 } 97 SetInternalName(util::StringView internalName)98 void SetInternalName(util::StringView internalName) 99 { 100 internalName_ = internalName; 101 } 102 BoxedClass()103 ir::ClassDefinition *BoxedClass() const 104 { 105 return boxedClass_; 106 } 107 SetBoxedClass(ir::ClassDefinition * const wrapperClass)108 void SetBoxedClass(ir::ClassDefinition *const wrapperClass) 109 { 110 boxedClass_ = wrapperClass; 111 } 112 IsConst()113 bool IsConst() const 114 { 115 return isConst_; 116 } 117 Decorators()118 const ArenaVector<Decorator *> &Decorators() const 119 { 120 return decorators_; 121 } 122 DecoratorsPtr()123 const ArenaVector<Decorator *> *DecoratorsPtr() const override 124 { 125 return &Decorators(); 126 } 127 AddDecorators(ArenaVector<ir::Decorator * > && decorators)128 void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override 129 { 130 decorators_ = std::move(decorators); 131 } 132 CanHaveDecorator(bool inTs)133 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 134 { 135 return !inTs; 136 } 137 138 static varbinder::EnumMemberResult EvaluateEnumMember(checker::TSChecker *checker, varbinder::EnumVariable *enumVar, 139 const ir::AstNode *expr); 140 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 141 void Iterate(const NodeTraverser &cb) const override; 142 void Dump(ir::AstDumper *dumper) const override; 143 void Dump(ir::SrcDumper *dumper) const override; 144 void Compile(compiler::PandaGen *pg) const override; 145 void Compile(compiler::ETSGen *etsg) const override; 146 checker::Type *Check(checker::TSChecker *checker) override; 147 checker::VerifiedType Check(checker::ETSChecker *checker) override; 148 Accept(ASTVisitorT * v)149 void Accept(ASTVisitorT *v) override 150 { 151 v->Accept(this); 152 } 153 154 TSEnumDeclaration *Construct(ArenaAllocator *allocator) override; 155 void CopyTo(AstNode *other) const override; 156 157 private: 158 bool RegisterUnexportedForDeclGen(ir::SrcDumper *dumper) const; 159 friend class SizeOfNodeTest; 160 varbinder::LocalScope *scope_ {nullptr}; 161 ArenaVector<ir::Decorator *> decorators_; 162 Identifier *key_; 163 ArenaVector<AstNode *> members_; 164 util::StringView internalName_; 165 ir::ClassDefinition *boxedClass_ {nullptr}; 166 bool isConst_; 167 }; 168 } // namespace ark::es2panda::ir 169 170 #endif 171