gcc/ada/
[official-gcc.git] / gcc / ada / a-cforma.ads
blob51e40a2ea2e45604819d591fe0fbe932212f3643
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-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_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 is
70 pragma Annotate (GNATprove, External_Axiomatization);
71 pragma Pure;
72 pragma SPARK_Mode (On);
74 function Equivalent_Keys (Left, Right : Key_Type) return Boolean with
75 Global => null;
77 type Map (Capacity : Count_Type) is private with
78 Iterable => (First => First,
79 Next => Next,
80 Has_Element => Has_Element,
81 Element => Element),
82 Default_Initial_Condition;
83 pragma Preelaborable_Initialization (Map);
85 type Cursor is private;
86 pragma Preelaborable_Initialization (Cursor);
88 Empty_Map : constant Map;
90 No_Element : constant Cursor;
92 function "=" (Left, Right : Map) return Boolean with
93 Global => null;
95 function Length (Container : Map) return Count_Type with
96 Global => null;
98 function Is_Empty (Container : Map) return Boolean with
99 Global => null;
101 procedure Clear (Container : in out Map) with
102 Global => null;
104 procedure Assign (Target : in out Map; Source : Map) with
105 Global => null,
106 Pre => Target.Capacity >= Length (Source);
108 function Copy (Source : Map; Capacity : Count_Type := 0) return Map with
109 Global => null,
110 Pre => Capacity = 0 or else Capacity >= Source.Capacity;
112 function Key (Container : Map; Position : Cursor) return Key_Type with
113 Global => null,
114 Pre => Has_Element (Container, Position);
116 function Element
117 (Container : Map;
118 Position : Cursor) return Element_Type
119 with
120 Global => null,
121 Pre => Has_Element (Container, Position);
123 procedure Replace_Element
124 (Container : in out Map;
125 Position : Cursor;
126 New_Item : Element_Type)
127 with
128 Global => null,
129 Pre => Has_Element (Container, Position);
131 procedure Move (Target : in out Map; Source : in out Map) with
132 Global => null,
133 Pre => Target.Capacity >= Length (Source);
135 procedure Insert
136 (Container : in out Map;
137 Key : Key_Type;
138 New_Item : Element_Type;
139 Position : out Cursor;
140 Inserted : out Boolean)
141 with
142 Global => null,
143 Pre => Length (Container) < Container.Capacity;
145 procedure Insert
146 (Container : in out Map;
147 Key : Key_Type;
148 New_Item : Element_Type)
149 with
150 Global => null,
151 Pre => Length (Container) < Container.Capacity
152 and then (not Contains (Container, Key));
154 procedure Include
155 (Container : in out Map;
156 Key : Key_Type;
157 New_Item : Element_Type)
158 with
159 Global => null,
160 Pre => Length (Container) < Container.Capacity;
162 procedure Replace
163 (Container : in out Map;
164 Key : Key_Type;
165 New_Item : Element_Type)
166 with
167 Global => null,
168 Pre => Contains (Container, Key);
170 procedure Exclude (Container : in out Map; Key : Key_Type) with
171 Global => null;
173 procedure Delete (Container : in out Map; Key : Key_Type) with
174 Global => null,
175 Pre => Contains (Container, Key);
177 procedure Delete (Container : in out Map; Position : in out Cursor) with
178 Global => null,
179 Pre => Has_Element (Container, Position);
181 procedure Delete_First (Container : in out Map) with
182 Global => null;
184 procedure Delete_Last (Container : in out Map) with
185 Global => null;
187 function First (Container : Map) return Cursor with
188 Global => null;
190 function First_Element (Container : Map) return Element_Type with
191 Global => null,
192 Pre => not Is_Empty (Container);
194 function First_Key (Container : Map) return Key_Type with
195 Global => null,
196 Pre => not Is_Empty (Container);
198 function Last (Container : Map) return Cursor with
199 Global => null;
201 function Last_Element (Container : Map) return Element_Type with
202 Global => null,
203 Pre => not Is_Empty (Container);
205 function Last_Key (Container : Map) return Key_Type with
206 Global => null,
207 Pre => not Is_Empty (Container);
209 function Next (Container : Map; Position : Cursor) return Cursor with
210 Global => null,
211 Pre => Has_Element (Container, Position) or else Position = No_Element;
213 procedure Next (Container : Map; Position : in out Cursor) with
214 Global => null,
215 Pre => Has_Element (Container, Position) or else Position = No_Element;
217 function Previous (Container : Map; Position : Cursor) return Cursor with
218 Global => null,
219 Pre => Has_Element (Container, Position) or else Position = No_Element;
221 procedure Previous (Container : Map; Position : in out Cursor) with
222 Global => null,
223 Pre => Has_Element (Container, Position) or else Position = No_Element;
225 function Find (Container : Map; Key : Key_Type) return Cursor with
226 Global => null;
228 function Element (Container : Map; Key : Key_Type) return Element_Type with
229 Global => null,
230 Pre => Contains (Container, Key);
232 function Floor (Container : Map; Key : Key_Type) return Cursor with
233 Global => null;
235 function Ceiling (Container : Map; Key : Key_Type) return Cursor with
236 Global => null;
238 function Contains (Container : Map; Key : Key_Type) return Boolean with
239 Global => null;
241 function Has_Element (Container : Map; Position : Cursor) return Boolean
242 with
243 Global => null;
245 function Strict_Equal (Left, Right : Map) return Boolean with
246 Global => null;
247 -- Strict_Equal returns True if the containers are physically equal, i.e.
248 -- they are structurally equal (function "=" returns True) and that they
249 -- have the same set of cursors.
251 function First_To_Previous (Container : Map; Current : Cursor) return Map
252 with
253 Global => null,
254 Pre => Has_Element (Container, Current) or else Current = No_Element;
255 function Current_To_Last (Container : Map; Current : Cursor) return Map
256 with
257 Global => null,
258 Pre => Has_Element (Container, Current) or else Current = No_Element;
259 -- First_To_Previous returns a container containing all elements preceding
260 -- Current (excluded) in Container. Current_To_Last returns a container
261 -- containing all elements following Current (included) in Container.
262 -- These two new functions can be used to express invariant properties in
263 -- loops which iterate over containers. First_To_Previous returns the part
264 -- of the container already scanned and Current_To_Last the part not
265 -- scanned yet.
267 function Overlap (Left, Right : Map) return Boolean with
268 Global => null;
269 -- Overlap returns True if the containers have common keys
271 private
272 pragma Inline (Next);
273 pragma Inline (Previous);
274 pragma SPARK_Mode (Off);
276 subtype Node_Access is Count_Type;
278 use Red_Black_Trees;
280 type Node_Type is record
281 Has_Element : Boolean := False;
282 Parent : Node_Access := 0;
283 Left : Node_Access := 0;
284 Right : Node_Access := 0;
285 Color : Red_Black_Trees.Color_Type := Red;
286 Key : Key_Type;
287 Element : Element_Type;
288 end record;
290 package Tree_Types is
291 new Ada.Containers.Red_Black_Trees.Generic_Bounded_Tree_Types (Node_Type);
293 type Map (Capacity : Count_Type) is
294 new Tree_Types.Tree_Type (Capacity) with null record;
296 type Cursor is record
297 Node : Node_Access;
298 end record;
300 Empty_Map : constant Map := (Capacity => 0, others => <>);
302 No_Element : constant Cursor := (Node => 0);
304 end Ada.Containers.Formal_Ordered_Maps;