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 "tsNonNullExpression.h"
17
18#include "checker/ETSchecker.h"
19#include "checker/TSchecker.h"
20#include "compiler/core/ETSGen.h"
21#include "compiler/core/pandagen.h"
22#include "ir/astDump.h"
23#include "ir/srcDump.h"
24
25namespace ark::es2panda::ir {
26void TSNonNullExpression::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
27{
28    if (auto *transformedNode = cb(expr_); expr_ != transformedNode) {
29        expr_->SetTransformedNode(transformationName, transformedNode);
30        expr_ = transformedNode->AsExpression();
31    }
32}
33
34void TSNonNullExpression::Iterate(const NodeTraverser &cb) const
35{
36    cb(expr_);
37}
38
39void TSNonNullExpression::Dump(ir::AstDumper *dumper) const
40{
41    dumper->Add({{"type", "TSNonNullExpression"}, {"expression", expr_}});
42}
43
44void TSNonNullExpression::Dump(ir::SrcDumper *dumper) const
45{
46    ASSERT(expr_ != nullptr);
47    expr_->Dump(dumper);
48    dumper->Add("!");
49}
50
51void TSNonNullExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const
52{
53    pg->GetAstCompiler()->Compile(this);
54}
55
56void TSNonNullExpression::Compile(compiler::ETSGen *etsg) const
57{
58    etsg->GetAstCompiler()->Compile(this);
59}
60
61checker::Type *TSNonNullExpression::Check([[maybe_unused]] checker::TSChecker *checker)
62{
63    return checker->GetAnalyzer()->Check(this);
64}
65
66checker::Type *TSNonNullExpression::Check(checker::ETSChecker *checker)
67{
68    return checker->GetAnalyzer()->Check(this);
69}
70
71TSNonNullExpression *TSNonNullExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
72{
73    auto *const expr = expr_->Clone(allocator, nullptr)->AsExpression();
74
75    if (auto *const clone = allocator->New<TSNonNullExpression>(expr); clone != nullptr) {
76        expr->SetParent(clone);
77        if (parent != nullptr) {
78            clone->SetParent(parent);
79        }
80        return clone;
81    }
82    throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
83}
84
85}  // namespace ark::es2panda::ir
86