OSDN Git Service

* setproctitle.c (setproctitle): Use "GNU/Linux" in comment.
[pf3gnuchains/gcc-fork.git] / libiberty / cp-demangle.c
index 1e775d9..c7afef0 100644 (file)
@@ -1,5 +1,6 @@
 /* Demangler for g++ V3 ABI.
-   Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
+   Free Software Foundation, Inc.
    Written by Ian Lance Taylor <ian@wasabisystems.com>.
 
    This file is part of the libiberty library, which is part of GCC.
@@ -277,8 +278,6 @@ struct d_growable_string
 enum { D_PRINT_BUFFER_LENGTH = 256 };
 struct d_print_info
 {
-  /* The options passed to the demangler.  */
-  int options;
   /* Fixed-length allocated buffer for demangled data, flushed to the
      callback with a NUL termination once full.  */
   char buf[D_PRINT_BUFFER_LENGTH];
@@ -298,6 +297,11 @@ struct d_print_info
   struct d_print_mod *modifiers;
   /* Set to 1 if we saw a demangling error.  */
   int demangle_failure;
+  /* The current index into any template argument packs we are using
+     for printing.  */
+  int pack_index;
+  /* Number of d_print_flush calls so far.  */
+  unsigned long int flush_count;
 };
 
 #ifdef CP_DEMANGLE_DEBUG
@@ -316,6 +320,9 @@ static struct demangle_component *
 d_make_name (struct d_info *, const char *, int);
 
 static struct demangle_component *
+d_make_demangle_mangled_name (struct d_info *, const char *);
+
+static struct demangle_component *
 d_make_builtin_type (struct d_info *,
                      const struct demangle_builtin_type_info *);
 
@@ -385,6 +392,8 @@ d_class_enum_type (struct d_info *);
 
 static struct demangle_component *d_array_type (struct d_info *);
 
+static struct demangle_component *d_vector_type (struct d_info *);
+
 static struct demangle_component *
 d_pointer_to_member_type (struct d_info *);
 
@@ -404,6 +413,13 @@ static struct demangle_component *d_local_name (struct d_info *);
 
 static int d_discriminator (struct d_info *);
 
+static struct demangle_component *d_lambda (struct d_info *);
+
+static struct demangle_component *d_unnamed_type (struct d_info *);
+
+static struct demangle_component *
+d_clone_suffix (struct d_info *, struct demangle_component *);
+
 static int
 d_add_substitution (struct d_info *, struct demangle_component *);
 
@@ -421,7 +437,7 @@ static void
 d_growable_string_callback_adapter (const char *, size_t, void *);
 
 static void
-d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
+d_print_init (struct d_print_info *, demangle_callbackref, void *);
 
 static inline void d_print_error (struct d_print_info *);
 
@@ -439,32 +455,32 @@ static inline void d_append_string (struct d_print_info *, const char *);
 static inline char d_last_char (struct d_print_info *);
 
 static void
-d_print_comp (struct d_print_info *, const struct demangle_component *);
+d_print_comp (struct d_print_info *, int, const struct demangle_component *);
 
 static void
 d_print_java_identifier (struct d_print_info *, const char *, int);
 
 static void
-d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
+d_print_mod_list (struct d_print_info *, int, struct d_print_mod *, int);
 
 static void
-d_print_mod (struct d_print_info *, const struct demangle_component *);
+d_print_mod (struct d_print_info *, int, const struct demangle_component *);
 
 static void
-d_print_function_type (struct d_print_info *,
+d_print_function_type (struct d_print_info *, int,
                        const struct demangle_component *,
                        struct d_print_mod *);
 
 static void
-d_print_array_type (struct d_print_info *,
+d_print_array_type (struct d_print_info *, int,
                     const struct demangle_component *,
                     struct d_print_mod *);
 
 static void
-d_print_expr_op (struct d_print_info *, const struct demangle_component *);
+d_print_expr_op (struct d_print_info *, int, const struct demangle_component *);
 
 static void
-d_print_cast (struct d_print_info *, const struct demangle_component *);
+d_print_cast (struct d_print_info *, int, const struct demangle_component *);
 
 static int d_demangle_callback (const char *, int,
                                 demangle_callbackref, void *);
@@ -596,6 +612,9 @@ d_dump (struct demangle_component *dc, int indent)
     case DEMANGLE_COMPONENT_REFERENCE:
       printf ("reference\n");
       break;
+    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
+      printf ("rvalue reference\n");
+      break;
     case DEMANGLE_COMPONENT_COMPLEX:
       printf ("complex\n");
       break;
@@ -614,6 +633,9 @@ d_dump (struct demangle_component *dc, int indent)
     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
       printf ("pointer to member type\n");
       break;
+    case DEMANGLE_COMPONENT_FIXED_TYPE:
+      printf ("fixed-point type\n");
+      break;
     case DEMANGLE_COMPONENT_ARGLIST:
       printf ("argument list\n");
       break;
@@ -647,6 +669,21 @@ d_dump (struct demangle_component *dc, int indent)
     case DEMANGLE_COMPONENT_LITERAL_NEG:
       printf ("negative literal\n");
       break;
+    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
+      printf ("java resource\n");
+      break;
+    case DEMANGLE_COMPONENT_COMPOUND_NAME:
+      printf ("compound name\n");
+      break;
+    case DEMANGLE_COMPONENT_CHARACTER:
+      printf ("character '%c'\n",  dc->u.s_character.character);
+      return;
+    case DEMANGLE_COMPONENT_DECLTYPE:
+      printf ("decltype\n");
+      break;
+    case DEMANGLE_COMPONENT_PACK_EXPANSION:
+      printf ("pack expansion\n");
+      break;
     }
 
   d_dump (d_left (dc), indent + 2);
@@ -694,8 +731,8 @@ cplus_demangle_fill_ctor (struct demangle_component *p,
 {
   if (p == NULL
       || name == NULL
-      || (kind < gnu_v3_complete_object_ctor
-         && kind > gnu_v3_complete_object_allocating_ctor))
+      || (int) kind < gnu_v3_complete_object_ctor
+      || (int) kind > gnu_v3_complete_object_allocating_ctor)
     return 0;
   p->type = DEMANGLE_COMPONENT_CTOR;
   p->u.s_ctor.kind = kind;
@@ -713,8 +750,8 @@ cplus_demangle_fill_dtor (struct demangle_component *p,
 {
   if (p == NULL
       || name == NULL
-      || (kind < gnu_v3_deleting_dtor
-         && kind > gnu_v3_base_object_dtor))
+      || (int) kind < gnu_v3_deleting_dtor
+      || (int) kind > gnu_v3_base_object_dtor)
     return 0;
   p->type = DEMANGLE_COMPONENT_DTOR;
   p->u.s_dtor.kind = kind;
@@ -766,6 +803,9 @@ d_make_comp (struct d_info *di, enum demangle_component_type type,
     case DEMANGLE_COMPONENT_TRINARY_ARG2:
     case DEMANGLE_COMPONENT_LITERAL:
     case DEMANGLE_COMPONENT_LITERAL_NEG:
+    case DEMANGLE_COMPONENT_COMPOUND_NAME:
+    case DEMANGLE_COMPONENT_VECTOR_TYPE:
+    case DEMANGLE_COMPONENT_CLONE:
       if (left == NULL || right == NULL)
        return NULL;
       break;
@@ -785,12 +825,16 @@ d_make_comp (struct d_info *di, enum demangle_component_type type,
     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
     case DEMANGLE_COMPONENT_POINTER:
     case DEMANGLE_COMPONENT_REFERENCE:
+    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
     case DEMANGLE_COMPONENT_COMPLEX:
     case DEMANGLE_COMPONENT_IMAGINARY:
     case DEMANGLE_COMPONENT_VENDOR_TYPE:
-    case DEMANGLE_COMPONENT_ARGLIST:
-    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
     case DEMANGLE_COMPONENT_CAST:
+    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
+    case DEMANGLE_COMPONENT_DECLTYPE:
+    case DEMANGLE_COMPONENT_PACK_EXPANSION:
+    case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
+    case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
       if (left == NULL)
        return NULL;
       break;
@@ -811,6 +855,8 @@ d_make_comp (struct d_info *di, enum demangle_component_type type,
     case DEMANGLE_COMPONENT_RESTRICT_THIS:
     case DEMANGLE_COMPONENT_VOLATILE_THIS:
     case DEMANGLE_COMPONENT_CONST_THIS:
+    case DEMANGLE_COMPONENT_ARGLIST:
+    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
       break;
 
       /* Other types should not be seen here.  */
@@ -828,6 +874,17 @@ d_make_comp (struct d_info *di, enum demangle_component_type type,
   return p;
 }
 
+/* Add a new demangle mangled name component.  */
+
+static struct demangle_component *
+d_make_demangle_mangled_name (struct d_info *di, const char *s)
+{
+  if (d_peek_char (di) != '_' || d_peek_next_char (di) != 'Z')
+    return d_make_name (di, s, strlen (s));
+  d_advance (di, 2);
+  return d_encoding (di, 0);
+}
+
 /* Add a new name component.  */
 
 static struct demangle_component *
@@ -890,6 +947,20 @@ d_make_extended_operator (struct d_info *di, int args,
   return p;
 }
 
+static struct demangle_component *
+d_make_default_arg (struct d_info *di, int num,
+                   struct demangle_component *sub)
+{
+  struct demangle_component *p = d_make_empty (di);
+  if (p)
+    {
+      p->type = DEMANGLE_COMPONENT_DEFAULT_ARG;
+      p->u.s_unary_num.num = num;
+      p->u.s_unary_num.sub = sub;
+    }
+  return p;
+}
+
 /* Add a new constructor component.  */
 
 static struct demangle_component *
@@ -934,6 +1005,22 @@ d_make_template_param (struct d_info *di, long i)
   return p;
 }
 
+/* Add a new function parameter.  */
+
+static struct demangle_component *
+d_make_function_param (struct d_info *di, long i)
+{
+  struct demangle_component *p;
+
+  p = d_make_empty (di);
+  if (p != NULL)
+    {
+      p->type = DEMANGLE_COMPONENT_FUNCTION_PARAM;
+      p->u.s_number.number = i;
+    }
+  return p;
+}
+
 /* Add a new standard substitution component.  */
 
 static struct demangle_component *
@@ -951,7 +1038,7 @@ d_make_sub (struct d_info *di, const char *name, int len)
   return p;
 }
 
-/* <mangled-name> ::= _Z <encoding>
+/* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
 
    TOP_LEVEL is non-zero when called at the top level.  */
 
@@ -959,11 +1046,28 @@ CP_STATIC_IF_GLIBCPP_V3
 struct demangle_component *
 cplus_demangle_mangled_name (struct d_info *di, int top_level)
 {
-  if (! d_check_char (di, '_'))
+  struct demangle_component *p;
+
+  if (! d_check_char (di, '_')
+      /* Allow missing _ if not at toplevel to work around a
+        bug in G++ abi-version=2 mangling; see the comment in
+        write_template_arg.  */
+      && top_level)
     return NULL;
   if (! d_check_char (di, 'Z'))
     return NULL;
-  return d_encoding (di, top_level);
+  p = d_encoding (di, top_level);
+
+  /* If at top level and parsing parameters, check for a clone
+     suffix.  */
+  if (top_level && (di->options & DMGL_PARAMS) != 0)
+    while (d_peek_char (di) == '.'
+          && (IS_LOWER (d_peek_next_char (di))
+              || d_peek_next_char (di) == '_'
+              || IS_DIGIT (d_peek_next_char (di))))
+      p = d_clone_suffix (di, p);
+
+  return p;
 }
 
 /* Return whether a function should have a return type.  The argument
@@ -1067,7 +1171,7 @@ d_encoding (struct d_info *di, int top_level)
        }
 
       peek = d_peek_char (di);
-      if (peek == '\0' || peek == 'E')
+      if (dc == NULL || peek == '\0' || peek == 'E')
        return dc;
       return d_make_comp (di, DEMANGLE_COMPONENT_TYPED_NAME, dc,
                          d_bare_function_type (di, has_return_type (dc)));
@@ -1100,6 +1204,10 @@ d_name (struct d_info *di)
     case 'Z':
       return d_local_name (di);
 
+    case 'L':
+    case 'U':
+      return d_unqualified_name (di);
+
     case 'S':
       {
        int subst;
@@ -1189,6 +1297,7 @@ d_nested_name (struct d_info *di)
 /* <prefix> ::= <prefix> <unqualified-name>
             ::= <template-prefix> <template-args>
             ::= <template-param>
+            ::= <decltype>
             ::=
             ::= <substitution>
 
@@ -1217,10 +1326,21 @@ d_prefix (struct d_info *di)
         <template-param> here.  */
 
       comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
-      if (IS_DIGIT (peek)
+      if (peek == 'D')
+       {
+         char peek2 = d_peek_next_char (di);
+         if (peek2 == 'T' || peek2 == 't')
+           /* Decltype.  */
+           dc = cplus_demangle_type (di);
+         else
+           /* Destructor name.  */
+           dc = d_unqualified_name (di);
+       }
+      else if (IS_DIGIT (peek)
          || IS_LOWER (peek)
          || peek == 'C'
-         || peek == 'D')
+         || peek == 'U'
+         || peek == 'L')
        dc = d_unqualified_name (di);
       else if (peek == 'S')
        dc = d_substitution (di, 1);
@@ -1235,6 +1355,16 @@ d_prefix (struct d_info *di)
        dc = d_template_param (di);
       else if (peek == 'E')
        return ret;
+      else if (peek == 'M')
+       {
+         /* Initializer scope for a lambda.  We don't need to represent
+            this; the normal code will just treat the variable as a type
+            scope, which gives appropriate output.  */
+         if (ret == NULL)
+           return NULL;
+         d_advance (di, 1);
+         continue;
+       }
       else
        return NULL;
 
@@ -1254,6 +1384,9 @@ d_prefix (struct d_info *di)
 /* <unqualified-name> ::= <operator-name>
                       ::= <ctor-dtor-name>
                       ::= <source-name>
+                     ::= <local-source-name> 
+
+    <local-source-name>        ::= L <source-name> <discriminator>
 */
 
 static struct demangle_component *
@@ -1275,6 +1408,31 @@ d_unqualified_name (struct d_info *di)
     }
   else if (peek == 'C' || peek == 'D')
     return d_ctor_dtor_name (di);
+  else if (peek == 'L')
+    {
+      struct demangle_component * ret;
+
+      d_advance (di, 1);
+
+      ret = d_source_name (di);
+      if (ret == NULL)
+       return NULL;
+      if (! d_discriminator (di))
+       return NULL;
+      return ret;
+    }
+  else if (peek == 'U')
+    {
+      switch (d_peek_next_char (di))
+       {
+       case 'l':
+         return d_lambda (di);
+       case 't':
+         return d_unnamed_type (di);
+       default:
+         return NULL;
+       }
+    }
   else
     return NULL;
 }
@@ -1328,6 +1486,20 @@ d_number (struct d_info *di)
     }
 }
 
