Skip to content

Commit 7b2721e

Browse files
committed
ast: Implement reconstruct() logic for Pattern nodes
This patch implements the `reconstruct()` pattern for the `Pattern` AST hierarchy. This is Phase 3 of the AST reconstruction refactor (following Types and Paths), designed to allow deep copying of AST nodes while guaranteeing the generation of fresh NodeIDs. This is essential for macro expansion (specifically `derive` macros), where patterns must be duplicated without sharing NodeIDs to avoid resolution collisions. Key changes include: - Implementing `reconstruct_impl` for all `Pattern` subclasses. - Adding `reconstruct` support to helper classes (`RangePatternBound`, `StructPatternField`, `PatternItems`). - Implementing typed `reconstruct()` wrappers for item families (`TupleStructItems`, `TuplePatternItems`, `SlicePatternItems`) to ensure type safety and avoid manual downcasting. - Adding null-safety checks in copy constructors and reconstruction logic to handle optional fields (e.g., open-ended ranges or stripped nodes) without crashing. - Integrating with `PathInExpression::reconstruct()` for struct patterns. gcc/rust/ChangeLog: * ast/rust-pattern.h (Pattern::reconstruct_impl): New virtual method. (LiteralPattern::reconstruct_impl): Implement reconstruction. (IdentifierPattern::reconstruct_impl): Likewise. (WildcardPattern::reconstruct_impl): Likewise. (RestPattern::reconstruct_impl): Likewise. (RangePatternBound::reconstruct): New method. (RangePatternBound::reconstruct_impl): New virtual method. (RangePatternBoundLiteral::reconstruct_impl): Implement reconstruction. (RangePatternBoundPath::reconstruct_impl): Likewise. (RangePatternBoundQualPath::reconstruct_impl): Likewise. (RangePattern::RangePattern): Fix copy constructor null safety. (RangePattern::reconstruct_impl): Implement reconstruction with guards. (ReferencePattern::ReferencePattern): Fix copy constructor null safety. (ReferencePattern::reconstruct_impl): Implement reconstruction. (StructPatternField::reconstruct): New method. (StructPatternField::reconstruct_impl): New virtual method. (StructPatternField::operator=): Default the assignment operator. (StructPatternFieldTuplePat::reconstruct_impl): Implement reconstruction. (StructPatternFieldIdentPat::reconstruct_impl): Likewise. (StructPatternFieldIdent::reconstruct_impl): Likewise. (StructPatternElements::reconstruct): New method. (StructPattern::reconstruct_impl): Implement reconstruction using path reconstruct. (PatternItems::reconstruct): New protected method. (PatternItems::reconstruct_impl): New virtual method. (TupleStructItems::reconstruct): New typed reconstruction method. (TupleStructItems::reconstruct_impl): New pure virtual implementation. (TupleStructItemsNoRest::reconstruct_impl): Implement reconstruction. (TupleStructItemsHasRest::reconstruct_impl): Likewise. (TupleStructPattern::reconstruct_impl): Implement reconstruction. (TuplePatternItems::reconstruct): New typed reconstruction method. (TuplePatternItems::reconstruct_impl): New pure virtual implementation. (TuplePatternItemsNoRest::reconstruct_impl): Implement reconstruction. (TuplePatternItemsHasRest::reconstruct_impl): Likewise. (TuplePattern::reconstruct_impl): Implement reconstruction. (GroupedPattern::reconstruct_impl): Likewise. (SlicePatternItems::reconstruct): New typed reconstruction method. (SlicePatternItems::reconstruct_impl): New pure virtual implementation. (SlicePatternItemsNoRest::reconstruct_impl): Implement reconstruction. (SlicePatternItemsHasRest::reconstruct_impl): Likewise. (SlicePattern::reconstruct_impl): Implement reconstruction. (AltPattern::reconstruct_impl): Likewise. Signed-off-by: Jayant Chauhan <0001jayant@gmail.com>
1 parent b98bdc9 commit 7b2721e

File tree

1 file changed

+263
-8
lines changed

1 file changed

+263
-8
lines changed

0 commit comments

Comments
 (0)