Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 : #ifndef RUST_AST_DUMP_H
19 : #define RUST_AST_DUMP_H
20 :
21 : #include "rust-ast-visitor.h"
22 : #include "rust-ast.h"
23 : #include "rust-ast-full.h"
24 : #include "rust-dump.h"
25 : #include "rust-system.h"
26 :
27 : #include "rust-ast-collector.h"
28 :
29 : namespace Rust {
30 : namespace AST {
31 :
32 4308 : class Dump
33 : {
34 : public:
35 : struct Configuration
36 : {
37 : enum class InternalComment
38 : {
39 : Dump,
40 : Hide,
41 : } dump_internal_comments;
42 : enum class NodeDescription
43 : {
44 : Dump,
45 : Hide,
46 : } dump_node_description;
47 : enum class Comment
48 : {
49 : Dump,
50 : Hide,
51 : } dump_comments;
52 : enum class Newline
53 : {
54 : Dump,
55 : Hide,
56 : } newline;
57 : enum class Indentation
58 : {
59 : Space4,
60 : None,
61 : } indentation;
62 : };
63 :
64 : Dump (std::ostream &stream);
65 : Dump (std::ostream &stream, Configuration configuration,
66 : std::set<std::string> excluded_node);
67 :
68 : /**
69 : * Run the visitor on an entire crate and its items
70 : */
71 : void go (AST::Crate &crate);
72 : void go (AST::Item &item);
73 :
74 4308 : template <typename T> void process (T &v)
75 : {
76 4308 : TokenCollector collector;
77 4308 : collector.visit (v);
78 :
79 4308 : TokenPtr previous = nullptr;
80 2596978 : for (auto item : collector.collect ())
81 : {
82 2592670 : switch (item.get_kind ())
83 : {
84 896908 : case AST::CollectItem::Kind::Token:
85 : {
86 896908 : TokenPtr current = item.get_token ();
87 3291628 : if (require_spacing (previous, current))
88 373673 : stream << " ";
89 1793816 : stream << current->as_string ();
90 896908 : previous = current;
91 896908 : }
92 896908 : break;
93 0 : case AST::CollectItem::Kind::Comment:
94 0 : if (configuration.dump_comments == Configuration::Comment::Dump)
95 0 : stream << " /* " << item.get_comment () << " */ ";
96 : break;
97 : case AST::CollectItem::Kind::Indentation:
98 325574 : for (size_t i = 0; i < item.get_indent_level (); i++)
99 : {
100 177479 : if (configuration.indentation
101 : == Configuration::Indentation::Space4)
102 3 : stream << " ";
103 : }
104 : break;
105 203103 : case AST::CollectItem::Kind::Newline:
106 203103 : if (configuration.newline == Configuration::Newline::Dump)
107 9 : stream << "\n";
108 203103 : previous = nullptr;
109 203103 : break;
110 672282 : case AST::CollectItem::Kind::BeginNodeDescription:
111 672282 : if (configuration.dump_node_description
112 : == Configuration::NodeDescription::Dump)
113 : {
114 0 : std::string comment = item.get_node_description ();
115 0 : if (excluded_node.find (comment) == excluded_node.end ())
116 0 : stream << " /* " << comment << " */ ";
117 0 : }
118 : break;
119 672282 : case AST::CollectItem::Kind::EndNodeDescription:
120 672282 : if (configuration.dump_node_description
121 : == Configuration::NodeDescription::Dump)
122 : {
123 0 : std::string comment = item.get_node_description ();
124 0 : if (excluded_node.find (comment) == excluded_node.end ())
125 0 : stream << " /* !" << comment << " */ ";
126 0 : }
127 : break;
128 0 : case AST::CollectItem::Kind::InternalComment:
129 0 : if (configuration.dump_internal_comments
130 : == Configuration::InternalComment::Dump)
131 : {
132 0 : std::string comment = item.get_internal_comment ();
133 0 : stream << " /* " << comment << " */ ";
134 0 : }
135 : break;
136 0 : default:
137 0 : rust_unreachable ();
138 : }
139 : }
140 4308 : }
141 :
142 : // Helper method to get a quick debug dump to standard error output
143 : static void debug (Visitable &v);
144 :
145 : private:
146 : std::ostream &stream;
147 : Indent indentation;
148 : Configuration configuration;
149 : std::set<std::string> excluded_node;
150 :
151 : static bool require_spacing (TokenPtr previous, TokenPtr current);
152 : };
153 :
154 : } // namespace AST
155 : } // namespace Rust
156 :
157 : // In the global namespace to make it easier to call from debugger
158 : void debug (Rust::AST::Visitable &v);
159 : void debug (Rust::AST::Crate &v);
160 :
161 : #endif // !RUST_AST_DUMP_H
|