public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Size clauses and convention C on enumeration types.
@ 2008-05-21  8:36 Arnaud Charlet
  0 siblings, 0 replies; only message in thread
From: Arnaud Charlet @ 2008-05-21  8:36 UTC (permalink / raw)
  To: gcc-patches; +Cc: Ed Schonberg

[-- Attachment #1: Type: text/plain, Size: 1998 bytes --]

Tested on i686-linux, commited on trunk

By default the compiler assumes that an enumeration type with convention C will
map to a C enum, and therefore should a size of Integer. However, the user may
provide a size clause on the type which must be respected. If the entity being
frozen is a subtype, the size clause may be inherited from the parent, and must
be respected as well. In either case a warning indicates to the user that the
C type must be chosen in accordance to the specified size.

Executing:

   gnatmake -f -q testenum.adb
   testenum

must produce the output:

types.ads:7:03: enumeration type with Convention C must have the size of a C int
Last =  3
SV   = A1

----
with Ada.Text_IO;
package CS_Common_Types is
  type SVID_Type is (M1, M2, A5, M4, M5, M6, A1, A2, A3, A4);
  for SVID_Type use (M1 => 1, M2 => 2, A5 => 3, M4 => 4, M5 => 5,
                               M6 => 6, A1 => 7, A2 => 8, A3 => 9, A4 => 10);

  for SVID_Type'Size use 8;

  pragma Convention (C, SVID_Type);

  package SVID_IO is new Ada.Text_IO.Enumeration_IO (SVID_Type);

  function SVID_String (SVID : SVID_Type) return String;
end CS_Common_Types;
---
package body CS_Common_Types is
  function SVID_String (SVID : SVID_Type) return String
  is
  begin
    return "";
  end SVID_String;
end CS_Common_Types;
---
with CS_Common_Types;  use CS_Common_Types; with Ada.Text_IO;
procedure TestEnum
is
  Last : Natural;
  Str   : String := " A1";
  SV   : SVID_Type;
begin
  SVID_IO.Get (Str, SV, Last);

  Ada.Text_IO.Put_Line ("Last = " & Last'Img);   -- Outputs 3 as expected
  Ada.Text_IO.Put_Line ("SV   = " & SV'Img);     -- Raises exception
end TestEnum;

2008-05-20  Ed Schonberg  <schonberg@adacore.com>

	* freeze.adb
	(Freeze_Enumeration_Type): For a subtype that inherits a foreign
	convention from its base type, do not set the type to that of integer,
	because it may inherit a size clause.
	Warn on a size clause with a size different
	from that of Integer, if the type has convention C.


[-- Attachment #2: difs --]
[-- Type: text/plain, Size: 1747 bytes --]

Index: freeze.adb
===================================================================
--- freeze.adb	(revision 134945)
+++ freeze.adb	(working copy)
@@ -3828,12 +3828,36 @@ package body Freeze is
 
    procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
    begin
+      --  By default, if no size clause is present, an enumeration type with
+      --  Convention C is assumed to interface to a C enum, and has integer
+      --  size. This applies to types. For subtypes, verify that its base
+      --  type has no size clause either.
+
       if Has_Foreign_Convention (Typ)
         and then not Has_Size_Clause (Typ)
+        and then not Has_Size_Clause (Base_Type (Typ))
         and then Esize (Typ) < Standard_Integer_Size
       then
          Init_Esize (Typ, Standard_Integer_Size);
+
       else
+         --  If the enumeration type interfaces to C, and it has a size clause
+         --  that specifies less than int size, it warrants a warning. The
+         --  user may intend the C type to be an enum or a char, so this is
+         --  not by itself an error that the Ada compiler can detect, but it
+         --  it is a worth a heads-up. For Boolean and Character types we
+         --  assume that the programmer has the proper C type in mind.
+
+         if Convention (Typ) = Convention_C
+           and then Has_Size_Clause (Typ)
+           and then Esize (Typ) /= Esize (Standard_Integer)
+           and then not Is_Boolean_Type (Typ)
+           and then not Is_Character_Type (Typ)
+         then
+            Error_Msg_N
+              ("C enum types have the size of a C int?", Size_Clause (Typ));
+         end if;
+
          Adjust_Esize_For_Alignment (Typ);
       end if;
    end Freeze_Enumeration_Type;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-05-21  8:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-21  8:36 [Ada] Size clauses and convention C on enumeration types Arnaud Charlet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).