annotate gcc/ada/libgnat/g-dyntab.ads @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . D Y N A M I C _ T A B L E S --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 2000-2017, AdaCore --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- Resizable one dimensional array support
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 -- This package provides an implementation of dynamically resizable one
kono
parents:
diff changeset
35 -- dimensional arrays. The idea is to mimic the normal Ada semantics for
kono
parents:
diff changeset
36 -- arrays as closely as possible with the one additional capability of
kono
parents:
diff changeset
37 -- dynamically modifying the value of the Last attribute.
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 -- This package provides a facility similar to that of Ada.Containers.Vectors.
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 -- Note that these three interfaces should remain synchronized to keep as much
kono
parents:
diff changeset
42 -- coherency as possible among these related units:
kono
parents:
diff changeset
43 --
kono
parents:
diff changeset
44 -- GNAT.Dynamic_Tables
kono
parents:
diff changeset
45 -- GNAT.Table
kono
parents:
diff changeset
46 -- Table (the compiler unit)
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 pragma Compiler_Unit_Warning;
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 with Ada.Unchecked_Conversion;
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 generic
kono
parents:
diff changeset
53 type Table_Component_Type is private;
kono
parents:
diff changeset
54 type Table_Index_Type is range <>;
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 Table_Low_Bound : Table_Index_Type := Table_Index_Type'First;
kono
parents:
diff changeset
57 Table_Initial : Positive := 8;
kono
parents:
diff changeset
58 Table_Increment : Natural := 100;
kono
parents:
diff changeset
59 Release_Threshold : Natural := 0; -- size in bytes
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 package GNAT.Dynamic_Tables is
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 -- Table_Component_Type and Table_Index_Type specify the type of the array,
kono
parents:
diff changeset
64 -- Table_Low_Bound is the lower bound. The effect is roughly to declare:
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 -- Table : array (Table_Low_Bound .. <>) of Table_Component_Type;
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 -- The lower bound of Table_Index_Type is ignored.
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 -- Table_Component_Type must not be a type with controlled parts.
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 -- The Table_Initial value controls the allocation of the table when it is
kono
parents:
diff changeset
73 -- first allocated.
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 -- The Table_Increment value controls the amount of increase, if the table
kono
parents:
diff changeset
76 -- has to be increased in size. The value given is a percentage value (e.g.
kono
parents:
diff changeset
77 -- 100 = increase table size by 100%, i.e. double it).
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 -- The Last and Set_Last subprograms provide control over the current
kono
parents:
diff changeset
80 -- logical allocation. They are quite efficient, so they can be used
kono
parents:
diff changeset
81 -- freely (expensive reallocation occurs only at major granularity
kono
parents:
diff changeset
82 -- chunks controlled by the allocation parameters).
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 -- Note: we do not make the table components aliased, since this would
kono
parents:
diff changeset
85 -- restrict the use of table for discriminated types. If it is necessary
kono
parents:
diff changeset
86 -- to take the access of a table element, use Unrestricted_Access.
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 -- WARNING: On HPPA, the virtual addressing approach used in this unit is
kono
parents:
diff changeset
89 -- incompatible with the indexing instructions on the HPPA. So when using
kono
parents:
diff changeset
90 -- this unit, compile your application with -mdisable-indexing.
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 -- WARNING: If the table is reallocated, then the address of all its
kono
parents:
diff changeset
93 -- components will change. So do not capture the address of an element
kono
parents:
diff changeset
94 -- and then use the address later after the table may be reallocated. One
kono
parents:
diff changeset
95 -- tricky case of this is passing an element of the table to a subprogram
kono
parents:
diff changeset
96 -- by reference where the table gets reallocated during the execution of
kono
parents:
diff changeset
97 -- the subprogram. The best rule to follow is never to pass a table element
kono
parents:
diff changeset
98 -- as a parameter except for the case of IN mode parameters with scalar
kono
parents:
diff changeset
99 -- values.
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 pragma Assert (Table_Low_Bound /= Table_Index_Type'Base'First);
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 subtype Valid_Table_Index_Type is Table_Index_Type'Base
kono
parents:
diff changeset
104 range Table_Low_Bound .. Table_Index_Type'Base'Last;
kono
parents:
diff changeset
105 subtype Table_Last_Type is Table_Index_Type'Base
kono
parents:
diff changeset
106 range Table_Low_Bound - 1 .. Table_Index_Type'Base'Last;
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 -- Table_Component_Type must not be a type with controlled parts.
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 -- The Table_Initial value controls the allocation of the table when it is
kono
parents:
diff changeset
111 -- first allocated.
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 -- The Table_Increment value controls the amount of increase, if the table
kono
parents:
diff changeset
114 -- has to be increased in size. The value given is a percentage value (e.g.
kono
parents:
diff changeset
115 -- 100 = increase table size by 100%, i.e. double it).
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 -- The Last and Set_Last subprograms provide control over the current
kono
parents:
diff changeset
118 -- logical allocation. They are quite efficient, so they can be used
kono
parents:
diff changeset
119 -- freely (expensive reallocation occurs only at major granularity
kono
parents:
diff changeset
120 -- chunks controlled by the allocation parameters).
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 -- Note: we do not make the table components aliased, since this would
kono
parents:
diff changeset
123 -- restrict the use of table for discriminated types. If it is necessary
kono
parents:
diff changeset
124 -- to take the access of a table element, use Unrestricted_Access.
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 type Table_Type is
kono
parents:
diff changeset
127 array (Valid_Table_Index_Type range <>) of Table_Component_Type;
kono
parents:
diff changeset
128 subtype Big_Table_Type is
kono
parents:
diff changeset
129 Table_Type (Table_Low_Bound .. Valid_Table_Index_Type'Last);
kono
parents:
diff changeset
130 -- We work with pointers to a bogus array type that is constrained with
kono
parents:
diff changeset
131 -- the maximum possible range bound. This means that the pointer is a thin
kono
parents:
diff changeset
132 -- pointer, which is more efficient. Since subscript checks in any case
kono
parents:
diff changeset
133 -- must be on the logical, rather than physical bounds, safety is not
kono
parents:
diff changeset
134 -- compromised by this approach.
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 -- To get subscript checking, rename a slice of the Table, like this:
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 -- Table : Table_Type renames T.Table (First .. Last (T));
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 -- and then refer to components of Table.
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 type Table_Ptr is access all Big_Table_Type;
kono
parents:
diff changeset
143 for Table_Ptr'Storage_Size use 0;
kono
parents:
diff changeset
144 -- The table is actually represented as a pointer to allow reallocation
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 type Table_Private is private;
kono
parents:
diff changeset
147 -- Table private data that is not exported in Instance
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 -- Private use only:
kono
parents:
diff changeset
150 subtype Empty_Table_Array_Type is
kono
parents:
diff changeset
151 Table_Type (Table_Low_Bound .. Table_Low_Bound - 1);
kono
parents:
diff changeset
152 type Empty_Table_Array_Ptr is access all Empty_Table_Array_Type;
kono
parents:
diff changeset
153 Empty_Table_Array : aliased Empty_Table_Array_Type;
kono
parents:
diff changeset
154 function Empty_Table_Array_Ptr_To_Table_Ptr is
kono
parents:
diff changeset
155 new Ada.Unchecked_Conversion (Empty_Table_Array_Ptr, Table_Ptr);
kono
parents:
diff changeset
156 Empty_Table_Ptr : constant Table_Ptr :=
kono
parents:
diff changeset
157 Empty_Table_Array_Ptr_To_Table_Ptr (Empty_Table_Array'Access);
kono
parents:
diff changeset
158 -- End private use only. The above are used to initialize Table to point to
kono
parents:
diff changeset
159 -- an empty array.
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 type Instance is record
kono
parents:
diff changeset
162 Table : Table_Ptr := Empty_Table_Ptr;
kono
parents:
diff changeset
163 -- The table itself. The lower bound is the value of First. Logically
kono
parents:
diff changeset
164 -- the upper bound is the current value of Last (although the actual
kono
parents:
diff changeset
165 -- size of the allocated table may be larger than this). The program may
kono
parents:
diff changeset
166 -- only access and modify Table entries in the range First .. Last.
kono
parents:
diff changeset
167 --
kono
parents:
diff changeset
168 -- It's a good idea to access this via a renaming of a slice, in order
kono
parents:
diff changeset
169 -- to ensure bounds checking, as in:
kono
parents:
diff changeset
170 --
kono
parents:
diff changeset
171 -- Tab : Table_Type renames X.Table (First .. X.Last);
kono
parents:
diff changeset
172 --
kono
parents:
diff changeset
173 -- Note: The Table component must come first. See declarations of
kono
parents:
diff changeset
174 -- SCO_Unit_Table and SCO_Table in scos.h.
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 Locked : Boolean := False;
kono
parents:
diff changeset
177 -- Table reallocation is permitted only if this is False. A client may
kono
parents:
diff changeset
178 -- set Locked to True, in which case any operation that might expand or
kono
parents:
diff changeset
179 -- shrink the table will cause an assertion failure. While a table is
kono
parents:
diff changeset
180 -- locked, its address in memory remains fixed and unchanging.
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 P : Table_Private;
kono
parents:
diff changeset
183 end record;
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 function Is_Empty (T : Instance) return Boolean;
kono
parents:
diff changeset
186 pragma Inline (Is_Empty);
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 procedure Init (T : in out Instance);
kono
parents:
diff changeset
189 -- Reinitializes the table to empty. There is no need to call this before
kono
parents:
diff changeset
190 -- using a table; tables default to empty.
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 procedure Free (T : in out Instance) renames Init;
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 function First return Table_Index_Type;
kono
parents:
diff changeset
195 pragma Inline (First);
kono
parents:
diff changeset
196 -- Export First as synonym for Table_Low_Bound (parallel with use of Last)
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 function Last (T : Instance) return Table_Last_Type;
kono
parents:
diff changeset
199 pragma Inline (Last);
kono
parents:
diff changeset
200 -- Returns the current value of the last used entry in the table, which can
kono
parents:
diff changeset
201 -- then be used as a subscript for Table.
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 procedure Release (T : in out Instance);
kono
parents:
diff changeset
204 -- Storage is allocated in chunks according to the values given in the
kono
parents:
diff changeset
205 -- Table_Initial and Table_Increment parameters. If Release_Threshold is
kono
parents:
diff changeset
206 -- 0 or the length of the table does not exceed this threshold then a call
kono
parents:
diff changeset
207 -- to Release releases all storage that is allocated, but is not logically
kono
parents:
diff changeset
208 -- part of the current array value; otherwise the call to Release leaves
kono
parents:
diff changeset
209 -- the current array value plus 0.1% of the current table length free
kono
parents:
diff changeset
210 -- elements located at the end of the table. This parameter facilitates
kono
parents:
diff changeset
211 -- reopening large tables and adding a few elements without allocating a
kono
parents:
diff changeset
212 -- chunk of memory. In both cases current array values are not affected by
kono
parents:
diff changeset
213 -- this call.
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 procedure Set_Last (T : in out Instance; New_Val : Table_Last_Type);
kono
parents:
diff changeset
216 pragma Inline (Set_Last);
kono
parents:
diff changeset
217 -- This procedure sets Last to the indicated value. If necessary the table
kono
parents:
diff changeset
218 -- is reallocated to accommodate the new value (i.e. on return the
kono
parents:
diff changeset
219 -- allocated table has an upper bound of at least Last). If Set_Last
kono
parents:
diff changeset
220 -- reduces the size of the table, then logically entries are removed from
kono
parents:
diff changeset
221 -- the table. If Set_Last increases the size of the table, then new entries
kono
parents:
diff changeset
222 -- are logically added to the table.
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 procedure Increment_Last (T : in out Instance);
kono
parents:
diff changeset
225 pragma Inline (Increment_Last);
kono
parents:
diff changeset
226 -- Adds 1 to Last (same as Set_Last (Last + 1))
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 procedure Decrement_Last (T : in out Instance);
kono
parents:
diff changeset
229 pragma Inline (Decrement_Last);
kono
parents:
diff changeset
230 -- Subtracts 1 from Last (same as Set_Last (Last - 1))
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 procedure Append (T : in out Instance; New_Val : Table_Component_Type);
kono
parents:
diff changeset
233 pragma Inline (Append);
kono
parents:
diff changeset
234 -- Appends New_Val onto the end of the table
kono
parents:
diff changeset
235 -- Equivalent to:
kono
parents:
diff changeset
236 -- Increment_Last (T);
kono
parents:
diff changeset
237 -- T.Table (T.Last) := New_Val;
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 procedure Append_All (T : in out Instance; New_Vals : Table_Type);
kono
parents:
diff changeset
240 -- Appends all components of New_Vals
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 procedure Set_Item
kono
parents:
diff changeset
243 (T : in out Instance;
kono
parents:
diff changeset
244 Index : Valid_Table_Index_Type;
kono
parents:
diff changeset
245 Item : Table_Component_Type);
kono
parents:
diff changeset
246 pragma Inline (Set_Item);
kono
parents:
diff changeset
247 -- Put Item in the table at position Index. If Index points to an existing
kono
parents:
diff changeset
248 -- item (i.e. it is in the range First .. Last (T)), the item is replaced.
kono
parents:
diff changeset
249 -- Otherwise (i.e. Index > Last (T)), the table is expanded, and Last is
kono
parents:
diff changeset
250 -- set to Index.
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 procedure Move (From, To : in out Instance);
kono
parents:
diff changeset
253 -- Moves from From to To, and sets From to empty
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 procedure Allocate (T : in out Instance; Num : Integer := 1);
kono
parents:
diff changeset
256 pragma Inline (Allocate);
kono
parents:
diff changeset
257 -- Adds Num to Last
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 generic
kono
parents:
diff changeset
260 with procedure Action
kono
parents:
diff changeset
261 (Index : Valid_Table_Index_Type;
kono
parents:
diff changeset
262 Item : Table_Component_Type;
kono
parents:
diff changeset
263 Quit : in out Boolean) is <>;
kono
parents:
diff changeset
264 procedure For_Each (Table : Instance);
kono
parents:
diff changeset
265 -- Calls procedure Action for each component of the table, or until one of
kono
parents:
diff changeset
266 -- these calls set Quit to True.
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 generic
kono
parents:
diff changeset
269 with function Lt (Comp1, Comp2 : Table_Component_Type) return Boolean;
kono
parents:
diff changeset
270 procedure Sort_Table (Table : in out Instance);
kono
parents:
diff changeset
271 -- This procedure sorts the components of the table into ascending order
kono
parents:
diff changeset
272 -- making calls to Lt to do required comparisons, and using assignments
kono
parents:
diff changeset
273 -- to move components around. The Lt function returns True if Comp1 is
kono
parents:
diff changeset
274 -- less than Comp2 (in the sense of the desired sort), and False if Comp1
kono
parents:
diff changeset
275 -- is greater than Comp2. For equal objects it does not matter if True or
kono
parents:
diff changeset
276 -- False is returned (it is slightly more efficient to return False). The
kono
parents:
diff changeset
277 -- sort is not stable (the order of equal items in the table is not
kono
parents:
diff changeset
278 -- preserved).
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 private
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 type Table_Private is record
kono
parents:
diff changeset
283 Last_Allocated : Table_Last_Type := Table_Low_Bound - 1;
kono
parents:
diff changeset
284 -- Subscript of the maximum entry in the currently allocated table.
kono
parents:
diff changeset
285 -- Initial value ensures that we initially allocate the table.
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 Last : Table_Last_Type := Table_Low_Bound - 1;
kono
parents:
diff changeset
288 -- Current value of Last function
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 -- Invariant: Last <= Last_Allocated
kono
parents:
diff changeset
291 end record;
kono
parents:
diff changeset
292
kono
parents:
diff changeset
293 end GNAT.Dynamic_Tables;