From patchwork Fri Oct 25 13:46:00 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ed Smith-Rowland <3dw4rd@verizon.net> X-Patchwork-Id: 286148 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 did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1AC992C0097 for ; Sat, 26 Oct 2013 00:46:28 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type; q=dns; s=default; b=gOGhUaYS8/aBc4NlP 2V1du0/ER4RMLuO2UClAWCk3MKMa92PLSxF/oHtVMpo31XMBAsHnTcproXmv57j3 HmOoahhTXL3BSNhPsz8pXoOJ0vA013hiJNYLwInlZQ56RWksHlFYkOn39HySE/41 Tw1Bm8SYeP1a5pGoLPkieMze9I= 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 :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type; s=default; bh=gckP2HIOxmNnG/Ce1w7Caul DjxY=; b=VLfkPtZ8XfmX0miMzUfBYiQal7VdPhmlplzcmWfa1D1XIm5RGzLrRB5 4o6EO4kqUcIgPgBtg+RgTjVm4spJ7+qN9R7PMPTzZ0RhmRkCvngAxo27AnQ5eBzR s70SBPelCH0+uuGJIW+5HHHEVePQT9R2aykeDW8devRhrpMhZ5Fs= Received: (qmail 15423 invoked by alias); 25 Oct 2013 13:46:21 -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 15414 invoked by uid 89); 25 Oct 2013 13:46:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: vms173013pub.verizon.net Received: from vms173013pub.verizon.net (HELO vms173013pub.verizon.net) (206.46.173.13) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 25 Oct 2013 13:46:19 +0000 Received: from [192.168.1.13] ([unknown] [74.103.25.251]) by vms173013.mailsrvcs.net (Sun Java(tm) System Messaging Server 7u2-7.02 32bit (built Apr 16 2009)) with ESMTPA id <0MV800GMQ8WNUB30@vms173013.mailsrvcs.net> for gcc-patches@gcc.gnu.org; Fri, 25 Oct 2013 08:46:00 -0500 (CDT) Message-id: <526A7618.5030601@verizon.net> Date: Fri, 25 Oct 2013 09:46:00 -0400 From: Ed Smith-Rowland <3dw4rd@verizon.net> User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-version: 1.0 To: Paolo Carlini , gcc-patches , Jason Merrill Subject: Re: PR C++/58708 - string literal operator templates broken References: <5260A01F.5010701@verizon.net> <526A4304.6020800@oracle.com> <526A584F.4050400@verizon.net> <526A5BF3.1040708@oracle.com> In-reply-to: <526A5BF3.1040708@oracle.com> Content-type: multipart/mixed; boundary=------------070202040007070708030407 On 10/25/2013 07:54 AM, Paolo Carlini wrote: > On 10/25/2013 01:38 PM, Ed Smith-Rowland wrote: >> So, you also want a library testcase? > Up to you: if you feel it would test something that the c++ front-end > testcase doesn't, why not. > > Paolo. > I think this patch should be sufficient - no containers, it just follows the pattern of the other tests in the cpp1y directory. I builds and tests of x86_64-linux. OK? Later we can think of library tools that could help authors of literals operators. The bits/bits/parse_numbers.h is a start on this. Ed gcc/cp: 2013-10-25 Edward Smith-Rowland <3dw4rd@verizon.net> PR c++/58708 * parser.c (make_string_pack): Discover non-const type and size of character and build parm pack with correct type and chars. gcc/testsuite: 2013-10-25 Edward Smith-Rowland <3dw4rd@verizon.net> PR c++/58708 *g++.dg/cpp1y/pr58708.C : New. Index: cp/parser.c =================================================================== --- cp/parser.c (revision 203997) +++ cp/parser.c (working copy) @@ -3793,22 +3793,39 @@ tree charvec; tree argpack = make_node (NONTYPE_ARGUMENT_PACK); const char *str = TREE_STRING_POINTER (value); - int i, len = TREE_STRING_LENGTH (value) - 1; + int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))); + int len = TREE_STRING_LENGTH (value) / sz - 1; tree argvec = make_tree_vec (2); - tree string_char_type_node = TREE_TYPE (TREE_TYPE (value)); + tree str_char_type_node = TREE_TYPE (TREE_TYPE (value)); + str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node); /* First template parm is character type. */ - TREE_VEC_ELT (argvec, 0) = string_char_type_node; + TREE_VEC_ELT (argvec, 0) = str_char_type_node; /* Fill in CHARVEC with all of the parameters. */ charvec = make_tree_vec (len); - for (i = 0; i < len; ++i) - TREE_VEC_ELT (charvec, i) = build_int_cst (string_char_type_node, str[i]); + if (sz == 1) + { + for (int i = 0; i < len; ++i) + TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, str[i]); + } + else if (sz == 2) + { + const uint16_t *num = (const uint16_t *)str; + for (int i = 0; i < len; ++i) + TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]); + } + else if (sz == 4) + { + const uint32_t *num = (const uint32_t *)str; + for (int i = 0; i < len; ++i) + TREE_VEC_ELT (charvec, i) = build_int_cst (str_char_type_node, num[i]); + } /* Build the argument packs. */ SET_ARGUMENT_PACK_ARGS (argpack, charvec); - TREE_TYPE (argpack) = string_char_type_node; + TREE_TYPE (argpack) = str_char_type_node; TREE_VEC_ELT (argvec, 1) = argpack; Index: testsuite/g++.dg/cpp1y/pr58708.C =================================================================== --- testsuite/g++.dg/cpp1y/pr58708.C (revision 0) +++ testsuite/g++.dg/cpp1y/pr58708.C (working copy) @@ -0,0 +1,68 @@ +// { dg-options -std=c++1y } +// { dg-do run } + +#include + +struct Foo +{ + bool type[4]; + bool const_type[4]; + int chars[10]; +}; + +template +Foo +operator""_foo() +{ + CharT arr[]{str...}; + + Foo foo; + + foo.type[0] = std::is_same::value; + foo.type[1] = std::is_same::value; + foo.type[2] = std::is_same::value; + foo.type[3] = std::is_same::value; + foo.const_type[0] = std::is_same::value; + foo.const_type[1] = std::is_same::value; + foo.const_type[2] = std::is_same::value; + foo.const_type[3] = std::is_same::value; + + for(int i; i < sizeof(arr)/sizeof(CharT) - 1; ++i) + foo.chars[i] = (int)arr[i]; + + return foo; +} + +int +main() +{ + Foo foo; + + foo = U"\x10000\x10001\x10002"_foo; + if (foo.type[3] != true) __builtin_abort(); + if (sizeof(foo.chars)/sizeof(char32_t)-1 != 3) __builtin_abort(); + if (foo.chars[0] != 65536) __builtin_abort(); + if (foo.chars[1] != 65537) __builtin_abort(); + if (foo.chars[2] != 65538) __builtin_abort(); + + foo = "\x61\x62\x63"_foo; + if (foo.type[0] != true) __builtin_abort(); + if (sizeof(foo.chars)/sizeof(char)-1 != 3) __builtin_abort(); + if (foo.chars[0] != 97) __builtin_abort(); + if (foo.chars[1] != 98) __builtin_abort(); + if (foo.chars[2] != 99) __builtin_abort(); + + foo = L"\x01020304\x05060708"_foo; + if (foo.type[1] != true) __builtin_abort(); + if (sizeof(foo.chars)/sizeof(wchar_t)-1 != 2) __builtin_abort(); + if (foo.chars[0] != 16909060) __builtin_abort(); + if (foo.chars[1] != 84281096) __builtin_abort(); + + foo = u"\x0102\x0304\x0506\x0708"_foo; + if (foo.type[2] != true) __builtin_abort(); + if (sizeof(foo.chars)/sizeof(char16_t)-1 != 4) __builtin_abort(); + if (foo.chars[0] != 258) __builtin_abort(); + if (foo.chars[1] != 772) __builtin_abort(); + if (foo.chars[2] != 1286) __builtin_abort(); + if (foo.chars[3] != 1800) __builtin_abort(); +}