1 /**
2 * Copyright (c) 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 "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 : 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 }
Iterate(const NodeTraverser & cb) const40 void AnnotationDeclaration::Iterate(const NodeTraverser &cb) const
41 {
42 if (expr_ != nullptr) {
43 cb(expr_);
44 }
45
46 for (auto *it : properties_) {
47 cb(it);
48 }
49 }
50
Dump(ir::AstDumper * dumper) const51 void AnnotationDeclaration::Dump(ir::AstDumper *dumper) const
52 {
53 dumper->Add({{"Expr", expr_}, {"properties", properties_}});
54 }
Dump(ir::SrcDumper * dumper) const55 void AnnotationDeclaration::Dump(ir::SrcDumper *dumper) const
56 { // re-understand
57 ASSERT(expr_ != nullptr);
58 dumper->Add("@interface ");
59 expr_->Dump(dumper);
60 dumper->Add(" {");
61
62 if (!properties_.empty()) {
63 dumper->IncrIndent();
64 dumper->Endl();
65 for (auto elem : properties_) {
66 elem->Dump(dumper);
67 if (elem == properties_.back()) {
68 dumper->DecrIndent();
69 }
70 }
71 }
72 dumper->Add("}");
73 dumper->Endl();
74 }
Compile(compiler::PandaGen * pg) const75 void AnnotationDeclaration::Compile(compiler::PandaGen *pg) const
76 {
77 pg->GetAstCompiler()->Compile(this);
78 }
79
Compile(compiler::ETSGen * etsg) const80 void AnnotationDeclaration::Compile(compiler::ETSGen *etsg) const
81 {
82 etsg->GetAstCompiler()->Compile(this);
83 }
84
Check(checker::TSChecker * checker)85 checker::Type *AnnotationDeclaration::Check(checker::TSChecker *checker)
86 {
87 return checker->GetAnalyzer()->Check(this);
88 }
89
Check(checker::ETSChecker * checker)90 checker::Type *AnnotationDeclaration::Check(checker::ETSChecker *checker)
91 {
92 return checker->GetAnalyzer()->Check(this);
93 }
94
GetBaseName() const95 Identifier *AnnotationDeclaration::GetBaseName() const
96 {
97 if (expr_->IsIdentifier()) {
98 return expr_->AsIdentifier();
99 }
100 auto *part = expr_->AsETSTypeReference()->Part();
101 return part->Name()->AsTSQualifiedName()->Right();
102 }
103 } // namespace ark::es2panda::ir