OSDN Git Service

2007-04-06 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-finimp.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --    S Y S T E M . F I N A L I Z A T I O N _ I M P L E M E N T A T I O N   --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2006, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with Ada.Unchecked_Conversion;
35
36 with System.Storage_Elements;
37 with System.Finalization_Root;
38
39 package System.Finalization_Implementation is
40    pragma Elaborate_Body;
41
42    package SSE renames System.Storage_Elements;
43    package SFR renames System.Finalization_Root;
44
45    ------------------------------------------------
46    -- Finalization Management Abstract Interface --
47    ------------------------------------------------
48
49    function To_Finalizable_Ptr is new Ada.Unchecked_Conversion
50      (Source => System.Address, Target => SFR.Finalizable_Ptr);
51
52    Collection_Finalization_Started : constant SFR.Finalizable_Ptr :=
53                                        To_Finalizable_Ptr (SSE.To_Address (1));
54    --  This is used to implement the rule in RM 4.8(10.2/2) that requires an
55    --  allocator to raise Program_Error if the collection finalization has
56    --  already started. See also Ada.Finalization.List_Controller. Finalize on
57    --  List_Controller first sets the list to Collection_Finalization_Started,
58    --  to indicate that finalization has started. An allocator will call
59    --  Attach_To_Final_List, which checks for the special value and raises
60    --  Program_Error if appropriate. The Collection_Finalization_Started value
61    --  must be different from 'Access of any finalizable object, and different
62    --  from null. See AI-280.
63
64    Global_Final_List : SFR.Finalizable_Ptr;
65    --  This list stores the controlled objects defined in library-level
66    --  packages. They will be finalized after the main program completion.
67
68    procedure Finalize_Global_List;
69    --  The procedure to be called in order to finalize the global list;
70
71    procedure Attach_To_Final_List
72      (L       : in out SFR.Finalizable_Ptr;
73       Obj     : in out SFR.Finalizable;
74       Nb_Link : Short_Short_Integer);
75    --  Attach finalizable object Obj to the linked list L. Nb_Link controls the
76    --  number of link of the linked_list, and is one of: 0 for no attachment, 1
77    --  for simple linked lists or 2 for doubly linked lists or even 3 for a
78    --  simple attachment of a whole array of elements. Attachment to a simply
79    --  linked list is not protected against concurrent access and should only
80    --  be used in contexts where it doesn't matter, such as for objects
81    --  allocated on the stack. In the case of an attachment on a doubly linked
82    --  list, L must not be null and Obj will be inserted AFTER the first
83    --  element and the attachment is protected against concurrent call.
84    --  Typically used to attach to a dynamically allocated object to a
85    --  List_Controller (whose first element is always a dummy element)
86
87    type Finalizable_Ptr_Ptr is access all SFR.Finalizable_Ptr;
88    --  A pointer to a finalization list. This is used as the type of the extra
89    --  implicit formal which are passed to build-in-place functions that return
90    --  controlled types (see Sem_Ch6). That extra formal is then passed on to
91    --  Move_Final_List (below).
92
93    procedure Move_Final_List
94      (From : in out SFR.Finalizable_Ptr;
95       To   : Finalizable_Ptr_Ptr);
96    --  Move all objects on From list to To list. This is used to implement
97    --  build-in-place function returns. The return object is initially placed
98    --  on a finalization list local to the return statement, in case the
99    --  return statement is left prematurely (due to raising an exception,
100    --  being aborted, or a goto or exit statement). Once the return statement
101    --  has completed successfully, Move_Final_List is called to move the
102    --  return object to the caller's finalization list.
103
104    procedure Finalize_List (L : SFR.Finalizable_Ptr);
105    --  Call Finalize on each element of the list L;
106
107    procedure Finalize_One (Obj  : in out SFR.Finalizable);
108    --  Call Finalize on Obj and remove its final list
109
110    ---------------------
111    -- Deep Procedures --
112    ---------------------
113
114    procedure Deep_Tag_Attach
115      (L : in out SFR.Finalizable_Ptr;
116       A : System.Address;
117       B : Short_Short_Integer);
118    --  Generic attachment for tagged objects with controlled components.
119    --  A is the address of the object, L the finalization list when it needs
120    --  to be attached and B the attachment level (see Attach_To_Final_List).
121
122    -----------------------------
123    -- Record Controller Types --
124    -----------------------------
125
126    --  Definition of the types of the controller component that is included
127    --  in records containing controlled components. This controller is
128    --  attached to the finalization chain of the upper-level and carries
129    --  the pointer of the finalization chain for the lower level.
130
131    type Limited_Record_Controller is new SFR.Root_Controlled with record
132       F : SFR.Finalizable_Ptr;
133    end record;
134
135    procedure Initialize (Object : in out Limited_Record_Controller);
136    --  Does nothing currently.
137
138    procedure Finalize (Object : in out Limited_Record_Controller);
139    --  Finalize the controlled components of the enclosing record by following
140    --  the list starting at Object.F.
141
142    type Record_Controller is
143       new Limited_Record_Controller with record
144          My_Address : System.Address;
145       end record;
146
147    procedure Initialize (Object : in out Record_Controller);
148    --  Initialize the field My_Address to the Object'Address
149
150    procedure Adjust (Object : in out Record_Controller);
151    --  Adjust the components and their finalization pointers by subtracting by
152    --  the offset of the target and the source addresses of the assignment.
153
154    --  Inherit Finalize from Limited_Record_Controller
155
156    procedure Detach_From_Final_List (Obj : in out SFR.Finalizable);
157    --  Remove the specified object from its Final list, which must be a doubly
158    --  linked list.
159
160 end System.Finalization_Implementation;