OSDN Git Service

* cp-demangle.c (cplus_demangle_type): decltype, pack expansion
[pf3gnuchains/gcc-fork.git] / libiberty / cp-demangle.c
index d664e5f..0ed8397 100644 (file)
@@ -1,5 +1,5 @@
 /* Demangler for g++ V3 ABI.
-   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
    Free Software Foundation, Inc.
    Written by Ian Lance Taylor <ian@wasabisystems.com>.
 
@@ -417,6 +417,9 @@ static struct demangle_component *d_lambda (struct d_info *);
 
 static struct demangle_component *d_unnamed_type (struct d_info *);
 
+static struct demangle_component *
+d_clone_suffix (struct d_info *, struct demangle_component *);
+
 static int
 d_add_substitution (struct d_info *, struct demangle_component *);
 
@@ -582,6 +585,12 @@ d_dump (struct demangle_component *dc, int indent)
     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
       printf ("hidden alias\n");
       break;
+    case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
+      printf ("transaction clone\n");
+      break;
+    case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
+      printf ("non-transaction clone\n");
+      break;
     case DEMANGLE_COMPONENT_RESTRICT:
       printf ("restrict\n");
       break;
@@ -729,7 +738,7 @@ cplus_demangle_fill_ctor (struct demangle_component *p,
   if (p == NULL
       || name == NULL
       || (int) kind < gnu_v3_complete_object_ctor
-      || (int) kind > gnu_v3_complete_object_allocating_ctor)
+      || (int) kind > gnu_v3_object_ctor_group)
     return 0;
   p->type = DEMANGLE_COMPONENT_CTOR;
   p->u.s_ctor.kind = kind;
@@ -748,7 +757,7 @@ cplus_demangle_fill_dtor (struct demangle_component *p,
   if (p == NULL
       || name == NULL
       || (int) kind < gnu_v3_deleting_dtor
-      || (int) kind > gnu_v3_base_object_dtor)
+      || (int) kind > gnu_v3_object_dtor_group)
     return 0;
   p->type = DEMANGLE_COMPONENT_DTOR;
   p->u.s_dtor.kind = kind;
@@ -802,6 +811,7 @@ d_make_comp (struct d_info *di, enum demangle_component_type type,
     case DEMANGLE_COMPONENT_LITERAL_NEG:
     case DEMANGLE_COMPONENT_COMPOUND_NAME:
     case DEMANGLE_COMPONENT_VECTOR_TYPE:
+    case DEMANGLE_COMPONENT_CLONE:
       if (left == NULL || right == NULL)
        return NULL;
       break;
@@ -819,6 +829,8 @@ d_make_comp (struct d_info *di, enum demangle_component_type type,
     case DEMANGLE_COMPONENT_GUARD:
     case DEMANGLE_COMPONENT_REFTEMP:
     case DEMANGLE_COMPONENT_HIDDEN_ALIAS:
+    case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
+    case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
     case DEMANGLE_COMPONENT_POINTER:
     case DEMANGLE_COMPONENT_REFERENCE:
     case DEMANGLE_COMPONENT_RVALUE_REFERENCE:
@@ -1034,7 +1046,7 @@ d_make_sub (struct d_info *di, const char *name, int len)
   return p;
 }
 
-/* <mangled-name> ::= _Z <encoding>
+/* <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
 
    TOP_LEVEL is non-zero when called at the top level.  */
 
