1 /**
2 * Copyright (c) 2021-2022 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 "labelledStatement.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23
24 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)25 void LabelledStatement::TransformChildren(const NodeTransformer &cb)
26 {
27 ident_ = cb(ident_)->AsIdentifier();
28 body_ = cb(body_)->AsStatement();
29 }
30
Iterate(const NodeTraverser & cb) const31 void LabelledStatement::Iterate(const NodeTraverser &cb) const
32 {
33 cb(ident_);
34 cb(body_);
35 }
36
Dump(ir::AstDumper * dumper) const37 void LabelledStatement::Dump(ir::AstDumper *dumper) const
38 {
39 dumper->Add({{"type", "LabelledStatement"}, {"label", ident_}, {"body", body_}});
40 }
41
Dump(ir::SrcDumper * dumper) const42 void LabelledStatement::Dump(ir::SrcDumper *dumper) const
43 {
44 ASSERT(ident_ != nullptr);
45 ident_->Dump(dumper);
46 dumper->Add(":");
47 dumper->Endl();
48 body_->Dump(dumper);
49 }
50
GetReferencedStatement() const51 const ir::AstNode *LabelledStatement::GetReferencedStatement() const
52 {
53 const auto *iter = body_;
54 while (iter->IsLabelledStatement()) {
55 iter = iter->AsLabelledStatement()->Body();
56 }
57
58 switch (iter->Type()) {
59 case ir::AstNodeType::DO_WHILE_STATEMENT:
60 case ir::AstNodeType::SWITCH_STATEMENT:
61 case ir::AstNodeType::FOR_UPDATE_STATEMENT:
62 case ir::AstNodeType::FOR_IN_STATEMENT:
63 case ir::AstNodeType::WHILE_STATEMENT: {
64 return iter;
65 }
66 default: {
67 return this;
68 }
69 }
70 }
71
Compile(compiler::PandaGen * pg) const72 void LabelledStatement::Compile(compiler::PandaGen *pg) const
73 {
74 pg->GetAstCompiler()->Compile(this);
75 }
76
Compile(compiler::ETSGen * etsg) const77 void LabelledStatement::Compile(compiler::ETSGen *etsg) const
78 {
79 etsg->GetAstCompiler()->Compile(this);
80 }
81
Check(checker::TSChecker * checker)82 checker::Type *LabelledStatement::Check(checker::TSChecker *checker)
83 {
84 return checker->GetAnalyzer()->Check(this);
85 }
86
Check(checker::ETSChecker * checker)87 checker::Type *LabelledStatement::Check(checker::ETSChecker *checker)
88 {
89 return checker->GetAnalyzer()->Check(this);
90 }
91 } // namespace panda::es2panda::ir
92