]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Add Ada 2022 features to sets containers
authorBob Duff <duff@adacore.com>
Mon, 23 May 2022 13:47:18 +0000 (09:47 -0400)
committerPierre-Marie de Rodat <derodat@adacore.com>
Mon, 4 Jul 2022 07:45:53 +0000 (07:45 +0000)
This patch adds some Ada 2022 features to the set children of
Ada.Containers.

gcc/ada/

* libgnat/a-cbhase.adb, libgnat/a-cbhase.ads,
libgnat/a-cborse.adb, libgnat/a-cborse.ads,
libgnat/a-cihase.adb, libgnat/a-cihase.ads,
libgnat/a-ciorse.adb, libgnat/a-ciorse.ads,
libgnat/a-cohase.adb, libgnat/a-cohase.ads,
libgnat/a-conhel.adb, libgnat/a-conhel.ads,
libgnat/a-coorse.adb, libgnat/a-coorse.ads: Add Has_Element,
Element, Query_Element, and Next subprograms that take a Set
parameter. Add Tampering_With_Cursors_Prohibited function. These
are all new in Ada 2022.

14 files changed:
gcc/ada/libgnat/a-cbhase.adb
gcc/ada/libgnat/a-cbhase.ads
gcc/ada/libgnat/a-cborse.adb
gcc/ada/libgnat/a-cborse.ads
gcc/ada/libgnat/a-cihase.adb
gcc/ada/libgnat/a-cihase.ads
gcc/ada/libgnat/a-ciorse.adb
gcc/ada/libgnat/a-ciorse.ads
gcc/ada/libgnat/a-cohase.adb
gcc/ada/libgnat/a-cohase.ads
gcc/ada/libgnat/a-conhel.adb
gcc/ada/libgnat/a-conhel.ads
gcc/ada/libgnat/a-coorse.adb
gcc/ada/libgnat/a-coorse.ads

index 9076d8e813201c394320cf01d00a0a4a64c60eae..b83ab8038f8dde2c9f2bf915e42f97425567ffe2 100644 (file)
@@ -1599,6 +1599,64 @@ is
       raise Program_Error with "attempt to stream reference";
    end Write;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean is
+   begin
+      pragma Assert (Vet (Position), "bad cursor in Has_Element");
+      pragma Assert ((Position.Container = null) = (Position.Node = 0),
+                     "bad nullity in Has_Element");
+      return Position.Container = Container'Unrestricted_Access;
+   end Has_Element;
+
+   function Tampering_With_Cursors_Prohibited
+     (Container : Set) return Boolean
+   is
+   begin
+      return Is_Busy (Container.TC);
+   end Tampering_With_Cursors_Prohibited;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Element (Position);
+   end Element;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type)) is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      Query_Element (Position, Process);
+   end Query_Element;
+
+   function Next (Container : Set; Position : Cursor) return Cursor is
+   begin
+      if Checks and then
+        not (Position = No_Element or else Has_Element (Container, Position))
+      then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Next (Position);
+   end Next;
+
+   procedure Next (Container : Set; Position : in out Cursor) is
+   begin
+      Position := Next (Container, Position);
+   end Next;
+
+   ------------------
+   -- Generic_Keys --
+   ------------------
+
    package body Generic_Keys is
 
       -----------------------
index c30a3644d1b70412dbe86346caf43c2d5eacc3cb..7079c51741824f3cd82207e39fd4ca917787d839 100644 (file)
@@ -369,6 +369,25 @@ is
      (Container : Set)
       return Set_Iterator_Interfaces.Forward_Iterator'Class;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean;
+
+   function Tampering_With_Cursors_Prohibited (Container : Set) return Boolean;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type));
+
+   function Next (Container : Set; Position : Cursor) return Cursor;
+
+   procedure Next (Container : Set; Position : in out Cursor);
+
+   ----------------
+
    generic
       type Key_Type (<>) is private;
 
index 55eca4077b5612cf362455edf112bfe276caab74..bc52b4553a5fea6d0860ea789b1f76e9a21f7894 100644 (file)
@@ -688,6 +688,62 @@ is
               else Cursor'(Container'Unrestricted_Access, Node));
    end Floor;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean is
