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_MODULE_DECLARATION_H 17 #define ES2PANDA_IR_TS_MODULE_DECLARATION_H 18 19 #include "varbinder/scope.h" 20 #include "ir/statement.h" 21 22 namespace ark::es2panda::ir { 23 class Expression; 24 25 class TSModuleDeclaration : public Statement { 26 public: 27 // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init) 28 struct ConstructorFlags { 29 bool declare; 30 bool global; 31 bool isExternalAmbient; 32 }; 33 // NOLINTEND(cppcoreguidelines-pro-type-member-init) 34 TSModuleDeclaration(ArenaAllocator * allocator,Expression * name,Statement * body,ConstructorFlags && flags)35 explicit TSModuleDeclaration(ArenaAllocator *allocator, Expression *name, Statement *body, ConstructorFlags &&flags) 36 : Statement(AstNodeType::TS_MODULE_DECLARATION), 37 decorators_(allocator->Adapter()), 38 name_(name), 39 body_(body), 40 declare_(flags.declare), 41 global_(flags.global), 42 isExternalAmbient_(flags.isExternalAmbient) 43 { 44 } 45 IsScopeBearer()46 [[nodiscard]] bool IsScopeBearer() const noexcept override 47 { 48 return true; 49 } 50 Scope()51 [[nodiscard]] varbinder::LocalScope *Scope() const noexcept override 52 { 53 return scope_; 54 } 55 SetScope(varbinder::LocalScope * scope)56 void SetScope(varbinder::LocalScope *scope) 57 { 58 ASSERT(scope_ == nullptr); 59 scope_ = scope; 60 } 61 ClearScope()62 void ClearScope() noexcept override 63 { 64 scope_ = nullptr; 65 } 66 Name()67 const Expression *Name() const 68 { 69 return name_; 70 } 71 Body()72 const Statement *Body() const 73 { 74 return body_; 75 } 76 Declare()77 bool Declare() const 78 { 79 return declare_; 80 } 81 Global()82 bool Global() const 83 { 84 return global_; 85 } 86 IsExternalOrAmbient()87 bool IsExternalOrAmbient() const 88 { 89 return isExternalAmbient_; 90 } 91 AddDecorators(ArenaVector<ir::Decorator * > && decorators)92 void AddDecorators([[maybe_unused]] ArenaVector<ir::Decorator *> &&decorators) override 93 { 94 decorators_ = std::move(decorators); 95 } 96 CanHaveDecorator(bool inTs)97 bool CanHaveDecorator([[maybe_unused]] bool inTs) const override 98 { 99 return !inTs; 100 } 101 102 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 103 void Iterate(const NodeTraverser &cb) const override; 104 void Dump(ir::AstDumper *dumper) const override; 105 void Dump(ir::SrcDumper *dumper) const override; 106 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 107 void Compile(compiler::ETSGen *etsg) const override; 108 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 109 checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; 110 Accept(ASTVisitorT * v)111 void Accept(ASTVisitorT *v) override 112 { 113 v->Accept(this); 114 } 115 116 private: 117 ArenaVector<Decorator *> decorators_; 118 varbinder::LocalScope *scope_ {nullptr}; 119 Expression *name_; 120 Statement *body_; 121 bool declare_; 122 bool global_; 123 bool isExternalAmbient_; 124 }; 125 } // namespace ark::es2panda::ir 126 127 #endif 128