• 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/compilerContext.h>
20 #include <compiler/core/emitter/emitter.h>
21 #include <compiler/core/pandagen.h>
22 #include <ir/astDump.h>
23 #include <ir/base/classDefinition.h>
24 #include <ir/base/decorator.h>
25 #include <ir/expressions/callExpression.h>
26 #include <ir/expressions/identifier.h>
27 
28 namespace panda::es2panda::ir {
29 
Iterate(const NodeTraverser & cb) const30 void ClassDeclaration::Iterate(const NodeTraverser &cb) const
31 {
32     cb(def_);
33 
34     for (auto *it : decorators_) {
35         cb(it);
36     }
37 }
38 
Dump(ir::AstDumper * dumper) const39 void ClassDeclaration::Dump(ir::AstDumper *dumper) const
40 {
41     dumper->Add({{"type", "ClassDeclaration"}, {"definition", def_}, {"decorators", decorators_},
42                  {"isAnnotationDeclaration", isAnnotationDecl_}});
43 }
44 
Compile(compiler::PandaGen * pg) const45 void ClassDeclaration::Compile(compiler::PandaGen *pg) const
46 {
47     // [ClassDeclaration] without [Identifier] must have parent node
48     // of [ExportDefaultDeclaration] during compiling phase. So we use
49     // the parent node to create a lreference with boundName of [*default*].
50     if (def_->Declare()) {
51         return;
52     }
53     if (isAnnotationDecl_) {
54         std::string annoName = std::string(def_->GetName());
55         if (pg->Context()->IsMergeAbc()) {
56             std::string prefix = std::string(pg->Context()->RecordName()) + ".";
57             annoName.insert(0, prefix);
58         }
59         pg->Context()->GetEmitter()->AddAnnotationRecord(annoName, this);
60         return;
61     }
62     const auto *node = def_->Ident() ? def_->Ident() : this->Parent();
63     auto lref = compiler::LReference::CreateLRef(pg, node, true);
64     def_->Compile(pg);
65     lref.SetValue();
66 }
67 
Check(checker::Checker * checker) const68 checker::Type *ClassDeclaration::Check([[maybe_unused]] checker::Checker *checker) const
69 {
70     return nullptr;
71 }
72 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)73 void ClassDeclaration::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
74 {
75     def_ = std::get<ir::AstNode *>(cb(def_))->AsClassDefinition();
76 
77     for (auto iter = decorators_.begin(); iter != decorators_.end(); iter++) {
78         *iter = std::get<ir::AstNode *>(cb(*iter))->AsDecorator();
79     }
80 }
81 
82 }  // namespace panda::es2panda::ir
83