Mercurial > hg > CbC > CbC_gcc
comparison gcc/ada/libgnat/a-cofove.ads @ 145:1830386684a0
gcc-9.2.0
author | anatofuz |
---|---|
date | Thu, 13 Feb 2020 11:34:05 +0900 |
parents | 84e7813d76e9 |
children |
comparison
equal
deleted
inserted
replaced
131:84e7813d76e9 | 145:1830386684a0 |
---|---|
4 -- -- | 4 -- -- |
5 -- A D A . C O N T A I N E R S . F O R M A L _ V E C T O R S -- | 5 -- A D A . C O N T A I N E R S . F O R M A L _ V E C T O R S -- |
6 -- -- | 6 -- -- |
7 -- S p e c -- | 7 -- S p e c -- |
8 -- -- | 8 -- -- |
9 -- Copyright (C) 2004-2018, Free Software Foundation, Inc. -- | 9 -- Copyright (C) 2004-2019, Free Software Foundation, Inc. -- |
10 -- -- | 10 -- -- |
11 -- This specification is derived from the Ada Reference Manual for use with -- | 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 -- | 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. -- | 13 -- apply solely to the contents of the part following the private keyword. -- |
14 -- -- | 14 -- -- |
38 with Ada.Containers.Functional_Vectors; | 38 with Ada.Containers.Functional_Vectors; |
39 | 39 |
40 generic | 40 generic |
41 type Index_Type is range <>; | 41 type Index_Type is range <>; |
42 type Element_Type is private; | 42 type Element_Type is private; |
43 | 43 with function "=" (Left, Right : Element_Type) return Boolean is <>; |
44 Bounded : Boolean := True; | |
45 -- If True, the containers are bounded; the initial capacity is the maximum | |
46 -- size, and heap allocation will be avoided. If False, the containers can | |
47 -- grow via heap allocation. | |
48 | 44 |
49 package Ada.Containers.Formal_Vectors with | 45 package Ada.Containers.Formal_Vectors with |
50 SPARK_Mode | 46 SPARK_Mode |
51 is | 47 is |
52 pragma Annotate (CodePeer, Skip_Analysis); | 48 pragma Annotate (CodePeer, Skip_Analysis); |
71 -- Maximal capacity of any vector. It is the minimum of the size of the | 67 -- Maximal capacity of any vector. It is the minimum of the size of the |
72 -- index range and the last possible Count_Type. | 68 -- index range and the last possible Count_Type. |
73 | 69 |
74 subtype Capacity_Range is Count_Type range 0 .. Last_Count; | 70 subtype Capacity_Range is Count_Type range 0 .. Last_Count; |
75 | 71 |
76 type Vector (Capacity : Capacity_Range) is limited private with | 72 type Vector (Capacity : Capacity_Range) is private with |
77 Default_Initial_Condition => Is_Empty (Vector); | 73 Default_Initial_Condition => Is_Empty (Vector), |
78 -- In the bounded case, Capacity is the capacity of the container, which | 74 Iterable => (First => Iter_First, |
79 -- never changes. In the unbounded case, Capacity is the initial capacity | 75 Has_Element => Iter_Has_Element, |
80 -- of the container, and operations such as Reserve_Capacity and Append can | 76 Next => Iter_Next, |
81 -- increase the capacity. The capacity never shrinks, except in the case of | 77 Element => Element); |
82 -- Clear. | |
83 -- | |
84 -- Note that all objects of type Vector are constrained, including in the | |
85 -- unbounded case; you can't assign from one object to another if the | |
86 -- Capacity is different. | |
87 | 78 |
88 function Length (Container : Vector) return Capacity_Range with | 79 function Length (Container : Vector) return Capacity_Range with |
89 Global => null, | 80 Global => null, |
90 Post => Length'Result <= Capacity (Container); | 81 Post => Length'Result <= Capacity (Container); |
91 | 82 |
184 -- limited which allows usage of 'Old and 'Loop_Entry attributes. | 175 -- limited which allows usage of 'Old and 'Loop_Entry attributes. |
185 | 176 |
186 Ghost, | 177 Ghost, |
187 Global => null, | 178 Global => null, |
188 Post => M.Length (Model'Result) = Length (Container); | 179 Post => M.Length (Model'Result) = Length (Container); |
180 pragma Annotate (GNATprove, Iterable_For_Proof, "Model", Model); | |
189 | 181 |
190 function Element | 182 function Element |
191 (S : M.Sequence; | 183 (S : M.Sequence; |
192 I : Index_Type) return Element_Type renames M.Get; | 184 I : Index_Type) return Element_Type renames M.Get; |
193 -- To improve readability of contracts, we rename the function used to | 185 -- To improve readability of contracts, we rename the function used to |
218 Item => New_Item); | 210 Item => New_Item); |
219 | 211 |
220 function Capacity (Container : Vector) return Capacity_Range with | 212 function Capacity (Container : Vector) return Capacity_Range with |
221 Global => null, | 213 Global => null, |
222 Post => | 214 Post => |
223 Capacity'Result = | 215 Capacity'Result = Container.Capacity; |
224 (if Bounded then | |
225 Container.Capacity | |
226 else | |
227 Capacity_Range'Last); | |
228 pragma Annotate (GNATprove, Inline_For_Proof, Capacity); | 216 pragma Annotate (GNATprove, Inline_For_Proof, Capacity); |
229 | 217 |
230 procedure Reserve_Capacity | 218 procedure Reserve_Capacity |
231 (Container : in out Vector; | 219 (Container : in out Vector; |
232 Capacity : Capacity_Range) | 220 Capacity : Capacity_Range) |
233 with | 221 with |
234 Global => null, | 222 Global => null, |
235 Pre => (if Bounded then Capacity <= Container.Capacity), | 223 Pre => Capacity <= Container.Capacity, |
236 Post => Model (Container) = Model (Container)'Old; | 224 Post => Model (Container) = Model (Container)'Old; |
237 | 225 |
238 function Is_Empty (Container : Vector) return Boolean with | 226 function Is_Empty (Container : Vector) return Boolean with |
239 Global => null, | 227 Global => null, |
240 Post => Is_Empty'Result = (Length (Container) = 0); | 228 Post => Is_Empty'Result = (Length (Container) = 0); |
241 | 229 |
242 procedure Clear (Container : in out Vector) with | 230 procedure Clear (Container : in out Vector) with |
243 Global => null, | 231 Global => null, |
244 Post => Length (Container) = 0; | 232 Post => Length (Container) = 0; |
245 -- Note that this reclaims storage in the unbounded case. You need to call | |
246 -- this before a container goes out of scope in order to avoid storage | |
247 -- leaks. In addition, "X := ..." can leak unless you Clear(X) first. | |
248 | 233 |
249 procedure Assign (Target : in out Vector; Source : Vector) with | 234 procedure Assign (Target : in out Vector; Source : Vector) with |
250 Global => null, | 235 Global => null, |
251 Pre => (if Bounded then Length (Source) <= Target.Capacity), | 236 Pre => Length (Source) <= Target.Capacity, |
252 Post => Model (Target) = Model (Source); | 237 Post => Model (Target) = Model (Source); |
253 | 238 |
254 function Copy | 239 function Copy |
255 (Source : Vector; | 240 (Source : Vector; |
256 Capacity : Capacity_Range := 0) return Vector | 241 Capacity : Capacity_Range := 0) return Vector |
257 with | 242 with |
258 Global => null, | 243 Global => null, |
259 Pre => (if Bounded then (Capacity = 0 or Length (Source) <= Capacity)), | 244 Pre => (Capacity = 0 or Length (Source) <= Capacity), |
260 Post => | 245 Post => |
261 Model (Copy'Result) = Model (Source) | 246 Model (Copy'Result) = Model (Source) |
262 and (if Capacity = 0 then | 247 and (if Capacity = 0 then |
263 Copy'Result.Capacity = Length (Source) | 248 Copy'Result.Capacity = Length (Source) |
264 else | 249 else |
265 Copy'Result.Capacity = Capacity); | 250 Copy'Result.Capacity = Capacity); |
266 | 251 |
267 procedure Move (Target : in out Vector; Source : in out Vector) | 252 procedure Move (Target : in out Vector; Source : in out Vector) |
268 with | 253 with |
269 Global => null, | 254 Global => null, |
270 Pre => (if Bounded then Length (Source) <= Capacity (Target)), | 255 Pre => Length (Source) <= Capacity (Target), |
271 Post => Model (Target) = Model (Source)'Old and Length (Source) = 0; | 256 Post => Model (Target) = Model (Source)'Old and Length (Source) = 0; |
272 | 257 |
273 function Element | 258 function Element |
274 (Container : Vector; | 259 (Container : Vector; |
275 Index : Index_Type) return Element_Type | 260 Index : Index_Type) return Element_Type |
877 (Model (Target), | 862 (Model (Target), |
878 Model (Source)'Old, | 863 Model (Source)'Old, |
879 Model (Target)'Old); | 864 Model (Target)'Old); |
880 end Generic_Sorting; | 865 end Generic_Sorting; |
881 | 866 |
867 --------------------------- | |
868 -- Iteration Primitives -- | |
869 --------------------------- | |
870 | |
871 function Iter_First (Container : Vector) return Extended_Index with | |
872 Global => null; | |
873 | |
874 function Iter_Has_Element | |
875 (Container : Vector; | |
876 Position : Extended_Index) return Boolean | |
877 with | |
878 Global => null, | |
879 Post => | |
880 Iter_Has_Element'Result = | |
881 (Position in Index_Type'First .. Last_Index (Container)); | |
882 pragma Annotate (GNATprove, Inline_For_Proof, Iter_Has_Element); | |
883 | |
884 function Iter_Next | |
885 (Container : Vector; | |
886 Position : Extended_Index) return Extended_Index | |
887 with | |
888 Global => null, | |
889 Pre => Iter_Has_Element (Container, Position); | |
890 | |
882 private | 891 private |
883 pragma SPARK_Mode (Off); | 892 pragma SPARK_Mode (Off); |
884 | 893 |
885 pragma Inline (First_Index); | 894 pragma Inline (First_Index); |
886 pragma Inline (Last_Index); | 895 pragma Inline (Last_Index); |
892 | 901 |
893 subtype Array_Index is Capacity_Range range 1 .. Capacity_Range'Last; | 902 subtype Array_Index is Capacity_Range range 1 .. Capacity_Range'Last; |
894 type Elements_Array is array (Array_Index range <>) of Element_Type; | 903 type Elements_Array is array (Array_Index range <>) of Element_Type; |
895 function "=" (L, R : Elements_Array) return Boolean is abstract; | 904 function "=" (L, R : Elements_Array) return Boolean is abstract; |
896 | 905 |
897 type Elements_Array_Ptr is access all Elements_Array; | 906 type Vector (Capacity : Capacity_Range) is record |
898 | 907 Last : Extended_Index := No_Index; |
899 type Vector (Capacity : Capacity_Range) is limited record | 908 Elements : Elements_Array (1 .. Capacity); |
900 | |
901 -- In the bounded case, the elements are stored in Elements. In the | |
902 -- unbounded case, the elements are initially stored in Elements, until | |
903 -- we run out of room, then we switch to Elements_Ptr. | |
904 | |
905 Last : Extended_Index := No_Index; | |
906 Elements_Ptr : Elements_Array_Ptr := null; | |
907 Elements : aliased Elements_Array (1 .. Capacity); | |
908 end record; | 909 end record; |
909 | |
910 -- The primary reason Vector is limited is that in the unbounded case, once | |
911 -- Elements_Ptr is in use, assignment statements won't work. "X := Y;" will | |
912 -- cause X and Y to share state; that is, X.Elements_Ptr = Y.Elements_Ptr, | |
913 -- so for example "Append (X, ...);" will modify BOTH X and Y. That would | |
914 -- allow SPARK to "prove" things that are false. We could fix that by | |
915 -- making Vector a controlled type, and override Adjust to make a deep | |
916 -- copy, but finalization is not allowed in SPARK. | |
917 -- | |
918 -- Note that (unfortunately) this means that 'Old and 'Loop_Entry are not | |
919 -- allowed on Vectors. | |
920 | 910 |
921 function Empty_Vector return Vector is | 911 function Empty_Vector return Vector is |
922 ((Capacity => 0, others => <>)); | 912 ((Capacity => 0, others => <>)); |
923 | 913 |
914 function Iter_First (Container : Vector) return Extended_Index is | |
915 (Index_Type'First); | |
916 | |
917 function Iter_Next | |
918 (Container : Vector; | |
919 Position : Extended_Index) return Extended_Index | |
920 is | |
921 (if Position = Extended_Index'Last then | |
922 Extended_Index'First | |
923 else | |
924 Extended_Index'Succ (Position)); | |
925 | |
926 function Iter_Has_Element | |
927 (Container : Vector; | |
928 Position : Extended_Index) return Boolean | |
929 is | |
930 (Position in Index_Type'First .. Container.Last); | |
931 | |
924 end Ada.Containers.Formal_Vectors; | 932 end Ada.Containers.Formal_Vectors; |