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 "variableDeclarator.h"
17
18 #include "compiler/base/lreference.h"
19 #include "compiler/core/pandagen.h"
20 #include "compiler/core/ETSGen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 #include "ir/astNode.h"
24 #include "ir/typeNode.h"
25 #include "ir/expression.h"
26 #include "ir/statements/variableDeclaration.h"
27
28 #include "checker/TSchecker.h"
29 #include "checker/ETSchecker.h"
30 #include "checker/ts/destructuringContext.h"
31
32 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)33 void VariableDeclarator::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
34 {
35 if (auto *transformedNode = cb(id_); id_ != transformedNode) {
36 id_->SetTransformedNode(transformationName, transformedNode);
37 id_ = transformedNode->AsExpression();
38 }
39
40 if (init_ != nullptr) {
41 if (auto *transformedNode = cb(init_); init_ != transformedNode) {
42 init_->SetTransformedNode(transformationName, transformedNode);
43 init_ = transformedNode->AsExpression();
44 }
45 }
46 }
47
Iterate(const NodeTraverser & cb) const48 void VariableDeclarator::Iterate(const NodeTraverser &cb) const
49 {
50 cb(id_);
51
52 if (init_ != nullptr) {
53 cb(init_);
54 }
55 }
56
Dump(ir::AstDumper * dumper) const57 void VariableDeclarator::Dump(ir::AstDumper *dumper) const
58 {
59 dumper->Add({{"type", "VariableDeclarator"}, {"id", id_}, {"init", AstDumper::Nullish(init_)}});
60 }
61
Dump(ir::SrcDumper * dumper) const62 void VariableDeclarator::Dump(ir::SrcDumper *dumper) const
63 {
64 if (id_ != nullptr) {
65 id_->Dump(dumper);
66 if (id_->IsAnnotatedExpression()) {
67 auto *type = id_->AsAnnotatedExpression()->TypeAnnotation();
68 if (type != nullptr) {
69 dumper->Add(": ");
70 type->Dump(dumper);
71 }
72 }
73 }
74 if (init_ != nullptr) {
75 dumper->Add(" = ");
76 init_->Dump(dumper);
77 }
78 }
79
Clone(ArenaAllocator * const allocator,AstNode * const parent)80 VariableDeclarator *VariableDeclarator::Clone(ArenaAllocator *const allocator, AstNode *const parent)
81 {
82 auto *const id = id_ != nullptr ? id_->Clone(allocator, nullptr)->AsExpression() : nullptr;
83 auto *const init = init_ != nullptr ? init_->Clone(allocator, nullptr)->AsExpression() : nullptr;
84
85 if (auto *const clone = allocator->New<VariableDeclarator>(flag_, id, init); clone != nullptr) {
86 if (id != nullptr) {
87 id->SetParent(clone);
88 }
89 if (init != nullptr) {
90 init->SetParent(clone);
91 }
92 if (parent != nullptr) {
93 clone->SetParent(parent);
94 }
95 return clone;
96 }
97 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
98 }
99
Compile(compiler::PandaGen * pg) const100 void VariableDeclarator::Compile([[maybe_unused]] compiler::PandaGen *pg) const
101 {
102 pg->GetAstCompiler()->Compile(this);
103 }
104
Compile(compiler::ETSGen * etsg) const105 void VariableDeclarator::Compile(compiler::ETSGen *etsg) const
106 {
107 etsg->GetAstCompiler()->Compile(this);
108 }
109
Check(checker::TSChecker * checker)110 checker::Type *VariableDeclarator::Check([[maybe_unused]] checker::TSChecker *checker)
111 {
112 return checker->GetAnalyzer()->Check(this);
113 }
114
Check(checker::ETSChecker * checker)115 checker::Type *VariableDeclarator::Check(checker::ETSChecker *checker)
116 {
117 return checker->GetAnalyzer()->Check(this);
118 }
119 } // namespace ark::es2panda::ir
120