• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "variableDeclaration.h"
17 
18 #include "macros.h"
19 #include "varbinder/scope.h"
20 #include "varbinder/variable.h"
21 #include "checker/TSchecker.h"
22 #include "checker/ETSchecker.h"
23 #include "compiler/core/ETSGen.h"
24 #include "compiler/core/pandagen.h"
25 #include "ir/astDump.h"
26 #include "ir/srcDump.h"
27 #include "ir/base/decorator.h"
28 #include "ir/expressions/arrayExpression.h"
29 #include "ir/expressions/identifier.h"
30 #include "ir/expressions/objectExpression.h"
31 #include "ir/statements/variableDeclarator.h"
32 
33 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)34 void VariableDeclaration::TransformChildren(const NodeTransformer &cb)
35 {
36     for (auto *&it : decorators_) {
37         it = cb(it)->AsDecorator();
38     }
39 
40     for (auto *&it : declarators_) {
41         it = cb(it)->AsVariableDeclarator();
42     }
43 }
44 
Iterate(const NodeTraverser & cb) const45 void VariableDeclaration::Iterate(const NodeTraverser &cb) const
46 {
47     for (auto *it : decorators_) {
48         cb(it);
49     }
50 
51     for (auto *it : declarators_) {
52         cb(it);
53     }
54 }
55 
Dump(ir::AstDumper * dumper) const56 void VariableDeclaration::Dump(ir::AstDumper *dumper) const
57 {
58     const char *kind = nullptr;
59 
60     switch (kind_) {
61         case VariableDeclarationKind::CONST: {
62             kind = "const";
63             break;
64         }
65         case VariableDeclarationKind::LET: {
66             kind = "let";
67             break;
68         }
69         case VariableDeclarationKind::VAR: {
70             kind = "var";
71             break;
72         }
73         default: {
74             UNREACHABLE();
75         }
76     }
77 
78     dumper->Add({{"type", "VariableDeclaration"},
79                  {"declarations", declarators_},
80                  {"kind", kind},
81                  {"decorators", AstDumper::Optional(decorators_)},
82                  {"declare", AstDumper::Optional(declare_)}});
83 }
84 
Dump(ir::SrcDumper * dumper) const85 void VariableDeclaration::Dump(ir::SrcDumper *dumper) const
86 {
87     switch (kind_) {
88         case VariableDeclarationKind::CONST:
89             dumper->Add("const ");
90             break;
91         case VariableDeclarationKind::LET:
92             dumper->Add("let ");
93             break;
94         case VariableDeclarationKind::VAR:
95             dumper->Add("var ");
96             break;
97         default:
98             UNREACHABLE();
99     }
100 
101     for (auto declarator : declarators_) {
102         declarator->Dump(dumper);
103         if (declarator != declarators_.back()) {
104             dumper->Add(", ");
105         }
106     }
107 
108     if ((parent_ != nullptr) && (parent_->IsBlockStatement() || parent_->IsBlockExpression())) {
109         dumper->Add(";");
110     }
111 }
112 
Compile(compiler::PandaGen * pg) const113 void VariableDeclaration::Compile(compiler::PandaGen *pg) const
114 {
115     pg->GetAstCompiler()->Compile(this);
116 }
117 
Compile(compiler::ETSGen * etsg) const118 void VariableDeclaration::Compile(compiler::ETSGen *etsg) const
119 {
120     etsg->GetAstCompiler()->Compile(this);
121 }
122 
Check(checker::TSChecker * checker)123 checker::Type *VariableDeclaration::Check(checker::TSChecker *checker)
124 {
125     return checker->GetAnalyzer()->Check(this);
126 }
127 
Check(checker::ETSChecker * checker)128 checker::Type *VariableDeclaration::Check([[maybe_unused]] checker::ETSChecker *checker)
129 {
130     return checker->GetAnalyzer()->Check(this);
131 }
132 }  // namespace panda::es2panda::ir
133