]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Missing support for 'Old with overloaded function
authorJavier Miranda <miranda@adacore.com>
Tue, 23 Apr 2024 17:30:23 +0000 (17:30 +0000)
committerMarc Poulhiès <poulhies@adacore.com>
Thu, 13 Jun 2024 13:30:28 +0000 (15:30 +0200)
The compiler reports an error when the prefix of 'Old is
a call to an overloaded function that has no parameters.

gcc/ada/

* sem_attr.adb (Analyze_Attribute): Enhance support for
using 'Old with a prefix that references an overloaded
function that has no parameters; add missing support
for the use of 'Old within qualified expressions.

* sem_util.ads (Preanalyze_And_Resolve_Without_Errors):
New subprogram.

* sem_util.adb (Preanalyze_And_Resolve_Without_Errors):
New subprogram.

gcc/ada/sem_attr.adb
gcc/ada/sem_util.adb
gcc/ada/sem_util.ads

index 2fd95f36d65cbe3677abab39df99904f4e1c1fb9..22fbca45ac5fe53d2d2c5d2443e4c15f768c9a7a 100644 (file)
@@ -5534,7 +5534,42 @@ package body Sem_Attr is
          --  The prefix must be preanalyzed as the full analysis will take
          --  place during expansion.
 
-         Preanalyze_And_Resolve (P);
+         --  If the attribute reference has an expected type or shall resolve
+         --  to a given type, the same applies to the prefix; otherwise the
+         --  prefix shall be resolved independently of context (RM 6.1.1(8/5)).
+
+         if Nkind (Parent (N)) = N_Qualified_Expression then
+            Preanalyze_And_Resolve (P, Etype (Parent (N)));
+
+         --  An special case occurs when the prefix is an overloaded function
+         --  call without formals; in order to identify such case we preanalyze
+         --  a duplicate of the prefix ignoring errors.
+
+         else
+            declare
+               P_Copy : constant Node_Id := New_Copy_Tree (P);
+
+            begin
+               Set_Parent (P_Copy, Parent (P));
+
+               Preanalyze_And_Resolve_Without_Errors (P_Copy);
+
+               --  In the special case of a call to an overloaded function
+               --  without extra formals we resolve it using its returned
+               --  type (which is the unique valid call); if this not the
+               --  case we will report the error later, as part of the
+               --  regular analysis of the full expression.
+
+               if Nkind (P_Copy) = N_Function_Call
+                 and then Is_Overloaded (Name (P_Copy))
+                 and then No (First_Formal (Entity (Name (P_Copy))))
+               then
+                  Preanalyze_And_Resolve (P, Etype (Name (P_Copy)));
+               else
+                  Preanalyze_And_Resolve (P);
+               end if;
+            end;
+         end if;
 
          --  Ensure that the prefix does not contain attributes 'Old or 'Result
 
index 5bea088c44e23499e5b18bb1351f914ff7ed9597..438dea7997787d48def6da7d37f904b996e863fe 100644 (file)
@@ -25790,6 +25790,18 @@ package body Sem_Util is
       return Kind;
    end Policy_In_Effect;
 
+   -------------------------------------------
+   -- Preanalyze_And_Resolve_Without_Errors --
+   -------------------------------------------
+
+   procedure Preanalyze_And_Resolve_Without_Errors (N : Node_Id) is
+      Status : constant Boolean := Get_Ignore_Errors;
+   begin
+      Set_Ignore_Errors (True);
+      Preanalyze_And_Resolve (N);
+      Set_Ignore_Errors (Status);
+   end Preanalyze_And_Resolve_Without_Errors;
+
    -------------------------------
    -- Preanalyze_Without_Errors --
    -------------------------------
index f282d1fad99e6022b37725757cf3e901585eec66..bda295f0a7f8dc2dffb36f558e635888a2b94124 100644 (file)
@@ -3388,6 +3388,9 @@ package Sem_Util is
    function Yields_Universal_Type (N : Node_Id) return Boolean;
    --  Determine whether unanalyzed node N yields a universal type
 
+   procedure Preanalyze_And_Resolve_Without_Errors (N : Node_Id);
+   --  Preanalyze and resolve N without reporting errors
+
    procedure Preanalyze_Without_Errors (N : Node_Id);
    --  Preanalyze N without reporting errors