1 /**
2 * Copyright (c) 2024-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 "annotationDeclaration.h"
17 #include "checker/TSchecker.h"
18 #include "compiler/core/ETSGen.h"
19 #include "compiler/core/pandagen.h"
20 #include "ir/astDump.h"
21 #include "ir/srcDump.h"
22
23 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)24 void AnnotationDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
25 {
26 for (auto *&it : VectorIterationGuard(properties_)) {
27 if (auto *transformedNode = cb(it); it != transformedNode) {
28 it->SetTransformedNode(transformationName, transformedNode);
29 it = transformedNode;
30 }
31 }
32
33 if (expr_ != nullptr) {
34 if (auto *transformedNode = cb(expr_); expr_ != transformedNode) {
35 expr_->SetTransformedNode(transformationName, transformedNode);
36 expr_ = transformedNode->AsIdentifier();
37 }
38 }
39
40 for (auto *&it : VectorIterationGuard(Annotations())) {
41 if (auto *transformedNode = cb(it); it != transformedNode) {
42 it->SetTransformedNode(transformationName, transformedNode);
43 it = transformedNode->AsAnnotationUsage();
44 }
45 }
46 }
Iterate(const NodeTraverser & cb) const47 void AnnotationDeclaration::Iterate(const NodeTraverser &cb) const
48 {
49 if (expr_ != nullptr) {
50 cb(expr_);
51 }
52
53 for (auto *it : VectorIterationGuard(properties_)) {
54 cb(it);
55 }
56
57 for (auto *it : VectorIterationGuard(Annotations())) {
58 cb(it);
59 }
60 }
61
Dump(ir::AstDumper * dumper) const62 void AnnotationDeclaration::Dump(ir::AstDumper *dumper) const
63 {
64 dumper->Add({{"Expr", expr_}, {"properties", properties_}, {"annotations", AstDumper::Optional(Annotations())}});
65 }
Dump(ir::SrcDumper * dumper) const66 void AnnotationDeclaration::Dump(ir::SrcDumper *dumper) const
67 { // re-understand
68 for (auto *anno : Annotations()) {
69 anno->Dump(dumper);
70 }
71 ES2PANDA_ASSERT(expr_ != nullptr);
72 dumper->Add("@interface ");
73 expr_->Dump(dumper);
74 dumper->Add(" {");
75
76 if (!properties_.empty()) {
77 dumper->IncrIndent();
78 dumper->Endl();
79 for (auto elem : properties_) {
80 elem->Dump(dumper);
81 if (elem == properties_.back()) {
82 dumper->DecrIndent();
83 }
84 }
85 }
86 dumper->Add("}");
87 dumper->Endl();
88 }
Compile(compiler::PandaGen * pg) const89 void AnnotationDeclaration::Compile(compiler::PandaGen *pg) const
90 {
91 pg->GetAstCompiler()->Compile(this);
92 }
93
Compile(compiler::ETSGen * etsg) const94 void AnnotationDeclaration::Compile(compiler::ETSGen *etsg) const
95 {
96 etsg->GetAstCompiler()->Compile(this);
97 }
98
Check(checker::TSChecker * checker)99 checker::Type *AnnotationDeclaration::Check(checker::TSChecker *checker)
100 {
101 return checker->GetAnalyzer()->Check(this);
102 }
103
Check(checker::ETSChecker * checker)104 checker::VerifiedType AnnotationDeclaration::Check(checker::ETSChecker *checker)
105 {
106 return {this, checker->GetAnalyzer()->Check(this)};
107 }
108
GetBaseName() const109 Identifier *AnnotationDeclaration::GetBaseName() const
110 {
111 if (expr_->IsIdentifier()) {
112 return expr_->AsIdentifier();
113 }
114 auto *part = Expr()->AsETSTypeReference()->Part();
115 if (part->Name()->IsIdentifier()) {
116 return part->Name()->AsIdentifier();
117 }
118 if (part->Name()->IsTSQualifiedName()) {
119 return part->Name()->AsTSQualifiedName()->Right();
120 }
121 return nullptr;
122 }
123
Construct(ArenaAllocator * allocator)124 AstNode *AnnotationDeclaration::Construct(ArenaAllocator *allocator)
125 {
126 return allocator->New<AnnotationDeclaration>(nullptr, allocator);
127 }
128
CopyTo(AstNode * other) const129 void AnnotationDeclaration::CopyTo(AstNode *other) const
130 {
131 auto otherImpl = other->AsAnnotationDeclaration();
132
133 otherImpl->internalName_ = internalName_;
134 otherImpl->scope_ = scope_;
135 otherImpl->expr_ = expr_;
136 otherImpl->properties_ = properties_;
137 otherImpl->policy_ = policy_;
138
139 AnnotationAllowed<Statement>::CopyTo(other);
140 }
141 } // namespace ark::es2panda::ir