@@ -1042,6 +1054,8 @@ CP_STATIC_IF_GLIBCPP_V3
 struct demangle_component *
 cplus_demangle_mangled_name (struct d_info *di, int top_level)
 {
+  struct demangle_component *p;
+
   if (! d_check_char (di, '_')
       /* Allow missing _ if not at toplevel to work around a
         bug in G++ abi-version=2 mangling; see the comment in
@@ -1050,7 +1064,18 @@ cplus_demangle_mangled_name (struct d_info *di, int top_level)
     return NULL;
   if (! d_check_char (di, 'Z'))
     return NULL;
-  return d_encoding (di, top_level);
+  p = d_encoding (di, top_level);
+
+  /* If at top level and parsing parameters, check for a clone
+     suffix.  */
+  if (top_level && (di->options & DMGL_PARAMS) != 0)
+    while (d_peek_char (di) == '.'
+          && (IS_LOWER (d_peek_next_char (di))
+              || d_peek_next_char (di) == '_'
+              || IS_DIGIT (d_peek_next_char (di))))
+      p = d_clone_suffix (di, p);
+
+  return p;
 }
 
 /* Return whether a function should have a return type.  The argument
@@ -1280,6 +1305,7 @@ d_nested_name (struct d_info *di)
 /* <prefix> ::= <prefix> <unqualified-name>
             ::= <template-prefix> <template-args>
             ::= <template-param>
+            ::= <decltype>
             ::=
             ::= <substitution>
 
@@ -1308,10 +1334,19 @@ d_prefix (struct d_info *di)
         <template-param> here.  */
 
       comb_type = DEMANGLE_COMPONENT_QUAL_NAME;
-      if (IS_DIGIT (peek)
+      if (peek == 'D')
+       {
+         char peek2 = d_peek_next_char (di);
+         if (peek2 == 'T' || peek2 == 't')
+           /* Decltype.  */
+           dc = cplus_demangle_type (di);
+         else
+           /* Destructor name.  */
+           dc = d_unqualified_name (di);
+       }
+      else if (IS_DIGIT (peek)
          || IS_LOWER (peek)
          || peek == 'C'
-         || peek == 'D'
          || peek == 'U'
          || peek == 'L')
        dc = d_unqualified_name (di);
@@ -1519,7 +1554,8 @@ d_identifier (struct d_info *di, int len)
 /* operator_name ::= many different two character encodings.
                  ::= cv <type>
                  ::= v <digit> <source-name>
-*/
+
+   This list is sorted for binary search.  */
 
 #define NL(s) s, (sizeof s) - 1
 
@@ -1531,6 +1567,8 @@ const struct demangle_operator_info cplus_demangle_operators[] =
   { "aa", NL ("&&"),        2 },
   { "ad", NL ("&"),         1 },
   { "an", NL ("&"),         2 },
+  { "at", NL ("alignof "),   1 },
+  { "az", NL ("alignof "),   1 },
   { "cl", NL ("()"),        2 },
   { "cm", NL (","),         2 },
   { "co", NL ("~"),         1 },
@@ -1576,8 +1614,6 @@ const struct demangle_operator_info cplus_demangle_operators[] =
   { "rs", NL (">>"),        2 },
   { "st", NL ("sizeof "),   1 },
   { "sz", NL ("sizeof "),   1 },
-  { "at", NL ("alignof "),   1 },
-  { "az", NL ("alignof "),   1 },
   { NULL, NULL, 0,          0 }
 };
 
@@ -1735,6 +1771,8 @@ d_java_resource (struct d_info *di)
                   ::= GR <name>
                  ::= GA <encoding>
                  ::= Gr <resource name>
+                 ::= GTt <encoding>
+                 ::= GTn <encoding>
 */
 
 static struct demangle_component *
@@ -1819,13 +1857,33 @@ d_special_name (struct d_info *di)
          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);
+         {
+           struct demangle_component *name = d_name (di);
+           return d_make_comp (di, DEMANGLE_COMPONENT_REFTEMP, name,
+                               d_number_component (di));
+         }
 
        case 'A':
          return d_make_comp (di, DEMANGLE_COMPONENT_HIDDEN_ALIAS,
                              d_encoding (di, 0), NULL);
 
+       case 'T':
+         switch (d_next_char (di))
+           {
+           case 'n':
+             return d_make_comp (di, DEMANGLE_COMPONENT_NONTRANSACTION_CLONE,
+                                 d_encoding (di, 0), NULL);
+           default:
+             /* ??? The proposal is that other letters (such as 'h') stand
+                for different variants of transaction cloning, such as
+                compiling directly for hardware transaction support.  But
+                they still should all be transactional clones of some sort
+                so go ahead and call them that.  */
+           case 't':
+             return d_make_comp (di, DEMANGLE_COMPONENT_TRANSACTION_CLONE,
+                                 d_encoding (di, 0), NULL);
+           }
+
        case 'r':
          return d_java_resource (di);
 
@@ -1909,6 +1967,9 @@ d_ctor_dtor_name (struct d_info *di)
          case '3':
            kind = gnu_v3_complete_object_allocating_ctor;
            break;
+         case '5':
+           kind = gnu_v3_object_ctor_group;
+           break;
          default:
            return NULL;
          }
@@ -1931,6 +1992,9 @@ d_ctor_dtor_name (struct d_info *di)
          case '2':
            kind = gnu_v3_base_object_dtor;
            break;
+         case '5':
+           kind = gnu_v3_object_dtor_group;
+           break;
          default:
            return NULL;
          }
@@ -2179,12 +2243,14 @@ cplus_demangle_type (struct d_info *di)
                             d_expression (di), NULL);
          if (ret && d_next_char (di) != 'E')
            ret = NULL;
+         can_subst = 1;
          break;
          
        case 'p':
          /* Pack expansion.  */
          ret = d_make_comp (di, DEMANGLE_COMPONENT_PACK_EXPANSION,
                             cplus_demangle_type (di), NULL);
+         can_subst = 1;
          break;
          
        case 'f':
@@ -2235,6 +2301,7 @@ cplus_demangle_type (struct d_info *di)
 
        case 'v':
          ret = d_vector_type (di);
