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