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