]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c++/58708 (string literal operator templates broken)
authorEdward Smith-Rowland <3dw4rd@verizon.net>
Fri, 1 Nov 2013 23:00:48 +0000 (23:00 +0000)
committerEdward Smith-Rowland <emsr@gcc.gnu.org>
Fri, 1 Nov 2013 23:00:48 +0000 (23:00 +0000)
gcc/cp:

2013-11-01  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-11-01  Edward Smith-Rowland  <3dw4rd@verizon.net>

PR c++/58708
* g++.dg/cpp1y/pr58708.C: New.

From-SVN: r204305

gcc/cp/ChangeLog
gcc/cp/parser.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1y/pr58708.C [new file with mode: 0644]

index d707fec81f17059b341468b16a3e98356281a7e8..707b94955608554d7a272703ec30a04ca997148a 100644 (file)
@@ -1,3 +1,9 @@
+2013-11-01  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.
+
 2013-11-01  Trevor Saunders  <tsaunders@mozilla.com>
 
        * semantics.c (build_anon_member_initialization): Convert fields to be
index bbc8e751a873294d1255ceb01fa2be4fd61226eb..d55f5f9c7869059fff281143093bd0956485c52b 100644 (file)
@@ -3793,22 +3793,39 @@ make_string_pack (tree value)
   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 e9bd852816fc8ea13fe9fc8bce4e6b8a5c1932eb..75975d0be10807910a23a1e4d47fa6e44ab45738 100644 (file)
@@ -1,3 +1,8 @@
+2013-11-01  Edward Smith-Rowland  <3dw4rd@verizon.net>
+
+       PR c++/58708
+       * g++.dg/cpp1y/pr58708.C: New.
+
 2013-11-01  Marc Glisse  <marc.glisse@inria.fr>
 
        PR c++/58834
diff --git a/gcc/testsuite/g++.dg/cpp1y/pr58708.C b/gcc/testsuite/g++.dg/cpp1y/pr58708.C
new file mode 100644 (file)
index 0000000..a9c19eb
--- /dev/null
@@ -0,0 +1,60 @@
+// { dg-options -std=c++1y }
+// { dg-do run }
+
+template<typename, typename>
+  struct is_same
+  {
+    static constexpr bool value = false;
+  };
+
+template<typename _Tp>
+  struct is_same<_Tp, _Tp>
+  {
+    static constexpr bool value = true;
+  };
+
+template<typename CharT, CharT... Str>
+  struct Foo
+  {
+    using char_type = CharT;
+    char_type chars[sizeof...(Str)]{Str...};
+  };
+
+template<typename CharT, CharT... Str>
+  Foo<CharT, Str...>
+  operator""_foo()
+  {
+    return Foo<CharT, Str...>();
+  }
+
+int
+main()
+{
+  auto fooU = U"\x10000\x10001\x10002"_foo;
+  if (is_same<decltype(fooU)::char_type, char32_t>::value != true) __builtin_abort();
+  if (sizeof(fooU.chars)/sizeof(char32_t) != 3) __builtin_abort();
+  if (fooU.chars[0] != 65536) __builtin_abort();
+  if (fooU.chars[1] != 65537) __builtin_abort();
+  if (fooU.chars[2] != 65538) __builtin_abort();
+
+  auto foo = "\x61\x62\x63"_foo;
+  if (is_same<decltype(foo)::char_type, char>::value != true) __builtin_abort();
+  if (sizeof(foo.chars)/sizeof(char) != 3) __builtin_abort();
+  if (foo.chars[0] != 97) __builtin_abort();
+  if (foo.chars[1] != 98) __builtin_abort();
+  if (foo.chars[2] != 99) __builtin_abort();
+
+  auto wfoo = L"\x01020304\x05060708"_foo;
+  if (is_same<decltype(wfoo)::char_type, wchar_t>::value != true) __builtin_abort();
+  if (sizeof(wfoo.chars)/sizeof(wchar_t) != 2) __builtin_abort();
+  if (wfoo.chars[0] != 16909060) __builtin_abort();
+  if (wfoo.chars[1] != 84281096) __builtin_abort();
+
+  auto foou = u"\x0102\x0304\x0506\x0708"_foo;
+  if (is_same<decltype(foou)::char_type, char16_t>::value != true) __builtin_abort();
+  if (sizeof(foou.chars)/sizeof(char16_t) != 4) __builtin_abort();
+  if (foou.chars[0] != 258) __builtin_abort();
+  if (foou.chars[1] != 772) __builtin_abort();
+  if (foou.chars[2] != 1286) __builtin_abort();
+  if (foou.chars[3] != 1800) __builtin_abort();
+}