[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.
This commit is contained in:
Javier Miranda 2022-02-15 18:39:42 +00:00 committed by Pierre-Marie de Rodat
parent fafccfbf77
commit 18e278727e
1 changed files with 25 additions and 4 deletions

View File

@ -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;