+   begin
+      pragma Assert
+        (Position.Container = null or else Vet (Container, Position.Node),
+         "bad cursor in Has_Element");
+      pragma Assert ((Position.Container = null) = (Position.Node = 0),
+                     "bad nullity in Has_Element");
+      return Position.Container = Container'Unrestricted_Access;
+   end Has_Element;
+
+   function Tampering_With_Cursors_Prohibited
+     (Container : Set) return Boolean
+   is
+   begin
+      return Is_Busy (Container.TC);
+   end Tampering_With_Cursors_Prohibited;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Element (Position);
+   end Element;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type)) is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      Query_Element (Position, Process);
+   end Query_Element;
+
+   function Next (Container : Set; Position : Cursor) return Cursor is
+   begin
+      if Checks and then
+        not (Position = No_Element or else Has_Element (Container, Position))
+      then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Next (Position);
+   end Next;
+
+   procedure Next (Container : Set; Position : in out Cursor) is
+   begin
+      Position := Next (Container, Position);
+   end Next;
+
    ------------------
    -- Generic_Keys --
    ------------------
index ceaf8857e43e9528cb06a283e4c5f1710402c17f..be22c250714b3dbf1675683a77633053d08d0584 100644 (file)
@@ -230,6 +230,25 @@ is
       Start     : Cursor)
       return Set_Iterator_Interfaces.Reversible_Iterator'class;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean;
+
+   function Tampering_With_Cursors_Prohibited (Container : Set) return Boolean;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type));
+
+   function Next (Container : Set; Position : Cursor) return Cursor;
+
+   procedure Next (Container : Set; Position : in out Cursor);
+
+   ----------------
+
    generic
       type Key_Type (<>) is private;
 
index 090d01cb4724c4e202083cad91687985695909a6..0a9aabdeaa69ab382dcb296267f387726b08bd9d 100644 (file)
@@ -2031,6 +2031,64 @@ is
       Element_Type'Output (Stream, Node.Element.all);
    end Write_Node;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean is
+   begin
+      pragma Assert (Vet (Position), "bad cursor in Has_Element");
+      pragma Assert ((Position.Container = null) = (Position.Node = null),
+                     "bad nullity in Has_Element");
+      return Position.Container = Container'Unrestricted_Access;
+   end Has_Element;
+
+   function Tampering_With_Cursors_Prohibited
+     (Container : Set) return Boolean
+   is
+   begin
+      return Is_Busy (Container.HT.TC);
+   end Tampering_With_Cursors_Prohibited;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Element (Position);
+   end Element;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type)) is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      Query_Element (Position, Process);
+   end Query_Element;
+
+   function Next (Container : Set; Position : Cursor) return Cursor is
+   begin
+      if Checks and then
+        not (Position = No_Element or else Has_Element (Container, Position))
+      then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Next (Position);
+   end Next;
+
+   procedure Next (Container : Set; Position : in out Cursor) is
+   begin
+      Position := Next (Container, Position);
+   end Next;
+
+   ------------------
+   -- Generic_Keys --
+   ------------------
+
    package body Generic_Keys is
 
       -----------------------
index cff713dc9c24718a7c725c5f05fab83301a36cb1..dcd1d6a86fa11efc21e47a17611492e09ee69f26 100644 (file)
@@ -355,6 +355,25 @@ is
    function Iterate (Container : Set)
      return Set_Iterator_Interfaces.Forward_Iterator'Class;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean;
+
+   function Tampering_With_Cursors_Prohibited (Container : Set) return Boolean;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type));
+
+   function Next (Container : Set; Position : Cursor) return Cursor;
+
+   procedure Next (Container : Set; Position : in out Cursor);
+
+   ----------------
+
    generic
       type Key_Type (<>) is private;
 
index b23b252057b20ba99335777c260be6ad90ef6021..d5502eaea464e64556f6005ce23112d99f313bf9 100644 (file)
@@ -721,6 +721,61 @@ is
       Deallocate (X);
    end Free;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean is
