1/** 2 * Copyright (c) 2021 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 <compiler/base/condition.h> 19#include <compiler/core/pandagen.h> 20#include <typescript/checker.h> 21#include <ir/astDump.h> 22 23namespace panda::es2panda::ir { 24 25void ConditionalExpression::Iterate(const NodeTraverser &cb) const 26{ 27 cb(test_); 28 cb(consequent_); 29 cb(alternate_); 30} 31 32void ConditionalExpression::Dump(ir::AstDumper *dumper) const 33{ 34 dumper->Add( 35 {{"type", "ConditionalExpression"}, {"test", test_}, {"consequent", consequent_}, {"alternate", alternate_}}); 36} 37 38void ConditionalExpression::Compile(compiler::PandaGen *pg) const 39{ 40 auto *falseLabel = pg->AllocLabel(); 41 auto *endLabel = pg->AllocLabel(); 42 43 compiler::Condition::Compile(pg, test_, falseLabel); 44 consequent_->Compile(pg); 45 pg->Branch(this, endLabel); 46 pg->SetLabel(this, falseLabel); 47 alternate_->Compile(pg); 48 pg->SetLabel(this, endLabel); 49} 50 51checker::Type *ConditionalExpression::Check(checker::Checker *checker) const 52{ 53 checker::Type *testType = test_->Check(checker); 54 55 checker->CheckTruthinessOfType(testType, test_->Start()); 56 checker->CheckTestingKnownTruthyCallableOrAwaitableType(test_, testType, consequent_); 57 58 checker::Type *consequentType = consequent_->Check(checker); 59 checker::Type *alternateType = alternate_->Check(checker); 60 61 return checker->CreateUnionType({consequentType, alternateType}); 62} 63 64void ConditionalExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) 65{ 66 test_ = std::get<ir::AstNode *>(cb(test_))->AsExpression(); 67 consequent_ = std::get<ir::AstNode *>(cb(consequent_))->AsExpression(); 68 alternate_ = std::get<ir::AstNode *>(cb(alternate_))->AsExpression(); 69} 70 71} // namespace panda::es2panda::ir 72