This is the mail archive of the 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] Fix bad handling of streams with mutable records

Tested on x86-linux, committed on mainline.

The components of an object of a record type that has discriminants with
defaults need to be properly initialized before being assigned (for example,
controlled components must be linked into the record_controller, and components
whose subtype is constrained by a discriminant of the record need to store
the appropriate discriminant value). Therefore, component assignments must
be performed on a temporary variable declared with the correct constraints,
and this temporary variable must be assigned back (as a whole) to the out
formal parameter of the Read subprogram.

Test case:
$ gnatmake test1
$ ./test1
with Ada.Streams.Stream_Io; with Ada.Text_Io;
procedure Test1 is

   type Integer_Array is array ( Integer range <> ) of Integer;
   type Float_Array is array ( Integer range <> ) of Float;

   type Test_Record_Type( Len1 : Integer; Len2 : Integer) is record
      Ia : Integer_Array( 1 .. Len1 );
      St : String (1 .. 20);
      Fa : Float_Array( 1 .. Len2 );
   end record;

   subtype Test_Record1_Type is Test_Record_Type(5, 15);
   type Object_Category_Type is (No_Object, Test_Object);

   type Object_Type (Category : Object_Category_Type := No_Object) is record
      case Category is
         when No_Object => null;
         when Test_Object => Test_Record : Test_Record1_Type;
      end case;
   end record;

   type Shell_Type is record Shell : Object_Type; end record;

   Test_Record1 : Test_Record1_Type :=
     ( Len1 => 5, Len2 => 15,
       Ia => (others => 123),
       St => (others => 'c'),
       Fa => ( others => 123.456 ));
   Try_Record : Shell_Type;
   Try_Object : Object_Type(Test_Object);
   Save_File : Ada.Streams.Stream_Io.File_Type;
   Restore_File : Ada.Streams.Stream_Io.File_Type;
   Stream : Ada.Streams.Stream_Io.Stream_Access;

   Try_Object.Test_Record := Test_Record1;
   Try_Record.Shell := Try_Object;
     (Save_File, Ada.Streams.Stream_Io.Out_File, "save.bin");
   Stream := Ada.Streams.Stream_Io.Stream (Save_File);
   Shell_Type'Output (Stream, Try_Record);
   Ada.Streams.Stream_Io.Close (Save_File);
     (Restore_File, Ada.Streams.Stream_Io.In_File, "save.bin");
   Stream := Ada.Streams.Stream_Io.Stream (Restore_File);
   Try_Record := Shell_Type'Input (Stream);
   Ada.Text_IO.Put_Line ("Success");
end Test1;

2005-01-03  Thomas Quinot  <>

	* (Make_Final_Call): Rewrite comment (was incorrectly
	copied from Make_Init_Call).

	* exp_strm.adb (Build_Mutable_Record_Read_Procedure): Do component
	reads and assignments on a temporary variable declared with appropriate

Attachment: difs.11
Description: Text document

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