1 /**
2  * Copyright (c) 2021-2024 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 ark::es2panda::ir {
TransformChildren(const NodeTransformer &cb, std::string_view transformationName)25 void LabelledStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
26 {
27     if (auto *transformedNode = cb(ident_); ident_ != transformedNode) {
28         ident_->SetTransformedNode(transformationName, transformedNode);
29         ident_ = transformedNode->AsIdentifier();
30     }
31 
32     if (auto *transformedNode = cb(body_); body_ != transformedNode) {
33         body_->SetTransformedNode(transformationName, transformedNode);
34         body_ = transformedNode->AsStatement();
35     }
36 }
37 
Iterate(const NodeTraverser &cb) const38 void LabelledStatement::Iterate(const NodeTraverser &cb) const
39 {
40     cb(ident_);
41     cb(body_);
42 }
43 
Dump(ir::AstDumper *dumper) const44 void LabelledStatement::Dump(ir::AstDumper *dumper) const
45 {
46     dumper->Add({{"type", "LabelledStatement"}, {"label", ident_}, {"body", body_}});
47 }
48 
Dump(ir::SrcDumper *dumper) const49 void LabelledStatement::Dump(ir::SrcDumper *dumper) const
50 {
51     ASSERT(ident_ != nullptr);
52     ident_->Dump(dumper);
53     dumper->Add(":");
54     dumper->Endl();
55     body_->Dump(dumper);
56 }
57 
GetReferencedStatement() const58 const ir::AstNode *LabelledStatement::GetReferencedStatement() const
59 {
60     const auto *iter = body_;
61     while (iter->IsLabelledStatement()) {
62         iter = iter->AsLabelledStatement()->Body();
63     }
64 
65     switch (iter->Type()) {
66         case ir::AstNodeType::DO_WHILE_STATEMENT:
67         case ir::AstNodeType::SWITCH_STATEMENT:
68         case ir::AstNodeType::FOR_UPDATE_STATEMENT:
69         case ir::AstNodeType::FOR_IN_STATEMENT:
70         case ir::AstNodeType::WHILE_STATEMENT: {
71             return iter;
72         }
73         default: {
74             return this;
75         }
76     }
77 }
78 
Compile(compiler::PandaGen *pg) const79 void LabelledStatement::Compile(compiler::PandaGen *pg) const
80 {
81     pg->GetAstCompiler()->Compile(this);
82 }
83 
Compile(compiler::ETSGen *etsg) const84 void LabelledStatement::Compile(compiler::ETSGen *etsg) const
85 {
86     etsg->GetAstCompiler()->Compile(this);
87 }
88 
Check(checker::TSChecker *checker)89 checker::Type *LabelledStatement::Check(checker::TSChecker *checker)
90 {
91     return checker->GetAnalyzer()->Check(this);
92 }
93 
Check(checker::ETSChecker *checker)94 checker::Type *LabelledStatement::Check(checker::ETSChecker *checker)
95 {
96     return checker->GetAnalyzer()->Check(this);
97 }
98 }  // namespace ark::es2panda::ir
99