+         can_subst = 1;
          break;
 
         case 'n':
@@ -2267,8 +2334,10 @@ static struct demangle_component **
 d_cv_qualifiers (struct d_info *di,
                  struct demangle_component **pret, int member_fn)
 {
+  struct demangle_component **pstart;
   char peek;
 
+  pstart = pret;
   peek = d_peek_char (di);
   while (peek == 'r' || peek == 'V' || peek == 'K')
     {
@@ -2305,6 +2374,28 @@ d_cv_qualifiers (struct d_info *di,
       peek = d_peek_char (di);
     }
 
+  if (!member_fn && peek == 'F')
+    {
+      while (pstart != pret)
+       {
+         switch ((*pstart)->type)
+           {
+           case DEMANGLE_COMPONENT_RESTRICT:
+             (*pstart)->type = DEMANGLE_COMPONENT_RESTRICT_THIS;
+             break;
+           case DEMANGLE_COMPONENT_VOLATILE:
+             (*pstart)->type = DEMANGLE_COMPONENT_VOLATILE_THIS;
+             break;
+           case DEMANGLE_COMPONENT_CONST:
+             (*pstart)->type = DEMANGLE_COMPONENT_CONST_THIS;
+             break;
+           default:
+             break;
+           }
+         pstart = &d_left (*pstart);
+       }
+    }
+
   return pret;
 }
 
@@ -2344,7 +2435,7 @@ d_parmlist (struct d_info *di)
       struct demangle_component *type;
 
       char peek = d_peek_char (di);
-      if (peek == '\0' || peek == 'E')
+      if (peek == '\0' || peek == 'E' || peek == '.')
        break;
       type = cplus_demangle_type (di);
       if (type == NULL)
@@ -2738,10 +2829,18 @@ d_expression (struct d_info *di)
       /* Function parameter used in a late-specified return type.  */
       int index;
       d_advance (di, 2);
-      index = d_compact_number (di);
-      if (index < 0)
-       return NULL;
-
+      if (d_peek_char (di) == 'T')
+       {
+         /* 'this' parameter.  */
+         d_advance (di, 1);
+         index = 0;
+       }
+      else
+       {
+         index = d_compact_number (di) + 1;
+         if (index == 0)
+           return NULL;
+       }
       return d_make_function_param (di, index);
     }
   else if (IS_DIGIT (peek)
@@ -3064,6 +3163,33 @@ d_unnamed_type (struct d_info *di)
   return ret;
 }
 
+/* <clone-suffix> ::= [ . <clone-type-identifier> ] [ . <nonnegative number> ]*
+*/
+
+static struct demangle_component *
+d_clone_suffix (struct d_info *di, struct demangle_component *encoding)
+{
+  const char *suffix = d_str (di);
+  const char *pend = suffix;
+  struct demangle_component *n;
+
+  if (*pend == '.' && (IS_LOWER (pend[1]) || pend[1] == '_'))
+    {
+      pend += 2;
+      while (IS_LOWER (*pend) || *pend == '_')
+       ++pend;
+    }
+  while (*pend == '.' && IS_DIGIT (pend[1]))
+    {
+      pend += 2;
+      while (IS_DIGIT (*pend))
+       ++pend;
+    }
+  d_advance (di, pend - suffix);
+  n = d_make_name (di, suffix, pend - suffix);
+  return d_make_comp (di, DEMANGLE_COMPONENT_CLONE, encoding, n);
+}
+
 /* Add a new substitution.  */
 
 static int
