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_STATEMENT_BLOCK_STATEMENT_H 17 #define ES2PANDA_IR_STATEMENT_BLOCK_STATEMENT_H 18 19 #include "ir/statement.h" 20 #include "utils/arena_containers.h" 21 22 namespace ark::es2panda::checker { 23 class ETSAnalyzer; 24 } // namespace ark::es2panda::checker 25 26 namespace ark::es2panda::ir { 27 class BlockStatement : public Statement { 28 public: BlockStatement(ArenaAllocator * allocator,ArenaVector<Statement * > && statementList)29 explicit BlockStatement(ArenaAllocator *allocator, ArenaVector<Statement *> &&statementList) 30 : Statement(AstNodeType::BLOCK_STATEMENT), 31 statements_(std::move(statementList)), 32 trailingBlocks_(allocator->Adapter()) 33 { 34 } 35 36 // NOTE (somas): this friend relationship can be removed once there are getters for private fields 37 friend class checker::ETSAnalyzer; 38 IsScopeBearer()39 [[nodiscard]] bool IsScopeBearer() const noexcept override 40 { 41 return true; 42 } 43 Scope()44 [[nodiscard]] varbinder::Scope *Scope() const noexcept override 45 { 46 return scope_; 47 } 48 SetScope(varbinder::Scope * scope)49 void SetScope(varbinder::Scope *scope) noexcept 50 { 51 scope_ = scope; 52 } 53 ClearScope()54 void ClearScope() noexcept override 55 { 56 scope_ = nullptr; 57 } 58 Statements()59 const ArenaVector<Statement *> &Statements() const 60 { 61 return statements_; 62 } 63 Statements()64 ArenaVector<Statement *> &Statements() 65 { 66 return statements_; 67 } 68 SetStatements(ArenaVector<Statement * > && statementList)69 void SetStatements(ArenaVector<Statement *> &&statementList) 70 { 71 statements_ = std::move(statementList); 72 73 for (auto *statement : statements_) { 74 statement->SetParent(this); 75 } 76 } 77 AddStatement(Statement * stmt)78 void AddStatement(Statement *stmt) 79 { 80 stmt->SetParent(this); 81 statements_.emplace_back(stmt); 82 } 83 AddStatements(ArenaVector<Statement * > & stmts)84 void AddStatements(ArenaVector<Statement *> &stmts) 85 { 86 for (auto *stmt : stmts) { 87 stmt->SetParent(this); 88 } 89 statements_.insert(statements_.end(), stmts.begin(), stmts.end()); 90 } 91 AddTrailingBlock(AstNode * stmt,BlockStatement * trailingBlock)92 void AddTrailingBlock(AstNode *stmt, BlockStatement *trailingBlock) 93 { 94 trailingBlocks_.emplace(stmt, trailingBlock); 95 } 96 97 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 98 99 AstNode *Clone(ArenaAllocator *const allocator, AstNode *const parent) override; 100 101 void Iterate(const NodeTraverser &cb) const override; 102 void Dump(ir::AstDumper *dumper) const override; 103 void Dump(ir::SrcDumper *dumper) const override; 104 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 105 void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; 106 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 107 checker::VerifiedType Check([[maybe_unused]] checker::ETSChecker *checker) override; 108 Accept(ASTVisitorT * v)109 void Accept(ASTVisitorT *v) override 110 { 111 v->Accept(this); 112 } 113 114 BlockStatement *Construct(ArenaAllocator *allocator) override; 115 void CopyTo(AstNode *other) const override; 116 117 private: 118 friend class SizeOfNodeTest; 119 varbinder::Scope *scope_ {}; 120 ArenaVector<Statement *> statements_; 121 ArenaUnorderedMap<AstNode *, BlockStatement *> trailingBlocks_; 122 }; 123 } // namespace ark::es2panda::ir 124 125 #endif 126