From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1914) id 5A7D53857806; Fri, 13 May 2022 08:08:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A7D53857806 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Pierre-Marie de Rodat To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-395] [Ada] Wrong address for class-wide interface access conversion X-Act-Checkin: gcc X-Git-Author: Javier Miranda X-Git-Refname: refs/heads/master X-Git-Oldrev: fafccfbf77ac245c7fa77c06e4ae001009c3425e X-Git-Newrev: 18e278727e1a0430f50e878dbfadb35dae54baff Message-Id: <20220513080816.5A7D53857806@sourceware.org> Date: Fri, 13 May 2022 08:08:16 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 May 2022 08:08:16 -0000 https://gcc.gnu.org/g:18e278727e1a0430f50e878dbfadb35dae54baff commit r13-395-g18e278727e1a0430f50e878dbfadb35dae54baff Author: Javier Miranda Date: Tue Feb 15 18:39:42 2022 +0000 [Ada] Wrong address for class-wide interface access conversion The compiler generates wrong code on instantiations of package Address_To_Access_Conversions when the generic formal is a class-wide interface type; this causes wrong dispatching calls when the access-to-class-wide-interface object returned by To_Pointer is used to dispatch a call. gcc/ada/ * exp_attr.adb (Expand_N_Attribute_Reference): The expansion of 'Address in a call to an instantiation of the implicit subprogram To_Pointer with a class-wide interface type target requires adding an implicit type conversion to force displacement of the "this" pointer. Diff: --- gcc/ada/exp_attr.adb | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/gcc/ada/exp_attr.adb b/gcc/ada/exp_attr.adb index cc043513911..e6d3e74971f 100644 --- a/gcc/ada/exp_attr.adb +++ b/gcc/ada/exp_attr.adb @@ -2543,6 +2543,28 @@ package body Exp_Attr is Analyze_And_Resolve (N, Addr); end; + -- 'Address is an actual parameter of the call to the implicit + -- subprogram To_Pointer instantiated with a class-wide interface + -- type; its expansion requires adding an implicit type conversion + -- to force displacement of the "this" pointer. + + elsif Tagged_Type_Expansion + and then Nkind (Parent (N)) = N_Function_Call + and then Nkind (Name (Parent (N))) in N_Has_Entity + and then Is_Intrinsic_Subprogram (Entity (Name (Parent (N)))) + and then Chars (Entity (Name (Parent (N)))) = Name_To_Pointer + and then Is_Interface (Designated_Type (Etype (Parent (N)))) + and then Is_Class_Wide_Type (Designated_Type (Etype (Parent (N)))) + then + declare + Iface_Typ : constant Entity_Id := + Designated_Type (Etype (Parent (N))); + begin + Rewrite (Pref, Convert_To (Iface_Typ, Relocate_Node (Pref))); + Analyze_And_Resolve (Pref, Iface_Typ); + return; + end; + -- Ada 2005 (AI-251): Class-wide interface objects are always -- "displaced" to reference the tag associated with the interface -- type. In order to obtain the real address of such objects we @@ -2554,9 +2576,9 @@ package body Exp_Attr is -- of nested subprograms), since the address needs to be assigned -- as-is to such components. - elsif Is_Class_Wide_Type (Ptyp) + elsif Tagged_Type_Expansion + and then Is_Class_Wide_Type (Ptyp) and then Is_Interface (Underlying_Type (Ptyp)) - and then Tagged_Type_Expansion and then not (Nkind (Pref) in N_Has_Entity and then Is_Subprogram (Entity (Pref))) and then not Is_Unnested_Component_Init (N) @@ -2564,8 +2586,7 @@ package body Exp_Attr is Rewrite (N, Make_Function_Call (Loc, Name => New_Occurrence_Of (RTE (RE_Base_Address), Loc), - Parameter_Associations => New_List ( - Relocate_Node (N)))); + Parameter_Associations => New_List (Relocate_Node (N)))); Analyze (N); return; end if;