Skip to content

Commit d91e005

Browse files
committed
ast: Add reconstruct_type() method
gcc/rust/ChangeLog: * ast/rust-ast.h: Add reconstruct_type() and reconstruct_type_impl() * ast/rust-type.h: Implement them. * ast/rust-macro.h: Likewise. * ast/rust-path.h: Likewise.
1 parent 45c09ee commit d91e005

File tree

4 files changed

+191
-20
lines changed

4 files changed

+191
-20
lines changed

gcc/rust/ast/rust-ast.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,13 @@ class Type : public Visitable
15251525
return std::unique_ptr<Type> (clone_type_impl ());
15261526
}
15271527

1528+
// Similar to `clone_type`, but generates a new instance of the node with a
1529+
// different NodeId
1530+
std::unique_ptr<Type> reconstruct_type () const
1531+
{
1532+
return reconstruct (this, &Type::reconstruct_type_impl);
1533+
}
1534+
15281535
// virtual destructor
15291536
virtual ~Type () {}
15301537

@@ -1546,9 +1553,11 @@ class Type : public Visitable
15461553

15471554
protected:
15481555
Type () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}
1556+
Type (NodeId node_id) : node_id (node_id) {}
15491557

1550-
// Clone function implementation as pure virtual method
1558+
// Clone and reconstruct function implementations as pure virtual methods
15511559
virtual Type *clone_type_impl () const = 0;
1560+
virtual Type *reconstruct_type_impl () const = 0;
15521561

15531562
NodeId node_id;
15541563
};
@@ -1562,10 +1571,15 @@ class TypeNoBounds : public Type
15621571
{
15631572
return std::unique_ptr<TypeNoBounds> (clone_type_no_bounds_impl ());
15641573
}
1574+
std::unique_ptr<TypeNoBounds> reconstruct_type_no_bounds () const
1575+
{
1576+
return reconstruct (this, &TypeNoBounds::reconstruct_type_no_bounds_impl);
1577+
}
15651578

15661579
protected:
15671580
// Clone function implementation as pure virtual method
15681581
virtual TypeNoBounds *clone_type_no_bounds_impl () const = 0;
1582+
virtual TypeNoBounds *reconstruct_type_no_bounds_impl () const = 0;
15691583

15701584
/* Save having to specify two clone methods in derived classes by making
15711585
* type clone return typenobounds clone. Hopefully won't affect performance
@@ -1575,6 +1589,11 @@ class TypeNoBounds : public Type
15751589
return clone_type_no_bounds_impl ();
15761590
}
15771591

1592+
TypeNoBounds *reconstruct_type_impl () const final override
1593+
{
1594+
return reconstruct_type_no_bounds_impl ();
1595+
}
1596+
15781597
TypeNoBounds () : Type () {}
15791598
};
15801599

@@ -1596,6 +1615,11 @@ class TypeParamBound : public Visitable
15961615
{
15971616
return std::unique_ptr<TypeParamBound> (clone_type_param_bound_impl ());
15981617
}
1618+
std::unique_ptr<TypeParamBound> reconstruct_type_param_bound () const
1619+
{
1620+
return reconstruct (this,
1621+
&TypeParamBound::reconstruct_type_param_bound_impl);
1622+
}
15991623

16001624
virtual std::string as_string () const = 0;
16011625

@@ -1608,7 +1632,10 @@ class TypeParamBound : public Visitable
16081632
protected:
16091633
// Clone function implementation as pure virtual method
16101634
virtual TypeParamBound *clone_type_param_bound_impl () const = 0;
1635+
virtual TypeParamBound *reconstruct_type_param_bound_impl () const = 0;
16111636

1637+
TypeParamBound () : node_id (Analysis::Mappings::get ().get_next_node_id ())
1638+
{}
16121639
TypeParamBound (NodeId node_id) : node_id (node_id) {}
16131640

16141641
NodeId node_id;
@@ -1670,6 +1697,10 @@ class Lifetime : public TypeParamBound
16701697
{
16711698
return new Lifetime (node_id, lifetime_type, lifetime_name, locus);
16721699
}
1700+
Lifetime *reconstruct_type_param_bound_impl () const override
1701+
{
1702+
return new Lifetime (lifetime_type, lifetime_name, locus);
1703+
}
16731704
};
16741705

16751706
/* Base generic parameter in AST. Abstract - can be represented by a Lifetime

gcc/rust/ast/rust-macro.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -756,22 +756,16 @@ class MacroInvocation : public TypeNoBounds,
756756
std::vector<std::unique_ptr<MacroInvocation>> pending_eager_invocs;
757757

758758
protected:
759-
/* Use covariance to implement clone function as returning this object rather
760-
* than base */
761759
MacroInvocation *clone_pattern_impl () const final override
762760
{
763761
return clone_macro_invocation_impl ();
764762
}
765763

766-
/* Use covariance to implement clone function as returning this object rather
767-
* than base */
768764
MacroInvocation *clone_expr_without_block_impl () const final override
769765
{
770766
return clone_macro_invocation_impl ();
771767
}
772768

