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 "tsQualifiedName.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 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 #include "ir/expressions/identifier.h"
25
26 namespace ark::es2panda::ir {
TSQualifiedName([[maybe_unused]] Tag const tag, TSQualifiedName const &other, ArenaAllocator *allocator)27 TSQualifiedName::TSQualifiedName([[maybe_unused]] Tag const tag, TSQualifiedName const &other,
28 ArenaAllocator *allocator)
29 : Expression(static_cast<Expression const &>(other))
30 {
31 left_ = other.left_ != nullptr ? other.left_->Clone(allocator, this)->AsExpression() : nullptr;
32 right_ = other.right_ != nullptr ? other.right_->Clone(allocator, this)->AsIdentifier() : nullptr;
33 }
34
Iterate(const NodeTraverser &cb) const35 void TSQualifiedName::Iterate(const NodeTraverser &cb) const
36 {
37 cb(left_);
38 cb(right_);
39 }
40
TransformChildren(const NodeTransformer &cb, std::string_view transformationName)41 void TSQualifiedName::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
42 {
43 if (auto *transformedNode = cb(left_); left_ != transformedNode) {
44 left_->SetTransformedNode(transformationName, transformedNode);
45 left_ = transformedNode->AsExpression();
46 }
47
48 if (auto *transformedNode = cb(right_); right_ != transformedNode) {
49 right_->SetTransformedNode(transformationName, transformedNode);
50 right_ = transformedNode->AsIdentifier();
51 }
52 }
53
Dump(ir::AstDumper *dumper) const54 void TSQualifiedName::Dump(ir::AstDumper *dumper) const
55 {
56 dumper->Add({{"type", "TSQualifiedName"}, {"left", left_}, {"right", right_}});
57 }
58
Dump(ir::SrcDumper *dumper) const59 void TSQualifiedName::Dump(ir::SrcDumper *dumper) const
60 {
61 dumper->Add("TSQualifiedName");
62 }
63
Compile([[maybe_unused]] compiler::PandaGen *pg) const64 void TSQualifiedName::Compile([[maybe_unused]] compiler::PandaGen *pg) const
65 {
66 pg->GetAstCompiler()->Compile(this);
67 }
Compile(compiler::ETSGen *etsg) const68 void TSQualifiedName::Compile(compiler::ETSGen *etsg) const
69 {
70 etsg->GetAstCompiler()->Compile(this);
71 }
72
Check([[maybe_unused]] checker::TSChecker *checker)73 checker::Type *TSQualifiedName::Check([[maybe_unused]] checker::TSChecker *checker)
74 {
75 return checker->GetAnalyzer()->Check(this);
76 }
77
Check(checker::ETSChecker *checker)78 checker::Type *TSQualifiedName::Check(checker::ETSChecker *checker)
79 {
80 return checker->GetAnalyzer()->Check(this);
81 }
82
ToString(ArenaAllocator *allocator) const83 util::StringView TSQualifiedName::ToString(ArenaAllocator *allocator) const
84 {
85 util::UString packageName(allocator);
86
87 const auto *iter = this;
88
89 while (iter->Left()->IsTSQualifiedName()) {
90 iter = iter->Left()->AsTSQualifiedName();
91 }
92
93 packageName.Append(iter->Left()->AsIdentifier()->Name());
94
95 const ir::AstNode *parent = iter;
96
97 while (parent != nullptr && parent->IsTSQualifiedName()) {
98 packageName.Append('.');
99 packageName.Append(parent->AsTSQualifiedName()->Right()->AsIdentifier()->Name());
100 parent = parent->Parent();
101 }
102
103 return packageName.View();
104 }
105
BaseToString(ArenaAllocator *allocator) const106 util::StringView TSQualifiedName::BaseToString(ArenaAllocator *allocator) const
107 {
108 util::UString packageName(allocator);
109
110 const auto *iter = this;
111
112 while (iter->Left()->IsTSQualifiedName()) {
113 iter = iter->Left()->AsTSQualifiedName();
114 }
115
116 packageName.Append(iter->Left()->AsIdentifier()->Name());
117
118 const ir::AstNode *parent = iter->Parent();
119
120 while (parent != nullptr && parent->IsTSQualifiedName()) {
121 packageName.Append('.');
122 packageName.Append(parent->AsTSQualifiedName()->Right()->AsIdentifier()->Name());
123 parent = parent->Parent();
124 }
125
126 return packageName.View();
127 }
128
129 template <typename T>
ResolveLeftMostQualifiedNameImpl(T self)130 static T ResolveLeftMostQualifiedNameImpl(T self)
131 {
132 auto *iter = self;
133
134 while (iter->Left()->IsTSQualifiedName()) {
135 iter = iter->Left()->AsTSQualifiedName();
136 }
137
138 return iter;
139 }
140
ResolveLeftMostQualifiedName()141 ir::TSQualifiedName *TSQualifiedName::ResolveLeftMostQualifiedName()
142 {
143 return ResolveLeftMostQualifiedNameImpl(this);
144 }
145
ResolveLeftMostQualifiedName() const146 const ir::TSQualifiedName *TSQualifiedName::ResolveLeftMostQualifiedName() const
147 {
148 return ResolveLeftMostQualifiedNameImpl(this);
149 }
150
Clone(ArenaAllocator *const allocator, AstNode *const parent)151 TSQualifiedName *TSQualifiedName::Clone(ArenaAllocator *const allocator, AstNode *const parent)
152 {
153 if (auto *const clone = allocator->New<TSQualifiedName>(Tag {}, *this, allocator); clone != nullptr) {
154 if (parent != nullptr) {
155 clone->SetParent(parent);
156 }
157
158 clone->SetRange(Range());
159 return clone;
160 }
161
162 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
163 }
164 } // namespace ark::es2panda::ir
165