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] Finalization of temporary controlled function results


Finalization of temporary controlled function results in expression with
actions nodes in the context of return statements:

This patch adds logic to recognize a simple return statement as one of the
cases that require special processing with respect to temporary controlled
function results that appear in expression_with_actions nodes.

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

--  types.ads

with Ada.Finalization; use Ada.Finalization;

package Types is
   type Ctrl is new Controlled with record
      Data : Natural := 1234;
   end record;

   function Equal_To_1 (Obj : Ctrl) return Boolean;
   procedure Finalize (Obj : in out Ctrl);
   function Make_Ctrl (Val : Natural) return Ctrl;
end Types;

--  types.adb

with Ada.Text_IO; use Ada.Text_IO;

package body Types is
   function Equal_To_1 (Obj : Ctrl) return Boolean is
   begin
      return Obj.Data = 1;
   end Equal_To_1;

   procedure Finalize (Obj : in out Ctrl) is
   begin
      Obj.Data := 0;
      Put_Line ("   fin");
   end Finalize;

   function Make_Ctrl (Val : Natural) return Ctrl is
   begin
      return (Controlled with Val);
   end Make_Ctrl;
end Types;

--  main.adb

with Ada.Text_IO; use Ada.Text_IO;
with Types;       use Types;

procedure Main is
   function Must_Be_True return Boolean is
      function Factorial (Val : Natural) return Natural is
      begin
         if Val > 1 then
            return Val * Factorial (Val - 1);
         else
            return 1;
         end if;
      end Factorial;
   begin
      return
        Factorial (3) = 6
          and then Equal_To_1 (Make_Ctrl (1));
   end Must_Be_True;
begin
   if not Must_Be_True then
      Put_Line ("ERROR: temporary function result finalized too early");
   end if;
end Main;

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

$ gnatmake -q main.adb
$ ./main
   fin
   fin

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

2013-02-06  Hristian Kirtchev  <kirtchev@adacore.com>

	* exp_ch4.adb (Find_Enclosing_Context): Recognize
	a simple return statement as one of the cases that require special
	processing with respect to temporary controlled function results.
	(Process_Transient_Object): Do attempt to finalize a temporary
	controlled function result when the associated context is
	a simple return statement.  Instead, leave this task to the
	general finalization mechanism.

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]