gdb/emacs/dir-locals: Update settings for c++-mode
[external/binutils.git] / binutils / stabs.c
1 /* stabs.c -- Parse stabs debugging information
2    Copyright (C) 1995-2018 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@cygnus.com>.
4
5    This file is part of GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21
22 /* This file contains code which parses stabs debugging information.
23    The organization of this code is based on the gdb stabs reading
24    code.  The job it does is somewhat different, because it is not
25    trying to identify the correct address for anything.  */
26
27 #include "sysdep.h"
28 #include "bfd.h"
29 #include "libiberty.h"
30 #include "safe-ctype.h"
31 #include "demangle.h"
32 #include "debug.h"
33 #include "budbg.h"
34 #include "filenames.h"
35 #include "aout/aout64.h"
36 #include "aout/stab_gnu.h"
37
38 /* The number of predefined XCOFF types.  */
39
40 #define XCOFF_TYPE_COUNT 34
41
42 /* This structure is used as a handle so that the stab parsing doesn't
43    need to use any static variables.  */
44
45 struct stab_handle
46 {
47   /* The BFD.  */
48   bfd *abfd;
49   /* TRUE if this is stabs in sections.  */
50   bfd_boolean sections;
51   /* The symbol table.  */
52   asymbol **syms;
53   /* The number of symbols.  */
54   long symcount;
55   /* The accumulated file name string.  */
56   char *so_string;
57   /* The value of the last N_SO symbol.  */
58   bfd_vma so_value;
59   /* The value of the start of the file, so that we can handle file
60      relative N_LBRAC and N_RBRAC symbols.  */
61   bfd_vma file_start_offset;
62   /* The offset of the start of the function, so that we can handle
63      function relative N_LBRAC and N_RBRAC symbols.  */
64   bfd_vma function_start_offset;
65   /* The version number of gcc which compiled the current compilation
66      unit, 0 if not compiled by gcc.  */
67   int gcc_compiled;
68   /* Whether an N_OPT symbol was seen that was not generated by gcc,
69      so that we can detect the SunPRO compiler.  */
70   bfd_boolean n_opt_found;
71   /* The main file name.  */
72   char *main_filename;
73   /* A stack of unfinished N_BINCL files.  */
74   struct bincl_file *bincl_stack;
75   /* A list of finished N_BINCL files.  */
76   struct bincl_file *bincl_list;
77   /* Whether we are inside a function or not.  */
78   bfd_boolean within_function;
79   /* The address of the end of the function, used if we have seen an
80      N_FUN symbol while in a function.  This is -1 if we have not seen
81      an N_FUN (the normal case).  */
82   bfd_vma function_end;
83   /* The depth of block nesting.  */
84   int block_depth;
85   /* List of pending variable definitions.  */
86   struct stab_pending_var *pending;
87   /* Number of files for which we have types.  */
88   unsigned int files;
89   /* Lists of types per file.  */
90   struct stab_types **file_types;
91   /* Predefined XCOFF types.  */
92   debug_type xcoff_types[XCOFF_TYPE_COUNT];
93   /* Undefined tags.  */
94   struct stab_tag *tags;
95   /* Set by parse_stab_type if it sees a structure defined as a cross
96      reference to itself.  Reset by parse_stab_type otherwise.  */
97   bfd_boolean self_crossref;
98 };
99
100 /* A list of these structures is used to hold pending variable
101    definitions seen before the N_LBRAC of a block.  */
102
103 struct stab_pending_var
104 {
105   /* Next pending variable definition.  */
106   struct stab_pending_var *next;
107   /* Name.  */
108   const char *name;
109   /* Type.  */
110   debug_type type;
111   /* Kind.  */
112   enum debug_var_kind kind;
113   /* Value.  */
114   bfd_vma val;
115 };
116
117 /* A list of these structures is used to hold the types for a single
118    file.  */
119
120 struct stab_types
121 {
122   /* Next set of slots for this file.  */
123   struct stab_types *next;
124   /* Types indexed by type number.  */
125 #define STAB_TYPES_SLOTS (16)
126   debug_type types[STAB_TYPES_SLOTS];
127 };
128
129 /* We keep a list of undefined tags that we encounter, so that we can
130    fill them in if the tag is later defined.  */
131
132 struct stab_tag
133 {
134   /* Next undefined tag.  */
135   struct stab_tag *next;
136   /* Tag name.  */
137   const char *name;
138   /* Type kind.  */
139   enum debug_type_kind kind;
140   /* Slot to hold real type when we discover it.  If we don't, we fill
141      in an undefined tag type.  */
142   debug_type slot;
143   /* Indirect type we have created to point at slot.  */
144   debug_type type;
145 };
146
147 static char *savestring (const char *, int);
148
149 static void bad_stab (const char *);
150 static void warn_stab (const char *, const char *);
151 static bfd_boolean parse_stab_string
152   (void *, struct stab_handle *, int, int, bfd_vma,
153    const char *, const char *);
154 static debug_type parse_stab_type
155   (void *, struct stab_handle *, const char *, const char **,
156    debug_type **, const char *);
157 static bfd_boolean parse_stab_type_number
158   (const char **, int *, const char *);
159 static debug_type parse_stab_range_type
160   (void *, struct stab_handle *, const char *, const char **,
161    const int *, const char *);
162 static debug_type parse_stab_sun_builtin_type
163   (void *, const char **, const char *);
164 static debug_type parse_stab_sun_floating_type
165   (void *, const char **, const char *);
166 static debug_type parse_stab_enum_type
167   (void *, const char **, const char *);
168 static debug_type parse_stab_struct_type
169   (void *, struct stab_handle *, const char *, const char **,
170    bfd_boolean, const int *, const char *);
171 static bfd_boolean parse_stab_baseclasses
172   (void *, struct stab_handle *, const char **, debug_baseclass **,
173    const char *);
174 static bfd_boolean parse_stab_struct_fields
175   (void *, struct stab_handle *, const char **, debug_field **,
176    bfd_boolean *, const char *);
177 static bfd_boolean parse_stab_cpp_abbrev
178   (void *, struct stab_handle *, const char **, debug_field *, const char *);
179 static bfd_boolean parse_stab_one_struct_field
180   (void *, struct stab_handle *, const char **, const char *,
181    debug_field *, bfd_boolean *, const char *);
182 static bfd_boolean parse_stab_members
183   (void *, struct stab_handle *, const char *, const char **, const int *,
184    debug_method **, const char *);
185 static debug_type parse_stab_argtypes
186   (void *, struct stab_handle *, debug_type, const char *, const char *,
187    debug_type, const char *, bfd_boolean, bfd_boolean, const char **);
188 static bfd_boolean parse_stab_tilde_field
189   (void *, struct stab_handle *, const char **, const int *, debug_type *,
190    bfd_boolean *, const char *);
191 static debug_type parse_stab_array_type
192   (void *, struct stab_handle *, const char **, bfd_boolean, const char *);
193 static void push_bincl (struct stab_handle *, const char *, bfd_vma);
194 static const char *pop_bincl (struct stab_handle *);
195 static bfd_boolean find_excl (struct stab_handle *, const char *, bfd_vma);
196 static bfd_boolean stab_record_variable
197   (void *, struct stab_handle *, const char *, debug_type,
198    enum debug_var_kind, bfd_vma);
199 static bfd_boolean stab_emit_pending_vars (void *, struct stab_handle *);
200 static debug_type *stab_find_slot (struct stab_handle *, const int *);
201 static debug_type stab_find_type (void *, struct stab_handle *, const int *);
202 static bfd_boolean stab_record_type
203   (void *, struct stab_handle *, const int *, debug_type);
204 static debug_type stab_xcoff_builtin_type
205   (void *, struct stab_handle *, int);
206 static debug_type stab_find_tagged_type
207   (void *, struct stab_handle *, const char *, int, enum debug_type_kind);
208 static debug_type *stab_demangle_argtypes
209   (void *, struct stab_handle *, const char *, bfd_boolean *, unsigned int);
210 static debug_type *stab_demangle_v3_argtypes
211   (void *, struct stab_handle *, const char *, bfd_boolean *);
212 static debug_type *stab_demangle_v3_arglist
213   (void *, struct stab_handle *, struct demangle_component *, bfd_boolean *);
214 static debug_type stab_demangle_v3_arg
215   (void *, struct stab_handle *, struct demangle_component *, debug_type,
216    bfd_boolean *);
217
218 static int demangle_flags = DMGL_ANSI;
219
220 /* Save a string in memory.  */
221
222 static char *
223 savestring (const char *start, int len)
224 {
225   char *ret;
226
227   ret = (char *) xmalloc (len + 1);
228   memcpy (ret, start, len);
229   ret[len] = '\0';
230   return ret;
231 }
232
233 /* Read a number from a string.  */
234
235 static bfd_vma
236 parse_number (const char **pp, bfd_boolean *poverflow, const char *p_end)
237 {
238   unsigned long ul;
239   const char *orig;
240
241   if (poverflow != NULL)
242     *poverflow = FALSE;
243
244   orig = *pp;
245   if (orig >= p_end)
246     return (bfd_vma) 0;
247
248   /* Stop early if we are passed an empty string.  */
249   if (*orig == 0)
250     return (bfd_vma) 0;
251
252   errno = 0;
253   ul = strtoul (*pp, (char **) pp, 0);
254   if (ul + 1 != 0 || errno == 0)
255     {
256       /* If bfd_vma is larger than unsigned long, and the number is
257          meant to be negative, we have to make sure that we sign
258          extend properly.  */
259       if (*orig == '-')
260         return (bfd_vma) (bfd_signed_vma) (long) ul;
261       return (bfd_vma) ul;
262     }
263
264   /* Note that even though strtoul overflowed, it should have set *pp
265      to the end of the number, which is where we want it.  */
266   if (sizeof (bfd_vma) > sizeof (unsigned long))
267     {
268       const char *p;
269       bfd_boolean neg;
270       int base;
271       bfd_vma over, lastdig;
272       bfd_boolean overflow;
273       bfd_vma v;
274
275       /* Our own version of strtoul, for a bfd_vma.  */
276       p = orig;
277
278       neg = FALSE;
279       if (*p == '+')
280         ++p;
281       else if (*p == '-')
282         {
283           neg = TRUE;
284           ++p;
285         }
286
287       base = 10;
288       if (*p == '0')
289         {
290           if (p[1] == 'x' || p[1] == 'X')
291             {
292               base = 16;
293               p += 2;
294             }
295           else
296             {
297               base = 8;
298               ++p;
299             }
300         }
301
302       over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
303       lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
304
305       overflow = FALSE;
306       v = 0;
307       while (1)
308         {
309           int d;
310
311           d = *p++;
312           if (ISDIGIT (d))
313             d -= '0';
314           else if (ISUPPER (d))
315             d -= 'A';
316           else if (ISLOWER (d))
317             d -= 'a';
318           else
319             break;
320
321           if (d >= base)
322             break;
323
324           if (v > over || (v == over && (bfd_vma) d > lastdig))
325             {
326               overflow = TRUE;
327               break;
328             }
329         }
330
331       if (! overflow)
332         {
333           if (neg)
334             v = - v;
335           return v;
336         }
337     }
338
339   /* If we get here, the number is too large to represent in a
340      bfd_vma.  */
341   if (poverflow != NULL)
342     *poverflow = TRUE;
343   else
344     warn_stab (orig, _("numeric overflow"));
345
346   return 0;
347 }
348
349 /* Give an error for a bad stab string.  */
350
351 static void
352 bad_stab (const char *p)
353 {
354   fprintf (stderr, _("Bad stab: %s\n"), p);
355 }
356
357 /* Warn about something in a stab string.  */
358
359 static void
360 warn_stab (const char *p, const char *err)
361 {
362   fprintf (stderr, _("Warning: %s: %s\n"), err, p);
363 }
364
365 /* Create a handle to parse stabs symbols with.  */
366
367 void *
368 start_stab (void *dhandle ATTRIBUTE_UNUSED, bfd *abfd, bfd_boolean sections,
369             asymbol **syms, long symcount)
370 {
371   struct stab_handle *ret;
372
373   ret = (struct stab_handle *) xmalloc (sizeof *ret);
374   memset (ret, 0, sizeof *ret);
375   ret->abfd = abfd;
376   ret->sections = sections;
377   ret->syms = syms;
378   ret->symcount = symcount;
379   ret->files = 1;
380   ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
381   ret->file_types[0] = NULL;
382   ret->function_end = (bfd_vma) -1;
383   return (void *) ret;
384 }
385
386 /* When we have processed all the stabs information, we need to go
387    through and fill in all the undefined tags.  */
388
389 bfd_boolean
390 finish_stab (void *dhandle, void *handle)
391 {
392   struct stab_handle *info = (struct stab_handle *) handle;
393   struct stab_tag *st;
394
395   if (info->within_function)
396     {
397       if (! stab_emit_pending_vars (dhandle, info)
398           || ! debug_end_function (dhandle, info->function_end))
399         return FALSE;
400       info->within_function = FALSE;
401       info->function_end = (bfd_vma) -1;
402     }
403
404   for (st = info->tags; st != NULL; st = st->next)
405     {
406       enum debug_type_kind kind;
407
408       kind = st->kind;
409       if (kind == DEBUG_KIND_ILLEGAL)
410         kind = DEBUG_KIND_STRUCT;
411       st->slot = debug_make_undefined_tagged_type (dhandle, st->name, kind);
412       if (st->slot == DEBUG_TYPE_NULL)
413         return FALSE;
414     }
415
416   return TRUE;
417 }
418
419 /* Handle a single stabs symbol.  */
420
421 bfd_boolean
422 parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value,
423             const char *string)
424 {
425   const char * string_end;
426   struct stab_handle *info = (struct stab_handle *) handle;
427
428   /* gcc will emit two N_SO strings per compilation unit, one for the
429      directory name and one for the file name.  We just collect N_SO
430      strings as we see them, and start the new compilation unit when
431      we see a non N_SO symbol.  */
432   if (info->so_string != NULL
433       && (type != N_SO || *string == '\0' || value != info->so_value))
434     {
435       if (! debug_set_filename (dhandle, info->so_string))
436         return FALSE;
437       info->main_filename = info->so_string;
438
439       info->gcc_compiled = 0;
440       info->n_opt_found = FALSE;
441
442       /* Generally, for stabs in the symbol table, the N_LBRAC and
443          N_RBRAC symbols are relative to the N_SO symbol value.  */
444       if (! info->sections)
445         info->file_start_offset = info->so_value;
446
447       /* We need to reset the mapping from type numbers to types.  We
448          can't free the old mapping, because of the use of
449          debug_make_indirect_type.  */
450       info->files = 1;
451       info->file_types = ((struct stab_types **)
452                           xmalloc (sizeof *info->file_types));
453       info->file_types[0] = NULL;
454       info->so_string = NULL;
455
456       /* Now process whatever type we just got.  */
457     }
458
459   string_end = string + strlen (string);
460
461   switch (type)
462     {
463     case N_FN:
464     case N_FN_SEQ:
465       break;
466
467     case N_LBRAC:
468       /* Ignore extra outermost context from SunPRO cc and acc.  */
469       if (info->n_opt_found && desc == 1)
470         break;
471
472       if (! info->within_function)
473         {
474           fprintf (stderr, _("N_LBRAC not within function\n"));
475           return FALSE;
476         }
477
478       /* Start an inner lexical block.  */
479       if (! debug_start_block (dhandle,
480                                (value
481                                 + info->file_start_offset
482                                 + info->function_start_offset)))
483         return FALSE;
484
485       /* Emit any pending variable definitions.  */
486       if (! stab_emit_pending_vars (dhandle, info))
487         return FALSE;
488
489       ++info->block_depth;
490       break;
491
492     case N_RBRAC:
493       /* Ignore extra outermost context from SunPRO cc and acc.  */
494       if (info->n_opt_found && desc == 1)
495         break;
496
497       /* We shouldn't have any pending variable definitions here, but,
498          if we do, we probably need to emit them before closing the
499          block.  */
500       if (! stab_emit_pending_vars (dhandle, info))
501         return FALSE;
502
503       /* End an inner lexical block.  */
504       if (! debug_end_block (dhandle,
505                              (value
506                               + info->file_start_offset
507                               + info->function_start_offset)))
508         return FALSE;
509
510       --info->block_depth;
511       if (info->block_depth < 0)
512         {
513           fprintf (stderr, _("Too many N_RBRACs\n"));
514           return FALSE;
515         }
516       break;
517
518     case N_SO:
519       /* This always ends a function.  */
520       if (info->within_function)
521         {
522           bfd_vma endval;
523
524           endval = value;
525           if (*string != '\0'
526               && info->function_end != (bfd_vma) -1
527               && info->function_end < endval)
528             endval = info->function_end;
529           if (! stab_emit_pending_vars (dhandle, info)
530               || ! debug_end_function (dhandle, endval))
531             return FALSE;
532           info->within_function = FALSE;
533           info->function_end = (bfd_vma) -1;
534         }
535
536       /* An empty string is emitted by gcc at the end of a compilation
537          unit.  */
538       if (*string == '\0')
539         return TRUE;
540
541       /* Just accumulate strings until we see a non N_SO symbol.  If
542          the string starts with a directory separator or some other
543          form of absolute path specification, we discard the previously
544          accumulated strings.  */
545       if (info->so_string == NULL)
546         info->so_string = xstrdup (string);
547       else
548         {
549           char *f;
550
551           f = info->so_string;
552
553           if (IS_ABSOLUTE_PATH (string))
554             info->so_string = xstrdup (string);
555           else
556             info->so_string = concat (info->so_string, string,
557                                       (const char *) NULL);
558           free (f);
559         }
560
561       info->so_value = value;
562
563       break;
564
565     case N_SOL:
566       /* Start an include file.  */
567       if (! debug_start_source (dhandle, string))
568         return FALSE;
569       break;
570
571     case N_BINCL:
572       /* Start an include file which may be replaced.  */
573       push_bincl (info, string, value);
574       if (! debug_start_source (dhandle, string))
575         return FALSE;
576       break;
577
578     case N_EINCL:
579       /* End an N_BINCL include.  */
580       if (! debug_start_source (dhandle, pop_bincl (info)))
581         return FALSE;
582       break;
583
584     case N_EXCL:
585       /* This is a duplicate of a header file named by N_BINCL which
586          was eliminated by the linker.  */
587       if (! find_excl (info, string, value))
588         return FALSE;
589       break;
590
591     case N_SLINE:
592       if (! debug_record_line (dhandle, desc,
593                                value + (info->within_function
594                                         ? info->function_start_offset : 0)))
595         return FALSE;
596       break;
597
598     case N_BCOMM:
599       if (! debug_start_common_block (dhandle, string))
600         return FALSE;
601       break;
602
603     case N_ECOMM:
604       if (! debug_end_common_block (dhandle, string))
605         return FALSE;
606       break;
607
608     case N_FUN:
609       if (*string == '\0')
610         {
611           if (info->within_function)
612             {
613               /* This always marks the end of a function; we don't
614                  need to worry about info->function_end.  */
615               if (info->sections)
616                 value += info->function_start_offset;
617               if (! stab_emit_pending_vars (dhandle, info)
618                   || ! debug_end_function (dhandle, value))
619                 return FALSE;
620               info->within_function = FALSE;
621               info->function_end = (bfd_vma) -1;
622             }
623           break;
624         }
625
626       /* A const static symbol in the .text section will have an N_FUN
627          entry.  We need to use these to mark the end of the function,
628          in case we are looking at gcc output before it was changed to
629          always emit an empty N_FUN.  We can't call debug_end_function
630          here, because it might be a local static symbol.  */
631       if (info->within_function
632           && (info->function_end == (bfd_vma) -1
633               || value < info->function_end))
634         info->function_end = value;
635
636       /* Fall through.  */
637       /* FIXME: gdb checks the string for N_STSYM, N_LCSYM or N_ROSYM
638          symbols, and if it does not start with :S, gdb relocates the
639          value to the start of the section.  gcc always seems to use
640          :S, so we don't worry about this.  */
641       /* Fall through.  */
642     default:
643       {
644         const char *colon;
645
646         colon = strchr (string, ':');
647         if (colon != NULL
648             && (colon[1] == 'f' || colon[1] == 'F'))
649           {
650             if (info->within_function)
651               {
652                 bfd_vma endval;
653
654                 endval = value;
655                 if (info->function_end != (bfd_vma) -1
656                     && info->function_end < endval)
657                   endval = info->function_end;
658                 if (! stab_emit_pending_vars (dhandle, info)
659                     || ! debug_end_function (dhandle, endval))
660                   return FALSE;
661                 info->function_end = (bfd_vma) -1;
662               }
663             /* For stabs in sections, line numbers and block addresses
664                are offsets from the start of the function.  */
665             if (info->sections)
666               info->function_start_offset = value;
667             info->within_function = TRUE;
668           }
669
670         if (! parse_stab_string (dhandle, info, type, desc, value, string, string_end))
671           return FALSE;
672       }
673       break;
674
675     case N_OPT:
676       if (string != NULL && strcmp (string, "gcc2_compiled.") == 0)
677         info->gcc_compiled = 2;
678       else if (string != NULL && strcmp (string, "gcc_compiled.") == 0)
679         info->gcc_compiled = 1;
680       else
681         info->n_opt_found = TRUE;
682       break;
683
684     case N_OBJ:
685     case N_ENDM:
686     case N_MAIN:
687     case N_WARNING:
688       break;
689     }
690
691   return TRUE;
692 }
693
694 /* Parse the stabs string.  */
695
696 static bfd_boolean
697 parse_stab_string (void *dhandle, struct stab_handle *info, int stabtype,
698                    int desc ATTRIBUTE_UNUSED, bfd_vma value,
699                    const char *string, const char * string_end)
700 {
701   const char *p;
702   char *name;
703   int type;
704   debug_type dtype;
705   bfd_boolean synonym;
706   bfd_boolean self_crossref;
707   debug_type *slot;
708
709   p = strchr (string, ':');
710   if (p == NULL)
711     return TRUE;
712
713   while (p[1] == ':')
714     {
715       p += 2;
716       p = strchr (p, ':');
717       if (p == NULL)
718         {
719           bad_stab (string);
720           return FALSE;
721         }
722     }
723
724   /* FIXME: Sometimes the special C++ names start with '.'.  */
725   name = NULL;
726   if (string[0] == '$')
727     {
728       switch (string[1])
729         {
730         case 't':
731           name = "this";
732           break;
733         case 'v':
734           /* Was: name = "vptr"; */
735           break;
736         case 'e':
737           name = "eh_throw";
738           break;
739         case '_':
740           /* This was an anonymous type that was never fixed up.  */
741           break;
742         case 'X':
743           /* SunPRO (3.0 at least) static variable encoding.  */
744           break;
745         default:
746           warn_stab (string, _("unknown C++ encoded name"));
747           break;
748         }
749     }
750
751   if (name == NULL)
752     {
753       if (p == string || (string[0] == ' ' && p == string + 1))
754         name = NULL;
755       else
756         name = savestring (string, p - string);
757     }
758
759   ++p;
760   if (ISDIGIT (*p) || *p == '(' || *p == '-')
761     type = 'l';
762   else if (*p == 0)
763     {
764       bad_stab (string);
765       return FALSE;
766     }
767   else
768     type = *p++;
769
770   switch (type)
771     {
772     case 'c':
773       /* c is a special case, not followed by a type-number.
774          SYMBOL:c=iVALUE for an integer constant symbol.
775          SYMBOL:c=rVALUE for a floating constant symbol.
776          SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
777          e.g. "b:c=e6,0" for "const b = blob1"
778          (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
779       if (*p != '=')
780         {
781           bad_stab (string);
782           return FALSE;
783         }
784       ++p;
785       switch (*p++)
786         {
787         case 'r':
788           /* Floating point constant.  */
789           if (! debug_record_float_const (dhandle, name, atof (p)))
790             return FALSE;
791           break;
792         case 'i':
793           /* Integer constant.  */
794           /* Defining integer constants this way is kind of silly,
795              since 'e' constants allows the compiler to give not only
796              the value, but the type as well.  C has at least int,
797              long, unsigned int, and long long as constant types;
798              other languages probably should have at least unsigned as
799              well as signed constants.  */
800           if (! debug_record_int_const (dhandle, name, atoi (p)))
801             return FALSE;
802           break;
803         case 'e':
804           /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
805              can be represented as integral.
806              e.g. "b:c=e6,0" for "const b = blob1"
807              (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
808           dtype = parse_stab_type (dhandle, info, (const char *) NULL,
809                                    &p, (debug_type **) NULL, string_end);
810           if (dtype == DEBUG_TYPE_NULL)
811             return FALSE;
812           if (*p != ',')
813             {
814               bad_stab (string);
815               return FALSE;
816             }
817           if (! debug_record_typed_const (dhandle, name, dtype, atoi (p)))
818             return FALSE;
819           break;
820         default:
821           bad_stab (string);
822           return FALSE;
823         }
824
825       break;
826
827     case 'C':
828       /* The name of a caught exception.  */
829       dtype = parse_stab_type (dhandle, info, (const char *) NULL,
830                                &p, (debug_type **) NULL, string_end);
831       if (dtype == DEBUG_TYPE_NULL)
832         return FALSE;
833       if (! debug_record_label (dhandle, name, dtype, value))
834         return FALSE;
835       break;
836
837     case 'f':
838     case 'F':
839       /* A function definition.  */
840       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
841                                (debug_type **) NULL, string_end);
842       if (dtype == DEBUG_TYPE_NULL)
843         return FALSE;
844       if (! debug_record_function (dhandle, name, dtype, type == 'F', value))
845         return FALSE;
846
847       /* Sun acc puts declared types of arguments here.  We don't care
848          about their actual types (FIXME -- we should remember the whole
849          function prototype), but the list may define some new types
850          that we have to remember, so we must scan it now.  */
851       while (*p == ';')
852         {
853           ++p;
854           if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
855                                (debug_type **) NULL, string_end)
856               == DEBUG_TYPE_NULL)
857             return FALSE;
858         }
859
860       break;
861
862     case 'G':
863       {
864         asymbol **ps;
865
866         /* A global symbol.  The value must be extracted from the
867            symbol table.  */
868         dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
869                                  (debug_type **) NULL, string_end);
870         if (dtype == DEBUG_TYPE_NULL)
871           return FALSE;
872         if (name != NULL)
873           {
874             char leading;
875             long c;
876
877             leading = bfd_get_symbol_leading_char (info->abfd);
878             for (c = info->symcount, ps = info->syms; c > 0; --c, ++ps)
879               {
880                 const char *n;
881
882                 n = bfd_asymbol_name (*ps);
883                 if (leading != '\0' && *n == leading)
884                   ++n;
885                 if (*n == *name && strcmp (n, name) == 0)
886                   break;
887               }
888
889             if (c > 0)
890               value = bfd_asymbol_value (*ps);
891           }
892
893         if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_GLOBAL,
894                                     value))
895           return FALSE;
896       }
897       break;
898
899       /* This case is faked by a conditional above, when there is no
900          code letter in the dbx data.  Dbx data never actually
901          contains 'l'.  */
902     case 'l':
903     case 's':
904       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
905                                (debug_type **) NULL, string_end);
906       if (dtype == DEBUG_TYPE_NULL)
907         return FALSE;
908       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
909                                   value))
910         return FALSE;
911       break;
912
913     case 'p':
914       /* A function parameter.  */
915       if (*p != 'F')
916         dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
917                                  (debug_type **) NULL, string_end);
918       else
919         {
920         /* pF is a two-letter code that means a function parameter in
921            Fortran.  The type-number specifies the type of the return
922            value.  Translate it into a pointer-to-function type.  */
923           ++p;
924           dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
925                                    (debug_type **) NULL, string_end);
926           if (dtype != DEBUG_TYPE_NULL)
927             {
928               debug_type ftype;
929
930               ftype = debug_make_function_type (dhandle, dtype,
931                                                 (debug_type *) NULL, FALSE);
932               dtype = debug_make_pointer_type (dhandle, ftype);
933             }
934         }
935       if (dtype == DEBUG_TYPE_NULL)
936         return FALSE;
937       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_STACK,
938                                     value))
939         return FALSE;
940
941       /* FIXME: At this point gdb considers rearranging the parameter
942          address on a big endian machine if it is smaller than an int.
943          We have no way to do that, since we don't really know much
944          about the target.  */
945       break;
946
947     case 'P':
948       if (stabtype == N_FUN)
949         {
950           /* Prototype of a function referenced by this file.  */
951           while (*p == ';')
952             {
953               ++p;
954               if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
955                                    (debug_type **) NULL, string_end)
956                   == DEBUG_TYPE_NULL)
957                 return FALSE;
958             }
959           break;
960         }
961       /* Fall through.  */
962     case 'R':
963       /* Parameter which is in a register.  */
964       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
965                                (debug_type **) NULL, string_end);
966       if (dtype == DEBUG_TYPE_NULL)
967         return FALSE;
968       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REG,
969                                     value))
970         return FALSE;
971       break;
972
973     case 'r':
974       /* Register variable (either global or local).  */
975       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
976                                (debug_type **) NULL, string_end);
977       if (dtype == DEBUG_TYPE_NULL)
978         return FALSE;
979       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_REGISTER,
980                                   value))
981         return FALSE;
982
983       /* FIXME: At this point gdb checks to combine pairs of 'p' and
984          'r' stabs into a single 'P' stab.  */
985       break;
986
987     case 'S':
988       /* Static symbol at top level of file.  */
989       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
990                                (debug_type **) NULL, string_end);
991       if (dtype == DEBUG_TYPE_NULL)
992         return FALSE;
993       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_STATIC,
994                                   value))
995         return FALSE;
996       break;
997
998     case 't':
999       /* A typedef.  */
1000       dtype = parse_stab_type (dhandle, info, name, &p, &slot, string_end);
1001       if (dtype == DEBUG_TYPE_NULL)
1002         return FALSE;
1003       if (name == NULL)
1004         {
1005           /* A nameless type.  Nothing to do.  */
1006           return TRUE;
1007         }
1008
1009       dtype = debug_name_type (dhandle, name, dtype);
1010       if (dtype == DEBUG_TYPE_NULL)
1011         return FALSE;
1012
1013       if (slot != NULL)
1014         *slot = dtype;
1015
1016       break;
1017
1018     case 'T':
1019       /* Struct, union, or enum tag.  For GNU C++, this can be followed
1020          by 't' which means we are typedef'ing it as well.  */
1021       if (*p != 't')
1022         {
1023           synonym = FALSE;
1024           /* FIXME: gdb sets synonym to TRUE if the current language
1025              is C++.  */
1026         }
1027       else
1028         {
1029           synonym = TRUE;
1030           ++p;
1031         }
1032
1033       dtype = parse_stab_type (dhandle, info, name, &p, &slot, string_end);
1034       if (dtype == DEBUG_TYPE_NULL)
1035         return FALSE;
1036       if (name == NULL)
1037         return TRUE;
1038
1039       /* INFO->SELF_CROSSREF is set by parse_stab_type if this type is
1040          a cross reference to itself.  These are generated by some
1041          versions of g++.  */
1042       self_crossref = info->self_crossref;
1043
1044       dtype = debug_tag_type (dhandle, name, dtype);
1045       if (dtype == DEBUG_TYPE_NULL)
1046         return FALSE;
1047       if (slot != NULL)
1048         *slot = dtype;
1049
1050       /* See if we have a cross reference to this tag which we can now
1051          fill in.  Avoid filling in a cross reference to ourselves,
1052          because that would lead to circular debugging information.  */
1053       if (! self_crossref)
1054         {
1055           register struct stab_tag **pst;
1056
1057           for (pst = &info->tags; *pst != NULL; pst = &(*pst)->next)
1058             {
1059               if ((*pst)->name[0] == name[0]
1060                   && strcmp ((*pst)->name, name) == 0)
1061                 {
1062                   (*pst)->slot = dtype;
1063                   *pst = (*pst)->next;
1064                   break;
1065                 }
1066             }
1067         }
1068
1069       if (synonym)
1070         {
1071           dtype = debug_name_type (dhandle, name, dtype);
1072           if (dtype == DEBUG_TYPE_NULL)
1073             return FALSE;
1074
1075           if (slot != NULL)
1076             *slot = dtype;
1077         }
1078
1079       break;
1080
1081     case 'V':
1082       /* Static symbol of local scope */
1083       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1084                                (debug_type **) NULL, string_end);
1085       if (dtype == DEBUG_TYPE_NULL)
1086         return FALSE;
1087       /* FIXME: gdb checks os9k_stabs here.  */
1088       if (! stab_record_variable (dhandle, info, name, dtype,
1089                                   DEBUG_LOCAL_STATIC, value))
1090         return FALSE;
1091       break;
1092
1093     case 'v':
1094       /* Reference parameter.  */
1095       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1096                                (debug_type **) NULL, string_end);
1097       if (dtype == DEBUG_TYPE_NULL)
1098         return FALSE;
1099       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REFERENCE,
1100                                     value))
1101         return FALSE;
1102       break;
1103
1104     case 'a':
1105       /* Reference parameter which is in a register.  */
1106       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1107                                (debug_type **) NULL, string_end);
1108       if (dtype == DEBUG_TYPE_NULL)
1109         return FALSE;
1110       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REF_REG,
1111                                     value))
1112         return FALSE;
1113       break;
1114
1115     case 'X':
1116       /* This is used by Sun FORTRAN for "function result value".
1117          Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1118          that Pascal uses it too, but when I tried it Pascal used
1119          "x:3" (local symbol) instead.  */
1120       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1121                                (debug_type **) NULL, string_end);
1122       if (dtype == DEBUG_TYPE_NULL)
1123         return FALSE;
1124       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
1125                                   value))
1126         return FALSE;
1127       break;
1128
1129     case 'Y':
1130       /* SUNPro C++ Namespace =Yn0.  */
1131       /* Skip the namespace mapping, as it is not used now.  */
1132       if (*(++p) == 'n' && *(++p) == '0')
1133         {
1134           /* =Yn0name; */
1135           while (*p != ';')
1136             ++p;
1137           ++p;
1138           return TRUE;
1139         }
1140       /* TODO SUNPro C++ support:
1141          Support default arguments after F,P parameters
1142          Ya = Anonymous unions
1143          YM,YD = Pointers to class members
1144          YT,YI = Templates
1145          YR = Run-time type information (RTTI)  */
1146
1147       /* Fall through.  */
1148
1149     default:
1150       bad_stab (string);
1151       return FALSE;
1152     }
1153
1154   /* FIXME: gdb converts structure values to structure pointers in a
1155      couple of cases, depending upon the target.  */
1156
1157   return TRUE;
1158 }
1159
1160 /* Parse a stabs type.  The typename argument is non-NULL if this is a
1161    typedef or a tag definition.  The pp argument points to the stab
1162    string, and is updated.  The slotp argument points to a place to
1163    store the slot used if the type is being defined.  */
1164
1165 static debug_type
1166 parse_stab_type (void *                dhandle,
1167                  struct stab_handle *  info,
1168                  const char *          type_name,
1169                  const char **         pp,
1170                  debug_type **         slotp,
1171                  const char *          p_end)
1172 {
1173   const char *orig;
1174   int typenums[2];
1175   int size;
1176   bfd_boolean stringp;
1177   int descriptor;
1178   debug_type dtype;
1179
1180   if (slotp != NULL)
1181     *slotp = NULL;
1182
1183   orig = *pp;
1184   if (orig >= p_end)
1185     return DEBUG_TYPE_NULL;
1186
1187   size = -1;
1188   stringp = FALSE;
1189
1190   info->self_crossref = FALSE;
1191
1192   /* Read type number if present.  The type number may be omitted.
1193      for instance in a two-dimensional array declared with type
1194      "ar1;1;10;ar1;1;10;4".  */
1195   if (! ISDIGIT (**pp) && **pp != '(' && **pp != '-')
1196     {
1197       /* 'typenums=' not present, type is anonymous.  Read and return
1198          the definition, but don't put it in the type vector.  */
1199       typenums[0] = typenums[1] = -1;
1200     }
1201   else
1202     {
1203       if (! parse_stab_type_number (pp, typenums, p_end))
1204         return DEBUG_TYPE_NULL;
1205
1206       if (**pp != '=')
1207         /* Type is not being defined here.  Either it already
1208            exists, or this is a forward reference to it.  */
1209         return stab_find_type (dhandle, info, typenums);
1210
1211       /* Only set the slot if the type is being defined.  This means
1212          that the mapping from type numbers to types will only record
1213          the name of the typedef which defines a type.  If we don't do
1214          this, then something like
1215              typedef int foo;
1216              int i;
1217          will record that i is of type foo.  Unfortunately, stabs
1218          information is ambiguous about variable types.  For this code,
1219              typedef int foo;
1220              int i;
1221              foo j;
1222          the stabs information records both i and j as having the same
1223          type.  This could be fixed by patching the compiler.  */
1224       if (slotp != NULL && typenums[0] >= 0 && typenums[1] >= 0)
1225         *slotp = stab_find_slot (info, typenums);
1226
1227       /* Type is being defined here.  */
1228       /* Skip the '='.  */
1229       ++*pp;
1230
1231       while (**pp == '@')
1232         {
1233           const char *p = *pp + 1;
1234           const char *attr;
1235
1236           if (ISDIGIT (*p) || *p == '(' || *p == '-')
1237             /* Member type.  */
1238             break;
1239
1240           /* Type attributes.  */
1241           attr = p;
1242
1243           for (; *p != ';'; ++p)
1244             {
1245               if (*p == '\0')
1246                 {
1247                   bad_stab (orig);
1248                   return DEBUG_TYPE_NULL;
1249                 }
1250             }
1251           *pp = p + 1;
1252
1253           switch (*attr)
1254             {
1255             case 's':
1256               size = atoi (attr + 1);
1257               size /= 8;  /* Size is in bits.  We store it in bytes.  */
1258               if (size <= 0)
1259                 size = -1;
1260               break;
1261
1262             case 'S':
1263               stringp = TRUE;
1264               break;
1265
1266             case 0:
1267               bad_stab (orig);
1268               return DEBUG_TYPE_NULL;
1269
1270             default:
1271               /* Ignore unrecognized type attributes, so future
1272                  compilers can invent new ones.  */
1273               break;
1274             }
1275         }
1276     }
1277
1278   descriptor = **pp;
1279   ++*pp;
1280
1281   switch (descriptor)
1282     {
1283     case 'x':
1284       {
1285         enum debug_type_kind code;
1286         const char *q1, *q2, *p;
1287
1288         /* A cross reference to another type.  */
1289         switch (**pp)
1290           {
1291           case 's':
1292             code = DEBUG_KIND_STRUCT;
1293             break;
1294           case 'u':
1295             code = DEBUG_KIND_UNION;
1296             break;
1297           case 'e':
1298             code = DEBUG_KIND_ENUM;
1299             break;
1300           case 0:
1301               bad_stab (orig);
1302               return DEBUG_TYPE_NULL;
1303             
1304           default:
1305             /* Complain and keep going, so compilers can invent new
1306                cross-reference types.  */
1307             warn_stab (orig, _("unrecognized cross reference type"));
1308             code = DEBUG_KIND_STRUCT;
1309             break;
1310           }
1311         ++*pp;
1312
1313         q1 = strchr (*pp, '<');
1314         p = strchr (*pp, ':');
1315         if (p == NULL)
1316           {
1317             bad_stab (orig);
1318             return DEBUG_TYPE_NULL;
1319           }
1320         if (q1 != NULL && p > q1 && p[1] == ':')
1321           {
1322             int nest = 0;
1323
1324             for (q2 = q1; *q2 != '\0'; ++q2)
1325               {
1326                 if (*q2 == '<')
1327                   ++nest;
1328                 else if (*q2 == '>')
1329                   --nest;
1330                 else if (*q2 == ':' && nest == 0)
1331                   break;
1332               }
1333             p = q2;
1334             if (*p != ':')
1335               {
1336                 bad_stab (orig);
1337                 return DEBUG_TYPE_NULL;
1338               }
1339           }
1340
1341         /* Some versions of g++ can emit stabs like
1342                fleep:T20=xsfleep:
1343            which define structures in terms of themselves.  We need to
1344            tell the caller to avoid building a circular structure.  */
1345         if (type_name != NULL
1346             && strncmp (type_name, *pp, p - *pp) == 0
1347             && type_name[p - *pp] == '\0')
1348           info->self_crossref = TRUE;
1349
1350         dtype = stab_find_tagged_type (dhandle, info, *pp, p - *pp, code);
1351
1352         *pp = p + 1;
1353       }
1354       break;
1355
1356     case '-':
1357     case '0':
1358     case '1':
1359     case '2':
1360     case '3':
1361     case '4':
1362     case '5':
1363     case '6':
1364     case '7':
1365     case '8':
1366     case '9':
1367     case '(':
1368       {
1369         const char *hold;
1370         int xtypenums[2];
1371
1372         /* This type is defined as another type.  */
1373         (*pp)--;
1374         hold = *pp;
1375
1376         /* Peek ahead at the number to detect void.  */
1377         if (! parse_stab_type_number (pp, xtypenums, p_end))
1378           return DEBUG_TYPE_NULL;
1379
1380         if (typenums[0] == xtypenums[0] && typenums[1] == xtypenums[1])
1381           {
1382             /* This type is being defined as itself, which means that
1383                it is void.  */
1384             dtype = debug_make_void_type (dhandle);
1385           }
1386         else
1387           {
1388             *pp = hold;
1389
1390             /* Go back to the number and have parse_stab_type get it.
1391                This means that we can deal with something like
1392                t(1,2)=(3,4)=... which the Lucid compiler uses.  */
1393             dtype = parse_stab_type (dhandle, info, (const char *) NULL,
1394                                      pp, (debug_type **) NULL, p_end);
1395             if (dtype == DEBUG_TYPE_NULL)
1396               return DEBUG_TYPE_NULL;
1397           }
1398
1399         if (typenums[0] != -1)
1400           {
1401             if (! stab_record_type (dhandle, info, typenums, dtype))
1402               return DEBUG_TYPE_NULL;
1403           }
1404
1405         break;
1406       }
1407
1408     case '*':
1409       dtype = debug_make_pointer_type (dhandle,
1410                                        parse_stab_type (dhandle, info,
1411                                                         (const char *) NULL,
1412                                                         pp,
1413                                                         (debug_type **) NULL,
1414                                                         p_end));
1415       break;
1416
1417     case '&':
1418       /* Reference to another type.  */
1419       dtype = (debug_make_reference_type
1420                (dhandle,
1421                 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1422                                  (debug_type **) NULL, p_end)));
1423       break;
1424
1425     case 'f':
1426       /* Function returning another type.  */
1427       /* FIXME: gdb checks os9k_stabs here.  */
1428       dtype = (debug_make_function_type
1429                (dhandle,
1430                 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1431                                  (debug_type **) NULL, p_end),
1432                 (debug_type *) NULL, FALSE));
1433       break;
1434
1435     case 'k':
1436       /* Const qualifier on some type (Sun).  */
1437       /* FIXME: gdb accepts 'c' here if os9k_stabs.  */
1438       dtype = debug_make_const_type (dhandle,
1439                                      parse_stab_type (dhandle, info,
1440                                                       (const char *) NULL,
1441                                                       pp,
1442                                                       (debug_type **) NULL,
1443                                                       p_end));
1444       break;
1445
1446     case 'B':
1447       /* Volatile qual on some type (Sun).  */
1448       /* FIXME: gdb accepts 'i' here if os9k_stabs.  */
1449       dtype = (debug_make_volatile_type
1450                (dhandle,
1451                 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1452                                  (debug_type **) NULL, p_end)));
1453       break;
1454
1455     case '@':
1456       /* Offset (class & variable) type.  This is used for a pointer
1457          relative to an object.  */
1458       {
1459         debug_type domain;
1460         debug_type memtype;
1461
1462         /* Member type.  */
1463
1464         domain = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1465                                   (debug_type **) NULL, p_end);
1466         if (domain == DEBUG_TYPE_NULL)
1467           return DEBUG_TYPE_NULL;
1468
1469         if (**pp != ',')
1470           {
1471             bad_stab (orig);
1472             return DEBUG_TYPE_NULL;
1473           }
1474         ++*pp;
1475
1476         memtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1477                                    (debug_type **) NULL, p_end);
1478         if (memtype == DEBUG_TYPE_NULL)
1479           return DEBUG_TYPE_NULL;
1480
1481         dtype = debug_make_offset_type (dhandle, domain, memtype);
1482       }
1483       break;
1484
1485     case '#':
1486       /* Method (class & fn) type.  */
1487       if (**pp == '#')
1488         {
1489           debug_type return_type;
1490
1491           ++*pp;
1492           return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1493                                          pp, (debug_type **) NULL, p_end);
1494           if (return_type == DEBUG_TYPE_NULL)
1495             return DEBUG_TYPE_NULL;
1496           if (**pp != ';')
1497             {
1498               bad_stab (orig);
1499               return DEBUG_TYPE_NULL;
1500             }
1501           ++*pp;
1502           dtype = debug_make_method_type (dhandle, return_type,
1503                                           DEBUG_TYPE_NULL,
1504                                           (debug_type *) NULL, FALSE);
1505         }
1506       else
1507         {
1508           debug_type domain;
1509           debug_type return_type;
1510           debug_type *args;
1511           unsigned int n;
1512           unsigned int alloc;
1513           bfd_boolean varargs;
1514
1515           domain = parse_stab_type (dhandle, info, (const char *) NULL,
1516                                     pp, (debug_type **) NULL, p_end);
1517           if (domain == DEBUG_TYPE_NULL)
1518             return DEBUG_TYPE_NULL;
1519
1520           if (**pp != ',')
1521             {
1522               bad_stab (orig);
1523               return DEBUG_TYPE_NULL;
1524             }
1525           ++*pp;
1526
1527           return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1528                                          pp, (debug_type **) NULL, p_end);
1529           if (return_type == DEBUG_TYPE_NULL)
1530             return DEBUG_TYPE_NULL;
1531
1532           alloc = 10;
1533           args = (debug_type *) xmalloc (alloc * sizeof *args);
1534           n = 0;
1535           while (**pp != ';')
1536             {
1537               if (**pp != ',')
1538                 {
1539                   bad_stab (orig);
1540                   return DEBUG_TYPE_NULL;
1541                 }
1542               ++*pp;
1543
1544               if (n + 1 >= alloc)
1545                 {
1546                   alloc += 10;
1547                   args = ((debug_type *)
1548                           xrealloc (args, alloc * sizeof *args));
1549                 }
1550
1551               args[n] = parse_stab_type (dhandle, info, (const char *) NULL,
1552                                          pp, (debug_type **) NULL, p_end);
1553               if (args[n] == DEBUG_TYPE_NULL)
1554                 return DEBUG_TYPE_NULL;
1555               ++n;
1556             }
1557           ++*pp;
1558
1559           /* If the last type is not void, then this function takes a
1560              variable number of arguments.  Otherwise, we must strip
1561              the void type.  */
1562           if (n == 0
1563               || debug_get_type_kind (dhandle, args[n - 1]) != DEBUG_KIND_VOID)
1564             varargs = TRUE;
1565           else
1566             {
1567               --n;
1568               varargs = FALSE;
1569             }
1570
1571           args[n] = DEBUG_TYPE_NULL;
1572
1573           dtype = debug_make_method_type (dhandle, return_type, domain, args,
1574                                           varargs);
1575         }
1576       break;
1577
1578     case 'r':
1579       /* Range type.  */
1580       dtype = parse_stab_range_type (dhandle, info, type_name, pp, typenums, p_end);
1581       break;
1582
1583     case 'b':
1584       /* FIXME: gdb checks os9k_stabs here.  */
1585       /* Sun ACC builtin int type.  */
1586       dtype = parse_stab_sun_builtin_type (dhandle, pp, p_end);
1587       break;
1588
1589     case 'R':
1590       /* Sun ACC builtin float type.  */
1591       dtype = parse_stab_sun_floating_type (dhandle, pp, p_end);
1592       break;
1593
1594     case 'e':
1595       /* Enumeration type.  */
1596       dtype = parse_stab_enum_type (dhandle, pp, p_end);
1597       break;
1598
1599     case 's':
1600     case 'u':
1601       /* Struct or union type.  */
1602       dtype = parse_stab_struct_type (dhandle, info, type_name, pp,
1603                                       descriptor == 's', typenums, p_end);
1604       break;
1605
1606     case 'a':
1607       /* Array type.  */
1608       if (**pp != 'r')
1609         {
1610           bad_stab (orig);
1611           return DEBUG_TYPE_NULL;
1612         }
1613       ++*pp;
1614
1615       dtype = parse_stab_array_type (dhandle, info, pp, stringp, p_end);
1616       break;
1617
1618     case 'S':
1619       dtype = debug_make_set_type (dhandle,
1620                                    parse_stab_type (dhandle, info,
1621                                                     (const char *) NULL,
1622                                                     pp,
1623                                                     (debug_type **) NULL,
1624                                                     p_end),
1625                                    stringp);
1626       break;
1627
1628     default:
1629       bad_stab (orig);
1630       return DEBUG_TYPE_NULL;
1631     }
1632
1633   if (dtype == DEBUG_TYPE_NULL)
1634     return DEBUG_TYPE_NULL;
1635
1636   if (typenums[0] != -1)
1637     {
1638       if (! stab_record_type (dhandle, info, typenums, dtype))
1639         return DEBUG_TYPE_NULL;
1640     }
1641
1642   if (size != -1)
1643     {
1644       if (! debug_record_type_size (dhandle, dtype, (unsigned int) size))
1645         return DEBUG_TYPE_NULL;
1646     }
1647
1648   return dtype;
1649 }
1650
1651 /* Read a number by which a type is referred to in dbx data, or
1652    perhaps read a pair (FILENUM, TYPENUM) in parentheses.  Just a
1653    single number N is equivalent to (0,N).  Return the two numbers by
1654    storing them in the vector TYPENUMS.  */
1655
1656 static bfd_boolean
1657 parse_stab_type_number (const char **pp, int *typenums, const char *p_end)
1658 {
1659   const char *orig;
1660
1661   orig = *pp;
1662
1663   if (**pp != '(')
1664     {
1665       typenums[0] = 0;
1666       typenums[1] = (int) parse_number (pp, (bfd_boolean *) NULL, p_end);
1667       return TRUE;
1668     }
1669
1670   ++*pp;
1671   typenums[0] = (int) parse_number (pp, (bfd_boolean *) NULL, p_end);
1672   if (**pp != ',')
1673     {
1674       bad_stab (orig);
1675       return FALSE;
1676     }
1677
1678   ++*pp;
1679   typenums[1] = (int) parse_number (pp, (bfd_boolean *) NULL, p_end);
1680   if (**pp != ')')
1681     {
1682       bad_stab (orig);
1683       return FALSE;
1684     }
1685
1686   ++*pp;
1687   return TRUE;
1688 }
1689
1690 /* Parse a range type.  */
1691
1692 static debug_type
1693 parse_stab_range_type (void *                dhandle,
1694                        struct stab_handle *  info,
1695                        const char *          type_name,
1696                        const char **         pp,
1697                        const int *           typenums,
1698                        const char *          p_end)
1699 {
1700   const char *orig;
1701   int rangenums[2];
1702   bfd_boolean self_subrange;
1703   debug_type index_type;
1704   const char *s2, *s3;
1705   bfd_signed_vma n2, n3;
1706   bfd_boolean ov2, ov3;
1707
1708   orig = *pp;
1709   if (orig >= p_end)
1710     return DEBUG_TYPE_NULL;
1711
1712   index_type = DEBUG_TYPE_NULL;
1713
1714   /* First comes a type we are a subrange of.
1715      In C it is usually 0, 1 or the type being defined.  */
1716   if (! parse_stab_type_number (pp, rangenums, p_end))
1717     return DEBUG_TYPE_NULL;
1718
1719   self_subrange = (rangenums[0] == typenums[0]
1720                    && rangenums[1] == typenums[1]);
1721
1722   if (**pp == '=')
1723     {
1724       *pp = orig;
1725       index_type = parse_stab_type (dhandle, info, (const char *) NULL,
1726                                     pp, (debug_type **) NULL, p_end);
1727       if (index_type == DEBUG_TYPE_NULL)
1728         return DEBUG_TYPE_NULL;
1729     }
1730
1731   if (**pp == ';')
1732     ++*pp;
1733
1734   /* The remaining two operands are usually lower and upper bounds of
1735      the range.  But in some special cases they mean something else.  */
1736   s2 = *pp;
1737   n2 = parse_number (pp, &ov2, p_end);
1738   if (**pp != ';')
1739     {
1740       bad_stab (orig);
1741       return DEBUG_TYPE_NULL;
1742     }
1743   ++*pp;
1744
1745   s3 = *pp;
1746   n3 = parse_number (pp, &ov3, p_end);
1747   if (**pp != ';')
1748     {
1749       bad_stab (orig);
1750       return DEBUG_TYPE_NULL;
1751     }
1752   ++*pp;
1753
1754   if (ov2 || ov3)
1755     {
1756       /* gcc will emit range stabs for long long types.  Handle this
1757          as a special case.  FIXME: This needs to be more general.  */
1758 #define LLLOW   "01000000000000000000000;"
1759 #define LLHIGH   "0777777777777777777777;"
1760 #define ULLHIGH "01777777777777777777777;"
1761       if (index_type == DEBUG_TYPE_NULL)
1762         {
1763           if (CONST_STRNEQ (s2, LLLOW)
1764               && CONST_STRNEQ (s3, LLHIGH))
1765             return debug_make_int_type (dhandle, 8, FALSE);
1766           if (! ov2
1767               && n2 == 0
1768               && CONST_STRNEQ (s3, ULLHIGH))
1769             return debug_make_int_type (dhandle, 8, TRUE);
1770         }
1771
1772       warn_stab (orig, _("numeric overflow"));
1773     }
1774
1775   if (index_type == DEBUG_TYPE_NULL)
1776     {
1777       /* A type defined as a subrange of itself, with both bounds 0,
1778          is void.  */
1779       if (self_subrange && n2 == 0 && n3 == 0)
1780         return debug_make_void_type (dhandle);
1781
1782       /* A type defined as a subrange of itself, with n2 positive and
1783          n3 zero, is a complex type, and n2 is the number of bytes.  */
1784       if (self_subrange && n3 == 0 && n2 > 0)
1785         return debug_make_complex_type (dhandle, n2);
1786
1787       /* If n3 is zero and n2 is positive, this is a floating point
1788          type, and n2 is the number of bytes.  */
1789       if (n3 == 0 && n2 > 0)
1790         return debug_make_float_type (dhandle, n2);
1791
1792       /* If the upper bound is -1, this is an unsigned int.  */
1793       if (n2 == 0 && n3 == -1)
1794         {
1795           /* When gcc is used with -gstabs, but not -gstabs+, it will emit
1796                  long long int:t6=r1;0;-1;
1797                  long long unsigned int:t7=r1;0;-1;
1798              We hack here to handle this reasonably.  */
1799           if (type_name != NULL)
1800             {
1801               if (strcmp (type_name, "long long int") == 0)
1802                 return debug_make_int_type (dhandle, 8, FALSE);
1803               else if (strcmp (type_name, "long long unsigned int") == 0)
1804                 return debug_make_int_type (dhandle, 8, TRUE);
1805             }
1806           /* FIXME: The size here really depends upon the target.  */
1807           return debug_make_int_type (dhandle, 4, TRUE);
1808         }
1809
1810       /* A range of 0 to 127 is char.  */
1811       if (self_subrange && n2 == 0 && n3 == 127)
1812         return debug_make_int_type (dhandle, 1, FALSE);
1813
1814       /* FIXME: gdb checks for the language CHILL here.  */
1815
1816       if (n2 == 0)
1817         {
1818           if (n3 < 0)
1819             return debug_make_int_type (dhandle, - n3, TRUE);
1820           else if (n3 == 0xff)
1821             return debug_make_int_type (dhandle, 1, TRUE);
1822           else if (n3 == 0xffff)
1823             return debug_make_int_type (dhandle, 2, TRUE);
1824           else if (n3 == (bfd_signed_vma) 0xffffffff)
1825             return debug_make_int_type (dhandle, 4, TRUE);
1826 #ifdef BFD64
1827           else if (n3 == (bfd_signed_vma) 0xffffffffffffffffLL)
1828             return debug_make_int_type (dhandle, 8, TRUE);
1829 #endif
1830         }
1831       else if (n3 == 0
1832                && n2 < 0
1833                && (self_subrange || n2 == -8))
1834         return debug_make_int_type (dhandle, - n2, TRUE);
1835       else if (n2 == - n3 - 1 || n2 == n3 + 1)
1836         {
1837           if (n3 == 0x7f)
1838             return debug_make_int_type (dhandle, 1, FALSE);
1839           else if (n3 == 0x7fff)
1840             return debug_make_int_type (dhandle, 2, FALSE);
1841           else if (n3 == 0x7fffffff)
1842             return debug_make_int_type (dhandle, 4, FALSE);
1843 #ifdef BFD64
1844           else if (n3 == ((((bfd_vma) 0x7fffffff) << 32) | 0xffffffff))
1845             return debug_make_int_type (dhandle, 8, FALSE);
1846 #endif
1847         }
1848     }
1849
1850   /* At this point I don't have the faintest idea how to deal with a
1851      self_subrange type; I'm going to assume that this is used as an
1852      idiom, and that all of them are special cases.  So . . .  */
1853   if (self_subrange)
1854     {
1855       bad_stab (orig);
1856       return DEBUG_TYPE_NULL;
1857     }
1858
1859   index_type = stab_find_type (dhandle, info, rangenums);
1860   if (index_type == DEBUG_TYPE_NULL)
1861     {
1862       /* Does this actually ever happen?  Is that why we are worrying
1863          about dealing with it rather than just calling error_type?  */
1864       warn_stab (orig, _("missing index type"));
1865       index_type = debug_make_int_type (dhandle, 4, FALSE);
1866     }
1867
1868   return debug_make_range_type (dhandle, index_type, n2, n3);
1869 }
1870
1871 /* Sun's ACC uses a somewhat saner method for specifying the builtin
1872    typedefs in every file (for int, long, etc):
1873
1874         type = b <signed> <width>; <offset>; <nbits>
1875         signed = u or s.  Possible c in addition to u or s (for char?).
1876         offset = offset from high order bit to start bit of type.
1877         width is # bytes in object of this type, nbits is # bits in type.
1878
1879    The width/offset stuff appears to be for small objects stored in
1880    larger ones (e.g. `shorts' in `int' registers).  We ignore it for now,
1881    FIXME.  */
1882
1883 static debug_type
1884 parse_stab_sun_builtin_type (void *dhandle, const char **pp, const char * p_end)
1885 {
1886   const char *orig;
1887   bfd_boolean unsignedp;
1888   bfd_vma bits;
1889
1890   orig = *pp;
1891   if (orig >= p_end)
1892     return DEBUG_TYPE_NULL;
1893
1894   switch (**pp)
1895     {
1896     case 's':
1897       unsignedp = FALSE;
1898       break;
1899     case 'u':
1900       unsignedp = TRUE;
1901       break;
1902     default:
1903       bad_stab (orig);
1904       return DEBUG_TYPE_NULL;
1905     }
1906   ++*pp;
1907
1908   /* OpenSolaris source code indicates that one of "cbv" characters
1909      can come next and specify the intrinsic 'iformat' encoding.
1910      'c' is character encoding, 'b' is boolean encoding, and 'v' is
1911      varargs encoding.  This field can be safely ignored because
1912      the type of the field is determined from the bitwidth extracted
1913      below.  */
1914   if (**pp == 'c' || **pp == 'b' || **pp == 'v')
1915     ++*pp;
1916
1917   /* The first number appears to be the number of bytes occupied
1918      by this type, except that unsigned short is 4 instead of 2.
1919      Since this information is redundant with the third number,
1920      we will ignore it.  */
1921   (void) parse_number (pp, (bfd_boolean *) NULL, p_end);
1922   if (**pp != ';')
1923     {
1924       bad_stab (orig);
1925       return DEBUG_TYPE_NULL;
1926     }
1927   ++*pp;
1928
1929   /* The second number is always 0, so ignore it too.  */
1930   (void) parse_number (pp, (bfd_boolean *) NULL, p_end);
1931   if (**pp != ';')
1932     {
1933       bad_stab (orig);
1934       return DEBUG_TYPE_NULL;
1935     }
1936   ++*pp;
1937
1938   /* The third number is the number of bits for this type.  */
1939   bits = parse_number (pp, (bfd_boolean *) NULL, p_end);
1940
1941   /* The type *should* end with a semicolon.  If it are embedded
1942      in a larger type the semicolon may be the only way to know where
1943      the type ends.  If this type is at the end of the stabstring we
1944      can deal with the omitted semicolon (but we don't have to like
1945      it).  Don't bother to complain(), Sun's compiler omits the semicolon
1946      for "void".  */
1947   if (**pp == ';')
1948     ++*pp;
1949
1950   if (bits == 0)
1951     return debug_make_void_type (dhandle);
1952
1953   return debug_make_int_type (dhandle, bits / 8, unsignedp);
1954 }
1955
1956 /* Parse a builtin floating type generated by the Sun compiler.  */
1957
1958 static debug_type
1959 parse_stab_sun_floating_type (void *dhandle, const char **pp, const char *p_end)
1960 {
1961   const char *orig;
1962   bfd_vma details;
1963   bfd_vma bytes;
1964
1965   orig = *pp;
1966   if (orig >= p_end)
1967     return DEBUG_TYPE_NULL;
1968
1969   /* The first number has more details about the type, for example
1970      FN_COMPLEX.  */
1971   details = parse_number (pp, (bfd_boolean *) NULL, p_end);
1972   if (**pp != ';')
1973     {
1974       bad_stab (orig);
1975       return DEBUG_TYPE_NULL;
1976     }
1977
1978   /* The second number is the number of bytes occupied by this type */
1979   bytes = parse_number (pp, (bfd_boolean *) NULL, p_end);
1980   if (**pp != ';')
1981     {
1982       bad_stab (orig);
1983       return DEBUG_TYPE_NULL;
1984     }
1985
1986   if (details == NF_COMPLEX
1987       || details == NF_COMPLEX16
1988       || details == NF_COMPLEX32)
1989     return debug_make_complex_type (dhandle, bytes);
1990
1991   return debug_make_float_type (dhandle, bytes);
1992 }
1993
1994 /* Handle an enum type.  */
1995
1996 static debug_type
1997 parse_stab_enum_type (void *dhandle, const char **pp, const char * p_end)
1998 {
1999   const char *orig;
2000   const char **names;
2001   bfd_signed_vma *values;
2002   unsigned int n;
2003   unsigned int alloc;
2004
2005   orig = *pp;
2006   if (orig >= p_end)
2007     return DEBUG_TYPE_NULL;
2008
2009   /* FIXME: gdb checks os9k_stabs here.  */
2010
2011   /* The aix4 compiler emits an extra field before the enum members;
2012      my guess is it's a type of some sort.  Just ignore it.  */
2013   if (**pp == '-')
2014     {
2015       while (**pp != ':' && **pp != 0)
2016         ++*pp;
2017
2018       if (**pp == 0)
2019         {
2020           bad_stab (orig);
2021           return DEBUG_TYPE_NULL;
2022         }
2023       ++*pp;
2024     }
2025
2026   /* Read the value-names and their values.
2027      The input syntax is NAME:VALUE,NAME:VALUE, and so on.
2028      A semicolon or comma instead of a NAME means the end.  */
2029   alloc = 10;
2030   names = (const char **) xmalloc (alloc * sizeof *names);
2031   values = (bfd_signed_vma *) xmalloc (alloc * sizeof *values);
2032   n = 0;
2033   while (**pp != '\0' && **pp != ';' && **pp != ',')
2034     {
2035       const char *p;
2036       char *name;
2037       bfd_signed_vma val;
2038
2039       p = *pp;
2040       while (*p != ':' && *p != 0)
2041         ++p;
2042
2043       if (*p == 0)
2044         {
2045           bad_stab (orig);
2046           free (names);
2047           free (values);
2048           return DEBUG_TYPE_NULL;
2049         }
2050
2051       name = savestring (*pp, p - *pp);
2052
2053       *pp = p + 1;
2054       val = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL, p_end);
2055       if (**pp != ',')
2056         {
2057           bad_stab (orig);
2058           free (name);
2059           free (names);
2060           free (values);
2061           return DEBUG_TYPE_NULL;
2062         }
2063       ++*pp;
2064
2065       if (n + 1 >= alloc)
2066         {
2067           alloc += 10;
2068           names = ((const char **)
2069                    xrealloc (names, alloc * sizeof *names));
2070           values = ((bfd_signed_vma *)
2071                     xrealloc (values, alloc * sizeof *values));
2072         }
2073
2074       names[n] = name;
2075       values[n] = val;
2076       ++n;
2077     }
2078
2079   names[n] = NULL;
2080   values[n] = 0;
2081
2082   if (**pp == ';')
2083     ++*pp;
2084
2085   return debug_make_enum_type (dhandle, names, values);
2086 }
2087
2088 /* Read the description of a structure (or union type) and return an object
2089    describing the type.
2090
2091    PP points to a character pointer that points to the next unconsumed token
2092    in the stabs string.  For example, given stabs "A:T4=s4a:1,0,32;;",
2093    *PP will point to "4a:1,0,32;;".  */
2094
2095 static debug_type
2096 parse_stab_struct_type (void *                dhandle,
2097                         struct stab_handle *  info,
2098                         const char *          tagname,
2099                         const char **         pp,
2100                         bfd_boolean           structp,
2101                         const int *           typenums,
2102                         const char *          p_end)
2103 {
2104   bfd_vma size;
2105   debug_baseclass *baseclasses;
2106   debug_field *fields = NULL;
2107   bfd_boolean statics;
2108   debug_method *methods;
2109   debug_type vptrbase;
2110   bfd_boolean ownvptr;
2111
2112   /* Get the size.  */
2113   size = parse_number (pp, (bfd_boolean *) NULL, p_end);
2114
2115   /* Get the other information.  */
2116   if (! parse_stab_baseclasses (dhandle, info, pp, &baseclasses, p_end)
2117       || ! parse_stab_struct_fields (dhandle, info, pp, &fields, &statics, p_end)
2118       || ! parse_stab_members (dhandle, info, tagname, pp, typenums, &methods, p_end)
2119       || ! parse_stab_tilde_field (dhandle, info, pp, typenums, &vptrbase,
2120                                    &ownvptr, p_end))
2121     {
2122       if (fields != NULL)
2123         free (fields);
2124       return DEBUG_TYPE_NULL;
2125     }
2126
2127   if (! statics
2128       && baseclasses == NULL
2129       && methods == NULL
2130       && vptrbase == DEBUG_TYPE_NULL
2131       && ! ownvptr)
2132     return debug_make_struct_type (dhandle, structp, size, fields);
2133
2134   return debug_make_object_type (dhandle, structp, size, fields, baseclasses,
2135                                  methods, vptrbase, ownvptr);
2136 }
2137
2138 /* The stabs for C++ derived classes contain baseclass information which
2139    is marked by a '!' character after the total size.  This function is
2140    called when we encounter the baseclass marker, and slurps up all the
2141    baseclass information.
2142
2143    Immediately following the '!' marker is the number of base classes that
2144    the class is derived from, followed by information for each base class.
2145    For each base class, there are two visibility specifiers, a bit offset
2146    to the base class information within the derived class, a reference to
2147    the type for the base class, and a terminating semicolon.
2148
2149    A typical example, with two base classes, would be "!2,020,19;0264,21;".
2150                                                        ^^ ^ ^ ^  ^ ^  ^
2151         Baseclass information marker __________________|| | | |  | |  |
2152         Number of baseclasses __________________________| | | |  | |  |
2153         Visibility specifiers (2) ________________________| | |  | |  |
2154         Offset in bits from start of class _________________| |  | |  |
2155         Type number for base class ___________________________|  | |  |
2156         Visibility specifiers (2) _______________________________| |  |
2157         Offset in bits from start of class ________________________|  |
2158         Type number of base class ____________________________________|
2159
2160   Return TRUE for success, FALSE for failure.  */
2161
2162 static bfd_boolean
2163 parse_stab_baseclasses (void *                dhandle,
2164                         struct stab_handle *  info,
2165                         const char **         pp,
2166                         debug_baseclass **    retp,
2167                         const char *          p_end)
2168 {
2169   const char *orig;
2170   unsigned int c, i;
2171   debug_baseclass *classes;
2172
2173   *retp = NULL;
2174
2175   orig = *pp;
2176   if (orig >= p_end)
2177     return FALSE;
2178
2179   if (**pp != '!')
2180     {
2181       /* No base classes.  */
2182       return TRUE;
2183     }
2184   ++*pp;
2185
2186   c = (unsigned int) parse_number (pp, (bfd_boolean *) NULL, p_end);
2187
2188   if (**pp != ',')
2189     {
2190       bad_stab (orig);
2191       return FALSE;
2192     }
2193   ++*pp;
2194
2195   classes = (debug_baseclass *) xmalloc ((c + 1) * sizeof (**retp));
2196
2197   for (i = 0; i < c; i++)
2198     {
2199       bfd_boolean is_virtual;
2200       enum debug_visibility visibility;
2201       bfd_vma bitpos;
2202       debug_type type;
2203
2204       switch (**pp)
2205         {
2206         case '0':
2207           is_virtual = FALSE;
2208           break;
2209         case '1':
2210           is_virtual = TRUE;
2211           break;
2212         case 0:
2213           bad_stab (orig);
2214           return FALSE;
2215         default:
2216           warn_stab (orig, _("unknown virtual character for baseclass"));
2217           is_virtual = FALSE;
2218           break;
2219         }
2220       ++*pp;
2221
2222       switch (**pp)
2223         {
2224         case '0':
2225           visibility = DEBUG_VISIBILITY_PRIVATE;
2226           break;
2227         case '1':
2228           visibility = DEBUG_VISIBILITY_PROTECTED;
2229           break;
2230         case '2':
2231           visibility = DEBUG_VISIBILITY_PUBLIC;
2232           break;
2233         case 0:
2234           bad_stab (orig);
2235           return FALSE;
2236         default:
2237           warn_stab (orig, _("unknown visibility character for baseclass"));
2238           visibility = DEBUG_VISIBILITY_PUBLIC;
2239           break;
2240         }
2241       ++*pp;
2242
2243       /* The remaining value is the bit offset of the portion of the
2244          object corresponding to this baseclass.  Always zero in the
2245          absence of multiple inheritance.  */
2246       bitpos = parse_number (pp, (bfd_boolean *) NULL, p_end);
2247       if (**pp != ',')
2248         {
2249           bad_stab (orig);
2250           return FALSE;
2251         }
2252       ++*pp;
2253
2254       type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2255                               (debug_type **) NULL, p_end);
2256       if (type == DEBUG_TYPE_NULL)
2257         return FALSE;
2258
2259       classes[i] = debug_make_baseclass (dhandle, type, bitpos, is_virtual,
2260                                          visibility);
2261       if (classes[i] == DEBUG_BASECLASS_NULL)
2262         return FALSE;
2263
2264       if (**pp != ';')
2265         return FALSE;
2266       ++*pp;
2267     }
2268
2269   classes[i] = DEBUG_BASECLASS_NULL;
2270
2271   *retp = classes;
2272
2273   return TRUE;
2274 }
2275
2276 /* Read struct or class data fields.  They have the form:
2277
2278         NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2279
2280    At the end, we see a semicolon instead of a field.
2281
2282    In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2283    a static field.
2284
2285    The optional VISIBILITY is one of:
2286
2287         '/0'    (VISIBILITY_PRIVATE)
2288         '/1'    (VISIBILITY_PROTECTED)
2289         '/2'    (VISIBILITY_PUBLIC)
2290         '/9'    (VISIBILITY_IGNORE)
2291
2292    or nothing, for C style fields with public visibility.
2293
2294    Returns 1 for success, 0 for failure.  */
2295
2296 static bfd_boolean
2297 parse_stab_struct_fields (void *                dhandle,
2298                           struct stab_handle *  info,
2299                           const char **         pp,
2300                           debug_field **        retp,
2301                           bfd_boolean *         staticsp,
2302                           const char *          p_end)
2303 {
2304   const char *orig;
2305   const char *p;
2306   debug_field *fields;
2307   unsigned int c;
2308   unsigned int alloc;
2309
2310   *retp = NULL;
2311   *staticsp = FALSE;
2312
2313   orig = *pp;
2314   if (orig >= p_end)
2315     return FALSE;
2316
2317   c = 0;
2318   alloc = 10;
2319   fields = (debug_field *) xmalloc (alloc * sizeof *fields);
2320   while (**pp != ';')
2321     {
2322       /* FIXME: gdb checks os9k_stabs here.  */
2323
2324       p = *pp;
2325
2326       /* Add 1 to c to leave room for NULL pointer at end.  */
2327       if (c + 1 >= alloc)
2328         {
2329           alloc += 10;
2330           fields = ((debug_field *)
2331                     xrealloc (fields, alloc * sizeof *fields));
2332         }
2333
2334       /* If it starts with CPLUS_MARKER it is a special abbreviation,
2335          unless the CPLUS_MARKER is followed by an underscore, in
2336          which case it is just the name of an anonymous type, which we
2337          should handle like any other type name.  We accept either '$'
2338          or '.', because a field name can never contain one of these
2339          characters except as a CPLUS_MARKER.  */
2340
2341       if ((*p == '$' || *p == '.') && p[1] != '_')
2342         {
2343           ++*pp;
2344           if (! parse_stab_cpp_abbrev (dhandle, info, pp, fields + c, p_end))
2345             {
2346               free (fields);
2347               return FALSE;
2348             }
2349           ++c;
2350           continue;
2351         }
2352
2353       /* Look for the ':' that separates the field name from the field
2354          values.  Data members are delimited by a single ':', while member
2355          functions are delimited by a pair of ':'s.  When we hit the member
2356          functions (if any), terminate scan loop and return.  */
2357
2358       p = strchr (p, ':');
2359       if (p == NULL)
2360         {
2361           bad_stab (orig);
2362           free (fields);
2363           return FALSE;
2364         }
2365
2366       if (p[1] == ':')
2367         break;
2368
2369       if (! parse_stab_one_struct_field (dhandle, info, pp, p, fields + c,
2370                                          staticsp, p_end))
2371         return FALSE;
2372
2373       ++c;
2374     }
2375
2376   fields[c] = DEBUG_FIELD_NULL;
2377
2378   *retp = fields;
2379
2380   return TRUE;
2381 }
2382
2383 /* Special GNU C++ name.  */
2384
2385 static bfd_boolean
2386 parse_stab_cpp_abbrev (void *                dhandle,
2387                        struct stab_handle *  info,
2388                        const char **         pp,
2389                        debug_field *         retp,
2390                        const char *          p_end)
2391 {
2392   const char *orig;
2393   int cpp_abbrev;
2394   debug_type context;
2395   const char *name;
2396   const char *type_name;
2397   debug_type type;
2398   bfd_vma bitpos;
2399
2400   *retp = DEBUG_FIELD_NULL;
2401
2402   orig = *pp;
2403   if (orig >= p_end)
2404     return FALSE;
2405
2406   if (**pp != 'v')
2407     {
2408       bad_stab (*pp);
2409       return FALSE;
2410     }
2411   ++*pp;
2412
2413   cpp_abbrev = **pp;
2414   if (cpp_abbrev == 0)
2415     {
2416       bad_stab (orig);
2417       return FALSE;
2418     }
2419   ++*pp;
2420
2421   /* At this point, *pp points to something like "22:23=*22...", where
2422      the type number before the ':' is the "context" and everything
2423      after is a regular type definition.  Lookup the type, find it's
2424      name, and construct the field name.  */
2425
2426   context = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2427                              (debug_type **) NULL, p_end);
2428   if (context == DEBUG_TYPE_NULL)
2429     return FALSE;
2430
2431   switch (cpp_abbrev)
2432     {
2433     case 'f':
2434       /* $vf -- a virtual function table pointer.  */
2435       name = "_vptr$";
2436       break;
2437     case 'b':
2438       /* $vb -- a virtual bsomethingorother */
2439       type_name = debug_get_type_name (dhandle, context);
2440       if (type_name == NULL)
2441         {
2442           warn_stab (orig, _("unnamed $vb type"));
2443           type_name = "FOO";
2444         }
2445       name = concat ("_vb$", type_name, (const char *) NULL);
2446       break;
2447     default:
2448       warn_stab (orig, _("unrecognized C++ abbreviation"));
2449       name = "INVALID_CPLUSPLUS_ABBREV";
2450       break;
2451     }
2452
2453   if (**pp != ':')
2454     {
2455       bad_stab (orig);
2456       return FALSE;
2457     }
2458   ++*pp;
2459
2460   type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2461                           (debug_type **) NULL, p_end);
2462   if (**pp != ',')
2463     {
2464       bad_stab (orig);
2465       return FALSE;
2466     }
2467   ++*pp;
2468
2469   bitpos = parse_number (pp, (bfd_boolean *) NULL, p_end);
2470   if (**pp != ';')
2471     {
2472       bad_stab (orig);
2473       return FALSE;
2474     }
2475   ++*pp;
2476
2477   *retp = debug_make_field (dhandle, name, type, bitpos, 0,
2478                             DEBUG_VISIBILITY_PRIVATE);
2479   if (*retp == DEBUG_FIELD_NULL)
2480     return FALSE;
2481
2482   return TRUE;
2483 }
2484
2485 /* Parse a single field in a struct or union.  */
2486
2487 static bfd_boolean
2488 parse_stab_one_struct_field (void *                dhandle,
2489                              struct stab_handle *  info,
2490                              const char **         pp,
2491                              const char *          p,
2492                              debug_field *         retp,
2493                              bfd_boolean *         staticsp,
2494                              const char *          p_end)
2495 {
2496   const char *orig;
2497   char *name;
2498   enum debug_visibility visibility;
2499   debug_type type;
2500   bfd_vma bitpos;
2501   bfd_vma bitsize;
2502
2503   orig = *pp;
2504   if (orig >= p_end)
2505     return FALSE;
2506
2507   /* FIXME: gdb checks ARM_DEMANGLING here.  */
2508
2509   name = savestring (*pp, p - *pp);
2510
2511   *pp = p + 1;
2512
2513   if (**pp != '/')
2514     visibility = DEBUG_VISIBILITY_PUBLIC;
2515   else
2516     {
2517       ++*pp;
2518       switch (**pp)
2519         {
2520         case '0':
2521           visibility = DEBUG_VISIBILITY_PRIVATE;
2522           break;
2523         case '1':
2524           visibility = DEBUG_VISIBILITY_PROTECTED;
2525           break;
2526         case '2':
2527           visibility = DEBUG_VISIBILITY_PUBLIC;
2528           break;
2529         case 0:
2530           bad_stab (orig);
2531           return FALSE;
2532         default:
2533           warn_stab (orig, _("unknown visibility character for field"));
2534           visibility = DEBUG_VISIBILITY_PUBLIC;
2535           break;
2536         }
2537       ++*pp;
2538     }
2539
2540   type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2541                           (debug_type **) NULL, p_end);
2542   if (type == DEBUG_TYPE_NULL)
2543     {
2544       free (name);
2545       return FALSE;
2546     }
2547
2548   if (**pp == ':')
2549     {
2550       char *varname;
2551
2552       /* This is a static class member.  */
2553       ++*pp;
2554       p = strchr (*pp, ';');
2555       if (p == NULL)
2556         {
2557           bad_stab (orig);
2558           free (name);
2559           return FALSE;
2560         }
2561
2562       varname = savestring (*pp, p - *pp);
2563
2564       *pp = p + 1;
2565
2566       *retp = debug_make_static_member (dhandle, name, type, varname,
2567                                         visibility);
2568       *staticsp = TRUE;
2569
2570       return TRUE;
2571     }
2572
2573   if (**pp != ',')
2574     {
2575       bad_stab (orig);
2576       free (name);
2577       return FALSE;
2578     }
2579   ++*pp;
2580
2581   bitpos = parse_number (pp, (bfd_boolean *) NULL, p_end);
2582   if (**pp != ',')
2583     {
2584       bad_stab (orig);
2585       free (name);
2586       return FALSE;
2587     }
2588   ++*pp;
2589
2590   bitsize = parse_number (pp, (bfd_boolean *) NULL, p_end);
2591   if (**pp != ';')
2592     {
2593       bad_stab (orig);
2594       free (name);
2595       return FALSE;
2596     }
2597   ++*pp;
2598
2599   if (bitpos == 0 && bitsize == 0)
2600     {
2601       /* This can happen in two cases: (1) at least for gcc 2.4.5 or
2602          so, it is a field which has been optimized out.  The correct
2603          stab for this case is to use VISIBILITY_IGNORE, but that is a
2604          recent invention.  (2) It is a 0-size array.  For example
2605          union { int num; char str[0]; } foo.  Printing "<no value>"
2606          for str in "p foo" is OK, since foo.str (and thus foo.str[3])
2607          will continue to work, and a 0-size array as a whole doesn't
2608          have any contents to print.
2609
2610          I suspect this probably could also happen with gcc -gstabs
2611          (not -gstabs+) for static fields, and perhaps other C++
2612          extensions.  Hopefully few people use -gstabs with gdb, since
2613          it is intended for dbx compatibility.  */
2614       visibility = DEBUG_VISIBILITY_IGNORE;
2615     }
2616
2617   /* FIXME: gdb does some stuff here to mark fields as unpacked.  */
2618
2619   *retp = debug_make_field (dhandle, name, type, bitpos, bitsize, visibility);
2620
2621   return TRUE;
2622 }
2623
2624 /* Read member function stabs info for C++ classes.  The form of each member
2625    function data is:
2626
2627         NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2628
2629    An example with two member functions is:
2630
2631         afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2632
2633    For the case of overloaded operators, the format is op$::*.funcs, where
2634    $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2635    name (such as `+=') and `.' marks the end of the operator name.  */
2636
2637 static bfd_boolean
2638 parse_stab_members (void *                dhandle,
2639                     struct stab_handle *  info,
2640                     const char *          tagname,
2641                     const char **         pp,
2642                     const int *           typenums,
2643                     debug_method **       retp,
2644                     const char *          p_end)
2645 {
2646   const char *orig;
2647   debug_method *methods;
2648   unsigned int c;
2649   unsigned int alloc;
2650   char *name = NULL;
2651   debug_method_variant *variants = NULL;
2652   char *argtypes = NULL;
2653
2654   *retp = NULL;
2655
2656   orig = *pp;
2657   if (orig >= p_end)
2658     return FALSE;
2659
2660   alloc = 0;
2661   methods = NULL;
2662   c = 0;
2663
2664   while (**pp != ';')
2665     {
2666       const char *p;
2667       unsigned int cvars;
2668       unsigned int allocvars;
2669       debug_type look_ahead_type;
2670
2671       p = strchr (*pp, ':');
2672       if (p == NULL || p[1] != ':')
2673         break;
2674
2675       /* FIXME: Some systems use something other than '$' here.  */
2676       if ((*pp)[0] != 'o' || (*pp)[1] != 'p' || (*pp)[2] != '$')
2677         {
2678           name = savestring (*pp, p - *pp);
2679           *pp = p + 2;
2680         }
2681       else
2682         {
2683           /* This is a completely weird case.  In order to stuff in the
2684              names that might contain colons (the usual name delimiter),
2685              Mike Tiemann defined a different name format which is
2686              signalled if the identifier is "op$".  In that case, the
2687              format is "op$::XXXX." where XXXX is the name.  This is
2688              used for names like "+" or "=".  YUUUUUUUK!  FIXME!  */
2689           *pp = p + 2;
2690           for (p = *pp; *p != '.' && *p != '\0'; p++)
2691             ;
2692           if (*p != '.')
2693             {
2694               bad_stab (orig);
2695               goto fail;
2696             }
2697           name = savestring (*pp, p - *pp);
2698           *pp = p + 1;
2699         }
2700
2701       allocvars = 10;
2702       variants = ((debug_method_variant *)
2703                   xmalloc (allocvars * sizeof *variants));
2704       cvars = 0;
2705
2706       look_ahead_type = DEBUG_TYPE_NULL;
2707
2708       do
2709         {
2710           debug_type type;
2711           bfd_boolean stub;
2712           enum debug_visibility visibility;
2713           bfd_boolean constp, volatilep, staticp;
2714           bfd_vma voffset;
2715           debug_type context;
2716           const char *physname;
2717           bfd_boolean varargs;
2718
2719           if (look_ahead_type != DEBUG_TYPE_NULL)
2720             {
2721               /* g++ version 1 kludge */
2722               type = look_ahead_type;
2723               look_ahead_type = DEBUG_TYPE_NULL;
2724             }
2725           else
2726             {
2727               type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2728                                       (debug_type **) NULL, p_end);
2729               if (type == DEBUG_TYPE_NULL)
2730                 goto fail;
2731
2732               if (**pp != ':')
2733                 {
2734                   bad_stab (orig);
2735                   goto fail;
2736                 }
2737             }
2738
2739           ++*pp;
2740           p = strchr (*pp, ';');
2741           if (p == NULL)
2742             {
2743               bad_stab (orig);
2744               goto fail;
2745             }
2746
2747           stub = FALSE;
2748           if (debug_get_type_kind (dhandle, type) == DEBUG_KIND_METHOD
2749               && debug_get_parameter_types (dhandle, type, &varargs) == NULL)
2750             stub = TRUE;
2751
2752           argtypes = savestring (*pp, p - *pp);
2753           *pp = p + 1;
2754
2755           switch (**pp)
2756             {
2757             case '0':
2758               visibility = DEBUG_VISIBILITY_PRIVATE;
2759               break;
2760             case '1':
2761               visibility = DEBUG_VISIBILITY_PROTECTED;
2762               break;
2763             case 0:
2764               bad_stab (orig);
2765               goto fail;
2766             default:
2767               visibility = DEBUG_VISIBILITY_PUBLIC;
2768               break;
2769             }
2770           ++*pp;
2771
2772           constp = FALSE;
2773           volatilep = FALSE;
2774           switch (**pp)
2775             {
2776             case 'A':
2777               /* Normal function.  */
2778               ++*pp;
2779               break;
2780             case 'B':
2781               /* const member function.  */
2782               constp = TRUE;
2783               ++*pp;
2784               break;
2785             case 'C':
2786               /* volatile member function.  */
2787               volatilep = TRUE;
2788               ++*pp;
2789               break;
2790             case 'D':
2791               /* const volatile member function.  */
2792               constp = TRUE;
2793               volatilep = TRUE;
2794               ++*pp;
2795               break;
2796             case '*':
2797             case '?':
2798             case '.':
2799               /* File compiled with g++ version 1; no information.  */
2800               break;
2801             default:
2802               warn_stab (orig, _("const/volatile indicator missing"));
2803               break;
2804             }
2805
2806           staticp = FALSE;
2807           switch (**pp)
2808             {
2809             case '*':
2810               /* virtual member function, followed by index.  The sign
2811                  bit is supposedly set to distinguish
2812                  pointers-to-methods from virtual function indices.  */
2813               ++*pp;
2814               voffset = parse_number (pp, (bfd_boolean *) NULL, p_end);
2815               if (**pp != ';')
2816                 {
2817                   bad_stab (orig);
2818                   goto fail;
2819                 }
2820               ++*pp;
2821               voffset &= 0x7fffffff;
2822
2823               if (**pp == ';' || **pp == '\0')
2824                 {
2825                   /* Must be g++ version 1.  */
2826                   context = DEBUG_TYPE_NULL;
2827                 }
2828               else
2829                 {
2830                   /* Figure out from whence this virtual function
2831                      came.  It may belong to virtual function table of
2832                      one of its baseclasses.  */
2833                   look_ahead_type = parse_stab_type (dhandle, info,
2834                                                      (const char *) NULL,
2835                                                      pp,
2836                                                      (debug_type **) NULL,
2837                                                      p_end);
2838                   if (**pp == ':')
2839                     {
2840                       /* g++ version 1 overloaded methods.  */
2841                       context = DEBUG_TYPE_NULL;
2842                     }
2843                   else
2844                     {
2845                       context = look_ahead_type;
2846                       look_ahead_type = DEBUG_TYPE_NULL;
2847                       if (**pp != ';')
2848                         {
2849                           bad_stab (orig);
2850                           goto fail;
2851                         }
2852                       ++*pp;
2853                     }
2854                 }
2855               break;
2856
2857             case '?':
2858               /* static member function.  */
2859               ++*pp;
2860               staticp = TRUE;
2861               voffset = 0;
2862               context = DEBUG_TYPE_NULL;
2863               if (strncmp (argtypes, name, strlen (name)) != 0)
2864                 stub = TRUE;
2865               break;
2866
2867             default:
2868               warn_stab (orig, "member function type missing");
2869               voffset = 0;
2870               context = DEBUG_TYPE_NULL;
2871               break;
2872
2873             case '.':
2874               ++*pp;
2875               voffset = 0;
2876               context = DEBUG_TYPE_NULL;
2877               break;
2878             }
2879
2880           /* If the type is not a stub, then the argtypes string is
2881              the physical name of the function.  Otherwise the
2882              argtypes string is the mangled form of the argument
2883              types, and the full type and the physical name must be
2884              extracted from them.  */
2885           physname = argtypes;
2886           if (stub)
2887             {
2888               debug_type class_type, return_type;
2889
2890               class_type = stab_find_type (dhandle, info, typenums);
2891               if (class_type == DEBUG_TYPE_NULL)
2892                 goto fail;
2893               return_type = debug_get_return_type (dhandle, type);
2894               if (return_type == DEBUG_TYPE_NULL)
2895                 {
2896                   bad_stab (orig);
2897                   goto fail;
2898                 }
2899               type = parse_stab_argtypes (dhandle, info, class_type, name,
2900                                           tagname, return_type, argtypes,
2901                                           constp, volatilep, &physname);
2902               if (type == DEBUG_TYPE_NULL)
2903                 goto fail;
2904             }
2905
2906           if (cvars + 1 >= allocvars)
2907             {
2908               allocvars += 10;
2909               variants = ((debug_method_variant *)
2910                           xrealloc (variants,
2911                                     allocvars * sizeof *variants));
2912             }
2913
2914           if (! staticp)
2915             variants[cvars] = debug_make_method_variant (dhandle, physname,
2916                                                          type, visibility,
2917                                                          constp, volatilep,
2918                                                          voffset, context);
2919           else
2920             variants[cvars] = debug_make_static_method_variant (dhandle,
2921                                                                 physname,
2922                                                                 type,
2923                                                                 visibility,
2924                                                                 constp,
2925                                                                 volatilep);
2926           if (variants[cvars] == DEBUG_METHOD_VARIANT_NULL)
2927             goto fail;
2928
2929           ++cvars;
2930         }
2931       while (**pp != ';' && **pp != '\0');
2932
2933       variants[cvars] = DEBUG_METHOD_VARIANT_NULL;
2934
2935       if (**pp != '\0')
2936         ++*pp;
2937
2938       if (c + 1 >= alloc)
2939         {
2940           alloc += 10;
2941           methods = ((debug_method *)
2942                      xrealloc (methods, alloc * sizeof *methods));
2943         }
2944
2945       methods[c] = debug_make_method (dhandle, name, variants);
2946
2947       ++c;
2948     }
2949
2950   if (methods != NULL)
2951     methods[c] = DEBUG_METHOD_NULL;
2952
2953   *retp = methods;
2954
2955   return TRUE;
2956
2957  fail:
2958   if (name != NULL)
2959     free (name);
2960   if (variants != NULL)
2961     free (variants);
2962   if (argtypes != NULL)
2963     free (argtypes);
2964   return FALSE;
2965 }
2966
2967 /* Parse a string representing argument types for a method.  Stabs
2968    tries to save space by packing argument types into a mangled
2969    string.  This string should give us enough information to extract
2970    both argument types and the physical name of the function, given
2971    the tag name.  */
2972
2973 static debug_type
2974 parse_stab_argtypes (void *dhandle, struct stab_handle *info,
2975                      debug_type class_type, const char *fieldname,
2976                      const char *tagname, debug_type return_type,
2977                      const char *argtypes, bfd_boolean constp,
2978                      bfd_boolean volatilep, const char **pphysname)
2979 {
2980   bfd_boolean is_full_physname_constructor;
2981   bfd_boolean is_constructor;
2982   bfd_boolean is_destructor;
2983   bfd_boolean is_v3;
2984   debug_type *args;
2985   bfd_boolean varargs;
2986   unsigned int physname_len = 0;
2987
2988   /* Constructors are sometimes handled specially.  */
2989   is_full_physname_constructor = ((argtypes[0] == '_'
2990                                    && argtypes[1] == '_'
2991                                    && (ISDIGIT (argtypes[2])
2992                                        || argtypes[2] == 'Q'
2993                                        || argtypes[2] == 't'))
2994                                   || CONST_STRNEQ (argtypes, "__ct"));
2995
2996   is_constructor = (is_full_physname_constructor
2997                     || (tagname != NULL
2998                         && strcmp (fieldname, tagname) == 0));
2999   is_destructor = ((argtypes[0] == '_'
3000                     && (argtypes[1] == '$' || argtypes[1] == '.')
3001                     && argtypes[2] == '_')
3002                    || CONST_STRNEQ (argtypes, "__dt"));
3003   is_v3 = argtypes[0] == '_' && argtypes[1] == 'Z';
3004
3005   if (!(is_destructor || is_full_physname_constructor || is_v3))
3006     {
3007       unsigned int len;
3008       const char *const_prefix;
3009       const char *volatile_prefix;
3010       char buf[20];
3011       unsigned int mangled_name_len;
3012       char *physname;
3013
3014       len = tagname == NULL ? 0 : strlen (tagname);
3015       const_prefix = constp ? "C" : "";
3016       volatile_prefix = volatilep ? "V" : "";
3017
3018       if (len == 0)
3019         sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
3020       else if (tagname != NULL && strchr (tagname, '<') != NULL)
3021         {
3022           /* Template methods are fully mangled.  */
3023           sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
3024           tagname = NULL;
3025           len = 0;
3026         }
3027       else
3028         sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
3029
3030       mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
3031                           + strlen (buf)
3032                           + len
3033                           + strlen (argtypes)
3034                           + 1);
3035
3036       if (fieldname[0] == 'o'
3037           && fieldname[1] == 'p'
3038           && (fieldname[2] == '$' || fieldname[2] == '.'))
3039         {
3040           const char *opname;
3041
3042           opname = cplus_mangle_opname (fieldname + 3, 0);
3043           if (opname == NULL)
3044             {
3045               fprintf (stderr, _("No mangling for \"%s\"\n"), fieldname);
3046               return DEBUG_TYPE_NULL;
3047             }
3048           mangled_name_len += strlen (opname);
3049           physname = (char *) xmalloc (mangled_name_len);
3050           strncpy (physname, fieldname, 3);
3051           strcpy (physname + 3, opname);
3052         }
3053       else
3054         {
3055           physname = (char *) xmalloc (mangled_name_len);
3056           if (is_constructor)
3057             physname[0] = '\0';
3058           else
3059             strcpy (physname, fieldname);
3060         }
3061
3062       physname_len = strlen (physname);
3063       strcat (physname, buf);
3064       if (tagname != NULL)
3065         strcat (physname, tagname);
3066       strcat (physname, argtypes);
3067
3068       *pphysname = physname;
3069     }
3070
3071   if (*argtypes == '\0' || is_destructor)
3072     {
3073       args = (debug_type *) xmalloc (sizeof *args);
3074       *args = NULL;
3075       return debug_make_method_type (dhandle, return_type, class_type, args,
3076                                      FALSE);
3077     }
3078
3079   args = stab_demangle_argtypes (dhandle, info, *pphysname, &varargs, physname_len);
3080   if (args == NULL)
3081     return DEBUG_TYPE_NULL;
3082
3083   return debug_make_method_type (dhandle, return_type, class_type, args,
3084                                  varargs);
3085 }
3086
3087 /* The tail end of stabs for C++ classes that contain a virtual function
3088    pointer contains a tilde, a %, and a type number.
3089    The type number refers to the base class (possibly this class itself) which
3090    contains the vtable pointer for the current class.
3091
3092    This function is called when we have parsed all the method declarations,
3093    so we can look for the vptr base class info.  */
3094
3095 static bfd_boolean
3096 parse_stab_tilde_field (void *                dhandle,
3097                         struct stab_handle *  info,
3098                         const char **         pp,
3099                         const int *           typenums,
3100                         debug_type *          retvptrbase,
3101                         bfd_boolean *         retownvptr,
3102                         const char *          p_end)
3103 {
3104   const char *orig;
3105   const char *hold;
3106   int vtypenums[2];
3107
3108   *retvptrbase = DEBUG_TYPE_NULL;
3109   *retownvptr = FALSE;
3110
3111   orig = *pp;
3112   if (orig >= p_end)
3113     return FALSE;
3114   
3115   /* If we are positioned at a ';', then skip it.  */
3116   if (**pp == ';')
3117     ++*pp;
3118
3119   if (**pp != '~')
3120     return TRUE;
3121   ++*pp;
3122
3123   if (**pp == '=' || **pp == '+' || **pp == '-')
3124     {
3125       /* Obsolete flags that used to indicate the presence of
3126          constructors and/or destructors.  */
3127       ++*pp;
3128     }
3129
3130   if (**pp != '%')
3131     return TRUE;
3132   ++*pp;
3133
3134   hold = *pp;
3135
3136   /* The next number is the type number of the base class (possibly
3137      our own class) which supplies the vtable for this class.  */
3138   if (! parse_stab_type_number (pp, vtypenums, p_end))
3139     return FALSE;
3140
3141   if (vtypenums[0] == typenums[0]
3142       && vtypenums[1] == typenums[1])
3143     *retownvptr = TRUE;
3144   else
3145     {
3146       debug_type vtype;
3147       const char *p;
3148
3149       *pp = hold;
3150
3151       vtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3152                                (debug_type **) NULL, p_end);
3153       for (p = *pp; *p != ';' && *p != '\0'; p++)
3154         ;
3155       if (*p != ';')
3156         {
3157           bad_stab (orig);
3158           return FALSE;
3159         }
3160
3161       *retvptrbase = vtype;
3162
3163       *pp = p + 1;
3164     }
3165
3166   return TRUE;
3167 }
3168
3169 /* Read a definition of an array type.  */
3170
3171 static debug_type
3172 parse_stab_array_type (void *                dhandle,
3173                        struct stab_handle *  info,
3174                        const char **         pp,
3175                        bfd_boolean           stringp,
3176                        const char *          p_end)
3177 {
3178   const char *orig;
3179   const char *p;
3180   int typenums[2];
3181   debug_type index_type;
3182   bfd_boolean adjustable;
3183   bfd_signed_vma lower, upper;
3184   debug_type element_type;
3185
3186   /* Format of an array type:
3187      "ar<index type>;lower;upper;<array_contents_type>".
3188      OS9000: "arlower,upper;<array_contents_type>".
3189
3190      Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
3191      for these, produce a type like float[][].  */
3192
3193   orig = *pp;
3194   if (orig >= p_end)
3195     return DEBUG_TYPE_NULL;
3196
3197   /* FIXME: gdb checks os9k_stabs here.  */
3198
3199   /* If the index type is type 0, we take it as int.  */
3200   p = *pp;
3201   if (! parse_stab_type_number (&p, typenums, p_end))
3202     return DEBUG_TYPE_NULL;
3203
3204   if (typenums[0] == 0 && typenums[1] == 0 && **pp != '=')
3205     {
3206       index_type = debug_find_named_type (dhandle, "int");
3207       if (index_type == DEBUG_TYPE_NULL)
3208         {
3209           index_type = debug_make_int_type (dhandle, 4, FALSE);
3210           if (index_type == DEBUG_TYPE_NULL)
3211             return DEBUG_TYPE_NULL;
3212         }
3213       *pp = p;
3214     }
3215   else
3216     {
3217       index_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3218                                     (debug_type **) NULL, p_end);
3219     }
3220
3221   if (**pp != ';')
3222     {
3223       bad_stab (orig);
3224       return DEBUG_TYPE_NULL;
3225     }
3226   ++*pp;
3227
3228   adjustable = FALSE;
3229
3230   if (! ISDIGIT (**pp) && **pp != '-' && **pp != 0)
3231     {
3232       ++*pp;
3233       adjustable = TRUE;
3234     }
3235
3236   lower = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL, p_end);
3237   if (**pp != ';')
3238     {
3239       bad_stab (orig);
3240       return DEBUG_TYPE_NULL;
3241     }
3242   ++*pp;
3243
3244   if (! ISDIGIT (**pp) && **pp != '-' && **pp != 0)
3245     {
3246       ++*pp;
3247       adjustable = TRUE;
3248     }
3249
3250   upper = (bfd_signed_vma) parse_number (pp, (bfd_boolean *) NULL, p_end);
3251   if (**pp != ';')
3252     {
3253       bad_stab (orig);
3254       return DEBUG_TYPE_NULL;
3255     }
3256   ++*pp;
3257
3258   element_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3259                                   (debug_type **) NULL, p_end);
3260   if (element_type == DEBUG_TYPE_NULL)
3261     return DEBUG_TYPE_NULL;
3262
3263   if (adjustable)
3264     {
3265       lower = 0;
3266       upper = -1;
3267     }
3268
3269   return debug_make_array_type (dhandle, element_type, index_type, lower,
3270                                 upper, stringp);
3271 }
3272
3273 /* This struct holds information about files we have seen using
3274    N_BINCL.  */
3275
3276 struct bincl_file
3277 {
3278   /* The next N_BINCL file.  */
3279   struct bincl_file *next;
3280   /* The next N_BINCL on the stack.  */
3281   struct bincl_file *next_stack;
3282   /* The file name.  */
3283   const char *name;
3284   /* The hash value.  */
3285   bfd_vma hash;
3286   /* The file index.  */
3287   unsigned int file;
3288   /* The list of types defined in this file.  */
3289   struct stab_types *file_types;
3290 };
3291
3292 /* Start a new N_BINCL file, pushing it onto the stack.  */
3293
3294 static void
3295 push_bincl (struct stab_handle *info, const char *name, bfd_vma hash)
3296 {
3297   struct bincl_file *n;
3298
3299   n = (struct bincl_file *) xmalloc (sizeof *n);
3300   n->next = info->bincl_list;
3301   n->next_stack = info->bincl_stack;
3302   n->name = name;
3303   n->hash = hash;
3304   n->file = info->files;
3305   n->file_types = NULL;
3306   info->bincl_list = n;
3307   info->bincl_stack = n;
3308
3309   ++info->files;
3310   info->file_types = ((struct stab_types **)
3311                       xrealloc (info->file_types,
3312                                 (info->files
3313                                  * sizeof *info->file_types)));
3314   info->file_types[n->file] = NULL;
3315 }
3316
3317 /* Finish an N_BINCL file, at an N_EINCL, popping the name off the
3318    stack.  */
3319
3320 static const char *
3321 pop_bincl (struct stab_handle *info)
3322 {
3323   struct bincl_file *o;
3324
3325   o = info->bincl_stack;
3326   if (o == NULL)
3327     return info->main_filename;
3328   info->bincl_stack = o->next_stack;
3329
3330   if (o->file >= info->files)
3331     return info->main_filename;
3332
3333   o->file_types = info->file_types[o->file];
3334
3335   if (info->bincl_stack == NULL)
3336     return info->main_filename;
3337   return info->bincl_stack->name;
3338 }
3339
3340 /* Handle an N_EXCL: get the types from the corresponding N_BINCL.  */
3341
3342 static bfd_boolean
3343 find_excl (struct stab_handle *info, const char *name, bfd_vma hash)
3344 {
3345   struct bincl_file *l;
3346
3347   ++info->files;
3348   info->file_types = ((struct stab_types **)
3349                       xrealloc (info->file_types,
3350                                 (info->files
3351                                  * sizeof *info->file_types)));
3352
3353   for (l = info->bincl_list; l != NULL; l = l->next)
3354     if (l->hash == hash && strcmp (l->name, name) == 0)
3355       break;
3356   if (l == NULL)
3357     {
3358       warn_stab (name, _("Undefined N_EXCL"));
3359       info->file_types[info->files - 1] = NULL;
3360       return TRUE;
3361     }
3362
3363   info->file_types[info->files - 1] = l->file_types;
3364
3365   return TRUE;
3366 }
3367
3368 /* Handle a variable definition.  gcc emits variable definitions for a
3369    block before the N_LBRAC, so we must hold onto them until we see
3370    it.  The SunPRO compiler emits variable definitions after the
3371    N_LBRAC, so we can call debug_record_variable immediately.  */
3372
3373 static bfd_boolean
3374 stab_record_variable (void *dhandle, struct stab_handle *info,
3375                       const char *name, debug_type type,
3376                       enum debug_var_kind kind, bfd_vma val)
3377 {
3378   struct stab_pending_var *v;
3379
3380   if ((kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
3381       || ! info->within_function
3382       || (info->gcc_compiled == 0 && info->n_opt_found))
3383     return debug_record_variable (dhandle, name, type, kind, val);
3384
3385   v = (struct stab_pending_var *) xmalloc (sizeof *v);
3386   memset (v, 0, sizeof *v);
3387
3388   v->next = info->pending;
3389   v->name = name;
3390   v->type = type;
3391   v->kind = kind;
3392   v->val = val;
3393   info->pending = v;
3394
3395   return TRUE;
3396 }
3397
3398 /* Emit pending variable definitions.  This is called after we see the
3399    N_LBRAC that starts the block.  */
3400
3401 static bfd_boolean
3402 stab_emit_pending_vars (void *dhandle, struct stab_handle *info)
3403 {
3404   struct stab_pending_var *v;
3405
3406   v = info->pending;
3407   while (v != NULL)
3408     {
3409       struct stab_pending_var *next;
3410
3411       if (! debug_record_variable (dhandle, v->name, v->type, v->kind, v->val))
3412         return FALSE;
3413
3414       next = v->next;
3415       free (v);
3416       v = next;
3417     }
3418
3419   info->pending = NULL;
3420
3421   return TRUE;
3422 }
3423
3424 /* Find the slot for a type in the database.  */
3425
3426 static debug_type *
3427 stab_find_slot (struct stab_handle *info, const int *typenums)
3428 {
3429   int filenum;
3430   int tindex;
3431   struct stab_types **ps;
3432
3433   filenum = typenums[0];
3434   tindex = typenums[1];
3435
3436   if (filenum < 0 || (unsigned int) filenum >= info->files)
3437     {
3438       fprintf (stderr, _("Type file number %d out of range\n"), filenum);
3439       return NULL;
3440     }
3441   if (tindex < 0)
3442     {
3443       fprintf (stderr, _("Type index number %d out of range\n"), tindex);
3444       return NULL;
3445     }
3446
3447   ps = info->file_types + filenum;
3448
3449   while (tindex >= STAB_TYPES_SLOTS)
3450     {
3451       if (*ps == NULL)
3452         {
3453           *ps = (struct stab_types *) xmalloc (sizeof **ps);
3454           memset (*ps, 0, sizeof **ps);
3455         }
3456       ps = &(*ps)->next;
3457       tindex -= STAB_TYPES_SLOTS;
3458     }
3459   if (*ps == NULL)
3460     {
3461       *ps = (struct stab_types *) xmalloc (sizeof **ps);
3462       memset (*ps, 0, sizeof **ps);
3463     }
3464
3465   return (*ps)->types + tindex;
3466 }
3467
3468 /* Find a type given a type number.  If the type has not been
3469    allocated yet, create an indirect type.  */
3470
3471 static debug_type
3472 stab_find_type (void *dhandle, struct stab_handle *info, const int *typenums)
3473 {
3474   debug_type *slot;
3475
3476   if (typenums[0] == 0 && typenums[1] < 0)
3477     {
3478       /* A negative type number indicates an XCOFF builtin type.  */
3479       return stab_xcoff_builtin_type (dhandle, info, typenums[1]);
3480     }
3481
3482   slot = stab_find_slot (info, typenums);
3483   if (slot == NULL)
3484     return DEBUG_TYPE_NULL;
3485
3486   if (*slot == DEBUG_TYPE_NULL)
3487     return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
3488
3489   return *slot;
3490 }
3491
3492 /* Record that a given type number refers to a given type.  */
3493
3494 static bfd_boolean
3495 stab_record_type (void *dhandle ATTRIBUTE_UNUSED, struct stab_handle *info,
3496                   const int *typenums, debug_type type)
3497 {
3498   debug_type *slot;
3499
3500   slot = stab_find_slot (info, typenums);
3501   if (slot == NULL)
3502     return FALSE;
3503
3504   /* gdb appears to ignore type redefinitions, so we do as well.  */
3505
3506   *slot = type;
3507
3508   return TRUE;
3509 }
3510
3511 /* Return an XCOFF builtin type.  */
3512
3513 static debug_type
3514 stab_xcoff_builtin_type (void *dhandle, struct stab_handle *info,
3515                          int typenum)
3516 {
3517   debug_type rettype;
3518   const char *name;
3519
3520   if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
3521     {
3522       fprintf (stderr, _("Unrecognized XCOFF type %d\n"), typenum);
3523       return DEBUG_TYPE_NULL;
3524     }
3525   if (info->xcoff_types[-typenum] != NULL)
3526     return info->xcoff_types[-typenum];
3527
3528   switch (-typenum)
3529     {
3530     case 1:
3531       /* The size of this and all the other types are fixed, defined
3532          by the debugging format.  */
3533       name = "int";
3534       rettype = debug_make_int_type (dhandle, 4, FALSE);
3535       break;
3536     case 2:
3537       name = "char";
3538       rettype = debug_make_int_type (dhandle, 1, FALSE);
3539       break;
3540     case 3:
3541       name = "short";
3542       rettype = debug_make_int_type (dhandle, 2, FALSE);
3543       break;
3544     case 4:
3545       name = "long";
3546       rettype = debug_make_int_type (dhandle, 4, FALSE);
3547       break;
3548     case 5:
3549       name = "unsigned char";
3550       rettype = debug_make_int_type (dhandle, 1, TRUE);
3551       break;
3552     case 6:
3553       name = "signed char";
3554       rettype = debug_make_int_type (dhandle, 1, FALSE);
3555       break;
3556     case 7:
3557       name = "unsigned short";
3558       rettype = debug_make_int_type (dhandle, 2, TRUE);
3559       break;
3560     case 8:
3561       name = "unsigned int";
3562       rettype = debug_make_int_type (dhandle, 4, TRUE);
3563       break;
3564     case 9:
3565       name = "unsigned";
3566       rettype = debug_make_int_type (dhandle, 4, TRUE);
3567       break;
3568     case 10:
3569       name = "unsigned long";
3570       rettype = debug_make_int_type (dhandle, 4, TRUE);
3571       break;
3572     case 11:
3573       name = "void";
3574       rettype = debug_make_void_type (dhandle);
3575       break;
3576     case 12:
3577       /* IEEE single precision (32 bit).  */
3578       name = "float";
3579       rettype = debug_make_float_type (dhandle, 4);
3580       break;
3581     case 13:
3582       /* IEEE double precision (64 bit).  */
3583       name = "double";
3584       rettype = debug_make_float_type (dhandle, 8);
3585       break;
3586     case 14:
3587       /* This is an IEEE double on the RS/6000, and different machines
3588          with different sizes for "long double" should use different
3589          negative type numbers.  See stabs.texinfo.  */
3590       name = "long double";
3591       rettype = debug_make_float_type (dhandle, 8);
3592       break;
3593     case 15:
3594       name = "integer";
3595       rettype = debug_make_int_type (dhandle, 4, FALSE);
3596       break;
3597     case 16:
3598       name = "boolean";
3599       rettype = debug_make_bool_type (dhandle, 4);
3600       break;
3601     case 17:
3602       name = "short real";
3603       rettype = debug_make_float_type (dhandle, 4);
3604       break;
3605     case 18:
3606       name = "real";
3607       rettype = debug_make_float_type (dhandle, 8);
3608       break;
3609     case 19:
3610       /* FIXME */
3611       name = "stringptr";
3612       rettype = NULL;
3613       break;
3614     case 20:
3615       /* FIXME */
3616       name = "character";
3617       rettype = debug_make_int_type (dhandle, 1, TRUE);
3618       break;
3619     case 21:
3620       name = "logical*1";
3621       rettype = debug_make_bool_type (dhandle, 1);
3622       break;
3623     case 22:
3624       name = "logical*2";
3625       rettype = debug_make_bool_type (dhandle, 2);
3626       break;
3627     case 23:
3628       name = "logical*4";
3629       rettype = debug_make_bool_type (dhandle, 4);
3630       break;
3631     case 24:
3632       name = "logical";
3633       rettype = debug_make_bool_type (dhandle, 4);
3634       break;
3635     case 25:
3636       /* Complex type consisting of two IEEE single precision values.  */
3637       name = "complex";
3638       rettype = debug_make_complex_type (dhandle, 8);
3639       break;
3640     case 26:
3641       /* Complex type consisting of two IEEE double precision values.  */
3642       name = "double complex";
3643       rettype = debug_make_complex_type (dhandle, 16);
3644       break;
3645     case 27:
3646       name = "integer*1";
3647       rettype = debug_make_int_type (dhandle, 1, FALSE);
3648       break;
3649     case 28:
3650       name = "integer*2";
3651       rettype = debug_make_int_type (dhandle, 2, FALSE);
3652       break;
3653     case 29:
3654       name = "integer*4";
3655       rettype = debug_make_int_type (dhandle, 4, FALSE);
3656       break;
3657     case 30:
3658       /* FIXME */
3659       name = "wchar";
3660       rettype = debug_make_int_type (dhandle, 2, FALSE);
3661       break;
3662     case 31:
3663       name = "long long";
3664       rettype = debug_make_int_type (dhandle, 8, FALSE);
3665       break;
3666     case 32:
3667       name = "unsigned long long";
3668       rettype = debug_make_int_type (dhandle, 8, TRUE);
3669       break;
3670     case 33:
3671       name = "logical*8";
3672       rettype = debug_make_bool_type (dhandle, 8);
3673       break;
3674     case 34:
3675       name = "integer*8";
3676       rettype = debug_make_int_type (dhandle, 8, FALSE);
3677       break;
3678     default:
3679       abort ();
3680     }
3681
3682   rettype = debug_name_type (dhandle, name, rettype);
3683
3684   info->xcoff_types[-typenum] = rettype;
3685
3686   return rettype;
3687 }
3688
3689 /* Find or create a tagged type.  */
3690
3691 static debug_type
3692 stab_find_tagged_type (void *dhandle, struct stab_handle *info,
3693                        const char *p, int len, enum debug_type_kind kind)
3694 {
3695   char *name;
3696   debug_type dtype;
3697   struct stab_tag *st;
3698
3699   name = savestring (p, len);
3700
3701   /* We pass DEBUG_KIND_ILLEGAL because we want all tags in the same
3702      namespace.  This is right for C, and I don't know how to handle
3703      other languages.  FIXME.  */
3704   dtype = debug_find_tagged_type (dhandle, name, DEBUG_KIND_ILLEGAL);
3705   if (dtype != DEBUG_TYPE_NULL)
3706     {
3707       free (name);
3708       return dtype;
3709     }
3710
3711   /* We need to allocate an entry on the undefined tag list.  */
3712   for (st = info->tags; st != NULL; st = st->next)
3713     {
3714       if (st->name[0] == name[0]
3715           && strcmp (st->name, name) == 0)
3716         {
3717           if (st->kind == DEBUG_KIND_ILLEGAL)
3718             st->kind = kind;
3719           free (name);
3720           break;
3721         }
3722     }
3723   if (st == NULL)
3724     {
3725       st = (struct stab_tag *) xmalloc (sizeof *st);
3726       memset (st, 0, sizeof *st);
3727
3728       st->next = info->tags;
3729       st->name = name;
3730       st->kind = kind;
3731       st->slot = DEBUG_TYPE_NULL;
3732       st->type = debug_make_indirect_type (dhandle, &st->slot, name);
3733       info->tags = st;
3734     }
3735
3736   return st->type;
3737 }
3738 \f
3739 /* In order to get the correct argument types for a stubbed method, we
3740    need to extract the argument types from a C++ mangled string.
3741    Since the argument types can refer back to the return type, this
3742    means that we must demangle the entire physical name.  In gdb this
3743    is done by calling cplus_demangle and running the results back
3744    through the C++ expression parser.  Since we have no expression
3745    parser, we must duplicate much of the work of cplus_demangle here.
3746
3747    We assume that GNU style demangling is used, since this is only
3748    done for method stubs, and only g++ should output that form of
3749    debugging information.  */
3750
3751 /* This structure is used to hold a pointer to type information which
3752    demangling a string.  */
3753
3754 struct stab_demangle_typestring
3755 {
3756   /* The start of the type.  This is not null terminated.  */
3757   const char *typestring;
3758   /* The length of the type.  */
3759   unsigned int len;
3760 };
3761
3762 /* This structure is used to hold information while demangling a
3763    string.  */
3764
3765 struct stab_demangle_info
3766 {
3767   /* The debugging information handle.  */
3768   void *dhandle;
3769   /* The stab information handle.  */
3770   struct stab_handle *info;
3771   /* The array of arguments we are building.  */
3772   debug_type *args;
3773   /* Whether the method takes a variable number of arguments.  */
3774   bfd_boolean varargs;
3775   /* The array of types we have remembered.  */
3776   struct stab_demangle_typestring *typestrings;
3777   /* The number of typestrings.  */
3778   unsigned int typestring_count;
3779   /* The number of typestring slots we have allocated.  */
3780   unsigned int typestring_alloc;
3781 };
3782
3783 static void stab_bad_demangle (const char *);
3784 static unsigned int stab_demangle_count (const char **);
3785 static bfd_boolean stab_demangle_get_count (const char **, unsigned int *);
3786 static bfd_boolean stab_demangle_prefix
3787   (struct stab_demangle_info *, const char **, unsigned int);
3788 static bfd_boolean stab_demangle_function_name
3789   (struct stab_demangle_info *, const char **, const char *);
3790 static bfd_boolean stab_demangle_signature
3791   (struct stab_demangle_info *, const char **);
3792 static bfd_boolean stab_demangle_qualified
3793   (struct stab_demangle_info *, const char **, debug_type *);
3794 static bfd_boolean stab_demangle_template
3795   (struct stab_demangle_info *, const char **, char **);
3796 static bfd_boolean stab_demangle_class
3797   (struct stab_demangle_info *, const char **, const char **);
3798 static bfd_boolean stab_demangle_args
3799   (struct stab_demangle_info *, const char **, debug_type **, bfd_boolean *);
3800 static bfd_boolean stab_demangle_arg
3801   (struct stab_demangle_info *, const char **, debug_type **,
3802    unsigned int *, unsigned int *);
3803 static bfd_boolean stab_demangle_type
3804   (struct stab_demangle_info *, const char **, debug_type *);
3805 static bfd_boolean stab_demangle_fund_type
3806   (struct stab_demangle_info *, const char **, debug_type *);
3807 static bfd_boolean stab_demangle_remember_type
3808   (struct stab_demangle_info *, const char *, int);
3809
3810 /* Warn about a bad demangling.  */
3811
3812 static void
3813 stab_bad_demangle (const char *s)
3814 {
3815   fprintf (stderr, _("bad mangled name `%s'\n"), s);
3816 }
3817
3818 /* Get a count from a stab string.  */
3819
3820 static unsigned int
3821 stab_demangle_count (const char **pp)
3822 {
3823   unsigned int count;
3824
3825   count = 0;
3826   while (ISDIGIT (**pp))
3827     {
3828       count *= 10;
3829       count += **pp - '0';
3830       ++*pp;
3831     }
3832   return count;
3833 }
3834
3835 /* Require a count in a string.  The count may be multiple digits, in
3836    which case it must end in an underscore.  */
3837
3838 static bfd_boolean
3839 stab_demangle_get_count (const char **pp, unsigned int *pi)
3840 {
3841   if (! ISDIGIT (**pp))
3842     return FALSE;
3843
3844   *pi = **pp - '0';
3845   ++*pp;
3846   if (ISDIGIT (**pp))
3847     {
3848       unsigned int count;
3849       const char *p;
3850
3851       count = *pi;
3852       p = *pp;
3853       do
3854         {
3855           count *= 10;
3856           count += *p - '0';
3857           ++p;
3858         }
3859       while (ISDIGIT (*p));
3860       if (*p == '_')
3861         {
3862           *pp = p + 1;
3863           *pi = count;
3864         }
3865     }
3866
3867   return TRUE;
3868 }
3869
3870 /* This function demangles a physical name, returning a NULL
3871    terminated array of argument types.  */
3872
3873 static debug_type *
3874 stab_demangle_argtypes (void *dhandle, struct stab_handle *info,
3875                         const char *physname, bfd_boolean *pvarargs,
3876                         unsigned int physname_len)
3877 {
3878   struct stab_demangle_info minfo;
3879
3880   /* Check for the g++ V3 ABI.  */
3881   if (physname[0] == '_' && physname[1] == 'Z')
3882     return stab_demangle_v3_argtypes (dhandle, info, physname, pvarargs);
3883
3884   minfo.dhandle = dhandle;
3885   minfo.info = info;
3886   minfo.args = NULL;
3887   minfo.varargs = FALSE;
3888   minfo.typestring_alloc = 10;
3889   minfo.typestrings = ((struct stab_demangle_typestring *)
3890                        xmalloc (minfo.typestring_alloc
3891                                 * sizeof *minfo.typestrings));
3892   minfo.typestring_count = 0;
3893
3894   /* cplus_demangle checks for special GNU mangled forms, but we can't
3895      see any of them in mangled method argument types.  */
3896
3897   if (! stab_demangle_prefix (&minfo, &physname, physname_len))
3898     goto error_return;
3899
3900   if (*physname != '\0')
3901     {
3902       if (! stab_demangle_signature (&minfo, &physname))
3903         goto error_return;
3904     }
3905
3906   free (minfo.typestrings);
3907   minfo.typestrings = NULL;
3908
3909   if (minfo.args == NULL)
3910     fprintf (stderr, _("no argument types in mangled string\n"));
3911
3912   *pvarargs = minfo.varargs;
3913   return minfo.args;
3914
3915  error_return:
3916   if (minfo.typestrings != NULL)
3917     free (minfo.typestrings);
3918   return NULL;
3919 }
3920
3921 /* Demangle the prefix of the mangled name.  */
3922
3923 static bfd_boolean
3924 stab_demangle_prefix (struct stab_demangle_info *minfo, const char **pp,
3925                       unsigned int physname_len)
3926 {
3927   const char *scan;
3928   unsigned int i;
3929
3930   /* cplus_demangle checks for global constructors and destructors,
3931      but we can't see them in mangled argument types.  */
3932
3933   if (physname_len)
3934     scan = *pp + physname_len;
3935   else
3936     {
3937       /* Look for `__'.  */
3938       scan = *pp;
3939       do
3940         scan = strchr (scan, '_');
3941       while (scan != NULL && *++scan != '_');
3942
3943       if (scan == NULL)
3944         {
3945           stab_bad_demangle (*pp);
3946           return FALSE;
3947         }
3948
3949       --scan;
3950
3951       /* We found `__'; move ahead to the last contiguous `__' pair.  */
3952       i = strspn (scan, "_");
3953       if (i > 2)
3954         scan += i - 2;
3955     }
3956
3957   if (scan == *pp
3958       && (ISDIGIT (scan[2])
3959           || scan[2] == 'Q'
3960           || scan[2] == 't'))
3961     {
3962       /* This is a GNU style constructor name.  */
3963       *pp = scan + 2;
3964       return TRUE;
3965     }
3966   else if (scan == *pp
3967            && ! ISDIGIT (scan[2])
3968            && scan[2] != 't')
3969     {
3970       /* Look for the `__' that separates the prefix from the
3971          signature.  */
3972       while (*scan == '_')
3973         ++scan;
3974       scan = strstr (scan, "__");
3975       if (scan == NULL || scan[2] == '\0')
3976         {
3977           stab_bad_demangle (*pp);
3978           return FALSE;
3979         }
3980
3981       return stab_demangle_function_name (minfo, pp, scan);
3982     }
3983   else if (scan[2] != '\0')
3984     {
3985       /* The name doesn't start with `__', but it does contain `__'.  */
3986       return stab_demangle_function_name (minfo, pp, scan);
3987     }
3988   else
3989     {
3990       stab_bad_demangle (*pp);
3991       return FALSE;
3992     }
3993   /*NOTREACHED*/
3994 }
3995
3996 /* Demangle a function name prefix.  The scan argument points to the
3997    double underscore which separates the function name from the
3998    signature.  */
3999
4000 static bfd_boolean
4001 stab_demangle_function_name (struct stab_demangle_info *minfo,
4002                              const char **pp, const char *scan)
4003 {
4004   const char *name;
4005
4006   /* The string from *pp to scan is the name of the function.  We
4007      don't care about the name, since we just looking for argument
4008      types.  However, for conversion operators, the name may include a
4009      type which we must remember in order to handle backreferences.  */
4010
4011   name = *pp;
4012   *pp = scan + 2;
4013
4014   if (*pp - name >= 5
4015            && CONST_STRNEQ (name, "type")
4016            && (name[4] == '$' || name[4] == '.'))
4017     {
4018       const char *tem;
4019
4020       /* This is a type conversion operator.  */
4021       tem = name + 5;
4022       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
4023         return FALSE;
4024     }
4025   else if (name[0] == '_'
4026            && name[1] == '_'
4027            && name[2] == 'o'
4028            && name[3] == 'p')
4029     {
4030       const char *tem;
4031
4032       /* This is a type conversion operator.  */
4033       tem = name + 4;
4034       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
4035         return FALSE;
4036     }
4037
4038   return TRUE;
4039 }
4040
4041 /* Demangle the signature.  This is where the argument types are
4042    found.  */
4043
4044 static bfd_boolean
4045 stab_demangle_signature (struct stab_demangle_info *minfo, const char **pp)
4046 {
4047   const char *orig;
4048   bfd_boolean expect_func, func_done;
4049   const char *hold;
4050
4051   orig = *pp;
4052
4053   expect_func = FALSE;
4054   func_done = FALSE;
4055   hold = NULL;
4056
4057   while (**pp != '\0')
4058     {
4059       switch (**pp)
4060         {
4061         case 'Q':
4062           hold = *pp;
4063           if (! stab_demangle_qualified (minfo, pp, (debug_type *) NULL)
4064               || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4065             return FALSE;
4066           expect_func = TRUE;
4067           hold = NULL;
4068           break;
4069
4070         case 'S':
4071           /* Static member function.  FIXME: Can this happen?  */
4072           if (hold == NULL)
4073             hold = *pp;
4074           ++*pp;
4075           break;
4076
4077         case 'C':
4078           /* Const member function.  */
4079           if (hold == NULL)
4080             hold = *pp;
4081           ++*pp;
4082           break;
4083
4084         case '0': case '1': case '2': case '3': case '4':
4085         case '5': case '6': case '7': case '8': case '9':
4086           if (hold == NULL)
4087             hold = *pp;
4088           if (! stab_demangle_class (minfo, pp, (const char **) NULL)
4089               || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4090             return FALSE;
4091           expect_func = TRUE;
4092           hold = NULL;
4093           break;
4094
4095         case 'F':
4096           /* Function.  I don't know if this actually happens with g++
4097              output.  */
4098           hold = NULL;
4099           func_done = TRUE;
4100           ++*pp;
4101           if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4102             return FALSE;
4103           break;
4104
4105         case 't':
4106           /* Template.  */
4107           if (hold == NULL)
4108             hold = *pp;
4109           if (! stab_demangle_template (minfo, pp, (char **) NULL)
4110               || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4111             return FALSE;
4112           hold = NULL;
4113           expect_func = TRUE;
4114           break;
4115
4116         case '_':
4117           /* At the outermost level, we cannot have a return type
4118              specified, so if we run into another '_' at this point we
4119              are dealing with a mangled name that is either bogus, or
4120              has been mangled by some algorithm we don't know how to
4121              deal with.  So just reject the entire demangling.  */
4122           stab_bad_demangle (orig);
4123           return FALSE;
4124
4125         default:
4126           /* Assume we have stumbled onto the first outermost function
4127              argument token, and start processing args.  */
4128           func_done = TRUE;
4129           if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4130             return FALSE;
4131           break;
4132         }
4133
4134       if (expect_func)
4135         {
4136           func_done = TRUE;
4137           if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4138             return FALSE;
4139         }
4140     }
4141
4142   if (! func_done)
4143     {
4144       /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
4145          bar__3fooi is 'foo::bar(int)'.  We get here when we find the
4146          first case, and need to ensure that the '(void)' gets added
4147          to the current declp.  */
4148       if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4149         return FALSE;
4150     }
4151
4152   return TRUE;
4153 }
4154
4155 /* Demangle a qualified name, such as "Q25Outer5Inner" which is the
4156    mangled form of "Outer::Inner".  */
4157
4158 static bfd_boolean
4159 stab_demangle_qualified (struct stab_demangle_info *minfo, const char **pp,
4160                          debug_type *ptype)
4161 {
4162   const char *orig;
4163   const char *p;
4164   unsigned int qualifiers;
4165   debug_type context;
4166
4167   orig = *pp;
4168
4169   switch ((*pp)[1])
4170     {
4171     case '_':
4172       /* GNU mangled name with more than 9 classes.  The count is
4173          preceded by an underscore (to distinguish it from the <= 9
4174          case) and followed by an underscore.  */
4175       p = *pp + 2;
4176       if (! ISDIGIT (*p) || *p == '0')
4177         {
4178           stab_bad_demangle (orig);
4179           return FALSE;
4180         }
4181       qualifiers = atoi (p);
4182       while (ISDIGIT (*p))
4183         ++p;
4184       if (*p != '_')
4185         {
4186           stab_bad_demangle (orig);
4187           return FALSE;
4188         }
4189       *pp = p + 1;
4190       break;
4191
4192     case '1': case '2': case '3': case '4': case '5':
4193     case '6': case '7': case '8': case '9':
4194       qualifiers = (*pp)[1] - '0';
4195       /* Skip an optional underscore after the count.  */
4196       if ((*pp)[2] == '_')
4197         ++*pp;
4198       *pp += 2;
4199       break;
4200
4201     case '0':
4202     default:
4203       stab_bad_demangle (orig);
4204       return FALSE;
4205     }
4206
4207   context = DEBUG_TYPE_NULL;
4208
4209   /* Pick off the names.  */
4210   while (qualifiers-- > 0)
4211     {
4212       if (**pp == '_')
4213         ++*pp;
4214       if (**pp == 't')
4215         {
4216           char *name;
4217
4218           if (! stab_demangle_template (minfo, pp,
4219                                         ptype != NULL ? &name : NULL))
4220             return FALSE;
4221
4222           if (ptype != NULL)
4223             {
4224               context = stab_find_tagged_type (minfo->dhandle, minfo->info,
4225                                                name, strlen (name),
4226                                                DEBUG_KIND_CLASS);
4227               free (name);
4228               if (context == DEBUG_TYPE_NULL)
4229                 return FALSE;
4230             }
4231         }
4232       else
4233         {
4234           unsigned int len;
4235
4236           len = stab_demangle_count (pp);
4237           if (strlen (*pp) < len)
4238             {
4239               stab_bad_demangle (orig);
4240               return FALSE;
4241             }
4242
4243           if (ptype != NULL)
4244             {
4245               const debug_field *fields;
4246
4247               fields = NULL;
4248               if (context != DEBUG_TYPE_NULL)
4249                 fields = debug_get_fields (minfo->dhandle, context);
4250
4251               context = DEBUG_TYPE_NULL;
4252
4253               if (fields != NULL)
4254                 {
4255                   char *name;
4256
4257                   /* Try to find the type by looking through the
4258                      fields of context until we find a field with the
4259                      same type.  This ought to work for a class
4260                      defined within a class, but it won't work for,
4261                      e.g., an enum defined within a class.  stabs does
4262                      not give us enough information to figure out the
4263                      latter case.  */
4264
4265                   name = savestring (*pp, len);
4266
4267                   for (; *fields != DEBUG_FIELD_NULL; fields++)
4268                     {
4269                       debug_type ft;
4270                       const char *dn;
4271
4272                       ft = debug_get_field_type (minfo->dhandle, *fields);
4273                       if (ft == NULL)
4274                         {
4275                           free (name);
4276                           return FALSE;
4277                         }
4278                       dn = debug_get_type_name (minfo->dhandle, ft);
4279                       if (dn != NULL && strcmp (dn, name) == 0)
4280                         {
4281                           context = ft;
4282                           break;
4283                         }
4284                     }
4285
4286                   free (name);
4287                 }
4288
4289               if (context == DEBUG_TYPE_NULL)
4290                 {
4291                   /* We have to fall back on finding the type by name.
4292                      If there are more types to come, then this must
4293                      be a class.  Otherwise, it could be anything.  */
4294
4295                   if (qualifiers == 0)
4296                     {
4297                       char *name;
4298
4299                       name = savestring (*pp, len);
4300                       context = debug_find_named_type (minfo->dhandle,
4301                                                        name);
4302                       free (name);
4303                     }
4304
4305                   if (context == DEBUG_TYPE_NULL)
4306                     {
4307                       context = stab_find_tagged_type (minfo->dhandle,
4308                                                        minfo->info,
4309                                                        *pp, len,
4310                                                        (qualifiers == 0
4311                                                         ? DEBUG_KIND_ILLEGAL
4312                                                         : DEBUG_KIND_CLASS));
4313                       if (context == DEBUG_TYPE_NULL)
4314                         return FALSE;
4315                     }
4316                 }
4317             }
4318
4319           *pp += len;
4320         }
4321     }
4322
4323   if (ptype != NULL)
4324     *ptype = context;
4325
4326   return TRUE;
4327 }
4328
4329 /* Demangle a template.  If PNAME is not NULL, this sets *PNAME to a
4330    string representation of the template.  */
4331
4332 static bfd_boolean
4333 stab_demangle_template (struct stab_demangle_info *minfo, const char **pp,
4334                         char **pname)
4335 {
4336   const char *orig;
4337   unsigned int r, i;
4338
4339   orig = *pp;
4340
4341   ++*pp;
4342
4343   /* Skip the template name.  */
4344   r = stab_demangle_count (pp);
4345   if (r == 0 || strlen (*pp) < r)
4346     {
4347       stab_bad_demangle (orig);
4348       return FALSE;
4349     }
4350   *pp += r;
4351
4352   /* Get the size of the parameter list.  */
4353   if (stab_demangle_get_count (pp, &r) == 0)
4354     {
4355       stab_bad_demangle (orig);
4356       return FALSE;
4357     }
4358
4359   for (i = 0; i < r; i++)
4360     {
4361       if (**pp == 'Z')
4362         {
4363           /* This is a type parameter.  */
4364           ++*pp;
4365           if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4366             return FALSE;
4367         }
4368       else
4369         {
4370           const char *old_p;
4371           bfd_boolean pointerp, realp, integralp, charp, boolp;
4372           bfd_boolean done;
4373
4374           old_p = *pp;
4375           pointerp = FALSE;
4376           realp = FALSE;
4377           integralp = FALSE;
4378           charp = FALSE;
4379           boolp = FALSE;
4380           done = FALSE;
4381
4382           /* This is a value parameter.  */
4383
4384           if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4385             return FALSE;
4386
4387           while (*old_p != '\0' && ! done)
4388             {
4389               switch (*old_p)
4390                 {
4391                 case 'P':
4392                 case 'p':
4393                 case 'R':
4394                   pointerp = TRUE;
4395                   done = TRUE;
4396                   break;
4397                 case 'C':       /* Const.  */
4398                 case 'S':       /* Signed.  */
4399                 case 'U':       /* Unsigned.  */
4400                 case 'V':       /* Volatile.  */
4401                 case 'F':       /* Function.  */
4402                 case 'M':       /* Member function.  */
4403                 case 'O':       /* ??? */
4404                   ++old_p;
4405                   break;
4406                 case 'Q':       /* Qualified name.  */
4407                   integralp = TRUE;
4408                   done = TRUE;
4409                   break;
4410                 case 'T':       /* Remembered type.  */
4411                   abort ();
4412                 case 'v':       /* Void.  */
4413                   abort ();
4414                 case 'x':       /* Long long.  */
4415                 case 'l':       /* Long.  */
4416                 case 'i':       /* Int.  */
4417                 case 's':       /* Short.  */
4418                 case 'w':       /* Wchar_t.  */
4419                   integralp = TRUE;
4420                   done = TRUE;
4421                   break;
4422                 case 'b':       /* Bool.  */
4423                   boolp = TRUE;
4424                   done = TRUE;
4425                   break;
4426                 case 'c':       /* Char.  */
4427                   charp = TRUE;
4428                   done = TRUE;
4429                   break;
4430                 case 'r':       /* Long double.  */
4431                 case 'd':       /* Double.  */
4432                 case 'f':       /* Float.  */
4433                   realp = TRUE;
4434                   done = TRUE;
4435                   break;
4436                 default:
4437                   /* Assume it's a user defined integral type.  */
4438                   integralp = TRUE;
4439                   done = TRUE;
4440                   break;
4441                 }
4442             }
4443
4444           if (integralp)
4445             {
4446               if (**pp == 'm')
4447                 ++*pp;
4448               while (ISDIGIT (**pp))
4449                 ++*pp;
4450             }
4451           else if (charp)
4452             {
4453               unsigned int val;
4454
4455               if (**pp == 'm')
4456                 ++*pp;
4457               val = stab_demangle_count (pp);
4458               if (val == 0)
4459                 {
4460                   stab_bad_demangle (orig);
4461                   return FALSE;
4462                 }
4463             }
4464           else if (boolp)
4465             {
4466               unsigned int val;
4467
4468               val = stab_demangle_count (pp);
4469               if (val != 0 && val != 1)
4470                 {
4471                   stab_bad_demangle (orig);
4472                   return FALSE;
4473                 }
4474             }
4475           else if (realp)
4476             {
4477               if (**pp == 'm')
4478                 ++*pp;
4479               while (ISDIGIT (**pp))
4480                 ++*pp;
4481               if (**pp == '.')
4482                 {
4483                   ++*pp;
4484                   while (ISDIGIT (**pp))
4485                     ++*pp;
4486                 }
4487               if (**pp == 'e')
4488                 {
4489                   ++*pp;
4490                   while (ISDIGIT (**pp))
4491                     ++*pp;
4492                 }
4493             }
4494           else if (pointerp)
4495             {
4496               unsigned int len;
4497
4498               len = stab_demangle_count (pp);
4499               if (len == 0)
4500                 {
4501                   stab_bad_demangle (orig);
4502                   return FALSE;
4503                 }
4504               *pp += len;
4505             }
4506         }
4507     }
4508
4509   /* We can translate this to a string fairly easily by invoking the
4510      regular demangling routine.  */
4511   if (pname != NULL)
4512     {
4513       char *s1, *s2, *s3, *s4 = NULL;
4514       char *from, *to;
4515
4516       s1 = savestring (orig, *pp - orig);
4517
4518       s2 = concat ("NoSuchStrinG__", s1, (const char *) NULL);
4519
4520       free (s1);
4521
4522       s3 = cplus_demangle (s2, demangle_flags);
4523
4524       free (s2);
4525
4526       if (s3 != NULL)
4527         s4 = strstr (s3, "::NoSuchStrinG");
4528       if (s3 == NULL || s4 == NULL)
4529         {
4530           stab_bad_demangle (orig);
4531           if (s3 != NULL)
4532             free (s3);
4533           return FALSE;
4534         }
4535
4536       /* Eliminating all spaces, except those between > characters,
4537          makes it more likely that the demangled name will match the
4538          name which g++ used as the structure name.  */
4539       for (from = to = s3; from != s4; ++from)
4540         if (*from != ' '
4541             || (from[1] == '>' && from > s3 && from[-1] == '>'))
4542           *to++ = *from;
4543
4544       *pname = savestring (s3, to - s3);
4545
4546       free (s3);
4547     }
4548
4549   return TRUE;
4550 }
4551
4552 /* Demangle a class name.  */
4553
4554 static bfd_boolean
4555 stab_demangle_class (struct stab_demangle_info *minfo ATTRIBUTE_UNUSED,
4556                      const char **pp, const char **pstart)
4557 {
4558   const char *orig;
4559   unsigned int n;
4560
4561   orig = *pp;
4562
4563   n = stab_demangle_count (pp);
4564   if (strlen (*pp) < n)
4565     {
4566       stab_bad_demangle (orig);
4567       return FALSE;
4568     }
4569
4570   if (pstart != NULL)
4571     *pstart = *pp;
4572
4573   *pp += n;
4574
4575   return TRUE;
4576 }
4577
4578 /* Demangle function arguments.  If the pargs argument is not NULL, it
4579    is set to a NULL terminated array holding the arguments.  */
4580
4581 static bfd_boolean
4582 stab_demangle_args (struct stab_demangle_info *minfo, const char **pp,
4583                     debug_type **pargs, bfd_boolean *pvarargs)
4584 {
4585   const char *orig;
4586   unsigned int alloc, count;
4587
4588   orig = *pp;
4589
4590   alloc = 10;
4591   if (pargs != NULL)
4592     {
4593       *pargs = (debug_type *) xmalloc (alloc * sizeof **pargs);
4594       *pvarargs = FALSE;
4595     }
4596   count = 0;
4597
4598   while (**pp != '_' && **pp != '\0' && **pp != 'e')
4599     {
4600       if (**pp == 'N' || **pp == 'T')
4601         {
4602           char temptype;
4603           unsigned int r, t;
4604
4605           temptype = **pp;
4606           ++*pp;
4607
4608           if (temptype == 'T')
4609             r = 1;
4610           else
4611             {
4612               if (! stab_demangle_get_count (pp, &r))
4613                 {
4614                   stab_bad_demangle (orig);
4615                   return FALSE;
4616                 }
4617             }
4618
4619           if (! stab_demangle_get_count (pp, &t))
4620             {
4621               stab_bad_demangle (orig);
4622               return FALSE;
4623             }
4624
4625           if (t >= minfo->typestring_count)
4626             {
4627               stab_bad_demangle (orig);
4628               return FALSE;
4629             }
4630           while (r-- > 0)
4631             {
4632               const char *tem;
4633
4634               tem = minfo->typestrings[t].typestring;
4635               if (! stab_demangle_arg (minfo, &tem, pargs, &count, &alloc))
4636                 return FALSE;
4637             }
4638         }
4639       else
4640         {
4641           if (! stab_demangle_arg (minfo, pp, pargs, &count, &alloc))
4642             return FALSE;
4643         }
4644     }
4645
4646   if (pargs != NULL)
4647     (*pargs)[count] = DEBUG_TYPE_NULL;
4648
4649   if (**pp == 'e')
4650     {
4651       if (pargs != NULL)
4652         *pvarargs = TRUE;
4653       ++*pp;
4654     }
4655
4656   return TRUE;
4657 }
4658
4659 /* Demangle a single argument.  */
4660
4661 static bfd_boolean
4662 stab_demangle_arg (struct stab_demangle_info *minfo, const char **pp,
4663                    debug_type **pargs, unsigned int *pcount,
4664                    unsigned int *palloc)
4665 {
4666   const char *start;
4667   debug_type type;
4668
4669   start = *pp;
4670   if (! stab_demangle_type (minfo, pp,
4671                             pargs == NULL ? (debug_type *) NULL : &type)
4672       || ! stab_demangle_remember_type (minfo, start, *pp - start))
4673     return FALSE;
4674
4675   if (pargs != NULL)
4676     {
4677       if (type == DEBUG_TYPE_NULL)
4678         return FALSE;
4679
4680       if (*pcount + 1 >= *palloc)
4681         {
4682           *palloc += 10;
4683           *pargs = ((debug_type *)
4684                     xrealloc (*pargs, *palloc * sizeof **pargs));
4685         }
4686       (*pargs)[*pcount] = type;
4687       ++*pcount;
4688     }
4689
4690   return TRUE;
4691 }
4692
4693 /* Demangle a type.  If the ptype argument is not NULL, *ptype is set
4694    to the newly allocated type.  */
4695
4696 static bfd_boolean
4697 stab_demangle_type (struct stab_demangle_info *minfo, const char **pp,
4698                     debug_type *ptype)
4699 {
4700   const char *orig;
4701
4702   orig = *pp;
4703
4704   switch (**pp)
4705     {
4706     case 'P':
4707     case 'p':
4708       /* A pointer type.  */
4709       ++*pp;
4710       if (! stab_demangle_type (minfo, pp, ptype))
4711         return FALSE;
4712       if (ptype != NULL)
4713         *ptype = debug_make_pointer_type (minfo->dhandle, *ptype);
4714       break;
4715
4716     case 'R':
4717       /* A reference type.  */
4718       ++*pp;
4719       if (! stab_demangle_type (minfo, pp, ptype))
4720         return FALSE;
4721       if (ptype != NULL)
4722         *ptype = debug_make_reference_type (minfo->dhandle, *ptype);
4723       break;
4724
4725     case 'A':
4726       /* An array.  */
4727       {
4728         unsigned long high;
4729
4730         ++*pp;
4731         high = 0;
4732         while (**pp != '\0' && **pp != '_')
4733           {
4734             if (! ISDIGIT (**pp))
4735               {
4736                 stab_bad_demangle (orig);
4737                 return FALSE;
4738               }
4739             high *= 10;
4740             high += **pp - '0';
4741             ++*pp;
4742           }
4743         if (**pp != '_')
4744           {
4745             stab_bad_demangle (orig);
4746             return FALSE;
4747           }
4748         ++*pp;
4749
4750         if (! stab_demangle_type (minfo, pp, ptype))
4751           return FALSE;
4752         if (ptype != NULL)
4753           {
4754             debug_type int_type;
4755
4756             int_type = debug_find_named_type (minfo->dhandle, "int");
4757             if (int_type == NULL)
4758               int_type = debug_make_int_type (minfo->dhandle, 4, FALSE);
4759             *ptype = debug_make_array_type (minfo->dhandle, *ptype, int_type,
4760                                             0, high, FALSE);
4761           }
4762       }
4763       break;
4764
4765     case 'T':
4766       /* A back reference to a remembered type.  */
4767       {
4768         unsigned int i;
4769         const char *p;
4770
4771         ++*pp;
4772         if (! stab_demangle_get_count (pp, &i))
4773           {
4774             stab_bad_demangle (orig);
4775             return FALSE;
4776           }
4777         if (i >= minfo->typestring_count)
4778           {
4779             stab_bad_demangle (orig);
4780             return FALSE;
4781           }
4782         p = minfo->typestrings[i].typestring;
4783         if (! stab_demangle_type (minfo, &p, ptype))
4784           return FALSE;
4785       }
4786       break;
4787
4788     case 'F':
4789       /* A function.  */
4790       {
4791         debug_type *args;
4792         bfd_boolean varargs;
4793
4794         ++*pp;
4795         if (! stab_demangle_args (minfo, pp,
4796                                   (ptype == NULL
4797                                    ? (debug_type **) NULL
4798                                    : &args),
4799                                   (ptype == NULL
4800                                    ? (bfd_boolean *) NULL
4801                                    : &varargs)))
4802           return FALSE;
4803         if (**pp != '_')
4804           {
4805             /* cplus_demangle will accept a function without a return
4806                type, but I don't know when that will happen, or what
4807                to do if it does.  */
4808             stab_bad_demangle (orig);
4809             return FALSE;
4810           }
4811         ++*pp;
4812         if (! stab_demangle_type (minfo, pp, ptype))
4813           return FALSE;
4814         if (ptype != NULL)
4815           *ptype = debug_make_function_type (minfo->dhandle, *ptype, args,
4816                                              varargs);
4817
4818       }
4819       break;
4820
4821     case 'M':
4822     case 'O':
4823       {
4824         bfd_boolean memberp;
4825         debug_type class_type = DEBUG_TYPE_NULL;
4826         debug_type *args;
4827         bfd_boolean varargs;
4828         unsigned int n;
4829         const char *name;
4830
4831         memberp = **pp == 'M';
4832         args = NULL;
4833         varargs = FALSE;
4834
4835         ++*pp;
4836         if (ISDIGIT (**pp))
4837           {
4838             n = stab_demangle_count (pp);
4839             if (strlen (*pp) < n)
4840               {
4841                 stab_bad_demangle (orig);
4842                 return FALSE;
4843               }
4844             name = *pp;
4845             *pp += n;
4846
4847             if (ptype != NULL)
4848               {
4849                 class_type = stab_find_tagged_type (minfo->dhandle,
4850                                                     minfo->info,
4851                                                     name, (int) n,
4852                                                     DEBUG_KIND_CLASS);
4853                 if (class_type == DEBUG_TYPE_NULL)
4854                   return FALSE;
4855               }
4856           }
4857         else if (**pp == 'Q')
4858           {
4859             if (! stab_demangle_qualified (minfo, pp,
4860                                            (ptype == NULL
4861                                             ? (debug_type *) NULL
4862                                             : &class_type)))
4863               return FALSE;
4864           }
4865         else
4866           {
4867             stab_bad_demangle (orig);
4868             return FALSE;
4869           }
4870
4871         if (memberp)
4872           {
4873             if (**pp == 'C')
4874               {
4875                 ++*pp;
4876               }
4877             else if (**pp == 'V')
4878               {
4879                 ++*pp;
4880               }
4881             if (**pp != 'F')
4882               {
4883                 stab_bad_demangle (orig);
4884                 return FALSE;
4885               }
4886             ++*pp;
4887             if (! stab_demangle_args (minfo, pp,
4888                                       (ptype == NULL
4889                                        ? (debug_type **) NULL
4890                                        : &args),
4891                                       (ptype == NULL
4892                                        ? (bfd_boolean *) NULL
4893                                        : &varargs)))
4894               return FALSE;
4895           }
4896
4897         if (**pp != '_')
4898           {
4899             stab_bad_demangle (orig);
4900             return FALSE;
4901           }
4902         ++*pp;
4903
4904         if (! stab_demangle_type (minfo, pp, ptype))
4905           return FALSE;
4906
4907         if (ptype != NULL)
4908           {
4909             if (! memberp)
4910               *ptype = debug_make_offset_type (minfo->dhandle, class_type,
4911                                                *ptype);
4912             else
4913               {
4914                 /* FIXME: We have no way to record constp or
4915                    volatilep.  */
4916                 *ptype = debug_make_method_type (minfo->dhandle, *ptype,
4917                                                  class_type, args, varargs);
4918               }
4919           }
4920       }
4921       break;
4922
4923     case 'G':
4924       ++*pp;
4925       if (! stab_demangle_type (minfo, pp, ptype))
4926         return FALSE;
4927       break;
4928
4929     case 'C':
4930       ++*pp;
4931       if (! stab_demangle_type (minfo, pp, ptype))
4932         return FALSE;
4933       if (ptype != NULL)
4934         *ptype = debug_make_const_type (minfo->dhandle, *ptype);
4935       break;
4936
4937     case 'Q':
4938       {
4939         if (! stab_demangle_qualified (minfo, pp, ptype))
4940           return FALSE;
4941       }
4942       break;
4943
4944     default:
4945       if (! stab_demangle_fund_type (minfo, pp, ptype))
4946         return FALSE;
4947       break;
4948     }
4949
4950   return TRUE;
4951 }
4952
4953 /* Demangle a fundamental type.  If the ptype argument is not NULL,
4954    *ptype is set to the newly allocated type.  */
4955
4956 static bfd_boolean
4957 stab_demangle_fund_type (struct stab_demangle_info *minfo, const char **pp,
4958                          debug_type *ptype)
4959 {
4960   const char *orig;
4961   bfd_boolean constp, volatilep, unsignedp, signedp;
4962   bfd_boolean done;
4963
4964   orig = *pp;
4965
4966   constp = FALSE;
4967   volatilep = FALSE;
4968   unsignedp = FALSE;
4969   signedp = FALSE;
4970
4971   done = FALSE;
4972   while (! done)
4973     {
4974       switch (**pp)
4975         {
4976         case 'C':
4977           constp = TRUE;
4978           ++*pp;
4979           break;
4980
4981         case 'U':
4982           unsignedp = TRUE;
4983           ++*pp;
4984           break;
4985
4986         case 'S':
4987           signedp = TRUE;
4988           ++*pp;
4989           break;
4990
4991         case 'V':
4992           volatilep = TRUE;
4993           ++*pp;
4994           break;
4995
4996         default:
4997           done = TRUE;
4998           break;
4999         }
5000     }
5001
5002   switch (**pp)
5003     {
5004     case '\0':
5005     case '_':
5006       /* cplus_demangle permits this, but I don't know what it means.  */
5007       stab_bad_demangle (orig);
5008       break;
5009
5010     case 'v': /* void */
5011       if (ptype != NULL)
5012         {
5013           *ptype = debug_find_named_type (minfo->dhandle, "void");
5014           if (*ptype == DEBUG_TYPE_NULL)
5015             *ptype = debug_make_void_type (minfo->dhandle);
5016         }
5017       ++*pp;
5018       break;
5019
5020     case 'x': /* long long */
5021       if (ptype != NULL)
5022         {
5023           *ptype = debug_find_named_type (minfo->dhandle,
5024                                           (unsignedp
5025                                            ? "long long unsigned int"
5026                                            : "long long int"));
5027           if (*ptype == DEBUG_TYPE_NULL)
5028             *ptype = debug_make_int_type (minfo->dhandle, 8, unsignedp);
5029         }
5030       ++*pp;
5031       break;
5032
5033     case 'l': /* long */
5034       if (ptype != NULL)
5035         {
5036           *ptype = debug_find_named_type (minfo->dhandle,
5037                                           (unsignedp
5038                                            ? "long unsigned int"
5039                                            : "long int"));
5040           if (*ptype == DEBUG_TYPE_NULL)
5041             *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5042         }
5043       ++*pp;
5044       break;
5045
5046     case 'i': /* int */
5047       if (ptype != NULL)
5048         {
5049           *ptype = debug_find_named_type (minfo->dhandle,
5050                                           (unsignedp
5051                                            ? "unsigned int"
5052                                            : "int"));
5053           if (*ptype == DEBUG_TYPE_NULL)
5054             *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5055         }
5056       ++*pp;
5057       break;
5058
5059     case 's': /* short */
5060       if (ptype != NULL)
5061         {
5062           *ptype = debug_find_named_type (minfo->dhandle,
5063                                           (unsignedp
5064                                            ? "short unsigned int"
5065                                            : "short int"));
5066           if (*ptype == DEBUG_TYPE_NULL)
5067             *ptype = debug_make_int_type (minfo->dhandle, 2, unsignedp);
5068         }
5069       ++*pp;
5070       break;
5071
5072     case 'b': /* bool */
5073       if (ptype != NULL)
5074         {
5075           *ptype = debug_find_named_type (minfo->dhandle, "bool");
5076           if (*ptype == DEBUG_TYPE_NULL)
5077             *ptype = debug_make_bool_type (minfo->dhandle, 4);
5078         }
5079       ++*pp;
5080       break;
5081
5082     case 'c': /* char */
5083       if (ptype != NULL)
5084         {
5085           *ptype = debug_find_named_type (minfo->dhandle,
5086                                           (unsignedp
5087                                            ? "unsigned char"
5088                                            : (signedp
5089                                               ? "signed char"
5090                                               : "char")));
5091           if (*ptype == DEBUG_TYPE_NULL)
5092             *ptype = debug_make_int_type (minfo->dhandle, 1, unsignedp);
5093         }
5094       ++*pp;
5095       break;
5096
5097     case 'w': /* wchar_t */
5098       if (ptype != NULL)
5099         {
5100           *ptype = debug_find_named_type (minfo->dhandle, "__wchar_t");
5101           if (*ptype == DEBUG_TYPE_NULL)
5102             *ptype = debug_make_int_type (minfo->dhandle, 2, TRUE);
5103         }
5104       ++*pp;
5105       break;
5106
5107     case 'r': /* long double */
5108       if (ptype != NULL)
5109         {
5110           *ptype = debug_find_named_type (minfo->dhandle, "long double");
5111           if (*ptype == DEBUG_TYPE_NULL)
5112             *ptype = debug_make_float_type (minfo->dhandle, 8);
5113         }
5114       ++*pp;
5115       break;
5116
5117     case 'd': /* double */
5118       if (ptype != NULL)
5119         {
5120           *ptype = debug_find_named_type (minfo->dhandle, "double");
5121           if (*ptype == DEBUG_TYPE_NULL)
5122             *ptype = debug_make_float_type (minfo->dhandle, 8);
5123         }
5124       ++*pp;
5125       break;
5126
5127     case 'f': /* float */
5128       if (ptype != NULL)
5129         {
5130           *ptype = debug_find_named_type (minfo->dhandle, "float");
5131           if (*ptype == DEBUG_TYPE_NULL)
5132             *ptype = debug_make_float_type (minfo->dhandle, 4);
5133         }
5134       ++*pp;
5135       break;
5136
5137     case 'G':
5138       ++*pp;
5139       if (! ISDIGIT (**pp))
5140         {
5141           stab_bad_demangle (orig);
5142           return FALSE;
5143         }
5144       /* Fall through.  */
5145     case '0': case '1': case '2': case '3': case '4':
5146     case '5': case '6': case '7': case '8': case '9':
5147       {
5148         const char *hold;
5149
5150         if (! stab_demangle_class (minfo, pp, &hold))
5151           return FALSE;
5152         if (ptype != NULL)
5153           {
5154             char *name;
5155
5156             name = savestring (hold, *pp - hold);
5157             *ptype = debug_find_named_type (minfo->dhandle, name);
5158             free (name);
5159             if (*ptype == DEBUG_TYPE_NULL)
5160               {
5161                 /* FIXME: It is probably incorrect to assume that
5162                    undefined types are tagged types.  */
5163                 *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5164                                                 hold, *pp - hold,
5165                                                 DEBUG_KIND_ILLEGAL);
5166                 if (*ptype == DEBUG_TYPE_NULL)
5167                   return FALSE;
5168               }
5169           }
5170       }
5171       break;
5172
5173     case 't':
5174       {
5175         char *name;
5176
5177         if (! stab_demangle_template (minfo, pp,
5178                                       ptype != NULL ? &name : NULL))
5179           return FALSE;
5180         if (ptype != NULL)
5181           {
5182             *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5183                                             name, strlen (name),
5184                                             DEBUG_KIND_CLASS);
5185             free (name);
5186             if (*ptype == DEBUG_TYPE_NULL)
5187               return FALSE;
5188           }
5189       }
5190       break;
5191
5192     default:
5193       stab_bad_demangle (orig);
5194       return FALSE;
5195     }
5196
5197   if (ptype != NULL)
5198     {
5199       if (constp)
5200         *ptype = debug_make_const_type (minfo->dhandle, *ptype);
5201       if (volatilep)
5202         *ptype = debug_make_volatile_type (minfo->dhandle, *ptype);
5203     }
5204
5205   return TRUE;
5206 }
5207
5208 /* Remember a type string in a demangled string.  */
5209
5210 static bfd_boolean
5211 stab_demangle_remember_type (struct stab_demangle_info *minfo,
5212                              const char *p, int len)
5213 {
5214   if (minfo->typestring_count >= minfo->typestring_alloc)
5215     {
5216       minfo->typestring_alloc += 10;
5217       minfo->typestrings = ((struct stab_demangle_typestring *)
5218                             xrealloc (minfo->typestrings,
5219                                       (minfo->typestring_alloc
5220                                        * sizeof *minfo->typestrings)));
5221     }
5222
5223   minfo->typestrings[minfo->typestring_count].typestring = p;
5224   minfo->typestrings[minfo->typestring_count].len = (unsigned int) len;
5225   ++minfo->typestring_count;
5226
5227   return TRUE;
5228 }
5229 \f
5230 /* Demangle names encoded using the g++ V3 ABI.  The newer versions of
5231    g++ which use this ABI do not encode ordinary method argument types
5232    in a mangled name; they simply output the argument types.  However,
5233    for a static method, g++ simply outputs the return type and the
5234    physical name.  So in that case we need to demangle the name here.
5235    Here PHYSNAME is the physical name of the function, and we set the
5236    variable pointed at by PVARARGS to indicate whether this function
5237    is varargs.  This returns NULL, or a NULL terminated array of
5238    argument types.  */
5239
5240 static debug_type *
5241 stab_demangle_v3_argtypes (void *dhandle, struct stab_handle *info,
5242                            const char *physname, bfd_boolean *pvarargs)
5243 {
5244   struct demangle_component *dc;
5245   void *mem;
5246   debug_type *pargs;
5247
5248   dc = cplus_demangle_v3_components (physname, DMGL_PARAMS | demangle_flags, &mem);
5249   if (dc == NULL)
5250     {
5251       stab_bad_demangle (physname);
5252       return NULL;
5253     }
5254
5255   /* We expect to see TYPED_NAME, and the right subtree describes the
5256      function type.  */
5257   if (dc->type != DEMANGLE_COMPONENT_TYPED_NAME
5258       || dc->u.s_binary.right->type != DEMANGLE_COMPONENT_FUNCTION_TYPE)
5259     {
5260       fprintf (stderr, _("Demangled name is not a function\n"));
5261       free (mem);
5262       return NULL;
5263     }
5264
5265   pargs = stab_demangle_v3_arglist (dhandle, info,
5266                                     dc->u.s_binary.right->u.s_binary.right,
5267                                     pvarargs);
5268
5269   free (mem);
5270
5271   return pargs;
5272 }
5273
5274 /* Demangle an argument list in a struct demangle_component tree.
5275    Returns a DEBUG_TYPE_NULL terminated array of argument types, and
5276    sets *PVARARGS to indicate whether this is a varargs function.  */
5277
5278 static debug_type *
5279 stab_demangle_v3_arglist (void *dhandle, struct stab_handle *info,
5280                           struct demangle_component *arglist,
5281                           bfd_boolean *pvarargs)
5282 {
5283   struct demangle_component *dc;
5284   unsigned int alloc, count;
5285   debug_type *pargs;
5286
5287   alloc = 10;
5288   pargs = (debug_type *) xmalloc (alloc * sizeof *pargs);
5289   *pvarargs = FALSE;
5290
5291   count = 0;
5292
5293   for (dc = arglist;
5294        dc != NULL;
5295        dc = dc->u.s_binary.right)
5296     {
5297       debug_type arg;
5298       bfd_boolean varargs;
5299
5300       if (dc->type != DEMANGLE_COMPONENT_ARGLIST)
5301         {
5302           fprintf (stderr, _("Unexpected type in v3 arglist demangling\n"));
5303           free (pargs);
5304           return NULL;
5305         }
5306
5307       /* PR 13925: Cope if the demangler returns an empty
5308          context for a function with no arguments.  */
5309       if (dc->u.s_binary.left == NULL)
5310         break;
5311
5312       arg = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left,
5313                                   NULL, &varargs);
5314       if (arg == NULL)
5315         {
5316           if (varargs)
5317             {
5318               *pvarargs = TRUE;
5319               continue;
5320             }
5321           free (pargs);
5322           return NULL;
5323         }
5324
5325       if (count + 1 >= alloc)
5326         {
5327           alloc += 10;
5328           pargs = (debug_type *) xrealloc (pargs, alloc * sizeof *pargs);
5329         }
5330
5331       pargs[count] = arg;
5332       ++count;
5333     }
5334
5335   pargs[count] = DEBUG_TYPE_NULL;
5336
5337   return pargs;
5338 }
5339
5340 /* Convert a struct demangle_component tree describing an argument
5341    type into a debug_type.  */
5342
5343 static debug_type
5344 stab_demangle_v3_arg (void *dhandle, struct stab_handle *info,
5345                       struct demangle_component *dc, debug_type context,
5346                       bfd_boolean *pvarargs)
5347 {
5348   debug_type dt;
5349
5350   if (pvarargs != NULL)
5351     *pvarargs = FALSE;
5352
5353   switch (dc->type)
5354     {
5355       /* FIXME: These are demangle component types which we probably
5356          need to handle one way or another.  */
5357     case DEMANGLE_COMPONENT_LOCAL_NAME:
5358     case DEMANGLE_COMPONENT_TYPED_NAME:
5359     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
5360     case DEMANGLE_COMPONENT_CTOR:
5361     case DEMANGLE_COMPONENT_DTOR:
5362     case DEMANGLE_COMPONENT_JAVA_CLASS:
5363     case DEMANGLE_COMPONENT_RESTRICT_THIS:
5364     case DEMANGLE_COMPONENT_VOLATILE_THIS:
5365     case DEMANGLE_COMPONENT_CONST_THIS:
5366     case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL:
5367     case DEMANGLE_COMPONENT_COMPLEX:
5368     case DEMANGLE_COMPONENT_IMAGINARY:
5369     case DEMANGLE_COMPONENT_VENDOR_TYPE:
5370     case DEMANGLE_COMPONENT_ARRAY_TYPE:
5371     case DEMANGLE_COMPONENT_PTRMEM_TYPE:
5372     case DEMANGLE_COMPONENT_ARGLIST:
5373     default:
5374       fprintf (stderr, _("Unrecognized demangle component %d\n"),
5375                (int) dc->type);
5376       return NULL;
5377
5378     case DEMANGLE_COMPONENT_NAME:
5379       if (context != NULL)
5380         {
5381           const debug_field *fields;
5382
5383           fields = debug_get_fields (dhandle, context);
5384           if (fields != NULL)
5385             {
5386               /* Try to find this type by looking through the context
5387                  class.  */
5388               for (; *fields != DEBUG_FIELD_NULL; fields++)
5389                 {
5390                   debug_type ft;
5391                   const char *dn;
5392
5393                   ft = debug_get_field_type (dhandle, *fields);
5394                   if (ft == NULL)
5395                     return NULL;
5396                   dn = debug_get_type_name (dhandle, ft);
5397                   if (dn != NULL
5398                       && (int) strlen (dn) == dc->u.s_name.len
5399                       && strncmp (dn, dc->u.s_name.s, dc->u.s_name.len) == 0)
5400                     return ft;
5401                 }
5402             }
5403         }
5404       return stab_find_tagged_type (dhandle, info, dc->u.s_name.s,
5405                                     dc->u.s_name.len, DEBUG_KIND_ILLEGAL);
5406
5407     case DEMANGLE_COMPONENT_QUAL_NAME:
5408       context = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left,
5409                                       context, NULL);
5410       if (context == NULL)
5411         return NULL;
5412       return stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.right,
5413                                    context, NULL);
5414
5415     case DEMANGLE_COMPONENT_TEMPLATE:
5416       {
5417         char *p;
5418         size_t alc;
5419
5420         /* We print this component to get a class name which we can
5421            use.  FIXME: This probably won't work if the template uses
5422            template parameters which refer to an outer template.  */
5423         p = cplus_demangle_print (DMGL_PARAMS | demangle_flags, dc, 20, &alc);
5424         if (p == NULL)
5425           {
5426             fprintf (stderr, _("Failed to print demangled template\n"));
5427             return NULL;
5428           }
5429         dt = stab_find_tagged_type (dhandle, info, p, strlen (p),
5430                                     DEBUG_KIND_CLASS);
5431         free (p);
5432         return dt;
5433       }
5434
5435     case DEMANGLE_COMPONENT_SUB_STD:
5436       return stab_find_tagged_type (dhandle, info, dc->u.s_string.string,
5437                                     dc->u.s_string.len, DEBUG_KIND_ILLEGAL);
5438
5439     case DEMANGLE_COMPONENT_RESTRICT:
5440     case DEMANGLE_COMPONENT_VOLATILE:
5441     case DEMANGLE_COMPONENT_CONST:
5442     case DEMANGLE_COMPONENT_POINTER:
5443     case DEMANGLE_COMPONENT_REFERENCE:
5444       dt = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left, NULL,
5445                                  NULL);
5446       if (dt == NULL)
5447         return NULL;
5448
5449       switch (dc->type)
5450         {
5451         default:
5452           abort ();
5453         case DEMANGLE_COMPONENT_RESTRICT:
5454           /* FIXME: We have no way to represent restrict.  */
5455           return dt;
5456         case DEMANGLE_COMPONENT_VOLATILE:
5457           return debug_make_volatile_type (dhandle, dt);
5458         case DEMANGLE_COMPONENT_CONST:
5459           return debug_make_const_type (dhandle, dt);
5460         case DEMANGLE_COMPONENT_POINTER:
5461           return debug_make_pointer_type (dhandle, dt);
5462         case DEMANGLE_COMPONENT_REFERENCE:
5463           return debug_make_reference_type (dhandle, dt);
5464         }
5465
5466     case DEMANGLE_COMPONENT_FUNCTION_TYPE:
5467       {
5468         debug_type *pargs;
5469         bfd_boolean varargs;
5470
5471         if (dc->u.s_binary.left == NULL)
5472           {
5473             /* In this case the return type is actually unknown.
5474                However, I'm not sure this will ever arise in practice;
5475                normally an unknown return type would only appear at
5476                the top level, which is handled above.  */
5477             dt = debug_make_void_type (dhandle);
5478           }
5479         else
5480           dt = stab_demangle_v3_arg (dhandle, info, dc->u.s_binary.left, NULL,
5481                                      NULL);
5482         if (dt == NULL)
5483           return NULL;
5484
5485         pargs = stab_demangle_v3_arglist (dhandle, info,
5486                                           dc->u.s_binary.right,
5487                                           &varargs);
5488         if (pargs == NULL)
5489           return NULL;
5490
5491         return debug_make_function_type (dhandle, dt, pargs, varargs);
5492       }
5493
5494     case DEMANGLE_COMPONENT_BUILTIN_TYPE:
5495       {
5496         char *p;
5497         size_t alc;
5498         debug_type ret;
5499
5500         /* We print this component in order to find out the type name.
5501            FIXME: Should we instead expose the
5502            demangle_builtin_type_info structure?  */
5503         p = cplus_demangle_print (DMGL_PARAMS | demangle_flags, dc, 20, &alc);
5504         if (p == NULL)
5505           {
5506             fprintf (stderr, _("Couldn't get demangled builtin type\n"));
5507             return NULL;
5508           }
5509
5510         /* The mangling is based on the type, but does not itself
5511            indicate what the sizes are.  So we have to guess.  */
5512         if (strcmp (p, "signed char") == 0)
5513           ret = debug_make_int_type (dhandle, 1, FALSE);
5514         else if (strcmp (p, "bool") == 0)
5515           ret = debug_make_bool_type (dhandle, 1);
5516         else if (strcmp (p, "char") == 0)
5517           ret = debug_make_int_type (dhandle, 1, FALSE);
5518         else if (strcmp (p, "double") == 0)
5519           ret = debug_make_float_type (dhandle, 8);
5520         else if (strcmp (p, "long double") == 0)
5521           ret = debug_make_float_type (dhandle, 8);
5522         else if (strcmp (p, "float") == 0)
5523           ret = debug_make_float_type (dhandle, 4);
5524         else if (strcmp (p, "__float128") == 0)
5525           ret = debug_make_float_type (dhandle, 16);
5526         else if (strcmp (p, "unsigned char") == 0)
5527           ret = debug_make_int_type (dhandle, 1, TRUE);
5528         else if (strcmp (p, "int") == 0)
5529           ret = debug_make_int_type (dhandle, 4, FALSE);
5530         else if (strcmp (p, "unsigned int") == 0)
5531           ret = debug_make_int_type (dhandle, 4, TRUE);
5532         else if (strcmp (p, "long") == 0)
5533           ret = debug_make_int_type (dhandle, 4, FALSE);
5534         else if (strcmp (p, "unsigned long") == 0)
5535           ret = debug_make_int_type (dhandle, 4, TRUE);
5536         else if (strcmp (p, "__int128") == 0)
5537           ret = debug_make_int_type (dhandle, 16, FALSE);
5538         else if (strcmp (p, "unsigned __int128") == 0)
5539           ret = debug_make_int_type (dhandle, 16, TRUE);
5540         else if (strcmp (p, "short") == 0)
5541           ret = debug_make_int_type (dhandle, 2, FALSE);
5542         else if (strcmp (p, "unsigned short") == 0)
5543           ret = debug_make_int_type (dhandle, 2, TRUE);
5544         else if (strcmp (p, "void") == 0)
5545           ret = debug_make_void_type (dhandle);
5546         else if (strcmp (p, "wchar_t") == 0)
5547           ret = debug_make_int_type (dhandle, 4, TRUE);
5548         else if (strcmp (p, "long long") == 0)
5549           ret = debug_make_int_type (dhandle, 8, FALSE);
5550         else if (strcmp (p, "unsigned long long") == 0)
5551           ret = debug_make_int_type (dhandle, 8, TRUE);
5552         else if (strcmp (p, "...") == 0)
5553           {
5554             if (pvarargs == NULL)
5555               fprintf (stderr, _("Unexpected demangled varargs\n"));
5556             else
5557               *pvarargs = TRUE;
5558             ret = NULL;
5559           }
5560         else
5561           {
5562             fprintf (stderr, _("Unrecognized demangled builtin type\n"));
5563             ret = NULL;
5564           }
5565
5566         free (p);
5567
5568         return ret;
5569       }
5570     }
5571 }