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 "assertStatement.h"
17
18#include "varbinder/ETSBinder.h"
19#include "compiler/base/condition.h"
20#include "compiler/core/pandagen.h"
21#include "compiler/core/ETSGen.h"
22#include "checker/ETSchecker.h"
23#include "checker/TSchecker.h"
24#include "ir/astDump.h"
25#include "ir/srcDump.h"
26#include "ir/expression.h"
27
28namespace ark::es2panda::ir {
29void AssertStatement::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
30{
31    if (auto *transformedNode = cb(test_); test_ != transformedNode) {
32        test_->SetTransformedNode(transformationName, transformedNode);
33        test_ = transformedNode->AsExpression();
34    }
35
36    if (second_ != nullptr) {
37        if (auto *transformedNode = cb(second_); second_ != transformedNode) {
38            second_->SetTransformedNode(transformationName, transformedNode);
39            second_ = transformedNode->AsExpression();
40        }
41    }
42}
43
44void AssertStatement::Iterate(const NodeTraverser &cb) const
45{
46    cb(test_);
47
48    if (second_ != nullptr) {
49        cb(second_);
50    }
51}
52
53void AssertStatement::Dump(ir::AstDumper *dumper) const
54{
55    dumper->Add({{"type", "AssertStatement"}, {"test", test_}, {"second", AstDumper::Nullish(second_)}});
56}
57
58void AssertStatement::Dump(ir::SrcDumper *dumper) const
59{
60    ASSERT(test_);
61    dumper->Add("assert(");
62    test_->Dump(dumper);
63    dumper->Add(")");
64
65    if (second_ != nullptr) {
66        dumper->Add(": ");
67        second_->Dump(dumper);
68    }
69
70    if (parent_->IsStatement()) {
71        dumper->Add(";");
72    }
73}
74
75void AssertStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const
76{
77    pg->GetAstCompiler()->Compile(this);
78}
79
80void AssertStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
81{
82    etsg->GetAstCompiler()->Compile(this);
83}
84
85checker::Type *AssertStatement::Check([[maybe_unused]] checker::TSChecker *checker)
86{
87    return checker->GetAnalyzer()->Check(this);
88}
89
90checker::Type *AssertStatement::Check([[maybe_unused]] checker::ETSChecker *checker)
91{
92    return checker->GetAnalyzer()->Check(this);
93}
94}  // namespace ark::es2panda::ir
95