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 "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 panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)29 void AssertStatement::TransformChildren(const NodeTransformer &cb)
30 {
31 test_ = cb(test_)->AsExpression();
32
33 if (second_ != nullptr) {
34 second_ = cb(second_)->AsExpression();
35 }
36 }
37
Iterate(const NodeTraverser & cb) const38 void AssertStatement::Iterate(const NodeTraverser &cb) const
39 {
40 cb(test_);
41
42 if (second_ != nullptr) {
43 cb(second_);
44 }
45 }
46
Dump(ir::AstDumper * dumper) const47 void AssertStatement::Dump(ir::AstDumper *dumper) const
48 {
49 dumper->Add({{"type", "AssertStatement"}, {"test", test_}, {"second", AstDumper::Nullish(second_)}});
50 }
51
Dump(ir::SrcDumper * dumper) const52 void AssertStatement::Dump(ir::SrcDumper *dumper) const
53 {
54 ASSERT(test_);
55 dumper->Add("assert(");
56 test_->Dump(dumper);
57 dumper->Add(")");
58 if (parent_->IsStatement()) {
59 dumper->Add(";");
60 }
61 }
62
Compile(compiler::PandaGen * pg) const63 void AssertStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const
64 {
65 pg->GetAstCompiler()->Compile(this);
66 }
67
Compile(compiler::ETSGen * etsg) const68 void AssertStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
69 {
70 etsg->GetAstCompiler()->Compile(this);
71 }
72
Check(checker::TSChecker * checker)73 checker::Type *AssertStatement::Check([[maybe_unused]] checker::TSChecker *checker)
74 {
75 return checker->GetAnalyzer()->Check(this);
76 }
77
Check(checker::ETSChecker * checker)78 checker::Type *AssertStatement::Check([[maybe_unused]] checker::ETSChecker *checker)
79 {
80 return checker->GetAnalyzer()->Check(this);
81 }
82 } // namespace panda::es2panda::ir
83