• 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 "annotationUsage.h"
17 #include "checker/TSchecker.h"
18 #include "compiler/core/ETSGen.h"
19 #include "compiler/core/pandagen.h"
20 
21 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)22 void AnnotationUsage::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
23 {
24     if (auto *transformedNode = cb(expr_); expr_ != transformedNode) {
25         expr_->SetTransformedNode(transformationName, transformedNode);
26         expr_ = transformedNode->AsIdentifier();
27     }
28 
29     for (auto *&it : VectorIterationGuard(properties_)) {
30         if (auto *transformedNode = cb(it); it != transformedNode) {
31             it->SetTransformedNode(transformationName, transformedNode);
32             it = transformedNode;
33         }
34     }
35 }
Iterate(const NodeTraverser & cb) const36 void AnnotationUsage::Iterate(const NodeTraverser &cb) const
37 {
38     if (expr_ != nullptr) {
39         cb(expr_);
40     }
41 
42     for (auto *it : VectorIterationGuard(properties_)) {
43         cb(it);
44     }
45 }
46 
Dump(ir::AstDumper * dumper) const47 void AnnotationUsage::Dump(ir::AstDumper *dumper) const
48 {
49     dumper->Add({{"expr_", expr_}, {"properties", properties_}});
50 }
Dump(ir::SrcDumper * dumper) const51 void AnnotationUsage::Dump(ir::SrcDumper *dumper) const
52 {
53     ES2PANDA_ASSERT(expr_ != nullptr);
54     dumper->Add("@");
55     expr_->Dump(dumper);
56     dumper->Add("(");
57 
58     if (!properties_.empty()) {
59         dumper->Add("{");
60         for (auto elem : properties_) {
61             ES2PANDA_ASSERT(elem->AsClassProperty()->Id() != nullptr);
62             dumper->Add(elem->AsClassProperty()->Id()->Name().Mutf8());
63             dumper->Add(":");
64             elem->AsClassProperty()->Value()->Dump(dumper);
65             if (elem != properties_.back()) {
66                 dumper->Add(",");
67             }
68         }
69         dumper->Add("}");
70     }
71     dumper->Add(") ");
72 }
73 
Clone(ArenaAllocator * const allocator,AstNode * const parent)74 AnnotationUsage *AnnotationUsage::Clone(ArenaAllocator *const allocator, AstNode *const parent)
75 {
76     auto *const expr = expr_ != nullptr ? expr_->Clone(allocator, nullptr)->AsExpression() : nullptr;
77     auto *const clone = allocator->New<AnnotationUsage>(expr, allocator);
78     ES2PANDA_ASSERT(clone != nullptr);
79 
80     if (expr != nullptr) {
81         expr->SetParent(clone);
82     }
83 
84     if (parent != nullptr) {
85         clone->SetParent(parent);
86     }
87 
88     for (auto *property : properties_) {
89         clone->AddProperty(property->Clone(allocator, clone));
90     }
91 
92     clone->SetRange(range_);
93     clone->SetScope(propertiesScope_);
94 
95     return clone;
96 }
97 
Compile(compiler::PandaGen * pg) const98 void AnnotationUsage::Compile(compiler::PandaGen *pg) const
99 {
100     pg->GetAstCompiler()->Compile(this);
101 }
102 
Compile(compiler::ETSGen * etsg) const103 void AnnotationUsage::Compile(compiler::ETSGen *etsg) const
104 {
105     etsg->GetAstCompiler()->Compile(this);
106 }
107 
Check(checker::TSChecker * checker)108 checker::Type *AnnotationUsage::Check(checker::TSChecker *checker)
109 {
110     return checker->GetAnalyzer()->Check(this);
111 }
112 
Check(checker::ETSChecker * checker)113 checker::VerifiedType AnnotationUsage::Check(checker::ETSChecker *checker)
114 {
115     return {this, checker->GetAnalyzer()->Check(this)};
116 }
117 
GetBaseName() const118 Identifier *AnnotationUsage::GetBaseName() const
119 {
120     if (expr_->IsIdentifier()) {
121         return expr_->AsIdentifier();
122     }
123     auto *part = expr_->AsETSTypeReference()->Part();
124     if (part->Name()->IsIdentifier()) {
125         ES2PANDA_ASSERT(part->Name()->AsIdentifier()->Name().Is(ERROR_LITERAL));
126         return part->Name()->AsIdentifier();
127     }
128 
129     return part->Name()->AsTSQualifiedName()->Right();
130 }
131 }  // namespace ark::es2panda::ir
132