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
28 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)29 void 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
Iterate(const NodeTraverser & cb) const44 void AssertStatement::Iterate(const NodeTraverser &cb) const
45 {
46 cb(test_);
47
48 if (second_ != nullptr) {
49 cb(second_);
50 }
51 }
52
Dump(ir::AstDumper * dumper) const53 void AssertStatement::Dump(ir::AstDumper *dumper) const
54 {
55 dumper->Add({{"type", "AssertStatement"}, {"test", test_}, {"second", AstDumper::Nullish(second_)}});
56 }
57
Dump(ir::SrcDumper * dumper) const58 void AssertStatement::Dump(ir::SrcDumper *dumper) const
59 {
60 ASSERT(test_);
61 dumper->Add("assert(");
62 test_->Dump(dumper);
63 dumper->Add(")");
64 if (parent_->IsStatement()) {
65 dumper->Add(";");
66 }
67 }
68
Compile(compiler::PandaGen * pg) const69 void AssertStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const
70 {
71 pg->GetAstCompiler()->Compile(this);
72 }
73
Compile(compiler::ETSGen * etsg) const74 void AssertStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
75 {
76 etsg->GetAstCompiler()->Compile(this);
77 }
78
Check(checker::TSChecker * checker)79 checker::Type *AssertStatement::Check([[maybe_unused]] checker::TSChecker *checker)
80 {
81 return checker->GetAnalyzer()->Check(this);
82 }
83
Check(checker::ETSChecker * checker)84 checker::Type *AssertStatement::Check([[maybe_unused]] checker::ETSChecker *checker)
85 {
86 return checker->GetAnalyzer()->Check(this);
87 }
88 } // namespace ark::es2panda::ir
89