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