Skip to content

Commit 36b8028

Browse files
Polygonalrphilberty
authored andcommitted
gccrs: Add rest pattern support for AST::SlicePattern
The main change can be found in ast/rust-pattern.h, which introduces 2 item types for AST::SlicePattern - one without rest pattern (SlicePatternItemsNoRest) & the other with it (SlicePatternItemsHasRest). This led to a number of cascading changes as seen in the changelog. gcc/rust/ChangeLog: * ast/rust-ast-collector.cc: Add support for the 2 new classes. * ast/rust-ast-collector.h: Header file update for above. * ast/rust-ast-full-decls.h: Add forward decls for the 2 new classes. * ast/rust-ast-visitor.cc: Add visit support for the 2 new classes. * ast/rust-ast-visitor.h: Header file update for above. * ast/rust-pattern.cc: Implementation of certain methods for the 2 new classes. * ast/rust-pattern.h: Define the 2 new classes. Update SlicePattern to be able to hold 2 kinds of items - SlicePatternItemsNoRest or SlicePatternItemsRest. * expand/rust-cfg-strip.cc: Add support for the 2 new classes. * expand/rust-cfg-strip.h: Header file update for above. * expand/rust-derive.h: Add visits for the 2 new classes. * hir/rust-ast-lower-base.cc: Add visits for the 2 new classes. * hir/rust-ast-lower-base.h: Header file update for above. * hir/rust-ast-lower-pattern.cc: Update lowering of SlicePattern to support SlicePatternItemsNoRest. * parse/rust-parse-impl.h (parse_slice_pattern()): Add support for parsing DOT_DOT into respective SlicePatternItems. * resolve/rust-ast-resolve-base.cc: Add visits for the 2 new classes. * resolve/rust-ast-resolve-base.h: Header file update for above. * resolve/rust-ast-resolve-pattern.cc: Update SlicePattern resolution to support new classes. Signed-off-by: Yap Zhi Heng <[email protected]>
1 parent eefda40 commit 36b8028

17 files changed

+447
-42
lines changed

gcc/rust/ast/rust-ast-collector.cc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2717,11 +2717,35 @@ TokenCollector::visit (GroupedPattern &pattern)
27172717
push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
27182718
}
27192719

2720+
void
2721+
TokenCollector::visit (SlicePatternItemsNoRest &items)
2722+
{
2723+
visit_items_joined_by_separator (items.get_patterns (), COMMA);
2724+
}
2725+
2726+
void
2727+
TokenCollector::visit (SlicePatternItemsHasRest &items)
2728+
{
2729+
if (!items.get_lower_patterns ().empty ())
2730+
{
2731+
visit_items_joined_by_separator (items.get_lower_patterns (), COMMA);
2732+
push (Rust::Token::make (COMMA, UNDEF_LOCATION));
2733+
}
2734+
2735+
push (Rust::Token::make (DOT_DOT, UNDEF_LOCATION));
2736+
2737+
if (!items.get_upper_patterns ().empty ())
2738+
{
2739+
push (Rust::Token::make (COMMA, UNDEF_LOCATION));
2740+
visit_items_joined_by_separator (items.get_upper_patterns (), COMMA);
2741+
}
2742+
}
2743+
27202744
void
27212745
TokenCollector::visit (SlicePattern &pattern)
27222746
{
27232747
push (Rust::Token::make (LEFT_SQUARE, pattern.get_locus ()));
2724-
visit_items_joined_by_separator (pattern.get_items (), COMMA);
2748+
visit (pattern.get_items ());
27252749
push (Rust::Token::make (RIGHT_SQUARE, UNDEF_LOCATION));
27262750
}
27272751

gcc/rust/ast/rust-ast-collector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ class TokenCollector : public ASTVisitor
378378
void visit (TuplePatternItemsRanged &tuple_items);
379379
void visit (TuplePattern &pattern);
380380
void visit (GroupedPattern &pattern);
381+
void visit (SlicePatternItemsNoRest &items);
382+
void visit (SlicePatternItemsHasRest &items);
381383
void visit (SlicePattern &pattern);
382384
void visit (AltPattern &pattern);
383385

gcc/rust/ast/rust-ast-full-decls.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ class TuplePatternItemsMultiple;
248248
class TuplePatternItemsRanged;
249249
class TuplePattern;
250250
class GroupedPattern;
251+
class SlicePatternItemsNoRest;
252+
class SlicePatternItemsHasRest;
251253
class SlicePattern;
252254
class AltPattern;
253255

