This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Implementation of AI05-0161 restriction No_Default_Stream_Attributes


This new restriction is intended to prevent the use of the predefined stream
attributes for elementary types. A consequence of this restriction is that
the default implementation of stream attributes for composite types cannot
be created if any of its elementary components lacks user-defined Read and
Write attributes. 

Given the following configuration file:

    pragma Restrictions (No_Default_Stream_Attributes);

Then the following must compile quietly:
---
   with Stdarg;
   procedure Main is
   begin
      null;
   end Main;
---
and the following must execute quietly:

   with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
   procedure Stream_Test is
      --  Check that when restriction No_Default_Stream_Attributes is active,
      --  stream operations on composite types are usable if the type of their
      --  elementary components have user-defined stream operations.

      type Count is new Integer;
      procedure Dump_It
        (S : not null access Ada.Streams.Root_Stream_Type'Class; It : Count);
      for Count'Write use Dump_It;

      procedure Grab_It
       (S : not null access Ada.Streams.Root_Stream_Type'Class; It : out Count);
      for Count'Read use Grab_It;
   
      type Rec is record
         Value : Count;
      end record;

      procedure Dump_It
        (S : not null access Ada.Streams.Root_Stream_Type'Class; It : Count) is
      begin
         String'Output (S, Count'Image (It));
      end;

      procedure Grab_It
        (S : not null access Ada.Streams.Root_Stream_Type'Class; It : out Count)
      is
      begin
         It := Count'Value (String'Input (S));
      end Grab_It;

      Rec_File : File_Type;
      S : Stream_Access;
      Obj : Rec := (Value => -1234);
      Recovered : Rec;
   begin
      Create (Rec_File, Name => "temp");
      S := Stream (Rec_File);
      Rec'Output (S, Obj);
      Close (Rec_File);

      Open (Rec_File, Name => "temp",  Mode => In_File);
      Recovered := Rec'Input (S);

      if Obj /= Recovered then
         raise Program_Error;
      end if;
   end;
---
After commenting out any of the attribute definitions above, compilation of
stream_test.adb must yield: 

    stream_test.adb:39:07:
       violation of restriction "No_Default_Stream_Attributes" at gnat.adc:1

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

2011-08-04  Ed Schonberg  <schonberg@adacore.com>

	* exp_ch3.adb (Stream_Operation_Ok): new predicate
	Needs_Elementary_Stream_Operation, to determine whether user-defined
	Read and Write attributes are available for the elementary components
	of the given type. If only the predefined attributes are available,
	then when restriction No_Default_Stream_Attributes is active the
	predefined stream attributes for the composite type cannot be created.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]