+/* Like d_number, but returns a demangle_component.  */
+
+static struct demangle_component *
+d_number_component (struct d_info *di)
+{
+  struct demangle_component *ret = d_make_empty (di);
+  if (ret)
+    {
+      ret->type = DEMANGLE_COMPONENT_NUMBER;
+      ret->u.s_number.number = d_number (di);
+    }
+  return ret;
+}
+
 /* identifier ::= <(unqualified source code identifier)>  */
 
 static struct demangle_component *
@@ -1386,13 +1558,14 @@ const struct demangle_operator_info cplus_demangle_operators[] =
   { "aa", NL ("&&"),        2 },
   { "ad", NL ("&"),         1 },
   { "an", NL ("&"),         2 },
-  { "cl", NL ("()"),        0 },
+  { "cl", NL ("()"),        2 },
   { "cm", NL (","),         2 },
   { "co", NL ("~"),         1 },
   { "dV", NL ("/="),        2 },
   { "da", NL ("delete[]"),  1 },
   { "de", NL ("*"),         1 },
   { "dl", NL ("delete"),    1 },
+  { "dt", NL ("."),         2 },
   { "dv", NL ("/"),         2 },
   { "eO", NL ("^="),        2 },
   { "eo", NL ("^"),         2 },
@@ -1430,6 +1603,8 @@ const struct demangle_operator_info cplus_demangle_operators[] =
   { "rs", NL (">>"),        2 },
   { "st", NL ("sizeof "),   1 },
   { "sz", NL ("sizeof "),   1 },
+  { "at", NL ("alignof "),   1 },
+  { "az", NL ("alignof "),   1 },
   { NULL, NULL, 0,          0 }
 };
 
@@ -1477,6 +1652,102 @@ d_operator_name (struct d_info *di)
     }
 }
 
+static struct demangle_component *
+d_make_character (struct d_info *di, int c)
+{
+  struct demangle_component *p;
+  p = d_make_empty (di);
+  if (p != NULL)
+    {
+      p->type = DEMANGLE_COMPONENT_CHARACTER;
+      p->u.s_character.character = c;
+    }
+  return p;
+}
+
+static struct demangle_component *
+d_java_resource (struct d_info *di)
+{
+  struct demangle_component *p = NULL;
+  struct demangle_component *next = NULL;
+  long len, i;
+  char c;
+  const char *str;
+
+  len = d_number (di);
+  if (len <= 1)
+    return NULL;
+
+  /* Eat the leading '_'.  */
+  if (d_next_char (di) != '_')
+    return NULL;
+  len--;
+
+  str = d_str (di);
+  i = 0;
+
+  while (len > 0)
+    {
+      c = str[i];
+      if (!c)
+       return NULL;
+
+      /* Each chunk is either a '$' escape...  */
+      if (c == '$')
+       {
+         i++;
+         switch (str[i++])
+           {
+           case 'S':
+             c = '/';
+             break;
+           case '_':
+             c = '.';
+             break;
+           case '$':
+             c = '$';
+             break;
+           default:
+             return NULL;
+           }
+         next = d_make_character (di, c);
+         d_advance (di, i);
+         str = d_str (di);
+         len -= i;
+         i = 0;
+         if (next == NULL)
+           return NULL;
+       }
+      /* ... or a sequence of characters.  */
+      else
+       {
+         while (i < len && str[i] && str[i] != '$')
+           i++;
+
+         next = d_make_name (di, str, i);
+         d_advance (di, i);
+         str = d_str (di);
+         len -= i;
+         i = 0;
+         if (next == NULL)
+           return NULL;
+       }
+
+      if (p == NULL)
+       p = next;
+      else
+       {
+         p = d_make_comp (di, DEMANGLE_COMPONENT_COMPOUND_NAME, p, next);
+         if (p == NULL)
+           return NULL;
+       }
+    }
+
+  p = d_make_comp (di, DEMANGLE_COMPONENT_JAVA_RESOURCE, p, NULL);
+
+  return p;
+}
+
 /* <special-name> ::= TV <type>
                   ::= TT <type>
                   ::= TI <type>
@@ -1490,6 +1761,7 @@ d_operator_name (struct d_info *di)
                   ::= TJ <type>
                   ::= GR <name>
                  ::= GA <encoding>
+                 ::= Gr <resource name>
 */
 
 static struct demangle_component *
@@ -1581,6 +1853,9 @@ d_special_name (struct d_info *di)
          return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
                              d_encoding (di, 0), NULL);
 
+       case 'r':
+         return d_java_resource (di);
+
        default:
          return NULL;
        }
@@ -1706,6 +1981,7 @@ d_ctor_dtor_name (struct d_info *di)
           ::= <CV-qualifiers> <type>
           ::= P <type>
           ::= R <type>
+          ::= O <type> (C++0x)
           ::= C <type>
           ::= G <type>
           ::= U <source-name> <type>
@@ -1746,6 +2022,14 @@ cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
   /* y */ { NL ("unsigned long long"), NL ("unsigned long long"),
            D_PRINT_UNSIGNED_LONG_LONG },
   /* z */ { NL ("..."),                NL ("..."),             D_PRINT_DEFAULT },
+  /* 26 */ { NL ("decimal32"), NL ("decimal32"),       D_PRINT_DEFAULT },
+  /* 27 */ { NL ("decimal64"), NL ("decimal64"),       D_PRINT_DEFAULT },
+  /* 28 */ { NL ("decimal128"),        NL ("decimal128"),      D_PRINT_DEFAULT },
+  /* 29 */ { NL ("half"),      NL ("half"),            D_PRINT_FLOAT },
+  /* 30 */ { NL ("char16_t"),  NL ("char16_t"),        D_PRINT_DEFAULT },
+  /* 31 */ { NL ("char32_t"),  NL ("char32_t"),        D_PRINT_DEFAULT },
+  /* 32 */ { NL ("decltype(nullptr)"), NL ("decltype(nullptr)"),
+            D_PRINT_DEFAULT },
 };
 
 CP_STATIC_IF_GLIBCPP_V3