gcc/rust/ast/rust-ast-visitor.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,12 +1347,27 @@ DefaultASTVisitor::visit (AST::GroupedPattern &pattern)
13471347
}
13481348

13491349
void
1350-
DefaultASTVisitor::visit (AST::SlicePattern &pattern)
1350+
DefaultASTVisitor::visit (AST::SlicePatternItemsNoRest &items)
13511351
{
1352-
for (auto &item : pattern.get_items ())
1352+
for (auto &item : items.get_patterns ())
1353+
visit (item);
1354+
}
1355+
1356+
void
1357+
DefaultASTVisitor::visit (AST::SlicePatternItemsHasRest &items)
1358+
{
1359+
for (auto &item : items.get_lower_patterns ())
1360+
visit (item);
1361+
for (auto &item : items.get_upper_patterns ())
13531362
visit (item);
13541363
}
13551364

1365+
void
1366+
DefaultASTVisitor::visit (AST::SlicePattern &pattern)
1367+
{
1368+
visit (pattern.get_items ());
1369+
}
1370+
13561371
void
13571372
DefaultASTVisitor::visit (AST::AltPattern &pattern)
13581373
{

gcc/rust/ast/rust-ast-visitor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ class ASTVisitor
212212
virtual void visit (TuplePatternItemsRanged &tuple_items) = 0;
213213
virtual void visit (TuplePattern &pattern) = 0;
214214
virtual void visit (GroupedPattern &pattern) = 0;
215+
virtual void visit (SlicePatternItemsNoRest &items) = 0;
216+
virtual void visit (SlicePatternItemsHasRest &items) = 0;
215217
virtual void visit (SlicePattern &pattern) = 0;
216218
virtual void visit (AltPattern &pattern) = 0;
217219

@@ -386,6 +388,8 @@ class DefaultASTVisitor : public ASTVisitor
386388
virtual void visit (AST::TuplePatternItemsRanged &tuple_items) override;
387389
virtual void visit (AST::TuplePattern &pattern) override;
388390
virtual void visit (AST::GroupedPattern &pattern) override;
391+
virtual void visit (AST::SlicePatternItemsNoRest &items) override;
392+
virtual void visit (AST::SlicePatternItemsHasRest &items) override;
389393
virtual void visit (AST::SlicePattern &pattern) override;
390394
virtual void visit (AST::AltPattern &pattern) override;
391395
virtual void visit (AST::EmptyStmt &stmt) override;

gcc/rust/ast/rust-pattern.cc

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,52 @@ GroupedExpr::as_string () const
327327
}
328328

329329
std::string
330-
SlicePattern::as_string () const
330+
SlicePatternItemsNoRest::as_string () const
331331
{
332-
std::string str ("SlicePattern: ");
332+
std::string str;
333333

334-
for (const auto &pattern : items)
334+
for (const auto &pattern : patterns)
335335
str += "\n " + pattern->as_string ();
336336

337337
return str;
338338
}
339339

340+
std::string
341+
SlicePatternItemsHasRest::as_string () const
342+
{
343+
std::string str;
344+
345+
str += "\n Lower patterns: ";
346+
if (lower_patterns.empty ())
347+
{
348+
str += "none";
349+
}
350+
else
351+
{
352+
for (const auto &lower : lower_patterns)
353+
str += "\n " + lower->as_string ();
354+
}
355+
356+
str += "\n Upper patterns: ";
357+
if (upper_patterns.empty ())
358+
{
359+
str += "none";
360+
}
361+
else
362+
{
363+
for (const auto &upper : upper_patterns)
364+
str += "\n " + upper->as_string ();
365+
}
366+
367+
return str;
368+
}
369+
370+
std::string
371+
SlicePattern::as_string () const
372+
{
373+
return "SlicePattern: " + items->as_string ();
374+
}
375+
340376
std::string
341377
AltPattern::as_string () const
342378
{
@@ -366,6 +402,18 @@ GroupedExpr::accept_vis (ASTVisitor &vis)
366402
vis.visit (*this);
367403
}
368404

405+
void
406+
SlicePatternItemsNoRest::accept_vis (ASTVisitor &vis)
407+
{
408+
vis.visit (*this);
409+
}
410+
411+
void
412+
SlicePatternItemsHasRest::accept_vis (ASTVisitor &vis)
413+
{
414+
vis.visit (*this);
415+
}
416+
369417
void
370418
SlicePattern::accept_vis (ASTVisitor &vis)
371419
{

0 commit comments

Comments
 (0)