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 "classDeclaration.h"
17
18 #include <compiler/base/lreference.h>
19 #include <compiler/core/pandagen.h>
20 #include <ir/astDump.h>
21 #include <ir/base/classDefinition.h>
22 #include <ir/base/decorator.h>
23 #include <ir/expressions/identifier.h>
24 #include <ir/module/exportDefaultDeclaration.h>
25
26 namespace panda::es2panda::ir {
27
Iterate(const NodeTraverser & cb) const28 void ClassDeclaration::Iterate(const NodeTraverser &cb) const
29 {
30 cb(def_);
31
32 for (auto *it : decorators_) {
33 cb(it);
34 }
35 }
36
Dump(ir::AstDumper * dumper) const37 void ClassDeclaration::Dump(ir::AstDumper *dumper) const
38 {
39 dumper->Add({{"type", "ClassDeclaration"}, {"definition", def_}, {"decorators", decorators_}});
40 }
41
Compile(compiler::PandaGen * pg) const42 void ClassDeclaration::Compile(compiler::PandaGen *pg) const
43 {
44 // [ClassDeclaration] without [Identifier] must have parent node
45 // of [ExportDefaultDeclaration] during compiling phase. So we use
46 // the parent node to create a lreference with boundName of [*default*].
47 if (def_->Declare()) {
48 return;
49 }
50 const auto *node = def_->Ident() ? def_->Ident() : this->Parent();
51 auto lref = compiler::LReference::CreateLRef(pg, node, true);
52 def_->Compile(pg);
53 lref.SetValue();
54 }
55
Check(checker::Checker * checker) const56 checker::Type *ClassDeclaration::Check([[maybe_unused]] checker::Checker *checker) const
57 {
58 return nullptr;
59 }
60
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)61 void ClassDeclaration::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
62 {
63 def_ = std::get<ir::AstNode *>(cb(def_))->AsClassDefinition();
64
65 for (auto iter = decorators_.begin(); iter != decorators_.end(); iter++) {
66 *iter = std::get<ir::AstNode *>(cb(*iter))->AsDecorator();
67 }
68 }
69
70 } // namespace panda::es2panda::ir
71