* debug.h (struct debug_write_fns): Remove ellipsis_type. Add int
[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 *, boolean *));
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             {
831               debug_type ftype;
832
833               ftype = debug_make_function_type (dhandle, dtype,
834                                                 (debug_type *) NULL, false);
835               dtype = debug_make_pointer_type (dhandle, ftype);
836             }
837         }
838       if (dtype == DEBUG_TYPE_NULL)
839         return false;
840       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_STACK,
841                                     value))
842         return false;
843
844       /* FIXME: At this point gdb considers rearranging the parameter
845          address on a big endian machine if it is smaller than an int.
846          We have no way to do that, since we don't really know much
847          about the target.  */
848
849       break;
850
851     case 'P':
852       if (stabtype == N_FUN)
853         {
854           /* Prototype of a function referenced by this file.  */
855           while (*p == ';')
856             {
857               ++p;
858               if (parse_stab_type (dhandle, info, (const char *) NULL, &p,
859                                    (debug_type **) NULL)
860                   == DEBUG_TYPE_NULL)
861                 return false;
862             }
863           break;
864         }
865       /* Fall through.  */
866     case 'R':
867       /* Parameter which is in a register.  */
868       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
869                                (debug_type **) NULL);
870       if (dtype == DEBUG_TYPE_NULL)
871         return false;
872       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REG,
873                                     value))
874         return false;
875       break;
876
877     case 'r':
878       /* Register variable (either global or local).  */
879       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
880                                (debug_type **) NULL);
881       if (dtype == DEBUG_TYPE_NULL)
882         return false;
883       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_REGISTER,
884                                   value))
885         return false;
886
887       /* FIXME: At this point gdb checks to combine pairs of 'p' and
888          'r' stabs into a single 'P' stab.  */
889
890       break;
891
892     case 'S':
893       /* Static symbol at top level of file */
894       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
895                                (debug_type **) NULL);
896       if (dtype == DEBUG_TYPE_NULL)
897         return false;
898       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_STATIC,
899                                   value))
900         return false;
901       break;
902
903     case 't':
904       /* A typedef.  */
905       dtype = parse_stab_type (dhandle, info, name, &p, &slot);
906       if (dtype == DEBUG_TYPE_NULL)
907         return false;
908       if (name == NULL)
909         {
910           /* A nameless type.  Nothing to do.  */
911           return true;
912         }
913
914       dtype = debug_name_type (dhandle, name, dtype);
915       if (dtype == DEBUG_TYPE_NULL)
916         return false;
917
918       if (slot != NULL)
919         *slot = dtype;
920
921       break;
922
923     case 'T':
924       /* Struct, union, or enum tag.  For GNU C++, this can be be followed
925          by 't' which means we are typedef'ing it as well.  */
926       if (*p != 't')
927         {
928           synonym = false;
929           /* FIXME: gdb sets synonym to true if the current language
930              is C++.  */
931         }
932       else
933         {
934           synonym = true;
935           ++p;
936         }
937
938       dtype = parse_stab_type (dhandle, info, name, &p, &slot);
939       if (dtype == DEBUG_TYPE_NULL)
940         return false;
941       if (name == NULL)
942         return true;
943
944       dtype = debug_tag_type (dhandle, name, dtype);
945       if (dtype == DEBUG_TYPE_NULL)
946         return false;
947       if (slot != NULL)
948         *slot = dtype;
949
950       /* See if we have a cross reference to this tag which we can now
951          fill in.  */
952       {
953         register struct stab_tag **pst;
954
955         for (pst = &info->tags; *pst != NULL; pst = &(*pst)->next)
956           {
957             if ((*pst)->name[0] == name[0]
958                 && strcmp ((*pst)->name, name) == 0)
959               {
960                 (*pst)->slot = dtype;
961                 *pst = (*pst)->next;
962                 break;
963               }
964           }
965       }
966
967       if (synonym)
968         {
969           dtype = debug_name_type (dhandle, name, dtype);
970           if (dtype == DEBUG_TYPE_NULL)
971             return false;
972
973           if (slot != NULL)
974             *slot = dtype;
975         }
976
977       break;
978
979     case 'V':
980       /* Static symbol of local scope */
981       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
982                                (debug_type **) NULL);
983       if (dtype == DEBUG_TYPE_NULL)
984         return false;
985       /* FIXME: gdb checks os9k_stabs here.  */
986       if (! stab_record_variable (dhandle, info, name, dtype,
987                                   DEBUG_LOCAL_STATIC, value))
988         return false;
989       break;
990
991     case 'v':
992       /* Reference parameter.  */
993       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
994                                (debug_type **) NULL);
995       if (dtype == DEBUG_TYPE_NULL)
996         return false;
997       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REFERENCE,
998                                     value))
999         return false;
1000       break;
1001
1002     case 'a':
1003       /* Reference parameter which is in a register.  */
1004       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1005                                (debug_type **) NULL);
1006       if (dtype == DEBUG_TYPE_NULL)
1007         return false;
1008       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REF_REG,
1009                                     value))
1010         return false;
1011       break;
1012
1013     case 'X':
1014       /* This is used by Sun FORTRAN for "function result value".
1015          Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1016          that Pascal uses it too, but when I tried it Pascal used
1017          "x:3" (local symbol) instead.  */
1018       dtype = parse_stab_type (dhandle, info, (const char *) NULL, &p,
1019                                (debug_type **) NULL);
1020       if (dtype == DEBUG_TYPE_NULL)
1021         return false;
1022       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
1023                                   value))
1024         return false;
1025       break;
1026
1027     default:
1028       bad_stab (string);
1029       return false;
1030     }
1031
1032   /* FIXME: gdb converts structure values to structure pointers in a
1033      couple of cases, depending upon the target.  */
1034
1035   return true;
1036 }
1037
1038 /* Parse a stabs type.  The typename argument is non-NULL if this is a
1039    typedef or a tag definition.  The pp argument points to the stab
1040    string, and is updated.  The slotp argument points to a place to
1041    store the slot used if the type is being defined.  */
1042
1043 static debug_type
1044 parse_stab_type (dhandle, info, typename, pp, slotp)
1045      PTR dhandle;
1046      struct stab_handle *info;
1047      const char *typename;
1048      const char **pp;
1049      debug_type **slotp;
1050 {
1051   const char *orig;
1052   int typenums[2];
1053   int size;
1054   boolean stringp;
1055   int descriptor;
1056   debug_type dtype;
1057
1058   if (slotp != NULL)
1059     *slotp = NULL;
1060
1061   orig = *pp;
1062
1063   size = -1;
1064   stringp = false;
1065
1066   /* Read type number if present.  The type number may be omitted.
1067      for instance in a two-dimensional array declared with type
1068      "ar1;1;10;ar1;1;10;4".  */
1069   if (! isdigit ((unsigned char) **pp) && **pp != '(' && **pp != '-')
1070     {
1071       /* 'typenums=' not present, type is anonymous.  Read and return
1072          the definition, but don't put it in the type vector.  */
1073       typenums[0] = typenums[1] = -1;
1074     }
1075   else
1076     {
1077       if (! parse_stab_type_number (pp, typenums))
1078         return DEBUG_TYPE_NULL;
1079
1080       if (**pp != '=')
1081         {
1082           /* Type is not being defined here.  Either it already
1083              exists, or this is a forward reference to it.  */
1084           return stab_find_type (dhandle, info, typenums);
1085         }
1086
1087       /* Only set the slot if the type is being defined.  This means
1088          that the mapping from type numbers to types will only record
1089          the name of the typedef which defines a type.  If we don't do
1090          this, then something like
1091              typedef int foo;
1092              int i;
1093          will record that i is of type foo.  Unfortunately, stabs
1094          information is ambiguous about variable types.  For this code,
1095              typedef int foo;
1096              int i;
1097              foo j;
1098          the stabs information records both i and j as having the same
1099          type.  This could be fixed by patching the compiler.  */
1100       if (slotp != NULL && typenums[0] >= 0 && typenums[1] >= 0)
1101         *slotp = stab_find_slot (info, typenums);
1102
1103       /* Type is being defined here.  */
1104       /* Skip the '='.  */
1105       ++*pp;
1106
1107       while (**pp == '@')
1108         {
1109           const char *p = *pp + 1;
1110           const char *attr;
1111
1112           if (isdigit ((unsigned char) *p) || *p == '(' || *p == '-')
1113             {
1114               /* Member type.  */
1115               break;
1116             }
1117
1118           /* Type attributes.  */
1119           attr = p;
1120
1121           for (; *p != ';'; ++p)
1122             {
1123               if (*p == '\0')
1124                 {
1125                   bad_stab (orig);
1126                   return DEBUG_TYPE_NULL;
1127                 }
1128             }
1129           *pp = p + 1;
1130
1131           switch (*attr)
1132             {
1133             case 's':
1134               size = atoi (attr + 1);
1135               if (size <= 0)
1136                 size = -1;
1137               break;
1138
1139             case 'S':
1140               stringp = true;
1141               break;
1142
1143             default:
1144               /* Ignore unrecognized type attributes, so future
1145                  compilers can invent new ones.  */
1146               break;
1147             }
1148         }
1149     }
1150
1151   descriptor = **pp;
1152   ++*pp;
1153
1154   switch (descriptor)
1155     {
1156     case 'x':
1157       {
1158         enum debug_type_kind code;
1159         const char *q1, *q2, *p;
1160
1161         /* A cross reference to another type.  */
1162
1163         switch (**pp)
1164           {
1165           case 's':
1166             code = DEBUG_KIND_STRUCT;
1167             break;
1168           case 'u':
1169             code = DEBUG_KIND_UNION;
1170             break;
1171           case 'e':
1172             code = DEBUG_KIND_ENUM;
1173             break;
1174           default:
1175             /* Complain and keep going, so compilers can invent new
1176                cross-reference types.  */
1177             warn_stab (orig, "unrecognized cross reference type");
1178             code = DEBUG_KIND_STRUCT;
1179             break;
1180           }
1181         ++*pp;
1182
1183         q1 = strchr (*pp, '<');
1184         p = strchr (*pp, ':');
1185         if (p == NULL)
1186           {
1187             bad_stab (orig);
1188             return DEBUG_TYPE_NULL;
1189           }
1190         while (q1 != NULL && p > q1 && p[1] == ':')
1191           {
1192             q2 = strchr (q1, '>');
1193             if (q2 == NULL || q2 < p)
1194               break;
1195             p += 2;
1196             p = strchr (p, ':');
1197             if (p == NULL)
1198               {
1199                 bad_stab (orig);
1200                 return DEBUG_TYPE_NULL;
1201               }
1202           }
1203
1204         dtype = stab_find_tagged_type (dhandle, info, *pp, p - *pp, code);
1205
1206         *pp = p + 1;
1207       }
1208       break;
1209
1210     case '-':
1211     case '0':
1212     case '1':
1213     case '2':
1214     case '3':
1215     case '4':
1216     case '5':
1217     case '6':
1218     case '7':
1219     case '8':
1220     case '9':
1221     case '(':
1222       {
1223         const char *hold;
1224         int xtypenums[2];
1225
1226         /* This type is defined as another type.  */
1227
1228         (*pp)--;
1229         hold = *pp;
1230
1231         /* Peek ahead at the number to detect void.  */
1232         if (! parse_stab_type_number (pp, xtypenums))
1233           return DEBUG_TYPE_NULL;
1234
1235         if (typenums[0] == xtypenums[0] && typenums[1] == xtypenums[1])
1236           {
1237             /* This type is being defined as itself, which means that
1238                it is void.  */
1239             dtype = debug_make_void_type (dhandle);
1240           }
1241         else
1242           {
1243             *pp = hold;
1244
1245             /* Go back to the number and have parse_stab_type get it.
1246                This means that we can deal with something like
1247                t(1,2)=(3,4)=... which the Lucid compiler uses.  */
1248             dtype = parse_stab_type (dhandle, info, (const char *) NULL,
1249                                      pp, (debug_type **) NULL);
1250             if (dtype == DEBUG_TYPE_NULL)
1251               return DEBUG_TYPE_NULL;
1252           }
1253
1254         if (typenums[0] != -1)
1255           {
1256             if (! stab_record_type (dhandle, info, typenums, dtype))
1257               return DEBUG_TYPE_NULL;
1258           }
1259
1260         break;
1261       }
1262
1263     case '*':
1264       dtype = debug_make_pointer_type (dhandle,
1265                                        parse_stab_type (dhandle, info,
1266                                                         (const char *) NULL,
1267                                                         pp,
1268                                                         (debug_type **) NULL));
1269       break;
1270
1271     case '&':
1272       /* Reference to another type.  */
1273       dtype = (debug_make_reference_type
1274                (dhandle,
1275                 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1276                                  (debug_type **) NULL)));
1277       break;
1278
1279     case 'f':
1280       /* Function returning another type.  */
1281       /* FIXME: gdb checks os9k_stabs here.  */
1282       dtype = (debug_make_function_type
1283                (dhandle,
1284                 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1285                                  (debug_type **) NULL),
1286                 (debug_type *) NULL, false));
1287       break;
1288
1289     case 'k':
1290       /* Const qualifier on some type (Sun).  */
1291       /* FIXME: gdb accepts 'c' here if os9k_stabs.  */
1292       dtype = debug_make_const_type (dhandle,
1293                                      parse_stab_type (dhandle, info,
1294                                                       (const char *) NULL,
1295                                                       pp,
1296                                                       (debug_type **) NULL));
1297       break;
1298
1299     case 'B':
1300       /* Volatile qual on some type (Sun).  */
1301       /* FIXME: gdb accepts 'i' here if os9k_stabs.  */
1302       dtype = (debug_make_volatile_type
1303                (dhandle,
1304                 parse_stab_type (dhandle, info, (const char *) NULL, pp,
1305                                  (debug_type **) NULL)));
1306       break;
1307
1308     case '@':
1309       /* Offset (class & variable) type.  This is used for a pointer
1310          relative to an object.  */
1311       {
1312         debug_type domain;
1313         debug_type memtype;
1314
1315         /* Member type.  */
1316
1317         domain = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1318                                   (debug_type **) NULL);
1319         if (domain == DEBUG_TYPE_NULL)
1320           return DEBUG_TYPE_NULL;
1321
1322         if (**pp != ',')
1323           {
1324             bad_stab (orig);
1325             return DEBUG_TYPE_NULL;
1326           }
1327         ++*pp;
1328
1329         memtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
1330                                    (debug_type **) NULL);
1331         if (memtype == DEBUG_TYPE_NULL)
1332           return DEBUG_TYPE_NULL;
1333
1334         dtype = debug_make_offset_type (dhandle, domain, memtype);
1335       }
1336       break;
1337
1338     case '#':
1339       /* Method (class & fn) type.  */
1340       if (**pp == '#')
1341         {
1342           debug_type return_type;
1343
1344           ++*pp;
1345           return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1346                                          pp, (debug_type **) NULL);
1347           if (return_type == DEBUG_TYPE_NULL)
1348             return DEBUG_TYPE_NULL;
1349           if (**pp != ';')
1350             {
1351               bad_stab (orig);
1352               return DEBUG_TYPE_NULL;
1353             }
1354           ++*pp;
1355           dtype = debug_make_method_type (dhandle, return_type,
1356                                           DEBUG_TYPE_NULL,
1357                                           (debug_type *) NULL, false);
1358         }
1359       else
1360         {
1361           debug_type domain;
1362           debug_type return_type;
1363           debug_type *args;
1364           unsigned int n;
1365           unsigned int alloc;
1366           boolean varargs;
1367
1368           domain = parse_stab_type (dhandle, info, (const char *) NULL,
1369                                     pp, (debug_type **) NULL);
1370           if (domain == DEBUG_TYPE_NULL)
1371             return DEBUG_TYPE_NULL;
1372
1373           if (**pp != ',')
1374             {
1375               bad_stab (orig);
1376               return DEBUG_TYPE_NULL;
1377             }
1378           ++*pp;
1379
1380           return_type = parse_stab_type (dhandle, info, (const char *) NULL,
1381                                          pp, (debug_type **) NULL);
1382           if (return_type == DEBUG_TYPE_NULL)
1383             return DEBUG_TYPE_NULL;
1384
1385           alloc = 10;
1386           args = (debug_type *) xmalloc (alloc * sizeof *args);
1387           n = 0;
1388           while (**pp != ';')
1389             {
1390               if (**pp != ',')
1391                 {
1392                   bad_stab (orig);
1393                   return DEBUG_TYPE_NULL;
1394                 }
1395               ++*pp;
1396
1397               if (n + 1 >= alloc)
1398                 {
1399                   alloc += 10;
1400                   args = ((debug_type *)
1401                           xrealloc ((PTR) args, alloc * sizeof *args));
1402                 }
1403
1404               args[n] = parse_stab_type (dhandle, info, (const char *) NULL,
1405                                          pp, (debug_type **) NULL);
1406               if (args[n] == DEBUG_TYPE_NULL)
1407                 return DEBUG_TYPE_NULL;
1408               ++n;
1409             }
1410           ++*pp;
1411
1412           /* If the last type is not void, then this function takes a
1413              variable number of arguments.  Otherwise, we must strip
1414              the void type.  */
1415           if (n == 0
1416               || debug_get_type_kind (dhandle, args[n - 1]) != DEBUG_KIND_VOID)
1417             varargs = true;
1418           else
1419             {
1420               --n;
1421               varargs = false;
1422             }
1423
1424           args[n] = DEBUG_TYPE_NULL;
1425
1426           dtype = debug_make_method_type (dhandle, return_type, domain, args,
1427                                           varargs);
1428         }
1429       break;
1430
1431     case 'r':
1432       /* Range type.  */
1433       dtype = parse_stab_range_type (dhandle, info, typename, pp, typenums);
1434       break;
1435
1436     case 'b':
1437       /* FIXME: gdb checks os9k_stabs here.  */
1438       /* Sun ACC builtin int type.  */
1439       dtype = parse_stab_sun_builtin_type (dhandle, pp);
1440       break;
1441
1442     case 'R':
1443       /* Sun ACC builtin float type.  */
1444       dtype = parse_stab_sun_floating_type (dhandle, pp);
1445       break;
1446
1447     case 'e':
1448       /* Enumeration type.  */
1449       dtype = parse_stab_enum_type (dhandle, pp);
1450       break;
1451
1452     case 's':
1453     case 'u':
1454       /* Struct or union type.  */
1455       dtype = parse_stab_struct_type (dhandle, info, typename, pp,
1456                                       descriptor == 's', typenums);
1457       break;
1458
1459     case 'a':
1460       /* Array type.  */
1461       if (**pp != 'r')
1462         {
1463           bad_stab (orig);
1464           return DEBUG_TYPE_NULL;
1465         }
1466       ++*pp;
1467
1468       dtype = parse_stab_array_type (dhandle, info, pp, stringp);
1469       break;
1470
1471     case 'S':
1472       dtype = debug_make_set_type (dhandle,
1473                                    parse_stab_type (dhandle, info,
1474                                                     (const char *) NULL,
1475                                                     pp,
1476                                                     (debug_type **) NULL),
1477                                    stringp);
1478       break;
1479
1480     default:
1481       bad_stab (orig);
1482       return DEBUG_TYPE_NULL;
1483     }
1484
1485   if (dtype == DEBUG_TYPE_NULL)
1486     return DEBUG_TYPE_NULL;
1487
1488   if (typenums[0] != -1)
1489     {
1490       if (! stab_record_type (dhandle, info, typenums, dtype))
1491         return DEBUG_TYPE_NULL;
1492     }
1493
1494   if (size != -1)
1495     {
1496       if (! debug_record_type_size (dhandle, dtype, (unsigned int) size))
1497         return false;
1498     }
1499
1500   return dtype;
1501 }
1502
1503 /* Read a number by which a type is referred to in dbx data, or
1504    perhaps read a pair (FILENUM, TYPENUM) in parentheses.  Just a
1505    single number N is equivalent to (0,N).  Return the two numbers by
1506    storing them in the vector TYPENUMS.  */
1507
1508 static boolean
1509 parse_stab_type_number (pp, typenums)
1510      const char **pp;
1511      int *typenums;
1512 {
1513   const char *orig;
1514
1515   orig = *pp;
1516
1517   if (**pp != '(')
1518     {
1519       typenums[0] = 0;
1520       typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1521     }
1522   else
1523     {
1524       ++*pp;
1525       typenums[0] = (int) parse_number (pp, (boolean *) NULL);
1526       if (**pp != ',')
1527         {
1528           bad_stab (orig);
1529           return false;
1530         }
1531       ++*pp;
1532       typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1533       if (**pp != ')')
1534         {
1535           bad_stab (orig);
1536           return false;
1537         }
1538       ++*pp;
1539     }
1540
1541   return true;
1542 }
1543
1544 /* Parse a range type.  */
1545
1546 static debug_type
1547 parse_stab_range_type (dhandle, info, typename, pp, typenums)
1548      PTR dhandle;
1549      struct stab_handle *info;
1550      const char *typename;
1551      const char **pp;
1552      const int *typenums;
1553 {
1554   const char *orig;
1555   int rangenums[2];
1556   boolean self_subrange;
1557   debug_type index_type;
1558   const char *s2, *s3;
1559   bfd_signed_vma n2, n3;
1560   boolean ov2, ov3;
1561
1562   orig = *pp;
1563
1564   index_type = DEBUG_TYPE_NULL;
1565
1566   /* First comes a type we are a subrange of.
1567      In C it is usually 0, 1 or the type being defined.  */
1568   if (! parse_stab_type_number (pp, rangenums))
1569     return DEBUG_TYPE_NULL;
1570
1571   self_subrange = (rangenums[0] == typenums[0]
1572                    && rangenums[1] == typenums[1]);
1573
1574   if (**pp == '=')
1575     {
1576       *pp = orig;
1577       index_type = parse_stab_type (dhandle, info, (const char *) NULL,
1578                                     pp, (debug_type **) NULL);
1579       if (index_type == DEBUG_TYPE_NULL)
1580         return DEBUG_TYPE_NULL;
1581     }
1582
1583   if (**pp == ';')
1584     ++*pp;
1585
1586   /* The remaining two operands are usually lower and upper bounds of
1587      the range.  But in some special cases they mean something else.  */
1588   s2 = *pp;
1589   n2 = parse_number (pp, &ov2);
1590   if (**pp != ';')
1591     {
1592       bad_stab (orig);
1593       return DEBUG_TYPE_NULL;
1594     }
1595   ++*pp;
1596
1597   s3 = *pp;
1598   n3 = parse_number (pp, &ov3);
1599   if (**pp != ';')
1600     {
1601       bad_stab (orig);
1602       return DEBUG_TYPE_NULL;
1603     }
1604   ++*pp;
1605
1606   if (ov2 || ov3)
1607     {
1608       /* gcc will emit range stabs for long long types.  Handle this
1609          as a special case.  FIXME: This needs to be more general.  */
1610 #define LLLOW  "01000000000000000000000;"
1611 #define LLHIGH "0777777777777777777777;"
1612 #define ULLHIGH "01777777777777777777777;"
1613       if (index_type == DEBUG_TYPE_NULL)
1614         {
1615           if (strncmp (s2, LLLOW, sizeof LLLOW - 1) == 0
1616               && strncmp (s3, LLHIGH, sizeof LLHIGH - 1) == 0)
1617             return debug_make_int_type (dhandle, 8, false);
1618           if (! ov2
1619               && n2 == 0
1620               && strncmp (s3, ULLHIGH, sizeof ULLHIGH - 1) == 0)
1621             return debug_make_int_type (dhandle, 8, true);
1622         }
1623
1624       warn_stab (orig, "numeric overflow");
1625     }
1626
1627   if (index_type == DEBUG_TYPE_NULL)
1628     {
1629       /* A type defined as a subrange of itself, with both bounds 0,
1630          is void.  */
1631       if (self_subrange && n2 == 0 && n3 == 0)
1632         return debug_make_void_type (dhandle);
1633
1634       /* If n3 is zero and n2 is positive, this is a floating point
1635          type, and n2 is the number of bytes.  */
1636       if (n3 == 0 && n2 > 0)
1637         return debug_make_float_type (dhandle, n2);
1638
1639       /* If the upper bound is -1, this is an unsigned int.  */
1640       if (n2 == 0 && n3 == -1)
1641         {
1642           /* When gcc is used with -gstabs, but not -gstabs+, it will emit
1643                  long long int:t6=r1;0;-1;
1644                  long long unsigned int:t7=r1;0;-1;
1645              We hack here to handle this reasonably.  */
1646           if (typename != NULL)
1647             {
1648               if (strcmp (typename, "long long int") == 0)
1649                 return debug_make_int_type (dhandle, 8, false);
1650               else if (strcmp (typename, "long long unsigned int") == 0)
1651                 return debug_make_int_type (dhandle, 8, true);
1652             }
1653           /* FIXME: The size here really depends upon the target.  */
1654           return debug_make_int_type (dhandle, 4, true);
1655         }
1656
1657       /* A range of 0 to 127 is char.  */
1658       if (self_subrange && n2 == 0 && n3 == 127)
1659         return debug_make_int_type (dhandle, 1, false);
1660
1661       /* FIXME: gdb checks for the language CHILL here.  */
1662
1663       if (n2 == 0)
1664         {
1665           if (n3 < 0)
1666             return debug_make_int_type (dhandle, - n3, true);
1667           else if (n3 == 0xff)
1668             return debug_make_int_type (dhandle, 1, true);
1669           else if (n3 == 0xffff)
1670             return debug_make_int_type (dhandle, 2, true);
1671           /* -1 is used for the upper bound of (4 byte) "unsigned int"
1672              and "unsigned long", and we already checked for that, so
1673              don't need to test for it here.  */
1674         }
1675       else if (n3 == 0
1676                && n2 < 0
1677                && (self_subrange || n2 == -8))
1678         return debug_make_int_type (dhandle, - n2, true);
1679       else if (n2 == - n3 - 1)
1680         {
1681           if (n3 == 0x7f)
1682             return debug_make_int_type (dhandle, 1, false);
1683           else if (n3 == 0x7fff)
1684             return debug_make_int_type (dhandle, 2, false);
1685           else if (n3 == 0x7fffffff)
1686             return debug_make_int_type (dhandle, 4, false);
1687         }
1688     }
1689
1690   /* At this point I don't have the faintest idea how to deal with a
1691      self_subrange type; I'm going to assume that this is used as an
1692      idiom, and that all of them are special cases.  So . . .  */
1693   if (self_subrange)
1694     {
1695       bad_stab (orig);
1696       return DEBUG_TYPE_NULL;
1697     }
1698
1699   index_type = stab_find_type (dhandle, info, rangenums);
1700   if (index_type == DEBUG_TYPE_NULL)
1701     {
1702       /* Does this actually ever happen?  Is that why we are worrying
1703          about dealing with it rather than just calling error_type?  */
1704       warn_stab (orig, "missing index type");
1705       index_type = debug_make_int_type (dhandle, 4, false);
1706     }
1707
1708   return debug_make_range_type (dhandle, index_type, n2, n3);
1709 }
1710
1711 /* Sun's ACC uses a somewhat saner method for specifying the builtin
1712    typedefs in every file (for int, long, etc):
1713
1714         type = b <signed> <width>; <offset>; <nbits>
1715         signed = u or s.  Possible c in addition to u or s (for char?).
1716         offset = offset from high order bit to start bit of type.
1717         width is # bytes in object of this type, nbits is # bits in type.
1718
1719    The width/offset stuff appears to be for small objects stored in
1720    larger ones (e.g. `shorts' in `int' registers).  We ignore it for now,
1721    FIXME.  */
1722
1723 static debug_type
1724 parse_stab_sun_builtin_type (dhandle, pp)
1725      PTR dhandle;
1726      const char **pp;
1727 {
1728   const char *orig;
1729   boolean unsignedp;
1730   bfd_vma bits;
1731
1732   orig = *pp;
1733
1734   switch (**pp)
1735     {
1736     case 's':
1737       unsignedp = false;
1738       break;
1739     case 'u':
1740       unsignedp = true;
1741       break;
1742     default:
1743       bad_stab (orig);
1744       return DEBUG_TYPE_NULL;
1745     }
1746   ++*pp;
1747
1748   /* For some odd reason, all forms of char put a c here.  This is strange
1749      because no other type has this honor.  We can safely ignore this because
1750      we actually determine 'char'acterness by the number of bits specified in
1751      the descriptor.  */
1752   if (**pp == 'c')
1753     ++*pp;
1754
1755   /* The first number appears to be the number of bytes occupied
1756      by this type, except that unsigned short is 4 instead of 2.
1757      Since this information is redundant with the third number,
1758      we will ignore it.  */
1759   (void) parse_number (pp, (boolean *) NULL);
1760   if (**pp != ';')
1761     {
1762       bad_stab (orig);
1763       return DEBUG_TYPE_NULL;
1764     }
1765   ++*pp;
1766
1767   /* The second number is always 0, so ignore it too. */
1768   (void) parse_number (pp, (boolean *) NULL);
1769   if (**pp != ';')
1770     {
1771       bad_stab (orig);
1772       return DEBUG_TYPE_NULL;
1773     }
1774   ++*pp;
1775
1776   /* The third number is the number of bits for this type. */
1777   bits = parse_number (pp, (boolean *) NULL);
1778
1779   /* The type *should* end with a semicolon.  If it are embedded
1780      in a larger type the semicolon may be the only way to know where
1781      the type ends.  If this type is at the end of the stabstring we
1782      can deal with the omitted semicolon (but we don't have to like
1783      it).  Don't bother to complain(), Sun's compiler omits the semicolon
1784      for "void".  */
1785   if (**pp == ';')
1786     ++*pp;
1787
1788   if (bits == 0)
1789     return debug_make_void_type (dhandle);
1790
1791   return debug_make_int_type (dhandle, bits / 8, unsignedp);
1792 }
1793
1794 /* Parse a builtin floating type generated by the Sun compiler.  */
1795
1796 static debug_type
1797 parse_stab_sun_floating_type (dhandle, pp)
1798      PTR dhandle;
1799      const char **pp;
1800 {
1801   const char *orig;
1802   bfd_vma details;
1803   bfd_vma bytes;
1804
1805   orig = *pp;
1806
1807   /* The first number has more details about the type, for example
1808      FN_COMPLEX.  */
1809   details = parse_number (pp, (boolean *) NULL);
1810   if (**pp != ';')
1811     {
1812       bad_stab (orig);
1813       return DEBUG_TYPE_NULL;
1814     }
1815
1816   /* The second number is the number of bytes occupied by this type */
1817   bytes = parse_number (pp, (boolean *) NULL);
1818   if (**pp != ';')
1819     {
1820       bad_stab (orig);
1821       return DEBUG_TYPE_NULL;
1822     }
1823
1824   if (details == NF_COMPLEX
1825       || details == NF_COMPLEX16
1826       || details == NF_COMPLEX32)
1827     return debug_make_complex_type (dhandle, bytes);
1828
1829   return debug_make_float_type (dhandle, bytes);      
1830 }
1831
1832 /* Handle an enum type.  */
1833
1834 static debug_type
1835 parse_stab_enum_type (dhandle, pp)
1836      PTR dhandle;
1837      const char **pp;
1838 {
1839   const char *orig;
1840   const char **names;
1841   bfd_signed_vma *values;
1842   unsigned int n;
1843   unsigned int alloc;
1844
1845   orig = *pp;
1846
1847   /* FIXME: gdb checks os9k_stabs here.  */
1848
1849   /* The aix4 compiler emits an extra field before the enum members;
1850      my guess is it's a type of some sort.  Just ignore it.  */
1851   if (**pp == '-')
1852     {
1853       while (**pp != ':')
1854         ++*pp;
1855       ++*pp;
1856     }
1857
1858   /* Read the value-names and their values.
1859      The input syntax is NAME:VALUE,NAME:VALUE, and so on.
1860      A semicolon or comma instead of a NAME means the end.  */
1861   alloc = 10;
1862   names = (const char **) xmalloc (alloc * sizeof *names);
1863   values = (bfd_signed_vma *) xmalloc (alloc * sizeof *values);
1864   n = 0;
1865   while (**pp != '\0' && **pp != ';' && **pp != ',')
1866     {
1867       const char *p;
1868       char *name;
1869       bfd_signed_vma val;
1870
1871       p = *pp;
1872       while (*p != ':')
1873         ++p;
1874
1875       name = savestring (*pp, p - *pp);
1876
1877       *pp = p + 1;
1878       val = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
1879       if (**pp != ',')
1880         {
1881           bad_stab (orig);
1882           return DEBUG_TYPE_NULL;
1883         }
1884       ++*pp;
1885
1886       if (n + 1 >= alloc)
1887         {
1888           alloc += 10;
1889           names = ((const char **)
1890                    xrealloc ((PTR) names, alloc * sizeof *names));
1891           values = ((bfd_signed_vma *)
1892                     xrealloc ((PTR) values, alloc * sizeof *values));
1893         }
1894
1895       names[n] = name;
1896       values[n] = val;
1897       ++n;
1898     }
1899
1900   names[n] = NULL;
1901   values[n] = 0;
1902
1903   if (**pp == ';')
1904     ++*pp;
1905
1906   return debug_make_enum_type (dhandle, names, values);
1907 }
1908
1909 /* Read the description of a structure (or union type) and return an object
1910    describing the type.
1911
1912    PP points to a character pointer that points to the next unconsumed token
1913    in the the stabs string.  For example, given stabs "A:T4=s4a:1,0,32;;",
1914    *PP will point to "4a:1,0,32;;".  */
1915
1916 static debug_type
1917 parse_stab_struct_type (dhandle, info, tagname, pp, structp, typenums)
1918      PTR dhandle;
1919      struct stab_handle *info;
1920      const char *tagname;
1921      const char **pp;
1922      boolean structp;
1923      const int *typenums;
1924 {
1925   const char *orig;
1926   bfd_vma size;
1927   debug_baseclass *baseclasses;
1928   debug_field *fields;
1929   boolean statics;
1930   debug_method *methods;
1931   debug_type vptrbase;
1932   boolean ownvptr;
1933
1934   orig = *pp;
1935
1936   /* Get the size.  */
1937   size = parse_number (pp, (boolean *) NULL);
1938
1939   /* Get the other information.  */
1940   if (! parse_stab_baseclasses (dhandle, info, pp, &baseclasses)
1941       || ! parse_stab_struct_fields (dhandle, info, pp, &fields, &statics)
1942       || ! parse_stab_members (dhandle, info, tagname, pp, typenums, &methods)
1943       || ! parse_stab_tilde_field (dhandle, info, pp, typenums, &vptrbase,
1944                                    &ownvptr))
1945     return DEBUG_TYPE_NULL;
1946
1947   if (! statics
1948       && baseclasses == NULL
1949       && methods == NULL
1950       && vptrbase == DEBUG_TYPE_NULL
1951       && ! ownvptr)
1952     return debug_make_struct_type (dhandle, structp, size, fields);
1953
1954   return debug_make_object_type (dhandle, structp, size, fields, baseclasses,
1955                                  methods, vptrbase, ownvptr);
1956 }
1957
1958 /* The stabs for C++ derived classes contain baseclass information which
1959    is marked by a '!' character after the total size.  This function is
1960    called when we encounter the baseclass marker, and slurps up all the
1961    baseclass information.
1962
1963    Immediately following the '!' marker is the number of base classes that
1964    the class is derived from, followed by information for each base class.
1965    For each base class, there are two visibility specifiers, a bit offset
1966    to the base class information within the derived class, a reference to
1967    the type for the base class, and a terminating semicolon.
1968
1969    A typical example, with two base classes, would be "!2,020,19;0264,21;".
1970                                                        ^^ ^ ^ ^  ^ ^  ^
1971         Baseclass information marker __________________|| | | |  | |  |
1972         Number of baseclasses __________________________| | | |  | |  |
1973         Visibility specifiers (2) ________________________| | |  | |  |
1974         Offset in bits from start of class _________________| |  | |  |
1975         Type number for base class ___________________________|  | |  |
1976         Visibility specifiers (2) _______________________________| |  |
1977         Offset in bits from start of class ________________________|  |
1978         Type number of base class ____________________________________|
1979
1980   Return true for success, false for failure.  */
1981
1982 static boolean
1983 parse_stab_baseclasses (dhandle, info, pp, retp)
1984      PTR dhandle;
1985      struct stab_handle *info;
1986      const char **pp;
1987      debug_baseclass **retp;
1988 {
1989   const char *orig;
1990   unsigned int c, i;
1991   debug_baseclass *classes;
1992
1993   *retp = NULL;
1994
1995   orig = *pp;
1996
1997   if (**pp != '!')
1998     {
1999       /* No base classes.  */
2000       return true;
2001     }
2002   ++*pp;
2003
2004   c = (unsigned int) parse_number (pp, (boolean *) NULL);
2005
2006   if (**pp != ',')
2007     {
2008       bad_stab (orig);
2009       return false;
2010     }
2011   ++*pp;
2012
2013   classes = (debug_baseclass *) xmalloc ((c + 1) * sizeof (**retp));
2014
2015   for (i = 0; i < c; i++)
2016     {
2017       boolean virtual;
2018       enum debug_visibility visibility;
2019       bfd_vma bitpos;
2020       debug_type type;
2021
2022       switch (**pp)
2023         {
2024         case '0':
2025           virtual = false;
2026           break;
2027         case '1':
2028           virtual = true;
2029           break;
2030         default:
2031           warn_stab (orig, "unknown virtual character for baseclass");
2032           virtual = false;
2033           break;
2034         }
2035       ++*pp;
2036
2037       switch (**pp)
2038         {
2039         case '0':
2040           visibility = DEBUG_VISIBILITY_PRIVATE;
2041           break;
2042         case '1':
2043           visibility = DEBUG_VISIBILITY_PROTECTED;
2044           break;
2045         case '2':
2046           visibility = DEBUG_VISIBILITY_PUBLIC;
2047           break;
2048         default:
2049           warn_stab (orig, "unknown visibility character for baseclass");
2050           visibility = DEBUG_VISIBILITY_PUBLIC;
2051           break;
2052         }
2053       ++*pp;
2054
2055       /* The remaining value is the bit offset of the portion of the
2056          object corresponding to this baseclass.  Always zero in the
2057          absence of multiple inheritance.  */
2058       bitpos = parse_number (pp, (boolean *) NULL);
2059       if (**pp != ',')
2060         {
2061           bad_stab (orig);
2062           return false;
2063         }
2064       ++*pp;
2065
2066       type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2067                               (debug_type **) NULL);
2068       if (type == DEBUG_TYPE_NULL)
2069         return false;
2070
2071       classes[i] = debug_make_baseclass (dhandle, type, bitpos, virtual,
2072                                          visibility);
2073       if (classes[i] == DEBUG_BASECLASS_NULL)
2074         return false;
2075
2076       if (**pp != ';')
2077         return false;
2078       ++*pp;
2079     }
2080
2081   classes[i] = DEBUG_BASECLASS_NULL;
2082
2083   *retp = classes;
2084
2085   return true;
2086 }
2087
2088 /* Read struct or class data fields.  They have the form:
2089
2090         NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2091
2092    At the end, we see a semicolon instead of a field.
2093
2094    In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2095    a static field.
2096
2097    The optional VISIBILITY is one of:
2098
2099         '/0'    (VISIBILITY_PRIVATE)
2100         '/1'    (VISIBILITY_PROTECTED)
2101         '/2'    (VISIBILITY_PUBLIC)
2102         '/9'    (VISIBILITY_IGNORE)
2103
2104    or nothing, for C style fields with public visibility.
2105
2106    Returns 1 for success, 0 for failure.  */
2107
2108 static boolean
2109 parse_stab_struct_fields (dhandle, info, pp, retp, staticsp)
2110      PTR dhandle;
2111      struct stab_handle *info;
2112      const char **pp;
2113      debug_field **retp;
2114      boolean *staticsp;
2115 {
2116   const char *orig;
2117   const char *p;
2118   debug_field *fields;
2119   unsigned int c;
2120   unsigned int alloc;
2121
2122   *retp = NULL;
2123   *staticsp = false;
2124
2125   orig = *pp;
2126
2127   c = 0;
2128   alloc = 10;
2129   fields = (debug_field *) xmalloc (alloc * sizeof *fields);
2130   while (**pp != ';')
2131     {
2132       /* FIXME: gdb checks os9k_stabs here.  */
2133
2134       p = *pp;
2135
2136       /* Add 1 to c to leave room for NULL pointer at end.  */
2137       if (c + 1 >= alloc)
2138         {
2139           alloc += 10;
2140           fields = ((debug_field *)
2141                     xrealloc ((PTR) fields, alloc * sizeof *fields));
2142         }
2143
2144       /* If it starts with CPLUS_MARKER it is a special abbreviation,
2145          unless the CPLUS_MARKER is followed by an underscore, in
2146          which case it is just the name of an anonymous type, which we
2147          should handle like any other type name.  We accept either '$'
2148          or '.', because a field name can never contain one of these
2149          characters except as a CPLUS_MARKER.  */
2150
2151       if ((*p == '$' || *p == '.') && p[1] != '_')
2152         {
2153           ++*pp;
2154           if (! parse_stab_cpp_abbrev (dhandle, info, pp, fields + c))
2155             return false;
2156           ++c;
2157           continue;
2158         }
2159
2160       /* Look for the ':' that separates the field name from the field
2161          values.  Data members are delimited by a single ':', while member
2162          functions are delimited by a pair of ':'s.  When we hit the member
2163          functions (if any), terminate scan loop and return. */
2164
2165       p = strchr (p, ':');
2166       if (p == NULL)
2167         {
2168           bad_stab (orig);
2169           return false;
2170         }
2171
2172       if (p[1] == ':')
2173         break;
2174
2175       if (! parse_stab_one_struct_field (dhandle, info, pp, p, fields + c,
2176                                          staticsp))
2177         return false;
2178
2179       ++c;
2180     }
2181
2182   fields[c] = DEBUG_FIELD_NULL;
2183
2184   *retp = fields;
2185
2186   return true;
2187 }
2188
2189 /* Special GNU C++ name.  */
2190
2191 static boolean
2192 parse_stab_cpp_abbrev (dhandle, info, pp, retp)
2193      PTR dhandle;
2194      struct stab_handle *info;
2195      const char **pp;
2196      debug_field *retp;
2197 {
2198   const char *orig;
2199   int cpp_abbrev;
2200   debug_type context;
2201   const char *name;
2202   const char *typename;
2203   debug_type type;
2204   bfd_vma bitpos;
2205
2206   *retp = DEBUG_FIELD_NULL;
2207
2208   orig = *pp;
2209
2210   if (**pp != 'v')
2211     {
2212       bad_stab (*pp);
2213       return false;
2214     }
2215   ++*pp;
2216
2217   cpp_abbrev = **pp;
2218   ++*pp;
2219
2220   /* At this point, *pp points to something like "22:23=*22...", where
2221      the type number before the ':' is the "context" and everything
2222      after is a regular type definition.  Lookup the type, find it's
2223      name, and construct the field name.  */
2224
2225   context = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2226                              (debug_type **) NULL);
2227   if (context == DEBUG_TYPE_NULL)
2228     return false;
2229
2230   switch (cpp_abbrev)
2231     {
2232     case 'f':
2233       /* $vf -- a virtual function table pointer.  */
2234       name = "_vptr$";
2235       break;
2236     case 'b':
2237       /* $vb -- a virtual bsomethingorother */
2238       typename = debug_get_type_name (dhandle, context);
2239       if (typename == NULL)
2240         {
2241           warn_stab (orig, "unnamed $vb type");
2242           typename = "FOO";
2243         }
2244       name = concat ("_vb$", typename, (const char *) NULL);
2245       break;
2246     default:
2247       warn_stab (orig, "unrecognized C++ abbreviation");
2248       name = "INVALID_CPLUSPLUS_ABBREV";
2249       break;
2250     }
2251
2252   if (**pp != ':')
2253     {
2254       bad_stab (orig);
2255       return false;
2256     }
2257   ++*pp;
2258
2259   type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2260                           (debug_type **) NULL);
2261   if (**pp != ',')
2262     {
2263       bad_stab (orig);
2264       return false;
2265     }
2266   ++*pp;
2267
2268   bitpos = parse_number (pp, (boolean *) NULL);
2269   if (**pp != ';')
2270     {
2271       bad_stab (orig);
2272       return false;
2273     }
2274   ++*pp;
2275
2276   *retp = debug_make_field (dhandle, name, type, bitpos, 0,
2277                             DEBUG_VISIBILITY_PRIVATE);
2278   if (*retp == DEBUG_FIELD_NULL)
2279     return false;
2280
2281   return true;
2282 }
2283
2284 /* Parse a single field in a struct or union.  */
2285
2286 static boolean
2287 parse_stab_one_struct_field (dhandle, info, pp, p, retp, staticsp)
2288      PTR dhandle;
2289      struct stab_handle *info;
2290      const char **pp;
2291      const char *p;
2292      debug_field *retp;
2293      boolean *staticsp;
2294 {
2295   const char *orig;
2296   char *name;
2297   enum debug_visibility visibility;
2298   debug_type type;
2299   bfd_vma bitpos;
2300   bfd_vma bitsize;
2301
2302   orig = *pp;
2303
2304   /* FIXME: gdb checks ARM_DEMANGLING here.  */
2305
2306   name = savestring (*pp, p - *pp);
2307
2308   *pp = p + 1;
2309
2310   if (**pp != '/')
2311     visibility = DEBUG_VISIBILITY_PUBLIC;
2312   else
2313     {
2314       ++*pp;
2315       switch (**pp)
2316         {
2317         case '0':
2318           visibility = DEBUG_VISIBILITY_PRIVATE;
2319           break;
2320         case '1':
2321           visibility = DEBUG_VISIBILITY_PROTECTED;
2322           break;
2323         case '2':
2324           visibility = DEBUG_VISIBILITY_PUBLIC;
2325           break;
2326         default:
2327           warn_stab (orig, "unknown visibility character for field");
2328           visibility = DEBUG_VISIBILITY_PUBLIC;
2329           break;
2330         }
2331       ++*pp;
2332     }
2333
2334   type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2335                           (debug_type **) NULL);
2336   if (type == DEBUG_TYPE_NULL)
2337     return false;
2338
2339   if (**pp == ':')
2340     {
2341       char *varname;
2342
2343       /* This is a static class member.  */
2344       ++*pp;
2345       p = strchr (*pp, ';');
2346       if (p == NULL)
2347         {
2348           bad_stab (orig);
2349           return false;
2350         }
2351
2352       varname = savestring (*pp, p - *pp);
2353
2354       *pp = p + 1;
2355
2356       *retp = debug_make_static_member (dhandle, name, type, varname,
2357                                         visibility);
2358       *staticsp = true;
2359
2360       return true;
2361     }
2362
2363   if (**pp != ',')
2364     {
2365       bad_stab (orig);
2366       return false;
2367     }
2368   ++*pp;
2369
2370   bitpos = parse_number (pp, (boolean *) NULL);
2371   if (**pp != ',')
2372     {
2373       bad_stab (orig);
2374       return false;
2375     }
2376   ++*pp;
2377
2378   bitsize = parse_number (pp, (boolean *) NULL);
2379   if (**pp != ';')
2380     {
2381       bad_stab (orig);
2382       return false;
2383     }
2384   ++*pp;
2385
2386   if (bitpos == 0 && bitsize == 0)
2387     {
2388       /* This can happen in two cases: (1) at least for gcc 2.4.5 or
2389          so, it is a field which has been optimized out.  The correct
2390          stab for this case is to use VISIBILITY_IGNORE, but that is a
2391          recent invention.  (2) It is a 0-size array.  For example
2392          union { int num; char str[0]; } foo.  Printing "<no value>"
2393          for str in "p foo" is OK, since foo.str (and thus foo.str[3])
2394          will continue to work, and a 0-size array as a whole doesn't
2395          have any contents to print.
2396
2397          I suspect this probably could also happen with gcc -gstabs
2398          (not -gstabs+) for static fields, and perhaps other C++
2399          extensions.  Hopefully few people use -gstabs with gdb, since
2400          it is intended for dbx compatibility.  */
2401       visibility = DEBUG_VISIBILITY_IGNORE;
2402     }
2403
2404   /* FIXME: gdb does some stuff here to mark fields as unpacked.  */
2405
2406   *retp = debug_make_field (dhandle, name, type, bitpos, bitsize, visibility);
2407
2408   return true;
2409 }
2410
2411 /* Read member function stabs info for C++ classes.  The form of each member
2412    function data is:
2413
2414         NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2415
2416    An example with two member functions is:
2417
2418         afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2419
2420    For the case of overloaded operators, the format is op$::*.funcs, where
2421    $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2422    name (such as `+=') and `.' marks the end of the operator name.  */
2423
2424 static boolean
2425 parse_stab_members (dhandle, info, tagname, pp, typenums, retp)
2426      PTR dhandle;
2427      struct stab_handle *info;
2428      const char *tagname;
2429      const char **pp;
2430      const int *typenums;
2431      debug_method **retp;
2432 {
2433   const char *orig;
2434   debug_method *methods;
2435   unsigned int c;
2436   unsigned int alloc;
2437
2438   *retp = NULL;
2439
2440   orig = *pp;
2441
2442   alloc = 0;
2443   methods = NULL;
2444   c = 0;
2445
2446   while (**pp != ';')
2447     {
2448       const char *p;
2449       char *name;
2450       debug_method_variant *variants;
2451       unsigned int cvars;
2452       unsigned int allocvars;
2453       debug_type look_ahead_type;
2454
2455       p = strchr (*pp, ':');
2456       if (p == NULL || p[1] != ':')
2457         break;
2458
2459       /* FIXME: Some systems use something other than '$' here.  */
2460       if ((*pp)[0] != 'o' || (*pp)[1] != 'p' || (*pp)[2] != '$')
2461         {
2462           name = savestring (*pp, p - *pp);
2463           *pp = p + 2;
2464         }
2465       else
2466         {
2467           /* This is a completely wierd case.  In order to stuff in the
2468              names that might contain colons (the usual name delimiter),
2469              Mike Tiemann defined a different name format which is
2470              signalled if the identifier is "op$".  In that case, the
2471              format is "op$::XXXX." where XXXX is the name.  This is
2472              used for names like "+" or "=".  YUUUUUUUK!  FIXME!  */
2473           *pp = p + 2;
2474           for (p = *pp; *p != '.' && *p != '\0'; p++)
2475             ;
2476           if (*p != '.')
2477             {
2478               bad_stab (orig);
2479               return false;
2480             }
2481           name = savestring (*pp, p - *pp);
2482           *pp = p + 1;
2483         }
2484
2485       allocvars = 10;
2486       variants = ((debug_method_variant *)
2487                   xmalloc (allocvars * sizeof *variants));
2488       cvars = 0;
2489
2490       look_ahead_type = DEBUG_TYPE_NULL;
2491
2492       do
2493         {
2494           debug_type type;
2495           char *argtypes;
2496           enum debug_visibility visibility;
2497           boolean constp, volatilep, staticp;
2498           bfd_vma voffset;
2499           debug_type context;
2500           const char *physname;
2501           boolean varargs;
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, &varargs) != 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   boolean varargs;
2763
2764   /* Constructors are sometimes handled specially.  */
2765   is_full_physname_constructor = ((argtypes[0] == '_'
2766                                    && argtypes[1] == '_'
2767                                    && (isdigit ((unsigned char) argtypes[2])
2768                                        || argtypes[2] == 'Q'
2769                                        || argtypes[2] == 't'))
2770                                   || strncmp (argtypes, "__ct", 4) == 0);
2771
2772   is_constructor = (is_full_physname_constructor
2773                     || (tagname != NULL
2774                         && strcmp (fieldname, tagname) == 0));
2775   is_destructor = ((argtypes[0] == '_'
2776                     && (argtypes[1] == '$' || argtypes[1] == '.')
2777                     && argtypes[2] == '_')
2778                    || strncmp (argtypes, "__dt", 4) == 0);
2779
2780   if (is_destructor || is_full_physname_constructor)
2781     *pphysname = argtypes;
2782   else
2783     {
2784       unsigned int len;
2785       const char *const_prefix;
2786       const char *volatile_prefix;
2787       char buf[20];
2788       unsigned int mangled_name_len;
2789       char *physname;
2790
2791       len = tagname == NULL ? 0 : strlen (tagname);
2792       const_prefix = constp ? "C" : "";
2793       volatile_prefix = volatilep ? "V" : "";
2794
2795       if (len == 0)
2796         sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2797       else if (tagname != NULL && strchr (tagname, '<') != NULL)
2798         {
2799           /* Template methods are fully mangled.  */
2800           sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
2801           tagname = NULL;
2802           len = 0;
2803         }
2804       else
2805         sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
2806
2807       mangled_name_len = ((is_constructor ? 0 : strlen (fieldname))
2808                           + strlen (buf)
2809                           + len
2810                           + strlen (argtypes)
2811                           + 1);
2812
2813       if (fieldname[0] == 'o'
2814           && fieldname[1] == 'p'
2815           && (fieldname[2] == '$' || fieldname[2] == '.'))
2816         {
2817           const char *opname;
2818
2819           opname = cplus_mangle_opname (fieldname + 3, 0);
2820           if (opname == NULL)
2821             {
2822               fprintf (stderr, "No mangling for \"%s\"\n", fieldname);
2823               return DEBUG_TYPE_NULL;
2824             }
2825           mangled_name_len += strlen (opname);
2826           physname = (char *) xmalloc (mangled_name_len);
2827           strncpy (physname, fieldname, 3);
2828           strcpy (physname + 3, opname);
2829         }
2830       else
2831         {
2832           physname = (char *) xmalloc (mangled_name_len);
2833           if (is_constructor)
2834             physname[0] = '\0';
2835           else
2836             strcpy (physname, fieldname);
2837         }
2838
2839       strcat (physname, buf);
2840       if (tagname != NULL)
2841         strcat (physname, tagname);
2842       strcat (physname, argtypes);
2843
2844       *pphysname = physname;
2845     }
2846
2847   if (*argtypes == '\0')
2848     {
2849       args = (debug_type *) xmalloc (sizeof *args);
2850       *args = NULL;
2851       return debug_make_method_type (dhandle, return_type, class_type, args,
2852                                      false);
2853     }
2854
2855   args = stab_demangle_argtypes (dhandle, info, *pphysname, &varargs);
2856   if (args == NULL)
2857     return DEBUG_TYPE_NULL;
2858
2859   return debug_make_method_type (dhandle, return_type, class_type, args,
2860                                  varargs);
2861 }
2862
2863 /* The tail end of stabs for C++ classes that contain a virtual function
2864    pointer contains a tilde, a %, and a type number.
2865    The type number refers to the base class (possibly this class itself) which
2866    contains the vtable pointer for the current class.
2867
2868    This function is called when we have parsed all the method declarations,
2869    so we can look for the vptr base class info.  */
2870
2871 static boolean
2872 parse_stab_tilde_field (dhandle, info, pp, typenums, retvptrbase, retownvptr)
2873      PTR dhandle;
2874      struct stab_handle *info;
2875      const char **pp;
2876      const int *typenums;
2877      debug_type *retvptrbase;
2878      boolean *retownvptr;
2879 {
2880   const char *orig;
2881   const char *hold;
2882   int vtypenums[2];
2883
2884   *retvptrbase = DEBUG_TYPE_NULL;
2885   *retownvptr = false;
2886
2887   orig = *pp;
2888
2889   /* If we are positioned at a ';', then skip it. */
2890   if (**pp == ';')
2891     ++*pp;
2892
2893   if (**pp != '~')
2894     return true;
2895
2896   ++*pp;
2897
2898   if (**pp == '=' || **pp == '+' || **pp == '-')
2899     {
2900       /* Obsolete flags that used to indicate the presence of
2901          constructors and/or destructors. */
2902       ++*pp;
2903     }
2904
2905   if (**pp != '%')
2906     return true;
2907
2908   ++*pp;
2909
2910   hold = *pp;
2911
2912   /* The next number is the type number of the base class (possibly
2913      our own class) which supplies the vtable for this class.  */
2914   if (! parse_stab_type_number (pp, vtypenums))
2915     return false;
2916
2917   if (vtypenums[0] == typenums[0]
2918       && vtypenums[1] == typenums[1])
2919     *retownvptr = true;
2920   else
2921     {
2922       debug_type vtype;
2923       const char *p;
2924
2925       *pp = hold;
2926
2927       vtype = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2928                                (debug_type **) NULL);
2929       for (p = *pp; *p != ';' && *p != '\0'; p++)
2930         ;
2931       if (*p != ';')
2932         {
2933           bad_stab (orig);
2934           return false;
2935         }
2936
2937       *retvptrbase = vtype;
2938
2939       *pp = p + 1;
2940     }
2941
2942   return true;    
2943 }
2944
2945 /* Read a definition of an array type.  */
2946
2947 static debug_type
2948 parse_stab_array_type (dhandle, info, pp, stringp)
2949      PTR dhandle;
2950      struct stab_handle *info;
2951      const char **pp;
2952      boolean stringp;
2953 {
2954   const char *orig;
2955   debug_type index_type;
2956   boolean adjustable;
2957   bfd_signed_vma lower, upper;
2958   debug_type element_type;
2959
2960   /* Format of an array type:
2961      "ar<index type>;lower;upper;<array_contents_type>".
2962      OS9000: "arlower,upper;<array_contents_type>".
2963
2964      Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
2965      for these, produce a type like float[][].  */
2966
2967   orig = *pp;
2968
2969   /* FIXME: gdb checks os9k_stabs here.  */
2970
2971   index_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
2972                                 (debug_type **) NULL);
2973   if (**pp != ';')
2974     {
2975       bad_stab (orig);
2976       return DEBUG_TYPE_NULL;
2977     }
2978   ++*pp;
2979
2980   adjustable = false;
2981
2982   if (! isdigit ((unsigned char) **pp) && **pp != '-')
2983     {
2984       ++*pp;
2985       adjustable = true;
2986     }
2987
2988   lower = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
2989   if (**pp != ';')
2990     {
2991       bad_stab (orig);
2992       return false;
2993     }
2994   ++*pp;
2995
2996   if (! isdigit ((unsigned char) **pp) && **pp != '-')
2997     {
2998       ++*pp;
2999       adjustable = true;
3000     }
3001
3002   upper = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
3003   if (**pp != ';')
3004     {
3005       bad_stab (orig);
3006       return false;
3007     }
3008   ++*pp;
3009
3010   element_type = parse_stab_type (dhandle, info, (const char *) NULL, pp,
3011                                   (debug_type **) NULL);
3012   if (element_type == DEBUG_TYPE_NULL)
3013     return false;
3014
3015   if (adjustable)
3016     {
3017       lower = 0;
3018       upper = -1;
3019     }
3020
3021   return debug_make_array_type (dhandle, element_type, index_type, lower,
3022                                 upper, stringp);
3023 }
3024
3025 /* Keep a stack of N_BINCL include files.  */
3026
3027 struct bincl_file
3028 {
3029   struct bincl_file *next;
3030   const char *name;
3031 };
3032
3033 /* Start a new N_BINCL file, pushing it onto the stack.  */
3034
3035 static void
3036 push_bincl (info, name)
3037      struct stab_handle *info;
3038      const char *name;
3039 {
3040   struct bincl_file *n;
3041
3042   n = (struct bincl_file *) xmalloc (sizeof *n);
3043   n->next = info->bincl_stack;
3044   n->name = name;
3045   info->bincl_stack = n;
3046
3047   ++info->files;
3048   info->file_types = ((struct stab_types **)
3049                       xrealloc ((PTR) info->file_types,
3050                                 (info->files
3051                                  * sizeof *info->file_types)));
3052   info->file_types[info->files - 1] = NULL;
3053 }
3054
3055 /* Finish an N_BINCL file, at an N_EINCL, popping the name off the
3056    stack.  */
3057
3058 static const char *
3059 pop_bincl (info)
3060      struct stab_handle *info;
3061 {
3062   struct bincl_file *o;
3063
3064   o = info->bincl_stack;
3065   if (o == NULL)
3066     return info->main_filename;
3067   info->bincl_stack = o->next;
3068   free (o);
3069   if (info->bincl_stack == NULL)
3070     return info->main_filename;
3071   return info->bincl_stack->name;
3072 }
3073
3074 /* Handle a variable definition.  gcc emits variable definitions for a
3075    block before the N_LBRAC, so we must hold onto them until we see
3076    it.  The SunPRO compiler emits variable definitions after the
3077    N_LBRAC, so we can call debug_record_variable immediately.  */
3078
3079 static boolean
3080 stab_record_variable (dhandle, info, name, type, kind, val)
3081      PTR dhandle;
3082      struct stab_handle *info;
3083      const char *name;
3084      debug_type type;
3085      enum debug_var_kind kind;
3086      bfd_vma val;
3087 {
3088   struct stab_pending_var *v;
3089
3090   if ((kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
3091       || ! info->within_function
3092       || (info->gcc_compiled == 0 && info->n_opt_found))
3093     return debug_record_variable (dhandle, name, type, kind, val);
3094
3095   v = (struct stab_pending_var *) xmalloc (sizeof *v);
3096   memset (v, 0, sizeof *v);
3097
3098   v->next = info->pending;
3099   v->name = name;
3100   v->type = type;
3101   v->kind = kind;
3102   v->val = val;
3103   info->pending = v;
3104
3105   return true;
3106 }
3107
3108 /* Emit pending variable definitions.  This is called after we see the
3109    N_LBRAC that starts the block.  */
3110
3111 static boolean
3112 stab_emit_pending_vars (dhandle, info)
3113      PTR dhandle;
3114      struct stab_handle *info;
3115 {
3116   struct stab_pending_var *v;
3117
3118   v = info->pending;
3119   while (v != NULL)
3120     {
3121       struct stab_pending_var *next;
3122
3123       if (! debug_record_variable (dhandle, v->name, v->type, v->kind, v->val))
3124         return false;
3125
3126       next = v->next;
3127       free (v);
3128       v = next;
3129     }
3130
3131   info->pending = NULL;
3132
3133   return true;
3134 }
3135
3136 /* Find the slot for a type in the database.  */
3137
3138 static debug_type *
3139 stab_find_slot (info, typenums)
3140      struct stab_handle *info;
3141      const int *typenums;
3142 {
3143   int filenum;
3144   int index;
3145   struct stab_types **ps;
3146
3147   filenum = typenums[0];
3148   index = typenums[1];
3149
3150   if (filenum < 0 || (unsigned int) filenum >= info->files)
3151     {
3152       fprintf (stderr, "Type file number %d out of range\n", filenum);
3153       return NULL;
3154     }
3155   if (index < 0)
3156     {
3157       fprintf (stderr, "Type index number %d out of range\n", index);
3158       return NULL;
3159     }
3160
3161   ps = info->file_types + filenum;
3162
3163   while (index >= STAB_TYPES_SLOTS)
3164     {
3165       if (*ps == NULL)
3166         {
3167           *ps = (struct stab_types *) xmalloc (sizeof **ps);
3168           memset (*ps, 0, sizeof **ps);
3169         }
3170       ps = &(*ps)->next;
3171       index -= STAB_TYPES_SLOTS;
3172     }
3173   if (*ps == NULL)
3174     {
3175       *ps = (struct stab_types *) xmalloc (sizeof **ps);
3176       memset (*ps, 0, sizeof **ps);
3177     }
3178
3179   return (*ps)->types + index;
3180 }
3181
3182 /* Find a type given a type number.  If the type has not been
3183    allocated yet, create an indirect type.  */
3184
3185 static debug_type
3186 stab_find_type (dhandle, info, typenums)
3187      PTR dhandle;
3188      struct stab_handle *info;
3189      const int *typenums;
3190 {
3191   debug_type *slot;
3192
3193   if (typenums[0] == 0 && typenums[1] < 0)
3194     {
3195       /* A negative type number indicates an XCOFF builtin type.  */
3196       return stab_xcoff_builtin_type (dhandle, info, typenums[1]);
3197     }
3198
3199   slot = stab_find_slot (info, typenums);
3200   if (slot == NULL)
3201     return DEBUG_TYPE_NULL;
3202
3203   if (*slot == DEBUG_TYPE_NULL)
3204     return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
3205
3206   return *slot;
3207 }
3208
3209 /* Record that a given type number refers to a given type.  */
3210
3211 static boolean
3212 stab_record_type (dhandle, info, typenums, type)
3213      PTR dhandle;
3214      struct stab_handle *info;
3215      const int *typenums;
3216      debug_type type;
3217 {
3218   debug_type *slot;
3219
3220   slot = stab_find_slot (info, typenums);
3221   if (slot == NULL)
3222     return false;
3223
3224   /* gdb appears to ignore type redefinitions, so we do as well.  */
3225
3226   *slot = type;
3227
3228   return true;
3229 }
3230
3231 /* Return an XCOFF builtin type.  */
3232
3233 static debug_type
3234 stab_xcoff_builtin_type (dhandle, info, typenum)
3235      PTR dhandle;
3236      struct stab_handle *info;
3237      int typenum;
3238 {
3239   debug_type rettype;
3240   const char *name;
3241
3242   if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
3243     {
3244       fprintf (stderr, "Unrecognized XCOFF type %d\n", typenum);
3245       return DEBUG_TYPE_NULL;
3246     }
3247   if (info->xcoff_types[-typenum] != NULL)
3248     return info->xcoff_types[-typenum];
3249
3250   switch (-typenum)
3251     {
3252     case 1:
3253       /* The size of this and all the other types are fixed, defined
3254          by the debugging format.  */
3255       name = "int";
3256       rettype = debug_make_int_type (dhandle, 4, false);
3257       break;
3258     case 2:
3259       name = "char";
3260       rettype = debug_make_int_type (dhandle, 1, false);
3261       break;
3262     case 3:
3263       name = "short";
3264       rettype = debug_make_int_type (dhandle, 2, false);
3265       break;
3266     case 4:
3267       name = "long";
3268       rettype = debug_make_int_type (dhandle, 4, false);
3269       break;
3270     case 5:
3271       name = "unsigned char";
3272       rettype = debug_make_int_type (dhandle, 1, true);
3273       break;
3274     case 6:
3275       name = "signed char";
3276       rettype = debug_make_int_type (dhandle, 1, false);
3277       break;
3278     case 7:
3279       name = "unsigned short";
3280       rettype = debug_make_int_type (dhandle, 2, true);
3281       break;
3282     case 8:
3283       name = "unsigned int";
3284       rettype = debug_make_int_type (dhandle, 4, true);
3285       break;
3286     case 9:
3287       name = "unsigned";
3288       rettype = debug_make_int_type (dhandle, 4, true);
3289     case 10:
3290       name = "unsigned long";
3291       rettype = debug_make_int_type (dhandle, 4, true);
3292       break;
3293     case 11:
3294       name = "void";
3295       rettype = debug_make_void_type (dhandle);
3296       break;
3297     case 12:
3298       /* IEEE single precision (32 bit).  */
3299       name = "float";
3300       rettype = debug_make_float_type (dhandle, 4);
3301       break;
3302     case 13:
3303       /* IEEE double precision (64 bit).  */
3304       name = "double";
3305       rettype = debug_make_float_type (dhandle, 8);
3306       break;
3307     case 14:
3308       /* This is an IEEE double on the RS/6000, and different machines
3309          with different sizes for "long double" should use different
3310          negative type numbers.  See stabs.texinfo.  */
3311       name = "long double";
3312       rettype = debug_make_float_type (dhandle, 8);
3313       break;
3314     case 15:
3315       name = "integer";
3316       rettype = debug_make_int_type (dhandle, 4, false);
3317       break;
3318     case 16:
3319       name = "boolean";
3320       rettype = debug_make_bool_type (dhandle, 4);
3321       break;
3322     case 17:
3323       name = "short real";
3324       rettype = debug_make_float_type (dhandle, 4);
3325       break;
3326     case 18:
3327       name = "real";
3328       rettype = debug_make_float_type (dhandle, 8);
3329       break;
3330     case 19:
3331       /* FIXME */
3332       name = "stringptr";
3333       rettype = NULL;
3334       break;
3335     case 20:
3336       /* FIXME */
3337       name = "character";
3338       rettype = debug_make_int_type (dhandle, 1, true);
3339       break;
3340     case 21:
3341       name = "logical*1";
3342       rettype = debug_make_bool_type (dhandle, 1);
3343       break;
3344     case 22:
3345       name = "logical*2";
3346       rettype = debug_make_bool_type (dhandle, 2);
3347       break;
3348     case 23:
3349       name = "logical*4";
3350       rettype = debug_make_bool_type (dhandle, 4);
3351       break;
3352     case 24:
3353       name = "logical";
3354       rettype = debug_make_bool_type (dhandle, 4);
3355       break;
3356     case 25:
3357       /* Complex type consisting of two IEEE single precision values.  */
3358       name = "complex";
3359       rettype = debug_make_complex_type (dhandle, 8);
3360       break;
3361     case 26:
3362       /* Complex type consisting of two IEEE double precision values.  */
3363       name = "double complex";
3364       rettype = debug_make_complex_type (dhandle, 16);
3365       break;
3366     case 27:
3367       name = "integer*1";
3368       rettype = debug_make_int_type (dhandle, 1, false);
3369       break;
3370     case 28:
3371       name = "integer*2";
3372       rettype = debug_make_int_type (dhandle, 2, false);
3373       break;
3374     case 29:
3375       name = "integer*4";
3376       rettype = debug_make_int_type (dhandle, 4, false);
3377       break;
3378     case 30:
3379       /* FIXME */
3380       name = "wchar";
3381       rettype = debug_make_int_type (dhandle, 2, false);
3382       break;
3383     case 31:
3384       name = "long long";
3385       rettype = debug_make_int_type (dhandle, 8, false);
3386       break;
3387     case 32:
3388       name = "unsigned long long";
3389       rettype = debug_make_int_type (dhandle, 8, true);
3390       break;
3391     case 33:
3392       name = "logical*8";
3393       rettype = debug_make_bool_type (dhandle, 8);
3394       break;
3395     case 34:
3396       name = "integer*8";
3397       rettype = debug_make_int_type (dhandle, 8, false);
3398       break;
3399     default:
3400       abort ();
3401     }
3402
3403   rettype = debug_name_type (dhandle, name, rettype);
3404
3405   info->xcoff_types[-typenum] = rettype;
3406
3407   return rettype;
3408 }
3409
3410 /* Find or create a tagged type.  */
3411
3412 static debug_type
3413 stab_find_tagged_type (dhandle, info, p, len, kind)
3414      PTR dhandle;
3415      struct stab_handle *info;
3416      const char *p;
3417      int len;
3418      enum debug_type_kind kind;
3419 {
3420   char *name;
3421   debug_type dtype;
3422   struct stab_tag *st;
3423
3424   name = savestring (p, len);
3425
3426   /* We pass DEBUG_KIND_ILLEGAL because we want all tags in the same
3427      namespace.  This is right for C, and I don't know how to handle
3428      other languages.  FIXME.  */
3429   dtype = debug_find_tagged_type (dhandle, name, DEBUG_KIND_ILLEGAL);
3430   if (dtype != DEBUG_TYPE_NULL)
3431     {
3432       free (name);
3433       return dtype;
3434     }
3435
3436   /* We need to allocate an entry on the undefined tag list.  */
3437   for (st = info->tags; st != NULL; st = st->next)
3438     {
3439       if (st->name[0] == name[0]
3440           && strcmp (st->name, name) == 0)
3441         {
3442           if (st->kind == DEBUG_KIND_ILLEGAL)
3443             st->kind = kind;
3444           free (name);
3445           break;
3446         }
3447     }
3448   if (st == NULL)
3449     {
3450       st = (struct stab_tag *) xmalloc (sizeof *st);
3451       memset (st, 0, sizeof *st);
3452
3453       st->next = info->tags;
3454       st->name = name;
3455       st->kind = kind;
3456       st->slot = DEBUG_TYPE_NULL;
3457       st->type = debug_make_indirect_type (dhandle, &st->slot, name);
3458       info->tags = st;
3459     }
3460
3461   return st->type;
3462 }
3463 \f
3464 /* In order to get the correct argument types for a stubbed method, we
3465    need to extract the argument types from a C++ mangled string.
3466    Since the argument types can refer back to the return type, this
3467    means that we must demangle the entire physical name.  In gdb this
3468    is done by calling cplus_demangle and running the results back
3469    through the C++ expression parser.  Since we have no expression
3470    parser, we must duplicate much of the work of cplus_demangle here.
3471
3472    We assume that GNU style demangling is used, since this is only
3473    done for method stubs, and only g++ should output that form of
3474    debugging information.  */
3475
3476 /* This structure is used to hold a pointer to type information which
3477    demangling a string.  */
3478
3479 struct stab_demangle_typestring
3480 {
3481   /* The start of the type.  This is not null terminated.  */
3482   const char *typestring;
3483   /* The length of the type.  */
3484   unsigned int len;
3485 };
3486
3487 /* This structure is used to hold information while demangling a
3488    string.  */
3489
3490 struct stab_demangle_info
3491 {
3492   /* The debugging information handle.  */
3493   PTR dhandle;
3494   /* The stab information handle.  */
3495   struct stab_handle *info;
3496   /* The array of arguments we are building.  */
3497   debug_type *args;
3498   /* Whether the method takes a variable number of arguments.  */
3499   boolean varargs;
3500   /* The array of types we have remembered.  */
3501   struct stab_demangle_typestring *typestrings;
3502   /* The number of typestrings.  */
3503   unsigned int typestring_count;
3504   /* The number of typestring slots we have allocated.  */
3505   unsigned int typestring_alloc;
3506 };
3507
3508 static void stab_bad_demangle PARAMS ((const char *));
3509 static unsigned int stab_demangle_count PARAMS ((const char **));
3510 static boolean stab_demangle_get_count
3511   PARAMS ((const char **, unsigned int *));
3512 static boolean stab_demangle_prefix
3513   PARAMS ((struct stab_demangle_info *, const char **));
3514 static boolean stab_demangle_function_name
3515   PARAMS ((struct stab_demangle_info *, const char **, const char *));
3516 static boolean stab_demangle_signature
3517   PARAMS ((struct stab_demangle_info *, const char **));
3518 static boolean stab_demangle_qualified
3519   PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3520 static boolean stab_demangle_template
3521   PARAMS ((struct stab_demangle_info *, const char **));
3522 static boolean stab_demangle_class
3523   PARAMS ((struct stab_demangle_info *, const char **, const char **));
3524 static boolean stab_demangle_args
3525   PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3526            boolean *));
3527 static boolean stab_demangle_arg
3528   PARAMS ((struct stab_demangle_info *, const char **, debug_type **,
3529            unsigned int *, unsigned int *));
3530 static boolean stab_demangle_type
3531   PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3532 static boolean stab_demangle_fund_type
3533   PARAMS ((struct stab_demangle_info *, const char **, debug_type *));
3534 static boolean stab_demangle_remember_type
3535   PARAMS ((struct stab_demangle_info *, const char *, int));
3536
3537 /* Warn about a bad demangling.  */
3538
3539 static void
3540 stab_bad_demangle (s)
3541      const char *s;
3542 {
3543   fprintf (stderr, "bad mangled name `%s'\n", s);
3544 }
3545
3546 /* Get a count from a stab string.  */
3547
3548 static unsigned int
3549 stab_demangle_count (pp)
3550      const char **pp;
3551 {
3552   unsigned int count;
3553
3554   count = 0;
3555   while (isdigit ((unsigned char) **pp))
3556     {
3557       count *= 10;
3558       count += **pp - '0';
3559       ++*pp;
3560     }
3561   return count;
3562 }
3563
3564 /* Require a count in a string.  The count may be multiple digits, in
3565    which case it must end in an underscore.  */
3566
3567 static boolean
3568 stab_demangle_get_count (pp, pi)
3569      const char **pp;
3570      unsigned int *pi;
3571 {
3572   if (! isdigit ((unsigned char) **pp))
3573     return false;
3574
3575   *pi = **pp - '0';
3576   ++*pp;
3577   if (isdigit ((unsigned char) **pp))
3578     {
3579       unsigned int count;
3580       const char *p;
3581
3582       count = *pi;
3583       p = *pp;
3584       do
3585         {
3586           count *= 10;
3587           count += *p - '0';
3588           ++p;
3589         }
3590       while (isdigit ((unsigned char) *p));
3591       if (*p == '_')
3592         {
3593           *pp = p + 1;
3594           *pi = count;
3595         }
3596     }
3597
3598   return true;
3599 }
3600
3601 /* This function demangles a physical name, returning a NULL
3602    terminated array of argument types.  */
3603
3604 static debug_type *
3605 stab_demangle_argtypes (dhandle, info, physname, pvarargs)
3606      PTR dhandle;
3607      struct stab_handle *info;
3608      const char *physname;
3609      boolean *pvarargs;
3610 {
3611   struct stab_demangle_info minfo;
3612
3613   minfo.dhandle = dhandle;
3614   minfo.info = info;
3615   minfo.args = NULL;
3616   minfo.varargs = false;
3617   minfo.typestring_alloc = 10;
3618   minfo.typestrings = ((struct stab_demangle_typestring *)
3619                        xmalloc (minfo.typestring_alloc
3620                                 * sizeof *minfo.typestrings));
3621   minfo.typestring_count = 0;
3622
3623   /* cplus_demangle checks for special GNU mangled forms, but we can't
3624      see any of them in mangled method argument types.  */
3625
3626   if (! stab_demangle_prefix (&minfo, &physname))
3627     goto error_return;
3628
3629   if (*physname != '\0')
3630     {
3631       if (! stab_demangle_signature (&minfo, &physname))
3632         goto error_return;
3633     }
3634
3635   free (minfo.typestrings);
3636   minfo.typestrings = NULL;
3637
3638   if (minfo.args == NULL)
3639     fprintf (stderr, "no argument types in mangled string\n");
3640
3641   *pvarargs = minfo.varargs;
3642   return minfo.args;
3643
3644  error_return:
3645   if (minfo.typestrings != NULL)
3646     free (minfo.typestrings);
3647   return NULL;
3648 }
3649
3650 /* Demangle the prefix of the mangled name.  */
3651
3652 static boolean
3653 stab_demangle_prefix (minfo, pp)
3654      struct stab_demangle_info *minfo;
3655      const char **pp;
3656 {
3657   const char *scan;
3658   unsigned int i;
3659
3660   /* cplus_demangle checks for global constructors and destructors,
3661      but we can't see them in mangled argument types.  */
3662
3663   /* Look for `__'.  */
3664   scan = *pp;
3665   do
3666     {
3667       scan = strchr (scan, '_');
3668     }
3669   while (scan != NULL && *++scan != '_');
3670
3671   if (scan == NULL)
3672     {
3673       stab_bad_demangle (*pp);
3674       return false;
3675     }
3676
3677   --scan;
3678
3679   /* We found `__'; move ahead to the last contiguous `__' pair.  */
3680   i = strspn (scan, "_");
3681   if (i > 2)
3682     scan += i - 2;
3683
3684   if (scan == *pp
3685       && (isdigit ((unsigned char) scan[2])
3686           || scan[2] == 'Q'
3687           || scan[2] == 't'))
3688     {
3689       /* This is a GNU style constructor name.  */
3690       *pp = scan + 2;
3691       return true;
3692     }
3693   else if (scan == *pp
3694            && ! isdigit ((unsigned char) scan[2])
3695            && scan[2] != 't')
3696     {
3697       /* Look for the `__' that separates the prefix from the
3698          signature.  */
3699       while (*scan == '_')
3700         ++scan;
3701       scan = strstr (scan, "__");
3702       if (scan == NULL || scan[2] == '\0')
3703         {
3704           stab_bad_demangle (*pp);
3705           return false;
3706         }
3707
3708       return stab_demangle_function_name (minfo, pp, scan);
3709     }
3710   else if (scan[2] != '\0')
3711     {
3712       /* The name doesn't start with `__', but it does contain `__'.  */
3713       return stab_demangle_function_name (minfo, pp, scan);
3714     }
3715   else
3716     {
3717       stab_bad_demangle (*pp);
3718       return false;
3719     }
3720   /*NOTREACHED*/
3721 }
3722
3723 /* Demangle a function name prefix.  The scan argument points to the
3724    double underscore which separates the function name from the
3725    signature.  */
3726
3727 static boolean
3728 stab_demangle_function_name (minfo, pp, scan)
3729      struct stab_demangle_info *minfo;
3730      const char **pp;
3731      const char *scan;
3732 {
3733   const char *name;
3734
3735   /* The string from *pp to scan is the name of the function.  We
3736      don't care about the name, since we just looking for argument
3737      types.  However, for conversion operators, the name may include a
3738      type which we must remember in order to handle backreferences.  */
3739
3740   name = *pp;
3741   *pp = scan + 2;
3742
3743   if (*pp - name >= 5
3744            && strncmp (name, "type", 4) == 0
3745            && (name[4] == '$' || name[4] == '.'))
3746     {
3747       const char *tem;
3748
3749       /* This is a type conversion operator.  */
3750       tem = name + 5;
3751       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3752         return false;
3753     }
3754   else if (name[0] == '_'
3755            && name[1] == '_'
3756            && name[2] == 'o'
3757            && name[3] == 'p')
3758     {
3759       const char *tem;
3760
3761       /* This is a type conversion operator.  */
3762       tem = name + 4;
3763       if (! stab_demangle_type (minfo, &tem, (debug_type *) NULL))
3764         return false;
3765     }
3766
3767   return true;
3768 }
3769
3770 /* Demangle the signature.  This is where the argument types are
3771    found.  */
3772
3773 static boolean
3774 stab_demangle_signature (minfo, pp)
3775      struct stab_demangle_info *minfo;
3776      const char **pp;
3777 {
3778   const char *orig;
3779   boolean expect_func, func_done;
3780   const char *hold;
3781
3782   orig = *pp;
3783
3784   expect_func = false;
3785   func_done = false;
3786   hold = NULL;
3787
3788   while (**pp != '\0')
3789     {
3790       switch (**pp)
3791         {
3792         case 'Q':
3793           hold = *pp;
3794           if (! stab_demangle_qualified (minfo, pp, (debug_type *) NULL)
3795               || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
3796             return false;
3797           expect_func = true;
3798           hold = NULL;
3799           break;
3800
3801         case 'S':
3802           /* Static member function.  FIXME: Can this happen?  */
3803           if (hold == NULL)
3804             hold = *pp;
3805           ++*pp;
3806           break;
3807
3808         case 'C':
3809           /* Const member function.  */
3810           if (hold == NULL)
3811             hold = *pp;
3812           ++*pp;
3813           break;
3814
3815         case '0': case '1': case '2': case '3': case '4':
3816         case '5': case '6': case '7': case '8': case '9':
3817           if (hold == NULL)
3818             hold = *pp;
3819           if (! stab_demangle_class (minfo, pp, (const char **) NULL)
3820               || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
3821             return false;
3822           expect_func = true;
3823           hold = NULL;
3824           break;
3825
3826         case 'F':
3827           /* Function.  I don't know if this actually happens with g++
3828              output.  */
3829           hold = NULL;
3830           func_done = true;
3831           ++*pp;
3832           if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
3833             return false;
3834           break;
3835
3836         case 't':
3837           /* Template.  */
3838           if (hold == NULL)
3839             hold = *pp;
3840           if (! stab_demangle_template (minfo, pp)
3841               || ! stab_demangle_remember_type (minfo, hold, *pp - hold))
3842             return false;
3843           hold = NULL;
3844           expect_func = true;
3845           break;
3846
3847         case '_':
3848           /* At the outermost level, we cannot have a return type
3849              specified, so if we run into another '_' at this point we
3850              are dealing with a mangled name that is either bogus, or
3851              has been mangled by some algorithm we don't know how to
3852              deal with.  So just reject the entire demangling.  */
3853           stab_bad_demangle (orig);
3854           return false;
3855
3856         default:
3857           /* Assume we have stumbled onto the first outermost function
3858              argument token, and start processing args.  */
3859           func_done = true;
3860           if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
3861             return false;
3862           break;
3863         }
3864
3865       if (expect_func)
3866         {
3867           func_done = true;
3868           if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
3869             return false;
3870         }
3871     }
3872
3873   if (! func_done)
3874     {
3875       /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
3876          bar__3fooi is 'foo::bar(int)'.  We get here when we find the
3877          first case, and need to ensure that the '(void)' gets added
3878          to the current declp.  */
3879       if (! stab_demangle_args (minfo, pp, &minfo->args, &minfo->varargs))
3880         return false;
3881     }
3882
3883   return true;
3884 }
3885
3886 /* Demangle a qualified name, such as "Q25Outer5Inner" which is the
3887    mangled form of "Outer::Inner".  */
3888
3889 static boolean
3890 stab_demangle_qualified (minfo, pp, ptype)
3891      struct stab_demangle_info *minfo;
3892      const char **pp;
3893      debug_type *ptype;
3894 {
3895   const char *orig;
3896   const char *p;
3897   unsigned int qualifiers;
3898   debug_type context;
3899
3900   orig = *pp;
3901
3902   switch ((*pp)[1])
3903     {
3904     case '_':
3905       /* GNU mangled name with more than 9 classes.  The count is
3906          preceded by an underscore (to distinguish it from the <= 9
3907          case) and followed by an underscore.  */
3908       p = *pp + 2;
3909       if (! isdigit ((unsigned char) *p) || *p == '0')
3910         {
3911           stab_bad_demangle (orig);
3912           return false;
3913         }
3914       qualifiers = atoi (p);
3915       while (isdigit ((unsigned char) *p))
3916         ++p;
3917       if (*p != '_')
3918         {
3919           stab_bad_demangle (orig);
3920           return false;
3921         }
3922       *pp = p + 1;
3923       break;
3924
3925     case '1': case '2': case '3': case '4': case '5':
3926     case '6': case '7': case '8': case '9':
3927       qualifiers = (*pp)[1] - '0';
3928       /* Skip an optional underscore after the count.  */
3929       if ((*pp)[2] == '_')
3930         ++*pp;
3931       *pp += 2;
3932       break;
3933
3934     case '0':
3935     default:
3936       stab_bad_demangle (orig);
3937       return false;
3938     }
3939
3940   context = DEBUG_TYPE_NULL;
3941
3942   /* Pick off the names.  */
3943   while (qualifiers-- > 0)
3944     {
3945       if (**pp == '_')
3946         ++*pp;
3947       if (**pp == 't')
3948         {
3949           /* FIXME: I don't know how to handle the ptype != NULL case
3950              here.  */
3951           if (! stab_demangle_template (minfo, pp))
3952             return false;
3953         }
3954       else
3955         {
3956           unsigned int len;
3957
3958           len = stab_demangle_count (pp);
3959           if (strlen (*pp) < len)
3960             {
3961               stab_bad_demangle (orig);
3962               return false;
3963             }
3964
3965           if (ptype != NULL)
3966             {
3967               const debug_field *fields;
3968
3969               fields = NULL;
3970               if (context != DEBUG_TYPE_NULL)
3971                 fields = debug_get_fields (minfo->dhandle, context);
3972
3973               context = DEBUG_TYPE_NULL;
3974
3975               if (fields != NULL)
3976                 {
3977                   char *name;
3978
3979                   /* Try to find the type by looking through the
3980                      fields of context until we find a field with the
3981                      same type.  This ought to work for a class
3982                      defined within a class, but it won't work for,
3983                      e.g., an enum defined within a class.  stabs does
3984                      not give us enough information to figure out the
3985                      latter case.  */
3986
3987                   name = savestring (*pp, len);
3988
3989                   for (; *fields != DEBUG_FIELD_NULL; fields++)
3990                     {
3991                       debug_type ft;
3992                       const char *dn;
3993
3994                       ft = debug_get_field_type (minfo->dhandle, *fields);
3995                       if (ft == NULL)
3996                         return false;
3997                       dn = debug_get_type_name (minfo->dhandle, ft);
3998                       if (dn != NULL && strcmp (dn, name) == 0)
3999                         {
4000                           context = ft;
4001                           break;
4002                         }
4003                     }
4004
4005                   free (name);
4006                 }
4007
4008               if (context == DEBUG_TYPE_NULL)
4009                 {
4010           /* We have to fall back on finding the type by name.
4011                      If there are more types to come, then this must
4012                      be a class.  Otherwise, it could be anything.  */
4013
4014                   if (qualifiers == 0)
4015                     {
4016                       char *name;
4017
4018                       name = savestring (*pp, len);
4019                       context = debug_find_named_type (minfo->dhandle,
4020                                                        name);
4021                       free (name);
4022                     }
4023
4024                   if (context == DEBUG_TYPE_NULL)
4025                     {
4026                       context = stab_find_tagged_type (minfo->dhandle,
4027                                                        minfo->info,
4028                                                        *pp, len,
4029                                                        (qualifiers == 0
4030                                                         ? DEBUG_KIND_ILLEGAL
4031                                                         : DEBUG_KIND_CLASS));
4032                       if (context == DEBUG_TYPE_NULL)
4033                         return false;
4034                     }
4035                 }
4036             }
4037
4038           *pp += len;
4039         }
4040     }
4041
4042   if (ptype != NULL)
4043     *ptype = context;
4044
4045   return true;
4046 }
4047
4048 /* Demangle a template.  */
4049
4050 static boolean
4051 stab_demangle_template (minfo, pp)
4052      struct stab_demangle_info *minfo;
4053      const char **pp;
4054 {
4055   const char *orig;
4056   unsigned int r, i;
4057
4058   orig = *pp;
4059
4060   ++*pp;
4061
4062   /* Skip the template name.  */
4063   r = stab_demangle_count (pp);
4064   if (r == 0 || strlen (*pp) < r)
4065     {
4066       stab_bad_demangle (orig);
4067       return false;
4068     }
4069   *pp += r;
4070
4071   /* Get the size of the parameter list.  */
4072   if (stab_demangle_get_count (pp, &r) == 0)
4073     {
4074       stab_bad_demangle (orig);
4075       return false;
4076     }
4077
4078   for (i = 0; i < r; i++)
4079     {
4080       if (**pp == 'Z')
4081         {
4082           /* This is a type parameter.  */
4083           ++*pp;
4084           if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4085             return false;
4086         }
4087       else
4088         {
4089           const char *old_p;
4090           boolean pointerp, realp, integralp, charp, boolp;
4091           boolean done;
4092
4093           old_p = *pp;
4094           pointerp = false;
4095           realp = false;
4096           integralp = false;
4097           charp = false;
4098           boolp = false;
4099           done = false;
4100
4101           /* This is a value parameter.  */
4102
4103           if (! stab_demangle_type (minfo, pp, (debug_type *) NULL))
4104             return false;
4105
4106           while (*old_p != '\0' && ! done)
4107             {
4108               switch (*old_p)
4109                 {
4110                 case 'P':
4111                 case 'p':
4112                 case 'R':
4113                   pointerp = true;
4114                   done = true;
4115                   break;
4116                 case 'C':       /* Const.  */
4117                 case 'S':       /* Signed.  */
4118                 case 'U':       /* Unsigned.  */
4119                 case 'V':       /* Volatile.  */
4120                 case 'F':       /* Function.  */
4121                 case 'M':       /* Member function.  */
4122                 case 'O':       /* ??? */
4123                   ++old_p;
4124                   break;
4125                 case 'Q':       /* Qualified name.  */
4126                   integralp = true;
4127                   done = true;
4128                   break;
4129                 case 'T':       /* Remembered type.  */
4130                   abort ();
4131                 case 'v':       /* Void.  */
4132                   abort ();
4133                 case 'x':       /* Long long.  */
4134                 case 'l':       /* Long.  */
4135                 case 'i':       /* Int.  */
4136                 case 's':       /* Short.  */
4137                 case 'w':       /* Wchar_t.  */
4138                   integralp = true;
4139                   done = true;
4140                   break;
4141                 case 'b':       /* Bool.  */
4142                   boolp = true;
4143                   done = true;
4144                   break;
4145                 case 'c':       /* Char.  */
4146                   charp = true;
4147                   done = true;
4148                   break;
4149                 case 'r':       /* Long double.  */
4150                 case 'd':       /* Double.  */
4151                 case 'f':       /* Float.  */
4152                   realp = true;
4153                   done = true;
4154                   break;
4155                 default:
4156                   /* Assume it's a uder defined integral type.  */
4157                   integralp = true;
4158                   done = true;
4159                   break;
4160                 }
4161             }
4162
4163           if (integralp)
4164             {
4165               if (**pp == 'm')
4166                 ++*pp;
4167               while (isdigit ((unsigned char) **pp))
4168                 ++*pp;
4169             }
4170           else if (charp)
4171             {
4172               unsigned int val;
4173
4174               if (**pp == 'm')
4175                 ++*pp;
4176               val = stab_demangle_count (pp);
4177               if (val == 0)
4178                 {
4179                   stab_bad_demangle (orig);
4180                   return false;
4181                 }
4182             }
4183           else if (boolp)
4184             {
4185               unsigned int val;
4186
4187               val = stab_demangle_count (pp);
4188               if (val != 0 && val != 1)
4189                 {
4190                   stab_bad_demangle (orig);
4191                   return false;
4192                 }
4193             }
4194           else if (realp)
4195             {
4196               if (**pp == 'm')
4197                 ++*pp;
4198               while (isdigit ((unsigned char) **pp))
4199                 ++*pp;
4200               if (**pp == '.')
4201                 {
4202                   ++*pp;
4203                   while (isdigit ((unsigned char) **pp))
4204                     ++*pp;
4205                 }
4206               if (**pp == 'e')
4207                 {
4208                   ++*pp;
4209                   while (isdigit ((unsigned char) **pp))
4210                     ++*pp;
4211                 }
4212             }
4213           else if (pointerp)
4214             {
4215               unsigned int len;
4216
4217               if (! stab_demangle_get_count (pp, &len))
4218                 {
4219                   stab_bad_demangle (orig);
4220                   return false;
4221                 }
4222               *pp += len;
4223             }
4224         }
4225     }
4226
4227   return true;
4228 }
4229
4230 /* Demangle a class name.  */
4231
4232 static boolean
4233 stab_demangle_class (minfo, pp, pstart)
4234      struct stab_demangle_info *minfo;
4235      const char **pp;
4236      const char **pstart;
4237 {
4238   const char *orig;
4239   unsigned int n;
4240
4241   orig = *pp;
4242
4243   n = stab_demangle_count (pp);
4244   if (strlen (*pp) < n)
4245     {
4246       stab_bad_demangle (orig);
4247       return false;
4248     }
4249
4250   if (pstart != NULL)
4251     *pstart = *pp;
4252
4253   *pp += n;
4254
4255   return true;
4256 }
4257
4258 /* Demangle function arguments.  If the pargs argument is not NULL, it
4259    is set to a NULL terminated array holding the arguments.  */
4260
4261 static boolean
4262 stab_demangle_args (minfo, pp, pargs, pvarargs)
4263      struct stab_demangle_info *minfo;
4264      const char **pp;
4265      debug_type **pargs;
4266      boolean *pvarargs;
4267 {
4268   const char *orig;
4269   unsigned int alloc, count;
4270
4271   orig = *pp;
4272
4273   alloc = 10;
4274   if (pargs != NULL)
4275     {
4276       *pargs = (debug_type *) xmalloc (alloc * sizeof **pargs);
4277       *pvarargs = false;
4278     }
4279   count = 0;
4280
4281   while (**pp != '_' && **pp != '\0' && **pp != 'e')
4282     {
4283       if (**pp == 'N' || **pp == 'T')
4284         {
4285           char temptype;
4286           unsigned int r, t;
4287
4288           temptype = **pp;
4289           ++*pp;
4290
4291           if (temptype == 'T')
4292             r = 1;
4293           else
4294             {
4295               if (! stab_demangle_get_count (pp, &r))
4296                 {
4297                   stab_bad_demangle (orig);
4298                   return false;
4299                 }
4300             }
4301
4302           if (! stab_demangle_get_count (pp, &t))
4303             {
4304               stab_bad_demangle (orig);
4305               return false;
4306             }
4307
4308           if (t >= minfo->typestring_count)
4309             {
4310               stab_bad_demangle (orig);
4311               return false;
4312             }
4313           while (r-- > 0)
4314             {
4315               const char *tem;
4316
4317               tem = minfo->typestrings[t].typestring;
4318               if (! stab_demangle_arg (minfo, &tem, pargs, &count, &alloc))
4319                 return false;
4320             }
4321         }
4322       else
4323         {
4324           if (! stab_demangle_arg (minfo, pp, pargs, &count, &alloc))
4325             return false;
4326         }
4327     }
4328
4329   if (pargs != NULL)
4330     (*pargs)[count] = DEBUG_TYPE_NULL;
4331
4332   if (**pp == 'e')
4333     {
4334       if (pargs != NULL)
4335         *pvarargs = true;
4336       ++*pp;
4337     }
4338
4339   return true;
4340 }
4341
4342 /* Demangle a single argument.  */
4343
4344 static boolean
4345 stab_demangle_arg (minfo, pp, pargs, pcount, palloc)
4346      struct stab_demangle_info *minfo;
4347      const char **pp;
4348      debug_type **pargs;
4349      unsigned int *pcount;
4350      unsigned int *palloc;
4351 {
4352   const char *start;
4353   debug_type type;
4354
4355   start = *pp;
4356   if (! stab_demangle_type (minfo, pp,
4357                             pargs == NULL ? (debug_type *) NULL : &type)
4358       || ! stab_demangle_remember_type (minfo, start, *pp - start))
4359     return false;
4360
4361   if (pargs != NULL)
4362     {
4363       if (type == DEBUG_TYPE_NULL)
4364         return false;
4365
4366       if (*pcount + 1 >= *palloc)
4367         {
4368           *palloc += 10;
4369           *pargs = ((debug_type *)
4370                     xrealloc (*pargs, *palloc * sizeof **pargs));
4371         }
4372       (*pargs)[*pcount] = type;
4373       ++*pcount;
4374     }
4375
4376   return true;
4377 }
4378
4379 /* Demangle a type.  If the ptype argument is not NULL, *ptype is set
4380    to the newly allocated type.  */
4381
4382 static boolean
4383 stab_demangle_type (minfo, pp, ptype)
4384      struct stab_demangle_info *minfo;
4385      const char **pp;
4386      debug_type *ptype;
4387 {
4388   const char *orig;
4389
4390   orig = *pp;
4391
4392   switch (**pp)
4393     {
4394     case 'P':
4395     case 'p':
4396       /* A pointer type.  */
4397       ++*pp;
4398       if (! stab_demangle_type (minfo, pp, ptype))
4399         return false;
4400       if (ptype != NULL)
4401         *ptype = debug_make_pointer_type (minfo->dhandle, *ptype);
4402       break;
4403
4404     case 'R':
4405       /* A reference type.  */
4406       ++*pp;
4407       if (! stab_demangle_type (minfo, pp, ptype))
4408         return false;
4409       if (ptype != NULL)
4410         *ptype = debug_make_reference_type (minfo->dhandle, *ptype);
4411       break;
4412
4413     case 'A':
4414       /* An array.  */
4415       {
4416         unsigned long high;
4417
4418         ++*pp;
4419         high = 0;
4420         while (**pp != '\0' && **pp != '_')
4421           {
4422             if (! isdigit ((unsigned char) **pp))
4423               {
4424                 stab_bad_demangle (orig);
4425                 return false;
4426               }
4427             high *= 10;
4428             high += **pp - '0';
4429             ++*pp;
4430           }
4431         if (**pp != '_')
4432           {
4433             stab_bad_demangle (orig);
4434             return false;
4435           }
4436         ++*pp;
4437
4438         if (! stab_demangle_type (minfo, pp, ptype))
4439           return false;
4440         if (ptype != NULL)
4441           {
4442             debug_type int_type;
4443
4444             int_type = debug_find_named_type (minfo->dhandle, "int");
4445             if (int_type == NULL)
4446               int_type = debug_make_int_type (minfo->dhandle, 4, false);
4447             *ptype = debug_make_array_type (minfo->dhandle, *ptype, int_type,
4448                                             0, high, false);
4449           }
4450       }
4451       break;
4452
4453     case 'T':
4454       /* A back reference to a remembered type.  */
4455       {
4456         unsigned int i;
4457         const char *p;
4458
4459         ++*pp;
4460         if (! stab_demangle_get_count (pp, &i))
4461           {
4462             stab_bad_demangle (orig);
4463             return false;
4464           }
4465         if (i >= minfo->typestring_count)
4466           {
4467             stab_bad_demangle (orig);
4468             return false;
4469           }
4470         p = minfo->typestrings[i].typestring;
4471         if (! stab_demangle_type (minfo, &p, ptype))
4472           return false;
4473       }
4474       break;
4475
4476     case 'F':
4477       /* A function.  */
4478       {
4479         debug_type *args;
4480         boolean varargs;
4481
4482         ++*pp;
4483         if (! stab_demangle_args (minfo, pp,
4484                                   (ptype == NULL
4485                                    ? (debug_type **) NULL
4486                                    : &args),
4487                                   (ptype == NULL
4488                                    ? (boolean *) NULL
4489                                    : &varargs)))
4490           return false;
4491         if (**pp != '_')
4492           {
4493             /* cplus_demangle will accept a function without a return
4494                type, but I don't know when that will happen, or what
4495                to do if it does.  */
4496             stab_bad_demangle (orig);
4497             return false;
4498           }
4499         ++*pp;
4500         if (! stab_demangle_type (minfo, pp, ptype))
4501           return false;
4502         if (ptype != NULL)
4503           *ptype = debug_make_function_type (minfo->dhandle, *ptype, args,
4504                                              varargs);
4505
4506       }
4507       break;
4508
4509     case 'M':
4510     case 'O':
4511       {
4512         boolean memberp, constp, volatilep;
4513         debug_type *args;
4514         boolean varargs;
4515         unsigned int n;
4516         const char *name;
4517
4518         memberp = **pp == 'M';
4519         constp = false;
4520         volatilep = false;
4521         args = NULL;
4522         varargs = false;
4523
4524         ++*pp;
4525         if (! isdigit ((unsigned char) **pp))
4526           {
4527             stab_bad_demangle (orig);
4528             return false;
4529           }
4530         n = stab_demangle_count (pp);
4531         if (strlen (*pp) < n)
4532           {
4533             stab_bad_demangle (orig);
4534             return false;
4535           }
4536         name = *pp;
4537         *pp += n;
4538
4539         if (memberp)
4540           {
4541             if (**pp == 'C')
4542               {
4543                 constp = true;
4544                 ++*pp;
4545               }
4546             else if (**pp == 'V')
4547               {
4548                 volatilep = true;
4549                 ++*pp;
4550               }
4551             if (**pp != 'F')
4552               {
4553                 stab_bad_demangle (orig);
4554                 return false;
4555               }
4556             ++*pp;
4557             if (! stab_demangle_args (minfo, pp,
4558                                       (ptype == NULL
4559                                        ? (debug_type **) NULL
4560                                        : &args),
4561                                       (ptype == NULL
4562                                        ? (boolean *) NULL
4563                                        : &varargs)))
4564               return false;
4565           }
4566
4567         if (**pp != '_')
4568           {
4569             stab_bad_demangle (orig);
4570             return false;
4571           }
4572         ++*pp;
4573
4574         if (! stab_demangle_type (minfo, pp, ptype))
4575           return false;
4576
4577         if (ptype != NULL)
4578           {
4579             debug_type class_type;
4580
4581             class_type = stab_find_tagged_type (minfo->dhandle, minfo->info,
4582                                                 name, (int) n,
4583                                                 DEBUG_KIND_CLASS);
4584             if (class_type == DEBUG_TYPE_NULL)
4585               return false;
4586
4587             if (! memberp)
4588               *ptype = debug_make_offset_type (minfo->dhandle, class_type,
4589                                                *ptype);
4590             else
4591               {
4592                 /* FIXME: We have no way to record constp or
4593                    volatilep.  */
4594                 *ptype = debug_make_method_type (minfo->dhandle, *ptype,
4595                                                  class_type, args, varargs);
4596               }
4597           }
4598       }
4599       break;
4600
4601     case 'G':
4602       ++*pp;
4603       if (! stab_demangle_type (minfo, pp, ptype))
4604         return false;
4605       break;
4606
4607     case 'C':
4608       ++*pp;
4609       if (! stab_demangle_type (minfo, pp, ptype))
4610         return false;
4611       if (ptype != NULL)
4612         *ptype = debug_make_const_type (minfo->dhandle, *ptype);
4613       break;
4614
4615     case 'Q':
4616       {
4617         const char *hold;
4618
4619         hold = *pp;
4620         if (! stab_demangle_qualified (minfo, pp, ptype))
4621           return false;
4622       }
4623       break;
4624
4625     default:
4626       if (! stab_demangle_fund_type (minfo, pp, ptype))
4627         return false;
4628       break;
4629     }
4630
4631   return true;
4632 }
4633
4634 /* Demangle a fundamental type.  If the ptype argument is not NULL,
4635    *ptype is set to the newly allocated type.  */
4636
4637 static boolean
4638 stab_demangle_fund_type (minfo, pp, ptype)
4639      struct stab_demangle_info *minfo;
4640      const char **pp;
4641      debug_type *ptype;
4642 {
4643   const char *orig;
4644   boolean constp, volatilep, unsignedp, signedp;
4645   boolean done;
4646
4647   orig = *pp;
4648
4649   constp = false;
4650   volatilep = false;
4651   unsignedp = false;
4652   signedp = false;
4653
4654   done = false;
4655   while (! done)
4656     {
4657       switch (**pp)
4658         {
4659         case 'C':
4660           constp = true;
4661           ++*pp;
4662           break;
4663
4664         case 'U':
4665           unsignedp = true;
4666           ++*pp;
4667           break;
4668
4669         case 'S':
4670           signedp = true;
4671           ++*pp;
4672           break;
4673
4674         case 'V':
4675           volatilep = true;
4676           ++*pp;
4677           break;
4678
4679         default:
4680           done = true;
4681           break;
4682         }
4683     }
4684
4685   switch (**pp)
4686     {
4687     case '\0':
4688     case '_':
4689       /* cplus_demangle permits this, but I don't know what it means.  */
4690       stab_bad_demangle (orig);
4691       break;
4692
4693     case 'v': /* void */
4694       if (ptype != NULL)
4695         {
4696           *ptype = debug_find_named_type (minfo->dhandle, "void");
4697           if (*ptype == DEBUG_TYPE_NULL)
4698             *ptype = debug_make_void_type (minfo->dhandle);
4699         }
4700       ++*pp;
4701       break;
4702
4703     case 'x': /* long long */
4704       if (ptype != NULL)
4705         {
4706           *ptype = debug_find_named_type (minfo->dhandle,
4707                                           (unsignedp
4708                                            ? "long long unsigned int"
4709                                            : "long long int"));
4710           if (*ptype == DEBUG_TYPE_NULL)
4711             *ptype = debug_make_int_type (minfo->dhandle, 8, unsignedp);
4712         }
4713       ++*pp;
4714       break;
4715
4716     case 'l': /* long */
4717       if (ptype != NULL)
4718         {
4719           *ptype = debug_find_named_type (minfo->dhandle,
4720                                           (unsignedp
4721                                            ? "long unsigned int"
4722                                            : "long int"));
4723           if (*ptype == DEBUG_TYPE_NULL)
4724             *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
4725         }
4726       ++*pp;
4727       break;
4728
4729     case 'i': /* int */
4730       if (ptype != NULL)
4731         {
4732           *ptype = debug_find_named_type (minfo->dhandle,
4733                                           (unsignedp
4734                                            ? "unsigned int"
4735                                            : "int"));
4736           if (*ptype == DEBUG_TYPE_NULL)
4737             *ptype = debug_make_int_type (minfo->dhandle, 4, unsignedp);
4738         }
4739       ++*pp;
4740       break;
4741
4742     case 's': /* short */
4743       if (ptype != NULL)
4744         {
4745           *ptype = debug_find_named_type (minfo->dhandle,
4746                                           (unsignedp
4747                                            ? "short unsigned int"
4748                                            : "short int"));
4749           if (*ptype == DEBUG_TYPE_NULL)
4750             *ptype = debug_make_int_type (minfo->dhandle, 2, unsignedp);
4751         }
4752       ++*pp;
4753       break;
4754
4755     case 'b': /* bool */
4756       if (ptype != NULL)
4757         {
4758           *ptype = debug_find_named_type (minfo->dhandle, "bool");
4759           if (*ptype == DEBUG_TYPE_NULL)
4760             *ptype = debug_make_bool_type (minfo->dhandle, 4);
4761         }
4762       ++*pp;
4763       break;
4764
4765     case 'c': /* char */
4766       if (ptype != NULL)
4767         {
4768           *ptype = debug_find_named_type (minfo->dhandle,
4769                                           (unsignedp
4770                                            ? "unsigned char"
4771                                            : (signedp
4772                                               ? "signed char"
4773                                               : "char")));
4774           if (*ptype == DEBUG_TYPE_NULL)
4775             *ptype = debug_make_int_type (minfo->dhandle, 1, unsignedp);
4776         }
4777       ++*pp;
4778       break;
4779
4780     case 'w': /* wchar_t */
4781       if (ptype != NULL)
4782         {
4783           *ptype = debug_find_named_type (minfo->dhandle, "__wchar_t");
4784           if (*ptype == DEBUG_TYPE_NULL)
4785             *ptype = debug_make_int_type (minfo->dhandle, 2, true);
4786         }
4787       ++*pp;
4788       break;
4789
4790     case 'r': /* long double */
4791       if (ptype != NULL)
4792         {
4793           *ptype = debug_find_named_type (minfo->dhandle, "long double");
4794           if (*ptype == DEBUG_TYPE_NULL)
4795             *ptype = debug_make_float_type (minfo->dhandle, 8);
4796         }
4797       ++*pp;
4798       break;
4799
4800     case 'd': /* double */
4801       if (ptype != NULL)
4802         {
4803           *ptype = debug_find_named_type (minfo->dhandle, "double");
4804           if (*ptype == DEBUG_TYPE_NULL)
4805             *ptype = debug_make_float_type (minfo->dhandle, 8);
4806         }
4807       ++*pp;
4808       break;
4809
4810     case 'f': /* float */
4811       if (ptype != NULL)
4812         {
4813           *ptype = debug_find_named_type (minfo->dhandle, "float");
4814           if (*ptype == DEBUG_TYPE_NULL)
4815             *ptype = debug_make_float_type (minfo->dhandle, 4);
4816         }
4817       ++*pp;
4818       break;
4819
4820     case 'G':
4821       ++*pp;
4822       if (! isdigit ((unsigned char) **pp))
4823         {
4824           stab_bad_demangle (orig);
4825           return false;
4826         }
4827       /* Fall through.  */
4828     case '0': case '1': case '2': case '3': case '4':
4829     case '5': case '6': case '7': case '8': case '9':
4830       {
4831         const char *hold;
4832
4833         if (! stab_demangle_class (minfo, pp, &hold))
4834           return false;
4835         if (ptype != NULL)
4836           {
4837             char *name;
4838
4839             name = savestring (hold, *pp - hold);
4840             *ptype = debug_find_named_type (minfo->dhandle, name);
4841             if (*ptype == DEBUG_TYPE_NULL)
4842               {
4843                 /* FIXME: It is probably incorrect to assume that
4844                    undefined types are tagged types.  */
4845                 *ptype = stab_find_tagged_type (minfo->dhandle, minfo->info,
4846                                                 hold, *pp - hold,
4847                                                 DEBUG_KIND_ILLEGAL);
4848               }
4849             free (name);
4850           }
4851       }
4852       break;
4853
4854     case 't':
4855       if (! stab_demangle_template (minfo, pp))
4856         return false;
4857       abort ();
4858       break;
4859
4860     default:
4861       stab_bad_demangle (orig);
4862       return false;
4863     }
4864
4865   if (ptype != NULL)
4866     {
4867       if (constp)
4868         *ptype = debug_make_const_type (minfo->dhandle, *ptype);
4869       if (volatilep)
4870         *ptype = debug_make_volatile_type (minfo->dhandle, *ptype);
4871     }
4872
4873   return true;
4874 }
4875
4876 /* Remember a type string in a demangled string.  */
4877
4878 static boolean
4879 stab_demangle_remember_type (minfo, p, len)
4880      struct stab_demangle_info *minfo;
4881      const char *p;
4882      int len;
4883 {
4884   if (minfo->typestring_count >= minfo->typestring_alloc)
4885     {
4886       minfo->typestring_alloc += 10;
4887       minfo->typestrings = ((struct stab_demangle_typestring *)
4888                             xrealloc (minfo->typestrings,
4889                                       (minfo->typestring_alloc
4890                                        * sizeof *minfo->typestrings)));
4891     }
4892
4893   minfo->typestrings[minfo->typestring_count].typestring = p;
4894   minfo->typestrings[minfo->typestring_count].len = (unsigned int) len;
4895   ++minfo->typestring_count;
4896
4897   return true;
4898 }