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