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 compiler::LocalRegScope lrs(pg, scope_);
41
42 for (const auto *it : statements_) {
43 it->Compile(pg);
44 }
45 }
46
Check(checker::Checker * checker) const47 checker::Type *BlockStatement::Check(checker::Checker *checker) const
48 {
49 checker::ScopeContext scopeCtx(checker, scope_);
50
51 for (const auto *it : statements_) {
52 it->Check(checker);
53 }
54
55 return nullptr;
56 }
57
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)58 void BlockStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
59 {
60 auto scopeCtx = binder::LexicalScope<binder::Scope>::Enter(binder, scope_);
61
62 for (auto iter = statements_.begin(); iter != statements_.end();) {
63 auto newStatements = cb(*iter);
64 if (std::holds_alternative<ir::AstNode *>(newStatements)) {
65 auto statement = std::get<ir::AstNode *>(newStatements);
66 if (statement == *iter) {
67 iter++;
68 } else if (statement == nullptr) {
69 iter = statements_.erase(iter);
70 } else {
71 *iter = statement->AsStatement();
72 iter++;
73 }
74 } else {
75 auto statements = std::get<std::vector<ir::AstNode *>>(newStatements);
76 for (auto *it : statements) {
77 iter = statements_.insert(iter, it->AsStatement());
78 iter++;
79 }
80 iter = statements_.erase(iter);
81 }
82 }
83 }
84
85 } // namespace panda::es2panda::ir
86