From patchwork Tue Jan 3 09:36:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 38339 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4525166wrt; Tue, 3 Jan 2023 01:41:30 -0800 (PST) X-Google-Smtp-Source: AMrXdXuNQU9EOF437dmAhdEswTlHZyRik7tIwoKQVhASuS0PawnH9HX88Jz2Ebel0bz2zVxAiVtq X-Received: by 2002:a17:906:bc1:b0:7fd:eb9e:6fd5 with SMTP id y1-20020a1709060bc100b007fdeb9e6fd5mr36729181ejg.45.1672738890240; Tue, 03 Jan 2023 01:41:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672738890; cv=none; d=google.com; s=arc-20160816; b=PqHNOjX9OdQ0Bd1u4QfTEt209pIvwkmWORNNhQl1OugDYq18U9AxatoKNNjpG99HGN StafGrTJuqUpFomYiEGfsC6BYbAaOn0+ecEDpKLwN/BjhZvjmyBHIMHB9b7mt322r8Ac IvEOWDpI84MUVxa1q8FtlRiJb8++vvafMLIWkW1Ct8KzilRT+rrvRBKInPzjwtFMLsQH bzL5bORYEasIFdjEbW0OGBzjAfH0F2xJ0wH+9aWNUaqUua/wtfkKHepChQXhOvszvRro fG0T10sI7XUnPL77ok5HOknNmv2khGG/rrUdSR4FEQ8P1fAaJGEFNQUCDnb0cR45RrAU x56w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:delivered-to:dkim-signature:dkim-filter; bh=OFkR/ThZQb8Ul5BOKDEU0tfwywoBuxWWbIsn2m5dc2o=; b=PIV6Qppt7RQ7Z+3QFpyddfOA8Er3hH/ytdUA0tDammCRzgyuKoI6hYnk6Z4/OwK5v0 Tm/GE/2G9dtSchOqTqAUFRxKlVj0s/ga+tze8FIbGgVxV1W+fgUym465Q7oBjwEbEHyx nkD9JxWy9gQ7g1p+4D0uyTxHnNSpVdskIN1bVuUEDpRUKLNAbVTuvqo7BvUYVbiXgEKY muxj8sjnaTpj30/M0JWjDWTAGX+PeuNcTsytgNi0KnesNt/+wMgwdLeayCmMFDfvUHkU Cm791noj7pm1+nQd/FEBeMjhvXtRi2uLYbEvSiUA+qEC502u5mFu05oQJbJfLYyykB1H vQdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=aS9qWHIe; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dr19-20020a170907721300b0078dd12d0a9fsi26745994ejc.548.2023.01.03.01.41.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 01:41:30 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=aS9qWHIe; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B45B2382FCB9 for ; Tue, 3 Jan 2023 09:38:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B45B2382FCB9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672738738; bh=OFkR/ThZQb8Ul5BOKDEU0tfwywoBuxWWbIsn2m5dc2o=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=aS9qWHIeAPj6pjvSjmnzgEg3MkbvpJ4SOdMsM8pou/Qk1VloaekVinNj+r/zg45lQ 1mptxTJX05XcMq7eya9nFGKY0ciNvn6wRsnGmObMpcLDZ9eJF5Koz35r8JE2i6BUrk hJdW+m3UR3PdrZX8o3zzoLDtm3v2R2MffJaT1q4A= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 18B6338493DB for ; Tue, 3 Jan 2023 09:36:10 +0000 (GMT) Received: by mail-wr1-x430.google.com with SMTP id t15so19982955wro.9 for ; Tue, 03 Jan 2023 01:36:10 -0800 (PST) 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=OFkR/ThZQb8Ul5BOKDEU0tfwywoBuxWWbIsn2m5dc2o=; b=Wy6Xd5EjYz97AYzJrit+NRVdHp1uG0h1yEHSruYJaW2GsKOHUp9I3vr4sFwD8uKTgr c5Lc0JO6jP5+mBSx8Fzej7CBKkG459BmHTb8qMg5V2SYDbLsqckkG1YpgNqvRJYT6Yb+ CqgPefussf1kkWulOySPCBa9/8tuj0aEaTNGeNpnhxtGOOJVFA9s1T26FiODlUaSFJl4 j7PO5zTtAFGUu3s+1WfR3xP5wbrIviaAtEW8CGluWKZ4eq9+jQCldBO0iFs7nSWri0Er DEnR05HdhWa++hd9wY62m4lo09JxFzStzOxaKs+QMMlZOPior6E+Gfgyx51sapT85bNq OHRQ== X-Gm-Message-State: AFqh2koBZdHfQtWkJQNjogcApT1OFdS7jA5JJmhDxnoogTda37+putKb O+ZdPMWx0KFHLdUBQzMvSYGC7Azr+2ycYJ1Nlmg= X-Received: by 2002:a5d:660c:0:b0:273:92ee:a0a with SMTP id n12-20020a5d660c000000b0027392ee0a0amr21863922wru.50.1672738568935; Tue, 03 Jan 2023 01:36:08 -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 e10-20020a5d594a000000b0028663fc8f4csm17864083wri.30.2023.01.03.01.36.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 01:36:08 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Fix premature finalization of return temporary Date: Tue, 3 Jan 2023 10:36:06 +0100 Message-Id: <20230103093606.76755-1-poulhies@adacore.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753993854254573128?= X-GMAIL-MSGID: =?utf-8?q?1753993854254573128?= From: Eric Botcazou Various parts of the expander and the code generator must have a consistent view on which temporaries generated for return statements must be finalized because they are regular temporaries, and which ones must not be since they are allocated on the return stack directly. The Is_Related_To_Func_Return predicate is used for this purpose and needs to be tested consistently. gcc/ada/ * exp_ch6.adb (Expand_Simple_Function_Return): Make sure that a captured function call also verifies Is_Related_To_Func_Return. Do not generate an actual subtype for special return objects. * exp_util.ads (Is_Related_To_Func_Return): Add commentary. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch6.adb | 16 ++++++++++++++-- gcc/ada/exp_util.ads | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb index d90ee41712e..db1fd1d172b 100644 --- a/gcc/ada/exp_ch6.adb +++ b/gcc/ada/exp_ch6.adb @@ -6435,12 +6435,21 @@ package body Exp_Ch6 is -- The result type of the function Utyp : constant Entity_Id := Underlying_Type (R_Type); + -- The underlying result type of the function Exp : Node_Id := Expression (N); pragma Assert (Present (Exp)); Exp_Is_Function_Call : constant Boolean := - Nkind (Exp) = N_Function_Call or else Is_Captured_Function_Call (Exp); + Nkind (Exp) = N_Function_Call + or else + (Is_Captured_Function_Call (Exp) + and then Is_Related_To_Func_Return (Entity (Prefix (Exp)))); + -- If the expression is a captured function call, then we need to make + -- sure that the object doing the capture is properly recognized by the + -- Is_Related_To_Func_Return predicate; otherwise, if it is of a type + -- that needs finalization, Requires_Cleanup_Actions would return true + -- because of it and Build_Finalizer would finalize it prematurely. Exp_Typ : constant Entity_Id := Etype (Exp); -- The type of the expression (not necessarily the same as R_Type) @@ -6624,7 +6633,8 @@ package body Exp_Ch6 is -- size. We create an actual subtype for this purpose. However we -- need not do it if the expression is a function call since this -- will be done in the called function and doing it here too would - -- cause a temporary with maximum size to be created. + -- cause a temporary with maximum size to be created. Likewise for + -- a special return object, since there is no copy in this case. declare Ubt : constant Entity_Id := Underlying_Type (Base_Type (Exp_Typ)); @@ -6633,6 +6643,8 @@ package body Exp_Ch6 is begin if not Exp_Is_Function_Call + and then not (Is_Entity_Name (Exp) + and then Is_Special_Return_Object (Entity (Exp))) and then Has_Defaulted_Discriminants (Ubt) and then not Is_Constrained (Ubt) and then not Has_Unchecked_Union (Ubt) diff --git a/gcc/ada/exp_util.ads b/gcc/ada/exp_util.ads index 0d09d259f8e..b770d029cd9 100644 --- a/gcc/ada/exp_util.ads +++ b/gcc/ada/exp_util.ads @@ -825,6 +825,10 @@ package Exp_Util is -- Determine whether object Id is related to an expanded return statement. -- The case concerned is "return Id.all;". + -- This is effectively used to determine which temporaries generated for + -- return statements must be finalized because they are regular temporaries + -- and which ones must not be since they are allocated on the return stack. + -- WARNING: There is a matching C declaration of this subprogram in fe.h function Is_Renamed_Object (N : Node_Id) return Boolean;