1 /**
2 * Copyright (c) 2021 - 2023 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 "classDefinition.h"
17
18 #include "checker/TSchecker.h"
19 #include "checker/ETSchecker.h"
20 #include "compiler/core/ETSGen.h"
21 #include "compiler/core/pandagen.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 #include "ir/base/classStaticBlock.h"
25 #include "ir/base/methodDefinition.h"
26 #include "ir/base/scriptFunction.h"
27 #include "ir/expressions/identifier.h"
28 #include "ir/ts/tsClassImplements.h"
29
30 namespace panda::es2panda::ir {
Ctor() const31 const FunctionExpression *ClassDefinition::Ctor() const
32 {
33 return ctor_ != nullptr ? ctor_->Value()->AsFunctionExpression() : nullptr;
34 }
35
HasPrivateMethod() const36 bool ClassDefinition::HasPrivateMethod() const
37 {
38 return std::any_of(body_.cbegin(), body_.cend(), [](auto *element) {
39 return element->IsMethodDefinition() && element->AsClassElement()->IsPrivateElement();
40 });
41 }
42
HasComputedInstanceField() const43 bool ClassDefinition::HasComputedInstanceField() const
44 {
45 return std::any_of(body_.cbegin(), body_.cend(), [](auto *element) {
46 return element->IsClassProperty() && element->AsClassElement()->IsComputed() &&
47 !(element->AsClassElement()->Modifiers() & ir::ModifierFlags::STATIC);
48 });
49 }
50
HasMatchingPrivateKey(const util::StringView & name) const51 bool ClassDefinition::HasMatchingPrivateKey(const util::StringView &name) const
52 {
53 return std::any_of(body_.cbegin(), body_.cend(), [&name](auto *element) {
54 return element->AsClassElement()->IsPrivateElement() && element->AsClassElement()->Id()->Name() == name;
55 });
56 }
57
TransformChildren(const NodeTransformer & cb)58 void ClassDefinition::TransformChildren(const NodeTransformer &cb)
59 {
60 if (ident_ != nullptr) {
61 ident_ = cb(ident_)->AsIdentifier();
62 }
63
64 if (typeParams_ != nullptr) {
65 typeParams_ = cb(typeParams_)->AsTSTypeParameterDeclaration();
66 }
67
68 if (superClass_ != nullptr) {
69 superClass_ = cb(superClass_)->AsExpression();
70 }
71
72 if (superTypeParams_ != nullptr) {
73 superTypeParams_ = cb(superTypeParams_)->AsTSTypeParameterInstantiation();
74 }
75
76 for (auto *&it : implements_) {
77 it = cb(it)->AsTSClassImplements();
78 }
79
80 if (ctor_ != nullptr) {
81 ctor_ = cb(ctor_)->AsMethodDefinition();
82 }
83
84 for (auto *&it : body_) {
85 it = cb(it);
86 }
87 }
88
Iterate(const NodeTraverser & cb) const89 void ClassDefinition::Iterate(const NodeTraverser &cb) const
90 {
91 if (ident_ != nullptr) {
92 cb(ident_);
93 }
94
95 if (typeParams_ != nullptr) {
96 cb(typeParams_);
97 }
98
99 if (superClass_ != nullptr) {
100 cb(superClass_);
101 }
102
103 if (superTypeParams_ != nullptr) {
104 cb(superTypeParams_);
105 }
106
107 for (auto *it : implements_) {
108 cb(it);
109 }
110
111 if (ctor_ != nullptr) {
112 cb(ctor_);
113 }
114
115 for (auto *it : body_) {
116 cb(it);
117 }
118 }
119
Dump(ir::AstDumper * dumper) const120 void ClassDefinition::Dump(ir::AstDumper *dumper) const
121 {
122 auto propFilter = [](AstNode *prop) -> bool {
123 return !prop->IsClassStaticBlock() || !prop->AsClassStaticBlock()->Function()->IsHidden();
124 };
125 dumper->Add({{"id", AstDumper::Nullish(ident_)},
126 {"typeParameters", AstDumper::Optional(typeParams_)},
127 {"superClass", AstDumper::Nullish(superClass_)},
128 {"superTypeParameters", AstDumper::Optional(superTypeParams_)},
129 {"implements", implements_},
130 {"constructor", AstDumper::Optional(ctor_)},
131 {"body", body_, propFilter}});
132 }
133
Dump(ir::SrcDumper * dumper) const134 void ClassDefinition::Dump(ir::SrcDumper *dumper) const
135 {
136 ASSERT(ident_ != nullptr);
137
138 if (IsExtern()) {
139 dumper->Add("extern ");
140 }
141
142 if (IsFinal()) {
143 dumper->Add("final ");
144 }
145
146 if (IsAbstract()) {
147 dumper->Add("abstract ");
148 }
149
150 dumper->Add("class ");
151 ident_->Dump(dumper);
152
153 if (typeParams_ != nullptr) {
154 dumper->Add("<");
155 typeParams_->Dump(dumper);
156 dumper->Add("> ");
157 }
158
159 if (superClass_ != nullptr) {
160 dumper->Add(" extends ");
161 superClass_->Dump(dumper);
162 }
163
164 if (!implements_.empty()) {
165 dumper->Add(" implements ");
166 for (auto interface : implements_) {
167 interface->Dump(dumper);
168 if (interface != implements_.back()) {
169 dumper->Add(", ");
170 }
171 }
172 }
173
174 dumper->Add(" {");
175 if (!body_.empty()) {
176 dumper->IncrIndent();
177 dumper->Endl();
178 for (auto elem : body_) {
179 elem->Dump(dumper);
180 if (elem == body_.back()) {
181 dumper->DecrIndent();
182 }
183 dumper->Endl();
184 }
185 }
186 dumper->Add("}");
187 dumper->Endl();
188 }
189
Compile(compiler::PandaGen * pg) const190 void ClassDefinition::Compile(compiler::PandaGen *pg) const
191 {
192 pg->GetAstCompiler()->Compile(this);
193 }
194
Compile(compiler::ETSGen * etsg) const195 void ClassDefinition::Compile(compiler::ETSGen *etsg) const
196 {
197 etsg->GetAstCompiler()->Compile(this);
198 }
199
Check(checker::TSChecker * checker)200 checker::Type *ClassDefinition::Check(checker::TSChecker *checker)
201 {
202 return checker->GetAnalyzer()->Check(this);
203 }
204
Check(checker::ETSChecker * checker)205 checker::Type *ClassDefinition::Check(checker::ETSChecker *checker)
206 {
207 return checker->GetAnalyzer()->Check(this);
208 }
209 } // namespace panda::es2panda::ir
210