• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 - 2023 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 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 
24 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)25 void ConditionalExpression::TransformChildren(const NodeTransformer &cb)
26 {
27     test_ = cb(test_)->AsExpression();
28     consequent_ = cb(consequent_)->AsExpression();
29     alternate_ = cb(alternate_)->AsExpression();
30 }
31 
Iterate(const NodeTraverser & cb) const32 void ConditionalExpression::Iterate(const NodeTraverser &cb) const
33 {
34     cb(test_);
35     cb(consequent_);
36     cb(alternate_);
37 }
38 
Dump(ir::AstDumper * dumper) const39 void ConditionalExpression::Dump(ir::AstDumper *dumper) const
40 {
41     dumper->Add(
42         {{"type", "ConditionalExpression"}, {"test", test_}, {"consequent", consequent_}, {"alternate", alternate_}});
43 }
44 
Dump(ir::SrcDumper * dumper) const45 void ConditionalExpression::Dump(ir::SrcDumper *dumper) const
46 {
47     ASSERT(test_ != nullptr);
48     dumper->Add("(");
49     test_->Dump(dumper);
50     dumper->Add(" ? ");
51     if (consequent_ != nullptr) {
52         consequent_->Dump(dumper);
53     }
54     dumper->Add(" : ");
55     if (alternate_ != nullptr) {
56         alternate_->Dump(dumper);
57     }
58     dumper->Add(")");
59     if ((parent_ != nullptr) && (parent_->IsBlockStatement() || parent_->IsBlockExpression())) {
60         dumper->Add(";");
61         dumper->Endl();
62     }
63 }
64 
Compile(compiler::PandaGen * pg) const65 void ConditionalExpression::Compile(compiler::PandaGen *pg) const
66 {
67     pg->GetAstCompiler()->Compile(this);
68 }
69 
Compile(compiler::ETSGen * etsg) const70 void ConditionalExpression::Compile(compiler::ETSGen *etsg) const
71 {
72     etsg->GetAstCompiler()->Compile(this);
73 }
74 
Check(checker::TSChecker * checker)75 checker::Type *ConditionalExpression::Check(checker::TSChecker *checker)
76 {
77     return checker->GetAnalyzer()->Check(this);
78 }
79 
Check(checker::ETSChecker * checker)80 checker::Type *ConditionalExpression::Check(checker::ETSChecker *checker)
81 {
82     return checker->GetAnalyzer()->Check(this);
83 }
84 
85 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)86 ConditionalExpression *ConditionalExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
87 {
88     auto *const test = test_ != nullptr ? test_->Clone(allocator)->AsExpression() : nullptr;
89     auto *const consequent = consequent_ != nullptr ? consequent_->Clone(allocator)->AsExpression() : nullptr;
90     auto *const alternate = alternate_ != nullptr ? alternate_->Clone(allocator)->AsExpression() : nullptr;
91 
92     if (auto *const clone = allocator->New<ConditionalExpression>(test, consequent, alternate); clone != nullptr) {
93         if (test != nullptr) {
94             test->SetParent(clone);
95         }
96         if (consequent != nullptr) {
97             consequent->SetParent(clone);
98         }
99         if (alternate != nullptr) {
100             alternate->SetParent(clone);
101         }
102         if (parent != nullptr) {
103             clone->SetParent(parent);
104         }
105         return clone;
106     }
107 
108     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
109 }
110 }  // namespace panda::es2panda::ir
111