Patchwork [Ada] Read references in ali files for out-mode parameters

login
register
mail settings
Submitter Arnaud Charlet
Date Feb. 6, 2013, 10:20 a.m.
Message ID <20130206102011.GA23837@adacore.com>
Download mbox | patch
Permalink /patch/218531/
State New
Headers show

Comments

Arnaud Charlet - Feb. 6, 2013, 10:20 a.m.
The frontend was unconditionally generating a read reference for OUT
parameters. After this patch the compiler generates the read reference
only for those cases specified by RM 6.4.1(12).

package Pkg is
  procedure Write (I : out Integer);
  procedure Update (I : in out Integer);
end Pkg;

package body Pkg is
   procedure Write (I : out Integer) is
      pragma Unreferenced (I);
   begin
      null;
   end Write;

   procedure Update (I : in out Integer) is
      pragma Unreferenced (I);
   begin
      null;
   end Update;
end Pkg;

with Pkg;
procedure TEST is
 J : Integer := 0;
begin
  Pkg.Write(J);  -- out
  Pkg.Update(J); -- in out
end;

Command:
  gcc -c test.adb
  grep -i ^3i2 test.ali
Output:
  3i2 J{integer} 5m13 6m14 6r14

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

2013-02-06  Javier Miranda  <miranda@adacore.com>

	* sem_res.adb (Resolve_Actuals): Generate a read
	reference for out-mode parameters in the cases specified by
	RM 6.4.1(12).

Patch

Index: sem_res.adb
===================================================================
--- sem_res.adb	(revision 195784)
+++ sem_res.adb	(working copy)
@@ -3409,7 +3409,46 @@ 
                   Generate_Reference (Orig_A, A, 'm');
 
                elsif not Is_Overloaded (A) then
-                  Generate_Reference (Orig_A, A);
+                  if Ekind (F) /= E_Out_Parameter then
+                     Generate_Reference (Orig_A, A);
+
+                  --  RM 6.4.1(12): For an out parameter that is passed by
+                  --  copy, the formal parameter object is created, and:
+
+                  --  * For an access type, the formal parameter is initialized
+                  --    from the value of the actual, without checking that the
+                  --    value satisfies any constraint, any predicate, or any
+                  --    exclusion of the null value.
+
+                  --  * For a scalar type that has the Default_Value aspect
+                  --    specified, the formal parameter is initialized from the
+                  --    value of the actual, without checking that the value
+                  --    satisfies any constraint or any predicate;
+
+                  --  * For a composite type with discriminants or that has
+                  --    implicit initial values for any subcomponents, the
+                  --    behavior is as for an in out parameter passed by copy.
+
+                  --  Hence for these cases we generate the read reference now
+                  --  (the write reference will be generated later by
+                  --   Note_Possible_Modification).
+
+                  elsif Is_By_Copy_Type (Etype (F))
+                    and then
+                      (Is_Access_Type (Etype (F))
+                         or else
+                           (Is_Scalar_Type (Etype (F))
+                              and then
+                                Present (Default_Aspect_Value (Etype (F))))
+                         or else
+                           (Is_Composite_Type (Etype (F))
+                              and then
+                                (Has_Discriminants (Etype (F))
+                                   or else
+                                 Is_Partially_Initialized_Type (Etype (F)))))
+                  then
+                     Generate_Reference (Orig_A, A);
+                  end if;
                end if;
             end if;
          end if;