@@ -1780,7 +2064,7 @@ cplus_demangle_type (struct d_info *di)
       if (pret == NULL)
        return NULL;
       *pret = cplus_demangle_type (di);
-      if (! d_add_substitution (di, ret))
+      if (! *pret || ! d_add_substitution (di, ret))
        return NULL;
       return ret;
     }
@@ -1872,6 +2156,12 @@ cplus_demangle_type (struct d_info *di)
       }
       break;
 
+    case 'O':
+      d_advance (di, 1);
+      ret = d_make_comp (di, DEMANGLE_COMPONENT_RVALUE_REFERENCE,
+                         cplus_demangle_type (di), NULL);
+      break;
+
     case 'P':
       d_advance (di, 1);
       ret = d_make_comp (di, DEMANGLE_COMPONENT_POINTER,
@@ -1881,7 +2171,7 @@ cplus_demangle_type (struct d_info *di)
     case 'R':
       d_advance (di, 1);
       ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
-                        cplus_demangle_type (di), NULL);
+                         cplus_demangle_type (di), NULL);
       break;
 
     case 'C':
@@ -1903,6 +2193,88 @@ cplus_demangle_type (struct d_info *di)
                         cplus_demangle_type (di), ret);
       break;
 
+    case 'D':
+      can_subst = 0;
+      d_advance (di, 1);
+      peek = d_next_char (di);
+      switch (peek)
+       {
+       case 'T':
+       case 't':
+         /* decltype (expression) */
+         ret = d_make_comp (di, DEMANGLE_COMPONENT_DECLTYPE,
+                            d_expression (di), NULL);
+         if (ret && d_next_char (di) != 'E')
+           ret = NULL;
+         break;
+         
+       case 'p':
+         /* Pack expansion.  */
+         ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
+                            cplus_demangle_type (di), NULL);
+         break;
+         
+       case 'f':
+         /* 32-bit decimal floating point */
+         ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[26]);
+         di->expansion += ret->u.s_builtin.type->len;
+         break;
+       case 'd':
+         /* 64-bit DFP */
+         ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[27]);
+         di->expansion += ret->u.s_builtin.type->len;
+         break;
+       case 'e':
+         /* 128-bit DFP */
+         ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[28]);
+         di->expansion += ret->u.s_builtin.type->len;
+         break;
+       case 'h':
+         /* 16-bit half-precision FP */
+         ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[29]);
+         di->expansion += ret->u.s_builtin.type->len;
+         break;
+       case 's':
+         /* char16_t */
+         ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[30]);
+         di->expansion += ret->u.s_builtin.type->len;
+         break;
+       case 'i':
+         /* char32_t */
+         ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[31]);
+         di->expansion += ret->u.s_builtin.type->len;
+         break;
+
+       case 'F':
+         /* Fixed point types. DF<int bits><length><fract bits><sat>  */
+         ret = d_make_empty (di);
+         ret->type = DEMANGLE_COMPONENT_FIXED_TYPE;
+         if ((ret->u.s_fixed.accum = IS_DIGIT (d_peek_char (di))))
+           /* For demangling we don't care about the bits.  */
+           d_number (di);
+         ret->u.s_fixed.length = cplus_demangle_type (di);
+         if (ret->u.s_fixed.length == NULL)
+           return NULL;
+         d_number (di);
+         peek = d_next_char (di);
+         ret->u.s_fixed.sat = (peek == 's');
+         break;
+
+       case 'v':
+         ret = d_vector_type (di);
+         break;
+
+        case 'n':
+          /* decltype(nullptr) */
+         ret = d_make_builtin_type (di, &cplus_demangle_builtin_types[32]);
+         di->expansion += ret->u.s_builtin.type->len;
+         break;
+
+       default:
+         return NULL;
+       }
+      break;
+
     default:
       return NULL;
     }
@@ -1922,8 +2294,10 @@ static struct demangle_component **
 d_cv_qualifiers (struct d_info *di,
                  struct demangle_component **pret, int member_fn)
 {
+  struct demangle_component **pstart;
   char peek;
 
+  pstart = pret;
   peek = d_peek_char (di);
   while (peek == 'r' || peek == 'V' || peek == 'K')
     {
@@ -1960,6 +2334,28 @@ d_cv_qualifiers (struct d_info *di,
       peek = d_peek_char (di);
     }
 
+  if (!member_fn && peek == 'F')
+    {
+      while (pstart != pret)
+       {
+         switch ((*pstart)->type)
+           {
+           case DEMANGLE_COMPONENT_RESTRICT:
+             (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
+             break;
+           case DEMANGLE_COMPONENT_VOLATILE:
+             (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
+             break;
+           case DEMANGLE_COMPONENT_CONST:
+             (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
+             break;
+           default:
+             break;
+           }
+         pstart = &d_left (*pstart);
+       }
+    }
+
   return pret;
 }
 
@@ -1984,50 +2380,30 @@ d_function_type (struct d_info *di)
   return ret;
 }
 
-/* <bare-function-type> ::= [J]<type>+  */
+/* <type>+ */
 
 static struct demangle_component *
-d_bare_function_type (struct d_info *di, int has_return_type)
+d_parmlist (struct d_info *di)
 {
-  struct demangle_component *return_type;
   struct demangle_component *tl;
   struct demangle_component **ptl;
-  char peek;
-
-  /* Detect special qualifier indicating that the first argument
-     is the return type.  */
-  peek = d_peek_char (di);
-  if (peek == 'J')
-    {
-      d_advance (di, 1);
-      has_return_type = 1;
-    }
 
-  return_type = NULL;
   tl = NULL;
   ptl = &tl;
   while (1)
     {
       struct demangle_component *type;
 
-      peek = d_peek_char (di);
-      if (peek == '\0' || peek == 'E')
+      char peek = d_peek_char (di);
+      if (peek == '\0' || peek == 'E' || peek == '.')
        break;
       type = cplus_demangle_type (di);
       if (type == NULL)
        return NULL;
-      if (has_return_type)
-       {
-         return_type = type;
-         has_return_type = 0;
-       }
-      else
-       {
-         *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
-         if (*ptl == NULL)
-           return NULL;
-         ptl = &d_right (*ptl);
-       }
+      *ptl = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, type, NULL);
+      if (*ptl == NULL)
+       return NULL;
+      ptl = &d_right (*ptl);
     }
 
   /* There should be at least one parameter type besides the optional
@@ -2042,10 +2418,45 @@ d_bare_function_type (struct d_info *di, int has_return_type)
       && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
     {
       di->expansion -= d_left (tl)->u.s_builtin.type->len;
-      tl = NULL;
+      d_left (tl) = NULL;
+    }
+
+  return tl;
+}
+
+/* <bare-function-type> ::= [J]<type>+  */
+
+static struct demangle_component *
+d_bare_function_type (struct d_info *di, int has_return_type)
+{
+  struct demangle_component *return_type;
+  struct demangle_component *tl;
+  char peek;
+
+  /* Detect special qualifier indicating that the first argument
+     is the return type.  */
+  peek = d_peek_char (di);
+  if (peek == 'J')
+    {
+      d_advance (di, 1);
+      has_return_type = 1;
+    }
+
+  if (has_return_type)
+    {
+      return_type = cplus_demangle_type (di);
+      if (return_type == NULL)
+       return NULL;
     }
+  else
+    return_type = NULL;
+
+  tl = d_parmlist (di);
+  if (tl == NULL)
+    return NULL;
 
-  return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
+  return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE,
+                     return_type, tl);
 }
 
 /* <class-enum-type> ::= <name>  */
@@ -2101,6 +2512,34 @@ d_array_type (struct d_info *di)
                      cplus_demangle_type (di));
 }
 
+/* <vector-type> ::= Dv <number> _ <type>
+                 ::= Dv _ <expression> _ <type> */
+
+static struct demangle_component *
+d_vector_type (struct d_info *di)
+{
+  char peek;
+  struct demangle_component *dim;
+
+  peek = d_peek_char (di);
+  if (peek == '_')
+    {
+      d_advance (di, 1);
+      dim = d_expression (di);
+    }
+  else
+    dim = d_number_component (di);
+
+  if (dim == NULL)
+    return NULL;
+
+  if (! d_check_char (di, '_'))
+    return NULL;
+
+  return d_make_comp (di, DEMANGLE_COMPONENT_VECTOR_TYPE, dim,
+                     cplus_demangle_type (di));
+}
+
 /* <pointer-to-member-type> ::= M <(class) type> <(member) type>  */
 
 static struct demangle_component *
@@ -2135,6 +2574,8 @@ d_pointer_to_member_type (struct d_info *di)
   if (pmem == NULL)
     return NULL;
   *pmem = cplus_demangle_type (di);
+  if (*pmem == NULL)
+    return NULL;
 
   if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
     {
@@ -2145,11 +2586,29 @@ d_pointer_to_member_type (struct d_info *di)
   return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
 }
 
-/* <template-param> ::= T_
-                    ::= T <(parameter-2 non-negative) number> _
-*/
+/* <non-negative number> _ */
 
-static struct demangle_component *
+static long
+d_compact_number (struct d_info *di)
+{
+  long num;
+  if (d_peek_char (di) == '_')
+    num = 0;
+  else if (d_peek_char (di) == 'n')
+    return -1;
+  else
+    num = d_number (di) + 1;
+
+  if (! d_check_char (di, '_'))
+    return -1;
+  return num;
+}
+
+/* <template-param> ::= T_
+                    ::= T <(parameter-2 non-negative) number> _
+*/
+
+static struct demangle_component *
 d_template_param (struct d_info *di)
 {
   long param;
@@ -2157,17 +2616,8 @@ d_template_param (struct d_info *di)
   if (! d_check_char (di, 'T'))
     return NULL;
 
-  if (d_peek_char (di) == '_')
-    param = 0;
-  else
-    {
-      param = d_number (di);
-      if (param < 0)
-       return NULL;
-      param += 1;
-    }
-
-  if (! d_check_char (di, '_'))
+  param = d_compact_number (di);
+  if (param < 0)
     return NULL;
 
   ++di->did_subs;
@@ -2192,6 +2642,13 @@ d_template_args (struct d_info *di)
   if (! d_check_char (di, 'I'))
     return NULL;
 
+  if (d_peek_char (di) == 'E')
+    {
+      /* An argument pack can be empty.  */
+      d_advance (di, 1);
+      return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, NULL, NULL);
+    }
+
   al = NULL;
   pal = &al;
   while (1)
