OSDN Git Service

* gcc-interface/decl.c (make_type_from_size) <INTEGER_TYPE>: Just copy
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-spitbo.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --                         G N A T . S P I T B O L                          --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --                     Copyright (C) 1997-2006, AdaCore                     --
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 --  SPITBOL-like interface facilities
35
36 --  This package provides a set of interfaces to semantic operations copied
37 --  from SPITBOL, including a complete implementation of SPITBOL pattern
38 --  matching. The code is derived from the original SPITBOL MINIMAL sources,
39 --  created by Robert Dewar. The translation is not exact, but the
40 --  algorithmic approaches are similar.
41
42 with Ada.Finalization;      use Ada.Finalization;
43 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
44 with Interfaces;            use Interfaces;
45
46 package GNAT.Spitbol is
47    pragma Preelaborate;
48
49    --  The Spitbol package relies heavily on the Unbounded_String package,
50    --  using the synonym VString for variable length string. The following
51    --  declarations define this type and other useful abbreviations.
52
53    subtype VString is Ada.Strings.Unbounded.Unbounded_String;
54
55    function V (Source : String) return VString
56      renames Ada.Strings.Unbounded.To_Unbounded_String;
57
58    function S (Source : VString) return String
59      renames Ada.Strings.Unbounded.To_String;
60
61    Nul : VString renames Ada.Strings.Unbounded.Null_Unbounded_String;
62
63    -------------------------
64    -- Facilities Provided --
65    -------------------------
66
67    --  The SPITBOL support in GNAT consists of this package together with
68    --  several child packages. In this package, we have first a set of
69    --  useful string functions, copied exactly from the corresponding
70    --  SPITBOL functions, except that we had to rename REVERSE because
71    --  reverse is a reserved word (it is now Reverse_String).
72
73    --  The second element of the parent package is a generic implementation
74    --  of a table facility. In SPITBOL, the TABLE function allows general
75    --  mappings from any datatype to any other datatype, and of course, as
76    --  always, we can freely mix multiple types in the same table.
77
78    --  The Ada version of tables is strongly typed, so the indexing type and
79    --  the range type are always of a consistent type. In this implementation
80    --  we only provide VString as an indexing type, since this is by far the
81    --  most common case. The generic instantiation specifies the range type
82    --  to be used.
83
84    --  Three child packages provide standard instantiations of this table
85    --  package for three common datatypes:
86
87    --    GNAT.Spitbol.Table_Boolean     (file g-sptabo.ads)
88
89    --      The range type is Boolean. The default value is False. This
90    --      means that this table is essentially a representation of a set.
91
92    --    GNAT.Spitbol.Table_Integer     (file g-sptain.ads)
93
94    --      The range type is Integer. The default value is Integer'First.
95    --      This provides a general mapping from strings to integers.
96
97    --    GNAT.Spitbol.Table_VString     (file g-sptavs.ads)
98
99    --      The range type is VString. The default value is the null string.
100    --      This provides a general mapping from strings to strings.
101
102    --  Finally there is another child package:
103
104    --    GNAT.Spitbol.Patterns          (file g-spipat.ads)
105
106    --       This child package provides a complete implementation of SPITBOL
107    --       pattern matching. The spec contains a complete tutorial on the
108    --       use of pattern matching.
109
110    ---------------------------------
111    -- Standard String Subprograms --
112    ---------------------------------
113
114    --  This section contains some operations on unbounded strings that are
115    --  closely related to those in the package Unbounded.Strings, but they
116    --  correspond to the SPITBOL semantics for these operations.
117
118    function Char (Num : Natural) return Character;
119    pragma Inline (Char);
120    --  Equivalent to Character'Val (Num)
121
122    function Lpad
123      (Str : VString;
124       Len : Natural;
125       Pad : Character := ' ') return VString;
126    function Lpad
127      (Str : String;
128       Len : Natural;
129       Pad : Character := ' ') return VString;
130    --  If the length of Str is greater than or equal to Len, then Str is
131    --  returned unchanged. Otherwise, The value returned is obtained by
132    --  concatenating Length (Str) - Len instances of the Pad character to
133    --  the left hand side.
134
135    procedure Lpad
136      (Str  : in out VString;
137       Len  : Natural;
138       Pad  : Character := ' ');
139    --  The procedure form is identical to the function form, except that
140    --  the result overwrites the input argument Str.
141
142    function Reverse_String (Str : VString) return VString;
143    function Reverse_String (Str : String)  return VString;
144    --  Returns result of reversing the string Str, i.e. the result returned
145    --  is a mirror image (end-for-end reversal) of the input string.
146
147    procedure Reverse_String (Str : in out VString);
148    --  The procedure form is identical to the function form, except that the
149    --  result overwrites the input argument Str.
150
151    function Rpad
152      (Str : VString;
153       Len : Natural;
154       Pad : Character := ' ') return VString;
155    function Rpad
156      (Str : String;
157       Len : Natural;
158       Pad : Character := ' ') return VString;
159    --  If the length of Str is greater than or equal to Len, then Str is
160    --  returned unchanged. Otherwise, The value returned is obtained by
161    --  concatenating Length (Str) - Len instances of the Pad character to
162    --  the right hand side.
163
164    procedure Rpad
165      (Str  : in out VString;
166       Len  : Natural;
167       Pad  : Character := ' ');
168    --  The procedure form is identical to the function form, except that the
169    --  result overwrites the input argument Str.
170
171    function Size (Source : VString) return Natural
172      renames Ada.Strings.Unbounded.Length;
173
174    function Substr
175      (Str   : VString;
176       Start : Positive;
177       Len   : Natural) return  VString;
178    function Substr
179      (Str   : String;
180       Start : Positive;
181       Len   : Natural) return  VString;
182    --  Returns the substring starting at the given character position (which
183    --  is always counted from the start of the string, regardless of bounds,
184    --  e.g. 2 means starting with the second character of the string), and
185    --  with the length (Len) given. Indexing_Error is raised if the starting
186    --  position is out of range, and Length_Error is raised if Len is too long.
187
188    function Trim (Str : VString) return VString;
189    function Trim (Str : String)  return VString;
190    --  Returns the string obtained by removing all spaces from the right
191    --  hand side of the string Str.
192
193    procedure Trim (Str : in out VString);
194    --  The procedure form is identical to the function form, except that the
195    --  result overwrites the input argument Str.
196
197    -----------------------
198    -- Utility Functions --
199    -----------------------
200
201    --  In SPITBOL, integer values can be freely treated as strings. The
202    --  following definitions help provide some of this capability in
203    --  some common cases.
204
205    function "&" (Num : Integer; Str : String)  return String;
206    function "&" (Str : String;  Num : Integer) return String;
207    function "&" (Num : Integer; Str : VString) return VString;
208    function "&" (Str : VString; Num : Integer) return VString;
209    --  In all these concatenation operations, the integer is converted to
210    --  its corresponding decimal string form, with no leading blank.
211
212    function S (Num : Integer) return String;
213    function V (Num : Integer) return VString;
214    --  These operators return the given integer converted to its decimal
215    --  string form with no leading blank.
216
217    function N (Str : VString) return Integer;
218    --  Converts string to number (same as Integer'Value (S (Str)))
219
220    -------------------
221    -- Table Support --
222    -------------------
223
224    --  So far, we only provide support for tables whose indexing data values
225    --  are strings (or unbounded strings). The values stored may be of any
226    --  type, as supplied by the generic formal parameter.
227
228    generic
229
230       type Value_Type is private;
231       --  Any non-limited type can be used as the value type in the table
232
233       Null_Value : Value_Type;
234       --  Value used to represent a value that is not present in the table
235
236       with function Img (A : Value_Type) return String;
237       --  Used to provide image of value in Dump procedure
238
239       with function "=" (A, B : Value_Type) return Boolean is <>;
240       --  This allows a user-defined equality function to override the
241       --  predefined equality function.
242
243    package Table is
244
245       ------------------------
246       -- Table Declarations --
247       ------------------------
248
249       type Table (N : Unsigned_32) is private;
250       --  This is the table type itself. A table is a mapping from string
251       --  values to values of Value_Type. The discriminant is an estimate of
252       --  the number of values in the table. If the estimate is much too
253       --  high, some space is wasted, if the estimate is too low, access to
254       --  table elements is slowed down. The type Table has copy semantics,
255       --  not reference semantics. This means that if a table is copied
256       --  using simple assignment, then the two copies refer to entirely
257       --  separate tables.
258
259       -----------------------------
260       -- Table Access Operations --
261       -----------------------------
262
263       function Get (T : Table; Name : VString)   return Value_Type;
264       function Get (T : Table; Name : Character) return Value_Type;
265       pragma Inline (Get);
266       function Get (T : Table; Name : String)    return Value_Type;
267
268       --  If an entry with the given name exists in the table, then the
269       --  corresponding Value_Type value is returned. Otherwise Null_Value
270       --  is returned.
271
272       function Present (T : Table; Name : VString)   return Boolean;
273       function Present (T : Table; Name : Character) return Boolean;
274       pragma Inline (Present);
275       function Present (T : Table; Name : String)    return Boolean;
276       --  Determines if an entry with the given name is present in the table.
277       --  A returned value of True means that it is in the table, otherwise
278       --  False indicates that it is not in the table.
279
280       procedure Delete (T : in out Table; Name : VString);
281       procedure Delete (T : in out Table; Name : Character);
282       pragma Inline (Delete);
283       procedure Delete (T : in out Table; Name : String);
284       --  Deletes the table element with the given name from the table. If
285       --  no element in the table has this name, then the call has no effect.
286
287       procedure Set (T : in out Table; Name  : VString;   Value : Value_Type);
288       procedure Set (T : in out Table; Name  : Character; Value : Value_Type);
289       pragma Inline (Set);
290       procedure Set (T : in out Table; Name  : String;    Value : Value_Type);
291       --  Sets the value of the element with the given name to the given
292       --  value. If Value is equal to Null_Value, the effect is to remove
293       --  the entry from the table. If no element with the given name is
294       --  currently in the table, then a new element with the given value
295       --  is created.
296
297       ----------------------------
298       -- Allocation and Copying --
299       ----------------------------
300
301       --  Table is a controlled type, so that all storage associated with
302       --  tables is properly reclaimed when a Table value is abandoned.
303       --  Tables have value semantics rather than reference semantics as
304       --  in Spitbol, i.e. when you assign a copy you end up with two
305       --  distinct copies of the table, as though COPY had been used in
306       --  Spitbol. It seems clearly more appropriate in Ada to require
307       --  the use of explicit pointers for reference semantics.
308
309       procedure Clear (T : in out Table);
310       --  Clears all the elements of the given table, freeing associated
311       --  storage. On return T is an empty table with no elements.
312
313       procedure Copy (From : Table; To : in out Table);
314       --  First all the elements of table To are cleared (as described for
315       --  the Clear procedure above), then all the elements of table From
316       --  are copied into To. In the case where the tables From and To have
317       --  the same declared size (i.e. the same discriminant), the call to
318       --  Copy has the same effect as the assignment of From to To. The
319       --  difference is that, unlike the assignment statement, which will
320       --  cause a Constraint_Error if the source and target are of different
321       --  sizes, Copy works fine with different sized tables.
322
323       ----------------
324       -- Conversion --
325       ----------------
326
327       type Table_Entry is record
328          Name  : VString;
329          Value : Value_Type;
330       end record;
331
332       type Table_Array is array (Positive range <>) of Table_Entry;
333
334       function Convert_To_Array (T : Table) return Table_Array;
335       --  Returns a Table_Array value with a low bound of 1, and a length
336       --  corresponding to the number of elements in the table. The elements
337       --  of the array give the elements of the table in unsorted order.
338
339       ---------------
340       -- Debugging --
341       ---------------
342
343       procedure Dump (T : Table; Str : String := "Table");
344       --  Dump contents of given table to the standard output file. The
345       --  string value Str is used as the name of the table in the dump.
346
347       procedure Dump (T : Table_Array; Str : String := "Table_Array");
348       --  Dump contents of given table array to the current output file. The
349       --  string value Str is used as the name of the table array in the dump.
350
351    private
352
353       ------------------
354       -- Private Part --
355       ------------------
356
357       --  A Table is a pointer to a hash table which contains the indicated
358       --  number of hash elements (the number is forced to the next odd value
359       --  if it is even to improve hashing performance). If more than one
360       --  of the entries in a table hashes to the same slot, the Next field
361       --  is used to chain entries from the header. The chains are not kept
362       --  ordered. A chain is terminated by a null pointer in Next. An unused
363       --  chain is marked by an element whose Name is null and whose value
364       --  is Null_Value.
365
366       type Hash_Element;
367       type Hash_Element_Ptr is access all Hash_Element;
368
369       type Hash_Element is record
370          Name  : String_Access    := null;
371          Value : Value_Type       := Null_Value;
372          Next  : Hash_Element_Ptr := null;
373       end record;
374
375       type Hash_Table is
376         array (Unsigned_32 range <>) of aliased Hash_Element;
377
378       type Table (N : Unsigned_32) is new Controlled with record
379          Elmts : Hash_Table (1 .. N);
380       end record;
381
382       pragma Finalize_Storage_Only (Table);
383
384       procedure Adjust (Object : in out Table);
385       --  The Adjust procedure does a deep copy of the table structure
386       --  so that the effect of assignment is, like other assignments
387       --  in Ada, value-oriented.
388
389       procedure Finalize (Object : in out Table);
390       --  This is the finalization routine that ensures that all storage
391       --  associated with a table is properly released when a table object
392       --  is abandoned and finalized.
393
394    end Table;
395
396 end GNAT.Spitbol;