Back to index

cell-binutils  2.17cvs20070401
cp-demint.c
Go to the documentation of this file.
00001 /* Demangler component interface functions.
00002    Copyright (C) 2004 Free Software Foundation, Inc.
00003    Written by Ian Lance Taylor <ian@wasabisystems.com>.
00004 
00005    This file is part of the libiberty library, which is part of GCC.
00006 
00007    This file is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    In addition to the permissions in the GNU General Public License, the
00013    Free Software Foundation gives you unlimited permission to link the
00014    compiled version of this file into combinations with other programs,
00015    and to distribute those combinations without any restriction coming
00016    from the use of this file.  (The General Public License restrictions
00017    do apply in other respects; for example, they cover modification of
00018    the file, and distribution when not linked into a combined
00019    executable.)
00020 
00021    This program is distributed in the hope that it will be useful,
00022    but WITHOUT ANY WARRANTY; without even the implied warranty of
00023    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024    GNU General Public License for more details.
00025 
00026    You should have received a copy of the GNU General Public License
00027    along with this program; if not, write to the Free Software
00028    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 
00029 */
00030 
00031 /* This file implements a few interface functions which are provided
00032    for use with struct demangle_component trees.  These functions are
00033    declared in demangle.h.  These functions are closely tied to the
00034    demangler code in cp-demangle.c, and other interface functions can
00035    be found in that file.  We put these functions in a separate file
00036    because they are not needed by the demangler, and so we avoid
00037    having them pulled in by programs which only need the
00038    demangler.  */
00039 
00040 #ifdef HAVE_CONFIG_H
00041 #include "config.h"
00042 #endif
00043 
00044 #ifdef HAVE_STDLIB_H
00045 #include <stdlib.h>
00046 #endif
00047 #ifdef HAVE_STRING_H
00048 #include <string.h>
00049 #endif
00050 
00051 #include "ansidecl.h"
00052 #include "libiberty.h"
00053 #include "demangle.h"
00054 #include "cp-demangle.h"
00055 
00056 /* Fill in most component types.  */
00057 
00058 int
00059 cplus_demangle_fill_component (struct demangle_component *p,
00060                                enum demangle_component_type type,
00061                                struct demangle_component *left,
00062                                 struct demangle_component *right)
00063 {
00064   if (p == NULL)
00065     return 0;
00066   switch (type)
00067     {
00068     case DEMANGLE_COMPONENT_QUAL_NAME:
00069     case DEMANGLE_COMPONENT_LOCAL_NAME:
00070     case DEMANGLE_COMPONENT_TYPED_NAME:
00071     case DEMANGLE_COMPONENT_TEMPLATE:
00072     case DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE:
00073     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
00074     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
00075     case DEMANGLE_COMPONENT_ARRAY_TYPE:
00076     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
00077     case DEMANGLE_COMPONENT_ARGLIST:
00078     case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST:
00079     case DEMANGLE_COMPONENT_UNARY:
00080     case DEMANGLE_COMPONENT_BINARY:
00081     case DEMANGLE_COMPONENT_BINARY_ARGS:
00082     case DEMANGLE_COMPONENT_TRINARY:
00083     case DEMANGLE_COMPONENT_TRINARY_ARG1:
00084     case DEMANGLE_COMPONENT_TRINARY_ARG2:
00085     case DEMANGLE_COMPONENT_LITERAL:
00086     case DEMANGLE_COMPONENT_LITERAL_NEG:
00087       break;
00088 
00089       /* These component types only have one subtree.  */
00090     case DEMANGLE_COMPONENT_VTABLE:
00091     case DEMANGLE_COMPONENT_VTT:
00092     case DEMANGLE_COMPONENT_TYPEINFO:
00093     case DEMANGLE_COMPONENT_TYPEINFO_NAME:
00094     case DEMANGLE_COMPONENT_TYPEINFO_FN:
00095     case DEMANGLE_COMPONENT_THUNK:
00096     case DEMANGLE_COMPONENT_VIRTUAL_THUNK:
00097     case DEMANGLE_COMPONENT_COVARIANT_THUNK:
00098     case DEMANGLE_COMPONENT_JAVA_CLASS:
00099     case DEMANGLE_COMPONENT_GUARD:
00100     case DEMANGLE_COMPONENT_REFTEMP:
00101     case DEMANGLE_COMPONENT_RESTRICT:
00102     case DEMANGLE_COMPONENT_VOLATILE:
00103     case DEMANGLE_COMPONENT_CONST:
00104     case DEMANGLE_COMPONENT_RESTRICT_THIS:
00105     case DEMANGLE_COMPONENT_VOLATILE_THIS:
00106     case DEMANGLE_COMPONENT_CONST_THIS:
00107     case DEMANGLE_COMPONENT_POINTER:
00108     case DEMANGLE_COMPONENT_REFERENCE:
00109     case DEMANGLE_COMPONENT_COMPLEX:
00110     case DEMANGLE_COMPONENT_IMAGINARY:
00111     case DEMANGLE_COMPONENT_VENDOR_TYPE:
00112     case DEMANGLE_COMPONENT_CAST:
00113       if (right != NULL)
00114        return 0;
00115       break;
00116 
00117     default:
00118       /* Other types do not use subtrees.  */
00119       return 0;
00120     }
00121 
00122   p->type = type;
00123   p->u.s_binary.left = left;
00124   p->u.s_binary.right = right;
00125 
00126   return 1;
00127 }
00128 
00129 /* Fill in a DEMANGLE_COMPONENT_BUILTIN_TYPE.  */
00130 
00131 int
00132 cplus_demangle_fill_builtin_type (struct demangle_component *p,
00133                                   const char *type_name)
00134 {
00135   int len;
00136   unsigned int i;
00137 
00138   if (p == NULL || type_name == NULL)
00139     return 0;
00140   len = strlen (type_name);
00141   for (i = 0; i < D_BUILTIN_TYPE_COUNT; ++i)
00142     {
00143       if (len == cplus_demangle_builtin_types[i].len
00144          && strcmp (type_name, cplus_demangle_builtin_types[i].name) == 0)
00145        {
00146          p->type = DEMANGLE_COMPONENT_BUILTIN_TYPE;
00147          p->u.s_builtin.type = &cplus_demangle_builtin_types[i];
00148          return 1;
00149        }
00150     }
00151   return 0;
00152 }
00153 
00154 /* Fill in a DEMANGLE_COMPONENT_OPERATOR.  */
00155 
00156 int
00157 cplus_demangle_fill_operator (struct demangle_component *p,
00158                               const char *opname, int args)
00159 {
00160   int len;
00161   unsigned int i;
00162 
00163   if (p == NULL || opname == NULL)
00164     return 0;
00165   len = strlen (opname);
00166   for (i = 0; cplus_demangle_operators[i].name != NULL; ++i)
00167     {
00168       if (len == cplus_demangle_operators[i].len
00169          && args == cplus_demangle_operators[i].args
00170          && strcmp (opname, cplus_demangle_operators[i].name) == 0)
00171        {
00172          p->type = DEMANGLE_COMPONENT_OPERATOR;
00173          p->u.s_operator.op = &cplus_demangle_operators[i];
00174          return 1;
00175        }
00176     }
00177   return 0;
00178 }
00179 
00180 /* Translate a mangled name into components.  */
00181 
00182 struct demangle_component *
00183 cplus_demangle_v3_components (const char *mangled, int options, void **mem)
00184 {
00185   size_t len;
00186   int type;
00187   struct d_info di;
00188   struct demangle_component *dc;
00189 
00190   len = strlen (mangled);
00191 
00192   if (mangled[0] == '_' && mangled[1] == 'Z')
00193     type = 0;
00194   else
00195     {
00196       if ((options & DMGL_TYPES) == 0)
00197        return NULL;
00198       type = 1;
00199     }
00200 
00201   cplus_demangle_init_info (mangled, options, len, &di);
00202 
00203   di.comps = ((struct demangle_component *)
00204              malloc (di.num_comps * sizeof (struct demangle_component)));
00205   di.subs = ((struct demangle_component **)
00206             malloc (di.num_subs * sizeof (struct demangle_component *)));
00207   if (di.comps == NULL || di.subs == NULL)
00208     {
00209       if (di.comps != NULL)
00210        free (di.comps);
00211       if (di.subs != NULL)
00212        free (di.subs);
00213       return NULL;
00214     }
00215 
00216   if (! type)
00217     dc = cplus_demangle_mangled_name (&di, 1);
00218   else
00219     dc = cplus_demangle_type (&di);
00220 
00221   /* If DMGL_PARAMS is set, then if we didn't consume the entire
00222      mangled string, then we didn't successfully demangle it.  */
00223   if ((options & DMGL_PARAMS) != 0 && d_peek_char (&di) != '\0')
00224     dc = NULL;
00225 
00226   free (di.subs);
00227 
00228   if (dc != NULL)
00229     *mem = di.comps;
00230   else
00231     free (di.comps);
00232 
00233   return dc;
00234 }