[Ada] Simplify detection of record components with default initialization

Message ID 20211011133914.GA1518656@adacore.com
State New
Headers show
Series
  • [Ada] Simplify detection of record components with default initialization
Related show

Commit Message

Eric Gallager via Gcc-patches Oct. 11, 2021, 1:39 p.m.
When detecting record components with default initialization we did two
iteration over the component list; now we do only one.

Also, there was no need to explicitly guard against an empty list where
the subsequent iteration with First/Next/Present works fine.

Cleanup related to expansion of aggregates in GNATprove mode; behaviour
is unaffected.

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

gcc/ada/

	* exp_aggr.adb (Has_Default_Init_Comps): Simplify.

Patch

diff --git a/gcc/ada/exp_aggr.adb b/gcc/ada/exp_aggr.adb
--- a/gcc/ada/exp_aggr.adb
+++ b/gcc/ada/exp_aggr.adb
@@ -8897,46 +8897,41 @@  package body Exp_Aggr is
    ----------------------------
 
    function Has_Default_Init_Comps (N : Node_Id) return Boolean is
-      Comps : constant List_Id := Component_Associations (N);
-      C     : Node_Id;
+      Assoc : Node_Id;
       Expr  : Node_Id;
+      --  Component association and expression, respectively
 
    begin
       pragma Assert (Nkind (N) in N_Aggregate | N_Extension_Aggregate);
 
-      if No (Comps) then
-         return False;
-      end if;
-
       if Has_Self_Reference (N) then
          return True;
       end if;
 
-      --  Check if any direct component has default initialized components
+      Assoc := First (Component_Associations (N));
+      while Present (Assoc) loop
+         --  Each component association has either a box or an expression
 
-      C := First (Comps);
-      while Present (C) loop
-         if Box_Present (C) then
-            return True;
-         end if;
+         pragma Assert (Box_Present (Assoc) xor Present (Expression (Assoc)));
 
-         Next (C);
-      end loop;
+         --  Check if any direct component has default initialized components
 
-      --  Recursive call in case of aggregate expression
+         if Box_Present (Assoc) then
+            return True;
 
-      C := First (Comps);
-      while Present (C) loop
-         Expr := Expression (C);
+         --  Recursive call in case of aggregate expression
 
-         if Present (Expr)
-           and then Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
-           and then Has_Default_Init_Comps (Expr)
-         then
-            return True;
+         else
+            Expr := Expression (Assoc);
+
+            if Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
+              and then Has_Default_Init_Comps (Expr)
+            then
+               return True;
+            end if;
          end if;
 
-         Next (C);
+         Next (Assoc);
       end loop;
 
       return False;