From patchwork Tue Jan 9 13:15:54 2024 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: 186372 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp100502dyi; Tue, 9 Jan 2024 05:19:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IEwyoPezSIWwO3sc6EFE9fWaY5ywYqhtmxLE82DaX27Dnt4xSs4h5ixkcGeC4umt4cLHYTn X-Received: by 2002:a05:620a:124f:b0:783:2af5:e3b9 with SMTP id a15-20020a05620a124f00b007832af5e3b9mr1453991qkl.147.1704806395831; Tue, 09 Jan 2024 05:19:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1704806395; cv=pass; d=google.com; s=arc-20160816; b=OK7ZZBqQ/ujyAHTC2KV+f0ARS2CiEVngN6kRkoucRBXnBSkIF7+21pAwvIBAaeZrR6 dpFJ8j4JSdDOQKHjh9ZlZ+9AlLyb1GwYzVQ2fGxIY7TvkO4Ba5MMTDCJoWQXPhTmzGu6 gRsvhgqspRp6W6mQAwU/cPXQMbipp+zp1XFW8LzeBPfC/8GVq/5X+XlxtprURB6qWK3j ixL5VSFLww/TrwaIPecj5zQeT+uY6jqNIVPVoKVDLbkfWkKk0LK35uodSQA01kE8cLZa cnTVGUZ/g/HzWErSci0mpDWThovTIvE0mk4sxNPU2Ym63jcAy3Si2s+8cHN4Es0KJAUy 8gHw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=NbSjQUD8CAzkFUMSUEMMpAqlaLxlPMgB93bpUHhvLI0=; fh=2OQ2D33Mj5i184RAM+HipkBHQbq3Jto2o2pTnCBu17E=; b=XOu64/7vTG1TH0bboBQNTaBPI2kyZIXIFORTQVlEKIK30H6qIixz7xdZYFmxkd5Mef HrfUaQ0iYel7xtywVedFJH2cs0R3yV4/sHjc4bGCQEzPLEARxcgUmG3ElnU8Zn0s3yUA i8YObQmOYeUWzELNUIIGbe011fFNXXOrFZwKmhXSqxTxowdhIeZF5LvfRlY6cbz2skmA HRQnxjvhD0KI74s0FmoIIbUNR39fNaX9w5mRYpHX3qQ3Y+CxnQk+1IQx1E/ApDzXyyc4 U2BUxOJ8gQlXVKbjRxmx86Obb9ZiFS/bDN5W7iqG5oKQlwlJBCrkxCUxUoskMca0qG9i Vnfg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b=ER4hPpRm; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id i2-20020a05620a248200b00783326ef9d3si302756qkn.707.2024.01.09.05.19.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 05:19:55 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@adacore.com header.s=google header.b=ER4hPpRm; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=adacore.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CD6053861828 for ; Tue, 9 Jan 2024 13:19:52 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id F1C963860C2C for ; Tue, 9 Jan 2024 13:15:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F1C963860C2C Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F1C963860C2C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::432 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704806159; cv=none; b=rt+Ot6D/r+H0rrHr49wLOQAIA/r7o1c2vNm7HfzZ+t7E81SrVGUkTrD8nadsWJYjj72nUgn/XlsxvbLdeZLoeMbR9LiyTQweXpIAEF9Wq+x4IQuzDW0xQQ2IO/s+7mpK599z3j9Dyor56OZphO8y0SDT81wf8IvGTvMwyCjHWXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704806159; c=relaxed/simple; bh=bi7SKPYjCm0nVmat8sm0vsfeWH2BkgyoxONW2kxKXgg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ovSjAQ5kL3+ql2cvXWt2iJK8Do5obHAzGtgBzUBzdnapAVCwNb9Gsnw0Bf2Ti82uSbv52/Djgs8e1rfjBDJ2X4fB2reYwr9WEEzetwFOjpZ1qG54LestLXpZhkewlr6Bg9O6dyKoDgTeRHQUlCzKhzI2Yrqo+XuCiEggl0KomWQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-3367f8f8cb0so2846728f8f.2 for ; Tue, 09 Jan 2024 05:15:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1704806156; x=1705410956; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=NbSjQUD8CAzkFUMSUEMMpAqlaLxlPMgB93bpUHhvLI0=; b=ER4hPpRmx7LG4Xp83yM/zQMmSsJMPGIjgssW1t2F/vHdPQptRUqEbKsXWo1jLHUwjX PQje9TJrexfbI7/p8jUbtqDMe6yo8/GhAYpUpqaa4HDhovaXMEG0BMpbzyXT0lfm6qRm oh2sB7xKlLhaFBedYcSpj6XCSxOUpsNsM+UDg9/o8OsvHlOjVKlK49A0UbVUssXByKfq OofXKuH5eXU5I3+m5hFkk2j2aun9UXiK219W0pjZjtkduAd2GoCDDszKoU9xmuW0SIym oZzepxa8T94f2En9YEFyEuWUXh3rm8EMs0/eQpga0W+n48moeNpV71b3LRa8bOQO2H3u BfdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704806156; x=1705410956; 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=NbSjQUD8CAzkFUMSUEMMpAqlaLxlPMgB93bpUHhvLI0=; b=KNeJGTVLZ2lm3IH0pA7az6sglj6dxB2q0IRRnbI1rDh2r0j+xCnQ0GolHhsX7jTyVc MkIzmoerStz8YAPnU22L+0MRBa9bH4hzqEi96A11mJOX/0eXwpUkdwmmtlBXZRSYN4Sb nOk7lhRXGsJSuIM57Gq9GMXlEkr+hb3SobEKGjtyM+WMVfkKQKUMdMy0q6XgSNrluaDb FNLkfBX5tptJjKnmuIPUzNtXBmpBfyzLUftpjsuSVpxD/TAjl5oxJCkIjLpbRWqjj362 I/gQju9lz4TejpmrvJpc1+ToK7S2npXzN8JLjoVLELByqJjyEY5Qgfm//NIJPCBiuxqz Oqjw== X-Gm-Message-State: AOJu0YyQFo47nDmG8sFIHwl/Tg1bP3GIhSMnPH3r/+Uk41t3NAlJd3MU hJcJY9DqGltb9UwJHSGyVg1E5vDRtZoFOxBc85A1/BuxcA== X-Received: by 2002:a7b:ce8f:0:b0:40e:42b0:b75d with SMTP id q15-20020a7bce8f000000b0040e42b0b75dmr2316422wmj.42.1704806155828; Tue, 09 Jan 2024 05:15:55 -0800 (PST) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id v13-20020adfe28d000000b0033662c2820bsm2385623wri.117.2024.01.09.05.15.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 05:15:55 -0800 (PST) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Gary Dismukes Subject: [COMMITTED] ada: Excess elements created for indexed aggregates with iterator_specifications Date: Tue, 9 Jan 2024 14:15:54 +0100 Message-ID: <20240109131554.744749-1-poulhies@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 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, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787619071020176049 X-GMAIL-MSGID: 1787619071020176049 From: Gary Dismukes In the case of an indexed aggregate of a container type with both Add_Unnamed and New_Indexed specified in the Aggregate aspect of the type (such as for the Vector type in Ada.Containers.Vectors), in cases where a component association is given by an iterator_specification, the compiler could end up generating a call to the New_Indexed operation rather than the Empty operation. For example, in the case of a Vector type, this could result in allocating a container of the size of the defaulted Capacity formal of the New_Vector function (with uninitialized components), and elements added in the aggregate would append to that preallocated Vector. The compiler is corrected so that the Empty function is called to initialize the implicit aggregate object, rather than the New_Indexed function. gcc/ada/ * exp_aggr.adb (Expand_Container_Aggregate): Add code to determine whether the aggregate is an indexed aggregate, setting a flag (Is_Indexed_Aggregate), which is tested to have proper separation of treatment for the Add_Unnamed (for positional aggregates) and New_Indexed (for indexed aggregates) cases. In the code generating associations for indexed aggregates, remove the code for Expressions cases entirely, since the code for indexed aggregates is governed by the presence of Component_Associations, and add an assertion that Expressions must be Empty. Also, exclude empty aggregates from entering that code. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_aggr.adb | 151 ++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 74 deletions(-) diff --git a/gcc/ada/exp_aggr.adb b/gcc/ada/exp_aggr.adb index 50063ed819e..6fceda3ceff 100644 --- a/gcc/ada/exp_aggr.adb +++ b/gcc/ada/exp_aggr.adb @@ -6606,6 +6606,8 @@ package body Exp_Aggr is Siz_Exp : Node_Id := Empty; Count_Type : Entity_Id; + Is_Indexed_Aggregate : Boolean := False; + function Aggregate_Size return Int; -- Compute number of entries in aggregate, including choices -- that cover a range or subtype, as well as iterated constructs. @@ -7042,6 +7044,35 @@ package body Exp_Aggr is ("\this will result in infinite recursion??", Parent (N)); end if; + -- Determine whether this is an indexed aggregate (see RM 4.3.5(25/5)). + + if Present (New_Indexed_Subp) then + if not Present (Add_Unnamed_Subp) then + Is_Indexed_Aggregate := True; + + else + declare + Comp_Assns : constant List_Id := Component_Associations (N); + Comp_Assn : Node_Id; + + begin + if Present (Comp_Assns) + and then not Is_Empty_List (Comp_Assns) + then + Comp_Assn := First (Comp_Assns); + + if Nkind (Comp_Assn) = N_Component_Association + or else + (Nkind (Comp_Assn) = N_Iterated_Component_Association + and then Present (Defining_Identifier (Comp_Assn))) + then + Is_Indexed_Aggregate := True; + end if; + end if; + end; + end if; + end if; + --------------------------- -- Positional aggregate -- --------------------------- @@ -7068,12 +7099,11 @@ package body Exp_Aggr is Next (Comp); end loop; end; - end if; -- Indexed aggregates are handled below. Unnamed aggregates -- such as sets may include iterated component associations. - if No (New_Indexed_Subp) then + elsif not Is_Indexed_Aggregate then Comp := First (Component_Associations (N)); while Present (Comp) loop if Nkind (Comp) = N_Iterated_Component_Association then @@ -7128,15 +7158,16 @@ package body Exp_Aggr is -- Indexed_Aggregate -- ----------------------- - -- For an indexed aggregate there must be an Assigned_Indexeed + -- For an indexed aggregate there must be an Assigned_Indexed -- subprogram. Note that unlike array aggregates, a container -- aggregate must be fully positional or fully indexed. In the -- first case the expansion has already taken place. -- TBA: the keys for an indexed aggregate must provide a dense -- range with no repetitions. - if Present (Assign_Indexed_Subp) + if Is_Indexed_Aggregate and then Present (Component_Associations (N)) + and then not Is_Empty_List (Component_Associations (N)) then declare Insert : constant Entity_Id := Entity (Assign_Indexed_Subp); @@ -7153,7 +7184,6 @@ package body Exp_Aggr is Comp : Node_Id; Index : Node_Id; - Pos : Int := 0; Stat : Node_Id; Key : Node_Id; @@ -7196,6 +7226,8 @@ package body Exp_Aggr is end Expand_Range_Component; begin + pragma Assert (not Present (Expressions (N))); + if Siz > 0 then -- Modify the call to the constructor to allocate the @@ -7216,89 +7248,60 @@ package body Exp_Aggr is Index))); end if; - if Present (Expressions (N)) then - Comp := First (Expressions (N)); - - while Present (Comp) loop - - -- Compute index position for successive components - -- in the list of expressions, and use the indexed - -- assignment procedure for each. - - Index := Make_Op_Add (Loc, - Left_Opnd => Type_Low_Bound (Index_Type), - Right_Opnd => Make_Integer_Literal (Loc, Pos)); - - Stat := Make_Procedure_Call_Statement (Loc, - Name => New_Occurrence_Of (Insert, Loc), - Parameter_Associations => - New_List (New_Occurrence_Of (Temp, Loc), - Index, - New_Copy_Tree (Comp))); - - Pos := Pos + 1; - - Append (Stat, Aggr_Code); - Next (Comp); - end loop; - end if; - - if Present (Component_Associations (N)) then - Comp := First (Component_Associations (N)); - - -- The choice may be a static value, or a range with - -- static bounds. + Comp := First (Component_Associations (N)); - while Present (Comp) loop - if Nkind (Comp) = N_Component_Association then - Key := First (Choices (Comp)); - while Present (Key) loop + -- The choice may be a static value, or a range with + -- static bounds. - -- If the expression is a box, the corresponding - -- component (s) is left uninitialized. + while Present (Comp) loop + if Nkind (Comp) = N_Component_Association then + Key := First (Choices (Comp)); + while Present (Key) loop - if Box_Present (Comp) then - goto Next_Key; + -- If the expression is a box, the corresponding + -- component (s) is left uninitialized. - elsif Nkind (Key) = N_Range then + if Box_Present (Comp) then + goto Next_Key; - -- Create loop for tne specified range, - -- with copies of the expression. + elsif Nkind (Key) = N_Range then - Stat := - Expand_Range_Component (Key, Expression (Comp)); + -- Create loop for tne specified range, + -- with copies of the expression. - else - Stat := Make_Procedure_Call_Statement (Loc, - Name => New_Occurrence_Of - (Entity (Assign_Indexed_Subp), Loc), - Parameter_Associations => - New_List (New_Occurrence_Of (Temp, Loc), - New_Copy_Tree (Key), - New_Copy_Tree (Expression (Comp)))); - end if; + Stat := + Expand_Range_Component (Key, Expression (Comp)); - Append (Stat, Aggr_Code); + else + Stat := Make_Procedure_Call_Statement (Loc, + Name => New_Occurrence_Of + (Entity (Assign_Indexed_Subp), Loc), + Parameter_Associations => + New_List (New_Occurrence_Of (Temp, Loc), + New_Copy_Tree (Key), + New_Copy_Tree (Expression (Comp)))); + end if; - <> - Next (Key); - end loop; + Append (Stat, Aggr_Code); - else - -- Iterated component association. Discard - -- positional insertion procedure. + <> + Next (Key); + end loop; - if No (Iterator_Specification (Comp)) then - Add_Named_Subp := Assign_Indexed_Subp; - Add_Unnamed_Subp := Empty; - end if; + else + -- Iterated component association. Discard + -- positional insertion procedure. - Expand_Iterated_Component (Comp); + if No (Iterator_Specification (Comp)) then + Add_Named_Subp := Assign_Indexed_Subp; + Add_Unnamed_Subp := Empty; end if; - Next (Comp); - end loop; - end if; + Expand_Iterated_Component (Comp); + end if; + + Next (Comp); + end loop; end; end if;