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