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
23 namespace ark::es2panda::ir {
Expr()24 Expression *TSAsExpression::Expr() noexcept
25 {
26 return expression_;
27 }
28
SetExpr(Expression * expr)29 void TSAsExpression::SetExpr(Expression *expr) noexcept
30 {
31 expression_ = expr;
32 if (expression_ != nullptr) {
33 SetStart(expression_->Start());
34 expression_->SetParent(this);
35 }
36 }
37
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)38 void 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
Iterate(const NodeTraverser & cb) const53 void TSAsExpression::Iterate(const NodeTraverser &cb) const
54 {
55 cb(expression_);
56 cb(TypeAnnotation());
57 }
58
Dump(ir::AstDumper * dumper) const59 void TSAsExpression::Dump(ir::AstDumper *dumper) const
60 {
61 dumper->Add({{"type", "TSAsExpression"}, {"expression", expression_}, {"typeAnnotation", TypeAnnotation()}});
62 }
63
Dump(ir::SrcDumper * dumper) const64 void TSAsExpression::Dump(ir::SrcDumper *dumper) const
65 {
66 expression_->Dump(dumper);
67 dumper->Add(" as ");
68 TypeAnnotation()->Dump(dumper);
69 }
70
Compile(compiler::PandaGen * pg) const71 void TSAsExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const
72 {
73 pg->GetAstCompiler()->Compile(this);
74 }
75
Compile(compiler::ETSGen * etsg) const76 void TSAsExpression::Compile(compiler::ETSGen *etsg) const
77 {
78 etsg->GetAstCompiler()->Compile(this);
79 }
80
Check(checker::TSChecker * checker)81 checker::Type *TSAsExpression::Check([[maybe_unused]] checker::TSChecker *checker)
82 {
83 return checker->GetAnalyzer()->Check(this);
84 }
85
Check(checker::ETSChecker * const checker)86 checker::Type *TSAsExpression::Check(checker::ETSChecker *const checker)
87 {
88 return checker->GetAnalyzer()->Check(this);
89 }
90
Clone(ArenaAllocator * const allocator,AstNode * const parent)91 TSAsExpression *TSAsExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
92 {
93 auto *const expression = expression_ != nullptr ? expression_->Clone(allocator, nullptr)->AsExpression() : nullptr;
94 auto *typeAnnotation = TypeAnnotation();
95 if (typeAnnotation != nullptr) {
96 typeAnnotation = typeAnnotation->Clone(allocator, nullptr);
97 }
98
99 if (auto *const clone = allocator->New<TSAsExpression>(expression, typeAnnotation, isConst_); clone != nullptr) {
100 if (expression != nullptr) {
101 expression->SetParent(clone);
102 }
103
104 if (typeAnnotation != nullptr) {
105 typeAnnotation->SetParent(clone);
106 }
107
108 clone->SetTsType(TsType());
109 if (parent != nullptr) {
110 clone->SetParent(parent);
111 }
112
113 clone->SetRange(Range());
114 return clone;
115 }
116
117 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
118 }
119 } // namespace ark::es2panda::ir
120