1 /*
2 * Copyright (c) 2021-2025 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 "srcDump.h"
17
18 #include <ir/astNode.h>
19 #include <ir/base/classDefinition.h>
20 #include <ir/base/classProperty.h>
21 #include <ir/ts/tsEnumDeclaration.h>
22 #include <ir/ts/tsInterfaceDeclaration.h>
23 #include <ir/ts/tsTypeAliasDeclaration.h>
24 #include <cmath>
25 #include <iostream>
26
27 namespace ark::es2panda::ir {
28
SrcDumper(const ir::AstNode * node)29 SrcDumper::SrcDumper(const ir::AstNode *node)
30 {
31 node->Dump(this);
32 }
33
SrcDumper(const ir::AstNode * node,bool isDeclgen,bool isIsolatedDeclgen)34 SrcDumper::SrcDumper(const ir::AstNode *node, bool isDeclgen, bool isIsolatedDeclgen)
35 : isDeclgen_(isDeclgen), isIsolatedDeclgen_(isIsolatedDeclgen)
36
37 {
38 node->Dump(this);
39 }
40
IncrIndent()41 void SrcDumper::IncrIndent()
42 {
43 indent_.push_back(' ');
44 indent_.push_back(' ');
45 }
46
DecrIndent()47 void SrcDumper::DecrIndent()
48 {
49 if (indent_.size() >= 2U) {
50 indent_.pop_back();
51 indent_.pop_back();
52 }
53 }
54
Endl(size_t num)55 void SrcDumper::Endl(size_t num)
56 {
57 while (num != 0U) {
58 ss_ << std::endl;
59 --num;
60 }
61 ss_ << indent_;
62 }
63
Add(const std::string & str)64 void SrcDumper::Add(const std::string &str)
65 {
66 ss_ << str;
67 }
68
Add(int32_t i)69 void SrcDumper::Add(int32_t i)
70 {
71 ss_ << i;
72 }
73
Add(int64_t l)74 void SrcDumper::Add(int64_t l)
75 {
76 ss_ << l;
77 }
78
Add(float f)79 void SrcDumper::Add(float f)
80 {
81 ss_ << f;
82 }
83
Add(double d)84 void SrcDumper::Add(double d)
85 {
86 ss_ << d;
87 }
88
IsDeclgen() const89 bool SrcDumper::IsDeclgen() const
90 {
91 return isDeclgen_;
92 }
93
DumpVariant(NodeVariant & node)94 void SrcDumper::DumpVariant(NodeVariant &node)
95 {
96 std::visit(
97 [this](auto &&value) {
98 using T = std::decay_t<decltype(value)>;
99 if constexpr (!std::is_same_v<T, std::monostate>) {
100 if constexpr (std::is_pointer_v<T>) {
101 DumpNodeIfPointer(value);
102 }
103 }
104 },
105 node);
106 }
107
108 template <typename T>
DumpNodeIfPointer(T * value)109 void SrcDumper::DumpNodeIfPointer(T *value)
110 {
111 if (value) {
112 isIndirectDepPhase_ = true;
113 value->Dump(this);
114 isIndirectDepPhase_ = false;
115 }
116 }
117
DumpNode(const std::string & key)118 void SrcDumper::DumpNode(const std::string &key)
119 {
120 auto it = unExportNode_.find(key);
121 if (it == unExportNode_.end()) {
122 return;
123 }
124
125 NodeVariant node = it->second;
126 unExportNode_.erase(it);
127
128 DumpVariant(node);
129 }
130
Run()131 void SrcDumper::Run()
132 {
133 while (!taskQueue_.empty()) {
134 auto task = std::move(taskQueue_.front());
135 taskQueue_.pop();
136 task();
137 }
138 }
139
140 } // namespace ark::es2panda::ir
141