1 /**
2 * Copyright (c) 2021 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 "varbinder/variableFlags.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/astNode.h"
25 #include "ir/typeNode.h"
26 #include "ir/expression.h"
27 #include "ir/statements/variableDeclaration.h"
28 #include "ir/expressions/arrayExpression.h"
29 #include "ir/expressions/objectExpression.h"
30 #include "ir/expressions/identifier.h"
31
32 #include "checker/TSchecker.h"
33 #include "checker/ETSchecker.h"
34 #include "checker/ts/destructuringContext.h"
35
36 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)37 void VariableDeclarator::TransformChildren(const NodeTransformer &cb)
38 {
39 id_ = cb(id_)->AsExpression();
40
41 if (init_ != nullptr) {
42 init_ = cb(init_)->AsExpression();
43 }
44 }
45
Iterate(const NodeTraverser & cb) const46 void VariableDeclarator::Iterate(const NodeTraverser &cb) const
47 {
48 cb(id_);
49
50 if (init_ != nullptr) {
51 cb(init_);
52 }
53 }
54
Dump(ir::AstDumper * dumper) const55 void VariableDeclarator::Dump(ir::AstDumper *dumper) const
56 {
57 dumper->Add({{"type", "VariableDeclarator"}, {"id", id_}, {"init", AstDumper::Nullish(init_)}});
58 }
59
Dump(ir::SrcDumper * dumper) const60 void VariableDeclarator::Dump(ir::SrcDumper *dumper) const
61 {
62 if (id_ != nullptr) {
63 id_->Dump(dumper);
64 if (id_->IsAnnotatedExpression()) {
65 auto *type = id_->AsAnnotatedExpression()->TypeAnnotation();
66 if (type != nullptr) {
67 dumper->Add(": ");
68 type->Dump(dumper);
69 }
70 }
71 }
72 if (init_ != nullptr) {
73 dumper->Add(" = ");
74 init_->Dump(dumper);
75 }
76 }
77
Compile(compiler::PandaGen * pg) const78 void VariableDeclarator::Compile([[maybe_unused]] compiler::PandaGen *pg) const
79 {
80 pg->GetAstCompiler()->Compile(this);
81 }
82
Compile(compiler::ETSGen * etsg) const83 void VariableDeclarator::Compile(compiler::ETSGen *etsg) const
84 {
85 etsg->GetAstCompiler()->Compile(this);
86 }
87
Check(checker::TSChecker * checker)88 checker::Type *VariableDeclarator::Check([[maybe_unused]] checker::TSChecker *checker)
89 {
90 return checker->GetAnalyzer()->Check(this);
91 }
92
Check(checker::ETSChecker * checker)93 checker::Type *VariableDeclarator::Check(checker::ETSChecker *checker)
94 {
95 return checker->GetAnalyzer()->Check(this);
96 }
97 } // namespace panda::es2panda::ir
98