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