From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 6B27F3858C39 for ; Tue, 3 Jan 2023 09:35:27 +0000 (GMT) Received: by mail-wr1-x436.google.com with SMTP id d4so20837901wrw.6 for ; Tue, 03 Jan 2023 01:35:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=fH7c+SULkQ+FqZtGUKzcQE//Vog8nl4q2gcG+JLnK1E=; b=FpuoSlotMtGTEskWyfSU5q9l/uMrf0rgTojGm/9PxMqG5uB3ogb/SELpHcQQv4YkH7 Fak0ide84Sac+wLInGYPZCK1EPAVjeYZiHdcIVqh5LZZAtzpsyLeb+1hO0ikJl/PI9EF LR9RpIw530odnmh7Oibj6W8vRccISrM1k+rzVEOe+MHt9HDukOsMuM2IcFNCSi+rYu7j aW6yXxPqE4F6xBLF8jNOVPpL+UHeUW6jW8mMUcZ/WQ2kKVDm4fAcsyT3uc9YnvcpP3VL pnHNDiBkDB78KmZMrmyq9401bmxmMj9dkiNpEY2MYbI/weFmTyS8XjwOPo9WfBtqhGRW 1q8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=fH7c+SULkQ+FqZtGUKzcQE//Vog8nl4q2gcG+JLnK1E=; b=7tBHcAJBjzysqDf5AUAKXT9jmwQ2vh+izcJ+0IuYyLmBkRsjO67zzDza1zzz94nHtX dToEUuaYqofTXqCKAm6ueLN8R4GtcDv+280tOelX9F/qF7TuMhsHG9xeEm3IWmcFK9vZ N+KkdahuKVvkt5w7uyzur8DgsGKTMCXqwFDr30d7qckeXLN4pd/iKuIEWI5mIjt97ZE3 OmHF3T9Il4kwkzs+DdSYgleUJIRMhCHEDozdERn59Wmb6WXAgRqA7vq5G/pSY1ZsMcbJ h0Z/CGpbRM+B8MrGx3Z8hRtE1yU01ZQeNvnq+CxiIXmnJifOQ07cYdQyV3n4xRpIPMHN TTsA== X-Gm-Message-State: AFqh2krGgHLFFxRl80oMsCFYQpsrqkGvAiTWti1YmNw4aboy2iy36EyU yw40Pmfinm+m4EylfSthsKgImvYMiP7OytPZ6Tg= X-Google-Smtp-Source: AMrXdXtR30ZkuyYzyEMc9SxolnGJ5++06tVx82kRIOMwC1loLlus/GqkwmmX+H0qOkPMo2TXybeW5g== X-Received: by 2002:adf:d846:0:b0:29f:4e42:33c5 with SMTP id k6-20020adfd846000000b0029f4e4233c5mr914898wrl.55.1672738526095; Tue, 03 Jan 2023 01:35:26 -0800 (PST) Received: from localhost.localdomain (static-176-191-105-132.ftth.abo.bbox.fr. [176.191.105.132]) by smtp.gmail.com with ESMTPSA id l25-20020adfa399000000b002421ed1d8c8sm31081255wrb.103.2023.01.03.01.35.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 01:35:25 -0800 (PST) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Fix detection of function calls in object declarations Date: Tue, 3 Jan 2023 10:35:23 +0100 Message-Id: <20230103093523.76239-1-poulhies@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_ASCII_DIVIDERS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Eric Botcazou The current code has relied on Original_Node to detect rewritten function calls in object declarations but that's not robust enough in the presence of function calls written in object notation. gcc/ada/ * exp_util.ads (Is_Captured_Function_Call): Declare. * exp_util.adb (Is_Captured_Function_Call): New predicate. * exp_ch3.adb (Expand_N_Object_Declaration): Use it to detect a rewritten function call as the initializing expression. * exp_ch6.adb (Expand_Simple_Function_Return): Use it to detect a rewritten function call as the returned expression. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch3.adb | 10 ++++------ gcc/ada/exp_ch6.adb | 6 +----- gcc/ada/exp_util.adb | 24 ++++++++++++++++++++++++ gcc/ada/exp_util.ads | 8 ++++++++ 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/gcc/ada/exp_ch3.adb b/gcc/ada/exp_ch3.adb index 6de5843b4ba..def63ed0513 100644 --- a/gcc/ada/exp_ch3.adb +++ b/gcc/ada/exp_ch3.adb @@ -7901,18 +7901,16 @@ package body Exp_Ch3 is -- secondary stack, then the declaration can be rewritten as -- the renaming of this dereference: - -- type Axx is access all Typ; - -- Rxx : constant Axx := Func (...)'reference; - -- Obj : Typ renames Rxx.all; + -- type Ann is access all Typ; + -- Rnn : constant Axx := Func (...)'reference; + -- Obj : Typ renames Rnn.all; -- This avoids an extra copy and, in the case where Typ needs -- finalization, a pair of Adjust/Finalize calls (see below). and then ((not Is_Library_Level_Entity (Def_Id) - and then Nkind (Expr_Q) = N_Explicit_Dereference - and then not Comes_From_Source (Expr_Q) - and then Nkind (Original_Node (Expr_Q)) = N_Function_Call + and then Is_Captured_Function_Call (Expr_Q) and then not Is_Class_Wide_Type (Typ)) -- If the initializing expression is a variable with the diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb index c026b63fcf6..0bc2559751b 100644 --- a/gcc/ada/exp_ch6.adb +++ b/gcc/ada/exp_ch6.adb @@ -6440,11 +6440,7 @@ package body Exp_Ch6 is pragma Assert (Present (Exp)); Exp_Is_Function_Call : constant Boolean := - Nkind (Exp) = N_Function_Call - or else (Nkind (Exp) = N_Explicit_Dereference - and then Is_Entity_Name (Prefix (Exp)) - and then Ekind (Entity (Prefix (Exp))) = E_Constant - and then Is_Related_To_Func_Return (Entity (Prefix (Exp)))); + Nkind (Exp) = N_Function_Call or else Is_Captured_Function_Call (Exp); Exp_Typ : constant Entity_Id := Etype (Exp); -- The type of the expression (not necessarily the same as R_Type) diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb index 5ab0d3039ca..3c68f917ca9 100644 --- a/gcc/ada/exp_util.adb +++ b/gcc/ada/exp_util.adb @@ -8160,6 +8160,30 @@ package body Exp_Util is end if; end Integer_Type_For; + ------------------------------- + -- Is_Captured_Function_Call -- + ------------------------------- + + function Is_Captured_Function_Call (N : Node_Id) return Boolean is + begin + if Nkind (N) = N_Explicit_Dereference + and then Is_Entity_Name (Prefix (N)) + and then Ekind (Entity (Prefix (N))) = E_Constant + then + declare + Value : constant Node_Id := Constant_Value (Entity (Prefix (N))); + + begin + return Present (Value) + and then Nkind (Value) = N_Reference + and then Nkind (Prefix (Value)) = N_Function_Call; + end; + + else + return False; + end if; + end Is_Captured_Function_Call; + -------------------------------------------------- -- Is_Displacement_Of_Object_Or_Function_Result -- -------------------------------------------------- diff --git a/gcc/ada/exp_util.ads b/gcc/ada/exp_util.ads index a21fb8b5c2a..0d09d259f8e 100644 --- a/gcc/ada/exp_util.ads +++ b/gcc/ada/exp_util.ads @@ -757,6 +757,14 @@ package Exp_Util is -- Return a suitable standard integer type containing at least S bits and -- of the signedness given by Uns. See also Small_Integer_Type_For. + function Is_Captured_Function_Call (N : Node_Id) return Boolean; + -- Return True if N is a captured function call, i.e. the result of calling + -- Remove_Side_Effects on an N_Function_Call node: + + -- type Ann is access all Typ; + -- Rnn : constant Ann := Func (...)'reference; + -- Rnn.all + function Is_Displacement_Of_Object_Or_Function_Result (Obj_Id : Entity_Id) return Boolean; -- Determine whether Obj_Id is a source entity that has been initialized by -- 2.34.1