public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-1033] ada: Update Controlling_Argument when copying trees
@ 2023-05-22  8:48 Marc Poulhi?s
  0 siblings, 0 replies; only message in thread
From: Marc Poulhi?s @ 2023-05-22  8:48 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:01b0a60038a91d5607e8f64d18bfa43984255632

commit r14-1033-g01b0a60038a91d5607e8f64d18bfa43984255632
Author: Piotr Trojanek <trojanek@adacore.com>
Date:   Fri Jan 27 12:37:25 2023 +0100

    ada: Update Controlling_Argument when copying trees
    
    When copying the AST we need to update fields that carry semantic
    meaning and not just copy them. We already updated some of them,
    e.g. the First/Next_Named_Association chain, but failed to update
    the Controlling_Argument.
    
    This fix doesn't appear to change anything for the compiler, but it is
    needed for GNATprove, where we no longer want to expand expression
    functions and instead we want to copy their preanalyzed expressions.
    
    gcc/ada/
    
            * sem_util.ads (New_Copy_Tree): Update comment.
            * sem_util.adb (New_Copy_Tree): Update Controlling_Argument, very
            much like we update the First/Next_Named_Association.

Diff:
---
 gcc/ada/sem_util.adb | 73 +++++++++++++++++++++++++++++++++++++++++++++++++---
 gcc/ada/sem_util.ads |  1 +
 2 files changed, 70 insertions(+), 4 deletions(-)

diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb
index 9cf21953fea..cb0cbf2cf3a 100644
--- a/gcc/ada/sem_util.adb
+++ b/gcc/ada/sem_util.adb
@@ -23136,6 +23136,13 @@ package body Sem_Util is
       pragma Inline (Update_CFS_Sloc);
       --  Update the Comes_From_Source and Sloc attributes of node or entity N
 
+      procedure Update_Controlling_Argument
+        (Old_Call : Node_Id;
+         New_Call : Node_Id);
+      pragma Inline (Update_Controlling_Argument);
+      --  Update Controlling_Argument of New_Call base on Old_Call to make it
+      --  points to the corresponding newly copied actual parameter.
+
       procedure Update_Named_Associations
         (Old_Call : Node_Id;
          New_Call : Node_Id);
@@ -23574,17 +23581,22 @@ package body Sem_Util is
               (Old_Assoc => N,
                New_Assoc => Result);
 
-            --  Update the First/Next_Named_Association chain for a replicated
-            --  call.
+            --  Update the First/Next_Named_Association chain and the
+            --  Controlling_Argument for a replicated call.
 
             if Nkind (N) in N_Entry_Call_Statement
-                          | N_Function_Call
-                          | N_Procedure_Call_Statement
+                          | N_Subprogram_Call
             then
                Update_Named_Associations
                  (Old_Call => N,
                   New_Call => Result);
 
+               if Nkind (N) in N_Subprogram_Call then
+                  Update_Controlling_Argument
+                    (Old_Call => N,
+                     New_Call => Result);
+               end if;
+
             --  Update the Renamed_Object attribute of a replicated object
             --  declaration.
 
@@ -23694,6 +23706,59 @@ package body Sem_Util is
          end if;
       end Update_CFS_Sloc;
 
+      ---------------------------------
+      -- Update_Controlling_Argument --
+      ---------------------------------
+
+      procedure Update_Controlling_Argument
+        (Old_Call : Node_Id;
+         New_Call : Node_Id)
+      is
+         New_Act : Node_Id;
+         Old_Act : Node_Id;
+
+         Old_Ctrl_Arg : constant Node_Id := Controlling_Argument (Old_Call);
+         --  Controlling argument of the old call node
+
+         Replaced : Boolean := False;
+         --  Flag to make sure that replacement works as expected
+
+      begin
+         if No (Old_Ctrl_Arg) then
+            return;
+         end if;
+
+         --  Recreate the Controlling_Argument of a call by traversing both the
+         --  old and new actual parameters in parallel.
+
+         New_Act := First (Parameter_Associations (New_Call));
+         Old_Act := First (Parameter_Associations (Old_Call));
+         while Present (Old_Act) loop
+
+            --  Actual parameter appears either in a named parameter
+            --  association or directly.
+
+            if Nkind (Old_Act) = N_Parameter_Association then
+               if Explicit_Actual_Parameter (Old_Act) = Old_Ctrl_Arg then
+                  Set_Controlling_Argument
+                    (New_Call, Explicit_Actual_Parameter (New_Act));
+                  Replaced := True;
+                  exit;
+               end if;
+
+            elsif Old_Act = Old_Ctrl_Arg then
+               Set_Controlling_Argument (New_Call, New_Act);
+               Replaced := True;
+               exit;
+            end if;
+
+            Next (New_Act);
+            Next (Old_Act);
+         end loop;
+
+         pragma Assert (Replaced);
+      end Update_Controlling_Argument;
+
       -------------------------------
       -- Update_Named_Associations --
       -------------------------------
diff --git a/gcc/ada/sem_util.ads b/gcc/ada/sem_util.ads
index 42c6d249e2f..060d04241d3 100644
--- a/gcc/ada/sem_util.ads
+++ b/gcc/ada/sem_util.ads
@@ -2646,6 +2646,7 @@ package Sem_Util is
    --
    --        First_Named_Actual
    --        Next_Named_Actual
+   --        Controlling_Argument
    --
    --      If applicable, the Etype field (if any) is updated to refer to a
    --      local itype or type (see below).

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-05-22  8:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-22  8:48 [gcc r14-1033] ada: Update Controlling_Argument when copying trees Marc Poulhi?s

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).