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