OSDN Git Service

2011-12-02 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-coinve.adb
index c6f8cb2..dca166f 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 2004-2010, Free Software Foundation, Inc.         --
+--          Copyright (C) 2004-2011, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -29,7 +29,8 @@
 
 with Ada.Containers.Generic_Array_Sort;
 with Ada.Unchecked_Deallocation;
-with System;  use type System.Address;
+
+with System; use type System.Address;
 
 package body Ada.Containers.Indefinite_Vectors is
 
@@ -39,6 +40,26 @@ package body Ada.Containers.Indefinite_Vectors is
    procedure Free is
      new Ada.Unchecked_Deallocation (Element_Type, Element_Access);
 
+   type Iterator is new Limited_Controlled and
+     Vector_Iterator_Interfaces.Reversible_Iterator with
+   record
+      Container : Vector_Access;
+      Index     : Index_Type;
+   end record;
+
+   overriding procedure Finalize (Object : in out Iterator);
+
+   overriding function First (Object : Iterator) return Cursor;
+   overriding function Last  (Object : Iterator) return Cursor;
+
+   overriding function Next
+     (Object   : Iterator;
+      Position : Cursor) return Cursor;
+
+   overriding function Previous
+     (Object   : Iterator;
+      Position : Cursor) return Cursor;
+
    ---------
    -- "&" --
    ---------
@@ -598,6 +619,20 @@ package body Ada.Containers.Indefinite_Vectors is
          Count);
    end Append;
 
+   ------------
+   -- Assign --
+   ------------
+
+   procedure Assign (Target : in out Vector; Source : Vector) is
+   begin
+      if Target'Address = Source'Address then
+         return;
+      end if;
+
+      Target.Clear;
+      Target.Append (Source);
+   end Assign;
+
    --------------
    -- Capacity --
    --------------
@@ -633,6 +668,41 @@ package body Ada.Containers.Indefinite_Vectors is
       end loop;
    end Clear;
 
