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