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_INTERFACE_DECLARATION_H 17 #define ES2PANDA_IR_TS_INTERFACE_DECLARATION_H 18 19 #include "varbinder/scope.h" 20 #include "ir/annotationAllowed.h" 21 #include "ir/jsDocAllowed.h" 22 23 namespace ark::es2panda::varbinder { 24 class Variable; 25 } // namespace ark::es2panda::varbinder 26 27 namespace ark::es2panda::ir { 28 class Identifier; 29 class TSInterfaceBody; 30 class TSInterfaceHeritage; 31 class TSTypeParameterDeclaration; 32 33 class TSInterfaceDeclaration : public JsDocAllowed<AnnotationAllowed<TypedStatement>> { 34 public: 35 // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init) 36 struct ConstructorData { 37 Identifier *id; 38 TSTypeParameterDeclaration *typeParams; 39 TSInterfaceBody *body; 40 bool isStatic; 41 bool isExternal; 42 es2panda::Language lang; 43 }; 44 // NOLINTEND(cppcoreguidelines-pro-type-member-init) 45 TSInterfaceDeclaration(ArenaAllocator * allocator,ArenaVector<TSInterfaceHeritage * > && extends,ConstructorData && data)46 explicit TSInterfaceDeclaration(ArenaAllocator *allocator, ArenaVector<TSInterfaceHeritage *> &&extends, 47 ConstructorData &&data) 48 : JsDocAllowed<AnnotationAllowed<TypedStatement>>(AstNodeType::TS_INTERFACE_DECLARATION, allocator), 49 decorators_(allocator->Adapter()), 50 id_(data.id), 51 typeParams_(data.typeParams), 52 body_(data.body), 53 extends_(std::move(extends)), 54 isStatic_(data.isStatic), 55 isExternal_(data.isExternal), 56 lang_(data.lang) 57 { 58 if (isStatic_) { 59 AddModifier(ir::ModifierFlags::STATIC); 60 } 61 } 62 IsScopeBearer()63 [[nodiscard]] bool IsScopeBearer() const noexcept override 64 { 65 return true; 66 } 67 Scope()68 [[nodiscard]] varbinder::LocalScope *Scope() const noexcept override 69 { 70 return scope_; 71 } 72 SetScope(varbinder::LocalScope * scope)73 void SetScope(varbinder::LocalScope *scope) 74 { 75 ES2PANDA_ASSERT(scope_ == nullptr); 76 scope_ = scope; 77 } 78 ClearScope()79 void ClearScope() noexcept override 80 { 81 scope_ = nullptr; 82 } 83 Body()84 TSInterfaceBody *Body() 85 { 86 return body_; 87 } 88 Body()89 const TSInterfaceBody *Body() const 90 { 91 return body_; 92 } 93 Id()94 Identifier *Id() 95 { 96 return id_; 97 } 98 Id()99 const Identifier *Id() const 100 { 101 return id_; 102 } 103 InternalName()104 const util::StringView &InternalName() const 105 { 106 return internalName_; 107 } 108 SetInternalName(util::StringView internalName)109 void SetInternalName(util::StringView internalName) 110 { 111 internalName_ = internalName; 112 } 113 IsStatic()114 bool IsStatic() const 115 { 116 return isStatic_; 117 } 118 IsFromExternal()119 bool IsFromExternal() const 120 { 121 return isExternal_; 122 } 123 TypeParams()124 const TSTypeParameterDeclaration *TypeParams() const 125 { 126 return typeParams_; 127 } 128 TypeParams()129 TSTypeParameterDeclaration *TypeParams() 130 { 131 return typeParams_; 132 } 133 Extends()134 ArenaVector<TSInterfaceHeritage *> &Extends() 135 { 136 return extends_; 137 } 138 Extends()139 const ArenaVector<TSInterfaceHeritage *> &Extends() const 140 { 141 return extends_; 142 } 143 Decorators()144 const ArenaVector<Decorator *> &Decorators() const 145 { 146 return decorators_; 147 } 148 DecoratorsPtr()149 const ArenaVector<Decorator *> *DecoratorsPtr() const override 150 { 151 return &Decorators(); 152 } 153 AddDecorators(ArenaVector<ir::Decorator * > && decorators)154 void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override 155 { 156 decorators_ = std::move(decorators); 157 } 158 CanHaveDecorator(bool inTs)159 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 160 { 161 return !inTs; 162 } 163 164 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 165 Language()166 es2panda::Language Language() const 167 { 168 return lang_; 169 } 170 GetAnonClass()171 ClassDeclaration *GetAnonClass() noexcept 172 { 173 return anonClass_; 174 } 175 GetAnonClass()176 ClassDeclaration *GetAnonClass() const noexcept 177 { 178 return anonClass_; 179 } 180 SetAnonClass(ClassDeclaration * anonClass)181 void SetAnonClass(ClassDeclaration *anonClass) noexcept 182 { 183 anonClass_ = anonClass; 184 } 185 186 void Iterate(const NodeTraverser &cb) const override; 187 void Dump(ir::AstDumper *dumper) const override; 188 void Dump(ir::SrcDumper *dumper) const override; 189 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 190 void Compile(compiler::ETSGen *etsg) const override; 191 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 192 checker::VerifiedType Check([[maybe_unused]] checker::ETSChecker *checker) override; 193 checker::Type *InferType(checker::TSChecker *checker, varbinder::Variable *bindingVar) const; 194 Accept(ASTVisitorT * v)195 void Accept(ASTVisitorT *v) override 196 { 197 v->Accept(this); 198 } 199 200 TSInterfaceDeclaration *Construct(ArenaAllocator *allocator) override; 201 void CopyTo(AstNode *other) const override; 202 203 private: 204 bool RegisterUnexportedForDeclGen(ir::SrcDumper *dumper) const; 205 friend class SizeOfNodeTest; 206 ArenaVector<Decorator *> decorators_; 207 varbinder::LocalScope *scope_ {nullptr}; 208 Identifier *id_; 209 TSTypeParameterDeclaration *typeParams_; 210 TSInterfaceBody *body_; 211 ArenaVector<TSInterfaceHeritage *> extends_; 212 util::StringView internalName_ {}; 213 bool isStatic_; 214 bool isExternal_; 215 es2panda::Language lang_; 216 ClassDeclaration *anonClass_ {nullptr}; 217 }; 218 } // namespace ark::es2panda::ir 219 220 #endif 221