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