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 "tsInterfaceDeclaration.h"
17
18 #include "macros.h"
19 #include "varbinder/declaration.h"
20 #include "varbinder/variable.h"
21 #include "checker/TSchecker.h"
22 #include "checker/ETSchecker.h"
23 #include "compiler/core/ETSGen.h"
24 #include "compiler/core/pandagen.h"
25 #include "ir/astDump.h"
26 #include "ir/srcDump.h"
27 #include "ir/base/decorator.h"
28 #include "ir/expressions/identifier.h"
29 #include "ir/ts/tsInterfaceBody.h"
30 #include "ir/ts/tsInterfaceHeritage.h"
31 #include "ir/ts/tsTypeParameter.h"
32 #include "ir/ts/tsTypeParameterDeclaration.h"
33
34 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer &cb, std::string_view transformationName)35 void TSInterfaceDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
36 {
37 for (auto *&it : decorators_) {
38 if (auto *transformedNode = cb(it); it != transformedNode) {
39 it->SetTransformedNode(transformationName, transformedNode);
40 it = transformedNode->AsDecorator();
41 }
42 }
43
44 if (auto *transformedNode = cb(id_); id_ != transformedNode) {
45 id_->SetTransformedNode(transformationName, transformedNode);
46 id_ = transformedNode->AsIdentifier();
47 }
48
49 if (typeParams_ != nullptr) {
50 if (auto *transformedNode = cb(typeParams_); typeParams_ != transformedNode) {
51 typeParams_->SetTransformedNode(transformationName, transformedNode);
52 typeParams_ = transformedNode->AsTSTypeParameterDeclaration();
53 }
54 }
55
56 for (auto *&it : extends_) {
57 if (auto *transformedNode = cb(it); it != transformedNode) {
58 it->SetTransformedNode(transformationName, transformedNode);
59 it = transformedNode->AsTSInterfaceHeritage();
60 }
61 }
62
63 if (auto *transformedNode = cb(body_); body_ != transformedNode) {
64 body_->SetTransformedNode(transformationName, transformedNode);
65 body_ = transformedNode->AsTSInterfaceBody();
66 }
67 }
68
Iterate(const NodeTraverser &cb) const69 void TSInterfaceDeclaration::Iterate(const NodeTraverser &cb) const
70 {
71 for (auto *it : decorators_) {
72 cb(it);
73 }
74
75 cb(id_);
76
77 if (typeParams_ != nullptr) {
78 cb(typeParams_);
79 }
80
81 for (auto *it : extends_) {
82 cb(it);
83 }
84
85 cb(body_);
86 }
87
Dump(ir::AstDumper *dumper) const88 void TSInterfaceDeclaration::Dump(ir::AstDumper *dumper) const
89 {
90 dumper->Add({{"type", "TSInterfaceDeclaration"},
91 {"decorators", AstDumper::Optional(decorators_)},
92 {"body", body_},
93 {"id", id_},
94 {"extends", extends_},
95 {"typeParameters", AstDumper::Optional(typeParams_)}});
96 }
97
Dump(ir::SrcDumper *dumper) const98 void TSInterfaceDeclaration::Dump(ir::SrcDumper *dumper) const
99 {
100 ASSERT(id_);
101
102 dumper->Add("interface ");
103 id_->Dump(dumper);
104
105 if (typeParams_ != nullptr) {
106 dumper->Add("<");
107 typeParams_->Dump(dumper);
108 dumper->Add(">");
109 }
110
111 if (!extends_.empty()) {
112 dumper->Add(" extends ");
113 for (auto ext : extends_) {
114 ext->Dump(dumper);
115 if (ext != extends_.back()) {
116 dumper->Add(", ");
117 }
118 }
119 }
120
121 dumper->Add(" {");
122 if (body_ != nullptr) {
123 dumper->IncrIndent();
124 dumper->Endl();
125 body_->Dump(dumper);
126 dumper->DecrIndent();
127 dumper->Endl();
128 }
129 dumper->Add("}");
130 dumper->Endl();
131 }
132
Compile([[maybe_unused]] compiler::PandaGen *pg) const133 void TSInterfaceDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const
134 {
135 pg->GetAstCompiler()->Compile(this);
136 }
137
Compile(compiler::ETSGen *etsg) const138 void TSInterfaceDeclaration::Compile(compiler::ETSGen *etsg) const
139 {
140 etsg->GetAstCompiler()->Compile(this);
141 }
142
Check([[maybe_unused]] checker::TSChecker *checker)143 checker::Type *TSInterfaceDeclaration::Check([[maybe_unused]] checker::TSChecker *checker)
144 {
145 return checker->GetAnalyzer()->Check(this);
146 }
147
Check(checker::ETSChecker *checker)148 checker::Type *TSInterfaceDeclaration::Check(checker::ETSChecker *checker)
149 {
150 return checker->GetAnalyzer()->Check(this);
151 }
152 } // namespace ark::es2panda::ir
153