@@ -2241,14 +2698,54 @@ d_template_arg (struct d_info *di)
     case 'L':
       return d_expr_primary (di);
 
+    case 'I':
+      /* An argument pack.  */
+      return d_template_args (di);
+
     default:
       return cplus_demangle_type (di);
     }
 }
 
+/* Subroutine of <expression> ::= cl <expression>+ E */
+
+static struct demangle_component *
+d_exprlist (struct d_info *di)
+{
+  struct demangle_component *list = NULL;
+  struct demangle_component **p = &list;
+
+  if (d_peek_char (di) == 'E')
+    {
+      d_advance (di, 1);
+      return d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, NULL, NULL);
+    }
+
+  while (1)
+    {
+      struct demangle_component *arg = d_expression (di);
+      if (arg == NULL)
+       return NULL;
+
+      *p = d_make_comp (di, DEMANGLE_COMPONENT_ARGLIST, arg, NULL);
+      if (*p == NULL)
+       return NULL;
+      p = &d_right (*p);
+
+      if (d_peek_char (di) == 'E')
+       {
+         d_advance (di, 1);
+         break;
+       }
+    }
+
+  return list;
+}
+
 /* <expression> ::= <(unary) operator-name> <expression>
                 ::= <(binary) operator-name> <expression> <expression>
                 ::= <(trinary) operator-name> <expression> <expression> <expression>
+               ::= cl <expression>+ E
                 ::= st <type>
                 ::= <template-param>
                 ::= sr <type> <unqualified-name>
@@ -2281,6 +2778,51 @@ d_expression (struct d_info *di)
                            d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
                                         d_template_args (di)));
     }
+  else if (peek == 's' && d_peek_next_char (di) == 'p')
+    {
+      d_advance (di, 2);
+      return d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
+                         d_expression (di), NULL);
+    }
+  else if (peek == 'f' && d_peek_next_char (di) == 'p')
+    {
+      /* Function parameter used in a late-specified return type.  */
+      int index;
+      d_advance (di, 2);
+      if (d_peek_char (di) == 'T')
+       {
+         /* 'this' parameter.  */
+         d_advance (di, 1);
+         index = 0;
+       }
+      else
+       {
+         index = d_compact_number (di) + 1;
+         if (index == 0)
+           return NULL;
+       }
+      return d_make_function_param (di, index);
+    }
+  else if (IS_DIGIT (peek)
+          || (peek == 'o' && d_peek_next_char (di) == 'n'))
+    {
+      /* We can get an unqualified name as an expression in the case of
+         a dependent function call, i.e. decltype(f(t)).  */
+      struct demangle_component *name;
+
+      if (peek == 'o')
+       /* operator-function-id, i.e. operator+(t).  */
+       d_advance (di, 2);
+
+      name = d_unqualified_name (di);
+      if (name == NULL)
+       return NULL;
+      if (d_peek_char (di) == 'I')
+       return d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
+                           d_template_args (di));
+      else
+       return name;
+    }
   else
     {
       struct demangle_component *op;
@@ -2316,18 +2858,39 @@ d_expression (struct d_info *di)
       switch (args)
        {
        case 1:
-         return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
-                             d_expression (di));
+         {
+           struct demangle_component *operand;
+           if (op->type == DEMANGLE_COMPONENT_CAST
+               && d_check_char (di, '_'))
+             operand = d_exprlist (di);
+           else
+             operand = d_expression (di);
+           return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
+                               operand);
+         }
        case 2:
          {
            struct demangle_component *left;
+           struct demangle_component *right;
+           const char *code = op->u.s_operator.op->code;
 
            left = d_expression (di);
+           if (!strcmp (code, "cl"))
+             right = d_exprlist (di);
+           else if (!strcmp (code, "dt") || !strcmp (code, "pt"))
+             {
+               right = d_unqualified_name (di);
+               if (d_peek_char (di) == 'I')
+                 right = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE,
+                                      right, d_template_args (di));
+             }
+           else
+             right = d_expression (di);
+
            return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
                                d_make_comp (di,
                                             DEMANGLE_COMPONENT_BINARY_ARGS,
-                                            left,
-                                            d_expression (di)));
+                                            left, right));
          }
        case 3:
          {
@@ -2363,7 +2926,9 @@ d_expr_primary (struct d_info *di)
 
   if (! d_check_char (di, 'L'))
     return NULL;
-  if (d_peek_char (di) == '_')
+  if (d_peek_char (di) == '_'
+      /* Workaround for G++ bug; see comment in write_template_arg.  */
+      || d_peek_char (di) == 'Z')
     ret = cplus_demangle_mangled_name (di, 0);
   else
     {
@@ -2441,10 +3006,31 @@ d_local_name (struct d_info *di)
   else
     {
       struct demangle_component *name;
+      int num = -1;
+
+      if (d_peek_char (di) == 'd')
+       {
+         /* Default argument scope: d <number> _.  */
+         d_advance (di, 1);
+         num = d_compact_number (di);
+         if (num < 0)
+           return NULL;
+       }
 
       name = d_name (di);
-      if (! d_discriminator (di))
-       return NULL;
+      if (name)
+       switch (name->type)
+         {
+           /* Lambdas and unnamed types have internal discriminators.  */
+         case DEMANGLE_COMPONENT_LAMBDA:
+         case DEMANGLE_COMPONENT_UNNAMED_TYPE:
+           break;
+         default:
+           if (! d_discriminator (di))
+             return NULL;
+         }
+      if (num >= 0)
+       name = d_make_default_arg (di, num, name);
       return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
     }
 }
@@ -2468,6 +3054,102 @@ d_discriminator (struct d_info *di)
   return 1;
 }
 
+/* <closure-type-name> ::= Ul <lambda-sig> E [ <nonnegative number> ] _ */
+
+static struct demangle_component *
+d_lambda (struct d_info *di)
+{
+  struct demangle_component *tl;
+  struct demangle_component *ret;
+  int num;
+
+  if (! d_check_char (di, 'U'))
+    return NULL;
+  if (! d_check_char (di, 'l'))
+    return NULL;
+
+  tl = d_parmlist (di);
+  if (tl == NULL)
+    return NULL;
+
+  if (! d_check_char (di, 'E'))
+    return NULL;
+
+  num = d_compact_number (di);
+  if (num < 0)
+    return NULL;
+
+  ret = d_make_empty (di);
+  if (ret)
+    {
+      ret->type = DEMANGLE_COMPONENT_LAMBDA;
+      ret->u.s_unary_num.sub = tl;
+      ret->u.s_unary_num.num = num;
+    }
+
+  if (! d_add_substitution (di, ret))
+    return NULL;
+
+  return ret;
+}
+
+/* <unnamed-type-name> ::= Ut [ <nonnegative number> ] _ */
+
+static struct demangle_component *
+d_unnamed_type (struct d_info *di)
+{
+  struct demangle_component *ret;
+  long num;
+
+  if (! d_check_char (di, 'U'))
+    return NULL;
+  if (! d_check_char (di, 't'))
+    return NULL;
+
+  num = d_compact_number (di);
+  if (num < 0)
+    return NULL;
+
+  ret = d_make_empty (di);
+  if (ret)
+    {
+      ret->type = DEMANGLE_COMPONENT_UNNAMED_TYPE;
+      ret->u.s_number.number = num;
+    }
+
+  if (! d_add_substitution (di, ret))
+    return NULL;
+
+  return ret;
+}
+
+/* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
+*/
+
+static struct demangle_component *
+d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
+{
+  const char *suffix = d_str (di);
+  const char *pend = suffix;
+  struct demangle_component *n;
+
+  if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
+    {
+      pend += 2;
+      while (IS_LOWER (*pend) || *pend == '_')
+       ++pend;
+    }
+  while (*pend == '.' && IS_DIGIT (pend[1]))
+    {
+      pend += 2;
+      while (IS_DIGIT (*pend))
+       ++pend;
+    }
+  d_advance (di, pend - suffix);
+  n = d_make_name (di, suffix, pend - suffix);
+  return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
+}
+
 /* Add a new substitution.  */
 
 static int
@@ -2537,21 +3219,24 @@ d_substitution (struct d_info *di, int prefix)
   c = d_next_char (di);
   if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
     {
-      int id;
+      unsigned int id;
 
       id = 0;
       if (c != '_')
        {
          do
            {
+             unsigned int new_id;
+
              if (IS_DIGIT (c))
-               id = id * 36 + c - '0';
+               new_id = id * 36 + c - '0';
              else if (IS_UPPER (c))
-               id = id * 36 + c - 'A' + 10;
+               new_id = id * 36 + c - 'A' + 10;
              else
                return NULL;
-             if (id < 0)
+             if (new_id < id)
                return NULL;
+             id = new_id;
              c = d_next_char (di);
            }
          while (c != '_');
@@ -2559,7 +3244,7 @@ d_substitution (struct d_info *di, int prefix)
          ++id;
        }
 
-      if (id >= di->next_sub)
+      if (id >= (unsigned int) di->next_sub)
        return NULL;
 
       ++di->did_subs;
@@ -2692,14 +3377,15 @@ d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
 /* Initialize a print information structure.  */
 
 static void
-d_print_init (struct d_print_info *dpi, int options,
-              demangle_callbackref callback, void *opaque)
+d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
+             void *opaque)
 {
-  dpi->options = options;
   dpi->len = 0;
   dpi->last_char = '\0';
   dpi->templates = NULL;
   dpi->modifiers = NULL;
+  dpi->pack_index = 0;
+  dpi->flush_count = 0;
 
   dpi->callback = callback;
   dpi->opaque = opaque;
@@ -2729,6 +3415,7 @@ d_print_flush (struct d_print_info *dpi)
   dpi->buf[dpi->len] = '\0';
   dpi->callback (dpi->buf, dpi->len, dpi->opaque);
   dpi->len = 0;
+  dpi->flush_count++;
 }
 
 /* Append characters and buffers for printing.  */
