• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (IsDeclare()) {
103         dumper->Add("declare ");
104     }
105     dumper->Add("interface ");
106     id_->Dump(dumper);
107 
108     if (typeParams_ != nullptr) {
109         dumper->Add("<");
110         typeParams_->Dump(dumper);
111         dumper->Add(">");
112     }
113     if (!extends_.empty()) {
114         dumper->Add(" extends ");
115         for (auto ext : extends_) {
116             ext->Dump(dumper);
117             if (ext != extends_.back()) {
118                 dumper->Add(", ");
119             }
120         }
121     }
122 
123     dumper->Add(" {");
124     if (body_ != nullptr) {
125         dumper->IncrIndent();
126         dumper->Endl();
127         body_->Dump(dumper);
128         dumper->DecrIndent();
129         dumper->Endl();
130     }
131     dumper->Add("}");
132     dumper->Endl();
133 }
134 
Compile(compiler::PandaGen * pg) const135 void TSInterfaceDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) const
136 {
137     pg->GetAstCompiler()->Compile(this);
138 }
139 
Compile(compiler::ETSGen * etsg) const140 void TSInterfaceDeclaration::Compile(compiler::ETSGen *etsg) const
141 {
142     etsg->GetAstCompiler()->Compile(this);
143 }
144 
Check(checker::TSChecker * checker)145 checker::Type *TSInterfaceDeclaration::Check([[maybe_unused]] checker::TSChecker *checker)
146 {
147     return checker->GetAnalyzer()->Check(this);
148 }
149 
Check(checker::ETSChecker * checker)150 checker::Type *TSInterfaceDeclaration::Check(checker::ETSChecker *checker)
151 {
152     return checker->GetAnalyzer()->Check(this);
153 }
154 }  // namespace ark::es2panda::ir
155