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 "etsTypeReference.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
23 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)24 void ETSTypeReference::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
25 {
26 if (auto *transformedNode = cb(part_); part_ != transformedNode) {
27 part_->SetTransformedNode(transformationName, transformedNode);
28 part_ = transformedNode->AsETSTypeReferencePart();
29 }
30 for (auto *&it : VectorIterationGuard(Annotations())) {
31 if (auto *transformedNode = cb(it); it != transformedNode) {
32 it->SetTransformedNode(transformationName, transformedNode);
33 it = transformedNode->AsAnnotationUsage();
34 }
35 }
36 }
37
Iterate(const NodeTraverser & cb) const38 void ETSTypeReference::Iterate(const NodeTraverser &cb) const
39 {
40 cb(part_);
41 for (auto *it : VectorIterationGuard(Annotations())) {
42 cb(it);
43 }
44 }
45
BaseName() const46 ir::Identifier *ETSTypeReference::BaseName() const
47 {
48 ir::ETSTypeReferencePart *partIter = part_;
49
50 while (partIter->Previous() != nullptr) {
51 partIter = partIter->Previous();
52 }
53
54 ir::Expression *baseName = partIter->Name();
55
56 if (baseName->IsIdentifier()) {
57 return baseName->AsIdentifier();
58 }
59
60 ir::TSQualifiedName *nameIter = baseName->AsTSQualifiedName();
61
62 while (nameIter->Left()->IsTSQualifiedName()) {
63 nameIter = nameIter->Left()->AsTSQualifiedName();
64 }
65
66 return nameIter->Left()->AsIdentifier();
67 }
68
Dump(ir::AstDumper * dumper) const69 void ETSTypeReference::Dump(ir::AstDumper *dumper) const
70 {
71 dumper->Add({{"type", "ETSTypeReference"}, {"part", part_}, {"annotations", AstDumper::Optional(Annotations())}});
72 }
73
Dump(ir::SrcDumper * dumper) const74 void ETSTypeReference::Dump(ir::SrcDumper *dumper) const
75 {
76 for (auto *anno : Annotations()) {
77 anno->Dump(dumper);
78 }
79 ES2PANDA_ASSERT(part_ != nullptr);
80 part_->Dump(dumper);
81 }
82
Compile(compiler::PandaGen * pg) const83 void ETSTypeReference::Compile(compiler::PandaGen *pg) const
84 {
85 pg->GetAstCompiler()->Compile(this);
86 }
Compile(compiler::ETSGen * etsg) const87 void ETSTypeReference::Compile(compiler::ETSGen *etsg) const
88 {
89 etsg->GetAstCompiler()->Compile(this);
90 }
91
Check(checker::TSChecker * checker)92 checker::Type *ETSTypeReference::Check(checker::TSChecker *checker)
93 {
94 return checker->GetAnalyzer()->Check(this);
95 }
96
Check(checker::ETSChecker * checker)97 checker::VerifiedType ETSTypeReference::Check(checker::ETSChecker *checker)
98 {
99 return {this, checker->GetAnalyzer()->Check(this)};
100 }
GetType(checker::ETSChecker * checker)101 checker::Type *ETSTypeReference::GetType(checker::ETSChecker *checker)
102 {
103 if (TsType() != nullptr) {
104 return TsType();
105 }
106 auto *type = part_->GetType(checker);
107 if (IsReadonlyType()) {
108 type = checker->GetReadonlyType(type);
109 }
110 return SetTsType(type);
111 }
112
Clone(ArenaAllocator * const allocator,AstNode * const parent)113 ETSTypeReference *ETSTypeReference::Clone(ArenaAllocator *const allocator, AstNode *const parent)
114 {
115 ETSTypeReferencePart *partClone = nullptr;
116 if (part_ != nullptr) {
117 auto *const clone = part_->Clone(allocator, nullptr);
118 ES2PANDA_ASSERT(clone != nullptr);
119 partClone = clone->AsETSTypeReferencePart();
120 }
121 auto *const clone = allocator->New<ETSTypeReference>(partClone, allocator);
122 ES2PANDA_ASSERT(clone != nullptr);
123
124 if (partClone != nullptr) {
125 partClone->SetParent(clone);
126 }
127
128 clone->flags_ = flags_;
129
130 if (parent != nullptr) {
131 clone->SetParent(parent);
132 }
133
134 if (!Annotations().empty()) {
135 ArenaVector<AnnotationUsage *> annotationUsages {allocator->Adapter()};
136 for (auto *annotationUsage : Annotations()) {
137 auto *annotationClone = annotationUsage->Clone(allocator, clone);
138 ES2PANDA_ASSERT(annotationClone != nullptr);
139 annotationUsages.push_back(annotationClone->AsAnnotationUsage());
140 }
141 clone->SetAnnotations(std::move(annotationUsages));
142 }
143
144 clone->SetRange(Range());
145 return clone;
146 }
147
Construct(ArenaAllocator * allocator)148 ETSTypeReference *ETSTypeReference::Construct(ArenaAllocator *allocator)
149 {
150 return allocator->New<ETSTypeReference>(nullptr, allocator);
151 }
152
CopyTo(AstNode * other) const153 void ETSTypeReference::CopyTo(AstNode *other) const
154 {
155 auto otherImpl = other->AsETSTypeReference();
156
157 otherImpl->part_ = part_;
158
159 TypeNode::CopyTo(other);
160 }
161
162 } // namespace ark::es2panda::ir
163