1 /**
2 * Copyright (c) 2021 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 panda::es2panda::ir {
Iterate(const NodeTraverser & cb) const27 void TSQualifiedName::Iterate(const NodeTraverser &cb) const
28 {
29 cb(left_);
30 cb(right_);
31 }
32
TransformChildren(const NodeTransformer & cb)33 void TSQualifiedName::TransformChildren(const NodeTransformer &cb)
34 {
35 left_ = cb(left_)->AsExpression();
36 right_ = cb(right_)->AsIdentifier();
37 }
38
Dump(ir::AstDumper * dumper) const39 void TSQualifiedName::Dump(ir::AstDumper *dumper) const
40 {
41 dumper->Add({{"type", "TSQualifiedName"}, {"left", left_}, {"right", right_}});
42 }
43
Dump(ir::SrcDumper * dumper) const44 void TSQualifiedName::Dump(ir::SrcDumper *dumper) const
45 {
46 dumper->Add("TSQualifiedName");
47 }
48
Compile(compiler::PandaGen * pg) const49 void TSQualifiedName::Compile([[maybe_unused]] compiler::PandaGen *pg) const
50 {
51 pg->GetAstCompiler()->Compile(this);
52 }
Compile(compiler::ETSGen * etsg) const53 void TSQualifiedName::Compile(compiler::ETSGen *etsg) const
54 {
55 etsg->GetAstCompiler()->Compile(this);
56 }
57
Check(checker::TSChecker * checker)58 checker::Type *TSQualifiedName::Check([[maybe_unused]] checker::TSChecker *checker)
59 {
60 return checker->GetAnalyzer()->Check(this);
61 }
62
Check(checker::ETSChecker * checker)63 checker::Type *TSQualifiedName::Check(checker::ETSChecker *checker)
64 {
65 return checker->GetAnalyzer()->Check(this);
66 }
67
ToString(ArenaAllocator * allocator) const68 util::StringView TSQualifiedName::ToString(ArenaAllocator *allocator) const
69 {
70 util::UString packageName(allocator);
71
72 const auto *iter = this;
73
74 while (iter->Left()->IsTSQualifiedName()) {
75 iter = iter->Left()->AsTSQualifiedName();
76 }
77
78 packageName.Append(iter->Left()->AsIdentifier()->Name());
79
80 const ir::AstNode *parent = iter;
81
82 while (parent != nullptr && parent->IsTSQualifiedName()) {
83 packageName.Append('.');
84 packageName.Append(parent->AsTSQualifiedName()->Right()->AsIdentifier()->Name());
85 parent = parent->Parent();
86 }
87
88 return packageName.View();
89 }
90
BaseToString(ArenaAllocator * allocator) const91 util::StringView TSQualifiedName::BaseToString(ArenaAllocator *allocator) const
92 {
93 util::UString packageName(allocator);
94
95 const auto *iter = this;
96
97 while (iter->Left()->IsTSQualifiedName()) {
98 iter = iter->Left()->AsTSQualifiedName();
99 }
100
101 packageName.Append(iter->Left()->AsIdentifier()->Name());
102
103 const ir::AstNode *parent = iter->Parent();
104
105 while (parent != nullptr && parent->IsTSQualifiedName()) {
106 packageName.Append('.');
107 packageName.Append(parent->AsTSQualifiedName()->Right()->AsIdentifier()->Name());
108 parent = parent->Parent();
109 }
110
111 return packageName.View();
112 }
113
114 template <typename T>
ResolveLeftMostQualifiedNameImpl(T self)115 static T ResolveLeftMostQualifiedNameImpl(T self)
116 {
117 auto *iter = self;
118
119 while (iter->Left()->IsTSQualifiedName()) {
120 iter = iter->Left()->AsTSQualifiedName();
121 }
122
123 return iter;
124 }
125
ResolveLeftMostQualifiedName()126 ir::TSQualifiedName *TSQualifiedName::ResolveLeftMostQualifiedName()
127 {
128 return ResolveLeftMostQualifiedNameImpl(this);
129 }
130
ResolveLeftMostQualifiedName() const131 const ir::TSQualifiedName *TSQualifiedName::ResolveLeftMostQualifiedName() const
132 {
133 return ResolveLeftMostQualifiedNameImpl(this);
134 }
135 } // namespace panda::es2panda::ir
136