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