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