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#include <ir/module/exportDefaultDeclaration.h>
28
29namespace panda::es2panda::ir {
30
31void ClassDeclaration::Iterate(const NodeTraverser &cb) const
32{
33    cb(def_);
34
35    for (auto *it : decorators_) {
36        cb(it);
37    }
38}
39
40void ClassDeclaration::Dump(ir::AstDumper *dumper) const
41{
42    dumper->Add({{"type", "ClassDeclaration"}, {"definition", def_}, {"decorators", decorators_},
43                 {"isAnnotationDeclaration", isAnnotationDecl_}});
44}
45
46void 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 = std::string(pg->Context()->RecordName()) + ".";
58            annoName.insert(0, prefix);
59        }
60        pg->Context()->GetEmitter()->AddAnnotationRecord(annoName, this);
61        return;
62    }
63    const auto *node = def_->Ident() ? def_->Ident() : this->Parent();
64    auto lref = compiler::LReference::CreateLRef(pg, node, true);
65    def_->Compile(pg);
66    lref.SetValue();
67}
68
69checker::Type *ClassDeclaration::Check([[maybe_unused]] checker::Checker *checker) const
70{
71    return nullptr;
72}
73
74void ClassDeclaration::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
75{
76    def_ = std::get<ir::AstNode *>(cb(def_))->AsClassDefinition();
77
78    for (auto iter = decorators_.begin(); iter != decorators_.end(); iter++) {
79        *iter = std::get<ir::AstNode *>(cb(*iter))->AsDecorator();
80    }
81}
82
83}  // namespace panda::es2panda::ir
84