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