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