From patchwork Mon Dec 12 12:07:17 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 130734 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 A7F221007D1 for ; Mon, 12 Dec 2011 23:07:42 +1100 (EST) Received: (qmail 19091 invoked by alias); 12 Dec 2011 12:07:39 -0000 Received: (qmail 19076 invoked by uid 22791); 12 Dec 2011 12:07:38 -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, 12 Dec 2011 12:07:19 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 5DD102BAFC1; Mon, 12 Dec 2011 07:07:18 -0500 (EST) 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 bIm0Wn4d1VVw; Mon, 12 Dec 2011 07:07:18 -0500 (EST) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 0EFB62BAFAA; Mon, 12 Dec 2011 07:07:18 -0500 (EST) Received: by kwai.gnat.com (Postfix, from userid 4192) id F15BC3FEE8; Mon, 12 Dec 2011 07:07:17 -0500 (EST) Date: Mon, 12 Dec 2011 07:07:17 -0500 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Ed Schonberg Subject: [Ada] Visibility in expression functions Message-ID: <20111212120717.GA12213@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 expression function is not a completion, the usage names in the expression must be determined at the point of declaration, even though the generated body is inserted at the end of the current declaration list or package to prevent early freezing. The following must be rejected with: forward_reference.ads:2:35: "F2" is undefined --- package Forward_Reference is function F1 return Boolean is (F2); -- Error: forward reference function F2 return Boolean is (True); end Forward_Reference; Tested on x86_64-pc-linux-gnu, committed on trunk 2011-12-12 Ed Schonberg * sem_ch6.adb (Analyze_Expression_Function): If the function is not a completion, pre-analyze the expression now to prevent spurious visibility on later entities. The body is inserted at the end of the current declaration list or package to prevent early freezing, but the visibility is established at the point of definition. Index: sem_ch6.adb =================================================================== --- sem_ch6.adb (revision 182230) +++ sem_ch6.adb (working copy) @@ -281,6 +281,7 @@ New_Body : Node_Id; New_Decl : Node_Id; New_Spec : Node_Id; + Ret : Node_Id; begin -- This is one of the occasions on which we transform the tree during @@ -302,15 +303,15 @@ Prev := Find_Corresponding_Spec (N); end if; + Ret := Make_Simple_Return_Statement (LocX, Expression (N)); + New_Body := Make_Subprogram_Body (Loc, Specification => New_Spec, Declarations => Empty_List, Handled_Statement_Sequence => Make_Handled_Sequence_Of_Statements (LocX, - Statements => New_List ( - Make_Simple_Return_Statement (LocX, - Expression => Expression (N))))); + Statements => New_List (Ret))); if Present (Prev) and then Ekind (Prev) = E_Generic_Function then @@ -362,10 +363,13 @@ -- To prevent premature freeze action, insert the new body at the end -- of the current declarations, or at the end of the package spec. + -- However, resolve usage names now, to prevent spurious visibility + -- on later entities. declare Decls : List_Id := List_Containing (N); Par : constant Node_Id := Parent (Decls); + Id : constant Entity_Id := Defining_Entity (New_Decl); begin if Nkind (Par) = N_Package_Specification @@ -377,6 +381,11 @@ end if; Insert_After (Last (Decls), New_Body); + Push_Scope (Id); + Install_Formals (Id); + Preanalyze_Spec_Expression (Expression (Ret), Etype (Id)); + End_Scope; + end; end if;