| 1 |
// Copyright (C) 2020-2025 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 |
|
| 26 |
#include "rust-ast-collector.h" |
| 27 |
|
| 28 |
namespace Rust { |
| 29 |
namespace AST { |
| 30 |
|
| 31 |
class Dump |
| 32 |
{ |
| 33 |
public: |
| 34 |
Dump (std::ostream &stream); |
| 35 |
|
| 36 |
/** |
| 37 |
* Run the visitor on an entire crate and its items |
| 38 |
*/ |
| 39 |
void go (AST::Crate &crate); |
| 40 |
void go (AST::Item &item); |
| 41 |
|
| 42 |
template <typename T> void process (T &v) |
| 43 |
{ |
| 44 |
TokenCollector collector; |
| 45 |
collector.visit (v); |
| 46 |
|
| 47 |
TokenPtr previous = nullptr; |
| 48 |
for (auto item : collector.collect ()) |
| 49 |
{ |
| 50 |
switch (item.get_kind ()) |
| 51 |
{ |
| 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; |
| 58 |
} |
| 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++) |
| 65 |
{ |
| 66 |
stream << " "; |
| 67 |
} |
| 68 |
break; |
| 69 |
case AST::CollectItem::Kind::Newline: |
| 70 |
stream << "\n"; |
| 71 |
previous = nullptr; |
| 72 |
break; |
| 73 |
default: |
| 74 |
rust_unreachable (); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// Helper method to get a quick debug dump to standard error output |
| 80 |
static void debug (Visitable &v); |
| 81 |
|
| 82 |
private: |
| 83 |
std::ostream &stream; |
| 84 |
Indent indentation; |
| 85 |
|
| 86 |
static bool require_spacing (TokenPtr previous, TokenPtr current); |
| 87 |
}; |
| 88 |
|
| 89 |
} // namespace AST |
| 90 |
} // namespace Rust |
| 91 |
|
| 92 |
// In the global namespace to make it easier to call from debugger |
| 93 |
void |
| 94 |
debug (Rust::AST::Visitable &v); |
| 95 |
|
| 96 |
#endif // !RUST_AST_DUMP_H |