@@ -3298,6 +3424,7 @@ d_print_init (struct d_print_info *dpi, demangle_callbackref callback,
   dpi->last_char = '\0';
   dpi->templates = NULL;
   dpi->modifiers = NULL;
+  dpi->pack_index = 0;
   dpi->flush_count = 0;
 
   dpi->callback = callback;
@@ -3834,7 +3961,9 @@ d_print_comp (struct d_print_info *dpi, int options,
       return;
 
     case DEMANGLE_COMPONENT_REFTEMP:
-      d_append_string (dpi, "reference temporary for ");
+      d_append_string (dpi, "reference temporary #");
+      d_print_comp (dpi, options, d_right (dc));
+      d_append_string (dpi, " for ");
       d_print_comp (dpi, options, d_left (dc));
       return;
 
@@ -3843,6 +3972,16 @@ d_print_comp (struct d_print_info *dpi, int options,
       d_print_comp (dpi, options, d_left (dc));
       return;
 
+    case DEMANGLE_COMPONENT_TRANSACTION_CLONE:
+      d_append_string (dpi, "transaction clone for ");
+      d_print_comp (dpi, options, d_left (dc));
+      return;
+
+    case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE:
+      d_append_string (dpi, "non-transaction clone for ");
+      d_print_comp (dpi, options, d_left (dc));
+      return;
+
     case DEMANGLE_COMPONENT_SUB_STD:
       d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
       return;
@@ -3885,6 +4024,13 @@ d_print_comp (struct d_print_info *dpi, int options,
            struct demangle_component *a = d_lookup_template_argument (dpi, sub);
            if (a && a->type == DEMANGLE_COMPONENT_TEMPLATE_ARGLIST)
              a = d_index_template_argument (a, dpi->pack_index);
+
+           if (a == NULL)
+             {
+               d_print_error (dpi);
+               return;
+             }
+
            sub = a;
          }
 
@@ -4139,7 +4285,46 @@ d_print_comp (struct d_print_info *dpi, int options,
       return;
 
     case DEMANGLE_COMPONENT_UNARY:
-      if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
+      if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
+         && d_left (dc)->u.s_operator.op->len == 1
+         && d_left (dc)->u.s_operator.op->name[0] == '&'
+         && d_right (dc)->type == DEMANGLE_COMPONENT_TYPED_NAME
+         && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_QUAL_NAME
+         && d_right (d_right (dc))->type == DEMANGLE_COMPONENT_FUNCTION_TYPE)
+       {
+         /* Address of a function (therefore in an expression context) must
+            have its argument list suppressed.
+
+            unary operator ... dc
+              operator & ... d_left (dc)
+              typed name ... d_right (dc)
+                qualified name ... d_left (d_right (dc))
+                  <names>
+                function type ... d_right (d_right (dc))
+                  argument list
+                    <arguments>  */
+
+         d_print_expr_op (dpi, options, d_left (dc));
+         d_print_comp (dpi, options, d_left (d_right (dc)));
+         return;
+       }
+      else if (d_left (dc)->type == DEMANGLE_COMPONENT_OPERATOR
+              && d_left (dc)->u.s_operator.op->len == 1
+              && d_left (dc)->u.s_operator.op->name[0] == '&'
+              && d_right (dc)->type == DEMANGLE_COMPONENT_QUAL_NAME)
+       {
+         /* Keep also already processed variant without the argument list.
+
+            unary operator ... dc
+              operator & ... d_left (dc)
+              qualified name ... d_right (dc)
+                <names>  */
+
+         d_print_expr_op (dpi, options, d_left (dc));
+         d_print_comp (dpi, options, d_right (dc));
+         return;
+       }
+      else if (d_left (dc)->type != DEMANGLE_COMPONENT_CAST)
        d_print_expr_op (dpi, options, d_left (dc));
       else
        {
@@ -4165,7 +4350,21 @@ d_print_comp (struct d_print_info *dpi, int options,
          && d_left (dc)->u.s_operator.op->name[0] == '>')
        d_append_char (dpi, '(');
 
-      d_print_subexpr (dpi, options, d_left (d_right (dc)));
+      if (strcmp (d_left (dc)->u.s_operator.op->code, "cl") == 0
+          && d_left (d_right (dc))->type == DEMANGLE_COMPONENT_TYPED_NAME)
+       {
+         /* Function call used in an expression should not have printed types
+            of the function arguments.  Values of the function arguments still
+            get printed below.  */
+
+         const struct demangle_component *func = d_left (d_right (dc));
+
+         if (d_right (func)->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
+           d_print_error (dpi);
+         d_print_subexpr (dpi, options, d_left (func));
+       }
+      else
+       d_print_subexpr (dpi, options, d_left (d_right (dc)));
       if (strcmp (d_left (dc)->u.s_operator.op->code, "ix") == 0)
        {
          d_append_char (dpi, '[');
@@ -4347,9 +4546,17 @@ d_print_comp (struct d_print_info *dpi, int options,
       return;
 
     case DEMANGLE_COMPONENT_FUNCTION_PARAM:
-      d_append_string (dpi, "{parm#");
-      d_append_num (dpi, dc->u.s_number.number + 1);
-      d_append_char (dpi, '}');
+      {
+       long num = dc->u.s_number.number;
+       if (num == 0)
+         d_append_string (dpi, "this");
+       else
+         {
+           d_append_string (dpi, "{parm#");
+           d_append_num (dpi, num);
+           d_append_char (dpi, '}');
+         }
+      }
       return;
 
     case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
@@ -4376,6 +4583,13 @@ d_print_comp (struct d_print_info *dpi, int options,
       d_append_char (dpi, '}');
       return;
 
+    case DEMANGLE_COMPONENT_CLONE:
+      d_print_comp (dpi, options, d_left (dc));
+      d_append_string (dpi, " [clone ");
+      d_print_comp (dpi, options, d_right (dc));
+      d_append_char (dpi, ']');
+      return;
+
     default:
       d_print_error (dpi);
       return;