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