1 /**
2 * Copyright (c) 2021-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 "classDeclaration.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 #include <type_traits>
24
25 namespace ark::es2panda::ir {
26
Construct(ArenaAllocator * allocator)27 ClassDeclaration *ClassDeclaration::Construct(ArenaAllocator *allocator)
28 {
29 return allocator->New<ClassDeclaration>(nullptr, allocator);
30 }
31
CopyTo(AstNode * other) const32 void ClassDeclaration::CopyTo(AstNode *other) const
33 {
34 auto otherImpl = other->AsClassDeclaration();
35
36 otherImpl->def_ = def_;
37 otherImpl->decorators_ = decorators_;
38
39 Statement::CopyTo(other);
40 }
41
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)42 void ClassDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
43 {
44 for (auto *&it : VectorIterationGuard(decorators_)) {
45 if (auto *transformedNode = cb(it); it != transformedNode) {
46 it->SetTransformedNode(transformationName, transformedNode);
47 it = transformedNode->AsDecorator();
48 }
49 }
50
51 if (auto *transformedNode = cb(def_); def_ != transformedNode) {
52 def_->SetTransformedNode(transformationName, transformedNode);
53 def_ = transformedNode->AsClassDefinition();
54 }
55 }
56
Iterate(const NodeTraverser & cb) const57 void ClassDeclaration::Iterate(const NodeTraverser &cb) const
58 {
59 for (auto *it : VectorIterationGuard(decorators_)) {
60 cb(it);
61 }
62
63 cb(def_);
64 }
65
Dump(ir::AstDumper * dumper) const66 void ClassDeclaration::Dump(ir::AstDumper *dumper) const
67 {
68 dumper->Add({{"type", "ClassDeclaration"}, {"definition", def_}, {"decorators", AstDumper::Optional(decorators_)}});
69 }
70
Dump(ir::SrcDumper * dumper) const71 void ClassDeclaration::Dump(ir::SrcDumper *dumper) const
72 {
73 if (def_ != nullptr) {
74 def_->Dump(dumper);
75 }
76 // NOTE(nsizov): support decorators when supported in ArkTS
77 ES2PANDA_ASSERT(decorators_.empty());
78 }
79
Compile(compiler::PandaGen * pg) const80 void ClassDeclaration::Compile(compiler::PandaGen *pg) const
81 {
82 pg->GetAstCompiler()->Compile(this);
83 }
84
Compile(compiler::ETSGen * etsg) const85 void ClassDeclaration::Compile(compiler::ETSGen *etsg) const
86 {
87 etsg->GetAstCompiler()->Compile(this);
88 }
89
Check(checker::TSChecker * checker)90 checker::Type *ClassDeclaration::Check(checker::TSChecker *checker)
91 {
92 return checker->GetAnalyzer()->Check(this);
93 }
94
Check(checker::ETSChecker * checker)95 checker::VerifiedType ClassDeclaration::Check(checker::ETSChecker *checker)
96 {
97 return {this, checker->GetAnalyzer()->Check(this)};
98 }
99 } // namespace ark::es2panda::ir
100