libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / rust / ast / rust-ast-dump.h
blobb76cbbf825d333d7c04abc71699822fa400db7a1
1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 // This file is part of GCC.
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.
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.
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
21 #include "rust-ast-visitor.h"
22 #include "rust-ast.h"
23 #include "rust-ast-full.h"
24 #include "rust-dump.h"
26 #include "rust-ast-collector.h"
28 namespace Rust {
29 namespace AST {
31 class Dump
33 public:
34 Dump (std::ostream &stream);
36 /**
37 * Run the visitor on an entire crate and its items
39 void go (AST::Crate &crate);
40 void go (AST::Item &item);
42 template <typename T> void process (T &v)
44 TokenCollector collector;
45 collector.visit (v);
47 TokenPtr previous = nullptr;
48 for (auto item : collector.collect ())
50 switch (item.get_kind ())
52 case AST::CollectItem::Kind::Token: {
53 TokenPtr current = item.get_token ();
54 if (require_spacing (previous, current))
55 stream << " ";
56 stream << current->as_string ();
57 previous = current;
59 break;
60 case AST::CollectItem::Kind::Comment:
61 stream << " /* " << item.get_comment () << " */ ";
62 break;
63 case AST::CollectItem::Kind::Indentation:
64 for (size_t i = 0; i < item.get_indent_level (); i++)
66 stream << " ";
68 break;
69 case AST::CollectItem::Kind::Newline:
70 stream << "\n";
71 previous = nullptr;
72 break;
73 default:
74 rust_unreachable ();
79 // Helper method to get a quick debug dump to standard error output
80 static void debug (Visitable &v);
82 private:
83 std::ostream &stream;
84 Indent indentation;
86 static bool require_spacing (TokenPtr previous, TokenPtr current);
89 } // namespace AST
90 } // namespace Rust
92 // In the global namespace to make it easier to call from debugger
93 void
94 debug (Rust::AST::Visitable &v);
96 #endif // !RUST_AST_DUMP_H