+   begin
+      pragma Assert
+        (Vet (Container.Tree, Position.Node), "bad cursor in Has_Element");
+      pragma Assert ((Position.Container = null) = (Position.Node = null),
+                     "bad nullity in Has_Element");
+      return Position.Container = Container'Unrestricted_Access;
+   end Has_Element;
+
+   function Tampering_With_Cursors_Prohibited
+     (Container : Set) return Boolean
+   is
+   begin
+      return Is_Busy (Container.Tree.TC);
+   end Tampering_With_Cursors_Prohibited;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Element (Position);
+   end Element;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type)) is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      Query_Element (Position, Process);
+   end Query_Element;
+
+   function Next (Container : Set; Position : Cursor) return Cursor is
+   begin
+      if Checks and then
+        not (Position = No_Element or else Has_Element (Container, Position))
+      then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Next (Position);
+   end Next;
+
+   procedure Next (Container : Set; Position : in out Cursor) is
+   begin
+      Position := Next (Container, Position);
+   end Next;
+
    ------------------
    -- Generic_Keys --
    ------------------
index 13272e2337b494c9141f36067f74ee315e86abc4..d053ac72649ed53bb58568755179d7b7506db59f 100644 (file)
@@ -238,6 +238,25 @@ is
       Start     : Cursor)
       return Set_Iterator_Interfaces.Reversible_Iterator'class;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean;
+
+   function Tampering_With_Cursors_Prohibited (Container : Set) return Boolean;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type));
+
+   function Next (Container : Set; Position : Cursor) return Cursor;
+
+   procedure Next (Container : Set; Position : in out Cursor);
+
+   ----------------
+
    generic
       type Key_Type (<>) is private;
 
index 986b354ad7266d3e83c206e10f39591200f15bd7..4656868d59c0b60c52d380882de271a3f0aea280 100644 (file)
@@ -1844,6 +1844,64 @@ is
       Element_Type'Write (Stream, Node.Element);
    end Write_Node;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean is
+   begin
+      pragma Assert (Vet (Position), "bad cursor in Has_Element");
+      pragma Assert ((Position.Container = null) = (Position.Node = null),
+                     "bad nullity in Has_Element");
+      return Position.Container = Container'Unrestricted_Access;
+   end Has_Element;
+
+   function Tampering_With_Cursors_Prohibited
+     (Container : Set) return Boolean
+   is
+   begin
+      return Is_Busy (Container.HT.TC);
+   end Tampering_With_Cursors_Prohibited;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Element (Position);
+   end Element;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type)) is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      Query_Element (Position, Process);
+   end Query_Element;
+
+   function Next (Container : Set; Position : Cursor) return Cursor is
+   begin
+      if Checks and then
+        not (Position = No_Element or else Has_Element (Container, Position))
+      then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Next (Position);
+   end Next;
+
+   procedure Next (Container : Set; Position : in out Cursor) is
+   begin
+      Position := Next (Container, Position);
+   end Next;
+
+   ------------------
+   -- Generic_Keys --
+   ------------------
+
    package body Generic_Keys is
 
       -----------------------
index ada212cda8dafc2ce46c442e10927f4bc628d239..9f562d899b9e3a580b9233ee1719f83771e2a520 100644 (file)
@@ -367,6 +367,25 @@ is
    function Iterate
      (Container : Set) return Set_Iterator_Interfaces.Forward_Iterator'Class;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean;
+
+   function Tampering_With_Cursors_Prohibited (Container : Set) return Boolean;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type));
+
+   function Next (Container : Set; Position : Cursor) return Cursor;
+
+   procedure Next (Container : Set; Position : in out Cursor);
+
+   ----------------
+
    generic
       type Key_Type (<>) is private;
 
index b24be67469ac42038dabda9ea90fca53558e36ee..46f1bcc436b67d5fcaaf099b30a01a4a2bd06921 100644 (file)
@@ -36,8 +36,6 @@ package body Ada.Containers.Helpers is
 
    package body Generic_Implementation is
 
-      use type SAC.Atomic_Unsigned;
-
       ------------
       -- Adjust --
       ------------
@@ -133,7 +131,7 @@ package body Ada.Containers.Helpers is
       procedure TC_Check (T_Counts : Tamper_Counts) is
       begin
          if T_Check then
-            if T_Counts.Busy > 0 then
+            if Is_Busy (T_Counts) then
                raise Program_Error with
                  "attempt to tamper with cursors";
             end if;
@@ -144,7 +142,7 @@ package body Ada.Containers.Helpers is
             --  Thus if the busy count is zero, then the lock count
             --  must also be zero.
 
