1 /**
2 * Copyright (c) 2021-2025 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 "conditionalExpression.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/pandagen.h"
20 #include "compiler/core/ETSGen.h"
21
22 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)23 void ConditionalExpression::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
24 {
25 if (auto *transformedNode = cb(test_); test_ != transformedNode) {
26 test_->SetTransformedNode(transformationName, transformedNode);
27 test_ = transformedNode->AsExpression();
28 }
29
30 if (auto *transformedNode = cb(consequent_); consequent_ != transformedNode) {
31 consequent_->SetTransformedNode(transformationName, transformedNode);
32 consequent_ = transformedNode->AsExpression();
33 }
34
35 if (auto *transformedNode = cb(alternate_); alternate_ != transformedNode) {
36 alternate_->SetTransformedNode(transformationName, transformedNode);
37 alternate_ = transformedNode->AsExpression();
38 }
39 }
40
Iterate(const NodeTraverser & cb) const41 void ConditionalExpression::Iterate(const NodeTraverser &cb) const
42 {
43 cb(test_);
44 cb(consequent_);
45 cb(alternate_);
46 }
47
Dump(ir::AstDumper * dumper) const48 void ConditionalExpression::Dump(ir::AstDumper *dumper) const
49 {
50 dumper->Add(
51 {{"type", "ConditionalExpression"}, {"test", test_}, {"consequent", consequent_}, {"alternate", alternate_}});
52 }
53
Dump(ir::SrcDumper * dumper) const54 void ConditionalExpression::Dump(ir::SrcDumper *dumper) const
55 {
56 ES2PANDA_ASSERT(test_ != nullptr);
57 dumper->Add("(");
58 test_->Dump(dumper);
59 dumper->Add(" ? ");
60 if (consequent_ != nullptr) {
61 consequent_->Dump(dumper);
62 }
63 dumper->Add(" : ");
64 if (alternate_ != nullptr) {
65 alternate_->Dump(dumper);
66 }
67 dumper->Add(")");
68 if ((parent_ != nullptr) && (parent_->IsBlockStatement() || parent_->IsBlockExpression())) {
69 dumper->Add(";");
70 dumper->Endl();
71 }
72 }
73
Compile(compiler::PandaGen * pg) const74 void ConditionalExpression::Compile(compiler::PandaGen *pg) const
75 {
76 pg->GetAstCompiler()->Compile(this);
77 }
78
Compile(compiler::ETSGen * etsg) const79 void ConditionalExpression::Compile(compiler::ETSGen *etsg) const
80 {
81 etsg->GetAstCompiler()->Compile(this);
82 }
83
Check(checker::TSChecker * checker)84 checker::Type *ConditionalExpression::Check(checker::TSChecker *checker)
85 {
86 return checker->GetAnalyzer()->Check(this);
87 }
88
Check(checker::ETSChecker * checker)89 checker::VerifiedType ConditionalExpression::Check(checker::ETSChecker *checker)
90 {
91 return {this, checker->GetAnalyzer()->Check(this)};
92 }
93
Clone(ArenaAllocator * const allocator,AstNode * const parent)94 ConditionalExpression *ConditionalExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
95 {
96 auto *const test = test_->Clone(allocator, nullptr)->AsExpression();
97 auto *const consequent = consequent_->Clone(allocator, nullptr)->AsExpression();
98 auto *const alternate = alternate_->Clone(allocator, nullptr)->AsExpression();
99
100 auto *const clone = allocator->New<ConditionalExpression>(test, consequent, alternate);
101
102 test->SetParent(clone);
103 consequent->SetParent(clone);
104 alternate->SetParent(clone);
105
106 if (parent != nullptr) {
107 clone->SetParent(parent);
108 }
109
110 clone->SetRange(Range());
111 return clone;
112 }
113 } // namespace ark::es2panda::ir
114