From patchwork Mon Jul 8 07:50:57 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 257481 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]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 856F82C0296 for ; Mon, 8 Jul 2013 17:51:10 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=GYP2WR58yFh/lo6aNKW0+Nbb3/0zLGCxSOqdWo1juXThQIyIxS wxe0odR60YL2FbDFEGaiFb6E/czUtf5XVl4ouo5ltEq7EXEuxxZmUrh/bVFW/ZER aTD4LCHcmw9t4NvlUZRU1AbUp+ID0Oggp2dcumBulPrizIUxR4uLOIWNM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=VXcCtZZiciPggAAdCZKp5YUskLI=; b=rTLUZl5ghpBqyQBr1QUN VE39YwL3AnvQDRCJCFQijL0B8CWeoDF1uQ0rQ4gZB+GPb7sa7H8EoUlr2SddCFoC wdOobxqEiBcjM5iMbRNd8a72VRH7pJ3gzPKm4l5s+PsDclMbXy3wZoBEEds7IYDa 8gV9OKEe/dkjSdsuj5U0Y98= Received: (qmail 17390 invoked by alias); 8 Jul 2013 07:51:04 -0000 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 Received: (qmail 17359 invoked by uid 89); 8 Jul 2013 07:50:59 -0000 X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO autolearn=ham version=3.3.1 Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Mon, 08 Jul 2013 07:50:58 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 491B31C676F; Mon, 8 Jul 2013 03:50:57 -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 LYJpAM29F4PZ; Mon, 8 Jul 2013 03:50:57 -0400 (EDT) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 301291C676A; Mon, 8 Jul 2013 03:50:57 -0400 (EDT) Received: by kwai.gnat.com (Postfix, from userid 4192) id 2F7E43FB31; Mon, 8 Jul 2013 03:50:57 -0400 (EDT) Date: Mon, 8 Jul 2013 03:50:57 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] The GNAT attribute 'Img can be renamed as a function. Message-ID: <20130708075057.GA1580@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Found: No This patch treats the GNAT-specific 'Img attribute as a callable entity which therefore can be renamed as a function. The prefix of the attribute reference is an object rather than a subtype, and it is not evaluated at the point of the renaming declaration. Executing the following : gnatmake -q inst inst must yield: 12345 456 F2 = 456, Flag = TRUE F2 = 789, Flag = FALSE --- with Text_IO; use Text_IO; procedure Inst is generic with function F return String; procedure Gen; procedure Gen is begin Put_Line (F); end Gen; V : Integer; procedure Inst_Img is new Gen (V'Img); Table : array (Boolean) of Integer := (123, 456); Flag : Boolean := False; function F2 return String; function F2 return String renames Table(Flag)'Img; begin V := 12345; Inst_Img; Table (False) := 789; Flag := True; Put_Line (Table (Flag)'Img); Put_Line ("F2 = " & F2 & ", Flag = " & Boolean'Image (Flag)); Flag := False; Put_Line ("F2 = " & F2 & ", Flag = " & Boolean'Image (Flag)); end Inst; Tested on x86_64-pc-linux-gnu, committed on trunk 2013-07-08 Ed Schonberg * sem_ch8.adb (Attribute_Renaming): Treat 'Img as an attribute that can be renamed as a function. Index: sem_ch8.adb =================================================================== --- sem_ch8.adb (revision 200757) +++ sem_ch8.adb (working copy) @@ -3318,12 +3318,14 @@ -- This procedure is called in the context of subprogram renaming, and -- thus the attribute must be one that is a subprogram. All of those - -- have at least one formal parameter, with the singular exception of - -- AST_Entry (which is a real oddity, it is odd that this can be renamed - -- at all!) + -- have at least one formal parameter, with the exceptions of AST_Entry + -- (which is a real oddity, it is odd that this can be renamed at all!) + -- and the GNAT attribute 'Img, which GNAT treats as renameable. if not Is_Non_Empty_List (Parameter_Specifications (Spec)) then - if Aname /= Name_AST_Entry then + if Aname /= Name_AST_Entry + and then Aname /= Name_Img + then Error_Msg_N ("subprogram renaming an attribute must have formals", N); return; @@ -3493,11 +3495,21 @@ and then Etype (Nam) /= RTE (RE_AST_Handler) then declare - P : constant Entity_Id := Prefix (Nam); + P : constant Node_Id := Prefix (Nam); begin - Find_Type (P); + -- The prefix of 'Img is an object that is evaluated for + -- each call of the function that renames it. + if Aname = Name_Img then + Preanalyze_And_Resolve (P); + + -- For all other attribute renamings, the prefix is a subtype. + + else + Find_Type (P); + end if; + if Is_Tagged_Type (Etype (P)) then Ensure_Freeze_Node (Etype (P)); Append_Freeze_Action (Etype (P), Body_Node);