2014-09-15 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[official-gcc.git] / gcc / ada / a-cfhama.ads
blobb5c440ec74df239d0e8cbac1d0bc97322eaaa10d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . F O R M A L _ H A S H E D _ M A P S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2014, Free Software Foundation, Inc. --
10 -- --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
14 -- --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
21 -- --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
25 -- --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
30 ------------------------------------------------------------------------------
32 -- This spec is derived from package Ada.Containers.Bounded_Hashed_Maps in the
33 -- Ada 2012 RM. The modifications are meant to facilitate formal proofs by
34 -- making it easier to express properties, and by making the specification of
35 -- this unit compatible with SPARK 2014. Note that the API of this unit may be
36 -- subject to incompatible changes as SPARK 2014 evolves.
38 -- The modifications are:
40 -- A parameter for the container is added to every function reading the
41 -- contents of a container: Key, Element, Next, Query_Element, Has_Element,
42 -- Iterate, Equivalent_Keys. This change is motivated by the need to have
43 -- cursors which are valid on different containers (typically a container C
44 -- and its previous version C'Old) for expressing properties, which is not
45 -- possible if cursors encapsulate an access to the underlying container.
47 -- There are four new functions:
49 -- function Strict_Equal (Left, Right : Map) return Boolean;
50 -- function Overlap (Left, Right : Map) return Boolean;
51 -- function First_To_Previous (Container : Map; Current : Cursor)
52 -- return Map;
53 -- function Current_To_Last (Container : Map; Current : Cursor)
54 -- return Map;
56 -- See detailed specifications for these subprograms
58 private with Ada.Containers.Hash_Tables;
60 generic
61 type Key_Type is private;
62 type Element_Type is private;
64 with function Hash (Key : Key_Type) return Hash_Type;
65 with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
66 with function "=" (Left, Right : Element_Type) return Boolean is <>;
68 package Ada.Containers.Formal_Hashed_Maps is
69 pragma Annotate (GNATprove, External_Axiomatization);
70 pragma Pure;
71 pragma SPARK_Mode (On);
73 type Map (Capacity : Count_Type; Modulus : Hash_Type) is private with
74 Iterable => (First => First,
75 Next => Next,
76 Has_Element => Has_Element,
77 Element => Element);
78 pragma Preelaborable_Initialization (Map);
80 type Cursor is private;
81 pragma Preelaborable_Initialization (Cursor);
83 Empty_Map : constant Map;
85 No_Element : constant Cursor;
87 function "=" (Left, Right : Map) return Boolean with
88 Global => null;
90 function Capacity (Container : Map) return Count_Type with
91 Global => null;
93 procedure Reserve_Capacity
94 (Container : in out Map;
95 Capacity : Count_Type)
96 with
97 Global => null,
98 Pre => Capacity <= Container.Capacity;
100 function Length (Container : Map) return Count_Type with
101 Global => null;
103 function Is_Empty (Container : Map) return Boolean with
104 Global => null;
106 procedure Clear (Container : in out Map) with
107 Global => null;
109 procedure Assign (Target : in out Map; Source : Map) with
110 Global => null,
111 Pre => Target.Capacity >= Length (Source);
113 function Copy
114 (Source : Map;
115 Capacity : Count_Type := 0) return Map
116 with
117 Global => null,
118 Pre => Capacity = 0 or else Capacity >= Source.Capacity;
119 -- Copy returns a container stricty equal to Source. It must have
120 -- the same cursors associated with each element. Therefore:
121 -- - capacity=0 means use container.capacity as capacity of target
122 -- - the modulus cannot be changed.
124 function Key (Container : Map; Position : Cursor) return Key_Type with
125 Global => null,
126 Pre => Has_Element (Container, Position);
128 function Element
129 (Container : Map;
130 Position : Cursor) return Element_Type
131 with
132 Global => null,
133 Pre => Has_Element (Container, Position);
135 procedure Replace_Element
136 (Container : in out Map;
137 Position : Cursor;
138 New_Item : Element_Type)
139 with
140 Global => null,
141 Pre => Has_Element (Container, Position);
143 procedure Move (Target : in out Map; Source : in out Map) with
144 Global => null,
145 Pre => Target.Capacity >= Length (Source);
147 procedure Insert
148 (Container : in out Map;
149 Key : Key_Type;
150 New_Item : Element_Type;
151 Position : out Cursor;
152 Inserted : out Boolean)
153 with
154 Global => null,
155 Pre => Length (Container) < Container.Capacity;
157 procedure Insert
158 (Container : in out Map;
159 Key : Key_Type;
160 New_Item : Element_Type)
161 with
162 Global => null,
163 Pre => Length (Container) < Container.Capacity
164 and then (not Contains (Container, Key));
166 procedure Include
167 (Container : in out Map;
168 Key : Key_Type;
169 New_Item : Element_Type)
170 with
171 Global => null,
172 Pre => Length (Container) < Container.Capacity;
174 procedure Replace
175 (Container : in out Map;
176 Key : Key_Type;
177 New_Item : Element_Type)
178 with
179 Global => null,
180 Pre => Contains (Container, Key);
182 procedure Exclude (Container : in out Map; Key : Key_Type) with
183 Global => null;
185 procedure Delete (Container : in out Map; Key : Key_Type) with
186 Global => null,
187 Pre => Contains (Container, Key);
189 procedure Delete (Container : in out Map; Position : in out Cursor) with
190 Global => null,
191 Pre => Has_Element (Container, Position);
193 function First (Container : Map) return Cursor with
194 Global => null;
196 function Next (Container : Map; Position : Cursor) return Cursor with
197 Global => null,
198 Pre => Has_Element (Container, Position) or else Position = No_Element;
200 procedure Next (Container : Map; Position : in out Cursor) with
201 Global => null,
202 Pre => Has_Element (Container, Position) or else Position = No_Element;
204 function Find (Container : Map; Key : Key_Type) return Cursor with
205 Global => null;
207 function Contains (Container : Map; Key : Key_Type) return Boolean with
208 Global => null;
210 function Element (Container : Map; Key : Key_Type) return Element_Type with
211 Global => null,
212 Pre => Contains (Container, Key);
214 function Has_Element (Container : Map; Position : Cursor) return Boolean
215 with
216 Global => null;
218 function Equivalent_Keys
219 (Left : Map;
220 CLeft : Cursor;
221 Right : Map;
222 CRight : Cursor) return Boolean
223 with
224 Global => null;
226 function Equivalent_Keys
227 (Left : Map;
228 CLeft : Cursor;
229 Right : Key_Type) return Boolean
230 with
231 Global => null;
233 function Equivalent_Keys
234 (Left : Key_Type;
235 Right : Map;
236 CRight : Cursor) return Boolean
237 with
238 Global => null;
240 function Default_Modulus (Capacity : Count_Type) return Hash_Type with
241 Global => null;
243 function Strict_Equal (Left, Right : Map) return Boolean with
244 Global => null;
245 -- Strict_Equal returns True if the containers are physically equal, i.e.
246 -- they are structurally equal (function "=" returns True) and that they
247 -- have the same set of cursors.
249 function First_To_Previous (Container : Map; Current : Cursor) return Map
250 with
251 Global => null,
252 Pre => Has_Element (Container, Current) or else Current = No_Element;
253 function Current_To_Last (Container : Map; Current : Cursor) return Map
254 with
255 Global => null,
256 Pre => Has_Element (Container, Current) or else Current = No_Element;
257 -- First_To_Previous returns a container containing all elements preceding
258 -- Current (excluded) in Container. Current_To_Last returns a container
259 -- containing all elements following Current (included) in Container.
260 -- These two new functions can be used to express invariant properties in
261 -- loops which iterate over containers. First_To_Previous returns the part
262 -- of the container already scanned and Current_To_Last the part not
263 -- scanned yet.
265 function Overlap (Left, Right : Map) return Boolean with
266 Global => null;
267 -- Overlap returns True if the containers have common keys
269 private
270 pragma Inline (Length);
271 pragma Inline (Is_Empty);
272 pragma Inline (Clear);
273 pragma Inline (Key);
274 pragma Inline (Element);
275 pragma Inline (Contains);
276 pragma Inline (Capacity);
277 pragma Inline (Has_Element);
278 pragma Inline (Equivalent_Keys);
279 pragma Inline (Next);
280 pragma SPARK_Mode (Off);
282 type Node_Type is record
283 Key : Key_Type;
284 Element : Element_Type;
285 Next : Count_Type;
286 Has_Element : Boolean := False;
287 end record;
289 package HT_Types is new
290 Ada.Containers.Hash_Tables.Generic_Bounded_Hash_Table_Types (Node_Type);
292 type Map (Capacity : Count_Type; Modulus : Hash_Type) is
293 new HT_Types.Hash_Table_Type (Capacity, Modulus) with null record;
295 use HT_Types;
297 type Cursor is record
298 Node : Count_Type;
299 end record;
301 Empty_Map : constant Map := (Capacity => 0, Modulus => 0, others => <>);
303 No_Element : constant Cursor := (Node => 0);
305 end Ada.Containers.Formal_Hashed_Maps;