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