OSDN Git Service

* configure: Regenerate.
[pf3gnuchains/gcc-fork.git] / gcc / cp / mangle.c
index 41c381d..7377a3e 100644 (file)
@@ -1,12 +1,13 @@
 /* Name mangling for the 3.0 C++ ABI.
-   Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
-   Written by Alex Samuel <sameul@codesourcery.com>
+   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007
+   Free Software Foundation, Inc.
+   Written by Alex Samuel <samuel@codesourcery.com>
 
    This file is part of GCC.
 
    GCC 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)
+   the Free Software Foundation; either version 3, or (at your option)
    any later version.
 
    GCC is distributed in the hope that it will be useful, but
    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 GCC; see the file COPYING.  If not, write to the Free
-   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.  */
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 /* This file implements mangling of C++ names according to the IA64
    C++ ABI specification.  A mangled name encodes a function or
    particular constructs when the appropriate decl for that construct
    is not available.  These are:
 
-     mangle_typeinfo_for_type:        typeinfo data
-     mangle_typeinfo_string_for_type: typeinfo type name
-     mangle_vtbl_for_type:            virtual table data
-     mangle_vtt_for_type:             VTT data
-     mangle_ctor_vtbl_for_type:       `C-in-B' constructor virtual table data
-     mangle_thunk:                    thunk function or entry
-
-*/
+     mangle_typeinfo_for_type:         typeinfo data
+     mangle_typeinfo_string_for_type:  typeinfo type name
+     mangle_vtbl_for_type:             virtual table data
+     mangle_vtt_for_type:              VTT data
+     mangle_ctor_vtbl_for_type:                `C-in-B' constructor virtual table data
+     mangle_thunk:                     thunk function or entry  */
 
 #include "config.h"
 #include "system.h"
@@ -74,7 +72,7 @@
   fprintf (stderr, "  %-24s: %-24s\n", (FN), (INPUT))
 # define MANGLE_TRACE_TREE(FN, NODE) \
   fprintf (stderr, "  %-24s: %-24s (%p)\n", \
-           (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
+          (FN), tree_code_name[TREE_CODE (NODE)], (void *) (NODE))
 #else
 # define MANGLE_TRACE(FN, INPUT)
 # define MANGLE_TRACE_TREE(FN, NODE)
@@ -96,7 +94,7 @@ typedef struct globals GTY(())
 {
   /* An array of the current substitution candidates, in the order
      we've seen them.  */
-  varray_type substitutions;
+  VEC(tree,gc) *substitutions;
 
   /* The entity that is being mangled.  */
   tree GTY ((skip)) entity;
@@ -233,32 +231,32 @@ static void write_java_integer_type_codes (const tree);
 
 /* Append a single character to the end of the mangled
    representation.  */
-#define write_char(CHAR)                                              \
+#define write_char(CHAR)                                               \
   obstack_1grow (mangle_obstack, (CHAR))
 
 /* Append a sized buffer to the end of the mangled representation.  */
-#define write_chars(CHAR, LEN)                                        \
+#define write_chars(CHAR, LEN)                                         \
   obstack_grow (mangle_obstack, (CHAR), (LEN))
 
 /* Append a NUL-terminated string to the end of the mangled
    representation.  */
-#define write_string(STRING)                                          \
+#define write_string(STRING)                                           \
   obstack_grow (mangle_obstack, (STRING), strlen (STRING))
 
 /* Nonzero if NODE1 and NODE2 are both TREE_LIST nodes and have the
    same purpose (context, which may be a type) and value (template
    decl).  See write_template_prefix for more information on what this
    is used for.  */
-#define NESTED_TEMPLATE_MATCH(NODE1, NODE2)                         \
-  (TREE_CODE (NODE1) == TREE_LIST                                     \
-   && TREE_CODE (NODE2) == TREE_LIST                                  \
-   && ((TYPE_P (TREE_PURPOSE (NODE1))                                 \
-        && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))\
-       || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2))             \
+#define NESTED_TEMPLATE_MATCH(NODE1, NODE2)                            \
+  (TREE_CODE (NODE1) == TREE_LIST                                      \
+   && TREE_CODE (NODE2) == TREE_LIST                                   \
+   && ((TYPE_P (TREE_PURPOSE (NODE1))                                  \
+       && same_type_p (TREE_PURPOSE (NODE1), TREE_PURPOSE (NODE2)))    \
+       || TREE_PURPOSE (NODE1) == TREE_PURPOSE (NODE2))                        \
    && TREE_VALUE (NODE1) == TREE_VALUE (NODE2))
 
 /* Write out an unsigned quantity in base 10.  */
-#define write_unsigned_number(NUMBER) \
+#define write_unsigned_number(NUMBER)                                  \
   write_number ((NUMBER), /*unsigned_p=*/1, 10)
 
 /* Save the current (incomplete) mangled name and release the obstack
@@ -275,7 +273,7 @@ save_partially_mangled_name (void)
     {
       gcc_assert (!partially_mangled_name);
       partially_mangled_name_len = obstack_object_size (mangle_obstack);
-      partially_mangled_name = xmalloc (partially_mangled_name_len);
+      partially_mangled_name = XNEWVEC (char, partially_mangled_name_len);
       memcpy (partially_mangled_name, obstack_base (mangle_obstack),
              partially_mangled_name_len);
       obstack_free (mangle_obstack, obstack_finish (mangle_obstack));
@@ -318,7 +316,7 @@ decl_is_template_id (const tree decl, tree* const template_info)
            *template_info = TYPE_TEMPLATE_INFO (type);
          return 1;
        }
-    } 
+    }
   else
     {
       /* Check if this is a primary template.  */
@@ -345,11 +343,11 @@ static void
 dump_substitution_candidates (void)
 {
   unsigned i;
+  tree el;
 
   fprintf (stderr, "  ++ substitutions  ");
-  for (i = 0; i < VARRAY_ACTIVE_SIZE (G.substitutions); ++i)
+  for (i = 0; VEC_iterate (tree, G.substitutions, i, el); ++i)
     {
-      tree el = VARRAY_TREE (G.substitutions, i);
       const char *name = "???";
 
       if (i > 0)
@@ -361,12 +359,12 @@ dump_substitution_candidates (void)
       else if (TYPE_NAME (el))
        name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (el)));
       fprintf (stderr, " S%d_ = ", i - 1);
-      if (TYPE_P (el) && 
-         (CP_TYPE_RESTRICT_P (el) 
-          || CP_TYPE_VOLATILE_P (el) 
+      if (TYPE_P (el) &&
+         (CP_TYPE_RESTRICT_P (el)
+          || CP_TYPE_VOLATILE_P (el)
           || CP_TYPE_CONST_P (el)))
        fprintf (stderr, "CV-");
-      fprintf (stderr, "%s (%s at %p)\n", 
+      fprintf (stderr, "%s (%s at %p)\n",
               name, tree_code_name[TREE_CODE (el)], (void *) el);
     }
 }
@@ -399,7 +397,7 @@ add_substitution (tree node)
   tree c;
 
   if (DEBUG_MANGLE)
-    fprintf (stderr, "  ++ add_substitution (%s at %10p)\n", 
+    fprintf (stderr, "  ++ add_substitution (%s at %10p)\n",
             tree_code_name[TREE_CODE (node)], (void *) node);
 
   /* Get the canonicalized substitution candidate for NODE.  */
@@ -413,19 +411,19 @@ add_substitution (tree node)
   /* Make sure NODE isn't already a candidate.  */
   {
     int i;
-    for (i = VARRAY_ACTIVE_SIZE (G.substitutions); --i >= 0; )
+    tree candidate;
+
+    for (i = 0; VEC_iterate (tree, G.substitutions, i, candidate); i++)
       {
-       const tree candidate = VARRAY_TREE (G.substitutions, i);
-       
        gcc_assert (!(DECL_P (node) && node == candidate));
-       gcc_assert (!(TYPE_P (node) && TYPE_P (candidate) 
+       gcc_assert (!(TYPE_P (node) && TYPE_P (candidate)
                      && same_type_p (node, candidate)));
       }
   }
 #endif /* ENABLE_CHECKING */
 
   /* Put the decl onto the varray of substitution candidates.  */
-  VARRAY_PUSH_TREE (G.substitutions, node);
+  VEC_safe_push (tree, gc, G.substitutions, node);
 
   if (DEBUG_MANGLE)
     dump_substitution_candidates ();
@@ -435,7 +433,7 @@ add_substitution (tree node)
    which may be a decl or a CLASS_TYPE, is a template-id with template
    name of substitution_index[INDEX] in the ::std namespace.  */
 
-static inline int 
+static inline int
 is_std_substitution (const tree node,
                     const substitution_identifier_index_t index)
 {
@@ -452,14 +450,14 @@ is_std_substitution (const tree node,
       type = node;
       decl = TYPE_NAME (node);
     }
-  else 
+  else
     /* These are not the droids you're looking for.  */
     return 0;
 
   return (DECL_NAMESPACE_STD_P (CP_DECL_CONTEXT (decl))
-         && TYPE_LANG_SPECIFIC (type) 
+         && TYPE_LANG_SPECIFIC (type)
          && TYPE_TEMPLATE_INFO (type)
-         && (DECL_NAME (TYPE_TI_TEMPLATE (type)) 
+         && (DECL_NAME (TYPE_TI_TEMPLATE (type))
              == subst_identifiers[index]));
 }
 
@@ -478,14 +476,14 @@ is_std_substitution_char (const tree node,
     return 0;
   /* Figure out its template args.  */
   if (DECL_P (node))
-    args = DECL_TI_ARGS (node);  
+    args = DECL_TI_ARGS (node);
   else if (CLASS_TYPE_P (node))
     args = CLASSTYPE_TI_ARGS (node);
   else
     /* Oops, not a template.  */
     return 0;
   /* NODE's template arg list should be <char>.  */
-  return 
+  return
     TREE_VEC_LENGTH (args) == 1
     && TREE_VEC_ELT (args, 0) == char_type_node;
 }
@@ -495,28 +493,28 @@ is_std_substitution_char (const tree node,
 
    First, check standard special-case substitutions.
 
-     <substitution> ::= St     
-         # ::std
+     <substitution> ::= St
+        # ::std
 
-                    ::= Sa     
+                   ::= Sa
         # ::std::allocator
 
-                    ::= Sb     
-         # ::std::basic_string
+                   ::= Sb
+        # ::std::basic_string
 
-                    ::= Ss 
-         # ::std::basic_string<char,
+                   ::= Ss
+        # ::std::basic_string<char,
                               ::std::char_traits<char>,
                               ::std::allocator<char> >
 
-                    ::= Si 
-         # ::std::basic_istream<char, ::std::char_traits<char> >
+                   ::= Si
+        # ::std::basic_istream<char, ::std::char_traits<char> >
 
-                    ::= So 
-         # ::std::basic_ostream<char, ::std::char_traits<char> >
+                   ::= So
+        # ::std::basic_ostream<char, ::std::char_traits<char> >
 
-                    ::= Sd 
-         # ::std::basic_iostream<char, ::std::char_traits<char> >   
+                   ::= Sd
+        # ::std::basic_iostream<char, ::std::char_traits<char> >
 
    Then examine the stack of currently available substitution
    candidates for entities appearing earlier in the same mangling
@@ -528,7 +526,7 @@ static int
 find_substitution (tree node)
 {
   int i;
-  const int size = VARRAY_ACTIVE_SIZE (G.substitutions);
+  const int size = VEC_length (tree, G.substitutions);
   tree decl;
   tree type;
 
@@ -546,7 +544,7 @@ find_substitution (tree node)
   type = TYPE_P (node) ? node : TREE_TYPE (node);
 
   /* Check for std::allocator.  */
-  if (decl 
+  if (decl
       && is_std_substitution (decl, SUBID_ALLOCATOR)
       && !CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)))
     {
@@ -559,10 +557,10 @@ find_substitution (tree node)
     {
       if (TYPE_P (node))
        {
-         /* If this is a type (i.e. a fully-qualified template-id), 
-            check for 
-                std::basic_string <char,
-                                   std::char_traits<char>,
+         /* If this is a type (i.e. a fully-qualified template-id),
+            check for
+                std::basic_string <char,
+                                   std::char_traits<char>,
                                    std::allocator<char> > .  */
          if (cp_type_quals (type) == TYPE_UNQUALIFIED
              && CLASSTYPE_USE_TEMPLATE (type))
@@ -595,7 +593,7 @@ find_substitution (tree node)
       && CLASSTYPE_USE_TEMPLATE (type)
       && CLASSTYPE_TEMPLATE_INFO (type) != NULL)
     {
-      /* First, check for the template 
+      /* First, check for the template
         args <char, std::char_traits<char> > .  */
       tree args = CLASSTYPE_TI_ARGS (type);
       if (TREE_VEC_LENGTH (args) == 2
@@ -605,20 +603,19 @@ find_substitution (tree node)
                                       SUBID_CHAR_TRAITS))
        {
          /* Got them.  Is this basic_istream?  */
-         tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
-         if (name == subst_identifiers[SUBID_BASIC_ISTREAM])
+         if (is_std_substitution (decl, SUBID_BASIC_ISTREAM))
            {
              write_string ("Si");
              return 1;
            }
          /* Or basic_ostream?  */
-         else if (name == subst_identifiers[SUBID_BASIC_OSTREAM])
+         else if (is_std_substitution (decl, SUBID_BASIC_OSTREAM))
            {
              write_string ("So");
              return 1;
            }
          /* Or basic_iostream?  */
-         else if (name == subst_identifiers[SUBID_BASIC_IOSTREAM])
+         else if (is_std_substitution (decl, SUBID_BASIC_IOSTREAM))
            {
              write_string ("Sd");
              return 1;
@@ -637,7 +634,7 @@ find_substitution (tree node)
      operation.  */
   for (i = 0; i < size; ++i)
     {
-      tree candidate = VARRAY_TREE (G.substitutions, i);
+      tree candidate = VEC_index (tree, G.substitutions, i);
       /* NODE is a matched to a candidate if it's the same decl node or
         if it's the same type.  */
       if (decl == candidate
@@ -658,7 +655,7 @@ find_substitution (tree node)
 /* TOP_LEVEL is true, if this is being called at outermost level of
   mangling. It should be false when mangling a decl appearing in an
   expression within some other mangling.
-  
+
   <mangled-name>      ::= _Z <encoding>  */
 
 static void
@@ -672,16 +669,16 @@ write_mangled_name (const tree decl, bool top_level)
       && !DECL_OVERLOADED_OPERATOR_P (decl))
     {
     unmangled_name:;
-      
+
       if (top_level)
        write_string (IDENTIFIER_POINTER (DECL_NAME (decl)));
       else
        {
          /* The standard notes: "The <encoding> of an extern "C"
-             function is treated like global-scope data, i.e. as its
-             <source-name> without a type."  We cannot write
-             overloaded operators that way though, because it contains
-             characters invalid in assembler.  */
+            function is treated like global-scope data, i.e. as its
+            <source-name> without a type."  We cannot write
+            overloaded operators that way though, because it contains
+            characters invalid in assembler.  */
          if (abi_version_at_least (2))
            write_string ("_Z");
          else
@@ -690,7 +687,8 @@ write_mangled_name (const tree decl, bool top_level)
        }
     }
   else if (TREE_CODE (decl) == VAR_DECL
-          /* The names of global variables aren't mangled.  */
+          /* The names of non-static global variables aren't mangled.  */
+          && DECL_EXTERNAL_LINKAGE_P (decl)
           && (CP_DECL_CONTEXT (decl) == global_namespace
               /* And neither are `extern "C"' variables.  */
               || DECL_EXTERN_C_P (decl)))
