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 "etsNullishTypes.h"
17
18 #include "checker/ETSchecker.h"
19
20 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)21 void ETSUndefinedType::TransformChildren([[maybe_unused]] const NodeTransformer &cb,
22 [[maybe_unused]] std::string_view const transformationName)
23 {
24 for (auto *&it : VectorIterationGuard(Annotations())) {
25 if (auto *transformedNode = cb(it); it != transformedNode) {
26 it->SetTransformedNode(transformationName, transformedNode);
27 it = transformedNode->AsAnnotationUsage();
28 }
29 }
30 }
31
Iterate(const NodeTraverser & cb) const32 void ETSUndefinedType::Iterate([[maybe_unused]] const NodeTraverser &cb) const
33 {
34 for (auto *it : VectorIterationGuard(Annotations())) {
35 cb(it);
36 }
37 }
38
Dump(ir::AstDumper * dumper) const39 void ETSUndefinedType::Dump(ir::AstDumper *dumper) const
40 {
41 dumper->Add({{"type", "ETSUndefinedType"}, {"annotations", AstDumper::Optional(Annotations())}});
42 }
43
Dump(ir::SrcDumper * dumper) const44 void ETSUndefinedType::Dump(ir::SrcDumper *dumper) const
45 {
46 for (auto *anno : Annotations()) {
47 anno->Dump(dumper);
48 }
49 dumper->Add("undefined");
50 }
51
Compile(compiler::PandaGen * pg) const52 void ETSUndefinedType::Compile([[maybe_unused]] compiler::PandaGen *pg) const
53 {
54 ES2PANDA_UNREACHABLE();
55 }
56
Check(checker::TSChecker * checker)57 checker::Type *ETSUndefinedType::Check([[maybe_unused]] checker::TSChecker *checker)
58 {
59 ES2PANDA_UNREACHABLE();
60 }
61
Check(checker::ETSChecker * checker)62 checker::VerifiedType ETSUndefinedType::Check([[maybe_unused]] checker::ETSChecker *checker)
63 {
64 return {this, checker->GetAnalyzer()->Check(this)};
65 }
66
GetType(checker::ETSChecker * checker)67 checker::Type *ETSUndefinedType::GetType([[maybe_unused]] checker::ETSChecker *checker)
68 {
69 SetTsType(checker->GlobalETSUndefinedType());
70 return TsType();
71 }
72
Clone(ArenaAllocator * allocator,AstNode * parent)73 ETSUndefinedType *ETSUndefinedType::Clone(ArenaAllocator *allocator, AstNode *parent)
74 {
75 auto *const clone = allocator->New<ir::ETSUndefinedType>(allocator);
76 ES2PANDA_ASSERT(clone);
77
78 if (parent != nullptr) {
79 clone->SetParent(parent);
80 }
81
82 if (!Annotations().empty()) {
83 ArenaVector<AnnotationUsage *> annotationUsages {allocator->Adapter()};
84 for (auto *annotationUsage : Annotations()) {
85 annotationUsages.push_back(annotationUsage->Clone(allocator, clone)->AsAnnotationUsage());
86 }
87 clone->SetAnnotations(std::move(annotationUsages));
88 }
89
90 return clone;
91 }
92
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)93 void ETSNullType::TransformChildren([[maybe_unused]] const NodeTransformer &cb,
94 [[maybe_unused]] std::string_view const transformationName)
95 {
96 for (auto *&it : VectorIterationGuard(Annotations())) {
97 if (auto *transformedNode = cb(it); it != transformedNode) {
98 it->SetTransformedNode(transformationName, transformedNode);
99 it = transformedNode->AsAnnotationUsage();
100 }
101 }
102 }
103
Iterate(const NodeTraverser & cb) const104 void ETSNullType::Iterate([[maybe_unused]] const NodeTraverser &cb) const
105 {
106 for (auto *it : VectorIterationGuard(Annotations())) {
107 cb(it);
108 }
109 }
110
Dump(ir::AstDumper * dumper) const111 void ETSNullType::Dump(ir::AstDumper *dumper) const
112 {
113 dumper->Add({{"type", "ETSNullType"}, {"annotations", AstDumper::Optional(Annotations())}});
114 }
115
Dump(ir::SrcDumper * dumper) const116 void ETSNullType::Dump(ir::SrcDumper *dumper) const
117 {
118 for (auto *anno : Annotations()) {
119 anno->Dump(dumper);
120 }
121 dumper->Add("null");
122 }
123
Compile(compiler::PandaGen * pg) const124 void ETSNullType::Compile([[maybe_unused]] compiler::PandaGen *pg) const
125 {
126 ES2PANDA_UNREACHABLE();
127 }
128
Check(checker::TSChecker * checker)129 checker::Type *ETSNullType::Check([[maybe_unused]] checker::TSChecker *checker)
130 {
131 ES2PANDA_UNREACHABLE();
132 }
133
Check(checker::ETSChecker * checker)134 checker::VerifiedType ETSNullType::Check([[maybe_unused]] checker::ETSChecker *checker)
135 {
136 return {this, checker->GetAnalyzer()->Check(this)};
137 }
138
GetType(checker::ETSChecker * checker)139 checker::Type *ETSNullType::GetType([[maybe_unused]] checker::ETSChecker *checker)
140 {
141 SetTsType(checker->GlobalETSNullType());
142 return TsType();
143 }
144
Clone(ArenaAllocator * allocator,AstNode * parent)145 ETSNullType *ETSNullType::Clone(ArenaAllocator *allocator, AstNode *parent)
146 {
147 auto *const clone = allocator->New<ir::ETSNullType>(allocator);
148 if (parent != nullptr) {
149 clone->SetParent(parent);
150 }
151
152 if (!Annotations().empty()) {
153 ArenaVector<AnnotationUsage *> annotationUsages {allocator->Adapter()};
154 for (auto *annotationUsage : Annotations()) {
155 ES2PANDA_ASSERT(annotationUsage->Clone(allocator, clone));
156 annotationUsages.push_back(annotationUsage->Clone(allocator, clone)->AsAnnotationUsage());
157 }
158 clone->SetAnnotations(std::move(annotationUsages));
159 }
160
161 return clone;
162 }
163 } // namespace ark::es2panda::ir
164