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