@@ -760,7 +758,7 @@ write_encoding (const tree decl)
          d = decl;
        }
 
-      write_bare_function_type (fn_type, 
+      write_bare_function_type (fn_type,
                                (!DECL_CONSTRUCTOR_P (decl)
                                 && !DECL_DESTRUCTOR_P (decl)
                                 && !DECL_CONV_FN_P (decl)
@@ -770,9 +768,9 @@ write_encoding (const tree decl)
 }
 
 /* <name> ::= <unscoped-name>
-          ::= <unscoped-template-name> <template-args>
+         ::= <unscoped-template-name> <template-args>
          ::= <nested-name>
-         ::= <local-name>  
+         ::= <local-name>
 
    If IGNORE_LOCAL_SCOPE is nonzero, this production of <name> is
    called from <local-name>, which mangles the enclosing scope
@@ -802,8 +800,8 @@ write_name (tree decl, const int ignore_local_scope)
      latter with a special substitution.  Also, a name that is
      directly in a local function scope is also mangled with
      <unscoped-name> rather than a full <nested-name>.  */
-  if (context == NULL 
-      || context == global_namespace 
+  if (context == NULL
+      || context == global_namespace
       || DECL_NAMESPACE_STD_P (context)
       || (ignore_local_scope && TREE_CODE (context) == FUNCTION_DECL))
     {
@@ -822,10 +820,10 @@ write_name (tree decl, const int ignore_local_scope)
   else
     {
       /* Handle local names, unless we asked not to (that is, invoked
-         under <local-name>, to handle only the part of the name under
-         the local scope).  */
+        under <local-name>, to handle only the part of the name under
+        the local scope).  */
       if (!ignore_local_scope)
-        {
+       {
          /* Scan up the list of scope context, looking for a
             function.  If we find one, this entity is in local
             function scope.  local_entity tracks context one scope
@@ -860,7 +858,7 @@ write_name (tree decl, const int ignore_local_scope)
 }
 
 /* <unscoped-name> ::= <unqualified-name>
-                   ::= St <unqualified-name>   # ::std::  */
+                  ::= St <unqualified-name>   # ::std::  */
 
 static void
 write_unscoped_name (const tree decl)
@@ -878,17 +876,17 @@ write_unscoped_name (const tree decl)
   else
     {
       /* If not, it should be either in the global namespace, or directly
-        in a local function scope.  */
-      gcc_assert (context == global_namespace 
+        in a local function scope.  */
+      gcc_assert (context == global_namespace
                  || context == NULL
                  || TREE_CODE (context) == FUNCTION_DECL);
-      
+
       write_unqualified_name (decl);
     }
 }
 
 /* <unscoped-template-name> ::= <unscoped-name>
-                            ::= <substitution>  */
+                           ::= <substitution>  */
 
 static void
 write_unscoped_template_name (const tree decl)
@@ -903,8 +901,8 @@ write_unscoped_template_name (const tree decl)
 
 /* Write the nested name, including CV-qualifiers, of DECL.
 
-   <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E  
-                 ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
+   <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
+                ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
 
    <CV-qualifiers> ::= [r] [V] [K]  */
 
@@ -916,9 +914,9 @@ write_nested_name (const tree decl)
   MANGLE_TRACE_TREE ("nested-name", decl);
 
   write_char ('N');
-  
+
   /* Write CV-qualifiers, if this is a member function.  */
-  if (TREE_CODE (decl) == FUNCTION_DECL 
+  if (TREE_CODE (decl) == FUNCTION_DECL
       && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
     {
       if (DECL_VOLATILE_MEMFUNC_P (decl))
@@ -944,8 +942,8 @@ write_nested_name (const tree decl)
 }
 
 /* <prefix> ::= <prefix> <unqualified-name>
-            ::= <template-param>
-            ::= <template-prefix> <template-args>
+           ::= <template-param>
+           ::= <template-prefix> <template-args>
            ::= # empty
            ::= <substitution>  */
 
@@ -987,7 +985,7 @@ write_prefix (const tree node)
     }
 
   /* In G++ 3.2, the name of the template parameter was used.  */
-  if (TREE_CODE (node) == TEMPLATE_TYPE_PARM 
+  if (TREE_CODE (node) == TEMPLATE_TYPE_PARM
       && !abi_version_at_least (2))
     G.need_abi_warning = true;
 
@@ -1011,8 +1009,8 @@ write_prefix (const tree node)
 }
 
 /* <template-prefix> ::= <prefix> <template component>
-                     ::= <template-param>
-                     ::= <substitution>  */
+                    ::= <template-param>
+                    ::= <substitution>  */
 
 static void
 write_template_prefix (const tree node)
@@ -1032,7 +1030,7 @@ write_template_prefix (const tree node)
   else
     {
       gcc_assert (CLASSTYPE_TEMPLATE_ID_P (type));
-  
+
       template = TYPE_TI_TEMPLATE (type);
     }
 
@@ -1084,11 +1082,14 @@ write_template_prefix (const tree node)
 }
 
 /* We don't need to handle thunks, vtables, or VTTs here.  Those are
-   mangled through special entry points.  
+   mangled through special entry points.
 
     <unqualified-name>  ::= <operator-name>
-                       ::= <special-name>  
-                       ::= <source-name>  */
+                       ::= <special-name>
+                       ::= <source-name>
+                       ::= <local-source-name> 
+
+    <local-source-name>        ::= L <source-name> <discriminator> */
 
 static void
 write_unqualified_name (const tree decl)
@@ -1101,10 +1102,10 @@ write_unqualified_name (const tree decl)
     write_special_name_destructor (decl);
   else if (DECL_NAME (decl) == NULL_TREE)
     write_source_name (DECL_ASSEMBLER_NAME (decl));
-  else if (DECL_CONV_FN_P (decl)) 
+  else if (DECL_CONV_FN_P (decl))
     {
-      /* Conversion operator. Handle it right here.  
-           <operator> ::= cv <type>  */
+      /* Conversion operator. Handle it right here.
+          <operator> ::= cv <type>  */
       tree type;
       if (decl_is_template_id (decl, NULL))
        {
@@ -1125,9 +1126,19 @@ write_unqualified_name (const tree decl)
        oni = assignment_operator_name_info;
       else
        oni = operator_name_info;
-      
+
       write_string (oni[DECL_OVERLOADED_OPERATOR_P (decl)].mangled_name);
     }
+  else if (VAR_OR_FUNCTION_DECL_P (decl) && ! TREE_PUBLIC (decl)
+          && DECL_NAMESPACE_SCOPE_P (decl)
+          && decl_linkage (decl) == lk_internal)
+    {
+      MANGLE_TRACE_TREE ("local-source-name", decl);
+      write_char ('L');
+      write_source_name (DECL_NAME (decl));
+      /* The default discriminator is 1, and that's all we ever use,
+        so there's no code to output one here.  */
+    }
   else
     write_source_name (DECL_NAME (decl));
 }
@@ -1141,7 +1152,7 @@ write_conversion_operator_name (const tree type)
   write_type (type);
 }
 
-/* Non-terminal <source-name>.  IDENTIFIER is an IDENTIFIER_NODE.  
+/* Non-terminal <source-name>.  IDENTIFIER is an IDENTIFIER_NODE.
 
      <source-name> ::= </length/ number> <identifier>  */
 
@@ -1171,11 +1182,11 @@ hwint_to_ascii (unsigned HOST_WIDE_INT number, const unsigned int base,
 {
   static const char base_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   unsigned digits = 0;
-  
+
   while (number)
     {
       unsigned HOST_WIDE_INT d = number / base;
-      
+
       *--buffer = base_digits[number - d * base];
       digits++;
       number = d;
@@ -1233,14 +1244,14 @@ write_integer_cst (const tree cst)
         representable.  */
       chunk = 1000000000;
       chunk_digits = 9;
-      
+
       if (sizeof (HOST_WIDE_INT) >= 8)
        {
          /* It is at least 64 bits, so 10^18 is representable.  */
          chunk_digits = 18;
          chunk *= chunk;
        }
-      
+
       type = c_common_signed_or_unsigned_type (1, TREE_TYPE (cst));
       base = build_int_cstu (type, chunk);
       n = build_int_cst_wide (type,
@@ -1249,16 +1260,16 @@ write_integer_cst (const tree cst)
       if (sign < 0)
        {
          write_char ('n');
-         n = fold (build1 (NEGATE_EXPR, type, n));
+         n = fold_build1 (NEGATE_EXPR, type, n);
        }
       do
        {
-         tree d = fold (build2 (FLOOR_DIV_EXPR, type, n, base));
-         tree tmp = fold (build2 (MULT_EXPR, type, d, base));
+         tree d = fold_build2 (FLOOR_DIV_EXPR, type, n, base);
+         tree tmp = fold_build2 (MULT_EXPR, type, d, base);
          unsigned c;
 
          done = integer_zerop (d);
-         tmp = fold (build2 (MINUS_EXPR, type, n, tmp));
+         tmp = fold_build2 (MINUS_EXPR, type, n, tmp);
          c = hwint_to_ascii (TREE_INT_CST_LOW (tmp), 10, ptr,
                              done ? 1 : chunk_digits);
          ptr -= c;
@@ -1268,11 +1279,11 @@ write_integer_cst (const tree cst)
       while (!done);
       write_chars (ptr, count);
     }
-  else 
+  else
     {
       /* A small num.  */
       unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (cst);
-      
+
       if (sign < 0)
        {
          write_char ('n');
@@ -1282,8 +1293,8 @@ write_integer_cst (const tree cst)
     }
 }
 
-/* Write out a floating-point literal.  
-    
+/* Write out a floating-point literal.
+
     "Floating-point literals are encoded using the bit pattern of the
     target processor's internal representation of that number, as a
     fixed-length lowercase hexadecimal string, high-order bytes first
@@ -1295,11 +1306,11 @@ write_integer_cst (const tree cst)
     for floating point numbers.  (Spaces are for readability, not
     part of the encoding.)
 
-        1.0f                    Lf 3f80 0000 E
-       -1.0f                    Lf bf80 0000 E
-        1.17549435e-38f         Lf 0080 0000 E
-        1.40129846e-45f         Lf 0000 0001 E
-        0.0f                    Lf 0000 0000 E"
+       1.0f                    Lf 3f80 0000 E
+       -1.0f                   Lf bf80 0000 E
+       1.17549435e-38f         Lf 0080 0000 E
+       1.40129846e-45f         Lf 0000 0001 E
+       0.0f                    Lf 0000 0000 E"
 
    Caller is responsible for the Lx and the E.  */
 static void
@@ -1318,7 +1329,7 @@ write_real_cst (const tree value)
                      TYPE_MODE (type));
 
       /* The value in target_real is in the target word order,
-         so we must write it out backward if that happens to be
+        so we must write it out backward if that happens to be
         little-endian.  write_number cannot be used, it will
         produce uppercase.  */
       if (FLOAT_WORDS_BIG_ENDIAN)
@@ -1328,7 +1339,7 @@ write_real_cst (const tree value)
 
       for (; i != limit; i += dir)
        {
-         sprintf (buffer, "%08lx", target_real[i]);
+         sprintf (buffer, "%08lx", (unsigned long) target_real[i]);
          write_chars (buffer, 8);
        }
     }
@@ -1340,7 +1351,7 @@ write_real_cst (const tree value)
         format for REAL_VALUE_TYPE.  */
       size_t i;
       for (i = 0; i < sizeof (TREE_REAL_CST (value)); ++i)
-       write_number (((unsigned char *) &TREE_REAL_CST (value))[i], 
+       write_number (((unsigned char *) &TREE_REAL_CST (value))[i],
                      /*unsigned_p*/ 1,
                      /*base*/ 16);
       G.need_abi_warning = 1;
@@ -1359,13 +1370,13 @@ write_identifier (const char *identifier)
 }
 
 /* Handle constructor productions of non-terminal <special-name>.
-   CTOR is a constructor FUNCTION_DECL. 
+   CTOR is a constructor FUNCTION_DECL.
 
      <special-name> ::= C1   # complete object constructor
-                    ::= C2   # base object constructor
-                    ::= C3   # complete object allocating constructor
+                   ::= C2   # base object constructor
+                   ::= C3   # complete object allocating constructor
 
-   Currently, allocating constructors are never used. 
+   Currently, allocating constructors are never used.
 
    We also need to provide mangled names for the maybe-in-charge
    constructor, so we treat it here too.  mangle_decl_string will
@@ -1389,11 +1400,11 @@ write_special_name_constructor (const tree ctor)
 }
 
 /* Handle destructor productions of non-terminal <special-name>.
-   DTOR is a destructor FUNCTION_DECL. 
+   DTOR is a destructor FUNCTION_DECL.
 
      <special-name> ::= D0 # deleting (in-charge) destructor
-                    ::= D1 # complete object (in-charge) destructor
-                    ::= D2 # base object (not-in-charge) destructor
+                   ::= D1 # complete object (in-charge) destructor
+                   ::= D2 # base object (not-in-charge) destructor
 
    We also need to provide mangled names for the maybe-incharge
    destructor, so we treat it here too.  mangle_decl_string will
@@ -1410,9 +1421,9 @@ write_special_name_destructor (const tree dtor)
     {
       gcc_assert (DECL_COMPLETE_DESTRUCTOR_P (dtor)
                  /* Even though we don't ever emit a definition of
-                    the old-style destructor, we still have to
-                    consider entities (like static variables) nested
-                    inside it.  */
+                    the old-style destructor, we still have to
+                    consider entities (like static variables) nested
+                    inside it.  */
                  || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (dtor));
       write_string ("D1");
     }
@@ -1425,8 +1436,6 @@ write_special_name_destructor (const tree dtor)
 static int
 discriminator_for_local_entity (tree entity)
 {
-  tree *type;
-
   /* Assume this is the only local entity with this name.  */
   int discriminator = 0;
 
@@ -1434,13 +1443,20 @@ discriminator_for_local_entity (tree entity)
     discriminator = DECL_DISCRIMINATOR (entity);
   else if (TREE_CODE (entity) == TYPE_DECL)
     {
+      int ix;
+
       /* Scan the list of local classes.  */
       entity = TREE_TYPE (entity);
-      for (type = &VARRAY_TREE (local_classes, 0); *type != entity; ++type)
-        if (TYPE_IDENTIFIER (*type) == TYPE_IDENTIFIER (entity)
-            && TYPE_CONTEXT (*type) == TYPE_CONTEXT (entity))
-         ++discriminator;
-    }  
+      for (ix = 0; ; ix++)
+       {
+         tree type = VEC_index (tree, local_classes, ix);
+         if (type == entity)
+           break;
+         if (TYPE_IDENTIFIER (type) == TYPE_IDENTIFIER (entity)
+             && TYPE_CONTEXT (type) == TYPE_CONTEXT (entity))
+           ++discriminator;
+       }
+    }
 
   return discriminator;
 }
@@ -1457,7 +1473,7 @@ discriminator_for_string_literal (tree function ATTRIBUTE_UNUSED,
   return 0;
 }
 
-/*   <discriminator> := _ <number>   
+/*   <discriminator> := _ <number>
 
    The discriminator is used only for the second and later occurrences
    of the same name within a single function. In this case <number> is
@@ -1481,7 +1497,7 @@ write_discriminator (const int discriminator)
    of ENTITY.
 
      <local-name> := Z <function encoding> E <entity name> [<discriminator>]
-                  := Z <function encoding> E s [<discriminator>]  */
+                 := Z <function encoding> E s [<discriminator>]  */
 
 static void
 write_local_name (const tree function, const tree local_entity,
@@ -1495,7 +1511,7 @@ write_local_name (const tree function, const tree local_entity,
   if (TREE_CODE (entity) == STRING_CST)
     {
       write_char ('s');
-      write_discriminator (discriminator_for_string_literal (function, 
+      write_discriminator (discriminator_for_string_literal (function,
                                                             entity));
     }
   else
@@ -1508,25 +1524,32 @@ write_local_name (const tree function, const tree local_entity,
     }
 }
 
-/* Non-terminals <type> and <CV-qualifier>.  
+/* Non-terminals <type> and <CV-qualifier>.
 
      <type> ::= <builtin-type>
-            ::= <function-type>
-            ::= <class-enum-type>
-            ::= <array-type>
-            ::= <pointer-to-member-type>
-            ::= <template-param>
-            ::= <substitution>
-            ::= <CV-qualifier>
-            ::= P <type>    # pointer-to
-            ::= R <type>    # reference-to
-            ::= C <type>    # complex pair (C 2000)
-            ::= G <type>    # imaginary (C 2000)     [not supported]
-            ::= U <source-name> <type>   # vendor extended type qualifier 
+           ::= <function-type>
+           ::= <class-enum-type>
+           ::= <array-type>
+           ::= <pointer-to-member-type>
+           ::= <template-param>
+           ::= <substitution>
+           ::= <CV-qualifier>
+           ::= P <type>    # pointer-to
+           ::= R <type>    # reference-to
+           ::= C <type>    # complex pair (C 2000)
+           ::= G <type>    # imaginary (C 2000)     [not supported]
+           ::= U <source-name> <type>   # vendor extended type qualifier
+
+   C++0x extensions
+
+     <type> ::= RR <type>   # rvalue reference-to
+     <type> ::= Dt <expression> # decltype of an id-expression or 
+                                # class member access
+     <type> ::= DT <expression> # decltype of an expression
 
    TYPE is a type node.  */
 
-static void 
+static void
 write_type (tree type)
 {
   /* This gets set to nonzero if TYPE turns out to be a (possibly
@@ -1540,7 +1563,7 @@ write_type (tree type)
 
   if (find_substitution (type))
     return;
-  
+
   if (write_CV_qualifiers_for_type (type) > 0)
     /* If TYPE was CV-qualified, we just wrote the qualifiers; now
        mangle the unqualified type.  The recursive call is needed here
@@ -1554,21 +1577,18 @@ write_type (tree type)
     write_array_type (type);
   else
     {
+      tree type_orig = type;
+
       /* See through any typedefs.  */
       type = TYPE_MAIN_VARIANT (type);
 
       if (TYPE_PTRMEM_P (type))
        write_pointer_to_member_type (type);
-      else switch (TREE_CODE (type))
-       {
-       case VOID_TYPE:
-       case BOOLEAN_TYPE:
-       case INTEGER_TYPE:  /* Includes wchar_t.  */
-       case REAL_TYPE:
-       {
+      else
+        {
          /* Handle any target-specific fundamental types.  */
          const char *target_mangling
-           = targetm.mangle_fundamental_type (type);
+           = targetm.mangle_type (type_orig);
 
          if (target_mangling)
            {
@@ -1576,74 +1596,104 @@ write_type (tree type)
              return;
            }
 
-         /* If this is a typedef, TYPE may not be one of
-            the standard builtin type nodes, but an alias of one.  Use
-            TYPE_MAIN_VARIANT to get to the underlying builtin type.  */
-         write_builtin_type (TYPE_MAIN_VARIANT (type));
-         ++is_builtin_type;
-         break;
-       }
-
-       case COMPLEX_TYPE:
-         write_char ('C');
-         write_type (TREE_TYPE (type));
-         break;
-
-       case FUNCTION_TYPE:
-       case METHOD_TYPE:
-         write_function_type (type);
-         break;
-
-       case UNION_TYPE:
-       case RECORD_TYPE:
-       case ENUMERAL_TYPE:
-         /* A pointer-to-member function is represented as a special
-            RECORD_TYPE, so check for this first.  */
-         if (TYPE_PTRMEMFUNC_P (type))
-           write_pointer_to_member_type (type);
-         else
-           write_class_enum_type (type);
-         break;
-
-       case TYPENAME_TYPE:
-       case UNBOUND_CLASS_TEMPLATE:
-         /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
-            ordinary nested names.  */
-         write_nested_name (TYPE_STUB_DECL (type));
-         break;
-
-       case POINTER_TYPE:
-         write_char ('P');
-         write_type (TREE_TYPE (type));
-         break;
-
-       case REFERENCE_TYPE:
-         write_char ('R');
-         write_type (TREE_TYPE (type));
-         break;
-
-       case TEMPLATE_TYPE_PARM:
-       case TEMPLATE_PARM_INDEX:
-         write_template_param (type);
-         break;
-
-       case TEMPLATE_TEMPLATE_PARM:
-         write_template_template_param (type);
-         break;
-
-       case BOUND_TEMPLATE_TEMPLATE_PARM:
-         write_template_template_param (type);
-         write_template_args 
-           (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
-         break;
-
-       case VECTOR_TYPE:
-         write_string ("U8__vector");
-         write_type (TREE_TYPE (type));
-         break;
-
-       default:
-         gcc_unreachable ();
+         switch (TREE_CODE (type))
+           {
+           case VOID_TYPE:
+           case BOOLEAN_TYPE:
+           case INTEGER_TYPE:  /* Includes wchar_t.  */
+           case REAL_TYPE:
+             {
+               /* If this is a typedef, TYPE may not be one of
+                  the standard builtin type nodes, but an alias of one.  Use
+                  TYPE_MAIN_VARIANT to get to the underlying builtin type.  */
+               write_builtin_type (TYPE_MAIN_VARIANT (type));
+               ++is_builtin_type;
+             }
+             break;
+
+           case COMPLEX_TYPE:
+             write_char ('C');
+             write_type (TREE_TYPE (type));
+             break;
+
+           case FUNCTION_TYPE:
+           case METHOD_TYPE:
+             write_function_type (type);
+             break;
+
+           case UNION_TYPE:
+           case RECORD_TYPE:
+           case ENUMERAL_TYPE:
+             /* A pointer-to-member function is represented as a special
+                RECORD_TYPE, so check for this first.  */
+             if (TYPE_PTRMEMFUNC_P (type))
+               write_pointer_to_member_type (type);
+             else
+               write_class_enum_type (type);
+             break;
+
+           case TYPENAME_TYPE:
+           case UNBOUND_CLASS_TEMPLATE:
+             /* We handle TYPENAME_TYPEs and UNBOUND_CLASS_TEMPLATEs like
+                ordinary nested names.  */
+             write_nested_name (TYPE_STUB_DECL (type));
+             break;
+
+           case POINTER_TYPE:
+             write_char ('P');
+             write_type (TREE_TYPE (type));
+             break;
+
+           case REFERENCE_TYPE:
+             if (TYPE_REF_IS_RVALUE (type))
+               write_char('O');
+              else
+                write_char ('R');
+             write_type (TREE_TYPE (type));
+             break;
+
+           case TEMPLATE_TYPE_PARM:
+           case TEMPLATE_PARM_INDEX:
+             write_template_param (type);
+             break;
+
+           case TEMPLATE_TEMPLATE_PARM:
+             write_template_template_param (type);
+             break;
+
+           case BOUND_TEMPLATE_TEMPLATE_PARM:
+             write_template_template_param (type);
+             write_template_args
+               (TI_ARGS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (type)));
+             break;
+
+           case VECTOR_TYPE:
+             write_string ("U8__vector");
+             write_type (TREE_TYPE (type));
+             break;
+
+            case TYPE_PACK_EXPANSION:
+              write_string ("U10__variadic");
+              write_type (PACK_EXPANSION_PATTERN (type));
+              break;
+
+            case DECLTYPE_TYPE:
+              write_char ('D');
+              if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type))
+                write_char ('t');
+              else
+                write_char ('T');
+              write_expression (DECLTYPE_TYPE_EXPR (type));
+              write_char ('E');
+              break;
+
+           case TYPEOF_TYPE:
+             sorry ("mangling typeof, use decltype instead");
+             break;
+
+           default:
+             gcc_unreachable ();
+           }
        }
     }
 
@@ -1666,7 +1716,7 @@ write_CV_qualifiers_for_type (const tree type)
 
        "In cases where multiple order-insensitive qualifiers are
        present, they should be ordered 'K' (closest to the base type),
-       'V', 'r', and 'U' (farthest from the base type) ..."  
+       'V', 'r', and 'U' (farthest from the base type) ..."
 
      Note that we do not use cp_type_quals below; given "const
      int[3]", the "const" is emitted with the "int", not with the
@@ -1691,31 +1741,31 @@ write_CV_qualifiers_for_type (const tree type)
   return num_qualifiers;
 }
 
-/* Non-terminal <builtin-type>. 
-
-     <builtin-type> ::= v   # void 
-                    ::= b   # bool
-                    ::= w   # wchar_t
-                    ::= 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          [not supported]
-                    ::= u <source-name>  # vendor extended type */
-
-static void 
+/* Non-terminal <builtin-type>.
+
+     <builtin-type> ::= v   # void
+                   ::= b   # bool
+                   ::= w   # wchar_t
+                   ::= 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          [not supported]
+                   ::= u <source-name>  # vendor extended type */
+
+static void
 write_builtin_type (tree type)
 {
   switch (TREE_CODE (type))
@@ -1729,10 +1779,6 @@ write_builtin_type (tree type)
       break;
 
     case INTEGER_TYPE:
-      /* If this is size_t, get the underlying int type.  */
-      if (TYPE_IS_SIZETYPE (type))
-       type = TYPE_DOMAIN (type);
-
       /* TYPE may still be wchar_t, since that isn't in
         integer_type_nodes.  */
       if (type == wchar_type_node)
@@ -1757,15 +1803,34 @@ write_builtin_type (tree type)
            {
              tree t = c_common_type_for_mode (TYPE_MODE (type),
                                               TYPE_UNSIGNED (type));
-             if (type == t)
+             if (type != t)
                {
-                 gcc_assert (TYPE_PRECISION (type) == 128);
-                 write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
+                 type = t;
+                 goto iagain;
                }
+
+             if (TYPE_PRECISION (type) == 128)
+               write_char (TYPE_UNSIGNED (type) ? 'o' : 'n');
              else
                {
-                 type = t;
-                 goto iagain;
+                 /* Allow for cases where TYPE is not one of the shared
+                    integer type nodes and write a "vendor extended builtin
+                    type" with a name the form intN or uintN, respectively.
+                    Situations like this can happen if you have an
+                    __attribute__((__mode__(__SI__))) type and use exotic
+                    switches like '-mint8' on AVR.  Of course, this is
+                    undefined by the C++ ABI (and '-mint8' is not even
+                    Standard C conforming), but when using such special
+                    options you're pretty much in nowhere land anyway.  */
+                 const char *prefix;
+                 char prec[11];        /* up to ten digits for an unsigned */
+
+                 prefix = TYPE_UNSIGNED (type) ? "uint" : "int";
+                 sprintf (prec, "%u", (unsigned) TYPE_PRECISION (type));
+                 write_char ('u');     /* "vendor extended builtin type" */
+                 write_unsigned_number (strlen (prefix) + strlen (prec));
+                 write_string (prefix);
+                 write_string (prec);
                }
            }
        }
@@ -1818,13 +1883,13 @@ write_function_type (const tree type)
 
        extern "C" typedef void function_t();
        function_t f; // f has C++ linkage, but its type is
-                     // `extern "C"'
+                    // `extern "C"'
 
        typedef void function_t();
        extern "C" function_t f; // Vice versa.
 
      See [dcl.link].  */
-  write_bare_function_type (type, /*include_return_type_p=*/1, 
+  write_bare_function_type (type, /*include_return_type_p=*/1,
                            /*decl=*/NULL);
   write_char ('E');
 }
@@ -1834,20 +1899,42 @@ write_function_type (const tree type)
    is mangled before the parameter types.  If non-NULL, DECL is
    FUNCTION_DECL for the function whose type is being emitted.
 
-     <bare-function-type> ::= </signature/ type>+  */
+   If DECL is a member of a Java type, then a literal 'J'
+   is output and the return type is mangled as if INCLUDE_RETURN_TYPE
+   were nonzero.
+
+     <bare-function-type> ::= [J]</signature/ type>+  */
 
 static void
 write_bare_function_type (const tree type, const int include_return_type_p,
                          const tree decl)
 {
+  int java_method_p;
+
   MANGLE_TRACE_TREE ("bare-function-type", type);
 
+  /* Detect Java methods and emit special encoding.  */
+  if (decl != NULL
+      && DECL_FUNCTION_MEMBER_P (decl)
+      && TYPE_FOR_JAVA (DECL_CONTEXT (decl))
+      && !DECL_CONSTRUCTOR_P (decl)
+      && !DECL_DESTRUCTOR_P (decl)
+      && !DECL_CONV_FN_P (decl))
+    {
+      java_method_p = 1;
+      write_char ('J');
+    }
+  else
+    {
+      java_method_p = 0;
+    }
+
   /* Mangle the return type, if requested.  */
-  if (include_return_type_p)
+  if (include_return_type_p || java_method_p)
     write_type (TREE_TYPE (type));
 
   /* Now mangle the types of the arguments.  */
-  write_method_parms (TYPE_ARG_TYPES (type), 
+  write_method_parms (TYPE_ARG_TYPES (type),
                      TREE_CODE (type) == METHOD_TYPE,
                      decl);
 }
@@ -1869,10 +1956,10 @@ write_method_parms (tree parm_types, const int method_p, const tree decl)
   int varargs_p = 1;
 
   /* If this is a member function, skip the first arg, which is the
-     this pointer.  
+     this pointer.
        "Member functions do not encode the type of their implicit this
-       parameter."  
-  
+       parameter."
+
      Similarly, there's no need to mangle artificial parameters, like
      the VTT parameters for constructors and destructors.  */
   if (method_p)
@@ -1887,8 +1974,8 @@ write_method_parms (tree parm_types, const int method_p, const tree decl)
        }
     }
 
-  for (first_parm_type = parm_types; 
-       parm_types; 
+  for (first_parm_type = parm_types;
+       parm_types;
        parm_types = TREE_CHAIN (parm_types))
     {
       tree parm = TREE_VALUE (parm_types);
@@ -1916,7 +2003,7 @@ write_method_parms (tree parm_types, const int method_p, const tree decl)
 
 /* <class-enum-type> ::= <name>  */
 
-static void 
+static void
 write_class_enum_type (const tree type)
 {
   write_name (TYPE_NAME (type), /*ignore_local_scope=*/0);
@@ -1932,7 +2019,7 @@ write_template_args (tree args)
 {
   int i;
   int length = TREE_VEC_LENGTH (args);
-  
+
   MANGLE_TRACE_TREE ("template-args", args);
 
   write_char ('I');
@@ -1948,7 +2035,7 @@ write_template_args (tree args)
     }
   for (i = 0; i < length; ++i)
     write_template_arg (TREE_VEC_ELT (args, i));
-  
+
   write_char ('E');
 }
 
@@ -1957,10 +2044,10 @@ write_template_args (tree args)
                ::= <expr-primary>
 
    <expr-primary> ::= <template-param>
-                 ::= L <type> <value number> E  # literal
-                 ::= L <mangled-name> E         # external name  
-                  ::= sr <type> <unqualified-name>
-                  ::= sr <type> <unqualified-name> <template-args> */
+                 ::= L <type> <value number> E         # literal
+                 ::= L <mangled-name> E                # external name
+                 ::= sr <type> <unqualified-name>
+                 ::= sr <type> <unqualified-name> <template-args> */
 
 static void
 write_expression (tree expr)
@@ -1969,17 +2056,6 @@ write_expression (tree expr)
 
   code = TREE_CODE (expr);
 
-  /* Handle pointers-to-members by making them look like expression
-     nodes.  */
-  if (code == PTRMEM_CST)
-    {
-      expr = build_nt (ADDR_EXPR,
-                      build_nt (SCOPE_REF,
-                                PTRMEM_CST_CLASS (expr),
-                                PTRMEM_CST_MEMBER (expr)));
-      code = TREE_CODE (expr);
-    }
-
   /* Skip NOP_EXPRs.  They can occur when (say) a pointer argument
      is converted (via qualification conversions) to another
      type.  */
@@ -1990,14 +2066,32 @@ write_expression (tree expr)
       code = TREE_CODE (expr);
     }
 
+  if (code == BASELINK)
+    {
+      expr = BASELINK_FUNCTIONS (expr);
+      code = TREE_CODE (expr);
+    }
+
+  /* Handle pointers-to-members by making them look like expression
+     nodes.  */
+  if (code == PTRMEM_CST)
+    {
+      expr = build_nt (ADDR_EXPR,
+                      build_qualified_name (/*type=*/NULL_TREE,
+                                            PTRMEM_CST_CLASS (expr),
+                                            PTRMEM_CST_MEMBER (expr),
+                                            /*template_p=*/false));
+      code = TREE_CODE (expr);
+    }
+
   /* Handle template parameters.  */
-  if (code == TEMPLATE_TYPE_PARM 
+  if (code == TEMPLATE_TYPE_PARM
       || code == TEMPLATE_TEMPLATE_PARM
       || code == BOUND_TEMPLATE_TEMPLATE_PARM
       || code == TEMPLATE_PARM_INDEX)
     write_template_param (expr);
   /* Handle literals.  */
-  else if (TREE_CODE_CLASS (code) == 'c' 
+  else if (TREE_CODE_CLASS (code) == tcc_constant
           || (abi_version_at_least (2) && code == CONST_DECL))
     write_template_arg_literal (expr);
   else if (DECL_P (expr))
@@ -2010,7 +2104,7 @@ write_expression (tree expr)
       write_mangled_name (expr, false);
       write_char ('E');
     }
-  else if (TREE_CODE (expr) == SIZEOF_EXPR 
+  else if (TREE_CODE (expr) == SIZEOF_EXPR
           && TYPE_P (TREE_OPERAND (expr, 0)))
     {
       write_string ("st");
@@ -2070,7 +2164,7 @@ write_expression (tree expr)
                else if (assignment_operator_name_info[i].identifier
                         == member)
                  {
-                   mangled_name 
+                   mangled_name
                      = assignment_operator_name_info[i].mangled_name;
                    break;
                  }
@@ -2110,9 +2204,9 @@ write_expression (tree expr)
 
       switch (code)
        {
-        case CALL_EXPR:
-          sorry ("call_expr cannot be mangled due to a defect in the C++ ABI");
-          break;
+       case CALL_EXPR:
+         sorry ("call_expr cannot be mangled due to a defect in the C++ ABI");
+         break;
 
        case CAST_EXPR:
          write_type (TREE_TYPE (expr));
@@ -2131,7 +2225,7 @@ write_expression (tree expr)
          write_expression (TREE_OPERAND (expr, 0));
          break;
 
-         
+
        /* Handle pointers-to-members specially.  */
        case SCOPE_REF:
          write_type (TREE_OPERAND (expr, 0));
@@ -2159,17 +2253,17 @@ write_expression (tree expr)
          break;
 
        default:
-         for (i = 0; i < TREE_CODE_LENGTH (code); ++i)
+         for (i = 0; i < TREE_OPERAND_LENGTH (expr); ++i)
            {
              tree operand = TREE_OPERAND (expr, i);
-             /* As a GNU expression, the middle operand of a
+             /* As a GNU extension, the middle operand of a
                 conditional may be omitted.  Since expression
                 manglings are supposed to represent the input token
                 stream, there's no good way to mangle such an
                 expression without extending the C++ ABI.  */
              if (code == COND_EXPR && i == 1 && !operand)
                {
-                 error ("omitted middle operand to `?:' operand "
+                 error ("omitted middle operand to %<?:%> operand "
                         "cannot be mangled");
                  continue;
                }
@@ -2179,7 +2273,7 @@ write_expression (tree expr)
     }
 }
 
-/* Literal subcase of non-terminal <template-arg>.  
+/* Literal subcase of non-terminal <template-arg>.
 
      "Literal arguments, e.g. "A<42L>", are encoded with their type
      and value. Negative integer values are preceded with "n"; for
@@ -2197,7 +2291,7 @@ write_template_arg_literal (const tree value)
     case CONST_DECL:
       write_integer_cst (DECL_INITIAL (value));
       break;
-      
+
     case INTEGER_CST:
       gcc_assert (!same_type_p (TREE_TYPE (value), boolean_type_node)
                  || integer_zerop (value) || integer_onep (value));
@@ -2211,16 +2305,16 @@ write_template_arg_literal (const tree value)
     default:
       gcc_unreachable ();
     }
-  
+
   write_char ('E');
 }
 
-/* Non-terminal <template-arg>.  
+/* Non-terminal <template-arg>.
 
-     <template-arg> ::= <type>                        # type
-                    ::= L <type> </value/ number> E   # literal
-                    ::= LZ <name> E                   # external name
-                    ::= X <expression> E              # expression  */
+     <template-arg> ::= <type>                         # type
+                   ::= L <type> </value/ number> E     # literal
+                   ::= LZ <name> E                     # external name
+                   ::= X <expression> E                # expression  */
 
 static void
 write_template_arg (tree node)
@@ -2230,7 +2324,7 @@ write_template_arg (tree node)
   MANGLE_TRACE_TREE ("template-arg", node);
 
   /* A template template parameter's argument list contains TREE_LIST
-     nodes of which the value field is the the actual argument.  */
+     nodes of which the value field is the actual argument.  */
   if (code == TREE_LIST)
     {
       node = TREE_VALUE (node);
@@ -2241,7 +2335,7 @@ write_template_arg (tree node)
          code = TREE_CODE (node);
        }
     }
-  
+
   if (TREE_CODE (node) == NOP_EXPR
       && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
     {
@@ -2255,18 +2349,26 @@ write_template_arg (tree node)
        G.need_abi_warning = 1;
     }
 
-  if (TYPE_P (node))
+  if (ARGUMENT_PACK_P (node))
+    {
+      /* Expand the template argument pack. */
+      tree args = ARGUMENT_PACK_ARGS (node);
+      int i, length = TREE_VEC_LENGTH (args);
+      for (i = 0; i < length; ++i)
+        write_template_arg (TREE_VEC_ELT (args, i));
+    }
+  else if (TYPE_P (node))
     write_type (node);
   else if (code == TEMPLATE_DECL)
     /* A template appearing as a template arg is a template template arg.  */
     write_template_template_arg (node);
-  else if ((TREE_CODE_CLASS (code) == 'c' && code != PTRMEM_CST)
+  else if ((TREE_CODE_CLASS (code) == tcc_constant && code != PTRMEM_CST)
           || (abi_version_at_least (2) && code == CONST_DECL))
     write_template_arg_literal (node);
   else if (DECL_P (node))
     {
       /* Until ABI version 2, non-type template arguments of
-        enumeration type were mangled using their names.  */ 
+        enumeration type were mangled using their names.  */
       if (code == CONST_DECL && !abi_version_at_least (2))
        G.need_abi_warning = 1;
       write_char ('L');
@@ -2307,10 +2409,10 @@ write_template_template_arg (const tree decl)
 }
 
 
-/* Non-terminal <array-type>.  TYPE is an ARRAY_TYPE.  
+/* Non-terminal <array-type>.  TYPE is an ARRAY_TYPE.
 
-     <array-type> ::= A [</dimension/ number>] _ </element/ type>  
-                  ::= A <expression> _ </element/ type>
+     <array-type> ::= A [</dimension/ number>] _ </element/ type>
+                 ::= A <expression> _ </element/ type>
 
      "Array types encode the dimension (number of elements) and the
      element type. For variable length arrays, the dimension (but not
@@ -2342,7 +2444,7 @@ write_array_type (const tree type)
          if (!abi_version_at_least (2))
            {
              /* value_dependent_expression_p presumes nothing is
-                dependent when PROCESSING_TEMPLATE_DECL is zero.  */
+                dependent when PROCESSING_TEMPLATE_DECL is zero.  */
              ++processing_template_decl;
              if (!value_dependent_expression_p (max))
                G.need_abi_warning = 1;
@@ -2350,7 +2452,7 @@ write_array_type (const tree type)
            }
          write_expression (max);
        }
-      
+
     }
   write_char ('_');
   write_type (TREE_TYPE (type));
@@ -2412,7 +2514,7 @@ write_template_param (const tree parm)
 }
 
 /*  <template-template-param>
-                        ::= <template-param> 
+                       ::= <template-param>
                        ::= <substitution>  */
 
 static void
@@ -2425,7 +2527,7 @@ write_template_template_param (const tree parm)
      only the template.  */
   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
     {
-      template 
+      template
        = TI_TEMPLATE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm));
       if (find_substitution (template))
        return;
@@ -2438,10 +2540,10 @@ write_template_template_param (const tree parm)
     add_substitution (template);
 }
 
-/* Non-terminal <substitution>.  
+/* Non-terminal <substitution>.
 
       <substitution> ::= S <seq-id> _
-                     ::= S_  */
+                    ::= S_  */
 
 static void
 write_substitution (const int seq_id)
@@ -2461,7 +2563,7 @@ start_mangling (const tree entity, const bool ident_p)
 {
   G.entity = entity;
   G.need_abi_warning = false;
-  if (!ident_p) 
+  if (!ident_p)
     {
       obstack_free (&name_obstack, name_base);
       mangle_obstack = &name_obstack;
@@ -2479,12 +2581,12 @@ static inline const char *
 finish_mangling (const bool warn)
 {
   if (warn_abi && warn && G.need_abi_warning)
-    warning ("the mangled name of `%D' will change in a future "
+    warning (OPT_Wabi, "the mangled name of %qD will change in a future "
             "version of GCC",
             G.entity);
 
   /* Clear all the substitutions.  */
-  VARRAY_CLEAR (G.substitutions);
+  VEC_truncate (tree, G.substitutions, 0);
 
   /* Null-terminate the string.  */
   write_char ('\0');
@@ -2499,7 +2601,7 @@ init_mangle (void)
 {
   gcc_obstack_init (&name_obstack);
   name_base = obstack_alloc (&name_obstack, 0);
-  VARRAY_TREE_INIT (G.substitutions, 1, "mangling substitutions");
+  G.substitutions = NULL;
 
   /* Cache these identifiers for quick comparison when checking for
      standard substitutions.  */
@@ -2524,7 +2626,7 @@ mangle_decl_string (const tree decl)
     write_type (TREE_TYPE (decl));
   else
     write_mangled_name (decl, true);
-  
+
   result = finish_mangling (/*warn=*/true);
   if (DEBUG_MANGLE)
     fprintf (stderr, "mangle_decl_string = '%s'\n\n", result);
@@ -2537,7 +2639,7 @@ mangle_decl_string (const tree decl)
 static inline tree
 get_identifier_nocopy (const char *name)
 {
-  hashnode ht_node = ht_lookup (ident_hash, (const unsigned char *) name, 
+  hashnode ht_node = ht_lookup (ident_hash, (const unsigned char *) name,
                                strlen (name), HT_ALLOCED);
   return HT_IDENT_TO_GCC_IDENT (ht_node);
 }
@@ -2547,8 +2649,9 @@ get_identifier_nocopy (const char *name)
 void
 mangle_decl (const tree decl)
 {
-  SET_DECL_ASSEMBLER_NAME (decl, 
-                          get_identifier_nocopy (mangle_decl_string (decl)));
+  tree id = get_identifier_nocopy (mangle_decl_string (decl));
+  id = targetm.mangle_decl_assembler_name (decl, id);
+  SET_DECL_ASSEMBLER_NAME (decl, id);
 }
 
 /* Generate the mangled representation of TYPE.  */
@@ -2629,13 +2732,13 @@ mangle_vtt_for_type (const tree type)
 
 /* Return an identifier for a construction vtable group.  TYPE is
    the most derived class in the hierarchy; BINFO is the base
-   subobject for which this construction vtable group will be used.  
+   subobject for which this construction vtable group will be used.
 
    This mangling isn't part of the ABI specification; in the ABI
    specification, the vtable group is dumped in the same COMDAT as the
    main vtable, and is referenced only from that vtable, so it doesn't
    need an external name.  For binary formats without COMDAT sections,
-   though, we need external names for the vtable groups.  
+   though, we need external names for the vtable groups.
 
    We use the production
 
@@ -2662,10 +2765,10 @@ mangle_ctor_vtbl_for_type (const tree type, const tree binfo)
 }
 
 /* Mangle a this pointer or result pointer adjustment.
-   
+
    <call-offset> ::= h <fixed offset number> _
                 ::= v <fixed offset number> _ <virtual offset number> _ */
-   
+
 static void
 mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
 {
@@ -2693,21 +2796,20 @@ mangle_call_offset (const tree fixed_offset, const tree virtual_offset)
    for the this pointer, even if there is none.
 
    <special-name> ::= T <call-offset> <base encoding>
-                  ::= Tc <this_adjust call-offset> <result_adjust call-offset>
-                                       <base encoding>
-*/
+                 ::= Tc <this_adjust call-offset> <result_adjust call-offset>
+                                       <base encoding>  */
 
 tree
 mangle_thunk (tree fn_decl, const int this_adjusting, tree fixed_offset,
              tree virtual_offset)
 {
   const char *result;
-  
+
   start_mangling (fn_decl, /*ident_p=*/true);
 
   write_string ("_Z");
   write_char ('T');
-  
+
   if (!this_adjusting)
     {
       /* Covariant thunk with no this adjustment */
@@ -2751,7 +2853,7 @@ static GTY ((param_is (union tree_node))) htab_t conv_type_names;
 static hashval_t
 hash_type (const void *val)
 {
-  return (hashval_t) TYPE_UID (TREE_TYPE ((tree) val));
+  return (hashval_t) TYPE_UID (TREE_TYPE ((const_tree) val));
 }
 
 /* Compare VAL1 (a node in the table) with VAL2 (a TYPE).  */
@@ -2759,7 +2861,7 @@ hash_type (const void *val)
 static int
 compare_type (const void *val1, const void *val2)
 {
-  return TREE_TYPE ((tree) val1) == (tree) val2;
+  return TREE_TYPE ((const_tree) val1) == (const_tree) val2;
 }
 
 /* Return an identifier for the mangled unqualified name for a
@@ -2772,16 +2874,19 @@ mangle_conv_op_name_for_type (const tree type)
   void **slot;
   tree identifier;
 
-  if (conv_type_names == NULL) 
+  if (type == error_mark_node)
+    return error_mark_node;
+
+  if (conv_type_names == NULL)
     conv_type_names = htab_create_ggc (31, &hash_type, &compare_type, NULL);
 
-  slot = htab_find_slot_with_hash (conv_type_names, type, 
+  slot = htab_find_slot_with_hash (conv_type_names, type,
                                   (hashval_t) TYPE_UID (type), INSERT);
   identifier = (tree)*slot;
   if (!identifier)
     {
       char buffer[64];
-      
+
        /* Create a unique name corresponding to TYPE.  */
       sprintf (buffer, "operator %lu",
               (unsigned long) htab_elements (conv_type_names));
@@ -2796,7 +2901,7 @@ mangle_conv_op_name_for_type (const tree type)
       IDENTIFIER_OPNAME_P (identifier) = 1;
       IDENTIFIER_TYPENAME_P (identifier) = 1;
     }
-  
+
   return identifier;
 }