1 /* General AST-related method implementations for Rust frontend.
2 Copyright (C) 2009-2025 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
20 #include "rust-system.h"
21 #include "rust-ast-full.h"
22 #include "rust-diagnostics.h"
23 #include "rust-ast-visitor.h"
24 #include "rust-macro.h"
25 #include "rust-session-manager.h"
27 #include "rust-parse.h"
28 #include "rust-operators.h"
34 LiteralPattern::as_string () const
36 return lit
.as_string ();
40 IdentifierPattern::as_string () const
42 // TODO: maybe rewrite to work with non-linearisable patterns
51 str
+= variable_ident
.as_string ();
53 if (has_pattern_to_bind ())
54 str
+= " @ " + to_bind
->as_string ();
60 RangePatternBoundLiteral::as_string () const
67 str
+= literal
.as_string ();
73 RangePattern::as_string () const
75 // TODO: maybe rewrite to work with non-linearisable bounds
76 if (has_ellipsis_syntax
)
77 return lower
->as_string () + "..." + upper
->as_string ();
79 return lower
->as_string () + "..=" + upper
->as_string ();
83 ReferencePattern::as_string () const
85 // TODO: maybe rewrite to work with non-linearisable patterns
86 std::string
str ("&");
94 str
+= pattern
->as_string ();
100 StructPatternField::as_string () const
103 std::string str
= append_attributes (outer_attrs
, OUTER
);
109 StructPatternFieldTuplePat::as_string () const
111 // TODO: maybe rewrite to work with non-linearisable patterns
112 std::string str
= StructPatternField::as_string ();
116 str
+= std::to_string (index
) + " : " + tuple_pattern
->as_string ();
122 StructPatternFieldIdentPat::as_string () const
124 // TODO: maybe rewrite to work with non-linearisable patterns
125 std::string str
= StructPatternField::as_string ();
129 str
+= ident
.as_string () + " : " + ident_pattern
->as_string ();
135 StructPatternFieldIdent::as_string () const
137 std::string str
= StructPatternField::as_string ();
147 str
+= ident
.as_string ();
153 StructPatternElements::as_string () const
155 std::string
str ("\n Fields: ");
157 if (!has_struct_pattern_fields ())
163 for (const auto &field
: fields
)
164 str
+= "\n " + field
->as_string ();
168 if (has_struct_pattern_etc
)
177 StructPattern::as_string () const
179 std::string
str ("StructPattern: \n Path: ");
181 str
+= path
.as_string ();
183 str
+= "\n Struct pattern elems: ";
184 if (!has_struct_pattern_elems ())
187 str
+= elems
.as_string ();
193 TupleStructItemsNoRange::as_string () const
197 for (const auto &pattern
: patterns
)
198 str
+= "\n " + pattern
->as_string ();
204 TupleStructItemsRange::as_string () const
206 std::string
str ("\n Lower patterns: ");
208 if (lower_patterns
.empty ())
214 for (const auto &lower
: lower_patterns
)
215 str
+= "\n " + lower
->as_string ();
218 str
+= "\n Upper patterns: ";
219 if (upper_patterns
.empty ())
225 for (const auto &upper
: upper_patterns
)
226 str
+= "\n " + upper
->as_string ();
233 TupleStructPattern::as_string () const
235 std::string
str ("TupleStructPattern: \n Path: ");
237 str
+= path
.as_string ();
239 str
+= "\n Tuple struct items: " + items
->as_string ();
245 TuplePatternItemsMultiple::as_string () const
249 for (const auto &pattern
: patterns
)
250 str
+= "\n " + pattern
->as_string ();
256 TuplePatternItemsRanged::as_string () const
260 str
+= "\n Lower patterns: ";
261 if (lower_patterns
.empty ())
267 for (const auto &lower
: lower_patterns
)
268 str
+= "\n " + lower
->as_string ();
271 str
+= "\n Upper patterns: ";
272 if (upper_patterns
.empty ())
278 for (const auto &upper
: upper_patterns
)
279 str
+= "\n " + upper
->as_string ();
286 TuplePattern::as_string () const
288 return "TuplePattern: " + items
->as_string ();
292 GroupedExpr::as_string () const
294 std::string
str ("Grouped expr:");
297 str
+= append_attributes (outer_attrs
, OUTER
);
300 str
+= append_attributes (inner_attrs
, INNER
);
302 str
+= "\n Expr in parens: " + expr_in_parens
->as_string ();
308 SlicePattern::as_string () const
310 std::string
str ("SlicePattern: ");
312 for (const auto &pattern
: items
)
313 str
+= "\n " + pattern
->as_string ();
319 AltPattern::as_string () const
321 std::string
str ("AltPattern: ");
323 for (const auto &pattern
: alts
)
324 str
+= "\n " + pattern
->as_string ();
330 AltPattern::accept_vis (ASTVisitor
&vis
)
336 GroupedPattern::accept_vis (ASTVisitor
&vis
)
342 GroupedExpr::accept_vis (ASTVisitor
&vis
)
348 SlicePattern::accept_vis (ASTVisitor
&vis
)
354 TuplePatternItemsRanged::accept_vis (ASTVisitor
&vis
)
360 TuplePattern::accept_vis (ASTVisitor
&vis
)
366 TuplePatternItemsMultiple::accept_vis (ASTVisitor
&vis
)
372 LiteralPattern::accept_vis (ASTVisitor
&vis
)
378 IdentifierPattern::accept_vis (ASTVisitor
&vis
)
384 WildcardPattern::accept_vis (ASTVisitor
&vis
)
390 RestPattern::accept_vis (ASTVisitor
&vis
)
396 RangePatternBoundLiteral::accept_vis (ASTVisitor
&vis
)
402 RangePatternBoundPath::accept_vis (ASTVisitor
&vis
)
408 RangePatternBoundQualPath::accept_vis (ASTVisitor
&vis
)
414 RangePattern::accept_vis (ASTVisitor
&vis
)
420 ReferencePattern::accept_vis (ASTVisitor
&vis
)
426 StructPatternFieldTuplePat::accept_vis (ASTVisitor
&vis
)
432 StructPatternFieldIdentPat::accept_vis (ASTVisitor
&vis
)
438 StructPatternFieldIdent::accept_vis (ASTVisitor
&vis
)
444 StructPattern::accept_vis (ASTVisitor
&vis
)
450 TupleStructItemsNoRange::accept_vis (ASTVisitor
&vis
)
456 TupleStructItemsRange::accept_vis (ASTVisitor
&vis
)
462 TupleStructPattern::accept_vis (ASTVisitor
&vis
)