From patchwork Mon Aug 29 13:55:20 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 112058 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 7E12CB6F97 for ; Mon, 29 Aug 2011 23:55:51 +1000 (EST) Received: (qmail 30976 invoked by alias); 29 Aug 2011 13:55:47 -0000 Received: (qmail 30967 invoked by uid 22791); 29 Aug 2011 13:55:45 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 29 Aug 2011 13:55:21 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 71B0B2BB064; Mon, 29 Aug 2011 09:55:20 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 2ZqdBPeEZkOv; Mon, 29 Aug 2011 09:55:20 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 50D222BB049; Mon, 29 Aug 2011 09:55:20 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id 4EEA692A55; Mon, 29 Aug 2011 09:55:20 -0400 (EDT) Date: Mon, 29 Aug 2011 09:55:20 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Javier Miranda Subject: [Ada] Crash processing nested aggregate in constant declaration Message-ID: <20110829135520.GA21542@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org The compiler may crash processing a constant object declaration whose expression is a nested aggregate. After this patch the following test compiles without errors. package Test_Nested_Aggregate is type T_Comp (Validity : Boolean := False) is record null; end record; type T_Root is abstract tagged record Comp_1 : Natural; Comp_2 : T_Comp; end record; type T_Deriv is new T_Root with record null; end record; K_Value : constant T_Deriv := (Comp_1 => 0, Comp_2 => (Validity => false) ); procedure Dummy; end; package body Test_Nested_Aggregate is procedure Dummy is begin null; end; end; Command: gcc -c test_nested_aggregate.adb Tested on x86_64-pc-linux-gnu, committed on trunk 2011-08-29 Javier Miranda * exp_aggr.adb (Expand_Record_Aggregate): Use the top-level enclosing aggregate to take a consistent decision on the need to convert into assignments aggregates that initialize constant objects. Index: exp_aggr.adb =================================================================== --- exp_aggr.adb (revision 178156) +++ exp_aggr.adb (working copy) @@ -5099,6 +5099,16 @@ -- semantics of Ada complicate the analysis and lead to anomalies in -- the gcc back-end if the aggregate is not expanded into assignments. + function Has_Visible_Private_Ancestor (Id : E) return Boolean; + -- If any ancestor of the current type is private, the aggregate + -- cannot be built in place. We canot rely on Has_Private_Ancestor, + -- because it will not be set when type and its parent are in the + -- same scope, and the parent component needs expansion. + + function Top_Level_Aggregate (N : Node_Id) return Node_Id; + -- For nested aggregates return the ultimate enclosing aggregate; for + -- non-nested aggregates return N. + ---------------------------------- -- Component_Not_OK_For_Backend -- ---------------------------------- @@ -5178,18 +5188,6 @@ return False; end Component_Not_OK_For_Backend; - -- Remaining Expand_Record_Aggregate variables - - Tag_Value : Node_Id; - Comp : Entity_Id; - New_Comp : Node_Id; - - function Has_Visible_Private_Ancestor (Id : E) return Boolean; - -- If any ancestor of the current type is private, the aggregate - -- cannot be built in place. We canot rely on Has_Private_Ancestor, - -- because it will not be set when type and its parent are in the - -- same scope, and the parent component needs expansion. - ----------------------------------- -- Has_Visible_Private_Ancestor -- ----------------------------------- @@ -5197,6 +5195,7 @@ function Has_Visible_Private_Ancestor (Id : E) return Boolean is R : constant Entity_Id := Root_Type (Id); T1 : Entity_Id := Id; + begin loop if Is_Private_Type (T1) then @@ -5211,6 +5210,31 @@ end loop; end Has_Visible_Private_Ancestor; + ------------------------- + -- Top_Level_Aggregate -- + ------------------------- + + function Top_Level_Aggregate (N : Node_Id) return Node_Id is + Aggr : Node_Id := N; + + begin + while Present (Parent (Aggr)) + and then Nkind_In (Parent (Aggr), N_Component_Association, + N_Aggregate) + loop + Aggr := Parent (Aggr); + end loop; + + return Aggr; + end Top_Level_Aggregate; + + -- Local variables + + Top_Level_Aggr : constant Node_Id := Top_Level_Aggregate (N); + Tag_Value : Node_Id; + Comp : Entity_Id; + New_Comp : Node_Id; + -- Start of processing for Expand_Record_Aggregate begin @@ -5317,8 +5341,8 @@ elsif Has_Mutable_Components (Typ) and then - (Nkind (Parent (N)) /= N_Object_Declaration - or else not Constant_Present (Parent (N))) + (Nkind (Parent (Top_Level_Aggr)) /= N_Object_Declaration + or else not Constant_Present (Parent (Top_Level_Aggr))) then Convert_To_Assignments (N, Typ);