From patchwork Mon May 21 19:50:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 917825 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-478103-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="Go7AfaIn"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40qTs70kRCz9s4b for ; Tue, 22 May 2018 05:50:18 +1000 (AEST) 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:subject:message-id:mime-version:content-type; q=dns; s= default; b=bT/oATajStAU17nWmB84Y+jLFZZMeKlfAoMOmiQjrdU8wBd33lkFG qSG1NsMtvsvVZ9oHiEHSqcAlaJKkaYicVRJJkp9nV3ALxIHKnnEwLijPGjPD7tac abJx0Ag6dH7rgIVOLgHJGpDnPmhfpkJRQ4QCdQrEXC+LHgxZFEGv44= 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:subject:message-id:mime-version:content-type; s= default; bh=kbKQhOhsAZmTDqVIHpH5KNBl2eY=; b=Go7AfaInPeqhFgoDYk3L PnMnFa0n/rEW1BuSkCfbLKQnz9Ry6xh2rTUzZglg/2JpAe8zRy1JlRE1btUrIXMY fs8msC/70k4iQ8up7sbo9k0ILFJvqb2zj2OwtQSv9HG1BtHJoYRP/pr185dtaeUl NGMlEYKyvnyEnAKWZSxetDk= Received: (qmail 114319 invoked by alias); 21 May 2018 19:50:11 -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 114309 invoked by uid 89); 21 May 2018 19:50:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Roll X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 21 May 2018 19:50:08 +0000 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 54A4F7C6B3 for ; Mon, 21 May 2018 19:50:07 +0000 (UTC) Received: from redhat.com (ovpn-125-8.rdu2.redhat.com [10.10.125.8]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F1394215CDA7; Mon, 21 May 2018 19:50:06 +0000 (UTC) Date: Mon, 21 May 2018 15:50:05 -0400 From: Marek Polacek To: Jason Merrill , GCC Patches Subject: C++ PATCH to implement P0614R1, Range-based for statements with initializer Message-ID: <20180521195005.GB3122@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.5 (2018-04-13) This is my attempt to implement P0614R1, a C++20 feature whereby we may now use an init-statement in a range-based for loop like this: for (int i = bar (); const auto &x : a) // ... The somewhat tricky part was to distinguish a range-based for from an ordinary for statement, hence the cp_parser_range_based_for_with_init_p shenanigans. Note that we must be able to cope with lambdas/statement-expressions in the initializer as well as with loops such as for (int i = 0; int j = 0; ) // ... and the following seems to handle all of it. What d'ya think? Bootstrapped/regtested on x86_64-linux, ok for trunk? 2018-05-21 Marek Polacek Implement P0614R1, Range-based for statements with initializer. * parser.c (cp_parser_range_based_for_with_init_p): New. (cp_parser_init_statement): Use it. Parse the optional init-statement for a range-based for loop. * g++.dg/cpp2a/range-for1.C: New test. * g++.dg/cpp2a/range-for2.C: New test. * g++.dg/cpp2a/range-for3.C: New test. * g++.dg/cpp2a/range-for4.C: New test. * g++.dg/cpp2a/range-for5.C: New test. * g++.dg/cpp2a/range-for6.C: New test. diff --git gcc/cp/parser.c gcc/cp/parser.c index c0058085ee9..47a825f2da0 100644 --- gcc/cp/parser.c +++ gcc/cp/parser.c @@ -11255,6 +11255,39 @@ cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr) } } +/* Return true if this is the C++20 version of range-based-for with + init-statement. */ + +static bool +cp_parser_range_based_for_with_init_p (cp_parser *parser) +{ + bool r = false; + + /* Save tokens so that we can put them back. */ + cp_lexer_save_tokens (parser->lexer); + + /* There has to be an unnested ; followed by an unnested :. */ + if (cp_parser_skip_to_closing_parenthesis_1 (parser, + /*recovering=*/false, + CPP_SEMICOLON, + /*consume_paren=*/false) != -1) + goto out; + + /* We found the semicolon, eat it now. */ + cp_lexer_consume_token (parser->lexer); + /* Now look for ':' that is not nested in () or {}. */ + r = (cp_parser_skip_to_closing_parenthesis_1 (parser, + /*recovering=*/false, + CPP_COLON, + /*consume_paren=*/false) == -1); + +out: + /* Roll back the tokens we skipped. */ + cp_lexer_rollback_tokens (parser->lexer); + + return r; +} + /* Return true if we're looking at (init; cond), false otherwise. */ static bool @@ -12242,7 +12275,7 @@ cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep, simple-declaration */ static bool -cp_parser_init_statement (cp_parser* parser, tree *decl) +cp_parser_init_statement (cp_parser *parser, tree *decl) { /* If the next token is a `;', then we have an empty expression-statement. Grammatically, this is also a @@ -12255,6 +12288,29 @@ cp_parser_init_statement (cp_parser* parser, tree *decl) bool is_range_for = false; bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p; + /* Try to parse the init-statement. */ + if (cp_parser_range_based_for_with_init_p (parser)) + { + tree dummy; + cp_parser_parse_tentatively (parser); + /* Parse the declaration. */ + cp_parser_simple_declaration (parser, + /*function_definition_allowed_p=*/false, + &dummy); + cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); + if (!cp_parser_parse_definitely (parser)) + /* That didn't work, try to parse it as an expression-statement. */ + cp_parser_expression_statement (parser, NULL_TREE); + + if (cxx_dialect < cxx2a) + { + pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0, + "range-based % loops with initializer only " + "available with -std=c++2a or -std=gnu++2a"); + *decl = error_mark_node; + } + } + /* A colon is used in range-based for. */ parser->colon_corrects_to_scope_p = false; @@ -12268,7 +12324,7 @@ cp_parser_init_statement (cp_parser* parser, tree *decl) parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p; if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) { - /* It is a range-for, consume the ':' */ + /* It is a range-for, consume the ':'. */ cp_lexer_consume_token (parser->lexer); is_range_for = true; if (cxx_dialect < cxx11) @@ -12280,9 +12336,9 @@ cp_parser_init_statement (cp_parser* parser, tree *decl) } } else - /* The ';' is not consumed yet because we told - cp_parser_simple_declaration not to. */ - cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); + /* The ';' is not consumed yet because we told + cp_parser_simple_declaration not to. */ + cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON); if (cp_parser_parse_definitely (parser)) return is_range_for; diff --git gcc/testsuite/g++.dg/cpp2a/range-for1.C gcc/testsuite/g++.dg/cpp2a/range-for1.C index e69de29bb2d..3a5523585a1 100644 --- gcc/testsuite/g++.dg/cpp2a/range-for1.C +++ gcc/testsuite/g++.dg/cpp2a/range-for1.C @@ -0,0 +1,16 @@ +// P0614R1 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +void +fn1 () +{ + int a[] = { 1, 2, 3, 4, 5 }; + + for (int i = 0; auto x : a) // { dg-warning "range-based .for. loops with initializer only available with" "" { target c++17_down } } + ++i; + + int i; + for (i = 0; auto x : a) // { dg-warning "range-based .for. loops with initializer only available with" "" { target c++17_down } } + ++i; +} diff --git gcc/testsuite/g++.dg/cpp2a/range-for2.C gcc/testsuite/g++.dg/cpp2a/range-for2.C index e69de29bb2d..acb16c57d1c 100644 --- gcc/testsuite/g++.dg/cpp2a/range-for2.C +++ gcc/testsuite/g++.dg/cpp2a/range-for2.C @@ -0,0 +1,16 @@ +// P0614R1 +// { dg-do compile } +// { dg-options "-std=c++2a" } + +void +fn1 () +{ + int a[] = { 1, 2, 3, 4, 5 }; + + for (int i = 0; auto x : a) + ++i; + + int i; + for (i = 0; auto x : a) + ++i; +} diff --git gcc/testsuite/g++.dg/cpp2a/range-for3.C gcc/testsuite/g++.dg/cpp2a/range-for3.C index e69de29bb2d..291e605b92f 100644 --- gcc/testsuite/g++.dg/cpp2a/range-for3.C +++ gcc/testsuite/g++.dg/cpp2a/range-for3.C @@ -0,0 +1,26 @@ +// P0614R1 +// { dg-do compile } +// { dg-options "-std=c++2a" } + +static const int a[] = { 1, 2, 3, 4, 5 }; +extern void foo (int); +extern void bar (int, int); + +constexpr int +baz () +{ + return 6; +} + +void +fn1 (int i) +{ + for ((i += 2); auto x : a) + foo (i); + + for (auto j = 0, k = 0; auto x : a) + bar (j + k, x); + + for (constexpr int j = baz (); auto x : a) + bar (x, j); +} diff --git gcc/testsuite/g++.dg/cpp2a/range-for4.C gcc/testsuite/g++.dg/cpp2a/range-for4.C index e69de29bb2d..6ba783f46cb 100644 --- gcc/testsuite/g++.dg/cpp2a/range-for4.C +++ gcc/testsuite/g++.dg/cpp2a/range-for4.C @@ -0,0 +1,27 @@ +// P0614R1 +// { dg-do run } +// { dg-options "-std=c++2a" } + +int +main () +{ + int a[] = { 1, 2, 3, 4, 5 }; + + for (int i = 1; auto x : a) + if (i++ != x) + __builtin_abort (); + + int i; + for (i = 1; auto x : a) + if (i++ != x) + __builtin_abort (); + + i = 0; + for (i++; auto x : a) + if (i != 1) + __builtin_abort (); + + for (int s[] = { 1, 1, 1 }; auto x : s) + if (x != 1) + __builtin_abort (); +} diff --git gcc/testsuite/g++.dg/cpp2a/range-for5.C gcc/testsuite/g++.dg/cpp2a/range-for5.C index e69de29bb2d..62f1c2f04e1 100644 --- gcc/testsuite/g++.dg/cpp2a/range-for5.C +++ gcc/testsuite/g++.dg/cpp2a/range-for5.C @@ -0,0 +1,46 @@ +// P0614R1 +// { dg-do compile } +// { dg-options "-std=c++2a" } + +void +fn1 () +{ + int a[] = { 1, 2, 3, 4, 5 }; + + for (int i = 0; auto x : a) + ++i; + + i = 0; // { dg-error "not declared" } + + for (int i = 0; auto x : a) + { + for (int j = 0; auto x : a) + { + for (int k = 0; auto x : a) + k++; + k++; // { dg-error "not declared" } + } + j++; // { dg-error "not declared" } + } +} + +void +fn2 () +{ + int a[] = { 1, 2, 3, 4, 5 }; + for (int i = 0; auto x : a) + int i = 3; // { dg-error "redeclaration" } +} +void +fn3 () +{ + int a[] = { 1, 2, 3, 4, 5 }; + + for (;:) // { dg-error "expected" } + { + } + + for (;;:) // { dg-error "expected" } + { + } +} diff --git gcc/testsuite/g++.dg/cpp2a/range-for6.C gcc/testsuite/g++.dg/cpp2a/range-for6.C index e69de29bb2d..4cee60a839e 100644 --- gcc/testsuite/g++.dg/cpp2a/range-for6.C +++ gcc/testsuite/g++.dg/cpp2a/range-for6.C @@ -0,0 +1,17 @@ +// P0614R1 +// { dg-do run } +// { dg-options "-std=c++2a" } + +int +main () +{ + int a[] = { 1, 2, 3, 4, 5 }; + + for (int i = []{ return 3; }(); auto x : a) + if (i != 3) + __builtin_abort (); + + for (int i = ({ 3; }); auto x : a) + if (i != 3) + __builtin_abort (); +}