diff mbox series

[COMMITTED,27/30] ada: Crash on access attribute with overloaded prefix denoting reference object

Message ID 20240801151738.400796-27-poulhies@adacore.com
State New
Headers show
Series [COMMITTED,01/30] ada: Remove obsolete workaround | expand

Commit Message

Marc Poulhiès Aug. 1, 2024, 3:17 p.m. UTC
From: Gary Dismukes <dismukes@adacore.com>

The compiler fails to accept an access attribute where the prefix is the name
of an object of a user-defined reference type, and is not prepared to deal
with the possibility of overloaded prefixes other than subprogram cases.
Such a prefix can either represent the reference object directly, or it
can be interpreted as an implicit dereferencing of the object's reference
value, depending on the expected type.  Special handling for this kind of
prefix is added alongside the normal handling for overloaded prefixes of
access attributes.

gcc/ada/

	* sem_attr.adb (Resolve_Attribute, Attribute_*Access): Resolve
	overloaded prefixes that denote objects of reference types,
	determining whether to use the prefix object directly, or expand
	it as an explicit dereference.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/sem_attr.adb | 37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/gcc/ada/sem_attr.adb b/gcc/ada/sem_attr.adb
index 5720e5e9357..a5c90e3f36d 100644
--- a/gcc/ada/sem_attr.adb
+++ b/gcc/ada/sem_attr.adb
@@ -11267,7 +11267,42 @@  package body Sem_Attr is
                if Is_Overloaded (P) then
                   Get_First_Interp (P, Index, It);
                   while Present (It.Nam) loop
-                     if Type_Conformant (Designated_Type (Typ), It.Nam) then
+
+                     --  Overloaded object names can occur when user-defined
+                     --  references are involved. The prefix can be interpreted
+                     --  as either just an object of the reference type, or an
+                     --  implicit dereferencing of such an object.
+
+                     if Is_Object (It.Nam) then
+                        if Covers (Designated_Type (Typ), Etype (It.Typ)) then
+
+                           --  If the interpretation is a discriminant for an
+                           --  implicit dereference, then build the dereference
+                           --  and resolve the rewritten attribute recursively.
+
+                           if Ekind (It.Nam) = E_Discriminant
+                             and then Has_Implicit_Dereference (It.Nam)
+                           then
+                              Build_Explicit_Dereference
+                                (P, Get_Reference_Discriminant (Etype (P)));
+                              Resolve_Attribute (N, Typ);
+
+                              return;
+                           end if;
+
+                           --  The prefix is definitely NOT overloaded anymore
+                           --  at this point, so we reset the Is_Overloaded
+                           --  flag to avoid any confusion when reanalyzing
+                           --  the node.
+
+                           Set_Is_Overloaded (P, False);
+                           Set_Is_Overloaded (N, False);
+                           Generate_Reference (Entity (P), P);
+
+                           exit;
+                        end if;
+
+                     elsif Type_Conformant (Designated_Type (Typ), It.Nam) then
                         Set_Entity (P, It.Nam);
 
                         --  The prefix is definitely NOT overloaded anymore at