From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 2BE4A38582A1 for ; Thu, 5 Jan 2023 14:38:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2BE4A38582A1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wm1-x32e.google.com with SMTP id m26-20020a05600c3b1a00b003d9811fcaafso1462201wms.5 for ; Thu, 05 Jan 2023 06:38:49 -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=pnRML+5gjoo25EPLkdR6X6sxUgRUCblPQIyYPtEMUYM=; b=at4SNvAioiM/OemKJlAtRm1BBTbDEuBfnnJ0UvT9fq+rqr6rDL3NWePHM4HELN03To f1C7kVai6VN46jesTJ8EATa5vwcGjmUL/TPyCSjCGODpoCl3W/Pce5AosrTQV0tw/wQ6 wYmR1ow940+yhjEynfCd7uJXWDOGoAx/MbI4EMB1VNwmFhyhns+xF8t8WgT7QWpkjBk3 K/pB6eCEpiIgb2fZiTMhwSHNQPehi3x8IgvFSNUBe3IZ7HwNxsmWdIdGCXKAoaZDqBWR fD+0Tx3xxE7sQhMyreerSsYZVRnpPu+6rMc8+YfmFwyM21xIK8Kbhq1CifQ0nVgYv/pc Kvow== 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=pnRML+5gjoo25EPLkdR6X6sxUgRUCblPQIyYPtEMUYM=; b=1Qm9xTK8QJvnzXTL5vk7o5mrplA4MfROFCLshJpUUBp4mnBzYywksGw50ogvw+aFjB BFOajx6PKR39FBlFOa58FOHVVShMTL5pmkeVOzYbWhaJcGm84XWPOG52NLpsljWI67g/ Hb5nvnSY9vrJ3yrcyynqasJzaM/dj1k48IBa4C4EdVG4Hb295LBewHZQEbvTbaSeZqVu m//TnetFRSrRakTL7CHLNVb/oWjV1YecbxRKPkZPkQ3qvjXRPC0U39i4qA8kJMGK56c4 GsCUf/EIaRNqbKWC/Ya2/1iZUCpMkrr0gt2hOSEPMxOLPBH+y7fzNuB0rjhUlfSINsPP 5UvA== X-Gm-Message-State: AFqh2krkMx5Mn7YeJuVzCrrH2baH09KGpIp2nQrHZJRS17WFnBfg1vTr R908mlf4N3a/3yS0WBABeZltEwwlQcEqhwzhNhc= X-Google-Smtp-Source: AMrXdXvZqWHn+BS0XAJBbu0ia6iovxhLDPbvIvJ69gtp7bg+8fyMMYQ03VQTY11qmlGpLoaUAz6lnw== X-Received: by 2002:a05:600c:1819:b0:3cf:8d51:fae1 with SMTP id n25-20020a05600c181900b003cf8d51fae1mr36899354wmp.0.1672929527387; Thu, 05 Jan 2023 06:38:47 -0800 (PST) Received: from poulhies-Precision-5550.lan (static-176-191-105-132.ftth.abo.bbox.fr. [176.191.105.132]) by smtp.gmail.com with ESMTPSA id j19-20020a05600c301300b003d6b71c0c92sm2768821wmh.45.2023.01.05.06.38.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 06:38:46 -0800 (PST) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Fix finalization issues in extended return statements Date: Thu, 5 Jan 2023 15:38:44 +0100 Message-Id: <20230105143844.155367-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.6 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 List-Id: From: Eric Botcazou The first issue pertains to return objects of (class-wide) interface types, which need to be adjusted if the type is not inherently limited. The second issue is for return objects of non-class-wide types that are initialized by a function call, which can use a direct renaming only if the object doing the capture of the function call is flagged by Is_Related_To_Func_Return. The third one is that, in the second case, we may need to reassign the tag. gcc/ada/ * exp_ch3.adb (Expand_N_Object_Declaration): For a special return object of an interface type that is not inherently limited, make a call to the Adjust primitive after doing the copy. For a special return object of a non-class-wide type initialized by a function call, use a direct renaming only if the object doing the capture is flagged by Is_Related_To_Func_Return. For a special return object using a direct renaming, reassign the tag, if need be. * exp_ch6.adb (Expand_Simple_Function_Return): Fix comment. * exp_util.adb (Is_Related_To_Func_Return): Accept both regular and renaming object declarations for return objects. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch3.adb | 34 ++++++++++++++++++++++++++++++++-- gcc/ada/exp_ch6.adb | 2 +- gcc/ada/exp_util.adb | 3 ++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/gcc/ada/exp_ch3.adb b/gcc/ada/exp_ch3.adb index def63ed0513..7dbf82671aa 100644 --- a/gcc/ada/exp_ch3.adb +++ b/gcc/ada/exp_ch3.adb @@ -7480,7 +7480,19 @@ package body Exp_Ch3 is -- creating the object (via allocator) and initializing it. if Is_Special_Return_Object (Def_Id) then - null; + + -- If the type needs finalization and is not inherently + -- limited, then the target is adjusted after the copy + -- and attached to the finalization list. + + if Needs_Finalization (Typ) + and then not Is_Limited_View (Typ) + then + Adj_Call := + Make_Adjust_Call ( + Obj_Ref => New_Occurrence_Of (Def_Id, Loc), + Typ => Base_Typ); + end if; elsif Tagged_Type_Expansion then declare @@ -7908,9 +7920,20 @@ package body Exp_Ch3 is -- This avoids an extra copy and, in the case where Typ needs -- finalization, a pair of Adjust/Finalize calls (see below). + -- However, in the case of a special return object, we need to + -- make sure that the object Rnn 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 this and Build_Finalizer would + -- finalize it prematurely (see Expand_Simple_Function_Return + -- for the same test in the case of a simple return). + and then ((not Is_Library_Level_Entity (Def_Id) and then Is_Captured_Function_Call (Expr_Q) + and then (not Is_Special_Return_Object (Def_Id) + or else Is_Related_To_Func_Return + (Entity (Prefix (Expr_Q)))) and then not Is_Class_Wide_Type (Typ)) -- If the initializing expression is a variable with the @@ -8554,7 +8577,8 @@ package body Exp_Ch3 is -- If we can rename the initialization expression, we need to make sure -- that we use the proper type in the case of a return object that lives - -- on the secondary stack. See other cases below for a similar handling. + -- on the secondary stack (see other cases below for a similar handling) + -- and that the tag is assigned in the case of any return object. elsif Rewrite_As_Renaming then if Is_Secondary_Stack_Return_Object (Def_Id) then @@ -8577,6 +8601,12 @@ package body Exp_Ch3 is end; end if; + if Is_Special_Return_Object (Def_Id) + and then Present (Tag_Assign) + then + Insert_Action_After (Init_After, Tag_Assign); + end if; + -- If this is the return object of a function returning on the secondary -- stack, convert the declaration to a renaming of the dereference of ah -- allocator for the secondary stack. diff --git a/gcc/ada/exp_ch6.adb b/gcc/ada/exp_ch6.adb index db1fd1d172b..b97d69b81b6 100644 --- a/gcc/ada/exp_ch6.adb +++ b/gcc/ada/exp_ch6.adb @@ -6449,7 +6449,7 @@ package body Exp_Ch6 is -- 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. + -- because of this 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) diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb index 3c68f917ca9..c8829cac85a 100644 --- a/gcc/ada/exp_util.adb +++ b/gcc/ada/exp_util.adb @@ -9196,7 +9196,8 @@ package body Exp_Util is and then Nkind (Unqual_Conv (Expr)) = N_Explicit_Dereference and then (Nkind (Parent (Expr)) = N_Simple_Return_Statement or else - (Nkind (Parent (Expr)) = N_Object_Renaming_Declaration + (Nkind (Parent (Expr)) in N_Object_Declaration + | N_Object_Renaming_Declaration and then Is_Return_Object (Defining_Entity (Parent (Expr))))); end Is_Related_To_Func_Return; -- 2.34.1