From patchwork Thu May 9 18:12:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1933563 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=e/Wqi4TY; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vb0Vl3wKFz20fc for ; Fri, 10 May 2024 04:13:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D839C385E836 for ; Thu, 9 May 2024 18:12:55 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 9D0953858D39 for ; Thu, 9 May 2024 18:12:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9D0953858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9D0953858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715278356; cv=none; b=PUaEUBLSFg6WSdSKu86o8wz9g85TH/h0lpjpTdM0xqaOQuyr0OXEvfIFv0O5e0UPO0ALOm1i24LKDzEFyLPtMnPlrBBePA5/prBu9dy4Vxpi2e80aosFe/J1f9lSvzVpCx0OddCkC4+tS6XBhQa2BSwarpSI8c0IymRhqRCYXm0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715278356; c=relaxed/simple; bh=8foedDo2DGpZvv1SssgBSajr1FzirhES8BzGVmJVbb8=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=i/5bcRy9MFtIWGdeEZ9xFDy4+tOiuBcZaSjpR2rAZBp0Qi/uhyUmMlTPFIkXnM/E9lxU5rxo6ibftt/EqOWj1qncX5L6SioV3sFBmcAfeqz4fvrnKv7zG/bITJVh9xAB/csnKS5uCOQFDQ7yNZ1sHPtdzeODrfpff2PVzsr7xr0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715278354; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=xdknCrpsmILT4Bt0/USBiB46pBH++VFslRU/pF0Zx1Y=; b=e/Wqi4TYiY/xIuvkunHvaQEzBm067Oce5wd0zZd29tkMYDeGYRul/IhkO4zoIdCmpwLWeT h1MgYagGZTJeINVnYBHRAD49jLzf7fd24BlnXRGsrFHCCWbkJR+lXRHopD5xDoI76e27F8 LicI2BC1jYJ301fn465ht87FxuhDBK4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-228-9zMhr0_MN0uIzrghyy0nAg-1; Thu, 09 May 2024 14:12:32 -0400 X-MC-Unique: 9zMhr0_MN0uIzrghyy0nAg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 358C2800206 for ; Thu, 9 May 2024 18:12:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.64]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EE378113016C; Thu, 9 May 2024 18:12:31 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 449ICU34502841 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 9 May 2024 20:12:30 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 449ICU4D502840; Thu, 9 May 2024 20:12:30 +0200 Date: Thu, 9 May 2024 20:12:30 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix parsing of abstract-declarator starting with ... followed by [ or ( [PR115012] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The C++26 P2662R3 Pack indexing paper mentions that both GCC and MSVC don't handle T...[10] parameter declaration when T is a pack. While that will change meaning in C++26, in C++11 .. C++23 this ought to be valid. Also, T...(args) as well. The following patch handles those in cp_parser_direct_declarator. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-05-09 Jakub Jelinek PR c++/115012 * parser.cc (cp_parser_direct_declarator): Handle abstract declarator starting with ... followed by [ or (. * g++.dg/cpp0x/variadic185.C: New test. * g++.dg/cpp0x/variadic186.C: New test. Jakub --- gcc/cp/parser.cc.jj 2024-05-09 10:30:58.000000000 +0200 +++ gcc/cp/parser.cc 2024-05-09 16:44:01.250777325 +0200 @@ -23916,7 +23916,12 @@ cp_parser_direct_declarator (cp_parser* { /* Peek at the next token. */ token = cp_lexer_peek_token (parser->lexer); - if (token->type == CPP_OPEN_PAREN) + if (token->type == CPP_OPEN_PAREN + || (first + && dcl_kind != CP_PARSER_DECLARATOR_NAMED + && token->type == CPP_ELLIPSIS + && cxx_dialect > cxx98 + && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))) { /* This is either a parameter-declaration-clause, or a parenthesized declarator. When we know we are parsing a @@ -23955,6 +23960,11 @@ cp_parser_direct_declarator (cp_parser* Thus again, we try a parameter-declaration-clause, and if that fails, we back out and return. */ + bool pack_expansion_p = token->type == CPP_ELLIPSIS; + + if (pack_expansion_p) + /* Consume the `...' */ + cp_lexer_consume_token (parser->lexer); if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) { @@ -24098,6 +24108,7 @@ cp_parser_direct_declarator (cp_parser* attrs, parens_loc); declarator->attributes = gnu_attrs; + declarator->parameter_pack_p |= pack_expansion_p; /* Any subsequent parameter lists are to do with return type, so are not those of the declared function. */ @@ -24121,7 +24132,7 @@ cp_parser_direct_declarator (cp_parser* /* If this is the first, we can try a parenthesized declarator. */ - if (first) + if (first && !pack_expansion_p) { bool saved_in_type_id_in_expr_p; @@ -24156,16 +24167,27 @@ cp_parser_direct_declarator (cp_parser* else break; } - else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) - && token->type == CPP_OPEN_SQUARE - && !cp_next_tokens_can_be_attribute_p (parser)) + else if (((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED) + && token->type == CPP_OPEN_SQUARE + && !cp_next_tokens_can_be_attribute_p (parser)) + || (first + && dcl_kind != CP_PARSER_DECLARATOR_NAMED + && token->type == CPP_ELLIPSIS + && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE) + && cxx_dialect > cxx98 + && !cp_nth_tokens_can_be_std_attribute_p (parser, 2))) { /* Parse an array-declarator. */ tree bounds, attrs; + bool pack_expansion_p = token->type == CPP_ELLIPSIS; if (ctor_dtor_or_conv_p) *ctor_dtor_or_conv_p = 0; + if (pack_expansion_p) + /* Consume the `...' */ + cp_lexer_consume_token (parser->lexer); + open_paren = NULL; first = false; parser->default_arg_ok_p = false; @@ -24220,6 +24242,7 @@ cp_parser_direct_declarator (cp_parser* attrs = cp_parser_std_attribute_spec_seq (parser); declarator = make_array_declarator (declarator, bounds); declarator->std_attributes = attrs; + declarator->parameter_pack_p |= pack_expansion_p; } else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT) { --- gcc/testsuite/g++.dg/cpp0x/variadic185.C.jj 2024-05-09 15:08:49.070651189 +0200 +++ gcc/testsuite/g++.dg/cpp0x/variadic185.C 2024-05-09 15:07:40.045583153 +0200 @@ -0,0 +1,39 @@ +// PR c++/115012 +// { dg-do compile { target { c++11 && c++23_down } } } +// { dg-final { scan-assembler "_Z3fooILi10EJidEEvDpAT__T0_" } } +// { dg-final { scan-assembler "_Z3barILi10EiEvPT0_" } } +// { dg-final { scan-assembler "_Z3bazILi10EJidEEvDpAT__T0_" } } +// { dg-final { scan-assembler "_Z3quxILi5EJifEEvDpAT__AT__T0_" } } + +template +void +foo (T... x[N]) +{ +} + +template +void +bar (T [N]) +{ +} + +template +void +baz (T... [N]) +{ +} + +template +void +qux (T... [N][N]) +{ +} + +void +corge (int a[10], double b[10], int c[5][5], float d[5][5]) +{ + foo <10, int, double> (a, b); + bar <10, int> (a); + baz <10, int, double> (a, b); + qux <5, int, float> (c, d); +} --- gcc/testsuite/g++.dg/cpp0x/variadic186.C.jj 2024-05-09 16:39:50.409153099 +0200 +++ gcc/testsuite/g++.dg/cpp0x/variadic186.C 2024-05-09 15:37:22.469569809 +0200 @@ -0,0 +1,43 @@ +// PR c++/115012 +// { dg-do compile { target { c++11 } } } +// { dg-final { scan-assembler "_Z3fooIJidEEvDpFT_iE" } } +// { dg-final { scan-assembler "_Z3barIiEvPFT_iE" } } +// { dg-final { scan-assembler "_Z3bazIJidEEvDpFT_iE" } } + +template +void +foo (T... x (int)) +{ +} + +template +void +bar (T (int)) +{ +} + +template +void +baz (T... (int)) +{ +} + +int +f1 (int) +{ + return 0; +} + +double +f2 (int) +{ + return 0; +} + +void +corge () +{ + foo (f1, f2); + bar (f1); + baz (f1, f2); +}