From patchwork Mon Aug 29 14:07:33 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 112060 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 C56C2B6F95 for ; Tue, 30 Aug 2011 00:07:53 +1000 (EST) Received: (qmail 4416 invoked by alias); 29 Aug 2011 14:07:50 -0000 Received: (qmail 4397 invoked by uid 22791); 29 Aug 2011 14:07:48 -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 14:07:35 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 2F3BC2BB14A; Mon, 29 Aug 2011 10:07:34 -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 lFjaeS96ovTr; Mon, 29 Aug 2011 10:07:34 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 9448A2BB14B; Mon, 29 Aug 2011 10:07:33 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id 9264F92A55; Mon, 29 Aug 2011 10:07:33 -0400 (EDT) Date: Mon, 29 Aug 2011 10:07:33 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Iterations over derived containers Message-ID: <20110829140733.GA15514@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 If the domain of iteration is a derived container type, the aspect Default_ Iterator is inherited. As for other calls to inherited operations, the actual must be view-converted to the type of the formal to be a valid argument. The following must compile quietly: gcc -c -gnat12 repro.adb --- with Ada.Text_IO; use Ada.Text_IO; with Ada.Containers.Vectors; procedure Repro is package T_Vectors is new Ada.Containers.Vectors (Index_Type => Positive, Element_Type => Integer); type T_Vector is new T_Vectors.Vector with null record; V : T_Vector; begin V.Append (1); V.Append (2); for C in iterate (V) loop Put_Line ("here"); end loop; for E of V loop Put_Line ("here"); end loop; end Repro; Tested on x86_64-pc-linux-gnu, committed on trunk 2011-08-29 Ed Schonberg * exp_ch5.adb (Expand_Iterator_Loop): Handle properly a loop over a container of a derived type. Index: exp_ch5.adb =================================================================== --- exp_ch5.adb (revision 178236) +++ exp_ch5.adb (working copy) @@ -2952,9 +2952,12 @@ if Of_Present (I_Spec) then declare - Default_Iter : constant Entity_Id := - Find_Aspect (Etype (Container), Aspect_Default_Iterator); - Ent : Entity_Id; + Default_Iter : constant Entity_Id := + Entity ( + Find_Aspect + (Etype (Container), Aspect_Default_Iterator)); + Container_Arg : Node_Id; + Ent : Entity_Id; begin Cursor := Make_Temporary (Loc, 'I'); @@ -2963,23 +2966,39 @@ null; else - Iter_Type := - Etype - (Find_Aspect - (Etype (Container), Aspect_Default_Iterator)); + Iter_Type := Etype (Default_Iter); -- Rewrite domain of iteration as a call to the default - -- iterator for the container type. + -- iterator for the container type. If the container is + -- a derived type and the aspect is inherited, convert + -- container to parent type. The Cursor type is also + -- inherited from the scope of the parent. + if Base_Type (Etype (Container)) = + Base_Type (Etype (First_Formal (Default_Iter))) + then + Container_Arg := New_Copy_Tree (Container); + + else + Pack := Scope (Default_Iter); + + Container_Arg := + Make_Type_Conversion (Loc, + Subtype_Mark => + New_Occurrence_Of ( + Etype (First_Formal (Default_Iter)), Loc), + Expression => New_Copy_Tree (Container)); + end if; + Rewrite (Name (I_Spec), Make_Function_Call (Loc, - Name => Default_Iter, + Name => New_Occurrence_Of (Default_Iter, Loc), Parameter_Associations => - New_List (Relocate_Node (Name (I_Spec))))); + New_List (Container_Arg))); Analyze_And_Resolve (Name (I_Spec)); end if; - -- Find cursor type in container package. + -- Find cursor type in proper container package. Ent := First_Entity (Pack); while Present (Ent) loop