From: Pierre-Marie de Rodat <derodat@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Bob Duff <duff@adacore.com>
Subject: [Ada] Add Ada 2022 features to sets containers
Date: Mon, 4 Jul 2022 07:50:08 +0000 [thread overview]
Message-ID: <20220704075008.GA99042@adacore.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 627 bytes --]
This patch adds some Ada 2022 features to the set children of
Ada.Containers.
Tested on x86_64-pc-linux-gnu, committed on trunk
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.
[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 20745 bytes --]
diff --git a/gcc/ada/libgnat/a-cbhase.adb b/gcc/ada/libgnat/a-cbhase.adb
--- a/gcc/ada/libgnat/a-cbhase.adb
+++ b/gcc/ada/libgnat/a-cbhase.adb
@@ -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
-----------------------
diff --git a/gcc/ada/libgnat/a-cbhase.ads b/gcc/ada/libgnat/a-cbhase.ads
--- a/gcc/ada/libgnat/a-cbhase.ads
+++ b/gcc/ada/libgnat/a-cbhase.ads
@@ -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;
diff --git a/gcc/ada/libgnat/a-cborse.adb b/gcc/ada/libgnat/a-cborse.adb
--- a/gcc/ada/libgnat/a-cborse.adb
+++ b/gcc/ada/libgnat/a-cborse.adb
@@ -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 --
------------------
diff --git a/gcc/ada/libgnat/a-cborse.ads b/gcc/ada/libgnat/a-cborse.ads
--- a/gcc/ada/libgnat/a-cborse.ads
+++ b/gcc/ada/libgnat/a-cborse.ads
@@ -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;
diff --git a/gcc/ada/libgnat/a-cihase.adb b/gcc/ada/libgnat/a-cihase.adb
--- a/gcc/ada/libgnat/a-cihase.adb
+++ b/gcc/ada/libgnat/a-cihase.adb
@@ -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
-----------------------
diff --git a/gcc/ada/libgnat/a-cihase.ads b/gcc/ada/libgnat/a-cihase.ads
--- a/gcc/ada/libgnat/a-cihase.ads
+++ b/gcc/ada/libgnat/a-cihase.ads
@@ -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;
diff --git a/gcc/ada/libgnat/a-ciorse.adb b/gcc/ada/libgnat/a-ciorse.adb
--- a/gcc/ada/libgnat/a-ciorse.adb
+++ b/gcc/ada/libgnat/a-ciorse.adb
@@ -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 --
------------------
diff --git a/gcc/ada/libgnat/a-ciorse.ads b/gcc/ada/libgnat/a-ciorse.ads
--- a/gcc/ada/libgnat/a-ciorse.ads
+++ b/gcc/ada/libgnat/a-ciorse.ads
@@ -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;
diff --git a/gcc/ada/libgnat/a-cohase.adb b/gcc/ada/libgnat/a-cohase.adb
--- a/gcc/ada/libgnat/a-cohase.adb
+++ b/gcc/ada/libgnat/a-cohase.adb
@@ -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
-----------------------
diff --git a/gcc/ada/libgnat/a-cohase.ads b/gcc/ada/libgnat/a-cohase.ads
--- a/gcc/ada/libgnat/a-cohase.ads
+++ b/gcc/ada/libgnat/a-cohase.ads
@@ -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;
diff --git a/gcc/ada/libgnat/a-conhel.adb b/gcc/ada/libgnat/a-conhel.adb
--- a/gcc/ada/libgnat/a-conhel.adb
+++ b/gcc/ada/libgnat/a-conhel.adb
@@ -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;
diff --git a/gcc/ada/libgnat/a-conhel.ads b/gcc/ada/libgnat/a-conhel.ads
--- a/gcc/ada/libgnat/a-conhel.ads
+++ b/gcc/ada/libgnat/a-conhel.ads
@@ -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
diff --git a/gcc/ada/libgnat/a-coorse.adb b/gcc/ada/libgnat/a-coorse.adb
--- a/gcc/ada/libgnat/a-coorse.adb
+++ b/gcc/ada/libgnat/a-coorse.adb
@@ -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 --
------------------
diff --git a/gcc/ada/libgnat/a-coorse.ads b/gcc/ada/libgnat/a-coorse.ads
--- a/gcc/ada/libgnat/a-coorse.ads
+++ b/gcc/ada/libgnat/a-coorse.ads
@@ -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;
reply other threads:[~2022-07-04 7:50 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220704075008.GA99042@adacore.com \
--to=derodat@adacore.com \
--cc=duff@adacore.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).