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_TYPE_ALIAS_DECLARATION_H 17 #define ES2PANDA_IR_TS_TYPE_ALIAS_DECLARATION_H 18 19 #include "ir/statement.h" 20 #include "ir/typed.h" 21 #include "ir/jsDocAllowed.h" 22 #include "ir/statements/annotationUsage.h" 23 24 namespace ark::es2panda::varbinder { 25 class Variable; 26 } // namespace ark::es2panda::varbinder 27 28 namespace ark::es2panda::ir { 29 class Identifier; 30 class TSTypeParameterDeclaration; 31 32 class TSTypeAliasDeclaration : public JsDocAllowed<AnnotatedStatement> { 33 public: TSTypeAliasDeclaration(ArenaAllocator * allocator,Identifier * id,TSTypeParameterDeclaration * typeParams,TypeNode * typeAnnotation)34 explicit TSTypeAliasDeclaration(ArenaAllocator *allocator, Identifier *id, TSTypeParameterDeclaration *typeParams, 35 TypeNode *typeAnnotation) 36 : JsDocAllowed<AnnotatedStatement>(AstNodeType::TS_TYPE_ALIAS_DECLARATION, typeAnnotation, allocator), 37 decorators_(allocator->Adapter()), 38 annotations_(allocator->Adapter()), 39 id_(id), 40 typeParams_(typeParams), 41 typeParamTypes_(allocator->Adapter()) 42 { 43 } 44 TSTypeAliasDeclaration(ArenaAllocator * allocator,Identifier * id)45 explicit TSTypeAliasDeclaration(ArenaAllocator *allocator, Identifier *id) 46 : JsDocAllowed<AnnotatedStatement>(allocator, AstNodeType::TS_TYPE_ALIAS_DECLARATION), 47 decorators_(allocator->Adapter()), 48 annotations_(allocator->Adapter()), 49 id_(id), 50 typeParams_(nullptr), 51 typeParamTypes_(allocator->Adapter()) 52 { 53 } 54 Id()55 Identifier *Id() 56 { 57 return id_; 58 } 59 Id()60 const Identifier *Id() const 61 { 62 return id_; 63 } 64 TypeParams()65 TSTypeParameterDeclaration *TypeParams() const 66 { 67 return typeParams_; 68 } 69 Decorators()70 const ArenaVector<Decorator *> &Decorators() const 71 { 72 return decorators_; 73 } 74 DecoratorsPtr()75 const ArenaVector<Decorator *> *DecoratorsPtr() const override 76 { 77 return &Decorators(); 78 } 79 SetTypeParameters(ir::TSTypeParameterDeclaration * typeParams)80 void SetTypeParameters(ir::TSTypeParameterDeclaration *typeParams) 81 { 82 typeParams_ = typeParams; 83 } 84 AddDecorators(ArenaVector<ir::Decorator * > && decorators)85 void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override 86 { 87 decorators_ = std::move(decorators); 88 } 89 CanHaveDecorator(bool inTs)90 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 91 { 92 return !inTs; 93 } 94 SetTypeParameterTypes(ArenaVector<checker::Type * > && typeParamTypes)95 void SetTypeParameterTypes(ArenaVector<checker::Type *> &&typeParamTypes) 96 { 97 typeParamTypes_ = std::move(typeParamTypes); 98 } 99 TypeParameterTypes()100 ArenaVector<checker::Type *> const &TypeParameterTypes() const 101 { 102 return typeParamTypes_; 103 } 104 Annotations()105 [[nodiscard]] ArenaVector<ir::AnnotationUsage *> &Annotations() noexcept 106 { 107 return annotations_; 108 } 109 Annotations()110 [[nodiscard]] const ArenaVector<ir::AnnotationUsage *> &Annotations() const noexcept 111 { 112 return annotations_; 113 } 114 SetAnnotations(ArenaVector<ir::AnnotationUsage * > && annotations)115 void SetAnnotations(ArenaVector<ir::AnnotationUsage *> &&annotations) 116 { 117 annotations_ = std::move(annotations); 118 for (AnnotationUsage *anno : annotations_) { 119 anno->SetParent(this); 120 } 121 } 122 123 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 124 void Iterate(const NodeTraverser &cb) const override; 125 void Dump(ir::AstDumper *dumper) const override; 126 void Dump(ir::SrcDumper *dumper) const override; 127 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 128 void Compile(compiler::ETSGen *etsg) const override; 129 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 130 checker::VerifiedType Check([[maybe_unused]] checker::ETSChecker *checker) override; 131 Accept(ASTVisitorT * v)132 void Accept(ASTVisitorT *v) override 133 { 134 v->Accept(this); 135 } 136 CleanUp()137 void CleanUp() override 138 { 139 AstNode::CleanUp(); 140 typeParamTypes_.clear(); 141 } 142 143 TSTypeAliasDeclaration *Construct(ArenaAllocator *allocator) override; 144 void CopyTo(AstNode *other) const override; 145 146 private: 147 bool RegisterUnexportedForDeclGen(ir::SrcDumper *dumper) const; 148 friend class SizeOfNodeTest; 149 ArenaVector<Decorator *> decorators_; 150 ArenaVector<AnnotationUsage *> annotations_; 151 Identifier *id_; 152 TSTypeParameterDeclaration *typeParams_; 153 ArenaVector<checker::Type *> typeParamTypes_; 154 }; 155 } // namespace ark::es2panda::ir 156 157 #endif 158