[Ada] Small adjustment in the handling of alignment for arrays

Message ID 20200707092736.GA41723@adacore.com
State New
Headers show
Series
  • [Ada] Small adjustment in the handling of alignment for arrays
Related show

Commit Message

Pierre-Marie de Rodat July 7, 2020, 9:27 a.m.
This moves the setting of the alignment of an array with known alignment
and size for the component type from Freeze_Array_Type to Layout_Type.

The rationale is that Layout_Type is invoked after Freeze_Array_Type
during freezing and computes the component size from the size of the
component type, which is a condition under which the setting is done.

The end result is that the alignment setting is done consistently on
base array types and subtypes, which share the same component size.

No functional changes.

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

gcc/ada/

	* freeze.adb (Freeze_Array_Type): Use Ctyp more consistently.
	Move code setting the alignment in the non-packed case to...
	* layout.adb (Layout_Type): ...here.

Patch

diff --git a/gcc/ada/freeze.adb b/gcc/ada/freeze.adb
--- a/gcc/ada/freeze.adb
+++ b/gcc/ada/freeze.adb
@@ -2573,13 +2573,13 @@  package body Freeze is
 
             --  Propagate flags for component type
 
-            if Is_Controlled (Component_Type (Arr))
+            if Is_Controlled (Ctyp)
               or else Has_Controlled_Component (Ctyp)
             then
                Set_Has_Controlled_Component (Arr);
             end if;
 
-            if Has_Unchecked_Union (Component_Type (Arr)) then
+            if Has_Unchecked_Union (Ctyp) then
                Set_Has_Unchecked_Union (Arr);
             end if;
 
@@ -2590,7 +2590,7 @@  package body Freeze is
             --  that the procedure can be used to check the array type
             --  invariants if any.
 
-            if Has_Invariants (Component_Type (Arr))
+            if Has_Invariants (Ctyp)
               and then not GNATprove_Mode
             then
                Set_Has_Own_Invariants (Arr);
@@ -2902,8 +2902,8 @@  package body Freeze is
                         --  If the Esize of the component is known and equal to
                         --  the component size then even packing is not needed.
 
-                        if Known_Static_Esize (Component_Type (Arr))
-                          and then Esize (Component_Type (Arr)) = Csiz
+                        if Known_Static_Esize (Ctyp)
+                          and then Esize (Ctyp) = Csiz
                         then
                            --  Here the array was requested to be packed, but
                            --  the packing request had no effect whatsoever,
@@ -3157,21 +3157,6 @@  package body Freeze is
 
          <<Skip_Packed>>
 
-         --  For non-packed arrays set the alignment of the array to the
-         --  alignment of the component type if it is unknown. Skip this
-         --  in atomic/VFA case (atomic/VFA arrays may need larger alignments).
-
-         if not Is_Packed (Arr)
-           and then Unknown_Alignment (Arr)
-           and then Known_Alignment (Ctyp)
-           and then Known_Static_Component_Size (Arr)
-           and then Known_Static_Esize (Ctyp)
-           and then Esize (Ctyp) = Component_Size (Arr)
-           and then not Is_Atomic_Or_VFA (Arr)
-         then
-            Set_Alignment (Arr, Alignment (Component_Type (Arr)));
-         end if;
-
          --  A Ghost type cannot have a component of protected or task type
          --  (SPARK RM 6.9(19)).
 


diff --git a/gcc/ada/layout.adb b/gcc/ada/layout.adb
--- a/gcc/ada/layout.adb
+++ b/gcc/ada/layout.adb
@@ -467,6 +467,22 @@  package body Layout is
                end if;
             end;
          end if;
+
+         --  For non-packed arrays set the alignment of the array to the
+         --  alignment of the component type if it is unknown. Skip this
+         --  in atomic/VFA case since a larger alignment may be needed.
+
+         if Is_Array_Type (E)
+           and then not Is_Packed (E)
+           and then Unknown_Alignment (E)
+           and then Known_Alignment (Component_Type (E))
+           and then Known_Static_Component_Size (E)
+           and then Known_Static_Esize (Component_Type (E))
+           and then Component_Size (E) = Esize (Component_Type (E))
+           and then not Is_Atomic_Or_VFA (E)
+         then
+            Set_Alignment (E, Alignment (Component_Type (E)));
+         end if;
       end if;
 
       --  Even if the backend performs the layout, we still do a little in