-            pragma Assert (T_Counts.Lock = 0);
+            pragma Assert (not Is_Locked (T_Counts));
          end if;
       end TC_Check;
 
@@ -154,7 +152,7 @@ package body Ada.Containers.Helpers is
 
       procedure TE_Check (T_Counts : Tamper_Counts) is
       begin
-         if T_Check and then T_Counts.Lock > 0 then
+         if T_Check and then Is_Locked (T_Counts) then
             raise Program_Error with
               "attempt to tamper with elements";
          end if;
index 47811f5a78ab14882ccbbf8bea7d2d8b4a93b3db..92e23d0f6df97524867adcedfaafbe359ffe651e 100644 (file)
@@ -121,9 +121,31 @@ package Ada.Containers.Helpers is
       pragma Inline (TE_Check);
       --  Tampering-with-elements check
 
-      -----------------
-      --  RAII Types --
-      -----------------
+      ---------------------------------------
+      -- Queries of busy and locked status --
+      ---------------------------------------
+
+      --  These are never called when tampering checks are suppressed.
+
+      use type SAC.Atomic_Unsigned;
+
+      pragma Warnings (Off);
+      --  Otherwise, the -gnatw.n switch triggers unwanted warnings on the
+      --  references to atomic variables below.
+
+      function Is_Busy (T_Counts : Tamper_Counts) return Boolean is
+        (if T_Check then T_Counts.Busy > 0 else raise Program_Error);
+      pragma Inline (Is_Busy);
+
+      function Is_Locked (T_Counts : Tamper_Counts) return Boolean is
+        (if T_Check then T_Counts.Lock > 0 else raise Program_Error);
+      pragma Inline (Is_Locked);
+
+      pragma Warnings (On);
+
+      ----------------
+      -- RAII Types --
+      ----------------
 
       --  Initialize of With_Busy increments the Busy count, and Finalize
       --  decrements it. Thus, to prohibit tampering with elements within a
index 7998ee8fe076ef6ae3df003d292c02d3be8f7963..848022e1c41fd155a7bf5006bca443a2bbaa0642 100644 (file)
@@ -643,6 +643,61 @@ is
       end if;
    end Free;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean is
+   begin
+      pragma Assert
+        (Vet (Container.Tree, Position.Node), "bad cursor in Has_Element");
+      pragma Assert ((Position.Container = null) = (Position.Node = null),
+                     "bad nullity in Has_Element");
+      return Position.Container = Container'Unrestricted_Access;
+   end Has_Element;
+
+   function Tampering_With_Cursors_Prohibited
+     (Container : Set) return Boolean
+   is
+   begin
+      return Is_Busy (Container.Tree.TC);
+   end Tampering_With_Cursors_Prohibited;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Element (Position);
+   end Element;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type)) is
+   begin
+      if Checks and then not Has_Element (Container, Position) then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      Query_Element (Position, Process);
+   end Query_Element;
+
+   function Next (Container : Set; Position : Cursor) return Cursor is
+   begin
+      if Checks and then
+        not (Position = No_Element or else Has_Element (Container, Position))
+      then
+         raise Program_Error with "Position for wrong Container";
+      end if;
+
+      return Next (Position);
+   end Next;
+
+   procedure Next (Container : Set; Position : in out Cursor) is
+   begin
+      Position := Next (Container, Position);
+   end Next;
+
    ------------------
    -- Generic_Keys --
    ------------------
index 18333364ab27567cbe85ac2569fe012776e0c229..9619599a5b73143cad46e13f8735918fd0ff30b7 100644 (file)
@@ -231,6 +231,25 @@ is
       Start     : Cursor)
       return Set_Iterator_Interfaces.Reversible_Iterator'class;
 
+   --  Ada 2022 features:
+
+   function Has_Element (Container : Set; Position : Cursor) return Boolean;
+
+   function Tampering_With_Cursors_Prohibited (Container : Set) return Boolean;
+
+   function Element (Container : Set; Position : Cursor) return Element_Type;
+
+   procedure Query_Element
+     (Container : Set;
+      Position  : Cursor;
+      Process   : not null access procedure (Element : Element_Type));
+
+   function Next (Container : Set; Position : Cursor) return Cursor;
+
+   procedure Next (Container : Set; Position : in out Cursor);
+
+   ----------------
+
    generic
       type Key_Type (<>) is private;