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