From patchwork Sat Mar 16 18:54:04 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 228239 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 F39C72C00A6 for ; Sun, 17 Mar 2013 05:54:22 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1364064864; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Message-ID:Date:From:User-Agent:MIME-Version:To:Subject: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=N7OuM+P 01wlGIQEVykFSD27yHHI=; b=Q+XZthE2iaGBbCV18gLsVvDUXrlkw13fSxSot6l o1TyyP5fOY3K0f2DphWQLBVVkSpCgynrhjB99ACTgYgorp/68TV1ch9Lzf0VuEJE jG5yGmfBbyqiilzRmdbsvgtqYMHGLrnx9dH3iWRzVNyYAg1YLqvZyJsiuWIUW5ml UTB0= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=UyMmFuT53iFX5LwBH22UoLpgR2Lcp3hyt87si6embIA7Mr0sKtRxE2w57imSHK 91R8UJiG2F+uSQOXNi48vq7zeNXSV5BVfp8vgBdTJEnaeezK8Zr3l+XY7j1tUkmJ DQBWoo5bf3nALn9TInbc/mN+qaGeBcaEHpnCHQRccHU2c=; Received: (qmail 22747 invoked by alias); 16 Mar 2013 18:54:12 -0000 Received: (qmail 22737 invoked by uid 22791); 16 Mar 2013 18:54:10 -0000 X-SWARE-Spam-Status: No, hits=-7.6 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, RP_MATCHES_RCVD, SPF_HELO_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 16 Mar 2013 18:54:05 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r2GIs4f2017348 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sat, 16 Mar 2013 14:54:05 -0400 Received: from [10.3.113.72] (ovpn-113-72.phx2.redhat.com [10.3.113.72]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r2GIs4P6016667 for ; Sat, 16 Mar 2013 14:54:04 -0400 Message-ID: <5144BFCC.2090707@redhat.com> Date: Sat, 16 Mar 2013 14:54:04 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko/20100101 Thunderbird/21.0a2 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/55240 (local variable and nsdmi) 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 In an NSDMI of a local class, current_function_decl is the enclosing function, but we still can't refer to local variables of that function. Tested x86_64-pc-linux-gnu, applying to trunk. commit 00297d25d51a02b5aba605ece2db8dd1a44c2145 Author: Jason Merrill Date: Wed Feb 13 17:29:38 2013 -0500 PR c++/55240 * parser.c (parsing_nsdmi): New. * semantics.c (outer_automatic_var_p): Check it. (finish_id_expression): Likewise. * cp-tree.h: Declare it. diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index c3b2aec..d9496d2 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -4259,6 +4259,7 @@ extern int comparing_specializations; extern int cp_unevaluated_operand; extern tree cp_convert_range_for (tree, tree, tree); +extern bool parsing_nsdmi (void); /* in pt.c */ diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index df3155f..b45daae 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -16937,6 +16937,19 @@ inject_this_parameter (tree ctype, cp_cv_quals quals) current_class_ptr = this_parm; } +/* Return true iff our current scope is a non-static data member + initializer. */ + +bool +parsing_nsdmi (void) +{ + /* We recognize NSDMI context by the context-less 'this' pointer set up + by the function above. */ + if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE) + return true; + return false; +} + /* Parse a late-specified return type, if any. This is not a separate non-terminal, but part of a function declarator, which looks like diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 3c76bad..efe09bb 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -2884,7 +2884,8 @@ outer_var_p (tree decl) { return ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL) && DECL_FUNCTION_SCOPE_P (decl) - && DECL_CONTEXT (decl) != current_function_decl); + && (DECL_CONTEXT (decl) != current_function_decl + || parsing_nsdmi ())); } /* As above, but also checks that DECL is automatic. */ @@ -3041,12 +3042,14 @@ finish_id_expression (tree id_expression, return integral_constant_value (decl); } + if (parsing_nsdmi ()) + containing_function = NULL_TREE; /* If we are in a lambda function, we can move out until we hit 1. the context, 2. a non-lambda function, or 3. a non-default capturing lambda function. */ - while (context != containing_function - && LAMBDA_FUNCTION_P (containing_function)) + else while (context != containing_function + && LAMBDA_FUNCTION_P (containing_function)) { lambda_expr = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function)); diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-local.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-local.C new file mode 100644 index 0000000..9b84c8c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-local.C @@ -0,0 +1,8 @@ +// PR c++/55240 +// { dg-do compile { target c++11 } } + +int main() +{ + int q = 1; // { dg-error "declared here" } + struct test { int x = q; } instance; // { dg-error "local variable" } +}