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 "decorator.h"
17
18 #include "es2panda.h"
19 #include "ir/expression.h"
20 #include "ir/astDump.h"
21 #include "ir/srcDump.h"
22 #include "checker/ETSchecker.h"
23 #include "checker/TSchecker.h"
24 #include "compiler/core/ETSGen.h"
25 #include "compiler/core/pandagen.h"
26
27 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)28 void Decorator::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
29 {
30 if (auto *transformedNode = cb(expr_); expr_ != transformedNode) {
31 expr_->SetTransformedNode(transformationName, transformedNode);
32 expr_ = transformedNode->AsExpression();
33 }
34 }
35
Iterate(const NodeTraverser & cb) const36 void Decorator::Iterate(const NodeTraverser &cb) const
37 {
38 cb(expr_);
39 }
40
Dump(ir::AstDumper * dumper) const41 void Decorator::Dump(ir::AstDumper *dumper) const
42 {
43 dumper->Add({{"type", "Decorator"}, {"expression", expr_}});
44 }
45
Dump(ir::SrcDumper * dumper) const46 void Decorator::Dump(ir::SrcDumper *dumper) const
47 {
48 dumper->Add("Decorator");
49 }
50
Compile(compiler::PandaGen * pg) const51 void Decorator::Compile(compiler::PandaGen *pg) const
52 {
53 pg->GetAstCompiler()->Compile(this);
54 }
55
Compile(compiler::ETSGen * etsg) const56 void Decorator::Compile(compiler::ETSGen *etsg) const
57 {
58 etsg->GetAstCompiler()->Compile(this);
59 }
60
Check(checker::TSChecker * checker)61 checker::Type *Decorator::Check(checker::TSChecker *checker)
62 {
63 return checker->GetAnalyzer()->Check(this);
64 }
65
Check(checker::ETSChecker * checker)66 checker::Type *Decorator::Check(checker::ETSChecker *checker)
67 {
68 return checker->GetAnalyzer()->Check(this);
69 }
70
Clone(ArenaAllocator * const allocator,AstNode * const parent)71 Decorator *Decorator::Clone(ArenaAllocator *const allocator, AstNode *const parent)
72 {
73 auto *const expr = expr_ != nullptr ? expr_->Clone(allocator, nullptr)->AsExpression() : nullptr;
74
75 if (auto *const clone = allocator->New<Decorator>(expr); clone != nullptr) {
76 if (expr != nullptr) {
77 expr->SetParent(clone);
78 }
79 if (parent != nullptr) {
80 clone->SetParent(parent);
81 }
82 return clone;
83 }
84
85 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
86 }
87 } // namespace ark::es2panda::ir
88