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 #include "blockStatement.h"
17 
18 #include <binder/binder.h>
19 #include <binder/scope.h>
20 #include <compiler/core/regScope.h>
21 #include <typescript/checker.h>
22 #include <ir/astDump.h>
23 
24 namespace panda::es2panda::ir {
25 
Iterate(const NodeTraverser &cb) const26 void BlockStatement::Iterate(const NodeTraverser &cb) const
27 {
28     for (auto *it : statements_) {
29         cb(it);
30     }
31 }
32 
Dump(ir::AstDumper *dumper) const33 void BlockStatement::Dump(ir::AstDumper *dumper) const
34 {
35     dumper->Add({{"type", IsProgram() ? "Program" : "BlockStatement"}, {"statements", statements_}});
36 }
37 
Compile(compiler::PandaGen *pg) const38 void BlockStatement::Compile(compiler::PandaGen *pg) const
39 {
40     if (scope_ != nullptr) {
41         compiler::LocalRegScope lrs(pg, scope_);
42 
43         for (const auto *it : statements_) {
44             it->Compile(pg);
45         }
46     } else {
47         for (const auto *it : statements_) {
48             it->Compile(pg);
49         }
50     }
51 }
52 
Check(checker::Checker *checker) const53 checker::Type *BlockStatement::Check(checker::Checker *checker) const
54 {
55     auto scopeCtx = checker::ScopeContext(checker, scope_ != nullptr ? scope_ : checker->Scope());
56 
57     for (const auto *it : statements_) {
58         it->Check(checker);
59     }
60 
61     return nullptr;
62 }
63 
UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)64 void BlockStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
65 {
66     auto scopeCtx = binder::LexicalScope<binder::Scope>::Enter(binder,
67         scope_ != nullptr ? scope_ : binder->GetScope());
68 
69     UpdateForMultipleTransformedStatements(cb, statements_);
70 }
71 
AddStatementAtPos(size_t insertPos, Statement *statement)72 void BlockStatement::AddStatementAtPos(size_t insertPos, Statement *statement)
73 {
74     ASSERT(insertPos <= statements_.size());
75     statements_.insert(statements_.begin() + insertPos, statement);
76 }
77 
78 }  // namespace panda::es2panda::ir
79