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 "tsAsExpression.h"
17
18#include "checker/TSchecker.h"
19#include "checker/ETSchecker.h"
20#include "compiler/core/ETSGen.h"
21#include "compiler/core/pandagen.h"
22
23namespace ark::es2panda::ir {
24Expression *TSAsExpression::Expr() noexcept
25{
26    return expression_;
27}
28
29void TSAsExpression::SetExpr(Expression *expr) noexcept
30{
31    expression_ = expr;
32    if (expression_ != nullptr) {
33        SetStart(expression_->Start());
34        expression_->SetParent(this);
35    }
36}
37
38void TSAsExpression::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
39{
40    if (auto *transformedNode = cb(expression_); expression_ != transformedNode) {
41        expression_->SetTransformedNode(transformationName, transformedNode);
42        expression_ = transformedNode->AsExpression();
43    }
44
45    if (auto *typeAnnotation = TypeAnnotation(); typeAnnotation != nullptr) {
46        if (auto *transformedNode = cb(typeAnnotation); typeAnnotation != transformedNode) {
47            typeAnnotation->SetTransformedNode(transformationName, transformedNode);
48            SetTsTypeAnnotation(static_cast<TypeNode *>(transformedNode));
49        }
50    }
51}
52
53void TSAsExpression::Iterate(const NodeTraverser &cb) const
54{
55    cb(expression_);
56    cb(TypeAnnotation());
57}
58
59void TSAsExpression::Dump(ir::AstDumper *dumper) const
60{
61    dumper->Add({{"type", "TSAsExpression"}, {"expression", expression_}, {"typeAnnotation", TypeAnnotation()}});
62}
63
64void TSAsExpression::Dump(ir::SrcDumper *dumper) const
65{
66    dumper->Add("(");
67    expression_->Dump(dumper);
68    dumper->Add(" as ");
69    TypeAnnotation()->Dump(dumper);
70    dumper->Add(")");
71}
72
73void TSAsExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const
74{
75    pg->GetAstCompiler()->Compile(this);
76}
77
78void TSAsExpression::Compile(compiler::ETSGen *etsg) const
79{
80    etsg->GetAstCompiler()->Compile(this);
81}
82
83checker::Type *TSAsExpression::Check([[maybe_unused]] checker::TSChecker *checker)
84{
85    return checker->GetAnalyzer()->Check(this);
86}
87
88checker::Type *TSAsExpression::Check(checker::ETSChecker *const checker)
89{
90    return checker->GetAnalyzer()->Check(this);
91}
92
93TSAsExpression *TSAsExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
94{
95    auto *const expression = expression_ != nullptr ? expression_->Clone(allocator, nullptr)->AsExpression() : nullptr;
96    auto *typeAnnotation = TypeAnnotation();
97    if (typeAnnotation != nullptr) {
98        typeAnnotation = typeAnnotation->Clone(allocator, nullptr);
99    }
100
101    if (auto *const clone = allocator->New<TSAsExpression>(expression, typeAnnotation, isConst_); clone != nullptr) {
102        if (expression != nullptr) {
103            expression->SetParent(clone);
104        }
105
106        if (typeAnnotation != nullptr) {
107            typeAnnotation->SetParent(clone);
108        }
109
110        clone->SetTsType(TsType());
111        if (parent != nullptr) {
112            clone->SetParent(parent);
113        }
114
115        clone->SetRange(Range());
116        return clone;
117    }
118
119    throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
120}
121}  // namespace ark::es2panda::ir
122