OSDN Git Service

Nathanael Nerode <neroden@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / ada / table.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                T A B L E                                 --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1992-2001 Free Software Foundation, Inc.          --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNAT was originally developed  by the GNAT team at  New York University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  This package provides an implementation of dynamically resizable one
36 --  dimensional arrays. The idea is to mimic the normal Ada semantics for
37 --  arrays as closely as possible with the one additional capability of
38 --  dynamically modifying the value of the Last attribute.
39
40 --  Note that this interface should remain synchronized with those in
41 --  GNAT.Table and GNAT.Dynamic_Tables to keep coherency between these
42 --  three related units.
43
44 with Types; use Types;
45
46 package Table is
47 pragma Elaborate_Body (Table);
48
49    generic
50       type Table_Component_Type is private;
51       type Table_Index_Type     is range <>;
52
53       Table_Low_Bound  : Table_Index_Type;
54       Table_Initial    : Pos;
55       Table_Increment  : Nat;
56       Table_Name       : String;
57
58    package Table is
59
60       --  Table_Component_Type and Table_Index_Type specify the type of the
61       --  array, Table_Low_Bound is the lower bound. Index_type must be an
62       --  integer type. The effect is roughly to declare:
63
64       --    Table : array (Table_Index_Type range Table_Low_Bound .. <>)
65       --                       of Table_Component_Type;
66
67       --    Note: since the upper bound can be one less than the lower
68       --    bound for an empty array, the table index type must be able
69       --    to cover this range, e.g. if the lower bound is 1, then the
70       --    Table_Index_Type should be Natural rather than Positive.
71
72       --  Table_Component_Type may be any Ada type, except that controlled
73       --  types are not supported. Note however that default initialization
74       --  will NOT occur for array components.
75
76       --  The Table_Initial values controls the allocation of the table when
77       --  it is first allocated, either by default, or by an explicit Init
78       --  call. The value used is Opt.Table_Factor * Table_Initial.
79
80       --  The Table_Increment value controls the amount of increase, if the
81       --  table has to be increased in size. The value given is a percentage
82       --  value (e.g. 100 = increase table size by 100%, i.e. double it).
83
84       --  The Table_Name parameter is simply use in debug output messages it
85       --  has no other usage, and is not referenced in non-debugging mode.
86
87       --  The Last and Set_Last subprograms provide control over the current
88       --  logical allocation. They are quite efficient, so they can be used
89       --  freely (expensive reallocation occurs only at major granularity
90       --  chunks controlled by the allocation parameters).
91
92       --  Note: we do not make the table components aliased, since this would
93       --  restict the use of table for discriminated types. If it is necessary
94       --  to take the access of a table element, use Unrestricted_Access.
95
96       type Table_Type is
97         array (Table_Index_Type range <>) of Table_Component_Type;
98
99       subtype Big_Table_Type is
100         Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
101       --  We work with pointers to a bogus array type that is constrained
102       --  with the maximum possible range bound. This means that the pointer
103       --  is a thin pointer, which is more efficient. Since subscript checks
104       --  in any case must be on the logical, rather than physical bounds,
105       --  safety is not compromised by this approach.
106
107       type Table_Ptr is access all Big_Table_Type;
108       --  The table is actually represented as a pointer to allow reallocation
109
110       Table : aliased Table_Ptr := null;
111       --  The table itself. The lower bound is the value of Low_Bound.
112       --  Logically the upper bound is the current value of Last (although
113       --  the actual size of the allocated table may be larger than this).
114       --  The program may only access and modify Table entries in the range
115       --  First .. Last.
116
117       Locked : Boolean := False;
118       --  Table expansion is permitted only if this switch is set to False. A
119       --  client may set Locked to True, in which case any attempt to expand
120       --  the table will cause an assertion failure. Note that while a table
121       --  is locked, its address in memory remains fixed and unchanging. This
122       --  feature is used to control table expansion during Gigi processing.
123       --  Gigi assumes that tables other than the Uint and Ureal tables do
124       --  not move during processing, which means that they cannot be expanded.
125       --  The Locked flag is used to enforce this restriction.
126
127       procedure Init;
128       --  This procedure allocates a new table of size Initial (freeing any
129       --  previously allocated larger table). It is not necessary to call
130       --  Init when a table is first instantiated (since the instantiation does
131       --  the same initialization steps). However, it is harmless to do so, and
132       --  Init is convenient in reestablishing a table for new use.
133
134       function Last return Table_Index_Type;
135       pragma Inline (Last);
136       --  Returns the current value of the last used entry in the table, which
137       --  can then be used as a subscript for Table. Note that the only way to
138       --  modify Last is to call the Set_Last procedure. Last must always be
139       --  used to determine the logically last entry.
140
141       procedure Release;
142       --  Storage is allocated in chunks according to the values given in the
143       --  Initial and Increment parameters. A call to Release releases all
144       --  storage that is allocated, but is not logically part of the current
145       --  array value. Current array values are not affected by this call.
146
147       procedure Free;
148       --  Free all allocated memory for the table. A call to init is required
149       --  before any use of this table after calling Free.
150
151       First : constant Table_Index_Type := Table_Low_Bound;
152       --  Export First as synonym for Low_Bound (parallel with use of Last)
153
154       procedure Set_Last (New_Val : Table_Index_Type);
155       pragma Inline (Set_Last);
156       --  This procedure sets Last to the indicated value. If necessary the
157       --  table is reallocated to accommodate the new value (i.e. on return
158       --  the allocated table has an upper bound of at least Last). If Set_Last
159       --  reduces the size of the table, then logically entries are removed
160       --  from the table. If Set_Last increases the size of the table, then
161       --  new entries are logically added to the table.
162
163       procedure Increment_Last;
164       pragma Inline (Increment_Last);
165       --  Adds 1 to Last (same as Set_Last (Last + 1).
166
167       procedure Decrement_Last;
168       pragma Inline (Decrement_Last);
169       --  Subtracts 1 from Last (same as Set_Last (Last - 1).
170
171       procedure Append (New_Val : Table_Component_Type);
172       pragma Inline (Append);
173       --  Equivalent to:
174       --    x.Increment_Last;
175       --    x.Table (x.Last) := New_Val;
176       --  i.e. the table size is increased by one, and the given new item
177       --  stored in the newly created table element.
178
179       procedure Set_Item
180         (Index : Table_Index_Type;
181          Item  : Table_Component_Type);
182       pragma Inline (Set_Item);
183       --  Put Item in the table at position Index. The table is expanded if
184       --  current table length is less than Index and in that case Last is set
185       --  to Index. Item will replace any value already present in the table
186       --  at this position.
187
188       type Saved_Table is private;
189       --  Type used for Save/Restore subprograms
190
191       function Save return Saved_Table;
192       --  Resets table to empty, but saves old contents of table in returned
193       --  value, for possible later restoration by a call to Restore.
194
195       procedure Restore (T : Saved_Table);
196       --  Given a Saved_Table value returned by a prior call to Save, restores
197       --  the table to the state it was in at the time of the Save call.
198
199       procedure Tree_Write;
200       --  Writes out contents of table using Tree_IO
201
202       procedure Tree_Read;
203       --  Initializes table by reading contents previously written
204       --  with the Tree_Write call (also using Tree_IO)
205
206    private
207
208       Last_Val : Int;
209       --  Current value of Last. Note that we declare this in the private part
210       --  because we don't want the client to modify Last except through one of
211       --  the official interfaces (since a modification to Last may require a
212       --  reallocation of the table).
213
214       Max : Int;
215       --  Subscript of the maximum entry in the currently allocated table
216
217       type Saved_Table is record
218          Last_Val : Int;
219          Max      : Int;
220          Table    : Table_Ptr;
221       end record;
222
223    end Table;
224 end Table;