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_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 global; 30 bool isExternalAmbient; 31 }; 32 // NOLINTEND(cppcoreguidelines-pro-type-member-init) 33 TSModuleDeclaration(ArenaAllocator * allocator,Expression * name,Statement * body,ConstructorFlags && flags)34 explicit TSModuleDeclaration(ArenaAllocator *allocator, Expression *name, Statement *body, ConstructorFlags &&flags) 35 : Statement(AstNodeType::TS_MODULE_DECLARATION), 36 decorators_(allocator->Adapter()), 37 name_(name), 38 body_(body), 39 global_(flags.global), 40 isExternalAmbient_(flags.isExternalAmbient) 41 { 42 } 43 IsScopeBearer()44 [[nodiscard]] bool IsScopeBearer() const noexcept override 45 { 46 return true; 47 } 48 Scope()49 [[nodiscard]] varbinder::LocalScope *Scope() const noexcept override 50 { 51 return scope_; 52 } 53 SetScope(varbinder::LocalScope * scope)54 void SetScope(varbinder::LocalScope *scope) 55 { 56 ES2PANDA_ASSERT(scope_ == nullptr); 57 scope_ = scope; 58 } 59 ClearScope()60 void ClearScope() noexcept override 61 { 62 scope_ = nullptr; 63 } 64 Name()65 const Expression *Name() const 66 { 67 return name_; 68 } 69 Body()70 const Statement *Body() const 71 { 72 return body_; 73 } 74 Global()75 bool Global() const 76 { 77 return global_; 78 } 79 IsExternalOrAmbient()80 bool IsExternalOrAmbient() const 81 { 82 return isExternalAmbient_; 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 95 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 96 void Iterate(const NodeTraverser &cb) const override; 97 void Dump(ir::AstDumper *dumper) const override; 98 void Dump(ir::SrcDumper *dumper) const override; 99 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 100 void Compile(compiler::ETSGen *etsg) const override; 101 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 102 checker::VerifiedType Check([[maybe_unused]] checker::ETSChecker *checker) override; 103 Accept(ASTVisitorT * v)104 void Accept(ASTVisitorT *v) override 105 { 106 v->Accept(this); 107 } 108 109 private: 110 ArenaVector<Decorator *> decorators_; 111 varbinder::LocalScope *scope_ {nullptr}; 112 Expression *name_; 113 Statement *body_; 114 bool global_; 115 bool isExternalAmbient_; 116 }; 117 } // namespace ark::es2panda::ir 118 119 #endif 120