+   ------------------------
+   -- Constant_Reference --
+   ------------------------
+
+   function Constant_Reference
+     (Container : Vector;
+      Position  : Cursor) return Constant_Reference_Type
+   is
+   begin
+      pragma Unreferenced (Container);
+
+      if Position.Container = null then
+         raise Constraint_Error with "Position cursor has no element";
+      end if;
+
+      if Position.Index > Position.Container.Last then
+         raise Constraint_Error with "Position cursor is out of range";
+      end if;
+
+      return
+       (Element => Position.Container.Elements.EA (Position.Index).all'Access);
+   end Constant_Reference;
+
+   function Constant_Reference
+     (Container : Vector;
+      Position  : Index_Type) return Constant_Reference_Type
+   is
+   begin
+      if (Position) > Container.Last then
+         raise Constraint_Error with "Index is out of range";
+      end if;
+
+      return (Element => Container.Elements.EA (Position).all'Access);
+   end Constant_Reference;
+
    --------------
    -- Contains --
    --------------
@@ -645,6 +715,34 @@ package body Ada.Containers.Indefinite_Vectors is
       return Find_Index (Container, Item) /= No_Index;
    end Contains;
 
+   ----------
+   -- Copy --
+   ----------
+
+   function Copy
+     (Source   : Vector;
+      Capacity : Count_Type := 0) return Vector
+   is
+      C : Count_Type;
+
+   begin
+      if Capacity = 0 then
+         C := Source.Length;
+
+      elsif Capacity >= Source.Length then
+         C := Capacity;
+
+      else
+         raise Capacity_Error
+           with "Requested capacity is less than Source length";
+      end if;
+
+      return Target : Vector do
+         Target.Reserve_Capacity (C);
+         Target.Assign (Source);
+      end return;
+   end Copy;
+
    ------------
    -- Delete --
    ------------
@@ -1010,6 +1108,17 @@ package body Ada.Containers.Indefinite_Vectors is
       end;
    end Finalize;
 
+   procedure Finalize (Object : in out Iterator) is
+   begin
+      if Object.Container /= null then
+         declare
+            B : Natural renames Object.Container.all.Busy;
+         begin
+            B := B - 1;
+         end;
+      end if;
+   end Finalize;
+
    ----------
    -- Find --
    ----------
@@ -1034,7 +1143,7 @@ package body Ada.Containers.Indefinite_Vectors is
          if Container.Elements.EA (J) /= null
            and then Container.Elements.EA (J).all = Item
          then
-            return (Container'Unchecked_Access, J);
+            return (Container'Unrestricted_Access, J);
          end if;
       end loop;
 
@@ -1072,7 +1181,13 @@ package body Ada.Containers.Indefinite_Vectors is
          return No_Element;
       end if;
 
-      return (Container'Unchecked_Access, Index_Type'First);
+      return (Container'Unrestricted_Access, Index_Type'First);
+   end First;
+
+   function First (Object : Iterator) return Cursor is
+      C : constant Cursor := (Object.Container, Index_Type'First);
+   begin
+      return C;
    end First;
 
    -------------------
@@ -1167,16 +1282,26 @@ package body Ada.Containers.Indefinite_Vectors is
          I, J : Index_Type'Base;
 
       begin
-         if Target.Last < Index_Type'First then
-            Move (Target => Target, Source => Source);
+
+         --  The semantics of Merge changed slightly per AI05-0021. It was
+         --  originally the case that if Target and Source denoted the same
+         --  container object, then the GNAT implementation of Merge did
+         --  nothing. However, it was argued that RM05 did not precisely
+         --  specify the semantics for this corner case. The decision of the
+         --  ARG was that if Target and Source denote the same non-empty
+         --  container object, then Program_Error is raised.
+
+         if Source.Last < Index_Type'First then  -- Source is empty
             return;
          end if;
 
          if Target'Address = Source'Address then
-            return;
+            raise Program_Error with
+              "Target and Source denote same non-empty container";
          end if;
 
-         if Source.Last < Index_Type'First then
+         if Target.Last < Index_Type'First then  -- Target is empty
+            Move (Target => Target, Source => Source);
             return;
          end if;
 
@@ -1341,8 +1466,8 @@ package body Ada.Containers.Indefinite_Vectors is
 
       --  There are two constraints we need to satisfy. The first constraint is
       --  that a container cannot have more than Count_Type'Last elements, so
-      --  we must check the sum of the current length and the insertion
-      --  count. Note that we cannot simply add these values, because of the
+      --  we must check the sum of the current length and the insertion count.
+      --  Note that we cannot simply add these values, because of the
       --  possibility of overflow.
 
       if Old_Length > Count_Type'Last - Count then
@@ -1361,10 +1486,12 @@ package body Ada.Containers.Indefinite_Vectors is
       --  acceptable, then we compute the new last index from that.
 
       if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
+
          --  We have to handle the case when there might be more values in the
          --  range of Index_Type than in the range of Count_Type.
 
          if Index_Type'First <= 0 then
+
             --  We know that No_Index (the same as Index_Type'First - 1) is
             --  less than 0, so it is safe to compute the following sum without
             --  fear of overflow.
@@ -1372,6 +1499,7 @@ package body Ada.Containers.Indefinite_Vectors is
             Index := No_Index + Index_Type'Base (Count_Type'Last);
 
             if Index <= Index_Type'Last then
+
                --  We have determined that range of Index_Type has at least as
                --  many values as in Count_Type, so Count_Type'Last is the
                --  maximum number of items that are allowed.
@@ -1396,6 +1524,7 @@ package body Ada.Containers.Indefinite_Vectors is
          end if;
 
       elsif Index_Type'First <= 0 then
+
          --  We know that No_Index (the same as Index_Type'First - 1) is less
          --  than 0, so it is safe to compute the following sum without fear of
          --  overflow.
@@ -1403,6 +1532,7 @@ package body Ada.Containers.Indefinite_Vectors is
          J := Count_Type'Base (No_Index) + Count_Type'Last;
 
          if J <= Count_Type'Base (Index_Type'Last) then
+
             --  We have determined that range of Index_Type has at least as
             --  many values as in Count_Type, so Count_Type'Last is the maximum
             --  number of items that are allowed.
@@ -1467,6 +1597,7 @@ package body Ada.Containers.Indefinite_Vectors is
          --  allocate the elements.
 
          for Idx in Container.Elements.EA'Range loop
+
             --  In order to preserve container invariants, we always attempt
             --  the element allocation first, before setting the Last index
             --  value, in case the allocation fails (either because there is no
@@ -1495,6 +1626,7 @@ package body Ada.Containers.Indefinite_Vectors is
       end if;
 
       if New_Length <= Container.Elements.EA'Length then
+
          --  In this case, we're inserting elements into a vector that has
          --  already allocated an internal array, and the existing array has
          --  enough unused storage for the new items.
@@ -1505,10 +1637,12 @@ package body Ada.Containers.Indefinite_Vectors is
 
          begin
             if Before > Container.Last then
+
                --  The new items are being appended to the vector, so no
                --  sliding of existing elements is required.
 
                for Idx in Before .. New_Last loop
+
                   --  In order to preserve container invariants, we always
                   --  attempt the element allocation first, before setting the
                   --  Last index value, in case the allocation fails (either
@@ -1532,7 +1666,6 @@ package body Ada.Containers.Indefinite_Vectors is
 
                if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
                   Index := Before + Index_Type'Base (Count);
-
                else
                   Index := Index_Type'Base (Count_Type'Base (Before) + Count);
                end if;
@@ -1598,6 +1731,7 @@ package body Ada.Containers.Indefinite_Vectors is
       end loop;
 
       if New_Capacity > Max_Length then
+
          --  We have reached the limit of capacity, so no further expansion
          --  will occur. (This is not a problem, as there is never a need to
          --  have more capacity than the maximum container length.)
@@ -1635,6 +1769,7 @@ package body Ada.Containers.Indefinite_Vectors is
            Src.EA (Index_Type'First .. Before - 1);
 
          if Before > Container.Last then
+
             --  The new items are being appended to the vector, so no
             --  sliding of existing elements is required.
 
@@ -1648,6 +1783,7 @@ package body Ada.Containers.Indefinite_Vectors is
             --  Now we append the new items.
 
             for Idx in Before .. New_Last loop
+
                --  In order to preserve container invariants, we always
                --  attempt the element allocation first, before setting the
                --  Last index value, in case the allocation fails (either
@@ -1688,6 +1824,7 @@ package body Ada.Containers.Indefinite_Vectors is
             --  items.
 
             for Idx in Before .. Index - 1 loop
+
                --  Note that container invariants have already been satisfied
                --  (in particular, the Last index value of the vector has
                --  already been updated), so if this allocation fails we simply
@@ -1714,6 +1851,7 @@ package body Ada.Containers.Indefinite_Vectors is
       Insert_Space (Container, Before, Count => N);
 
       if N = 0 then
+
          --  There's nothing else to do here (vetting of parameters was
          --  performed already in Insert_Space), so we simply return.
 
@@ -1721,6 +1859,7 @@ package body Ada.Containers.Indefinite_Vectors is
       end if;
 
       if Container'Address /= New_Item'Address then
+
          --  This is the simple case.  New_Item denotes an object different
          --  from Container, so there's nothing special we need to do to copy
          --  the source items to their destination, because all of the source
@@ -1788,6 +1927,7 @@ package body Ada.Containers.Indefinite_Vectors is
          end loop;
 
          if Src'Length = N then
+
             --  The new items were effectively appended to the container, so we
             --  have already copied all of the items that need to be copied.
             --  We return early here, even though the source slice below is
@@ -1800,12 +1940,11 @@ package body Ada.Containers.Indefinite_Vectors is
       end;
 
       --  Index value J is the first index of the second source slice. (It is
-      --  also 1 greater than the last index of the destination slice.) Note
-      --  that we want to avoid computing J, if J is greater than
-      --  Index_Type'Base'Last, in order to avoid overflow. We prevent that by
-      --  returning early above, immediately after copying the first slice of
-      --  the source, and determining that this second slice of the source is
-      --  empty.
+      --  also 1 greater than the last index of the destination slice.) Note:
+      --  avoid computing J if J is greater than Index_Type'Base'Last, in order
+      --  to avoid overflow. Prevent that by returning early above, immediately
+      --  after copying the first slice of the source, and determining that
+      --  this second slice of the source is empty.
 
       if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
          J := Before + Index_Type'Base (N);
@@ -1826,11 +1965,10 @@ package body Ada.Containers.Indefinite_Vectors is
          Dst_Index : Index_Type'Base;
 
       begin
-         --  We next copy the source items that follow the space we
-         --  inserted. Index value Dst_Index is the first index of that portion
-         --  of the destination that receives this slice of the source. (For
-         --  the reasons given above, this slice is guaranteed to be
-         --  non-empty.)
+         --  We next copy the source items that follow the space we inserted.
+         --  Index value Dst_Index is the first index of that portion of the
+         --  destination that receives this slice of the source. (For the
+         --  reasons given above, this slice is guaranteed to be non-empty.)
 
          if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
             Dst_Index := J - Index_Type'Base (Src'Length);
@@ -1858,7 +1996,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
    begin
       if Before.Container /= null
-        and then Before.Container /= Container'Unchecked_Access
+        and then Before.Container /= Container'Unrestricted_Access
       then
          raise Program_Error with "Before cursor denotes wrong container";
       end if;
@@ -1894,7 +2032,8 @@ package body Ada.Containers.Indefinite_Vectors is
 
    begin
       if Before.Container /= null
-        and then Before.Container /= Vector_Access'(Container'Unchecked_Access)
+        and then Before.Container /=
+                   Vector_Access'(Container'Unrestricted_Access)
       then
          raise Program_Error with "Before cursor denotes wrong container";
       end if;
@@ -1905,7 +2044,7 @@ package body Ada.Containers.Indefinite_Vectors is
          then
             Position := No_Element;
          else
-            Position := (Container'Unchecked_Access, Before.Index);
+            Position := (Container'Unrestricted_Access, Before.Index);
          end if;
 
          return;
@@ -1927,7 +2066,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
       Insert (Container, Index, New_Item);
 
-      Position := Cursor'(Container'Unchecked_Access, Index);
+      Position := Cursor'(Container'Unrestricted_Access, Index);
    end Insert;
 
    procedure Insert
@@ -1940,7 +2079,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
    begin
       if Before.Container /= null
-        and then Before.Container /= Container'Unchecked_Access
+        and then Before.Container /= Container'Unrestricted_Access
       then
          raise Program_Error with "Before cursor denotes wrong container";
       end if;
@@ -1977,7 +2116,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
    begin
       if Before.Container /= null
-        and then Before.Container /= Container'Unchecked_Access
+        and then Before.Container /= Container'Unrestricted_Access
       then
          raise Program_Error with "Before cursor denotes wrong container";
       end if;
@@ -1988,7 +2127,7 @@ package body Ada.Containers.Indefinite_Vectors is
          then
             Position := No_Element;
          else
-            Position := (Container'Unchecked_Access, Before.Index);
+            Position := (Container'Unrestricted_Access, Before.Index);
          end if;
 
          return;
@@ -2010,7 +2149,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
       Insert (Container, Index, New_Item, Count);
 
-      Position := (Container'Unchecked_Access, Index);
+      Position := (Container'Unrestricted_Access, Index);
    end Insert;
 
    ------------------
@@ -2098,6 +2237,7 @@ package body Ada.Containers.Indefinite_Vectors is
          --  range of Index_Type than in the range of Count_Type.
 
          if Index_Type'First <= 0 then
+
             --  We know that No_Index (the same as Index_Type'First - 1) is
             --  less than 0, so it is safe to compute the following sum without
             --  fear of overflow.
@@ -2105,6 +2245,7 @@ package body Ada.Containers.Indefinite_Vectors is
             Index := No_Index + Index_Type'Base (Count_Type'Last);
 
             if Index <= Index_Type'Last then
+
                --  We have determined that range of Index_Type has at least as
                --  many values as in Count_Type, so Count_Type'Last is the
                --  maximum number of items that are allowed.
@@ -2129,6 +2270,7 @@ package body Ada.Containers.Indefinite_Vectors is
          end if;
 
       elsif Index_Type'First <= 0 then
+
          --  We know that No_Index (the same as Index_Type'First - 1) is less
          --  than 0, so it is safe to compute the following sum without fear of
          --  overflow.
@@ -2136,6 +2278,7 @@ package body Ada.Containers.Indefinite_Vectors is
          J := Count_Type'Base (No_Index) + Count_Type'Last;
 
          if J <= Count_Type'Base (Index_Type'Last) then
+
             --  We have determined that range of Index_Type has at least as
             --  many values as in Count_Type, so Count_Type'Last is the maximum
             --  number of items that are allowed.
@@ -2192,9 +2335,9 @@ package body Ada.Containers.Indefinite_Vectors is
 
          --  In an indefinite vector, elements are allocated individually, and
          --  stored as access values on the internal array (the length of which
-         --  represents the vector "capacity"), which is separately
-         --  allocated. We have no elements here (because we're inserting
-         --  "space"), so all we need to do is allocate the backbone.
+         --  represents the vector "capacity"), which is separately allocated.
+         --  We have no elements here (because we're inserting "space"), so all
+         --  we need to do is allocate the backbone.
 
          Container.Elements := new Elements_Type (New_Last);
          Container.Last := New_Last;
@@ -2204,9 +2347,9 @@ package body Ada.Containers.Indefinite_Vectors is
 
       --  The tampering bits exist to prevent an item from being harmfully
       --  manipulated while it is being visited. Query, Update, and Iterate
-      --  increment the busy count on entry, and decrement the count on
-      --  exit. Insert checks the count to determine whether it is being called
-      --  while the associated callback procedure is executing.
+      --  increment the busy count on entry, and decrement the count on exit.
+      --  Insert checks the count to determine whether it is being called while
+      --  the associated callback procedure is executing.
 
       if Container.Busy > 0 then
          raise Program_Error with
@@ -2223,6 +2366,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
          begin
             if Before <= Container.Last then
+
                --  The new space is being inserted before some existing
                --  elements, so we must slide the existing elements up to their
                --  new home. We use the wider of Index_Type'Base and
@@ -2264,6 +2408,7 @@ package body Ada.Containers.Indefinite_Vectors is
       end loop;
 
       if New_Capacity > Max_Length then
+
          --  We have reached the limit of capacity, so no further expansion
          --  will occur. (This is not a problem, as there is never a need to
          --  have more capacity than the maximum container length.)
@@ -2301,6 +2446,7 @@ package body Ada.Containers.Indefinite_Vectors is
            Src.EA (Index_Type'First .. Before - 1);
 
          if Before <= Container.Last then
+
             --  The new items are being inserted before some existing elements,
             --  so we must slide the existing elements up to their new home.
 
@@ -2334,7 +2480,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
    begin
       if Before.Container /= null
-        and then Before.Container /= Container'Unchecked_Access
+        and then Before.Container /= Container'Unrestricted_Access
       then
          raise Program_Error with "Before cursor denotes wrong container";
       end if;
@@ -2345,7 +2491,7 @@ package body Ada.Containers.Indefinite_Vectors is
          then
             Position := No_Element;
          else
-            Position := (Container'Unchecked_Access, Before.Index);
+            Position := (Container'Unrestricted_Access, Before.Index);
          end if;
 
          return;
@@ -2367,7 +2513,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
       Insert_Space (Container, Index, Count);
 
-      Position := Cursor'(Container'Unchecked_Access, Index);
+      Position := Cursor'(Container'Unrestricted_Access, Index);
    end Insert_Space;
 
    --------------
@@ -2387,15 +2533,14 @@ package body Ada.Containers.Indefinite_Vectors is
      (Container : Vector;
       Process   : not null access procedure (Position : Cursor))
    is
-      V : Vector renames Container'Unrestricted_Access.all;
-      B : Natural renames V.Busy;
+      B : Natural renames Container'Unrestricted_Access.all.Busy;
 
    begin
       B := B + 1;
 
       begin
          for Indx in Index_Type'First .. Container.Last loop
-            Process (Cursor'(Container'Unchecked_Access, Indx));
+            Process (Cursor'(Container'Unrestricted_Access, Indx));
          end loop;
       exception
          when others =>
@@ -2406,6 +2551,38 @@ package body Ada.Containers.Indefinite_Vectors is
       B := B - 1;
    end Iterate;
 
+   function Iterate (Container : Vector)
+      return Vector_Iterator_Interfaces.Reversible_Iterator'class
+   is
+      B  : Natural renames Container'Unrestricted_Access.all.Busy;
+
+   begin
+      return It : constant Iterator :=
+                    (Limited_Controlled with
+                       Container => Container'Unrestricted_Access,
+                       Index     => Index_Type'First)
+      do
+         B := B + 1;
+      end return;
+   end Iterate;
+
+   function Iterate
+     (Container : Vector;
+      Start     : Cursor)
+      return Vector_Iterator_Interfaces.Reversible_Iterator'class
+   is
+      B  : Natural renames Container'Unrestricted_Access.all.Busy;
+
+   begin
+      return It : constant Iterator :=
+                    (Limited_Controlled with
+                       Container => Container'Unrestricted_Access,
+                       Index     => Start.Index)
+      do
+         B := B + 1;
+      end return;
+   end Iterate;
+
    ----------
    -- Last --
    ----------
@@ -2416,7 +2593,13 @@ package body Ada.Containers.Indefinite_Vectors is
          return No_Element;
       end if;
 
-      return (Container'Unchecked_Access, Container.Last);
+      return (Container'Unrestricted_Access, Container.Last);
+   end Last;
+
+   function Last (Object : Iterator) return Cursor is
+      C : constant Cursor := (Object.Container, Object.Container.Last);
+   begin
+      return C;
    end Last;
 
    -----------------
@@ -2533,9 +2716,14 @@ package body Ada.Containers.Indefinite_Vectors is
       return No_Element;
    end Next;
 
-   ----------
-   -- Next --
-   ----------
+   function Next (Object : Iterator; Position : Cursor) return Cursor is
+   begin
+      if Position.Index = Object.Container.Last then
+         return  No_Element;
+      else
+         return (Object.Container, Position.Index + 1);
+      end if;
+   end Next;
 
    procedure Next (Position : in out Cursor) is
    begin
@@ -2601,6 +2789,15 @@ package body Ada.Containers.Indefinite_Vectors is
       return No_Element;
    end Previous;
 
+   function Previous (Object : Iterator; Position : Cursor) return Cursor is
+   begin
+      if Position.Index > Index_Type'First then
+         return (Object.Container, Position.Index - 1);
+      else
+         return No_Element;
+      end if;
+   end Previous;
+
    -------------------
    -- Query_Element --
    -------------------
@@ -2695,6 +2892,58 @@ package body Ada.Containers.Indefinite_Vectors is
       raise Program_Error with "attempt to stream vector cursor";
    end Read;
 
+   procedure Read
+     (Stream : not null access Root_Stream_Type'Class;
+      Item   : out Reference_Type)
+   is
+   begin
+      raise Program_Error with "attempt to stream reference";
+   end Read;
+
+   procedure Read
+     (Stream : not null access Root_Stream_Type'Class;
+      Item   : out Constant_Reference_Type)
+   is
+   begin
+      raise Program_Error with "attempt to stream reference";
+   end Read;
+
+   ---------------
+   -- Reference --
+   ---------------
+
+   function Reference
+     (Container : Vector;
+      Position  : Cursor) return Reference_Type
+   is
+   begin
+      pragma Unreferenced (Container);
+
+      if Position.Container = null then
+         raise Constraint_Error with "Position cursor has no element";
+      end if;
+
+      if Position.Index > Position.Container.Last then
+         raise Constraint_Error with "Position cursor is out of range";
+      end if;
+
+      return
+        (Element =>
+           Position.Container.Elements.EA (Position.Index).all'Access);
+   end Reference;
+
+   function Reference
+     (Container : Vector;
+      Position  : Index_Type) return Reference_Type
+   is
+   begin
+      if Position > Container.Last then
+         raise Constraint_Error with "Index is out of range";
+      end if;
+
+      return (Element => Container.Elements.EA (Position).all'Access);
+   end Reference;
+
    ---------------------
    -- Replace_Element --
    ---------------------
@@ -2776,10 +3025,12 @@ package body Ada.Containers.Indefinite_Vectors is
       --  container length.
 
       if Capacity = 0 then
+
          --  This is a request to trim back storage, to the minimum amount
          --  possible given the current state of the container.
 
          if N = 0 then
+
             --  The container is empty, so in this unique case we can
             --  deallocate the entire internal array. Note that an empty
             --  container can never be busy, so there's no need to check the
@@ -2787,6 +3038,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
             declare
                X : Elements_Access := Container.Elements;
+
             begin
                --  First we remove the internal array from the container, to
                --  handle the case when the deallocation raises an exception
@@ -2802,6 +3054,7 @@ package body Ada.Containers.Indefinite_Vectors is
             end;
 
          elsif N < Container.Elements.EA'Length then
+
             --  The container is not empty, and the current length is less than
             --  the current capacity, so there's storage available to trim. In
             --  this case, we allocate a new internal array having a length
@@ -2854,6 +3107,7 @@ package body Ada.Containers.Indefinite_Vectors is
       --  any possibility of overflow.
 
       if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
+
          --  We perform a two-part test. First we determine whether the
          --  computed Last value lies in the base range of the type, and then
          --  determine whether it lies in the range of the index (sub)type.
@@ -2882,6 +3136,7 @@ package body Ada.Containers.Indefinite_Vectors is
          end if;
 
       elsif Index_Type'First <= 0 then
+
          --  Here we can compute Last directly, in the normal way. We know that
          --  No_Index is less than 0, so there is no danger of overflow when
          --  adding the (positive) value of Capacity.
@@ -2920,6 +3175,7 @@ package body Ada.Containers.Indefinite_Vectors is
       --  this is a request for expansion or contraction of storage.
 
       if Container.Elements = null then
+
          --  The container is empty (it doesn't even have an internal array),
          --  so this represents a request to allocate storage having the given
          --  capacity.
@@ -2929,17 +3185,19 @@ package body Ada.Containers.Indefinite_Vectors is
       end if;
 
       if Capacity <= N then
+
          --  This is a request to trim back storage, but only to the limit of
          --  what's already in the container. (Reserve_Capacity never deletes
          --  active elements, it only reclaims excess storage.)
 
          if N < Container.Elements.EA'Length then
+
             --  The container is not empty (because the requested capacity is
             --  positive, and less than or equal to the container length), and
-            --  the current length is less than the current capacity, so
-            --  there's storage available to trim. In this case, we allocate a
-            --  new internal array having a length that exactly matches the
-            --  number of items in the container.
+            --  the current length is less than the current capacity, so there
+            --  is storage available to trim. In this case, we allocate a new
+            --  internal array having a length that exactly matches the number
+            --  of items in the container.
 
             if Container.Busy > 0 then
                raise Program_Error with
@@ -2982,6 +3240,7 @@ package body Ada.Containers.Indefinite_Vectors is
       --  current capacity is.
 
       if Capacity = Container.Elements.EA'Length then
+
          --  The requested capacity matches the existing capacity, so there's
          --  nothing to do here. We treat this case as a no-op, and simply
          --  return without checking the busy bit.
@@ -3081,7 +3340,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
    begin
       if Position.Container /= null
-        and then Position.Container /= Container'Unchecked_Access
+        and then Position.Container /= Container'Unrestricted_Access
       then
          raise Program_Error with "Position cursor denotes wrong container";
       end if;
@@ -3098,7 +3357,7 @@ package body Ada.Containers.Indefinite_Vectors is
          if Container.Elements.EA (Indx) /= null
            and then Container.Elements.EA (Indx).all = Item
          then
-            return (Container'Unchecked_Access, Indx);
+            return (Container'Unrestricted_Access, Indx);
          end if;
       end loop;
 
@@ -3144,7 +3403,7 @@ package body Ada.Containers.Indefinite_Vectors is
 
       begin
          for Indx in reverse Index_Type'First .. Container.Last loop
-            Process (Cursor'(Container'Unchecked_Access, Indx));
+            Process (Cursor'(Container'Unrestricted_Access, Indx));
          end loop;
       exception
          when others =>
@@ -3259,7 +3518,7 @@ package body Ada.Containers.Indefinite_Vectors is
          return No_Element;
       end if;
 
-      return Cursor'(Container'Unchecked_Access, Index);
+      return Cursor'(Container'Unrestricted_Access, Index);
    end To_Cursor;
 
    --------------
@@ -3301,6 +3560,7 @@ package body Ada.Containers.Indefinite_Vectors is
       --  create a Last index value greater than Index_Type'Last.
 
       if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
+
          --  We perform a two-part test. First we determine whether the
          --  computed Last value lies in the base range of the type, and then
          --  determine whether it lies in the range of the index (sub)type.
@@ -3329,6 +3589,7 @@ package body Ada.Containers.Indefinite_Vectors is
          end if;
 
       elsif Index_Type'First <= 0 then
+
          --  Here we can compute Last directly, in the normal way. We know that
          --  No_Index is less than 0, so there is no danger of overflow when
          --  adding the (positive) value of Length.
@@ -3389,6 +3650,7 @@ package body Ada.Containers.Indefinite_Vectors is
       --  create a Last index value greater than Index_Type'Last.
 
       if Index_Type'Base'Last >= Count_Type'Pos (Count_Type'Last) then
+
          --  We perform a two-part test. First we determine whether the
          --  computed Last value lies in the base range of the type, and then
          --  determine whether it lies in the range of the index (sub)type.
@@ -3417,6 +3679,7 @@ package body Ada.Containers.Indefinite_Vectors is
          end if;
 
       elsif Index_Type'First <= 0 then
+
          --  Here we can compute Last directly, in the normal way. We know that
          --  No_Index is less than 0, so there is no danger of overflow when
          --  adding the (positive) value of Length.
@@ -3463,6 +3726,7 @@ package body Ada.Containers.Indefinite_Vectors is
       --  initialized when the handler executes. So here we initialize our loop
       --  variable earlier than we prefer, before entering the block, so there
       --  is no ambiguity.
+
       Last := Index_Type'First;
 
       begin
@@ -3579,4 +3843,20 @@ package body Ada.Containers.Indefinite_Vectors is
       raise Program_Error with "attempt to stream vector cursor";
    end Write;
 
+   procedure Write
+     (Stream : not null access Root_Stream_Type'Class;
+      Item   : Reference_Type)
+   is
+   begin
+      raise Program_Error with "attempt to stream reference";
+   end Write;
+
+   procedure Write
+     (Stream : not null access Root_Stream_Type'Class;
+      Item   : Constant_Reference_Type)
+   is
+   begin
+      raise Program_Error with "attempt to stream reference";
+   end Write;
+
 end Ada.Containers.Indefinite_Vectors;