Line data Source code
1 : /* General AST-related method implementations for Rust frontend.
2 : Copyright (C) 2009-2026 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 0 : indent_spaces (enum indent_mode mode)
27 : {
28 0 : static int indent = 0;
29 0 : std::string str = "";
30 0 : if (out == mode)
31 0 : indent--;
32 0 : for (int i = 0; i < indent; i++)
33 0 : str += " ";
34 0 : if (enter == mode)
35 0 : indent++;
36 :
37 0 : return str;
38 : }
39 :
40 : std::string
41 0 : get_string_in_delims (std::string str_input, DelimType delim_type)
42 : {
43 0 : switch (delim_type)
44 : {
45 0 : case PARENS:
46 0 : return "(" + str_input + ")";
47 0 : case SQUARE:
48 0 : return "[" + str_input + "]";
49 0 : case CURLY:
50 0 : return "{" + str_input + "}";
51 0 : default:
52 0 : return "ERROR-MARK-STRING (delims)";
53 : }
54 : rust_unreachable ();
55 : }
56 :
57 : std::string
58 0 : get_mode_dump_desc (AttrMode mode)
59 : {
60 0 : switch (mode)
61 : {
62 0 : case OUTER:
63 0 : return "outer attributes";
64 0 : case INNER:
65 0 : return "inner attributes";
66 0 : default:
67 0 : rust_unreachable ();
68 : return "";
69 : }
70 : }
71 :
72 : std::string
73 0 : append_attributes (std::vector<Attribute> attrs, AttrMode mode)
74 : {
75 0 : indent_spaces (enter);
76 :
77 0 : std::string str
78 0 : = "\n" + indent_spaces (stay) + get_mode_dump_desc (mode) + ": ";
79 : // str += "\n" + indent_spaces (stay) + "inner attributes: ";
80 0 : if (attrs.empty ())
81 : {
82 0 : 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 0 : for (const auto &attr : attrs)
89 0 : str += "\n" + indent_spaces (stay) + attr.as_string ();
90 : }
91 :
92 0 : indent_spaces (out);
93 :
94 0 : return str;
95 : }
96 :
97 : std::string
98 0 : unquote_string (std::string input)
99 : {
100 0 : rust_assert (input.front () == '"');
101 0 : rust_assert (input.back () == '"');
102 0 : return input.substr (1, input.length () - 2);
103 : }
104 :
105 : } // namespace AST
106 : } // namespace Rust
|