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