From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by sourceware.org (Postfix) with ESMTPS id 146A13851407 for ; Wed, 13 Jul 2022 10:02:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 146A13851407 Received: by mail-ed1-x52b.google.com with SMTP id w12so12684814edd.13 for ; Wed, 13 Jul 2022 03:02:59 -0700 (PDT) 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=9oVPtnxQC6MnpSigJ0xT2jhun7gTXB3vpfx736lvdOU=; b=7rQf7qXPtXNyv6slZw/3wEjfLbYqkpK7mVGHDekZQoLUMP39UnRUfUfft3EUi2L+iW DPw6sDeNC/M8qc/58SH4PN7eEJMHJGZN8PMD3B84WqQtClNgQyMHGOyhH59RjVbZnPWh qurL3spHnahVugV4y9rQ3YyO2G0FI2TaMm1hzZXLmo7XHqvLtQuxSW1bKKnikAmoxp+/ cbrXJdaWYT/+Ebrat6Kv1OfJpQ6GeL0+2vEOCsXMSehLoWfs65CRbtWYMDHz7PJ+l6oP B2pbWa3fmjFuvL8OwtY/Pmx/h2pxKoEXgUqQeC6PS7ZhJ5VQ0XCLV4gmcMCFqzUdLKRY f/Hg== X-Gm-Message-State: AJIora+vWk7ZAXgMBChKuVVNCW+bfti/dPVFdEkcqDoxdgtd2mNbLLeL IhHv216KoAYV93GH5j5ccoaxYuoM02uL6g== X-Google-Smtp-Source: AGRyM1sp9yTEZhCsbL6MmQHWHKGOahS1lEP9tQ8cyx65ZZkdRblfTwZJmwnnJL1iqI7yN6R1dMln3w== X-Received: by 2002:a05:6402:248d:b0:437:dd4c:e70e with SMTP id q13-20020a056402248d00b00437dd4ce70emr3655665eda.75.1657706577856; Wed, 13 Jul 2022 03:02:57 -0700 (PDT) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id z9-20020a170906270900b00722e50dab2csm4762063ejc.109.2022.07.13.03.02.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Jul 2022 03:02:57 -0700 (PDT) Date: Wed, 13 Jul 2022 10:02:56 +0000 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Piotr Trojanek Subject: [Ada] Fix for visibility of aspect expressions inside generic units Message-ID: <20220713100256.GA994685@adacore.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2oS5YaxWCcQjTEyO" Content-Disposition: inline X-Spam-Status: No, score=-13.2 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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-List-Received-Date: Wed, 13 Jul 2022 10:03:01 -0000 --2oS5YaxWCcQjTEyO Content-Type: text/plain; charset=us-ascii Content-Disposition: inline When a generic unit contains references to global entities (i.e. entities declared outside of this generic unit), those references are saved: from the analyzed copy of a generic unit (which is then discarded) into a generic template (which is then instantiated, possibly many times). To save those references we maintain an association from nodes in the generic template to nodes in the analyzed copy. However, this association breaks when analysis of the generic copy calls Relocate_Node, which conceptually only moves the node, while in fact it creates a copy with a new Node_Id. In particular, this association was broken by calls to Relocate_Node that happen when transforming various aspects into corresponding pragmas or attribute definition clases. For the most common Pre and Post aspects this was fixed years ago by not using Relocate_Node and simply sharing the tree. This patch extends this fix to other aspects, in particular those that allow non-static expressions. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_ch13.adb (Relocate_Expression): New routine with code that previously was only applied to Pre and Post aspects. (Analyze_Aspect_Specifications): Apply the above routine to other aspects, in particular to aspects Address, Attach_Handler, Predicate and Interrupt_Priority. --2oS5YaxWCcQjTEyO Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="patch.diff" diff --git a/gcc/ada/sem_ch13.adb b/gcc/ada/sem_ch13.adb --- a/gcc/ada/sem_ch13.adb +++ b/gcc/ada/sem_ch13.adb @@ -1650,6 +1650,18 @@ package body Sem_Ch13 is -- pragma of the same kind. Flag Is_Generic should be set when the -- context denotes a generic instance. + function Relocate_Expression (Source : Node_Id) return Node_Id; + -- Outside of a generic this function is equivalent to Relocate_Node. + -- Inside a generic it is an identity function, because Relocate_Node + -- would create a new node that is not associated with the generic + -- template. This association is needed to save references to entities + -- that are global to the generic (and might be not visible from where + -- the generic is instantiated). + -- + -- Inside a generic the original tree is shared between aspect and + -- a corresponding pragma (or an attribute definition clause). This + -- parallels what is done in sem_prag.adb (see Get_Argument). + -------------- -- Decorate -- -------------- @@ -1835,6 +1847,19 @@ package body Sem_Ch13 is end if; end Insert_Pragma; + ------------------------- + -- Relocate_Expression -- + ------------------------- + + function Relocate_Expression (Source : Node_Id) return Node_Id is + begin + if Inside_A_Generic then + return Source; + else + return Atree.Relocate_Node (Source); + end if; + end Relocate_Expression; + -- Local variables Aspect : Node_Id; @@ -3229,7 +3254,7 @@ package body Sem_Ch13 is Make_Attribute_Definition_Clause (Loc, Name => Ent, Chars => Nam, - Expression => Relocate_Node (Expr)); + Expression => Relocate_Expression (Expr)); -- If the address is specified, then we treat the entity as -- referenced, to avoid spurious warnings. This is analogous @@ -3293,7 +3318,7 @@ package body Sem_Ch13 is Make_Pragma_Argument_Association (Sloc (Ent), Expression => Ent), Make_Pragma_Argument_Association (Sloc (Expr), - Expression => Relocate_Node (Expr))), + Expression => Relocate_Expression (Expr))), Pragma_Name => Name_Attach_Handler); -- We need to insert this pragma into the tree to get proper @@ -3335,7 +3360,7 @@ package body Sem_Ch13 is Make_Pragma_Argument_Association (Sloc (Ent), Expression => Ent), Make_Pragma_Argument_Association (Sloc (Expr), - Expression => Relocate_Node (Expr))), + Expression => Relocate_Expression (Expr))), Pragma_Name => Name_Predicate); -- Mark type has predicates, and remember what kind of @@ -3580,7 +3605,7 @@ package body Sem_Ch13 is Make_Attribute_Definition_Clause (Loc, Name => Ent, Chars => Nam, - Expression => Relocate_Node (Expr)); + Expression => Relocate_Expression (Expr)); end if; -- Suppress/Unsuppress @@ -4599,32 +4624,12 @@ package body Sem_Ch13 is -- Build the precondition/postcondition pragma - -- We use Relocate_Node here rather than New_Copy_Tree - -- because subsequent visibility analysis of the aspect - -- depends on this sharing. This should be cleaned up??? - - -- If the context is generic, we want to preserve the - -- original tree, and simply share it between aspect and - -- generated attribute. This parallels what is done in - -- sem_prag.adb (see Get_Argument). - - declare - New_Expr : Node_Id; - - begin - if Inside_A_Generic then - New_Expr := Expr; - else - New_Expr := Relocate_Node (Expr); - end if; - - Aitem := Make_Aitem_Pragma - (Pragma_Argument_Associations => New_List ( - Make_Pragma_Argument_Association (Eloc, - Chars => Name_Check, - Expression => New_Expr)), - Pragma_Name => Pname); - end; + Aitem := Make_Aitem_Pragma + (Pragma_Argument_Associations => New_List ( + Make_Pragma_Argument_Association (Eloc, + Chars => Name_Check, + Expression => Relocate_Expression (Expr))), + Pragma_Name => Pname); -- Add message unless exception messages are suppressed --2oS5YaxWCcQjTEyO--