• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 
23 namespace ark::es2panda::ir {
TSQualifiedName(Expression * left,Identifier * right,ArenaAllocator * allocator)24 TSQualifiedName::TSQualifiedName(Expression *left, Identifier *right, ArenaAllocator *allocator)
25     : Expression(AstNodeType::TS_QUALIFIED_NAME), left_(left), right_(right), allocator_(allocator)
26 {
27     ES2PANDA_ASSERT(left_ != nullptr && right_ != nullptr);
28 }
29 
TSQualifiedName(Tag const tag,TSQualifiedName const & other,ArenaAllocator * allocator)30 TSQualifiedName::TSQualifiedName([[maybe_unused]] Tag const tag, TSQualifiedName const &other,
31                                  ArenaAllocator *allocator)
32     : Expression(static_cast<Expression const &>(other))
33 {
34     left_ = other.left_->Clone(allocator, this)->AsExpression();
35     right_ = other.right_->Clone(allocator, this)->AsIdentifier();
36     allocator_ = allocator;
37 }
38 
Iterate(const NodeTraverser & cb) const39 void TSQualifiedName::Iterate(const NodeTraverser &cb) const
40 {
41     cb(left_);
42     cb(right_);
43 }
44 
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)45 void TSQualifiedName::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
46 {
47     if (auto *transformedNode = cb(left_); left_ != transformedNode) {
48         left_->SetTransformedNode(transformationName, transformedNode);
49         left_ = transformedNode->AsExpression();
50     }
51 
52     if (auto *transformedNode = cb(right_); right_ != transformedNode) {
53         right_->SetTransformedNode(transformationName, transformedNode);
54         right_ = transformedNode->AsIdentifier();
55     }
56 }
57 
Dump(ir::AstDumper * dumper) const58 void TSQualifiedName::Dump(ir::AstDumper *dumper) const
59 {
60     dumper->Add({{"type", "TSQualifiedName"}, {"left", left_}, {"right", right_}});
61 }
62 
Dump(ir::SrcDumper * dumper) const63 void TSQualifiedName::Dump(ir::SrcDumper *dumper) const
64 {
65     left_->Dump(dumper);
66     dumper->Add(".");
67     right_->Dump(dumper);
68 }
69 
Compile(compiler::PandaGen * pg) const70 void TSQualifiedName::Compile([[maybe_unused]] compiler::PandaGen *pg) const
71 {
72     pg->GetAstCompiler()->Compile(this);
73 }
Compile(compiler::ETSGen * etsg) const74 void TSQualifiedName::Compile(compiler::ETSGen *etsg) const
75 {
76     etsg->GetAstCompiler()->Compile(this);
77 }
78 
Check(checker::TSChecker * checker)79 checker::Type *TSQualifiedName::Check([[maybe_unused]] checker::TSChecker *checker)
80 {
81     return checker->GetAnalyzer()->Check(this);
82 }
83 
Check(checker::ETSChecker * checker)84 checker::VerifiedType TSQualifiedName::Check(checker::ETSChecker *checker)
85 {
86     return {this, checker->GetAnalyzer()->Check(this)};
87 }
88 
Name() const89 util::StringView TSQualifiedName::Name() const
90 {
91     util::UString packageName(allocator_);
92 
93     const auto *iter = this;
94 
95     while (iter->Left()->IsTSQualifiedName()) {
96         iter = iter->Left()->AsTSQualifiedName();
97     }
98 
99     packageName.Append(iter->Left()->AsIdentifier()->Name());
100 
101     const ir::AstNode *parent = iter;
102 
103     while (parent != nullptr && parent->IsTSQualifiedName()) {
104         packageName.Append('.');
105         packageName.Append(parent->AsTSQualifiedName()->Right()->AsIdentifier()->Name());
106         parent = parent->Parent();
107     }
108 
109     return packageName.View();
110 }
111 
112 template <typename T>
ResolveLeftMostQualifiedNameImpl(T self)113 static T ResolveLeftMostQualifiedNameImpl(T self)
114 {
115     auto *iter = self;
116 
117     while (iter->Left()->IsTSQualifiedName()) {
118         iter = iter->Left()->AsTSQualifiedName();
119     }
120 
121     return iter;
122 }
123 
ResolveLeftMostQualifiedName()124 ir::TSQualifiedName *TSQualifiedName::ResolveLeftMostQualifiedName()
125 {
126     return ResolveLeftMostQualifiedNameImpl(this);
127 }
128 
ResolveLeftMostQualifiedName() const129 const ir::TSQualifiedName *TSQualifiedName::ResolveLeftMostQualifiedName() const
130 {
131     return ResolveLeftMostQualifiedNameImpl(this);
132 }
133 
Clone(ArenaAllocator * const allocator,AstNode * const parent)134 TSQualifiedName *TSQualifiedName::Clone(ArenaAllocator *const allocator, AstNode *const parent)
135 {
136     auto *const clone = allocator->New<TSQualifiedName>(Tag {}, *this, allocator);
137     ES2PANDA_ASSERT(clone != nullptr);
138     if (parent != nullptr) {
139         clone->SetParent(parent);
140     }
141     clone->SetRange(Range());
142     return clone;
143 }
144 }  // namespace ark::es2panda::ir
145