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