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 "namedType.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/expressions/identifier.h"
22 #include "ir/ts/tsTypeParameterInstantiation.h"
23 #include "ir/astDump.h"
24 #include "ir/srcDump.h"
25
26 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)27 void NamedType::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
28 {
29 if (auto *transformedNode = cb(name_); name_ != transformedNode) {
30 name_->SetTransformedNode(transformationName, transformedNode);
31 name_ = transformedNode->AsIdentifier();
32 }
33
34 if (typeParams_ != nullptr) {
35 if (auto *transformedNode = cb(typeParams_); typeParams_ != transformedNode) {
36 typeParams_->SetTransformedNode(transformationName, transformedNode);
37 typeParams_ = transformedNode->AsTSTypeParameterInstantiation();
38 }
39 }
40
41 if (next_ != nullptr) {
42 if (auto *transformedNode = cb(next_); next_ != transformedNode) {
43 next_->SetTransformedNode(transformationName, transformedNode);
44 next_ = transformedNode->AsNamedType();
45 }
46 }
47
48 for (auto *&it : VectorIterationGuard(Annotations())) {
49 if (auto *transformedNode = cb(it); it != transformedNode) {
50 it->SetTransformedNode(transformationName, transformedNode);
51 it = transformedNode->AsAnnotationUsage();
52 }
53 }
54 }
55
Iterate(const NodeTraverser & cb) const56 void NamedType::Iterate(const NodeTraverser &cb) const
57 {
58 cb(name_);
59
60 if (typeParams_ != nullptr) {
61 cb(typeParams_);
62 }
63
64 if (next_ != nullptr) {
65 cb(next_);
66 }
67 for (auto *it : VectorIterationGuard(Annotations())) {
68 cb(it);
69 }
70 }
71
Dump(AstDumper * dumper) const72 void NamedType::Dump(AstDumper *dumper) const
73 {
74 dumper->Add({{"type", "NamedType"},
75 {"name", name_},
76 {"typeParameters", AstDumper::Optional(typeParams_)},
77 {"next", AstDumper::Optional(next_)},
78 {"isNullable", nullable_},
79 {"annotations", AstDumper::Optional(Annotations())}});
80 }
81
Dump(SrcDumper * dumper) const82 void NamedType::Dump(SrcDumper *dumper) const
83 {
84 for (auto *anno : Annotations()) {
85 anno->Dump(dumper);
86 }
87 dumper->Add("NamedType");
88 }
89
Compile(compiler::PandaGen * pg) const90 void NamedType::Compile(compiler::PandaGen *pg) const
91 {
92 pg->GetAstCompiler()->Compile(this);
93 }
94
Compile(compiler::ETSGen * etsg) const95 void NamedType::Compile(compiler::ETSGen *etsg) const
96 {
97 etsg->GetAstCompiler()->Compile(this);
98 }
99
Check(checker::TSChecker * checker)100 checker::Type *NamedType::Check(checker::TSChecker *checker)
101 {
102 return checker->GetAnalyzer()->Check(this);
103 }
104
Check(checker::ETSChecker * checker)105 checker::VerifiedType NamedType::Check(checker::ETSChecker *checker)
106 {
107 return {this, checker->GetAnalyzer()->Check(this)};
108 }
109 } // namespace ark::es2panda::ir
110