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 "etsStructDeclaration.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/base/lreference.h"
20 #include "compiler/core/pandagen.h"
21 #include "compiler/core/ETSGen.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24 #include "ir/base/classDefinition.h"
25 #include "ir/base/decorator.h"
26
27 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)28 void ETSStructDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
29 {
30 for (auto *&it : decorators_) {
31 if (auto *transformedNode = cb(it); it != transformedNode) {
32 it->SetTransformedNode(transformationName, transformedNode);
33 it = transformedNode->AsDecorator();
34 }
35 }
36
37 if (auto *transformedNode = cb(def_); def_ != transformedNode) {
38 def_->SetTransformedNode(transformationName, transformedNode);
39 def_ = transformedNode->AsClassDefinition();
40 }
41 }
42
Iterate(const NodeTraverser &cb) const43 void ETSStructDeclaration::Iterate(const NodeTraverser &cb) const
44 {
45 for (auto *it : decorators_) {
46 cb(it);
47 }
48
49 cb(def_);
50 }
51
Dump(ir::AstDumper *dumper) const52 void ETSStructDeclaration::Dump(ir::AstDumper *dumper) const
53 {
54 dumper->Add(
55 {{"type", "ETSStructDeclaration"}, {"definition", def_}, {"decorators", AstDumper::Optional(decorators_)}});
56 }
57
Dump(ir::SrcDumper *dumper) const58 void ETSStructDeclaration::Dump(ir::SrcDumper *dumper) const
59 {
60 dumper->Add("ETSStructDeclaration");
61 }
62
Compile(compiler::PandaGen *pg) const63 void ETSStructDeclaration::Compile(compiler::PandaGen *pg) const
64 {
65 pg->GetAstCompiler()->Compile(this);
66 }
67
Compile(compiler::ETSGen *etsg) const68 void ETSStructDeclaration::Compile(compiler::ETSGen *etsg) const
69 {
70 etsg->GetAstCompiler()->Compile(this);
71 }
72
Check(checker::TSChecker *checker)73 checker::Type *ETSStructDeclaration::Check(checker::TSChecker *checker)
74 {
75 return checker->GetAnalyzer()->Check(this);
76 }
77
Check(checker::ETSChecker *checker)78 checker::Type *ETSStructDeclaration::Check(checker::ETSChecker *checker)
79 {
80 return checker->GetAnalyzer()->Check(this);
81 }
82
Clone(ArenaAllocator *const allocator, AstNode *const parent)83 ETSStructDeclaration *ETSStructDeclaration::Clone(ArenaAllocator *const allocator, AstNode *const parent)
84 {
85 auto *const def = def_ != nullptr ? def_->Clone(allocator, nullptr)->AsClassDefinition() : nullptr;
86
87 if (auto *const clone = allocator->New<ETSStructDeclaration>(def, allocator); clone != nullptr) {
88 for (auto *const decorator : decorators_) {
89 clone->AddDecorator(decorator->Clone(allocator, clone));
90 }
91
92 if (def != nullptr) {
93 def->SetParent(clone);
94 }
95
96 if (parent != nullptr) {
97 clone->SetParent(parent);
98 }
99 return clone;
100 }
101
102 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
103 }
104 } // namespace ark::es2panda::ir
105