]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Crash on access attribute with overloaded prefix denoting reference object
authorGary Dismukes <dismukes@adacore.com>
Tue, 2 Jul 2024 00:42:12 +0000 (00:42 +0000)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Thu, 1 Aug 2024 15:14:38 +0000 (17:14 +0200)
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.

gcc/ada/sem_attr.adb

index 5720e5e935726d98114c74777f9e4a47fa1548f5..a5c90e3f36ded933c7f0036ecb7245cf32548351 100644 (file)
@@ -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