Debian Codegraph @

gcc/rust/ast/rust-ast-formatting.cc

raw
1 /* General AST-related method implementations for Rust frontend.
2 Copyright (C) 2009-2025 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "rust-ast-full.h"
21
22 namespace Rust {
23 namespace AST {
24
25 std::string
26 indent_spaces (enum indent_mode mode)
27 {
28 static int indent = 0;
29 std::string str = "";
30 if (out == mode)
31 indent--;
32 for (int i = 0; i < indent; i++)
33 str += " ";
34 if (enter == mode)
35 indent++;
36
37 return str;
38 }
39
40 std::string
41 get_string_in_delims (std::string str_input, DelimType delim_type)
42 {
43 switch (delim_type)
44 {
45 case PARENS:
46 return "(" + str_input + ")";
47 case SQUARE:
48 return "[" + str_input + "]";
49 case CURLY:
50 return "{" + str_input + "}";
51 default:
52 return "ERROR-MARK-STRING (delims)";
53 }
54 rust_unreachable ();
55 }
56
57 std::string
58 get_mode_dump_desc (AttrMode mode)
59 {
60 switch (mode)
61 {
62 case OUTER:
63 return "outer attributes";
64 case INNER:
65 return "inner attributes";
66 default:
67 rust_unreachable ();
68 return "";
69 }
70 }
71
72 std::string
73 append_attributes (std::vector<Attribute> attrs, AttrMode mode)
74 {
75 indent_spaces (enter);
76
77 std::string str
78 = "\n" + indent_spaces (stay) + get_mode_dump_desc (mode) + ": ";
79 // str += "\n" + indent_spaces (stay) + "inner attributes: ";
80 if (attrs.empty ())
81 {
82 str += "none";
83 }
84 else
85 {
86 /* note that this does not print them with outer or "inner attribute"
87 * syntax - just prints the body */
88 for (const auto &attr : attrs)
89 str += "\n" + indent_spaces (stay) + attr.as_string ();
90 }
91
92 indent_spaces (out);
93
94 return str;
95 }
96
97 std::string
98 unquote_string (std::string input)
99 {
100 rust_assert (input.front () == '"');
101 rust_assert (input.back () == '"');
102 return input.substr (1, input.length () - 2);
103 }
104
105 } // namespace AST
106 } // namespace Rust