@@ -2758,6 +3445,14 @@ d_append_string (struct d_print_info *dpi, const char *s)
   d_append_buffer (dpi, s, strlen (s));
 }
 
+static inline void
+d_append_num (struct d_print_info *dpi, long l)
+{
+  char buf[25];
+  sprintf (buf,"%ld", l);
+  d_append_string (dpi, buf);
+}
+
 static inline char
 d_last_char (struct d_print_info *dpi)
 {
@@ -2781,9 +3476,9 @@ cplus_demangle_print_callback (int options,
 {
   struct d_print_info dpi;
 
-  d_print_init (&dpi, options, callback, opaque);
+  d_print_init (&dpi, callback, opaque);
 
-  d_print_comp (&dpi, dc);
+  d_print_comp (&dpi, options, dc);
 
   d_print_flush (&dpi);
 
@@ -2820,12 +3515,136 @@ cplus_demangle_print (int options, const struct demangle_component *dc,
   return dgs.buf;
 }
 
+/* Returns the I'th element of the template arglist ARGS, or NULL on
+   failure.  */
+
+static struct demangle_component *
+d_index_template_argument (struct demangle_component *args, int i)
+{
+  struct demangle_component *a;
+
+  for (a = args;
+       a != NULL;
+       a = d_right (a))
+    {
+      if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
+       return NULL;
+      if (i <= 0)
+       break;
+      --i;
+    }
+  if (i != 0 || a == NULL)
+    return NULL;
+
+  return d_left (a);
+}
+
+/* Returns the template argument from the current context indicated by DC,
+   which is a DEMANGLE_COMPONENT_TEMPLATE_PARAM, or NULL.  */
+
+static struct demangle_component *
+d_lookup_template_argument (struct d_print_info *dpi,
+                           const struct demangle_component *dc)
+{
+  if (dpi->templates == NULL)
+    {
+      d_print_error (dpi);
+      return NULL;
+    }
+       
+  return d_index_template_argument
+    (d_right (dpi->templates->template_decl),
+     dc->u.s_number.number);
+}
+
+/* Returns a template argument pack used in DC (any will do), or NULL.  */
+
+static struct demangle_component *
+d_find_pack (struct d_print_info *dpi,
+            const struct demangle_component *dc)
+{
+  struct demangle_component *a;
+  if (dc == NULL)
+    return NULL;
+
+  switch (dc->type)
+    {
+    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
+      a = d_lookup_template_argument (dpi, dc);
+      if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
+       return a;
+      return NULL;
+
+    case DEMANGLE_COMPONENT_PACK_EXPANSION:
+      return NULL;
+      
+    case DEMANGLE_COMPONENT_LAMBDA:
+    case DEMANGLE_COMPONENT_NAME:
+    case DEMANGLE_COMPONENT_OPERATOR:
+    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
+    case DEMANGLE_COMPONENT_SUB_STD:
+    case DEMANGLE_COMPONENT_CHARACTER:
+    case DEMANGLE_COMPONENT_FUNCTION_PARAM:
+      return NULL;
+
+    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
+      return d_find_pack (dpi, dc->u.s_extended_operator.name);
+    case DEMANGLE_COMPONENT_CTOR:
+      return d_find_pack (dpi, dc->u.s_ctor.name);
+    case DEMANGLE_COMPONENT_DTOR:
+      return d_find_pack (dpi, dc->u.s_dtor.name);
+
+    default:
+      a = d_find_pack (dpi, d_left (dc));
+      if (a)
+       return a;
+      return d_find_pack (dpi, d_right (dc));
+    }
+}
+
+/* Returns the length of the template argument pack DC.  */
+
+static int
+d_pack_length (const struct demangle_component *dc)
+{
+  int count = 0;
+  while (dc && dc->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST
+        && d_left (dc) != NULL)
+    {
+      ++count;
+      dc = d_right (dc);
+    }
+  return count;
+}
+
+/* DC is a component of a mangled expression.  Print it, wrapped in parens
+   if needed.  */
+
+static void
+d_print_subexpr (struct d_print_info *dpi, int options,
+                const struct demangle_component *dc)
+{
+  int simple = 0;
+  if (dc->type == DEMANGLE_COMPONENT_NAME
+      || dc->type == DEMANGLE_COMPONENT_FUNCTION_PARAM)
+    simple = 1;
+  if (!simple)
+    d_append_char (dpi, '(');
+  d_print_comp (dpi, options, dc);
+  if (!simple)
+    d_append_char (dpi, ')');
+}
+
 /* Subroutine to handle components.  */
 
 static void
-d_print_comp (struct d_print_info *dpi,
+d_print_comp (struct d_print_info *dpi, int options,
               const struct demangle_component *dc)
 {
+  /* Magic variable to let reference smashing skip over the next modifier
+     without needing to modify *dc.  */
+  const struct demangle_component *mod_inner = NULL;
+
   if (dc == NULL)
     {
       d_print_error (dpi);
@@ -2837,7 +3656,7 @@ d_print_comp (struct d_print_info *dpi,
   switch (dc->type)
     {
     case DEMANGLE_COMPONENT_NAME:
-      if ((dpi->options & DMGL_JAVA) == 0)
+      if ((options & DMGL_JAVA) == 0)
        d_append_buffer (dpi, dc->u.s_name.s, dc->u.s_name.len);
       else
        d_print_java_identifier (dpi, dc->u.s_name.s, dc->u.s_name.len);
@@ -2845,12 +3664,12 @@ d_print_comp (struct d_print_info *dpi,
 
     case DEMANGLE_COMPONENT_QUAL_NAME:
     case DEMANGLE_COMPONENT_LOCAL_NAME:
-      d_print_comp (dpi, d_left (dc));
-      if ((dpi->options & DMGL_JAVA) == 0)
+      d_print_comp (dpi, options, d_left (dc));
+      if ((options & DMGL_JAVA) == 0)
        d_append_string (dpi, "::");
       else
        d_append_char (dpi, '.');
-      d_print_comp (dpi, d_right (dc));
+      d_print_comp (dpi, options, d_right (dc));
       return;
 
     case DEMANGLE_COMPONENT_TYPED_NAME:
@@ -2865,6 +3684,7 @@ d_print_comp (struct d_print_info *dpi,
           the right place for the type.  We also have to pass down
           any CV-qualifiers, which apply to the this parameter.  */
        hold_modifiers = dpi->modifiers;
+       dpi->modifiers = 0;
        i = 0;
        typed_name = d_left (dc);
        while (typed_name != NULL)
@@ -2890,6 +3710,12 @@ d_print_comp (struct d_print_info *dpi,
            typed_name = d_left (typed_name);
          }
 
+       if (typed_name == NULL)
+         {
+           d_print_error (dpi);
+           return;
+         }
+
        /* If typed_name is a template, then it applies to the
           function type as well.  */
        if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
@@ -2908,6 +3734,8 @@ d_print_comp (struct d_print_info *dpi,
            struct demangle_component *local_name;
 
            local_name = d_right (typed_name);
+           if (local_name->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
+             local_name = local_name->u.s_unary_num.sub;
            while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
                   || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
                   || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
@@ -2931,7 +3759,7 @@ d_print_comp (struct d_print_info *dpi,
              }
          }
 
-       d_print_comp (dpi, d_right (dc));
+       d_print_comp (dpi, options, d_right (dc));
 
        if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
          dpi->templates = dpt.next;
@@ -2944,7 +3772,7 @@ d_print_comp (struct d_print_info *dpi,
            if (! adpm[i].printed)
              {
                d_append_char (dpi, ' ');
-               d_print_mod (dpi, adpm[i].mod);
+               d_print_mod (dpi, options, adpm[i].mod);
              }
          }
 
@@ -2967,7 +3795,7 @@ d_print_comp (struct d_print_info *dpi,
 
         dcl = d_left (dc);
 
-        if ((dpi->options & DMGL_JAVA) != 0
+        if ((options & DMGL_JAVA) != 0
             && dcl->type == DEMANGLE_COMPONENT_NAME
             && dcl->u.s_name.len == 6
             && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
@@ -2975,16 +3803,16 @@ d_print_comp (struct d_print_info *dpi,
             /* Special-case Java arrays, so that JArray<TYPE> appears
                instead as TYPE[].  */
 
-            d_print_comp (dpi, d_right (dc));
+            d_print_comp (dpi, options, d_right (dc));
             d_append_string (dpi, "[]");
           }
         else
           {
-           d_print_comp (dpi, dcl);
+           d_print_comp (dpi, options, dcl);
            if (d_last_char (dpi) == '<')
              d_append_char (dpi, ' ');
            d_append_char (dpi, '<');
-           d_print_comp (dpi, d_right (dc));
+           d_print_comp (dpi, options, d_right (dc));
            /* Avoid generating two consecutive '>' characters, to avoid
               the C++ syntactic ambiguity.  */
            if (d_last_char (dpi) == '>')
@@ -2999,30 +3827,13 @@ d_print_comp (struct d_print_info *dpi,
 
     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
       {
-       long i;
-       struct demangle_component *a;
        struct d_print_template *hold_dpt;
+       struct demangle_component *a = d_lookup_template_argument (dpi, dc);
 
-       if (dpi->templates == NULL)
-         {
-           d_print_error (dpi);
-           return;
-         }
-       i = dc->u.s_number.number;
-       for (a = d_right (dpi->templates->template_decl);
-            a != NULL;
-            a = d_right (a))
-         {
-           if (a->type != DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
-             {
-               d_print_error (dpi);
-               return;
-             }
-           if (i <= 0)
-             break;
-           --i;
-         }
-       if (i != 0 || a == NULL)
+       if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
+         a = d_index_template_argument (a, dpi->pack_index);
+
+       if (a == NULL)
          {
            d_print_error (dpi);
            return;
@@ -3036,7 +3847,7 @@ d_print_comp (struct d_print_info *dpi,
        hold_dpt = dpi->templates;
        dpi->templates = hold_dpt->next;
 
-       d_print_comp (dpi, d_left (a));
+       d_print_comp (dpi, options, a);
 
        dpi->templates = hold_dpt;
 
@@ -3044,79 +3855,79 @@ d_print_comp (struct d_print_info *dpi,
       }
 
     case DEMANGLE_COMPONENT_CTOR:
-      d_print_comp (dpi, dc->u.s_ctor.name);
+      d_print_comp (dpi, options, dc->u.s_ctor.name);
       return;
 
     case DEMANGLE_COMPONENT_DTOR:
       d_append_char (dpi, '~');
-      d_print_comp (dpi, dc->u.s_dtor.name);
+      d_print_comp (dpi, options, dc->u.s_dtor.name);
       return;
 
     case DEMANGLE_COMPONENT_VTABLE:
       d_append_string (dpi, "vtable for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_VTT:
       d_append_string (dpi, "VTT for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
       d_append_string (dpi, "construction vtable for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       d_append_string (dpi, "-in-");
-      d_print_comp (dpi, d_right (dc));
+      d_print_comp (dpi, options, d_right (dc));
       return;
 
     case DEMANGLE_COMPONENT_TYPEINFO:
       d_append_string (dpi, "typeinfo for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
       d_append_string (dpi, "typeinfo name for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_TYPEINFO_FN:
       d_append_string (dpi, "typeinfo fn for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_THUNK:
       d_append_string (dpi, "non-virtual thunk to ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
       d_append_string (dpi, "virtual thunk to ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
       d_append_string (dpi, "covariant return thunk to ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_JAVA_CLASS:
       d_append_string (dpi, "java Class for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_GUARD:
       d_append_string (dpi, "guard variable for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_REFTEMP:
       d_append_string (dpi, "reference temporary for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
       d_append_string (dpi, "hidden alias for ");
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_SUB_STD:
@@ -3143,21 +3954,50 @@ d_print_comp (struct d_print_info *dpi,
                  break;
                if (pdpm->mod->type == dc->type)
                  {
-                   d_print_comp (dpi, d_left (dc));
+                   d_print_comp (dpi, options, d_left (dc));
                    return;
                  }
              }
          }
       }
+      goto modifier;
+
+    case DEMANGLE_COMPONENT_REFERENCE:
+    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
+      {
+       /* Handle reference smashing: & + && = &.  */
+       const struct demangle_component *sub = d_left (dc);
+       if (sub->type == DEMANGLE_COMPONENT_TEMPLATE_PARAM)
+         {
+           struct demangle_component *a = d_lookup_template_argument (dpi, sub);
+           if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
+             a = d_index_template_argument (a, dpi->pack_index);
+
+           if (a == NULL)
+             {
+               d_print_error (dpi);
+               return;
+             }
+
+           sub = a;
+         }
+
+       if (sub->type == DEMANGLE_COMPONENT_REFERENCE
+           || sub->type == dc->type)
+         dc = sub;
+       else if (sub->type == DEMANGLE_COMPONENT_RVALUE_REFERENCE)
+         mod_inner = d_left (sub);
+      }
       /* Fall through.  */
+
     case DEMANGLE_COMPONENT_RESTRICT_THIS:
     case DEMANGLE_COMPONENT_VOLATILE_THIS:
     case DEMANGLE_COMPONENT_CONST_THIS:
     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
     case DEMANGLE_COMPONENT_POINTER:
-    case DEMANGLE_COMPONENT_REFERENCE:
     case DEMANGLE_COMPONENT_COMPLEX:
     case DEMANGLE_COMPONENT_IMAGINARY:
+    modifier:
       {
        /* We keep a list of modifiers on the stack.  */
        struct d_print_mod dpm;
@@ -3168,12 +4008,15 @@ d_print_comp (struct d_print_info *dpi,
        dpm.printed = 0;
        dpm.templates = dpi->templates;
 
-       d_print_comp (dpi, d_left (dc));
+       if (!mod_inner)
+         mod_inner = d_left (dc);
+
+       d_print_comp (dpi, options, mod_inner);
 
        /* If the modifier didn't get printed by the type, print it
           now.  */
        if (! dpm.printed)
-         d_print_mod (dpi, dc);
+         d_print_mod (dpi, options, dc);
 
        dpi->modifiers = dpm.next;
 
@@ -3181,7 +4024,7 @@ d_print_comp (struct d_print_info *dpi,
       }
 
     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
-      if ((dpi->options & DMGL_JAVA) == 0)
+      if ((options & DMGL_JAVA) == 0)
        d_append_buffer (dpi, dc->u.s_builtin.type->name,
                         dc->u.s_builtin.type->len);
       else
@@ -3190,16 +4033,21 @@ d_print_comp (struct d_print_info *dpi,
       return;
 
     case DEMANGLE_COMPONENT_VENDOR_TYPE:
-      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, options, d_left (dc));
       return;
 
     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
       {
-       if ((dpi->options & DMGL_RET_POSTFIX) != 0)
-         d_print_function_type (dpi, dc, dpi->modifiers);
+       if ((options & DMGL_RET_POSTFIX) != 0)
+         d_print_function_type (dpi,
+                                options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
+                                dc, dpi->modifiers);
 
        /* Print return type if present */
-       if (d_left (dc) != NULL)
+       if (d_left (dc) != NULL && (options & DMGL_RET_POSTFIX) != 0)
+         d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
+                       d_left (dc));
+       else if (d_left (dc) != NULL && (options & DMGL_RET_DROP) == 0)
          {
            struct d_print_mod dpm;
 
@@ -3211,7 +4059,8 @@ d_print_comp (struct d_print_info *dpi,
            dpm.printed = 0;
            dpm.templates = dpi->templates;
 
-           d_print_comp (dpi, d_left (dc));
+           d_print_comp (dpi, options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
+                         d_left (dc));
 
            dpi->modifiers = dpm.next;
 
@@ -3220,12 +4069,14 @@ d_print_comp (struct d_print_info *dpi,
 
            /* In standard prefix notation, there is a space between the
               return type and the function signature.  */
-           if ((dpi->options & DMGL_RET_POSTFIX) == 0)
+           if ((options & DMGL_RET_POSTFIX) == 0)
              d_append_char (dpi, ' ');
          }
 
-       if ((dpi->options & DMGL_RET_POSTFIX) == 0) 
-         d_print_function_type (dpi, dc, dpi->modifiers);
+       if ((options & DMGL_RET_POSTFIX) == 0)
+         d_print_function_type (dpi,
+                                options & ~(DMGL_RET_POSTFIX | DMGL_RET_DROP),
+                                dc, dpi->modifiers);
 
        return;
       }
@@ -3278,7 +4129,7 @@ d_print_comp (struct d_print_info *dpi,
            pdpm = pdpm->next;
          }
 
-       d_print_comp (dpi, d_right (dc));
+       d_print_comp (dpi, options, d_right (dc));
 
        dpi->modifiers = hold_modifiers;
 
@@ -3288,15 +4139,16 @@ d_print_comp (struct d_print_info *dpi,
        while (i > 1)
          {
            --i;
-           d_print_mod (dpi, adpm[i].mod);
+           d_print_mod (dpi, options, adpm[i].mod);
          }
 
-       d_print_array_type (dpi, dc, dpi->modifiers);
+       d_print_array_type (dpi, options, dc, dpi->modifiers);
 
        return;
       }
 
     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
+    case DEMANGLE_COMPONENT_VECTOR_TYPE:
       {
        struct d_print_mod dpm;
 
@@ -3306,29 +4158,54 @@ d_print_comp (struct d_print_info *dpi,
        dpm.printed = 0;
        dpm.templates = dpi->templates;
 
-       d_print_comp (dpi, d_right (dc));
+       d_print_comp (dpi, options, d_right (dc));
 
        /* If the modifier didn't get printed by the type, print it
           now.  */
        if (! dpm.printed)
-         {
-           d_append_char (dpi, ' ');
-           d_print_comp (dpi, d_left (dc));
-           d_append_string (dpi, "::*");
-         }
+         d_print_mod (dpi, options, dc);
 
        dpi->modifiers = dpm.next;
 
        return;
       }
 
+    case DEMANGLE_COMPONENT_FIXED_TYPE:
+      if (dc->u.s_fixed.sat)
+       d_append_string (dpi, "_Sat ");
+      /* Don't print "int _Accum".  */
+      if (dc->u.s_fixed.length->u.s_builtin.type
+         != &cplus_demangle_builtin_types['i'-'a'])
+       {
+         d_print_comp (dpi, options, dc->u.s_fixed.length);
+         d_append_char (dpi, ' ');
+       }
+      if (dc->u.s_fixed.accum)
+       d_append_string (dpi, "_Accum");
+      else
+       d_append_string (dpi, "_Fract");
+      return;
+
     case DEMANGLE_COMPONENT_ARGLIST:
     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
-      d_print_comp (dpi, d_left (dc));
+      if (d_left (dc) != NULL)
+       d_print_comp (dpi, options, d_left (dc));
       if (d_right (dc) != NULL)
        {
+         size_t len;
+         unsigned long int flush_count;
+         /* Make sure ", " isn't flushed by d_append_string, otherwise
+            dpi->len -= 2 wouldn't work.  */
+         if (dpi->len >= sizeof (dpi->buf) - 2)
+           d_print_flush (dpi);
          d_append_string (dpi, ", ");
-         d_print_comp (dpi, d_right (dc));
+         len = dpi->len;
+         flush_count = dpi->flush_count;
+         d_print_comp (dpi, options, d_right (dc));
+         /* If that didn't print anything (which can happen with empty
+            template argument packs), remove the comma and space.  */
+         if (dpi->flush_count == flush_count && dpi->len == len)
+           dpi->len -= 2;
        }
       return;
 
@@ -3347,26 +4224,63 @@ d_print_comp (struct d_print_info *dpi,
 
     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
       d_append_string (dpi, "operator ");
-      d_print_comp (dpi, dc->u.s_extended_operator.name);
+      d_print_comp (dpi, options, dc->u.s_extended_operator.name);
       return;
 
     case DEMANGLE_COMPONENT_CAST:
       d_append_string (dpi, "operator ");
-      d_print_cast (dpi, dc);
+      d_print_cast (dpi, options, dc);
       return;
 
     case DEMANGLE_COMPONENT_UNARY:
-      if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
-       d_print_expr_op (dpi, d_left (dc));
+      if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
+         && d_left (dc)->u.s_operator.op->len == 1
+         && d_left (dc)->u.s_operator.op->name[0] == '&'
+         && d_right (dc)->type == DEMANGLE_COMPONENT_TYPED_NAME
+         && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_QUAL_NAME
+         && d_right (d_right (dc))->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
+       {
+         /* Address of a function (therefore in an expression context) must
+            have its argument list suppressed.
+
+            unary operator ... dc
+              operator & ... d_left (dc)
+              typed name ... d_right (dc)
+                qualified name ... d_left (d_right (dc))
+                  <names>
+                function type ... d_right (d_right (dc))
+                  argument list
+                    <arguments>  */
+
+         d_print_expr_op (dpi, options, d_left (dc));
+         d_print_comp (dpi, options, d_left (d_right (dc)));
+         return;
+       }
+      else if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
+              && d_left (dc)->u.s_operator.op->len == 1
+              && d_left (dc)->u.s_operator.op->name[0] == '&'
+              && d_right (dc)->type == DEMANGLE_COMPONENT_QUAL_NAME)
+       {
+         /* Keep also already processed variant without the argument list.
+
+            unary operator ... dc
+              operator & ... d_left (dc)
+              qualified name ... d_right (dc)
+                <names>  */
+
+         d_print_expr_op (dpi, options, d_left (dc));
+         d_print_comp (dpi, options, d_right (dc));
+         return;
+       }
+      else if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
+       d_print_expr_op (dpi, options, d_left (dc));
       else
        {
          d_append_char (dpi, '(');
-         d_print_cast (dpi, d_left (dc));
+         d_print_cast (dpi, options, d_left (dc));
          d_append_char (dpi, ')');
        }
-      d_append_char (dpi, '(');
-      d_print_comp (dpi, d_right (dc));
-      d_append_char (dpi, ')');
+      d_print_subexpr (dpi, options, d_right (dc));
       return;
 
     case DEMANGLE_COMPONENT_BINARY:
@@ -3384,13 +4298,33 @@ d_print_comp (struct d_print_info *dpi,
          && d_left (dc)->u.s_operator.op->name[0] == '>')
        d_append_char (dpi, '(');
 
-      d_append_char (dpi, '(');
-      d_print_comp (dpi, d_left (d_right (dc)));
-      d_append_string (dpi, ") ");
-      d_print_expr_op (dpi, d_left (dc));
-      d_append_string (dpi, " (");
-      d_print_comp (dpi, d_right (d_right (dc)));
-      d_append_char (dpi, ')');
+      if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
+          && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
+       {
+         /* Function call used in an expression should not have printed types
+            of the function arguments.  Values of the function arguments still
+            get printed below.  */
+
+         const struct demangle_component *func = d_left (d_right (dc));
+
+         if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
+           d_print_error (dpi);
+         d_print_subexpr (dpi, options, d_left (func));
+       }
+      else
+       d_print_subexpr (dpi, options, d_left (d_right (dc)));
+      if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
+       {
+         d_append_char (dpi, '[');
+         d_print_comp (dpi, options, d_right (d_right (dc)));
+         d_append_char (dpi, ']');
+       }
+      else
+       {
+         if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
+           d_print_expr_op (dpi, options, d_left (dc));
+         d_print_subexpr (dpi, options, d_right (d_right (dc)));
+       }
 
       if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
          && d_left (dc)->u.s_operator.op->len == 1
@@ -3411,15 +4345,11 @@ d_print_comp (struct d_print_info *dpi,
          d_print_error (dpi);
          return;
        }
-      d_append_char (dpi, '(');
-      d_print_comp (dpi, d_left (d_right (dc)));
-      d_append_string (dpi, ") ");
-      d_print_expr_op (dpi, d_left (dc));
-      d_append_string (dpi, " (");
-      d_print_comp (dpi, d_left (d_right (d_right (dc))));
-      d_append_string (dpi, ") : (");
-      d_print_comp (dpi, d_right (d_right (d_right (dc))));
-      d_append_char (dpi, ')');
+      d_print_subexpr (dpi, options, d_left (d_right (dc)));
+      d_print_expr_op (dpi, options, d_left (dc));
+      d_print_subexpr (dpi, options, d_left (d_right (d_right (dc))));
+      d_append_string (dpi, " : ");
+      d_print_subexpr (dpi, options, d_right (d_right (d_right (dc))));
       return;
 
     case DEMANGLE_COMPONENT_TRINARY_ARG1:
@@ -3450,7 +4380,7 @@ d_print_comp (struct d_print_info *dpi,
                  {
                    if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
                      d_append_char (dpi, '-');
-                   d_print_comp (dpi, d_right (dc));
+                   d_print_comp (dpi, options, d_right (dc));
                    switch (tp)
                      {
                      default:
@@ -3500,18 +4430,114 @@ d_print_comp (struct d_print_info *dpi,
          }
 
        d_append_char (dpi, '(');
-       d_print_comp (dpi, d_left (dc));
+       d_print_comp (dpi, options, d_left (dc));
        d_append_char (dpi, ')');
        if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
          d_append_char (dpi, '-');
        if (tp == D_PRINT_FLOAT)
          d_append_char (dpi, '[');
-       d_print_comp (dpi, d_right (dc));
+       d_print_comp (dpi, options, d_right (dc));
        if (tp == D_PRINT_FLOAT)
          d_append_char (dpi, ']');
       }
       return;
 
+    case DEMANGLE_COMPONENT_NUMBER:
+      d_append_num (dpi, dc->u.s_number.number);
+      return;
+
+    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
+      d_append_string (dpi, "java resource ");
+      d_print_comp (dpi, options, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_COMPOUND_NAME:
+      d_print_comp (dpi, options, d_left (dc));
+      d_print_comp (dpi, options, d_right (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_CHARACTER:
+      d_append_char (dpi, dc->u.s_character.character);
+      return;
+
+    case DEMANGLE_COMPONENT_DECLTYPE:
+      d_append_string (dpi, "decltype (");
+      d_print_comp (dpi, options, d_left (dc));
+      d_append_char (dpi, ')');
+      return;
+
+    case DEMANGLE_COMPONENT_PACK_EXPANSION:
+      {
+       int len;
+       int i;
+       struct demangle_component *a = d_find_pack (dpi, d_left (dc));
+       if (a == NULL)
+         {
+           /* d_find_pack won't find anything if the only packs involved
+              in this expansion are function parameter packs; in that
+              case, just print the pattern and "...".  */
+           d_print_subexpr (dpi, options, d_left (dc));
+           d_append_string (dpi, "...");
+           return;
+         }
+
+       len = d_pack_length (a);
+       dc = d_left (dc);
+       for (i = 0; i < len; ++i)
+         {
+           dpi->pack_index = i;
+           d_print_comp (dpi, options, dc);
+           if (i < len-1)
+             d_append_string (dpi, ", ");
+         }
+      }
+      return;
+
+    case DEMANGLE_COMPONENT_FUNCTION_PARAM:
+      {
+       long num = dc->u.s_number.number;
+       if (num == 0)
+         d_append_string (dpi, "this");
+       else
+         {
+           d_append_string (dpi, "{parm#");
+           d_append_num (dpi, num);
+           d_append_char (dpi, '}');
+         }
+      }
+      return;
+
+    case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
+      d_append_string (dpi, "global constructors keyed to ");
+      d_print_comp (dpi, options, dc->u.s_binary.left);
+      return;
+
+    case DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS:
+      d_append_string (dpi, "global destructors keyed to ");
+      d_print_comp (dpi, options, dc->u.s_binary.left);
+      return;
+
+    case DEMANGLE_COMPONENT_LAMBDA:
+      d_append_string (dpi, "{lambda(");
+      d_print_comp (dpi, options, dc->u.s_unary_num.sub);
+      d_append_string (dpi, ")#");
+      d_append_num (dpi, dc->u.s_unary_num.num + 1);
+      d_append_char (dpi, '}');
+      return;
+
+    case DEMANGLE_COMPONENT_UNNAMED_TYPE:
+      d_append_string (dpi, "{unnamed type#");
+      d_append_num (dpi, dc->u.s_number.number + 1);
+      d_append_char (dpi, '}');
+      return;
+
+    case DEMANGLE_COMPONENT_CLONE:
+      d_print_comp (dpi, options, d_left (dc));
+      d_append_string (dpi, " [clone ");
+      d_print_comp (dpi, options, d_right (dc));
+      d_append_char (dpi, ']');
+      return;
+
     default:
       d_print_error (dpi);
       return;
@@ -3574,7 +4600,7 @@ d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
    qualifiers on this after printing a function.  */
 
 static void
-d_print_mod_list (struct d_print_info *dpi,
+d_print_mod_list (struct d_print_info *dpi, int options,
                   struct d_print_mod *mods, int suffix)
 {
   struct d_print_template *hold_dpt;
@@ -3588,7 +4614,7 @@ d_print_mod_list (struct d_print_info *dpi,
              || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
              || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
     {
-      d_print_mod_list (dpi, mods->next, suffix);
+      d_print_mod_list (dpi, options, mods->next, suffix);
       return;
     }
 
@@ -3599,13 +4625,13 @@ d_print_mod_list (struct d_print_info *dpi,
 
   if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
     {
-      d_print_function_type (dpi, mods->mod, mods->next);
+      d_print_function_type (dpi, options, mods->mod, mods->next);
       dpi->templates = hold_dpt;
       return;
     }
   else if (mods->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
     {
-      d_print_array_type (dpi, mods->mod, mods->next);
+      d_print_array_type (dpi, options, mods->mod, mods->next);
       dpi->templates = hold_dpt;
       return;
     }
@@ -3621,37 +4647,46 @@ d_print_mod_list (struct d_print_info *dpi,
 
       hold_modifiers = dpi->modifiers;
       dpi->modifiers = NULL;
-      d_print_comp (dpi, d_left (mods->mod));
+      d_print_comp (dpi, options, d_left (mods->mod));
       dpi->modifiers = hold_modifiers;
 
-      if ((dpi->options & DMGL_JAVA) == 0)
+      if ((options & DMGL_JAVA) == 0)
        d_append_string (dpi, "::");
       else
        d_append_char (dpi, '.');
 
       dc = d_right (mods->mod);
+
+      if (dc->type == DEMANGLE_COMPONENT_DEFAULT_ARG)
+       {
+         d_append_string (dpi, "{default arg#");
+         d_append_num (dpi, dc->u.s_unary_num.num + 1);
+         d_append_string (dpi, "}::");
+         dc = dc->u.s_unary_num.sub;
+       }
+
       while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
             || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
             || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
        dc = d_left (dc);
 
-      d_print_comp (dpi, dc);
+      d_print_comp (dpi, options, dc);
 
       dpi->templates = hold_dpt;
       return;
     }
 
-  d_print_mod (dpi, mods->mod);
+  d_print_mod (dpi, options, mods->mod);
 
   dpi->templates = hold_dpt;
 
-  d_print_mod_list (dpi, mods->next, suffix);
+  d_print_mod_list (dpi, options, mods->next, suffix);
 }
 
 /* Print a modifier.  */
 
 static void
-d_print_mod (struct d_print_info *dpi,
+d_print_mod (struct d_print_info *dpi, int options,
              const struct demangle_component *mod)
 {
   switch (mod->type)
@@ -3670,16 +4705,19 @@ d_print_mod (struct d_print_info *dpi,
       return;
     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
       d_append_char (dpi, ' ');
-      d_print_comp (dpi, d_right (mod));
+      d_print_comp (dpi, options, d_right (mod));
       return;
     case DEMANGLE_COMPONENT_POINTER:
       /* There is no pointer symbol in Java.  */
-      if ((dpi->options & DMGL_JAVA) == 0)
+      if ((options & DMGL_JAVA) == 0)
        d_append_char (dpi, '*');
       return;
     case DEMANGLE_COMPONENT_REFERENCE:
       d_append_char (dpi, '&');
       return;
+    case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
+      d_append_string (dpi, "&&");
+      return;
     case DEMANGLE_COMPONENT_COMPLEX:
       d_append_string (dpi, "complex ");
       return;
@@ -3689,16 +4727,22 @@ d_print_mod (struct d_print_info *dpi,
     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
       if (d_last_char (dpi) != '(')
        d_append_char (dpi, ' ');
-      d_print_comp (dpi, d_left (mod));
+      d_print_comp (dpi, options, d_left (mod));
       d_append_string (dpi, "::*");
       return;
     case DEMANGLE_COMPONENT_TYPED_NAME:
-      d_print_comp (dpi, d_left (mod));
+      d_print_comp (dpi, options, d_left (mod));
+      return;
+    case DEMANGLE_COMPONENT_VECTOR_TYPE:
+      d_append_string (dpi, " __vector(");
+      d_print_comp (dpi, options, d_left (mod));
+      d_append_char (dpi, ')');
       return;
+
     default:
       /* Otherwise, we have something that won't go back on the
         modifier stack, so we can just print it.  */
-      d_print_comp (dpi, mod);
+      d_print_comp (dpi, options, mod);
       return;
     }
 }
@@ -3706,29 +4750,27 @@ d_print_mod (struct d_print_info *dpi,
 /* Print a function type, except for the return type.  */
 
 static void
-d_print_function_type (struct d_print_info *dpi,
+d_print_function_type (struct d_print_info *dpi, int options,
                        const struct demangle_component *dc,
                        struct d_print_mod *mods)
 {
   int need_paren;
-  int saw_mod;
   int need_space;
   struct d_print_mod *p;
   struct d_print_mod *hold_modifiers;
 
   need_paren = 0;
-  saw_mod = 0;
   need_space = 0;
   for (p = mods; p != NULL; p = p->next)
     {
       if (p->printed)
        break;
 
-      saw_mod = 1;
       switch (p->mod->type)
        {
        case DEMANGLE_COMPONENT_POINTER:
        case DEMANGLE_COMPONENT_REFERENCE:
+       case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
          need_paren = 1;
          break;
        case DEMANGLE_COMPONENT_RESTRICT:
@@ -3752,9 +4794,6 @@ d_print_function_type (struct d_print_info *dpi,
        break;
     }
 
-  if (d_left (dc) != NULL && ! saw_mod)
-    need_paren = 1;
-
   if (need_paren)
     {
       if (! need_space)
@@ -3771,7 +4810,7 @@ d_print_function_type (struct d_print_info *dpi,
   hold_modifiers = dpi->modifiers;
   dpi->modifiers = NULL;
 
-  d_print_mod_list (dpi, mods, 0);
+  d_print_mod_list (dpi, options, mods, 0);
 
   if (need_paren)
     d_append_char (dpi, ')');
@@ -3779,11 +4818,11 @@ d_print_function_type (struct d_print_info *dpi,
   d_append_char (dpi, '(');
 
   if (d_right (dc) != NULL)
-    d_print_comp (dpi, d_right (dc));
+    d_print_comp (dpi, options, d_right (dc));
 
   d_append_char (dpi, ')');
 
-  d_print_mod_list (dpi, mods, 1);
+  d_print_mod_list (dpi, options, mods, 1);
 
   dpi->modifiers = hold_modifiers;
 }
@@ -3791,7 +4830,7 @@ d_print_function_type (struct d_print_info *dpi,
 /* Print an array type, except for the element type.  */
 
 static void
-d_print_array_type (struct d_print_info *dpi,
+d_print_array_type (struct d_print_info *dpi, int options,
                     const struct demangle_component *dc,
                     struct d_print_mod *mods)
 {
@@ -3825,7 +4864,7 @@ d_print_array_type (struct d_print_info *dpi,
       if (need_paren)
        d_append_string (dpi, " (");
 
-      d_print_mod_list (dpi, mods, 0);
+      d_print_mod_list (dpi, options, mods, 0);
 
       if (need_paren)
        d_append_char (dpi, ')');
@@ -3837,7 +4876,7 @@ d_print_array_type (struct d_print_info *dpi,
   d_append_char (dpi, '[');
 
   if (d_left (dc) != NULL)
-    d_print_comp (dpi, d_left (dc));
+    d_print_comp (dpi, options, d_left (dc));
 
   d_append_char (dpi, ']');
 }
@@ -3845,24 +4884,24 @@ d_print_array_type (struct d_print_info *dpi,
 /* Print an operator in an expression.  */
 
 static void
-d_print_expr_op (struct d_print_info *dpi,
+d_print_expr_op (struct d_print_info *dpi, int options,
                  const struct demangle_component *dc)
 {
   if (dc->type == DEMANGLE_COMPONENT_OPERATOR)
     d_append_buffer (dpi, dc->u.s_operator.op->name,
                     dc->u.s_operator.op->len);
   else
-    d_print_comp (dpi, dc);
+    d_print_comp (dpi, options, dc);
 }
 
 /* Print a cast.  */
 
 static void
-d_print_cast (struct d_print_info *dpi,
+d_print_cast (struct d_print_info *dpi, int options,
               const struct demangle_component *dc)
 {
   if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
-    d_print_comp (dpi, d_left (dc));
+    d_print_comp (dpi, options, d_left (dc));
   else
     {
       struct d_print_mod *hold_dpm;
@@ -3880,14 +4919,14 @@ d_print_cast (struct d_print_info *dpi,
       dpi->templates = &dpt;
       dpt.template_decl = d_left (dc);
 
-      d_print_comp (dpi, d_left (d_left (dc)));
+      d_print_comp (dpi, options, d_left (d_left (dc)));
 
       dpi->templates = dpt.next;
 
       if (d_last_char (dpi) == '<')
        d_append_char (dpi, ' ');
       d_append_char (dpi, '<');
-      d_print_comp (dpi, d_right (d_left (dc)));
+      d_print_comp (dpi, options, d_right (d_left (dc)));
       /* Avoid generating two consecutive '>' characters, to avoid
         the C++ syntactic ambiguity.  */
       if (d_last_char (dpi) == '>')
@@ -3938,33 +4977,30 @@ static int
 d_demangle_callback (const char *mangled, int options,
                      demangle_callbackref callback, void *opaque)
 {
-  int type;
+  enum
+    {
+      DCT_TYPE,
+      DCT_MANGLED,
+      DCT_GLOBAL_CTORS,
+      DCT_GLOBAL_DTORS
+    }
+  type;
   struct d_info di;
   struct demangle_component *dc;
   int status;
 
   if (mangled[0] == '_' && mangled[1] == 'Z')
-    type = 0;
+    type = DCT_MANGLED;
   else if (strncmp (mangled, "_GLOBAL_", 8) == 0
           && (mangled[8] == '.' || mangled[8] == '_' || mangled[8] == '$')
           && (mangled[9] == 'D' || mangled[9] == 'I')
           && mangled[10] == '_')
-    {
-      const char *intro;
-
-      intro = (mangled[9] == 'I')
-              ? "global constructors keyed to "
-              : "global destructors keyed to ";
-
-      callback (intro, strlen (intro), opaque);
-      callback (mangled + 11, strlen (mangled + 11), opaque);
-      return 1;
-    }
+    type = mangled[9] == 'I' ? DCT_GLOBAL_CTORS : DCT_GLOBAL_DTORS;
   else
     {
       if ((options & DMGL_TYPES) == 0)
        return 0;
-      type = 1;
+      type = DCT_TYPE;
     }
 
   cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
@@ -3981,10 +5017,26 @@ d_demangle_callback (const char *mangled, int options,
     di.subs = alloca (di.num_subs * sizeof (*di.subs));
 #endif
 
-    if (type)
-      dc = cplus_demangle_type (&di);
-    else
-      dc = cplus_demangle_mangled_name (&di, 1);
+    switch (type)
+      {
+      case DCT_TYPE:
+       dc = cplus_demangle_type (&di);
+       break;
+      case DCT_MANGLED:
+       dc = cplus_demangle_mangled_name (&di, 1);
+       break;
+      case DCT_GLOBAL_CTORS:
+      case DCT_GLOBAL_DTORS:
+       d_advance (&di, 11);
+       dc = d_make_comp (&di,
+                         (type == DCT_GLOBAL_CTORS
+                          ? DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS
+                          : DEMANGLE_COMPONENT_GLOBAL_DESTRUCTORS),
+                         d_make_demangle_mangled_name (&di, d_str (&di)),
+                         NULL);
+       d_advance (&di, strlen (d_str (&di)));
+       break;
+      }
 
     /* If DMGL_PARAMS is set, then if we didn't consume the entire
        mangled string, then we didn't successfully demangle it.  If
@@ -4029,7 +5081,7 @@ d_demangle (const char *mangled, int options, size_t *palc)
       return NULL;
     }
 
-  *palc = dgs.allocation_failure ? 1 : 0;
+  *palc = dgs.allocation_failure ? 1 : dgs.alc;
   return dgs.buf;
 }