* arsup.c: Remove ARGSUSED.
[external/binutils.git] / binutils / stabs.c
1 /* stabs.c -- Parse stabs debugging information
2    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
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 *));
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
2912   /* Constructors are sometimes handled specially.  */
2913   is_full_physname_constructor = ((argtypes[0] == '_'
2914                                    && argtypes[1] == '_'
2915                                    && (ISDIGIT (argtypes[2])
2916                                        || argtypes[2] == 'Q'
2917                                        || argtypes[2] == 't'))
2918                                   || strncmp (argtypes, "__ct", 4) == 0);
2919
2920   is_constructor = (is_full_physname_constructor
2921                     || (tagname != NULL
2922                         && strcmp (fieldname, tagname) == 0));
2923   is_destructor = ((argtypes[0] == '_'
2924                     && (argtypes[1] == '$' || argtypes[1] == '.')
2925                     && argtypes[2] == '_')
2926                    || strncmp (argtypes, "__dt", 4) == 0);
2927
2928   if (is_destructor || is_full_physname_constructor)
2929     *pphysname = argtypes;
2930   else
2931     {
2932       unsigned int len;
2933       const char *const_prefix;
2934       const char *volatile_prefix;
2935       char buf[20];
2936       unsigned int mangled_name_len;
2937       char *physname;
2938
2939       len = tagname == NULL ? 0 : strlen (tagname);
2940       const_prefix = constp ? "C" : "";
2941       volatile_prefix = volatilep ? "V" : "";
2942
2943       if (len == 0)
2944         sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2945       else if (tagname != NULL && strchr (tagname, '<') != NULL)
2946         {
2947           /* Template methods are fully mangled.  */
2948           sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2949           tagname = NULL;
2950           len = 0;
2951         }
2952       else
2953         sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
2954
2955       mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
2956                           + strlen (buf)
2957                           + len
2958                           + strlen (argtypes)
2959                           + 1);
2960
2961       if (fieldname[0] == 'o'
2962           && fieldname[1] == 'p'
2963           && (fieldname[2] == '$' || fieldname[2] == '.'))
2964         {
2965           const char *opname;
2966
2967           opname = cplus_mangle_opname (fieldname + 3, 0);
2968           if (opname == NULL)
2969             {
2970               fprintf (stderr, _("No mangling for \"%s\"\n"), fieldname);
2971               return DEBUG_TYPE_NULL;
2972             }
2973           mangled_name_len += strlen (opname);
2974           physname = (char *) xmalloc (mangled_name_len);
2975           strncpy (physname, fieldname, 3);
2976           strcpy (physname + 3, opname);
2977         }
2978       else
2979         {
2980           physname = (char *) xmalloc (mangled_name_len);
2981           if (is_constructor)
2982             physname[0] = '\0';
2983           else
2984             strcpy (physname, fieldname);
2985         }
2986
2987       strcat (physname, buf);
2988       if (tagname != NULL)
2989         strcat (physname, tagname);
2990       strcat (physname, argtypes);
2991
2992       *pphysname = physname;
2993     }
2994
2995   if (*argtypes == '\0' || is_destructor)
2996     {
2997       args = (debug_type *) xmalloc (sizeof *args);
2998       *args = NULL;
2999       return debug_make_method_type (dhandle, return_type, class_type, args,
3000                                      false);
3001     }
3002
3003   args = stab_demangle_argtypes (dhandle, info, *pphysname, &varargs);
3004   if (args == NULL)
3005     return DEBUG_TYPE_NULL;
3006
3007   return debug_make_method_type (dhandle, return_type, class_type, args,
3008                                  varargs);
3009 }
3010
3011 /* The tail end of stabs for C++ classes that contain a virtual function
3012    pointer contains a tilde, a %, and a type number.
3013    The type number refers to the base class (possibly this class itself) which
3014    contains the vtable pointer for the current class.
3015
3016    This function is called when we have parsed all the method declarations,
3017    so we can look for the vptr base class info.  */
3018
3019 static boolean
3020 parse_stab_tilde_field (dhandle, info, pp, typenums, retvptrbase, retownvptr)
3021      PTR dhandle;
3022      struct stab_handle *info;
3023      const char **pp;
3024      const int *typenums;
3025      debug_type *retvptrbase;
3026      boolean *retownvptr;
3027 {
3028   const char *orig;
3029   const char *hold;
3030   int vtypenums[2];
3031
3032   *retvptrbase = DEBUG_TYPE_NULL;
3033   *retownvptr = false;
3034
3035   orig = *pp;
3036
3037   /* If we are positioned at a ';', then skip it.  */
3038   if (**pp == ';')
3039     ++*pp;
3040
3041   if (**pp != '~')
3042     return true;
3043
3044   ++*pp;
3045
3046   if (**pp == '=' || **pp == '+' || **pp == '-')
3047     {
3048       /* Obsolete flags that used to indicate the presence of
3049          constructors and/or destructors.  */
3050       ++*pp;
3051     }
3052
3053   if (**pp != '%')
3054     return true;
3055
3056   ++*pp;
3057
3058   hold = *pp;
3059
3060   /* The next number is the type number of the base class (possibly
3061      our own class) which supplies the vtable for this class.  */
3062   if (! parse_stab_type_number (pp, vtypenums))
3063     return false;
3064
3065   if (vtypenums[0] == typenums[0]
3066       && vtypenums[1] == typenums[1])
3067     *retownvptr = true;
3068   else
3069     {
3070       debug_type vtype;
3071       const char *p;
3072
3073       *pp = hold;
3074
3075       vtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3076                                (debug_type **) NULL);
3077       for (p = *pp; *p != ';' && *p != '\0'; p++)
3078         ;
3079       if (*p != ';')
3080         {
3081           bad_stab (orig);
3082           return false;
3083         }
3084
3085       *retvptrbase = vtype;
3086
3087       *pp = p + 1;
3088     }
3089
3090   return true;
3091 }
3092
3093 /* Read a definition of an array type.  */
3094
3095 static debug_type
3096 parse_stab_array_type (dhandle, info, pp, stringp)
3097      PTR dhandle;
3098      struct stab_handle *info;
3099      const char **pp;
3100      boolean stringp;
3101 {
3102   const char *orig;
3103   const char *p;
3104   int typenums[2];
3105   debug_type index_type;
3106   boolean adjustable;
3107   bfd_signed_vma lower, upper;
3108   debug_type element_type;
3109
3110   /* Format of an array type:
3111      "ar<index type>;lower;upper;<array_contents_type>".
3112      OS9000: "arlower,upper;<array_contents_type>".
3113
3114      Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
3115      for these, produce a type like float[][].  */
3116
3117   orig = *pp;
3118
3119   /* FIXME: gdb checks os9k_stabs here.  */
3120
3121   /* If the index type is type 0, we take it as int.  */
3122   p = *pp;
3123   if (! parse_stab_type_number (&p, typenums))
3124     return DEBUG_TYPE_NULL;
3125   if (typenums[0] == 0 && typenums[1] == 0 && **pp != '=')
3126     {
3127       index_type = debug_find_named_type (dhandle, "int");
3128       if (index_type == DEBUG_TYPE_NULL)
3129         {
3130           index_type = debug_make_int_type (dhandle, 4, false);
3131           if (index_type == DEBUG_TYPE_NULL)
3132             return DEBUG_TYPE_NULL;
3133         }
3134       *pp = p;
3135     }
3136   else
3137     {
3138       index_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3139                                     (debug_type **) NULL);
3140     }
3141
3142   if (**pp != ';')
3143     {
3144       bad_stab (orig);
3145       return DEBUG_TYPE_NULL;
3146     }
3147   ++*pp;
3148
3149   adjustable = false;
3150
3151   if (! ISDIGIT (**pp) && **pp != '-')
3152     {
3153       ++*pp;
3154       adjustable = true;
3155     }
3156
3157   lower = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
3158   if (**pp != ';')
3159     {
3160       bad_stab (orig);
3161       return DEBUG_TYPE_NULL;
3162     }
3163   ++*pp;
3164
3165   if (! ISDIGIT (**pp) && **pp != '-')
3166     {
3167       ++*pp;
3168       adjustable = true;
3169     }
3170
3171   upper = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
3172   if (**pp != ';')
3173     {
3174       bad_stab (orig);
3175       return DEBUG_TYPE_NULL;
3176     }
3177   ++*pp;
3178
3179   element_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3180                                   (debug_type **) NULL);
3181   if (element_type == DEBUG_TYPE_NULL)
3182     return DEBUG_TYPE_NULL;
3183
3184   if (adjustable)
3185     {
3186       lower = 0;
3187       upper = -1;
3188     }
3189
3190   return debug_make_array_type (dhandle, element_type, index_type, lower,
3191                                 upper, stringp);
3192 }
3193
3194 /* This struct holds information about files we have seen using
3195    N_BINCL.  */
3196
3197 struct bincl_file
3198 {
3199   /* The next N_BINCL file.  */
3200   struct bincl_file *next;
3201   /* The next N_BINCL on the stack.  */
3202   struct bincl_file *next_stack;
3203   /* The file name.  */
3204   const char *name;
3205   /* The hash value.  */
3206   bfd_vma hash;
3207   /* The file index.  */
3208   unsigned int file;
3209   /* The list of types defined in this file.  */
3210   struct stab_types *file_types;
3211 };
3212
3213 /* Start a new N_BINCL file, pushing it onto the stack.  */
3214
3215 static void
3216 push_bincl (info, name, hash)
3217      struct stab_handle *info;
3218      const char *name;
3219      bfd_vma hash;
3220 {
3221   struct bincl_file *n;
3222
3223   n = (struct bincl_file *) xmalloc (sizeof *n);
3224   n->next = info->bincl_list;
3225   n->next_stack = info->bincl_stack;
3226   n->name = name;
3227   n->hash = hash;
3228   n->file = info->files;
3229   n->file_types = NULL;
3230   info->bincl_list = n;
3231   info->bincl_stack = n;
3232
3233   ++info->files;
3234   info->file_types = ((struct stab_types **)
3235                       xrealloc ((PTR) info->file_types,
3236                                 (info->files
3237                                  * sizeof *info->file_types)));
3238   info->file_types[n->file] = NULL;
3239 }
3240
3241 /* Finish an N_BINCL file, at an N_EINCL, popping the name off the
3242    stack.  */
3243
3244 static const char *
3245 pop_bincl (info)
3246      struct stab_handle *info;
3247 {
3248   struct bincl_file *o;
3249
3250   o = info->bincl_stack;
3251   if (o == NULL)
3252     return info->main_filename;
3253   info->bincl_stack = o->next_stack;
3254
3255   o->file_types = info->file_types[o->file];
3256
3257   if (info->bincl_stack == NULL)
3258     return info->main_filename;
3259   return info->bincl_stack->name;
3260 }
3261
3262 /* Handle an N_EXCL: get the types from the corresponding N_BINCL.  */
3263
3264 static boolean
3265 find_excl (info, name, hash)
3266      struct stab_handle *info;
3267      const char *name;
3268      bfd_vma hash;
3269 {
3270   struct bincl_file *l;
3271
3272   ++info->files;
3273   info->file_types = ((struct stab_types **)
3274                       xrealloc ((PTR) info->file_types,
3275                                 (info->files
3276                                  * sizeof *info->file_types)));
3277
3278   for (l = info->bincl_list; l != NULL; l = l->next)
3279     if (l->hash == hash && strcmp (l->name, name) == 0)
3280       break;
3281   if (l == NULL)
3282     {
3283       warn_stab (name, _("Undefined N_EXCL"));
3284       info->file_types[info->files - 1] = NULL;
3285       return true;
3286     }
3287
3288   info->file_types[info->files - 1] = l->file_types;
3289
3290   return true;
3291 }
3292
3293 /* Handle a variable definition.  gcc emits variable definitions for a
3294    block before the N_LBRAC, so we must hold onto them until we see
3295    it.  The SunPRO compiler emits variable definitions after the
3296    N_LBRAC, so we can call debug_record_variable immediately.  */
3297
3298 static boolean
3299 stab_record_variable (dhandle, info, name, type, kind, val)
3300      PTR dhandle;
3301      struct stab_handle *info;
3302      const char *name;
3303      debug_type type;
3304      enum debug_var_kind kind;
3305      bfd_vma val;
3306 {
3307   struct stab_pending_var *v;
3308
3309   if ((kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
3310       || ! info->within_function
3311       || (info->gcc_compiled == 0 && info->n_opt_found))
3312     return debug_record_variable (dhandle, name, type, kind, val);
3313
3314   v = (struct stab_pending_var *) xmalloc (sizeof *v);
3315   memset (v, 0, sizeof *v);
3316
3317   v->next = info->pending;
3318   v->name = name;
3319   v->type = type;
3320   v->kind = kind;
3321   v->val = val;
3322   info->pending = v;
3323
3324   return true;
3325 }
3326
3327 /* Emit pending variable definitions.  This is called after we see the
3328    N_LBRAC that starts the block.  */
3329
3330 static boolean
3331 stab_emit_pending_vars (dhandle, info)
3332      PTR dhandle;
3333      struct stab_handle *info;
3334 {
3335   struct stab_pending_var *v;
3336
3337   v = info->pending;
3338   while (v != NULL)
3339     {
3340       struct stab_pending_var *next;
3341
3342       if (! debug_record_variable (dhandle, v->name, v->type, v->kind, v->val))
3343         return false;
3344
3345       next = v->next;
3346       free (v);
3347       v = next;
3348     }
3349
3350   info->pending = NULL;
3351
3352   return true;
3353 }
3354
3355 /* Find the slot for a type in the database.  */
3356
3357 static debug_type *
3358 stab_find_slot (info, typenums)
3359      struct stab_handle *info;
3360      const int *typenums;
3361 {
3362   int filenum;
3363   int index;
3364   struct stab_types **ps;
3365
3366   filenum = typenums[0];
3367   index = typenums[1];
3368
3369   if (filenum < 0 || (unsigned int) filenum >= info->files)
3370     {
3371       fprintf (stderr, _("Type file number %d out of range\n"), filenum);
3372       return NULL;
3373     }
3374   if (index < 0)
3375     {
3376       fprintf (stderr, _("Type index number %d out of range\n"), index);
3377       return NULL;
3378     }
3379
3380   ps = info->file_types + filenum;
3381
3382   while (index >= STAB_TYPES_SLOTS)
3383     {
3384       if (*ps == NULL)
3385         {
3386           *ps = (struct stab_types *) xmalloc (sizeof **ps);
3387           memset (*ps, 0, sizeof **ps);
3388         }
3389       ps = &(*ps)->next;
3390       index -= STAB_TYPES_SLOTS;
3391     }
3392   if (*ps == NULL)
3393     {
3394       *ps = (struct stab_types *) xmalloc (sizeof **ps);
3395       memset (*ps, 0, sizeof **ps);
3396     }
3397
3398   return (*ps)->types + index;
3399 }
3400
3401 /* Find a type given a type number.  If the type has not been
3402    allocated yet, create an indirect type.  */
3403
3404 static debug_type
3405 stab_find_type (dhandle, info, typenums)
3406      PTR dhandle;
3407      struct stab_handle *info;
3408      const int *typenums;
3409 {
3410   debug_type *slot;
3411
3412   if (typenums[0] == 0 && typenums[1] < 0)
3413     {
3414       /* A negative type number indicates an XCOFF builtin type.  */
3415       return stab_xcoff_builtin_type (dhandle, info, typenums[1]);
3416     }
3417
3418   slot = stab_find_slot (info, typenums);
3419   if (slot == NULL)
3420     return DEBUG_TYPE_NULL;
3421
3422   if (*slot == DEBUG_TYPE_NULL)
3423     return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
3424
3425   return *slot;
3426 }
3427
3428 /* Record that a given type number refers to a given type.  */
3429
3430 static boolean
3431 stab_record_type (dhandle, info, typenums, type)
3432      PTR dhandle ATTRIBUTE_UNUSED;
3433      struct stab_handle *info;
3434      const int *typenums;
3435      debug_type type;
3436 {
3437   debug_type *slot;
3438
3439   slot = stab_find_slot (info, typenums);
3440   if (slot == NULL)
3441     return false;
3442
3443   /* gdb appears to ignore type redefinitions, so we do as well.  */
3444
3445   *slot = type;
3446
3447   return true;
3448 }
3449
3450 /* Return an XCOFF builtin type.  */
3451
3452 static debug_type
3453 stab_xcoff_builtin_type (dhandle, info, typenum)
3454      PTR dhandle;
3455      struct stab_handle *info;
3456      int typenum;
3457 {
3458   debug_type rettype;
3459   const char *name;
3460
3461   if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
3462     {
3463       fprintf (stderr, _("Unrecognized XCOFF type %d\n"), typenum);
3464       return DEBUG_TYPE_NULL;
3465     }
3466   if (info->xcoff_types[-typenum] != NULL)
3467     return info->xcoff_types[-typenum];
3468
3469   switch (-typenum)
3470     {
3471     case 1:
3472       /* The size of this and all the other types are fixed, defined
3473          by the debugging format.  */
3474       name = "int";
3475       rettype = debug_make_int_type (dhandle, 4, false);
3476       break;
3477     case 2:
3478       name = "char";
3479       rettype = debug_make_int_type (dhandle, 1, false);
3480       break;
3481     case 3:
3482       name = "short";
3483       rettype = debug_make_int_type (dhandle, 2, false);
3484       break;
3485     case 4:
3486       name = "long";
3487       rettype = debug_make_int_type (dhandle, 4, false);
3488       break;
3489     case 5:
3490       name = "unsigned char";
3491       rettype = debug_make_int_type (dhandle, 1, true);
3492       break;
3493     case 6:
3494       name = "signed char";
3495       rettype = debug_make_int_type (dhandle, 1, false);
3496       break;
3497     case 7:
3498       name = "unsigned short";
3499       rettype = debug_make_int_type (dhandle, 2, true);
3500       break;
3501     case 8:
3502       name = "unsigned int";
3503       rettype = debug_make_int_type (dhandle, 4, true);
3504       break;
3505     case 9:
3506       name = "unsigned";
3507       rettype = debug_make_int_type (dhandle, 4, true);
3508     case 10:
3509       name = "unsigned long";
3510       rettype = debug_make_int_type (dhandle, 4, true);
3511       break;
3512     case 11:
3513       name = "void";
3514       rettype = debug_make_void_type (dhandle);
3515       break;
3516     case 12:
3517       /* IEEE single precision (32 bit).  */
3518       name = "float";
3519       rettype = debug_make_float_type (dhandle, 4);
3520       break;
3521     case 13:
3522       /* IEEE double precision (64 bit).  */
3523       name = "double";
3524       rettype = debug_make_float_type (dhandle, 8);
3525       break;
3526     case 14:
3527       /* This is an IEEE double on the RS/6000, and different machines
3528          with different sizes for "long double" should use different
3529          negative type numbers.  See stabs.texinfo.  */
3530       name = "long double";
3531       rettype = debug_make_float_type (dhandle, 8);
3532       break;
3533     case 15:
3534       name = "integer";
3535       rettype = debug_make_int_type (dhandle, 4, false);
3536       break;
3537     case 16:
3538       name = "boolean";
3539       rettype = debug_make_bool_type (dhandle, 4);
3540       break;
3541     case 17:
3542       name = "short real";
3543       rettype = debug_make_float_type (dhandle, 4);
3544       break;
3545     case 18:
3546       name = "real";
3547       rettype = debug_make_float_type (dhandle, 8);
3548       break;
3549     case 19:
3550       /* FIXME */
3551       name = "stringptr";
3552       rettype = NULL;
3553       break;
3554     case 20:
3555       /* FIXME */
3556       name = "character";
3557       rettype = debug_make_int_type (dhandle, 1, true);
3558       break;
3559     case 21:
3560       name = "logical*1";
3561       rettype = debug_make_bool_type (dhandle, 1);
3562       break;
3563     case 22:
3564       name = "logical*2";
3565       rettype = debug_make_bool_type (dhandle, 2);
3566       break;
3567     case 23:
3568       name = "logical*4";
3569       rettype = debug_make_bool_type (dhandle, 4);
3570       break;
3571     case 24:
3572       name = "logical";
3573       rettype = debug_make_bool_type (dhandle, 4);
3574       break;
3575     case 25:
3576       /* Complex type consisting of two IEEE single precision values.  */
3577       name = "complex";
3578       rettype = debug_make_complex_type (dhandle, 8);
3579       break;
3580     case 26:
3581       /* Complex type consisting of two IEEE double precision values.  */
3582       name = "double complex";
3583       rettype = debug_make_complex_type (dhandle, 16);
3584       break;
3585     case 27:
3586       name = "integer*1";
3587       rettype = debug_make_int_type (dhandle, 1, false);
3588       break;
3589     case 28:
3590       name = "integer*2";
3591       rettype = debug_make_int_type (dhandle, 2, false);
3592       break;
3593     case 29:
3594       name = "integer*4";
3595       rettype = debug_make_int_type (dhandle, 4, false);
3596       break;
3597     case 30:
3598       /* FIXME */
3599       name = "wchar";
3600       rettype = debug_make_int_type (dhandle, 2, false);
3601       break;
3602     case 31:
3603       name = "long long";
3604       rettype = debug_make_int_type (dhandle, 8, false);
3605       break;
3606     case 32:
3607       name = "unsigned long long";
3608       rettype = debug_make_int_type (dhandle, 8, true);
3609       break;
3610     case 33:
3611       name = "logical*8";
3612       rettype = debug_make_bool_type (dhandle, 8);
3613       break;
3614     case 34:
3615       name = "integer*8";
3616       rettype = debug_make_int_type (dhandle, 8, false);
3617       break;
3618     default:
3619       abort ();
3620     }
3621
3622   rettype = debug_name_type (dhandle, name, rettype);
3623
3624   info->xcoff_types[-typenum] = rettype;
3625
3626   return rettype;
3627 }
3628
3629 /* Find or create a tagged type.  */
3630
3631 static debug_type
3632 stab_find_tagged_type (dhandle, info, p, len, kind)
3633      PTR dhandle;
3634      struct stab_handle *info;
3635      const char *p;
3636      int len;
3637      enum debug_type_kind kind;
3638 {
3639   char *name;
3640   debug_type dtype;
3641   struct stab_tag *st;
3642
3643   name = savestring (p, len);
3644
3645   /* We pass DEBUG_KIND_ILLEGAL because we want all tags in the same
3646      namespace.  This is right for C, and I don't know how to handle
3647      other languages.  FIXME.  */
3648   dtype = debug_find_tagged_type (dhandle, name, DEBUG_KIND_ILLEGAL);
3649   if (dtype != DEBUG_TYPE_NULL)
3650     {
3651       free (name);
3652       return dtype;
3653     }
3654
3655   /* We need to allocate an entry on the undefined tag list.  */
3656   for (st = info->tags; st != NULL; st = st->next)
3657     {
3658       if (st->name[0] == name[0]
3659           && strcmp (st->name, name) == 0)
3660         {
3661           if (st->kind == DEBUG_KIND_ILLEGAL)
3662             st->kind = kind;
3663           free (name);
3664           break;
3665         }
3666     }
3667   if (st == NULL)
3668     {
3669       st = (struct stab_tag *) xmalloc (sizeof *st);
3670       memset (st, 0, sizeof *st);
3671
3672       st->next = info->tags;
3673       st->name = name;
3674       st->kind = kind;
3675       st->slot = DEBUG_TYPE_NULL;
3676       st->type = debug_make_indirect_type (dhandle, &st->slot, name);
3677       info->tags = st;
3678     }
3679
3680   return st->type;
3681 }
3682 \f
3683 /* In order to get the correct argument types for a stubbed method, we
3684    need to extract the argument types from a C++ mangled string.
3685    Since the argument types can refer back to the return type, this
3686    means that we must demangle the entire physical name.  In gdb this
3687    is done by calling cplus_demangle and running the results back
3688    through the C++ expression parser.  Since we have no expression
3689    parser, we must duplicate much of the work of cplus_demangle here.
3690
3691    We assume that GNU style demangling is used, since this is only
3692    done for method stubs, and only g++ should output that form of
3693    debugging information.  */
3694
3695 /* This structure is used to hold a pointer to type information which
3696    demangling a string.  */
3697
3698 struct stab_demangle_typestring
3699 {
3700   /* The start of the type.  This is not null terminated.  */
3701   const char *typestring;
3702   /* The length of the type.  */
3703   unsigned int len;
3704 };
3705
3706 /* This structure is used to hold information while demangling a
3707    string.  */
3708
3709 struct stab_demangle_info
3710 {
3711   /* The debugging information handle.  */
3712   PTR dhandle;
3713   /* The stab information handle.  */
3714   struct stab_handle *info;
3715   /* The array of arguments we are building.  */
3716   debug_type *args;
3717   /* Whether the method takes a variable number of arguments.  */
3718   boolean varargs;
3719   /* The array of types we have remembered.  */
3720   struct stab_demangle_typestring *typestrings;
3721   /* The number of typestrings.  */
3722   unsigned int typestring_count;
3723   /* The number of typestring slots we have allocated.  */
3724   unsigned int typestring_alloc;
3725 };
3726
3727 static void stab_bad_demangle PARAMS ((const char *));
3728 static unsigned int stab_demangle_count PARAMS ((const char **));
3729 static boolean stab_demangle_get_count
3730   PARAMS ((const char **, unsigned int *));
3731 static boolean stab_demangle_prefix
3732   PARAMS ((struct stab_demangle_info *, const char **));
3733 static boolean stab_demangle_function_name
3734   PARAMS ((struct stab_demangle_info *, const char **, const char *));
3735 static boolean stab_demangle_signature
3736   PARAMS ((struct stab_demangle_info *, const char **));
3737 static boolean stab_demangle_qualified
3738   PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3739 static boolean stab_demangle_template
3740   PARAMS ((struct stab_demangle_info *, const char **, char **));
3741 static boolean stab_demangle_class
3742   PARAMS ((struct stab_demangle_info *, const char **, const char **));
3743 static boolean stab_demangle_args
3744   PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3745            boolean *));
3746 static boolean stab_demangle_arg
3747   PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3748            unsigned int *, unsigned int *));
3749 static boolean stab_demangle_type
3750   PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3751 static boolean stab_demangle_fund_type
3752   PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3753 static boolean stab_demangle_remember_type
3754   PARAMS ((struct stab_demangle_info *, const char *, int));
3755
3756 /* Warn about a bad demangling.  */
3757
3758 static void
3759 stab_bad_demangle (s)
3760      const char *s;
3761 {
3762   fprintf (stderr, _("bad mangled name `%s'\n"), s);
3763 }
3764
3765 /* Get a count from a stab string.  */
3766
3767 static unsigned int
3768 stab_demangle_count (pp)
3769      const char **pp;
3770 {
3771   unsigned int count;
3772
3773   count = 0;
3774   while (ISDIGIT (**pp))
3775     {
3776       count *= 10;
3777       count += **pp - '0';
3778       ++*pp;
3779     }
3780   return count;
3781 }
3782
3783 /* Require a count in a string.  The count may be multiple digits, in
3784    which case it must end in an underscore.  */
3785
3786 static boolean
3787 stab_demangle_get_count (pp, pi)
3788      const char **pp;
3789      unsigned int *pi;
3790 {
3791   if (! ISDIGIT (**pp))
3792     return false;
3793
3794   *pi = **pp - '0';
3795   ++*pp;
3796   if (ISDIGIT (**pp))
3797     {
3798       unsigned int count;
3799       const char *p;
3800
3801       count = *pi;
3802       p = *pp;
3803       do
3804         {
3805           count *= 10;
3806           count += *p - '0';
3807           ++p;
3808         }
3809       while (ISDIGIT (*p));
3810       if (*p == '_')
3811         {
3812           *pp = p + 1;
3813           *pi = count;
3814         }
3815     }
3816
3817   return true;
3818 }
3819
3820 /* This function demangles a physical name, returning a NULL
3821    terminated array of argument types.  */
3822
3823 static debug_type *
3824 stab_demangle_argtypes (dhandle, info, physname, pvarargs)
3825      PTR dhandle;
3826      struct stab_handle *info;
3827      const char *physname;
3828      boolean *pvarargs;
3829 {
3830   struct stab_demangle_info minfo;
3831
3832   minfo.dhandle = dhandle;
3833   minfo.info = info;
3834   minfo.args = NULL;
3835   minfo.varargs = false;
3836   minfo.typestring_alloc = 10;
3837   minfo.typestrings = ((struct stab_demangle_typestring *)
3838                        xmalloc (minfo.typestring_alloc
3839                                 * sizeof *minfo.typestrings));
3840   minfo.typestring_count = 0;
3841
3842   /* cplus_demangle checks for special GNU mangled forms, but we can't
3843      see any of them in mangled method argument types.  */
3844
3845   if (! stab_demangle_prefix (&minfo, &physname))
3846     goto error_return;
3847
3848   if (*physname != '\0')
3849     {
3850       if (! stab_demangle_signature (&minfo, &physname))
3851         goto error_return;
3852     }
3853
3854   free (minfo.typestrings);
3855   minfo.typestrings = NULL;
3856
3857   if (minfo.args == NULL)
3858     fprintf (stderr, _("no argument types in mangled string\n"));
3859
3860   *pvarargs = minfo.varargs;
3861   return minfo.args;
3862
3863  error_return:
3864   if (minfo.typestrings != NULL)
3865     free (minfo.typestrings);
3866   return NULL;
3867 }
3868
3869 /* Demangle the prefix of the mangled name.  */
3870
3871 static boolean
3872 stab_demangle_prefix (minfo, pp)
3873      struct stab_demangle_info *minfo;
3874      const char **pp;
3875 {
3876   const char *scan;
3877   unsigned int i;
3878
3879   /* cplus_demangle checks for global constructors and destructors,
3880      but we can't see them in mangled argument types.  */
3881
3882   /* Look for `__'.  */
3883   scan = *pp;
3884   do
3885     {
3886       scan = strchr (scan, '_');
3887     }
3888   while (scan != NULL && *++scan != '_');
3889
3890   if (scan == NULL)
3891     {
3892       stab_bad_demangle (*pp);
3893       return false;
3894     }
3895
3896   --scan;
3897
3898   /* We found `__'; move ahead to the last contiguous `__' pair.  */
3899   i = strspn (scan, "_");
3900   if (i > 2)
3901     scan += i - 2;
3902
3903   if (scan == *pp
3904       && (ISDIGIT (scan[2])
3905           || scan[2] == 'Q'
3906           || scan[2] == 't'))
3907     {
3908       /* This is a GNU style constructor name.  */
3909       *pp = scan + 2;
3910       return true;
3911     }
3912   else if (scan == *pp
3913            && ! ISDIGIT (scan[2])
3914            && scan[2] != 't')
3915     {
3916       /* Look for the `__' that separates the prefix from the
3917          signature.  */
3918       while (*scan == '_')
3919         ++scan;
3920       scan = strstr (scan, "__");
3921       if (scan == NULL || scan[2] == '\0')
3922         {
3923           stab_bad_demangle (*pp);
3924           return false;
3925         }
3926
3927       return stab_demangle_function_name (minfo, pp, scan);
3928     }
3929   else if (scan[2] != '\0')
3930     {
3931       /* The name doesn't start with `__', but it does contain `__'.  */
3932       return stab_demangle_function_name (minfo, pp, scan);
3933     }
3934   else
3935     {
3936       stab_bad_demangle (*pp);
3937       return false;
3938     }
3939   /*NOTREACHED*/
3940 }
3941
3942 /* Demangle a function name prefix.  The scan argument points to the
3943    double underscore which separates the function name from the
3944    signature.  */
3945
3946 static boolean
3947 stab_demangle_function_name (minfo, pp, scan)
3948      struct stab_demangle_info *minfo;
3949      const char **pp;
3950      const char *scan;
3951 {
3952   const char *name;
3953
3954   /* The string from *pp to scan is the name of the function.  We
3955      don't care about the name, since we just looking for argument
3956      types.  However, for conversion operators, the name may include a
3957      type which we must remember in order to handle backreferences.  */
3958
3959   name = *pp;
3960   *pp = scan + 2;
3961
3962   if (*pp - name >= 5
3963            && strncmp (name, "type", 4) == 0
3964            && (name[4] == '$' || name[4] == '.'))
3965     {
3966       const char *tem;
3967
3968       /* This is a type conversion operator.  */
3969       tem = name + 5;
3970       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3971         return false;
3972     }
3973   else if (name[0] == '_'
3974            && name[1] == '_'
3975            && name[2] == 'o'
3976            && name[3] == 'p')
3977     {
3978       const char *tem;
3979
3980       /* This is a type conversion operator.  */
3981       tem = name + 4;
3982       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3983         return false;
3984     }
3985
3986   return true;
3987 }
3988
3989 /* Demangle the signature.  This is where the argument types are
3990    found.  */
3991
3992 static boolean
3993 stab_demangle_signature (minfo, pp)
3994      struct stab_demangle_info *minfo;
3995      const char **pp;
3996 {
3997   const char *orig;
3998   boolean expect_func, func_done;
3999   const char *hold;
4000
4001   orig = *pp;
4002
4003   expect_func = false;
4004   func_done = false;
4005   hold = NULL;
4006
4007   while (**pp != '\0')
4008     {
4009       switch (**pp)
4010         {
4011         case 'Q':
4012           hold = *pp;
4013           if (! stab_demangle_qualified (minfo, pp, (debug_type *) NULL)
4014               || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4015             return false;
4016           expect_func = true;
4017           hold = NULL;
4018           break;
4019
4020         case 'S':
4021           /* Static member function.  FIXME: Can this happen?  */
4022           if (hold == NULL)
4023             hold = *pp;
4024           ++*pp;
4025           break;
4026
4027         case 'C':
4028           /* Const member function.  */
4029           if (hold == NULL)
4030             hold = *pp;
4031           ++*pp;
4032           break;
4033
4034         case '0': case '1': case '2': case '3': case '4':
4035         case '5': case '6': case '7': case '8': case '9':
4036           if (hold == NULL)
4037             hold = *pp;
4038           if (! stab_demangle_class (minfo, pp, (const char **) NULL)
4039               || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4040             return false;
4041           expect_func = true;
4042           hold = NULL;
4043           break;
4044
4045         case 'F':
4046           /* Function.  I don't know if this actually happens with g++
4047              output.  */
4048           hold = NULL;
4049           func_done = true;
4050           ++*pp;
4051           if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4052             return false;
4053           break;
4054
4055         case 't':
4056           /* Template.  */
4057           if (hold == NULL)
4058             hold = *pp;
4059           if (! stab_demangle_template (minfo, pp, (char **) NULL)
4060               || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
4061             return false;
4062           hold = NULL;
4063           expect_func = true;
4064           break;
4065
4066         case '_':
4067           /* At the outermost level, we cannot have a return type
4068              specified, so if we run into another '_' at this point we
4069              are dealing with a mangled name that is either bogus, or
4070              has been mangled by some algorithm we don't know how to
4071              deal with.  So just reject the entire demangling.  */
4072           stab_bad_demangle (orig);
4073           return false;
4074
4075         default:
4076           /* Assume we have stumbled onto the first outermost function
4077              argument token, and start processing args.  */
4078           func_done = true;
4079           if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4080             return false;
4081           break;
4082         }
4083
4084       if (expect_func)
4085         {
4086           func_done = true;
4087           if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4088             return false;
4089         }
4090     }
4091
4092   if (! func_done)
4093     {
4094       /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
4095          bar__3fooi is 'foo::bar(int)'.  We get here when we find the
4096          first case, and need to ensure that the '(void)' gets added
4097          to the current declp.  */
4098       if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
4099         return false;
4100     }
4101
4102   return true;
4103 }
4104
4105 /* Demangle a qualified name, such as "Q25Outer5Inner" which is the
4106    mangled form of "Outer::Inner".  */
4107
4108 static boolean
4109 stab_demangle_qualified (minfo, pp, ptype)
4110      struct stab_demangle_info *minfo;
4111      const char **pp;
4112      debug_type *ptype;
4113 {
4114   const char *orig;
4115   const char *p;
4116   unsigned int qualifiers;
4117   debug_type context;
4118
4119   orig = *pp;
4120
4121   switch ((*pp)[1])
4122     {
4123     case '_':
4124       /* GNU mangled name with more than 9 classes.  The count is
4125          preceded by an underscore (to distinguish it from the <= 9
4126          case) and followed by an underscore.  */
4127       p = *pp + 2;
4128       if (! ISDIGIT (*p) || *p == '0')
4129         {
4130           stab_bad_demangle (orig);
4131           return false;
4132         }
4133       qualifiers = atoi (p);
4134       while (ISDIGIT (*p))
4135         ++p;
4136       if (*p != '_')
4137         {
4138           stab_bad_demangle (orig);
4139           return false;
4140         }
4141       *pp = p + 1;
4142       break;
4143
4144     case '1': case '2': case '3': case '4': case '5':
4145     case '6': case '7': case '8': case '9':
4146       qualifiers = (*pp)[1] - '0';
4147       /* Skip an optional underscore after the count.  */
4148       if ((*pp)[2] == '_')
4149         ++*pp;
4150       *pp += 2;
4151       break;
4152
4153     case '0':
4154     default:
4155       stab_bad_demangle (orig);
4156       return false;
4157     }
4158
4159   context = DEBUG_TYPE_NULL;
4160
4161   /* Pick off the names.  */
4162   while (qualifiers-- > 0)
4163     {
4164       if (**pp == '_')
4165         ++*pp;
4166       if (**pp == 't')
4167         {
4168           char *name;
4169
4170           if (! stab_demangle_template (minfo, pp,
4171                                         ptype != NULL ? &name : NULL))
4172             return false;
4173
4174           if (ptype != NULL)
4175             {
4176               context = stab_find_tagged_type (minfo->dhandle, minfo->info,
4177                                                name, strlen (name),
4178                                                DEBUG_KIND_CLASS);
4179               free (name);
4180               if (context == DEBUG_TYPE_NULL)
4181                 return false;
4182             }
4183         }
4184       else
4185         {
4186           unsigned int len;
4187
4188           len = stab_demangle_count (pp);
4189           if (strlen (*pp) < len)
4190             {
4191               stab_bad_demangle (orig);
4192               return false;
4193             }
4194
4195           if (ptype != NULL)
4196             {
4197               const debug_field *fields;
4198
4199               fields = NULL;
4200               if (context != DEBUG_TYPE_NULL)
4201                 fields = debug_get_fields (minfo->dhandle, context);
4202
4203               context = DEBUG_TYPE_NULL;
4204
4205               if (fields != NULL)
4206                 {
4207                   char *name;
4208
4209                   /* Try to find the type by looking through the
4210                      fields of context until we find a field with the
4211                      same type.  This ought to work for a class
4212                      defined within a class, but it won't work for,
4213                      e.g., an enum defined within a class.  stabs does
4214                      not give us enough information to figure out the
4215                      latter case.  */
4216
4217                   name = savestring (*pp, len);
4218
4219                   for (; *fields != DEBUG_FIELD_NULL; fields++)
4220                     {
4221                       debug_type ft;
4222                       const char *dn;
4223
4224                       ft = debug_get_field_type (minfo->dhandle, *fields);
4225                       if (ft == NULL)
4226                         return false;
4227                       dn = debug_get_type_name (minfo->dhandle, ft);
4228                       if (dn != NULL && strcmp (dn, name) == 0)
4229                         {
4230                           context = ft;
4231                           break;
4232                         }
4233                     }
4234
4235                   free (name);
4236                 }
4237
4238               if (context == DEBUG_TYPE_NULL)
4239                 {
4240                   /* We have to fall back on finding the type by name.
4241                      If there are more types to come, then this must
4242                      be a class.  Otherwise, it could be anything.  */
4243
4244                   if (qualifiers == 0)
4245                     {
4246                       char *name;
4247
4248                       name = savestring (*pp, len);
4249                       context = debug_find_named_type (minfo->dhandle,
4250                                                        name);
4251                       free (name);
4252                     }
4253
4254                   if (context == DEBUG_TYPE_NULL)
4255                     {
4256                       context = stab_find_tagged_type (minfo->dhandle,
4257                                                        minfo->info,
4258                                                        *pp, len,
4259                                                        (qualifiers == 0
4260                                                         ? DEBUG_KIND_ILLEGAL
4261                                                         : DEBUG_KIND_CLASS));
4262                       if (context == DEBUG_TYPE_NULL)
4263                         return false;
4264                     }
4265                 }
4266             }
4267
4268           *pp += len;
4269         }
4270     }
4271
4272   if (ptype != NULL)
4273     *ptype = context;
4274
4275   return true;
4276 }
4277
4278 /* Demangle a template.  If PNAME is not NULL, this sets *PNAME to a
4279    string representation of the template.  */
4280
4281 static boolean
4282 stab_demangle_template (minfo, pp, pname)
4283      struct stab_demangle_info *minfo;
4284      const char **pp;
4285      char **pname;
4286 {
4287   const char *orig;
4288   unsigned int r, i;
4289
4290   orig = *pp;
4291
4292   ++*pp;
4293
4294   /* Skip the template name.  */
4295   r = stab_demangle_count (pp);
4296   if (r == 0 || strlen (*pp) < r)
4297     {
4298       stab_bad_demangle (orig);
4299       return false;
4300     }
4301   *pp += r;
4302
4303   /* Get the size of the parameter list.  */
4304   if (stab_demangle_get_count (pp, &r) == 0)
4305     {
4306       stab_bad_demangle (orig);
4307       return false;
4308     }
4309
4310   for (i = 0; i < r; i++)
4311     {
4312       if (**pp == 'Z')
4313         {
4314           /* This is a type parameter.  */
4315           ++*pp;
4316           if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4317             return false;
4318         }
4319       else
4320         {
4321           const char *old_p;
4322           boolean pointerp, realp, integralp, charp, boolp;
4323           boolean done;
4324
4325           old_p = *pp;
4326           pointerp = false;
4327           realp = false;
4328           integralp = false;
4329           charp = false;
4330           boolp = false;
4331           done = false;
4332
4333           /* This is a value parameter.  */
4334
4335           if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4336             return false;
4337
4338           while (*old_p != '\0' && ! done)
4339             {
4340               switch (*old_p)
4341                 {
4342                 case 'P':
4343                 case 'p':
4344                 case 'R':
4345                   pointerp = true;
4346                   done = true;
4347                   break;
4348                 case 'C':       /* Const.  */
4349                 case 'S':       /* Signed.  */
4350                 case 'U':       /* Unsigned.  */
4351                 case 'V':       /* Volatile.  */
4352                 case 'F':       /* Function.  */
4353                 case 'M':       /* Member function.  */
4354                 case 'O':       /* ??? */
4355                   ++old_p;
4356                   break;
4357                 case 'Q':       /* Qualified name.  */
4358                   integralp = true;
4359                   done = true;
4360                   break;
4361                 case 'T':       /* Remembered type.  */
4362                   abort ();
4363                 case 'v':       /* Void.  */
4364                   abort ();
4365                 case 'x':       /* Long long.  */
4366                 case 'l':       /* Long.  */
4367                 case 'i':       /* Int.  */
4368                 case 's':       /* Short.  */
4369                 case 'w':       /* Wchar_t.  */
4370                   integralp = true;
4371                   done = true;
4372                   break;
4373                 case 'b':       /* Bool.  */
4374                   boolp = true;
4375                   done = true;
4376                   break;
4377                 case 'c':       /* Char.  */
4378                   charp = true;
4379                   done = true;
4380                   break;
4381                 case 'r':       /* Long double.  */
4382                 case 'd':       /* Double.  */
4383                 case 'f':       /* Float.  */
4384                   realp = true;
4385                   done = true;
4386                   break;
4387                 default:
4388                   /* Assume it's a user defined integral type.  */
4389                   integralp = true;
4390                   done = true;
4391                   break;
4392                 }
4393             }
4394
4395           if (integralp)
4396             {
4397               if (**pp == 'm')
4398                 ++*pp;
4399               while (ISDIGIT (**pp))
4400                 ++*pp;
4401             }
4402           else if (charp)
4403             {
4404               unsigned int val;
4405
4406               if (**pp == 'm')
4407                 ++*pp;
4408               val = stab_demangle_count (pp);
4409               if (val == 0)
4410                 {
4411                   stab_bad_demangle (orig);
4412                   return false;
4413                 }
4414             }
4415           else if (boolp)
4416             {
4417               unsigned int val;
4418
4419               val = stab_demangle_count (pp);
4420               if (val != 0 && val != 1)
4421                 {
4422                   stab_bad_demangle (orig);
4423                   return false;
4424                 }
4425             }
4426           else if (realp)
4427             {
4428               if (**pp == 'm')
4429                 ++*pp;
4430               while (ISDIGIT (**pp))
4431                 ++*pp;
4432               if (**pp == '.')
4433                 {
4434                   ++*pp;
4435                   while (ISDIGIT (**pp))
4436                     ++*pp;
4437                 }
4438               if (**pp == 'e')
4439                 {
4440                   ++*pp;
4441                   while (ISDIGIT (**pp))
4442                     ++*pp;
4443                 }
4444             }
4445           else if (pointerp)
4446             {
4447               unsigned int len;
4448
4449               if (! stab_demangle_get_count (pp, &len))
4450                 {
4451                   stab_bad_demangle (orig);
4452                   return false;
4453                 }
4454               *pp += len;
4455             }
4456         }
4457     }
4458
4459   /* We can translate this to a string fairly easily by invoking the
4460      regular demangling routine.  */
4461   if (pname != NULL)
4462     {
4463       char *s1, *s2, *s3, *s4 = NULL;
4464       char *from, *to;
4465
4466       s1 = savestring (orig, *pp - orig);
4467
4468       s2 = concat ("NoSuchStrinG__", s1, (const char *) NULL);
4469
4470       free (s1);
4471
4472       s3 = cplus_demangle (s2, DMGL_ANSI);
4473
4474       free (s2);
4475
4476       if (s3 != NULL)
4477         s4 = strstr (s3, "::NoSuchStrinG");
4478       if (s3 == NULL || s4 == NULL)
4479         {
4480           stab_bad_demangle (orig);
4481           if (s3 != NULL)
4482             free (s3);
4483           return false;
4484         }
4485
4486       /* Eliminating all spaces, except those between > characters,
4487          makes it more likely that the demangled name will match the
4488          name which g++ used as the structure name.  */
4489       for (from = to = s3; from != s4; ++from)
4490         if (*from != ' '
4491             || (from[1] == '>' && from > s3 && from[-1] == '>'))
4492           *to++ = *from;
4493
4494       *pname = savestring (s3, to - s3);
4495
4496       free (s3);
4497     }
4498
4499   return true;
4500 }
4501
4502 /* Demangle a class name.  */
4503
4504 static boolean
4505 stab_demangle_class (minfo, pp, pstart)
4506      struct stab_demangle_info *minfo ATTRIBUTE_UNUSED;
4507      const char **pp;
4508      const char **pstart;
4509 {
4510   const char *orig;
4511   unsigned int n;
4512
4513   orig = *pp;
4514
4515   n = stab_demangle_count (pp);
4516   if (strlen (*pp) < n)
4517     {
4518       stab_bad_demangle (orig);
4519       return false;
4520     }
4521
4522   if (pstart != NULL)
4523     *pstart = *pp;
4524
4525   *pp += n;
4526
4527   return true;
4528 }
4529
4530 /* Demangle function arguments.  If the pargs argument is not NULL, it
4531    is set to a NULL terminated array holding the arguments.  */
4532
4533 static boolean
4534 stab_demangle_args (minfo, pp, pargs, pvarargs)
4535      struct stab_demangle_info *minfo;
4536      const char **pp;
4537      debug_type **pargs;
4538      boolean *pvarargs;
4539 {
4540   const char *orig;
4541   unsigned int alloc, count;
4542
4543   orig = *pp;
4544
4545   alloc = 10;
4546   if (pargs != NULL)
4547     {
4548       *pargs = (debug_type *) xmalloc (alloc * sizeof **pargs);
4549       *pvarargs = false;
4550     }
4551   count = 0;
4552
4553   while (**pp != '_' && **pp != '\0' && **pp != 'e')
4554     {
4555       if (**pp == 'N' || **pp == 'T')
4556         {
4557           char temptype;
4558           unsigned int r, t;
4559
4560           temptype = **pp;
4561           ++*pp;
4562
4563           if (temptype == 'T')
4564             r = 1;
4565           else
4566             {
4567               if (! stab_demangle_get_count (pp, &r))
4568                 {
4569                   stab_bad_demangle (orig);
4570                   return false;
4571                 }
4572             }
4573
4574           if (! stab_demangle_get_count (pp, &t))
4575             {
4576               stab_bad_demangle (orig);
4577               return false;
4578             }
4579
4580           if (t >= minfo->typestring_count)
4581             {
4582               stab_bad_demangle (orig);
4583               return false;
4584             }
4585           while (r-- > 0)
4586             {
4587               const char *tem;
4588
4589               tem = minfo->typestrings[t].typestring;
4590               if (! stab_demangle_arg (minfo, &tem, pargs, &count, &alloc))
4591                 return false;
4592             }
4593         }
4594       else
4595         {
4596           if (! stab_demangle_arg (minfo, pp, pargs, &count, &alloc))
4597             return false;
4598         }
4599     }
4600
4601   if (pargs != NULL)
4602     (*pargs)[count] = DEBUG_TYPE_NULL;
4603
4604   if (**pp == 'e')
4605     {
4606       if (pargs != NULL)
4607         *pvarargs = true;
4608       ++*pp;
4609     }
4610
4611   return true;
4612 }
4613
4614 /* Demangle a single argument.  */
4615
4616 static boolean
4617 stab_demangle_arg (minfo, pp, pargs, pcount, palloc)
4618      struct stab_demangle_info *minfo;
4619      const char **pp;
4620      debug_type **pargs;
4621      unsigned int *pcount;
4622      unsigned int *palloc;
4623 {
4624   const char *start;
4625   debug_type type;
4626
4627   start = *pp;
4628   if (! stab_demangle_type (minfo, pp,
4629                             pargs == NULL ? (debug_type *) NULL : &type)
4630       || ! stab_demangle_remember_type (minfo, start, *pp - start))
4631     return false;
4632
4633   if (pargs != NULL)
4634     {
4635       if (type == DEBUG_TYPE_NULL)
4636         return false;
4637
4638       if (*pcount + 1 >= *palloc)
4639         {
4640           *palloc += 10;
4641           *pargs = ((debug_type *)
4642                     xrealloc (*pargs, *palloc * sizeof **pargs));
4643         }
4644       (*pargs)[*pcount] = type;
4645       ++*pcount;
4646     }
4647
4648   return true;
4649 }
4650
4651 /* Demangle a type.  If the ptype argument is not NULL, *ptype is set
4652    to the newly allocated type.  */
4653
4654 static boolean
4655 stab_demangle_type (minfo, pp, ptype)
4656      struct stab_demangle_info *minfo;
4657      const char **pp;
4658      debug_type *ptype;
4659 {
4660   const char *orig;
4661
4662   orig = *pp;
4663
4664   switch (**pp)
4665     {
4666     case 'P':
4667     case 'p':
4668       /* A pointer type.  */
4669       ++*pp;
4670       if (! stab_demangle_type (minfo, pp, ptype))
4671         return false;
4672       if (ptype != NULL)
4673         *ptype = debug_make_pointer_type (minfo->dhandle, *ptype);
4674       break;
4675
4676     case 'R':
4677       /* A reference type.  */
4678       ++*pp;
4679       if (! stab_demangle_type (minfo, pp, ptype))
4680         return false;
4681       if (ptype != NULL)
4682         *ptype = debug_make_reference_type (minfo->dhandle, *ptype);
4683       break;
4684
4685     case 'A':
4686       /* An array.  */
4687       {
4688         unsigned long high;
4689
4690         ++*pp;
4691         high = 0;
4692         while (**pp != '\0' && **pp != '_')
4693           {
4694             if (! ISDIGIT (**pp))
4695               {
4696                 stab_bad_demangle (orig);
4697                 return false;
4698               }
4699             high *= 10;
4700             high += **pp - '0';
4701             ++*pp;
4702           }
4703         if (**pp != '_')
4704           {
4705             stab_bad_demangle (orig);
4706             return false;
4707           }
4708         ++*pp;
4709
4710         if (! stab_demangle_type (minfo, pp, ptype))
4711           return false;
4712         if (ptype != NULL)
4713           {
4714             debug_type int_type;
4715
4716             int_type = debug_find_named_type (minfo->dhandle, "int");
4717             if (int_type == NULL)
4718               int_type = debug_make_int_type (minfo->dhandle, 4, false);
4719             *ptype = debug_make_array_type (minfo->dhandle, *ptype, int_type,
4720                                             0, high, false);
4721           }
4722       }
4723       break;
4724
4725     case 'T':
4726       /* A back reference to a remembered type.  */
4727       {
4728         unsigned int i;
4729         const char *p;
4730
4731         ++*pp;
4732         if (! stab_demangle_get_count (pp, &i))
4733           {
4734             stab_bad_demangle (orig);
4735             return false;
4736           }
4737         if (i >= minfo->typestring_count)
4738           {
4739             stab_bad_demangle (orig);
4740             return false;
4741           }
4742         p = minfo->typestrings[i].typestring;
4743         if (! stab_demangle_type (minfo, &p, ptype))
4744           return false;
4745       }
4746       break;
4747
4748     case 'F':
4749       /* A function.  */
4750       {
4751         debug_type *args;
4752         boolean varargs;
4753
4754         ++*pp;
4755         if (! stab_demangle_args (minfo, pp,
4756                                   (ptype == NULL
4757                                    ? (debug_type **) NULL
4758                                    : &args),
4759                                   (ptype == NULL
4760                                    ? (boolean *) NULL
4761                                    : &varargs)))
4762           return false;
4763         if (**pp != '_')
4764           {
4765             /* cplus_demangle will accept a function without a return
4766                type, but I don't know when that will happen, or what
4767                to do if it does.  */
4768             stab_bad_demangle (orig);
4769             return false;
4770           }
4771         ++*pp;
4772         if (! stab_demangle_type (minfo, pp, ptype))
4773           return false;
4774         if (ptype != NULL)
4775           *ptype = debug_make_function_type (minfo->dhandle, *ptype, args,
4776                                              varargs);
4777
4778       }
4779       break;
4780
4781     case 'M':
4782     case 'O':
4783       {
4784         boolean memberp, constp, volatilep;
4785         debug_type class_type = DEBUG_TYPE_NULL;
4786         debug_type *args;
4787         boolean varargs;
4788         unsigned int n;
4789         const char *name;
4790
4791         memberp = **pp == 'M';
4792         constp = false;
4793         volatilep = false;
4794         args = NULL;
4795         varargs = false;
4796
4797         ++*pp;
4798         if (ISDIGIT (**pp))
4799           {
4800             n = stab_demangle_count (pp);
4801             if (strlen (*pp) < n)
4802               {
4803                 stab_bad_demangle (orig);
4804                 return false;
4805               }
4806             name = *pp;
4807             *pp += n;
4808
4809             if (ptype != NULL)
4810               {
4811                 class_type = stab_find_tagged_type (minfo->dhandle,
4812                                                     minfo->info,
4813                                                     name, (int) n,
4814                                                     DEBUG_KIND_CLASS);
4815                 if (class_type == DEBUG_TYPE_NULL)
4816                   return false;
4817               }
4818           }
4819         else if (**pp == 'Q')
4820           {
4821             if (! stab_demangle_qualified (minfo, pp,
4822                                            (ptype == NULL
4823                                             ? (debug_type *) NULL
4824                                             : &class_type)))
4825               return false;
4826           }
4827         else
4828           {
4829             stab_bad_demangle (orig);
4830             return false;
4831           }
4832
4833         if (memberp)
4834           {
4835             if (**pp == 'C')
4836               {
4837                 constp = true;
4838                 ++*pp;
4839               }
4840             else if (**pp == 'V')
4841               {
4842                 volatilep = true;
4843                 ++*pp;
4844               }
4845             if (**pp != 'F')
4846               {
4847                 stab_bad_demangle (orig);
4848                 return false;
4849               }
4850             ++*pp;
4851             if (! stab_demangle_args (minfo, pp,
4852                                       (ptype == NULL
4853                                        ? (debug_type **) NULL
4854                                        : &args),
4855                                       (ptype == NULL
4856                                        ? (boolean *) NULL
4857                                        : &varargs)))
4858               return false;
4859           }
4860
4861         if (**pp != '_')
4862           {
4863             stab_bad_demangle (orig);
4864             return false;
4865           }
4866         ++*pp;
4867
4868         if (! stab_demangle_type (minfo, pp, ptype))
4869           return false;
4870
4871         if (ptype != NULL)
4872           {
4873             if (! memberp)
4874               *ptype = debug_make_offset_type (minfo->dhandle, class_type,
4875                                                *ptype);
4876             else
4877               {
4878                 /* FIXME: We have no way to record constp or
4879                    volatilep.  */
4880                 *ptype = debug_make_method_type (minfo->dhandle, *ptype,
4881                                                  class_type, args, varargs);
4882               }
4883           }
4884       }
4885       break;
4886
4887     case 'G':
4888       ++*pp;
4889       if (! stab_demangle_type (minfo, pp, ptype))
4890         return false;
4891       break;
4892
4893     case 'C':
4894       ++*pp;
4895       if (! stab_demangle_type (minfo, pp, ptype))
4896         return false;
4897       if (ptype != NULL)
4898         *ptype = debug_make_const_type (minfo->dhandle, *ptype);
4899       break;
4900
4901     case 'Q':
4902       {
4903         const char *hold;
4904
4905         hold = *pp;
4906         if (! stab_demangle_qualified (minfo, pp, ptype))
4907           return false;
4908       }
4909       break;
4910
4911     default:
4912       if (! stab_demangle_fund_type (minfo, pp, ptype))
4913         return false;
4914       break;
4915     }
4916
4917   return true;
4918 }
4919
4920 /* Demangle a fundamental type.  If the ptype argument is not NULL,
4921    *ptype is set to the newly allocated type.  */
4922
4923 static boolean
4924 stab_demangle_fund_type (minfo, pp, ptype)
4925      struct stab_demangle_info *minfo;
4926      const char **pp;
4927      debug_type *ptype;
4928 {
4929   const char *orig;
4930   boolean constp, volatilep, unsignedp, signedp;
4931   boolean done;
4932
4933   orig = *pp;
4934
4935   constp = false;
4936   volatilep = false;
4937   unsignedp = false;
4938   signedp = false;
4939
4940   done = false;
4941   while (! done)
4942     {
4943       switch (**pp)
4944         {
4945         case 'C':
4946           constp = true;
4947           ++*pp;
4948           break;
4949
4950         case 'U':
4951           unsignedp = true;
4952           ++*pp;
4953           break;
4954
4955         case 'S':
4956           signedp = true;
4957           ++*pp;
4958           break;
4959
4960         case 'V':
4961           volatilep = true;
4962           ++*pp;
4963           break;
4964
4965         default:
4966           done = true;
4967           break;
4968         }
4969     }
4970
4971   switch (**pp)
4972     {
4973     case '\0':
4974     case '_':
4975       /* cplus_demangle permits this, but I don't know what it means.  */
4976       stab_bad_demangle (orig);
4977       break;
4978
4979     case 'v': /* void */
4980       if (ptype != NULL)
4981         {
4982           *ptype = debug_find_named_type (minfo->dhandle, "void");
4983           if (*ptype == DEBUG_TYPE_NULL)
4984             *ptype = debug_make_void_type (minfo->dhandle);
4985         }
4986       ++*pp;
4987       break;
4988
4989     case 'x': /* long long */
4990       if (ptype != NULL)
4991         {
4992           *ptype = debug_find_named_type (minfo->dhandle,
4993                                           (unsignedp
4994                                            ? "long long unsigned int"
4995                                            : "long long int"));
4996           if (*ptype == DEBUG_TYPE_NULL)
4997             *ptype = debug_make_int_type (minfo->dhandle, 8, unsignedp);
4998         }
4999       ++*pp;
5000       break;
5001
5002     case 'l': /* long */
5003       if (ptype != NULL)
5004         {
5005           *ptype = debug_find_named_type (minfo->dhandle,
5006                                           (unsignedp
5007                                            ? "long unsigned int"
5008                                            : "long int"));
5009           if (*ptype == DEBUG_TYPE_NULL)
5010             *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5011         }
5012       ++*pp;
5013       break;
5014
5015     case 'i': /* int */
5016       if (ptype != NULL)
5017         {
5018           *ptype = debug_find_named_type (minfo->dhandle,
5019                                           (unsignedp
5020                                            ? "unsigned int"
5021                                            : "int"));
5022           if (*ptype == DEBUG_TYPE_NULL)
5023             *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
5024         }
5025       ++*pp;
5026       break;
5027
5028     case 's': /* short */
5029       if (ptype != NULL)
5030         {
5031           *ptype = debug_find_named_type (minfo->dhandle,
5032                                           (unsignedp
5033                                            ? "short unsigned int"
5034                                            : "short int"));
5035           if (*ptype == DEBUG_TYPE_NULL)
5036             *ptype = debug_make_int_type (minfo->dhandle, 2, unsignedp);
5037         }
5038       ++*pp;
5039       break;
5040
5041     case 'b': /* bool */
5042       if (ptype != NULL)
5043         {
5044           *ptype = debug_find_named_type (minfo->dhandle, "bool");
5045           if (*ptype == DEBUG_TYPE_NULL)
5046             *ptype = debug_make_bool_type (minfo->dhandle, 4);
5047         }
5048       ++*pp;
5049       break;
5050
5051     case 'c': /* char */
5052       if (ptype != NULL)
5053         {
5054           *ptype = debug_find_named_type (minfo->dhandle,
5055                                           (unsignedp
5056                                            ? "unsigned char"
5057                                            : (signedp
5058                                               ? "signed char"
5059                                               : "char")));
5060           if (*ptype == DEBUG_TYPE_NULL)
5061             *ptype = debug_make_int_type (minfo->dhandle, 1, unsignedp);
5062         }
5063       ++*pp;
5064       break;
5065
5066     case 'w': /* wchar_t */
5067       if (ptype != NULL)
5068         {
5069           *ptype = debug_find_named_type (minfo->dhandle, "__wchar_t");
5070           if (*ptype == DEBUG_TYPE_NULL)
5071             *ptype = debug_make_int_type (minfo->dhandle, 2, true);
5072         }
5073       ++*pp;
5074       break;
5075
5076     case 'r': /* long double */
5077       if (ptype != NULL)
5078         {
5079           *ptype = debug_find_named_type (minfo->dhandle, "long double");
5080           if (*ptype == DEBUG_TYPE_NULL)
5081             *ptype = debug_make_float_type (minfo->dhandle, 8);
5082         }
5083       ++*pp;
5084       break;
5085
5086     case 'd': /* double */
5087       if (ptype != NULL)
5088         {
5089           *ptype = debug_find_named_type (minfo->dhandle, "double");
5090           if (*ptype == DEBUG_TYPE_NULL)
5091             *ptype = debug_make_float_type (minfo->dhandle, 8);
5092         }
5093       ++*pp;
5094       break;
5095
5096     case 'f': /* float */
5097       if (ptype != NULL)
5098         {
5099           *ptype = debug_find_named_type (minfo->dhandle, "float");
5100           if (*ptype == DEBUG_TYPE_NULL)
5101             *ptype = debug_make_float_type (minfo->dhandle, 4);
5102         }
5103       ++*pp;
5104       break;
5105
5106     case 'G':
5107       ++*pp;
5108       if (! ISDIGIT (**pp))
5109         {
5110           stab_bad_demangle (orig);
5111           return false;
5112         }
5113       /* Fall through.  */
5114     case '0': case '1': case '2': case '3': case '4':
5115     case '5': case '6': case '7': case '8': case '9':
5116       {
5117         const char *hold;
5118
5119         if (! stab_demangle_class (minfo, pp, &hold))
5120           return false;
5121         if (ptype != NULL)
5122           {
5123             char *name;
5124
5125             name = savestring (hold, *pp - hold);
5126             *ptype = debug_find_named_type (minfo->dhandle, name);
5127             free (name);
5128             if (*ptype == DEBUG_TYPE_NULL)
5129               {
5130                 /* FIXME: It is probably incorrect to assume that
5131                    undefined types are tagged types.  */
5132                 *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5133                                                 hold, *pp - hold,
5134                                                 DEBUG_KIND_ILLEGAL);
5135                 if (*ptype == DEBUG_TYPE_NULL)
5136                   return false;
5137               }
5138           }
5139       }
5140       break;
5141
5142     case 't':
5143       {
5144         char *name;
5145
5146         if (! stab_demangle_template (minfo, pp,
5147                                       ptype != NULL ? &name : NULL))
5148           return false;
5149         if (ptype != NULL)
5150           {
5151             *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
5152                                             name, strlen (name),
5153                                             DEBUG_KIND_CLASS);
5154             free (name);
5155             if (*ptype == DEBUG_TYPE_NULL)
5156               return false;
5157           }
5158       }
5159       break;
5160
5161     default:
5162       stab_bad_demangle (orig);
5163       return false;
5164     }
5165
5166   if (ptype != NULL)
5167     {
5168       if (constp)
5169         *ptype = debug_make_const_type (minfo->dhandle, *ptype);
5170       if (volatilep)
5171         *ptype = debug_make_volatile_type (minfo->dhandle, *ptype);
5172     }
5173
5174   return true;
5175 }
5176
5177 /* Remember a type string in a demangled string.  */
5178
5179 static boolean
5180 stab_demangle_remember_type (minfo, p, len)
5181      struct stab_demangle_info *minfo;
5182      const char *p;
5183      int len;
5184 {
5185   if (minfo->typestring_count >= minfo->typestring_alloc)
5186     {
5187       minfo->typestring_alloc += 10;
5188       minfo->typestrings = ((struct stab_demangle_typestring *)
5189                             xrealloc (minfo->typestrings,
5190                                       (minfo->typestring_alloc
5191                                        * sizeof *minfo->typestrings)));
5192     }
5193
5194   minfo->typestrings[minfo->typestring_count].typestring = p;
5195   minfo->typestrings[minfo->typestring_count].len = (unsigned int) len;
5196   ++minfo->typestring_count;
5197
5198   return true;
5199 }