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