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 <binder/scope.h>
19 #include <binder/variable.h>
20 #include <typescript/checker.h>
21 #include <ir/astDump.h>
22 #include <ir/statements/variableDeclarator.h>
23
24 namespace panda::es2panda::ir {
25
Iterate(const NodeTraverser & cb) const26 void VariableDeclaration::Iterate(const NodeTraverser &cb) const
27 {
28 for (auto *it : declarators_) {
29 cb(it);
30 }
31 }
32
Dump(ir::AstDumper * dumper) const33 void VariableDeclaration::Dump(ir::AstDumper *dumper) const
34 {
35 const char *kind = nullptr;
36
37 switch (kind_) {
38 case VariableDeclarationKind::CONST: {
39 kind = "const";
40 break;
41 }
42 case VariableDeclarationKind::LET: {
43 kind = "let";
44 break;
45 }
46 case VariableDeclarationKind::VAR: {
47 kind = "var";
48 break;
49 }
50 default: {
51 UNREACHABLE();
52 }
53 }
54
55 dumper->Add({{"type", "VariableDeclaration"},
56 {"declarations", declarators_},
57 {"kind", kind},
58 {"declare", AstDumper::Optional(declare_)}});
59 }
60
Compile(compiler::PandaGen * pg) const61 void VariableDeclaration::Compile(compiler::PandaGen *pg) const
62 {
63 for (const auto *it : declarators_) {
64 it->Compile(pg);
65 }
66 }
67
Check(checker::Checker * checker) const68 checker::Type *VariableDeclaration::Check(checker::Checker *checker) const
69 {
70 for (auto *it : declarators_) {
71 it->Check(checker);
72 }
73
74 return nullptr;
75 }
76
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)77 void VariableDeclaration::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
78 {
79 for (auto iter = declarators_.begin(); iter != declarators_.end(); iter++) {
80 *iter = std::get<ir::AstNode *>(cb(*iter))->AsVariableDeclarator();
81 }
82 }
83
84 } // namespace panda::es2panda::ir
85