• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 - 2023 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 "astNode.h"
17 #include "ir/astDump.h"
18 #include "ir/srcDump.h"
19 #include "typeNode.h"
20 
21 namespace panda::es2panda::ir {
22 
AstNode(AstNode const & other)23 AstNode::AstNode(AstNode const &other)
24 {
25     range_ = other.range_;
26     type_ = other.type_;
27     if (other.variable_ != nullptr) {
28         variable_ = other.variable_;
29     }
30     flags_ = other.flags_;
31     // boxing_unboxing_flags_ {};  leave default value!
32 }
33 
34 template <typename R, typename T>
GetTopStatementImpl(T * self)35 static R GetTopStatementImpl(T *self)
36 {
37     auto iter = self;
38 
39     while (iter->Parent()) {
40         iter = iter->Parent();
41     }
42 
43     return reinterpret_cast<R>(iter);
44 }
45 
GetTopStatement()46 ir::BlockStatement *AstNode::GetTopStatement()
47 {
48     return GetTopStatementImpl<ir::BlockStatement *>(this);
49 }
50 
GetTopStatement() const51 const ir::BlockStatement *AstNode::GetTopStatement() const
52 {
53     return GetTopStatementImpl<const ir::BlockStatement *>(this);
54 }
55 
TransformChildrenRecursively(const NodeTransformer & cb)56 void AstNode::TransformChildrenRecursively(const NodeTransformer &cb)
57 {
58     TransformChildren([=](AstNode *child) {
59         child->TransformChildrenRecursively(cb);
60         return cb(child);
61     });
62 }
63 
IterateRecursively(const NodeTraverser & cb) const64 void AstNode::IterateRecursively(const NodeTraverser &cb) const
65 {
66     Iterate([=](AstNode *child) {
67         cb(child);
68         child->IterateRecursively(cb);
69     });
70 }
71 
AnyChildHelper(bool * found,const NodePredicate & cb,AstNode * ast)72 void AnyChildHelper(bool *found, const NodePredicate &cb, AstNode *ast)
73 {
74     if (*found) {
75         return;
76     }
77 
78     if (cb(ast)) {
79         *found = true;
80         return;
81     }
82 
83     ast->Iterate([=](AstNode *child) { AnyChildHelper(found, cb, child); });
84 }
85 
IsAnyChild(const NodePredicate & cb) const86 bool AstNode::IsAnyChild(const NodePredicate &cb) const
87 {
88     bool found = false;
89     Iterate([&found, cb](AstNode *child) { AnyChildHelper(&found, cb, child); });
90     return found;
91 }
92 
FindChildHelper(AstNode * & found,const NodePredicate & cb,AstNode * ast)93 void FindChildHelper(AstNode *&found, const NodePredicate &cb, AstNode *ast)
94 {
95     if (found != nullptr) {
96         return;
97     }
98 
99     if (cb(ast)) {
100         found = ast;
101         return;
102     }
103 
104     ast->Iterate([&found, cb](AstNode *child) { FindChildHelper(found, cb, child); });
105 }
106 
FindChild(const NodePredicate & cb) const107 AstNode *AstNode::FindChild(const NodePredicate &cb) const
108 {
109     AstNode *found = nullptr;
110     Iterate([&found, cb](AstNode *child) { FindChildHelper(found, cb, child); });
111     return found;
112 }
113 
DumpJSON() const114 std::string AstNode::DumpJSON() const
115 {
116     ir::AstDumper dumper {this};
117     return dumper.Str();
118 }
119 
DumpEtsSrc() const120 std::string AstNode::DumpEtsSrc() const
121 {
122     ir::SrcDumper dumper {this};
123     return dumper.Str();
124 }
125 }  // namespace panda::es2panda::ir
126