(linenum→info "unix/slp.c:2238")

binutils/2.18/libiberty/cp-demangle.c

    1: /* Demangler for g++ V3 ABI.
    2:    Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
    3:    Written by Ian Lance Taylor <ian@wasabisystems.com>.
    4: 
    5:    This file is part of the libiberty library, which is part of GCC.
    6: 
    7:    This file is free software; you can redistribute it and/or modify
    8:    it under the terms of the GNU General Public License as published by
    9:    the Free Software Foundation; either version 2 of the License, or
   10:    (at your option) any later version.
   11: 
   12:    In addition to the permissions in the GNU General Public License, the
   13:    Free Software Foundation gives you unlimited permission to link the
   14:    compiled version of this file into combinations with other programs,
   15:    and to distribute those combinations without any restriction coming
   16:    from the use of this file.  (The General Public License restrictions
   17:    do apply in other respects; for example, they cover modification of
   18:    the file, and distribution when not linked into a combined
   19:    executable.)
   20: 
   21:    This program is distributed in the hope that it will be useful,
   22:    but WITHOUT ANY WARRANTY; without even the implied warranty of
   23:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   24:    GNU General Public License for more details.
   25: 
   26:    You should have received a copy of the GNU General Public License
   27:    along with this program; if not, write to the Free Software
   28:    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 
   29: */
   30: 
   31: /* This code implements a demangler for the g++ V3 ABI.  The ABI is
   32:    described on this web page:
   33:        http://www.codesourcery.com/cxx-abi/abi.html#mangling
   34: 
   35:    This code was written while looking at the demangler written by
   36:    Alex Samuel <samuel@codesourcery.com>.
   37: 
   38:    This code first pulls the mangled name apart into a list of
   39:    components, and then walks the list generating the demangled
   40:    name.
   41: 
   42:    This file will normally define the following functions, q.v.:
   43:       char *cplus_demangle_v3(const char *mangled, int options)
   44:       char *java_demangle_v3(const char *mangled)
   45:       int cplus_demangle_v3_callback(const char *mangled, int options,
   46:                                      demangle_callbackref callback)
   47:       int java_demangle_v3_callback(const char *mangled,
   48:                                     demangle_callbackref callback)
   49:       enum gnu_v3_ctor_kinds is_gnu_v3_mangled_ctor (const char *name)
   50:       enum gnu_v3_dtor_kinds is_gnu_v3_mangled_dtor (const char *name)
   51: 
   52:    Also, the interface to the component list is public, and defined in
   53:    demangle.h.  The interface consists of these types, which are
   54:    defined in demangle.h:
   55:       enum demangle_component_type
   56:       struct demangle_component
   57:       demangle_callbackref
   58:    and these functions defined in this file:
   59:       cplus_demangle_fill_name
   60:       cplus_demangle_fill_extended_operator
   61:       cplus_demangle_fill_ctor
   62:       cplus_demangle_fill_dtor
   63:       cplus_demangle_print
   64:       cplus_demangle_print_callback
   65:    and other functions defined in the file cp-demint.c.
   66: 
   67:    This file also defines some other functions and variables which are
   68:    only to be used by the file cp-demint.c.
   69: 
   70:    Preprocessor macros you can define while compiling this file:
   71: 
   72:    IN_LIBGCC2
   73:       If defined, this file defines the following functions, q.v.:
   74:          char *__cxa_demangle (const char *mangled, char *buf, size_t *len,
   75:                                int *status)
   76:          int __gcclibcxx_demangle_callback (const char *,
   77:                                             void (*)
   78:                                               (const char *, size_t, void *),
   79:                                             void *)
   80:       instead of cplus_demangle_v3[_callback]() and
   81:       java_demangle_v3[_callback]().
   82: 
   83:    IN_GLIBCPP_V3
   84:       If defined, this file defines only __cxa_demangle() and
   85:       __gcclibcxx_demangle_callback(), and no other publically visible
   86:       functions or variables.
   87: 
   88:    STANDALONE_DEMANGLER
   89:       If defined, this file defines a main() function which demangles
   90:       any arguments, or, if none, demangles stdin.
   91: 
   92:    CP_DEMANGLE_DEBUG
   93:       If defined, turns on debugging mode, which prints information on
   94:       stdout about the mangled string.  This is not generally useful.
   95: */
   96: 
   97: #if defined (_AIX) && !defined (__GNUC__)
   98:  #pragma alloca
   99: #endif
  100: 
  101: #ifdef HAVE_CONFIG_H
  102: #include "config.h"
  103: #endif
  104: 
  105: #include <stdio.h>
  106: 
  107: #ifdef HAVE_STDLIB_H
  108: #include <stdlib.h>
  109: #endif
  110: #ifdef HAVE_STRING_H
  111: #include <string.h>
  112: #endif
  113: 
  114: #ifdef HAVE_ALLOCA_H
  115: # include <alloca.h>
  116: #else
  117: # ifndef alloca
  118: #  ifdef __GNUC__
  119: #   define alloca __builtin_alloca
  120: #  else
  121: extern char *alloca ();
  122: #  endif /* __GNUC__ */
  123: # endif /* alloca */
  124: #endif /* HAVE_ALLOCA_H */
  125: 
  126: #include "ansidecl.h"
  127: #include "libiberty.h"
  128: #include "demangle.h"
  129: #include "cp-demangle.h"
  130: 
  131: /* If IN_GLIBCPP_V3 is defined, some functions are made static.  We
  132:    also rename them via #define to avoid compiler errors when the
  133:    static definition conflicts with the extern declaration in a header
  134:    file.  */
  135: #ifdef IN_GLIBCPP_V3
  136: 
  137: #define CP_STATIC_IF_GLIBCPP_V3 static
  138: 
  139: #define cplus_demangle_fill_name d_fill_name
  140: static int d_fill_name (struct demangle_component *, const char *, int);
  141: 
  142: #define cplus_demangle_fill_extended_operator d_fill_extended_operator
  143: static int
  144: d_fill_extended_operator (struct demangle_component *, int,
  145:                           struct demangle_component *);
  146: 
  147: #define cplus_demangle_fill_ctor d_fill_ctor
  148: static int
  149: d_fill_ctor (struct demangle_component *, enum gnu_v3_ctor_kinds,
  150:              struct demangle_component *);
  151: 
  152: #define cplus_demangle_fill_dtor d_fill_dtor
  153: static int
  154: d_fill_dtor (struct demangle_component *, enum gnu_v3_dtor_kinds,
  155:              struct demangle_component *);
  156: 
  157: #define cplus_demangle_mangled_name d_mangled_name
  158: static struct demangle_component *d_mangled_name (struct d_info *, int);
  159: 
  160: #define cplus_demangle_type d_type
  161: static struct demangle_component *d_type (struct d_info *);
  162: 
  163: #define cplus_demangle_print d_print
  164: static char *d_print (int, const struct demangle_component *, int, size_t *);
  165: 
  166: #define cplus_demangle_print_callback d_print_callback
  167: static int d_print_callback (int, const struct demangle_component *,
  168:                              demangle_callbackref, void *);
  169: 
  170: #define cplus_demangle_init_info d_init_info
  171: static void d_init_info (const char *, int, size_t, struct d_info *);
  172: 
  173: #else /* ! defined(IN_GLIBCPP_V3) */
  174: #define CP_STATIC_IF_GLIBCPP_V3
  175: #endif /* ! defined(IN_GLIBCPP_V3) */
  176: 
  177: /* See if the compiler supports dynamic arrays.  */
  178: 
  179: #ifdef __GNUC__
  180: #define CP_DYNAMIC_ARRAYS
  181: #else
  182: #ifdef __STDC__
  183: #ifdef __STDC_VERSION__
  184: #if __STDC_VERSION__ >= 199901L
  185: #define CP_DYNAMIC_ARRAYS
  186: #endif /* __STDC__VERSION >= 199901L */
  187: #endif /* defined (__STDC_VERSION__) */
  188: #endif /* defined (__STDC__) */
  189: #endif /* ! defined (__GNUC__) */
  190: 
  191: /* We avoid pulling in the ctype tables, to prevent pulling in
  192:    additional unresolved symbols when this code is used in a library.
  193:    FIXME: Is this really a valid reason?  This comes from the original
  194:    V3 demangler code.
  195: 
  196:    As of this writing this file has the following undefined references
  197:    when compiled with -DIN_GLIBCPP_V3: realloc, free, memcpy, strcpy,
  198:    strcat, strlen.  */
  199: 
  200: #define IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
  201: #define IS_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
  202: #define IS_LOWER(c) ((c) >= 'a' && (c) <= 'z')
  203: 
  204: /* The prefix prepended by GCC to an identifier represnting the
  205:    anonymous namespace.  */
  206: #define ANONYMOUS_NAMESPACE_PREFIX "_GLOBAL_"
  207: #define ANONYMOUS_NAMESPACE_PREFIX_LEN \
  208:   (sizeof (ANONYMOUS_NAMESPACE_PREFIX) - 1)
  209: 
  210: /* Information we keep for the standard substitutions.  */
  211: 
  212: struct d_standard_sub_info
  213: {
  214:   /* The code for this substitution.  */
  215:   char code;
  216:   /* The simple string it expands to.  */
  217:   const char *simple_expansion;
  218:   /* The length of the simple expansion.  */
  219:   int simple_len;
  220:   /* The results of a full, verbose, expansion.  This is used when
  221:      qualifying a constructor/destructor, or when in verbose mode.  */
  222:   const char *full_expansion;
  223:   /* The length of the full expansion.  */
  224:   int full_len;
  225:   /* What to set the last_name field of d_info to; NULL if we should
  226:      not set it.  This is only relevant when qualifying a
  227:      constructor/destructor.  */
  228:   const char *set_last_name;
  229:   /* The length of set_last_name.  */
  230:   int set_last_name_len;
  231: };
  232: 
  233: /* Accessors for subtrees of struct demangle_component.  */
  234: 
  235: #define d_left(dc) ((dc)->u.s_binary.left)
  236: #define d_right(dc) ((dc)->u.s_binary.right)
  237: 
  238: /* A list of templates.  This is used while printing.  */
  239: 
  240: struct d_print_template
  241: {
  242:   /* Next template on the list.  */
  243:   struct d_print_template *next;
  244:   /* This template.  */
  245:   const struct demangle_component *template_decl;
  246: };
  247: 
  248: /* A list of type modifiers.  This is used while printing.  */
  249: 
  250: struct d_print_mod
  251: {
  252:   /* Next modifier on the list.  These are in the reverse of the order
  253:      in which they appeared in the mangled string.  */
  254:   struct d_print_mod *next;
  255:   /* The modifier.  */
  256:   const struct demangle_component *mod;
  257:   /* Whether this modifier was printed.  */
  258:   int printed;
  259:   /* The list of templates which applies to this modifier.  */
  260:   struct d_print_template *templates;
  261: };
  262: 
  263: /* We use these structures to hold information during printing.  */
  264: 
  265: struct d_growable_string
  266: {
  267:   /* Buffer holding the result.  */
  268:   char *buf;
  269:   /* Current length of data in buffer.  */
  270:   size_t len;
  271:   /* Allocated size of buffer.  */
  272:   size_t alc;
  273:   /* Set to 1 if we had a memory allocation failure.  */
  274:   int allocation_failure;
  275: };
  276: 
  277: enum { D_PRINT_BUFFER_LENGTH = 256 };
  278: struct d_print_info
  279: {
  280:   /* The options passed to the demangler.  */
  281:   int options;
  282:   /* Fixed-length allocated buffer for demangled data, flushed to the
  283:      callback with a NUL termination once full.  */
  284:   char buf[D_PRINT_BUFFER_LENGTH];
  285:   /* Current length of data in buffer.  */
  286:   size_t len;
  287:   /* The last character printed, saved individually so that it survives
  288:      any buffer flush.  */
  289:   char last_char;
  290:   /* Callback function to handle demangled buffer flush.  */
  291:   demangle_callbackref callback;
  292:   /* Opaque callback argument.  */
  293:   void *opaque;
  294:   /* The current list of templates, if any.  */
  295:   struct d_print_template *templates;
  296:   /* The current list of modifiers (e.g., pointer, reference, etc.),
  297:      if any.  */
  298:   struct d_print_mod *modifiers;
  299:   /* Set to 1 if we saw a demangling error.  */
  300:   int demangle_failure;
  301: };
  302: 
  303: #ifdef CP_DEMANGLE_DEBUG
  304: static void d_dump (struct demangle_component *, int);
  305: #endif
  306: 
  307: static struct demangle_component *
  308: d_make_empty (struct d_info *);
  309: 
  310: static struct demangle_component *
  311: d_make_comp (struct d_info *, enum demangle_component_type,
  312:              struct demangle_component *,
  313:              struct demangle_component *);
  314: 
  315: static struct demangle_component *
  316: d_make_name (struct d_info *, const char *, int);
  317: 
  318: static struct demangle_component *
  319: d_make_builtin_type (struct d_info *,
  320:                      const struct demangle_builtin_type_info *);
  321: 
  322: static struct demangle_component *
  323: d_make_operator (struct d_info *,
  324:                  const struct demangle_operator_info *);
  325: 
  326: static struct demangle_component *
  327: d_make_extended_operator (struct d_info *, int,
  328:                           struct demangle_component *);
  329: 
  330: static struct demangle_component *
  331: d_make_ctor (struct d_info *, enum gnu_v3_ctor_kinds,
  332:              struct demangle_component *);
  333: 
  334: static struct demangle_component *
  335: d_make_dtor (struct d_info *, enum gnu_v3_dtor_kinds,
  336:              struct demangle_component *);
  337: 
  338: static struct demangle_component *
  339: d_make_template_param (struct d_info *, long);
  340: 
  341: static struct demangle_component *
  342: d_make_sub (struct d_info *, const char *, int);
  343: 
  344: static int
  345: has_return_type (struct demangle_component *);
  346: 
  347: static int
  348: is_ctor_dtor_or_conversion (struct demangle_component *);
  349: 
  350: static struct demangle_component *d_encoding (struct d_info *, int);
  351: 
  352: static struct demangle_component *d_name (struct d_info *);
  353: 
  354: static struct demangle_component *d_nested_name (struct d_info *);
  355: 
  356: static struct demangle_component *d_prefix (struct d_info *);
  357: 
  358: static struct demangle_component *d_unqualified_name (struct d_info *);
  359: 
  360: static struct demangle_component *d_source_name (struct d_info *);
  361: 
  362: static long d_number (struct d_info *);
  363: 
  364: static struct demangle_component *d_identifier (struct d_info *, int);
  365: 
  366: static struct demangle_component *d_operator_name (struct d_info *);
  367: 
  368: static struct demangle_component *d_special_name (struct d_info *);
  369: 
  370: static int d_call_offset (struct d_info *, int);
  371: 
  372: static struct demangle_component *d_ctor_dtor_name (struct d_info *);
  373: 
  374: static struct demangle_component **
  375: d_cv_qualifiers (struct d_info *, struct demangle_component **, int);
  376: 
  377: static struct demangle_component *
  378: d_function_type (struct d_info *);
  379: 
  380: static struct demangle_component *
  381: d_bare_function_type (struct d_info *, int);
  382: 
  383: static struct demangle_component *
  384: d_class_enum_type (struct d_info *);
  385: 
  386: static struct demangle_component *d_array_type (struct d_info *);
  387: 
  388: static struct demangle_component *
  389: d_pointer_to_member_type (struct d_info *);
  390: 
  391: static struct demangle_component *
  392: d_template_param (struct d_info *);
  393: 
  394: static struct demangle_component *d_template_args (struct d_info *);
  395: 
  396: static struct demangle_component *
  397: d_template_arg (struct d_info *);
  398: 
  399: static struct demangle_component *d_expression (struct d_info *);
  400: 
  401: static struct demangle_component *d_expr_primary (struct d_info *);
  402: 
  403: static struct demangle_component *d_local_name (struct d_info *);
  404: 
  405: static int d_discriminator (struct d_info *);
  406: 
  407: static int
  408: d_add_substitution (struct d_info *, struct demangle_component *);
  409: 
  410: static struct demangle_component *d_substitution (struct d_info *, int);
  411: 
  412: static void d_growable_string_init (struct d_growable_string *, size_t);
  413: 
  414: static inline void
  415: d_growable_string_resize (struct d_growable_string *, size_t);
  416: 
  417: static inline void
  418: d_growable_string_append_buffer (struct d_growable_string *,
  419:                                  const char *, size_t);
  420: static void
  421: d_growable_string_callback_adapter (const char *, size_t, void *);
  422: 
  423: static void
  424: d_print_init (struct d_print_info *, int, demangle_callbackref, void *);
  425: 
  426: static inline void d_print_error (struct d_print_info *);
  427: 
  428: static inline int d_print_saw_error (struct d_print_info *);
  429: 
  430: static inline void d_print_flush (struct d_print_info *);
  431: 
  432: static inline void d_append_char (struct d_print_info *, char);
  433: 
  434: static inline void d_append_buffer (struct d_print_info *,
  435:                                     const char *, size_t);
  436: 
  437: static inline void d_append_string (struct d_print_info *, const char *);
  438: 
  439: static inline char d_last_char (struct d_print_info *);
  440: 
  441: static void
  442: d_print_comp (struct d_print_info *, const struct demangle_component *);
  443: 
  444: static void
  445: d_print_java_identifier (struct d_print_info *, const char *, int);
  446: 
  447: static void
  448: d_print_mod_list (struct d_print_info *, struct d_print_mod *, int);
  449: 
  450: static void
  451: d_print_mod (struct d_print_info *, const struct demangle_component *);
  452: 
  453: static void
  454: d_print_function_type (struct d_print_info *,
  455:                        const struct demangle_component *,
  456:                        struct d_print_mod *);
  457: 
  458: static void
  459: d_print_array_type (struct d_print_info *,
  460:                     const struct demangle_component *,
  461:                     struct d_print_mod *);
  462: 
  463: static void
  464: d_print_expr_op (struct d_print_info *, const struct demangle_component *);
  465: 
  466: static void
  467: d_print_cast (struct d_print_info *, const struct demangle_component *);
  468: 
  469: static int d_demangle_callback (const char *, int,
  470:                                 demangle_callbackref, void *);
  471: static char *d_demangle (const char *, int, size_t *);
  472: 
  473: #ifdef CP_DEMANGLE_DEBUG
  474: 
  475: static void
  476: d_dump (struct demangle_component *dc, int indent)
  477: {
  478:   int i;
  479: 
  480:   if (dc == NULL)
  481:     {
  482:       if (indent == 0)
  483:         printf ("failed demangling\n");
  484:       return;
  485:     }
  486: 
  487:   for (i = 0; i < indent; ++i)
  488:     putchar (' ');
  489: 
  490:   switch (dc->type)
  491:     {
  492:     case DEMANGLE_COMPONENT_NAME:
  493:       printf ("name '%.*s'\n", dc->u.s_name.len, dc->u.s_name.s);
  494:       return;
  495:     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
  496:       printf ("template parameter %ld\n", dc->u.s_number.number);
  497:       return;
  498:     case DEMANGLE_COMPONENT_CTOR:
  499:       printf ("constructor %d\n", (int) dc->u.s_ctor.kind);
  500:       d_dump (dc->u.s_ctor.name, indent + 2);
  501:       return;
  502:     case DEMANGLE_COMPONENT_DTOR:
  503:       printf ("destructor %d\n", (int) dc->u.s_dtor.kind);
  504:       d_dump (dc->u.s_dtor.name, indent + 2);
  505:       return;
  506:     case DEMANGLE_COMPONENT_SUB_STD:
  507:       printf ("standard substitution %s\n", dc->u.s_string.string);
  508:       return;
  509:     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
  510:       printf ("builtin type %s\n", dc->u.s_builtin.type->name);
  511:       return;
  512:     case DEMANGLE_COMPONENT_OPERATOR:
  513:       printf ("operator %s\n", dc->u.s_operator.op->name);
  514:       return;
  515:     case DEMANGLE_COMPONENT_EXTENDED_OPERATOR:
  516:       printf ("extended operator with %d args\n",
  517:               dc->u.s_extended_operator.args);
  518:       d_dump (dc->u.s_extended_operator.name, indent + 2);
  519:       return;
  520: 
  521:     case DEMANGLE_COMPONENT_QUAL_NAME:
  522:       printf ("qualified name\n");
  523:       break;
  524:     case DEMANGLE_COMPONENT_LOCAL_NAME:
  525:       printf ("local name\n");
  526:       break;
  527:     case DEMANGLE_COMPONENT_TYPED_NAME:
  528:       printf ("typed name\n");
  529:       break;
  530:     case DEMANGLE_COMPONENT_TEMPLATE:
  531:       printf ("template\n");
  532:       break;
  533:     case DEMANGLE_COMPONENT_VTABLE:
  534:       printf ("vtable\n");
  535:       break;
  536:     case DEMANGLE_COMPONENT_VTT:
  537:       printf ("VTT\n");
  538:       break;
  539:     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
  540:       printf ("construction vtable\n");