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 "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 24namespace ark::es2panda::ir { 25void ConditionalExpression::TransformChildren(const NodeTransformer &cb, std::string_view transformationName) 26{ 27 if (auto *transformedNode = cb(test_); test_ != transformedNode) { 28 test_->SetTransformedNode(transformationName, transformedNode); 29 test_ = transformedNode->AsExpression(); 30 } 31 32 if (auto *transformedNode = cb(consequent_); consequent_ != transformedNode) { 33 consequent_->SetTransformedNode(transformationName, transformedNode); 34 consequent_ = transformedNode->AsExpression(); 35 } 36 37 if (auto *transformedNode = cb(alternate_); alternate_ != transformedNode) { 38 alternate_->SetTransformedNode(transformationName, transformedNode); 39 alternate_ = transformedNode->AsExpression(); 40 } 41} 42 43void ConditionalExpression::Iterate(const NodeTraverser &cb) const 44{ 45 cb(test_); 46 cb(consequent_); 47 cb(alternate_); 48} 49 50void ConditionalExpression::Dump(ir::AstDumper *dumper) const 51{ 52 dumper->Add( 53 {{"type", "ConditionalExpression"}, {"test", test_}, {"consequent", consequent_}, {"alternate", alternate_}}); 54} 55 56void ConditionalExpression::Dump(ir::SrcDumper *dumper) const 57{ 58 ASSERT(test_ != nullptr); 59 dumper->Add("("); 60 test_->Dump(dumper); 61 dumper->Add(" ? "); 62 if (consequent_ != nullptr) { 63 consequent_->Dump(dumper); 64 } 65 dumper->Add(" : "); 66 if (alternate_ != nullptr) { 67 alternate_->Dump(dumper); 68 } 69 dumper->Add(")"); 70 if ((parent_ != nullptr) && (parent_->IsBlockStatement() || parent_->IsBlockExpression())) { 71 dumper->Add(";"); 72 dumper->Endl(); 73 } 74} 75 76void ConditionalExpression::Compile(compiler::PandaGen *pg) const 77{ 78 pg->GetAstCompiler()->Compile(this); 79} 80 81void ConditionalExpression::Compile(compiler::ETSGen *etsg) const 82{ 83 etsg->GetAstCompiler()->Compile(this); 84} 85 86checker::Type *ConditionalExpression::Check(checker::TSChecker *checker) 87{ 88 return checker->GetAnalyzer()->Check(this); 89} 90 91checker::Type *ConditionalExpression::Check(checker::ETSChecker *checker) 92{ 93 return checker->GetAnalyzer()->Check(this); 94} 95 96ConditionalExpression *ConditionalExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) 97{ 98 auto *const test = test_->Clone(allocator, nullptr)->AsExpression(); 99 auto *const consequent = consequent_->Clone(allocator, nullptr)->AsExpression(); 100 auto *const alternate = alternate_->Clone(allocator, nullptr)->AsExpression(); 101 102 if (auto *const clone = allocator->New<ConditionalExpression>(test, consequent, alternate); clone != nullptr) { 103 test->SetParent(clone); 104 consequent->SetParent(clone); 105 alternate->SetParent(clone); 106 107 if (parent != nullptr) { 108 clone->SetParent(parent); 109 } 110 111 clone->SetRange(Range()); 112 return clone; 113 } 114 115 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); 116} 117} // namespace ark::es2panda::ir 118