From patchwork Wed Dec 1 10:25:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 48326 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 171C3385842A for ; Wed, 1 Dec 2021 10:27:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 171C3385842A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1638354471; bh=QzZ0aGhxA1IjWPk1Ahk08saQ6DA/piKavqhDtzpuhDA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=PEdA4c+FfLDaPs3enwYawmYz3tj9ZYENyFwUQR1mWipH+/fAd7aqykRY4HlHgSBKJ fvnSIKfM6aohoxeQn8Hfjb1bJeTrK5oNwBiBPWar4h4jOTKJlDGCOEPqy0Jzl4Gm2M KbbVdeWLtI+cWESThZ64lvqD51bZLIjy3wUfRrcc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 1A547385840C for ; Wed, 1 Dec 2021 10:25:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1A547385840C Received: by mail-wm1-x32f.google.com with SMTP id p3-20020a05600c1d8300b003334fab53afso21821394wms.3 for ; Wed, 01 Dec 2021 02:25:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=QzZ0aGhxA1IjWPk1Ahk08saQ6DA/piKavqhDtzpuhDA=; b=iWwUrr5WYgnJOHtTiWuLMNpZl0iKcdh93Y9s2doQN48IEHzmSet57wupGjyXD4tI6s iPzGi6WdKUaUK/oFZeLFYZvMl76BJRunNiQdT6HxLLLf3nLOeJ4P4frcvFk0GKGjn7ET Rjd8JjqnyK8RBOayvn72xxruq9ZE+Xfu5OzWLCSXO91XYJ1Z5BbKZFzIMMb1g2uSd2BO yxRri3+KrGpE22JnhE8iPrvU9KK4UQISNdV0kWJCYvSh4ZieHp7nG/N2Xh3toEgfCwlg iJPwhpEkFsjcHZ8OwG5Pzi9VL6dMWSoecaSvAb28izCR8Rj6eA1DbD2HjHOhz+4b/j0S 7Clg== X-Gm-Message-State: AOAM531cgBEcGh4UL5N0qNYMvgK7SCc2qVNy+oNVW58c9gNm/DskZ9tp KpkAy92Le/dSLF+iMo78Ue4NYzPvVyM1aDBX X-Google-Smtp-Source: ABdhPJy4xn9KHiRhsqI28hKto1F0JrCkW+rP36sdx9mEy8vAnzNrE2doagw6rsF31iqVssSu7l3VDQ== X-Received: by 2002:a05:600c:1d0e:: with SMTP id l14mr5764875wms.64.1638354321606; Wed, 01 Dec 2021 02:25:21 -0800 (PST) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id u23sm20157977wru.21.2021.12.01.02.25.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Dec 2021 02:25:20 -0800 (PST) Date: Wed, 1 Dec 2021 10:25:19 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Avoid duplication for Sets functions Message-ID: <20211201102519.GA1635006@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Etienne Servais Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" ARM A.18.7 96.9/3 and 96.12/3 state: > Equivalent to Constant_Reference (Container, Find (Container, Key)). > Equivalent to Reference_Preserving_Key (Container, Find (Container, Key)). Every other procedure and functions noted as "equivalent" for sets in this ARM function use these equivalences. Use it for these two functions. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * libgnat/a-cbhase.adb, libgnat/a-cborse.adb, libgnat/a-cihase.adb, libgnat/a-ciorse.adb, libgnat/a-cohase.adb, libgnat/a-coorse.adb (Constant_Reference,Reference_Preserving_Key): Refactor using the equivalence stated in the RM. 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 @@ -1629,26 +1629,14 @@ is (Container : aliased Set; Key : Key_Type) return Constant_Reference_Type is - Node : constant Count_Type := - Key_Keys.Find (Container'Unrestricted_Access.all, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = 0 then + if Checks and then Position = No_Element then raise Constraint_Error with "key not in set"; end if; - declare - N : Node_Type renames Container.Nodes (Node); - TC : constant Tamper_Counts_Access := - Container.TC'Unrestricted_Access; - begin - return R : constant Constant_Reference_Type := - (Element => N.Element'Unchecked_Access, - Control => (Controlled with TC)) - do - Busy (TC.all); - end return; - end; + return Constant_Reference (Container, Position); end Constant_Reference; -------------- @@ -1836,29 +1824,14 @@ is (Container : aliased in out Set; Key : Key_Type) return Reference_Type is - Node : constant Count_Type := Key_Keys.Find (Container, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = 0 then + if Checks and then Position = No_Element then raise Constraint_Error with "key not in set"; end if; - declare - P : constant Cursor := Find (Container, Key); - begin - return R : constant Reference_Type := - (Element => Container.Nodes (Node).Element'Unrestricted_Access, - Control => - (Controlled with - Container.TC'Unrestricted_Access, - Container'Unrestricted_Access, - Index => Key_Keys.Index (Container, Key), - Old_Pos => P, - Old_Hash => Hash (Key))) - do - Busy (Container.TC); - end return; - end; + return Reference_Preserving_Key (Container, Position); end Reference_Preserving_Key; ------------- 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 @@ -739,25 +739,14 @@ is (Container : aliased Set; Key : Key_Type) return Constant_Reference_Type is - Node : constant Count_Type := Key_Keys.Find (Container, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = 0 then + if Checks and then Position = No_Element then raise Constraint_Error with "key not in set"; end if; - declare - N : Node_Type renames Container.Nodes (Node); - TC : constant Tamper_Counts_Access := - Container.TC'Unrestricted_Access; - begin - return R : constant Constant_Reference_Type := - (Element => N.Element'Unchecked_Access, - Control => (Controlled with TC)) - do - Busy (TC.all); - end return; - end; + return Constant_Reference (Container, Position); end Constant_Reference; -------------- @@ -960,28 +949,14 @@ is (Container : aliased in out Set; Key : Key_Type) return Reference_Type is - Node : constant Count_Type := Key_Keys.Find (Container, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = 0 then + if Checks and then Position = No_Element then raise Constraint_Error with "key not in set"; end if; - declare - N : Node_Type renames Container.Nodes (Node); - begin - return R : constant Reference_Type := - (Element => N.Element'Unchecked_Access, - Control => - (Controlled with - Container.TC'Unrestricted_Access, - Container => Container'Unchecked_Access, - Pos => Find (Container, Key), - Old_Key => new Key_Type'(Key))) - do - Busy (Container.TC); - end return; - end; + return Reference_Preserving_Key (Container, Position); end Reference_Preserving_Key; ------------- 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 @@ -2063,29 +2063,14 @@ is (Container : aliased Set; Key : Key_Type) return Constant_Reference_Type is - HT : Hash_Table_Type renames Container'Unrestricted_Access.HT; - Node : constant Node_Access := Key_Keys.Find (HT, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = null then + if Checks and then Position = No_Element then raise Constraint_Error with "Key not in set"; end if; - if Checks and then Node.Element = null then - raise Program_Error with "Node has no element"; - end if; - - declare - TC : constant Tamper_Counts_Access := - HT.TC'Unrestricted_Access; - begin - return R : constant Constant_Reference_Type := - (Element => Node.Element.all'Access, - Control => (Controlled with TC)) - do - Busy (TC.all); - end return; - end; + return Constant_Reference (Container, Position); end Constant_Reference; -------------- @@ -2280,34 +2265,14 @@ is (Container : aliased in out Set; Key : Key_Type) return Reference_Type is - Node : constant Node_Access := Key_Keys.Find (Container.HT, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = null then + if Checks and then Position = No_Element then raise Constraint_Error with "Key not in set"; end if; - if Checks and then Node.Element = null then - raise Program_Error with "Node has no element"; - end if; - - declare - HT : Hash_Table_Type renames Container.HT; - P : constant Cursor := Find (Container, Key); - begin - return R : constant Reference_Type := - (Element => Node.Element.all'Access, - Control => - (Controlled with - HT.TC'Unrestricted_Access, - Container => Container'Unchecked_Access, - Index => HT_Ops.Index (HT, P.Node), - Old_Pos => P, - Old_Hash => Hash (Key))) - do - Busy (HT.TC); - end return; - end; + return Reference_Preserving_Key (Container, Position); end Reference_Preserving_Key; ------------- 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 @@ -771,29 +771,14 @@ is (Container : aliased Set; Key : Key_Type) return Constant_Reference_Type is - Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = null then + if Checks and then Position = No_Element then raise Constraint_Error with "Key not in set"; end if; - if Checks and then Node.Element = null then - raise Program_Error with "Node has no element"; - end if; - - declare - Tree : Tree_Type renames Container'Unrestricted_Access.all.Tree; - TC : constant Tamper_Counts_Access := - Tree.TC'Unrestricted_Access; - begin - return R : constant Constant_Reference_Type := - (Element => Node.Element.all'Access, - Control => (Controlled with TC)) - do - Busy (TC.all); - end return; - end; + return Constant_Reference (Container, Position); end Constant_Reference; -------------- @@ -1029,32 +1014,14 @@ is (Container : aliased in out Set; Key : Key_Type) return Reference_Type is - Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = null then + if Checks and then Position = No_Element then raise Constraint_Error with "Key not in set"; end if; - if Checks and then Node.Element = null then - raise Program_Error with "Node has no element"; - end if; - - declare - Tree : Tree_Type renames Container.Tree; - begin - return R : constant Reference_Type := - (Element => Node.Element.all'Unchecked_Access, - Control => - (Controlled with - Tree.TC'Unrestricted_Access, - Container => Container'Unchecked_Access, - Pos => Find (Container, Key), - Old_Key => new Key_Type'(Key))) - do - Busy (Tree.TC); - end return; - end; + return Reference_Preserving_Key (Container, Position); end Reference_Preserving_Key; ----------------------------------- 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 @@ -1876,25 +1876,14 @@ is (Container : aliased Set; Key : Key_Type) return Constant_Reference_Type is - HT : Hash_Table_Type renames Container'Unrestricted_Access.HT; - Node : constant Node_Access := Key_Keys.Find (HT, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = null then + if Checks and then Position = No_Element then raise Constraint_Error with "Key not in set"; end if; - declare - TC : constant Tamper_Counts_Access := - HT.TC'Unrestricted_Access; - begin - return R : constant Constant_Reference_Type := - (Element => Node.Element'Access, - Control => (Controlled with TC)) - do - Busy (TC.all); - end return; - end; + return Constant_Reference (Container, Position); end Constant_Reference; -------------- @@ -2087,30 +2076,14 @@ is (Container : aliased in out Set; Key : Key_Type) return Reference_Type is - Node : constant Node_Access := Key_Keys.Find (Container.HT, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = null then + if Checks and then Position = No_Element then raise Constraint_Error with "key not in set"; end if; - declare - HT : Hash_Table_Type renames Container.HT; - P : constant Cursor := Find (Container, Key); - begin - return R : constant Reference_Type := - (Element => Node.Element'Access, - Control => - (Controlled with - HT.TC'Unrestricted_Access, - Container'Unrestricted_Access, - Index => HT_Ops.Index (HT, P.Node), - Old_Pos => P, - Old_Hash => Hash (Key))) - do - Busy (HT.TC); - end return; - end; + return Reference_Preserving_Key (Container, Position); end Reference_Preserving_Key; ------------- 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 @@ -693,25 +693,14 @@ is (Container : aliased Set; Key : Key_Type) return Constant_Reference_Type is - Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = null then + if Checks and then Position = No_Element then raise Constraint_Error with "key not in set"; end if; - declare - Tree : Tree_Type renames Container'Unrestricted_Access.all.Tree; - TC : constant Tamper_Counts_Access := - Tree.TC'Unrestricted_Access; - begin - return R : constant Constant_Reference_Type := - (Element => Node.Element'Access, - Control => (Controlled with TC)) - do - Busy (TC.all); - end return; - end; + return Constant_Reference (Container, Position); end Constant_Reference; -------------- @@ -915,28 +904,14 @@ is (Container : aliased in out Set; Key : Key_Type) return Reference_Type is - Node : constant Node_Access := Key_Keys.Find (Container.Tree, Key); + Position : constant Cursor := Find (Container, Key); begin - if Checks and then Node = null then + if Checks and then Position = No_Element then raise Constraint_Error with "Key not in set"; end if; - declare - Tree : Tree_Type renames Container.Tree; - begin - return R : constant Reference_Type := - (Element => Node.Element'Access, - Control => - (Controlled with - Tree.TC'Unrestricted_Access, - Container => Container'Unchecked_Access, - Pos => Find (Container, Key), - Old_Key => new Key_Type'(Key))) - do - Busy (Tree.TC); - end return; - end; + return Reference_Preserving_Key (Container, Position); end Reference_Preserving_Key; -------------