773-
/* Use covariance to implement clone function as returning this object rather
774-
* than base */
775769
MacroInvocation *clone_type_no_bounds_impl () const final override
776770
{
777771
return clone_macro_invocation_impl ();
@@ -782,12 +776,31 @@ class MacroInvocation : public TypeNoBounds,
782776
return clone_macro_invocation_impl ();
783777
}
784778

779+
MacroInvocation *reconstruct_type_no_bounds_impl () const final override
780+
{
781+
return reconstruct_macro_invocation_impl ();
782+
}
783+
785784
public:
786785
/*virtual*/ MacroInvocation *clone_macro_invocation_impl () const
787786
{
788787
return new MacroInvocation (*this);
789788
}
790789

790+
std::unique_ptr<MacroInvocation> reconstruct_macro_invocation () const
791+
{
792+
return reconstruct (this,
793+
&MacroInvocation::reconstruct_macro_invocation_impl);
794+
}
795+
796+
MacroInvocation *reconstruct_macro_invocation_impl () const
797+
{
798+
return new MacroInvocation (
799+
kind, builtin_kind, invoc_data, outer_attrs, locus, is_semi_coloned,
800+
reconstruct_vec (pending_eager_invocs,
801+
&MacroInvocation::reconstruct_macro_invocation));
802+
}
803+
791804
void add_semicolon () override { is_semi_coloned = true; }
792805

793806
Pattern::Kind get_pattern_kind () override

gcc/rust/ast/rust-path.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ class ConstGenericParam : public GenericParam
377377
return *type;
378378
}
379379

380+
tl::optional<GenericArg> &get_default_value () { return default_value; }
381+
const tl::optional<GenericArg> &get_default_value () const
382+
{
383+
return default_value;
384+
}
385+
380386
GenericArg &get_default_value_unchecked ()
381387
{
382388
rust_assert (has_default_value ());
@@ -779,6 +785,11 @@ class TypePathSegment
779785
{
780786
return new TypePathSegment (*this);
781787
}
788+
virtual TypePathSegment *reconstruct_type_path_segment_impl () const
789+
{
790+
return new TypePathSegment (lang_item, ident_segment,
791+
has_separating_scope_resolution, locus);
792+
}
782793

783794
public:
784795
virtual ~TypePathSegment () {}
@@ -790,6 +801,12 @@ class TypePathSegment
790801
{
791802
return std::unique_ptr<TypePathSegment> (clone_type_path_segment_impl ());
792803
}
804+
// Unique pointer custom reconstruct function
805+
std::unique_ptr<TypePathSegment> reconstruct_type_path_segment () const
806+
{
807+
return std::unique_ptr<TypePathSegment> (
808+
reconstruct_type_path_segment_impl ());
809+
}
793810

794811
TypePathSegment (PathIdentSegment ident_segment,
795812
bool has_separating_scope_resolution, location_t locus)
@@ -814,6 +831,15 @@ class TypePathSegment
814831
node_id (Analysis::Mappings::get ().get_next_node_id ())
815832
{}
816833

834+
// General constructor
835+
TypePathSegment (tl::optional<LangItem::Kind> lang_item,
836+
tl::optional<PathIdentSegment> ident_segment,
837+
bool has_separating_scope_resolution, location_t locus)
838+
: lang_item (lang_item), ident_segment (ident_segment), locus (locus),
839+
has_separating_scope_resolution (has_separating_scope_resolution),
840+
node_id (Analysis::Mappings::get ().get_next_node_id ())
841+
{}
842+
817843
TypePathSegment (TypePathSegment const &other)
818844
: lang_item (other.lang_item), ident_segment (other.ident_segment),
819845
locus (other.locus),
@@ -1145,6 +1171,13 @@ class TypePath : public TypeNoBounds
11451171
{
11461172
return new TypePath (*this);
11471173
}
1174+
TypePath *reconstruct_type_no_bounds_impl () const override
1175+
{
1176+
return new TypePath (
1177+
reconstruct_vec (segments,
1178+
&TypePathSegment::reconstruct_type_path_segment),
1179+
locus, has_opening_scope_resolution);
1180+
}
11481181

11491182
public:
11501183
/* Returns whether the TypePath has an opening scope resolution operator
@@ -1436,6 +1469,14 @@ class QualifiedPathInType : public TypeNoBounds
14361469
{
14371470
return new QualifiedPathInType (*this);
14381471
}
1472+
QualifiedPathInType *reconstruct_type_no_bounds_impl () const override
1473+
{
1474+
return new QualifiedPathInType (
1475+
path_type, associated_segment->reconstruct_type_path_segment (),
1476+
reconstruct_vec (segments,
1477+
&TypePathSegment::reconstruct_type_path_segment),
1478+
locus);
1479+
}
14391480

14401481
public:
14411482
QualifiedPathInType (

0 commit comments

Comments
 (0)