Update the C++ demangler to that from libiberty in the gcc svn trunk,
rev 141363.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@8710 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/Makefile.am b/coregrind/Makefile.am
index eca4af4..ac3e119 100644
--- a/coregrind/Makefile.am
+++ b/coregrind/Makefile.am
@@ -149,9 +149,11 @@
 	m_debuginfo/priv_readelf.h	\
 	m_debuginfo/priv_readxcoff.h	\
 	m_demangle/ansidecl.h	\
+	m_demangle/cp-demangle.h \
 	m_demangle/dyn-string.h	\
 	m_demangle/demangle.h	\
 	m_demangle/safe-ctype.h \
+	m_demangle/vg_libciface.h \
 	m_scheduler/priv_sema.h \
 	m_syswrap/priv_types_n_macros.h \
 	m_syswrap/priv_syswrap-generic.h \
diff --git a/coregrind/m_demangle/ansidecl.h b/coregrind/m_demangle/ansidecl.h
index 9a7c577..c19955a 100644
--- a/coregrind/m_demangle/ansidecl.h
+++ b/coregrind/m_demangle/ansidecl.h
@@ -15,7 +15,7 @@
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /* ANSI and traditional C compatibility macros
 
@@ -136,17 +136,25 @@
 #define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
 #endif /* GCC_VERSION */
 
-#if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(_WIN32)
+#if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(_WIN32) || (defined(__alpha) && defined(__cplusplus))
 /* All known AIX compilers implement these things (but don't always
    define __STDC__).  The RISC/OS MIPS compiler defines these things
    in SVR4 mode, but does not define __STDC__.  */
+/* eraxxon@alumni.rice.edu: The Compaq C++ compiler, unlike many other
+   C++ compilers, does not define __STDC__, though it acts as if this
+   was so. (Verified versions: 5.7, 6.2, 6.3, 6.5) */
 
 #define ANSI_PROTOTYPES	1
 #define PTR		void *
 #define PTRCONST	void *const
 #define LONG_DOUBLE	long double
 
+/* PARAMS is often defined elsewhere (e.g. by libintl.h), so wrap it in
+   a #ifndef.  */
+#ifndef PARAMS
 #define PARAMS(ARGS)		ARGS
+#endif
+
 #define VPARAMS(ARGS)		ARGS
 #define VA_START(VA_LIST, VAR)	va_start(VA_LIST, VAR)
 
@@ -250,23 +258,53 @@
 
 /* Attributes on labels were valid as of gcc 2.93. */
 #ifndef ATTRIBUTE_UNUSED_LABEL
-# if (GCC_VERSION >= 2093)
+# if (!defined (__cplusplus) && GCC_VERSION >= 2093)
 #  define ATTRIBUTE_UNUSED_LABEL ATTRIBUTE_UNUSED
 # else
 #  define ATTRIBUTE_UNUSED_LABEL
-# endif /* GNUC >= 2.93 */
+# endif /* !__cplusplus && GNUC >= 2.93 */
 #endif /* ATTRIBUTE_UNUSED_LABEL */
 
 #ifndef ATTRIBUTE_UNUSED
 #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
 #endif /* ATTRIBUTE_UNUSED */
 
+/* Before GCC 3.4, the C++ frontend couldn't parse attributes placed after the
+   identifier name.  */
+#if ! defined(__cplusplus) || (GCC_VERSION >= 3004)
+# define ARG_UNUSED(NAME) NAME ATTRIBUTE_UNUSED
+#else /* !__cplusplus || GNUC >= 3.4 */
+# define ARG_UNUSED(NAME) NAME
+#endif /* !__cplusplus || GNUC >= 3.4 */
+
 #ifndef ATTRIBUTE_NORETURN
 #define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
 #endif /* ATTRIBUTE_NORETURN */
 
+/* Attribute `nonnull' was valid as of gcc 3.3.  */
+#ifndef ATTRIBUTE_NONNULL
+# if (GCC_VERSION >= 3003)
+#  define ATTRIBUTE_NONNULL(m) __attribute__ ((__nonnull__ (m)))
+# else
+#  define ATTRIBUTE_NONNULL(m)
+# endif /* GNUC >= 3.3 */
+#endif /* ATTRIBUTE_NONNULL */
+
+/* Attribute `pure' was valid as of gcc 3.0.  */
+#ifndef ATTRIBUTE_PURE
+# if (GCC_VERSION >= 3000)
+#  define ATTRIBUTE_PURE __attribute__ ((__pure__))
+# else
+#  define ATTRIBUTE_PURE
+# endif /* GNUC >= 3.0 */
+#endif /* ATTRIBUTE_PURE */
+
+/* Use ATTRIBUTE_PRINTF when the format specifier must not be NULL.
+   This was the case for the `printf' format attribute by itself
+   before GCC 3.3, but as of 3.3 we need to add the `nonnull'
+   attribute to retain this behavior.  */
 #ifndef ATTRIBUTE_PRINTF
-#define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n)))
+#define ATTRIBUTE_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n))) ATTRIBUTE_NONNULL(m)
 #define ATTRIBUTE_PRINTF_1 ATTRIBUTE_PRINTF(1, 2)
 #define ATTRIBUTE_PRINTF_2 ATTRIBUTE_PRINTF(2, 3)
 #define ATTRIBUTE_PRINTF_3 ATTRIBUTE_PRINTF(3, 4)
@@ -274,6 +312,77 @@
 #define ATTRIBUTE_PRINTF_5 ATTRIBUTE_PRINTF(5, 6)
 #endif /* ATTRIBUTE_PRINTF */
 
+/* Use ATTRIBUTE_FPTR_PRINTF when the format attribute is to be set on
+   a function pointer.  Format attributes were allowed on function
+   pointers as of gcc 3.1.  */
+#ifndef ATTRIBUTE_FPTR_PRINTF
+# if (GCC_VERSION >= 3001)
+#  define ATTRIBUTE_FPTR_PRINTF(m, n) ATTRIBUTE_PRINTF(m, n)
+# else
+#  define ATTRIBUTE_FPTR_PRINTF(m, n)
+# endif /* GNUC >= 3.1 */
+# define ATTRIBUTE_FPTR_PRINTF_1 ATTRIBUTE_FPTR_PRINTF(1, 2)
+# define ATTRIBUTE_FPTR_PRINTF_2 ATTRIBUTE_FPTR_PRINTF(2, 3)
+# define ATTRIBUTE_FPTR_PRINTF_3 ATTRIBUTE_FPTR_PRINTF(3, 4)
+# define ATTRIBUTE_FPTR_PRINTF_4 ATTRIBUTE_FPTR_PRINTF(4, 5)
+# define ATTRIBUTE_FPTR_PRINTF_5 ATTRIBUTE_FPTR_PRINTF(5, 6)
+#endif /* ATTRIBUTE_FPTR_PRINTF */
+
+/* Use ATTRIBUTE_NULL_PRINTF when the format specifier may be NULL.  A
+   NULL format specifier was allowed as of gcc 3.3.  */
+#ifndef ATTRIBUTE_NULL_PRINTF
+# if (GCC_VERSION >= 3003)
+#  define ATTRIBUTE_NULL_PRINTF(m, n) __attribute__ ((__format__ (__printf__, m, n)))
+# else
+#  define ATTRIBUTE_NULL_PRINTF(m, n)
+# endif /* GNUC >= 3.3 */
+# define ATTRIBUTE_NULL_PRINTF_1 ATTRIBUTE_NULL_PRINTF(1, 2)
+# define ATTRIBUTE_NULL_PRINTF_2 ATTRIBUTE_NULL_PRINTF(2, 3)
+# define ATTRIBUTE_NULL_PRINTF_3 ATTRIBUTE_NULL_PRINTF(3, 4)
+# define ATTRIBUTE_NULL_PRINTF_4 ATTRIBUTE_NULL_PRINTF(4, 5)
+# define ATTRIBUTE_NULL_PRINTF_5 ATTRIBUTE_NULL_PRINTF(5, 6)
+#endif /* ATTRIBUTE_NULL_PRINTF */
+
+/* Attribute `sentinel' was valid as of gcc 3.5.  */
+#ifndef ATTRIBUTE_SENTINEL
+# if (GCC_VERSION >= 3005)
+#  define ATTRIBUTE_SENTINEL __attribute__ ((__sentinel__))
+# else
+#  define ATTRIBUTE_SENTINEL
+# endif /* GNUC >= 3.5 */
+#endif /* ATTRIBUTE_SENTINEL */
+
+
+#ifndef ATTRIBUTE_ALIGNED_ALIGNOF
+# if (GCC_VERSION >= 3000)
+#  define ATTRIBUTE_ALIGNED_ALIGNOF(m) __attribute__ ((__aligned__ (__alignof__ (m))))
+# else
+#  define ATTRIBUTE_ALIGNED_ALIGNOF(m)
+# endif /* GNUC >= 3.0 */
+#endif /* ATTRIBUTE_ALIGNED_ALIGNOF */
+
+/* Useful for structures whose layout must much some binary specification
+   regardless of the alignment and padding qualities of the compiler.  */
+#ifndef ATTRIBUTE_PACKED
+# define ATTRIBUTE_PACKED __attribute__ ((packed))
+#endif
+
+/* Attribute `hot' and `cold' was valid as of gcc 4.3.  */
+#ifndef ATTRIBUTE_COLD
+# if (GCC_VERSION >= 4003)
+#  define ATTRIBUTE_COLD __attribute__ ((__cold__))
+# else
+#  define ATTRIBUTE_COLD
+# endif /* GNUC >= 4.3 */
+#endif /* ATTRIBUTE_COLD */
+#ifndef ATTRIBUTE_HOT
+# if (GCC_VERSION >= 4003)
+#  define ATTRIBUTE_HOT __attribute__ ((__hot__))
+# else
+#  define ATTRIBUTE_HOT
+# endif /* GNUC >= 4.3 */
+#endif /* ATTRIBUTE_HOT */
+
 /* We use __extension__ in some places to suppress -pedantic warnings
    about GCC extensions.  This feature didn't work properly before
    gcc 2.8.  */
@@ -281,15 +390,4 @@
 #define __extension__
 #endif
 
-/* Bootstrap support:  Adjust certain macros defined by Autoconf,
-   which are only valid for the stage1 compiler.  If we detect
-   a modern version of GCC, we are probably in stage2 or beyond,
-   so unconditionally reset the values.  Note that const, inline,
-   etc. have been dealt with above.  */
-#if (GCC_VERSION >= 2007)
-# ifndef HAVE_LONG_DOUBLE
-#  define HAVE_LONG_DOUBLE 1
-# endif
-#endif /* GCC >= 2.7 */
-
 #endif	/* ansidecl.h	*/
diff --git a/coregrind/m_demangle/cp-demangle.c b/coregrind/m_demangle/cp-demangle.c
index 6d3e1d5..00aba99 100644
--- a/coregrind/m_demangle/cp-demangle.c
+++ b/coregrind/m_demangle/cp-demangle.c
@@ -1,14 +1,24 @@
-/* Demangler for IA64 / g++ V3 ABI.
-   Copyright (C) 2000, 2001 Free Software Foundation, Inc.
-   Written by Alex Samuel <samuel@codesourcery.com>. 
+/* Demangler for g++ V3 ABI.
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
+   Free Software Foundation, Inc.
+   Written by Ian Lance Taylor <ian@wasabisystems.com>.
 
-   This file is part of GNU CC.
+   This file is part of the libiberty library, which is part of GCC.
 
-   This program is free software; you can redistribute it and/or modify
+   This file is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
+   In addition to the permissions in the GNU General Public License, the
+   Free Software Foundation gives you unlimited permission to link the
+   compiled version of this file into combinations with other programs,
+   and to distribute those combinations without any restriction coming
+   from the use of this file.  (The General Public License restrictions
+   do apply in other respects; for example, they cover modification of
+   the file, and distribution when not linked into a combined
+   executable.)
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@@ -16,3988 +26,4764 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 
 */
 
-/* This file implements demangling of C++ names mangled according to
-   the IA64 / g++ V3 ABI.  Use the cp_demangle function to
-   demangle a mangled name, or compile with the preprocessor macro
-   STANDALONE_DEMANGLER defined to create a demangling filter
-   executable (functionally similar to c++filt, but includes this
-   demangler only).  */
+/* This code implements a demangler for the g++ V3 ABI.  The ABI is
+   described on this web page:
+       http://www.codesourcery.com/cxx-abi/abi.html#mangling
 
-/*#include <sys/types.h>*/
+   This code was written while looking at the demangler written by
+   Alex Samuel <samuel@codesourcery.com>.
 
+   This code first pulls the mangled name apart into a list of
+   components, and then walks the list generating the demangled
+   name.
+
+   This file will normally define the following functions, q.v.:
+      char *cplus_demangle_v3(const char *mangled, int options)
+      char *java_demangle_v3(const char *mangled)
+      int cplus_demangle_v3_callback(const char *mangled, int options,
+                                     demangle_callbackref callback)
+      int java_demangle_v3_callback(const char *mangled,
+                                    demangle_callbackref callback)
+      enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
+      enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
+
+   Also, the interface to the component list is public, and defined in
+   demangle.h.  The interface consists of these types, which are
+   defined in demangle.h:
+      enum demangle_component_type
+      struct demangle_component
+      demangle_callbackref
+   and these functions defined in this file:
+      cplus_demangle_fill_name
+      cplus_demangle_fill_extended_operator
+      cplus_demangle_fill_ctor
+      cplus_demangle_fill_dtor
+      cplus_demangle_print
+      cplus_demangle_print_callback
+   and other functions defined in the file cp-demint.c.
+
+   This file also defines some other functions and variables which are
+   only to be used by the file cp-demint.c.
+
+   Preprocessor macros you can define while compiling this file:
+
+   IN_LIBGCC2
+      If defined, this file defines the following functions, q.v.:
+         char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
+                               int *status)
+         int __gcclibcxx_demangle_callback (const char *,
+                                            void (*)
+                                              (const char *, size_t, void *),
+                                            void *)
+      instead of cplus_demangle_v3[_callback]() and
+      java_demangle_v3[_callback]().
+
+   IN_GLIBCPP_V3
+      If defined, this file defines only __cxa_demangle() and
+      __gcclibcxx_demangle_callback(), and no other publically visible
+      functions or variables.
+
+   STANDALONE_DEMANGLER
+      If defined, this file defines a main() function which demangles
+      any arguments, or, if none, demangles stdin.
+
+   CP_DEMANGLE_DEBUG
+      If defined, turns on debugging mode, which prints information on
+      stdout about the mangled string.  This is not generally useful.
+*/
+
+#if 0 /* in valgrind */
+#if defined (_AIX) && !defined (__GNUC__)
+ #pragma alloca
+#endif
+#endif /* ! in valgrind */
+
+#if 0 /* in valgrind */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+#endif /* ! in valgrind */
 
-/*#ifdef HAVE_STDLIB_H
+#if 0 /* in valgrind */
+#include <stdio.h>
+#endif /* ! in valgrind */
+
+#if 0 /* in valgrind */
+#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
-#endif*/
-
-/*#ifdef HAVE_STRING_H
+#endif
+#ifdef HAVE_STRING_H
 #include <string.h>
-#endif*/
-
-#include "pub_core_basics.h"
-#include "pub_core_libcbase.h"
-#include "pub_core_libcassert.h"
-#include "pub_core_mallocfree.h"
-#include "ansidecl.h"
-#include "dyn-string.h"
-#include "demangle.h"
-
-#ifndef STANDALONE
-#define size_t       Int
-
-#define malloc(_cc,s)    VG_(arena_malloc) (VG_AR_DEMANGLE, _cc, s)
-#define free(p)          VG_(arena_free)   (VG_AR_DEMANGLE, p)
-#define realloc(_cc,p,s) VG_(arena_realloc)(VG_AR_DEMANGLE, _cc, p, s)
 #endif
+#endif /* ! in valgrind */
 
-/* If CP_DEMANGLE_DEBUG is defined, a trace of the grammar evaluation,
-   and other debugging output, will be generated. */
-#ifdef CP_DEMANGLE_DEBUG
-#define DEMANGLE_TRACE(PRODUCTION, DM)                                  \
-  fprintf (stderr, " -> %-24s at position %3d\n",                       \
-           (PRODUCTION), current_position (DM));
+#if 0 /* in valgrind */
+#ifdef HAVE_ALLOCA_H
+# include <alloca.h>
 #else
-#define DEMANGLE_TRACE(PRODUCTION, DM)
-#endif
+# ifndef alloca
+#  ifdef __GNUC__
+#   define alloca __builtin_alloca
+#  else
+extern char *alloca ();
+#  endif /* __GNUC__ */
+# endif /* alloca */
+#endif /* HAVE_ALLOCA_H */
+#endif /* ! in valgrind */
 
-/* Don't include <ctype.h>, to prevent additional unresolved symbols
-   from being dragged into the C++ runtime library.  */
-#define IS_DIGIT(CHAR) ((CHAR) >= '0' && (CHAR) <= '9')
-#define IS_ALPHA(CHAR)                                                  \
-  (((CHAR) >= 'a' && (CHAR) <= 'z')                                     \
-   || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
+#if 0 /* in valgrind */
+#include "ansidecl.h"
+#include "libiberty.h"
+#endif /* ! in valgrind */
+
+#include "vg_libciface.h"
+
+#include "demangle.h"
+#include "cp-demangle.h"
+
+/* If IN_GLIBCPP_V3 is defined, some functions are made static.  We
+   also rename them via #define to avoid compiler errors when the
+   static definition conflicts with the extern declaration in a header
+   file.  */
+#ifdef IN_GLIBCPP_V3
+
+#define CP_STATIC_IF_GLIBCPP_V3 static
+
+#define cplus_demangle_fill_name d_fill_name
+static int d_fill_name (struct demangle_component *, const char *, int);
+
+#define cplus_demangle_fill_extended_operator d_fill_extended_operator
+static int
+d_fill_extended_operator (struct demangle_component *, int,
+                          struct demangle_component *);
+
+#define cplus_demangle_fill_ctor d_fill_ctor
+static int
+d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
+             struct demangle_component *);
+
+#define cplus_demangle_fill_dtor d_fill_dtor
+static int
+d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
+             struct demangle_component *);
+
+#define cplus_demangle_mangled_name d_mangled_name
+static struct demangle_component *d_mangled_name (struct d_info *, int);
+
+#define cplus_demangle_type d_type
+static struct demangle_component *d_type (struct d_info *);
+
+#define cplus_demangle_print d_print
+static char *d_print (int, const struct demangle_component *, int, size_t *);
+
+#define cplus_demangle_print_callback d_print_callback
+static int d_print_callback (int, const struct demangle_component *,
+                             demangle_callbackref, void *);
+
+#define cplus_demangle_init_info d_init_info
+static void d_init_info (const char *, int, size_t, struct d_info *);
+
+#else /* ! defined(IN_GLIBCPP_V3) */
+#define CP_STATIC_IF_GLIBCPP_V3
+#endif /* ! defined(IN_GLIBCPP_V3) */
+
+/* See if the compiler supports dynamic arrays.  */
+
+#ifdef __GNUC__
+#define CP_DYNAMIC_ARRAYS
+#else
+#ifdef __STDC__
+#ifdef __STDC_VERSION__
+#if __STDC_VERSION__ >= 199901L
+#define CP_DYNAMIC_ARRAYS
+#endif /* __STDC__VERSION >= 199901L */
+#endif /* defined (__STDC_VERSION__) */
+#endif /* defined (__STDC__) */
+#endif /* ! defined (__GNUC__) */
+
+/* We avoid pulling in the ctype tables, to prevent pulling in
+   additional unresolved symbols when this code is used in a library.
+   FIXME: Is this really a valid reason?  This comes from the original
+   V3 demangler code.
+
+   As of this writing this file has the following undefined references
+   when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
+   strcat, strlen.  */
+
+#define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
+#define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
+#define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
 
 /* The prefix prepended by GCC to an identifier represnting the
    anonymous namespace.  */
 #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
+#define ANONYMOUS_NAMESPACE_PREFIX_LEN \
+  (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
 
-/* Character(s) to use for namespace separation in demangled output */
-#define NAMESPACE_SEPARATOR (dm->style == DMGL_JAVA ? "." : "::")
+/* Information we keep for the standard substitutions.  */
 
-/* If flag_verbose is zero, some simplifications will be made to the
-   output to make it easier to read and suppress details that are
-   generally not of interest to the average C++ programmer.
-   Otherwise, the demangled representation will attempt to convey as
-   much information as the mangled form.  */
-static int flag_verbose;
-
-/* If flag_strict is non-zero, demangle strictly according to the
-   specification -- don't demangle special g++ manglings.  */
-static int flag_strict;
-
-/* String_list_t is an extended form of dyn_string_t which provides a
-   link field and a caret position for additions to the string.  A
-   string_list_t may safely be cast to and used as a dyn_string_t.  */
-
-struct string_list_def
+struct d_standard_sub_info
 {
-  /* The dyn_string; must be first.  */
-  struct dyn_string string;
-
-  /* The position at which additional text is added to this string
-     (using the result_add* macros).  This value is an offset from the
-     end of the string, not the beginning (and should be
-     non-positive).  */
-  int caret_position;
-
-  /* The next string in the list.  */
-  struct string_list_def *next;
+  /* The code for this substitution.  */
+  char code;
+  /* The simple string it expands to.  */
+  const char *simple_expansion;
+  /* The length of the simple expansion.  */
+  int simple_len;
+  /* The results of a full, verbose, expansion.  This is used when
+     qualifying a constructor/destructor, or when in verbose mode.  */
+  const char *full_expansion;
+  /* The length of the full expansion.  */
+  int full_len;
+  /* What to set the last_name field of d_info to; NULL if we should
+     not set it.  This is only relevant when qualifying a
+     constructor/destructor.  */
+  const char *set_last_name;
+  /* The length of set_last_name.  */
+  int set_last_name_len;
 };
 
-typedef struct string_list_def *string_list_t;
+/* Accessors for subtrees of struct demangle_component.  */
 
-/* Data structure representing a potential substitution.  */
+#define d_left(dc) ((dc)->u.s_binary.left)
+#define d_right(dc) ((dc)->u.s_binary.right)
 
-struct substitution_def
+/* A list of templates.  This is used while printing.  */
+
+struct d_print_template
 {
-  /* The demangled text of the substitution.  */
-  dyn_string_t text;
-
-  /* Whether this substitution represents a template item.  */
-  int template_p : 1;
+  /* Next template on the list.  */
+  struct d_print_template *next;
+  /* This template.  */
+  const struct demangle_component *template_decl;
 };
 
-/* Data structure representing a template argument list.  */
+/* A list of type modifiers.  This is used while printing.  */
 
-struct template_arg_list_def
+struct d_print_mod
 {
-  /* The next (lower) template argument list in the stack of currently
-     active template arguments.  */
-  struct template_arg_list_def *next;
-
-  /* The first element in the list of template arguments in
-     left-to-right order.  */
-  string_list_t first_argument;
-
-  /* The last element in the arguments lists.  */
-  string_list_t last_argument;
+  /* Next modifier on the list.  These are in the reverse of the order
+     in which they appeared in the mangled string.  */
+  struct d_print_mod *next;
+  /* The modifier.  */
+  const struct demangle_component *mod;
+  /* Whether this modifier was printed.  */
+  int printed;
+  /* The list of templates which applies to this modifier.  */
+  struct d_print_template *templates;
 };
 
-typedef struct template_arg_list_def *template_arg_list_t;
+/* We use these structures to hold information during printing.  */
 
-/* Data structure to maintain the state of the current demangling.  */
-
-struct demangling_def
+struct d_growable_string
 {
-  /* The full mangled name being mangled.  */
-  const char *name;
-
-  /* Pointer into name at the current position.  */
-  const char *next;
-
-  /* Stack for strings containing demangled result generated so far.
-     Text is emitted to the topmost (first) string.  */
-  string_list_t result;
-
-  /* The number of presently available substitutions.  */
-  int num_substitutions;
-
-  /* The allocated size of the substitutions array.  */
-  int substitutions_allocated;
-
-  /* An array of available substitutions.  The number of elements in
-     the array is given by num_substitions, and the allocated array
-     size in substitutions_size.  
-
-     The most recent substition is at the end, so
-
-       - `S_'  corresponds to substititutions[num_substitutions - 1] 
-       - `S0_' corresponds to substititutions[num_substitutions - 2]
-
-     etc. */
-  struct substitution_def *substitutions;
-
-  /* The stack of template argument lists.  */
-  template_arg_list_t template_arg_lists;
-
-  /* The most recently demangled source-name.  */
-  dyn_string_t last_source_name;
-  
-  /* Language style to use for demangled output. */
-  int style;
-
-  /* Set to non-zero iff this name is a constructor.  The actual value
-     indicates what sort of constructor this is; see demangle.h.  */
-  enum gnu_v3_ctor_kinds is_constructor;
-
-  /* Set to non-zero iff this name is a destructor.  The actual value
-     indicates what sort of destructor this is; see demangle.h.  */
-  enum gnu_v3_dtor_kinds is_destructor;
-
+  /* Buffer holding the result.  */
+  char *buf;
+  /* Current length of data in buffer.  */
+  size_t len;
+  /* Allocated size of buffer.  */
+  size_t alc;
+  /* Set to 1 if we had a memory allocation failure.  */
+  int allocation_failure;
 };
 
-typedef struct demangling_def *demangling_t;
+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];
+  /* Current length of data in buffer.  */
+  size_t len;
+  /* The last character printed, saved individually so that it survives
+     any buffer flush.  */
+  char last_char;
+  /* Callback function to handle demangled buffer flush.  */
+  demangle_callbackref callback;
+  /* Opaque callback argument.  */
+  void *opaque;
+  /* The current list of templates, if any.  */
+  struct d_print_template *templates;
+  /* The current list of modifiers (e.g., pointer, reference, etc.),
+     if any.  */
+  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;
+};
 
-/* This type is the standard return code from most functions.  Values
-   other than STATUS_OK contain descriptive messages.  */
-typedef const char *status_t;
-
-/* Special values that can be used as a status_t.  */
-#define STATUS_OK                       NULL
-#define STATUS_ERROR                    "Error."
-#define STATUS_UNIMPLEMENTED            "Unimplemented."
-#define STATUS_INTERNAL_ERROR           "Internal error."
-
-/* This status code indicates a failure in malloc or realloc.  */
-static const char *const status_allocation_failed = "Allocation failed.";
-#define STATUS_ALLOCATION_FAILED        status_allocation_failed
-
-/* Non-zero if STATUS indicates that no error has occurred.  */
-#define STATUS_NO_ERROR(STATUS)         ((STATUS) == STATUS_OK)
-
-/* Evaluate EXPR, which must produce a status_t.  If the status code
-   indicates an error, return from the current function with that
-   status code.  */
-#define RETURN_IF_ERROR(EXPR)                                           \
-  do                                                                    \
-    {                                                                   \
-      status_t s = EXPR;                                                \
-      if (!STATUS_NO_ERROR (s))                                         \
-	return s;                                                       \
-    }                                                                   \
-  while (0)
-
-static status_t int_to_dyn_string 
-  PARAMS ((int, dyn_string_t));
-static string_list_t string_list_new
-  PARAMS ((int));
-static void string_list_delete
-  PARAMS ((string_list_t));
-static status_t result_add_separated_char
-  PARAMS ((demangling_t, int));
-static status_t result_push
-  PARAMS ((demangling_t));
-static string_list_t result_pop
-  PARAMS ((demangling_t));
-static int result_get_caret
-  PARAMS ((demangling_t));
-static void result_set_caret
-  PARAMS ((demangling_t, int));
-static void result_shift_caret
-  PARAMS ((demangling_t, int));
-static int result_previous_char_is_space
-  PARAMS ((demangling_t));
-static int substitution_start
-  PARAMS ((demangling_t));
-static status_t substitution_add
-  PARAMS ((demangling_t, int, int));
-static dyn_string_t substitution_get
-  PARAMS ((demangling_t, int, int *));
 #ifdef CP_DEMANGLE_DEBUG
-static void substitutions_print 
-  PARAMS ((demangling_t, FILE *));
+static void d_dump (struct demangle_component *, int);
 #endif
-static template_arg_list_t template_arg_list_new
-  PARAMS ((void));
-static void template_arg_list_delete
-  PARAMS ((template_arg_list_t));
-static void template_arg_list_add_arg 
-  PARAMS ((template_arg_list_t, string_list_t));
-static string_list_t template_arg_list_get_arg
-  PARAMS ((template_arg_list_t, int));
-static void push_template_arg_list
-  PARAMS ((demangling_t, template_arg_list_t));
-static void pop_to_template_arg_list
-  PARAMS ((demangling_t, template_arg_list_t));
+
+static struct demangle_component *
+d_make_empty (struct d_info *);
+
+static struct demangle_component *
+d_make_comp (struct d_info *, enum demangle_component_type,
+             struct demangle_component *,
+             struct demangle_component *);
+
+static struct demangle_component *
+d_make_name (struct d_info *, const char *, int);
+
+static struct demangle_component *
+d_make_builtin_type (struct d_info *,
+                     const struct demangle_builtin_type_info *);
+
+static struct demangle_component *
+d_make_operator (struct d_info *,
+                 const struct demangle_operator_info *);
+
+static struct demangle_component *
+d_make_extended_operator (struct d_info *, int,
+                          struct demangle_component *);
+
+static struct demangle_component *
+d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
+             struct demangle_component *);
+
+static struct demangle_component *
+d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
+             struct demangle_component *);
+
+static struct demangle_component *
+d_make_template_param (struct d_info *, long);
+
+static struct demangle_component *
+d_make_sub (struct d_info *, const char *, int);
+
+static int
+has_return_type (struct demangle_component *);
+
+static int
+is_ctor_dtor_or_conversion (struct demangle_component *);
+
+static struct demangle_component *d_encoding (struct d_info *, int);
+
+static struct demangle_component *d_name (struct d_info *);
+
+static struct demangle_component *d_nested_name (struct d_info *);
+
+static struct demangle_component *d_prefix (struct d_info *);
+
+static struct demangle_component *d_unqualified_name (struct d_info *);
+
+static struct demangle_component *d_source_name (struct d_info *);
+
+static long d_number (struct d_info *);
+
+static struct demangle_component *d_identifier (struct d_info *, int);
+
+static struct demangle_component *d_operator_name (struct d_info *);
+
+static struct demangle_component *d_special_name (struct d_info *);
+
+static int d_call_offset (struct d_info *, int);
+
+static struct demangle_component *d_ctor_dtor_name (struct d_info *);
+
+static struct demangle_component **
+d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
+
+static struct demangle_component *
+d_function_type (struct d_info *);
+
+static struct demangle_component *
+d_bare_function_type (struct d_info *, int);
+
+static struct demangle_component *
+d_class_enum_type (struct d_info *);
+
+static struct demangle_component *d_array_type (struct d_info *);
+
+static struct demangle_component *
+d_pointer_to_member_type (struct d_info *);
+
+static struct demangle_component *
+d_template_param (struct d_info *);
+
+static struct demangle_component *d_template_args (struct d_info *);
+
+static struct demangle_component *
+d_template_arg (struct d_info *);
+
+static struct demangle_component *d_expression (struct d_info *);
+
+static struct demangle_component *d_expr_primary (struct d_info *);
+
+static struct demangle_component *d_local_name (struct d_info *);
+
+static int d_discriminator (struct d_info *);
+
+static int
+d_add_substitution (struct d_info *, struct demangle_component *);
+
+static struct demangle_component *d_substitution (struct d_info *, int);
+
+static void d_growable_string_init (struct d_growable_string *, size_t);
+
+static inline void
+d_growable_string_resize (struct d_growable_string *, size_t);
+
+static inline void
+d_growable_string_append_buffer (struct d_growable_string *,
+                                 const char *, size_t);
+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 *);
+
+static inline void d_print_error (struct d_print_info *);
+
+static inline int d_print_saw_error (struct d_print_info *);
+
+static inline void d_print_flush (struct d_print_info *);
+
+static inline void d_append_char (struct d_print_info *, char);
+
+static inline void d_append_buffer (struct d_print_info *,
+                                    const char *, size_t);
+
+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 *);
+
+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);
+
+static void
+d_print_mod (struct d_print_info *, const struct demangle_component *);
+
+static void
+d_print_function_type (struct d_print_info *,
+                       const struct demangle_component *,
+                       struct d_print_mod *);
+
+static void
+d_print_array_type (struct d_print_info *,
+                    const struct demangle_component *,
+                    struct d_print_mod *);
+
+static void
+d_print_expr_op (struct d_print_info *, const struct demangle_component *);
+
+static void
+d_print_cast (struct d_print_info *, const struct demangle_component *);
+
+static int d_demangle_callback (const char *, int,
+                                demangle_callbackref, void *);
+static char *d_demangle (const char *, int, size_t *);
+
 #ifdef CP_DEMANGLE_DEBUG
-static void template_arg_list_print
-  PARAMS ((template_arg_list_t, FILE *));
-#endif
-static template_arg_list_t current_template_arg_list
-  PARAMS ((demangling_t));
-static demangling_t demangling_new
-  PARAMS ((const char *, int));
-static void demangling_delete 
-  PARAMS ((demangling_t));
 
-/* The last character of DS.  Warning: DS is evaluated twice.  */
-#define dyn_string_last_char(DS)                                        \
-  (dyn_string_buf (DS)[dyn_string_length (DS) - 1])
-
-/* Append a space character (` ') to DS if it does not already end
-   with one.  Evaluates to 1 on success, or 0 on allocation failure.  */
-#define dyn_string_append_space(DS)                                     \
-      ((dyn_string_length (DS) > 0                                      \
-        && dyn_string_last_char (DS) != ' ')                            \
-       ? dyn_string_append_char ((DS), ' ')                             \
-       : 1)
-
-/* Returns the index of the current position in the mangled name.  */
-#define current_position(DM)    ((DM)->next - (DM)->name)
-
-/* Returns the character at the current position of the mangled name.  */
-#define peek_char(DM)           (*((DM)->next))
-
-/* Returns the character one past the current position of the mangled
-   name.  */
-#define peek_char_next(DM)                                              \
-  (peek_char (DM) == '\0' ? '\0' : (*((DM)->next + 1)))
-
-/* Returns the character at the current position, and advances the
-   current position to the next character.  */
-#define next_char(DM)           (*((DM)->next)++)
-
-/* Returns non-zero if the current position is the end of the mangled
-   name, i.e. one past the last character.  */
-#define end_of_name_p(DM)       (peek_char (DM) == '\0')
-
-/* Advances the current position by one character.  */
-#define advance_char(DM)        (++(DM)->next)
-
-/* Returns the string containing the current demangled result.  */
-#define result_string(DM)       (&(DM)->result->string)
-
-/* Returns the position at which new text is inserted into the
-   demangled result.  */
-#define result_caret_pos(DM)                                            \
-  (result_length (DM) +                                                 \
-   ((string_list_t) result_string (DM))->caret_position)
-
-/* Adds a dyn_string_t to the demangled result.  */
-#define result_add_string(DM, STRING)                                   \
-  (dyn_string_insert (&(DM)->result->string,                            \
-		      result_caret_pos (DM), (STRING))                  \
-   ? STATUS_OK : STATUS_ALLOCATION_FAILED)
-
-/* Adds NUL-terminated string CSTR to the demangled result.    */
-#define result_add(DM, CSTR)                                            \
-  (dyn_string_insert_cstr (&(DM)->result->string,                       \
-			   result_caret_pos (DM), (CSTR))               \
-   ? STATUS_OK : STATUS_ALLOCATION_FAILED)
-
-/* Adds character CHAR to the demangled result.  */
-#define result_add_char(DM, CHAR)                                       \
-  (dyn_string_insert_char (&(DM)->result->string,                       \
-			   result_caret_pos (DM), (CHAR))               \
-   ? STATUS_OK : STATUS_ALLOCATION_FAILED)
-
-/* Inserts a dyn_string_t to the demangled result at position POS.  */
-#define result_insert_string(DM, POS, STRING)                           \
-  (dyn_string_insert (&(DM)->result->string, (POS), (STRING))           \
-   ? STATUS_OK : STATUS_ALLOCATION_FAILED)
-
-/* Inserts NUL-terminated string CSTR to the demangled result at
-   position POS.  */
-#define result_insert(DM, POS, CSTR)                                    \
-  (dyn_string_insert_cstr (&(DM)->result->string, (POS), (CSTR))        \
-   ? STATUS_OK : STATUS_ALLOCATION_FAILED)
-
-/* Inserts character CHAR to the demangled result at position POS.  */
-#define result_insert_char(DM, POS, CHAR)                               \
-  (dyn_string_insert_char (&(DM)->result->string, (POS), (CHAR))        \
-   ? STATUS_OK : STATUS_ALLOCATION_FAILED)
-
-/* The length of the current demangled result.  */
-#define result_length(DM)                                               \
-  dyn_string_length (&(DM)->result->string)
-
-/* Appends a (less-than, greater-than) character to the result in DM
-   to (open, close) a template argument or parameter list.  Appends a
-   space first if necessary to prevent spurious elision of angle
-   brackets with the previous character.  */
-#define result_open_template_list(DM) result_add_separated_char(DM, '<')
-#define result_close_template_list(DM) result_add_separated_char(DM, '>')
-
-/* Appends a base 10 representation of VALUE to DS.  STATUS_OK on
-   success.  On failure, deletes DS and returns an error code.  */
-
-static status_t
-int_to_dyn_string (value, ds)
-     int value;
-     dyn_string_t ds;
+static void
+d_dump (struct demangle_component *dc, int indent)
 {
   int i;
-  int mask = 1;
 
-  /* Handle zero up front.  */
-  if (value == 0)
+  if (dc == NULL)
     {
-      if (!dyn_string_append_char (ds, '0'))
-	return STATUS_ALLOCATION_FAILED;
-      return STATUS_OK;
+      if (indent == 0)
+        printf ("failed demangling\n");
+      return;
     }
 
-  /* For negative numbers, emit a minus sign.  */
-  if (value < 0)
+  for (i = 0; i < indent; ++i)
+    putchar (' ');
+
+  switch (dc->type)
     {
-      if (!dyn_string_append_char (ds, '-'))
-	return STATUS_ALLOCATION_FAILED;
-      value = -value;
-    }
-  
-  /* Find the power of 10 of the first digit.  */
-  i = value;
-  while (i > 9)
-    {
-      mask *= 10;
-      i /= 10;
+    case DEMANGLE_COMPONENT_NAME:
+      printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
+      return;
+    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
+      printf ("template parameter %ld\n", dc->u.s_number.number);
+      return;
+    case DEMANGLE_COMPONENT_CTOR:
+      printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
+      d_dump (dc->u.s_ctor.name, indent + 2);
+      return;
+    case DEMANGLE_COMPONENT_DTOR:
+      printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
+      d_dump (dc->u.s_dtor.name, indent + 2);
+      return;
+    case DEMANGLE_COMPONENT_SUB_STD:
+      printf ("standard substitution %s\n", dc->u.s_string.string);
+      return;
+    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
+      printf ("builtin type %s\n", dc->u.s_builtin.type->name);
+      return;
+    case DEMANGLE_COMPONENT_OPERATOR:
+      printf ("operator %s\n", dc->u.s_operator.op->name);
+      return;
+    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
+      printf ("extended operator with %d args\n",
+	      dc->u.s_extended_operator.args);
+      d_dump (dc->u.s_extended_operator.name, indent + 2);
+      return;
+
+    case DEMANGLE_COMPONENT_QUAL_NAME:
+      printf ("qualified name\n");
+      break;
+    case DEMANGLE_COMPONENT_LOCAL_NAME:
+      printf ("local name\n");
+      break;
+    case DEMANGLE_COMPONENT_TYPED_NAME:
+      printf ("typed name\n");
+      break;
+    case DEMANGLE_COMPONENT_TEMPLATE:
+      printf ("template\n");
+      break;
+    case DEMANGLE_COMPONENT_VTABLE:
+      printf ("vtable\n");
+      break;
+    case DEMANGLE_COMPONENT_VTT:
+      printf ("VTT\n");
+      break;
+    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
+      printf ("construction vtable\n");
+      break;
+    case DEMANGLE_COMPONENT_TYPEINFO:
+      printf ("typeinfo\n");
+      break;
+    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
+      printf ("typeinfo name\n");
+      break;
+    case DEMANGLE_COMPONENT_TYPEINFO_FN:
+      printf ("typeinfo function\n");
+      break;
+    case DEMANGLE_COMPONENT_THUNK:
+      printf ("thunk\n");
+      break;
+    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
+      printf ("virtual thunk\n");
+      break;
+    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
+      printf ("covariant thunk\n");
+      break;
+    case DEMANGLE_COMPONENT_JAVA_CLASS:
+      printf ("java class\n");
+      break;
+    case DEMANGLE_COMPONENT_GUARD:
+      printf ("guard\n");
+      break;
+    case DEMANGLE_COMPONENT_REFTEMP:
+      printf ("reference temporary\n");
+      break;
+    case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
+      printf ("hidden alias\n");
+      break;
+    case DEMANGLE_COMPONENT_RESTRICT:
+      printf ("restrict\n");
+      break;
+    case DEMANGLE_COMPONENT_VOLATILE:
+      printf ("volatile\n");
+      break;
+    case DEMANGLE_COMPONENT_CONST:
+      printf ("const\n");
+      break;
+    case DEMANGLE_COMPONENT_RESTRICT_THIS:
+      printf ("restrict this\n");
+      break;
+    case DEMANGLE_COMPONENT_VOLATILE_THIS:
+      printf ("volatile this\n");
+      break;
+    case DEMANGLE_COMPONENT_CONST_THIS:
+      printf ("const this\n");
+      break;
+    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
+      printf ("vendor type qualifier\n");
+      break;
+    case DEMANGLE_COMPONENT_POINTER:
+      printf ("pointer\n");
+      break;
+    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;
+    case DEMANGLE_COMPONENT_IMAGINARY:
+      printf ("imaginary\n");
+      break;
+    case DEMANGLE_COMPONENT_VENDOR_TYPE:
+      printf ("vendor type\n");
+      break;
+    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
+      printf ("function type\n");
+      break;
+    case DEMANGLE_COMPONENT_ARRAY_TYPE:
+      printf ("array type\n");
+      break;
+    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
+      printf ("pointer to member type\n");
+      break;
+    case DEMANGLE_COMPONENT_ARGLIST:
+      printf ("argument list\n");
+      break;
+    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
+      printf ("template argument list\n");
+      break;
+    case DEMANGLE_COMPONENT_CAST:
+      printf ("cast\n");
+      break;
+    case DEMANGLE_COMPONENT_UNARY:
+      printf ("unary operator\n");
+      break;
+    case DEMANGLE_COMPONENT_BINARY:
+      printf ("binary operator\n");
+      break;
+    case DEMANGLE_COMPONENT_BINARY_ARGS:
+      printf ("binary operator arguments\n");
+      break;
+    case DEMANGLE_COMPONENT_TRINARY:
+      printf ("trinary operator\n");
+      break;
+    case DEMANGLE_COMPONENT_TRINARY_ARG1:
+      printf ("trinary operator arguments 1\n");
+      break;
+    case DEMANGLE_COMPONENT_TRINARY_ARG2:
+      printf ("trinary operator arguments 1\n");
+      break;
+    case DEMANGLE_COMPONENT_LITERAL:
+      printf ("literal\n");
+      break;
+    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;
     }
 
-  /* Write the digits.  */
-  while (mask > 0)
-    {
-      int digit = value / mask;
-
-      if (!dyn_string_append_char (ds, '0' + digit))
-	return STATUS_ALLOCATION_FAILED;
-
-      value -= digit * mask;
-      mask /= 10;
-    }
-
-  return STATUS_OK;
-}
-
-/* Creates a new string list node.  The contents of the string are
-   empty, but the initial buffer allocation is LENGTH.  The string
-   list node should be deleted with string_list_delete.  Returns NULL
-   if allocation fails.  */
-
-static string_list_t 
-string_list_new (length)
-     int length;
-{
-  string_list_t s = (string_list_t) malloc ("demangle.sln.1",
-                                            sizeof (struct string_list_def));
-  if (s == NULL)
-    return NULL;
-  s->caret_position = 0;
-  if (!dyn_string_init ((dyn_string_t) s, length))
-    return NULL;
-  return s;
-}  
-
-/* Deletes the entire string list starting at NODE.  */
-
-static void
-string_list_delete (node)
-     string_list_t node;
-{
-  while (node != NULL)
-    {
-      string_list_t next = node->next;
-      dyn_string_delete ((dyn_string_t) node);
-      node = next;
-    }
-}
-
-/* Appends CHARACTER to the demangled result.  If the current trailing
-   character of the result is CHARACTER, a space is inserted first.  */
-
-static status_t
-result_add_separated_char (dm, character)
-     demangling_t dm;
-     int character;
-{
-  char *result = dyn_string_buf (result_string (dm));
-  int caret_pos = result_caret_pos (dm);
-
-  /* Add a space if the last character is already the character we
-     want to add.  */
-  if (caret_pos > 0 && result[caret_pos - 1] == character)
-    RETURN_IF_ERROR (result_add_char (dm, ' '));
-  /* Add the character.  */
-  RETURN_IF_ERROR (result_add_char (dm, character));
-
-  return STATUS_OK;
-}
-
-/* Allocates and pushes a new string onto the demangled results stack
-   for DM.  Subsequent demangling with DM will emit to the new string.
-   Returns STATUS_OK on success, STATUS_ALLOCATION_FAILED on
-   allocation failure.  */
-
-static status_t
-result_push (dm)
-     demangling_t dm;
-{
-  string_list_t new_string = string_list_new (0);
-  if (new_string == NULL)
-    /* Allocation failed.  */
-    return STATUS_ALLOCATION_FAILED;
-
-  /* Link the new string to the front of the list of result strings.  */
-  new_string->next = (string_list_t) dm->result;
-  dm->result = new_string;
-  return STATUS_OK;
-}
-
-/* Removes and returns the topmost element on the demangled results
-   stack for DM.  The caller assumes ownership for the returned
-   string.  */
-
-static string_list_t
-result_pop (dm)
-     demangling_t dm;
-{
-  string_list_t top = dm->result;
-  dm->result = top->next;
-  return top;
-}
-
-/* Returns the current value of the caret for the result string.  The
-   value is an offet from the end of the result string.  */
-
-static int
-result_get_caret (dm)
-     demangling_t dm;
-{
-  return ((string_list_t) result_string (dm))->caret_position;
-}
-
-/* Sets the value of the caret for the result string, counted as an
-   offet from the end of the result string.  */
-
-static void
-result_set_caret (dm, position)
-     demangling_t dm;
-     int position;
-{
-  ((string_list_t) result_string (dm))->caret_position = position;
-}
-
-/* Shifts the position of the next addition to the result by
-   POSITION_OFFSET.  A negative value shifts the caret to the left.  */
-
-static void
-result_shift_caret (dm, position_offset)
-     demangling_t dm;
-     int position_offset;
-{
-  ((string_list_t) result_string (dm))->caret_position += position_offset;
-}
-
-/* Returns non-zero if the character that comes right before the place
-   where text will be added to the result is a space.  In this case,
-   the caller should suppress adding another space.  */
-
-static int
-result_previous_char_is_space (dm)
-     demangling_t dm;
-{
-  char *result = dyn_string_buf (result_string (dm));
-  int pos = result_caret_pos (dm);
-  return pos > 0 && result[pos - 1] == ' ';
-}
-
-/* Returns the start position of a fragment of the demangled result
-   that will be a substitution candidate.  Should be called at the
-   start of productions that can add substitutions.  */
-
-static int
-substitution_start (dm)
-     demangling_t dm;
-{
-  return result_caret_pos (dm);
-}
-
-/* Adds the suffix of the current demangled result of DM starting at
-   START_POSITION as a potential substitution.  If TEMPLATE_P is
-   non-zero, this potential substitution is a template-id.  */
-
-static status_t
-substitution_add (dm, start_position, template_p)
-     demangling_t dm;
-     int start_position;
-     int template_p;
-{
-  dyn_string_t result = result_string (dm);
-  dyn_string_t substitution = dyn_string_new (0);
-  int i;
-
-  if (substitution == NULL)
-    return STATUS_ALLOCATION_FAILED;
-
-  /* Extract the substring of the current demangling result that
-     represents the subsitution candidate.  */
-  if (!dyn_string_substring (substitution, 
-			     result, start_position, result_caret_pos (dm)))
-    {
-      dyn_string_delete (substitution);
-      return STATUS_ALLOCATION_FAILED;
-    }
-
-  /* If there's no room for the new entry, grow the array.  */
-  if (dm->substitutions_allocated == dm->num_substitutions)
-    {
-      size_t new_array_size;
-      if (dm->substitutions_allocated > 0)
-	dm->substitutions_allocated *= 2;
-      else
-	dm->substitutions_allocated = 2;
-      new_array_size = 
-	sizeof (struct substitution_def) * dm->substitutions_allocated;
-
-      dm->substitutions = (struct substitution_def *)
-	realloc ("demangle.sa.1", dm->substitutions, new_array_size);
-      if (dm->substitutions == NULL)
-	/* Realloc failed.  */
-	{
-	  dyn_string_delete (substitution);
-	  return STATUS_ALLOCATION_FAILED;
-	}
-    }
-
-  /* Add the substitution to the array.  */
-  i = dm->num_substitutions++;
-  dm->substitutions[i].text = substitution;
-  dm->substitutions[i].template_p = template_p;
-
-#ifdef CP_DEMANGLE_DEBUG
-  substitutions_print (dm, stderr);
-#endif
-
-  return STATUS_OK;
-}
-
-/* Returns the Nth-most-recent substitution.  Sets *TEMPLATE_P to
-   non-zero if the substitution is a template-id, zero otherwise.  
-   N is numbered from zero.  DM retains ownership of the returned
-   string.  If N is negative, or equal to or greater than the current
-   number of substitution candidates, returns NULL.  */
-
-static dyn_string_t
-substitution_get (dm, n, template_p)
-     demangling_t dm;
-     int n;
-     int *template_p;
-{
-  struct substitution_def *sub;
-
-  /* Make sure N is in the valid range.  */
-  if (n < 0 || n >= dm->num_substitutions)
-    return NULL;
-
-  sub = &(dm->substitutions[n]);
-  *template_p = sub->template_p;
-  return sub->text;
-}
-
-#ifdef CP_DEMANGLE_DEBUG
-/* Debugging routine to print the current substitutions to FP.  */
-
-static void
-substitutions_print (dm, fp)
-     demangling_t dm;
-     FILE *fp;
-{
-  int seq_id;
-  int num = dm->num_substitutions;
-
-  fprintf (fp, "SUBSTITUTIONS:\n");
-  for (seq_id = -1; seq_id < num - 1; ++seq_id)
-    {
-      int template_p;
-      dyn_string_t text = substitution_get (dm, seq_id + 1, &template_p);
-
-      if (seq_id == -1)
-	fprintf (fp, " S_ ");
-      else
-	fprintf (fp, " S%d_", seq_id);
-      fprintf (fp, " %c: %s\n", template_p ? '*' : ' ', dyn_string_buf (text));
-    }
+  d_dump (d_left (dc), indent + 2);
+  d_dump (d_right (dc), indent + 2);
 }
 
 #endif /* CP_DEMANGLE_DEBUG */
 
-/* Creates a new template argument list.  Returns NULL if allocation
-   fails.  */
+/* Fill in a DEMANGLE_COMPONENT_NAME.  */
 
-static template_arg_list_t
-template_arg_list_new ()
+CP_STATIC_IF_GLIBCPP_V3
+int
+cplus_demangle_fill_name (struct demangle_component *p, const char *s, int len)
 {
-  template_arg_list_t new_list =
-    (template_arg_list_t) malloc ("demangle.talt.1",
-                                  sizeof (struct template_arg_list_def));
-  if (new_list == NULL)
+  if (p == NULL || s == NULL || len == 0)
+    return 0;
+  p->type = DEMANGLE_COMPONENT_NAME;
+  p->u.s_name.s = s;
+  p->u.s_name.len = len;
+  return 1;
+}
+
+/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
+
+CP_STATIC_IF_GLIBCPP_V3
+int
+cplus_demangle_fill_extended_operator (struct demangle_component *p, int args,
+                                       struct demangle_component *name)
+{
+  if (p == NULL || args < 0 || name == NULL)
+    return 0;
+  p->type = DEMANGLE_COMPONENT_EXTENDED_OPERATOR;
+  p->u.s_extended_operator.args = args;
+  p->u.s_extended_operator.name = name;
+  return 1;
+}
+
+/* Fill in a DEMANGLE_COMPONENT_CTOR.  */
+
+CP_STATIC_IF_GLIBCPP_V3
+int
+cplus_demangle_fill_ctor (struct demangle_component *p,
+                          enum gnu_v3_ctor_kinds kind,
+                          struct demangle_component *name)
+{
+  if (p == NULL
+      || name == NULL
+      || (kind < gnu_v3_complete_object_ctor
+	  && kind > gnu_v3_complete_object_allocating_ctor))
+    return 0;
+  p->type = DEMANGLE_COMPONENT_CTOR;
+  p->u.s_ctor.kind = kind;
+  p->u.s_ctor.name = name;
+  return 1;
+}
+
+/* Fill in a DEMANGLE_COMPONENT_DTOR.  */
+
+CP_STATIC_IF_GLIBCPP_V3
+int
+cplus_demangle_fill_dtor (struct demangle_component *p,
+                          enum gnu_v3_dtor_kinds kind,
+                          struct demangle_component *name)
+{
+  if (p == NULL
+      || name == NULL
+      || (kind < gnu_v3_deleting_dtor
+	  && kind > gnu_v3_base_object_dtor))
+    return 0;
+  p->type = DEMANGLE_COMPONENT_DTOR;
+  p->u.s_dtor.kind = kind;
+  p->u.s_dtor.name = name;
+  return 1;
+}
+
+/* Add a new component.  */
+
+static struct demangle_component *
+d_make_empty (struct d_info *di)
+{
+  struct demangle_component *p;
+
+  if (di->next_comp >= di->num_comps)
     return NULL;
-  /* Initialize the new list to have no arguments.  */
-  new_list->first_argument = NULL;
-  new_list->last_argument = NULL;
-  /* Return the new list.  */
-  return new_list;
+  p = &di->comps[di->next_comp];
+  ++di->next_comp;
+  return p;
 }
 
-/* Deletes a template argument list and the template arguments it
-   contains.  */
+/* Add a new generic component.  */
 
-static void
-template_arg_list_delete (list)
-     template_arg_list_t list;
+static struct demangle_component *
+d_make_comp (struct d_info *di, enum demangle_component_type type,
+             struct demangle_component *left,
+             struct demangle_component *right)
 {
-  /* If there are any arguments on LIST, delete them.  */
-  if (list->first_argument != NULL)
-    string_list_delete (list->first_argument);
-  /* Delete LIST.  */
-  free (list);
-}
+  struct demangle_component *p;
 
-/* Adds ARG to the template argument list ARG_LIST.  */
-
-static void 
-template_arg_list_add_arg (arg_list, arg)
-     template_arg_list_t arg_list;
-     string_list_t arg;
-{
-  if (arg_list->first_argument == NULL)
-    /* If there were no arguments before, ARG is the first one.  */
-    arg_list->first_argument = arg;
-  else
-    /* Make ARG the last argument on the list.  */
-    arg_list->last_argument->next = arg;
-  /* Make ARG the last on the list.  */
-  arg_list->last_argument = arg;
-  arg->next = NULL;
-}
-
-/* Returns the template arugment at position INDEX in template
-   argument list ARG_LIST.  */
-
-static string_list_t
-template_arg_list_get_arg (arg_list, index2)
-     template_arg_list_t arg_list;
-     int index2;
-{
-  string_list_t arg = arg_list->first_argument;
-  /* Scan down the list of arguments to find the one at position
-     INDEX.  */
-  while (index2--)
+  /* We check for errors here.  A typical error would be a NULL return
+     from a subroutine.  We catch those here, and return NULL
+     upward.  */
+  switch (type)
     {
-      arg = arg->next;
-      if (arg == NULL)
-	/* Ran out of arguments before INDEX hit zero.  That's an
-	   error.  */
+      /* These types require two parameters.  */
+    case DEMANGLE_COMPONENT_QUAL_NAME:
+    case DEMANGLE_COMPONENT_LOCAL_NAME:
+    case DEMANGLE_COMPONENT_TYPED_NAME:
+    case DEMANGLE_COMPONENT_TEMPLATE:
+    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
+    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
+    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
+    case DEMANGLE_COMPONENT_UNARY:
+    case DEMANGLE_COMPONENT_BINARY:
+    case DEMANGLE_COMPONENT_BINARY_ARGS:
+    case DEMANGLE_COMPONENT_TRINARY:
+    case DEMANGLE_COMPONENT_TRINARY_ARG1:
+    case DEMANGLE_COMPONENT_TRINARY_ARG2:
+    case DEMANGLE_COMPONENT_LITERAL:
+    case DEMANGLE_COMPONENT_LITERAL_NEG:
+    case DEMANGLE_COMPONENT_COMPOUND_NAME:
+      if (left == NULL || right == NULL)
 	return NULL;
-    }
-  /* Return the argument at position INDEX.  */
-  return arg;
-}
+      break;
 
-/* Pushes ARG_LIST onto the top of the template argument list stack.  */
+      /* These types only require one parameter.  */
+    case DEMANGLE_COMPONENT_VTABLE:
+    case DEMANGLE_COMPONENT_VTT:
+    case DEMANGLE_COMPONENT_TYPEINFO:
+    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
+    case DEMANGLE_COMPONENT_TYPEINFO_FN:
+    case DEMANGLE_COMPONENT_THUNK:
+    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
+    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
+    case DEMANGLE_COMPONENT_JAVA_CLASS:
+    case DEMANGLE_COMPONENT_GUARD:
+    case DEMANGLE_COMPONENT_REFTEMP:
+    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_CAST:
+    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
+    case DEMANGLE_COMPONENT_DECLTYPE:
+    case DEMANGLE_COMPONENT_PACK_EXPANSION:
+      if (left == NULL)
+	return NULL;
+      break;
 
-static void
-push_template_arg_list (dm, arg_list)
-     demangling_t dm;
-     template_arg_list_t arg_list;
-{
-  arg_list->next = dm->template_arg_lists;
-  dm->template_arg_lists = arg_list;
-#ifdef CP_DEMANGLE_DEBUG
-  fprintf (stderr, " ** pushing template arg list\n");
-  template_arg_list_print (arg_list, stderr);
-#endif 
-}
+      /* This needs a right parameter, but the left parameter can be
+	 empty.  */
+    case DEMANGLE_COMPONENT_ARRAY_TYPE:
+      if (right == NULL)
+	return NULL;
+      break;
 
-/* Pops and deletes elements on the template argument list stack until
-   arg_list is the topmost element.  If arg_list is NULL, all elements
-   are popped and deleted.  */
+      /* These are allowed to have no parameters--in some cases they
+	 will be filled in later.  */
+    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
+    case DEMANGLE_COMPONENT_RESTRICT:
+    case DEMANGLE_COMPONENT_VOLATILE:
+    case DEMANGLE_COMPONENT_CONST:
+    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;
 
-static void
-pop_to_template_arg_list (dm, arg_list)
-     demangling_t dm;
-     template_arg_list_t arg_list;
-{
-  while (dm->template_arg_lists != arg_list)
-    {
-      template_arg_list_t top = dm->template_arg_lists;
-      /* Disconnect the topmost element from the list.  */
-      dm->template_arg_lists = top->next;
-      /* Delete the popped element.  */
-      template_arg_list_delete (top);
-#ifdef CP_DEMANGLE_DEBUG
-      fprintf (stderr, " ** removing template arg list\n");
-#endif
-    }
-}
-
-#ifdef CP_DEMANGLE_DEBUG
-
-/* Prints the contents of ARG_LIST to FP.  */
-
-static void
-template_arg_list_print (arg_list, fp)
-  template_arg_list_t arg_list;
-  FILE *fp;
-{
-  string_list_t arg;
-  int index = -1;
-
-  fprintf (fp, "TEMPLATE ARGUMENT LIST:\n");
-  for (arg = arg_list->first_argument; arg != NULL; arg = arg->next)
-    {
-      if (index == -1)
-	fprintf (fp, " T_  : ");
-      else
-	fprintf (fp, " T%d_ : ", index);
-      ++index;
-      fprintf (fp, "%s\n", dyn_string_buf ((dyn_string_t) arg));
-    }
-}
-
-#endif /* CP_DEMANGLE_DEBUG */
-
-/* Returns the topmost element on the stack of template argument
-   lists.  If there is no list of template arguments, returns NULL.  */
-
-static template_arg_list_t
-current_template_arg_list (dm)
-     demangling_t dm;
-{
-  return dm->template_arg_lists;
-}
-
-/* Allocates a demangling_t object for demangling mangled NAME.  A new
-   result must be pushed before the returned object can be used.
-   Returns NULL if allocation fails.  */
-
-static demangling_t
-demangling_new (name, style)
-     const char *name;
-     int style;
-{
-  demangling_t dm;
-  dm = (demangling_t) malloc ("demangle.dn.1",
-                              sizeof (struct demangling_def));
-  if (dm == NULL)
-    return NULL;
-
-  dm->name = name;
-  dm->next = name;
-  dm->result = NULL;
-  dm->num_substitutions = 0;
-  dm->substitutions_allocated = 10;
-  dm->template_arg_lists = NULL;
-  dm->last_source_name = dyn_string_new (0);
-  if (dm->last_source_name == NULL)
-    return NULL;
-  dm->substitutions = (struct substitution_def *)
-    malloc ("demangle.dn.2",
-            dm->substitutions_allocated * sizeof (struct substitution_def));
-  if (dm->substitutions == NULL)
-    {
-      dyn_string_delete (dm->last_source_name);
+      /* Other types should not be seen here.  */
+    default:
       return NULL;
     }
-  dm->style = style;
-  dm->is_constructor = 0;
-  dm->is_destructor = 0;
 
-  return dm;
+  p = d_make_empty (di);
+  if (p != NULL)
+    {
+      p->type = type;
+      p->u.s_binary.left = left;
+      p->u.s_binary.right = right;
+    }
+  return p;
 }
 
-/* Deallocates a demangling_t object and all memory associated with
-   it.  */
+/* Add a new name component.  */
 
-static void
-demangling_delete (dm)
-     demangling_t dm;
+static struct demangle_component *
+d_make_name (struct d_info *di, const char *s, int len)
 {
-  int i;
-  template_arg_list_t arg_list = dm->template_arg_lists;
+  struct demangle_component *p;
 
-  /* Delete the stack of template argument lists.  */
-  while (arg_list != NULL)
-    {
-      template_arg_list_t next = arg_list->next;
-      template_arg_list_delete (arg_list);
-      arg_list = next;
-    }
-  /* Delete the list of substitutions.  */
-  for (i = dm->num_substitutions; --i >= 0; )
-    dyn_string_delete (dm->substitutions[i].text);
-  free (dm->substitutions);
-  /* Delete the demangled result.  */
-  string_list_delete (dm->result);
-  /* Delete the stored identifier name.  */
-  dyn_string_delete (dm->last_source_name);
-  /* Delete the context object itself.  */
-  free (dm);
+  p = d_make_empty (di);
+  if (! cplus_demangle_fill_name (p, s, len))
+    return NULL;
+  return p;
 }
 
-/* These functions demangle an alternative of the corresponding
-   production in the mangling spec.  The first argument of each is a
-   demangling context structure for the current demangling
-   operation.  Most emit demangled text directly to the topmost result
-   string on the result string stack in the demangling context
-   structure.  */
+/* Add a new builtin type component.  */
 
-static status_t demangle_char
-  PARAMS ((demangling_t, int));
-static status_t demangle_mangled_name 
-  PARAMS ((demangling_t));
-static status_t demangle_encoding
-  PARAMS ((demangling_t));
-static status_t demangle_name
-  PARAMS ((demangling_t, int *));
-static status_t demangle_nested_name
-  PARAMS ((demangling_t, int *));
-static status_t demangle_prefix_v3
-  PARAMS ((demangling_t, int *));
-static status_t demangle_unqualified_name
-  PARAMS ((demangling_t, int *));
-static status_t demangle_source_name
-  PARAMS ((demangling_t));
-static status_t demangle_number
-  PARAMS ((demangling_t, int *, int, int));
-static status_t demangle_number_literally
-  PARAMS ((demangling_t, dyn_string_t, int, int));
-static status_t demangle_identifier
-  PARAMS ((demangling_t, int, dyn_string_t));
-static status_t demangle_operator_name
-  PARAMS ((demangling_t, int, int *));
-static status_t demangle_nv_offset
-  PARAMS ((demangling_t));
-static status_t demangle_v_offset
-  PARAMS ((demangling_t));
-static status_t demangle_call_offset
-  PARAMS ((demangling_t));
-static status_t demangle_special_name
-  PARAMS ((demangling_t));
-static status_t demangle_ctor_dtor_name
-  PARAMS ((demangling_t));
-static status_t demangle_type_ptr
-  PARAMS ((demangling_t, int *, int));
-static status_t demangle_type
-  PARAMS ((demangling_t));
-static status_t demangle_CV_qualifiers
-  PARAMS ((demangling_t, dyn_string_t));
-static status_t demangle_builtin_type
-  PARAMS ((demangling_t));
-static status_t demangle_function_type
-  PARAMS ((demangling_t, int *));
-static status_t demangle_bare_function_type
-  PARAMS ((demangling_t, int *));
-static status_t demangle_class_enum_type
-  PARAMS ((demangling_t, int *));
-static status_t demangle_array_type
-  PARAMS ((demangling_t, int *));
-static status_t demangle_template_param
-  PARAMS ((demangling_t));
-static status_t demangle_template_args_1
-  PARAMS ((demangling_t, template_arg_list_t));
-static status_t demangle_template_args
-  PARAMS ((demangling_t));
-static status_t demangle_literal
-  PARAMS ((demangling_t));
-static status_t demangle_template_arg
-  PARAMS ((demangling_t));
-static status_t demangle_expression_v3
-  PARAMS ((demangling_t));
-static status_t demangle_scope_expression
-  PARAMS ((demangling_t));
-static status_t demangle_expr_primary
-  PARAMS ((demangling_t));
-static status_t demangle_substitution
-  PARAMS ((demangling_t, int *));
-static status_t demangle_local_name
-  PARAMS ((demangling_t));
-static status_t demangle_discriminator 
-  PARAMS ((demangling_t, int));
-static status_t cp_demangle
-  PARAMS ((const char *, dyn_string_t, int));
-#ifdef IN_LIBGCC2
-static status_t cp_demangle_type
-  PARAMS ((const char*, dyn_string_t));
-#endif
-
-/* When passed to demangle_bare_function_type, indicates that the
-   function's return type is not encoded before its parameter types.  */
-#define BFT_NO_RETURN_TYPE    NULL
-
-/* Check that the next character is C.  If so, consume it.  If not,
-   return an error.  */
-
-static status_t
-demangle_char (dm, c)
-     demangling_t dm;
-     int c;
+static struct demangle_component *
+d_make_builtin_type (struct d_info *di,
+                     const struct demangle_builtin_type_info *type)
 {
-  //static char *error_message = NULL;    // unused
+  struct demangle_component *p;
 
-  if (peek_char (dm) == c)
+  if (type == NULL)
+    return NULL;
+  p = d_make_empty (di);
+  if (p != NULL)
     {
-      advance_char (dm);
-      return STATUS_OK;
+      p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
+      p->u.s_builtin.type = type;
     }
-  else
+  return p;
+}
+
+/* Add a new operator component.  */
+
+static struct demangle_component *
+d_make_operator (struct d_info *di, const struct demangle_operator_info *op)
+{
+  struct demangle_component *p;
+
+  p = d_make_empty (di);
+  if (p != NULL)
     {
-	vg_assert (0);
-	/*
-      if (error_message == NULL)
-	error_message = strdup ("Expected ?");
-      error_message[9] = c;
-      return error_message;
-      */
+      p->type = DEMANGLE_COMPONENT_OPERATOR;
+      p->u.s_operator.op = op;
+    }
+  return p;
+}
+
+/* Add a new extended operator component.  */
+
+static struct demangle_component *
+d_make_extended_operator (struct d_info *di, int args,
+                          struct demangle_component *name)
+{
+  struct demangle_component *p;
+
+  p = d_make_empty (di);
+  if (! cplus_demangle_fill_extended_operator (p, args, name))
+    return NULL;
+  return p;
+}
+
+/* Add a new constructor component.  */
+
+static struct demangle_component *
+d_make_ctor (struct d_info *di, enum gnu_v3_ctor_kinds kind,
+             struct demangle_component *name)
+{
+  struct demangle_component *p;
+
+  p = d_make_empty (di);
+  if (! cplus_demangle_fill_ctor (p, kind, name))
+    return NULL;
+  return p;
+}
+
+/* Add a new destructor component.  */
+
+static struct demangle_component *
+d_make_dtor (struct d_info *di, enum gnu_v3_dtor_kinds kind,
+             struct demangle_component *name)
+{
+  struct demangle_component *p;
+
+  p = d_make_empty (di);
+  if (! cplus_demangle_fill_dtor (p, kind, name))
+    return NULL;
+  return p;
+}
+
+/* Add a new template parameter.  */
+
+static struct demangle_component *
+d_make_template_param (struct d_info *di, long i)
+{
+  struct demangle_component *p;
+
+  p = d_make_empty (di);
+  if (p != NULL)
+    {
+      p->type = DEMANGLE_COMPONENT_TEMPLATE_PARAM;
+      p->u.s_number.number = i;
+    }
+  return p;
+}
+
+/* Add a new standard substitution component.  */
+
+static struct demangle_component *
+d_make_sub (struct d_info *di, const char *name, int len)
+{
+  struct demangle_component *p;
+
+  p = d_make_empty (di);
+  if (p != NULL)
+    {
+      p->type = DEMANGLE_COMPONENT_SUB_STD;
+      p->u.s_string.string = name;
+      p->u.s_string.len = len;
+    }
+  return p;
+}
+
+/* <mangled-name> ::= _Z <encoding>
+
+   TOP_LEVEL is non-zero when called at the top level.  */
+
+CP_STATIC_IF_GLIBCPP_V3
+struct demangle_component *
+cplus_demangle_mangled_name (struct d_info *di, int top_level)
+{
+  if (! d_check_char (di, '_'))
+    return NULL;
+  if (! d_check_char (di, 'Z'))
+    return NULL;
+  return d_encoding (di, top_level);
+}
+
+/* Return whether a function should have a return type.  The argument
+   is the function name, which may be qualified in various ways.  The
+   rules are that template functions have return types with some
+   exceptions, function types which are not part of a function name
+   mangling have return types with some exceptions, and non-template
+   function names do not have return types.  The exceptions are that
+   constructors, destructors, and conversion operators do not have
+   return types.  */
+
+static int
+has_return_type (struct demangle_component *dc)
+{
+  if (dc == NULL)
+    return 0;
+  switch (dc->type)
+    {
+    default:
+      return 0;
+    case DEMANGLE_COMPONENT_TEMPLATE:
+      return ! is_ctor_dtor_or_conversion (d_left (dc));
+    case DEMANGLE_COMPONENT_RESTRICT_THIS:
+    case DEMANGLE_COMPONENT_VOLATILE_THIS:
+    case DEMANGLE_COMPONENT_CONST_THIS:
+      return has_return_type (d_left (dc));
     }
 }
 
-/* Demangles and emits a <mangled-name>.  
+/* Return whether a name is a constructor, a destructor, or a
+   conversion operator.  */
 
-    <mangled-name>      ::= _Z <encoding>  */
-
-static status_t
-demangle_mangled_name (dm)
-     demangling_t dm;
+static int
+is_ctor_dtor_or_conversion (struct demangle_component *dc)
 {
-  DEMANGLE_TRACE ("mangled-name", dm);
-  RETURN_IF_ERROR (demangle_char (dm, '_'));
-  RETURN_IF_ERROR (demangle_char (dm, 'Z'));
-  RETURN_IF_ERROR (demangle_encoding (dm));
-  return STATUS_OK;
+  if (dc == NULL)
+    return 0;
+  switch (dc->type)
+    {
+    default:
+      return 0;
+    case DEMANGLE_COMPONENT_QUAL_NAME:
+    case DEMANGLE_COMPONENT_LOCAL_NAME:
+      return is_ctor_dtor_or_conversion (d_right (dc));
+    case DEMANGLE_COMPONENT_CTOR:
+    case DEMANGLE_COMPONENT_DTOR:
+    case DEMANGLE_COMPONENT_CAST:
+      return 1;
+    }
 }
 
-/* Demangles and emits an <encoding>.  
+/* <encoding> ::= <(function) name> <bare-function-type>
+              ::= <(data) name>
+              ::= <special-name>
 
-    <encoding>		::= <function name> <bare-function-type>
-			::= <data name>
-			::= <special-name>  */
+   TOP_LEVEL is non-zero when called at the top level, in which case
+   if DMGL_PARAMS is not set we do not demangle the function
+   parameters.  We only set this at the top level, because otherwise
+   we would not correctly demangle names in local scopes.  */
 
-static status_t
-demangle_encoding (dm)
-     demangling_t dm;
+static struct demangle_component *
+d_encoding (struct d_info *di, int top_level)
 {
-  int encode_return_type;
-  int start_position;
-  template_arg_list_t old_arg_list = current_template_arg_list (dm);
-  char peek = peek_char (dm);
-
-  DEMANGLE_TRACE ("encoding", dm);
-  
-  /* Remember where the name starts.  If it turns out to be a template
-     function, we'll have to insert the return type here.  */
-  start_position = result_caret_pos (dm);
+  char peek = d_peek_char (di);
 
   if (peek == 'G' || peek == 'T')
-    RETURN_IF_ERROR (demangle_special_name (dm));
+    return d_special_name (di);
   else
     {
-      /* Now demangle the name.  */
-      RETURN_IF_ERROR (demangle_name (dm, &encode_return_type));
+      struct demangle_component *dc;
 
-      /* If there's anything left, the name was a function name, with
-	 maybe its return type, and its parameter types, following.  */
-      if (!end_of_name_p (dm) 
-	  && peek_char (dm) != 'E')
+      dc = d_name (di);
+
+      if (dc != NULL && top_level && (di->options & DMGL_PARAMS) == 0)
 	{
-	  if (encode_return_type)
-	    /* Template functions have their return type encoded.  The
-	       return type should be inserted at start_position.  */
-	    RETURN_IF_ERROR 
-	      (demangle_bare_function_type (dm, &start_position));
-	  else
-	    /* Non-template functions don't have their return type
-	       encoded.  */
-	    RETURN_IF_ERROR 
-	      (demangle_bare_function_type (dm, BFT_NO_RETURN_TYPE)); 
+	  /* Strip off any initial CV-qualifiers, as they really apply
+	     to the `this' parameter, and they were not output by the
+	     v2 demangler without DMGL_PARAMS.  */
+	  while (dc->type == DEMANGLE_COMPONENT_RESTRICT_THIS
+		 || dc->type == DEMANGLE_COMPONENT_VOLATILE_THIS
+		 || dc->type == DEMANGLE_COMPONENT_CONST_THIS)
+	    dc = d_left (dc);
+
+	  /* If the top level is a DEMANGLE_COMPONENT_LOCAL_NAME, then
+	     there may be CV-qualifiers on its right argument which
+	     really apply here; this happens when parsing a class
+	     which is local to a function.  */
+	  if (dc->type == DEMANGLE_COMPONENT_LOCAL_NAME)
+	    {
+	      struct demangle_component *dcr;
+
+	      dcr = d_right (dc);
+	      while (dcr->type == DEMANGLE_COMPONENT_RESTRICT_THIS
+		     || dcr->type == DEMANGLE_COMPONENT_VOLATILE_THIS
+		     || dcr->type == DEMANGLE_COMPONENT_CONST_THIS)
+		dcr = d_left (dcr);
+	      dc->u.s_binary.right = dcr;
+	    }
+
+	  return dc;
 	}
+
+      peek = d_peek_char (di);
+      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)));
     }
-
-  /* Pop off template argument lists that were built during the
-     mangling of this name, to restore the old template context.  */
-  pop_to_template_arg_list (dm, old_arg_list);
-
-  return STATUS_OK;
 }
 
-/* Demangles and emits a <name>.
+/* <name> ::= <nested-name>
+          ::= <unscoped-name>
+          ::= <unscoped-template-name> <template-args>
+          ::= <local-name>
 
-    <name>              ::= <unscoped-name>
-                        ::= <unscoped-template-name> <template-args>
-			::= <nested-name>
-                        ::= <local-name>
+   <unscoped-name> ::= <unqualified-name>
+                   ::= St <unqualified-name>
 
-    <unscoped-name>     ::= <unqualified-name>
-			::= St <unqualified-name>   # ::std::
+   <unscoped-template-name> ::= <unscoped-name>
+                            ::= <substitution>
+*/
 
-    <unscoped-template-name>    
-                        ::= <unscoped-name>
-                        ::= <substitution>  */
-
-static status_t
-demangle_name (dm, encode_return_type)
-     demangling_t dm;
-     int *encode_return_type;
+static struct demangle_component *
+d_name (struct d_info *di)
 {
-  int start = substitution_start (dm);
-  char peek = peek_char (dm);
-  int is_std_substitution = 0;
-
-  /* Generally, the return type is encoded if the function is a
-     template-id, and suppressed otherwise.  There are a few cases,
-     though, in which the return type is not encoded even for a
-     templated function.  In these cases, this flag is set.  */
-  int suppress_return_type = 0;
-
-  DEMANGLE_TRACE ("name", dm);
+  char peek = d_peek_char (di);
+  struct demangle_component *dc;
 
   switch (peek)
     {
     case 'N':
-      /* This is a <nested-name>.  */
-      RETURN_IF_ERROR (demangle_nested_name (dm, encode_return_type));
-      break;
+      return d_nested_name (di);
 
     case 'Z':
-      RETURN_IF_ERROR (demangle_local_name (dm));
-      *encode_return_type = 0;
-      break;
+      return d_local_name (di);
 
+    case 'L':
+      return d_unqualified_name (di);
+	
     case 'S':
-      /* The `St' substitution allows a name nested in std:: to appear
-	 without being enclosed in a nested name.  */
-      if (peek_char_next (dm) == 't') 
-	{
-	  (void) next_char (dm);
-	  (void) next_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "std::"));
-	  RETURN_IF_ERROR 
-	    (demangle_unqualified_name (dm, &suppress_return_type));
-	  is_std_substitution = 1;
-	}
-      else
-	RETURN_IF_ERROR (demangle_substitution (dm, encode_return_type));
-      /* Check if a template argument list immediately follows.
-	 If so, then we just demangled an <unqualified-template-name>.  */
-      if (peek_char (dm) == 'I') 
-	{
-	  /* A template name of the form std::<unqualified-name> is a
-             substitution candidate.  */
-	  if (is_std_substitution)
-	    RETURN_IF_ERROR (substitution_add (dm, start, 0));
-	  /* Demangle the <template-args> here.  */
-	  RETURN_IF_ERROR (demangle_template_args (dm));
-	  *encode_return_type = !suppress_return_type;
-	}
-      else
-	*encode_return_type = 0;
+      {
+	int subst;
 
-      break;
+	if (d_peek_next_char (di) != 't')
+	  {
+	    dc = d_substitution (di, 0);
+	    subst = 1;
+	  }
+	else
+	  {
+	    d_advance (di, 2);
+	    dc = d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME,
+			      d_make_name (di, "std", 3),
+			      d_unqualified_name (di));
+	    di->expansion += 3;
+	    subst = 0;
+	  }
+
+	if (d_peek_char (di) != 'I')
+	  {
+	    /* The grammar does not permit this case to occur if we
+	       called d_substitution() above (i.e., subst == 1).  We
+	       don't bother to check.  */
+	  }
+	else
+	  {
+	    /* This is <template-args>, which means that we just saw
+	       <unscoped-template-name>, which is a substitution
+	       candidate if we didn't just get it from a
+	       substitution.  */
+	    if (! subst)
+	      {
+		if (! d_add_substitution (di, dc))
+		  return NULL;
+	      }
+	    dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
+			      d_template_args (di));
+	  }
+
+	return dc;
+      }
 
     default:
-      /* This is an <unscoped-name> or <unscoped-template-name>.  */
-      RETURN_IF_ERROR (demangle_unqualified_name (dm, &suppress_return_type));
-
-      /* If the <unqualified-name> is followed by template args, this
-	 is an <unscoped-template-name>.  */
-      if (peek_char (dm) == 'I')
+      dc = d_unqualified_name (di);
+      if (d_peek_char (di) == 'I')
 	{
-	  /* Add a substitution for the unqualified template name.  */
-	  RETURN_IF_ERROR (substitution_add (dm, start, 0));
-
-	  RETURN_IF_ERROR (demangle_template_args (dm));
-	  *encode_return_type = !suppress_return_type;
+	  /* This is <template-args>, which means that we just saw
+	     <unscoped-template-name>, which is a substitution
+	     candidate.  */
+	  if (! d_add_substitution (di, dc))
+	    return NULL;
+	  dc = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, dc,
+			    d_template_args (di));
 	}
-      else
-	*encode_return_type = 0;
-
-      break;
+      return dc;
     }
-
-  return STATUS_OK;
 }
 
-/* Demangles and emits a <nested-name>. 
+/* <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
+                 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
+*/
 
-    <nested-name>     ::= N [<CV-qualifiers>] <prefix> <unqulified-name> E  */
-
-static status_t
-demangle_nested_name (dm, encode_return_type)
-     demangling_t dm;
-     int *encode_return_type;
+static struct demangle_component *
+d_nested_name (struct d_info *di)
 {
-  char peek;
+  struct demangle_component *ret;
+  struct demangle_component **pret;
 
-  DEMANGLE_TRACE ("nested-name", dm);
+  if (! d_check_char (di, 'N'))
+    return NULL;
 
-  RETURN_IF_ERROR (demangle_char (dm, 'N'));
+  pret = d_cv_qualifiers (di, &ret, 1);
+  if (pret == NULL)
+    return NULL;
 
-  peek = peek_char (dm);
-  if (peek == 'r' || peek == 'V' || peek == 'K')
-    {
-      dyn_string_t cv_qualifiers;
-      status_t status;
+  *pret = d_prefix (di);
+  if (*pret == NULL)
+    return NULL;
 
-      /* Snarf up CV qualifiers.  */
-      cv_qualifiers = dyn_string_new (24);
-      if (cv_qualifiers == NULL)
-	return STATUS_ALLOCATION_FAILED;
-      demangle_CV_qualifiers (dm, cv_qualifiers);
+  if (! d_check_char (di, 'E'))
+    return NULL;
 
-      /* Emit them, preceded by a space.  */
-      status = result_add_char (dm, ' ');
-      if (STATUS_NO_ERROR (status)) 
-	status = result_add_string (dm, cv_qualifiers);
-      /* The CV qualifiers that occur in a <nested-name> will be
-	 qualifiers for member functions.  These are placed at the end
-	 of the function.  Therefore, shift the caret to the left by
-	 the length of the qualifiers, so other text is inserted
-	 before them and they stay at the end.  */
-      result_shift_caret (dm, -dyn_string_length (cv_qualifiers) - 1);
-      /* Clean up.  */
-      dyn_string_delete (cv_qualifiers);
-      RETURN_IF_ERROR (status);
-    }
-
-  RETURN_IF_ERROR (demangle_prefix_v3 (dm, encode_return_type));
-  /* No need to demangle the final <unqualified-name>; demangle_prefix
-     will handle it.  */
-  RETURN_IF_ERROR (demangle_char (dm, 'E'));
-
-  return STATUS_OK;
+  return ret;
 }
 
-/* Demangles and emits a <prefix>.
+/* <prefix> ::= <prefix> <unqualified-name>
+            ::= <template-prefix> <template-args>
+            ::= <template-param>
+            ::=
+            ::= <substitution>
 
-    <prefix>            ::= <prefix> <unqualified-name>
-                        ::= <template-prefix> <template-args>
-			::= # empty
-			::= <substitution>
+   <template-prefix> ::= <prefix> <(template) unqualified-name>
+                     ::= <template-param>
+                     ::= <substitution>
+*/
 
-    <template-prefix>   ::= <prefix>
-                        ::= <substitution>  */
-
-static status_t
-demangle_prefix_v3 (dm, encode_return_type)
-     demangling_t dm;
-     int *encode_return_type;
+static struct demangle_component *
+d_prefix (struct d_info *di)
 {
-  int start = substitution_start (dm);
-  int nested = 0;
-
-  /* ENCODE_RETURN_TYPE is updated as we decend the nesting chain.
-     After <template-args>, it is set to non-zero; after everything
-     else it is set to zero.  */
-
-  /* Generally, the return type is encoded if the function is a
-     template-id, and suppressed otherwise.  There are a few cases,
-     though, in which the return type is not encoded even for a
-     templated function.  In these cases, this flag is set.  */
-  int suppress_return_type = 0;
-
-  DEMANGLE_TRACE ("prefix", dm);
+  struct demangle_component *ret = NULL;
 
   while (1)
     {
       char peek;
+      enum demangle_component_type comb_type;
+      struct demangle_component *dc;
 
-      if (end_of_name_p (dm))
-	return "Unexpected end of name in <compound-name>.";
+      peek = d_peek_char (di);
+      if (peek == '\0')
+	return NULL;
 
-      peek = peek_char (dm);
-      
-      /* We'll initialize suppress_return_type to false, and set it to true
-	 if we end up demangling a constructor name.  However, make
-	 sure we're not actually about to demangle template arguments
-	 -- if so, this is the <template-args> following a
-	 <template-prefix>, so we'll want the previous flag value
-	 around.  */
-      if (peek != 'I')
-	suppress_return_type = 0;
+      /* The older code accepts a <local-name> here, but I don't see
+	 that in the grammar.  The older code does not accept a
+	 <template-param> here.  */
 
-      if (IS_DIGIT ((unsigned char) peek)
-	  || (peek >= 'a' && peek <= 'z')
-	  || peek == 'C' || peek == 'D'
-	  || peek == 'S')
-	{
-	  /* We have another level of scope qualification.  */
-	  if (nested)
-	    RETURN_IF_ERROR (result_add (dm, NAMESPACE_SEPARATOR));
-	  else
-	    nested = 1;
-
-	  if (peek == 'S')
-	    /* The substitution determines whether this is a
-	       template-id.  */
-	    RETURN_IF_ERROR (demangle_substitution (dm, encode_return_type));
-	  else
-	    {
-	      /* It's just a name.  */
-	      RETURN_IF_ERROR 
-		(demangle_unqualified_name (dm, &suppress_return_type));
-	      *encode_return_type = 0;
-	    }
-	}
-      else if (peek == 'Z')
-	RETURN_IF_ERROR (demangle_local_name (dm));
+      comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
+      if (IS_DIGIT (peek)
+	  || IS_LOWER (peek)
+	  || peek == 'C'
+	  || peek == 'D'
+	  || peek == 'L')
+	dc = d_unqualified_name (di);
+      else if (peek == 'S')
+	dc = d_substitution (di, 1);
       else if (peek == 'I')
 	{
-	  RETURN_IF_ERROR (demangle_template_args (dm));
-
-	  /* Now we want to indicate to the caller that we've
-	     demangled template arguments, thus the prefix was a
-	     <template-prefix>.  That's so that the caller knows to
-	     demangle the function's return type, if this turns out to
-	     be a function name.  But, if it's a member template
-	     constructor or a templated conversion operator, report it
-	     as untemplated.  Those never get encoded return types.  */
-	  *encode_return_type = !suppress_return_type;
+	  if (ret == NULL)
+	    return NULL;
+	  comb_type = DEMANGLE_COMPONENT_TEMPLATE;
+	  dc = d_template_args (di);
 	}
+      else if (peek == 'T')
+	dc = d_template_param (di);
       else if (peek == 'E')
-	/* All done.  */
-	return STATUS_OK;
+	return ret;
       else
-	return "Unexpected character in <compound-name>.";
+	return NULL;
 
-      if (peek != 'S'
-	  && peek_char (dm) != 'E')
-	/* Add a new substitution for the prefix thus far.  */
-	RETURN_IF_ERROR (substitution_add (dm, start, *encode_return_type));
+      if (ret == NULL)
+	ret = dc;
+      else
+	ret = d_make_comp (di, comb_type, ret, dc);
+
+      if (peek != 'S' && d_peek_char (di) != 'E')
+	{
+	  if (! d_add_substitution (di, ret))
+	    return NULL;
+	}
     }
 }
 
-/* Demangles and emits an <unqualified-name>.  If this
-   <unqualified-name> is for a special function type that should never
-   have its return type encoded (particularly, a constructor or
-   conversion operator), *SUPPRESS_RETURN_TYPE is set to 1; otherwise,
-   it is set to zero.
+/* <unqualified-name> ::= <operator-name>
+                      ::= <ctor-dtor-name>
+                      ::= <source-name>
+		      ::= <local-source-name> 
 
-    <unqualified-name>  ::= <operator-name>
-			::= <special-name>  
-			::= <source-name>  */
+    <local-source-name>	::= L <source-name> <discriminator>
+*/
 
-static status_t
-demangle_unqualified_name (dm, suppress_return_type)
-     demangling_t dm;
-     int *suppress_return_type;
+static struct demangle_component *
+d_unqualified_name (struct d_info *di)
 {
-  char peek = peek_char (dm);
+  char peek;
 
-  DEMANGLE_TRACE ("unqualified-name", dm);
-
-  /* By default, don't force suppression of the return type (though
-     non-template functions still don't get a return type encoded).  */ 
-  *suppress_return_type = 0;
-
-  if (IS_DIGIT ((unsigned char) peek))
-    RETURN_IF_ERROR (demangle_source_name (dm));
-  else if (peek >= 'a' && peek <= 'z')
+  peek = d_peek_char (di);
+  if (IS_DIGIT (peek))
+    return d_source_name (di);
+  else if (IS_LOWER (peek))
     {
-      int num_args;
+      struct demangle_component *ret;
 
-      /* Conversion operators never have a return type encoded.  */
-      if (peek == 'c' && peek_char_next (dm) == 'v')
-	*suppress_return_type = 1;
-
-      RETURN_IF_ERROR (demangle_operator_name (dm, 0, &num_args));
+      ret = d_operator_name (di);
+      if (ret != NULL && ret->type == DEMANGLE_COMPONENT_OPERATOR)
+	di->expansion += sizeof "operator" + ret->u.s_operator.op->len - 2;
+      return ret;
     }
   else if (peek == 'C' || peek == 'D')
+    return d_ctor_dtor_name (di);
+  else if (peek == 'L')
     {
-      /* Constructors never have a return type encoded.  */
-      if (peek == 'C')
-	*suppress_return_type = 1;
+      struct demangle_component * ret;
 
-      RETURN_IF_ERROR (demangle_ctor_dtor_name (dm));
+      d_advance (di, 1);
+
+      ret = d_source_name (di);
+      if (ret == NULL)
+	return NULL;
+      if (! d_discriminator (di))
+	return NULL;
+      return ret;
     }
   else
-    return "Unexpected character in <unqualified-name>.";
-
-  return STATUS_OK;
+    return NULL;
 }
 
-/* Demangles and emits <source-name>.  
+/* <source-name> ::= <(positive length) number> <identifier>  */
 
-    <source-name> ::= <length number> <identifier>  */
-
-static status_t
-demangle_source_name (dm)
-     demangling_t dm;
+static struct demangle_component *
+d_source_name (struct d_info *di)
 {
-  int length;
+  long len;
+  struct demangle_component *ret;
 
-  DEMANGLE_TRACE ("source-name", dm);
-
-  /* Decode the length of the identifier.  */
-  RETURN_IF_ERROR (demangle_number (dm, &length, 10, 0));
-  if (length == 0)
-    return "Zero length in <source-name>.";
-
-  /* Now the identifier itself.  It's placed into last_source_name,
-     where it can be used to build a constructor or destructor name.  */
-  RETURN_IF_ERROR (demangle_identifier (dm, length, 
-					dm->last_source_name));
-
-  /* Emit it.  */
-  RETURN_IF_ERROR (result_add_string (dm, dm->last_source_name));
-
-  return STATUS_OK;
+  len = d_number (di);
+  if (len <= 0)
+    return NULL;
+  ret = d_identifier (di, len);
+  di->last_name = ret;
+  return ret;
 }
 
-/* Demangles a number, either a <number> or a <positive-number> at the
-   current position, consuming all consecutive digit characters.  Sets
-   *VALUE to the resulting numberand returns STATUS_OK.  The number is
-   interpreted as BASE, which must be either 10 or 36.  If IS_SIGNED
-   is non-zero, negative numbers -- prefixed with `n' -- are accepted.
+/* number ::= [n] <(non-negative decimal integer)>  */
 
-    <number> ::= [n] <positive-number>
-
-    <positive-number> ::= <decimal integer>  */
-
-static status_t
-demangle_number (dm, value, base, is_signed)
-     demangling_t dm;
-     int *value;
-     int base;
-     int is_signed;
+static long
+d_number (struct d_info *di)
 {
-  dyn_string_t number = dyn_string_new (10);
+  int negative;
+  char peek;
+  long ret;
 
-  DEMANGLE_TRACE ("number", dm);
-
-  if (number == NULL)
-    return STATUS_ALLOCATION_FAILED;
-
-  demangle_number_literally (dm, number, base, is_signed);
-  /*
-  *value = strtol (dyn_string_buf (number), NULL, base);
-  */
-  /* vg_assert( base == 10 ); */
-  if ( base != 10 && base != 36 ) {
-     dyn_string_delete(number);
-     return STATUS_UNIMPLEMENTED;
-  }
-
-  if (base == 36) {
-     *value = VG_(atoll36) (dyn_string_buf (number));
-  } else {
-     *value = VG_(atoll) (dyn_string_buf (number));
-  }
-  dyn_string_delete (number);
-
-  return STATUS_OK;
-}
-
-/* Demangles a number at the current position.  The digits (and minus
-   sign, if present) that make up the number are appended to STR.
-   Only base-BASE digits are accepted; BASE must be either 10 or 36.
-   If IS_SIGNED, negative numbers -- prefixed with `n' -- are
-   accepted.  Does not consume a trailing underscore or other
-   terminating character.  */
-
-static status_t
-demangle_number_literally (dm, str, base, is_signed)
-     demangling_t dm;
-     dyn_string_t str;
-     int base;
-     int is_signed;
-{
-  DEMANGLE_TRACE ("number*", dm);
-
-  if (base != 10 && base != 36)
-    return STATUS_INTERNAL_ERROR;
-
-  /* An `n' denotes a negative number.  */
-  if (is_signed && peek_char (dm) == 'n')
+  negative = 0;
+  peek = d_peek_char (di);
+  if (peek == 'n')
     {
-      /* Skip past the n.  */
-      advance_char (dm);
-      /* The normal way to write a negative number is with a minus
-	 sign.  */
-      if (!dyn_string_append_char (str, '-'))
-	return STATUS_ALLOCATION_FAILED;
+      negative = 1;
+      d_advance (di, 1);
+      peek = d_peek_char (di);
     }
 
-  /* Loop until we hit a non-digit.  */
+  ret = 0;
   while (1)
     {
-      char peek = peek_char (dm);
-      if (IS_DIGIT ((unsigned char) peek)
-	  || (base == 36 && peek >= 'A' && peek <= 'Z'))
+      if (! IS_DIGIT (peek))
 	{
-	  /* Accumulate digits.  */
-	  if (!dyn_string_append_char (str, next_char (dm)))
-	    return STATUS_ALLOCATION_FAILED;
+	  if (negative)
+	    ret = - ret;
+	  return ret;
 	}
-      else
-	/* Not a digit?  All done.  */
-	break;
+      ret = ret * 10 + peek - '0';
+      d_advance (di, 1);
+      peek = d_peek_char (di);
     }
-
-  return STATUS_OK;
 }
 
-/* Demangles an identifier at the current position of LENGTH
-   characters and places it in IDENTIFIER.  */
+/* identifier ::= <(unqualified source code identifier)>  */
 
-static status_t
-demangle_identifier (dm, length, identifier)
-     demangling_t dm;
-     int length;
-     dyn_string_t identifier;
+static struct demangle_component *
+d_identifier (struct d_info *di, int len)
 {
-  DEMANGLE_TRACE ("identifier", dm);
+  const char *name;
 
-  dyn_string_clear (identifier);
-  if (!dyn_string_resize (identifier, length))
-    return STATUS_ALLOCATION_FAILED;
+  name = d_str (di);
 
-  while (length-- > 0)
+  if (di->send - name < len)
+    return NULL;
+
+  d_advance (di, len);
+
+  /* A Java mangled name may have a trailing '$' if it is a C++
+     keyword.  This '$' is not included in the length count.  We just
+     ignore the '$'.  */
+  if ((di->options & DMGL_JAVA) != 0
+      && d_peek_char (di) == '$')
+    d_advance (di, 1);
+
+  /* Look for something which looks like a gcc encoding of an
+     anonymous namespace, and replace it with a more user friendly
+     name.  */
+  if (len >= (int) ANONYMOUS_NAMESPACE_PREFIX_LEN + 2
+      && memcmp (name, ANONYMOUS_NAMESPACE_PREFIX,
+		 ANONYMOUS_NAMESPACE_PREFIX_LEN) == 0)
     {
-      if (end_of_name_p (dm))
-	return "Unexpected end of name in <identifier>.";
-      if (!dyn_string_append_char (identifier, next_char (dm)))
-	return STATUS_ALLOCATION_FAILED;
-    }
+      const char *s;
 
-  /* GCC encodes anonymous namespaces using a `_GLOBAL_[_.$]N.'
-     followed by the source file name and some random characters.
-     Unless we're in strict mode, decipher these names appropriately.  */
-  if (!flag_strict)
-    {
-      char *name = dyn_string_buf (identifier);
-      int prefix_length = VG_(strlen) (ANONYMOUS_NAMESPACE_PREFIX);
-
-      /* Compare the first, fixed part.  */
-      if (VG_(strncmp) (name, ANONYMOUS_NAMESPACE_PREFIX, prefix_length) == 0)
-        {
-	  name += prefix_length;
-	  /* The next character might be a period, an underscore, or
-	     dollar sign, depending on the target architecture's
-	     assembler's capabilities.  After that comes an `N'.  */
-	  if ((*name == '.' || *name == '_' || *name == '$')
-	      && *(name + 1) == 'N')
-	    /* This looks like the anonymous namespace identifier.
-	       Replace it with something comprehensible.  */
-	    dyn_string_copy_cstr (identifier, "(anonymous namespace)");
-	}
-    }
-
-  return STATUS_OK;
-}
-
-/* Demangles and emits an <operator-name>.  If SHORT_NAME is non-zero,
-   the short form is emitted; otherwise the full source form
-   (`operator +' etc.) is emitted.  *NUM_ARGS is set to the number of
-   operands that the operator takes.  
-
-    <operator-name>
-                  ::= nw        # new           
-                  ::= na        # new[]
-                  ::= dl        # delete        
-                  ::= da        # delete[]      
-		  ::= ps        # + (unary)
-                  ::= ng        # - (unary)     
-                  ::= ad        # & (unary)     
-                  ::= de        # * (unary)     
-                  ::= co        # ~             
-                  ::= pl        # +             
-                  ::= mi        # -             
-                  ::= ml        # *             
-                  ::= dv        # /             
-                  ::= rm        # %             
-                  ::= an        # &             
-                  ::= or        # |             
-                  ::= eo        # ^             
-                  ::= aS        # =             
-                  ::= pL        # +=            
-                  ::= mI        # -=            
-                  ::= mL        # *=            
-                  ::= dV        # /=            
-                  ::= rM        # %=            
-                  ::= aN        # &=            
-                  ::= oR        # |=            
-                  ::= eO        # ^=            
-                  ::= ls        # <<            
-                  ::= rs        # >>            
-                  ::= lS        # <<=           
-                  ::= rS        # >>=           
-                  ::= eq        # ==            
-                  ::= ne        # !=            
-                  ::= lt        # <             
-                  ::= gt        # >             
-                  ::= le        # <=            
-                  ::= ge        # >=            
-                  ::= nt        # !             
-                  ::= aa        # &&            
-                  ::= oo        # ||            
-                  ::= pp        # ++            
-                  ::= mm        # --            
-                  ::= cm        # ,             
-                  ::= pm        # ->*           
-                  ::= pt        # ->            
-                  ::= cl        # ()            
-                  ::= ix        # []            
-                  ::= qu        # ?
-                  ::= sz        # sizeof 
-                  ::= cv <type> # cast        
-		  ::= v [0-9] <source-name>  # vendor extended operator  */
-
-static status_t
-demangle_operator_name (dm, short_name, num_args)
-     demangling_t dm;
-     int short_name;
-     int *num_args;
-{
-  struct operator_code
-  {
-    /* The mangled code for this operator.  */
-    const char *const code;
-    /* The source name of this operator.  */
-    const char *const name;
-    /* The number of arguments this operator takes.  */
-    const int num_args;
-  };
-
-  static const struct operator_code operators[] = 
-  {
-    { "aN", "&="       , 2 },
-    { "aS", "="        , 2 },
-    { "aa", "&&"       , 2 },
-    { "ad", "&"        , 1 },
-    { "an", "&"        , 2 },
-    { "cl", "()"       , 0 },
-    { "cm", ","        , 2 },
-    { "co", "~"        , 1 },
-    { "dV", "/="       , 2 },
-    { "da", " delete[]", 1 },
-    { "de", "*"        , 1 },
-    { "dl", " delete"  , 1 },
-    { "dv", "/"        , 2 },
-    { "eO", "^="       , 2 },
-    { "eo", "^"        , 2 },
-    { "eq", "=="       , 2 },
-    { "ge", ">="       , 2 },
-    { "gt", ">"        , 2 },
-    { "ix", "[]"       , 2 },
-    { "lS", "<<="      , 2 },
-    { "le", "<="       , 2 },
-    { "ls", "<<"       , 2 },
-    { "lt", "<"        , 2 },
-    { "mI", "-="       , 2 },
-    { "mL", "*="       , 2 },
-    { "mi", "-"        , 2 },
-    { "ml", "*"        , 2 },
-    { "mm", "--"       , 1 },
-    { "na", " new[]"   , 1 },
-    { "ne", "!="       , 2 },
-    { "ng", "-"        , 1 },
-    { "nt", "!"        , 1 },
-    { "nw", " new"     , 1 },
-    { "oR", "|="       , 2 },
-    { "oo", "||"       , 2 },
-    { "or", "|"        , 2 },
-    { "pL", "+="       , 2 },
-    { "pl", "+"        , 2 },
-    { "pm", "->*"      , 2 },
-    { "pp", "++"       , 1 },
-    { "ps", "+"        , 1 },
-    { "pt", "->"       , 2 },
-    { "qu", "?"        , 3 },
-    { "rM", "%="       , 2 },
-    { "rS", ">>="      , 2 },
-    { "rm", "%"        , 2 },
-    { "rs", ">>"       , 2 },
-    { "sz", " sizeof"  , 1 }
-  };
-
-  const int num_operators = 
-    sizeof (operators) / sizeof (struct operator_code);
-
-  int c0 = next_char (dm);
-  int c1 = next_char (dm);
-  const struct operator_code* p1 = operators;
-  const struct operator_code* p2 = operators + num_operators;
-
-  DEMANGLE_TRACE ("operator-name", dm);
-
-  /* Is this a vendor-extended operator?  */
-  if (c0 == 'v' && IS_DIGIT (c1))
-    {
-      RETURN_IF_ERROR (result_add (dm, "operator "));
-      RETURN_IF_ERROR (demangle_source_name (dm));
-      *num_args = 0;
-      return STATUS_OK;
-    }
-
-  /* Is this a conversion operator?  */
-  if (c0 == 'c' && c1 == 'v')
-    {
-      RETURN_IF_ERROR (result_add (dm, "operator "));
-      /* Demangle the converted-to type.  */
-      RETURN_IF_ERROR (demangle_type (dm));
-      *num_args = 0;
-      return STATUS_OK;
-    }
-
-  /* Perform a binary search for the operator code.  */
-  while (1)
-    {
-      const struct operator_code* p = p1 + (p2 - p1) / 2;
-      char match0 = p->code[0];
-      char match1 = p->code[1];
-
-      if (c0 == match0 && c1 == match1)
-	/* Found it.  */
+      s = name + ANONYMOUS_NAMESPACE_PREFIX_LEN;
+      if ((*s == '.' || *s == '_' || *s == '$')
+	  && s[1] == 'N')
 	{
-	  if (!short_name)
-	    RETURN_IF_ERROR (result_add (dm, "operator"));
-	  RETURN_IF_ERROR (result_add (dm, p->name));
-	  *num_args = p->num_args;
+	  di->expansion -= len - sizeof "(anonymous namespace)";
+	  return d_make_name (di, "(anonymous namespace)",
+			      sizeof "(anonymous namespace)" - 1);
+	}
+    }
 
-	  return STATUS_OK;
+  return d_make_name (di, name, len);
+}
+
+/* operator_name ::= many different two character encodings.
+                 ::= cv <type>
+                 ::= v <digit> <source-name>
+*/
+
+#define NL(s) s, (sizeof s) - 1
+
+CP_STATIC_IF_GLIBCPP_V3
+const struct demangle_operator_info cplus_demangle_operators[] =
+{
+  { "aN", NL ("&="),        2 },
+  { "aS", NL ("="),         2 },
+  { "aa", NL ("&&"),        2 },
+  { "ad", NL ("&"),         1 },
+  { "an", NL ("&"),         2 },
+  { "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 },
+  { "eq", NL ("=="),        2 },
+  { "ge", NL (">="),        2 },
+  { "gt", NL (">"),         2 },
+  { "ix", NL ("[]"),        2 },
+  { "lS", NL ("<<="),       2 },
+  { "le", NL ("<="),        2 },
+  { "ls", NL ("<<"),        2 },
+  { "lt", NL ("<"),         2 },
+  { "mI", NL ("-="),        2 },
+  { "mL", NL ("*="),        2 },
+  { "mi", NL ("-"),         2 },
+  { "ml", NL ("*"),         2 },
+  { "mm", NL ("--"),        1 },
+  { "na", NL ("new[]"),     1 },
+  { "ne", NL ("!="),        2 },
+  { "ng", NL ("-"),         1 },
+  { "nt", NL ("!"),         1 },
+  { "nw", NL ("new"),       1 },
+  { "oR", NL ("|="),        2 },
+  { "oo", NL ("||"),        2 },
+  { "or", NL ("|"),         2 },
+  { "pL", NL ("+="),        2 },
+  { "pl", NL ("+"),         2 },
+  { "pm", NL ("->*"),       2 },
+  { "pp", NL ("++"),        1 },
+  { "ps", NL ("+"),         1 },
+  { "pt", NL ("->"),        2 },
+  { "qu", NL ("?"),         3 },
+  { "rM", NL ("%="),        2 },
+  { "rS", NL (">>="),       2 },
+  { "rm", NL ("%"),         2 },
+  { "rs", NL (">>"),        2 },
+  { "st", NL ("sizeof "),   1 },
+  { "sz", NL ("sizeof "),   1 },
+  { NULL, NULL, 0,          0 }
+};
+
+static struct demangle_component *
+d_operator_name (struct d_info *di)
+{
+  char c1;
+  char c2;
+
+  c1 = d_next_char (di);
+  c2 = d_next_char (di);
+  if (c1 == 'v' && IS_DIGIT (c2))
+    return d_make_extended_operator (di, c2 - '0', d_source_name (di));
+  else if (c1 == 'c' && c2 == 'v')
+    return d_make_comp (di, DEMANGLE_COMPONENT_CAST,
+			cplus_demangle_type (di), NULL);
+  else
+    {
+      /* LOW is the inclusive lower bound.  */
+      int low = 0;
+      /* HIGH is the exclusive upper bound.  We subtract one to ignore
+	 the sentinel at the end of the array.  */
+      int high = ((sizeof (cplus_demangle_operators)
+		   / sizeof (cplus_demangle_operators[0]))
+		  - 1);
+
+      while (1)
+	{
+	  int i;
+	  const struct demangle_operator_info *p;
+
+	  i = low + (high - low) / 2;
+	  p = cplus_demangle_operators + i;
+
+	  if (c1 == p->code[0] && c2 == p->code[1])
+	    return d_make_operator (di, p);
+
+	  if (c1 < p->code[0] || (c1 == p->code[0] && c2 < p->code[1]))
+	    high = i;
+	  else
+	    low = i + 1;
+	  if (low == high)
+	    return NULL;
+	}
+    }
+}
+
+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 == p1)
-	/* Couldn't find it.  */
-	return "Unknown code in <operator-name>.";
-
-      /* Try again.  */
-      if (c0 < match0 || (c0 == match0 && c1 < match1))
-	p2 = p;
+      if (p == NULL)
+	p = next;
       else
-	p1 = p;
+	{
+	  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;
 }
 
-/* Demangles and omits an <nv-offset>.
+/* <special-name> ::= TV <type>
+                  ::= TT <type>
+                  ::= TI <type>
+                  ::= TS <type>
+                  ::= GV <(object) name>
+                  ::= T <call-offset> <(base) encoding>
+                  ::= Tc <call-offset> <call-offset> <(base) encoding>
+   Also g++ extensions:
+                  ::= TC <type> <(offset) number> _ <(base) type>
+                  ::= TF <type>
+                  ::= TJ <type>
+                  ::= GR <name>
+		  ::= GA <encoding>
+		  ::= Gr <resource name>
+*/
 
-    <nv-offset> ::= <offset number>   # non-virtual base override  */
-
-static status_t
-demangle_nv_offset (dm)
-     demangling_t dm;
+static struct demangle_component *
+d_special_name (struct d_info *di)
 {
-  dyn_string_t number;
-  status_t status = STATUS_OK;
-
-  DEMANGLE_TRACE ("h-offset", dm);
-
-  /* Demangle the offset.  */
-  number = dyn_string_new (4);
-  if (number == NULL)
-    return STATUS_ALLOCATION_FAILED;
-  demangle_number_literally (dm, number, 10, 1);
-
-  /* Don't display the offset unless in verbose mode.  */
-  if (flag_verbose)
+  di->expansion += 20;
+  if (d_check_char (di, 'T'))
     {
-      status = result_add (dm, " [nv:");
-      if (STATUS_NO_ERROR (status))
-	status = result_add_string (dm, number);
-      if (STATUS_NO_ERROR (status))
-	status = result_add_char (dm, ']');
-    }
-
-  /* Clean up.  */
-  dyn_string_delete (number);
-  RETURN_IF_ERROR (status);
-  return STATUS_OK;
-}
-
-/* Demangles and emits a <v-offset>. 
-
-    <v-offset>  ::= <offset number> _ <virtual offset number>
-			# virtual base override, with vcall offset  */
-
-static status_t
-demangle_v_offset (dm)
-     demangling_t dm;
-{
-  dyn_string_t number;
-  status_t status = STATUS_OK;
-
-  DEMANGLE_TRACE ("v-offset", dm);
-
-  /* Demangle the offset.  */
-  number = dyn_string_new (4);
-  if (number == NULL)
-    return STATUS_ALLOCATION_FAILED;
-  demangle_number_literally (dm, number, 10, 1);
-
-  /* Don't display the offset unless in verbose mode.  */
-  if (flag_verbose)
-    {
-      status = result_add (dm, " [v:");
-      if (STATUS_NO_ERROR (status))
-	status = result_add_string (dm, number);
-      if (STATUS_NO_ERROR (status))
-	result_add_char (dm, ',');
-    }
-  dyn_string_delete (number);
-  RETURN_IF_ERROR (status);
-
-  /* Demangle the separator.  */
-  RETURN_IF_ERROR (demangle_char (dm, '_'));
-
-  /* Demangle the vcall offset.  */
-  number = dyn_string_new (4);
-  if (number == NULL)
-    return STATUS_ALLOCATION_FAILED;
-  demangle_number_literally (dm, number, 10, 1);
-
-  /* Don't display the vcall offset unless in verbose mode.  */
-  if (flag_verbose)
-    {
-      status = result_add_string (dm, number);
-      if (STATUS_NO_ERROR (status))
-	status = result_add_char (dm, ']');
-    }
-  dyn_string_delete (number);
-  RETURN_IF_ERROR (status);
-
-  return STATUS_OK;
-}
-
-/* Demangles and emits a <call-offset>.
-
-    <call-offset> ::= h <nv-offset> _
-		  ::= v <v-offset> _  */
-
-static status_t
-demangle_call_offset (dm)
-     demangling_t dm;
-{
-  DEMANGLE_TRACE ("call-offset", dm);
-
-  switch (peek_char (dm))
-    {
-    case 'h':
-      advance_char (dm);
-      /* Demangle the offset.  */
-      RETURN_IF_ERROR (demangle_nv_offset (dm));
-      /* Demangle the separator.  */
-      RETURN_IF_ERROR (demangle_char (dm, '_'));
-      break;
-
-    case 'v':
-      advance_char (dm);
-      /* Demangle the offset.  */
-      RETURN_IF_ERROR (demangle_v_offset (dm));
-      /* Demangle the separator.  */
-      RETURN_IF_ERROR (demangle_char (dm, '_'));
-      break;
-
-    default:
-      return "Unrecognized <call-offset>.";
-    }
-
-  return STATUS_OK;
-}
-
-/* Demangles and emits a <special-name>.  
-
-    <special-name> ::= GV <object name>   # Guard variable
-                   ::= TV <type>          # virtual table
-                   ::= TT <type>          # VTT
-                   ::= TI <type>          # typeinfo structure
-		   ::= TS <type>          # typeinfo name  
-
-   Other relevant productions include thunks:
-
-    <special-name> ::= T <call-offset> <base encoding>
- 			 # base is the nominal target function of thunk
-
-    <special-name> ::= Tc <call-offset> <call-offset> <base encoding>
-			 # base is the nominal target function of thunk
-			 # first call-offset is 'this' adjustment
-			 # second call-offset is result adjustment
-
-   where
-
-    <call-offset>  ::= h <nv-offset> _
-		   ::= v <v-offset> _
-
-   Also demangles the special g++ manglings,
-
-    <special-name> ::= TC <type> <offset number> _ <base type>
-                                          # construction vtable
-		   ::= TF <type>	  # typeinfo function (old ABI only)
-		   ::= TJ <type>	  # java Class structure  */
-
-static status_t
-demangle_special_name (dm)
-     demangling_t dm;
-{
-  dyn_string_t number;
-  int unused;
-  char peek = peek_char (dm);
-
-  DEMANGLE_TRACE ("special-name", dm);
-
-  if (peek == 'G')
-    {
-      /* Consume the G.  */
-      advance_char (dm);
-      switch (peek_char (dm))
+      switch (d_next_char (di))
 	{
 	case 'V':
-	  /* A guard variable name.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "guard variable for "));
-	  RETURN_IF_ERROR (demangle_name (dm, &unused));
-	  break;
-
-	case 'R':
-	  /* A reference temporary.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "reference temporary for "));
-	  RETURN_IF_ERROR (demangle_name (dm, &unused));
-	  break;
-	  
-	default:
-	  return "Unrecognized <special-name>.";
-	}
-    }
-  else if (peek == 'T')
-    {
-      status_t status = STATUS_OK;
-
-      /* Other C++ implementation miscellania.  Consume the T.  */
-      advance_char (dm);
-
-      switch (peek_char (dm))
-	{
-	case 'V':
-	  /* Virtual table.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "vtable for "));
-	  RETURN_IF_ERROR (demangle_type (dm));
-	  break;
-
+	  di->expansion -= 5;
+	  return d_make_comp (di, DEMANGLE_COMPONENT_VTABLE,
+			      cplus_demangle_type (di), NULL);
 	case 'T':
-	  /* VTT structure.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "VTT for "));
-	  RETURN_IF_ERROR (demangle_type (dm));
-	  break;
-
+	  di->expansion -= 10;
+	  return d_make_comp (di, DEMANGLE_COMPONENT_VTT,
+			      cplus_demangle_type (di), NULL);
 	case 'I':
-	  /* Typeinfo structure.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "typeinfo for "));
-	  RETURN_IF_ERROR (demangle_type (dm));
-	  break;
-
-	case 'F':
-	  /* Typeinfo function.  Used only in old ABI with new mangling.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "typeinfo fn for "));
-	  RETURN_IF_ERROR (demangle_type (dm));
-	  break;
-
+	  return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO,
+			      cplus_demangle_type (di), NULL);
 	case 'S':
-	  /* Character string containing type name, used in typeinfo. */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "typeinfo name for "));
-	  RETURN_IF_ERROR (demangle_type (dm));
-	  break;
-
-	case 'J':
-	  /* The java Class variable corresponding to a C++ class.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "java Class for "));
-	  RETURN_IF_ERROR (demangle_type (dm));
-	  break;
+	  return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_NAME,
+			      cplus_demangle_type (di), NULL);
 
 	case 'h':
-	  /* Non-virtual thunk.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "non-virtual thunk"));
-	  RETURN_IF_ERROR (demangle_nv_offset (dm));
-	  /* Demangle the separator.  */
-	  RETURN_IF_ERROR (demangle_char (dm, '_'));
-	  /* Demangle and emit the target name and function type.  */
-	  RETURN_IF_ERROR (result_add (dm, " to "));
-	  RETURN_IF_ERROR (demangle_encoding (dm));
-	  break;
+	  if (! d_call_offset (di, 'h'))
+	    return NULL;
+	  return d_make_comp (di, DEMANGLE_COMPONENT_THUNK,
+			      d_encoding (di, 0), NULL);
 
 	case 'v':
-	  /* Virtual thunk.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "virtual thunk"));
-	  RETURN_IF_ERROR (demangle_v_offset (dm));
-	  /* Demangle the separator.  */
-	  RETURN_IF_ERROR (demangle_char (dm, '_'));
-	  /* Demangle and emit the target function.  */
-	  RETURN_IF_ERROR (result_add (dm, " to "));
-	  RETURN_IF_ERROR (demangle_encoding (dm));
-	  break;
+	  if (! d_call_offset (di, 'v'))
+	    return NULL;
+	  return d_make_comp (di, DEMANGLE_COMPONENT_VIRTUAL_THUNK,
+			      d_encoding (di, 0), NULL);
 
 	case 'c':
-	  /* Covariant return thunk.  */
-	  advance_char (dm);
-	  RETURN_IF_ERROR (result_add (dm, "covariant return thunk"));
-	  RETURN_IF_ERROR (demangle_call_offset (dm));
-	  RETURN_IF_ERROR (demangle_call_offset (dm));
-	  /* Demangle and emit the target function.  */
-	  RETURN_IF_ERROR (result_add (dm, " to "));
-	  RETURN_IF_ERROR (demangle_encoding (dm));
-	  break;
+	  if (! d_call_offset (di, '\0'))
+	    return NULL;
+	  if (! d_call_offset (di, '\0'))
+	    return NULL;
+	  return d_make_comp (di, DEMANGLE_COMPONENT_COVARIANT_THUNK,
+			      d_encoding (di, 0), NULL);
 
 	case 'C':
-	  /* TC is a special g++ mangling for a construction vtable. */
-	  if (!flag_strict)
-	    {
-	      dyn_string_t derived_type;
+	  {
+	    struct demangle_component *derived_type;
+	    long offset;
+	    struct demangle_component *base_type;
 
-	      advance_char (dm);
-	      RETURN_IF_ERROR (result_add (dm, "construction vtable for "));
+	    derived_type = cplus_demangle_type (di);
+	    offset = d_number (di);
+	    if (offset < 0)
+	      return NULL;
+	    if (! d_check_char (di, '_'))
+	      return NULL;
+	    base_type = cplus_demangle_type (di);
+	    /* We don't display the offset.  FIXME: We should display
+	       it in verbose mode.  */
+	    di->expansion += 5;
+	    return d_make_comp (di, DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
+				base_type, derived_type);
+	  }
 
-	      /* Demangle the derived type off to the side.  */
-	      RETURN_IF_ERROR (result_push (dm));
-	      RETURN_IF_ERROR (demangle_type (dm));
-	      derived_type = (dyn_string_t) result_pop (dm);
-
-	      /* Demangle the offset.  */
-	      number = dyn_string_new (4);
-	      if (number == NULL)
-		{
-		  dyn_string_delete (derived_type);
-		  return STATUS_ALLOCATION_FAILED;
-		}
-	      demangle_number_literally (dm, number, 10, 1);
-	      /* Demangle the underscore separator.  */
-	      status = demangle_char (dm, '_');
-
-	      /* Demangle the base type.  */
-	      if (STATUS_NO_ERROR (status))
-		status = demangle_type (dm);
-
-	      /* Emit the derived type.  */
-	      if (STATUS_NO_ERROR (status))
-		status = result_add (dm, "-in-");
-	      if (STATUS_NO_ERROR (status))
-		status = result_add_string (dm, derived_type);
-	      dyn_string_delete (derived_type);
-
-	      /* Don't display the offset unless in verbose mode.  */
-	      if (flag_verbose)
-		{
-		  status = result_add_char (dm, ' ');
-		  if (STATUS_NO_ERROR (status))
-		    result_add_string (dm, number);
-		}
-	      dyn_string_delete (number);
-	      RETURN_IF_ERROR (status);
-	      break;
-	    }
-	  /* If flag_strict, fall through.  */
+	case 'F':
+	  return d_make_comp (di, DEMANGLE_COMPONENT_TYPEINFO_FN,
+			      cplus_demangle_type (di), NULL);
+	case 'J':
+	  return d_make_comp (di, DEMANGLE_COMPONENT_JAVA_CLASS,
+			      cplus_demangle_type (di), NULL);
 
 	default:
-	  return "Unrecognized <special-name>.";
+	  return NULL;
+	}
+    }
+  else if (d_check_char (di, 'G'))
+    {
+      switch (d_next_char (di))
+	{
+	case 'V':
+	  return d_make_comp (di, DEMANGLE_COMPONENT_GUARD, d_name (di), NULL);
+
+	case 'R':
+	  return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, d_name (di),
+			      NULL);
+
+	case 'A':
+	  return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
+			      d_encoding (di, 0), NULL);
+
+	case 'r':
+	  return d_java_resource (di);
+
+	default:
+	  return NULL;
 	}
     }
   else
-    return STATUS_ERROR;
-
-  return STATUS_OK;
+    return NULL;
 }
 
-/* Demangles and emits a <ctor-dtor-name>.  
-   
-    <ctor-dtor-name>
-                   ::= C1  # complete object (in-charge) ctor
-                   ::= C2  # base object (not-in-charge) ctor
-                   ::= C3  # complete object (in-charge) allocating ctor
-                   ::= D0  # deleting (in-charge) dtor
-                   ::= D1  # complete object (in-charge) dtor
-                   ::= D2  # base object (not-in-charge) dtor  */
+/* <call-offset> ::= h <nv-offset> _
+                 ::= v <v-offset> _
 
-static status_t
-demangle_ctor_dtor_name (dm)
-     demangling_t dm;
+   <nv-offset> ::= <(offset) number>
+
+   <v-offset> ::= <(offset) number> _ <(virtual offset) number>
+
+   The C parameter, if not '\0', is a character we just read which is
+   the start of the <call-offset>.
+
+   We don't display the offset information anywhere.  FIXME: We should
+   display it in verbose mode.  */
+
+static int
+d_call_offset (struct d_info *di, int c)
 {
-  static const char *const ctor_flavors[] = 
-  {
-    "in-charge",
-    "not-in-charge",
-    "allocating"
-  };
-  static const char *const dtor_flavors[] = 
-  {
-    "in-charge deleting",
-    "in-charge",
-    "not-in-charge"
-  };
+  if (c == '\0')
+    c = d_next_char (di);
 
-  int flavor;
-  char peek = peek_char (dm);
-
-  DEMANGLE_TRACE ("ctor-dtor-name", dm);
-  
-  if (peek == 'C')
+  if (c == 'h')
+    d_number (di);
+  else if (c == 'v')
     {
-      /* A constructor name.  Consume the C.  */
-      advance_char (dm);
-      flavor = next_char (dm);
-      if (flavor < '1' || flavor > '3')
-	return "Unrecognized constructor.";
-      RETURN_IF_ERROR (result_add_string (dm, dm->last_source_name));
-      switch (flavor)
-	{
-	case '1': dm->is_constructor = gnu_v3_complete_object_ctor;
-	  break;
-	case '2': dm->is_constructor = gnu_v3_base_object_ctor;
-	  break;
-	case '3': dm->is_constructor = gnu_v3_complete_object_allocating_ctor;
-	  break;
-	}
-      /* Print the flavor of the constructor if in verbose mode.  */
-      if (flag_verbose)
-	{
-	  RETURN_IF_ERROR (result_add (dm, "["));
-	  RETURN_IF_ERROR (result_add (dm, ctor_flavors[flavor - '1']));
-	  RETURN_IF_ERROR (result_add_char (dm, ']'));
-	}
-    }
-  else if (peek == 'D')
-    {
-      /* A destructor name.  Consume the D.  */
-      advance_char (dm);
-      flavor = next_char (dm);
-      if (flavor < '0' || flavor > '2')
-	return "Unrecognized destructor.";
-      RETURN_IF_ERROR (result_add_char (dm, '~'));
-      RETURN_IF_ERROR (result_add_string (dm, dm->last_source_name));
-      switch (flavor)
-	{
-	case '0': dm->is_destructor = gnu_v3_deleting_dtor;
-	  break;
-	case '1': dm->is_destructor = gnu_v3_complete_object_dtor;
-	  break;
-	case '2': dm->is_destructor = gnu_v3_base_object_dtor;
-	  break;
-	}
-      /* Print the flavor of the destructor if in verbose mode.  */
-      if (flag_verbose)
-	{
-	  RETURN_IF_ERROR (result_add (dm, " ["));
-	  RETURN_IF_ERROR (result_add (dm, dtor_flavors[flavor - '0']));
-	  RETURN_IF_ERROR (result_add_char (dm, ']'));
-	}
+      d_number (di);
+      if (! d_check_char (di, '_'))
+	return 0;
+      d_number (di);
     }
   else
-    return STATUS_ERROR;
+    return 0;
 
-  return STATUS_OK;
+  if (! d_check_char (di, '_'))
+    return 0;
+
+  return 1;
 }
 
-/* Handle pointer, reference, and pointer-to-member cases for
-   demangle_type.  All consecutive `P's, `R's, and 'M's are joined to
-   build a pointer/reference type.  We snarf all these, plus the
-   following <type>, all at once since we need to know whether we have
-   a pointer to data or pointer to function to construct the right
-   output syntax.  C++'s pointer syntax is hairy.  
+/* <ctor-dtor-name> ::= C1
+                    ::= C2
+                    ::= C3
+                    ::= D0
+                    ::= D1
+                    ::= D2
+*/
 
-   This function adds substitution candidates for every nested
-   pointer/reference type it processes, including the outermost, final
-   type, assuming the substitution starts at SUBSTITUTION_START in the
-   demangling result.  For example, if this function demangles
-   `PP3Foo', it will add a substitution for `Foo', `Foo*', and
-   `Foo**', in that order.
-
-   *INSERT_POS is a quantity used internally, when this function calls
-   itself recursively, to figure out where to insert pointer
-   punctuation on the way up.  On entry to this function, INSERT_POS
-   should point to a temporary value, but that value need not be
-   initialized.
-
-     <type> ::= P <type>
-            ::= R <type>
-            ::= <pointer-to-member-type>
-
-     <pointer-to-member-type> ::= M </class/ type> </member/ type>  */
-
-static status_t
-demangle_type_ptr (dm, insert_pos, substitution_start2)
-     demangling_t dm;
-     int *insert_pos;
-     int substitution_start2;
+static struct demangle_component *
+d_ctor_dtor_name (struct d_info *di)
 {
-  status_t status;
-  int is_substitution_candidate = 1;
-
-  DEMANGLE_TRACE ("type*", dm);
-
-  /* Scan forward, collecting pointers and references into symbols,
-     until we hit something else.  Then emit the type.  */
-  switch (peek_char (dm))
+  if (di->last_name != NULL)
     {
-    case 'P':
-      /* A pointer.  Snarf the `P'.  */
-      advance_char (dm);
-      /* Demangle the underlying type.  */
-      RETURN_IF_ERROR (demangle_type_ptr (dm, insert_pos, 
-					  substitution_start2));
-      /* Insert an asterisk where we're told to; it doesn't
-	 necessarily go at the end.  If we're doing Java style output, 
-	 there is no pointer symbol.  */
-      if (dm->style != DMGL_JAVA)
-	RETURN_IF_ERROR (result_insert_char (dm, *insert_pos, '*'));
-      /* The next (outermost) pointer or reference character should go
-	 after this one.  */
-      ++(*insert_pos);
-      break;
-
-    case 'R':
-      /* A reference.  Snarf the `R'.  */
-      advance_char (dm);
-      /* Demangle the underlying type.  */
-      RETURN_IF_ERROR (demangle_type_ptr (dm, insert_pos, 
-					  substitution_start2));
-      /* Insert an ampersand where we're told to; it doesn't
-	 necessarily go at the end.  */
-      RETURN_IF_ERROR (result_insert_char (dm, *insert_pos, '&'));
-      /* The next (outermost) pointer or reference character should go
-	 after this one.  */
-      ++(*insert_pos);
-      break;
-
-    case 'M':
-    {
-      /* A pointer-to-member.  */
-      dyn_string_t class_type;
-      
-      /* Eat the 'M'.  */
-      advance_char (dm);
-      
-      /* Capture the type of which this is a pointer-to-member.  */
-      RETURN_IF_ERROR (result_push (dm));
-      RETURN_IF_ERROR (demangle_type (dm));
-      class_type = (dyn_string_t) result_pop (dm);
-      
-      if (peek_char (dm) == 'F')
-	/* A pointer-to-member function.  We want output along the
-	   lines of `void (C::*) (int, int)'.  Demangle the function
-	   type, which would in this case give `void () (int, int)'
-	   and set *insert_pos to the spot between the first
-	   parentheses.  */
-	status = demangle_type_ptr (dm, insert_pos, substitution_start2);
-      else if (peek_char (dm) == 'A')
-	/* A pointer-to-member array variable.  We want output that
-	   looks like `int (Klass::*) [10]'.  Demangle the array type
-	   as `int () [10]', and set *insert_pos to the spot between
-	   the parentheses.  */
-	status = demangle_array_type (dm, insert_pos);
-      else
-        {
-	  /* A pointer-to-member variable.  Demangle the type of the
-             pointed-to member.  */
-	  status = demangle_type (dm);
-	  /* Make it pretty.  */
-	  if (STATUS_NO_ERROR (status)
-	      && !result_previous_char_is_space (dm))
-	    status = result_add_char (dm, ' ');
-	  /* The pointer-to-member notation (e.g. `C::*') follows the
-             member's type.  */
-	  *insert_pos = result_caret_pos (dm);
-	}
-
-      /* Build the pointer-to-member notation.  */
-      if (STATUS_NO_ERROR (status))
-	status = result_insert (dm, *insert_pos, "::*");
-      if (STATUS_NO_ERROR (status))
-	status = result_insert_string (dm, *insert_pos, class_type);
-      /* There may be additional levels of (pointer or reference)
-	 indirection in this type.  If so, the `*' and `&' should be
-	 added after the pointer-to-member notation (e.g. `C::*&' for
-	 a reference to a pointer-to-member of class C).  */
-      *insert_pos += dyn_string_length (class_type) + 3;
-
-      /* Clean up. */
-      dyn_string_delete (class_type);
-
-      RETURN_IF_ERROR (status);
+      if (di->last_name->type == DEMANGLE_COMPONENT_NAME)
+	di->expansion += di->last_name->u.s_name.len;
+      else if (di->last_name->type == DEMANGLE_COMPONENT_SUB_STD)
+	di->expansion += di->last_name->u.s_string.len;
     }
-    break;
+  switch (d_peek_char (di))
+    {
+    case 'C':
+      {
+	enum gnu_v3_ctor_kinds kind;
+
+	switch (d_peek_next_char (di))
+	  {
+	  case '1':
+	    kind = gnu_v3_complete_object_ctor;
+	    break;
+	  case '2':
+	    kind = gnu_v3_base_object_ctor;
+	    break;
+	  case '3':
+	    kind = gnu_v3_complete_object_allocating_ctor;
+	    break;
+	  default:
+	    return NULL;
+	  }
+	d_advance (di, 2);
+	return d_make_ctor (di, kind, di->last_name);
+      }
+
+    case 'D':
+      {
+	enum gnu_v3_dtor_kinds kind;
+
+	switch (d_peek_next_char (di))
+	  {
+	  case '0':
+	    kind = gnu_v3_deleting_dtor;
+	    break;
+	  case '1':
+	    kind = gnu_v3_complete_object_dtor;
+	    break;
+	  case '2':
+	    kind = gnu_v3_base_object_dtor;
+	    break;
+	  default:
+	    return NULL;
+	  }
+	d_advance (di, 2);
+	return d_make_dtor (di, kind, di->last_name);
+      }
+
+    default:
+      return NULL;
+    }
+}
+
+/* <type> ::= <builtin-type>
+          ::= <function-type>
+          ::= <class-enum-type>
+          ::= <array-type>
+          ::= <pointer-to-member-type>
+          ::= <template-param>
+          ::= <template-template-param> <template-args>
+          ::= <substitution>
+          ::= <CV-qualifiers> <type>
+          ::= P <type>
+          ::= R <type>
+          ::= O <type> (C++0x)
+          ::= C <type>
+          ::= G <type>
+          ::= U <source-name> <type>
+
+   <builtin-type> ::= various one letter codes
+                  ::= u <source-name>
+*/
+
+CP_STATIC_IF_GLIBCPP_V3
+const struct demangle_builtin_type_info
+cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT] =
+{
+  /* a */ { NL ("signed char"),	NL ("signed char"),	D_PRINT_DEFAULT },
+  /* b */ { NL ("bool"),	NL ("boolean"),		D_PRINT_BOOL },
+  /* c */ { NL ("char"),	NL ("byte"),		D_PRINT_DEFAULT },
+  /* d */ { NL ("double"),	NL ("double"),		D_PRINT_FLOAT },
+  /* e */ { NL ("long double"),	NL ("long double"),	D_PRINT_FLOAT },
+  /* f */ { NL ("float"),	NL ("float"),		D_PRINT_FLOAT },
+  /* g */ { NL ("__float128"),	NL ("__float128"),	D_PRINT_FLOAT },
+  /* h */ { NL ("unsigned char"), NL ("unsigned char"),	D_PRINT_DEFAULT },
+  /* i */ { NL ("int"),		NL ("int"),		D_PRINT_INT },
+  /* j */ { NL ("unsigned int"), NL ("unsigned"),	D_PRINT_UNSIGNED },
+  /* k */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
+  /* l */ { NL ("long"),	NL ("long"),		D_PRINT_LONG },
+  /* m */ { NL ("unsigned long"), NL ("unsigned long"),	D_PRINT_UNSIGNED_LONG },
+  /* n */ { NL ("__int128"),	NL ("__int128"),	D_PRINT_DEFAULT },
+  /* o */ { NL ("unsigned __int128"), NL ("unsigned __int128"),
+	    D_PRINT_DEFAULT },
+  /* p */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
+  /* q */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
+  /* r */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
+  /* s */ { NL ("short"),	NL ("short"),		D_PRINT_DEFAULT },
+  /* t */ { NL ("unsigned short"), NL ("unsigned short"), D_PRINT_DEFAULT },
+  /* u */ { NULL, 0,		NULL, 0,		D_PRINT_DEFAULT },
+  /* v */ { NL ("void"),	NL ("void"),		D_PRINT_VOID },
+  /* w */ { NL ("wchar_t"),	NL ("char"),		D_PRINT_DEFAULT },
+  /* x */ { NL ("long long"),	NL ("long"),		D_PRINT_LONG_LONG },
+  /* 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 },
+};
+
+CP_STATIC_IF_GLIBCPP_V3
+struct demangle_component *
+cplus_demangle_type (struct d_info *di)
+{
+  char peek;
+  struct demangle_component *ret = NULL;
+  int can_subst;
+
+  /* The ABI specifies that when CV-qualifiers are used, the base type
+     is substitutable, and the fully qualified type is substitutable,
+     but the base type with a strict subset of the CV-qualifiers is
+     not substitutable.  The natural recursive implementation of the
+     CV-qualifiers would cause subsets to be substitutable, so instead
+     we pull them all off now.
+
+     FIXME: The ABI says that order-insensitive vendor qualifiers
+     should be handled in the same way, but we have no way to tell
+     which vendor qualifiers are order-insensitive and which are
+     order-sensitive.  So we just assume that they are all
+     order-sensitive.  g++ 3.4 supports only one vendor qualifier,
+     __vector, and it treats it as order-sensitive when mangling
+     names.  */
+
+  peek = d_peek_char (di);
+  if (peek == 'r' || peek == 'V' || peek == 'K')
+    {
+      struct demangle_component **pret;
+
+      pret = d_cv_qualifiers (di, &ret, 0);
+      if (pret == NULL)
+	return NULL;
+      *pret = cplus_demangle_type (di);
+      if (! *pret || ! d_add_substitution (di, ret))
+	return NULL;
+      return ret;
+    }
+
+  can_subst = 1;
+
+  switch (peek)
+    {
+    case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
+    case 'h': case 'i': case 'j':           case 'l': case 'm': case 'n':
+    case 'o':                               case 's': case 't':
+    case 'v': case 'w': case 'x': case 'y': case 'z':
+      ret = d_make_builtin_type (di,
+				 &cplus_demangle_builtin_types[peek - 'a']);
+      di->expansion += ret->u.s_builtin.type->len;
+      can_subst = 0;
+      d_advance (di, 1);
+      break;
+
+    case 'u':
+      d_advance (di, 1);
+      ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE,
+			 d_source_name (di), NULL);
+      break;
 
     case 'F':
-      /* Ooh, tricky, a pointer-to-function.  When we demangle the
-	 function type, the return type should go at the very
-	 beginning.  */
-      *insert_pos = result_caret_pos (dm);
-      /* The parentheses indicate this is a function pointer or
-	 reference type.  */
-      RETURN_IF_ERROR (result_add (dm, "()"));
-      /* Now demangle the function type.  The return type will be
-	 inserted before the `()', and the argument list will go after
-	 it.  */
-      RETURN_IF_ERROR (demangle_function_type (dm, insert_pos));
-      /* We should now have something along the lines of 
-	 `void () (int, int)'.  The pointer or reference characters
-	 have to inside the first set of parentheses.  *insert_pos has
-	 already been updated to point past the end of the return
-	 type.  Move it one character over so it points inside the
-	 `()'.  */
-      ++(*insert_pos);
+      ret = d_function_type (di);
+      break;
+
+    case '0': case '1': case '2': case '3': case '4':
+    case '5': case '6': case '7': case '8': case '9':
+    case 'N':
+    case 'Z':
+      ret = d_class_enum_type (di);
       break;
 
     case 'A':
-      /* An array pointer or reference.  demangle_array_type will figure
-	 out where the asterisks and ampersands go.  */
-      RETURN_IF_ERROR (demangle_array_type (dm, insert_pos));
+      ret = d_array_type (di);
       break;
 
-    default:
-      /* No more pointer or reference tokens; this is therefore a
-	 pointer to data.  Finish up by demangling the underlying
-	 type.  */
-      RETURN_IF_ERROR (demangle_type (dm));
-      /* The pointer or reference characters follow the underlying
-	 type, as in `int*&'.  */
-      *insert_pos = result_caret_pos (dm);
-      /* Because of the production <type> ::= <substitution>,
-	 demangle_type will already have added the underlying type as
-	 a substitution candidate.  Don't do it again.  */
-      is_substitution_candidate = 0;
+    case 'M':
+      ret = d_pointer_to_member_type (di);
       break;
-    }
-  
-  if (is_substitution_candidate)
-    RETURN_IF_ERROR (substitution_add (dm, substitution_start2, 0));
-  
-  return STATUS_OK;
-}
 
-/* Demangles and emits a <type>.  
-
-    <type> ::= <builtin-type>
-	   ::= <function-type>
-	   ::= <class-enum-type>
-	   ::= <array-type>
-	   ::= <pointer-to-member-type>
-	   ::= <template-param>
-	   ::= <template-template-param> <template-args>
-           ::= <CV-qualifiers> <type>
-	   ::= P <type>   # pointer-to
-	   ::= R <type>   # reference-to
-	   ::= C <type>   # complex pair (C 2000)
-	   ::= G <type>   # imaginary (C 2000)
-	   ::= U <source-name> <type>     # vendor extended type qualifier
-	   ::= <substitution>  */
-
-static status_t
-demangle_type (dm)
-     demangling_t dm;
-{
-  int start = substitution_start (dm);
-  char peek = peek_char (dm);
-  char peek_next;
-  int encode_return_type = 0;
-  template_arg_list_t old_arg_list = current_template_arg_list (dm);
-  int insert_pos;
-
-  /* A <type> can be a <substitution>; therefore, this <type> is a
-     substitution candidate unless a special condition holds (see
-     below).  */
-  int is_substitution_candidate = 1;
-
-  DEMANGLE_TRACE ("type", dm);
-
-  /* A <class-enum-type> can start with a digit (a <source-name>), an
-     N (a <nested-name>), or a Z (a <local-name>).  */
-  if (IS_DIGIT ((unsigned char) peek) || peek == 'N' || peek == 'Z')
-    RETURN_IF_ERROR (demangle_class_enum_type (dm, &encode_return_type));
-  /* Lower-case letters begin <builtin-type>s, except for `r', which
-     denotes restrict.  */
-  else if (peek >= 'a' && peek <= 'z' && peek != 'r')
-    {
-      RETURN_IF_ERROR (demangle_builtin_type (dm));
-      /* Built-in types are not substitution candidates.  */
-      is_substitution_candidate = 0;
-    }
-  else
-    switch (peek)
-      {
-      case 'r':
-      case 'V':
-      case 'K':
-	/* CV-qualifiers (including restrict).  We have to demangle
-	   them off to the side, since C++ syntax puts them in a funny
-	   place for qualified pointer and reference types.  */
+    case 'T':
+      ret = d_template_param (di);
+      if (d_peek_char (di) == 'I')
 	{
-	  status_t status;
-	  dyn_string_t cv_qualifiers = dyn_string_new (24);
-	  int old_caret_position = result_get_caret (dm);
-
-	  if (cv_qualifiers == NULL)
-	    return STATUS_ALLOCATION_FAILED;
-
-	  /* Decode all adjacent CV qualifiers.  */
-	  demangle_CV_qualifiers (dm, cv_qualifiers);
-	  /* Emit them, and shift the caret left so that the
-	     underlying type will be emitted before the qualifiers.  */
-	  status = result_add_string (dm, cv_qualifiers);
-	  result_shift_caret (dm, -dyn_string_length (cv_qualifiers));
-	  /* Clean up.  */
-	  dyn_string_delete (cv_qualifiers);
-	  RETURN_IF_ERROR (status);
-	  /* Also prepend a blank, if needed.  */
-	  RETURN_IF_ERROR (result_add_char (dm, ' '));
-	  result_shift_caret (dm, -1);
-
-	  /* Demangle the underlying type.  It will be emitted before
-	     the CV qualifiers, since we moved the caret.  */
-	  RETURN_IF_ERROR (demangle_type (dm));
-
-	  /* Put the caret back where it was previously.  */
-	  result_set_caret (dm, old_caret_position);
+	  /* This is <template-template-param> <template-args>.  The
+	     <template-template-param> part is a substitution
+	     candidate.  */
+	  if (! d_add_substitution (di, ret))
+	    return NULL;
+	  ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
+			     d_template_args (di));
 	}
-	break;
+      break;
 
-      case 'F':
-	return "Non-pointer or -reference function type.";
+    case 'S':
+      /* If this is a special substitution, then it is the start of
+	 <class-enum-type>.  */
+      {
+	char peek_next;
 
-      case 'A':
-	RETURN_IF_ERROR (demangle_array_type (dm, NULL));
-	break;
-
-      case 'T':
-	/* It's either a <template-param> or a
-	   <template-template-param>.  In either case, demangle the
-	   `T' token first.  */
-	RETURN_IF_ERROR (demangle_template_param (dm));
-
-	/* Check for a template argument list; if one is found, it's a
-	     <template-template-param> ::= <template-param>
-                                       ::= <substitution>  */
-	if (peek_char (dm) == 'I')
+	peek_next = d_peek_next_char (di);
+	if (IS_DIGIT (peek_next)
+	    || peek_next == '_'
+	    || IS_UPPER (peek_next))
 	  {
-	    /* Add a substitution candidate.  The template parameter
-	       `T' token is a substitution candidate by itself,
-	       without the template argument list.  */
-	    RETURN_IF_ERROR (substitution_add (dm, start, encode_return_type));
-
-	    /* Now demangle the template argument list.  */
-	    RETURN_IF_ERROR (demangle_template_args (dm));
-	    /* The entire type, including the template template
-	       parameter and its argument list, will be added as a
-	       substitution candidate below.  */
-	  }
-
-	break;
-
-      case 'S':
-	/* First check if this is a special substitution.  If it is,
-	   this is a <class-enum-type>.  Special substitutions have a
-	   letter following the `S'; other substitutions have a digit
-	   or underscore.  */
-	peek_next = peek_char_next (dm);
-	if (IS_DIGIT (peek_next) || peek_next == '_')
-	  {
-	    RETURN_IF_ERROR (demangle_substitution (dm, &encode_return_type));
-	    
-	    /* The substituted name may have been a template name.
-	       Check if template arguments follow, and if so, demangle
-	       them.  */
-	    if (peek_char (dm) == 'I')
-	      RETURN_IF_ERROR (demangle_template_args (dm));
+	    ret = d_substitution (di, 0);
+	    /* The substituted name may have been a template name and
+	       may be followed by tepmlate args.  */
+	    if (d_peek_char (di) == 'I')
+	      ret = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, ret,
+				 d_template_args (di));
 	    else
-	      /* A substitution token is not itself a substitution
-		 candidate.  (However, if the substituted template is
-		 instantiated, the resulting type is.)  */
-	      is_substitution_candidate = 0;
+	      can_subst = 0;
 	  }
 	else
 	  {
-	    /* Now some trickiness.  We have a special substitution
-	       here.  Often, the special substitution provides the
-	       name of a template that's subsequently instantiated,
-	       for instance `SaIcE' => std::allocator<char>.  In these
-	       cases we need to add a substitution candidate for the
-	       entire <class-enum-type> and thus don't want to clear
-	       the is_substitution_candidate flag.
-
-	       However, it's possible that what we have here is a
-	       substitution token representing an entire type, such as
-	       `Ss' => std::string.  In this case, we mustn't add a
-	       new substitution candidate for this substitution token.
-	       To detect this case, remember where the start of the
-	       substitution token is.  */
- 	    const char *next = dm->next;
-	    /* Now demangle the <class-enum-type>.  */
-	    RETURN_IF_ERROR 
-	      (demangle_class_enum_type (dm, &encode_return_type));
-	    /* If all that was just demangled is the two-character
-	       special substitution token, suppress the addition of a
-	       new candidate for it.  */
-	    if (dm->next == next + 2)
-	      is_substitution_candidate = 0;
+	    ret = d_class_enum_type (di);
+	    /* If the substitution was a complete type, then it is not
+	       a new substitution candidate.  However, if the
+	       substitution was followed by template arguments, then
+	       the whole thing is a substitution candidate.  */
+	    if (ret != NULL && ret->type == DEMANGLE_COMPONENT_SUB_STD)
+	      can_subst = 0;
 	  }
-
-	break;
-
-      case 'P':
-      case 'R':
-      case 'M':
-	RETURN_IF_ERROR (demangle_type_ptr (dm, &insert_pos, start));
-	/* demangle_type_ptr adds all applicable substitution
-	   candidates.  */
-	is_substitution_candidate = 0;
-	break;
-
-      case 'C':
-	/* A C99 complex type.  */
-	RETURN_IF_ERROR (result_add (dm, "complex "));
-	advance_char (dm);
-	RETURN_IF_ERROR (demangle_type (dm));
-	break;
-
-      case 'G':
-	/* A C99 imaginary type.  */
-	RETURN_IF_ERROR (result_add (dm, "imaginary "));
-	advance_char (dm);
-	RETURN_IF_ERROR (demangle_type (dm));
-	break;
-
-      case 'U':
-	/* Vendor-extended type qualifier.  */
-	advance_char (dm);
-	RETURN_IF_ERROR (demangle_source_name (dm));
-	RETURN_IF_ERROR (result_add_char (dm, ' '));
-	RETURN_IF_ERROR (demangle_type (dm));
-	break;
-
-      default:
-	return "Unexpected character in <type>.";
       }
-
-  if (is_substitution_candidate)
-    /* Add a new substitution for the type. If this type was a
-       <template-param>, pass its index since from the point of
-       substitutions; a <template-param> token is a substitution
-       candidate distinct from the type that is substituted for it.  */
-    RETURN_IF_ERROR (substitution_add (dm, start, encode_return_type));
-
-  /* Pop off template argument lists added during mangling of this
-     type.  */
-  pop_to_template_arg_list (dm, old_arg_list);
-
-  return STATUS_OK;
-}
-
-/* C++ source names of builtin types, indexed by the mangled code
-   letter's position in the alphabet ('a' -> 0, 'b' -> 1, etc).  */
-static const char *const builtin_type_names[26] = 
-{
-  "signed char",              /* a */
-  "bool",                     /* b */
-  "char",                     /* c */
-  "double",                   /* d */
-  "long double",              /* e */
-  "float",                    /* f */
-  "__float128",               /* g */
-  "unsigned char",            /* h */
-  "int",                      /* i */
-  "unsigned",                 /* j */
-  NULL,                       /* k */
-  "long",                     /* l */
-  "unsigned long",            /* m */
-  "__int128",                 /* n */
-  "unsigned __int128",        /* o */
-  NULL,                       /* p */
-  NULL,                       /* q */
-  NULL,                       /* r */
-  "short",                    /* s */
-  "unsigned short",           /* t */
-  NULL,                       /* u */
-  "void",                     /* v */
-  "wchar_t",                  /* w */
-  "long long",                /* x */
-  "unsigned long long",       /* y */
-  "..."                       /* z */
-};
-
-/* Java source names of builtin types.  Types that arn't valid in Java
-   are also included here - we don't fail if someone attempts to demangle a 
-   C++ symbol in Java style. */
-static const char *const java_builtin_type_names[26] = 
-{
-  "signed char",                /* a */
-  "boolean", /* C++ "bool" */   /* b */
-  "byte", /* C++ "char" */      /* c */
-  "double",                     /* d */
-  "long double",                /* e */
-  "float",                      /* f */
-  "__float128",                 /* g */
-  "unsigned char",              /* h */
-  "int",                        /* i */
-  "unsigned",                   /* j */
-  NULL,                         /* k */
-  "long",                       /* l */
-  "unsigned long",              /* m */
-  "__int128",                   /* n */
-  "unsigned __int128",          /* o */
-  NULL,                         /* p */
-  NULL,                         /* q */
-  NULL,                         /* r */
-  "short",                      /* s */
-  "unsigned short",             /* t */
-  NULL,                         /* u */
-  "void",                       /* v */
-  "char", /* C++ "wchar_t" */   /* w */
-  "long", /* C++ "long long" */ /* x */
-  "unsigned long long",         /* y */
-  "..."                         /* z */
-};
-
-/* Demangles and emits a <builtin-type>.  
-
-    <builtin-type> ::= v  # void
-		   ::= w  # wchar_t
-		   ::= b  # bool
-		   ::= c  # char
-		   ::= a  # signed char
-		   ::= h  # unsigned char
-		   ::= s  # short
-		   ::= t  # unsigned short
-		   ::= i  # int
-		   ::= j  # unsigned int
-		   ::= l  # long
-		   ::= m  # unsigned long
-		   ::= x  # long long, __int64
-		   ::= y  # unsigned long long, __int64
-		   ::= n  # __int128
-		   ::= o  # unsigned __int128
-		   ::= f  # float
-		   ::= d  # double
-		   ::= e  # long double, __float80
-		   ::= g  # __float128
-		   ::= z  # ellipsis
-		   ::= u <source-name>    # vendor extended type  */
-
-static status_t
-demangle_builtin_type (dm)
-     demangling_t dm;
-{
-
-  char code = peek_char (dm);
-
-  DEMANGLE_TRACE ("builtin-type", dm);
-
-  if (code == 'u')
-    {
-      advance_char (dm);
-      RETURN_IF_ERROR (demangle_source_name (dm));
-      return STATUS_OK;
-    }
-  else if (code >= 'a' && code <= 'z')
-    {
-      const char *type_name;
-      /* Java uses different names for some built-in types. */
-      if (dm->style == DMGL_JAVA)
-        type_name = java_builtin_type_names[code - 'a'];
-      else
-        type_name = builtin_type_names[code - 'a'];
-      if (type_name == NULL)
-	return "Unrecognized <builtin-type> code.";
-
-      RETURN_IF_ERROR (result_add (dm, type_name));
-      advance_char (dm);
-      return STATUS_OK;
-    }
-  else
-    return "Non-alphabetic <builtin-type> code.";
-}
-
-/* Demangles all consecutive CV-qualifiers (const, volatile, and
-   restrict) at the current position.  The qualifiers are appended to
-   QUALIFIERS.  Returns STATUS_OK.  */
-
-static status_t
-demangle_CV_qualifiers (dm, qualifiers)
-     demangling_t dm;
-     dyn_string_t qualifiers;
-{
-  DEMANGLE_TRACE ("CV-qualifiers", dm);
-
-  while (1)
-    {
-      switch (peek_char (dm))
-	{
-	case 'r':
-	  if (!dyn_string_append_space (qualifiers))
-	    return STATUS_ALLOCATION_FAILED;
-	  if (!dyn_string_append_cstr (qualifiers, "restrict"))
-	    return STATUS_ALLOCATION_FAILED;
-	  break;
-
-	case 'V':
-	  if (!dyn_string_append_space (qualifiers))
-	    return STATUS_ALLOCATION_FAILED;
-	  if (!dyn_string_append_cstr (qualifiers, "volatile"))
-	    return STATUS_ALLOCATION_FAILED;
-	  break;
-
-	case 'K':
-	  if (!dyn_string_append_space (qualifiers))
-	    return STATUS_ALLOCATION_FAILED;
-	  if (!dyn_string_append_cstr (qualifiers, "const"))
-	    return STATUS_ALLOCATION_FAILED;
-	  break;
-
-	default:
-	  return STATUS_OK;
-	}
-
-      advance_char (dm);
-    }
-}
-
-/* Demangles and emits a <function-type>.  *FUNCTION_NAME_POS is the
-   position in the result string of the start of the function
-   identifier, at which the function's return type will be inserted;
-   *FUNCTION_NAME_POS is updated to position past the end of the
-   function's return type.
-
-    <function-type> ::= F [Y] <bare-function-type> E  */
-
-static status_t
-demangle_function_type (dm, function_name_pos)
-     demangling_t dm;
-     int *function_name_pos;
-{
-  DEMANGLE_TRACE ("function-type", dm);
-  RETURN_IF_ERROR (demangle_char (dm, 'F'));  
-  if (peek_char (dm) == 'Y')
-    {
-      /* Indicate this function has C linkage if in verbose mode.  */
-      if (flag_verbose)
-	RETURN_IF_ERROR (result_add (dm, " [extern \"C\"] "));
-      advance_char (dm);
-    }
-  RETURN_IF_ERROR (demangle_bare_function_type (dm, function_name_pos));
-  RETURN_IF_ERROR (demangle_char (dm, 'E'));
-  return STATUS_OK;
-}
-
-/* Demangles and emits a <bare-function-type>.  RETURN_TYPE_POS is the
-   position in the result string at which the function return type
-   should be inserted.  If RETURN_TYPE_POS is BFT_NO_RETURN_TYPE, the
-   function's return type is assumed not to be encoded.  
-
-    <bare-function-type> ::= <signature type>+  */
-
-static status_t
-demangle_bare_function_type (dm, return_type_pos)
-     demangling_t dm;
-     int *return_type_pos;
-{
-  /* Sequence is the index of the current function parameter, counting
-     from zero.  The value -1 denotes the return type.  */
-  int sequence = 
-    (return_type_pos == BFT_NO_RETURN_TYPE ? 0 : -1);
-
-  DEMANGLE_TRACE ("bare-function-type", dm);
-
-  RETURN_IF_ERROR (result_add_char (dm, '('));
-  while (!end_of_name_p (dm) && peek_char (dm) != 'E')
-    {
-      if (sequence == -1)
-	/* We're decoding the function's return type.  */
-	{
-	  dyn_string_t return_type;
-	  status_t status = STATUS_OK;
-
-	  /* Decode the return type off to the side.  */
-	  RETURN_IF_ERROR (result_push (dm));
-	  RETURN_IF_ERROR (demangle_type (dm));
-	  return_type = (dyn_string_t) result_pop (dm);
-
-	  /* Add a space to the end of the type.  Insert the return
-             type where we've been asked to. */
-	  if (!dyn_string_append_space (return_type))
-	    status = STATUS_ALLOCATION_FAILED;
-	  if (STATUS_NO_ERROR (status))
-	    {
-	      if (!dyn_string_insert (result_string (dm), *return_type_pos, 
-				      return_type))
-		status = STATUS_ALLOCATION_FAILED;
-	      else
-		*return_type_pos += dyn_string_length (return_type);
-	    }
-
-	  dyn_string_delete (return_type);
-	  RETURN_IF_ERROR (status);
-	}
-      else 
-	{
-	  /* Skip `void' parameter types.  One should only occur as
-	     the only type in a parameter list; in that case, we want
-	     to print `foo ()' instead of `foo (void)'.  */
-	  if (peek_char (dm) == 'v')
-	    /* Consume the v.  */
-	    advance_char (dm);
-	  else
-	    {
-	      /* Separate parameter types by commas.  */
-	      if (sequence > 0)
-		RETURN_IF_ERROR (result_add (dm, ", "));
-	      /* Demangle the type.  */
-	      RETURN_IF_ERROR (demangle_type (dm));
-	    }
-	}
-
-      ++sequence;
-    }
-  RETURN_IF_ERROR (result_add_char (dm, ')'));
-
-  /* We should have demangled at least one parameter type (which would
-     be void, for a function that takes no parameters), plus the
-     return type, if we were supposed to demangle that.  */
-  if (sequence == -1)
-    return "Missing function return type.";
-  else if (sequence == 0)
-    return "Missing function parameter.";
-
-  return STATUS_OK;
-}
-
-/* Demangles and emits a <class-enum-type>.  *ENCODE_RETURN_TYPE is set to
-   non-zero if the type is a template-id, zero otherwise.  
-
-    <class-enum-type> ::= <name>  */
-
-static status_t
-demangle_class_enum_type (dm, encode_return_type)
-     demangling_t dm;
-     int *encode_return_type;
-{
-  DEMANGLE_TRACE ("class-enum-type", dm);
-
-  RETURN_IF_ERROR (demangle_name (dm, encode_return_type));
-  return STATUS_OK;
-}
-
-/* Demangles and emits an <array-type>.  
-
-   If PTR_INSERT_POS is not NULL, the array type is formatted as a
-   pointer or reference to an array, except that asterisk and
-   ampersand punctuation is omitted (since it's not know at this
-   point).  *PTR_INSERT_POS is set to the position in the demangled
-   name at which this punctuation should be inserted.  For example,
-   `A10_i' is demangled to `int () [10]' and *PTR_INSERT_POS points
-   between the parentheses.
-
-   If PTR_INSERT_POS is NULL, the array type is assumed not to be
-   pointer- or reference-qualified.  Then, for example, `A10_i' is
-   demangled simply as `int[10]'.  
-
-    <array-type> ::= A [<dimension number>] _ <element type>  
-                 ::= A <dimension expression> _ <element type>  */
-
-static status_t
-demangle_array_type (dm, ptr_insert_pos)
-     demangling_t dm;
-     int *ptr_insert_pos;
-{
-  status_t status = STATUS_OK;
-  dyn_string_t array_size = NULL;
-  char peek;
-
-  DEMANGLE_TRACE ("array-type", dm);
-
-  RETURN_IF_ERROR (demangle_char (dm, 'A'));
-
-  /* Demangle the array size into array_size.  */
-  peek = peek_char (dm);
-  if (peek == '_')
-    /* Array bound is omitted.  This is a C99-style VLA.  */
-    ;
-  else if (IS_DIGIT (peek_char (dm))) 
-    {
-      /* It looks like a constant array bound.  */
-      array_size = dyn_string_new (10);
-      if (array_size == NULL)
-	return STATUS_ALLOCATION_FAILED;
-      status = demangle_number_literally (dm, array_size, 10, 0);
-    }
-  else
-    {
-      /* Anything is must be an expression for a nont-constant array
-	 bound.  This happens if the array type occurs in a template
-	 and the array bound references a template parameter.  */
-      RETURN_IF_ERROR (result_push (dm));
-      RETURN_IF_ERROR (demangle_expression_v3 (dm));
-      array_size = (dyn_string_t) result_pop (dm);
-    }
-  /* array_size may have been allocated by now, so we can't use
-     RETURN_IF_ERROR until it's been deallocated.  */
-
-  /* Demangle the base type of the array.  */
-  if (STATUS_NO_ERROR (status))
-    status = demangle_char (dm, '_');
-  if (STATUS_NO_ERROR (status))
-    status = demangle_type (dm);
-
-  if (ptr_insert_pos != NULL)
-    {
-      /* This array is actually part of an pointer- or
-	 reference-to-array type.  Format appropriately, except we
-	 don't know which and how much punctuation to use.  */
-      if (STATUS_NO_ERROR (status))
-	status = result_add (dm, " () ");
-      /* Let the caller know where to insert the punctuation.  */
-      *ptr_insert_pos = result_caret_pos (dm) - 2;
-    }
-
-  /* Emit the array dimension syntax.  */
-  if (STATUS_NO_ERROR (status))
-    status = result_add_char (dm, '[');
-  if (STATUS_NO_ERROR (status) && array_size != NULL)
-    status = result_add_string (dm, array_size);
-  if (STATUS_NO_ERROR (status))
-    status = result_add_char (dm, ']');
-  if (array_size != NULL)
-    dyn_string_delete (array_size);
-  
-  RETURN_IF_ERROR (status);
-
-  return STATUS_OK;
-}
-
-/* Demangles and emits a <template-param>.  
-
-    <template-param> ::= T_       # first template parameter
-                     ::= T <parameter-2 number> _  */
-
-static status_t
-demangle_template_param (dm)
-     demangling_t dm;
-{
-  int parm_number;
-  template_arg_list_t current_arg_list = current_template_arg_list (dm);
-  string_list_t arg;
-
-  DEMANGLE_TRACE ("template-param", dm);
-
-  /* Make sure there is a template argmust list in which to look up
-     this parameter reference.  */
-  if (current_arg_list == NULL)
-    return "Template parameter outside of template.";
-
-  RETURN_IF_ERROR (demangle_char (dm, 'T'));
-  if (peek_char (dm) == '_')
-    parm_number = 0;
-  else
-    {
-      RETURN_IF_ERROR (demangle_number (dm, &parm_number, 10, 0));
-      ++parm_number;
-    }
-  RETURN_IF_ERROR (demangle_char (dm, '_'));
-
-  arg = template_arg_list_get_arg (current_arg_list, parm_number);
-  if (arg == NULL)
-    /* parm_number exceeded the number of arguments in the current
-       template argument list.  */
-    return "Template parameter number out of bounds.";
-  RETURN_IF_ERROR (result_add_string (dm, (dyn_string_t) arg));
-
-  return STATUS_OK;
-}
-
-/* Demangles and emits a <template-args>.  
-
-    <template-args> ::= I <template-arg>+ E  */
-
-static status_t
-demangle_template_args_1 (dm, arg_list)
-     demangling_t dm;
-     template_arg_list_t arg_list;
-{
-  int first = 1;
-
-  DEMANGLE_TRACE ("template-args", dm);
-
-  RETURN_IF_ERROR (demangle_char (dm, 'I'));
-  RETURN_IF_ERROR (result_open_template_list (dm));
-  do
-    {
-      string_list_t arg;
-
-      if (first)
-	first = 0;
-      else
-	RETURN_IF_ERROR (result_add (dm, ", "));
-
-      /* Capture the template arg.  */
-      RETURN_IF_ERROR (result_push (dm));
-      RETURN_IF_ERROR (demangle_template_arg (dm));
-      arg = result_pop (dm);
-
-      /* Emit it in the demangled name.  */
-      RETURN_IF_ERROR (result_add_string (dm, (dyn_string_t) arg));
-
-      /* Save it for use in expanding <template-param>s.  */
-      template_arg_list_add_arg (arg_list, arg);
-    }
-  while (peek_char (dm) != 'E');
-  /* Append the '>'.  */
-  RETURN_IF_ERROR (result_close_template_list (dm));
-
-  /* Consume the 'E'.  */
-  advance_char (dm);
-
-  return STATUS_OK;
-}
-
-static status_t
-demangle_template_args (dm)
-     demangling_t dm;
-{
-  //int first = 1;      // unused
-  dyn_string_t old_last_source_name;
-  dyn_string_t new_name;
-  template_arg_list_t arg_list = template_arg_list_new ();
-  status_t status;
-
-  if (arg_list == NULL)
-    return STATUS_ALLOCATION_FAILED;
-
-  /* Preserve the most recently demangled source name.  */
-  old_last_source_name = dm->last_source_name;
-  new_name = dyn_string_new (0);
-
-  if (new_name == NULL)
-    {
-      template_arg_list_delete (arg_list);
-      return STATUS_ALLOCATION_FAILED;
-    }
-
-  dm->last_source_name = new_name;
-  
-  status = demangle_template_args_1 (dm, arg_list);
-  /* Restore the most recent demangled source name.  */
-  dyn_string_delete (dm->last_source_name);
-  dm->last_source_name = old_last_source_name;
-
-  if (!STATUS_NO_ERROR (status))
-    {
-      template_arg_list_delete (arg_list);
-      return status;
-    }
-
-  /* Push the list onto the top of the stack of template argument
-     lists, so that arguments from it are used from now on when
-     expanding <template-param>s.  */
-  push_template_arg_list (dm, arg_list);
-
-  return STATUS_OK;
-}
-
-/* This function, which does not correspond to a production in the
-   mangling spec, handles the `literal' production for both
-   <template-arg> and <expr-primary>.  It does not expect or consume
-   the initial `L' or final `E'.  The demangling is given by:
-
-     <literal> ::= <type> </value/ number>
-
-   and the emitted output is `(type)number'.  */
-
-static status_t
-demangle_literal (dm)
-     demangling_t dm;
-{
-  char peek = peek_char (dm);
-  dyn_string_t value_string;
-  status_t status;
-
-  DEMANGLE_TRACE ("literal", dm);
-
-  if (!flag_verbose && peek >= 'a' && peek <= 'z')
-    {
-      /* If not in verbose mode and this is a builtin type, see if we
-	 can produce simpler numerical output.  In particular, for
-	 integer types shorter than `long', just write the number
-	 without type information; for bools, write `true' or `false'.
-	 Other refinements could be made here too.  */
-
-      /* This constant string is used to map from <builtin-type> codes
-	 (26 letters of the alphabet) to codes that determine how the 
-	 value will be displayed.  The codes are:
-	   b: display as bool
-	   i: display as int
-           l: display as long
-	 A space means the value will be represented using cast
-	 notation. */
-      static const char *const code_map = "ibi    iii ll     ii  i  ";
-
-      char code = code_map[peek - 'a'];
-      /* FIXME: Implement demangling of floats and doubles.  */
-      if (code == 'u')
-	return STATUS_UNIMPLEMENTED;
-      if (code == 'b')
-	{
-	  /* It's a boolean.  */
-	  char value;
-
-	  /* Consume the b.  */
-	  advance_char (dm);
-	  /* Look at the next character.  It should be 0 or 1,
-	     corresponding to false or true, respectively.  */
-	  value = peek_char (dm);
-	  if (value == '0')
-	    RETURN_IF_ERROR (result_add (dm, "false"));
-	  else if (value == '1')
-	    RETURN_IF_ERROR (result_add (dm, "true"));
-	  else
-	    return "Unrecognized bool constant.";
-	  /* Consume the 0 or 1.  */
-	  advance_char (dm);
-	  return STATUS_OK;
-	}
-      else if (code == 'i' || code == 'l')
-	{
-	  /* It's an integer or long.  */
-
-	  /* Consume the type character.  */
-	  advance_char (dm);
-
-	  /* Demangle the number and write it out.  */
-	  value_string = dyn_string_new (0);
-	  status = demangle_number_literally (dm, value_string, 10, 1);
-	  if (STATUS_NO_ERROR (status))
-	    status = result_add_string (dm, value_string);
-	  /* For long integers, append an l.  */
-	  if (code == 'l' && STATUS_NO_ERROR (status))
-	    status = result_add_char (dm, code);
-	  dyn_string_delete (value_string);
-
-	  RETURN_IF_ERROR (status);
-	  return STATUS_OK;
-	}
-      /* ...else code == ' ', so fall through to represent this
-	 literal's type explicitly using cast syntax.  */
-    }
-
-  RETURN_IF_ERROR (result_add_char (dm, '('));
-  RETURN_IF_ERROR (demangle_type (dm));
-  RETURN_IF_ERROR (result_add_char (dm, ')'));
-
-  value_string = dyn_string_new (0);
-  if (value_string == NULL)
-    return STATUS_ALLOCATION_FAILED;
-
-  status = demangle_number_literally (dm, value_string, 10, 1);
-  if (STATUS_NO_ERROR (status))
-    status = result_add_string (dm, value_string);
-  dyn_string_delete (value_string);
-  RETURN_IF_ERROR (status);
-
-  return STATUS_OK;
-}
-
-/* Demangles and emits a <template-arg>.  
-
-    <template-arg> ::= <type>                     # type
-                   ::= L <type> <value number> E  # literal
-                   ::= LZ <encoding> E            # external name
-                   ::= X <expression> E           # expression  */
-
-static status_t
-demangle_template_arg (dm)
-     demangling_t dm;
-{
-  DEMANGLE_TRACE ("template-arg", dm);
-
-  switch (peek_char (dm))
-    {
-    case 'L':
-      advance_char (dm);
-
-      if (peek_char (dm) == 'Z')
-	{
-	  /* External name.  */
-	  advance_char (dm);
-	  /* FIXME: Standard is contradictory here.  */
-	  RETURN_IF_ERROR (demangle_encoding (dm));
-	}
-      else
-	RETURN_IF_ERROR (demangle_literal (dm));
-      RETURN_IF_ERROR (demangle_char (dm, 'E'));
       break;
 
-    case 'X':
-      /* Expression.  */
-      advance_char (dm);
-      RETURN_IF_ERROR (demangle_expression_v3 (dm));
-      RETURN_IF_ERROR (demangle_char (dm, 'E'));
+    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,
+			 cplus_demangle_type (di), NULL);
+      break;
+
+    case 'R':
+      d_advance (di, 1);
+      ret = d_make_comp (di, DEMANGLE_COMPONENT_REFERENCE,
+                         cplus_demangle_type (di), NULL);
+      break;
+
+    case 'C':
+      d_advance (di, 1);
+      ret = d_make_comp (di, DEMANGLE_COMPONENT_COMPLEX,
+			 cplus_demangle_type (di), NULL);
+      break;
+
+    case 'G':
+      d_advance (di, 1);
+      ret = d_make_comp (di, DEMANGLE_COMPONENT_IMAGINARY,
+			 cplus_demangle_type (di), NULL);
+      break;
+
+    case 'U':
+      d_advance (di, 1);
+      ret = d_source_name (di);
+      ret = d_make_comp (di, DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
+			 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;
+	}
       break;
 
     default:
-      RETURN_IF_ERROR (demangle_type (dm));
-      break;
+      return NULL;
     }
 
-  return STATUS_OK;
-}
-
-/* Demangles and emits an <expression>.
-
-    <expression> ::= <unary operator-name> <expression>
-		 ::= <binary operator-name> <expression> <expression>
-		 ::= <expr-primary>  
-                 ::= <scope-expression>  */
-
-static status_t
-demangle_expression_v3 (dm)
-     demangling_t dm;
-{
-  char peek = peek_char (dm);
-
-  DEMANGLE_TRACE ("expression", dm);
-
-  if (peek == 'L' || peek == 'T')
-    RETURN_IF_ERROR (demangle_expr_primary (dm));
-  else if (peek == 's' && peek_char_next (dm) == 'r')
-    RETURN_IF_ERROR (demangle_scope_expression (dm));
-  else
-    /* An operator expression.  */
+  if (can_subst)
     {
-      int num_args;
-      status_t status = STATUS_OK;
-      dyn_string_t operator_name;
-
-      /* We have an operator name.  Since we want to output binary
-	 operations in infix notation, capture the operator name
-	 first.  */
-      RETURN_IF_ERROR (result_push (dm));
-      RETURN_IF_ERROR (demangle_operator_name (dm, 1, &num_args));
-      operator_name = (dyn_string_t) result_pop (dm);
-
-      /* If it's binary, do an operand first.  */
-      if (num_args > 1)
-	{
-	  status = result_add_char (dm, '(');
-	  if (STATUS_NO_ERROR (status))
-	    status = demangle_expression_v3 (dm);
-	  if (STATUS_NO_ERROR (status))
-	    status = result_add_char (dm, ')');
-	}
-
-      /* Emit the operator.  */  
-      if (STATUS_NO_ERROR (status))
-	status = result_add_string (dm, operator_name);
-      dyn_string_delete (operator_name);
-      RETURN_IF_ERROR (status);
-      
-      /* Emit its second (if binary) or only (if unary) operand.  */
-      RETURN_IF_ERROR (result_add_char (dm, '('));
-      RETURN_IF_ERROR (demangle_expression_v3 (dm));
-      RETURN_IF_ERROR (result_add_char (dm, ')'));
-
-      /* The ternary operator takes a third operand.  */
-      if (num_args == 3)
-	{
-	  RETURN_IF_ERROR (result_add (dm, ":("));
-	  RETURN_IF_ERROR (demangle_expression_v3 (dm));
-	  RETURN_IF_ERROR (result_add_char (dm, ')'));
-	}
+      if (! d_add_substitution (di, ret))
+	return NULL;
     }
 
-  return STATUS_OK;
+  return ret;
 }
 
-/* Demangles and emits a <scope-expression>.  
+/* <CV-qualifiers> ::= [r] [V] [K]  */
 
-    <scope-expression> ::= sr <qualifying type> <source-name>
-                       ::= sr <qualifying type> <encoding>  */
-
-static status_t
-demangle_scope_expression (dm)
-     demangling_t dm;
+static struct demangle_component **
+d_cv_qualifiers (struct d_info *di,
+                 struct demangle_component **pret, int member_fn)
 {
-  RETURN_IF_ERROR (demangle_char (dm, 's'));
-  RETURN_IF_ERROR (demangle_char (dm, 'r'));
-  RETURN_IF_ERROR (demangle_type (dm));
-  RETURN_IF_ERROR (result_add (dm, "::"));
-  RETURN_IF_ERROR (demangle_encoding (dm));
-  return STATUS_OK;
-}
+  char peek;
 
-/* Demangles and emits an <expr-primary>.  
-
-    <expr-primary> ::= <template-param>
-		   ::= L <type> <value number> E  # literal
-		   ::= L <mangled-name> E         # external name  */
-
-static status_t
-demangle_expr_primary (dm)
-     demangling_t dm;
-{
-  char peek = peek_char (dm);
-
-  DEMANGLE_TRACE ("expr-primary", dm);
-
-  if (peek == 'T')
-    RETURN_IF_ERROR (demangle_template_param (dm));
-  else if (peek == 'L')
+  peek = d_peek_char (di);
+  while (peek == 'r' || peek == 'V' || peek == 'K')
     {
-      /* Consume the `L'.  */
-      advance_char (dm);
-      peek = peek_char (dm);
+      enum demangle_component_type t;
 
-      if (peek == '_')
-	RETURN_IF_ERROR (demangle_mangled_name (dm));
+      d_advance (di, 1);
+      if (peek == 'r')
+	{
+	  t = (member_fn
+	       ? DEMANGLE_COMPONENT_RESTRICT_THIS
+	       : DEMANGLE_COMPONENT_RESTRICT);
+	  di->expansion += sizeof "restrict";
+	}
+      else if (peek == 'V')
+	{
+	  t = (member_fn
+	       ? DEMANGLE_COMPONENT_VOLATILE_THIS
+	       : DEMANGLE_COMPONENT_VOLATILE);
+	  di->expansion += sizeof "volatile";
+	}
       else
-	RETURN_IF_ERROR (demangle_literal (dm));
+	{
+	  t = (member_fn
+	       ? DEMANGLE_COMPONENT_CONST_THIS
+	       : DEMANGLE_COMPONENT_CONST);
+	  di->expansion += sizeof "const";
+	}
 
-      RETURN_IF_ERROR (demangle_char (dm, 'E'));
+      *pret = d_make_comp (di, t, NULL, NULL);
+      if (*pret == NULL)
+	return NULL;
+      pret = &d_left (*pret);
+
+      peek = d_peek_char (di);
     }
-  else
-    return STATUS_ERROR;
 
-  return STATUS_OK;
+  return pret;
 }
 
-/* Demangles and emits a <substitution>.  Sets *TEMPLATE_P to non-zero
-   if the substitution is the name of a template, zero otherwise. 
+/* <function-type> ::= F [Y] <bare-function-type> E  */
 
-     <substitution> ::= S <seq-id> _
-                    ::= S_
+static struct demangle_component *
+d_function_type (struct d_info *di)
+{
+  struct demangle_component *ret;
 
-                    ::= St   # ::std::
-                    ::= Sa   # ::std::allocator
-                    ::= Sb   # ::std::basic_string
-                    ::= Ss   # ::std::basic_string<char,
-				    		   ::std::char_traits<char>,
-						   ::std::allocator<char> >
-                    ::= Si   # ::std::basic_istream<char,  
-                                                    std::char_traits<char> >
-                    ::= So   # ::std::basic_ostream<char,  
-                                                    std::char_traits<char> >
-                    ::= Sd   # ::std::basic_iostream<char, 
-                                                    std::char_traits<char> >
+  if (! d_check_char (di, 'F'))
+    return NULL;
+  if (d_peek_char (di) == 'Y')
+    {
+      /* Function has C linkage.  We don't print this information.
+	 FIXME: We should print it in verbose mode.  */
+      d_advance (di, 1);
+    }
+  ret = d_bare_function_type (di, 1);
+  if (! d_check_char (di, 'E'))
+    return NULL;
+  return ret;
+}
+
+/* <bare-function-type> ::= [J]<type>+  */
+
+static struct demangle_component *
+d_bare_function_type (struct d_info *di, int has_return_tipe)
+{
+  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_tipe = 1;
+    }
+
+  return_type = NULL;
+  tl = NULL;
+  ptl = &tl;
+  while (1)
+    {
+      struct demangle_component *type;
+
+      peek = d_peek_char (di);
+      if (peek == '\0' || peek == 'E')
+	break;
+      type = cplus_demangle_type (di);
+      if (type == NULL)
+	return NULL;
+      if (has_return_tipe)
+	{
+	  return_type = type;
+	  has_return_tipe = 0;
+	}
+      else
+	{
+	  *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
+     return type.  A function which takes no arguments will have a
+     single parameter type void.  */
+  if (tl == NULL)
+    return NULL;
+
+  /* If we have a single parameter type void, omit it.  */
+  if (d_right (tl) == NULL
+      && d_left (tl)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
+      && d_left (tl)->u.s_builtin.type->print == D_PRINT_VOID)
+    {
+      di->expansion -= d_left (tl)->u.s_builtin.type->len;
+      tl = NULL;
+    }
+
+  return d_make_comp (di, DEMANGLE_COMPONENT_FUNCTION_TYPE, return_type, tl);
+}
+
+/* <class-enum-type> ::= <name>  */
+
+static struct demangle_component *
+d_class_enum_type (struct d_info *di)
+{
+  return d_name (di);
+}
+
+/* <array-type> ::= A <(positive dimension) number> _ <(element) type>
+                ::= A [<(dimension) expression>] _ <(element) type>
 */
 
-static status_t
-demangle_substitution (dm, template_p)
-     demangling_t dm;
-     int *template_p;
+static struct demangle_component *
+d_array_type (struct d_info *di)
 {
-  int seq_id;
-  int peek;
-  dyn_string_t text;
+  char peek;
+  struct demangle_component *dim;
 
-  DEMANGLE_TRACE ("substitution", dm);
+  if (! d_check_char (di, 'A'))
+    return NULL;
 
-  RETURN_IF_ERROR (demangle_char (dm, 'S'));
-
-  /* Scan the substitution sequence index.  A missing number denotes
-     the first index.  */
-  peek = peek_char (dm);
+  peek = d_peek_char (di);
   if (peek == '_')
-    seq_id = -1;
-  /* If the following character is 0-9 or a capital letter, interpret
-     the sequence up to the next underscore as a base-36 substitution
-     index.  */
-  else if (IS_DIGIT ((unsigned char) peek) 
-	   || (peek >= 'A' && peek <= 'Z'))
-    RETURN_IF_ERROR (demangle_number (dm, &seq_id, 36, 0));
-  else 
+    dim = NULL;
+  else if (IS_DIGIT (peek))
     {
-      const char *new_last_source_name = NULL;
+      const char *s;
 
-      switch (peek)
+      s = d_str (di);
+      do
 	{
-	case 't':
-	  RETURN_IF_ERROR (result_add (dm, "std"));
-	  break;
-
-	case 'a':
-	  RETURN_IF_ERROR (result_add (dm, "std::allocator"));
-	  new_last_source_name = "allocator";
-	  *template_p = 1;
-	  break;
-
-	case 'b':
-	  RETURN_IF_ERROR (result_add (dm, "std::basic_string"));
-	  new_last_source_name = "basic_string";
-	  *template_p = 1;
-	  break;
-	  
-	case 's':
-	  if (!flag_verbose)
-	    {
-	      RETURN_IF_ERROR (result_add (dm, "std::string"));
-	      new_last_source_name = "string";
-	    }
-	  else
-	    {
-	      RETURN_IF_ERROR (result_add (dm, "std::basic_string<char, std::char_traits<char>, std::allocator<char> >"));
-	      new_last_source_name = "basic_string";
-	    }
-	  *template_p = 0;
-	  break;
-
-	case 'i':
-	  if (!flag_verbose)
-	    {
-	      RETURN_IF_ERROR (result_add (dm, "std::istream"));
-	      new_last_source_name = "istream";
-	    }
-	  else
-	    {
-	      RETURN_IF_ERROR (result_add (dm, "std::basic_istream<char, std::char_traints<char> >"));
-	      new_last_source_name = "basic_istream";
-	    }
-	  *template_p = 0;
-	  break;
-
-	case 'o':
-	  if (!flag_verbose)
-	    {
-	      RETURN_IF_ERROR (result_add (dm, "std::ostream"));
-	      new_last_source_name = "ostream";
-	    }
-	  else
-	    {
-	      RETURN_IF_ERROR (result_add (dm, "std::basic_ostream<char, std::char_traits<char> >"));
-	      new_last_source_name = "basic_ostream";
-	    }
-	  *template_p = 0;
-	  break;
-
-	case 'd':
-	  if (!flag_verbose) 
-	    {
-	      RETURN_IF_ERROR (result_add (dm, "std::iostream"));
-	      new_last_source_name = "iostream";
-	    }
-	  else
-	    {
-	      RETURN_IF_ERROR (result_add (dm, "std::basic_iostream<char, std::char_traits<char> >"));
-	      new_last_source_name = "basic_iostream";
-	    }
-	  *template_p = 0;
-	  break;
-
-	default:
-	  return "Unrecognized <substitution>.";
+	  d_advance (di, 1);
+	  peek = d_peek_char (di);
 	}
-      
-      /* Consume the character we just processed.  */
-      advance_char (dm);
-
-      if (new_last_source_name != NULL)
-	{
-	  if (!dyn_string_copy_cstr (dm->last_source_name, 
-				     new_last_source_name))
-	    return STATUS_ALLOCATION_FAILED;
-	}
-
-      return STATUS_OK;
-    }
-
-  /* Look up the substitution text.  Since `S_' is the most recent
-     substitution, `S0_' is the second-most-recent, etc., shift the
-     numbering by one.  */
-  text = substitution_get (dm, seq_id + 1, template_p);
-  if (text == NULL) 
-    return "Substitution number out of range.";
-
-  /* Emit the substitution text.  */
-  RETURN_IF_ERROR (result_add_string (dm, text));
-
-  RETURN_IF_ERROR (demangle_char (dm, '_'));
-  return STATUS_OK;
-}
-
-/* Demangles and emits a <local-name>.  
-
-    <local-name> := Z <function encoding> E <entity name> [<discriminator>]
-                 := Z <function encoding> E s [<discriminator>]  */
-
-static status_t
-demangle_local_name (dm)
-     demangling_t dm;
-{
-  DEMANGLE_TRACE ("local-name", dm);
-
-  RETURN_IF_ERROR (demangle_char (dm, 'Z'));
-  RETURN_IF_ERROR (demangle_encoding (dm));
-  RETURN_IF_ERROR (demangle_char (dm, 'E'));
-  RETURN_IF_ERROR (result_add (dm, "::"));
-
-  if (peek_char (dm) == 's')
-    {
-      /* Local character string literal.  */
-      RETURN_IF_ERROR (result_add (dm, "string literal"));
-      /* Consume the s.  */
-      advance_char (dm);
-      RETURN_IF_ERROR (demangle_discriminator (dm, 0));
+      while (IS_DIGIT (peek));
+      dim = d_make_name (di, s, d_str (di) - s);
+      if (dim == NULL)
+	return NULL;
     }
   else
     {
-      int unused;
-      /* Local name for some other entity.  Demangle its name.  */
-      RETURN_IF_ERROR (demangle_name (dm, &unused));
-      RETURN_IF_ERROR (demangle_discriminator (dm, 1));
-     }
+      dim = d_expression (di);
+      if (dim == NULL)
+	return NULL;
+    }
 
-   return STATUS_OK;
- }
+  if (! d_check_char (di, '_'))
+    return NULL;
 
- /* Optimonally demangles and emits a <discriminator>.  If there is no
-    <discriminator> at the current position in the mangled string, the
-    descriminator is assumed to be zero.  Emit the discriminator number
-    in parentheses, unless SUPPRESS_FIRST is non-zero and the
-    discriminator is zero.  
+  return d_make_comp (di, DEMANGLE_COMPONENT_ARRAY_TYPE, dim,
+		      cplus_demangle_type (di));
+}
 
-     <discriminator> ::= _ <number>  */
+/* <pointer-to-member-type> ::= M <(class) type> <(member) type>  */
 
-static status_t
-demangle_discriminator (dm, suppress_first)
-     demangling_t dm;
-     int suppress_first;
+static struct demangle_component *
+d_pointer_to_member_type (struct d_info *di)
 {
-  /* Output for <discriminator>s to the demangled name is completely
-     suppressed if not in verbose mode.  */
+  struct demangle_component *cl;
+  struct demangle_component *mem;
+  struct demangle_component **pmem;
 
-  if (peek_char (dm) == '_')
+  if (! d_check_char (di, 'M'))
+    return NULL;
+
+  cl = cplus_demangle_type (di);
+
+  /* The ABI specifies that any type can be a substitution source, and
+     that M is followed by two types, and that when a CV-qualified
+     type is seen both the base type and the CV-qualified types are
+     substitution sources.  The ABI also specifies that for a pointer
+     to a CV-qualified member function, the qualifiers are attached to
+     the second type.  Given the grammar, a plain reading of the ABI
+     suggests that both the CV-qualified member function and the
+     non-qualified member function are substitution sources.  However,
+     g++ does not work that way.  g++ treats only the CV-qualified
+     member function as a substitution source.  FIXME.  So to work
+     with g++, we need to pull off the CV-qualifiers here, in order to
+     avoid calling add_substitution() in cplus_demangle_type().  But
+     for a CV-qualified member which is not a function, g++ does
+     follow the ABI, so we need to handle that case here by calling
+     d_add_substitution ourselves.  */
+
+  pmem = d_cv_qualifiers (di, &mem, 1);
+  if (pmem == NULL)
+    return NULL;
+  *pmem = cplus_demangle_type (di);
+  if (*pmem == NULL)
+    return NULL;
+
+  if (pmem != &mem && (*pmem)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
     {
-      /* Consume the underscore.  */
-      advance_char (dm);
-      if (flag_verbose)
-	RETURN_IF_ERROR (result_add (dm, " [#"));
-      /* Check if there's a number following the underscore.  */
-      if (IS_DIGIT ((unsigned char) peek_char (dm)))
+      if (! d_add_substitution (di, mem))
+	return NULL;
+    }
+
+  return d_make_comp (di, DEMANGLE_COMPONENT_PTRMEM_TYPE, cl, mem);
+}
+
+/* <template-param> ::= T_
+                    ::= T <(parameter-2 non-negative) number> _
+*/
+
+static struct demangle_component *
+d_template_param (struct d_info *di)
+{
+  long param;
+
+  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, '_'))
+    return NULL;
+
+  ++di->did_subs;
+
+  return d_make_template_param (di, param);
+}
+
+/* <template-args> ::= I <template-arg>+ E  */
+
+static struct demangle_component *
+d_template_args (struct d_info *di)
+{
+  struct demangle_component *hold_last_name;
+  struct demangle_component *al;
+  struct demangle_component **pal;
+
+  /* Preserve the last name we saw--don't let the template arguments
+     clobber it, as that would give us the wrong name for a subsequent
+     constructor or destructor.  */
+  hold_last_name = di->last_name;
+
+  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)
+    {
+      struct demangle_component *a;
+
+      a = d_template_arg (di);
+      if (a == NULL)
+	return NULL;
+
+      *pal = d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE_ARGLIST, a, NULL);
+      if (*pal == NULL)
+	return NULL;
+      pal = &d_right (*pal);
+
+      if (d_peek_char (di) == 'E')
 	{
-	  int discriminator;
-	  /* Demangle the number.  */
-	  RETURN_IF_ERROR (demangle_number (dm, &discriminator, 10, 0));
-	  if (flag_verbose)
-	    /* Write the discriminator.  The mangled number is two
-	       less than the discriminator ordinal, counting from
-	       zero.  */
-	    RETURN_IF_ERROR (int_to_dyn_string (discriminator + 1,
-						(dyn_string_t) dm->result));
+	  d_advance (di, 1);
+	  break;
 	}
+    }
+
+  di->last_name = hold_last_name;
+
+  return al;
+}
+
+/* <template-arg> ::= <type>
+                  ::= X <expression> E
+                  ::= <expr-primary>
+*/
+
+static struct demangle_component *
+d_template_arg (struct d_info *di)
+{
+  struct demangle_component *ret;
+
+  switch (d_peek_char (di))
+    {
+    case 'X':
+      d_advance (di, 1);
+      ret = d_expression (di);
+      if (! d_check_char (di, 'E'))
+	return NULL;
+      return ret;
+
+    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>
+                ::= sr <type> <unqualified-name> <template-args>
+                ::= <expr-primary>
+*/
+
+static struct demangle_component *
+d_expression (struct d_info *di)
+{
+  char peek;
+
+  peek = d_peek_char (di);
+  if (peek == 'L')
+    return d_expr_primary (di);
+  else if (peek == 'T')
+    return d_template_param (di);
+  else if (peek == 's' && d_peek_next_char (di) == 'r')
+    {
+      struct demangle_component *type;
+      struct demangle_component *name;
+
+      d_advance (di, 2);
+      type = cplus_demangle_type (di);
+      name = d_unqualified_name (di);
+      if (d_peek_char (di) != 'I')
+	return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type, name);
       else
-	return STATUS_ERROR;
-      if (flag_verbose)
-	RETURN_IF_ERROR (result_add_char (dm, ']'));
+	return d_make_comp (di, DEMANGLE_COMPONENT_QUAL_NAME, type,
+			    d_make_comp (di, DEMANGLE_COMPONENT_TEMPLATE, name,
+					 d_template_args (di)));
     }
-  else if (!suppress_first)
+  else if (peek == 's' && d_peek_next_char (di) == 'T')
     {
-      if (flag_verbose)
-	RETURN_IF_ERROR (result_add (dm, " [#0]"));
+      /* Just demangle a parameter placeholder as its type.  */
+      d_advance (di, 2);
+      return cplus_demangle_type (di);
     }
-
-  return STATUS_OK;
-}
-
-/* Demangle NAME into RESULT, which must be an initialized
-   dyn_string_t.  On success, returns STATUS_OK.  On failure, returns
-   an error message, and the contents of RESULT are unchanged.  */
-
-static status_t
-cp_demangle (name, result, style)
-     const char *name;
-     dyn_string_t result;
-     int style;
-{
-  status_t status;
-  int length = VG_(strlen) (name);
-
-  if (length > 2 && name[0] == '_' && name[1] == 'Z')
+  else if (IS_DIGIT (peek))
     {
-      demangling_t dm = demangling_new (name, style);
-      if (dm == NULL)
-	return STATUS_ALLOCATION_FAILED;
-
-      status = result_push (dm);
-      if (status != STATUS_OK)
-	{
-	  demangling_delete (dm);
-	  return status;
-	}
-
-      status = demangle_mangled_name (dm);
-      if (STATUS_NO_ERROR (status))
-	{
-	  dyn_string_t demangled = (dyn_string_t) result_pop (dm);
-	  if (!dyn_string_copy (result, demangled))
-	    {
-	      demangling_delete (dm);
-	      return STATUS_ALLOCATION_FAILED;
-	    }
-	  dyn_string_delete (demangled);
-	}
-      
-      demangling_delete (dm);
+      /* We can get an unqualified name as an expression in the case of
+         a dependent member access, i.e. decltype(T().i).  */
+      struct demangle_component *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
     {
-      /* It's evidently not a mangled C++ name.  It could be the name
-	 of something with C linkage, though, so just copy NAME into
-	 RESULT.  */
-      if (!dyn_string_copy_cstr (result, name))
-	return STATUS_ALLOCATION_FAILED;
-      status = STATUS_OK;
-    }
+      struct demangle_component *op;
+      int args;
 
-  return status; 
+      op = d_operator_name (di);
+      if (op == NULL)
+	return NULL;
+
+      if (op->type == DEMANGLE_COMPONENT_OPERATOR)
+	di->expansion += op->u.s_operator.op->len - 2;
+
+      if (op->type == DEMANGLE_COMPONENT_OPERATOR
+	  && strcmp (op->u.s_operator.op->code, "st") == 0)
+	return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
+			    cplus_demangle_type (di));
+
+      switch (op->type)
+	{
+	default:
+	  return NULL;
+	case DEMANGLE_COMPONENT_OPERATOR:
+	  args = op->u.s_operator.op->args;
+	  break;
+	case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
+	  args = op->u.s_extended_operator.args;
+	  break;
+	case DEMANGLE_COMPONENT_CAST:
+	  if (d_peek_char (di) == 'v')
+	    /* T() encoded as an operand of void.  */
+	    return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
+				cplus_demangle_type (di));
+	  else
+	    args = 1;
+	  break;
+	}
+
+      switch (args)
+	{
+	case 1:
+	  return d_make_comp (di, DEMANGLE_COMPONENT_UNARY, op,
+			      d_expression (di));
+	case 2:
+	  {
+	    struct demangle_component *left;
+	    struct demangle_component *right;
+
+	    left = d_expression (di);
+	    if (!strcmp (op->u.s_operator.op->code, "cl"))
+	      right = d_exprlist (di);
+	    else
+	      right = d_expression (di);
+
+	    return d_make_comp (di, DEMANGLE_COMPONENT_BINARY, op,
+				d_make_comp (di,
+					     DEMANGLE_COMPONENT_BINARY_ARGS,
+					     left, right));
+	  }
+	case 3:
+	  {
+	    struct demangle_component *first;
+	    struct demangle_component *second;
+
+	    first = d_expression (di);
+	    second = d_expression (di);
+	    return d_make_comp (di, DEMANGLE_COMPONENT_TRINARY, op,
+				d_make_comp (di,
+					     DEMANGLE_COMPONENT_TRINARY_ARG1,
+					     first,
+					     d_make_comp (di,
+							  DEMANGLE_COMPONENT_TRINARY_ARG2,
+							  second,
+							  d_expression (di))));
+	  }
+	default:
+	  return NULL;
+	}
+    }
 }
 
-/* Demangle TYPE_NAME into RESULT, which must be an initialized
-   dyn_string_t.  On success, returns STATUS_OK.  On failiure, returns
-   an error message, and the contents of RESULT are unchanged.  */
+/* <expr-primary> ::= L <type> <(value) number> E
+                  ::= L <type> <(value) float> E
+                  ::= L <mangled-name> E
+*/
 
-#ifdef IN_LIBGCC2
-static status_t
-cp_demangle_type (type_name, result)
-     const char* type_name;
-     dyn_string_t result;
+static struct demangle_component *
+d_expr_primary (struct d_info *di)
 {
-  status_t status;
-  demangling_t dm = demangling_new (type_name);
-  
-  if (dm == NULL)
-    return STATUS_ALLOCATION_FAILED;
+  struct demangle_component *ret;
 
-  /* Demangle the type name.  The demangled name is stored in dm.  */
-  status = result_push (dm);
-  if (status != STATUS_OK)
+  if (! d_check_char (di, 'L'))
+    return NULL;
+  if (d_peek_char (di) == '_')
+    ret = cplus_demangle_mangled_name (di, 0);
+  else
     {
-      demangling_delete (dm);
-      return status;
+      struct demangle_component *type;
+      enum demangle_component_type t;
+      const char *s;
+
+      type = cplus_demangle_type (di);
+      if (type == NULL)
+	return NULL;
+
+      /* If we have a type we know how to print, we aren't going to
+	 print the type name itself.  */
+      if (type->type == DEMANGLE_COMPONENT_BUILTIN_TYPE
+	  && type->u.s_builtin.type->print != D_PRINT_DEFAULT)
+	di->expansion -= type->u.s_builtin.type->len;
+
+      /* Rather than try to interpret the literal value, we just
+	 collect it as a string.  Note that it's possible to have a
+	 floating point literal here.  The ABI specifies that the
+	 format of such literals is machine independent.  That's fine,
+	 but what's not fine is that versions of g++ up to 3.2 with
+	 -fabi-version=1 used upper case letters in the hex constant,
+	 and dumped out gcc's internal representation.  That makes it
+	 hard to tell where the constant ends, and hard to dump the
+	 constant in any readable form anyhow.  We don't attempt to
+	 handle these cases.  */
+
+      t = DEMANGLE_COMPONENT_LITERAL;
+      if (d_peek_char (di) == 'n')
+	{
+	  t = DEMANGLE_COMPONENT_LITERAL_NEG;
+	  d_advance (di, 1);
+	}
+      s = d_str (di);
+      while (d_peek_char (di) != 'E')
+	{
+	  if (d_peek_char (di) == '\0')
+	    return NULL;
+	  d_advance (di, 1);
+	}
+      ret = d_make_comp (di, t, type, d_make_name (di, s, d_str (di) - s));
+    }
+  if (! d_check_char (di, 'E'))
+    return NULL;
+  return ret;
+}
+
+/* <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>]
+                ::= Z <(function) encoding> E s [<discriminator>]
+*/
+
+static struct demangle_component *
+d_local_name (struct d_info *di)
+{
+  struct demangle_component *function;
+
+  if (! d_check_char (di, 'Z'))
+    return NULL;
+
+  function = d_encoding (di, 0);
+
+  if (! d_check_char (di, 'E'))
+    return NULL;
+
+  if (d_peek_char (di) == 's')
+    {
+      d_advance (di, 1);
+      if (! d_discriminator (di))
+	return NULL;
+      return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function,
+			  d_make_name (di, "string literal",
+				       sizeof "string literal" - 1));
+    }
+  else
+    {
+      struct demangle_component *name;
+
+      name = d_name (di);
+      if (! d_discriminator (di))
+	return NULL;
+      return d_make_comp (di, DEMANGLE_COMPONENT_LOCAL_NAME, function, name);
+    }
+}
+
+/* <discriminator> ::= _ <(non-negative) number>
+
+   We demangle the discriminator, but we don't print it out.  FIXME:
+   We should print it out in verbose mode.  */
+
+static int
+d_discriminator (struct d_info *di)
+{
+  long discrim;
+
+  if (d_peek_char (di) != '_')
+    return 1;
+  d_advance (di, 1);
+  discrim = d_number (di);
+  if (discrim < 0)
+    return 0;
+  return 1;
+}
+
+/* Add a new substitution.  */
+
+static int
+d_add_substitution (struct d_info *di, struct demangle_component *dc)
+{
+  if (dc == NULL)
+    return 0;
+  if (di->next_sub >= di->num_subs)
+    return 0;
+  di->subs[di->next_sub] = dc;
+  ++di->next_sub;
+  return 1;
+}
+
+/* <substitution> ::= S <seq-id> _
+                  ::= S_
+                  ::= St
+                  ::= Sa
+                  ::= Sb
+                  ::= Ss
+                  ::= Si
+                  ::= So
+                  ::= Sd
+
+   If PREFIX is non-zero, then this type is being used as a prefix in
+   a qualified name.  In this case, for the standard substitutions, we
+   need to check whether we are being used as a prefix for a
+   constructor or destructor, and return a full template name.
+   Otherwise we will get something like std::iostream::~iostream()
+   which does not correspond particularly well to any function which
+   actually appears in the source.
+*/
+
+static const struct d_standard_sub_info standard_subs[] =
+{
+  { 't', NL ("std"),
+    NL ("std"),
+    NULL, 0 },
+  { 'a', NL ("std::allocator"),
+    NL ("std::allocator"),
+    NL ("allocator") },
+  { 'b', NL ("std::basic_string"),
+    NL ("std::basic_string"),
+    NL ("basic_string") },
+  { 's', NL ("std::string"),
+    NL ("std::basic_string<char, std::char_traits<char>, std::allocator<char> >"),
+    NL ("basic_string") },
+  { 'i', NL ("std::istream"),
+    NL ("std::basic_istream<char, std::char_traits<char> >"),
+    NL ("basic_istream") },
+  { 'o', NL ("std::ostream"),
+    NL ("std::basic_ostream<char, std::char_traits<char> >"),
+    NL ("basic_ostream") },
+  { 'd', NL ("std::iostream"),
+    NL ("std::basic_iostream<char, std::char_traits<char> >"),
+    NL ("basic_iostream") }
+};
+
+static struct demangle_component *
+d_substitution (struct d_info *di, int prefix)
+{
+  char c;
+
+  if (! d_check_char (di, 'S'))
+    return NULL;
+
+  c = d_next_char (di);
+  if (c == '_' || IS_DIGIT (c) || IS_UPPER (c))
+    {
+      unsigned int id;
+
+      id = 0;
+      if (c != '_')
+	{
+	  do
+	    {
+	      unsigned int new_id;
+
+	      if (IS_DIGIT (c))
+		new_id = id * 36 + c - '0';
+	      else if (IS_UPPER (c))
+		new_id = id * 36 + c - 'A' + 10;
+	      else
+		return NULL;
+	      if (new_id < id)
+		return NULL;
+	      id = new_id;
+	      c = d_next_char (di);
+	    }
+	  while (c != '_');
+
+	  ++id;
+	}
+
+      if (id >= (unsigned int) di->next_sub)
+	return NULL;
+
+      ++di->did_subs;
+
+      return di->subs[id];
+    }
+  else
+    {
+      int verbose;
+      const struct d_standard_sub_info *p;
+      const struct d_standard_sub_info *pend;
+
+      verbose = (di->options & DMGL_VERBOSE) != 0;
+      if (! verbose && prefix)
+	{
+	  char peek;
+
+	  peek = d_peek_char (di);
+	  if (peek == 'C' || peek == 'D')
+	    verbose = 1;
+	}
+
+      pend = (&standard_subs[0]
+	      + sizeof standard_subs / sizeof standard_subs[0]);
+      for (p = &standard_subs[0]; p < pend; ++p)
+	{
+	  if (c == p->code)
+	    {
+	      const char *s;
+	      int len;
+
+	      if (p->set_last_name != NULL)
+		di->last_name = d_make_sub (di, p->set_last_name,
+					    p->set_last_name_len);
+	      if (verbose)
+		{
+		  s = p->full_expansion;
+		  len = p->full_len;
+		}
+	      else
+		{
+		  s = p->simple_expansion;
+		  len = p->simple_len;
+		}
+	      di->expansion += len;
+	      return d_make_sub (di, s, len);
+	    }
+	}
+
+      return NULL;
+    }
+}
+
+/* Initialize a growable string.  */
+
+static void
+d_growable_string_init (struct d_growable_string *dgs, size_t estimate)
+{
+  dgs->buf = NULL;
+  dgs->len = 0;
+  dgs->alc = 0;
+  dgs->allocation_failure = 0;
+
+  if (estimate > 0)
+    d_growable_string_resize (dgs, estimate);
+}
+
+/* Grow a growable string to a given size.  */
+
+static inline void
+d_growable_string_resize (struct d_growable_string *dgs, size_t need)
+{
+  size_t newalc;
+  char *newbuf;
+
+  if (dgs->allocation_failure)
+    return;
+
+  /* Start allocation at two bytes to avoid any possibility of confusion
+     with the special value of 1 used as a return in *palc to indicate
+     allocation failures.  */
+  newalc = dgs->alc > 0 ? dgs->alc : 2;
+  while (newalc < need)
+    newalc <<= 1;
+
+  newbuf = (char *) realloc ("demangle.dgsr.1", dgs->buf, newalc);
+  if (newbuf == NULL)
+    {
+      free (dgs->buf);
+      dgs->buf = NULL;
+      dgs->len = 0;
+      dgs->alc = 0;
+      dgs->allocation_failure = 1;
+      return;
+    }
+  dgs->buf = newbuf;
+  dgs->alc = newalc;
+}
+
+/* Append a buffer to a growable string.  */
+
+static inline void
+d_growable_string_append_buffer (struct d_growable_string *dgs,
+                                 const char *s, size_t l)
+{
+  size_t need;
+
+  need = dgs->len + l + 1;
+  if (need > dgs->alc)
+    d_growable_string_resize (dgs, need);
+
+  if (dgs->allocation_failure)
+    return;
+
+  memcpy (dgs->buf + dgs->len, s, l);
+  dgs->buf[dgs->len + l] = '\0';
+  dgs->len += l;
+}
+
+/* Bridge growable strings to the callback mechanism.  */
+
+static void
+d_growable_string_callback_adapter (const char *s, size_t l, void *opaque)
+{
+  struct d_growable_string *dgs = (struct d_growable_string*) opaque;
+
+  d_growable_string_append_buffer (dgs, s, l);
+}
+
+/* Initialize a print information structure.  */
+
+static void
+d_print_init (struct d_print_info *dpi, int options,
+              demangle_callbackref callback, void *opaque)
+{
+  dpi->options = options;
+  dpi->len = 0;
+  dpi->last_char = '\0';
+  dpi->templates = NULL;
+  dpi->modifiers = NULL;
+
+  dpi->callback = callback;
+  dpi->opaque = opaque;
+
+  dpi->demangle_failure = 0;
+}
+
+/* Indicate that an error occurred during printing, and test for error.  */
+
+static inline void
+d_print_error (struct d_print_info *dpi)
+{
+  dpi->demangle_failure = 1;
+}
+
+static inline int
+d_print_saw_error (struct d_print_info *dpi)
+{
+  return dpi->demangle_failure != 0;
+}
+
+/* Flush buffered characters to the callback.  */
+
+static inline void
+d_print_flush (struct d_print_info *dpi)
+{
+  dpi->buf[dpi->len] = '\0';
+  dpi->callback (dpi->buf, dpi->len, dpi->opaque);
+  dpi->len = 0;
+}
+
+/* Append characters and buffers for printing.  */
+
+static inline void
+d_append_char (struct d_print_info *dpi, char c)
+{
+  if (dpi->len == sizeof (dpi->buf) - 1)
+    d_print_flush (dpi);
+
+  dpi->buf[dpi->len++] = c;
+  dpi->last_char = c;
+}
+
+static inline void
+d_append_buffer (struct d_print_info *dpi, const char *s, size_t l)
+{
+  size_t i;
+
+  for (i = 0; i < l; i++)
+    d_append_char (dpi, s[i]);
+}
+
+static inline void
+d_append_string (struct d_print_info *dpi, const char *s)
+{
+  d_append_buffer (dpi, s, strlen (s));
+}
+
+static inline char
+d_last_char (struct d_print_info *dpi)
+{
+  return dpi->last_char;
+}
+
+/* Turn components into a human readable string.  OPTIONS is the
+   options bits passed to the demangler.  DC is the tree to print.
+   CALLBACK is a function to call to flush demangled string segments
+   as they fill the intermediate buffer, and OPAQUE is a generalized
+   callback argument.  On success, this returns 1.  On failure,
+   it returns 0, indicating a bad parse.  It does not use heap
+   memory to build an output string, so cannot encounter memory
+   allocation failure.  */
+
+CP_STATIC_IF_GLIBCPP_V3
+int
+cplus_demangle_print_callback (int options,
+                               const struct demangle_component *dc,
+                               demangle_callbackref callback, void *opaque)
+{
+  struct d_print_info dpi;
+
+  d_print_init (&dpi, options, callback, opaque);
+
+  d_print_comp (&dpi, dc);
+
+  d_print_flush (&dpi);
+
+  return ! d_print_saw_error (&dpi);
+}
+
+/* Turn components into a human readable string.  OPTIONS is the
+   options bits passed to the demangler.  DC is the tree to print.
+   ESTIMATE is a guess at the length of the result.  This returns a
+   string allocated by malloc, or NULL on error.  On success, this
+   sets *PALC to the size of the allocated buffer.  On failure, this
+   sets *PALC to 0 for a bad parse, or to 1 for a memory allocation
+   failure.  */
+
+CP_STATIC_IF_GLIBCPP_V3
+char *
+cplus_demangle_print (int options, const struct demangle_component *dc,
+                      int estimate, size_t *palc)
+{
+  struct d_growable_string dgs;
+
+  d_growable_string_init (&dgs, estimate);
+
+  if (! cplus_demangle_print_callback (options, dc,
+                                       d_growable_string_callback_adapter,
+                                       &dgs))
+    {
+      free (dgs.buf);
+      *palc = 0;
+      return NULL;
     }
 
-  status = demangle_type (dm);
+  *palc = dgs.allocation_failure ? 1 : dgs.alc;
+  return dgs.buf;
+}
 
-  if (STATUS_NO_ERROR (status))
+/* 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))
     {
-      /* The demangling succeeded.  Pop the result out of dm and copy
-	 it into RESULT.  */
-      dyn_string_t demangled = (dyn_string_t) result_pop (dm);
-      if (!dyn_string_copy (result, demangled))
-	return STATUS_ALLOCATION_FAILED;
-      dyn_string_delete (demangled);
+      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_NAME:
+    case DEMANGLE_COMPONENT_OPERATOR:
+    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
+    case DEMANGLE_COMPONENT_SUB_STD:
+    case DEMANGLE_COMPONENT_CHARACTER:
+      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,
+		 const struct demangle_component *dc)
+{
+  int simple = 0;
+  if (dc->type == DEMANGLE_COMPONENT_NAME)
+    simple = 1;
+  if (!simple)
+    d_append_char (dpi, '(');
+  d_print_comp (dpi, dc);
+  if (!simple)
+    d_append_char (dpi, ')');
+}
+
+/* Subroutine to handle components.  */
+
+static void
+d_print_comp (struct d_print_info *dpi,
+              const struct demangle_component *dc)
+{
+  if (dc == NULL)
+    {
+      d_print_error (dpi);
+      return;
+    }
+  if (d_print_saw_error (dpi))
+    return;
+
+  switch (dc->type)
+    {
+    case DEMANGLE_COMPONENT_NAME:
+      if ((dpi->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);
+      return;
+
+    case DEMANGLE_COMPONENT_QUAL_NAME:
+    case DEMANGLE_COMPONENT_LOCAL_NAME:
+      d_print_comp (dpi, d_left (dc));
+      if ((dpi->options & DMGL_JAVA) == 0)
+	d_append_string (dpi, "::");
+      else
+	d_append_char (dpi, '.');
+      d_print_comp (dpi, d_right (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_TYPED_NAME:
+      {
+	struct d_print_mod *hold_modifiers;
+	struct demangle_component *typed_name;
+	struct d_print_mod adpm[4];
+	unsigned int i;
+	struct d_print_template dpt;
+
+	/* Pass the name down to the type so that it can be printed in
+	   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;
+	i = 0;
+	typed_name = d_left (dc);
+	while (typed_name != NULL)
+	  {
+	    if (i >= sizeof adpm / sizeof adpm[0])
+	      {
+		d_print_error (dpi);
+		return;
+	      }
+
+	    adpm[i].next = dpi->modifiers;
+	    dpi->modifiers = &adpm[i];
+	    adpm[i].mod = typed_name;
+	    adpm[i].printed = 0;
+	    adpm[i].templates = dpi->templates;
+	    ++i;
+
+	    if (typed_name->type != DEMANGLE_COMPONENT_RESTRICT_THIS
+		&& typed_name->type != DEMANGLE_COMPONENT_VOLATILE_THIS
+		&& typed_name->type != DEMANGLE_COMPONENT_CONST_THIS)
+	      break;
+
+	    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)
+	  {
+	    dpt.next = dpi->templates;
+	    dpi->templates = &dpt;
+	    dpt.template_decl = typed_name;
+	  }
+
+	/* If typed_name is a DEMANGLE_COMPONENT_LOCAL_NAME, then
+	   there may be CV-qualifiers on its right argument which
+	   really apply here; this happens when parsing a class which
+	   is local to a function.  */
+	if (typed_name->type == DEMANGLE_COMPONENT_LOCAL_NAME)
+	  {
+	    struct demangle_component *local_name;
+
+	    local_name = d_right (typed_name);
+	    while (local_name->type == DEMANGLE_COMPONENT_RESTRICT_THIS
+		   || local_name->type == DEMANGLE_COMPONENT_VOLATILE_THIS
+		   || local_name->type == DEMANGLE_COMPONENT_CONST_THIS)
+	      {
+		if (i >= sizeof adpm / sizeof adpm[0])
+		  {
+		    d_print_error (dpi);
+		    return;
+		  }
+
+		adpm[i] = adpm[i - 1];
+		adpm[i].next = &adpm[i - 1];
+		dpi->modifiers = &adpm[i];
+
+		adpm[i - 1].mod = local_name;
+		adpm[i - 1].printed = 0;
+		adpm[i - 1].templates = dpi->templates;
+		++i;
+
+		local_name = d_left (local_name);
+	      }
+	  }
+
+	d_print_comp (dpi, d_right (dc));
+
+	if (typed_name->type == DEMANGLE_COMPONENT_TEMPLATE)
+	  dpi->templates = dpt.next;
+
+	/* If the modifiers didn't get printed by the type, print them
+	   now.  */
+	while (i > 0)
+	  {
+	    --i;
+	    if (! adpm[i].printed)
+	      {
+		d_append_char (dpi, ' ');
+		d_print_mod (dpi, adpm[i].mod);
+	      }
+	  }
+
+	dpi->modifiers = hold_modifiers;
+
+	return;
+      }
+
+    case DEMANGLE_COMPONENT_TEMPLATE:
+      {
+	struct d_print_mod *hold_dpm;
+	struct demangle_component *dcl;
+
+	/* Don't push modifiers into a template definition.  Doing so
+	   could give the wrong definition for a template argument.
+	   Instead, treat the template essentially as a name.  */
+
+	hold_dpm = dpi->modifiers;
+	dpi->modifiers = NULL;
+
+        dcl = d_left (dc);
+
+        if ((dpi->options & DMGL_JAVA) != 0
+            && dcl->type == DEMANGLE_COMPONENT_NAME
+            && dcl->u.s_name.len == 6
+            && strncmp (dcl->u.s_name.s, "JArray", 6) == 0)
+          {
+            /* Special-case Java arrays, so that JArray<TYPE> appears
+               instead as TYPE[].  */
+
+            d_print_comp (dpi, d_right (dc));
+            d_append_string (dpi, "[]");
+          }
+        else
+          {
+	    d_print_comp (dpi, dcl);
+	    if (d_last_char (dpi) == '<')
+	      d_append_char (dpi, ' ');
+	    d_append_char (dpi, '<');
+	    d_print_comp (dpi, d_right (dc));
+	    /* Avoid generating two consecutive '>' characters, to avoid
+	       the C++ syntactic ambiguity.  */
+	    if (d_last_char (dpi) == '>')
+	      d_append_char (dpi, ' ');
+	    d_append_char (dpi, '>');
+          }
+
+	dpi->modifiers = hold_dpm;
+
+	return;
+      }
+
+    case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
+      {
+	struct d_print_template *hold_dpt;
+	struct demangle_component *a = d_lookup_template_argument (dpi, dc);
+
+	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;
+	  }
+
+	/* While processing this parameter, we need to pop the list of
+	   templates.  This is because the template parameter may
+	   itself be a reference to a parameter of an outer
+	   template.  */
+
+	hold_dpt = dpi->templates;
+	dpi->templates = hold_dpt->next;
+
+	d_print_comp (dpi, a);
+
+	dpi->templates = hold_dpt;
+
+	return;
+      }
+
+    case DEMANGLE_COMPONENT_CTOR:
+      d_print_comp (dpi, dc->u.s_ctor.name);
+      return;
+
+    case DEMANGLE_COMPONENT_DTOR:
+      d_append_char (dpi, '~');
+      d_print_comp (dpi, dc->u.s_dtor.name);
+      return;
+
+    case DEMANGLE_COMPONENT_VTABLE:
+      d_append_string (dpi, "vtable for ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_VTT:
+      d_append_string (dpi, "VTT for ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
+      d_append_string (dpi, "construction vtable for ");
+      d_print_comp (dpi, d_left (dc));
+      d_append_string (dpi, "-in-");
+      d_print_comp (dpi, d_right (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_TYPEINFO:
+      d_append_string (dpi, "typeinfo for ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_TYPEINFO_NAME:
+      d_append_string (dpi, "typeinfo name for ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_TYPEINFO_FN:
+      d_append_string (dpi, "typeinfo fn for ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_THUNK:
+      d_append_string (dpi, "non-virtual thunk to ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
+      d_append_string (dpi, "virtual thunk to ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_COVARIANT_THUNK:
+      d_append_string (dpi, "covariant return thunk to ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_JAVA_CLASS:
+      d_append_string (dpi, "java Class for ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_GUARD:
+      d_append_string (dpi, "guard variable for ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_REFTEMP:
+      d_append_string (dpi, "reference temporary for ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
+      d_append_string (dpi, "hidden alias for ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_SUB_STD:
+      d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
+      return;
+
+    case DEMANGLE_COMPONENT_RESTRICT:
+    case DEMANGLE_COMPONENT_VOLATILE:
+    case DEMANGLE_COMPONENT_CONST:
+      {
+	struct d_print_mod *pdpm;
+
+	/* When printing arrays, it's possible to have cases where the
+	   same CV-qualifier gets pushed on the stack multiple times.
+	   We only need to print it once.  */
+
+	for (pdpm = dpi->modifiers; pdpm != NULL; pdpm = pdpm->next)
+	  {
+	    if (! pdpm->printed)
+	      {
+		if (pdpm->mod->type != DEMANGLE_COMPONENT_RESTRICT
+		    && pdpm->mod->type != DEMANGLE_COMPONENT_VOLATILE
+		    && pdpm->mod->type != DEMANGLE_COMPONENT_CONST)
+		  break;
+		if (pdpm->mod->type == dc->type)
+		  {
+		    d_print_comp (dpi, d_left (dc));
+		    return;
+		  }
+	      }
+	  }
+      }
+      /* 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_RVALUE_REFERENCE:
+    case DEMANGLE_COMPONENT_COMPLEX:
+    case DEMANGLE_COMPONENT_IMAGINARY:
+      {
+	/* We keep a list of modifiers on the stack.  */
+	struct d_print_mod dpm;
+
+	dpm.next = dpi->modifiers;
+	dpi->modifiers = &dpm;
+	dpm.mod = dc;
+	dpm.printed = 0;
+	dpm.templates = dpi->templates;
+
+	d_print_comp (dpi, d_left (dc));
+
+	/* If the modifier didn't get printed by the type, print it
+	   now.  */
+	if (! dpm.printed)
+	  d_print_mod (dpi, dc);
+
+	dpi->modifiers = dpm.next;
+
+	return;
+      }
+
+    case DEMANGLE_COMPONENT_BUILTIN_TYPE:
+      if ((dpi->options & DMGL_JAVA) == 0)
+	d_append_buffer (dpi, dc->u.s_builtin.type->name,
+			 dc->u.s_builtin.type->len);
+      else
+	d_append_buffer (dpi, dc->u.s_builtin.type->java_name,
+			 dc->u.s_builtin.type->java_len);
+      return;
+
+    case DEMANGLE_COMPONENT_VENDOR_TYPE:
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_FUNCTION_TYPE:
+      {
+	if ((dpi->options & DMGL_RET_POSTFIX) != 0)
+	  d_print_function_type (dpi, dc, dpi->modifiers);
+
+	/* Print return type if present */
+	if (d_left (dc) != NULL)
+	  {
+	    struct d_print_mod dpm;
+
+	    /* We must pass this type down as a modifier in order to
+	       print it in the right location.  */
+	    dpm.next = dpi->modifiers;
+	    dpi->modifiers = &dpm;
+	    dpm.mod = dc;
+	    dpm.printed = 0;
+	    dpm.templates = dpi->templates;
+
+	    d_print_comp (dpi, d_left (dc));
+
+	    dpi->modifiers = dpm.next;
+
+	    if (dpm.printed)
+	      return;
+
+	    /* In standard prefix notation, there is a space between the
+	       return type and the function signature.  */
+	    if ((dpi->options & DMGL_RET_POSTFIX) == 0)
+	      d_append_char (dpi, ' ');
+	  }
+
+	if ((dpi->options & DMGL_RET_POSTFIX) == 0) 
+	  d_print_function_type (dpi, dc, dpi->modifiers);
+
+	return;
+      }
+
+    case DEMANGLE_COMPONENT_ARRAY_TYPE:
+      {
+	struct d_print_mod *hold_modifiers;
+	struct d_print_mod adpm[4];
+	unsigned int i;
+	struct d_print_mod *pdpm;
+
+	/* We must pass this type down as a modifier in order to print
+	   multi-dimensional arrays correctly.  If the array itself is
+	   CV-qualified, we act as though the element type were
+	   CV-qualified.  We do this by copying the modifiers down
+	   rather than fiddling pointers, so that we don't wind up
+	   with a d_print_mod higher on the stack pointing into our
+	   stack frame after we return.  */
+
+	hold_modifiers = dpi->modifiers;
+
+	adpm[0].next = hold_modifiers;
+	dpi->modifiers = &adpm[0];
+	adpm[0].mod = dc;
+	adpm[0].printed = 0;
+	adpm[0].templates = dpi->templates;
+
+	i = 1;
+	pdpm = hold_modifiers;
+	while (pdpm != NULL
+	       && (pdpm->mod->type == DEMANGLE_COMPONENT_RESTRICT
+		   || pdpm->mod->type == DEMANGLE_COMPONENT_VOLATILE
+		   || pdpm->mod->type == DEMANGLE_COMPONENT_CONST))
+	  {
+	    if (! pdpm->printed)
+	      {
+		if (i >= sizeof adpm / sizeof adpm[0])
+		  {
+		    d_print_error (dpi);
+		    return;
+		  }
+
+		adpm[i] = *pdpm;
+		adpm[i].next = dpi->modifiers;
+		dpi->modifiers = &adpm[i];
+		pdpm->printed = 1;
+		++i;
+	      }
+
+	    pdpm = pdpm->next;
+	  }
+
+	d_print_comp (dpi, d_right (dc));
+
+	dpi->modifiers = hold_modifiers;
+
+	if (adpm[0].printed)
+	  return;
+
+	while (i > 1)
+	  {
+	    --i;
+	    d_print_mod (dpi, adpm[i].mod);
+	  }
+
+	d_print_array_type (dpi, dc, dpi->modifiers);
+
+	return;
+      }
+
+    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
+      {
+	struct d_print_mod dpm;
+
+	dpm.next = dpi->modifiers;
+	dpi->modifiers = &dpm;
+	dpm.mod = dc;
+	dpm.printed = 0;
+	dpm.templates = dpi->templates;
+
+	d_print_comp (dpi, 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, "::*");
+	  }
+
+	dpi->modifiers = dpm.next;
+
+	return;
+      }
+
+    case DEMANGLE_COMPONENT_ARGLIST:
+    case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
+      if (d_left (dc) != NULL)
+	d_print_comp (dpi, d_left (dc));
+      if (d_right (dc) != NULL)
+	{
+	  d_append_string (dpi, ", ");
+	  d_print_comp (dpi, d_right (dc));
+	}
+      return;
+
+    case DEMANGLE_COMPONENT_OPERATOR:
+      {
+	char c;
+
+	d_append_string (dpi, "operator");
+	c = dc->u.s_operator.op->name[0];
+	if (IS_LOWER (c))
+	  d_append_char (dpi, ' ');
+	d_append_buffer (dpi, dc->u.s_operator.op->name,
+			 dc->u.s_operator.op->len);
+	return;
+      }
+
+    case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
+      d_append_string (dpi, "operator ");
+      d_print_comp (dpi, dc->u.s_extended_operator.name);
+      return;
+
+    case DEMANGLE_COMPONENT_CAST:
+      d_append_string (dpi, "operator ");
+      d_print_cast (dpi, dc);
+      return;
+
+    case DEMANGLE_COMPONENT_UNARY:
+      if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
+	d_print_expr_op (dpi, d_left (dc));
+      else
+	{
+	  d_append_char (dpi, '(');
+	  d_print_cast (dpi, d_left (dc));
+	  d_append_char (dpi, ')');
+	}
+      if (d_left (dc)->type == DEMANGLE_COMPONENT_CAST
+	  && d_right (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
+	/* type() -- FIXME what about type(multiple,args) */
+	d_append_string (dpi, "()");
+      else
+	d_print_subexpr (dpi, d_right (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_BINARY:
+      if (d_right (dc)->type != DEMANGLE_COMPONENT_BINARY_ARGS)
+	{
+	  d_print_error (dpi);
+	  return;
+	}
+
+      /* We wrap an expression which uses the greater-than operator in
+	 an extra layer of parens so that it does not get confused
+	 with the '>' which ends the template parameters.  */
+      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_append_char (dpi, '(');
+
+      d_print_subexpr (dpi, d_left (d_right (dc)));
+      if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") != 0)
+	d_print_expr_op (dpi, d_left (dc));
+      d_print_subexpr (dpi, d_right (d_right (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_append_char (dpi, ')');
+
+      return;
+
+    case DEMANGLE_COMPONENT_BINARY_ARGS:
+      /* We should only see this as part of DEMANGLE_COMPONENT_BINARY.  */
+      d_print_error (dpi);
+      return;
+
+    case DEMANGLE_COMPONENT_TRINARY:
+      if (d_right (dc)->type != DEMANGLE_COMPONENT_TRINARY_ARG1
+	  || d_right (d_right (dc))->type != DEMANGLE_COMPONENT_TRINARY_ARG2)
+	{
+	  d_print_error (dpi);
+	  return;
+	}
+      d_print_subexpr (dpi, d_left (d_right (dc)));
+      d_print_expr_op (dpi, d_left (dc));
+      d_print_subexpr (dpi, d_left (d_right (d_right (dc))));
+      d_append_string (dpi, " : ");
+      d_print_subexpr (dpi, d_right (d_right (d_right (dc))));
+      return;
+
+    case DEMANGLE_COMPONENT_TRINARY_ARG1:
+    case DEMANGLE_COMPONENT_TRINARY_ARG2:
+      /* We should only see these are part of DEMANGLE_COMPONENT_TRINARY.  */
+      d_print_error (dpi);
+      return;
+
+    case DEMANGLE_COMPONENT_LITERAL:
+    case DEMANGLE_COMPONENT_LITERAL_NEG:
+      {
+	enum d_builtin_type_print tp;
+
+	/* For some builtin types, produce simpler output.  */
+	tp = D_PRINT_DEFAULT;
+	if (d_left (dc)->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)
+	  {
+	    tp = d_left (dc)->u.s_builtin.type->print;
+	    switch (tp)
+	      {
+	      case D_PRINT_INT:
+	      case D_PRINT_UNSIGNED:
+	      case D_PRINT_LONG:
+	      case D_PRINT_UNSIGNED_LONG:
+	      case D_PRINT_LONG_LONG:
+	      case D_PRINT_UNSIGNED_LONG_LONG:
+		if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME)
+		  {
+		    if (dc->type == DEMANGLE_COMPONENT_LITERAL_NEG)
+		      d_append_char (dpi, '-');
+		    d_print_comp (dpi, d_right (dc));
+		    switch (tp)
+		      {
+		      default:
+			break;
+		      case D_PRINT_UNSIGNED:
+			d_append_char (dpi, 'u');
+			break;
+		      case D_PRINT_LONG:
+			d_append_char (dpi, 'l');
+			break;
+		      case D_PRINT_UNSIGNED_LONG:
+			d_append_string (dpi, "ul");
+			break;
+		      case D_PRINT_LONG_LONG:
+			d_append_string (dpi, "ll");
+			break;
+		      case D_PRINT_UNSIGNED_LONG_LONG:
+			d_append_string (dpi, "ull");
+			break;
+		      }
+		    return;
+		  }
+		break;
+
+	      case D_PRINT_BOOL:
+		if (d_right (dc)->type == DEMANGLE_COMPONENT_NAME
+		    && d_right (dc)->u.s_name.len == 1
+		    && dc->type == DEMANGLE_COMPONENT_LITERAL)
+		  {
+		    switch (d_right (dc)->u.s_name.s[0])
+		      {
+		      case '0':
+			d_append_string (dpi, "false");
+			return;
+		      case '1':
+			d_append_string (dpi, "true");
+			return;
+		      default:
+			break;
+		      }
+		  }
+		break;
+
+	      default:
+		break;
+	      }
+	  }
+
+	d_append_char (dpi, '(');
+	d_print_comp (dpi, 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));
+	if (tp == D_PRINT_FLOAT)
+	  d_append_char (dpi, ']');
+      }
+      return;
+
+    case DEMANGLE_COMPONENT_JAVA_RESOURCE:
+      d_append_string (dpi, "java resource ");
+      d_print_comp (dpi, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_COMPOUND_NAME:
+      d_print_comp (dpi, d_left (dc));
+      d_print_comp (dpi, 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, d_left (dc));
+      d_append_char (dpi, ')');
+      return;
+
+    case DEMANGLE_COMPONENT_PACK_EXPANSION:
+      {
+	struct demangle_component *a = d_find_pack (dpi, d_left (dc));
+	int len = d_pack_length (a);
+	int i;
+
+	dc = d_left (dc);
+	for (i = 0; i < len; ++i)
+	  {
+	    dpi->pack_index = i;
+	    d_print_comp (dpi, dc);
+	    if (i < len-1)
+	      d_append_string (dpi, ", ");
+	  }
+      }
+      return;
+
+    default:
+      d_print_error (dpi);
+      return;
+    }
+}
+
+/* Print a Java dentifier.  For Java we try to handle encoded extended
+   Unicode characters.  The C++ ABI doesn't mention Unicode encoding,
+   so we don't it for C++.  Characters are encoded as
+   __U<hex-char>+_.  */
+
+static void
+d_print_java_identifier (struct d_print_info *dpi, const char *name, int len)
+{
+  const char *p;
+  const char *end;
+
+  end = name + len;
+  for (p = name; p < end; ++p)
+    {
+      if (end - p > 3
+	  && p[0] == '_'
+	  && p[1] == '_'
+	  && p[2] == 'U')
+	{
+	  unsigned long c;
+	  const char *q;
+
+	  c = 0;
+	  for (q = p + 3; q < end; ++q)
+	    {
+	      int dig;
+
+	      if (IS_DIGIT (*q))
+		dig = *q - '0';
+	      else if (*q >= 'A' && *q <= 'F')
+		dig = *q - 'A' + 10;
+	      else if (*q >= 'a' && *q <= 'f')
+		dig = *q - 'a' + 10;
+	      else
+		break;
+
+	      c = c * 16 + dig;
+	    }
+	  /* If the Unicode character is larger than 256, we don't try
+	     to deal with it here.  FIXME.  */
+	  if (q < end && *q == '_' && c < 256)
+	    {
+	      d_append_char (dpi, c);
+	      p = q;
+	      continue;
+	    }
+	}
+
+      d_append_char (dpi, *p);
+    }
+}
+
+/* Print a list of modifiers.  SUFFIX is 1 if we are printing
+   qualifiers on this after printing a function.  */
+
+static void
+d_print_mod_list (struct d_print_info *dpi,
+                  struct d_print_mod *mods, int suffix)
+{
+  struct d_print_template *hold_dpt;
+
+  if (mods == NULL || d_print_saw_error (dpi))
+    return;
+
+  if (mods->printed
+      || (! suffix
+	  && (mods->mod->type == DEMANGLE_COMPONENT_RESTRICT_THIS
+	      || mods->mod->type == DEMANGLE_COMPONENT_VOLATILE_THIS
+	      || mods->mod->type == DEMANGLE_COMPONENT_CONST_THIS)))
+    {
+      d_print_mod_list (dpi, mods->next, suffix);
+      return;
     }
 
-  /* Clean up.  */
-  demangling_delete (dm);
+  mods->printed = 1;
+
+  hold_dpt = dpi->templates;
+  dpi->templates = mods->templates;
+
+  if (mods->mod->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
+    {
+      d_print_function_type (dpi, 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);
+      dpi->templates = hold_dpt;
+      return;
+    }
+  else if (mods->mod->type == DEMANGLE_COMPONENT_LOCAL_NAME)
+    {
+      struct d_print_mod *hold_modifiers;
+      struct demangle_component *dc;
+
+      /* When this is on the modifier stack, we have pulled any
+	 qualifiers off the right argument already.  Otherwise, we
+	 print it as usual, but don't let the left argument see any
+	 modifiers.  */
+
+      hold_modifiers = dpi->modifiers;
+      dpi->modifiers = NULL;
+      d_print_comp (dpi, d_left (mods->mod));
+      dpi->modifiers = hold_modifiers;
+
+      if ((dpi->options & DMGL_JAVA) == 0)
+	d_append_string (dpi, "::");
+      else
+	d_append_char (dpi, '.');
+
+      dc = d_right (mods->mod);
+      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);
+
+      dpi->templates = hold_dpt;
+      return;
+    }
+
+  d_print_mod (dpi, mods->mod);
+
+  dpi->templates = hold_dpt;
+
+  d_print_mod_list (dpi, mods->next, suffix);
+}
+
+/* Print a modifier.  */
+
+static void
+d_print_mod (struct d_print_info *dpi,
+             const struct demangle_component *mod)
+{
+  switch (mod->type)
+    {
+    case DEMANGLE_COMPONENT_RESTRICT:
+    case DEMANGLE_COMPONENT_RESTRICT_THIS:
+      d_append_string (dpi, " restrict");
+      return;
+    case DEMANGLE_COMPONENT_VOLATILE:
+    case DEMANGLE_COMPONENT_VOLATILE_THIS:
+      d_append_string (dpi, " volatile");
+      return;
+    case DEMANGLE_COMPONENT_CONST:
+    case DEMANGLE_COMPONENT_CONST_THIS:
+      d_append_string (dpi, " const");
+      return;
+    case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
+      d_append_char (dpi, ' ');
+      d_print_comp (dpi, d_right (mod));
+      return;
+    case DEMANGLE_COMPONENT_POINTER:
+      /* There is no pointer symbol in Java.  */
+      if ((dpi->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;
+    case DEMANGLE_COMPONENT_IMAGINARY:
+      d_append_string (dpi, "imaginary ");
+      return;
+    case DEMANGLE_COMPONENT_PTRMEM_TYPE:
+      if (d_last_char (dpi) != '(')
+	d_append_char (dpi, ' ');
+      d_print_comp (dpi, d_left (mod));
+      d_append_string (dpi, "::*");
+      return;
+    case DEMANGLE_COMPONENT_TYPED_NAME:
+      d_print_comp (dpi, d_left (mod));
+      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);
+      return;
+    }
+}
+
+/* Print a function type, except for the return type.  */
+
+static void
+d_print_function_type (struct d_print_info *dpi,
+                       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:
+	case DEMANGLE_COMPONENT_VOLATILE:
+	case DEMANGLE_COMPONENT_CONST:
+	case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
+	case DEMANGLE_COMPONENT_COMPLEX:
+	case DEMANGLE_COMPONENT_IMAGINARY:
+	case DEMANGLE_COMPONENT_PTRMEM_TYPE:
+	  need_space = 1;
+	  need_paren = 1;
+	  break;
+	case DEMANGLE_COMPONENT_RESTRICT_THIS:
+	case DEMANGLE_COMPONENT_VOLATILE_THIS:
+	case DEMANGLE_COMPONENT_CONST_THIS:
+	  break;
+	default:
+	  break;
+	}
+      if (need_paren)
+	break;
+    }
+
+  if (d_left (dc) != NULL && ! saw_mod)
+    need_paren = 1;
+
+  if (need_paren)
+    {
+      if (! need_space)
+	{
+	  if (d_last_char (dpi) != '('
+	      && d_last_char (dpi) != '*')
+	    need_space = 1;
+	}
+      if (need_space && d_last_char (dpi) != ' ')
+	d_append_char (dpi, ' ');
+      d_append_char (dpi, '(');
+    }
+
+  hold_modifiers = dpi->modifiers;
+  dpi->modifiers = NULL;
+
+  d_print_mod_list (dpi, mods, 0);
+
+  if (need_paren)
+    d_append_char (dpi, ')');
+
+  d_append_char (dpi, '(');
+
+  if (d_right (dc) != NULL)
+    d_print_comp (dpi, d_right (dc));
+
+  d_append_char (dpi, ')');
+
+  d_print_mod_list (dpi, mods, 1);
+
+  dpi->modifiers = hold_modifiers;
+}
+
+/* Print an array type, except for the element type.  */
+
+static void
+d_print_array_type (struct d_print_info *dpi,
+                    const struct demangle_component *dc,
+                    struct d_print_mod *mods)
+{
+  int need_space;
+
+  need_space = 1;
+  if (mods != NULL)
+    {
+      int need_paren;
+      struct d_print_mod *p;
+
+      need_paren = 0;
+      for (p = mods; p != NULL; p = p->next)
+	{
+	  if (! p->printed)
+	    {
+	      if (p->mod->type == DEMANGLE_COMPONENT_ARRAY_TYPE)
+		{
+		  need_space = 0;
+		  break;
+		}
+	      else
+		{
+		  need_paren = 1;
+		  need_space = 1;
+		  break;
+		}
+	    }
+	}
+
+      if (need_paren)
+	d_append_string (dpi, " (");
+
+      d_print_mod_list (dpi, mods, 0);
+
+      if (need_paren)
+	d_append_char (dpi, ')');
+    }
+
+  if (need_space)
+    d_append_char (dpi, ' ');
+
+  d_append_char (dpi, '[');
+
+  if (d_left (dc) != NULL)
+    d_print_comp (dpi, d_left (dc));
+
+  d_append_char (dpi, ']');
+}
+
+/* Print an operator in an expression.  */
+
+static void
+d_print_expr_op (struct d_print_info *dpi,
+                 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);
+}
+
+/* Print a cast.  */
+
+static void
+d_print_cast (struct d_print_info *dpi,
+              const struct demangle_component *dc)
+{
+  if (d_left (dc)->type != DEMANGLE_COMPONENT_TEMPLATE)
+    d_print_comp (dpi, d_left (dc));
+  else
+    {
+      struct d_print_mod *hold_dpm;
+      struct d_print_template dpt;
+
+      /* It appears that for a templated cast operator, we need to put
+	 the template parameters in scope for the operator name, but
+	 not for the parameters.  The effect is that we need to handle
+	 the template printing here.  */
+
+      hold_dpm = dpi->modifiers;
+      dpi->modifiers = NULL;
+
+      dpt.next = dpi->templates;
+      dpi->templates = &dpt;
+      dpt.template_decl = d_left (dc);
+
+      d_print_comp (dpi, 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)));
+      /* Avoid generating two consecutive '>' characters, to avoid
+	 the C++ syntactic ambiguity.  */
+      if (d_last_char (dpi) == '>')
+	d_append_char (dpi, ' ');
+      d_append_char (dpi, '>');
+
+      dpi->modifiers = hold_dpm;
+    }
+}
+
+/* Initialize the information structure we use to pass around
+   information.  */
+
+CP_STATIC_IF_GLIBCPP_V3
+void
+cplus_demangle_init_info (const char *mangled, int options, size_t len,
+                          struct d_info *di)
+{
+  di->s = mangled;
+  di->send = mangled + len;
+  di->options = options;
+
+  di->n = mangled;
+
+  /* We can not need more components than twice the number of chars in
+     the mangled string.  Most components correspond directly to
+     chars, but the ARGLIST types are exceptions.  */
+  di->num_comps = 2 * len;
+  di->next_comp = 0;
+
+  /* Similarly, we can not need more substitutions than there are
+     chars in the mangled string.  */
+  di->num_subs = len;
+  di->next_sub = 0;
+  di->did_subs = 0;
+
+  di->last_name = NULL;
+
+  di->expansion = 0;
+}
+
+/* Internal implementation for the demangler.  If MANGLED is a g++ v3 ABI
+   mangled name, return strings in repeated callback giving the demangled
+   name.  OPTIONS is the usual libiberty demangler options.  On success,
+   this returns 1.  On failure, returns 0.  */
+
+static int
+d_demangle_callback (const char *mangled, int options,
+                     demangle_callbackref callback, void *opaque)
+{
+  int type;
+  struct d_info di;
+  struct demangle_component *dc;
+  int status;
+
+  if (mangled[0] == '_' && mangled[1] == 'Z')
+    type = 0;
+  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;
+    }
+  else
+    {
+      if ((options & DMGL_TYPES) == 0)
+	return 0;
+      type = 1;
+    }
+
+  cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
+
+  {
+#ifdef CP_DYNAMIC_ARRAYS
+    __extension__ struct demangle_component comps[di.num_comps];
+    __extension__ struct demangle_component *subs[di.num_subs];
+
+    di.comps = comps;
+    di.subs = subs;
+#else
+    di.comps = alloca (di.num_comps * sizeof (*di.comps));
+    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);
+
+    /* If DMGL_PARAMS is set, then if we didn't consume the entire
+       mangled string, then we didn't successfully demangle it.  If
+       DMGL_PARAMS is not set, we didn't look at the trailing
+       parameters.  */
+    if (((options & DMGL_PARAMS) != 0) && d_peek_char (&di) != '\0')
+      dc = NULL;
+
+#ifdef CP_DEMANGLE_DEBUG
+    d_dump (dc, 0);
+#endif
+
+    status = (dc != NULL)
+             ? cplus_demangle_print_callback (options, dc, callback, opaque)
+             : 0;
+  }
 
   return status;
 }
 
-extern char *__cxa_demangle PARAMS ((const char *, char *, size_t *, int *));
+/* Entry point for the demangler.  If MANGLED is a g++ v3 ABI mangled
+   name, return a buffer allocated with malloc holding the demangled
+   name.  OPTIONS is the usual libiberty demangler options.  On
+   success, this sets *PALC to the allocated size of the returned
+   buffer.  On failure, this sets *PALC to 0 for a bad name, or 1 for
+   a memory allocation failure, and returns NULL.  */
 
-/* ia64 ABI-mandated entry point in the C++ runtime library for performing
-   demangling.  MANGLED_NAME is a NUL-terminated character string
-   containing the name to be demangled.  
+static char *
+d_demangle (const char *mangled, int options, size_t *palc)
+{
+  struct d_growable_string dgs;
+  int status;
+
+  d_growable_string_init (&dgs, 0);
+
+  status = d_demangle_callback (mangled, options,
+                                d_growable_string_callback_adapter, &dgs);
+  if (status == 0)
+    {
+      free (dgs.buf);
+      *palc = 0;
+      return NULL;
+    }
+
+  *palc = dgs.allocation_failure ? 1 : 0;
+  return dgs.buf;
+}
+
+#if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
+
+extern char *__cxa_demangle (const char *, char *, size_t *, int *);
+
+/* ia64 ABI-mandated entry point in the C++ runtime library for
+   performing demangling.  MANGLED_NAME is a NUL-terminated character
+   string containing the name to be demangled.
 
    OUTPUT_BUFFER is a region of memory, allocated with malloc, of
    *LENGTH bytes, into which the demangled name is stored.  If
    OUTPUT_BUFFER is not long enough, it is expanded using realloc.
    OUTPUT_BUFFER may instead be NULL; in that case, the demangled name
-   is placed in a region of memory allocated with malloc.  
+   is placed in a region of memory allocated with malloc.
 
-   If LENGTH is non-NULL, the length of the buffer conaining the
-   demangled name, is placed in *LENGTH.  
+   If LENGTH is non-NULL, the length of the buffer containing the
+   demangled name, is placed in *LENGTH.
 
    The return value is a pointer to the start of the NUL-terminated
    demangled name, or NULL if the demangling fails.  The caller is
-   responsible for deallocating this memory using free.  
+   responsible for deallocating this memory using free.
 
    *STATUS is set to one of the following values:
       0: The demangling operation succeeded.
-     -1: A memory allocation failiure occurred.
+     -1: A memory allocation failure occurred.
      -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
      -3: One of the arguments is invalid.
 
-   The demagling is performed using the C++ ABI mangling rules, with
+   The demangling is performed using the C++ ABI mangling rules, with
    GNU extensions.  */
 
 char *
-__cxa_demangle (mangled_name, output_buffer, length, status)
-     const char *mangled_name;
-     char *output_buffer;
-     size_t *length;
-     int *status;
+__cxa_demangle (const char *mangled_name, char *output_buffer,
+                size_t *length, int *status)
 {
-  struct dyn_string demangled_name;
-  status_t result;
+  char *demangled;
+  size_t alc;
 
-  if (status == NULL)
-    return NULL;
-
-  if (mangled_name == NULL) {
-    *status = -3;
-    return NULL;
-  }
-
-  /* Did the caller provide a buffer for the demangled name?  */
-  if (output_buffer == NULL) {
-    /* No; dyn_string will malloc a buffer for us.  */
-    if (!dyn_string_init (&demangled_name, 0)) 
-      {
-	*status = -1;
-	return NULL;
-      }
-  }
-  else {
-    /* Yes.  Check that the length was provided.  */
-    if (length == NULL) {
-      *status = -3;
+  if (mangled_name == NULL)
+    {
+      if (status != NULL)
+	*status = -3;
       return NULL;
     }
-    /* Install the buffer into a dyn_string.  */
-    demangled_name.allocated = *length;
-    demangled_name.length = 0;
-    demangled_name.s = output_buffer;
-  }
 
-  if (mangled_name[0] == '_' && mangled_name[1] == 'Z')
-    /* MANGLED_NAME apprears to be a function or variable name.
-       Demangle it accordingly.  */
-    result = cp_demangle (mangled_name, &demangled_name, 0);
-  else
-    /* Try to demangled MANGLED_NAME as the name of a type.  */
-    result = cp_demangle_type (mangled_name, &demangled_name);
-
-  if (result == STATUS_OK) 
-    /* The demangling succeeded.  */
+  if (output_buffer != NULL && length == NULL)
     {
-      /* If LENGTH isn't NULL, store the allocated buffer length
-	 there; the buffer may have been realloced by dyn_string
-	 functions.  */
+      if (status != NULL)
+	*status = -3;
+      return NULL;
+    }
+
+  demangled = d_demangle (mangled_name, DMGL_PARAMS | DMGL_TYPES, &alc);
+
+  if (demangled == NULL)
+    {
+      if (status != NULL)
+	{
+	  if (alc == 1)
+	    *status = -1;
+	  else
+	    *status = -2;
+	}
+      return NULL;
+    }
+
+  if (output_buffer == NULL)
+    {
       if (length != NULL)
-	*length = demangled_name.allocated;
-      /* The operation was a success.  */
-      *status = 0;
-      return dyn_string_buf (&demangled_name);
-    }
-  else if (result == STATUS_ALLOCATION_FAILED)
-    /* A call to malloc or realloc failed during the demangling
-       operation.  */
-    {
-      *status = -1;
-      return NULL;
+	*length = alc;
     }
   else
-    /* The demangling failed for another reason, most probably because
-       MANGLED_NAME isn't a valid mangled name.  */
     {
-      /* If the buffer containing the demangled name wasn't provided
-	 by the caller, free it.  */
-      if (output_buffer == NULL)
-	free (dyn_string_buf (&demangled_name));
-      *status = -2;
-      return NULL;
+      if (strlen (demangled) < *length)
+	{
+	  strcpy (output_buffer, demangled);
+	  free (demangled);
+	  demangled = output_buffer;
+	}
+      else
+	{
+	  free (output_buffer);
+	  *length = alc;
+	}
     }
+
+  if (status != NULL)
+    *status = 0;
+
+  return demangled;
 }
 
-#else /* !IN_LIBGCC2 */
+extern int __gcclibcxx_demangle_callback (const char *,
+                                          void (*)
+                                            (const char *, size_t, void *),
+                                          void *);
 
-/* Variant entry point for integration with the existing cplus-dem
-   demangler.  Attempts to demangle MANGLED.  If the demangling
-   succeeds, returns a buffer, allocated with malloc, containing the
-   demangled name.  The caller must deallocate the buffer using free.
-   If the demangling failes, returns NULL.  */
+/* Alternative, allocationless entry point in the C++ runtime library
+   for performing demangling.  MANGLED_NAME is a NUL-terminated character
+   string containing the name to be demangled.
+
+   CALLBACK is a callback function, called with demangled string
+   segments as demangling progresses; it is called at least once,
+   but may be called more than once.  OPAQUE is a generalized pointer
+   used as a callback argument.
+
+   The return code is one of the following values, equivalent to
+   the STATUS values of __cxa_demangle() (excluding -1, since this
+   function performs no memory allocations):
+      0: The demangling operation succeeded.
+     -2: MANGLED_NAME is not a valid name under the C++ ABI mangling rules.
+     -3: One of the arguments is invalid.
+
+   The demangling is performed using the C++ ABI mangling rules, with
+   GNU extensions.  */
+
+int
+__gcclibcxx_demangle_callback (const char *mangled_name,
+                               void (*callback) (const char *, size_t, void *),
+                               void *opaque)
+{
+  int status;
+
+  if (mangled_name == NULL || callback == NULL)
+    return -3;
+
+  status = d_demangle_callback (mangled_name, DMGL_PARAMS | DMGL_TYPES,
+                                callback, opaque);
+  if (status == 0)
+    return -2;
+
+  return 0;
+}
+
+#else /* ! (IN_LIBGCC2 || IN_GLIBCPP_V3) */
+
+/* Entry point for libiberty demangler.  If MANGLED is a g++ v3 ABI
+   mangled name, return a buffer allocated with malloc holding the
+   demangled name.  Otherwise, return NULL.  */
 
 char *
-ML_(cplus_demangle_v3) (mangled)
-     const char* mangled;
+cplus_demangle_v3 (const char *mangled, int options)
 {
-  dyn_string_t demangled;
-  status_t status;
+  size_t alc;
 
-  /* If this isn't a mangled name, don't pretend to demangle it.  */
-  if (VG_(strncmp) (mangled, "_Z", 2) != 0)
-    return NULL;
+  return d_demangle (mangled, options, &alc);
+}
 
-  /* Create a dyn_string to hold the demangled name.  */
-  demangled = dyn_string_new (0);
-  /* Attempt the demangling.  */
-  status = cp_demangle ((char *) mangled, demangled, 0);
-
-  if (STATUS_NO_ERROR (status))
-    /* Demangling succeeded.  */
-    {
-      /* Grab the demangled result from the dyn_string.  It was
-	 allocated with malloc, so we can return it directly.  */
-      char *return_value = dyn_string_release (demangled);
-      /* Hand back the demangled name.  */
-      return return_value;
-    }
-  else if (status == STATUS_ALLOCATION_FAILED)
-    {
-	vg_assert (0);
-	/*
-      fprintf (stderr, "Memory allocation failed.\n");
-      abort ();
-      */
-    }
-  else
-    /* Demangling failed.  */
-    {
-      dyn_string_delete (demangled);
-      return NULL;
-    }
+int
+cplus_demangle_v3_callback (const char *mangled, int options,
+                            demangle_callbackref callback, void *opaque)
+{
+  return d_demangle_callback (mangled, options, callback, opaque);
 }
 
 /* Demangle a Java symbol.  Java uses a subset of the V3 ABI C++ mangling 
    conventions, but the output formatting is a little different.
-   This instructs the C++ demangler not to emit pointer characters ("*"), and 
-   to use Java's namespace separator symbol ("." instead of "::").  It then 
-   does an additional pass over the demangled output to replace instances 
-   of JArray<TYPE> with TYPE[].  */
+   This instructs the C++ demangler not to emit pointer characters ("*"), to
+   use Java's namespace separator symbol ("." instead of "::"), and to output
+   JArray<TYPE> as TYPE[].  */
 
 char *
-ML_(java_demangle_v3) (mangled)
-     const char* mangled;
+java_demangle_v3 (const char *mangled)
 {
-  dyn_string_t demangled;
-  char *next;
-  char *end;
-  int len;
-  status_t status;
-  int nesting = 0;
-  char *cplus_demangled;
-  char *return_value;
-    
-  /* Create a dyn_string to hold the demangled name.  */
-  demangled = dyn_string_new (0);
+  size_t alc;
 
-  /* Attempt the demangling.  */
-  status = cp_demangle ((char *) mangled, demangled, DMGL_JAVA);
-
-  if (STATUS_NO_ERROR (status))
-    /* Demangling succeeded.  */
-    {
-      /* Grab the demangled result from the dyn_string. */
-      cplus_demangled = dyn_string_release (demangled);
-    }
-  else if (status == STATUS_ALLOCATION_FAILED)
-    {
-	vg_assert (0);
-	/*
-      fprintf (stderr, "Memory allocation failed.\n");
-      abort ();
-      */
-    }
-  else
-    /* Demangling failed.  */
-    {
-      dyn_string_delete (demangled);
-      return NULL;
-    }
-  
-  len = VG_(strlen) (cplus_demangled);
-  next = cplus_demangled;
-  end = next + len;
-  demangled = NULL;
-
-  /* Replace occurrences of JArray<TYPE> with TYPE[]. */
-  while (next < end)
-    {
-      char *open_str = VG_(strstr) (next, "JArray<");
-      char *close_str = NULL;
-      if (nesting > 0)
-	close_str = VG_(strchr) (next, '>');
-    
-      if (open_str != NULL && (close_str == NULL || close_str > open_str))
-        {
-	  ++nesting;
-	  
-	  if (!demangled)
-	    demangled = dyn_string_new(len);
-
-          /* Copy prepending symbols, if any. */
-	  if (open_str > next)
-	    {
-	      open_str[0] = 0;
-	      dyn_string_append_cstr (demangled, next);
-	    }	  
-	  next = open_str + 7;
-	}
-      else if (close_str != NULL)
-        {
-	  --nesting;
-	  
-          /* Copy prepending type symbol, if any. Squash any spurious 
-	     whitespace. */
-	  if (close_str > next && next[0] != ' ')
-	    {
-	      close_str[0] = 0;
-	      dyn_string_append_cstr (demangled, next);
-	    }
-	  dyn_string_append_cstr (demangled, "[]");	  
-	  next = close_str + 1;
-	}
-      else
-        {
-	  /* There are no more arrays. Copy the rest of the symbol, or
-	     simply return the original symbol if no changes were made. */
-	  if (next == cplus_demangled)
-	    return cplus_demangled;
-
-          dyn_string_append_cstr (demangled, next);
-	  next = end;
-	}
-    }
-
-  free (cplus_demangled);
-  
-  return_value = dyn_string_release (demangled);
-  return return_value;
+  return d_demangle (mangled, DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX, &alc);
 }
 
-#endif /* IN_LIBGCC2 */
-
-#if 0
-
-/* Demangle NAME in the G++ V3 ABI demangling style, and return either
-   zero, indicating that some error occurred, or a demangling_t
-   holding the results.  */
-static demangling_t
-demangle_v3_with_details (name)
-     const char *name;
+int
+java_demangle_v3_callback (const char *mangled,
+                           demangle_callbackref callback, void *opaque)
 {
-  demangling_t dm;
-  status_t status;
-
-  if (VG_(strncmp) (name, "_Z", 2))
-    return 0;
-
-  dm = demangling_new (name, DMGL_GNU_V3);
-  if (dm == NULL)
-    {
-	vg_assert (0);
-	/*
-      fprintf (stderr, "Memory allocation failed.\n");
-      abort ();
-      */
-    }
-
-  status = result_push (dm);
-  if (! STATUS_NO_ERROR (status))
-    {
-      demangling_delete (dm);
-      vg_assert (0);
-      /*
-      fprintf (stderr, "%s\n", status);
-      abort ();
-      */
-    }
-
-  status = demangle_mangled_name (dm);
-  if (STATUS_NO_ERROR (status))
-    return dm;
-
-  demangling_delete (dm);
-  return 0;
+  return d_demangle_callback (mangled,
+                              DMGL_JAVA | DMGL_PARAMS | DMGL_RET_POSTFIX,
+                              callback, opaque);
 }
 
+#endif /* IN_LIBGCC2 || IN_GLIBCPP_V3 */
 
-/* Return non-zero iff NAME is the mangled form of a constructor name
-   in the G++ V3 ABI demangling style.  Specifically, return:
-   - '1' if NAME is a complete object constructor,
-   - '2' if NAME is a base object constructor, or
-   - '3' if NAME is a complete object allocating constructor.  */
-enum gnu_v3_ctor_kinds
-is_gnu_v3_mangled_ctor (name)
-     const char *name;
+#ifndef IN_GLIBCPP_V3
+
+/* Demangle a string in order to find out whether it is a constructor
+   or destructor.  Return non-zero on success.  Set *CTOR_KIND and
+   *DTOR_KIND appropriately.  */
+
+static int
+is_ctor_or_dtor (const char *mangled,
+                 enum gnu_v3_ctor_kinds *ctor_kind,
+                 enum gnu_v3_dtor_kinds *dtor_kind)
 {
-  demangling_t dm = demangle_v3_with_details (name);
+  struct d_info di;
+  struct demangle_component *dc;
+  int ret;
 
-  if (dm)
-    {
-      enum gnu_v3_ctor_kinds result = dm->is_constructor;
-      demangling_delete (dm);
-      return result;
-    }
-  else
-    return 0;
-}
+  *ctor_kind = (enum gnu_v3_ctor_kinds) 0;
+  *dtor_kind = (enum gnu_v3_dtor_kinds) 0;
 
+  cplus_demangle_init_info (mangled, DMGL_GNU_V3, strlen (mangled), &di);
 
-/* Return non-zero iff NAME is the mangled form of a destructor name
-   in the G++ V3 ABI demangling style.  Specifically, return:
-   - '0' if NAME is a deleting destructor,
-   - '1' if NAME is a complete object destructor, or
-   - '2' if NAME is a base object destructor.  */
-enum gnu_v3_dtor_kinds
-is_gnu_v3_mangled_dtor (name)
-     const char *name;
-{
-  demangling_t dm = demangle_v3_with_details (name);
+  {
+#ifdef CP_DYNAMIC_ARRAYS
+    __extension__ struct demangle_component comps[di.num_comps];
+    __extension__ struct demangle_component *subs[di.num_subs];
 
-  if (dm)
-    {
-      enum gnu_v3_dtor_kinds result = dm->is_destructor;
-      demangling_delete (dm);
-      return result;
-    }
-  else
-    return 0;
-}
-
+    di.comps = comps;
+    di.subs = subs;
+#else
+    di.comps = alloca (di.num_comps * sizeof (*di.comps));
+    di.subs = alloca (di.num_subs * sizeof (*di.subs));
 #endif
 
+    dc = cplus_demangle_mangled_name (&di, 1);
+
+    /* Note that because we did not pass DMGL_PARAMS, we don't expect
+       to demangle the entire string.  */
+
+    ret = 0;
+    while (dc != NULL)
+      {
+	switch (dc->type)
+	  {
+	  default:
+	    dc = NULL;
+	    break;
+	  case DEMANGLE_COMPONENT_TYPED_NAME:
+	  case DEMANGLE_COMPONENT_TEMPLATE:
+	  case DEMANGLE_COMPONENT_RESTRICT_THIS:
+	  case DEMANGLE_COMPONENT_VOLATILE_THIS:
+	  case DEMANGLE_COMPONENT_CONST_THIS:
+	    dc = d_left (dc);
+	    break;
+	  case DEMANGLE_COMPONENT_QUAL_NAME:
+	  case DEMANGLE_COMPONENT_LOCAL_NAME:
+	    dc = d_right (dc);
+	    break;
+	  case DEMANGLE_COMPONENT_CTOR:
+	    *ctor_kind = dc->u.s_ctor.kind;
+	    ret = 1;
+	    dc = NULL;
+	    break;
+	  case DEMANGLE_COMPONENT_DTOR:
+	    *dtor_kind = dc->u.s_dtor.kind;
+	    ret = 1;
+	    dc = NULL;
+	    break;
+	  }
+      }
+  }
+
+  return ret;
+}
+
+/* Return whether NAME is the mangled form of a g++ V3 ABI constructor
+   name.  A non-zero return indicates the type of constructor.  */
+
+enum gnu_v3_ctor_kinds
+is_gnu_v3_mangled_ctor (const char *name)
+{
+  enum gnu_v3_ctor_kinds ctor_kind;
+  enum gnu_v3_dtor_kinds dtor_kind;
+
+  if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
+    return (enum gnu_v3_ctor_kinds) 0;
+  return ctor_kind;
+}
+
+
+/* Return whether NAME is the mangled form of a g++ V3 ABI destructor
+   name.  A non-zero return indicates the type of destructor.  */
+
+enum gnu_v3_dtor_kinds
+is_gnu_v3_mangled_dtor (const char *name)
+{
+  enum gnu_v3_ctor_kinds ctor_kind;
+  enum gnu_v3_dtor_kinds dtor_kind;
+
+  if (! is_ctor_or_dtor (name, &ctor_kind, &dtor_kind))
+    return (enum gnu_v3_dtor_kinds) 0;
+  return dtor_kind;
+}
+
+#endif /* IN_GLIBCPP_V3 */
+
 #ifdef STANDALONE_DEMANGLER
 
+#if 0 /* in valgrind */
 #include "getopt.h"
+#include "dyn-string.h"
+#endif /* ! in valgrind */
 
-static void print_usage
-  PARAMS ((FILE* fp, int exit_value));
+static void print_usage (FILE* fp, int exit_value);
+
+#define IS_ALPHA(CHAR)                                                  \
+  (((CHAR) >= 'a' && (CHAR) <= 'z')                                     \
+   || ((CHAR) >= 'A' && (CHAR) <= 'Z'))
 
 /* Non-zero if CHAR is a character than can occur in a mangled name.  */
 #define is_mangled_char(CHAR)                                           \
@@ -4010,14 +4796,12 @@
 /* Prints usage summary to FP and then exits with EXIT_VALUE.  */
 
 static void
-print_usage (fp, exit_value)
-     FILE* fp;
-     int exit_value;
+print_usage (FILE* fp, int exit_value)
 {
   fprintf (fp, "Usage: %s [options] [names ...]\n", program_name);
   fprintf (fp, "Options:\n");
   fprintf (fp, "  -h,--help       Display this message.\n");
-  fprintf (fp, "  -s,--strict     Demangle standard names only.\n");
+  fprintf (fp, "  -p,--no-params  Don't display function parameters\n");
   fprintf (fp, "  -v,--verbose    Produce verbose demanglings.\n");
   fprintf (fp, "If names are provided, they are demangled.  Otherwise filters standard input.\n");
 
@@ -4027,10 +4811,10 @@
 /* Option specification for getopt_long.  */
 static const struct option long_options[] = 
 {
-  { "help",    no_argument, NULL, 'h' },
-  { "strict",  no_argument, NULL, 's' },
-  { "verbose", no_argument, NULL, 'v' },
-  { NULL,      no_argument, NULL, 0   },
+  { "help",	 no_argument, NULL, 'h' },
+  { "no-params", no_argument, NULL, 'p' },
+  { "verbose",   no_argument, NULL, 'v' },
+  { NULL,        no_argument, NULL, 0   },
 };
 
 /* Main entry for a demangling filter executable.  It will demangle
@@ -4039,13 +4823,11 @@
    with their demangled equivalents.  */
 
 int
-main (argc, argv)
-     int argc;
-     char *argv[];
+main (int argc, char *argv[])
 {
-  status_t status;
   int i;
   int opt_char;
+  int options = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
 
   /* Use the program name of this program, as invoked.  */
   program_name = argv[0];
@@ -4053,7 +4835,7 @@
   /* Parse options.  */
   do 
     {
-      opt_char = getopt_long (argc, argv, "hsv", long_options, NULL);
+      opt_char = getopt_long (argc, argv, "hpv", long_options, NULL);
       switch (opt_char)
 	{
 	case '?':  /* Unrecognized option.  */
@@ -4064,12 +4846,12 @@
 	  print_usage (stdout, 0);
 	  break;
 
-	case 's':
-	  flag_strict = 1;
+	case 'p':
+	  options &= ~ DMGL_PARAMS;
 	  break;
 
 	case 'v':
-	  flag_verbose = 1;
+	  options |= DMGL_VERBOSE;
 	  break;
 	}
     }
@@ -4079,41 +4861,12 @@
     /* No command line arguments were provided.  Filter stdin.  */
     {
       dyn_string_t mangled = dyn_string_new (3);
-      dyn_string_t demangled = dyn_string_new (0);
-      status_t status;
+      char *s;
 
       /* Read all of input.  */
       while (!feof (stdin))
 	{
-	  char c = getchar ();
-
-	  /* The first character of a mangled name is an underscore.  */
-	  if (feof (stdin))
-	    break;
-	  if (c != '_')
-	    {
-	      /* It's not a mangled name.  Print the character and go
-		 on.  */
-	      putchar (c);
-	      continue;
-	    }
-	  c = getchar ();
-	  
-	  /* The second character of a mangled name is a capital `Z'.  */
-	  if (feof (stdin))
-	    break;
-	  if (c != 'Z')
-	    {
-	      /* It's not a mangled name.  Print the previous
-		 underscore, the `Z', and go on.  */
-	      putchar ('_');
-	      putchar (c);
-	      continue;
-	    }
-
-	  /* Start keeping track of the candidate mangled name.  */
-	  dyn_string_append_char (mangled, '_');
-	  dyn_string_append_char (mangled, 'Z');
+	  char c;
 
 	  /* Pile characters into mangled until we hit one that can't
 	     occur in a mangled name.  */
@@ -4126,62 +4879,70 @@
 	      c = getchar ();
 	    }
 
-	  /* Attempt to demangle the name.  */
-	  status = cp_demangle (dyn_string_buf (mangled), demangled, 0);
-
-	  /* If the demangling succeeded, great!  Print out the
-	     demangled version.  */
-	  if (STATUS_NO_ERROR (status))
-	    fputs (dyn_string_buf (demangled), stdout);
-	  /* Abort on allocation failures.  */
-	  else if (status == STATUS_ALLOCATION_FAILED)
+	  if (dyn_string_length (mangled) > 0)
 	    {
-	      fprintf (stderr, "Memory allocation failed.\n");
-	      abort ();
+#ifdef IN_GLIBCPP_V3
+	      s = __cxa_demangle (dyn_string_buf (mangled), NULL, NULL, NULL);
+#else
+	      s = cplus_demangle_v3 (dyn_string_buf (mangled), options);
+#endif
+
+	      if (s != NULL)
+		{
+		  fputs (s, stdout);
+		  free (s);
+		}
+	      else
+		{
+		  /* It might not have been a mangled name.  Print the
+		     original text.  */
+		  fputs (dyn_string_buf (mangled), stdout);
+		}
+
+	      dyn_string_clear (mangled);
 	    }
-	  /* Otherwise, it might not have been a mangled name.  Just
-	     print out the original text.  */
-	  else
-	    fputs (dyn_string_buf (mangled), stdout);
 
 	  /* If we haven't hit EOF yet, we've read one character that
 	     can't occur in a mangled name, so print it out.  */
 	  if (!feof (stdin))
 	    putchar (c);
-
-	  /* Clear the candidate mangled name, to start afresh next
-	     time we hit a `_Z'.  */
-	  dyn_string_clear (mangled);
 	}
 
       dyn_string_delete (mangled);
-      dyn_string_delete (demangled);
     }
   else
     /* Demangle command line arguments.  */
     {
-      dyn_string_t result = dyn_string_new (0);
-
       /* Loop over command line arguments.  */
       for (i = optind; i < argc; ++i)
 	{
+	  char *s;
+#ifdef IN_GLIBCPP_V3
+	  int status;
+#endif
+
 	  /* Attempt to demangle.  */
-	  status = cp_demangle (argv[i], result, 0);
+#ifdef IN_GLIBCPP_V3
+	  s = __cxa_demangle (argv[i], NULL, NULL, &status);
+#else
+	  s = cplus_demangle_v3 (argv[i], options);
+#endif
 
 	  /* If it worked, print the demangled name.  */
-	  if (STATUS_NO_ERROR (status))
-	    printf ("%s\n", dyn_string_buf (result));
-	  /* Abort on allocaiton failures.  */
-	  else if (status == STATUS_ALLOCATION_FAILED)
+	  if (s != NULL)
 	    {
-	      fprintf (stderr, "Memory allocation failed.\n");
-	      abort ();
+	      printf ("%s\n", s);
+	      free (s);
 	    }
-	  /* If not, print the error message to stderr instead.  */
-	  else 
-	    fprintf (stderr, "%s\n", status);
+	  else
+	    {
+#ifdef IN_GLIBCPP_V3
+	      fprintf (stderr, "Failed: %s (status %d)\n", argv[i], status);
+#else
+	      fprintf (stderr, "Failed: %s\n", argv[i]);
+#endif
+	    }
 	}
-      dyn_string_delete (result);
     }
 
   return 0;
diff --git a/coregrind/m_demangle/cp-demangle.h b/coregrind/m_demangle/cp-demangle.h
new file mode 100644
index 0000000..aad3743
--- /dev/null
+++ b/coregrind/m_demangle/cp-demangle.h
@@ -0,0 +1,168 @@
+/* Internal demangler interface for g++ V3 ABI.
+   Copyright (C) 2003, 2004, 2005, 2006, 2007 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.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   In addition to the permissions in the GNU General Public License, the
+   Free Software Foundation gives you unlimited permission to link the
+   compiled version of this file into combinations with other programs,
+   and to distribute those combinations without any restriction coming
+   from the use of this file.  (The General Public License restrictions
+   do apply in other respects; for example, they cover modification of
+   the file, and distribution when not linked into a combined
+   executable.)
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 
+*/
+
+/* This file provides some definitions shared by cp-demangle.c and
+   cp-demint.c.  It should not be included by any other files.  */
+
+/* Information we keep for operators.  */
+
+struct demangle_operator_info
+{
+  /* Mangled name.  */
+  const char *code;
+  /* Real name.  */
+  const char *name;
+  /* Length of real name.  */
+  int len;
+  /* Number of arguments.  */
+  int args;
+};
+
+/* How to print the value of a builtin type.  */
+
+enum d_builtin_type_print
+{
+  /* Print as (type)val.  */
+  D_PRINT_DEFAULT,
+  /* Print as integer.  */
+  D_PRINT_INT,
+  /* Print as unsigned integer, with trailing "u".  */
+  D_PRINT_UNSIGNED,
+  /* Print as long, with trailing "l".  */
+  D_PRINT_LONG,
+  /* Print as unsigned long, with trailing "ul".  */
+  D_PRINT_UNSIGNED_LONG,
+  /* Print as long long, with trailing "ll".  */
+  D_PRINT_LONG_LONG,
+  /* Print as unsigned long long, with trailing "ull".  */
+  D_PRINT_UNSIGNED_LONG_LONG,
+  /* Print as bool.  */
+  D_PRINT_BOOL,
+  /* Print as float--put value in square brackets.  */
+  D_PRINT_FLOAT,
+  /* Print in usual way, but here to detect void.  */
+  D_PRINT_VOID
+};
+
+/* Information we keep for a builtin type.  */
+
+struct demangle_builtin_type_info
+{
+  /* Type name.  */
+  const char *name;
+  /* Length of type name.  */
+  int len;
+  /* Type name when using Java.  */
+  const char *java_name;
+  /* Length of java name.  */
+  int java_len;
+  /* How to print a value of this type.  */
+  enum d_builtin_type_print print;
+};
+
+/* The information structure we pass around.  */
+
+struct d_info
+{
+  /* The string we are demangling.  */
+  const char *s;
+  /* The end of the string we are demangling.  */
+  const char *send;
+  /* The options passed to the demangler.  */
+  int options;
+  /* The next character in the string to consider.  */
+  const char *n;
+  /* The array of components.  */
+  struct demangle_component *comps;
+  /* The index of the next available component.  */
+  int next_comp;
+  /* The number of available component structures.  */
+  int num_comps;
+  /* The array of substitutions.  */
+  struct demangle_component **subs;
+  /* The index of the next substitution.  */
+  int next_sub;
+  /* The number of available entries in the subs array.  */
+  int num_subs;
+  /* The number of substitutions which we actually made from the subs
+     array, plus the number of template parameter references we
+     saw.  */
+  int did_subs;
+  /* The last name we saw, for constructors and destructors.  */
+  struct demangle_component *last_name;
+  /* A running total of the length of large expansions from the
+     mangled name to the demangled name, such as standard
+     substitutions and builtin types.  */
+  int expansion;
+};
+
+/* To avoid running past the ending '\0', don't:
+   - call d_peek_next_char if d_peek_char returned '\0'
+   - call d_advance with an 'i' that is too large
+   - call d_check_char(di, '\0')
+   Everything else is safe.  */
+#define d_peek_char(di) (*((di)->n))
+#define d_peek_next_char(di) ((di)->n[1])
+#define d_advance(di, i) ((di)->n += (i))
+#define d_check_char(di, c) (d_peek_char(di) == c ? ((di)->n++, 1) : 0)
+#define d_next_char(di) (d_peek_char(di) == '\0' ? '\0' : *((di)->n++))
+#define d_str(di) ((di)->n)
+
+/* Functions and arrays in cp-demangle.c which are referenced by
+   functions in cp-demint.c.  */
+#ifdef IN_GLIBCPP_V3
+#define CP_STATIC_IF_GLIBCPP_V3 static
+#else
+#define CP_STATIC_IF_GLIBCPP_V3 extern
+#endif
+
+#ifndef IN_GLIBCPP_V3
+extern const struct demangle_operator_info cplus_demangle_operators[];
+#endif
+
+#define D_BUILTIN_TYPE_COUNT (32)
+
+CP_STATIC_IF_GLIBCPP_V3
+const struct demangle_builtin_type_info
+cplus_demangle_builtin_types[D_BUILTIN_TYPE_COUNT];
+
+CP_STATIC_IF_GLIBCPP_V3
+struct demangle_component *
+cplus_demangle_mangled_name (struct d_info *, int);
+
+CP_STATIC_IF_GLIBCPP_V3
+struct demangle_component *
+cplus_demangle_type (struct d_info *);
+
+extern void
+cplus_demangle_init_info (const char *, int, size_t, struct d_info *);
+
+/* cp-demangle.c needs to define this a little differently */
+#undef CP_STATIC_IF_GLIBCPP_V3
diff --git a/coregrind/m_demangle/cplus-dem.c b/coregrind/m_demangle/cplus-dem.c
index 06c03b4..bc4cc18 100644
--- a/coregrind/m_demangle/cplus-dem.c
+++ b/coregrind/m_demangle/cplus-dem.c
@@ -1,6 +1,6 @@
 /* Demangler for GNU C++
    Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
-   2000, 2001 Free Software Foundation, Inc.
+   2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
    Written by James Clark (jjc@jclark.uucp)
    Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
    Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
@@ -11,6 +11,15 @@
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.
 
+In addition to the permissions in the GNU Library General Public
+License, the Free Software Foundation gives you unlimited permission
+to link the compiled version of this file into combinations with other
+programs, and to distribute those combinations without any restriction
+coming from the use of this file.  (The Library Public License
+restrictions do apply in other respects; for example, they cover
+modification of the file, and distribution when not linked into a
+combined executable.)
+
 Libiberty is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
@@ -18,8 +27,8 @@
 
 You should have received a copy of the GNU Library General Public
 License along with libiberty; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
 
@@ -30,38 +39,48 @@
 /* This file lives in both GCC and libiberty.  When making changes, please
    try not to break either.  */
 
-#define __NO_STRING_INLINES
-
+#if 0 /* in valgrind */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+#endif /* ! in valgrind */
 
+#if 0 /* in valgrind */
 #include "safe-ctype.h"
-#include "pub_core_basics.h"
-#include "pub_core_libcbase.h"
-#include "pub_core_libcassert.h"
-#include "pub_tool_libcprint.h"
-#include "pub_core_mallocfree.h"
+#endif /* ! in valgrind */
 
-/*#include <sys/types.h>
+#if 0 /* in valgrind */
+#include <sys/types.h>
 #include <string.h>
-#include <stdio.h>*/
+#include <stdio.h>
+#endif /* ! in valgrind */
 
-/*#ifdef HAVE_STDLIB_H
+#if 0 /* in valgrind */
+#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #else
-char * malloc ();
-char * realloc ();
-#endif*/
+void * malloc ();
+void * realloc ();
+#endif
+#endif /* ! in valgrind */
 
-#include "demangle.h"
-#include "dyn-string.h"
+#if 0 /* in valgrind */
+#include <demangle.h>
 #undef CURRENT_DEMANGLING_STYLE
 #define CURRENT_DEMANGLING_STYLE work->options
+#endif /* ! in valgrind */
 
-/*#include "libiberty.h"*/
+#if 0 /* in valgrind */
+#include "libiberty.h"
+#endif /* ! in valgrind */
 
-static char *ada_demangle  PARAMS ((const char *, int));
+#include "vg_libciface.h"
+
+#include "ansidecl.h"
+#include "demangle.h"
+#include "safe-ctype.h"
+
+static char *ada_demangle (const char *, int);
 
 #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
 
@@ -69,36 +88,7 @@
    that will be output when using the `%d' format with `printf'.  */
 #define INTBUF_SIZE 32
 
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
-#endif
-
-#ifndef STANDALONE
-#define size_t  Int
-
-#define xstrdup(_cc,ptr)        VG_(arena_strdup) (VG_AR_DEMANGLE, _cc, ptr)
-#define free(ptr)               VG_(arena_free)   (VG_AR_DEMANGLE, ptr)
-#define xmalloc(_cc,size)       VG_(arena_malloc) (VG_AR_DEMANGLE, _cc, size)
-#define xrealloc(_cc,ptr, size) VG_(arena_realloc)(VG_AR_DEMANGLE, _cc, ptr, size)
-
-#define abort() vg_assert(0)
-#undef strstr
-#define strstr  VG_(strstr)
-#define sprintf VG_(sprintf)
-#define strcpy  VG_(strcpy)
-#define strncpy VG_(strncpy)
-#define strncat VG_(strncat)
-#define strchr  VG_(strchr)
-#define strpbrk VG_(strpbrk)
-#define strlen  VG_(strlen)
-#define strcmp  VG_(strcmp)
-#define strncmp VG_(strncmp)
-#define memcpy  VG_(memcpy)
-#define memset  VG_(memset)
-#define memcmp  VG_(memcmp)
-#endif
-
-extern void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
+extern void fancy_abort (void) ATTRIBUTE_NORETURN;
 
 /* In order to allow a single demangler executable to demangle strings
    using various common values of CPLUS_MARKER, as well as any specific
@@ -126,14 +116,11 @@
 
 static char char_str[2] = { '\000', '\000' };
 
-/*
 void
-set_cplus_marker_for_demangling (ch)
-     int ch;
+set_cplus_marker_for_demangling (int ch)
 {
   cplus_markers[0] = ch;
 }
-*/
 
 typedef struct string		/* Beware: these aren't required to be */
 {				/*  '\0' terminated.  */
@@ -344,8 +331,6 @@
 };
 
 #define STRING_EMPTY(str)	((str) -> b == (str) -> p)
-#define PREPEND_BLANK(str)	{if (!STRING_EMPTY(str)) \
-    string_prepend(str, " ");}
 #define APPEND_BLANK(str)	{if (!STRING_EMPTY(str)) \
     string_append(str, " ");}
 #define LEN_STRING(str)         ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
@@ -359,163 +344,127 @@
 
 /* Prototypes for local functions */
 
-static void
-delete_work_stuff PARAMS ((struct work_stuff *));
+static void delete_work_stuff (struct work_stuff *);
 
-static void
-delete_non_B_K_work_stuff PARAMS ((struct work_stuff *));
+static void delete_non_B_K_work_stuff (struct work_stuff *);
 
-static char *
-mop_up PARAMS ((struct work_stuff *, string *, int));
+static char *mop_up (struct work_stuff *, string *, int);
 
-static void
-squangle_mop_up PARAMS ((struct work_stuff *));
+static void squangle_mop_up (struct work_stuff *);
 
-static void
-work_stuff_copy_to_from PARAMS ((struct work_stuff *, struct work_stuff *));
+static void work_stuff_copy_to_from (struct work_stuff *, struct work_stuff *);
 
 #if 0
 static int
-demangle_method_args PARAMS ((struct work_stuff *, const char **, string *));
+demangle_method_args (struct work_stuff *, const char **, string *);
 #endif
 
 static char *
-internal_cplus_demangle PARAMS ((struct work_stuff *, const char *));
+internal_cplus_demangle (struct work_stuff *, const char *);
 
 static int
-demangle_template_template_parm PARAMS ((struct work_stuff *work,
-					 const char **, string *));
+demangle_template_template_parm (struct work_stuff *work,
+                                 const char **, string *);
 
 static int
-demangle_template PARAMS ((struct work_stuff *work, const char **, string *,
-			   string *, int, int));
+demangle_template (struct work_stuff *work, const char **, string *,
+                   string *, int, int);
 
 static int
-arm_pt PARAMS ((struct work_stuff *, const char *, int, const char **,
-		const char **));
+arm_pt (struct work_stuff *, const char *, int, const char **,
+        const char **);
 
 static int
-demangle_class_name PARAMS ((struct work_stuff *, const char **, string *));
+demangle_class_name (struct work_stuff *, const char **, string *);
 
 static int
-demangle_qualified PARAMS ((struct work_stuff *, const char **, string *,
-			    int, int));
+demangle_qualified (struct work_stuff *, const char **, string *,
+                    int, int);
 
-static int
-demangle_class PARAMS ((struct work_stuff *, const char **, string *));
+static int demangle_class (struct work_stuff *, const char **, string *);
 
-static int
-demangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));
+static int demangle_fund_type (struct work_stuff *, const char **, string *);
 
-static int
-demangle_signature PARAMS ((struct work_stuff *, const char **, string *));
+static int demangle_signature (struct work_stuff *, const char **, string *);
 
-static int
-demangle_prefix PARAMS ((struct work_stuff *, const char **, string *));
+static int demangle_prefix (struct work_stuff *, const char **, string *);
 
-static int
-gnu_special PARAMS ((struct work_stuff *, const char **, string *));
+static int gnu_special (struct work_stuff *, const char **, string *);
 
-static int
-arm_special PARAMS ((const char **, string *));
+static int arm_special (const char **, string *);
+
+static void string_need (string *, int);
+
+static void string_delete (string *);
 
 static void
-string_need PARAMS ((string *, int));
+string_init (string *);
 
-static void
-string_delete PARAMS ((string *));
-
-static void
-string_init PARAMS ((string *));
-
-static void
-string_clear PARAMS ((string *));
+static void string_clear (string *);
 
 #if 0
-static int
-string_empty PARAMS ((string *));
+static int string_empty (string *);
 #endif
 
-static void
-string_append PARAMS ((string *, const char *));
+static void string_append (string *, const char *);
 
-static void
-string_appends PARAMS ((string *, string *));
+static void string_appends (string *, string *);
 
-static void
-string_appendn PARAMS ((string *, const char *, int));
+static void string_appendn (string *, const char *, int);
 
-static void
-string_prepend PARAMS ((string *, const char *));
+static void string_prepend (string *, const char *);
 
-static void
-string_prependn PARAMS ((string *, const char *, int));
+static void string_prependn (string *, const char *, int);
 
-static void
-string_append_template_idx PARAMS ((string *, int));
+static void string_append_template_idx (string *, int);
+
+static int get_count (const char **, int *);
+
+static int consume_count (const char **);
+
+static int consume_count_with_underscores (const char**);
+
+static int demangle_args (struct work_stuff *, const char **, string *);
+
+static int demangle_nested_args (struct work_stuff*, const char**, string*);
+
+static int do_type (struct work_stuff *, const char **, string *);
+
+static int do_arg (struct work_stuff *, const char **, string *);
 
 static int
-get_count PARAMS ((const char **, int *));
+demangle_function_name (struct work_stuff *, const char **, string *,
+                        const char *);
 
 static int
-consume_count PARAMS ((const char **));
+iterate_demangle_function (struct work_stuff *,
+                           const char **, string *, const char *);
+
+static void remember_type (struct work_stuff *, const char *, int);
+
+static void remember_Btype (struct work_stuff *, const char *, int, int);
+
+static int register_Btype (struct work_stuff *);
+
+static void remember_Ktype (struct work_stuff *, const char *, int);
+
+static void forget_types (struct work_stuff *);
+
+static void forget_B_and_K_types (struct work_stuff *);
+
+static void string_prepends (string *, string *);
 
 static int
-consume_count_with_underscores PARAMS ((const char**));
+demangle_template_value_parm (struct work_stuff*, const char**,
+                              string*, type_kind_t);
 
 static int
-demangle_args PARAMS ((struct work_stuff *, const char **, string *));
+do_hpacc_template_const_value (struct work_stuff *, const char **, string *);
 
 static int
-demangle_nested_args PARAMS ((struct work_stuff*, const char**, string*));
+do_hpacc_template_literal (struct work_stuff *, const char **, string *);
 
-static int
-do_type PARAMS ((struct work_stuff *, const char **, string *));
-
-static int
-do_arg PARAMS ((struct work_stuff *, const char **, string *));
-
-static void
-demangle_function_name PARAMS ((struct work_stuff *, const char **, string *,
-				const char *));
-
-static int
-iterate_demangle_function PARAMS ((struct work_stuff *,
-				   const char **, string *, const char *));
-
-static void
-remember_type PARAMS ((struct work_stuff *, const char *, int));
-
-static void
-remember_Btype PARAMS ((struct work_stuff *, const char *, int, int));
-
-static int
-register_Btype PARAMS ((struct work_stuff *));
-
-static void
-remember_Ktype PARAMS ((struct work_stuff *, const char *, int));
-
-static void
-forget_types PARAMS ((struct work_stuff *));
-
-static void
-forget_B_and_K_types PARAMS ((struct work_stuff *));
-
-static void
-string_prepends PARAMS ((string *, string *));
-
-static int
-demangle_template_value_parm PARAMS ((struct work_stuff*, const char**,
-				      string*, type_kind_t));
-
-static int
-do_hpacc_template_const_value PARAMS ((struct work_stuff *, const char **, string *));
-
-static int
-do_hpacc_template_literal PARAMS ((struct work_stuff *, const char **, string *));
-
-static int
-snarf_numeric_literal PARAMS ((const char **, string *));
+static int snarf_numeric_literal (const char **, string *);
 
 /* There is a TYPE_QUAL value for each type qualifier.  They can be
    combined by bitwise-or to form the complete set of qualifiers for a
@@ -526,36 +475,28 @@
 #define TYPE_QUAL_VOLATILE 0x2
 #define TYPE_QUAL_RESTRICT 0x4
 
-static int
-code_for_qualifier PARAMS ((int));
+static int code_for_qualifier (int);
 
-static const char*
-qualifier_string PARAMS ((int));
+static const char* qualifier_string (int);
 
-static const char*
-demangle_qualifier PARAMS ((int));
+static const char* demangle_qualifier (int);
+
+static int demangle_expression (struct work_stuff *, const char **, string *, 
+                                type_kind_t);
 
 static int
-demangle_expression PARAMS ((struct work_stuff *, const char **, string *, 
-			     type_kind_t));
+demangle_integral_value (struct work_stuff *, const char **, string *);
 
 static int
-demangle_integral_value PARAMS ((struct work_stuff *, const char **,
-				 string *));
-
-static int
-demangle_real_value PARAMS ((struct work_stuff *, const char **, string *));
+demangle_real_value (struct work_stuff *, const char **, string *);
 
 static void
-demangle_arm_hp_template PARAMS ((struct work_stuff *, const char **, int,
-				  string *));
+demangle_arm_hp_template (struct work_stuff *, const char **, int, string *);
 
 static void
-recursively_demangle PARAMS ((struct work_stuff *, const char **, string *,
-			      int));
+recursively_demangle (struct work_stuff *, const char **, string *, int);
 
-static void
-grow_vect PARAMS ((void **, size_t *, size_t, int));
+static void grow_vect (char **, size_t *, size_t, int);
 
 /* Translate count to integer, consuming tokens in the process.
    Conversion terminates on the first non-digit character.
@@ -566,8 +507,7 @@
    Overflow consumes the rest of the digits, and returns -1.  */
 
 static int
-consume_count (type)
-     const char **type;
+consume_count (const char **type)
 {
   int count = 0;
 
@@ -606,8 +546,7 @@
    failure, since 0 can be a valid value.  */
 
 static int
-consume_count_with_underscores (mangled)
-     const char **mangled;
+consume_count_with_underscores (const char **mangled)
 {
   int idx;
 
@@ -640,8 +579,7 @@
    corresponding to this qualifier.  */
 
 static int
-code_for_qualifier (c)
-  int c;
+code_for_qualifier (int c)
 {
   switch (c)
     {
@@ -666,8 +604,7 @@
    TYPE_QUALS.  */
 
 static const char*
-qualifier_string (type_quals)
-     int type_quals;
+qualifier_string (int type_quals)
 {
   switch (type_quals)
     {
@@ -708,18 +645,13 @@
    called with a valid qualifier code.  */
 
 static const char*
-demangle_qualifier (c)
-  int c;
+demangle_qualifier (int c)
 {
   return qualifier_string (code_for_qualifier (c));
 }
 
-#if 0
 int
-cplus_demangle_opname (opname, result, options)
-     const char *opname;
-     char *result;
-     int options;
+cplus_demangle_opname (const char *opname, char *result, int options)
 {
   int len, len1, ret;
   string type;
@@ -844,7 +776,6 @@
   return ret;
 
 }
-#endif /* 0 */
 
 /* Takes operator name as e.g. "++" and returns mangled
    operator name (e.g. "postincrement_expr"), or NULL if not found.
@@ -852,11 +783,8 @@
    If OPTIONS & DMGL_ANSI == 1, return the ANSI name;
    if OPTIONS & DMGL_ANSI == 0, return the old GNU name.  */
 
-/*
 const char *
-cplus_mangle_opname (opname, options)
-     const char *opname;
-     int options;
+cplus_mangle_opname (const char *opname, int options)
 {
   size_t i;
   int len;
@@ -871,15 +799,12 @@
     }
   return (0);
 }
-*/
 
 /* Add a routine to set the demangling style to be sure it is valid and
    allow for any demangler initialization that maybe necessary. */
 
-/*
 enum demangling_styles
-cplus_demangle_set_style (style)
-     enum demangling_styles style;
+cplus_demangle_set_style (enum demangling_styles style)
 {
   const struct demangler_engine *demangler = libiberty_demanglers; 
 
@@ -892,14 +817,11 @@
 
   return unknown_demangling;
 }
-*/
 
 /* Do string name to style translation */
 
-/*
 enum demangling_styles
-cplus_demangle_name_to_style (name)
-     const char *name;
+cplus_demangle_name_to_style (const char *name)
 {
   const struct demangler_engine *demangler = libiberty_demanglers; 
 
@@ -909,7 +831,6 @@
 
   return unknown_demangling;
 }
-*/
 
 /* char *cplus_demangle (const char *mangled, int options)
 
@@ -940,15 +861,13 @@
    MANGLED.  */
 
 char *
-ML_(cplus_demangle) (mangled, options)
-     const char *mangled;
-     int options;
+ML_(cplus_demangle) (const char *mangled, int options)
 {
   char *ret;
   struct work_stuff work[1];
 
   if (current_demangling_style == no_demangling)
-    return xstrdup ("demangle.cd.1", mangled);
+    return xstrdup (mangled);
 
   memset ((char *) work, 0, sizeof (work));
   work->options = options;
@@ -958,14 +877,14 @@
   /* The V3 ABI demangling is implemented elsewhere.  */
   if (GNU_V3_DEMANGLING || AUTO_DEMANGLING)
     {
-      ret = ML_(cplus_demangle_v3) (mangled/*, work->options*/);
+      ret = cplus_demangle_v3 (mangled, work->options);
       if (ret || GNU_V3_DEMANGLING)
 	return ret;
     }
 
   if (JAVA_DEMANGLING)
     {
-      ret = ML_(java_demangle_v3) (mangled);
+      ret = java_demangle_v3 (mangled);
       if (ret)
         return ret;
     }
@@ -984,18 +903,14 @@
    updating *OLD_VECT and *SIZE as necessary.  */
 
 static void
-grow_vect (old_vect, size, min_size, element_size)
-     void **old_vect;
-     size_t *size;
-     size_t min_size;
-     int element_size;
+grow_vect (char **old_vect, size_t *size, size_t min_size, int element_size)
 {
   if (*size < min_size)
     {
       *size *= 2;
       if (*size < min_size)
 	*size = min_size;
-      *old_vect = xrealloc ("demangle.gv.1", *old_vect, *size * element_size);
+      *old_vect = XRESIZEVAR (char, *old_vect, *size * element_size);
     }
 }
 
@@ -1008,18 +923,14 @@
    The resulting string is valid until the next call of ada_demangle.  */
 
 static char *
-ada_demangle (mangled, option)
-     const char *mangled;
-     int option ATTRIBUTE_UNUSED;
+ada_demangle (const char *mangled, int option ATTRIBUTE_UNUSED)
 {
   int i, j;
   int len0;
   const char* p;
   char *demangled = NULL;
-  int at_start_name;
   int changed;
-  char *demangling_buffer = NULL;
-  size_t demangling_buffer_size = 0;
+  size_t demangled_size = 0;
   
   changed = 0;
 
@@ -1047,10 +958,9 @@
     }
   
   /* Make demangled big enough for possible expansion by operator name.  */
-  grow_vect ((void **) &(demangling_buffer),
-	     &demangling_buffer_size,  2 * len0 + 1,
+  grow_vect (&demangled,
+	     &demangled_size,  2 * len0 + 1,
 	     sizeof (char));
-  demangled = demangling_buffer;
   
   if (ISDIGIT ((unsigned char) mangled[len0 - 1])) {
     for (i = len0 - 2; i >= 0 && ISDIGIT ((unsigned char) mangled[i]); i -= 1)
@@ -1071,15 +981,12 @@
        i += 1, j += 1)
     demangled[j] = mangled[i];
   
-  at_start_name = 1;
   while (i < len0)
     {
-      at_start_name = 0;
-      
       if (i < len0 - 2 && mangled[i] == '_' && mangled[i + 1] == '_')
 	{
 	  demangled[j] = '.';
-	  changed = at_start_name = 1;
+	  changed = 1;
 	  i += 2; j += 1;
 	}
       else
@@ -1100,10 +1007,10 @@
     return demangled;
   
  Suppress:
-  grow_vect ((void **) &(demangling_buffer),
-	     &demangling_buffer_size,  strlen (mangled) + 3,
+  grow_vect (&demangled,
+	     &demangled_size,  strlen (mangled) + 3,
 	     sizeof (char));
-  demangled = demangling_buffer;
+
   if (mangled[0] == '<')
      strcpy (demangled, mangled);
   else
@@ -1119,9 +1026,7 @@
    calls go directly to this routine to avoid resetting that info. */
 
 static char *
-internal_cplus_demangle (work, mangled)
-     struct work_stuff *work;
-     const char *mangled;
+internal_cplus_demangle (struct work_stuff *work, const char *mangled)
 {
 
   string decl;
@@ -1186,8 +1091,7 @@
 
 /* Clear out and squangling related storage */
 static void
-squangle_mop_up (work)
-     struct work_stuff *work;
+squangle_mop_up (struct work_stuff *work)
 {
   /* clean up the B and K type mangling types. */
   forget_B_and_K_types (work);
@@ -1205,9 +1109,7 @@
 /* Copy the work state and storage.  */
 
 static void
-work_stuff_copy_to_from (to, from)
-     struct work_stuff *to;
-     struct work_stuff *from;
+work_stuff_copy_to_from (struct work_stuff *to, struct work_stuff *from)
 {
   int i;
 
@@ -1218,61 +1120,52 @@
 
   /* Deep-copy dynamic storage.  */
   if (from->typevec_size)
-    to->typevec
-      = (char **) xmalloc ("demangle.wsctf.1",
-                           from->typevec_size * sizeof (to->typevec[0]));
+    to->typevec = XNEWVEC (char *, from->typevec_size);
 
   for (i = 0; i < from->ntypes; i++)
     {
       int len = strlen (from->typevec[i]) + 1;
 
-      to->typevec[i] = xmalloc ("demangle.wsctf.2", len);
+      to->typevec[i] = XNEWVEC (char, len);
       memcpy (to->typevec[i], from->typevec[i], len);
     }
 
   if (from->ksize)
-    to->ktypevec
-      = (char **) xmalloc ("demangle.wsctf.3",
-                           from->ksize * sizeof (to->ktypevec[0]));
+    to->ktypevec = XNEWVEC (char *, from->ksize);
 
   for (i = 0; i < from->numk; i++)
     {
       int len = strlen (from->ktypevec[i]) + 1;
 
-      to->ktypevec[i] = xmalloc ("demangle.wsctf.4", len);
+      to->ktypevec[i] = XNEWVEC (char, len);
       memcpy (to->ktypevec[i], from->ktypevec[i], len);
     }
 
   if (from->bsize)
-    to->btypevec
-      = (char **) xmalloc ("demangle.wsctf.5",
-                           from->bsize * sizeof (to->btypevec[0]));
+    to->btypevec = XNEWVEC (char *, from->bsize);
 
   for (i = 0; i < from->numb; i++)
     {
       int len = strlen (from->btypevec[i]) + 1;
 
-      to->btypevec[i] = xmalloc ("demangle.wsctf.6", len);
+      to->btypevec[i] = XNEWVEC (char , len);
       memcpy (to->btypevec[i], from->btypevec[i], len);
     }
 
   if (from->ntmpl_args)
-    to->tmpl_argvec
-      = xmalloc ("demangle.wsctf.7",
-                 from->ntmpl_args * sizeof (to->tmpl_argvec[0]));
+    to->tmpl_argvec = XNEWVEC (char *, from->ntmpl_args);
 
   for (i = 0; i < from->ntmpl_args; i++)
     {
       int len = strlen (from->tmpl_argvec[i]) + 1;
 
-      to->tmpl_argvec[i] = xmalloc ("demangle.wsctf.8", len);
+      to->tmpl_argvec[i] = XNEWVEC (char, len);
       memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
     }
 
   if (from->previous_argument)
     {
-      to->previous_argument = (string*) xmalloc ("demangle.wsctf.9",
-                                                 sizeof (string));
+      to->previous_argument = XNEW (string);
       string_init (to->previous_argument);
       string_appends (to->previous_argument, from->previous_argument);
     }
@@ -1282,8 +1175,7 @@
 /* Delete dynamic stuff in work_stuff that is not to be re-used.  */
 
 static void
-delete_non_B_K_work_stuff (work)
-     struct work_stuff *work;
+delete_non_B_K_work_stuff (struct work_stuff *work)
 {
   /* Discard the remembered types, if any.  */
 
@@ -1316,8 +1208,7 @@
 
 /* Delete all dynamic storage in work_stuff.  */
 static void
-delete_work_stuff (work)
-     struct work_stuff *work;
+delete_work_stuff (struct work_stuff *work)
 {
   delete_non_B_K_work_stuff (work);
   squangle_mop_up (work);
@@ -1327,10 +1218,7 @@
 /* Clear out any mangled storage */
 
 static char *
-mop_up (work, declp, success)
-     struct work_stuff *work;
-     string *declp;
-     int success;
+mop_up (struct work_stuff *work, string *declp, int success)
 {
   char *demangled = NULL;
 
@@ -1382,10 +1270,8 @@
 	argument list.  */
 
 static int
-demangle_signature (work, mangled, declp)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
+demangle_signature (struct work_stuff *work,
+                    const char **mangled, string *declp)
 {
   int success = 1;
   int func_done = 0;
@@ -1485,6 +1371,7 @@
 	      {
 		string_append (&s, SCOPE_STRING (work));
 		string_prepends (declp, &s);
+		string_delete (&s);
 	      }
 	    oldmangled = NULL;
 	    expect_func = 1;
@@ -1564,7 +1451,6 @@
 	    {
 	      /* Read the return type. */
 	      string return_type;
-	      string_init (&return_type);
 
 	      (*mangled)++;
 	      success = do_type (work, mangled, &return_type);
@@ -1675,10 +1561,8 @@
 #if 0
 
 static int
-demangle_method_args (work, mangled, declp)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
+demangle_method_args (struct work_stuff *work, const char **mangled,
+                      string *declp)
 {
   int success = 0;
 
@@ -1698,10 +1582,8 @@
 #endif
 
 static int
-demangle_template_template_parm (work, mangled, tname)
-     struct work_stuff *work;
-     const char **mangled;
-     string *tname;
+demangle_template_template_parm (struct work_stuff *work,
+                                 const char **mangled, string *tname)
 {
   int i;
   int r;
@@ -1762,11 +1644,8 @@
 }
 
 static int
-demangle_expression (work, mangled, s, tk)
-     struct work_stuff *work;
-     const char** mangled;
-     string* s;
-     type_kind_t tk;
+demangle_expression (struct work_stuff *work, const char **mangled,
+                     string *s, type_kind_t tk)
 {
   int need_operator = 0;
   int success;
@@ -1785,7 +1664,7 @@
 
 	  len = strlen (*mangled);
 
-	  for (i = 0; i < (size_t)ARRAY_SIZE (optable); ++i)
+	  for (i = 0; i < ARRAY_SIZE (optable); ++i)
 	    {
 	      size_t l = strlen (optable[i].in);
 
@@ -1822,10 +1701,8 @@
 }
 
 static int
-demangle_integral_value (work, mangled, s)
-     struct work_stuff *work;
-     const char** mangled;
-     string* s;
+demangle_integral_value (struct work_stuff *work,
+                         const char **mangled, string *s)
 {
   int success;
 
@@ -1839,32 +1716,46 @@
 
       /* By default, we let the number decide whether we shall consume an
 	 underscore.  */
-      int consume_following_underscore = 0;
+      int multidigit_without_leading_underscore = 0;
       int leave_following_underscore = 0;
 
       success = 0;
 
-      /* Negative numbers are indicated with a leading `m'.  */
-      if (**mangled == 'm')
-	{
-	  string_appendn (s, "-", 1);
-	  (*mangled)++;
+      if (**mangled == '_')
+        {
+	  if (mangled[0][1] == 'm')
+	    {
+	      /* Since consume_count_with_underscores does not handle the
+		 `m'-prefix we must do it here, using consume_count and
+		 adjusting underscores: we have to consume the underscore
+		 matching the prepended one.  */
+	      multidigit_without_leading_underscore = 1;
+	      string_appendn (s, "-", 1);
+	      (*mangled) += 2;
+	    }
+	  else
+	    {
+	      /* Do not consume a following underscore;
+	         consume_count_with_underscores will consume what
+	         should be consumed.  */
+	      leave_following_underscore = 1;
+	    }
 	}
-      else if (mangled[0][0] == '_' && mangled[0][1] == 'm')
+      else
 	{
-	  /* Since consume_count_with_underscores does not handle the
-	     `m'-prefix we must do it here, using consume_count and
-	     adjusting underscores: we have to consume the underscore
-	     matching the prepended one.  */
-	  consume_following_underscore = 1;
-	  string_appendn (s, "-", 1);
-	  (*mangled) += 2;
-	}
-      else if (**mangled == '_')
-	{
-	  /* Do not consume a following underscore;
-	     consume_following_underscore will consume what should be
-	     consumed.  */
+	  /* Negative numbers are indicated with a leading `m'.  */
+	  if (**mangled == 'm')
+	  {
+	    string_appendn (s, "-", 1);
+	    (*mangled)++;
+	  }
+	  /* Since consume_count_with_underscores does not handle
+	     multi-digit numbers that do not start with an underscore,
+	     and this number can be an integer template parameter,
+	     we have to call consume_count. */
+	  multidigit_without_leading_underscore = 1;
+	  /* These multi-digit numbers never end on an underscore,
+	     so if there is one then don't eat it. */
 	  leave_following_underscore = 1;
 	}
 
@@ -1872,7 +1763,7 @@
 	 underscore, since consume_count_with_underscores expects
 	 the leading underscore (that we consumed) if it is to handle
 	 multi-digit numbers.  */
-      if (consume_following_underscore)
+      if (multidigit_without_leading_underscore)
 	value = consume_count (mangled);
       else
 	value = consume_count_with_underscores (mangled);
@@ -1890,7 +1781,7 @@
 	     is wrong.  If other (arbitrary) cases are followed by an
 	     underscore, we need to do something more radical.  */
 
-	  if ((value > 9 || consume_following_underscore)
+	  if ((value > 9 || multidigit_without_leading_underscore)
 	      && ! leave_following_underscore
 	      && **mangled == '_')
 	    (*mangled)++;
@@ -1898,7 +1789,7 @@
 	  /* All is well.  */
 	  success = 1;
 	}
-    }
+      }
 
   return success;
 }
@@ -1906,10 +1797,8 @@
 /* Demangle the real value in MANGLED.  */
 
 static int
-demangle_real_value (work, mangled, s)
-     struct work_stuff *work;
-     const char **mangled;
-     string* s;
+demangle_real_value (struct work_stuff *work,
+                     const char **mangled, string *s)
 {
   if (**mangled == 'E')
     return demangle_expression (work, mangled, s, tk_real);
@@ -1949,11 +1838,8 @@
 }
 
 static int
-demangle_template_value_parm (work, mangled, s, tk)
-     struct work_stuff *work;
-     const char **mangled;
-     string* s;
-     type_kind_t tk;
+demangle_template_value_parm (struct work_stuff *work, const char **mangled,
+                              string *s, type_kind_t tk)
 {
   int success = 1;
 
@@ -2023,7 +1909,7 @@
 	    string_appendn (s, "0", 1);
 	  else
 	    {
-	      char *p = xmalloc ("demangle.dtvp.1", symbol_len + 1), *q;
+	      char *p = XNEWVEC (char, symbol_len + 1), *q;
 	      strncpy (p, *mangled, symbol_len);
 	      p [symbol_len] = '\0';
 	      /* We use cplus_demangle here, rather than
@@ -2061,29 +1947,20 @@
    types.  */
 
 static int
-demangle_template (work, mangled, tname, trawname, is_type, remember)
-     struct work_stuff *work;
-     const char **mangled;
-     string *tname;
-     string *trawname;
-     int is_type;
-     int remember;
+demangle_template (struct work_stuff *work, const char **mangled,
+                   string *tname, string *trawname,
+                   int is_type, int remember)
 {
   int i;
   int r;
   int need_comma = 0;
   int success = 0;
-  const char *start;
   int is_java_array = 0;
   string temp;
-  int bindex = 0;
 
   (*mangled)++;
   if (is_type)
     {
-      if (remember)
-	bindex = register_Btype (work);
-      start = *mangled;
       /* get template name */
       if (**mangled == 'z')
 	{
@@ -2138,8 +2015,7 @@
   if (!is_type)
     {
       /* Create an array for saving the template argument values. */
-      work->tmpl_argvec = (char**) xmalloc ("demangle.dt.1",
-                                            r * sizeof (char *));
+      work->tmpl_argvec = XNEWVEC (char *, r);
       work->ntmpl_args = r;
       for (i = 0; i < r; i++)
 	work->tmpl_argvec[i] = 0;
@@ -2164,7 +2040,7 @@
 		{
 		  /* Save the template argument. */
 		  int len = temp.p - temp.b;
-		  work->tmpl_argvec[i] = xmalloc ("demangle.dt.2", len + 1);
+		  work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
 		  memcpy (work->tmpl_argvec[i], temp.b, len);
 		  work->tmpl_argvec[i][len] = '\0';
 		}
@@ -2192,7 +2068,7 @@
 		{
 		  /* Save the template argument. */
 		  int len = r2;
-		  work->tmpl_argvec[i] = xmalloc ("demangle.dt.3", len + 1);
+		  work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
 		  memcpy (work->tmpl_argvec[i], *mangled, len);
 		  work->tmpl_argvec[i][len] = '\0';
 		}
@@ -2238,7 +2114,7 @@
 	  if (!is_type)
 	    {
 	      int len = s->p - s->b;
-	      work->tmpl_argvec[i] = xmalloc ("demangle.dt.4", len + 1);
+	      work->tmpl_argvec[i] = XNEWVEC (char, len + 1);
 	      memcpy (work->tmpl_argvec[i], s->b, len);
 	      work->tmpl_argvec[i][len] = '\0';
 
@@ -2260,7 +2136,10 @@
     }
 
   if (is_type && remember)
-    remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
+    {
+      const int bindex = register_Btype (work);
+      remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
+    }
 
   /*
     if (work -> static_type)
@@ -2279,11 +2158,8 @@
 }
 
 static int
-arm_pt (work, mangled, n, anchor, args)
-     struct work_stuff *work;
-     const char *mangled;
-     int n;
-     const char **anchor, **args;
+arm_pt (struct work_stuff *work, const char *mangled,
+        int n, const char **anchor, const char **args)
 {
   /* Check if ARM template with "__pt__" in it ("parameterized type") */
   /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
@@ -2336,11 +2212,8 @@
 }
 
 static void
-demangle_arm_hp_template (work, mangled, n, declp)
-     struct work_stuff *work;
-     const char **mangled;
-     int n;
-     string *declp;
+demangle_arm_hp_template (struct work_stuff *work, const char **mangled,
+                          int n, string *declp)
 {
   const char *p;
   const char *args;
@@ -2352,6 +2225,7 @@
   if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
     {
       char *start_spec_args = NULL;
+      int hold_options;
 
       /* First check for and omit template specialization pseudo-arguments,
          such as in "Spec<#1,#1.*>" */
@@ -2364,10 +2238,16 @@
       string_init (&arg);
       if (work->temp_start == -1) /* non-recursive call */
         work->temp_start = declp->p - declp->b;
+
+      /* We want to unconditionally demangle parameter types in
+	 template parameters.  */
+      hold_options = work->options;
+      work->options |= DMGL_PARAMS;
+
       string_append (declp, "<");
       while (1)
         {
-          string_clear (&arg);
+          string_delete (&arg);
           switch (**mangled)
             {
               case 'T':
@@ -2410,21 +2290,29 @@
       string_delete (&arg);
       if (**mangled == '_')
         (*mangled)++;
+      work->options = hold_options;
       return;
     }
   /* ARM template? (Also handles HP cfront extensions) */
   else if (arm_pt (work, *mangled, n, &p, &args))
     {
+      int hold_options;
       string type_str;
 
       string_init (&arg);
       string_appendn (declp, *mangled, p - *mangled);
       if (work->temp_start == -1)  /* non-recursive call */
 	work->temp_start = declp->p - declp->b;
+
+      /* We want to unconditionally demangle parameter types in
+	 template parameters.  */
+      hold_options = work->options;
+      work->options |= DMGL_PARAMS;
+
       string_append (declp, "<");
       /* should do error checking here */
       while (args < e) {
-	string_clear (&arg);
+	string_delete (&arg);
 
 	/* Check for type or literal here */
 	switch (*args)
@@ -2439,6 +2327,7 @@
 	      goto cfront_template_args_done;
             string_append (&arg, "(");
             string_appends (&arg, &type_str);
+            string_delete (&type_str);
             string_append (&arg, ")");
             if (*args != 'L')
               goto cfront_template_args_done;
@@ -2456,8 +2345,18 @@
             break;
           default:
             /* Not handling other HP cfront stuff */
-            if (!do_type (work, &args, &arg))
-              goto cfront_template_args_done;
+            {
+              const char* old_args = args;
+              if (!do_type (work, &args, &arg))
+                goto cfront_template_args_done;
+
+              /* Fail if we didn't make any progress: prevent infinite loop. */
+              if (args == old_args)
+		{
+		  work->options = hold_options;
+		  return;
+		}
+            }
 	  }
 	string_appends (declp, &arg);
 	string_append (declp, ",");
@@ -2467,6 +2366,7 @@
       if (args >= e)
 	--declp->p; /* remove extra comma */
       string_append (declp, ">");
+      work->options = hold_options;
     }
   else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
 	   && (*mangled)[9] == 'N'
@@ -2490,10 +2390,8 @@
    already been dealt with */
 
 static int
-demangle_class_name (work, mangled, declp)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
+demangle_class_name (struct work_stuff *work, const char **mangled,
+                     string *declp)
 {
   int n;
   int success = 0;
@@ -2546,10 +2444,7 @@
 */
 
 static int
-demangle_class (work, mangled, declp)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
+demangle_class (struct work_stuff *work, const char **mangled, string *declp)
 {
   int success = 0;
   int btype;
@@ -2601,11 +2496,8 @@
    demangle_signature.  */
 
 static int
-iterate_demangle_function (work, mangled, declp, scan)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
-     const char *scan;
+iterate_demangle_function (struct work_stuff *work, const char **mangled,
+                           string *declp, const char *scan)
 {
   const char *mangle_init = *mangled;
   int success = 0;
@@ -2619,10 +2511,7 @@
      "__"-sequence.  This is the normal case.  */
   if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
       || strstr (scan + 2, "__") == NULL)
-    {
-      demangle_function_name (work, mangled, declp, scan);
-      return 1;
-    }
+    return demangle_function_name (work, mangled, declp, scan);
 
   /* Save state so we can restart if the guess at the correct "__" was
      wrong.  */
@@ -2634,15 +2523,17 @@
   /* Iterate over occurrences of __, allowing names and types to have a
      "__" sequence in them.  We must start with the first (not the last)
      occurrence, since "__" most often occur between independent mangled
-     parts, hence starting at the last occurrence inside a signature
+     parts, hence starting at the last occurence inside a signature
      might get us a "successful" demangling of the signature.  */
 
   while (scan[2])
     {
-      demangle_function_name (work, mangled, declp, scan);
-      success = demangle_signature (work, mangled, declp);
-      if (success)
-	break;
+      if (demangle_function_name (work, mangled, declp, scan))
+	{
+	  success = demangle_signature (work, mangled, declp);
+	  if (success)
+	    break;
+	}
 
       /* Reset demangle state for the next round.  */
       *mangled = mangle_init;
@@ -2704,10 +2595,8 @@
  */
 
 static int
-demangle_prefix (work, mangled, declp)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
+demangle_prefix (struct work_stuff *work, const char **mangled,
+                 string *declp)
 {
   int success = 1;
   const char *scan;
@@ -2777,9 +2666,7 @@
     {
       /* We found a sequence of two or more '_', ensure that we start at
 	 the last pair in the sequence.  */
-      /* i = strspn (scan, "_"); */
-      i = 0;
-      while (scan[i] == '_') i++;
+      i = strspn (scan, "_");
       if (i > 2)
 	{
 	  scan += (i - 2);
@@ -2917,10 +2804,7 @@
  */
 
 static int
-gnu_special (work, mangled, declp)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
+gnu_special (struct work_stuff *work, const char **mangled, string *declp)
 {
   int n;
   int success = 1;
@@ -2979,14 +2863,7 @@
 		}
 	      else
 		{
-		  /*n = strcspn (*mangled, cplus_markers);*/
-		  const char *check = *mangled;
-		  n = 0;
-		  while (*check)
-		    if (strchr (cplus_markers, *check++) == NULL)
-		      n++;
-		    else
-		      break;
+		  n = strcspn (*mangled, cplus_markers);
 		}
 	      string_appendn (declp, *mangled, n);
 	      (*mangled) += n;
@@ -3128,16 +3005,13 @@
 }
 
 static void
-recursively_demangle(work, mangled, result, namelength)
-     struct work_stuff *work;
-     const char **mangled;
-     string *result;
-     int namelength;
+recursively_demangle(struct work_stuff *work, const char **mangled,
+                     string *result, int namelength)
 {
   char * recurse = (char *)NULL;
   char * recurse_dem = (char *)NULL;
 
-  recurse = (char *) xmalloc ("demangle.rd.1", namelength + 1);
+  recurse = XNEWVEC (char, namelength + 1);
   memcpy (recurse, *mangled, namelength);
   recurse[namelength] = '\000';
 
@@ -3180,9 +3054,7 @@
  */
 
 static int
-arm_special (mangled, declp)
-     const char **mangled;
-     string *declp;
+arm_special (const char **mangled, string *declp)
 {
   int n;
   int success = 1;
@@ -3265,15 +3137,12 @@
  */
 
 static int
-demangle_qualified (work, mangled, result, isfuncname, append)
-     struct work_stuff *work;
-     const char **mangled;
-     string *result;
-     int isfuncname;
-     int append;
+demangle_qualified (struct work_stuff *work, const char **mangled,
+                    string *result, int isfuncname, int append)
 {
   int qualifiers = 0;
   int success = 1;
+  char num[2];
   string temp;
   string last_name;
   int bindex = register_Btype (work);
@@ -3320,7 +3189,9 @@
     case '8':
     case '9':
       /* The count is in a single digit.  */
-      qualifiers = (*mangled)[1] - '0';
+      num[0] = (*mangled)[1];
+      num[1] = '\0';
+      qualifiers = atoi (num);
 
       /* If there is an underscore after the digit, skip it.  This is
 	 said to be for ARM-qualified names, but the ARM makes no
@@ -3338,11 +3209,7 @@
     }
 
   if (!success)
-    {
-      string_delete (&last_name);
-      string_delete (&temp);
-      return success;
-    }
+    return success;
 
   /* Pick off the names and collect them in the temp buffer in the order
      in which they are found, separated by '::'.  */
@@ -3399,17 +3266,11 @@
             }
           else
             {
-	      string temp_last_name;
-	      string_init (&temp_last_name);
-              success = do_type (work, mangled, &temp_last_name);
+              string_delete (&last_name);
+              success = do_type (work, mangled, &last_name);
               if (!success)
-	        {
-		  string_delete (&temp_last_name);
-                  break;
-		}
-              string_appends (&temp, &temp_last_name);
-	      string_appends (&last_name, &temp_last_name);
-	      string_delete (&temp_last_name);
+                break;
+              string_appends (&temp, &last_name);
             }
 	}
 
@@ -3499,9 +3360,7 @@
 */
 
 static int
-get_count (type, count)
-     const char **type;
-     int *count;
+get_count (const char **type, int *count)
 {
   const char *p;
   int n;
@@ -3537,10 +3396,7 @@
    value returned is really a type_kind_t.  */
 
 static int
-do_type (work, mangled, result)
-     struct work_stuff *work;
-     const char **mangled;
-     string *result;
+do_type (struct work_stuff *work, const char **mangled, string *result)
 {
   int n;
   int done;
@@ -3548,10 +3404,8 @@
   string decl;
   const char *remembered_type;
   int type_quals;
-  string btype;
   type_kind_t tk = tk_none;
 
-  string_init (&btype);
   string_init (&decl);
   string_init (result);
 
@@ -3669,6 +3523,7 @@
 		string temp;
 		do_type (work, mangled, &temp);
 		string_prepends (&decl, &temp);
+		string_delete (&temp);
 	      }
 	    else if (**mangled == 't')
 	      {
@@ -3679,7 +3534,7 @@
 		if (success)
 		  {
 		    string_prependn (&decl, temp.b, temp.p - temp.b);
-		    string_clear (&temp);
+		    string_delete (&temp);
 		  }
 		else
 		  break;
@@ -3850,20 +3705,15 @@
    The value returned is really a type_kind_t.  */
 
 static int
-demangle_fund_type (work, mangled, result)
-     struct work_stuff *work;
-     const char **mangled;
-     string *result;
+demangle_fund_type (struct work_stuff *work,
+                    const char **mangled, string *result)
 {
   int done = 0;
   int success = 1;
-  char buf[10];
-  //unsigned int dec = 0;     // unused
-  string btype;
+  char buf[INTBUF_SIZE + 5 /* 'int%u_t' */];
+  /* unsigned int dec = 0; */ /* JRS 2008-Oct-26: unused (see below) */
   type_kind_t tk = tk_integral;
 
-  string_init (&btype);
-
   /* First pick off any type qualifiers.  There can be more than one.  */
 
   while (!done)
@@ -4001,9 +3851,15 @@
 	  buf[2] = '\0';
 	  *mangled += min (strlen (*mangled), 2);
 	}
+      /* JRS 2008-Oct-26: the next two commented out lines have been
+         replaced by the sprintf that follows.  This is to avoid use
+         of sscanf.  This hack is merely copied from the old demangler
+         port (by Michael Matz, Simon Hausmann?) -- I have no idea if
+         it is really correct/safe, but it looks ok. */
       /*sscanf (buf, "%x", &dec);
       sprintf (buf, "int%u_t", dec);*/
-      sprintf (buf, "i_xx_t");
+      sprintf (buf, "%s", "intXX_t");
+      /* end JRS 2008-Oct-26 */
       APPEND_BLANK (result);
       string_append (result, buf);
       break;
@@ -4022,22 +3878,25 @@
     case '9':
       {
         int bindex = register_Btype (work);
-        string loc_btype;
-        string_init (&loc_btype);
-        if (demangle_class_name (work, mangled, &loc_btype)) {
-          remember_Btype (work, loc_btype.b, LEN_STRING (&loc_btype), bindex);
+        string btype;
+        string_init (&btype);
+        if (demangle_class_name (work, mangled, &btype)) {
+          remember_Btype (work, btype.b, LEN_STRING (&btype), bindex);
           APPEND_BLANK (result);
-          string_appends (result, &loc_btype);
+          string_appends (result, &btype);
         }
         else
           success = 0;
-        string_delete (&loc_btype);
+        string_delete (&btype);
         break;
       }
     case 't':
       {
+        string btype;
+        string_init (&btype);
         success = demangle_template (work, mangled, &btype, 0, 1, 1);
         string_appends (result, &btype);
+        string_delete (&btype);
         break;
       }
     default:
@@ -4045,8 +3904,6 @@
       break;
     }
 
-  string_delete (&btype);
-
   return success ? ((int) tk) : 0;
 }
 
@@ -4055,10 +3912,8 @@
    **mangled points to 'S' or 'U' */
 
 static int
-do_hpacc_template_const_value (work, mangled, result)
-     struct work_stuff *work ATTRIBUTE_UNUSED;
-     const char **mangled;
-     string *result;
+do_hpacc_template_const_value (struct work_stuff *work ATTRIBUTE_UNUSED,
+                               const char **mangled, string *result)
 {
   int unsigned_const;
 
@@ -4112,10 +3967,8 @@
    **mangled is pointing to the 'A' */
 
 static int
-do_hpacc_template_literal (work, mangled, result)
-     struct work_stuff *work;
-     const char **mangled;
-     string *result;
+do_hpacc_template_literal (struct work_stuff *work, const char **mangled,
+                           string *result)
 {
   int literal_len = 0;
   char * recurse;
@@ -4136,7 +3989,7 @@
   string_append (result, "&");
 
   /* Now recursively demangle the literal name */
-  recurse = (char *) xmalloc ("demangle.dhtl.1", literal_len + 1);
+  recurse = XNEWVEC (char, literal_len + 1);
   memcpy (recurse, *mangled, literal_len);
   recurse[literal_len] = '\000';
 
@@ -4158,9 +4011,7 @@
 }
 
 static int
-snarf_numeric_literal (args, arg)
-     const char ** args;
-     string * arg;
+snarf_numeric_literal (const char **args, string *arg)
 {
   if (**args == '-')
     {
@@ -4189,18 +4040,13 @@
    and free'd should anything go wrong.  */
 
 static int
-do_arg (work, mangled, result)
-     struct work_stuff *work;
-     const char **mangled;
-     string *result;
+do_arg (struct work_stuff *work, const char **mangled, string *result)
 {
   /* Remember where we started so that we can record the type, for
      non-squangling type remembering.  */
   const char *start = *mangled;
-  string temp_result;
 
   string_init (result);
-  string_init (&temp_result);
 
   if (work->nrepeats > 0)
     {
@@ -4243,21 +4089,12 @@
      do not want to add additional types to the back-referenceable
      type vector when processing a repeated type.  */
   if (work->previous_argument)
-    string_clear (work->previous_argument);
+    string_delete (work->previous_argument);
   else
-    {
-      work->previous_argument = (string*) xmalloc ("demangle.da.1",
-                                                   sizeof (string));
-      string_init (work->previous_argument);
-    }
+    work->previous_argument = XNEW (string);
 
-  if (!do_type (work, mangled, &temp_result))
-    {
-      string_delete (&temp_result);
-      return 0;
-    }
-  string_appends (work->previous_argument, &temp_result);
-  string_delete (&temp_result);
+  if (!do_type (work, mangled, work->previous_argument))
+    return 0;
 
   string_appends (result, work->previous_argument);
 
@@ -4266,10 +4103,7 @@
 }
 
 static void
-remember_type (work, start, len)
-     struct work_stuff *work;
-     const char *start;
-     int len;
+remember_type (struct work_stuff *work, const char *start, int len)
 {
   char *tem;
 
@@ -4281,19 +4115,16 @@
       if (work -> typevec_size == 0)
 	{
 	  work -> typevec_size = 3;
-	  work -> typevec
-	    = (char **) xmalloc ("demangle.rt.1",
-                                 sizeof (char *) * work -> typevec_size);
+	  work -> typevec = XNEWVEC (char *, work->typevec_size);
 	}
       else
 	{
 	  work -> typevec_size *= 2;
 	  work -> typevec
-	    = (char **) xrealloc ("demangle.rt.2", (char *)work -> typevec,
-				  sizeof (char *) * work -> typevec_size);
+	    = XRESIZEVEC (char *, work->typevec, work->typevec_size);
 	}
     }
-  tem = xmalloc ("demangle.rt.3", len + 1);
+  tem = XNEWVEC (char, len + 1);
   memcpy (tem, start, len);
   tem[len] = '\0';
   work -> typevec[work -> ntypes++] = tem;
@@ -4302,10 +4133,7 @@
 
 /* Remember a K type class qualifier. */
 static void
-remember_Ktype (work, start, len)
-     struct work_stuff *work;
-     const char *start;
-     int len;
+remember_Ktype (struct work_stuff *work, const char *start, int len)
 {
   char *tem;
 
@@ -4314,19 +4142,16 @@
       if (work -> ksize == 0)
 	{
 	  work -> ksize = 5;
-	  work -> ktypevec
-	    = (char **) xmalloc ("demangle.rK.1",
-                                 sizeof (char *) * work -> ksize);
+	  work -> ktypevec = XNEWVEC (char *, work->ksize);
 	}
       else
 	{
 	  work -> ksize *= 2;
 	  work -> ktypevec
-	    = (char **) xrealloc ("demangle.rK.2", (char *)work -> ktypevec,
-				  sizeof (char *) * work -> ksize);
+	    = XRESIZEVEC (char *, work->ktypevec, work->ksize);
 	}
     }
-  tem = xmalloc ("demangle.rK.3", len + 1);
+  tem = XNEWVEC (char, len + 1);
   memcpy (tem, start, len);
   tem[len] = '\0';
   work -> ktypevec[work -> numk++] = tem;
@@ -4337,8 +4162,7 @@
    registers map<temp<char> > as B0, and temp<char> as B1 */
 
 static int
-register_Btype (work)
-     struct work_stuff *work;
+register_Btype (struct work_stuff *work)
 {
   int ret;
 
@@ -4347,16 +4171,13 @@
       if (work -> bsize == 0)
 	{
 	  work -> bsize = 5;
-	  work -> btypevec
-	    = (char **) xmalloc ("demangle.rB.1", 
-                                 sizeof (char *) * work -> bsize);
+	  work -> btypevec = XNEWVEC (char *, work->bsize);
 	}
       else
 	{
 	  work -> bsize *= 2;
 	  work -> btypevec
-	    = (char **) xrealloc ("demangle.rB.2", (char *)work -> btypevec,
-				  sizeof (char *) * work -> bsize);
+	    = XRESIZEVEC (char *, work->btypevec, work->bsize);
 	}
     }
   ret = work -> numb++;
@@ -4367,23 +4188,20 @@
 /* Store a value into a previously registered B code type. */
 
 static void
-remember_Btype (work, start, len, ind)
-     struct work_stuff *work;
-     const char *start;
-     int len, ind;
+remember_Btype (struct work_stuff *work, const char *start,
+                int len, int index)
 {
   char *tem;
 
-  tem = xmalloc ("demangle.remember_Btype.1", len + 1);
+  tem = XNEWVEC (char, len + 1);
   memcpy (tem, start, len);
   tem[len] = '\0';
-  work -> btypevec[ind] = tem;
+  work -> btypevec[index] = tem;
 }
 
 /* Lose all the info related to B and K type codes. */
 static void
-forget_B_and_K_types (work)
-     struct work_stuff *work;
+forget_B_and_K_types (struct work_stuff *work)
 {
   int i;
 
@@ -4410,8 +4228,7 @@
 /* Forget the remembered types, but not the type vector itself.  */
 
 static void
-forget_types (work)
-     struct work_stuff *work;
+forget_types (struct work_stuff *work)
 {
   int i;
 
@@ -4469,10 +4286,8 @@
  */
 
 static int
-demangle_args (work, mangled, declp)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
+demangle_args (struct work_stuff *work, const char **mangled,
+               string *declp)
 {
   string arg;
   int need_comma = 0;
@@ -4562,10 +4377,7 @@
 	  if (need_comma && PRINT_ARG_TYPES)
 	    string_append (declp, ", ");
 	  if (!do_arg (work, mangled, &arg))
-	    {
-	      string_delete (&arg);
-	      return (0);
-	    }
+	    return (0);
 	  if (PRINT_ARG_TYPES)
 	    string_appends (declp, &arg);
 	  string_delete (&arg);
@@ -4597,10 +4409,8 @@
    and method pointers or references, not top-level declarations.  */
 
 static int
-demangle_nested_args (work, mangled, declp)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
+demangle_nested_args (struct work_stuff *work, const char **mangled,
+                      string *declp)
 {
   string* saved_previous_argument;
   int result;
@@ -4626,7 +4436,7 @@
   if (work->previous_argument)
     {
       string_delete (work->previous_argument);
-      free ((char*) work->previous_argument);
+      free ((char *) work->previous_argument);
     }
   work->previous_argument = saved_previous_argument;
   --work->forgetting_types;
@@ -4635,12 +4445,11 @@
   return result;
 }
 
-static void
-demangle_function_name (work, mangled, declp, scan)
-     struct work_stuff *work;
-     const char **mangled;
-     string *declp;
-     const char *scan;
+/* Returns 1 if a valid function name was found or 0 otherwise.  */
+
+static int 
+demangle_function_name (struct work_stuff *work, const char **mangled,
+                        string *declp, const char *scan)
 {
   size_t i;
   string type;
@@ -4678,13 +4487,13 @@
 	{
 	  work -> constructor += 1;
 	  string_clear (declp);
-	  return;
+	  return 1;
 	}
       else if (strcmp (declp -> b, "__dt") == 0)
 	{
 	  work -> destructor += 1;
 	  string_clear (declp);
-	  return;
+	  return 1;
 	}
     }
 
@@ -4697,7 +4506,7 @@
       if (declp->p - declp->b >= 10 /* op$assign_ */
 	  && memcmp (declp->b + 3, "assign_", 7) == 0)
 	{
-	  for (i = 0; i < (size_t)ARRAY_SIZE (optable); i++)
+	  for (i = 0; i < ARRAY_SIZE (optable); i++)
 	    {
 	      int len = declp->p - declp->b - 10;
 	      if ((int) strlen (optable[i].in) == len
@@ -4713,7 +4522,7 @@
 	}
       else
 	{
-	  for (i = 0; i < (size_t)ARRAY_SIZE (optable); i++)
+	  for (i = 0; i < ARRAY_SIZE (optable); i++)
 	    {
 	      int len = declp->p - declp->b - 3;
 	      if ((int) strlen (optable[i].in) == len
@@ -4761,7 +4570,7 @@
       if (declp->b[4] == '\0')
 	{
 	  /* Operator.  */
-	  for (i = 0; i < (size_t)ARRAY_SIZE (optable); i++)
+	  for (i = 0; i < ARRAY_SIZE (optable); i++)
 	    {
 	      if (strlen (optable[i].in) == 2
 		  && memcmp (optable[i].in, declp->b + 2, 2) == 0)
@@ -4773,28 +4582,12 @@
 		}
 	    }
 	}
-
-      /* BEGIN hack inserted 20050403 by JRS to deal with apparently
-         non-cfront compliant new[]/delete[] manglings generated by
-         the Portland Group's C++ compiler. */
-      else 
-      if (strcmp (declp -> b, "__nwa") == 0) {
-         string_clear (declp);
-         string_append (declp, "operator new[]");
-      }
-      else 
-      if (strcmp (declp -> b, "__dla") == 0) {
-         string_clear (declp);
-         string_append (declp, "operator delete[]");
-      }
-      /* END hack */
-
       else
 	{
 	  if (declp->b[2] == 'a' && declp->b[5] == '\0')
 	    {
 	      /* Assignment.  */
-	      for (i = 0; i < (size_t)ARRAY_SIZE (optable); i++)
+	      for (i = 0; i < ARRAY_SIZE (optable); i++)
 		{
 		  if (strlen (optable[i].in) == 3
 		      && memcmp (optable[i].in, declp->b + 2, 3) == 0)
@@ -4808,14 +4601,19 @@
 	    }
 	}
     }
+
+  /* If a function name was obtained but it's not valid, we were not
+     successful.  */
+  if (LEN_STRING (declp) == 1 && declp->b[0] == '.')
+    return 0;
+  else
+    return 1;
 }
 
 /* a mini string-handling package */
 
 static void
-string_need (s, n)
-     string *s;
-     int n;
+string_need (string *s, int n)
 {
   int tem;
 
@@ -4825,7 +4623,7 @@
 	{
 	  n = 32;
 	}
-      s->p = s->b = xmalloc ("demangle.sn.1", n);
+      s->p = s->b = XNEWVEC (char, n);
       s->e = s->b + n;
     }
   else if (s->e - s->p < n)
@@ -4833,15 +4631,14 @@
       tem = s->p - s->b;
       n += tem;
       n *= 2;
-      s->b = xrealloc ("demangle.sn.2", s->b, n);
+      s->b = XRESIZEVEC (char, s->b, n);
       s->p = s->b + tem;
       s->e = s->b + n;
     }
 }
 
 static void
-string_delete (s)
-     string *s;
+string_delete (string *s)
 {
   if (s->b != NULL)
     {
@@ -4851,15 +4648,13 @@
 }
 
 static void
-string_init (s)
-     string *s;
+string_init (string *s)
 {
   s->b = s->p = s->e = NULL;
 }
 
 static void
-string_clear (s)
-     string *s;
+string_clear (string *s)
 {
   s->p = s->b;
 }
@@ -4867,8 +4662,7 @@
 #if 0
 
 static int
-string_empty (s)
-     string *s;
+string_empty (string *s)
 {
   return (s->b == s->p);
 }
@@ -4876,9 +4670,7 @@
 #endif
 
 static void
-string_append (p, s)
-     string *p;
-     const char *s;
+string_append (string *p, const char *s)
 {
   int n;
   if (s == NULL || *s == '\0')
@@ -4890,8 +4682,7 @@
 }
 
 static void
-string_appends (p, s)
-     string *p, *s;
+string_appends (string *p, string *s)
 {
   int n;
 
@@ -4905,10 +4696,7 @@
 }
 
 static void
-string_appendn (p, s, n)
-     string *p;
-     const char *s;
-     int n;
+string_appendn (string *p, const char *s, int n)
 {
   if (n != 0)
     {
@@ -4919,9 +4707,7 @@
 }
 
 static void
-string_prepend (p, s)
-     string *p;
-     const char *s;
+string_prepend (string *p, const char *s)
 {
   if (s != NULL && *s != '\0')
     {
@@ -4930,8 +4716,7 @@
 }
 
 static void
-string_prepends (p, s)
-     string *p, *s;
+string_prepends (string *p, string *s)
 {
   if (s->b != s->p)
     {
@@ -4940,10 +4725,7 @@
 }
 
 static void
-string_prependn (p, s, n)
-     string *p;
-     const char *s;
-     int n;
+string_prependn (string *p, const char *s, int n)
 {
   char *q;
 
@@ -4960,345 +4742,9 @@
 }
 
 static void
-string_append_template_idx (s, idx)
-     string *s;
-     int idx;
+string_append_template_idx (string *s, int idx)
 {
   char buf[INTBUF_SIZE + 1 /* 'T' */];
   sprintf(buf, "T%d", idx);
   string_append (s, buf);
 }
-
-/* To generate a standalone demangler program for testing purposes,
-   just compile and link this file with -DMAIN and libiberty.a.  When
-   run, it demangles each command line arg, or each stdin string, and
-   prints the result on stdout.  */
-
-#ifdef MAIN
-
-#include "getopt.h"
-
-static const char *program_name;
-static const char *program_version = VERSION;
-static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE;
-
-static void demangle_it PARAMS ((char *));
-static void usage PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN;
-static void fatal PARAMS ((const char *)) ATTRIBUTE_NORETURN;
-static void print_demangler_list PARAMS ((FILE *));
-
-static void
-demangle_it (mangled_name)
-     char *mangled_name;
-{
-  char *result;
-
-  /* For command line args, also try to demangle type encodings.  */
-  result = cplus_demangle (mangled_name, flags | DMGL_TYPES);
-  if (result == NULL)
-    {
-      printf ("%s\n", mangled_name);
-    }
-  else
-    {
-      printf ("%s\n", result);
-      free (result);
-    }
-}
-
-static void 
-print_demangler_list (stream)
-     FILE *stream;
-{
-  const struct demangler_engine *demangler; 
-
-  fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
-  
-  for (demangler = libiberty_demanglers + 1;
-       demangler->demangling_style != unknown_demangling;
-       ++demangler)
-    fprintf (stream, ",%s", demangler->demangling_style_name);
-
-  fprintf (stream, "}");
-}
-
-static void
-usage (stream, status)
-     FILE *stream;
-     int status;
-{
-  fprintf (stream, "\
-Usage: %s [-_] [-n] [--strip-underscores] [--no-strip-underscores] \n",
-	   program_name);
-
-  fprintf (stream, "\
-       [-s ");
-  print_demangler_list (stream);
-  fprintf (stream, "]\n");
-
-  fprintf (stream, "\
-       [--format ");
-  print_demangler_list (stream);
-  fprintf (stream, "]\n");
-
-  fprintf (stream, "\
-       [--help] [--version] [arg...]\n");
-  exit (status);
-}
-
-#define MBUF_SIZE 32767
-char mbuffer[MBUF_SIZE];
-
-/* Defined in the automatically-generated underscore.c.  */
-extern int prepends_underscore;
-
-int strip_underscore = 0;
-
-static const struct option long_options[] = {
-  {"strip-underscores", no_argument, 0, '_'},
-  {"format", required_argument, 0, 's'},
-  {"help", no_argument, 0, 'h'},
-  {"no-strip-underscores", no_argument, 0, 'n'},
-  {"version", no_argument, 0, 'v'},
-  {0, no_argument, 0, 0}
-};
-
-/* More 'friendly' abort that prints the line and file.
-   config.h can #define abort fancy_abort if you like that sort of thing.  */
-
-void
-fancy_abort ()
-{
-  fatal ("Internal gcc abort.");
-}
-
-
-static const char *
-standard_symbol_characters PARAMS ((void));
-
-static const char *
-hp_symbol_characters PARAMS ((void));
-
-static const char *
-gnu_v3_symbol_characters PARAMS ((void));
-
-/* Return the string of non-alnum characters that may occur 
-   as a valid symbol component, in the standard assembler symbol
-   syntax.  */
-
-static const char *
-standard_symbol_characters ()
-{
-  return "_$.";
-}
-
-
-/* Return the string of non-alnum characters that may occur
-   as a valid symbol name component in an HP object file.
-
-   Note that, since HP's compiler generates object code straight from
-   C++ source, without going through an assembler, its mangled
-   identifiers can use all sorts of characters that no assembler would
-   tolerate, so the alphabet this function creates is a little odd.
-   Here are some sample mangled identifiers offered by HP:
-
-	typeid*__XT24AddressIndExpClassMember_
-	[Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
-	__ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
-
-   This still seems really weird to me, since nowhere else in this
-   file is there anything to recognize curly brackets, parens, etc.
-   I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
-   this is right, but I still strongly suspect that there's a
-   misunderstanding here.
-
-   If we decide it's better for c++filt to use HP's assembler syntax
-   to scrape identifiers out of its input, here's the definition of
-   the symbol name syntax from the HP assembler manual:
-
-       Symbols are composed of uppercase and lowercase letters, decimal
-       digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
-       underscore (_). A symbol can begin with a letter, digit underscore or
-       dollar sign. If a symbol begins with a digit, it must contain a
-       non-digit character.
-
-   So have fun.  */
-static const char *
-hp_symbol_characters ()
-{
-  return "_$.<>#,*&[]:(){}";
-}
-
-
-/* Return the string of non-alnum characters that may occur 
-   as a valid symbol component in the GNU C++ V3 ABI mangling
-   scheme.  */
-
-static const char *
-gnu_v3_symbol_characters ()
-{
-  return "_$.";
-}
-
-
-extern int main PARAMS ((int, char **));
-
-int
-main (argc, argv)
-     int argc;
-     char **argv;
-{
-  char *result;
-  int c;
-  const char *valid_symbols;
-  enum demangling_styles style = auto_demangling;
-
-  program_name = argv[0];
-
-  strip_underscore = prepends_underscore;
-
-  while ((c = getopt_long (argc, argv, "_ns:", long_options, (int *) 0)) != EOF)
-    {
-      switch (c)
-	{
-	case '?':
-	  usage (stderr, 1);
-	  break;
-	case 'h':
-	  usage (stdout, 0);
-	case 'n':
-	  strip_underscore = 0;
-	  break;
-	case 'v':
-	  printf ("GNU %s (C++ demangler), version %s\n", program_name, program_version);
-	  return (0);
-	case '_':
-	  strip_underscore = 1;
-	  break;
-	case 's':
-	  {
-	    style = cplus_demangle_name_to_style (optarg);
-	    if (style == unknown_demangling)
-	      {
-		fprintf (stderr, "%s: unknown demangling style `%s'\n",
-			 program_name, optarg);
-		return (1);
-	      }
-	    else
-	      cplus_demangle_set_style (style);
-	  }
-	  break;
-	}
-    }
-
-  if (optind < argc)
-    {
-      for ( ; optind < argc; optind++)
-	{
-	  demangle_it (argv[optind]);
-	}
-    }
-  else
-    {
-      switch (current_demangling_style)
-	{
-	case gnu_demangling:
-	case lucid_demangling:
-	case arm_demangling:
-	case java_demangling:
-	case edg_demangling:
-	case gnat_demangling:
-	case auto_demangling:
-	  valid_symbols = standard_symbol_characters ();
-	  break;
-	case hp_demangling:
-	  valid_symbols = hp_symbol_characters ();
-	  break;
-	case gnu_v3_demangling:
-	  valid_symbols = gnu_v3_symbol_characters ();
-	  break;
-	default:
-	  /* Folks should explicitly indicate the appropriate alphabet for
-	     each demangling.  Providing a default would allow the
-	     question to go unconsidered.  */
-	  abort ();
-	}
-
-      for (;;)
-	{
-	  int i = 0;
-	  c = getchar ();
-	  /* Try to read a label.  */
-	  while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c)))
-	    {
-	      if (i >= MBUF_SIZE-1)
-		break;
-	      mbuffer[i++] = c;
-	      c = getchar ();
-	    }
-	  if (i > 0)
-	    {
-	      int skip_first = 0;
-
-	      if (mbuffer[0] == '.' || mbuffer[0] == '$')
-		++skip_first;
-	      if (strip_underscore && mbuffer[skip_first] == '_')
-		++skip_first;
-
-	      if (skip_first > i)
-		skip_first = i;
-
-	      mbuffer[i] = 0;
-	      flags |= (int) style;
-	      result = cplus_demangle (mbuffer + skip_first, flags);
-	      if (result)
-		{
-		  if (mbuffer[0] == '.')
-		    putc ('.', stdout);
-		  fputs (result, stdout);
-		  free (result);
-		}
-	      else
-		fputs (mbuffer, stdout);
-
-	      fflush (stdout);
-	    }
-	  if (c == EOF)
-	    break;
-	  putchar (c);
-	  fflush (stdout);
-	}
-    }
-
-  return (0);
-}
-
-static void
-fatal (str)
-     const char *str;
-{
-  fprintf (stderr, "%s: %s\n", program_name, str);
-  exit (1);
-}
-
-PTR
-xmalloc (size)
-  size_t size;
-{
-  register PTR value = (PTR) malloc (size);
-  if (value == 0)
-    fatal ("virtual memory exhausted");
-  return value;
-}
-
-PTR
-xrealloc (ptr, size)
-  PTR ptr;
-  size_t size;
-{
-  register PTR value = (PTR) realloc (ptr, size);
-  if (value == 0)
-    fatal ("virtual memory exhausted");
-  return value;
-}
-#endif	/* main */
diff --git a/coregrind/m_demangle/demangle.c b/coregrind/m_demangle/demangle.c
index a5a9e41..5974fb9 100644
--- a/coregrind/m_demangle/demangle.c
+++ b/coregrind/m_demangle/demangle.c
@@ -34,9 +34,11 @@
 #include "pub_core_mallocfree.h"
 #include "pub_core_options.h"
 #include "pub_core_libcassert.h"
-#include "demangle.h"
 #include "pub_core_libcprint.h"
 
+#include "vg_libciface.h"
+#include "demangle.h"
+
 /* The demangler's job is to take a raw symbol name and turn it into
    something a Human Bean can understand.  There are two levels of
    mangling.
@@ -68,6 +70,20 @@
    - do the below-main hack
 */
 
+/* Note that the C++ demangler is from GNU libiberty and is almost
+   completely unmodified.  We use vg_libciface.h as a way to
+   impedance-match the libiberty code into our own framework.
+
+   The current code is from libiberty in the gcc tree, gcc svn
+   r141363, dated 26 Oct 2008 (when the gcc trunk was in Stage 3
+   leading up to a gcc-4.4 release).  As of r141363, libiberty is LGPL
+   2.1, which AFAICT is compatible with "GPL 2 or later" and so is OK
+   for inclusion in Valgrind.
+
+   To update to a newer libiberty, it might be simplest to svn diff
+   the gcc tree libibery against r141363 and then apply those diffs
+   here. */
+
 /* This is the main, standard demangler entry point. */
 
 void VG_(demangle) ( Bool do_cxx_demangle, 
diff --git a/coregrind/m_demangle/demangle.h b/coregrind/m_demangle/demangle.h
index 833706c..145d9fd 100644
--- a/coregrind/m_demangle/demangle.h
+++ b/coregrind/m_demangle/demangle.h
@@ -1,29 +1,42 @@
 /* Defs for interface to demanglers.
-   Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001
-   Free Software Foundation, Inc.
+   Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002,
+   2003, 2004, 2005, 2007 Free Software Foundation, Inc.
    
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License
+   as published by the Free Software Foundation; either version 2, or
+   (at your option) any later version.
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+   In addition to the permissions in the GNU Library General Public
+   License, the Free Software Foundation gives you unlimited
+   permission to link the compiled version of this file into
+   combinations with other programs, and to distribute those
+   combinations without any restriction coming from the use of this
+   file.  (The Library Public License restrictions do apply in other
+   respects; for example, they cover modification of the file, and
+   distribution when not linked into a combined executable.)
 
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  */
 
 
-#ifndef DEMANGLE_H
+#if !defined (DEMANGLE_H)
 #define DEMANGLE_H
 
-#include "ansidecl.h"
+#if 0 /* in valgrind */
+#include "libiberty.h"
+#endif /* ! in valgrind */
 
-#define current_demangling_style ML_(current_demangling_style)
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
 
 /* Options passed to cplus_demangle (in 2nd parameter). */
 
@@ -31,6 +44,10 @@
 #define DMGL_PARAMS	 (1 << 0)	/* Include function args */
 #define DMGL_ANSI	 (1 << 1)	/* Include const, volatile, etc */
 #define DMGL_JAVA	 (1 << 2)	/* Demangle as Java rather than C++. */
+#define DMGL_VERBOSE	 (1 << 3)	/* Include implementation details.  */
+#define DMGL_TYPES	 (1 << 4)	/* Also try to demangle type encodings.  */
+#define DMGL_RET_POSTFIX (1 << 5)       /* Print function return types (when
+                                           present) after function signature */
 
 #define DMGL_AUTO	 (1 << 8)
 #define DMGL_GNU	 (1 << 9)
@@ -106,42 +123,44 @@
 } libiberty_demanglers[];
 
 extern char *
-ML_(cplus_demangle) PARAMS ((const char *mangled, int options));
+ML_(cplus_demangle) (const char *mangled, int options);
 
-/*
 extern int
-cplus_demangle_opname PARAMS ((const char *opname, char *result, int options));
-*/
+cplus_demangle_opname (const char *opname, char *result, int options);
 
-/*
 extern const char *
-cplus_mangle_opname PARAMS ((const char *opname, int options));
-*/
+cplus_mangle_opname (const char *opname, int options);
 
 /* Note: This sets global state.  FIXME if you care about multi-threading. */
 
-/*
 extern void
-set_cplus_marker_for_demangling PARAMS ((int ch));
-*/
+set_cplus_marker_for_demangling (int ch);
 
-/*
 extern enum demangling_styles 
-cplus_demangle_set_style PARAMS ((enum demangling_styles style));
-*/
+cplus_demangle_set_style (enum demangling_styles style);
 
-/*
 extern enum demangling_styles 
-cplus_demangle_name_to_style PARAMS ((const char *name));
-*/
+cplus_demangle_name_to_style (const char *name);
 
-/* V3 ABI demangling entry points, defined in cp-demangle.c.  */
-extern char*
-ML_(cplus_demangle_v3) PARAMS ((const char* mangled));
+/* Callback typedef for allocation-less demangler interfaces. */
+typedef void (*demangle_callbackref) (const char *, size_t, void *);
+
+/* V3 ABI demangling entry points, defined in cp-demangle.c.  Callback
+   variants return non-zero on success, zero on error.  char* variants
+   return a string allocated by malloc on success, NULL on error.  */
+extern int
+cplus_demangle_v3_callback (const char *mangled, int options,
+                            demangle_callbackref callback, void *opaque);
 
 extern char*
-ML_(java_demangle_v3) PARAMS ((const char* mangled));
+cplus_demangle_v3 (const char *mangled, int options);
 
+extern int
+java_demangle_v3_callback (const char *mangled,
+                           demangle_callbackref callback, void *opaque);
+
+extern char*
+java_demangle_v3 (const char *mangled);
 
 enum gnu_v3_ctor_kinds {
   gnu_v3_complete_object_ctor = 1,
@@ -153,10 +172,8 @@
    in the G++ V3 ABI demangling style.  Specifically, return an `enum
    gnu_v3_ctor_kinds' value indicating what kind of constructor
    it is.  */
-/*
 extern enum gnu_v3_ctor_kinds
-	is_gnu_v3_mangled_ctor PARAMS ((const char *name));
-*/
+	is_gnu_v3_mangled_ctor (const char *name);
 
 
 enum gnu_v3_dtor_kinds {
@@ -169,9 +186,414 @@
    in the G++ V3 ABI demangling style.  Specifically, return an `enum
    gnu_v3_dtor_kinds' value, indicating what kind of destructor
    it is.  */
-/*
 extern enum gnu_v3_dtor_kinds
-	is_gnu_v3_mangled_dtor PARAMS ((const char *name));
-*/
+	is_gnu_v3_mangled_dtor (const char *name);
+
+/* The V3 demangler works in two passes.  The first pass builds a tree
+   representation of the mangled name, and the second pass turns the
+   tree representation into a demangled string.  Here we define an
+   interface to permit a caller to build their own tree
+   representation, which they can pass to the demangler to get a
+   demangled string.  This can be used to canonicalize user input into
+   something which the demangler might output.  It could also be used
+   by other demanglers in the future.  */
+
+/* These are the component types which may be found in the tree.  Many
+   component types have one or two subtrees, referred to as left and
+   right (a component type with only one subtree puts it in the left
+   subtree).  */
+
+enum demangle_component_type
+{
+  /* A name, with a length and a pointer to a string.  */
+  DEMANGLE_COMPONENT_NAME,
+  /* A qualified name.  The left subtree is a class or namespace or
+     some such thing, and the right subtree is a name qualified by
+     that class.  */
+  DEMANGLE_COMPONENT_QUAL_NAME,
+  /* A local name.  The left subtree describes a function, and the
+     right subtree is a name which is local to that function.  */
+  DEMANGLE_COMPONENT_LOCAL_NAME,
+  /* A typed name.  The left subtree is a name, and the right subtree
+     describes that name as a function.  */
+  DEMANGLE_COMPONENT_TYPED_NAME,
+  /* A template.  The left subtree is a template name, and the right
+     subtree is a template argument list.  */
+  DEMANGLE_COMPONENT_TEMPLATE,
+  /* A template parameter.  This holds a number, which is the template
+     parameter index.  */
+  DEMANGLE_COMPONENT_TEMPLATE_PARAM,
+  /* A constructor.  This holds a name and the kind of
+     constructor.  */
+  DEMANGLE_COMPONENT_CTOR,
+  /* A destructor.  This holds a name and the kind of destructor.  */
+  DEMANGLE_COMPONENT_DTOR,
+  /* A vtable.  This has one subtree, the type for which this is a
+     vtable.  */
+  DEMANGLE_COMPONENT_VTABLE,
+  /* A VTT structure.  This has one subtree, the type for which this
+     is a VTT.  */
+  DEMANGLE_COMPONENT_VTT,
+  /* A construction vtable.  The left subtree is the type for which
+     this is a vtable, and the right subtree is the derived type for
+     which this vtable is built.  */
+  DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE,
+  /* A typeinfo structure.  This has one subtree, the type for which
+     this is the tpeinfo structure.  */
+  DEMANGLE_COMPONENT_TYPEINFO,
+  /* A typeinfo name.  This has one subtree, the type for which this
+     is the typeinfo name.  */
+  DEMANGLE_COMPONENT_TYPEINFO_NAME,
+  /* A typeinfo function.  This has one subtree, the type for which
+     this is the tpyeinfo function.  */
+  DEMANGLE_COMPONENT_TYPEINFO_FN,
+  /* A thunk.  This has one subtree, the name for which this is a
+     thunk.  */
+  DEMANGLE_COMPONENT_THUNK,
+  /* A virtual thunk.  This has one subtree, the name for which this
+     is a virtual thunk.  */
+  DEMANGLE_COMPONENT_VIRTUAL_THUNK,
+  /* A covariant thunk.  This has one subtree, the name for which this
+     is a covariant thunk.  */
+  DEMANGLE_COMPONENT_COVARIANT_THUNK,
+  /* A Java class.  This has one subtree, the type.  */
+  DEMANGLE_COMPONENT_JAVA_CLASS,
+  /* A guard variable.  This has one subtree, the name for which this
+     is a guard variable.  */
+  DEMANGLE_COMPONENT_GUARD,
+  /* A reference temporary.  This has one subtree, the name for which
+     this is a temporary.  */
+  DEMANGLE_COMPONENT_REFTEMP,
+  /* A hidden alias.  This has one subtree, the encoding for which it
+     is providing alternative linkage.  */
+  DEMANGLE_COMPONENT_HIDDEN_ALIAS,
+  /* A standard substitution.  This holds the name of the
+     substitution.  */
+  DEMANGLE_COMPONENT_SUB_STD,
+  /* The restrict qualifier.  The one subtree is the type which is
+     being qualified.  */
+  DEMANGLE_COMPONENT_RESTRICT,
+  /* The volatile qualifier.  The one subtree is the type which is
+     being qualified.  */
+  DEMANGLE_COMPONENT_VOLATILE,
+  /* The const qualifier.  The one subtree is the type which is being
+     qualified.  */
+  DEMANGLE_COMPONENT_CONST,
+  /* The restrict qualifier modifying a member function.  The one
+     subtree is the type which is being qualified.  */
+  DEMANGLE_COMPONENT_RESTRICT_THIS,
+  /* The volatile qualifier modifying a member function.  The one
+     subtree is the type which is being qualified.  */
+  DEMANGLE_COMPONENT_VOLATILE_THIS,
+  /* The const qualifier modifying a member function.  The one subtree
+     is the type which is being qualified.  */
+  DEMANGLE_COMPONENT_CONST_THIS,
+  /* A vendor qualifier.  The left subtree is the type which is being
+     qualified, and the right subtree is the name of the
+     qualifier.  */
+  DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL,
+  /* A pointer.  The one subtree is the type which is being pointed
+     to.  */
+  DEMANGLE_COMPONENT_POINTER,
+  /* A reference.  The one subtree is the type which is being
+     referenced.  */
+  DEMANGLE_COMPONENT_REFERENCE,
+  /* C++0x: An rvalue reference.  The one subtree is the type which is
+     being referenced.  */
+  DEMANGLE_COMPONENT_RVALUE_REFERENCE,
+  /* A complex type.  The one subtree is the base type.  */
+  DEMANGLE_COMPONENT_COMPLEX,
+  /* An imaginary type.  The one subtree is the base type.  */
+  DEMANGLE_COMPONENT_IMAGINARY,
+  /* A builtin type.  This holds the builtin type information.  */
+  DEMANGLE_COMPONENT_BUILTIN_TYPE,
+  /* A vendor's builtin type.  This holds the name of the type.  */
+  DEMANGLE_COMPONENT_VENDOR_TYPE,
+  /* A function type.  The left subtree is the return type.  The right
+     subtree is a list of ARGLIST nodes.  Either or both may be
+     NULL.  */
+  DEMANGLE_COMPONENT_FUNCTION_TYPE,
+  /* An array type.  The left subtree is the dimension, which may be
+     NULL, or a string (represented as DEMANGLE_COMPONENT_NAME), or an
+     expression.  The right subtree is the element type.  */
+  DEMANGLE_COMPONENT_ARRAY_TYPE,
+  /* A pointer to member type.  The left subtree is the class type,
+     and the right subtree is the member type.  CV-qualifiers appear
+     on the latter.  */
+  DEMANGLE_COMPONENT_PTRMEM_TYPE,
+  /* An argument list.  The left subtree is the current argument, and
+     the right subtree is either NULL or another ARGLIST node.  */
+  DEMANGLE_COMPONENT_ARGLIST,
+  /* A template argument list.  The left subtree is the current
+     template argument, and the right subtree is either NULL or
+     another TEMPLATE_ARGLIST node.  */
+  DEMANGLE_COMPONENT_TEMPLATE_ARGLIST,
+  /* An operator.  This holds information about a standard
+     operator.  */
+  DEMANGLE_COMPONENT_OPERATOR,
+  /* An extended operator.  This holds the number of arguments, and
+     the name of the extended operator.  */
+  DEMANGLE_COMPONENT_EXTENDED_OPERATOR,
+  /* A typecast, represented as a unary operator.  The one subtree is
+     the type to which the argument should be cast.  */
+  DEMANGLE_COMPONENT_CAST,
+  /* A unary expression.  The left subtree is the operator, and the
+     right subtree is the single argument.  */
+  DEMANGLE_COMPONENT_UNARY,
+  /* A binary expression.  The left subtree is the operator, and the
+     right subtree is a BINARY_ARGS.  */
+  DEMANGLE_COMPONENT_BINARY,
+  /* Arguments to a binary expression.  The left subtree is the first
+     argument, and the right subtree is the second argument.  */
+  DEMANGLE_COMPONENT_BINARY_ARGS,
+  /* A trinary expression.  The left subtree is the operator, and the
+     right subtree is a TRINARY_ARG1.  */
+  DEMANGLE_COMPONENT_TRINARY,
+  /* Arguments to a trinary expression.  The left subtree is the first
+     argument, and the right subtree is a TRINARY_ARG2.  */
+  DEMANGLE_COMPONENT_TRINARY_ARG1,
+  /* More arguments to a trinary expression.  The left subtree is the
+     second argument, and the right subtree is the third argument.  */
+  DEMANGLE_COMPONENT_TRINARY_ARG2,
+  /* A literal.  The left subtree is the type, and the right subtree
+     is the value, represented as a DEMANGLE_COMPONENT_NAME.  */
+  DEMANGLE_COMPONENT_LITERAL,
+  /* A negative literal.  Like LITERAL, but the value is negated.
+     This is a minor hack: the NAME used for LITERAL points directly
+     to the mangled string, but since negative numbers are mangled
+     using 'n' instead of '-', we want a way to indicate a negative
+     number which involves neither modifying the mangled string nor
+     allocating a new copy of the literal in memory.  */
+  DEMANGLE_COMPONENT_LITERAL_NEG,
+  /* A libgcj compiled resource.  The left subtree is the name of the
+     resource.  */
+  DEMANGLE_COMPONENT_JAVA_RESOURCE,
+  /* A name formed by the concatenation of two parts.  The left
+     subtree is the first part and the right subtree the second.  */
+  DEMANGLE_COMPONENT_COMPOUND_NAME,
+  /* A name formed by a single character.  */
+  DEMANGLE_COMPONENT_CHARACTER,
+  /* A decltype type.  */
+  DEMANGLE_COMPONENT_DECLTYPE,
+  /* A pack expansion.  */
+  DEMANGLE_COMPONENT_PACK_EXPANSION
+};
+
+/* Types which are only used internally.  */
+
+struct demangle_operator_info;
+struct demangle_builtin_type_info;
+
+/* A node in the tree representation is an instance of a struct
+   demangle_component.  Note that the field names of the struct are
+   not well protected against macros defined by the file including
+   this one.  We can fix this if it ever becomes a problem.  */
+
+struct demangle_component
+{
+  /* The type of this component.  */
+  enum demangle_component_type type;
+
+  union
+  {
+    /* For DEMANGLE_COMPONENT_NAME.  */
+    struct
+    {
+      /* A pointer to the name (which need not NULL terminated) and
+	 its length.  */
+      const char *s;
+      int len;
+    } s_name;
+
+    /* For DEMANGLE_COMPONENT_OPERATOR.  */
+    struct
+    {
+      /* Operator.  */
+      const struct demangle_operator_info *op;
+    } s_operator;
+
+    /* For DEMANGLE_COMPONENT_EXTENDED_OPERATOR.  */
+    struct
+    {
+      /* Number of arguments.  */
+      int args;
+      /* Name.  */
+      struct demangle_component *name;
+    } s_extended_operator;
+
+    /* For DEMANGLE_COMPONENT_CTOR.  */
+    struct
+    {
+      /* Kind of constructor.  */
+      enum gnu_v3_ctor_kinds kind;
+      /* Name.  */
+      struct demangle_component *name;
+    } s_ctor;
+
+    /* For DEMANGLE_COMPONENT_DTOR.  */
+    struct
+    {
+      /* Kind of destructor.  */
+      enum gnu_v3_dtor_kinds kind;
+      /* Name.  */
+      struct demangle_component *name;
+    } s_dtor;
+
+    /* For DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
+    struct
+    {
+      /* Builtin type.  */
+      const struct demangle_builtin_type_info *type;
+    } s_builtin;
+
+    /* For DEMANGLE_COMPONENT_SUB_STD.  */
+    struct
+    {
+      /* Standard substitution string.  */
+      const char* string;
+      /* Length of string.  */
+      int len;
+    } s_string;
+
+    /* For DEMANGLE_COMPONENT_TEMPLATE_PARAM.  */
+    struct
+    {
+      /* Template parameter index.  */
+      long number;
+    } s_number;
+
+    /* For DEMANGLE_COMPONENT_CHARACTER.  */
+    struct
+    {
+      int character;
+    } s_character;
+
+    /* For other types.  */
+    struct
+    {
+      /* Left (or only) subtree.  */
+      struct demangle_component *left;
+      /* Right subtree.  */
+      struct demangle_component *right;
+    } s_binary;
+
+  } u;
+};
+
+/* People building mangled trees are expected to allocate instances of
+   struct demangle_component themselves.  They can then call one of
+   the following functions to fill them in.  */
+
+/* Fill in most component types with a left subtree and a right
+   subtree.  Returns non-zero on success, zero on failure, such as an
+   unrecognized or inappropriate component type.  */
+
+extern int
+cplus_demangle_fill_component (struct demangle_component *fill,
+                               enum demangle_component_type,
+                               struct demangle_component *left,
+                               struct demangle_component *right);
+
+/* Fill in a DEMANGLE_COMPONENT_NAME.  Returns non-zero on success,
+   zero for bad arguments.  */
+
+extern int
+cplus_demangle_fill_name (struct demangle_component *fill,
+                          const char *, int);
+
+/* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE, using the name of the
+   builtin type (e.g., "int", etc.).  Returns non-zero on success,
+   zero if the type is not recognized.  */
+
+extern int
+cplus_demangle_fill_builtin_type (struct demangle_component *fill,
+                                  const char *type_name);
+
+/* Fill in a DEMANGLE_COMPONENT_OPERATOR, using the name of the
+   operator and the number of arguments which it takes (the latter is
+   used to disambiguate operators which can be both binary and unary,
+   such as '-').  Returns non-zero on success, zero if the operator is
+   not recognized.  */
+
+extern int
+cplus_demangle_fill_operator (struct demangle_component *fill,
+                              const char *opname, int args);
+
+/* Fill in a DEMANGLE_COMPONENT_EXTENDED_OPERATOR, providing the
+   number of arguments and the name.  Returns non-zero on success,
+   zero for bad arguments.  */
+
+extern int
+cplus_demangle_fill_extended_operator (struct demangle_component *fill,
+                                       int numargs,
+                                       struct demangle_component *nm);
+
+/* Fill in a DEMANGLE_COMPONENT_CTOR.  Returns non-zero on success,
+   zero for bad arguments.  */
+
+extern int
+cplus_demangle_fill_ctor (struct demangle_component *fill,
+                          enum gnu_v3_ctor_kinds kind,
+                          struct demangle_component *name);
+
+/* Fill in a DEMANGLE_COMPONENT_DTOR.  Returns non-zero on success,
+   zero for bad arguments.  */
+
+extern int
+cplus_demangle_fill_dtor (struct demangle_component *fill,
+                          enum gnu_v3_dtor_kinds kind,
+                          struct demangle_component *name);
+
+/* This function translates a mangled name into a struct
+   demangle_component tree.  The first argument is the mangled name.
+   The second argument is DMGL_* options.  This returns a pointer to a
+   tree on success, or NULL on failure.  On success, the third
+   argument is set to a block of memory allocated by malloc.  This
+   block should be passed to free when the tree is no longer
+   needed.  */
+
+extern struct demangle_component *
+cplus_demangle_v3_components (const char *mangled, int options, void **mem);
+
+/* This function takes a struct demangle_component tree and returns
+   the corresponding demangled string.  The first argument is DMGL_*
+   options.  The second is the tree to demangle.  The third is a guess
+   at the length of the demangled string, used to initially allocate
+   the return buffer.  The fourth is a pointer to a size_t.  On
+   success, this function returns a buffer allocated by malloc(), and
+   sets the size_t pointed to by the fourth argument to the size of
+   the allocated buffer (not the length of the returned string).  On
+   failure, this function returns NULL, and sets the size_t pointed to
+   by the fourth argument to 0 for an invalid tree, or to 1 for a
+   memory allocation error.  */
+
+extern char *
+cplus_demangle_print (int options,
+                      const struct demangle_component *tree,
+                      int estimated_length,
+                      size_t *p_allocated_size);
+
+/* This function takes a struct demangle_component tree and passes back
+   a demangled string in one or more calls to a callback function.
+   The first argument is DMGL_* options.  The second is the tree to
+   demangle.  The third is a pointer to a callback function; on each call
+   this receives an element of the demangled string, its length, and an
+   opaque value.  The fourth is the opaque value passed to the callback.
+   The callback is called once or more to return the full demangled
+   string.  The demangled element string is always nul-terminated, though
+   its length is also provided for convenience.  In contrast to
+   cplus_demangle_print(), this function does not allocate heap memory
+   to grow output strings (except perhaps where alloca() is implemented
+   by malloc()), and so is normally safe for use where the heap has been
+   corrupted.  On success, this function returns 1; on failure, 0.  */
+
+extern int
+cplus_demangle_print_callback (int options,
+                               const struct demangle_component *tree,
+                               demangle_callbackref callback, void *opaque);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
 
 #endif	/* DEMANGLE_H */
diff --git a/coregrind/m_demangle/dyn-string.c b/coregrind/m_demangle/dyn-string.c
index 1dcbba8..0836971 100644
--- a/coregrind/m_demangle/dyn-string.c
+++ b/coregrind/m_demangle/dyn-string.c
@@ -1,5 +1,5 @@
 /* An abstract string datatype.
-   Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
    Contributed by Mark Mitchell (mark@markmitchell.com).
 
 This file is part of GNU CC.
@@ -9,6 +9,15 @@
 the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
+In addition to the permissions in the GNU General Public License, the
+Free Software Foundation gives you unlimited permission to link the
+compiled version of this file into combinations with other programs,
+and to distribute those combinations without any restriction coming
+from the use of this file.  (The General Public License restrictions
+do apply in other respects; for example, they cover modification of
+the file, and distribution when not linked into a combined
+executable.)
+
 GNU CC is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@@ -16,43 +25,30 @@
 
 You should have received a copy of the GNU General Public License
 along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, 51 Franklin Street - Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
+/////////////////////////////
+#include <string.h>
+#include <stdlib.h>
+/////////////////////////////
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-/*#ifdef HAVE_STRING_H
+#include <stdio.h>
+
+#ifdef HAVE_STRING_H
 #include <string.h>
-#endif*/
+#endif
 
-/*#ifdef HAVE_STDLIB_H
+#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
-#endif*/
+#endif
 
-#include "pub_core_basics.h"
-#include "pub_core_libcbase.h"
-#include "pub_core_libcassert.h"
-#include "pub_core_mallocfree.h"
-#include "ansidecl.h"
+#include "libiberty.h"
 #include "dyn-string.h"
 
-#ifndef STANDALONE
-#define malloc(_cc,s)    VG_(arena_malloc) (VG_AR_DEMANGLE, _cc, s)
-#define free(p)          VG_(arena_free)   (VG_AR_DEMANGLE, p)
-#define realloc(_cc,p,s) VG_(arena_realloc)(VG_AR_DEMANGLE, _cc, p, s)
-#endif
-
-/* If this file is being compiled for inclusion in the C++ runtime
-   library, as part of the demangler implementation, we don't want to
-   abort if an allocation fails.  Instead, percolate an error code up
-   through the call chain.  */
-
-#ifdef IN_LIBGCC2
-#define RETURN_ON_ALLOCATION_FAILURE
-#endif
-
 /* Performs in-place initialization of a dyn_string struct.  This
    function can be used with a dyn_string struct on the stack or
    embedded in another object.  The contents of of the string itself
@@ -64,9 +60,7 @@
    fails, returns 0.  Otherwise returns 1.  */
 
 int
-dyn_string_init (ds_struct_ptr, space)
-     struct dyn_string *ds_struct_ptr;
-     int space;
+dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
 {
   /* We need at least one byte in which to store the terminating NUL.  */
   if (space == 0)
@@ -77,7 +71,7 @@
   if (ds_struct_ptr->s == NULL)
     return 0;
 #else
-  ds_struct_ptr->s = (char *) malloc ("demangle.dsi.1", space);
+  ds_struct_ptr->s = XNEWVEC (char, space);
 #endif
   ds_struct_ptr->allocated = space;
   ds_struct_ptr->length = 0;
@@ -93,12 +87,11 @@
    returns the newly allocated string.  */
 
 dyn_string_t 
-dyn_string_new (space)
-     int space;
+dyn_string_new (int space)
 {
   dyn_string_t result;
 #ifdef RETURN_ON_ALLOCATION_FAILURE
-  result = (dyn_string_t) malloc ("demangle.dsn.1", sizeof (struct dyn_string));
+  result = (dyn_string_t) malloc (sizeof (struct dyn_string));
   if (result == NULL)
     return NULL;
   if (!dyn_string_init (result, space))
@@ -107,7 +100,7 @@
       return NULL;
     }
 #else
-  result = (dyn_string_t) malloc ("demangle.dsn.2", sizeof (struct dyn_string));
+  result = XNEW (struct dyn_string);
   dyn_string_init (result, space);
 #endif
   return result;
@@ -116,8 +109,7 @@
 /* Free the memory used by DS.  */
 
 void 
-dyn_string_delete (ds)
-     dyn_string_t ds;
+dyn_string_delete (dyn_string_t ds)
 {
   free (ds->s);
   free (ds);
@@ -128,8 +120,7 @@
    DS is then set to the empty string.  Deletes DS itself.  */
 
 char*
-dyn_string_release (ds)
-     dyn_string_t ds;
+dyn_string_release (dyn_string_t ds)
 {
   /* Store the old buffer.  */
   char* result = ds->s;
@@ -149,9 +140,7 @@
    operation fails, deletes DS and returns NULL.  */
 
 dyn_string_t 
-dyn_string_resize (ds, space)
-     dyn_string_t ds;
-     int space;
+dyn_string_resize (dyn_string_t ds, int space)
 {
   int new_allocated = ds->allocated;
 
@@ -167,14 +156,14 @@
       ds->allocated = new_allocated;
       /* We actually need more space.  */
 #ifdef RETURN_ON_ALLOCATION_FAILURE
-      ds->s = (char *) realloc ("demangle.dsr.1", ds->s, ds->allocated);
+      ds->s = (char *) realloc (ds->s, ds->allocated);
       if (ds->s == NULL)
 	{
 	  free (ds);
 	  return NULL;
 	}
 #else
-      ds->s = (char *) realloc ("demangle.dsr.2", ds->s, ds->allocated);
+      ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
 #endif
     }
 
@@ -184,8 +173,7 @@
 /* Sets the contents of DS to the empty string.  */
 
 void
-dyn_string_clear (ds)
-     dyn_string_t ds;
+dyn_string_clear (dyn_string_t ds)
 {
   /* A dyn_string always has room for at least the NUL terminator.  */
   ds->s[0] = '\0';
@@ -197,18 +185,16 @@
    RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0.  */
 
 int
-dyn_string_copy (dest, src)
-     dyn_string_t dest;
-     dyn_string_t src;
+dyn_string_copy (dyn_string_t dest, dyn_string_t src)
 {
   if (dest == src)
-      VG_(core_panic) ("dyn_string_copy: src==dest");
+    abort ();
 
   /* Make room in DEST.  */
   if (dyn_string_resize (dest, src->length) == NULL)
     return 0;
   /* Copy DEST into SRC.  */
-  VG_(strcpy) (dest->s, src->s);
+  strcpy (dest->s, src->s);
   /* Update the size of DEST.  */
   dest->length = src->length;
   return 1;
@@ -219,16 +205,14 @@
    and returns 0.  */
 
 int
-dyn_string_copy_cstr (dest, src)
-     dyn_string_t dest;
-     const char *src;
+dyn_string_copy_cstr (dyn_string_t dest, const char *src)
 {
-  int length = VG_(strlen) (src);
+  int length = strlen (src);
   /* Make room in DEST.  */
   if (dyn_string_resize (dest, length) == NULL)
     return 0;
   /* Copy DEST into SRC.  */
-  VG_(strcpy) (dest->s, src);
+  strcpy (dest->s, src);
   /* Update the size of DEST.  */
   dest->length = length;
   return 1;
@@ -240,9 +224,7 @@
    returns 0.  */
 
 int
-dyn_string_prepend (dest, src)
-     dyn_string_t dest;
-     dyn_string_t src;
+dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
 {
   return dyn_string_insert (dest, 0, src);
 }
@@ -252,9 +234,7 @@
    if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
 
 int
-dyn_string_prepend_cstr (dest, src)
-     dyn_string_t dest;
-     const char *src;
+dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
 {
   return dyn_string_insert_cstr (dest, 0, src);
 }
@@ -265,15 +245,12 @@
    and returns 0.  */
 
 int
-dyn_string_insert (dest, pos, src)
-     dyn_string_t dest;
-     int pos;
-     dyn_string_t src;
+dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
 {
   int i;
 
   if (src == dest)
-    VG_(core_panic)( "dyn_string_insert: src==dest" );
+    abort ();
 
   if (dyn_string_resize (dest, dest->length + src->length) == NULL)
     return 0;
@@ -281,7 +258,7 @@
   for (i = dest->length; i >= pos; --i)
     dest->s[i + src->length] = dest->s[i];
   /* Splice in the new stuff.  */
-  VG_(strncpy) (dest->s + pos, src->s, src->length);
+  strncpy (dest->s + pos, src->s, src->length);
   /* Compute the new length.  */
   dest->length += src->length;
   return 1;
@@ -293,13 +270,10 @@
    and returns 0.  */
 
 int
-dyn_string_insert_cstr (dest, pos, src)
-     dyn_string_t dest;
-     int pos;
-     const char *src;
+dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
 {
   int i;
-  int length = VG_(strlen) (src);
+  int length = strlen (src);
 
   if (dyn_string_resize (dest, dest->length + length) == NULL)
     return 0;
@@ -307,7 +281,7 @@
   for (i = dest->length; i >= pos; --i)
     dest->s[i + length] = dest->s[i];
   /* Splice in the new stuff.  */
-  VG_(strncpy) (dest->s + pos, src, length);
+  strncpy (dest->s + pos, src, length);
   /* Compute the new length.  */
   dest->length += length;
   return 1;
@@ -318,10 +292,7 @@
    RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0.  */
 
 int
-dyn_string_insert_char (dest, pos, c)
-     dyn_string_t dest;
-     int pos;
-     int c;
+dyn_string_insert_char (dyn_string_t dest, int pos, int c)
 {
   int i;
 
@@ -342,13 +313,11 @@
    returns 0.  */
 
 int
-dyn_string_append (dest, s)
-     dyn_string_t dest;
-     dyn_string_t s;
+dyn_string_append (dyn_string_t dest, dyn_string_t s)
 {
   if (dyn_string_resize (dest, dest->length + s->length) == 0)
     return 0;
-  VG_(strcpy) (dest->s + dest->length, s->s);
+  strcpy (dest->s + dest->length, s->s);
   dest->length += s->length;
   return 1;
 }
@@ -358,17 +327,15 @@
    deletes DEST and returns 0.  */
 
 int
-dyn_string_append_cstr (dest, s)
-     dyn_string_t dest;
-     const char *s;
+dyn_string_append_cstr (dyn_string_t dest, const char *s)
 {
-  int len = VG_(strlen) (s);
+  int len = strlen (s);
 
   /* The new length is the old length plus the size of our string, plus
      one for the null at the end.  */
   if (dyn_string_resize (dest, dest->length + len) == NULL)
     return 0;
-  VG_(strcpy) (dest->s + dest->length, s);
+  strcpy (dest->s + dest->length, s);
   dest->length += len;
   return 1;
 }
@@ -377,9 +344,7 @@
    if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0.  */
 
 int
-dyn_string_append_char (dest, c)
-     dyn_string_t dest;
-     int c;
+dyn_string_append_char (dyn_string_t dest, int c)
 {
   /* Make room for the extra character.  */
   if (dyn_string_resize (dest, dest->length + 1) == NULL)
@@ -400,18 +365,14 @@
    deletes DEST and returns 0.  */
 
 int
-dyn_string_substring (dest, src, start, end)
-     dyn_string_t dest;
-     dyn_string_t src;
-     int start;
-     int end;
+dyn_string_substring (dyn_string_t dest, dyn_string_t src,
+                      int start, int end)
 {
   int i;
   int length = end - start;
 
-  /*
-  vg_assert (start > end || start > src->length || end > src->length);
-  */
+  if (start > end || start > src->length || end > src->length)
+    abort ();
 
   /* Make room for the substring.  */
   if (dyn_string_resize (dest, length) == NULL)
@@ -430,13 +391,11 @@
 /* Returns non-zero if DS1 and DS2 have the same contents.  */
 
 int
-dyn_string_eq (ds1, ds2)
-     dyn_string_t ds1;
-     dyn_string_t ds2;
+dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)
 {
   /* If DS1 and DS2 have different lengths, they must not be the same.  */
   if (ds1->length != ds2->length)
     return 0;
   else
-    return !VG_(strcmp) (ds1->s, ds2->s);
+    return !strcmp (ds1->s, ds2->s);
 }
diff --git a/coregrind/m_demangle/dyn-string.h b/coregrind/m_demangle/dyn-string.h
index 9615cd6..44e33de 100644
--- a/coregrind/m_demangle/dyn-string.h
+++ b/coregrind/m_demangle/dyn-string.h
@@ -1,5 +1,5 @@
 /* An abstract string datatype.
-   Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
    Contributed by Mark Mitchell (mark@markmitchell.com).
 
 This file is part of GCC.
@@ -16,10 +16,8 @@
 
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
-#ifndef __DYN_STRING_H
-#define __DYN_STRING_H
+the Free Software Foundation, 51 Franklin Street - Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 
 typedef struct dyn_string
@@ -39,58 +37,24 @@
 
 /* Compare DS1 to DS2 with strcmp.  */
 #define dyn_string_compare(DS1, DS2)                                    \
-  (VG_(strcmp) ((DS1)->s, (DS2)->s))
+  (strcmp ((DS1)->s, (DS2)->s))
 
 
-/* dyn_string functions are used in the demangling implementation
-   included in the G++ runtime library.  To prevent collisions with
-   names in user programs, the functions that are used in the
-   demangler are given implementation-reserved names.  */
-
-#if 1 /* def IN_LIBGCC2 */
-
-#define dyn_string_init                 VG_(__cxa_dyn_string_init)
-#define dyn_string_new                  VG_(__cxa_dyn_string_new)
-#define dyn_string_delete               VG_(__cxa_dyn_string_delete)
-#define dyn_string_release              VG_(__cxa_dyn_string_release)
-#define dyn_string_resize               VG_(__cxa_dyn_string_resize)
-#define dyn_string_clear                VG_(__cxa_dyn_string_clear)
-#define dyn_string_copy                 VG_(__cxa_dyn_string_copy)
-#define dyn_string_copy_cstr            VG_(__cxa_dyn_string_copy_cstr)
-#define dyn_string_prepend              VG_(__cxa_dyn_string_prepend)
-#define dyn_string_prepend_cstr         VG_(__cxa_dyn_string_prepend_cstr)
-#define dyn_string_insert               VG_(__cxa_dyn_string_insert)
-#define dyn_string_insert_cstr          VG_(__cxa_dyn_string_insert_cstr)
-#define dyn_string_insert_char          VG_(__cxa_dyn_string_insert_char)
-#define dyn_string_append               VG_(__cxa_dyn_string_append)
-#define dyn_string_append_cstr          VG_(__cxa_dyn_string_append_cstr)
-#define dyn_string_append_char          VG_(__cxa_dyn_string_append_char)
-#define dyn_string_substring            VG_(__cxa_dyn_string_substring)
-#define dyn_string_eq                   VG_(__cxa_dyn_string_eq)
-
-#endif /* IN_LIBGCC2 */
-
-
-extern int dyn_string_init              PARAMS ((struct dyn_string *, int));
-extern dyn_string_t dyn_string_new      PARAMS ((int));
-extern void dyn_string_delete           PARAMS ((dyn_string_t));
-extern char *dyn_string_release         PARAMS ((dyn_string_t));
-extern dyn_string_t dyn_string_resize   PARAMS ((dyn_string_t, int));
-extern void dyn_string_clear            PARAMS ((dyn_string_t));
-extern int dyn_string_copy              PARAMS ((dyn_string_t, dyn_string_t));
-extern int dyn_string_copy_cstr         PARAMS ((dyn_string_t, const char *));
-extern int dyn_string_prepend           PARAMS ((dyn_string_t, dyn_string_t));
-extern int dyn_string_prepend_cstr      PARAMS ((dyn_string_t, const char *));
-extern int dyn_string_insert            PARAMS ((dyn_string_t, int,
-						 dyn_string_t));
-extern int dyn_string_insert_cstr       PARAMS ((dyn_string_t, int,
-						 const char *));
-extern int dyn_string_insert_char       PARAMS ((dyn_string_t, int, int));
-extern int dyn_string_append            PARAMS ((dyn_string_t, dyn_string_t));
-extern int dyn_string_append_cstr       PARAMS ((dyn_string_t, const char *));
-extern int dyn_string_append_char       PARAMS ((dyn_string_t, int));
-extern int dyn_string_substring         PARAMS ((dyn_string_t, 
-						 dyn_string_t, int, int));
-extern int dyn_string_eq                PARAMS ((dyn_string_t, dyn_string_t));
-
-#endif
+extern int dyn_string_init (struct dyn_string *, int);
+extern dyn_string_t dyn_string_new (int);
+extern void dyn_string_delete (dyn_string_t);
+extern char *dyn_string_release (dyn_string_t);
+extern dyn_string_t dyn_string_resize (dyn_string_t, int);
+extern void dyn_string_clear (dyn_string_t);
+extern int dyn_string_copy (dyn_string_t, dyn_string_t);
+extern int dyn_string_copy_cstr (dyn_string_t, const char *);
+extern int dyn_string_prepend (dyn_string_t, dyn_string_t);
+extern int dyn_string_prepend_cstr (dyn_string_t, const char *);
+extern int dyn_string_insert (dyn_string_t, int, dyn_string_t);
+extern int dyn_string_insert_cstr (dyn_string_t, int, const char *);
+extern int dyn_string_insert_char (dyn_string_t, int, int);
+extern int dyn_string_append (dyn_string_t, dyn_string_t);
+extern int dyn_string_append_cstr (dyn_string_t, const char *);
+extern int dyn_string_append_char (dyn_string_t, int);
+extern int dyn_string_substring (dyn_string_t,  dyn_string_t, int, int);
+extern int dyn_string_eq (dyn_string_t, dyn_string_t);
diff --git a/coregrind/m_demangle/safe-ctype.c b/coregrind/m_demangle/safe-ctype.c
index c685be3..0972b4b 100644
--- a/coregrind/m_demangle/safe-ctype.c
+++ b/coregrind/m_demangle/safe-ctype.c
@@ -1,6 +1,7 @@
 /* <ctype.h> replacement macros.
 
-   Copyright (C) 2000 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002, 2003, 2004,
+   2005 Free Software Foundation, Inc.
    Contributed by Zack Weinberg <zackw@stanford.edu>.
 
 This file is part of the libiberty library.
@@ -16,22 +17,111 @@
 
 You should have received a copy of the GNU Library General Public
 License along with libiberty; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
-/* This is a compatible replacement of the standard C library's <ctype.h>
-   with the following properties:
+/*
 
-   - Implements all isxxx() macros required by C99.
-   - Also implements some character classes useful when
-     parsing C-like languages.
-   - Does not change behavior depending on the current locale.
-   - Behaves properly for all values in the range of a signed or
-     unsigned char.  */
+@defvr Extension HOST_CHARSET
+This macro indicates the basic character set and encoding used by the
+host: more precisely, the encoding used for character constants in
+preprocessor @samp{#if} statements (the C "execution character set").
+It is defined by @file{safe-ctype.h}, and will be an integer constant
+with one of the following values:
+
+@ftable @code
+@item HOST_CHARSET_UNKNOWN
+The host character set is unknown - that is, not one of the next two
+possibilities.
+
+@item HOST_CHARSET_ASCII
+The host character set is ASCII.
+
+@item HOST_CHARSET_EBCDIC
+The host character set is some variant of EBCDIC.  (Only one of the
+nineteen EBCDIC varying characters is tested; exercise caution.)
+@end ftable
+@end defvr
+
+@deffn  Extension ISALPHA  (@var{c})
+@deffnx Extension ISALNUM  (@var{c})
+@deffnx Extension ISBLANK  (@var{c})
+@deffnx Extension ISCNTRL  (@var{c})
+@deffnx Extension ISDIGIT  (@var{c})
+@deffnx Extension ISGRAPH  (@var{c})
+@deffnx Extension ISLOWER  (@var{c})
+@deffnx Extension ISPRINT  (@var{c})
+@deffnx Extension ISPUNCT  (@var{c})
+@deffnx Extension ISSPACE  (@var{c})
+@deffnx Extension ISUPPER  (@var{c})
+@deffnx Extension ISXDIGIT (@var{c})
+
+These twelve macros are defined by @file{safe-ctype.h}.  Each has the
+same meaning as the corresponding macro (with name in lowercase)
+defined by the standard header @file{ctype.h}.  For example,
+@code{ISALPHA} returns true for alphabetic characters and false for
+others.  However, there are two differences between these macros and
+those provided by @file{ctype.h}:
+
+@itemize @bullet
+@item These macros are guaranteed to have well-defined behavior for all 
+values representable by @code{signed char} and @code{unsigned char}, and
+for @code{EOF}.
+
+@item These macros ignore the current locale; they are true for these
+fixed sets of characters:
+@multitable {@code{XDIGIT}} {yada yada yada yada yada yada yada yada}
+@item @code{ALPHA}  @tab @kbd{A-Za-z}
+@item @code{ALNUM}  @tab @kbd{A-Za-z0-9}
+@item @code{BLANK}  @tab @kbd{space tab}
+@item @code{CNTRL}  @tab @code{!PRINT}
+@item @code{DIGIT}  @tab @kbd{0-9}
+@item @code{GRAPH}  @tab @code{ALNUM || PUNCT}
+@item @code{LOWER}  @tab @kbd{a-z}
+@item @code{PRINT}  @tab @code{GRAPH ||} @kbd{space}
+@item @code{PUNCT}  @tab @kbd{`~!@@#$%^&*()_-=+[@{]@}\|;:'",<.>/?}
+@item @code{SPACE}  @tab @kbd{space tab \n \r \f \v}
+@item @code{UPPER}  @tab @kbd{A-Z}
+@item @code{XDIGIT} @tab @kbd{0-9A-Fa-f}
+@end multitable
+
+Note that, if the host character set is ASCII or a superset thereof,
+all these macros will return false for all values of @code{char} outside
+the range of 7-bit ASCII.  In particular, both ISPRINT and ISCNTRL return
+false for characters with numeric values from 128 to 255.
+@end itemize
+@end deffn
+
+@deffn  Extension ISIDNUM         (@var{c})
+@deffnx Extension ISIDST          (@var{c})
+@deffnx Extension IS_VSPACE       (@var{c})
+@deffnx Extension IS_NVSPACE      (@var{c})
+@deffnx Extension IS_SPACE_OR_NUL (@var{c})
+@deffnx Extension IS_ISOBASIC     (@var{c})
+These six macros are defined by @file{safe-ctype.h} and provide
+additional character classes which are useful when doing lexical
+analysis of C or similar languages.  They are true for the following
+sets of characters:
+
+@multitable {@code{SPACE_OR_NUL}} {yada yada yada yada yada yada yada yada}
+@item @code{IDNUM}        @tab @kbd{A-Za-z0-9_}
+@item @code{IDST}         @tab @kbd{A-Za-z_}
+@item @code{VSPACE}       @tab @kbd{\r \n}
+@item @code{NVSPACE}      @tab @kbd{space tab \f \v \0}
+@item @code{SPACE_OR_NUL} @tab @code{VSPACE || NVSPACE}
+@item @code{ISOBASIC}     @tab @code{VSPACE || NVSPACE || PRINT}
+@end multitable
+@end deffn
+
+*/
 
 #include "ansidecl.h"
-#include "safe-ctype.h"
-/*#include <stdio.h>*/  /* for EOF */
+#include <safe-ctype.h>
+#include <stdio.h>  /* for EOF */
+
+#if EOF != -1
+ #error "<safe-ctype.h> requires EOF == -1"
+#endif
 
 /* Shorthand */
 #define bl _sch_isblank
@@ -48,25 +138,23 @@
 #define xd _sch_isxdigit
 
 /* Masks.  */
-#define L  lo|is   |pr	/* lower case letter */
-#define XL lo|is|xd|pr	/* lowercase hex digit */
-#define U  up|is   |pr	/* upper case letter */
-#define XU up|is|xd|pr	/* uppercase hex digit */
-#define D  di   |xd|pr	/* decimal digit */
-#define P  pn      |pr	/* punctuation */
-#define _  pn|is   |pr	/* underscore */
+#define L  (const unsigned short) (lo|is   |pr)	/* lower case letter */
+#define XL (const unsigned short) (lo|is|xd|pr)	/* lowercase hex digit */
+#define U  (const unsigned short) (up|is   |pr)	/* upper case letter */
+#define XU (const unsigned short) (up|is|xd|pr)	/* uppercase hex digit */
+#define D  (const unsigned short) (di   |xd|pr)	/* decimal digit */
+#define P  (const unsigned short) (pn      |pr)	/* punctuation */
+#define _  (const unsigned short) (pn|is   |pr)	/* underscore */
 
-#define C           cn	/* control character */
-#define Z  nv      |cn	/* NUL */
-#define M  nv|sp   |cn	/* cursor movement: \f \v */
-#define V  vs|sp   |cn	/* vertical space: \r \n */
-#define T  nv|sp|bl|cn	/* tab */
-#define S  nv|sp|bl|pr	/* space */
+#define C  (const unsigned short) (         cn)	/* control character */
+#define Z  (const unsigned short) (nv      |cn)	/* NUL */
+#define M  (const unsigned short) (nv|sp   |cn)	/* cursor movement: \f \v */
+#define V  (const unsigned short) (vs|sp   |cn)	/* vertical space: \r \n */
+#define T  (const unsigned short) (nv|sp|bl|cn)	/* tab */
+#define S  (const unsigned short) (nv|sp|bl|pr)	/* space */
 
 /* Are we ASCII? */
-#if '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
-  && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21 \
-/*  && EOF == -1*/
+#if HOST_CHARSET == HOST_CHARSET_ASCII
 
 const unsigned short _sch_istable[256] =
 {
@@ -159,5 +247,9 @@
 };
 
 #else
- #error "Unsupported host character set"
-#endif /* not ASCII */
+# if HOST_CHARSET == HOST_CHARSET_EBCDIC
+  #error "FIXME: write tables for EBCDIC"
+# else
+  #error "Unrecognized host character set"
+# endif
+#endif
diff --git a/coregrind/m_demangle/safe-ctype.h b/coregrind/m_demangle/safe-ctype.h
index b2ad849..3b16827 100644
--- a/coregrind/m_demangle/safe-ctype.h
+++ b/coregrind/m_demangle/safe-ctype.h
@@ -16,8 +16,8 @@
 
 You should have received a copy of the GNU Library General Public
 License along with libiberty; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* This is a compatible replacement of the standard C library's <ctype.h>
    with the following properties:
@@ -35,9 +35,22 @@
 #ifndef SAFE_CTYPE_H
 #define SAFE_CTYPE_H
 
-#ifdef isalpha
- #error "safe-ctype.h and ctype.h may not be used simultaneously"
+/* Determine host character set.  */
+#define HOST_CHARSET_UNKNOWN 0
+#define HOST_CHARSET_ASCII   1
+#define HOST_CHARSET_EBCDIC  2
+
+#if  '\n' == 0x0A && ' ' == 0x20 && '0' == 0x30 \
+   && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
+#  define HOST_CHARSET HOST_CHARSET_ASCII
 #else
+# if '\n' == 0x15 && ' ' == 0x40 && '0' == 0xF0 \
+   && 'A' == 0xC1 && 'a' == 0x81 && '!' == 0x5A
+#  define HOST_CHARSET HOST_CHARSET_EBCDIC
+# else
+#  define HOST_CHARSET HOST_CHARSET_UNKNOWN
+# endif
+#endif
 
 /* Categories.  */
 
@@ -99,5 +112,42 @@
 #define TOUPPER(c) _sch_toupper[(c) & 0xff]
 #define TOLOWER(c) _sch_tolower[(c) & 0xff]
 
-#endif /* no ctype.h */
+/* Prevent the users of safe-ctype.h from accidently using the routines
+   from ctype.h.  Initially, the approach was to produce an error when
+   detecting that ctype.h has been included.  But this was causing
+   trouble as ctype.h might get indirectly included as a result of
+   including another system header (for instance gnulib's stdint.h).
+   So we include ctype.h here and then immediately redefine its macros.  */
+
+#if 0 /* in valgrind */
+#include <ctype.h>
+#endif /* ! in valgrind */
+
+#undef isalpha
+#define isalpha(c) do_not_use_isalpha_with_safe_ctype
+#undef isalnum
+#define isalnum(c) do_not_use_isalnum_with_safe_ctype
+#undef iscntrl
+#define iscntrl(c) do_not_use_iscntrl_with_safe_ctype
+#undef isdigit
+#define isdigit(c) do_not_use_isdigit_with_safe_ctype
+#undef isgraph
+#define isgraph(c) do_not_use_isgraph_with_safe_ctype
+#undef islower
+#define islower(c) do_not_use_islower_with_safe_ctype
+#undef isprint
+#define isprint(c) do_not_use_isprint_with_safe_ctype
+#undef ispunct
+#define ispunct(c) do_not_use_ispunct_with_safe_ctype
+#undef isspace
+#define isspace(c) do_not_use_isspace_with_safe_ctype
+#undef isupper
+#define isupper(c) do_not_use_isupper_with_safe_ctype
+#undef isxdigit
+#define isxdigit(c) do_not_use_isxdigit_with_safe_ctype
+#undef toupper
+#define toupper(c) do_not_use_toupper_with_safe_ctype
+#undef tolower
+#define tolower(c) do_not_use_tolower_with_safe_ctype
+
 #endif /* SAFE_CTYPE_H */
diff --git a/coregrind/m_demangle/vg_libciface.h b/coregrind/m_demangle/vg_libciface.h
new file mode 100644
index 0000000..61156c9
--- /dev/null
+++ b/coregrind/m_demangle/vg_libciface.h
@@ -0,0 +1,97 @@
+
+/*--------------------------------------------------------------------*/
+/*--- Demangling of C++ mangled names.              vg_libciface.h ---*/
+/*--------------------------------------------------------------------*/
+
+/*
+   This file is part of Valgrind, a dynamic binary instrumentation
+   framework.
+
+   Copyright (C) 2000-2008 Julian Seward 
+      jseward@acm.org
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307, USA.
+
+   The GNU General Public License is contained in the file COPYING.
+*/
+
+/* This file contains a bunch of macro definitions etc which are
+   really "impedance matchers".  They make it possible for
+   cp-demangle.c, cplus-dem.c, dyn-string.c and safe-ctype.c (taken
+   from GNU libiberty) to be compiled in the Valgrind framework with a
+   minimum of changes.
+
+   This file should only be included in files which contain libiberty
+   code. */
+
+#include "pub_core_basics.h"
+#include "pub_core_libcbase.h"
+#include "pub_core_libcassert.h"
+#include "pub_core_libcprint.h"
+#include "pub_core_mallocfree.h"
+
+
+#define abort()              vg_assert(0)
+#define atoi(_str)           VG_(atoll)((_str))
+#define free(_pt)            VG_(arena_free)   (VG_AR_DEMANGLE,(_pt))
+#define memcmp(_s1,_s2,_sz)  VG_(memcmp)((_s1),(_s2),(_sz))
+#define memcpy(_dd,_ss,_sz)  VG_(memcpy)((_dd),(_ss),(_sz))
+#define memset(_ss,_cc,_sz)  VG_(memset)((_ss),(_cc),(_sz))
+#define realloc(_cc,_pt,_sz) VG_(arena_realloc)(VG_AR_DEMANGLE,(_cc),(_pt),(_sz))
+#define sprintf(_buf,_fmt,_args...)  VG_(sprintf)((_buf),(_fmt),(_args))
+#define strcat(_dd,_ss)      VG_(strcat)((_dd),(_ss))
+#define strchr(_ss,_cc)      VG_(strchr)((_ss),(_cc))
+#define strcmp(_s1,_s2)      VG_(strcmp)((_s1),(_s2))
+#define strcpy(_dd,_ss)      VG_(strcpy)((_dd),(_ss))
+#define strcspn(_ss,_rr)     VG_(strcspn)((_ss),(_rr))
+#define strlen(_str)         VG_(strlen)(_str)
+#define strncat(_dd,_ss,_nn) VG_(strncat)((_dd),(_ss),(_nn))
+#define strncmp(_s1,_s2,_sz) VG_(strncmp)((_s1),(_s2),(_sz))
+#define strncpy(_dd,_ss,_sz) VG_(strncpy)((_dd),(_ss),(_sz))
+#define strpbrk(_ss,_aa)     VG_(strpbrk)((_ss),(_aa))
+#define strspn(_ss,_aa)      VG_(strspn)((_ss),(_aa))
+#define strstr(_hh,_nn)      VG_(strstr)((_hh),(_nn))
+
+#define size_t SizeT
+
+#define xmalloc(_nn)      \
+   VG_(arena_malloc)(VG_AR_DEMANGLE, "m_demangle.xmalloc", (_nn))
+#define xrealloc(_pt,_sz) \
+   VG_(arena_realloc)(VG_AR_DEMANGLE,"m_demangle.xrealloc",(_pt),(_sz))
+#define xstrdup(_str) \
+   VG_(arena_strdup)(VG_AR_DEMANGLE,"m_demangle.xstrdup",(_str))
+
+/* Taken from libiberty.h: */
+
+#define ARRAY_SIZE(_arr) \
+        (sizeof (_arr) / sizeof ((_arr)[0]))
+
+#define XNEWVEC(_Ty, _Nn)    \
+        ((_Ty *) xmalloc(sizeof (_Ty) * (_Nn)))
+
+#define XRESIZEVEC(_Ty, _Pt, _Nn) \
+        ((_Ty *) xrealloc((void *) (_Pt), sizeof (_Ty) * (_Nn)))
+
+#define XRESIZEVAR(_Ty, _Pt, _Sz) \
+        ((_Ty *) xrealloc((_Pt), (_Sz)))
+
+#define XNEW(_Ty) \
+        ((_Ty *) xmalloc(sizeof (_Ty)))
+
+
+/*--------------------------------------------------------------------*/
+/*--- end                                           vg_libciface.h ---*/
+/*--------------------------------------------------------------------*/
diff --git a/coregrind/m_libcbase.c b/coregrind/m_libcbase.c
index f63a0ed..c8dee84 100644
--- a/coregrind/m_libcbase.c
+++ b/coregrind/m_libcbase.c
@@ -226,9 +226,9 @@
    String functions
    ------------------------------------------------------------------ */
 
-Int VG_(strlen) ( const Char* str )
+SizeT VG_(strlen) ( const Char* str )
 {
-   Int i = 0;
+   SizeT i = 0;
    while (str[i] != 0) i++;
    return i;
 }
@@ -276,7 +276,7 @@
    zero termination. */
 void VG_(strncpy_safely) ( Char* dest, const Char* src, SizeT ndest )
 {
-   Int i = 0;
+   SizeT i = 0;
    while (True) {
       dest[i] = 0;
       if (src[i] == 0) return;
@@ -288,7 +288,7 @@
 
 Char* VG_(strncpy) ( Char* dest, const Char* src, SizeT ndest )
 {
-   Int i = 0;
+   SizeT i = 0;
    while (True) {
       if (i >= ndest) return dest;     /* reached limit */
       dest[i] = src[i];
@@ -335,7 +335,7 @@
 
 Int VG_(strncmp) ( const Char* s1, const Char* s2, SizeT nmax )
 {
-   Int n = 0;
+   SizeT n = 0;
    while (True) {
       if (n >= nmax) return 0;
       if (*s1 == 0 && *s2 == 0) return 0;
@@ -367,7 +367,7 @@
 
 Char* VG_(strstr) ( const Char* haystack, Char* needle )
 {
-   Int n; 
+   SizeT n; 
    if (haystack == NULL)
       return NULL;
    n = VG_(strlen)(needle);
@@ -398,6 +398,35 @@
    return NULL;
 }
 
+SizeT VG_(strspn) ( const Char* s, const Char* accept )
+{
+   const Char *p, *a;
+   SizeT count = 0;
+   for (p = s; *p != '\0'; ++p) {
+      for (a = accept; *a != '\0'; ++a)
+         if (*p == *a)
+            break;
+      if (*a == '\0')
+         return count;
+      else
+         ++count;
+   }
+   return count;
+}
+
+SizeT VG_(strcspn) ( const Char* s, const char* reject )
+{
+   SizeT count = 0;
+   while (*s != '\0') {
+      if (VG_(strchr) (reject, *s++) == NULL)
+         ++count;
+      else
+         return count;
+   }
+   return count;
+}
+
+
 /* ---------------------------------------------------------------------
    A simple string matching routine, purloined from Hugs98.
       '*'    matches any sequence of zero or more characters
diff --git a/include/pub_tool_libcbase.h b/include/pub_tool_libcbase.h
index 458d3d5..d8d7f3e 100644
--- a/include/pub_tool_libcbase.h
+++ b/include/pub_tool_libcbase.h
@@ -75,7 +75,7 @@
 #define VG_STREQ(s1,s2) ( (s1 != NULL && s2 != NULL \
                            && VG_(strcmp)((s1),(s2))==0) ? True : False )
 
-extern Int   VG_(strlen)         ( const Char* str );
+extern SizeT VG_(strlen)         ( const Char* str );
 extern Char* VG_(strcat)         ( Char* dest, const Char* src );
 extern Char* VG_(strncat)        ( Char* dest, const Char* src, SizeT n );
 extern Char* VG_(strpbrk)        ( const Char* s, const Char* accpt );
@@ -86,6 +86,8 @@
 extern Char* VG_(strstr)         ( const Char* haystack, Char* needle );
 extern Char* VG_(strchr)         ( const Char* s, Char c );
 extern Char* VG_(strrchr)        ( const Char* s, Char c );
+extern SizeT VG_(strspn)         ( const Char* s, const Char* accept );
+extern SizeT VG_(strcspn)        ( const Char* s, const char* reject );
 
 /* Like strcmp() and strncmp(), but stop comparing at any whitespace. */
 extern Int   VG_(strcmp_ws)      ( const Char* s1, const Char* s2 );