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