MLLIF
a MLIR-based Language to Language Interoperability Flyover
Loading...
Searching...
No Matches
Tree.cxx
Go to the documentation of this file.
1/*
2 * Copyright 2025 Yeong-won Seo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
18
19// NOLINTNEXTLINE(*-no-recursion)
20auto mllif::mlir::Node::print(llvm::raw_ostream &os) const -> void {
21 os << '<' << tag();
22
23 if (name().size()) {
24 os << " id=\"" << Escape(name()) << '"';
25 }
26
27 if (attributes().size()) {
28 for (const auto &[key, value] : _attributes) {
29 os << ' ' << key << "=\"" << Escape(value) << '"';
30 }
31 }
32
33 if (children().empty()) {
34 os << "/>";
35 return;
36 }
37
38 os << '>';
39 for (const auto &child : children()) {
40 child.print(os);
41 }
42 os << "</" << tag() << '>';
43}
44
45// NOLINTNEXTLINE(*-no-recursion)
46auto mllif::mlir::Node::insert_inplace(std::deque<std::string> &path, const std::string &tag) -> Node * {
47 if (path.empty()) {
48 return nullptr;
49 }
50
51 for (auto &child : children()) {
52 if (const auto p = child.insert(path, tag)) {
53 return p;
54 }
55 }
56
57 auto &node = children().emplace_back(path.size() > 1 ? "namespace" : tag, path.front());
58 path.pop_front();
59 auto p = node.insert_inplace(path, tag);
60 if (!p) {
61 p = &node;
62 }
63
64 return p;
65}
66
67// NOLINTNEXTLINE(*-no-recursion)
68auto mllif::mlir::Node::insert(std::deque<std::string> &path, const std::string &tag) -> Node * {
69 if (path.empty() || path.front() != name()) {
70 return nullptr;
71 }
72
73 path.pop_front();
74 if (path.empty()) {
75 return this;
76 }
77
78 return insert_inplace(path, tag);
79}
std::vector< std::pair< std::string, std::string > > & attributes()
Gets attributes of node.
Definition Tree.h:99
std::string & name()
Gets a name of node.
Definition Tree.h:87
Node * insert_inplace(std::deque< std::string > &path, const std::string &tag)
Inserts new node in-place at given path (first element of path is not id of this node)
Definition Tree.cxx:46
Node * insert(std::deque< std::string > &path, const std::string &tag)
Inserts new node at given path (first element of path is id of this node)
Definition Tree.cxx:68
std::string & tag()
Gets a tag of node.
Definition Tree.h:75
void print(llvm::raw_ostream &os) const
Serialize subtree as XML to stream.
Definition Tree.cxx:20
std::vector< Node > & children()
Gets children of node.
Definition Tree.h:111
Node(std::string tag, std::string name)
Creates new node.
Definition Tree.h:69