• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 #include "ir/ts/tsQualifiedName.h"
25 #include "ir/ets/etsTypeReferencePart.h"
26 
27 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)28 void ETSTypeReference::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
29 {
30     if (auto *transformedNode = cb(part_); part_ != transformedNode) {
31         part_->SetTransformedNode(transformationName, transformedNode);
32         part_ = transformedNode->AsETSTypeReferencePart();
33     }
34 }
35 
Iterate(const NodeTraverser & cb) const36 void ETSTypeReference::Iterate(const NodeTraverser &cb) const
37 {
38     cb(part_);
39 }
40 
BaseName() const41 ir::Identifier *ETSTypeReference::BaseName() const
42 {
43     ir::ETSTypeReferencePart *partIter = part_;
44 
45     while (partIter->Previous() != nullptr) {
46         partIter = partIter->Previous();
47     }
48 
49     ir::Expression *baseName = partIter->Name();
50 
51     if (baseName->IsIdentifier()) {
52         return baseName->AsIdentifier();
53     }
54 
55     ir::TSQualifiedName *nameIter = baseName->AsTSQualifiedName();
56 
57     while (nameIter->Left()->IsTSQualifiedName()) {
58         nameIter = nameIter->Left()->AsTSQualifiedName();
59     }
60 
61     return nameIter->Left()->AsIdentifier();
62 }
63 
Dump(ir::AstDumper * dumper) const64 void ETSTypeReference::Dump(ir::AstDumper *dumper) const
65 {
66     dumper->Add({{"type", "ETSTypeReference"}, {"part", part_}});
67 }
68 
Dump(ir::SrcDumper * dumper) const69 void ETSTypeReference::Dump(ir::SrcDumper *dumper) const
70 {
71     ASSERT(part_ != nullptr);
72     part_->Dump(dumper);
73 }
74 
Compile(compiler::PandaGen * pg) const75 void ETSTypeReference::Compile(compiler::PandaGen *pg) const
76 {
77     pg->GetAstCompiler()->Compile(this);
78 }
Compile(compiler::ETSGen * etsg) const79 void ETSTypeReference::Compile(compiler::ETSGen *etsg) const
80 {
81     etsg->GetAstCompiler()->Compile(this);
82 }
83 
Check(checker::TSChecker * checker)84 checker::Type *ETSTypeReference::Check(checker::TSChecker *checker)
85 {
86     return checker->GetAnalyzer()->Check(this);
87 }
88 
Check(checker::ETSChecker * checker)89 checker::Type *ETSTypeReference::Check(checker::ETSChecker *checker)
90 {
91     return checker->GetAnalyzer()->Check(this);
92 }
93 
GetType(checker::ETSChecker * checker)94 checker::Type *ETSTypeReference::GetType(checker::ETSChecker *checker)
95 {
96     if (TsType() == nullptr) {
97         SetTsType(part_->GetType(checker));
98     }
99     return TsType();
100 }
101 
Clone(ArenaAllocator * const allocator,AstNode * const parent)102 ETSTypeReference *ETSTypeReference::Clone(ArenaAllocator *const allocator, AstNode *const parent)
103 {
104     auto *const partClone = part_ != nullptr ? part_->Clone(allocator, nullptr)->AsETSTypeReferencePart() : nullptr;
105 
106     if (auto *const clone = allocator->New<ETSTypeReference>(partClone); clone != nullptr) {
107         if (partClone != nullptr) {
108             partClone->SetParent(clone);
109         }
110 
111         clone->flags_ = flags_;
112 
113         if (parent != nullptr) {
114             clone->SetParent(parent);
115         }
116 
117         clone->SetRange(Range());
118         return clone;
119     }
120 
121     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
122 }
123 }  // namespace ark::es2panda::ir
124