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 2624 : 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 : };
53 :
54 : Dump (std::ostream &stream);
55 : Dump (std::ostream &stream, Configuration configuration,
56 : std::set<std::string> excluded_node);
57 :
58 : /**
59 : * Run the visitor on an entire crate and its items
60 : */
61 : void go (AST::Crate &crate);
62 : void go (AST::Item &item);
63 :
64 2624 : template <typename T> void process (T &v)
65 : {
66 2624 : TokenCollector collector;
67 2624 : collector.visit (v);
68 :
69 2624 : TokenPtr previous = nullptr;
70 206886 : for (auto item : collector.collect ())
71 : {
72 204262 : switch (item.get_kind ())
73 : {
74 72891 : case AST::CollectItem::Kind::Token:
75 : {
76 72891 : TokenPtr current = item.get_token ();
77 265214 : if (require_spacing (previous, current))
78 36483 : stream << " ";
79 145782 : stream << current->as_string ();
80 72891 : previous = current;
81 72891 : }
82 72891 : break;
83 0 : case AST::CollectItem::Kind::Comment:
84 0 : if (configuration.dump_comments == Configuration::Comment::Dump)
85 0 : stream << " /* " << item.get_comment () << " */ ";
86 : break;
87 : case AST::CollectItem::Kind::Indentation:
88 25687 : for (size_t i = 0; i < item.get_indent_level (); i++)
89 : {
90 14793 : stream << " ";
91 : }
92 : break;
93 16399 : case AST::CollectItem::Kind::Newline:
94 16399 : stream << "\n";
95 16399 : previous = nullptr;
96 16399 : break;
97 52039 : case AST::CollectItem::Kind::BeginNodeDescription:
98 52039 : if (configuration.dump_node_description
99 : == Configuration::NodeDescription::Dump)
100 : {
101 0 : std::string comment = item.get_node_description ();
102 0 : if (excluded_node.find (comment) == excluded_node.end ())
103 0 : stream << " /* " << comment << " */ ";
104 : }
105 : break;
106 52039 : case AST::CollectItem::Kind::EndNodeDescription:
107 52039 : if (configuration.dump_node_description
108 : == Configuration::NodeDescription::Dump)
109 : {
110 0 : std::string comment = item.get_node_description ();
111 0 : if (excluded_node.find (comment) == excluded_node.end ())
112 0 : stream << " /* !" << comment << " */ ";
113 0 : }
114 : break;
115 0 : case AST::CollectItem::Kind::InternalComment:
116 0 : if (configuration.dump_internal_comments
117 : == Configuration::InternalComment::Dump)
118 : {
119 0 : std::string comment = item.get_internal_comment ();
120 0 : stream << " /* " << comment << " */ ";
121 0 : }
122 : break;
123 0 : default:
124 0 : rust_unreachable ();
125 : }
126 : }
127 2624 : }
128 :
129 : // Helper method to get a quick debug dump to standard error output
130 : static void debug (Visitable &v);
131 :
132 : private:
133 : std::ostream &stream;
134 : Indent indentation;
135 : Configuration configuration;
136 : std::set<std::string> excluded_node;
137 :
138 : static bool require_spacing (TokenPtr previous, TokenPtr current);
139 : };
140 :
141 : } // namespace AST
142 : } // namespace Rust
143 :
144 : // In the global namespace to make it easier to call from debugger
145 : void debug (Rust::AST::Visitable &v);
146 :
147 : #endif // !RUST_AST_DUMP_H
|