[Ada] Alignment may be specified as zero
diff mbox series

Message ID 20190814095313.GA52437@adacore.com
State New
Headers show
Series
  • [Ada] Alignment may be specified as zero
Related show

Commit Message

Pierre-Marie de Rodat Aug. 14, 2019, 9:53 a.m. UTC
An Alignment clause or an aspect_specification for Alignment may be
specified as 0, which is treated the same as 1.

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

2019-08-14  Bob Duff  <duff@adacore.com>

gcc/ada/

	* sem_ch13.adb (Get_Alignment_Value): Return 1 for Alignment 0,
	and do not give an error.
	* doc/gnat_rm/representation_clauses_and_pragmas.rst: Update the
	corresponding documentation.
	* gnat_rm.texi: Regenerate.

gcc/testsuite/

	* gnat.dg/alignment15.adb: New testcase.

Patch
diff mbox series

--- gcc/ada/doc/gnat_rm/representation_clauses_and_pragmas.rst
+++ gcc/ada/doc/gnat_rm/representation_clauses_and_pragmas.rst
@@ -30,9 +30,11 @@  Alignment Clauses
 
 .. index:: Alignment Clause
 
-GNAT requires that all alignment clauses specify a power of 2, and all
-default alignments are always a power of 2.  The default alignment
-values are as follows:
+GNAT requires that all alignment clauses specify 0 or a power of 2, and
+all default alignments are always a power of 2. Specifying 0 is the
+same as specifying 1.
+
+The default alignment values are as follows:
 
 * *Elementary Types*.
 
@@ -610,23 +612,23 @@  alignment of the type (this is true for all types). In some cases the
      end record;
 
 
-On a typical 32-bit architecture, the X component will occupy four bytes 
-and the Y component will occupy one byte, for a total of 5 bytes. As a 
-result ``R'Value_Size`` will be 40 (bits) since this is the minimum size 
-required to store a value of this type. For example, it is permissible 
-to have a component of type R in an array whose component size is 
-specified to be 40 bits. 
-
-However, ``R'Object_Size`` will be 64 (bits). The difference is due to 
-the alignment requirement for objects of the record type. The X 
-component will require four-byte alignment because that is what type 
-Integer requires, whereas the Y component, a Character, will only 
-require 1-byte alignment. Since the alignment required for X is the 
-greatest of all the components' alignments, that is the alignment 
-required for the enclosing record type, i.e., 4 bytes or 32 bits. As 
-indicated above, the actual object size must be rounded up so that it is 
-a multiple of the alignment value. Therefore, 40 bits rounded up to the 
-next multiple of 32 yields 64 bits. 
+On a typical 32-bit architecture, the X component will occupy four bytes
+and the Y component will occupy one byte, for a total of 5 bytes. As a
+result ``R'Value_Size`` will be 40 (bits) since this is the minimum size
+required to store a value of this type. For example, it is permissible
+to have a component of type R in an array whose component size is
+specified to be 40 bits.
+
+However, ``R'Object_Size`` will be 64 (bits). The difference is due to
+the alignment requirement for objects of the record type. The X
+component will require four-byte alignment because that is what type
+Integer requires, whereas the Y component, a Character, will only
+require 1-byte alignment. Since the alignment required for X is the
+greatest of all the components' alignments, that is the alignment
+required for the enclosing record type, i.e., 4 bytes or 32 bits. As
+indicated above, the actual object size must be rounded up so that it is
+a multiple of the alignment value. Therefore, 40 bits rounded up to the
+next multiple of 32 yields 64 bits.
 
 For all other types, the ``Object_Size``
 and ``Value_Size`` are the same (and equivalent to the RM attribute ``Size``).

--- gcc/ada/gnat_rm.texi
+++ gcc/ada/gnat_rm.texi
@@ -21,7 +21,7 @@ 
 
 @copying
 @quotation
-GNAT Reference Manual , Jul 31, 2019
+GNAT Reference Manual , Aug 01, 2019
 
 AdaCore
 
@@ -18369,9 +18369,11 @@  and this section describes the additional capabilities provided.
 
 @geindex Alignment Clause
 
-GNAT requires that all alignment clauses specify a power of 2, and all
-default alignments are always a power of 2.  The default alignment
-values are as follows:
+GNAT requires that all alignment clauses specify 0 or a power of 2, and
+all default alignments are always a power of 2. Specifying 0 is the
+same as specifying 1.
+
+The default alignment values are as follows:
 
 
 @itemize *

--- gcc/ada/sem_ch13.adb
+++ gcc/ada/sem_ch13.adb
@@ -11509,7 +11509,7 @@  package body Sem_Ch13 is
       if Align = No_Uint then
          return No_Uint;
 
-      elsif Align <= 0 then
+      elsif Align < 0 then
 
          --  This error is suppressed in ASIS mode to allow for different ASIS
          --  back ends or ASIS-based tools to query the illegal clause.
@@ -11520,6 +11520,11 @@  package body Sem_Ch13 is
 
          return No_Uint;
 
+      --  If Alignment is specified to be 0, we treat it the same as 1
+
+      elsif Align = 0 then
+         return Uint_1;
+
       else
          for J in Int range 0 .. 64 loop
             declare

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/alignment15.adb
@@ -0,0 +1,17 @@ 
+--  { dg-compile }
+
+procedure Alignment15 is
+   type T0 is record
+      X : Integer;
+   end record;
+   for T0'Alignment use 0;
+
+   type T00 is record
+      X : Integer;
+   end record with Alignment => 0;
+
+   Dummy0  : T0;
+   Dummy00 : T00;
+begin
+   null;
+end;