1/**
2 * Copyright (c) 2021-2024 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 "checker/TSchecker.h"
19#include "compiler/core/ETSGen.h"
20#include "compiler/core/pandagen.h"
21#include "ir/astDump.h"
22#include "ir/srcDump.h"
23
24namespace ark::es2panda::ir {
25void ClassDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
26{
27    for (auto *&it : decorators_) {
28        if (auto *transformedNode = cb(it); it != transformedNode) {
29            it->SetTransformedNode(transformationName, transformedNode);
30            it = transformedNode->AsDecorator();
31        }
32    }
33
34    if (auto *transformedNode = cb(def_); def_ != transformedNode) {
35        def_->SetTransformedNode(transformationName, transformedNode);
36        def_ = transformedNode->AsClassDefinition();
37    }
38}
39
40void ClassDeclaration::Iterate(const NodeTraverser &cb) const
41{
42    for (auto *it : decorators_) {
43        cb(it);
44    }
45
46    cb(def_);
47}
48
49void ClassDeclaration::Dump(ir::AstDumper *dumper) const
50{
51    dumper->Add({{"type", "ClassDeclaration"}, {"definition", def_}, {"decorators", AstDumper::Optional(decorators_)}});
52}
53
54void ClassDeclaration::Dump(ir::SrcDumper *dumper) const
55{
56    if (def_ != nullptr) {
57        def_->Dump(dumper);
58    }
59    // NOTE(nsizov): support decorators when supported in ArkTS
60    ASSERT(decorators_.empty());
61}
62
63void ClassDeclaration::Compile(compiler::PandaGen *pg) const
64{
65    pg->GetAstCompiler()->Compile(this);
66}
67
68void ClassDeclaration::Compile(compiler::ETSGen *etsg) const
69{
70    etsg->GetAstCompiler()->Compile(this);
71}
72
73checker::Type *ClassDeclaration::Check(checker::TSChecker *checker)
74{
75    return checker->GetAnalyzer()->Check(this);
76}
77
78checker::Type *ClassDeclaration::Check(checker::ETSChecker *checker)
79{
80    return checker->GetAnalyzer()->Check(this);
81}
82}  // namespace ark::es2panda::ir
83