diff mbox series

[Ada] Unchecked_Convert_To: set Parent

Message ID 20210707162505.GA2542998@adacore.com
State New
Headers show
Series [Ada] Unchecked_Convert_To: set Parent | expand

Commit Message

Pierre-Marie de Rodat July 7, 2021, 4:25 p.m. UTC
A previous change to Unchecked_Convert_To removed the setting of the
Parent of the new node, because it was thought to be unnecessary.
However, in rare cases, it is necessary because for example
Remove_Side_Effects is called on the new node before attaching it
to the tree.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* tbuild.adb (Unchecked_Convert_To): Set the Parent of the new
	node to the Parent of the old node.
	* tbuild.ads (Unchecked_Convert_To): Document differences
	between Convert_To and Unchecked_Convert_To. The previous
	documentation claimed they are identical except for the
	uncheckedness of the conversion.
diff mbox series

Patch

diff --git a/gcc/ada/tbuild.adb b/gcc/ada/tbuild.adb
--- a/gcc/ada/tbuild.adb
+++ b/gcc/ada/tbuild.adb
@@ -919,10 +919,15 @@  package body Tbuild is
       --  All other cases
 
       else
-         Result :=
-           Make_Unchecked_Type_Conversion (Loc,
-             Subtype_Mark => New_Occurrence_Of (Typ, Loc),
-             Expression   => Relocate_Node (Expr));
+         declare
+            Expr_Parent : constant Node_Id := Parent (Expr);
+         begin
+            Result :=
+              Make_Unchecked_Type_Conversion (Loc,
+                Subtype_Mark => New_Occurrence_Of (Typ, Loc),
+                Expression   => Relocate_Node (Expr));
+            Set_Parent (Result, Expr_Parent);
+         end;
       end if;
 
       Set_Etype (Result, Typ);


diff --git a/gcc/ada/tbuild.ads b/gcc/ada/tbuild.ads
--- a/gcc/ada/tbuild.ads
+++ b/gcc/ada/tbuild.ads
@@ -340,7 +340,10 @@  package Tbuild is
      (Typ  : Entity_Id;
       Expr : Node_Id) return Node_Id;
    --  Like Convert_To, but if a conversion is actually needed, constructs an
-   --  N_Unchecked_Type_Conversion node to do the required conversion.
+   --  N_Unchecked_Type_Conversion node to do the required conversion. Unlike
+   --  Convert_To, a new node is not required if Expr is already of the correct
+   --  BASE type, and if a new node is created, the Parent of Expr is copied to
+   --  it.
 
    -------------------------------------
    -- Subprograms for Use by Gnat1drv --