• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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     dumper->Add("(");
67     expression_->Dump(dumper);
68     dumper->Add(" as ");
69     TypeAnnotation()->Dump(dumper);
70     dumper->Add(")");
71 }
72 
Compile(compiler::PandaGen * pg) const73 void TSAsExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const
74 {
75     pg->GetAstCompiler()->Compile(this);
76 }
77 
Compile(compiler::ETSGen * etsg) const78 void TSAsExpression::Compile(compiler::ETSGen *etsg) const
79 {
80     etsg->GetAstCompiler()->Compile(this);
81 }
82 
Check(checker::TSChecker * checker)83 checker::Type *TSAsExpression::Check([[maybe_unused]] checker::TSChecker *checker)
84 {
85     return checker->GetAnalyzer()->Check(this);
86 }
87 
Check(checker::ETSChecker * const checker)88 checker::VerifiedType TSAsExpression::Check(checker::ETSChecker *const checker)
89 {
90     return {this, checker->GetAnalyzer()->Check(this)};
91 }
92 
Clone(ArenaAllocator * const allocator,AstNode * const parent)93 TSAsExpression *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     auto *const clone = allocator->New<TSAsExpression>(expression, typeAnnotation, isConst_);
102     ES2PANDA_ASSERT(clone != nullptr);
103 
104     if (expression != nullptr) {
105         expression->SetParent(clone);
106     }
107 
108     if (typeAnnotation != nullptr) {
109         typeAnnotation->SetParent(clone);
110     }
111 
112     if (Start().Program()->Extension() != ScriptExtension::ETS) {
113         clone->SetTsType(TsType());
114     }
115 
116     if (parent != nullptr) {
117         clone->SetParent(parent);
118     }
119 
120     clone->SetRange(Range());
121     return clone;
122 }
123 }  // namespace ark::es2panda::ir
124