• 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 "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     const auto *node = def_->Ident() ? def_->Ident() : this->Parent();
48     auto lref = compiler::LReference::CreateLRef(pg, node, true);
49     def_->Compile(pg);
50     lref.SetValue();
51 }
52 
Check(checker::Checker * checker) const53 checker::Type *ClassDeclaration::Check([[maybe_unused]] checker::Checker *checker) const
54 {
55     return nullptr;
56 }
57 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)58 void ClassDeclaration::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
59 {
60     def_ = std::get<ir::AstNode *>(cb(def_))->AsClassDefinition();
61 
62     for (auto iter = decorators_.begin(); iter != decorators_.end(); iter++) {
63         *iter = std::get<ir::AstNode *>(cb(*iter))->AsDecorator();
64     }
65 }
66 
67 }  // namespace panda::es2panda::ir
68