[PR testsuite/116860] Testsuite adjustment for recently added tests
[official-gcc.git] / gcc / rust / ast / rust-pattern.cc
blob85b3f5f3f5162da54b4cbd32772626b14dd93a06
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
9 version.
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.
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"
26 #include "rust-lex.h"
27 #include "rust-parse.h"
28 #include "rust-operators.h"
30 namespace Rust {
31 namespace AST {
33 std::string
34 LiteralPattern::as_string () const
36 return lit.as_string ();
39 std::string
40 IdentifierPattern::as_string () const
42 // TODO: maybe rewrite to work with non-linearisable patterns
43 std::string str;
45 if (is_ref)
46 str += "ref ";
48 if (is_mut)
49 str += "mut ";
51 str += variable_ident.as_string ();
53 if (has_pattern_to_bind ())
54 str += " @ " + to_bind->as_string ();
56 return str;
59 std::string
60 RangePatternBoundLiteral::as_string () const
62 std::string str;
64 if (has_minus)
65 str += "-";
67 str += literal.as_string ();
69 return str;
72 std::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 ();
78 else
79 return lower->as_string () + "..=" + upper->as_string ();
82 std::string
83 ReferencePattern::as_string () const
85 // TODO: maybe rewrite to work with non-linearisable patterns
86 std::string str ("&");
88 if (has_two_amps)
89 str += "&";
91 if (is_mut)
92 str += "mut ";
94 str += pattern->as_string ();
96 return str;
99 std::string
100 StructPatternField::as_string () const
102 // outer attributes
103 std::string str = append_attributes (outer_attrs, OUTER);
105 return str;
108 std::string
109 StructPatternFieldTuplePat::as_string () const
111 // TODO: maybe rewrite to work with non-linearisable patterns
112 std::string str = StructPatternField::as_string ();
114 str += "\n";
116 str += std::to_string (index) + " : " + tuple_pattern->as_string ();
118 return str;
121 std::string
122 StructPatternFieldIdentPat::as_string () const
124 // TODO: maybe rewrite to work with non-linearisable patterns
125 std::string str = StructPatternField::as_string ();
127 str += "\n";
129 str += ident.as_string () + " : " + ident_pattern->as_string ();
131 return str;
134 std::string
135 StructPatternFieldIdent::as_string () const
137 std::string str = StructPatternField::as_string ();
139 str += "\n";
141 if (has_ref)
142 str += "ref ";
144 if (has_mut)
145 str += "mut ";
147 str += ident.as_string ();
149 return str;
152 std::string
153 StructPatternElements::as_string () const
155 std::string str ("\n Fields: ");
157 if (!has_struct_pattern_fields ())
159 str += "none";
161 else
163 for (const auto &field : fields)
164 str += "\n " + field->as_string ();
167 str += "\n Etc: ";
168 if (has_struct_pattern_etc)
169 str += "true";
170 else
171 str += "false";
173 return str;
176 std::string
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 ())
185 str += "none";
186 else
187 str += elems.as_string ();
189 return str;
192 std::string
193 TupleStructItemsNoRange::as_string () const
195 std::string str;
197 for (const auto &pattern : patterns)
198 str += "\n " + pattern->as_string ();
200 return str;
203 std::string
204 TupleStructItemsRange::as_string () const
206 std::string str ("\n Lower patterns: ");
208 if (lower_patterns.empty ())
210 str += "none";
212 else
214 for (const auto &lower : lower_patterns)
215 str += "\n " + lower->as_string ();
218 str += "\n Upper patterns: ";
219 if (upper_patterns.empty ())
221 str += "none";
223 else
225 for (const auto &upper : upper_patterns)
226 str += "\n " + upper->as_string ();
229 return str;
232 std::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 ();
241 return str;
244 std::string
245 TuplePatternItemsMultiple::as_string () const
247 std::string str;
249 for (const auto &pattern : patterns)
250 str += "\n " + pattern->as_string ();
252 return str;
255 std::string
256 TuplePatternItemsRanged::as_string () const
258 std::string str;
260 str += "\n Lower patterns: ";
261 if (lower_patterns.empty ())
263 str += "none";
265 else
267 for (const auto &lower : lower_patterns)
268 str += "\n " + lower->as_string ();
271 str += "\n Upper patterns: ";
272 if (upper_patterns.empty ())
274 str += "none";
276 else
278 for (const auto &upper : upper_patterns)
279 str += "\n " + upper->as_string ();
282 return str;
285 std::string
286 TuplePattern::as_string () const
288 return "TuplePattern: " + items->as_string ();
291 std::string
292 GroupedExpr::as_string () const
294 std::string str ("Grouped expr:");
296 // outer attrs
297 str += append_attributes (outer_attrs, OUTER);
299 // inner attributes
300 str += append_attributes (inner_attrs, INNER);
302 str += "\n Expr in parens: " + expr_in_parens->as_string ();
304 return str;
307 std::string
308 SlicePattern::as_string () const
310 std::string str ("SlicePattern: ");
312 for (const auto &pattern : items)
313 str += "\n " + pattern->as_string ();
315 return str;
318 std::string
319 AltPattern::as_string () const
321 std::string str ("AltPattern: ");
323 for (const auto &pattern : alts)
324 str += "\n " + pattern->as_string ();
326 return str;
329 void
330 AltPattern::accept_vis (ASTVisitor &vis)
332 vis.visit (*this);
335 void
336 GroupedPattern::accept_vis (ASTVisitor &vis)
338 vis.visit (*this);
341 void
342 GroupedExpr::accept_vis (ASTVisitor &vis)
344 vis.visit (*this);
347 void
348 SlicePattern::accept_vis (ASTVisitor &vis)
350 vis.visit (*this);
353 void
354 TuplePatternItemsRanged::accept_vis (ASTVisitor &vis)
356 vis.visit (*this);
359 void
360 TuplePattern::accept_vis (ASTVisitor &vis)
362 vis.visit (*this);
365 void
366 TuplePatternItemsMultiple::accept_vis (ASTVisitor &vis)
368 vis.visit (*this);
371 void
372 LiteralPattern::accept_vis (ASTVisitor &vis)
374 vis.visit (*this);
377 void
378 IdentifierPattern::accept_vis (ASTVisitor &vis)
380 vis.visit (*this);
383 void
384 WildcardPattern::accept_vis (ASTVisitor &vis)
386 vis.visit (*this);
389 void
390 RestPattern::accept_vis (ASTVisitor &vis)
392 vis.visit (*this);
395 void
396 RangePatternBoundLiteral::accept_vis (ASTVisitor &vis)
398 vis.visit (*this);
401 void
402 RangePatternBoundPath::accept_vis (ASTVisitor &vis)
404 vis.visit (*this);
407 void
408 RangePatternBoundQualPath::accept_vis (ASTVisitor &vis)
410 vis.visit (*this);
413 void
414 RangePattern::accept_vis (ASTVisitor &vis)
416 vis.visit (*this);
419 void
420 ReferencePattern::accept_vis (ASTVisitor &vis)
422 vis.visit (*this);
425 void
426 StructPatternFieldTuplePat::accept_vis (ASTVisitor &vis)
428 vis.visit (*this);
431 void
432 StructPatternFieldIdentPat::accept_vis (ASTVisitor &vis)
434 vis.visit (*this);
437 void
438 StructPatternFieldIdent::accept_vis (ASTVisitor &vis)
440 vis.visit (*this);
443 void
444 StructPattern::accept_vis (ASTVisitor &vis)
446 vis.visit (*this);
449 void
450 TupleStructItemsNoRange::accept_vis (ASTVisitor &vis)
452 vis.visit (*this);
455 void
456 TupleStructItemsRange::accept_vis (ASTVisitor &vis)
458 vis.visit (*this);
461 void
462 TupleStructPattern::accept_vis (ASTVisitor &vis)
464 vis.visit (*this);
467 } // namespace AST
468 } // namespace Rust