From: Eric Botcazou <ebotca...@adacore.com>

Even though the actual subtype of the anonymous array is not yet set on the
object itself by the time Insert_Conditional_Object_Declaration is called,
it is set on its initialization expression, so it can simply be forwarded
to Insert_Conditional_Object_Declaration from there, which avoids creating
a new one for each new object and triggering a subtype mismatch later.

gcc/ada/ChangeLog:

        * exp_ch4.adb (Insert_Conditional_Object_Declaration): Remove Decl
        formal parameter, add Typ and Const formal parameters.
        (Expand_N_Case_Expression): Fix pasto in comment.  Adjust call to
        Insert_Conditional_Object_Declaration and tidy up surrounding code.
        (Expand_N_If_Expression): Adjust couple of calls to
        Insert_Conditional_Object_Declaration.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_ch4.adb | 43 +++++++++++++++++++++----------------------
 1 file changed, 21 insertions(+), 22 deletions(-)

diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb
index 01be3dff89b..1c2a8763711 100644
--- a/gcc/ada/exp_ch4.adb
+++ b/gcc/ada/exp_ch4.adb
@@ -193,12 +193,12 @@ package body Exp_Ch4 is
 
    procedure Insert_Conditional_Object_Declaration
      (Obj_Id : Entity_Id;
+      Typ    : Entity_Id;
       Expr   : Node_Id;
-      Decl   : Node_Id);
-   --  Expr is the dependent expression of a conditional expression and Decl
-   --  is the declaration of an object whose initialization expression is the
-   --  conditional expression. Insert in the actions of Expr the declaration
-   --  of Obj_Id modeled on Decl and with Expr as initialization expression.
+      Const  : Boolean);
+   --  Expr is the dependent expression of a conditional expression. Insert in
+   --  the actions of Expr the declaration of Obj_Id with type Typ and Expr as
+   --  initialization expression. Const is True when Obj_Id is a constant.
 
    procedure Insert_Dereference_Action (N : Node_Id);
    --  N is an expression whose type is an access. When the type of the
@@ -5313,7 +5313,7 @@ package body Exp_Ch4 is
       --  'Unrestricted_Access.
 
       --  Generate:
-      --    type Ptr_Typ is not null access all [constant] Typ;
+      --    type Target_Typ is not null access all [constant] Typ;
 
       else
          Target_Typ := Make_Temporary (Loc, 'P');
@@ -5411,20 +5411,16 @@ package body Exp_Ch4 is
             elsif Optimize_Object_Decl then
                Obj := Make_Temporary (Loc, 'C', Alt_Expr);
 
-               Insert_Conditional_Object_Declaration (Obj, Alt_Expr, Par);
-
-               Alt_Expr :=
-                 Make_Attribute_Reference (Alt_Loc,
-                   Prefix         => New_Occurrence_Of (Obj, Alt_Loc),
-                   Attribute_Name => Name_Unrestricted_Access);
-
-               LHS := New_Occurrence_Of (Target, Loc);
-               Set_Assignment_OK (LHS);
+               Insert_Conditional_Object_Declaration
+                 (Obj, Typ, Alt_Expr, Const => Constant_Present (Par));
 
                Stmts := New_List (
                  Make_Assignment_Statement (Alt_Loc,
-                   Name       => LHS,
-                   Expression => Alt_Expr));
+                   Name       => New_Occurrence_Of (Target, Loc),
+                   Expression =>
+                     Make_Attribute_Reference (Alt_Loc,
+                       Prefix         => New_Occurrence_Of (Obj, Alt_Loc),
+                       Attribute_Name => Name_Unrestricted_Access)));
 
             --  Take the unrestricted access of the expression value for non-
             --  scalar types. This approach avoids big copies and covers the
@@ -6022,8 +6018,10 @@ package body Exp_Ch4 is
             Target   : constant Entity_Id := Make_Temporary (Loc, 'C', N);
 
          begin
-            Insert_Conditional_Object_Declaration (Then_Obj, Thenx, Par);
-            Insert_Conditional_Object_Declaration (Else_Obj, Elsex, Par);
+            Insert_Conditional_Object_Declaration
+              (Then_Obj, Typ, Thenx, Const => Constant_Present (Par));
+            Insert_Conditional_Object_Declaration
+              (Else_Obj, Typ, Elsex, Const => Constant_Present (Par));
 
             --  Generate:
             --    type Ptr_Typ is not null access all [constant] Typ;
@@ -13294,16 +13292,17 @@ package body Exp_Ch4 is
 
    procedure Insert_Conditional_Object_Declaration
      (Obj_Id : Entity_Id;
+      Typ    : Entity_Id;
       Expr   : Node_Id;
-      Decl   : Node_Id)
+      Const  : Boolean)
    is
       Loc      : constant Source_Ptr := Sloc (Expr);
       Obj_Decl : constant Node_Id :=
         Make_Object_Declaration (Loc,
           Defining_Identifier => Obj_Id,
           Aliased_Present     => True,
-          Constant_Present    => Constant_Present (Decl),
-          Object_Definition   => New_Copy_Tree (Object_Definition (Decl)),
+          Constant_Present    => Const,
+          Object_Definition   => New_Occurrence_Of (Typ, Loc),
           Expression          => Relocate_Node (Expr));
       --  We make the object unconditionally aliased to avoid dangling bound
       --  issues when its nominal subtype is an unconstrained array type.
-- 
2.43.0

Reply via email to