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-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-lex.h"
26 : #include "rust-parse.h"
27 : #include "rust-operators.h"
28 :
29 : namespace Rust {
30 : namespace AST {
31 :
32 : RangeKind
33 177 : tokenid_to_rangekind (TokenId id)
34 : {
35 177 : switch (id)
36 : {
37 : case DOT_DOT_EQ:
38 : return RangeKind::INCLUDED;
39 : case ELLIPSIS:
40 : return RangeKind::ELLIPSIS;
41 : case DOT_DOT:
42 : return RangeKind::EXCLUDED;
43 0 : default:
44 0 : rust_unreachable ();
45 : }
46 : }
47 :
48 : std::string
49 0 : LiteralPattern::as_string () const
50 : {
51 0 : return (has_minus ? "-" : "") + lit.as_string ();
52 : }
53 :
54 : std::string
55 0 : IdentifierPattern::as_string () const
56 : {
57 : // TODO: maybe rewrite to work with non-linearisable patterns
58 0 : std::string str;
59 :
60 0 : if (is_ref)
61 0 : str += "ref ";
62 :
63 0 : if (is_mut)
64 0 : str += "mut ";
65 :
66 0 : str += variable_ident.as_string ();
67 :
68 0 : if (has_subpattern ())
69 0 : str += " @ " + subpattern.get ()->as_string ();
70 :
71 0 : return str;
72 : }
73 :
74 : std::string
75 0 : RangePatternBoundLiteral::as_string () const
76 : {
77 0 : std::string str;
78 :
79 0 : if (has_minus)
80 0 : str += "-";
81 :
82 0 : str += literal.as_string ();
83 :
84 0 : return str;
85 : }
86 :
87 : std::string
88 0 : RangePattern::as_string () const
89 : {
90 : // TODO: maybe rewrite to work with non-linearisable bounds
91 0 : switch (range_kind)
92 : {
93 0 : case RangeKind::EXCLUDED:
94 0 : return lower.get ()->as_string () + ".." + upper.get ()->as_string ();
95 0 : case RangeKind::INCLUDED:
96 0 : return lower.get ()->as_string () + "..=" + upper.get ()->as_string ();
97 0 : case RangeKind::ELLIPSIS:
98 0 : return lower.get ()->as_string () + "..." + upper.get ()->as_string ();
99 0 : default:
100 0 : rust_unreachable ();
101 : }
102 : }
103 :
104 : std::string
105 0 : ReferencePattern::as_string () const
106 : {
107 : // TODO: maybe rewrite to work with non-linearisable patterns
108 0 : std::string str ("&");
109 :
110 0 : if (has_two_amps)
111 0 : str += "&";
112 :
113 0 : if (is_mut)
114 0 : str += "mut ";
115 :
116 0 : str += pattern.get ()->as_string ();
117 :
118 0 : return str;
119 : }
120 :
121 : std::string
122 0 : StructPatternField::as_string () const
123 : {
124 : // outer attributes
125 0 : std::string str = append_attributes (outer_attrs, OUTER);
126 :
127 0 : return str;
128 : }
129 :
130 : std::string
131 0 : StructPatternFieldTuplePat::as_string () const
132 : {
133 : // TODO: maybe rewrite to work with non-linearisable patterns
134 0 : std::string str = StructPatternField::as_string ();
135 :
136 0 : str += "\n";
137 :
138 0 : str += std::to_string (index) + " : " + tuple_pattern.get ()->as_string ();
139 :
140 0 : return str;
141 : }
142 :
143 : std::string
144 0 : StructPatternFieldIdentPat::as_string () const
145 : {
146 : // TODO: maybe rewrite to work with non-linearisable patterns
147 0 : std::string str = StructPatternField::as_string ();
148 :
149 0 : str += "\n";
150 :
151 0 : str += ident.as_string () + " : " + ident_pattern.get ()->as_string ();
152 :
153 0 : return str;
154 : }
155 :
156 : std::string
157 0 : StructPatternFieldIdent::as_string () const
158 : {
159 0 : std::string str = StructPatternField::as_string ();
160 :
161 0 : str += "\n";
162 :
163 0 : if (has_ref)
164 0 : str += "ref ";
165 :
166 0 : if (has_mut)
167 0 : str += "mut ";
168 :
169 0 : str += ident.as_string ();
170 :
171 0 : return str;
172 : }
173 :
174 : std::string
175 0 : StructPatternElements::as_string () const
176 : {
177 0 : std::string str ("\n Fields: ");
178 :
179 0 : if (!has_struct_pattern_fields ())
180 : {
181 0 : str += "none";
182 : }
183 : else
184 : {
185 0 : for (const auto &field : fields.get ())
186 0 : str += "\n " + field->as_string ();
187 : }
188 :
189 0 : str += "\n Has rest: ";
190 0 : if (has_rest_pattern)
191 0 : str += "true";
192 : else
193 0 : str += "false";
194 :
195 0 : return str;
196 : }
197 :
198 : std::string
199 0 : StructPattern::as_string () const
200 : {
201 0 : std::string str ("StructPattern: \n Path: ");
202 :
203 0 : str += path.as_string ();
204 :
205 0 : str += "\n Struct pattern elems: ";
206 0 : if (!has_struct_pattern_elems ())
207 0 : str += "none";
208 : else
209 0 : str += elems.as_string ();
210 :
211 0 : return str;
212 : }
213 :
214 : std::string
215 0 : TupleStructItemsNoRest::as_string () const
216 : {
217 0 : std::string str;
218 :
219 0 : for (const auto &pattern : patterns.get ())
220 0 : str += "\n " + pattern->as_string ();
221 :
222 0 : return str;
223 : }
224 :
225 : std::string
226 0 : TupleStructItemsHasRest::as_string () const
227 : {
228 0 : std::string str ("\n Lower patterns: ");
229 :
230 0 : if (lower_patterns.get ().empty ())
231 : {
232 0 : str += "none";
233 : }
234 : else
235 : {
236 0 : for (const auto &lower : lower_patterns.get ())
237 0 : str += "\n " + lower->as_string ();
238 : }
239 :
240 0 : str += "\n Upper patterns: ";
241 0 : if (upper_patterns.get ().empty ())
242 : {
243 0 : str += "none";
244 : }
245 : else
246 : {
247 0 : for (const auto &upper : upper_patterns.get ())
248 0 : str += "\n " + upper->as_string ();
249 : }
250 :
251 0 : return str;
252 : }
253 :
254 : std::string
255 0 : TupleStructPattern::as_string () const
256 : {
257 0 : std::string str ("TupleStructPattern: \n Path: ");
258 :
259 0 : str += path.as_string ();
260 :
261 0 : str += "\n Tuple struct items: " + items.get ()->as_string ();
262 :
263 0 : return str;
264 : }
265 :
266 : std::string
267 0 : TuplePatternItemsNoRest::as_string () const
268 : {
269 0 : std::string str;
270 :
271 0 : for (const auto &pattern : patterns.get ())
272 0 : str += "\n " + pattern->as_string ();
273 :
274 0 : return str;
275 : }
276 :
277 : std::string
278 0 : TuplePatternItemsHasRest::as_string () const
279 : {
280 0 : std::string str;
281 :
282 0 : str += "\n Lower patterns: ";
283 0 : if (lower_patterns.get ().empty ())
284 : {
285 0 : str += "none";
286 : }
287 : else
288 : {
289 0 : for (const auto &lower : lower_patterns.get ())
290 0 : str += "\n " + lower->as_string ();
291 : }
292 :
293 0 : str += "\n Upper patterns: ";
294 0 : if (upper_patterns.get ().empty ())
295 : {
296 0 : str += "none";
297 : }
298 : else
299 : {
300 0 : for (const auto &upper : upper_patterns.get ())
301 0 : str += "\n " + upper->as_string ();
302 : }
303 :
304 0 : return str;
305 : }
306 :
307 : std::string
308 0 : TuplePattern::as_string () const
309 : {
310 0 : return "TuplePattern: " + items.get ()->as_string ();
311 : }
312 :
313 : std::string
314 0 : GroupedExpr::as_string () const
315 : {
316 0 : std::string str ("Grouped expr:");
317 :
318 : // outer attrs
319 0 : str += append_attributes (outer_attrs, OUTER);
320 :
321 : // inner attributes
322 0 : str += append_attributes (inner_attrs, INNER);
323 :
324 0 : str += "\n Expr in parens: " + expr_in_parens->as_string ();
325 :
326 0 : return str;
327 : }
328 :
329 : std::string
330 0 : SlicePattern::as_string () const
331 : {
332 0 : std::string str;
333 :
334 0 : str = "SlicePattern: ";
335 0 : for (const auto &pattern : patterns.get ())
336 0 : str += "\n " + pattern->as_string ();
337 :
338 0 : return str;
339 : }
340 :
341 : std::string
342 0 : AltPattern::as_string () const
343 : {
344 0 : std::string str ("AltPattern: ");
345 :
346 0 : for (const auto &pattern : alts.get ())
347 0 : str += "\n " + pattern->as_string ();
348 :
349 0 : return str;
350 : }
351 :
352 : void
353 14465 : AltPattern::accept_vis (ASTVisitor &vis)
354 : {
355 14465 : vis.visit (*this);
356 14465 : }
357 :
358 : void
359 1063 : GroupedPattern::accept_vis (ASTVisitor &vis)
360 : {
361 1063 : vis.visit (*this);
362 1063 : }
363 :
364 : void
365 1580163 : GroupedExpr::accept_vis (ASTVisitor &vis)
366 : {
367 1580163 : vis.visit (*this);
368 1580163 : }
369 :
370 : void
371 5749 : SlicePattern::accept_vis (ASTVisitor &vis)
372 : {
373 5749 : vis.visit (*this);
374 5749 : }
375 :
376 : void
377 859 : TuplePatternItemsHasRest::accept_vis (ASTVisitor &vis)
378 : {
379 859 : vis.visit (*this);
380 859 : }
381 :
382 : void
383 668813 : TuplePattern::accept_vis (ASTVisitor &vis)
384 : {
385 668813 : vis.visit (*this);
386 668813 : }
387 :
388 : void
389 667492 : TuplePatternItemsNoRest::accept_vis (ASTVisitor &vis)
390 : {
391 667492 : vis.visit (*this);
392 667492 : }
393 :
394 : void
395 10425621 : LiteralPattern::accept_vis (ASTVisitor &vis)
396 : {
397 10425621 : vis.visit (*this);
398 10425621 : }
399 :
400 : void
401 6449118 : IdentifierPattern::accept_vis (ASTVisitor &vis)
402 : {
403 6449118 : vis.visit (*this);
404 6449118 : }
405 :
406 : void
407 1037514 : WildcardPattern::accept_vis (ASTVisitor &vis)
408 : {
409 1037514 : vis.visit (*this);
410 1037514 : }
411 :
412 : void
413 3663 : RestPattern::accept_vis (ASTVisitor &vis)
414 : {
415 3663 : vis.visit (*this);
416 3663 : }
417 :
418 : void
419 47174 : RangePatternBoundLiteral::accept_vis (ASTVisitor &vis)
420 : {
421 47174 : vis.visit (*this);
422 47174 : }
423 :
424 : void
425 420 : RangePatternBoundPath::accept_vis (ASTVisitor &vis)
426 : {
427 420 : vis.visit (*this);
428 420 : }
429 :
430 : void
431 0 : RangePatternBoundQualPath::accept_vis (ASTVisitor &vis)
432 : {
433 0 : vis.visit (*this);
434 0 : }
435 :
436 : void
437 23973 : RangePattern::accept_vis (ASTVisitor &vis)
438 : {
439 23973 : vis.visit (*this);
440 23973 : }
441 :
442 : void
443 60509 : ReferencePattern::accept_vis (ASTVisitor &vis)
444 : {
445 60509 : vis.visit (*this);
446 60509 : }
447 :
448 : void
449 528 : StructPatternFieldTuplePat::accept_vis (ASTVisitor &vis)
450 : {
451 528 : vis.visit (*this);
452 528 : }
453 :
454 : void
455 13723 : StructPatternFieldIdentPat::accept_vis (ASTVisitor &vis)
456 : {
457 13723 : vis.visit (*this);
458 13723 : }
459 :
460 : void
461 5894 : StructPatternFieldIdent::accept_vis (ASTVisitor &vis)
462 : {
463 5894 : vis.visit (*this);
464 5894 : }
465 :
466 : void
467 14536 : StructPattern::accept_vis (ASTVisitor &vis)
468 : {
469 14536 : vis.visit (*this);
470 14536 : }
471 :
472 : void
473 243042 : TupleStructItemsNoRest::accept_vis (ASTVisitor &vis)
474 : {
475 243042 : vis.visit (*this);
476 243042 : }
477 :
478 : void
479 2922 : TupleStructItemsHasRest::accept_vis (ASTVisitor &vis)
480 : {
481 2922 : vis.visit (*this);
482 2922 : }
483 :
484 : void
485 246989 : TupleStructPattern::accept_vis (ASTVisitor &vis)
486 : {
487 246989 : vis.visit (*this);
488 246989 : }
489 :
490 : } // namespace AST
491 : } // namespace Rust
|