2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / a-cforma.ads
blob58a768c9b1f8850a057ac58bd12b8851291b5abd
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 _ O R D E R E D _ M A P S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2015, 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_Ordered_Maps in
33 -- the 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 -- content of a container: Key, Element, Next, Query_Element, Previous,
42 -- Has_Element, Iterate, Reverse_Iterate. This change is motivated by the
43 -- need to have cursors which are valid on different containers (typically a
44 -- container C and its previous version C'Old) for expressing properties,
45 -- which is not possible if cursors encapsulate an access to the underlying
46 -- container. The operators "<" and ">" that could not be modified that way
47 -- have been removed.
49 -- There are four new functions:
51 -- function Strict_Equal (Left, Right : Map) return Boolean;
52 -- function Overlap (Left, Right : Map) return Boolean;
53 -- function First_To_Previous (Container : Map; Current : Cursor)
54 -- return Map;
55 -- function Current_To_Last (Container : Map; Current : Cursor)
56 -- return Map;
58 -- See detailed specifications for these subprograms
60 private with Ada.Containers.Red_Black_Trees;
62 generic
63 type Key_Type is private;
64 type Element_Type is private;
66 with function "<" (Left, Right : Key_Type) return Boolean is <>;
67 with function "=" (Left, Right : Element_Type) return Boolean is <>;
69 package Ada.Containers.Formal_Ordered_Maps with
70 Pure,
71 SPARK_Mode
73 pragma Annotate (GNATprove, External_Axiomatization);
75 function Equivalent_Keys (Left, Right : Key_Type) return Boolean with
76 Global => null;
78 type Map (Capacity : Count_Type) is private with
79 Iterable => (First => First,
80 Next => Next,
81 Has_Element => Has_Element,
82 Element => Element),
83 Default_Initial_Condition => Is_Empty (Map);
84 pragma Preelaborable_Initialization (Map);
86 type Cursor is private;
87 pragma Preelaborable_Initialization (Cursor);
89 Empty_Map : constant Map;
91 No_Element : constant Cursor;
93 function "=" (Left, Right : Map) return Boolean with
94 Global => null;
96 function Length (Container : Map) return Count_Type with
97 Global => null;
99 function Is_Empty (Container : Map) return Boolean with
100 Global => null;
102 procedure Clear (Container : in out Map) with
103 Global => null;
105 procedure Assign (Target : in out Map; Source : Map) with
106 Global => null,
107 Pre => Target.Capacity >= Length (Source);
109 function Copy (Source : Map; Capacity : Count_Type := 0) return Map with
110 Global => null,
111 Pre => Capacity = 0 or else Capacity >= Source.Capacity;
113 function Key (Container : Map; Position : Cursor) return Key_Type with
114 Global => null,
115 Pre => Has_Element (Container, Position);
117 function Element
118 (Container : Map;
119 Position : Cursor) return Element_Type
120 with
121 Global => null,
122 Pre => Has_Element (Container, Position);
124 procedure Replace_Element
125 (Container : in out Map;
126 Position : Cursor;
127 New_Item : Element_Type)
128 with
129 Global => null,
130 Pre => Has_Element (Container, Position);
132 procedure Move (Target : in out Map; Source : in out Map) with
133 Global => null,
134 Pre => Target.Capacity >= Length (Source);
136 procedure Insert
137 (Container : in out Map;
138 Key : Key_Type;
139 New_Item : Element_Type;
140 Position : out Cursor;
141 Inserted : out Boolean)
142 with
143 Global => null,
144 Pre => Length (Container) < Container.Capacity;
146 procedure Insert
147 (Container : in out Map;
148 Key : Key_Type;
149 New_Item : Element_Type)
150 with
151 Global => null,
152 Pre => Length (Container) < Container.Capacity
153 and then (not Contains (Container, Key));
155 procedure Include
156 (Container : in out Map;
157 Key : Key_Type;
158 New_Item : Element_Type)
159 with
160 Global => null,
161 Pre => Length (Container) < Container.Capacity;
163 procedure Replace
164 (Container : in out Map;
165 Key : Key_Type;
166 New_Item : Element_Type)
167 with
168 Global => null,
169 Pre => Contains (Container, Key);
171 procedure Exclude (Container : in out Map; Key : Key_Type) with
172 Global => null;
174 procedure Delete (Container : in out Map; Key : Key_Type) with
175 Global => null,
176 Pre => Contains (Container, Key);
178 procedure Delete (Container : in out Map; Position : in out Cursor) with
179 Global => null,
180 Pre => Has_Element (Container, Position);
182 procedure Delete_First (Container : in out Map) with
183 Global => null;
185 procedure Delete_Last (Container : in out Map) with
186 Global => null;
188 function First (Container : Map) return Cursor with
189 Global => null;
191 function First_Element (Container : Map) return Element_Type with
192 Global => null,
193 Pre => not Is_Empty (Container);
195 function First_Key (Container : Map) return Key_Type with
196 Global => null,
197 Pre => not Is_Empty (Container);
199 function Last (Container : Map) return Cursor with
200 Global => null;
202 function Last_Element (Container : Map) return Element_Type with
203 Global => null,
204 Pre => not Is_Empty (Container);
206 function Last_Key (Container : Map) return Key_Type with
207 Global => null,
208 Pre => not Is_Empty (Container);
210 function Next (Container : Map; Position : Cursor) return Cursor with
211 Global => null,
212 Pre => Has_Element (Container, Position) or else Position = No_Element;
214 procedure Next (Container : Map; Position : in out Cursor) with
215 Global => null,
216 Pre => Has_Element (Container, Position) or else Position = No_Element;
218 function Previous (Container : Map; Position : Cursor) return Cursor with
219 Global => null,
220 Pre => Has_Element (Container, Position) or else Position = No_Element;
222 procedure Previous (Container : Map; Position : in out Cursor) with
223 Global => null,
224 Pre => Has_Element (Container, Position) or else Position = No_Element;
226 function Find (Container : Map; Key : Key_Type) return Cursor with
227 Global => null;
229 function Element (Container : Map; Key : Key_Type) return Element_Type with
230 Global => null,
231 Pre => Contains (Container, Key);
233 function Floor (Container : Map; Key : Key_Type) return Cursor with
234 Global => null;
236 function Ceiling (Container : Map; Key : Key_Type) return Cursor with
237 Global => null;
239 function Contains (Container : Map; Key : Key_Type) return Boolean with
240 Global => null;
242 function Has_Element (Container : Map; Position : Cursor) return Boolean
243 with
244 Global => null;
246 function Strict_Equal (Left, Right : Map) return Boolean with
247 Ghost,
248 Global => null;
249 -- Strict_Equal returns True if the containers are physically equal, i.e.
250 -- they are structurally equal (function "=" returns True) and that they
251 -- have the same set of cursors.
253 function First_To_Previous (Container : Map; Current : Cursor) return Map
254 with
255 Ghost,
256 Global => null,
257 Pre => Has_Element (Container, Current) or else Current = No_Element;
259 function Current_To_Last (Container : Map; Current : Cursor) return Map
260 with
261 Ghost,
262 Global => null,
263 Pre => Has_Element (Container, Current) or else Current = No_Element;
264 -- First_To_Previous returns a container containing all elements preceding
265 -- Current (excluded) in Container. Current_To_Last returns a container
266 -- containing all elements following Current (included) in Container.
267 -- These two new functions can be used to express invariant properties in
268 -- loops which iterate over containers. First_To_Previous returns the part
269 -- of the container already scanned and Current_To_Last the part not
270 -- scanned yet.
272 function Overlap (Left, Right : Map) return Boolean with
273 Global => null;
274 -- Overlap returns True if the containers have common keys
276 private
277 pragma SPARK_Mode (Off);
279 pragma Inline (Next);
280 pragma Inline (Previous);
282 subtype Node_Access is Count_Type;
284 use Red_Black_Trees;
286 type Node_Type is record
287 Has_Element : Boolean := False;
288 Parent : Node_Access := 0;
289 Left : Node_Access := 0;
290 Right : Node_Access := 0;
291 Color : Red_Black_Trees.Color_Type := Red;
292 Key : Key_Type;
293 Element : Element_Type;
294 end record;
296 package Tree_Types is
297 new Ada.Containers.Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
299 type Map (Capacity : Count_Type) is
300 new Tree_Types.Tree_Type (Capacity) with null record;
302 type Cursor is record
303 Node : Node_Access;
304 end record;
306 Empty_Map : constant Map := (Capacity => 0, others => <>);
308 No_Element : constant Cursor := (Node => 0);
310 end Ada.Containers.Formal_Ordered_Maps;