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] Expression_With_Actions and finalization


This patch modifies the expansion of Expression_With_Actions in order to handle
the finalization of a function call nested deep within the condition of an if
statement.

------------
-- Source --
------------

--  main.adb

with Ada.Finalization; use Ada.Finalization;
with Ada.Text_IO;      use Ada.Text_IO;

procedure Main is
   type Ctrl is new Controlled with record
     Id : Natural := 0;
   end record;

   procedure Finalize (Obj : in out Ctrl) is
   begin
      Put_Line ("  finalize Id:" & Obj.Id'Img);
   end Finalize;

   function Make_Ctrl (Id : Natural) return Ctrl is
   begin
      return (Controlled with Id => Id);
   end Make_Ctrl;

   function Equal (Left : Ctrl; Right : Ctrl) return Boolean is
   begin
      return Left.Id = Right.Id;
   end Equal;

   procedure Test
     (Obj     : Ctrl;
      Id      : Natural;
      Check_1 : Integer;
      Check_2 : Integer)
   is
   begin
      Put_Line ("before if statement");
      if Check_1 > 1
        and then
          (Check_2 > 1
             or else Equal (Obj, Make_Ctrl (Id)))
      then
         Put_Line ("inside if statement");
      end if;
       Put_Line ("after if statement");
   end Test;

   Id  : constant Natural := 1234;
   Obj : constant Ctrl := (Controlled with Id => Id);
begin
   Test (Obj, Id, 2, 1);
end Main;

-------------------------------------
-- Compilation and expected output --
-------------------------------------

$ gnatmake -q -gnat05 main.adb
$ ./main
before if statement
  finalize Id: 1234
  finalize Id: 1234
inside if statement
after if statement
  finalize Id: 1234

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

2011-09-01  Hristian Kirtchev  <kirtchev@adacore.com>

	* exp_ch4.adb (Find_Insertion_Node): New routine. Determines the proper
	insertion node in a tree of nested Expression_With_Actions nodes.
	(Process_Transient_Object): In the case where a complex if statement
	has been converted into nested Expression_With_Actions nodes, the
	"hook" object and the associated access type must be inserted before
	the top most Expression_With_Actions.

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]