Back to index

cell-binutils  2.17cvs20070401
stabs.c
Go to the documentation of this file.
00001 /* stabs.c -- Parse stabs debugging information
00002    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
00003    Free Software Foundation, Inc.
00004    Written by Ian Lance Taylor <ian@cygnus.com>.
00005 
00006    This file is part of GNU Binutils.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
00021    02110-1301, USA.  */
00022 
00023 /* This file contains code which parses stabs debugging information.
00024    The organization of this code is based on the gdb stabs reading
00025    code.  The job it does is somewhat different, because it is not
00026    trying to identify the correct address for anything.  */
00027 
00028 #include <stdio.h>
00029 
00030 #include "bfd.h"
00031 #include "bucomm.h"
00032 #include "libiberty.h"
00033 #include "safe-ctype.h"
00034 #include "demangle.h"
00035 #include "debug.h"
00036 #include "budbg.h"
00037 #include "filenames.h"
00038 #include "aout/aout64.h"
00039 #include "aout/stab_gnu.h"
00040 
00041 /* The number of predefined XCOFF types.  */
00042 
00043 #define XCOFF_TYPE_COUNT 34
00044 
00045 /* This structure is used as a handle so that the stab parsing doesn't
00046    need to use any static variables.  */
00047 
00048 struct stab_handle
00049 {
00050   /* The BFD.  */
00051   bfd *abfd;
00052   /* TRUE if this is stabs in sections.  */
00053   bfd_boolean sections;
00054   /* The symbol table.  */
00055   asymbol **syms;
00056   /* The number of symbols.  */
00057   long symcount;
00058   /* The accumulated file name string.  */
00059   char *so_string;
00060   /* The value of the last N_SO symbol.  */
00061   bfd_vma so_value;
00062   /* The value of the start of the file, so that we can handle file
00063      relative N_LBRAC and N_RBRAC symbols.  */
00064   bfd_vma file_start_offset;
00065   /* The offset of the start of the function, so that we can handle
00066      function relative N_LBRAC and N_RBRAC symbols.  */
00067   bfd_vma function_start_offset;
00068   /* The version number of gcc which compiled the current compilation
00069      unit, 0 if not compiled by gcc.  */
00070   int gcc_compiled;
00071   /* Whether an N_OPT symbol was seen that was not generated by gcc,
00072      so that we can detect the SunPRO compiler.  */
00073   bfd_boolean n_opt_found;
00074   /* The main file name.  */
00075   char *main_filename;
00076   /* A stack of unfinished N_BINCL files.  */
00077   struct bincl_file *bincl_stack;
00078   /* A list of finished N_BINCL files.  */
00079   struct bincl_file *bincl_list;
00080   /* Whether we are inside a function or not.  */
00081   bfd_boolean within_function;
00082   /* The address of the end of the function, used if we have seen an
00083      N_FUN symbol while in a function.  This is -1 if we have not seen
00084      an N_FUN (the normal case).  */
00085   bfd_vma function_end;
00086   /* The depth of block nesting.  */
00087   int block_depth;
00088   /* List of pending variable definitions.  */
00089   struct stab_pending_var *pending;
00090   /* Number of files for which we have types.  */
00091   unsigned int files;
00092   /* Lists of types per file.  */
00093   struct stab_types **file_types;
00094   /* Predefined XCOFF types.  */
00095   debug_type xcoff_types[XCOFF_TYPE_COUNT];
00096   /* Undefined tags.  */
00097   struct stab_tag *tags;
00098   /* Set by parse_stab_type if it sees a structure defined as a cross
00099      reference to itself.  Reset by parse_stab_type otherwise.  */
00100   bfd_boolean self_crossref;
00101 };
00102 
00103 /* A list of these structures is used to hold pending variable
00104    definitions seen before the N_LBRAC of a block.  */
00105 
00106 struct stab_pending_var
00107 {
00108   /* Next pending variable definition.  */
00109   struct stab_pending_var *next;
00110   /* Name.  */
00111   const char *name;
00112   /* Type.  */
00113   debug_type type;
00114   /* Kind.  */
00115   enum debug_var_kind kind;
00116   /* Value.  */
00117   bfd_vma val;
00118 };
00119 
00120 /* A list of these structures is used to hold the types for a single
00121    file.  */
00122 
00123 struct stab_types
00124 {
00125   /* Next set of slots for this file.  */
00126   struct stab_types *next;
00127   /* Types indexed by type number.  */
00128 #define STAB_TYPES_SLOTS (16)
00129   debug_type types[STAB_TYPES_SLOTS];
00130 };
00131 
00132 /* We keep a list of undefined tags that we encounter, so that we can
00133    fill them in if the tag is later defined.  */
00134 
00135 struct stab_tag
00136 {
00137   /* Next undefined tag.  */
00138   struct stab_tag *next;
00139   /* Tag name.  */
00140   const char *name;
00141   /* Type kind.  */
00142   enum debug_type_kind kind;
00143   /* Slot to hold real type when we discover it.  If we don't, we fill
00144      in an undefined tag type.  */
00145   debug_type slot;
00146   /* Indirect type we have created to point at slot.  */
00147   debug_type type;
00148 };
00149 
00150 static char *savestring (const char *, int);
00151 static bfd_vma parse_number (const char **, bfd_boolean *);
00152 static void bad_stab (const char *);
00153 static void warn_stab (const char *, const char *);
00154 static bfd_boolean parse_stab_string
00155   (void *, struct stab_handle *, int, int, bfd_vma, const char *);
00156 static debug_type parse_stab_type
00157   (void *, struct stab_handle *, const char *, const char **, debug_type **);
00158 static bfd_boolean parse_stab_type_number (const char **, int *);
00159 static debug_type parse_stab_range_type
00160   (void *, struct stab_handle *, const char *, const char **, const int *);
00161 static debug_type parse_stab_sun_builtin_type (void *, const char **);
00162 static debug_type parse_stab_sun_floating_type (void *, const char **);
00163 static debug_type parse_stab_enum_type (void *, const char **);
00164 static debug_type parse_stab_struct_type
00165   (void *, struct stab_handle *, const char *, const char **,
00166    bfd_boolean, const int *);
00167 static bfd_boolean parse_stab_baseclasses
00168   (void *, struct stab_handle *, const char **, debug_baseclass **);
00169 static bfd_boolean parse_stab_struct_fields
00170   (void *, struct stab_handle *, const char **, debug_field **, bfd_boolean *);
00171 static bfd_boolean parse_stab_cpp_abbrev
00172   (void *, struct stab_handle *, const char **, debug_field *);
00173 static bfd_boolean parse_stab_one_struct_field
00174   (void *, struct stab_handle *, const char **, const char *,
00175    debug_field *, bfd_boolean *);
00176 static bfd_boolean parse_stab_members
00177   (void *, struct stab_handle *, const char *, const char **, const int *,
00178    debug_method **);
00179 static debug_type parse_stab_argtypes
00180   (void *, struct stab_handle *, debug_type, const char *, const char *,
00181    debug_type, const char *, bfd_boolean, bfd_boolean, const char **);
00182 static bfd_boolean parse_stab_tilde_field
00183   (void *, struct stab_handle *, const char **, const int *, debug_type *,
00184    bfd_boolean *);
00185 static debug_type parse_stab_array_type
00186   (void *, struct stab_handle *, const char **, bfd_boolean);
00187 static void push_bincl (struct stab_handle *, const char *, bfd_vma);
00188 static const char *pop_bincl (struct stab_handle *);
00189 static bfd_boolean find_excl (struct stab_handle *, const char *, bfd_vma);
00190 static bfd_boolean stab_record_variable
00191   (void *, struct stab_handle *, const char *, debug_type,
00192    enum debug_var_kind, bfd_vma);
00193 static bfd_boolean stab_emit_pending_vars (void *, struct stab_handle *);
00194 static debug_type *stab_find_slot (struct stab_handle *, const int *);
00195 static debug_type stab_find_type (void *, struct stab_handle *, const int *);
00196 static bfd_boolean stab_record_type
00197   (void *, struct stab_handle *, const int *, debug_type);
00198 static debug_type stab_xcoff_builtin_type
00199   (void *, struct stab_handle *, int);
00200 static debug_type stab_find_tagged_type
00201   (void *, struct stab_handle *, const char *, int, enum debug_type_kind);
00202 static debug_type *stab_demangle_argtypes
00203   (void *, struct stab_handle *, const char *, bfd_boolean *, unsigned int);
00204 static debug_type *stab_demangle_v3_argtypes
00205   (void *, struct stab_handle *, const char *, bfd_boolean *);
00206 static debug_type *stab_demangle_v3_arglist
00207   (void *, struct stab_handle *, struct demangle_component *, bfd_boolean *);
00208 static debug_type stab_demangle_v3_arg
00209   (void *, struct stab_handle *, struct demangle_component *, debug_type,
00210    bfd_boolean *);
00211 
00212 /* Save a string in memory.  */
00213 
00214 static char *
00215 savestring (const char *start, int len)
00216 {
00217   char *ret;
00218 
00219   ret = (char *) xmalloc (len + 1);
00220   memcpy (ret, start, len);
00221   ret[len] = '\0';
00222   return ret;
00223 }
00224 
00225 /* Read a number from a string.  */
00226 
00227 static bfd_vma
00228 parse_number (const char **pp, bfd_boolean *poverflow)
00229 {
00230   unsigned long ul;
00231   const char *orig;
00232 
00233   if (poverflow != NULL)
00234     *poverflow = FALSE;
00235 
00236   orig = *pp;
00237 
00238   errno = 0;
00239   ul = strtoul (*pp, (char **) pp, 0);
00240   if (ul + 1 != 0 || errno == 0)
00241     {
00242       /* If bfd_vma is larger than unsigned long, and the number is
00243          meant to be negative, we have to make sure that we sign
00244          extend properly.  */
00245       if (*orig == '-')
00246        return (bfd_vma) (bfd_signed_vma) (long) ul;
00247       return (bfd_vma) ul;
00248     }
00249 
00250   /* Note that even though strtoul overflowed, it should have set *pp
00251      to the end of the number, which is where we want it.  */
00252   if (sizeof (bfd_vma) > sizeof (unsigned long))
00253     {
00254       const char *p;
00255       bfd_boolean neg;
00256       int base;
00257       bfd_vma over, lastdig;
00258       bfd_boolean overflow;
00259       bfd_vma v;
00260 
00261       /* Our own version of strtoul, for a bfd_vma.  */
00262       p = orig;
00263 
00264       neg = FALSE;
00265       if (*p == '+')
00266        ++p;
00267       else if (*p == '-')
00268        {
00269          neg = TRUE;
00270          ++p;
00271        }
00272 
00273       base = 10;
00274       if (*p == '0')
00275        {
00276          if (p[1] == 'x' || p[1] == 'X')
00277            {
00278              base = 16;
00279              p += 2;
00280            }
00281          else
00282            {
00283              base = 8;
00284              ++p;
00285            }
00286        }
00287 
00288       over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
00289       lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
00290 
00291       overflow = FALSE;
00292       v = 0;
00293       while (1)
00294        {
00295          int d;
00296 
00297          d = *p++;
00298          if (ISDIGIT (d))
00299            d -= '0';
00300          else if (ISUPPER (d))
00301            d -= 'A';
00302          else if (ISLOWER (d))
00303            d -= 'a';
00304          else
00305            break;
00306 
00307          if (d >= base)
00308            break;
00309 
00310          if (v > over || (v == over && (bfd_vma) d > lastdig))
00311            {
00312              overflow = TRUE;
00313              break;
00314            }
00315        }
00316 
00317       if (! overflow)
00318        {
00319          if (neg)
00320            v = - v;
00321          return v;
00322        }
00323     }
00324 
00325   /* If we get here, the number is too large to represent in a
00326      bfd_vma.  */
00327   if (poverflow != NULL)
00328     *poverflow = TRUE;
00329   else
00330     warn_stab (orig, _("numeric overflow"));
00331 
00332   return 0;
00333 }
00334 
00335 /* Give an error for a bad stab string.  */
00336 
00337 static void
00338 bad_stab (const char *p)
00339 {
00340   fprintf (stderr, _("Bad stab: %s\n"), p);
00341 }
00342 
00343 /* Warn about something in a stab string.  */
00344 
00345 static void
00346 warn_stab (const char *p, const char *err)
00347 {
00348   fprintf (stderr, _("Warning: %s: %s\n"), err, p);
00349 }
00350 
00351 /* Create a handle to parse stabs symbols with.  */
00352 
00353 void *
00354 start_stab (void *dhandle ATTRIBUTE_UNUSED, bfd *abfd, bfd_boolean sections,
00355            asymbol **syms, long symcount)
00356 {
00357   struct stab_handle *ret;
00358 
00359   ret = (struct stab_handle *) xmalloc (sizeof *ret);
00360   memset (ret, 0, sizeof *ret);
00361   ret->abfd = abfd;
00362   ret->sections = sections;
00363   ret->syms = syms;
00364   ret->symcount = symcount;
00365   ret->files = 1;
00366   ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
00367   ret->file_types[0] = NULL;
00368   ret->function_end = (bfd_vma) -1;
00369   return (void *) ret;
00370 }
00371 
00372 /* When we have processed all the stabs information, we need to go
00373    through and fill in all the undefined tags.  */
00374 
00375 bfd_boolean
00376 finish_stab (void *dhandle, void *handle)
00377 {
00378   struct stab_handle *info = (struct stab_handle *) handle;
00379   struct stab_tag *st;
00380 
00381   if (info->within_function)
00382     {
00383       if (! stab_emit_pending_vars (dhandle, info)
00384          || ! debug_end_function (dhandle, info->function_end))
00385        return FALSE;
00386       info->within_function = FALSE;
00387       info->function_end = (bfd_vma) -1;
00388     }
00389 
00390   for (st = info->tags; st != NULL; st = st->next)
00391     {
00392       enum debug_type_kind kind;
00393 
00394       kind = st->kind;
00395       if (kind == DEBUG_KIND_ILLEGAL)
00396        kind = DEBUG_KIND_STRUCT;
00397       st->slot = debug_make_undefined_tagged_type (dhandle, st->name, kind);
00398       if (st->slot == DEBUG_TYPE_NULL)
00399        return FALSE;
00400     }
00401 
00402   return TRUE;
00403 }
00404 
00405 /* Handle a single stabs symbol.  */
00406 
00407 bfd_boolean
00408 parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value,
00409            const char *string)
00410 {
00411   struct stab_handle *info = (struct stab_handle *) handle;
00412 
00413   /* gcc will emit two N_SO strings per compilation unit, one for the
00414      directory name and one for the file name.  We just collect N_SO
00415      strings as we see them, and start the new compilation unit when
00416      we see a non N_SO symbol.  */
00417   if (info->so_string != NULL
00418       && (type != N_SO || *string == '\0' || value != info->so_value))
00419     {
00420       if (! debug_set_filename (dhandle, info->so_string))
00421        return FALSE;
00422       info->main_filename = info->so_string;
00423 
00424       info->gcc_compiled = 0;
00425       info->n_opt_found = FALSE;
00426 
00427       /* Generally, for stabs in the symbol table, the N_LBRAC and
00428         N_RBRAC symbols are relative to the N_SO symbol value.  */
00429       if (! info->sections)
00430        info->file_start_offset = info->so_value;
00431 
00432       /* We need to reset the mapping from type numbers to types.  We
00433         can't free the old mapping, because of the use of
00434         debug_make_indirect_type.  */
00435       info->files = 1;
00436       info->file_types = ((struct stab_types **)
00437                        xmalloc (sizeof *info->file_types));
00438       info->file_types[0] = NULL;
00439 
00440       info->so_string = NULL;
00441 
00442       /* Now process whatever type we just got.  */
00443     }
00444 
00445   switch (type)
00446     {
00447     case N_FN:
00448     case N_FN_SEQ:
00449       break;
00450 
00451     case N_LBRAC:
00452       /* Ignore extra outermost context from SunPRO cc and acc.  */
00453       if (info->n_opt_found && desc == 1)
00454        break;
00455 
00456       if (! info->within_function)
00457        {
00458          fprintf (stderr, _("N_LBRAC not within function\n"));
00459          return FALSE;
00460        }
00461 
00462       /* Start an inner lexical block.  */
00463       if (! debug_start_block (dhandle,
00464                             (value
00465                             + info->file_start_offset
00466                             + info->function_start_offset)))
00467        return FALSE;
00468 
00469       /* Emit any pending variable definitions.  */
00470       if (! stab_emit_pending_vars (dhandle, info))
00471        return FALSE;
00472 
00473       ++info->block_depth;
00474       break;
00475 
00476     case N_RBRAC:
00477       /* Ignore extra outermost context from SunPRO cc and acc.  */
00478       if (info->n_opt_found && desc == 1)
00479        break;
00480 
00481       /* We shouldn't have any pending variable definitions here, but,
00482          if we do, we probably need to emit them before closing the
00483          block.  */
00484       if (! stab_emit_pending_vars (dhandle, info))
00485        return FALSE;
00486 
00487       /* End an inner lexical block.  */
00488       if (! debug_end_block (dhandle,
00489                           (value
00490                            + info->file_start_offset
00491                            + info->function_start_offset)))
00492        return FALSE;
00493 
00494       --info->block_depth;
00495       if (info->block_depth < 0)
00496        {
00497          fprintf (stderr, _("Too many N_RBRACs\n"));
00498          return FALSE;
00499        }
00500       break;
00501 
00502     case N_SO:
00503       /* This always ends a function.  */
00504       if (info->within_function)
00505        {
00506          bfd_vma endval;
00507 
00508          endval = value;
00509          if (*string != '\0'
00510              && info->function_end != (bfd_vma) -1
00511              && info->function_end < endval)
00512            endval = info->function_end;
00513          if (! stab_emit_pending_vars (dhandle, info)
00514              || ! debug_end_function (dhandle, endval))
00515            return FALSE;
00516          info->within_function = FALSE;
00517          info->function_end = (bfd_vma) -1;
00518        }
00519 
00520       /* An empty string is emitted by gcc at the end of a compilation
00521          unit.  */
00522       if (*string == '\0')
00523        return TRUE;
00524 
00525       /* Just accumulate strings until we see a non N_SO symbol.  If
00526          the string starts with a directory separator or some other
00527         form of absolute path specification, we discard the previously
00528          accumulated strings.  */
00529       if (info->so_string == NULL)
00530        info->so_string = xstrdup (string);
00531       else
00532        {
00533          char *f;
00534 
00535          f = info->so_string;
00536 
00537          if (IS_ABSOLUTE_PATH (string))
00538            info->so_string = xstrdup (string);
00539          else
00540            info->so_string = concat (info->so_string, string,
00541                                   (const char *) NULL);
00542          free (f);
00543        }
00544 
00545       info->so_value = value;
00546 
00547       break;
00548 
00549     case N_SOL:
00550       /* Start an include file.  */
00551       if (! debug_start_source (dhandle, string))
00552        return FALSE;
00553       break;
00554 
00555     case N_BINCL:
00556       /* Start an include file which may be replaced.  */
00557       push_bincl (info, string, value);
00558       if (! debug_start_source (dhandle, string))
00559        return FALSE;
00560       break;
00561 
00562     case N_EINCL:
00563       /* End an N_BINCL include.  */
00564       if (! debug_start_source (dhandle, pop_bincl (info)))
00565        return FALSE;
00566       break;
00567 
00568     case N_EXCL:
00569       /* This is a duplicate of a header file named by N_BINCL which
00570          was eliminated by the linker.  */
00571       if (! find_excl (info, string, value))
00572        return FALSE;
00573       break;
00574 
00575     case N_SLINE:
00576       if (! debug_record_line (dhandle, desc,
00577                             value + (info->within_function
00578                                    ? info->function_start_offset : 0)))
00579        return FALSE;
00580       break;
00581 
00582     case N_BCOMM:
00583       if (! debug_start_common_block (dhandle, string))
00584        return FALSE;
00585       break;
00586 
00587     case N_ECOMM:
00588       if (! debug_end_common_block (dhandle, string))
00589        return FALSE;
00590       break;
00591 
00592     case N_FUN:
00593       if (*string == '\0')
00594        {
00595          if (info->within_function)
00596            {
00597              /* This always marks the end of a function; we don't
00598                  need to worry about info->function_end.  */
00599              if (info->sections)
00600               value += info->function_start_offset;
00601              if (! stab_emit_pending_vars (dhandle, info)
00602                 || ! debug_end_function (dhandle, value))
00603               return FALSE;
00604              info->within_function = FALSE;
00605              info->function_end = (bfd_vma) -1;
00606            }
00607          break;
00608        }
00609 
00610       /* A const static symbol in the .text section will have an N_FUN
00611          entry.  We need to use these to mark the end of the function,
00612          in case we are looking at gcc output before it was changed to
00613          always emit an empty N_FUN.  We can't call debug_end_function
00614          here, because it might be a local static symbol.  */
00615       if (info->within_function
00616          && (info->function_end == (bfd_vma) -1
00617              || value < info->function_end))
00618        info->function_end = value;
00619 
00620       /* Fall through.  */
00621       /* FIXME: gdb checks the string for N_STSYM, N_LCSYM or N_ROSYM
00622          symbols, and if it does not start with :S, gdb relocates the
00623          value to the start of the section.  gcc always seems to use
00624          :S, so we don't worry about this.  */
00625       /* Fall through.  */
00626     default:
00627       {
00628        const char *colon;
00629 
00630        colon = strchr (string, ':');
00631        if (colon != NULL
00632            && (colon[1] == 'f' || colon[1] == 'F'))
00633          {
00634            if (info->within_function)
00635              {
00636               bfd_vma endval;
00637 
00638               endval = value;
00639               if (info->function_end != (bfd_vma) -1
00640                   && info->function_end < endval)
00641                 endval = info->function_end;
00642               if (! stab_emit_pending_vars (dhandle, info)
00643                   || ! debug_end_function (dhandle, endval))
00644                 return FALSE;
00645               info->function_end = (bfd_vma) -1;
00646              }
00647            /* For stabs in sections, line numbers and block addresses
00648                are offsets from the start of the function.  */
00649            if (info->sections)
00650              info->function_start_offset = value;
00651            info->within_function = TRUE;
00652          }
00653 
00654        if (! parse_stab_string (dhandle, info, type, desc, value, string))
00655          return FALSE;
00656       }
00657       break;
00658 
00659     case N_OPT:
00660       if (string != NULL && strcmp (string, "gcc2_compiled.") == 0)
00661        info->gcc_compiled = 2;
00662       else if (string != NULL && strcmp (string, "gcc_compiled.") == 0)
00663        info->gcc_compiled = 1;
00664       else
00665        info->n_opt_found = TRUE;
00666       break;
00667 
00668     case N_OBJ:
00669     case N_ENDM:
00670     case N_MAIN:
00671     case N_WARNING:
00672       break;
00673     }
00674 
00675   return TRUE;
00676 }
00677 
00678 /* Parse the stabs string.  */
00679 
00680 static bfd_boolean
00681 parse_stab_string (void *dhandle, struct stab_handle *info, int stabtype,
00682                  int desc, bfd_vma value, const char *string)
00683 {
00684   const char *p;
00685   char *name;
00686   int type;
00687   debug_type dtype;
00688   bfd_boolean synonym;
00689   bfd_boolean self_crossref;
00690   unsigned int lineno;
00691   debug_type *slot;
00692 
00693   p = strchr (string, ':');
00694   if (p == NULL)
00695     return TRUE;
00696 
00697   while (p[1] == ':')
00698     {
00699       p += 2;
00700       p = strchr (p, ':');
00701       if (p == NULL)
00702        {
00703          bad_stab (string);
00704          return FALSE;
00705        }
00706     }
00707 
00708   /* GCC 2.x puts the line number in desc.  SunOS apparently puts in
00709      the number of bytes occupied by a type or object, which we
00710      ignore.  */
00711   if (info->gcc_compiled >= 2)
00712     lineno = desc;
00713   else
00714     lineno = 0;
00715 
00716   /* FIXME: Sometimes the special C++ names start with '.'.  */
00717   name = NULL;
00718   if (string[0] == '$')
00719     {
00720       switch (string[1])
00721        {
00722        case 't':
00723          name = "this";
00724          break;
00725        case 'v':
00726          /* Was: name = "vptr"; */
00727          break;
00728        case 'e':
00729          name = "eh_throw";
00730          break;
00731        case '_':
00732          /* This was an anonymous type that was never fixed up.  */
00733          break;
00734        case 'X':
00735          /* SunPRO (3.0 at least) static variable encoding.  */
00736          break;
00737        default:
00738          warn_stab (string, _("unknown C++ encoded name"));
00739          break;
00740        }
00741     }
00742 
00743   if (name == NULL)
00744     {
00745       if (p == string || (string[0] == ' ' && p == string + 1))
00746        name = NULL;
00747       else
00748        name = savestring (string, p - string);
00749     }
00750 
00751   ++p;
00752   if (ISDIGIT (*p) || *p == '(' || *p == '-')
00753     type = 'l';
00754   else
00755     type = *p++;
00756 
00757   switch (type)
00758     {
00759     case 'c':
00760       /* c is a special case, not followed by a type-number.
00761         SYMBOL:c=iVALUE for an integer constant symbol.
00762         SYMBOL:c=rVALUE for a floating constant symbol.
00763         SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
00764         e.g. "b:c=e6,0" for "const b = blob1"
00765         (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
00766       if (*p != '=')
00767        {
00768          bad_stab (string);
00769          return FALSE;
00770        }
00771       ++p;
00772       switch (*p++)
00773        {
00774        case 'r':
00775          /* Floating point constant.  */
00776          if (! debug_record_float_const (dhandle, name, atof (p)))
00777            return FALSE;
00778          break;
00779        case 'i':
00780          /* Integer constant.  */
00781          /* Defining integer constants this way is kind of silly,
00782             since 'e' constants allows the compiler to give not only
00783             the value, but the type as well.  C has at least int,
00784             long, unsigned int, and long long as constant types;
00785             other languages probably should have at least unsigned as
00786             well as signed constants.  */
00787          if (! debug_record_int_const (dhandle, name, atoi (p)))
00788            return FALSE;
00789          break;
00790        case 'e':
00791          /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
00792             can be represented as integral.
00793             e.g. "b:c=e6,0" for "const b = blob1"
00794             (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
00795          dtype = parse_stab_type (dhandle, info, (const char *) NULL,
00796                                &p, (debug_type **) NULL);
00797          if (dtype == DEBUG_TYPE_NULL)
00798            return FALSE;
00799          if (*p != ',')
00800            {
00801              bad_stab (string);
00802              return FALSE;
00803            }
00804          if (! debug_record_typed_const (dhandle, name, dtype, atoi (p)))
00805            return FALSE;
00806          break;
00807        default:
00808          bad_stab (string);
00809          return FALSE;
00810        }
00811 
00812       break;
00813 
00814     case 'C':
00815       /* The name of a caught exception.  */
00816       dtype = parse_stab_type (dhandle, info, (const char *) NULL,
00817                             &p, (debug_type **) NULL);
00818       if (dtype == DEBUG_TYPE_NULL)
00819        return FALSE;
00820       if (! debug_record_label (dhandle, name, dtype, value))
00821        return FALSE;
00822       break;
00823 
00824     case 'f':
00825     case 'F':
00826       /* A function definition.  */
00827       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
00828                             (debug_type **) NULL);
00829       if (dtype == DEBUG_TYPE_NULL)
00830        return FALSE;
00831       if (! debug_record_function (dhandle, name, dtype, type == 'F', value))
00832        return FALSE;
00833 
00834       /* Sun acc puts declared types of arguments here.  We don't care
00835         about their actual types (FIXME -- we should remember the whole
00836         function prototype), but the list may define some new types
00837         that we have to remember, so we must scan it now.  */
00838       while (*p == ';')
00839        {
00840          ++p;
00841          if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
00842                             (debug_type **) NULL)
00843              == DEBUG_TYPE_NULL)
00844            return FALSE;
00845        }
00846 
00847       break;
00848 
00849     case 'G':
00850       {
00851        char leading;
00852        long c;
00853        asymbol **ps;
00854 
00855        /* A global symbol.  The value must be extracted from the
00856           symbol table.  */
00857        dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
00858                              (debug_type **) NULL);
00859        if (dtype == DEBUG_TYPE_NULL)
00860          return FALSE;
00861        leading = bfd_get_symbol_leading_char (info->abfd);
00862        for (c = info->symcount, ps = info->syms; c > 0; --c, ++ps)
00863          {
00864            const char *n;
00865 
00866            n = bfd_asymbol_name (*ps);
00867            if (leading != '\0' && *n == leading)
00868              ++n;
00869            if (*n == *name && strcmp (n, name) == 0)
00870              break;
00871          }
00872        if (c > 0)
00873          value = bfd_asymbol_value (*ps);
00874        if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_GLOBAL,
00875                                 value))
00876          return FALSE;
00877       }
00878       break;
00879 
00880       /* This case is faked by a conditional above, when there is no
00881         code letter in the dbx data.  Dbx data never actually
00882         contains 'l'.  */
00883     case 'l':
00884     case 's':
00885       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
00886                             (debug_type **) NULL);
00887       if (dtype == DEBUG_TYPE_NULL)
00888        return FALSE;
00889       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
00890                               value))
00891        return FALSE;
00892       break;
00893 
00894     case 'p':
00895       /* A function parameter.  */
00896       if (*p != 'F')
00897        dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
00898                              (debug_type **) NULL);
00899       else
00900        {
00901        /* pF is a two-letter code that means a function parameter in
00902           Fortran.  The type-number specifies the type of the return
00903           value.  Translate it into a pointer-to-function type.  */
00904          ++p;
00905          dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
00906                                (debug_type **) NULL);
00907          if (dtype != DEBUG_TYPE_NULL)
00908            {
00909              debug_type ftype;
00910 
00911              ftype = debug_make_function_type (dhandle, dtype,
00912                                           (debug_type *) NULL, FALSE);
00913              dtype = debug_make_pointer_type (dhandle, ftype);
00914            }
00915        }
00916       if (dtype == DEBUG_TYPE_NULL)
00917        return FALSE;
00918       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_STACK,
00919                                 value))
00920        return FALSE;
00921 
00922       /* FIXME: At this point gdb considers rearranging the parameter
00923         address on a big endian machine if it is smaller than an int.
00924         We have no way to do that, since we don't really know much
00925         about the target.  */
00926       break;
00927 
00928     case 'P':
00929       if (stabtype == N_FUN)
00930        {
00931          /* Prototype of a function referenced by this file.  */
00932          while (*p == ';')
00933            {
00934              ++p;
00935              if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
00936                                (debug_type **) NULL)
00937                 == DEBUG_TYPE_NULL)
00938               return FALSE;
00939            }
00940          break;
00941        }
00942       /* Fall through.  */
00943     case 'R':
00944       /* Parameter which is in a register.  */
00945       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
00946                             (debug_type **) NULL);
00947       if (dtype == DEBUG_TYPE_NULL)
00948        return FALSE;
00949       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REG,
00950                                 value))
00951        return FALSE;
00952       break;
00953 
00954     case 'r':
00955       /* Register variable (either global or local).  */
00956       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
00957                             (debug_type **) NULL);
00958       if (dtype == DEBUG_TYPE_NULL)
00959        return FALSE;
00960       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_REGISTER,
00961                               value))
00962        return FALSE;
00963 
00964       /* FIXME: At this point gdb checks to combine pairs of 'p' and
00965         'r' stabs into a single 'P' stab.  */
00966       break;
00967 
00968     case 'S':
00969       /* Static symbol at top level of file.  */
00970       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
00971                             (debug_type **) NULL);
00972       if (dtype == DEBUG_TYPE_NULL)
00973        return FALSE;
00974       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_STATIC,
00975                               value))
00976        return FALSE;
00977       break;
00978 
00979     case 't':
00980       /* A typedef.  */
00981       dtype = parse_stab_type (dhandle, info, name, &p, &slot);
00982       if (dtype == DEBUG_TYPE_NULL)
00983        return FALSE;
00984       if (name == NULL)
00985        {
00986          /* A nameless type.  Nothing to do.  */
00987          return TRUE;
00988        }
00989 
00990       dtype = debug_name_type (dhandle, name, dtype);
00991       if (dtype == DEBUG_TYPE_NULL)
00992        return FALSE;
00993 
00994       if (slot != NULL)
00995        *slot = dtype;
00996 
00997       break;
00998 
00999     case 'T':
01000       /* Struct, union, or enum tag.  For GNU C++, this can be be followed
01001         by 't' which means we are typedef'ing it as well.  */
01002       if (*p != 't')
01003        {
01004          synonym = FALSE;
01005          /* FIXME: gdb sets synonym to TRUE if the current language
01006              is C++.  */
01007        }
01008       else
01009        {
01010          synonym = TRUE;
01011          ++p;
01012        }
01013 
01014       dtype = parse_stab_type (dhandle, info, name, &p, &slot);
01015       if (dtype == DEBUG_TYPE_NULL)
01016        return FALSE;
01017       if (name == NULL)
01018        return TRUE;
01019 
01020       /* INFO->SELF_CROSSREF is set by parse_stab_type if this type is
01021          a cross reference to itself.  These are generated by some
01022          versions of g++.  */
01023       self_crossref = info->self_crossref;
01024 
01025       dtype = debug_tag_type (dhandle, name, dtype);
01026       if (dtype == DEBUG_TYPE_NULL)
01027        return FALSE;
01028       if (slot != NULL)
01029        *slot = dtype;
01030 
01031       /* See if we have a cross reference to this tag which we can now
01032          fill in.  Avoid filling in a cross reference to ourselves,
01033          because that would lead to circular debugging information.  */
01034       if (! self_crossref)
01035        {
01036          register struct stab_tag **pst;
01037 
01038          for (pst = &info->tags; *pst != NULL; pst = &(*pst)->next)
01039            {
01040              if ((*pst)->name[0] == name[0]
01041                 && strcmp ((*pst)->name, name) == 0)
01042               {
01043                 (*pst)->slot = dtype;
01044                 *pst = (*pst)->next;
01045                 break;
01046               }
01047            }
01048        }
01049 
01050       if (synonym)
01051        {
01052          dtype = debug_name_type (dhandle, name, dtype);
01053          if (dtype == DEBUG_TYPE_NULL)
01054            return FALSE;
01055 
01056          if (slot != NULL)
01057            *slot = dtype;
01058        }
01059 
01060       break;
01061 
01062     case 'V':
01063       /* Static symbol of local scope */
01064       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
01065                             (debug_type **) NULL);
01066       if (dtype == DEBUG_TYPE_NULL)
01067        return FALSE;
01068       /* FIXME: gdb checks os9k_stabs here.  */
01069       if (! stab_record_variable (dhandle, info, name, dtype,
01070                               DEBUG_LOCAL_STATIC, value))
01071        return FALSE;
01072       break;
01073 
01074     case 'v':
01075       /* Reference parameter.  */
01076       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
01077                             (debug_type **) NULL);
01078       if (dtype == DEBUG_TYPE_NULL)
01079        return FALSE;
01080       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REFERENCE,
01081                                 value))
01082        return FALSE;
01083       break;
01084 
01085     case 'a':
01086       /* Reference parameter which is in a register.  */
01087       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
01088                             (debug_type **) NULL);
01089       if (dtype == DEBUG_TYPE_NULL)
01090        return FALSE;
01091       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REF_REG,
01092                                 value))
01093        return FALSE;
01094       break;
01095 
01096     case 'X':
01097       /* This is used by Sun FORTRAN for "function result value".
01098         Sun claims ("dbx and dbxtool interfaces", 2nd ed)
01099         that Pascal uses it too, but when I tried it Pascal used
01100         "x:3" (local symbol) instead.  */
01101       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
01102                             (debug_type **) NULL);
01103       if (dtype == DEBUG_TYPE_NULL)
01104        return FALSE;
01105       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
01106                               value))
01107        return FALSE;
01108       break;
01109 
01110     default:
01111       bad_stab (string);
01112       return FALSE;
01113     }
01114 
01115   /* FIXME: gdb converts structure values to structure pointers in a
01116      couple of cases, depending upon the target.  */
01117 
01118   return TRUE;
01119 }
01120 
01121 /* Parse a stabs type.  The typename argument is non-NULL if this is a
01122    typedef or a tag definition.  The pp argument points to the stab
01123    string, and is updated.  The slotp argument points to a place to
01124    store the slot used if the type is being defined.  */
01125 
01126 static debug_type
01127 parse_stab_type (void *dhandle, struct stab_handle *info, const char *typename, const char **pp, debug_type **slotp)
01128 {
01129   const char *orig;
01130   int typenums[2];
01131   int size;
01132   bfd_boolean stringp;
01133   int descriptor;
01134   debug_type dtype;
01135 
01136   if (slotp != NULL)
01137     *slotp = NULL;
01138 
01139   orig = *pp;
01140 
01141   size = -1;
01142   stringp = FALSE;
01143 
01144   info->self_crossref = FALSE;
01145 
01146   /* Read type number if present.  The type number may be omitted.
01147      for instance in a two-dimensional array declared with type
01148      "ar1;1;10;ar1;1;10;4".  */
01149   if (! ISDIGIT (**pp) && **pp != '(' && **pp != '-')
01150     {
01151       /* 'typenums=' not present, type is anonymous.  Read and return
01152         the definition, but don't put it in the type vector.  */
01153       typenums[0] = typenums[1] = -1;
01154     }
01155   else
01156     {
01157       if (! parse_stab_type_number (pp, typenums))
01158        return DEBUG_TYPE_NULL;
01159 
01160       if (**pp != '=')
01161        /* Type is not being defined here.  Either it already
01162           exists, or this is a forward reference to it.  */
01163        return stab_find_type (dhandle, info, typenums);
01164 
01165       /* Only set the slot if the type is being defined.  This means
01166          that the mapping from type numbers to types will only record
01167          the name of the typedef which defines a type.  If we don't do
01168          this, then something like
01169             typedef int foo;
01170             int i;
01171         will record that i is of type foo.  Unfortunately, stabs
01172         information is ambiguous about variable types.  For this code,
01173             typedef int foo;
01174             int i;
01175             foo j;
01176         the stabs information records both i and j as having the same
01177         type.  This could be fixed by patching the compiler.  */
01178       if (slotp != NULL && typenums[0] >= 0 && typenums[1] >= 0)
01179        *slotp = stab_find_slot (info, typenums);
01180 
01181       /* Type is being defined here.  */
01182       /* Skip the '='.  */
01183       ++*pp;
01184 
01185       while (**pp == '@')
01186        {
01187          const char *p = *pp + 1;
01188          const char *attr;
01189 
01190          if (ISDIGIT (*p) || *p == '(' || *p == '-')
01191            /* Member type.  */
01192            break;
01193 
01194          /* Type attributes.  */
01195          attr = p;
01196 
01197          for (; *p != ';'; ++p)
01198            {
01199              if (*p == '\0')
01200               {
01201                 bad_stab (orig);
01202                 return DEBUG_TYPE_NULL;
01203               }
01204            }
01205          *pp = p + 1;
01206 
01207          switch (*attr)
01208            {
01209            case 's':
01210              size = atoi (attr + 1);
01211              size /= 8;  /* Size is in bits.  We store it in bytes.  */
01212              if (size <= 0)
01213               size = -1;
01214              break;
01215 
01216            case 'S':
01217              stringp = TRUE;
01218              break;
01219 
01220            default:
01221              /* Ignore unrecognized type attributes, so future
01222                compilers can invent new ones.  */
01223              break;
01224            }
01225        }
01226     }
01227 
01228   descriptor = **pp;
01229   ++*pp;
01230 
01231   switch (descriptor)
01232     {
01233     case 'x':
01234       {
01235        enum debug_type_kind code;
01236        const char *q1, *q2, *p;
01237 
01238        /* A cross reference to another type.  */
01239        switch (**pp)
01240          {
01241          case 's':
01242            code = DEBUG_KIND_STRUCT;
01243            break;
01244          case 'u':
01245            code = DEBUG_KIND_UNION;
01246            break;
01247          case 'e':
01248            code = DEBUG_KIND_ENUM;
01249            break;
01250          default:
01251            /* Complain and keep going, so compilers can invent new
01252               cross-reference types.  */
01253            warn_stab (orig, _("unrecognized cross reference type"));
01254            code = DEBUG_KIND_STRUCT;
01255            break;
01256          }
01257        ++*pp;
01258 
01259        q1 = strchr (*pp, '<');
01260        p = strchr (*pp, ':');
01261        if (p == NULL)
01262          {
01263            bad_stab (orig);
01264            return DEBUG_TYPE_NULL;
01265          }
01266        if (q1 != NULL && p > q1 && p[1] == ':')
01267          {
01268            int nest = 0;
01269 
01270            for (q2 = q1; *q2 != '\0'; ++q2)
01271              {
01272               if (*q2 == '<')
01273                 ++nest;
01274               else if (*q2 == '>')
01275                 --nest;
01276               else if (*q2 == ':' && nest == 0)
01277                 break;
01278              }
01279            p = q2;
01280            if (*p != ':')
01281              {
01282               bad_stab (orig);
01283               return DEBUG_TYPE_NULL;
01284              }
01285          }
01286 
01287        /* Some versions of g++ can emit stabs like
01288               fleep:T20=xsfleep:
01289           which define structures in terms of themselves.  We need to
01290           tell the caller to avoid building a circular structure.  */
01291        if (typename != NULL
01292            && strncmp (typename, *pp, p - *pp) == 0
01293            && typename[p - *pp] == '\0')
01294          info->self_crossref = TRUE;
01295 
01296        dtype = stab_find_tagged_type (dhandle, info, *pp, p - *pp, code);
01297 
01298        *pp = p + 1;
01299       }
01300       break;
01301 
01302     case '-':
01303     case '0':
01304     case '1':
01305     case '2':
01306     case '3':
01307     case '4':
01308     case '5':
01309     case '6':
01310     case '7':
01311     case '8':
01312     case '9':
01313     case '(':
01314       {
01315        const char *hold;
01316        int xtypenums[2];
01317 
01318        /* This type is defined as another type.  */
01319        (*pp)--;
01320        hold = *pp;
01321 
01322        /* Peek ahead at the number to detect void.  */
01323        if (! parse_stab_type_number (pp, xtypenums))
01324          return DEBUG_TYPE_NULL;
01325 
01326        if (typenums[0] == xtypenums[0] && typenums[1] == xtypenums[1])
01327          {
01328            /* This type is being defined as itself, which means that
01329                it is void.  */
01330            dtype = debug_make_void_type (dhandle);
01331          }
01332        else
01333          {
01334            *pp = hold;
01335 
01336            /* Go back to the number and have parse_stab_type get it.
01337               This means that we can deal with something like
01338               t(1,2)=(3,4)=... which the Lucid compiler uses.  */
01339            dtype = parse_stab_type (dhandle, info, (const char *) NULL,
01340                                  pp, (debug_type **) NULL);
01341            if (dtype == DEBUG_TYPE_NULL)
01342              return DEBUG_TYPE_NULL;
01343          }
01344 
01345        if (typenums[0] != -1)
01346          {
01347            if (! stab_record_type (dhandle, info, typenums, dtype))
01348              return DEBUG_TYPE_NULL;
01349          }
01350 
01351        break;
01352       }
01353 
01354     case '*':
01355       dtype = debug_make_pointer_type (dhandle,
01356                                    parse_stab_type (dhandle, info,
01357                                                  (const char *) NULL,
01358                                                  pp,
01359                                                  (debug_type **) NULL));
01360       break;
01361 
01362     case '&':
01363       /* Reference to another type.  */
01364       dtype = (debug_make_reference_type
01365               (dhandle,
01366               parse_stab_type (dhandle, info, (const char *) NULL, pp,
01367                              (debug_type **) NULL)));
01368       break;
01369 
01370     case 'f':
01371       /* Function returning another type.  */
01372       /* FIXME: gdb checks os9k_stabs here.  */
01373       dtype = (debug_make_function_type
01374               (dhandle,
01375               parse_stab_type (dhandle, info, (const char *) NULL, pp,
01376                              (debug_type **) NULL),
01377               (debug_type *) NULL, FALSE));
01378       break;
01379 
01380     case 'k':
01381       /* Const qualifier on some type (Sun).  */
01382       /* FIXME: gdb accepts 'c' here if os9k_stabs.  */
01383       dtype = debug_make_const_type (dhandle,
01384                                  parse_stab_type (dhandle, info,
01385                                                 (const char *) NULL,
01386                                                 pp,
01387                                                 (debug_type **) NULL));
01388       break;
01389 
01390     case 'B':
01391       /* Volatile qual on some type (Sun).  */
01392       /* FIXME: gdb accepts 'i' here if os9k_stabs.  */
01393       dtype = (debug_make_volatile_type
01394               (dhandle,
01395               parse_stab_type (dhandle, info, (const char *) NULL, pp,
01396                              (debug_type **) NULL)));
01397       break;
01398 
01399     case '@':
01400       /* Offset (class & variable) type.  This is used for a pointer
01401          relative to an object.  */
01402       {
01403        debug_type domain;
01404        debug_type memtype;
01405 
01406        /* Member type.  */
01407 
01408        domain = parse_stab_type (dhandle, info, (const char *) NULL, pp,
01409                               (debug_type **) NULL);
01410        if (domain == DEBUG_TYPE_NULL)
01411          return DEBUG_TYPE_NULL;
01412 
01413        if (**pp != ',')
01414          {
01415            bad_stab (orig);
01416            return DEBUG_TYPE_NULL;
01417          }
01418        ++*pp;
01419 
01420        memtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
01421                                (debug_type **) NULL);
01422        if (memtype == DEBUG_TYPE_NULL)
01423          return DEBUG_TYPE_NULL;
01424 
01425        dtype = debug_make_offset_type (dhandle, domain, memtype);
01426       }
01427       break;
01428 
01429     case '#':
01430       /* Method (class & fn) type.  */
01431       if (**pp == '#')
01432        {
01433          debug_type return_type;
01434 
01435          ++*pp;
01436          return_type = parse_stab_type (dhandle, info, (const char *) NULL,
01437                                     pp, (debug_type **) NULL);
01438          if (return_type == DEBUG_TYPE_NULL)
01439            return DEBUG_TYPE_NULL;
01440          if (**pp != ';')
01441            {
01442              bad_stab (orig);
01443              return DEBUG_TYPE_NULL;
01444            }
01445          ++*pp;
01446          dtype = debug_make_method_type (dhandle, return_type,
01447                                      DEBUG_TYPE_NULL,
01448                                      (debug_type *) NULL, FALSE);
01449        }
01450       else
01451        {
01452          debug_type domain;
01453          debug_type return_type;
01454          debug_type *args;
01455          unsigned int n;
01456          unsigned int alloc;
01457          bfd_boolean varargs;
01458 
01459          domain = parse_stab_type (dhandle, info, (const char *) NULL,
01460                                 pp, (debug_type **) NULL);
01461          if (domain == DEBUG_TYPE_NULL)
01462            return DEBUG_TYPE_NULL;
01463 
01464          if (**pp != ',')
01465            {
01466              bad_stab (orig);
01467              return DEBUG_TYPE_NULL;
01468            }
01469          ++*pp;
01470 
01471          return_type = parse_stab_type (dhandle, info, (const char *) NULL,
01472                                     pp, (debug_type **) NULL);
01473          if (return_type == DEBUG_TYPE_NULL)
01474            return DEBUG_TYPE_NULL;
01475 
01476          alloc = 10;
01477          args = (debug_type *) xmalloc (alloc * sizeof *args);
01478          n = 0;
01479          while (**pp != ';')
01480            {
01481              if (**pp != ',')
01482               {
01483                 bad_stab (orig);
01484                 return DEBUG_TYPE_NULL;
01485               }
01486              ++*pp;
01487 
01488              if (n + 1 >= alloc)
01489               {
01490                 alloc += 10;
01491                 args = ((debug_type *)
01492                        xrealloc (args, alloc * sizeof *args));
01493               }
01494 
01495              args[n] = parse_stab_type (dhandle, info, (const char *) NULL,
01496                                     pp, (debug_type **) NULL);
01497              if (args[n] == DEBUG_TYPE_NULL)
01498               return DEBUG_TYPE_NULL;
01499              ++n;
01500            }
01501          ++*pp;
01502 
01503          /* If the last type is not void, then this function takes a
01504             variable number of arguments.  Otherwise, we must strip
01505             the void type.  */
01506          if (n == 0
01507              || debug_get_type_kind (dhandle, args[n - 1]) != DEBUG_KIND_VOID)
01508            varargs = TRUE;
01509          else
01510            {
01511              --n;
01512              varargs = FALSE;
01513            }
01514 
01515          args[n] = DEBUG_TYPE_NULL;
01516 
01517          dtype = debug_make_method_type (dhandle, return_type, domain, args,
01518                                      varargs);
01519        }
01520       break;
01521 
01522     case 'r':
01523       /* Range type.  */
01524       dtype = parse_stab_range_type (dhandle, info, typename, pp, typenums);
01525       break;
01526 
01527     case 'b':
01528       /* FIXME: gdb checks os9k_stabs here.  */
01529       /* Sun ACC builtin int type.  */
01530       dtype = parse_stab_sun_builtin_type (dhandle, pp);
01531       break;
01532 
01533     case 'R':
01534       /* Sun ACC builtin float type.  */
01535       dtype = parse_stab_sun_floating_type (dhandle, pp);
01536       break;
01537 
01538     case 'e':
01539       /* Enumeration type.  */
01540       dtype = parse_stab_enum_type (dhandle, pp);
01541       break;
01542 
01543     case 's':
01544     case 'u':
01545       /* Struct or union type.  */
01546       dtype = parse_stab_struct_type (dhandle, info, typename, pp,
01547                                   descriptor == 's', typenums);
01548       break;
01549 
01550     case 'a':
01551       /* Array type.  */
01552       if (**pp != 'r')
01553        {
01554          bad_stab (orig);
01555          return DEBUG_TYPE_NULL;
01556        }
01557       ++*pp;
01558 
01559       dtype = parse_stab_array_type (dhandle, info, pp, stringp);
01560       break;
01561 
01562     case 'S':
01563       dtype = debug_make_set_type (dhandle,
01564                                parse_stab_type (dhandle, info,
01565                                               (const char *) NULL,
01566                                               pp,
01567                                               (debug_type **) NULL),
01568                                stringp);
01569       break;
01570 
01571     default:
01572       bad_stab (orig);
01573       return DEBUG_TYPE_NULL;
01574     }
01575 
01576   if (dtype == DEBUG_TYPE_NULL)
01577     return DEBUG_TYPE_NULL;
01578 
01579   if (typenums[0] != -1)
01580     {
01581       if (! stab_record_type (dhandle, info, typenums, dtype))
01582        return DEBUG_TYPE_NULL;
01583     }
01584 
01585   if (size != -1)
01586     {
01587       if (! debug_record_type_size (dhandle, dtype, (unsigned int) size))
01588        return DEBUG_TYPE_NULL;
01589     }
01590 
01591   return dtype;
01592 }
01593 
01594 /* Read a number by which a type is referred to in dbx data, or
01595    perhaps read a pair (FILENUM, TYPENUM) in parentheses.  Just a
01596    single number N is equivalent to (0,N).  Return the two numbers by
01597    storing them in the vector TYPENUMS.  */
01598 
01599 static bfd_boolean
01600 parse_stab_type_number (const char **pp, int *typenums)
01601 {
01602   const char *orig;
01603 
01604   orig = *pp;
01605 
01606   if (**pp != '(')
01607     {
01608       typenums[0] = 0;
01609       typenums[1] = (int) parse_number (pp, (bfd_boolean *) NULL);
01610     }
01611   else
01612     {
01613       ++*pp;
01614       typenums[0] = (int) parse_number (pp, (bfd_boolean *) NULL);
01615       if (**pp != ',')
01616        {
01617          bad_stab (orig);
01618          return FALSE;
01619        }
01620       ++*pp;
01621       typenums[1] = (int) parse_number (pp, (bfd_boolean *) NULL);
01622       if (**pp != ')')
01623        {
01624          bad_stab (orig);
01625          return FALSE;
01626        }
01627       ++*pp;
01628     }
01629 
01630   return TRUE;
01631 }
01632 
01633 /* Parse a range type.  */
01634 
01635 static debug_type
01636 parse_stab_range_type (void *dhandle, struct stab_handle *info, const char *typename, const char **pp, const int *typenums)
01637 {
01638   const char *orig;
01639   int rangenums[2];
01640   bfd_boolean self_subrange;
01641   debug_type index_type;
01642   const char *s2, *s3;
01643   bfd_signed_vma n2, n3;
01644   bfd_boolean ov2, ov3;
01645 
01646   orig = *pp;
01647 
01648   index_type = DEBUG_TYPE_NULL;
01649 
01650   /* First comes a type we are a subrange of.
01651      In C it is usually 0, 1 or the type being defined.  */
01652   if (! parse_stab_type_number (pp, rangenums))
01653     return DEBUG_TYPE_NULL;
01654 
01655   self_subrange = (rangenums[0] == typenums[0]
01656                  && rangenums[1] == typenums[1]);
01657 
01658   if (**pp == '=')
01659     {
01660       *pp = orig;
01661       index_type = parse_stab_type (dhandle, info, (const char *) NULL,
01662                                 pp, (debug_type **) NULL);
01663       if (index_type == DEBUG_TYPE_NULL)
01664        return DEBUG_TYPE_NULL;
01665     }
01666 
01667   if (**pp == ';')
01668     ++*pp;
01669 
01670   /* The remaining two operands are usually lower and upper bounds of
01671      the range.  But in some special cases they mean something else.  */
01672   s2 = *pp;
01673   n2 = parse_number (pp, &ov2);
01674   if (**pp != ';')
01675     {
01676       bad_stab (orig);
01677       return DEBUG_TYPE_NULL;
01678     }
01679   ++*pp;
01680 
01681   s3 = *pp;
01682   n3 = parse_number (pp, &ov3);
01683   if (**pp != ';')
01684     {
01685       bad_stab (orig);
01686       return DEBUG_TYPE_NULL;
01687     }
01688   ++*pp;
01689 
01690   if (ov2 || ov3)
01691     {
01692       /* gcc will emit range stabs for long long types.  Handle this
01693          as a special case.  FIXME: This needs to be more general.  */
01694 #define LLLOW   "01000000000000000000000;"
01695 #define LLHIGH   "0777777777777777777777;"
01696 #define ULLHIGH "01777777777777777777777;"
01697       if (index_type == DEBUG_TYPE_NULL)
01698        {
01699          if (CONST_STRNEQ (s2, LLLOW)
01700              && CONST_STRNEQ (s3, LLHIGH))
01701            return debug_make_int_type (dhandle, 8, FALSE);
01702          if (! ov2
01703              && n2 == 0
01704              && CONST_STRNEQ (s3, ULLHIGH))
01705            return debug_make_int_type (dhandle, 8, TRUE);
01706        }
01707 
01708       warn_stab (orig, _("numeric overflow"));
01709     }
01710 
01711   if (index_type == DEBUG_TYPE_NULL)
01712     {
01713       /* A type defined as a subrange of itself, with both bounds 0,
01714          is void.  */
01715       if (self_subrange && n2 == 0 && n3 == 0)
01716        return debug_make_void_type (dhandle);
01717 
01718       /* A type defined as a subrange of itself, with n2 positive and
01719         n3 zero, is a complex type, and n2 is the number of bytes.  */
01720       if (self_subrange && n3 == 0 && n2 > 0)
01721        return debug_make_complex_type (dhandle, n2);
01722 
01723       /* If n3 is zero and n2 is positive, this is a floating point
01724          type, and n2 is the number of bytes.  */
01725       if (n3 == 0 && n2 > 0)
01726        return debug_make_float_type (dhandle, n2);
01727 
01728       /* If the upper bound is -1, this is an unsigned int.  */
01729       if (n2 == 0 && n3 == -1)
01730        {
01731          /* When gcc is used with -gstabs, but not -gstabs+, it will emit
01732                 long long int:t6=r1;0;-1;
01733                long long unsigned int:t7=r1;0;-1;
01734             We hack here to handle this reasonably.  */
01735          if (typename != NULL)
01736            {
01737              if (strcmp (typename, "long long int") == 0)
01738               return debug_make_int_type (dhandle, 8, FALSE);
01739              else if (strcmp (typename, "long long unsigned int") == 0)
01740               return debug_make_int_type (dhandle, 8, TRUE);
01741            }
01742          /* FIXME: The size here really depends upon the target.  */
01743          return debug_make_int_type (dhandle, 4, TRUE);
01744        }
01745 
01746       /* A range of 0 to 127 is char.  */
01747       if (self_subrange && n2 == 0 && n3 == 127)
01748        return debug_make_int_type (dhandle, 1, FALSE);
01749 
01750       /* FIXME: gdb checks for the language CHILL here.  */
01751 
01752       if (n2 == 0)
01753        {
01754          if (n3 < 0)
01755            return debug_make_int_type (dhandle, - n3, TRUE);
01756          else if (n3 == 0xff)
01757            return debug_make_int_type (dhandle, 1, TRUE);
01758          else if (n3 == 0xffff)
01759            return debug_make_int_type (dhandle, 2, TRUE);
01760          else if (n3 == (bfd_signed_vma) 0xffffffff)
01761            return debug_make_int_type (dhandle, 4, TRUE);
01762 #ifdef BFD64
01763          else if (n3 == ((((bfd_signed_vma) 0xffffffff) << 32) | 0xffffffff))
01764            return debug_make_int_type (dhandle, 8, TRUE);
01765 #endif
01766        }
01767       else if (n3 == 0
01768               && n2 < 0
01769               && (self_subrange || n2 == -8))
01770        return debug_make_int_type (dhandle, - n2, TRUE);
01771       else if (n2 == - n3 - 1 || n2 == n3 + 1)
01772        {
01773          if (n3 == 0x7f)
01774            return debug_make_int_type (dhandle, 1, FALSE);
01775          else if (n3 == 0x7fff)
01776            return debug_make_int_type (dhandle, 2, FALSE);
01777          else if (n3 == 0x7fffffff)
01778            return debug_make_int_type (dhandle, 4, FALSE);
01779 #ifdef BFD64
01780          else if (n3 == ((((bfd_vma) 0x7fffffff) << 32) | 0xffffffff))
01781            return debug_make_int_type (dhandle, 8, FALSE);
01782 #endif
01783        }
01784     }
01785 
01786   /* At this point I don't have the faintest idea how to deal with a
01787      self_subrange type; I'm going to assume that this is used as an
01788      idiom, and that all of them are special cases.  So . . .  */
01789   if (self_subrange)
01790     {
01791       bad_stab (orig);
01792       return DEBUG_TYPE_NULL;
01793     }
01794 
01795   index_type = stab_find_type (dhandle, info, rangenums);
01796   if (index_type == DEBUG_TYPE_NULL)
01797     {
01798       /* Does this actually ever happen?  Is that why we are worrying
01799          about dealing with it rather than just calling error_type?  */
01800       warn_stab (orig, _("missing index type"));
01801       index_type = debug_make_int_type (dhandle, 4, FALSE);
01802     }
01803 
01804   return debug_make_range_type (dhandle, index_type, n2, n3);
01805 }
01806 
01807 /* Sun's ACC uses a somewhat saner method for specifying the builtin
01808    typedefs in every file (for int, long, etc):
01809 
01810        type = b <signed> <width>; <offset>; <nbits>
01811        signed = u or s.  Possible c in addition to u or s (for char?).
01812        offset = offset from high order bit to start bit of type.
01813        width is # bytes in object of this type, nbits is # bits in type.
01814 
01815    The width/offset stuff appears to be for small objects stored in
01816    larger ones (e.g. `shorts' in `int' registers).  We ignore it for now,
01817    FIXME.  */
01818 
01819 static debug_type
01820 parse_stab_sun_builtin_type (void *dhandle, const char **pp)
01821 {
01822   const char *orig;
01823   bfd_boolean unsignedp;
01824   bfd_vma bits;
01825 
01826   orig = *pp;
01827 
01828   switch (**pp)
01829     {
01830     case 's':
01831       unsignedp = FALSE;
01832       break;
01833     case 'u':
01834       unsignedp = TRUE;
01835       break;
01836     default:
01837       bad_stab (orig);
01838       return DEBUG_TYPE_NULL;
01839     }
01840   ++*pp;
01841 
01842   /* For some odd reason, all forms of char put a c here.  This is strange
01843      because no other type has this honor.  We can safely ignore this because
01844      we actually determine 'char'acterness by the number of bits specified in
01845      the descriptor.  */
01846   if (**pp == 'c')
01847     ++*pp;
01848 
01849   /* The first number appears to be the number of bytes occupied
01850      by this type, except that unsigned short is 4 instead of 2.
01851      Since this information is redundant with the third number,
01852      we will ignore it.  */
01853   (void) parse_number (pp, (bfd_boolean *) NULL);
01854   if (**pp != ';')
01855     {
01856       bad_stab (orig);
01857       return DEBUG_TYPE_NULL;
01858     }
01859   ++*pp;
01860 
01861   /* The second number is always 0, so ignore it too.  */
01862   (void) parse_number (pp, (bfd_boolean *) NULL);
01863   if (**pp != ';')
01864     {
01865       bad_stab (orig);
01866       return DEBUG_TYPE_NULL;
01867     }
01868   ++*pp;
01869 
01870   /* The third number is the number of bits for this type.  */
01871   bits = parse_number (pp, (bfd_boolean *) NULL);
01872 
01873   /* The type *should* end with a semicolon.  If it are embedded
01874      in a larger type the semicolon may be the only way to know where
01875      the type ends.  If this type is at the end of the stabstring we
01876      can deal with the omitted semicolon (but we don't have to like
01877      it).  Don't bother to complain(), Sun's compiler omits the semicolon
01878      for "void".  */
01879   if (**pp == ';')
01880     ++*pp;
01881 
01882   if (bits == 0)
01883     return debug_make_void_type (dhandle);
01884 
01885   return debug_make_int_type (dhandle, bits / 8, unsignedp);
01886 }
01887 
01888 /* Parse a builtin floating type generated by the Sun compiler.  */
01889 
01890 static debug_type
01891 parse_stab_sun_floating_type (void *dhandle, const char **pp)
01892 {
01893   const char *orig;
01894   bfd_vma details;
01895   bfd_vma bytes;
01896 
01897   orig = *pp;
01898 
01899   /* The first number has more details about the type, for example
01900      FN_COMPLEX.  */
01901   details = parse_number (pp, (bfd_boolean *) NULL);
01902   if (**pp != ';')
01903     {
01904       bad_stab (orig);
01905       return DEBUG_TYPE_NULL;
01906     }
01907 
01908   /* The second number is the number of bytes occupied by this type */
01909   bytes = parse_number (pp, (bfd_boolean *) NULL);
01910   if (**pp != ';')
01911     {
01912       bad_stab (orig);
01913       return DEBUG_TYPE_NULL;
01914     }
01915 
01916   if (details == NF_COMPLEX
01917       || details == NF_COMPLEX16
01918       || details == NF_COMPLEX32)
01919     return debug_make_complex_type (dhandle, bytes);
01920 
01921   return debug_make_float_type (dhandle, bytes);
01922 }
01923 
01924 /* Handle an enum type.  */
01925 
01926 static debug_type
01927 parse_stab_enum_type (void *dhandle, const char **pp)
01928 {
01929   const char *orig;
01930   const char **names;
01931   bfd_signed_vma *values;
01932   unsigned int n;
01933   unsigned int alloc;
01934 
01935   orig = *pp;
01936 
01937   /* FIXME: gdb checks os9k_stabs here.  */
01938 
01939   /* The aix4 compiler emits an extra field before the enum members;
01940      my guess is it's a type of some sort.  Just ignore it.  */
01941   if (**pp == '-')
01942     {
01943       while (**pp != ':')
01944        ++*pp;
01945       ++*pp;
01946     }
01947 
01948   /* Read the value-names and their values.
01949      The input syntax is NAME:VALUE,NAME:VALUE, and so on.
01950      A semicolon or comma instead of a NAME means the end.  */
01951   alloc = 10;
01952   names = (const char **) xmalloc (alloc * sizeof *names);
01953   values = (bfd_signed_vma *) xmalloc (alloc * sizeof *values);
01954   n = 0;
01955   while (**pp != '\0' && **pp != ';' && **pp != ',')
01956     {
01957       const char *p;
01958       char *name;
01959       bfd_signed_vma val;
01960 
01961       p = *pp;
01962       while (*p != ':')
01963        ++p;
01964 
01965       name = savestring (*pp, p - *pp);
01966 
01967       *pp = p + 1;
01968       val = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL);
01969       if (**pp != ',')
01970        {
01971          bad_stab (orig);
01972          return DEBUG_TYPE_NULL;
01973        }
01974       ++*pp;
01975 
01976       if (n + 1 >= alloc)
01977        {
01978          alloc += 10;
01979          names = ((const char **)
01980                  xrealloc (names, alloc * sizeof *names));
01981          values = ((bfd_signed_vma *)
01982                   xrealloc (values, alloc * sizeof *values));
01983        }
01984 
01985       names[n] = name;
01986       values[n] = val;
01987       ++n;
01988     }
01989 
01990   names[n] = NULL;
01991   values[n] = 0;
01992 
01993   if (**pp == ';')
01994     ++*pp;
01995 
01996   return debug_make_enum_type (dhandle, names, values);
01997 }
01998 
01999 /* Read the description of a structure (or union type) and return an object
02000    describing the type.
02001 
02002    PP points to a character pointer that points to the next unconsumed token
02003    in the stabs string.  For example, given stabs "A:T4=s4a:1,0,32;;",
02004    *PP will point to "4a:1,0,32;;".  */
02005 
02006 static debug_type
02007 parse_stab_struct_type (void *dhandle, struct stab_handle *info,
02008                      const char *tagname, const char **pp,
02009                      bfd_boolean structp, const int *typenums)
02010 {
02011   const char *orig;
02012   bfd_vma size;
02013   debug_baseclass *baseclasses;
02014   debug_field *fields;
02015   bfd_boolean statics;
02016   debug_method *methods;
02017   debug_type vptrbase;
02018   bfd_boolean ownvptr;
02019 
02020   orig = *pp;
02021 
02022   /* Get the size.  */
02023   size = parse_number (pp, (bfd_boolean *) NULL);
02024 
02025   /* Get the other information.  */
02026   if (! parse_stab_baseclasses (dhandle, info, pp, &baseclasses)
02027       || ! parse_stab_struct_fields (dhandle, info, pp, &fields, &statics)
02028       || ! parse_stab_members (dhandle, info, tagname, pp, typenums, &methods)
02029       || ! parse_stab_tilde_field (dhandle, info, pp, typenums, &vptrbase,
02030                                &ownvptr))
02031     return DEBUG_TYPE_NULL;
02032 
02033   if (! statics
02034       && baseclasses == NULL
02035       && methods == NULL
02036       && vptrbase == DEBUG_TYPE_NULL
02037       && ! ownvptr)
02038     return debug_make_struct_type (dhandle, structp, size, fields);
02039 
02040   return debug_make_object_type (dhandle, structp, size, fields, baseclasses,
02041                              methods, vptrbase, ownvptr);
02042 }
02043 
02044 /* The stabs for C++ derived classes contain baseclass information which
02045    is marked by a '!' character after the total size.  This function is
02046    called when we encounter the baseclass marker, and slurps up all the
02047    baseclass information.
02048 
02049    Immediately following the '!' marker is the number of base classes that
02050    the class is derived from, followed by information for each base class.
02051    For each base class, there are two visibility specifiers, a bit offset
02052    to the base class information within the derived class, a reference to
02053    the type for the base class, and a terminating semicolon.
02054 
02055    A typical example, with two base classes, would be "!2,020,19;0264,21;".
02056                                                  ^^ ^ ^ ^  ^ ^  ^
02057        Baseclass information marker __________________|| | | |  | |  |
02058        Number of baseclasses __________________________| | | |  | |  |
02059        Visibility specifiers (2) ________________________| | |  | |  |
02060        Offset in bits from start of class _________________| |  | |  |
02061        Type number for base class ___________________________|  | |  |
02062        Visibility specifiers (2) _______________________________| |  |
02063        Offset in bits from start of class ________________________|  |
02064        Type number of base class ____________________________________|
02065 
02066   Return TRUE for success, FALSE for failure.  */
02067 
02068 static bfd_boolean
02069 parse_stab_baseclasses (void *dhandle, struct stab_handle *info,
02070                      const char **pp, debug_baseclass **retp)
02071 {
02072   const char *orig;
02073   unsigned int c, i;
02074   debug_baseclass *classes;
02075 
02076   *retp = NULL;
02077 
02078   orig = *pp;
02079 
02080   if (**pp != '!')
02081     {
02082       /* No base classes.  */
02083       return TRUE;
02084     }
02085   ++*pp;
02086 
02087   c = (unsigned int) parse_number (pp, (bfd_boolean *) NULL);
02088 
02089   if (**pp != ',')
02090     {
02091       bad_stab (orig);
02092       return FALSE;
02093     }
02094   ++*pp;
02095 
02096   classes = (debug_baseclass *) xmalloc ((c + 1) * sizeof (**retp));
02097 
02098   for (i = 0; i < c; i++)
02099     {
02100       bfd_boolean virtual;
02101       enum debug_visibility visibility;
02102       bfd_vma bitpos;
02103       debug_type type;
02104 
02105       switch (**pp)
02106        {
02107        case '0':
02108          virtual = FALSE;
02109          break;
02110        case '1':
02111          virtual = TRUE;
02112          break;
02113        default:
02114          warn_stab (orig, _("unknown virtual character for baseclass"));
02115          virtual = FALSE;
02116          break;
02117        }
02118       ++*pp;
02119 
02120       switch (**pp)
02121        {
02122        case '0':
02123          visibility = DEBUG_VISIBILITY_PRIVATE;
02124          break;
02125        case '1':
02126          visibility = DEBUG_VISIBILITY_PROTECTED;
02127          break;
02128        case '2':
02129          visibility = DEBUG_VISIBILITY_PUBLIC;
02130          break;
02131        default:
02132          warn_stab (orig, _("unknown visibility character for baseclass"));
02133          visibility = DEBUG_VISIBILITY_PUBLIC;
02134          break;
02135        }
02136       ++*pp;
02137 
02138       /* The remaining value is the bit offset of the portion of the
02139         object corresponding to this baseclass.  Always zero in the
02140         absence of multiple inheritance.  */
02141       bitpos = parse_number (pp, (bfd_boolean *) NULL);
02142       if (**pp != ',')
02143        {
02144          bad_stab (orig);
02145          return FALSE;
02146        }
02147       ++*pp;
02148 
02149       type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
02150                            (debug_type **) NULL);
02151       if (type == DEBUG_TYPE_NULL)
02152        return FALSE;
02153 
02154       classes[i] = debug_make_baseclass (dhandle, type, bitpos, virtual,
02155                                     visibility);
02156       if (classes[i] == DEBUG_BASECLASS_NULL)
02157        return FALSE;
02158 
02159       if (**pp != ';')
02160        return FALSE;
02161       ++*pp;
02162     }
02163 
02164   classes[i] = DEBUG_BASECLASS_NULL;
02165 
02166   *retp = classes;
02167 
02168   return TRUE;
02169 }
02170 
02171 /* Read struct or class data fields.  They have the form:
02172 
02173        NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
02174 
02175    At the end, we see a semicolon instead of a field.
02176 
02177    In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
02178    a static field.
02179 
02180    The optional VISIBILITY is one of:
02181 
02182        '/0'   (VISIBILITY_PRIVATE)
02183        '/1'   (VISIBILITY_PROTECTED)
02184        '/2'   (VISIBILITY_PUBLIC)
02185        '/9'   (VISIBILITY_IGNORE)
02186 
02187    or nothing, for C style fields with public visibility.
02188 
02189    Returns 1 for success, 0 for failure.  */
02190 
02191 static bfd_boolean
02192 parse_stab_struct_fields (void *dhandle, struct stab_handle *info,
02193                        const char **pp, debug_field **retp,
02194                        bfd_boolean *staticsp)
02195 {
02196   const char *orig;
02197   const char *p;
02198   debug_field *fields;
02199   unsigned int c;
02200   unsigned int alloc;
02201 
02202   *retp = NULL;
02203   *staticsp = FALSE;
02204 
02205   orig = *pp;
02206 
02207   c = 0;
02208   alloc = 10;
02209   fields = (debug_field *) xmalloc (alloc * sizeof *fields);
02210   while (**pp != ';')
02211     {
02212       /* FIXME: gdb checks os9k_stabs here.  */
02213 
02214       p = *pp;
02215 
02216       /* Add 1 to c to leave room for NULL pointer at end.  */
02217       if (c + 1 >= alloc)
02218        {
02219          alloc += 10;
02220          fields = ((debug_field *)
02221                   xrealloc (fields, alloc * sizeof *fields));
02222        }
02223 
02224       /* If it starts with CPLUS_MARKER it is a special abbreviation,
02225         unless the CPLUS_MARKER is followed by an underscore, in
02226         which case it is just the name of an anonymous type, which we
02227         should handle like any other type name.  We accept either '$'
02228         or '.', because a field name can never contain one of these
02229         characters except as a CPLUS_MARKER.  */
02230 
02231       if ((*p == '$' || *p == '.') && p[1] != '_')
02232        {
02233          ++*pp;
02234          if (! parse_stab_cpp_abbrev (dhandle, info, pp, fields + c))
02235            return FALSE;
02236          ++c;
02237          continue;
02238        }
02239 
02240       /* Look for the ':' that separates the field name from the field
02241         values.  Data members are delimited by a single ':', while member
02242         functions are delimited by a pair of ':'s.  When we hit the member
02243         functions (if any), terminate scan loop and return.  */
02244 
02245       p = strchr (p, ':');
02246       if (p == NULL)
02247        {
02248          bad_stab (orig);
02249          return FALSE;
02250        }
02251 
02252       if (p[1] == ':')
02253        break;
02254 
02255       if (! parse_stab_one_struct_field (dhandle, info, pp, p, fields + c,
02256                                     staticsp))
02257        return FALSE;
02258 
02259       ++c;
02260     }
02261 
02262   fields[c] = DEBUG_FIELD_NULL;
02263 
02264   *retp = fields;
02265 
02266   return TRUE;
02267 }
02268 
02269 /* Special GNU C++ name.  */
02270 
02271 static bfd_boolean
02272 parse_stab_cpp_abbrev (void *dhandle, struct stab_handle *info,
02273                      const char **pp, debug_field *retp)
02274 {
02275   const char *orig;
02276   int cpp_abbrev;
02277   debug_type context;
02278   const char *name;
02279   const char *typename;
02280   debug_type type;
02281   bfd_vma bitpos;
02282 
02283   *retp = DEBUG_FIELD_NULL;
02284 
02285   orig = *pp;
02286 
02287   if (**pp != 'v')
02288     {
02289       bad_stab (*pp);
02290       return FALSE;
02291     }
02292   ++*pp;
02293 
02294   cpp_abbrev = **pp;
02295   ++*pp;
02296 
02297   /* At this point, *pp points to something like "22:23=*22...", where
02298      the type number before the ':' is the "context" and everything
02299      after is a regular type definition.  Lookup the type, find it's
02300      name, and construct the field name.  */
02301 
02302   context = parse_stab_type (dhandle, info, (const char *) NULL, pp,
02303                           (debug_type **) NULL);
02304   if (context == DEBUG_TYPE_NULL)
02305     return FALSE;
02306 
02307   switch (cpp_abbrev)
02308     {
02309     case 'f':
02310       /* $vf -- a virtual function table pointer.  */
02311       name = "_vptr$";
02312       break;
02313     case 'b':
02314       /* $vb -- a virtual bsomethingorother */
02315       typename = debug_get_type_name (dhandle, context);
02316       if (typename == NULL)
02317        {
02318          warn_stab (orig, _("unnamed $vb type"));
02319          typename = "FOO";
02320        }
02321       name = concat ("_vb$", typename, (const char *) NULL);
02322       break;
02323     default:
02324       warn_stab (orig, _("unrecognized C++ abbreviation"));
02325       name = "INVALID_CPLUSPLUS_ABBREV";
02326       break;
02327     }
02328 
02329   if (**pp != ':')
02330     {
02331       bad_stab (orig);
02332       return FALSE;
02333     }
02334   ++*pp;
02335 
02336   type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
02337                        (debug_type **) NULL);
02338   if (**pp != ',')
02339     {
02340       bad_stab (orig);
02341       return FALSE;
02342     }
02343   ++*pp;
02344 
02345   bitpos = parse_number (pp, (bfd_boolean *) NULL);
02346   if (**pp != ';')
02347     {
02348       bad_stab (orig);
02349       return FALSE;
02350     }
02351   ++*pp;
02352 
02353   *retp = debug_make_field (dhandle, name, type, bitpos, 0,
02354                          DEBUG_VISIBILITY_PRIVATE);
02355   if (*retp == DEBUG_FIELD_NULL)
02356     return FALSE;
02357 
02358   return TRUE;
02359 }
02360 
02361 /* Parse a single field in a struct or union.  */
02362 
02363 static bfd_boolean
02364 parse_stab_one_struct_field (void *dhandle, struct stab_handle *info,
02365                           const char **pp, const char *p,
02366                           debug_field *retp, bfd_boolean *staticsp)
02367 {
02368   const char *orig;
02369   char *name;
02370   enum debug_visibility visibility;
02371   debug_type type;
02372   bfd_vma bitpos;
02373   bfd_vma bitsize;
02374 
02375   orig = *pp;
02376 
02377   /* FIXME: gdb checks ARM_DEMANGLING here.  */
02378 
02379   name = savestring (*pp, p - *pp);
02380 
02381   *pp = p + 1;
02382 
02383   if (**pp != '/')
02384     visibility = DEBUG_VISIBILITY_PUBLIC;
02385   else
02386     {
02387       ++*pp;
02388       switch (**pp)
02389        {
02390        case '0':
02391          visibility = DEBUG_VISIBILITY_PRIVATE;
02392          break;
02393        case '1':
02394          visibility = DEBUG_VISIBILITY_PROTECTED;
02395          break;
02396        case '2':
02397          visibility = DEBUG_VISIBILITY_PUBLIC;
02398          break;
02399        default:
02400          warn_stab (orig, _("unknown visibility character for field"));
02401          visibility = DEBUG_VISIBILITY_PUBLIC;
02402          break;
02403        }
02404       ++*pp;
02405     }
02406 
02407   type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
02408                        (debug_type **) NULL);
02409   if (type == DEBUG_TYPE_NULL)
02410     return FALSE;
02411 
02412   if (**pp == ':')
02413     {
02414       char *varname;
02415 
02416       /* This is a static class member.  */
02417       ++*pp;
02418       p = strchr (*pp, ';');
02419       if (p == NULL)
02420        {
02421          bad_stab (orig);
02422          return FALSE;
02423        }
02424 
02425       varname = savestring (*pp, p - *pp);
02426 
02427       *pp = p + 1;
02428 
02429       *retp = debug_make_static_member (dhandle, name, type, varname,
02430                                    visibility);
02431       *staticsp = TRUE;
02432 
02433       return TRUE;
02434     }
02435 
02436   if (**pp != ',')
02437     {
02438       bad_stab (orig);
02439       return FALSE;
02440     }
02441   ++*pp;
02442 
02443   bitpos = parse_number (pp, (bfd_boolean *) NULL);
02444   if (**pp != ',')
02445     {
02446       bad_stab (orig);
02447       return FALSE;
02448     }
02449   ++*pp;
02450 
02451   bitsize = parse_number (pp, (bfd_boolean *) NULL);
02452   if (**pp != ';')
02453     {
02454       bad_stab (orig);
02455       return FALSE;
02456     }
02457   ++*pp;
02458 
02459   if (bitpos == 0 && bitsize == 0)
02460     {
02461       /* This can happen in two cases: (1) at least for gcc 2.4.5 or
02462         so, it is a field which has been optimized out.  The correct
02463         stab for this case is to use VISIBILITY_IGNORE, but that is a
02464         recent invention.  (2) It is a 0-size array.  For example
02465         union { int num; char str[0]; } foo.  Printing "<no value>"
02466         for str in "p foo" is OK, since foo.str (and thus foo.str[3])
02467         will continue to work, and a 0-size array as a whole doesn't
02468         have any contents to print.
02469 
02470         I suspect this probably could also happen with gcc -gstabs
02471         (not -gstabs+) for static fields, and perhaps other C++
02472         extensions.  Hopefully few people use -gstabs with gdb, since
02473         it is intended for dbx compatibility.  */
02474       visibility = DEBUG_VISIBILITY_IGNORE;
02475     }
02476 
02477   /* FIXME: gdb does some stuff here to mark fields as unpacked.  */
02478 
02479   *retp = debug_make_field (dhandle, name, type, bitpos, bitsize, visibility);
02480 
02481   return TRUE;
02482 }
02483 
02484 /* Read member function stabs info for C++ classes.  The form of each member
02485    function data is:
02486 
02487        NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
02488 
02489    An example with two member functions is:
02490 
02491        afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
02492 
02493    For the case of overloaded operators, the format is op$::*.funcs, where
02494    $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
02495    name (such as `+=') and `.' marks the end of the operator name.  */
02496 
02497 static bfd_boolean
02498 parse_stab_members (void *dhandle, struct stab_handle *info,
02499                   const char *tagname, const char **pp,
02500                   const int *typenums, debug_method **retp)
02501 {
02502   const char *orig;
02503   debug_method *methods;
02504   unsigned int c;
02505   unsigned int alloc;
02506 
02507   *retp = NULL;
02508 
02509   orig = *pp;
02510 
02511   alloc = 0;
02512   methods = NULL;
02513   c = 0;
02514 
02515   while (**pp != ';')
02516     {
02517       const char *p;
02518       char *name;
02519       debug_method_variant *variants;
02520       unsigned int cvars;
02521       unsigned int allocvars;
02522       debug_type look_ahead_type;
02523 
02524       p = strchr (*pp, ':');
02525       if (p == NULL || p[1] != ':')
02526        break;
02527 
02528       /* FIXME: Some systems use something other than '$' here.  */
02529       if ((*pp)[0] != 'o' || (*pp)[1] != 'p' || (*pp)[2] != '$')
02530        {
02531          name = savestring (*pp, p - *pp);
02532          *pp = p + 2;
02533        }
02534       else
02535        {
02536          /* This is a completely weird case.  In order to stuff in the
02537             names that might contain colons (the usual name delimiter),
02538             Mike Tiemann defined a different name format which is
02539             signalled if the identifier is "op$".  In that case, the
02540             format is "op$::XXXX." where XXXX is the name.  This is
02541             used for names like "+" or "=".  YUUUUUUUK!  FIXME!  */
02542          *pp = p + 2;
02543          for (p = *pp; *p != '.' && *p != '\0'; p++)
02544            ;
02545          if (*p != '.')
02546            {
02547              bad_stab (orig);
02548              return FALSE;
02549            }
02550          name = savestring (*pp, p - *pp);
02551          *pp = p + 1;
02552        }
02553 
02554       allocvars = 10;
02555       variants = ((debug_method_variant *)
02556                 xmalloc (allocvars * sizeof *variants));
02557       cvars = 0;
02558 
02559       look_ahead_type = DEBUG_TYPE_NULL;
02560 
02561       do
02562        {
02563          debug_type type;
02564          bfd_boolean stub;
02565          char *argtypes;
02566          enum debug_visibility visibility;
02567          bfd_boolean constp, volatilep, staticp;
02568          bfd_vma voffset;
02569          debug_type context;
02570          const char *physname;
02571          bfd_boolean varargs;
02572 
02573          if (look_ahead_type != DEBUG_TYPE_NULL)
02574            {
02575              /* g++ version 1 kludge */
02576              type = look_ahead_type;
02577              look_ahead_type = DEBUG_TYPE_NULL;
02578            }
02579          else
02580            {
02581              type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
02582                                   (debug_type **) NULL);
02583              if (type == DEBUG_TYPE_NULL)
02584               return FALSE;
02585              if (**pp != ':')
02586               {
02587                 bad_stab (orig);
02588                 return FALSE;
02589               }
02590            }
02591 
02592          ++*pp;
02593          p = strchr (*pp, ';');
02594          if (p == NULL)
02595            {
02596              bad_stab (orig);
02597              return FALSE;
02598            }
02599 
02600          stub = FALSE;
02601          if (debug_get_type_kind (dhandle, type) == DEBUG_KIND_METHOD
02602              && debug_get_parameter_types (dhandle, type, &varargs) == NULL)
02603            stub = TRUE;
02604 
02605          argtypes = savestring (*pp, p - *pp);
02606          *pp = p + 1;
02607 
02608          switch (**pp)
02609            {
02610            case '0':
02611              visibility = DEBUG_VISIBILITY_PRIVATE;
02612              break;
02613            case '1':
02614              visibility = DEBUG_VISIBILITY_PROTECTED;
02615              break;
02616            default:
02617              visibility = DEBUG_VISIBILITY_PUBLIC;
02618              break;
02619            }
02620          ++*pp;
02621 
02622          constp = FALSE;
02623          volatilep = FALSE;
02624          switch (**pp)
02625            {
02626            case 'A':
02627              /* Normal function.  */
02628              ++*pp;
02629              break;
02630            case 'B':
02631              /* const member function.  */
02632              constp = TRUE;
02633              ++*pp;
02634              break;
02635            case 'C':
02636              /* volatile member function.  */
02637              volatilep = TRUE;
02638              ++*pp;
02639              break;
02640            case 'D':
02641              /* const volatile member function.  */
02642              constp = TRUE;
02643              volatilep = TRUE;
02644              ++*pp;
02645              break;
02646            case '*':
02647            case '?':
02648            case '.':
02649              /* File compiled with g++ version 1; no information.  */
02650              break;
02651            default:
02652              warn_stab (orig, _("const/volatile indicator missing"));
02653              break;
02654            }
02655 
02656          staticp = FALSE;
02657          switch (**pp)
02658            {
02659            case '*':
02660              /* virtual member function, followed by index.  The sign
02661                bit is supposedly set to distinguish
02662                pointers-to-methods from virtual function indicies.  */
02663              ++*pp;
02664              voffset = parse_number (pp, (bfd_boolean *) NULL);
02665              if (**pp != ';')
02666               {
02667                 bad_stab (orig);
02668                 return FALSE;
02669               }
02670              ++*pp;
02671              voffset &= 0x7fffffff;
02672 
02673              if (**pp == ';' || *pp == '\0')
02674               {
02675                 /* Must be g++ version 1.  */
02676                 context = DEBUG_TYPE_NULL;
02677               }
02678              else
02679               {
02680                 /* Figure out from whence this virtual function
02681                    came.  It may belong to virtual function table of
02682                    one of its baseclasses.  */
02683                 look_ahead_type = parse_stab_type (dhandle, info,
02684                                                (const char *) NULL,
02685                                                pp,
02686                                                (debug_type **) NULL);
02687                 if (**pp == ':')
02688                   {
02689                     /* g++ version 1 overloaded methods.  */
02690                     context = DEBUG_TYPE_NULL;
02691                   }
02692                 else
02693                   {
02694                     context = look_ahead_type;
02695                     look_ahead_type = DEBUG_TYPE_NULL;
02696                     if (**pp != ';')
02697                      {
02698                        bad_stab (orig);
02699                        return FALSE;
02700                      }
02701                     ++*pp;
02702                   }
02703               }
02704              break;
02705 
02706            case '?':
02707              /* static member function.  */
02708              ++*pp;
02709              staticp = TRUE;
02710              voffset = 0;
02711              context = DEBUG_TYPE_NULL;
02712              if (strncmp (argtypes, name, strlen (name)) != 0)
02713               stub = TRUE;
02714              break;
02715 
02716            default:
02717              warn_stab (orig, "member function type missing");
02718              voffset = 0;
02719              context = DEBUG_TYPE_NULL;
02720              break;
02721 
02722            case '.':
02723              ++*pp;
02724              voffset = 0;
02725              context = DEBUG_TYPE_NULL;
02726              break;
02727            }
02728 
02729          /* If the type is not a stub, then the argtypes string is
02730              the physical name of the function.  Otherwise the
02731              argtypes string is the mangled form of the argument
02732              types, and the full type and the physical name must be
02733              extracted from them.  */
02734          if (! stub)
02735            physname = argtypes;
02736          else
02737            {
02738              debug_type class_type, return_type;
02739 
02740              class_type = stab_find_type (dhandle, info, typenums);
02741              if (class_type == DEBUG_TYPE_NULL)
02742               return FALSE;
02743              return_type = debug_get_return_type (dhandle, type);
02744              if (return_type == DEBUG_TYPE_NULL)
02745               {
02746                 bad_stab (orig);
02747                 return FALSE;
02748               }
02749              type = parse_stab_argtypes (dhandle, info, class_type, name,
02750                                      tagname, return_type, argtypes,
02751                                      constp, volatilep, &physname);
02752              if (type == DEBUG_TYPE_NULL)
02753               return FALSE;
02754            }
02755 
02756          if (cvars + 1 >= allocvars)
02757            {
02758              allocvars += 10;
02759              variants = ((debug_method_variant *)
02760                        xrealloc (variants,
02761                                 allocvars * sizeof *variants));
02762            }
02763 
02764          if (! staticp)
02765            variants[cvars] = debug_make_method_variant (dhandle, physname,
02766                                                   type, visibility,
02767                                                   constp, volatilep,
02768                                                   voffset, context);
02769          else
02770            variants[cvars] = debug_make_static_method_variant (dhandle,
02771                                                         physname,
02772                                                         type,
02773                                                         visibility,
02774                                                         constp,
02775                                                         volatilep);
02776          if (variants[cvars] == DEBUG_METHOD_VARIANT_NULL)
02777            return FALSE;
02778 
02779          ++cvars;
02780        }
02781       while (**pp != ';' && **pp != '\0');
02782 
02783       variants[cvars] = DEBUG_METHOD_VARIANT_NULL;
02784 
02785       if (**pp != '\0')
02786        ++*pp;
02787 
02788       if (c + 1 >= alloc)
02789        {
02790          alloc += 10;
02791          methods = ((debug_method *)
02792                    xrealloc (methods, alloc * sizeof *methods));
02793        }
02794 
02795       methods[c] = debug_make_method (dhandle, name, variants);
02796 
02797       ++c;
02798     }
02799 
02800   if (methods != NULL)
02801     methods[c] = DEBUG_METHOD_NULL;
02802 
02803   *retp = methods;
02804 
02805   return TRUE;
02806 }
02807 
02808 /* Parse a string representing argument types for a method.  Stabs
02809    tries to save space by packing argument types into a mangled
02810    string.  This string should give us enough information to extract
02811    both argument types and the physical name of the function, given
02812    the tag name.  */
02813 
02814 static debug_type
02815 parse_stab_argtypes (void *dhandle, struct stab_handle *info,
02816                    debug_type class_type, const char *fieldname,
02817                    const char *tagname, debug_type return_type,
02818                    const char *argtypes, bfd_boolean constp,
02819                    bfd_boolean volatilep, const char **pphysname)
02820 {
02821   bfd_boolean is_full_physname_constructor;
02822   bfd_boolean is_constructor;
02823   bfd_boolean is_destructor;
02824   bfd_boolean is_v3;
02825   debug_type *args;
02826   bfd_boolean varargs;
02827   unsigned int physname_len = 0;
02828 
02829   /* Constructors are sometimes handled specially.  */
02830   is_full_physname_constructor = ((argtypes[0] == '_'
02831                                && argtypes[1] == '_'
02832                                && (ISDIGIT (argtypes[2])
02833                                    || argtypes[2] == 'Q'
02834                                    || argtypes[2] == 't'))
02835                               || CONST_STRNEQ (argtypes, "__ct"));
02836 
02837   is_constructor = (is_full_physname_constructor
02838                   || (tagname != NULL
02839                      && strcmp (fieldname, tagname) == 0));
02840   is_destructor = ((argtypes[0] == '_'
02841                   && (argtypes[1] == '$' || argtypes[1] == '.')
02842                   && argtypes[2] == '_')
02843                  || CONST_STRNEQ (argtypes, "__dt"));
02844   is_v3 = argtypes[0] == '_' && argtypes[1] == 'Z';
02845 
02846   if (is_destructor || is_full_physname_constructor || is_v3)
02847     *pphysname = argtypes;
02848   else
02849     {
02850       unsigned int len;
02851       const char *const_prefix;
02852       const char *volatile_prefix;
02853       char buf[20];
02854       unsigned int mangled_name_len;
02855       char *physname;
02856 
02857       len = tagname == NULL ? 0 : strlen (tagname);
02858       const_prefix = constp ? "C" : "";
02859       volatile_prefix = volatilep ? "V" : "";
02860 
02861       if (len == 0)
02862        sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
02863       else if (tagname != NULL && strchr (tagname, '<') != NULL)
02864        {
02865          /* Template methods are fully mangled.  */
02866          sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
02867          tagname = NULL;
02868          len = 0;
02869        }
02870       else
02871        sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
02872 
02873       mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
02874                        + strlen (buf)
02875                        + len
02876                        + strlen (argtypes)
02877                        + 1);
02878 
02879       if (fieldname[0] == 'o'
02880          && fieldname[1] == 'p'
02881          && (fieldname[2] == '$' || fieldname[2] == '.'))
02882        {
02883          const char *opname;
02884 
02885          opname = cplus_mangle_opname (fieldname + 3, 0);
02886          if (opname == NULL)
02887            {
02888              fprintf (stderr, _("No mangling for \"%s\"\n"), fieldname);
02889              return DEBUG_TYPE_NULL;
02890            }
02891          mangled_name_len += strlen (opname);
02892          physname = (char *) xmalloc (mangled_name_len);
02893          strncpy (physname, fieldname, 3);
02894          strcpy (physname + 3, opname);
02895        }
02896       else
02897        {
02898          physname = (char *) xmalloc (mangled_name_len);
02899          if (is_constructor)
02900            physname[0] = '\0';
02901          else
02902            strcpy (physname, fieldname);
02903        }
02904 
02905       physname_len = strlen (physname);
02906       strcat (physname, buf);
02907       if (tagname != NULL)
02908        strcat (physname, tagname);
02909       strcat (physname, argtypes);
02910 
02911       *pphysname = physname;
02912     }
02913 
02914   if (*argtypes == '\0' || is_destructor)
02915     {
02916       args = (debug_type *) xmalloc (sizeof *args);
02917       *args = NULL;
02918       return debug_make_method_type (dhandle, return_type, class_type, args,
02919                                  FALSE);
02920     }
02921 
02922   args = stab_demangle_argtypes (dhandle, info, *pphysname, &varargs, physname_len);
02923   if (args == NULL)
02924     return DEBUG_TYPE_NULL;
02925 
02926   return debug_make_method_type (dhandle, return_type, class_type, args,
02927                              varargs);
02928 }
02929 
02930 /* The tail end of stabs for C++ classes that contain a virtual function
02931    pointer contains a tilde, a %, and a type number.
02932    The type number refers to the base class (possibly this class itself) which
02933    contains the vtable pointer for the current class.
02934 
02935    This function is called when we have parsed all the method declarations,
02936    so we can look for the vptr base class info.  */
02937 
02938 static bfd_boolean
02939 parse_stab_tilde_field (void *dhandle, struct stab_handle *info,
02940                      const char **pp, const int *typenums,
02941                      debug_type *retvptrbase, bfd_boolean *retownvptr)
02942 {
02943   const char *orig;
02944   const char *hold;
02945   int vtypenums[2];
02946 
02947   *retvptrbase = DEBUG_TYPE_NULL;
02948   *retownvptr = FALSE;
02949 
02950   orig = *pp;
02951 
02952   /* If we are positioned at a ';', then skip it.  */
02953   if (**pp == ';')
02954     ++*pp;
02955 
02956   if (**pp != '~')
02957     return TRUE;
02958 
02959   ++*pp;
02960 
02961   if (**pp == '=' || **pp == '+' || **pp == '-')
02962     {
02963       /* Obsolete flags that used to indicate the presence of
02964         constructors and/or destructors.  */
02965       ++*pp;
02966     }
02967 
02968   if (**pp != '%')
02969     return TRUE;
02970 
02971   ++*pp;
02972 
02973   hold = *pp;
02974 
02975   /* The next number is the type number of the base class (possibly
02976      our own class) which supplies the vtable for this class.  */
02977   if (! parse_stab_type_number (pp, vtypenums))
02978     return FALSE;
02979 
02980   if (vtypenums[0] == typenums[0]
02981       && vtypenums[1] == typenums[1])
02982     *retownvptr = TRUE;
02983   else
02984     {
02985       debug_type vtype;
02986       const char *p;
02987 
02988       *pp = hold;
02989 
02990       vtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
02991                             (debug_type **) NULL);
02992       for (p = *pp; *p != ';' && *p != '\0'; p++)
02993        ;
02994       if (*p != ';')
02995        {
02996          bad_stab (orig);
02997          return FALSE;
02998        }
02999 
03000       *retvptrbase = vtype;
03001 
03002       *pp = p + 1;
03003     }
03004 
03005   return TRUE;
03006 }
03007 
03008 /* Read a definition of an array type.  */
03009 
03010 static debug_type
03011 parse_stab_array_type (void *dhandle, struct stab_handle *info,
03012                      const char **pp, bfd_boolean stringp)
03013 {
03014   const char *orig;
03015   const char *p;
03016   int typenums[2];
03017   debug_type index_type;
03018   bfd_boolean adjustable;
03019   bfd_signed_vma lower, upper;
03020   debug_type element_type;
03021 
03022   /* Format of an array type:
03023      "ar<index type>;lower;upper;<array_contents_type>".
03024      OS9000: "arlower,upper;<array_contents_type>".
03025 
03026      Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
03027      for these, produce a type like float[][].  */
03028 
03029   orig = *pp;
03030 
03031   /* FIXME: gdb checks os9k_stabs here.  */
03032 
03033   /* If the index type is type 0, we take it as int.  */
03034   p = *pp;
03035   if (! parse_stab_type_number (&p, typenums))
03036     return DEBUG_TYPE_NULL;
03037   if (typenums[0] == 0 && typenums[1] == 0 && **pp != '=')
03038     {
03039       index_type = debug_find_named_type (dhandle, "int");
03040       if (index_type == DEBUG_TYPE_NULL)
03041        {
03042          index_type = debug_make_int_type (dhandle, 4, FALSE);
03043          if (index_type == DEBUG_TYPE_NULL)
03044            return DEBUG_TYPE_NULL;
03045        }
03046       *pp = p;
03047     }
03048   else
03049     {
03050       index_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
03051                                 (debug_type **) NULL);
03052     }
03053 
03054   if (**pp != ';')
03055     {
03056       bad_stab (orig);
03057       return DEBUG_TYPE_NULL;
03058     }
03059   ++*pp;
03060 
03061   adjustable = FALSE;
03062 
03063   if (! ISDIGIT (**pp) && **pp != '-')
03064     {
03065       ++*pp;
03066       adjustable = TRUE;
03067     }
03068 
03069   lower = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL);
03070   if (**pp != ';')
03071     {
03072       bad_stab (orig);
03073       return DEBUG_TYPE_NULL;
03074     }
03075   ++*pp;
03076 
03077   if (! ISDIGIT (**pp) && **pp != '-')
03078     {
03079       ++*pp;
03080       adjustable = TRUE;
03081     }
03082 
03083   upper = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL);
03084   if (**pp != ';')
03085     {
03086       bad_stab (orig);
03087       return DEBUG_TYPE_NULL;
03088     }
03089   ++*pp;
03090 
03091   element_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
03092                               (debug_type **) NULL);
03093   if (element_type == DEBUG_TYPE_NULL)
03094     return DEBUG_TYPE_NULL;
03095 
03096   if (adjustable)
03097     {
03098       lower = 0;
03099       upper = -1;
03100     }
03101 
03102   return debug_make_array_type (dhandle, element_type, index_type, lower,
03103                             upper, stringp);
03104 }
03105 
03106 /* This struct holds information about files we have seen using
03107    N_BINCL.  */
03108 
03109 struct bincl_file
03110 {
03111   /* The next N_BINCL file.  */
03112   struct bincl_file *next;
03113   /* The next N_BINCL on the stack.  */
03114   struct bincl_file *next_stack;
03115   /* The file name.  */
03116   const char *name;
03117   /* The hash value.  */
03118   bfd_vma hash;
03119   /* The file index.  */
03120   unsigned int file;
03121   /* The list of types defined in this file.  */
03122   struct stab_types *file_types;
03123 };
03124 
03125 /* Start a new N_BINCL file, pushing it onto the stack.  */
03126 
03127 static void
03128 push_bincl (struct stab_handle *info, const char *name, bfd_vma hash)
03129 {
03130   struct bincl_file *n;
03131 
03132   n = (struct bincl_file *) xmalloc (sizeof *n);
03133   n->next = info->bincl_list;
03134   n->next_stack = info->bincl_stack;
03135   n->name = name;
03136   n->hash = hash;
03137   n->file = info->files;
03138   n->file_types = NULL;
03139   info->bincl_list = n;
03140   info->bincl_stack = n;
03141 
03142   ++info->files;
03143   info->file_types = ((struct stab_types **)
03144                     xrealloc (info->file_types,
03145                             (info->files
03146                              * sizeof *info->file_types)));
03147   info->file_types[n->file] = NULL;
03148 }
03149 
03150 /* Finish an N_BINCL file, at an N_EINCL, popping the name off the
03151    stack.  */
03152 
03153 static const char *
03154 pop_bincl (struct stab_handle *info)
03155 {
03156   struct bincl_file *o;
03157 
03158   o = info->bincl_stack;
03159   if (o == NULL)
03160     return info->main_filename;
03161   info->bincl_stack = o->next_stack;
03162 
03163   o->file_types = info->file_types[o->file];
03164 
03165   if (info->bincl_stack == NULL)
03166     return info->main_filename;
03167   return info->bincl_stack->name;
03168 }
03169 
03170 /* Handle an N_EXCL: get the types from the corresponding N_BINCL.  */
03171 
03172 static bfd_boolean
03173 find_excl (struct stab_handle *info, const char *name, bfd_vma hash)
03174 {
03175   struct bincl_file *l;
03176 
03177   ++info->files;
03178   info->file_types = ((struct stab_types **)
03179                     xrealloc (info->file_types,
03180                             (info->files
03181                              * sizeof *info->file_types)));
03182 
03183   for (l = info->bincl_list; l != NULL; l = l->next)
03184     if (l->hash == hash && strcmp (l->name, name) == 0)
03185       break;
03186   if (l == NULL)
03187     {
03188       warn_stab (name, _("Undefined N_EXCL"));
03189       info->file_types[info->files - 1] = NULL;
03190       return TRUE;
03191     }
03192 
03193   info->file_types[info->files - 1] = l->file_types;
03194 
03195   return TRUE;
03196 }
03197 
03198 /* Handle a variable definition.  gcc emits variable definitions for a
03199    block before the N_LBRAC, so we must hold onto them until we see
03200    it.  The SunPRO compiler emits variable definitions after the
03201    N_LBRAC, so we can call debug_record_variable immediately.  */
03202 
03203 static bfd_boolean
03204 stab_record_variable (void *dhandle, struct stab_handle *info,
03205                     const char *name, debug_type type,
03206                     enum debug_var_kind kind, bfd_vma val)
03207 {
03208   struct stab_pending_var *v;
03209 
03210   if ((kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
03211       || ! info->within_function
03212       || (info->gcc_compiled == 0 && info->n_opt_found))
03213     return debug_record_variable (dhandle, name, type, kind, val);
03214 
03215   v = (struct stab_pending_var *) xmalloc (sizeof *v);
03216   memset (v, 0, sizeof *v);
03217 
03218   v->next = info->pending;
03219   v->name = name;
03220   v->type = type;
03221   v->kind = kind;
03222   v->val = val;
03223   info->pending = v;
03224 
03225   return TRUE;
03226 }
03227 
03228 /* Emit pending variable definitions.  This is called after we see the
03229    N_LBRAC that starts the block.  */
03230 
03231 static bfd_boolean
03232 stab_emit_pending_vars (void *dhandle, struct stab_handle *info)
03233 {
03234   struct stab_pending_var *v;
03235 
03236   v = info->pending;
03237   while (v != NULL)
03238     {
03239       struct stab_pending_var *next;
03240 
03241       if (! debug_record_variable (dhandle, v->name, v->type, v->kind, v->val))
03242        return FALSE;
03243 
03244       next = v->next;
03245       free (v);
03246       v = next;
03247     }
03248 
03249   info->pending = NULL;
03250 
03251   return TRUE;
03252 }
03253 
03254 /* Find the slot for a type in the database.  */
03255 
03256 static debug_type *
03257 stab_find_slot (struct stab_handle *info, const int *typenums)
03258 {
03259   int filenum;
03260   int index;
03261   struct stab_types **ps;
03262 
03263   filenum = typenums[0];
03264   index = typenums[1];
03265 
03266   if (filenum < 0 || (unsigned int) filenum >= info->files)
03267     {
03268       fprintf (stderr, _("Type file number %d out of range\n"), filenum);
03269       return NULL;
03270     }
03271   if (index < 0)
03272     {
03273       fprintf (stderr, _("Type index number %d out of range\n"), index);
03274       return NULL;
03275     }
03276 
03277   ps = info->file_types + filenum;
03278 
03279   while (index >= STAB_TYPES_SLOTS)
03280     {
03281       if (*ps == NULL)
03282        {
03283          *ps = (struct stab_types *) xmalloc (sizeof **ps);
03284          memset (*ps, 0, sizeof **ps);
03285        }
03286       ps = &(*ps)->next;
03287       index -= STAB_TYPES_SLOTS;
03288     }
03289   if (*ps == NULL)
03290     {
03291       *ps = (struct stab_types *) xmalloc (sizeof **ps);
03292       memset (*ps, 0, sizeof **ps);
03293     }
03294 
03295   return (*ps)->types + index;
03296 }
03297 
03298 /* Find a type given a type number.  If the type has not been
03299    allocated yet, create an indirect type.  */
03300 
03301 static debug_type
03302 stab_find_type (void *dhandle, struct stab_handle *info, const int *typenums)
03303 {
03304   debug_type *slot;
03305 
03306   if (typenums[0] == 0 && typenums[1] < 0)
03307     {
03308       /* A negative type number indicates an XCOFF builtin type.  */
03309       return stab_xcoff_builtin_type (dhandle, info, typenums[1]);
03310     }
03311 
03312   slot = stab_find_slot (info, typenums);
03313   if (slot == NULL)
03314     return DEBUG_TYPE_NULL;
03315 
03316   if (*slot == DEBUG_TYPE_NULL)
03317     return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
03318 
03319   return *slot;
03320 }
03321 
03322 /* Record that a given type number refers to a given type.  */
03323 
03324 static bfd_boolean
03325 stab_record_type (void *dhandle ATTRIBUTE_UNUSED, struct stab_handle *info,
03326                 const int *typenums, debug_type type)
03327 {
03328   debug_type *slot;
03329 
03330   slot = stab_find_slot (info, typenums);
03331   if (slot == NULL)
03332     return FALSE;
03333 
03334   /* gdb appears to ignore type redefinitions, so we do as well.  */
03335 
03336   *slot = type;
03337 
03338   return TRUE;
03339 }
03340 
03341 /* Return an XCOFF builtin type.  */
03342 
03343 static debug_type
03344 stab_xcoff_builtin_type (void *dhandle, struct stab_handle *info,
03345                       int typenum)
03346 {
03347   debug_type rettype;
03348   const char *name;
03349 
03350   if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
03351     {
03352       fprintf (stderr, _("Unrecognized XCOFF type %d\n"), typenum);
03353       return DEBUG_TYPE_NULL;
03354     }
03355   if (info->xcoff_types[-typenum] != NULL)
03356     return info->xcoff_types[-typenum];
03357 
03358   switch (-typenum)
03359     {
03360     case 1:
03361       /* The size of this and all the other types are fixed, defined
03362         by the debugging format.  */
03363       name = "int";
03364       rettype = debug_make_int_type (dhandle, 4, FALSE);
03365       break;
03366     case 2:
03367       name = "char";
03368       rettype = debug_make_int_type (dhandle, 1, FALSE);
03369       break;
03370     case 3:
03371       name = "short";
03372       rettype = debug_make_int_type (dhandle, 2, FALSE);
03373       break;
03374     case 4:
03375       name = "long";
03376       rettype = debug_make_int_type (dhandle, 4, FALSE);
03377       break;
03378     case 5:
03379       name = "unsigned char";
03380       rettype = debug_make_int_type (dhandle, 1, TRUE);
03381       break;
03382     case 6:
03383       name = "signed char";
03384       rettype = debug_make_int_type (dhandle, 1, FALSE);
03385       break;
03386     case 7:
03387       name = "unsigned short";
03388       rettype = debug_make_int_type (dhandle, 2, TRUE);
03389       break;
03390     case 8:
03391       name = "unsigned int";
03392       rettype = debug_make_int_type (dhandle, 4, TRUE);
03393       break;
03394     case 9:
03395       name = "unsigned";
03396       rettype = debug_make_int_type (dhandle, 4, TRUE);
03397     case 10:
03398       name = "unsigned long";
03399       rettype = debug_make_int_type (dhandle, 4, TRUE);
03400       break;
03401     case 11:
03402       name = "void";
03403       rettype = debug_make_void_type (dhandle);
03404       break;
03405     case 12:
03406       /* IEEE single precision (32 bit).  */
03407       name = "float";
03408       rettype = debug_make_float_type (dhandle, 4);
03409       break;
03410     case 13:
03411       /* IEEE double precision (64 bit).  */
03412       name = "double";
03413       rettype = debug_make_float_type (dhandle, 8);
03414       break;
03415     case 14:
03416       /* This is an IEEE double on the RS/6000, and different machines
03417         with different sizes for "long double" should use different
03418         negative type numbers.  See stabs.texinfo.  */
03419       name = "long double";
03420       rettype = debug_make_float_type (dhandle, 8);
03421       break;
03422     case 15:
03423       name = "integer";
03424       rettype = debug_make_int_type (dhandle, 4, FALSE);
03425       break;
03426     case 16:
03427       name = "boolean";
03428       rettype = debug_make_bool_type (dhandle, 4);
03429       break;
03430     case 17:
03431       name = "short real";
03432       rettype = debug_make_float_type (dhandle, 4);
03433       break;
03434     case 18:
03435       name = "real";
03436       rettype = debug_make_float_type (dhandle, 8);
03437       break;
03438     case 19:
03439       /* FIXME */
03440       name = "stringptr";
03441       rettype = NULL;
03442       break;
03443     case 20:
03444       /* FIXME */
03445       name = "character";
03446       rettype = debug_make_int_type (dhandle, 1, TRUE);
03447       break;
03448     case 21:
03449       name = "logical*1";
03450       rettype = debug_make_bool_type (dhandle, 1);
03451       break;
03452     case 22:
03453       name = "logical*2";
03454       rettype = debug_make_bool_type (dhandle, 2);
03455       break;
03456     case 23:
03457       name = "logical*4";
03458       rettype = debug_make_bool_type (dhandle, 4);
03459       break;
03460     case 24:
03461       name = "logical";
03462       rettype = debug_make_bool_type (dhandle, 4);
03463       break;
03464     case 25:
03465       /* Complex type consisting of two IEEE single precision values.  */
03466       name = "complex";
03467       rettype = debug_make_complex_type (dhandle, 8);
03468       break;
03469     case 26:
03470       /* Complex type consisting of two IEEE double precision values.  */
03471       name = "double complex";
03472       rettype = debug_make_complex_type (dhandle, 16);
03473       break;
03474     case 27:
03475       name = "integer*1";
03476       rettype = debug_make_int_type (dhandle, 1, FALSE);
03477       break;
03478     case 28:
03479       name = "integer*2";
03480       rettype = debug_make_int_type (dhandle, 2, FALSE);
03481       break;
03482     case 29:
03483       name = "integer*4";
03484       rettype = debug_make_int_type (dhandle, 4, FALSE);
03485       break;
03486     case 30:
03487       /* FIXME */
03488       name = "wchar";
03489       rettype = debug_make_int_type (dhandle, 2, FALSE);
03490       break;
03491     case 31:
03492       name = "long long";
03493       rettype = debug_make_int_type (dhandle, 8, FALSE);
03494       break;
03495     case 32:
03496       name = "unsigned long long";
03497       rettype = debug_make_int_type (dhandle, 8, TRUE);
03498       break;
03499     case 33:
03500       name = "logical*8";
03501       rettype = debug_make_bool_type (dhandle, 8);
03502       break;
03503     case 34:
03504       name = "integer*8";
03505       rettype = debug_make_int_type (dhandle, 8, FALSE);
03506       break;
03507     default:
03508       abort ();
03509     }
03510 
03511   rettype = debug_name_type (dhandle, name, rettype);
03512 
03513   info->xcoff_types[-typenum] = rettype;
03514 
03515   return rettype;
03516 }
03517 
03518 /* Find or create a tagged type.  */
03519 
03520 static debug_type
03521 stab_find_tagged_type (void *dhandle, struct stab_handle *info,
03522                      const char *p, int len, enum debug_type_kind kind)
03523 {
03524   char *name;
03525   debug_type dtype;
03526   struct stab_tag *st;
03527 
03528   name = savestring (p, len);
03529 
03530   /* We pass DEBUG_KIND_ILLEGAL because we want all tags in the same
03531      namespace.  This is right for C, and I don't know how to handle
03532      other languages.  FIXME.  */
03533   dtype = debug_find_tagged_type (dhandle, name, DEBUG_KIND_ILLEGAL);
03534   if (dtype != DEBUG_TYPE_NULL)
03535     {
03536       free (name);
03537       return dtype;
03538     }
03539 
03540   /* We need to allocate an entry on the undefined tag list.  */
03541   for (st = info->tags; st != NULL; st = st->next)
03542     {
03543       if (st->name[0] == name[0]
03544          && strcmp (st->name, name) == 0)
03545        {
03546          if (st->kind == DEBUG_KIND_ILLEGAL)
03547            st->kind = kind;
03548          free (name);
03549          break;
03550        }
03551     }
03552   if (st == NULL)
03553     {
03554       st = (struct stab_tag *) xmalloc (sizeof *st);
03555       memset (st, 0, sizeof *st);
03556 
03557       st->next = info->tags;
03558       st->name = name;
03559       st->kind = kind;
03560       st->slot = DEBUG_TYPE_NULL;
03561       st->type = debug_make_indirect_type (dhandle, &st->slot, name);
03562       info->tags = st;
03563     }
03564 
03565   return st->type;
03566 }
03567 
03568 /* In order to get the correct argument types for a stubbed method, we
03569    need to extract the argument types from a C++ mangled string.
03570    Since the argument types can refer back to the return type, this
03571    means that we must demangle the entire physical name.  In gdb this
03572    is done by calling cplus_demangle and running the results back
03573    through the C++ expression parser.  Since we have no expression
03574    parser, we must duplicate much of the work of cplus_demangle here.
03575 
03576    We assume that GNU style demangling is used, since this is only
03577    done for method stubs, and only g++ should output that form of
03578    debugging information.  */
03579 
03580 /* This structure is used to hold a pointer to type information which
03581    demangling a string.  */
03582 
03583 struct stab_demangle_typestring
03584 {
03585   /* The start of the type.  This is not null terminated.  */
03586   const char *typestring;
03587   /* The length of the type.  */
03588   unsigned int len;
03589 };
03590 
03591 /* This structure is used to hold information while demangling a
03592    string.  */
03593 
03594 struct stab_demangle_info
03595 {
03596   /* The debugging information handle.  */
03597   void *dhandle;
03598   /* The stab information handle.  */
03599   struct stab_handle *info;
03600   /* The array of arguments we are building.  */
03601   debug_type *args;
03602   /* Whether the method takes a variable number of arguments.  */
03603   bfd_boolean varargs;
03604   /* The array of types we have remembered.  */
03605   struct stab_demangle_typestring *typestrings;
03606   /* The number of typestrings.  */
03607   unsigned int typestring_count;
03608   /* The number of typestring slots we have allocated.  */
03609   unsigned int typestring_alloc;
03610 };
03611 
03612 static void stab_bad_demangle (const char *);
03613 static unsigned int stab_demangle_count (const char **);
03614 static bfd_boolean stab_demangle_get_count (const char **, unsigned int *);
03615 static bfd_boolean stab_demangle_prefix
03616   (struct stab_demangle_info *, const char **, unsigned int);
03617 static bfd_boolean stab_demangle_function_name
03618   (struct stab_demangle_info *, const char **, const char *);
03619 static bfd_boolean stab_demangle_signature
03620   (struct stab_demangle_info *, const char **);
03621 static bfd_boolean stab_demangle_qualified
03622   (struct stab_demangle_info *, const char **, debug_type *);
03623 static bfd_boolean stab_demangle_template
03624   (struct stab_demangle_info *, const char **, char **);
03625 static bfd_boolean stab_demangle_class
03626   (struct stab_demangle_info *, const char **, const char **);
03627 static bfd_boolean stab_demangle_args
03628   (struct stab_demangle_info *, const char **, debug_type **, bfd_boolean *);
03629 static bfd_boolean stab_demangle_arg
03630   (struct stab_demangle_info *, const char **, debug_type **,
03631    unsigned int *, unsigned int *);
03632 static bfd_boolean stab_demangle_type
03633   (struct stab_demangle_info *, const char **, debug_type *);
03634 static bfd_boolean stab_demangle_fund_type
03635   (struct stab_demangle_info *, const char **, debug_type *);
03636 static bfd_boolean stab_demangle_remember_type
03637   (struct stab_demangle_info *, const char *, int);
03638 
03639 /* Warn about a bad demangling.  */
03640 
03641 static void
03642 stab_bad_demangle (const char *s)
03643 {
03644   fprintf (stderr, _("bad mangled name `%s'\n"), s);
03645 }
03646 
03647 /* Get a count from a stab string.  */
03648 
03649 static unsigned int
03650 stab_demangle_count (const char **pp)
03651 {
03652   unsigned int count;
03653 
03654   count = 0;
03655   while (ISDIGIT (**pp))
03656     {
03657       count *= 10;
03658       count += **pp - '0';
03659       ++*pp;
03660     }
03661   return count;
03662 }
03663 
03664 /* Require a count in a string.  The count may be multiple digits, in
03665    which case it must end in an underscore.  */
03666 
03667 static bfd_boolean
03668 stab_demangle_get_count (const char **pp, unsigned int *pi)
03669 {
03670   if (! ISDIGIT (**pp))
03671     return FALSE;
03672 
03673   *pi = **pp - '0';
03674   ++*pp;
03675   if (ISDIGIT (**pp))
03676     {
03677       unsigned int count;
03678       const char *p;
03679 
03680       count = *pi;
03681       p = *pp;
03682       do
03683        {
03684          count *= 10;
03685          count += *p - '0';
03686          ++p;
03687        }
03688       while (ISDIGIT (*p));
03689       if (*p == '_')
03690        {
03691          *pp = p + 1;
03692          *pi = count;
03693        }
03694     }
03695 
03696   return TRUE;
03697 }
03698 
03699 /* This function demangles a physical name, returning a NULL
03700    terminated array of argument types.  */
03701 
03702 static debug_type *
03703 stab_demangle_argtypes (void *dhandle, struct stab_handle *info,
03704                      const char *physname, bfd_boolean *pvarargs,
03705                      unsigned int physname_len)
03706 {
03707   struct stab_demangle_info minfo;
03708 
03709   /* Check for the g++ V3 ABI.  */
03710   if (physname[0] == '_' && physname[1] == 'Z')
03711     return stab_demangle_v3_argtypes (dhandle, info, physname, pvarargs);
03712 
03713   minfo.dhandle = dhandle;
03714   minfo.info = info;
03715   minfo.args = NULL;
03716   minfo.varargs = FALSE;
03717   minfo.typestring_alloc = 10;
03718   minfo.typestrings = ((struct stab_demangle_typestring *)
03719                      xmalloc (minfo.typestring_alloc
03720                             * sizeof *minfo.typestrings));
03721   minfo.typestring_count = 0;
03722 
03723   /* cplus_demangle checks for special GNU mangled forms, but we can't
03724      see any of them in mangled method argument types.  */
03725 
03726   if (! stab_demangle_prefix (&minfo, &physname, physname_len))
03727     goto error_return;
03728 
03729   if (*physname != '\0')
03730     {
03731       if (! stab_demangle_signature (&minfo, &physname))
03732        goto error_return;
03733     }
03734 
03735   free (minfo.typestrings);
03736   minfo.typestrings = NULL;
03737 
03738   if (minfo.args == NULL)
03739     fprintf (stderr, _("no argument types in mangled string\n"));
03740 
03741   *pvarargs = minfo.varargs;
03742   return minfo.args;
03743 
03744  error_return:
03745   if (minfo.typestrings != NULL)
03746     free (minfo.typestrings);
03747   return NULL;
03748 }
03749 
03750 /* Demangle the prefix of the mangled name.  */
03751 
03752 static bfd_boolean
03753 stab_demangle_prefix (struct stab_demangle_info *minfo, const char **pp,
03754                     unsigned int physname_len)
03755 {
03756   const char *scan;
03757   unsigned int i;
03758 
03759   /* cplus_demangle checks for global constructors and destructors,
03760      but we can't see them in mangled argument types.  */
03761 
03762   if (physname_len)
03763     scan = *pp + physname_len;
03764   else
03765     {
03766       /* Look for `__'.  */
03767       scan = *pp;
03768       do
03769        scan = strchr (scan, '_');
03770       while (scan != NULL && *++scan != '_');
03771 
03772       if (scan == NULL)
03773        {
03774          stab_bad_demangle (*pp);
03775          return FALSE;
03776        }
03777 
03778       --scan;
03779 
03780       /* We found `__'; move ahead to the last contiguous `__' pair.  */
03781       i = strspn (scan, "_");
03782       if (i > 2)
03783        scan += i - 2;
03784     }
03785 
03786   if (scan == *pp
03787       && (ISDIGIT (scan[2])
03788          || scan[2] == 'Q'
03789          || scan[2] == 't'))
03790     {
03791       /* This is a GNU style constructor name.  */
03792       *pp = scan + 2;
03793       return TRUE;
03794     }
03795   else if (scan == *pp
03796           && ! ISDIGIT (scan[2])
03797           && scan[2] != 't')
03798     {
03799       /* Look for the `__' that separates the prefix from the
03800          signature.  */
03801       while (*scan == '_')
03802        ++scan;
03803       scan = strstr (scan, "__");
03804       if (scan == NULL || scan[2] == '\0')
03805        {
03806          stab_bad_demangle (*pp);
03807          return FALSE;
03808        }
03809 
03810       return stab_demangle_function_name (minfo, pp, scan);
03811     }
03812   else if (scan[2] != '\0')
03813     {
03814       /* The name doesn't start with `__', but it does contain `__'.  */
03815       return stab_demangle_function_name (minfo, pp, scan);
03816     }
03817   else
03818     {
03819       stab_bad_demangle (*pp);
03820       return FALSE;
03821     }
03822   /*NOTREACHED*/
03823 }
03824 
03825 /* Demangle a function name prefix.  The scan argument points to the
03826    double underscore which separates the function name from the
03827    signature.  */
03828 
03829 static bfd_boolean
03830 stab_demangle_function_name (struct stab_demangle_info *minfo,
03831                           const char **pp, const char *scan)
03832 {
03833   const char *name;
03834 
03835   /* The string from *pp to scan is the name of the function.  We
03836      don't care about the name, since we just looking for argument
03837      types.  However, for conversion operators, the name may include a
03838      type which we must remember in order to handle backreferences.  */
03839 
03840   name = *pp;
03841   *pp = scan + 2;
03842 
03843   if (*pp - name >= 5
03844           && CONST_STRNEQ (name, "type")
03845           && (name[4] == '$' || name[4] == '.'))
03846     {
03847       const char *tem;
03848 
03849       /* This is a type conversion operator.  */
03850       tem = name + 5;
03851       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
03852        return FALSE;
03853     }
03854   else if (name[0] == '_'
03855           && name[1] == '_'
03856           && name[2] == 'o'
03857           && name[3] == 'p')
03858     {
03859       const char *tem;
03860 
03861       /* This is a type conversion operator.  */
03862       tem = name + 4;
03863       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
03864        return FALSE;
03865     }
03866 
03867   return TRUE;
03868 }
03869 
03870 /* Demangle the signature.  This is where the argument types are
03871    found.  */
03872 
03873 static bfd_boolean
03874 stab_demangle_signature (struct stab_demangle_info *minfo, const char **pp)
03875 {
03876   const char *orig;
03877   bfd_boolean expect_func, func_done;
03878   const char *hold;
03879 
03880   orig = *pp;
03881 
03882   expect_func = FALSE;
03883   func_done = FALSE;
03884   hold = NULL;
03885 
03886   while (**pp != '\0')
03887     {
03888       switch (**pp)
03889        {
03890        case 'Q':
03891          hold = *pp;
03892          if (! stab_demangle_qualified (minfo, pp, (debug_type *) NULL)
03893              || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
03894            return FALSE;
03895          expect_func = TRUE;
03896          hold = NULL;
03897          break;
03898 
03899        case 'S':
03900          /* Static member function.  FIXME: Can this happen?  */
03901          if (hold == NULL)
03902            hold = *pp;
03903          ++*pp;
03904          break;
03905 
03906        case 'C':
03907          /* Const member function.  */
03908          if (hold == NULL)
03909            hold = *pp;
03910          ++*pp;
03911          break;
03912 
03913        case '0': case '1': case '2': case '3': case '4':
03914        case '5': case '6': case '7': case '8': case '9':
03915          if (hold == NULL)
03916            hold = *pp;
03917          if (! stab_demangle_class (minfo, pp, (const char **) NULL)
03918              || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
03919            return FALSE;
03920          expect_func = TRUE;
03921          hold = NULL;
03922          break;
03923 
03924        case 'F':
03925          /* Function.  I don't know if this actually happens with g++
03926              output.  */
03927          hold = NULL;
03928          func_done = TRUE;
03929          ++*pp;
03930          if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
03931            return FALSE;
03932          break;
03933 
03934        case 't':
03935          /* Template.  */
03936          if (hold == NULL)
03937            hold = *pp;
03938          if (! stab_demangle_template (minfo, pp, (char **) NULL)
03939              || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
03940            return FALSE;
03941          hold = NULL;
03942          expect_func = TRUE;
03943          break;
03944 
03945        case '_':
03946          /* At the outermost level, we cannot have a return type
03947             specified, so if we run into another '_' at this point we
03948             are dealing with a mangled name that is either bogus, or
03949             has been mangled by some algorithm we don't know how to
03950             deal with.  So just reject the entire demangling.  */
03951          stab_bad_demangle (orig);
03952          return FALSE;
03953 
03954        default:
03955          /* Assume we have stumbled onto the first outermost function
03956             argument token, and start processing args.  */
03957          func_done = TRUE;
03958          if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
03959            return FALSE;
03960          break;
03961        }
03962 
03963       if (expect_func)
03964        {
03965          func_done = TRUE;
03966          if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
03967            return FALSE;
03968        }
03969     }
03970 
03971   if (! func_done)
03972     {
03973       /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
03974         bar__3fooi is 'foo::bar(int)'.  We get here when we find the
03975         first case, and need to ensure that the '(void)' gets added
03976         to the current declp.  */
03977       if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
03978        return FALSE;
03979     }
03980 
03981   return TRUE;
03982 }
03983 
03984 /* Demangle a qualified name, such as "Q25Outer5Inner" which is the
03985    mangled form of "Outer::Inner".  */
03986 
03987 static bfd_boolean
03988 stab_demangle_qualified (struct stab_demangle_info *minfo, const char **pp,
03989                       debug_type *ptype)
03990 {
03991   const char *orig;
03992   const char *p;
03993   unsigned int qualifiers;
03994   debug_type context;
03995 
03996   orig = *pp;
03997 
03998   switch ((*pp)[1])
03999     {
04000     case '_':
04001       /* GNU mangled name with more than 9 classes.  The count is
04002         preceded by an underscore (to distinguish it from the <= 9
04003         case) and followed by an underscore.  */
04004       p = *pp + 2;
04005       if (! ISDIGIT (*p) || *p == '0')
04006        {
04007          stab_bad_demangle (orig);
04008          return FALSE;
04009        }
04010       qualifiers = atoi (p);
04011       while (ISDIGIT (*p))
04012        ++p;
04013       if (*p != '_')
04014        {
04015          stab_bad_demangle (orig);
04016          return FALSE;
04017        }
04018       *pp = p + 1;
04019       break;
04020 
04021     case '1': case '2': case '3': case '4': case '5':
04022     case '6': case '7': case '8': case '9':
04023       qualifiers = (*pp)[1] - '0';
04024       /* Skip an optional underscore after the count.  */
04025       if ((*pp)[2] == '_')
04026        ++*pp;
04027       *pp += 2;
04028       break;
04029 
04030     case '0':
04031     default:
04032       stab_bad_demangle (orig);
04033       return FALSE;
04034     }
04035 
04036   context = DEBUG_TYPE_NULL;
04037 
04038   /* Pick off the names.  */
04039   while (qualifiers-- > 0)
04040     {
04041       if (**pp == '_')
04042        ++*pp;
04043       if (**pp == 't')
04044        {
04045          char *name;
04046 
04047          if (! stab_demangle_template (minfo, pp,
04048                                    ptype != NULL ? &name : NULL))
04049            return FALSE;
04050 
04051          if (ptype != NULL)
04052            {
04053              context = stab_find_tagged_type (minfo->dhandle, minfo->info,
04054                                           name, strlen (name),
04055                                           DEBUG_KIND_CLASS);
04056              free (name);
04057              if (context == DEBUG_TYPE_NULL)
04058               return FALSE;
04059            }
04060        }
04061       else
04062        {
04063          unsigned int len;
04064 
04065          len = stab_demangle_count (pp);
04066          if (strlen (*pp) < len)
04067            {
04068              stab_bad_demangle (orig);
04069              return FALSE;
04070            }
04071 
04072          if (ptype != NULL)
04073            {
04074              const debug_field *fields;
04075 
04076              fields = NULL;
04077              if (context != DEBUG_TYPE_NULL)
04078               fields = debug_get_fields (minfo->dhandle, context);
04079 
04080              context = DEBUG_TYPE_NULL;
04081 
04082              if (fields != NULL)
04083               {
04084                 char *name;
04085 
04086                 /* Try to find the type by looking through the
04087                      fields of context until we find a field with the
04088                      same type.  This ought to work for a class
04089                      defined within a class, but it won't work for,
04090                      e.g., an enum defined within a class.  stabs does
04091                      not give us enough information to figure out the
04092                      latter case.  */
04093 
04094                 name = savestring (*pp, len);
04095 
04096                 for (; *fields != DEBUG_FIELD_NULL; fields++)
04097                   {
04098                     debug_type ft;
04099                     const char *dn;
04100 
04101                     ft = debug_get_field_type (minfo->dhandle, *fields);
04102                     if (ft == NULL)
04103                      return FALSE;
04104                     dn = debug_get_type_name (minfo->dhandle, ft);
04105                     if (dn != NULL && strcmp (dn, name) == 0)
04106                      {
04107                        context = ft;
04108                        break;
04109                      }
04110                   }
04111 
04112                 free (name);
04113               }
04114 
04115              if (context == DEBUG_TYPE_NULL)
04116               {
04117                 /* We have to fall back on finding the type by name.
04118                      If there are more types to come, then this must
04119                      be a class.  Otherwise, it could be anything.  */
04120 
04121                 if (qualifiers == 0)
04122                   {
04123                     char *name;
04124 
04125                     name = savestring (*pp, len);
04126                     context = debug_find_named_type (minfo->dhandle,
04127                                                  name);
04128                     free (name);
04129                   }
04130 
04131                 if (context == DEBUG_TYPE_NULL)
04132                   {
04133                     context = stab_find_tagged_type (minfo->dhandle,
04134                                                  minfo->info,
04135                                                  *pp, len,
04136                                                  (qualifiers == 0
04137                                                  ? DEBUG_KIND_ILLEGAL
04138                                                  : DEBUG_KIND_CLASS));
04139                     if (context == DEBUG_TYPE_NULL)
04140                      return FALSE;
04141                   }
04142               }
04143            }
04144 
04145          *pp += len;
04146        }
04147     }
04148 
04149   if (ptype != NULL)
04150     *ptype = context;
04151 
04152   return TRUE;
04153 }
04154 
04155 /* Demangle a template.  If PNAME is not NULL, this sets *PNAME to a
04156    string representation of the template.  */
04157 
04158 static bfd_boolean
04159 stab_demangle_template (struct stab_demangle_info *minfo, const char **pp,
04160                      char **pname)
04161 {
04162   const char *orig;
04163   unsigned int r, i;
04164 
04165   orig = *pp;
04166 
04167   ++*pp;
04168 
04169   /* Skip the template name.  */
04170   r = stab_demangle_count (pp);
04171   if (r == 0 || strlen (*pp) < r)
04172     {
04173       stab_bad_demangle (orig);
04174       return FALSE;
04175     }
04176   *pp += r;
04177 
04178   /* Get the size of the parameter list.  */
04179   if (stab_demangle_get_count (pp, &r) == 0)
04180     {
04181       stab_bad_demangle (orig);
04182       return FALSE;
04183     }
04184 
04185   for (i = 0; i < r; i++)
04186     {
04187       if (**pp == 'Z')
04188        {
04189          /* This is a type parameter.  */
04190          ++*pp;
04191          if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
04192            return FALSE;
04193        }
04194       else
04195        {
04196          const char *old_p;
04197          bfd_boolean pointerp, realp, integralp, charp, boolp;
04198          bfd_boolean done;
04199 
04200          old_p = *pp;
04201          pointerp = FALSE;
04202          realp = FALSE;
04203          integralp = FALSE;
04204          charp = FALSE;
04205          boolp = FALSE;
04206          done = FALSE;
04207 
04208          /* This is a value parameter.  */
04209 
04210          if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
04211            return FALSE;
04212 
04213          while (*old_p != '\0' && ! done)
04214            {
04215              switch (*old_p)
04216               {
04217               case 'P':
04218               case 'p':
04219               case 'R':
04220                 pointerp = TRUE;
04221                 done = TRUE;
04222                 break;
04223               case 'C':     /* Const.  */
04224               case 'S':     /* Signed.  */
04225               case 'U':     /* Unsigned.  */
04226               case 'V':     /* Volatile.  */
04227               case 'F':     /* Function.  */
04228               case 'M':     /* Member function.  */
04229               case 'O':     /* ??? */
04230                 ++old_p;
04231                 break;
04232               case 'Q':     /* Qualified name.  */
04233                 integralp = TRUE;
04234                 done = TRUE;
04235                 break;
04236               case 'T':     /* Remembered type.  */
04237                 abort ();
04238               case 'v':     /* Void.  */
04239                 abort ();
04240               case 'x':     /* Long long.  */
04241               case 'l':     /* Long.  */
04242               case 'i':     /* Int.  */
04243               case 's':     /* Short.  */
04244               case 'w':     /* Wchar_t.  */
04245                 integralp = TRUE;
04246                 done = TRUE;
04247                 break;
04248               case 'b':     /* Bool.  */
04249                 boolp = TRUE;
04250                 done = TRUE;
04251                 break;
04252               case 'c':     /* Char.  */
04253                 charp = TRUE;
04254                 done = TRUE;
04255                 break;
04256               case 'r':     /* Long double.  */
04257               case 'd':     /* Double.  */
04258               case 'f':     /* Float.  */
04259                 realp = TRUE;
04260                 done = TRUE;
04261                 break;
04262               default:
04263                 /* Assume it's a user defined integral type.  */
04264                 integralp = TRUE;
04265                 done = TRUE;
04266                 break;
04267               }
04268            }
04269 
04270          if (integralp)
04271            {
04272              if (**pp == 'm')
04273               ++*pp;
04274              while (ISDIGIT (**pp))
04275               ++*pp;
04276            }
04277          else if (charp)
04278            {
04279              unsigned int val;
04280 
04281              if (**pp == 'm')
04282               ++*pp;
04283              val = stab_demangle_count (pp);
04284              if (val == 0)
04285               {
04286                 stab_bad_demangle (orig);
04287                 return FALSE;
04288               }
04289            }
04290          else if (boolp)
04291            {
04292              unsigned int val;
04293 
04294              val = stab_demangle_count (pp);
04295              if (val != 0 && val != 1)
04296               {
04297                 stab_bad_demangle (orig);
04298                 return FALSE;
04299               }
04300            }
04301          else if (realp)
04302            {
04303              if (**pp == 'm')
04304               ++*pp;
04305              while (ISDIGIT (**pp))
04306               ++*pp;
04307              if (**pp == '.')
04308               {
04309                 ++*pp;
04310                 while (ISDIGIT (**pp))
04311                   ++*pp;
04312               }
04313              if (**pp == 'e')
04314               {
04315                 ++*pp;
04316                 while (ISDIGIT (**pp))
04317                   ++*pp;
04318               }
04319            }
04320          else if (pointerp)
04321            {
04322              unsigned int len;
04323 
04324              len = stab_demangle_count (pp);
04325              if (len == 0)
04326               {
04327                 stab_bad_demangle (orig);
04328                 return FALSE;
04329               }
04330              *pp += len;
04331            }
04332        }
04333     }
04334 
04335   /* We can translate this to a string fairly easily by invoking the
04336      regular demangling routine.  */
04337   if (pname != NULL)
04338     {
04339       char *s1, *s2, *s3, *s4 = NULL;
04340       char *from, *to;
04341 
04342       s1 = savestring (orig, *pp - orig);
04343 
04344       s2 = concat ("NoSuchStrinG__", s1, (const char *) NULL);
04345 
04346       free (s1);
04347 
04348       s3 = cplus_demangle (s2, DMGL_ANSI);
04349 
04350       free (s2);
04351 
04352       if (s3 != NULL)
04353        s4 = strstr (s3, "::NoSuchStrinG");
04354       if (s3 == NULL || s4 == NULL)
04355        {
04356          stab_bad_demangle (orig);
04357          if (s3 != NULL)
04358            free (s3);
04359          return FALSE;
04360        }
04361 
04362       /* Eliminating all spaces, except those between > characters,
04363          makes it more likely that the demangled name will match the
04364          name which g++ used as the structure name.  */
04365       for (from = to = s3; from != s4; ++from)
04366        if (*from != ' '
04367            || (from[1] == '>' && from > s3 && from[-1] == '>'))
04368          *to++ = *from;
04369 
04370       *pname = savestring (s3, to - s3);
04371 
04372       free (s3);
04373     }
04374 
04375   return TRUE;
04376 }
04377 
04378 /* Demangle a class name.  */
04379 
04380 static bfd_boolean
04381 stab_demangle_class (struct stab_demangle_info *minfo ATTRIBUTE_UNUSED,
04382                    const char **pp, const char **pstart)
04383 {
04384   const char *orig;
04385   unsigned int n;
04386 
04387   orig = *pp;
04388 
04389   n = stab_demangle_count (pp);
04390   if (strlen (*pp) < n)
04391     {
04392       stab_bad_demangle (orig);
04393       return FALSE;
04394     }
04395 
04396   if (pstart != NULL)
04397     *pstart = *pp;
04398 
04399   *pp += n;
04400 
04401   return TRUE;
04402 }
04403 
04404 /* Demangle function arguments.  If the pargs argument is not NULL, it
04405    is set to a NULL terminated array holding the arguments.  */
04406 
04407 static bfd_boolean
04408 stab_demangle_args (struct stab_demangle_info *minfo, const char **pp,
04409                   debug_type **pargs, bfd_boolean *pvarargs)
04410 {
04411   const char *orig;
04412   unsigned int alloc, count;
04413 
04414   orig = *pp;
04415 
04416   alloc = 10;
04417   if (pargs != NULL)
04418     {
04419       *pargs = (debug_type *) xmalloc (alloc * sizeof **pargs);
04420       *pvarargs = FALSE;
04421     }
04422   count = 0;
04423 
04424   while (**pp != '_' && **pp != '\0' && **pp != 'e')
04425     {
04426       if (**pp == 'N' || **pp == 'T')
04427        {
04428          char temptype;
04429          unsigned int r, t;
04430 
04431          temptype = **pp;
04432          ++*pp;
04433 
04434          if (temptype == 'T')
04435            r = 1;
04436          else
04437            {
04438              if (! stab_demangle_get_count (pp, &r))
04439               {
04440                 stab_bad_demangle (orig);
04441                 return FALSE;
04442               }
04443            }
04444 
04445          if (! stab_demangle_get_count (pp, &t))
04446            {
04447              stab_bad_demangle (orig);
04448              return FALSE;
04449            }
04450 
04451          if (t >= minfo->typestring_count)
04452            {
04453              stab_bad_demangle (orig);
04454              return FALSE;
04455            }
04456          while (r-- > 0)
04457            {
04458              const char *tem;
04459 
04460              tem = minfo->typestrings[t].typestring;
04461              if (! stab_demangle_arg (minfo, &tem, pargs, &count, &alloc))
04462               return FALSE;
04463            }
04464        }
04465       else
04466        {
04467          if (! stab_demangle_arg (minfo, pp, pargs, &count, &alloc))
04468            return FALSE;
04469        }
04470     }
04471 
04472   if (pargs != NULL)
04473     (*pargs)[count] = DEBUG_TYPE_NULL;
04474 
04475   if (**pp == 'e')
04476     {
04477       if (pargs != NULL)
04478        *pvarargs = TRUE;
04479       ++*pp;
04480     }
04481 
04482   return TRUE;
04483 }
04484 
04485 /* Demangle a single argument.  */
04486 
04487 static bfd_boolean
04488 stab_demangle_arg (struct stab_demangle_info *minfo, const char **pp,
04489                  debug_type **pargs, unsigned int *pcount,
04490                  unsigned int *palloc)
04491 {
04492   const char *start;
04493   debug_type type;
04494 
04495   start = *pp;
04496   if (! stab_demangle_type (minfo, pp,
04497                          pargs == NULL ? (debug_type *) NULL : &type)
04498       || ! stab_demangle_remember_type (minfo, start, *pp - start))
04499     return FALSE;
04500 
04501   if (pargs != NULL)
04502     {
04503       if (type == DEBUG_TYPE_NULL)
04504        return FALSE;
04505 
04506       if (*pcount + 1 >= *palloc)
04507        {
04508          *palloc += 10;
04509          *pargs = ((debug_type *)
04510                   xrealloc (*pargs, *palloc * sizeof **pargs));
04511        }
04512       (*pargs)[*pcount] = type;
04513       ++*pcount;
04514     }
04515 
04516   return TRUE;
04517 }
04518 
04519 /* Demangle a type.  If the ptype argument is not NULL, *ptype is set
04520    to the newly allocated type.  */
04521 
04522 static bfd_boolean
04523 stab_demangle_type (struct stab_demangle_info *minfo, const char **pp,
04524                   debug_type *ptype)
04525 {
04526   const char *orig;
04527 
04528   orig = *pp;
04529 
04530   switch (**pp)
04531     {
04532     case 'P':
04533     case 'p':
04534       /* A pointer type.  */
04535       ++*pp;
04536       if (! stab_demangle_type (minfo, pp, ptype))
04537        return FALSE;
04538       if (ptype != NULL)
04539        *ptype = debug_make_pointer_type (minfo->dhandle, *ptype);
04540       break;
04541 
04542     case 'R':
04543       /* A reference type.  */
04544       ++*pp;
04545       if (! stab_demangle_type (minfo, pp, ptype))
04546        return FALSE;
04547       if (ptype != NULL)
04548        *ptype = debug_make_reference_type (minfo->dhandle, *ptype);
04549       break;
04550 
04551     case 'A':
04552       /* An array.  */
04553       {
04554        unsigned long high;
04555 
04556        ++*pp;
04557        high = 0;
04558        while (**pp != '\0' && **pp != '_')
04559          {
04560            if (! ISDIGIT (**pp))
04561              {
04562               stab_bad_demangle (orig);
04563               return FALSE;
04564              }
04565            high *= 10;
04566            high += **pp - '0';
04567            ++*pp;
04568          }
04569        if (**pp != '_')
04570          {
04571            stab_bad_demangle (orig);
04572            return FALSE;
04573          }
04574        ++*pp;
04575 
04576        if (! stab_demangle_type (minfo, pp, ptype))
04577          return FALSE;
04578        if (ptype != NULL)
04579          {
04580            debug_type int_type;
04581 
04582            int_type = debug_find_named_type (minfo->dhandle, "int");
04583            if (int_type == NULL)
04584              int_type = debug_make_int_type (minfo->dhandle, 4, FALSE);
04585            *ptype = debug_make_array_type (minfo->dhandle, *ptype, int_type,
04586                                        0, high, FALSE);
04587          }
04588       }
04589       break;
04590 
04591     case 'T':
04592       /* A back reference to a remembered type.  */
04593       {
04594        unsigned int i;
04595        const char *p;
04596 
04597        ++*pp;
04598        if (! stab_demangle_get_count (pp, &i))
04599          {
04600            stab_bad_demangle (orig);
04601            return FALSE;
04602          }
04603        if (i >= minfo->typestring_count)
04604          {
04605            stab_bad_demangle (orig);
04606            return FALSE;
04607          }
04608        p = minfo->typestrings[i].typestring;
04609        if (! stab_demangle_type (minfo, &p, ptype))
04610          return FALSE;
04611       }
04612       break;
04613 
04614     case 'F':
04615       /* A function.  */
04616       {
04617        debug_type *args;
04618        bfd_boolean varargs;
04619 
04620        ++*pp;
04621        if (! stab_demangle_args (minfo, pp,
04622                               (ptype == NULL
04623                                ? (debug_type **) NULL
04624                                : &args),
04625                               (ptype == NULL
04626                                ? (bfd_boolean *) NULL
04627                                : &varargs)))
04628          return FALSE;
04629        if (**pp != '_')
04630          {
04631            /* cplus_demangle will accept a function without a return
04632               type, but I don't know when that will happen, or what
04633               to do if it does.  */
04634            stab_bad_demangle (orig);
04635            return FALSE;
04636          }
04637        ++*pp;
04638        if (! stab_demangle_type (minfo, pp, ptype))
04639          return FALSE;
04640        if (ptype != NULL)
04641          *ptype = debug_make_function_type (minfo->dhandle, *ptype, args,
04642                                         varargs);
04643 
04644       }
04645       break;
04646 
04647     case 'M':
04648     case 'O':
04649       {
04650        bfd_boolean memberp, constp, volatilep;
04651        debug_type class_type = DEBUG_TYPE_NULL;
04652        debug_type *args;
04653        bfd_boolean varargs;
04654        unsigned int n;
04655        const char *name;
04656 
04657        memberp = **pp == 'M';
04658        constp = FALSE;
04659        volatilep = FALSE;
04660        args = NULL;
04661        varargs = FALSE;
04662 
04663        ++*pp;
04664        if (ISDIGIT (**pp))
04665          {
04666            n = stab_demangle_count (pp);
04667            if (strlen (*pp) < n)
04668              {
04669               stab_bad_demangle (orig);
04670               return FALSE;
04671              }
04672            name = *pp;
04673            *pp += n;
04674 
04675            if (ptype != NULL)
04676              {
04677               class_type = stab_find_tagged_type (minfo->dhandle,
04678                                               minfo->info,
04679                                               name, (int) n,
04680                                               DEBUG_KIND_CLASS);
04681               if (class_type == DEBUG_TYPE_NULL)
04682                 return FALSE;
04683              }
04684          }
04685        else if (**pp == 'Q')
04686          {
04687            if (! stab_demangle_qualified (minfo, pp,
04688                                       (ptype == NULL
04689                                        ? (debug_type *) NULL
04690                                        : &class_type)))
04691              return FALSE;
04692          }
04693        else
04694          {
04695            stab_bad_demangle (orig);
04696            return FALSE;
04697          }
04698 
04699        if (memberp)
04700          {
04701            if (**pp == 'C')
04702              {
04703               constp = TRUE;
04704               ++*pp;
04705              }
04706            else if (**pp == 'V')
04707              {
04708               volatilep = TRUE;
04709               ++*pp;
04710              }
04711            if (**pp != 'F')
04712              {
04713               stab_bad_demangle (orig);
04714               return FALSE;
04715              }
04716            ++*pp;
04717            if (! stab_demangle_args (minfo, pp,
04718                                   (ptype == NULL
04719                                    ? (debug_type **) NULL
04720                                    : &args),
04721                                   (ptype == NULL
04722                                    ? (bfd_boolean *) NULL
04723                                    : &varargs)))
04724              return FALSE;
04725          }
04726 
04727        if (**pp != '_')
04728          {
04729            stab_bad_demangle (orig);
04730            return FALSE;
04731          }
04732        ++*pp;
04733 
04734        if (! stab_demangle_type (minfo, pp, ptype))
04735          return FALSE;
04736 
04737        if (ptype != NULL)
04738          {
04739            if (! memberp)
04740              *ptype = debug_make_offset_type (minfo->dhandle, class_type,
04741                                           *ptype);
04742            else
04743              {
04744               /* FIXME: We have no way to record constp or
04745                    volatilep.  */
04746               *ptype = debug_make_method_type (minfo->dhandle, *ptype,
04747                                            class_type, args, varargs);
04748              }
04749          }
04750       }
04751       break;
04752 
04753     case 'G':
04754       ++*pp;
04755       if (! stab_demangle_type (minfo, pp, ptype))
04756        return FALSE;
04757       break;
04758 
04759     case 'C':
04760       ++*pp;
04761       if (! stab_demangle_type (minfo, pp, ptype))
04762        return FALSE;
04763       if (ptype != NULL)
04764        *ptype = debug_make_const_type (minfo->dhandle, *ptype);
04765       break;
04766 
04767     case 'Q':
04768       {
04769        const char *hold;
04770 
04771        hold = *pp;
04772        if (! stab_demangle_qualified (minfo, pp, ptype))
04773          return FALSE;
04774       }
04775       break;
04776 
04777     default:
04778       if (! stab_demangle_fund_type (minfo, pp, ptype))
04779        return FALSE;
04780       break;
04781     }
04782 
04783   return TRUE;
04784 }
04785 
04786 /* Demangle a fundamental type.  If the ptype argument is not NULL,
04787    *ptype is set to the newly allocated type.  */
04788 
04789 static bfd_boolean
04790 stab_demangle_fund_type (struct stab_demangle_info *minfo, const char **pp,
04791                       debug_type *ptype)
04792 {
04793   const char *orig;
04794   bfd_boolean constp, volatilep, unsignedp, signedp;
04795   bfd_boolean done;
04796 
04797   orig = *pp;
04798 
04799   constp = FALSE;
04800   volatilep = FALSE;
04801   unsignedp = FALSE;
04802   signedp = FALSE;
04803 
04804   done = FALSE;
04805   while (! done)
04806     {
04807       switch (**pp)
04808        {
04809        case 'C':
04810          constp = TRUE;
04811          ++*pp;
04812          break;
04813 
04814        case 'U':
04815          unsignedp = TRUE;
04816          ++*pp;
04817          break;
04818 
04819        case 'S':
04820          signedp = TRUE;
04821          ++*pp;
04822          break;
04823 
04824        case 'V':
04825          volatilep = TRUE;
04826          ++*pp;
04827          break;
04828 
04829        default:
04830          done = TRUE;
04831          break;
04832        }
04833     }
04834 
04835   switch (**pp)
04836     {
04837     case '\0':
04838     case '_':
04839       /* cplus_demangle permits this, but I don't know what it means.  */
04840       stab_bad_demangle (orig);
04841       break;
04842 
04843     case 'v': /* void */
04844       if (ptype != NULL)
04845        {
04846          *ptype = debug_find_named_type (minfo->dhandle, "void");
04847          if (*ptype == DEBUG_TYPE_NULL)
04848            *ptype = debug_make_void_type (minfo->dhandle);
04849        }
04850       ++*pp;
04851       break;
04852 
04853     case 'x': /* long long */
04854       if (ptype != NULL)
04855        {
04856          *ptype = debug_find_named_type (minfo->dhandle,
04857                                      (unsignedp
04858                                       ? "long long unsigned int"
04859                                       : "long long int"));
04860          if (*ptype == DEBUG_TYPE_NULL)
04861            *ptype = debug_make_int_type (minfo->dhandle, 8, unsignedp);
04862        }
04863       ++*pp;
04864       break;
04865 
04866     case 'l': /* long */
04867       if (ptype != NULL)
04868        {
04869          *ptype = debug_find_named_type (minfo->dhandle,
04870                                      (unsignedp
04871                                       ? "long unsigned int"
04872                                       : "long int"));
04873          if (*ptype == DEBUG_TYPE_NULL)
04874            *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
04875        }
04876       ++*pp;
04877       break;
04878 
04879     case 'i': /* int */
04880       if (ptype != NULL)
04881        {
04882          *ptype = debug_find_named_type (minfo->dhandle,
04883                                      (unsignedp
04884                                       ? "unsigned int"
04885                                       : "int"));
04886          if (*ptype == DEBUG_TYPE_NULL)
04887            *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
04888        }
04889       ++*pp;
04890       break;
04891 
04892     case 's': /* short */
04893       if (ptype != NULL)
04894        {
04895          *ptype = debug_find_named_type (minfo->dhandle,
04896                                      (unsignedp
04897                                       ? "short unsigned int"
04898                                       : "short int"));
04899          if (*ptype == DEBUG_TYPE_NULL)
04900            *ptype = debug_make_int_type (minfo->dhandle, 2, unsignedp);
04901        }
04902       ++*pp;
04903       break;
04904 
04905     case 'b': /* bool */
04906       if (ptype != NULL)
04907        {
04908          *ptype = debug_find_named_type (minfo->dhandle, "bool");
04909          if (*ptype == DEBUG_TYPE_NULL)
04910            *ptype = debug_make_bool_type (minfo->dhandle, 4);
04911        }
04912       ++*pp;
04913       break;
04914 
04915     case 'c': /* char */
04916       if (ptype != NULL)
04917        {
04918          *ptype = debug_find_named_type (minfo->dhandle,
04919                                      (unsignedp
04920                                       ? "unsigned char"
04921                                       : (signedp
04922                                          ? "signed char"
04923                                          : "char")));
04924          if (*ptype == DEBUG_TYPE_NULL)
04925            *ptype = debug_make_int_type (minfo->dhandle, 1, unsignedp);
04926        }
04927       ++*pp;
04928       break;
04929 
04930     case 'w': /* wchar_t */
04931       if (ptype != NULL)
04932        {
04933          *ptype = debug_find_named_type (minfo->dhandle, "__wchar_t");
04934          if (*ptype == DEBUG_TYPE_NULL)
04935            *ptype = debug_make_int_type (minfo->dhandle, 2, TRUE);
04936        }
04937       ++*pp;
04938       break;
04939 
04940     case 'r': /* long double */
04941       if (ptype != NULL)
04942        {
04943          *ptype = debug_find_named_type (minfo->dhandle, "long double");
04944          if (*ptype == DEBUG_TYPE_NULL)
04945            *ptype = debug_make_float_type (minfo->dhandle, 8);
04946        }
04947       ++*pp;
04948       break;
04949 
04950     case 'd': /* double */
04951       if (ptype != NULL)
04952        {
04953          *ptype = debug_find_named_type (minfo->dhandle, "double");
04954          if (*ptype == DEBUG_TYPE_NULL)
04955            *ptype = debug_make_float_type (minfo->dhandle, 8);
04956        }
04957       ++*pp;
04958       break;
04959 
04960     case 'f': /* float */
04961       if (ptype != NULL)
04962        {
04963          *ptype = debug_find_named_type (minfo->dhandle, "float");
04964          if (*ptype == DEBUG_TYPE_NULL)
04965            *ptype = debug_make_float_type (minfo->dhandle, 4);
04966        }
04967       ++*pp;
04968       break;
04969 
04970     case 'G':
04971       ++*pp;
04972       if (! ISDIGIT (**pp))
04973        {
04974          stab_bad_demangle (orig);
04975          return FALSE;
04976        }
04977       /* Fall through.  */
04978     case '0': case '1': case '2': case '3': case '4':
04979     case '5': case '6': case '7': case '8': case '9':
04980       {
04981        const char *hold;
04982 
04983        if (! stab_demangle_class (minfo, pp, &hold))
04984          return FALSE;
04985        if (ptype != NULL)
04986          {
04987            char *name;
04988 
04989            name = savestring (hold, *pp - hold);
04990            *ptype = debug_find_named_type (minfo->dhandle, name);
04991            free (name);
04992            if (*ptype == DEBUG_TYPE_NULL)
04993              {
04994               /* FIXME: It is probably incorrect to assume that
04995                    undefined types are tagged types.  */
04996               *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
04997                                           hold, *pp - hold,
04998                                           DEBUG_KIND_ILLEGAL);
04999               if (*ptype == DEBUG_TYPE_NULL)
05000                 return FALSE;
05001              }
05002          }
05003       }
05004       break;
05005 
05006     case 't':
05007       {
05008        char *name;
05009 
05010        if (! stab_demangle_template (minfo, pp,
05011                                   ptype != NULL ? &name : NULL))
05012          return FALSE;
05013        if (ptype != NULL)
05014          {
05015            *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
05016                                        name, strlen (name),
05017                                        DEBUG_KIND_CLASS);
05018            free (name);
05019            if (*ptype == DEBUG_TYPE_NULL)
05020              return FALSE;
05021          }
05022       }
05023       break;
05024 
05025     default:
05026       stab_bad_demangle (orig);
05027       return FALSE;
05028     }
05029 
05030   if (ptype != NULL)
05031     {
05032       if (constp)
05033        *ptype = debug_make_const_type (minfo->dhandle, *ptype);
05034       if (volatilep)
05035        *ptype = debug_make_volatile_type (minfo->dhandle, *ptype);
05036     }
05037 
05038   return TRUE;
05039 }
05040 
05041 /* Remember a type string in a demangled string.  */
05042 
05043 static bfd_boolean
05044 stab_demangle_remember_type (struct stab_demangle_info *minfo,
05045                           const char *p, int len)
05046 {
05047   if (minfo->typestring_count >= minfo->typestring_alloc)
05048     {
05049       minfo->typestring_alloc += 10;
05050       minfo->typestrings = ((struct stab_demangle_typestring *)
05051                          xrealloc (minfo->typestrings,
05052                                   (minfo->typestring_alloc
05053                                    * sizeof *minfo->typestrings)));
05054     }
05055 
05056   minfo->typestrings[minfo->typestring_count].typestring = p;
05057   minfo->typestrings[minfo->typestring_count].len = (unsigned int) len;
05058   ++minfo->typestring_count;
05059 
05060   return TRUE;
05061 }
05062 
05063 /* Demangle names encoded using the g++ V3 ABI.  The newer versions of
05064    g++ which use this ABI do not encode ordinary method argument types
05065    in a mangled name; they simply output the argument types.  However,
05066    for a static method, g++ simply outputs the return type and the
05067    physical name.  So in that case we need to demangle the name here.
05068    Here PHYSNAME is the physical name of the function, and we set the
05069    variable pointed at by PVARARGS to indicate whether this function
05070    is varargs.  This returns NULL, or a NULL terminated array of
05071    argument types.  */
05072 
05073 static debug_type *
05074 stab_demangle_v3_argtypes (void *dhandle, struct stab_handle *info,
05075                         const char *physname, bfd_boolean *pvarargs)
05076 {
05077   struct demangle_component *dc;
05078   void *mem;
05079   debug_type *pargs;
05080 
05081   dc = cplus_demangle_v3_components (physname, DMGL_PARAMS | DMGL_ANSI, &mem);
05082   if (dc == NULL)
05083     {
05084       stab_bad_demangle (physname);
05085       return NULL;
05086     }
05087 
05088   /* We expect to see TYPED_NAME, and the right subtree describes the
05089      function type.  */
05090   if (dc->type != DEMANGLE_COMPONENT_TYPED_NAME
05091       || dc->u.s_binary.right->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
05092     {
05093       fprintf (stderr, _("Demangled name is not a function\n"));
05094       free (mem);
05095       return NULL;
05096     }
05097 
05098   pargs = stab_demangle_v3_arglist (dhandle, info,
05099                                 dc->u.s_binary.right->u.s_binary.right,
05100                                 pvarargs);
05101 
05102   free (mem);
05103 
05104   return pargs;
05105 }
05106 
05107 /* Demangle an argument list in a struct demangle_component tree.
05108    Returns a DEBUG_TYPE_NULL terminated array of argument types, and
05109    sets *PVARARGS to indicate whether this is a varargs function.  */
05110 
05111 static debug_type *
05112 stab_demangle_v3_arglist (void *dhandle, struct stab_handle *info,
05113                        struct demangle_component *arglist,
05114                        bfd_boolean *pvarargs)
05115 {
05116   struct demangle_component *dc;
05117   unsigned int alloc, count;
05118   debug_type *pargs;
05119 
05120   alloc = 10;
05121   pargs = (debug_type *) xmalloc (alloc * sizeof *pargs);
05122   *pvarargs = FALSE;
05123 
05124   count = 0;
05125 
05126   for (dc = arglist;
05127        dc != NULL;
05128        dc = dc->u.s_binary.right)
05129     {
05130       debug_type arg;
05131       bfd_boolean varargs;
05132 
05133       if (dc->type != DEMANGLE_COMPONENT_ARGLIST)
05134        {
05135          fprintf (stderr, _("Unexpected type in v3 arglist demangling\n"));
05136          free (pargs);
05137          return NULL;
05138        }
05139 
05140       arg = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left,
05141                               NULL, &varargs);
05142       if (arg == NULL)
05143        {
05144          if (varargs)
05145            {
05146              *pvarargs = TRUE;
05147              continue;
05148            }
05149          free (pargs);
05150          return NULL;
05151        }
05152 
05153       if (count + 1 >= alloc)
05154        {
05155          alloc += 10;
05156          pargs = (debug_type *) xrealloc (pargs, alloc * sizeof *pargs);
05157        }
05158 
05159       pargs[count] = arg;
05160       ++count;
05161     }
05162 
05163   pargs[count] = DEBUG_TYPE_NULL;
05164 
05165   return pargs;
05166 }
05167 
05168 /* Convert a struct demangle_component tree describing an argument
05169    type into a debug_type.  */
05170 
05171 static debug_type
05172 stab_demangle_v3_arg (void *dhandle, struct stab_handle *info,
05173                     struct demangle_component *dc, debug_type context,
05174                     bfd_boolean *pvarargs)
05175 {
05176   debug_type dt;
05177 
05178   if (pvarargs != NULL)
05179     *pvarargs = FALSE;
05180 
05181   switch (dc->type)
05182     {
05183       /* FIXME: These are demangle component types which we probably
05184         need to handle one way or another.  */
05185     case DEMANGLE_COMPONENT_LOCAL_NAME:
05186     case DEMANGLE_COMPONENT_TYPED_NAME:
05187     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
05188     case DEMANGLE_COMPONENT_CTOR:
05189     case DEMANGLE_COMPONENT_DTOR:
05190     case DEMANGLE_COMPONENT_JAVA_CLASS:
05191     case DEMANGLE_COMPONENT_RESTRICT_THIS:
05192     case DEMANGLE_COMPONENT_VOLATILE_THIS:
05193     case DEMANGLE_COMPONENT_CONST_THIS:
05194     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
05195     case DEMANGLE_COMPONENT_COMPLEX:
05196     case DEMANGLE_COMPONENT_IMAGINARY:
05197     case DEMANGLE_COMPONENT_VENDOR_TYPE:
05198     case DEMANGLE_COMPONENT_ARRAY_TYPE:
05199     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
05200     case DEMANGLE_COMPONENT_ARGLIST:
05201     default:
05202       fprintf (stderr, _("Unrecognized demangle component %d\n"),
05203               (int) dc->type);
05204       return NULL;
05205 
05206     case DEMANGLE_COMPONENT_NAME:
05207       if (context != NULL)
05208        {
05209          const debug_field *fields;
05210 
05211          fields = debug_get_fields (dhandle, context);
05212          if (fields != NULL)
05213            {
05214              /* Try to find this type by looking through the context
05215                class.  */
05216              for (; *fields != DEBUG_FIELD_NULL; fields++)
05217               {
05218                 debug_type ft;
05219                 const char *dn;
05220 
05221                 ft = debug_get_field_type (dhandle, *fields);
05222                 if (ft == NULL)
05223                   return NULL;
05224                 dn = debug_get_type_name (dhandle, ft);
05225                 if (dn != NULL
05226                     && (int) strlen (dn) == dc->u.s_name.len
05227                     && strncmp (dn, dc->u.s_name.s, dc->u.s_name.len) == 0)
05228                   return ft;
05229               }
05230            }
05231        }
05232       return stab_find_tagged_type (dhandle, info, dc->u.s_name.s,
05233                                 dc->u.s_name.len, DEBUG_KIND_ILLEGAL);
05234 
05235     case DEMANGLE_COMPONENT_QUAL_NAME:
05236       context = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left,
05237                                   context, NULL);
05238       if (context == NULL)
05239        return NULL;
05240       return stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.right,
05241                                context, NULL);
05242 
05243     case DEMANGLE_COMPONENT_TEMPLATE:
05244       {
05245        char *p;
05246        size_t alc;
05247 
05248        /* We print this component to get a class name which we can
05249           use.  FIXME: This probably won't work if the template uses
05250           template parameters which refer to an outer template.  */
05251        p = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, dc, 20, &alc);
05252        if (p == NULL)
05253          {
05254            fprintf (stderr, _("Failed to print demangled template\n"));
05255            return NULL;
05256          }
05257        dt = stab_find_tagged_type (dhandle, info, p, strlen (p),
05258                                 DEBUG_KIND_CLASS);
05259        free (p);
05260        return dt;
05261       }
05262 
05263     case DEMANGLE_COMPONENT_SUB_STD:
05264       return stab_find_tagged_type (dhandle, info, dc->u.s_string.string,
05265                                 dc->u.s_string.len, DEBUG_KIND_ILLEGAL);
05266 
05267     case DEMANGLE_COMPONENT_RESTRICT:
05268     case DEMANGLE_COMPONENT_VOLATILE:
05269     case DEMANGLE_COMPONENT_CONST:
05270     case DEMANGLE_COMPONENT_POINTER:
05271     case DEMANGLE_COMPONENT_REFERENCE:
05272       dt = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left, NULL,
05273                              NULL);
05274       if (dt == NULL)
05275        return NULL;
05276 
05277       switch (dc->type)
05278        {
05279        default:
05280          abort ();
05281        case DEMANGLE_COMPONENT_RESTRICT:
05282          /* FIXME: We have no way to represent restrict.  */
05283          return dt;
05284        case DEMANGLE_COMPONENT_VOLATILE:
05285          return debug_make_volatile_type (dhandle, dt);
05286        case DEMANGLE_COMPONENT_CONST:
05287          return debug_make_const_type (dhandle, dt);
05288        case DEMANGLE_COMPONENT_POINTER:
05289          return debug_make_pointer_type (dhandle, dt);
05290        case DEMANGLE_COMPONENT_REFERENCE:
05291          return debug_make_reference_type (dhandle, dt);
05292        }
05293 
05294     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
05295       {
05296        debug_type *pargs;
05297        bfd_boolean varargs;
05298 
05299        if (dc->u.s_binary.left == NULL)
05300          {
05301            /* In this case the return type is actually unknown.
05302               However, I'm not sure this will ever arise in practice;
05303               normally an unknown return type would only appear at
05304               the top level, which is handled above.  */
05305            dt = debug_make_void_type (dhandle);
05306          }
05307        else
05308          dt = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left, NULL,
05309                                  NULL);
05310        if (dt == NULL)
05311          return NULL;
05312 
05313        pargs = stab_demangle_v3_arglist (dhandle, info,
05314                                      dc->u.s_binary.right,
05315                                      &varargs);
05316        if (pargs == NULL)
05317          return NULL;
05318 
05319        return debug_make_function_type (dhandle, dt, pargs, varargs);
05320       }
05321 
05322     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
05323       {
05324        char *p;
05325        size_t alc;
05326        debug_type ret;
05327 
05328        /* We print this component in order to find out the type name.
05329           FIXME: Should we instead expose the
05330           demangle_builtin_type_info structure?  */
05331        p = cplus_demangle_print (DMGL_PARAMS | DMGL_ANSI, dc, 20, &alc);
05332        if (p == NULL)
05333          {
05334            fprintf (stderr, _("Couldn't get demangled builtin type\n"));
05335            return NULL;
05336          }
05337 
05338        /* The mangling is based on the type, but does not itself
05339           indicate what the sizes are.  So we have to guess.  */
05340        if (strcmp (p, "signed char") == 0)
05341          ret = debug_make_int_type (dhandle, 1, FALSE);
05342        else if (strcmp (p, "bool") == 0)
05343          ret = debug_make_bool_type (dhandle, 1);
05344        else if (strcmp (p, "char") == 0)
05345          ret = debug_make_int_type (dhandle, 1, FALSE);
05346        else if (strcmp (p, "double") == 0)
05347          ret = debug_make_float_type (dhandle, 8);
05348        else if (strcmp (p, "long double") == 0)
05349          ret = debug_make_float_type (dhandle, 8);
05350        else if (strcmp (p, "float") == 0)
05351          ret = debug_make_float_type (dhandle, 4);
05352        else if (strcmp (p, "__float128") == 0)
05353          ret = debug_make_float_type (dhandle, 16);
05354        else if (strcmp (p, "unsigned char") == 0)
05355          ret = debug_make_int_type (dhandle, 1, TRUE);
05356        else if (strcmp (p, "int") == 0)
05357          ret = debug_make_int_type (dhandle, 4, FALSE);
05358        else if (strcmp (p, "unsigned int") == 0)
05359          ret = debug_make_int_type (dhandle, 4, TRUE);
05360        else if (strcmp (p, "long") == 0)
05361          ret = debug_make_int_type (dhandle, 4, FALSE);
05362        else if (strcmp (p, "unsigned long") == 0)
05363          ret = debug_make_int_type (dhandle, 4, TRUE);
05364        else if (strcmp (p, "__int128") == 0)
05365          ret = debug_make_int_type (dhandle, 16, FALSE);
05366        else if (strcmp (p, "unsigned __int128") == 0)
05367          ret = debug_make_int_type (dhandle, 16, TRUE);
05368        else if (strcmp (p, "short") == 0)
05369          ret = debug_make_int_type (dhandle, 2, FALSE);
05370        else if (strcmp (p, "unsigned short") == 0)
05371          ret = debug_make_int_type (dhandle, 2, TRUE);
05372        else if (strcmp (p, "void") == 0)
05373          ret = debug_make_void_type (dhandle);
05374        else if (strcmp (p, "wchar_t") == 0)
05375          ret = debug_make_int_type (dhandle, 4, TRUE);
05376        else if (strcmp (p, "long long") == 0)
05377          ret = debug_make_int_type (dhandle, 8, FALSE);
05378        else if (strcmp (p, "unsigned long long") == 0)
05379          ret = debug_make_int_type (dhandle, 8, TRUE);
05380        else if (strcmp (p, "...") == 0)
05381          {
05382            if (pvarargs == NULL)
05383              fprintf (stderr, _("Unexpected demangled varargs\n"));
05384            else
05385              *pvarargs = TRUE;
05386            ret = NULL;
05387          }
05388        else
05389          {
05390            fprintf (stderr, _("Unrecognized demangled builtin type\n"));
05391            ret = NULL;
05392          }
05393 
05394        free (p);
05395 
05396        return ret;
05397       }
05398     }
05399 }