From patchwork Fri Jun 18 08:38:27 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: 1494164 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G5t2m2MPyz9sSs for ; Fri, 18 Jun 2021 18:51:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 720FB3AA9C36 for ; Fri, 18 Jun 2021 08:51:54 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTPS id 6AF0D39A0463 for ; Fri, 18 Jun 2021 08:38:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6AF0D39A0463 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id BB520117558; Fri, 18 Jun 2021 04:38:27 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 7XrDfX1FriyM; Fri, 18 Jun 2021 04:38:27 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id 8E963117B54; Fri, 18 Jun 2021 04:38:27 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id 8A82E180; Fri, 18 Jun 2021 04:38:27 -0400 (EDT) Date: Fri, 18 Jun 2021 04:38:27 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Subject: [Ada] Fix detection of overlapping actuals with renamings Message-ID: <20210618083827.GA130201@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: Piotr Trojanek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Routine Denotes_Same_Object wrongly handled renamings of renamings. In a code like this: B : Integer renames A; C : Integer renames B; names "B" and "C" differ and their renamed object names "A" and "B" differ too. This patch rewrites this routine to literally follow the RM, which fixes the problem with renamings. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_util.adb (Denotes_Same_Object): Explicitly test for node kinds being the same; deal with renamings one-by-one; adjust numbers in references to the Ada RM. diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb --- a/gcc/ada/sem_util.adb +++ b/gcc/ada/sem_util.adb @@ -7388,84 +7388,46 @@ package body Sem_Util is return True; end Is_Valid_Renaming; - -- Local variables - - Obj1 : Node_Id := A1; - Obj2 : Node_Id := A2; - -- Start of processing for Denotes_Same_Object begin - -- Both names statically denote the same stand-alone object or parameter - -- (RM 6.4.1(6.5/3)) + -- Both names statically denote the same stand-alone object or + -- parameter (RM 6.4.1(6.6/3)). - if Is_Entity_Name (Obj1) - and then Is_Entity_Name (Obj2) - and then Entity (Obj1) = Entity (Obj2) + if Is_Entity_Name (A1) + and then Is_Entity_Name (A2) + and then Entity (A1) = Entity (A2) then return True; - end if; - - -- For renamings, the prefix of any dereference within the renamed - -- object_name is not a variable, and any expression within the - -- renamed object_name contains no references to variables nor - -- calls on nonstatic functions (RM 6.4.1(6.10/3)). - - if Is_Renaming (Obj1) then - if Is_Valid_Renaming (Obj1) then - Obj1 := Renamed_Entity (Entity (Obj1)); - else - return False; - end if; - end if; - - if Is_Renaming (Obj2) then - if Is_Valid_Renaming (Obj2) then - Obj2 := Renamed_Entity (Entity (Obj2)); - else - return False; - end if; - end if; - - -- No match if not same node kind (such cases are handled by - -- Denotes_Same_Prefix) - - if Nkind (Obj1) /= Nkind (Obj2) then - return False; - - -- After handling valid renamings, one of the two names statically - -- denoted a renaming declaration whose renamed object_name is known - -- to denote the same object as the other (RM 6.4.1(6.10/3)) - - elsif Is_Entity_Name (Obj1) then - if Is_Entity_Name (Obj2) then - return Entity (Obj1) = Entity (Obj2); - else - return False; - end if; -- Both names are selected_components, their prefixes are known to -- denote the same object, and their selector_names denote the same - -- component (RM 6.4.1(6.6/3)). + -- component (RM 6.4.1(6.7/3)). - elsif Nkind (Obj1) = N_Selected_Component then - return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) + elsif Nkind (A1) = N_Selected_Component + and then Nkind (A2) = N_Selected_Component + then + return Denotes_Same_Object (Prefix (A1), Prefix (A2)) and then - Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2)); + Entity (Selector_Name (A1)) = Entity (Selector_Name (A2)); -- Both names are dereferences and the dereferenced names are known to - -- denote the same object (RM 6.4.1(6.7/3)) + -- denote the same object (RM 6.4.1(6.8/3)). - elsif Nkind (Obj1) = N_Explicit_Dereference then - return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)); + elsif Nkind (A1) = N_Explicit_Dereference + and then Nkind (A2) = N_Explicit_Dereference + then + return Denotes_Same_Object (Prefix (A1), Prefix (A2)); -- Both names are indexed_components, their prefixes are known to denote -- the same object, and each of the pairs of corresponding index values -- are either both static expressions with the same static value or both - -- names that are known to denote the same object (RM 6.4.1(6.8/3)) + -- names that are known to denote the same object (RM 6.4.1(6.9/3)). - elsif Nkind (Obj1) = N_Indexed_Component then - if not Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then + elsif Nkind (A1) = N_Indexed_Component + and then Nkind (A2) = N_Indexed_Component + then + if not Denotes_Same_Object (Prefix (A1), Prefix (A2)) then return False; else declare @@ -7473,8 +7435,8 @@ package body Sem_Util is Indx2 : Node_Id; begin - Indx1 := First (Expressions (Obj1)); - Indx2 := First (Expressions (Obj2)); + Indx1 := First (Expressions (A1)); + Indx2 := First (Expressions (A2)); while Present (Indx1) loop -- Indexes must denote the same static value or same object @@ -7501,33 +7463,53 @@ package body Sem_Util is -- Both names are slices, their prefixes are known to denote the same -- object, and the two slices have statically matching index constraints - -- (RM 6.4.1(6.9/3)) + -- (RM 6.4.1(6.10/3)). - elsif Nkind (Obj1) = N_Slice - and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) + elsif Nkind (A1) = N_Slice + and then Nkind (A2) = N_Slice then - declare - Lo1, Lo2, Hi1, Hi2 : Node_Id; + if not Denotes_Same_Object (Prefix (A1), Prefix (A2)) then + return False; + else + declare + Lo1, Lo2, Hi1, Hi2 : Node_Id; - begin - Get_Index_Bounds (Discrete_Range (Obj1), Lo1, Hi1); - Get_Index_Bounds (Discrete_Range (Obj2), Lo2, Hi2); + begin + Get_Index_Bounds (Discrete_Range (A1), Lo1, Hi1); + Get_Index_Bounds (Discrete_Range (A2), Lo2, Hi2); - -- Check whether bounds are statically identical. There is no - -- attempt to detect partial overlap of slices. + -- Check whether bounds are statically identical. There is no + -- attempt to detect partial overlap of slices. - return Denotes_Same_Object (Lo1, Lo2) - and then - Denotes_Same_Object (Hi1, Hi2); - end; + return Denotes_Same_Object (Lo1, Lo2) + and then + Denotes_Same_Object (Hi1, Hi2); + end; + end if; - -- In the recursion, literals appear as indexes + -- One of the two names statically denotes a renaming declaration whose + -- renamed object_name is known to denote the same object as the other; + -- the prefix of any dereference within the renamed object_name is not a + -- variable, and any expression within the renamed object_name contains + -- no references to variables nor calls on nonstatic functions (RM + -- 6.4.1(6.11/3)). - elsif Nkind (Obj1) = N_Integer_Literal - and then - Nkind (Obj2) = N_Integer_Literal + elsif Is_Renaming (A1) + and then Is_Valid_Renaming (A1) + then + return Denotes_Same_Object (Renamed_Entity (Entity (A1)), A2); + + elsif Is_Renaming (A2) + and then Is_Valid_Renaming (A2) + then + return Denotes_Same_Object (A1, Renamed_Entity (Entity (A2))); + + -- In the recursion, literals appear as slice bounds + + elsif Nkind (A1) = N_Integer_Literal + and then Nkind (A2) = N_Integer_Literal then - return Intval (Obj1) = Intval (Obj2); + return Intval (A1) = Intval (A2); else return False;