1 /** 2 * Copyright (c) 2021 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 panda::es2panda::checker { 22 class ETSAnalyzer; 23 } // namespace panda::es2panda::checker 24 25 namespace panda::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 bool IsScopeBearer() const override 39 { 40 return true; 41 } 42 Scope()43 varbinder::Scope *Scope() const override 44 { 45 return scope_; 46 } 47 SetScope(varbinder::Scope * scope)48 void SetScope(varbinder::Scope *scope) 49 { 50 scope_ = scope; 51 } 52 Statements()53 const ArenaVector<Statement *> &Statements() const 54 { 55 return statements_; 56 } 57 Statements()58 ArenaVector<Statement *> &Statements() 59 { 60 return statements_; 61 } 62 AddTrailingBlock(AstNode * stmt,BlockStatement * trailingBlock)63 void AddTrailingBlock(AstNode *stmt, BlockStatement *trailingBlock) 64 { 65 trailingBlocks_.emplace(stmt, trailingBlock); 66 } 67 68 void TransformChildren(const NodeTransformer &cb) override; SetReturnType(checker::ETSChecker * checker,checker::Type * type)69 void SetReturnType(checker::ETSChecker *checker, checker::Type *type) override 70 { 71 for (auto *statement : statements_) { 72 if (statement != nullptr) { 73 statement->SetReturnType(checker, type); 74 } 75 } 76 } 77 78 void Iterate(const NodeTraverser &cb) const override; 79 void Dump(ir::AstDumper *dumper) const override; 80 void Dump(ir::SrcDumper *dumper) const override; 81 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 82 void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; 83 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 84 checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; 85 Accept(ASTVisitorT * v)86 void Accept(ASTVisitorT *v) override 87 { 88 v->Accept(this); 89 } 90 91 private: 92 varbinder::Scope *scope_ {}; 93 ArenaVector<Statement *> statements_; 94 ArenaUnorderedMap<AstNode *, BlockStatement *> trailingBlocks_; 95 }; 96 } // namespace panda::es2panda::ir 97 98 #endif 99