Implement generic debugging support. Implement a stabs reader and
[platform/upstream/binutils.git] / binutils / stabs.c
1 /* stabs.c -- Parse stabs debugging information
2    Copyright (C) 1995 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 "debug.h"
34 #include "budbg.h"
35
36 /* Meaningless definition needs by aout64.h.  FIXME.  */
37 #define BYTES_IN_WORD 4
38
39 #include "aout/aout64.h"
40 #include "aout/stab_gnu.h"
41
42 /* The number of predefined XCOFF types.  */
43
44 #define XCOFF_TYPE_COUNT 34
45
46 /* This structure is used as a handle so that the stab parsing doesn't
47    need to use any static variables.  */
48
49 struct stab_handle
50 {
51   /* The type of the last stab symbol, so that we can detect N_SO
52      pairs.  */
53   int last_type;
54   /* The offset of the start of the function, so that we can handle
55      function relative N_RBRAC symbols.  */
56   bfd_vma function_start_offset;
57   /* The version number of gcc which compiled the current compilation
58      unit, 0 if not compiled by gcc.  */
59   int gcc_compiled;
60   /* Whether an N_OPT symbol was seen that was not generated by gcc,
61      so that we can detect the SunPRO compiler.  */
62   boolean n_opt_found;
63   /* The main file name.  */
64   char *main_filename;
65   /* A stack of N_BINCL files.  */
66   struct bincl_file *bincl_stack;
67   /* Whether we are inside a function or not.  */
68   boolean within_function;
69   /* The depth of block nesting.  */
70   int block_depth;
71   /* List of pending variable definitions.  */
72   struct stab_pending_var *pending;
73   /* Number of files for which we have types.  */
74   unsigned int files;
75   /* Lists of types per file.  */
76   struct stab_types **file_types;
77   /* Predefined XCOFF types.  */
78   debug_type xcoff_types[XCOFF_TYPE_COUNT];
79   /* Undefined tags.  */
80   struct stab_tag *tags;
81 };
82
83 /* A list of these structures is used to hold pending variable
84    definitions seen before the N_LBRAC of a block.  */
85
86 struct stab_pending_var
87 {
88   /* Next pending variable definition.  */
89   struct stab_pending_var *next;
90   /* Name.  */
91   const char *name;
92   /* Type.  */
93   debug_type type;
94   /* Kind.  */
95   enum debug_var_kind kind;
96   /* Value.  */
97   bfd_vma val;
98 };
99
100 /* A list of these structures is used to hold the types for a single
101    file.  */
102
103 struct stab_types
104 {
105   /* Next set of slots for this file.  */
106   struct stab_types *next;
107   /* Types indexed by type number.  */
108 #define STAB_TYPES_SLOTS (16)
109   debug_type types[STAB_TYPES_SLOTS];
110 };
111
112 /* We keep a list of undefined tags that we encounter, so that we can
113    fill them in if the tag is later defined.  */
114
115 struct stab_tag
116 {
117   /* Next undefined tag.  */
118   struct stab_tag *next;
119   /* Tag name.  */
120   const char *name;
121   /* Type kind.  */
122   enum debug_type_kind kind;
123   /* Slot to hold real type when we discover it.  If we don't, we fill
124      in an undefined tag type.  */
125   debug_type slot;
126   /* Indirect type we have created to point at slot.  */
127   debug_type type;
128 };
129
130 static char *savestring PARAMS ((const char *, int));
131 static bfd_vma parse_number PARAMS ((const char **, boolean *));
132 static void bad_stab PARAMS ((const char *));
133 static void warn_stab PARAMS ((const char *, const char *));
134 static boolean parse_stab_string
135   PARAMS ((PTR, struct stab_handle *, int, int, bfd_vma, const char *));
136 static debug_type parse_stab_type
137   PARAMS ((PTR, struct stab_handle *, const char **, debug_type **));
138 static boolean parse_stab_type_number
139   PARAMS ((const char **, int *));
140 static debug_type parse_stab_range_type
141   PARAMS ((PTR, struct stab_handle *, const char **, const int *));
142 static debug_type parse_stab_sun_builtin_type PARAMS ((PTR, const char **));
143 static debug_type parse_stab_sun_floating_type
144   PARAMS ((PTR, const char **));
145 static debug_type parse_stab_enum_type PARAMS ((PTR, const char **));
146 static debug_type parse_stab_struct_type
147   PARAMS ((PTR, struct stab_handle *, const char **, boolean, const int *));
148 static boolean parse_stab_baseclasses
149   PARAMS ((PTR, struct stab_handle *, const char **, debug_baseclass **));
150 static boolean parse_stab_struct_fields
151   PARAMS ((PTR, struct stab_handle *, const char **, debug_field **,
152            boolean *));
153 static boolean parse_stab_cpp_abbrev
154   PARAMS ((PTR, struct stab_handle *, const char **, debug_field *));
155 static boolean parse_stab_one_struct_field
156   PARAMS ((PTR, struct stab_handle *, const char **, const char *,
157            debug_field *, boolean *));
158 static boolean parse_stab_members
159   PARAMS ((PTR, struct stab_handle *, const char **, debug_method **));
160 static boolean parse_stab_tilde_field
161   PARAMS ((PTR, struct stab_handle *, const char **, const int *,
162            debug_type *, boolean *));
163 static debug_type parse_stab_array_type
164   PARAMS ((PTR, struct stab_handle *, const char **, boolean));
165 static void push_bincl PARAMS ((struct stab_handle *, const char *));
166 static const char *pop_bincl PARAMS ((struct stab_handle *));
167 static boolean stab_record_variable
168   PARAMS ((PTR, struct stab_handle *, const char *, debug_type,
169            enum debug_var_kind, bfd_vma));
170 static boolean stab_emit_pending_vars PARAMS ((PTR, struct stab_handle *));
171 static debug_type *stab_find_slot
172   PARAMS ((struct stab_handle *, const int *));
173 static debug_type stab_find_type
174   PARAMS ((PTR, struct stab_handle *, const int *));
175 static boolean stab_record_type
176   PARAMS ((PTR, struct stab_handle *, const int *, debug_type));
177 static debug_type stab_xcoff_builtin_type
178   PARAMS ((PTR, struct stab_handle *, int));
179
180 /* Save a string in memory.  */
181
182 static char *
183 savestring (start, len)
184      const char *start;
185      int len;
186 {
187   char *ret;
188
189   ret = (char *) xmalloc (len + 1);
190   memcpy (ret, start, len);
191   ret[len] = '\0';
192   return ret;
193 }
194
195 /* Read a number from a string.  */
196
197 static bfd_vma
198 parse_number (pp, poverflow)
199      const char **pp;
200      boolean *poverflow;
201 {
202   unsigned long ul;
203   const char *orig;
204
205   if (poverflow != NULL)
206     *poverflow = false;
207
208   orig = *pp;
209
210   errno = 0;
211   ul = strtoul (*pp, (char **) pp, 0);
212   if (ul + 1 != 0 || errno == 0)
213     return (bfd_vma) ul;
214
215   /* Note that even though strtoul overflowed, it should have set *pp
216      to the end of the number, which is where we want it.  */
217
218   if (sizeof (bfd_vma) > sizeof (unsigned long))
219     {
220       const char *p;
221       boolean neg;
222       int base;
223       bfd_vma over, lastdig;
224       boolean overflow;
225       bfd_vma v;
226
227       /* Our own version of strtoul, for a bfd_vma.  */
228
229       p = orig;
230
231       neg = false;
232       if (*p == '+')
233         ++p;
234       else if (*p == '-')
235         {
236           neg = true;
237           ++p;
238         }
239
240       base = 10;
241       if (*p == '0')
242         {
243           if (p[1] == 'x' || p[1] == 'X')
244             {
245               base = 16;
246               p += 2;
247             }
248           else
249             {
250               base = 8;
251               ++p;
252             }
253         }
254
255       over = ((bfd_vma) (bfd_signed_vma) -1) / (bfd_vma) base;
256       lastdig = ((bfd_vma) (bfd_signed_vma) -1) % (bfd_vma) base;
257
258       overflow = false;
259       v = 0;
260       while (1)
261         {
262           int d;
263
264           d = *p++;
265           if (isdigit ((unsigned char) d))
266             d -= '0';
267           else if (isupper ((unsigned char) d))
268             d -= 'A';
269           else if (islower ((unsigned char) d))
270             d -= 'a';
271           else
272             break;
273
274           if (d >= base)
275             break;
276
277           if (v > over || (v == over && (bfd_vma) d > lastdig))
278             {
279               overflow = true;
280               break;
281             }
282         }
283
284       if (! overflow)
285         {
286           if (neg)
287             v = - v;
288           return v;
289         }
290     }
291
292   /* If we get here, the number is too large to represent in a
293      bfd_vma.  */
294
295   if (poverflow != NULL)
296     *poverflow = true;
297   else
298     warn_stab (orig, "numeric overflow");
299
300   return 0;
301 }
302
303 /* Give an error for a bad stab string.  */
304
305 static void
306 bad_stab (p)
307      const char *p;
308 {
309   fprintf (stderr, "Bad stab: %s\n", p);
310 }
311
312 /* Warn about something in a stab string.  */
313
314 static void
315 warn_stab (p, err)
316      const char *p;
317      const char *err;
318 {
319   fprintf (stderr, "Warning: %s: %s\n", err, p);
320 }
321
322 /* Create a handle to parse stabs symbols with.  */
323
324 /*ARGSUSED*/
325 PTR
326 start_stab (dhandle)
327      PTR dhandle;
328 {
329   struct stab_handle *ret;
330
331   ret = (struct stab_handle *) xmalloc (sizeof *ret);
332   memset (ret, 0, sizeof *ret);
333   ret->files = 1;
334   ret->file_types = (struct stab_types **) xmalloc (sizeof *ret->file_types);
335   ret->file_types[0] = NULL;
336   return (PTR) ret;
337 }
338
339 /* When we have processed all the stabs information, we need to go
340    through and fill in all the undefined tags.  */
341
342 boolean
343 finish_stab (dhandle, handle)
344      PTR dhandle;
345      PTR handle;
346 {
347   struct stab_handle *info = (struct stab_handle *) handle;
348   struct stab_tag *st;
349
350   if (info->within_function)
351     {
352       if (! debug_end_function (dhandle, (bfd_vma) -1))
353         return false;
354       info->within_function = false;
355     }
356
357   for (st = info->tags; st != NULL; st = st->next)
358     {
359       st->slot = debug_make_undefined_tagged_type (dhandle, st->name,
360                                                    st->kind);
361       if (st->slot == DEBUG_TYPE_NULL)
362         return false;
363     }
364
365   return true;
366 }
367
368 /* Handle a single stabs symbol.  */
369
370 boolean
371 parse_stab (dhandle, handle, type, desc, value, string)
372      PTR dhandle;
373      PTR handle;
374      int type;
375      int desc;
376      bfd_vma value;
377      const char *string;
378 {
379   struct stab_handle *info = (struct stab_handle *) handle;
380
381   switch (type)
382     {
383     case N_FN:
384     case N_FN_SEQ:
385       break;
386
387     case N_LBRAC:
388       /* Ignore extra outermost context from SunPRO cc and acc.  */
389       if (info->n_opt_found && desc == 1)
390         break;
391
392       if (! info->within_function)
393         {
394           fprintf (stderr, "N_LBRAC not within function\n");
395           return false;
396         }
397
398       /* Start an inner lexical block.  */
399       if (! debug_start_block (dhandle, value + info->function_start_offset))
400         return false;
401
402       /* Emit any pending variable definitions.  */
403       if (! stab_emit_pending_vars (dhandle, info))
404         return false;
405
406       ++info->block_depth;
407       break;
408
409     case N_RBRAC:
410       /* Ignore extra outermost context from SunPRO cc and acc.  */
411       if (info->n_opt_found && desc == 1)
412         break;
413
414       /* We shouldn't have any pending variable definitions here, but,
415          if we do, we probably need to emit them before closing the
416          block.  */
417       if (! stab_emit_pending_vars (dhandle, info))
418         return false;
419
420       /* End an inner lexical block.  */
421       if (! debug_end_block (dhandle, value + info->function_start_offset))
422         return false;
423
424       --info->block_depth;
425       if (info->block_depth == 0)
426         {
427           info->within_function = false;
428           if (! debug_end_function (dhandle,
429                                     value + info->function_start_offset))
430             return false;
431         }
432       break;
433
434     case N_SO:
435       /* Start a file.  If we get two in a row, the first is the
436          directory name.  An empty string is emitted by gcc at the end
437          of a compilation unit.  */
438       if (*string == '\0')
439         {
440           if (info->within_function)
441             {
442               if (! debug_end_function (dhandle, (bfd_vma) -1))
443                 return false;
444               info->within_function = false;
445             }
446           return true;
447         }
448       info->gcc_compiled = 0;
449       info->n_opt_found = false;
450       if (info->last_type == N_SO)
451         {
452           char *o;
453
454           if (! debug_append_filename (dhandle, string))
455             return false;
456           o = info->main_filename;
457           info->main_filename = concat (o, string, (const char *) NULL);
458           free (o);
459         }
460       else
461         {
462           if (! debug_set_filename (dhandle, string))
463             return false;
464           if (info->main_filename != NULL)
465             free (info->main_filename);
466           info->main_filename = xstrdup (string);
467
468           /* We need to reset the mapping from type numbers to types.
469              We can't free the old mapping, because of the use of
470              debug_make_indirect_type.  */
471           info->files = 1;
472           info->file_types = ((struct stab_types **)
473                               xmalloc (sizeof *info->file_types));
474           info->file_types[0] = NULL;
475         }
476       break;
477
478     case N_SOL:
479       /* Start an include file.  */
480       if (! debug_start_source (dhandle, string))
481         return false;
482       break;
483
484     case N_BINCL:
485       /* Start an include file which may be replaced.  */
486       push_bincl (info, string);
487       if (! debug_start_source (dhandle, string))
488         return false;
489       break;
490
491     case N_EINCL:
492       /* End an N_BINCL include.  */
493       if (! debug_start_source (dhandle, pop_bincl (info)))
494         return false;
495       break;
496
497     case N_EXCL:
498       /* This is a duplicate of a header file named by N_BINCL which
499          was eliminated by the linker.  */
500       ++info->files;
501       info->file_types = ((struct stab_types **)
502                           xrealloc ((PTR) info->file_types,
503                                     (info->files
504                                      * sizeof *info->file_types)));
505       info->file_types[info->files - 1] = NULL;
506       break;
507
508     case N_SLINE:
509       if (! debug_record_line (dhandle, desc,
510                                value + info->function_start_offset))
511         return false;
512       break;
513
514     case N_BCOMM:
515       if (! debug_start_common_block (dhandle, string))
516         return false;
517       break;
518
519     case N_ECOMM:
520       if (! debug_end_common_block (dhandle, string))
521         return false;
522       break;
523
524       /* FIXME: gdb checks the string for N_STSYM, N_LCSYM or N_ROSYM
525          symbols, and if it does not start with :S, gdb relocates the
526          value to the start of the section.  gcc always seems to use
527          :S, so we don't worry about this.  */
528     default:
529       {
530         const char *colon;
531
532         colon = strchr (string, ':');
533         if (colon != NULL
534             && (colon[1] == 'f' || colon[1] == 'F'))
535           {
536             if (info->within_function)
537               {
538                 if (! debug_end_function (dhandle, (bfd_vma) -1))
539                   return false;
540               }
541             info->function_start_offset = value;
542             info->within_function = true;
543           }
544
545         if (! parse_stab_string (dhandle, info, type, desc, value, string))
546           return false;
547       }
548       break;
549
550     case N_OPT:
551       if (string != NULL && strcmp (string, "gcc2_compiled.") == 0)
552         info->gcc_compiled = 2;
553       else if (string != NULL && strcmp (string, "gcc_compiled.") == 0)
554         info->gcc_compiled = 1;
555       else
556         info->n_opt_found = true;
557       break;
558
559     case N_OBJ:
560     case N_ENDM:
561     case N_MAIN:
562       break;
563     }
564
565   info->last_type = type;
566
567   return true;
568 }
569
570 /* Parse the stabs string.  */
571
572 static boolean
573 parse_stab_string (dhandle, info, stabtype, desc, value, string)
574      PTR dhandle;
575      struct stab_handle *info;
576      int stabtype;
577      int desc;
578      bfd_vma value;
579      const char *string;
580 {
581   const char *p;
582   char *name;
583   int type;
584   debug_type dtype;
585   boolean synonym;
586   unsigned int lineno;
587   debug_type *slot;
588
589   p = strchr (string, ':');
590   if (p == NULL)
591     return true;
592
593   while (p[1] == ':')
594     {
595       p += 2;
596       p = strchr (p, ':');
597       if (p == NULL)
598         {
599           bad_stab (string);
600           return false;
601         }
602     }
603
604   /* GCC 2.x puts the line number in desc.  SunOS apparently puts in
605      the number of bytes occupied by a type or object, which we
606      ignore.  */
607   if (info->gcc_compiled >= 2)
608     lineno = desc;
609   else
610     lineno = 0;
611
612   /* FIXME: Sometimes the special C++ names start with '.'.  */
613   name = NULL;
614   if (string[0] == '$')
615     {
616       switch (string[1])
617         {
618         case 't':
619           name = "this";
620           break;
621         case 'v':
622           /* Was: name = "vptr"; */
623           break;
624         case 'e':
625           name = "eh_throw";
626           break;
627         case '_':
628           /* This was an anonymous type that was never fixed up.  */
629           break;
630         case 'X':
631           /* SunPRO (3.0 at least) static variable encoding.  */
632           break;
633         default:
634           warn_stab (string, "unknown C++ encoded name");
635           break;
636         }
637     }
638
639   if (name == NULL)
640     {
641       if (p == string || (string[0] == ' ' && p == string + 1))
642         name = NULL;
643       else
644         name = savestring (string, p - string);
645     }
646
647   ++p;
648   if (isdigit ((unsigned char) *p) || *p == '(' || *p == '-')
649     type = 'l';
650   else
651     type = *p++;
652
653   switch (type)
654     {
655     case 'c':
656       /* c is a special case, not followed by a type-number.
657          SYMBOL:c=iVALUE for an integer constant symbol.
658          SYMBOL:c=rVALUE for a floating constant symbol.
659          SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
660          e.g. "b:c=e6,0" for "const b = blob1"
661          (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
662       if (*p != '=')
663         {
664           bad_stab (string);
665           return false;
666         }
667       ++p;
668       switch (*p++)
669         {
670         case 'r':
671           /* Floating point constant.  */
672           if (! debug_record_float_const (dhandle, name, atof (p)))
673             return false;
674           break;
675         case 'i':
676           /* Integer constant.  */
677           /* Defining integer constants this way is kind of silly,
678              since 'e' constants allows the compiler to give not only
679              the value, but the type as well.  C has at least int,
680              long, unsigned int, and long long as constant types;
681              other languages probably should have at least unsigned as
682              well as signed constants.  */
683           if (! debug_record_int_const (dhandle, name, atoi (p)))
684             return false;
685           break;
686         case 'e':
687           /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
688              can be represented as integral.
689              e.g. "b:c=e6,0" for "const b = blob1"
690              (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
691           dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
692           if (dtype == DEBUG_TYPE_NULL)
693             return false;
694           if (*p != ',')
695             {
696               bad_stab (string);
697               return false;
698             }
699           if (! debug_record_typed_const (dhandle, name, dtype, atoi (p)))
700             return false;
701           break;
702         default:
703           bad_stab (string);
704           return false;
705         }
706
707       break;
708
709     case 'C':
710       /* The name of a caught exception.  */
711       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
712       if (dtype == DEBUG_TYPE_NULL)
713         return false;
714       if (! debug_record_label (dhandle, name, dtype, value))
715         return false;
716       break;
717
718     case 'f':
719     case 'F':
720       /* A function definition.  */
721       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
722       if (dtype == DEBUG_TYPE_NULL)
723         return false;
724       if (! debug_record_function (dhandle, name, dtype, type == 'F', value))
725         return false;
726
727       /* Sun acc puts declared types of arguments here.  We don't care
728          about their actual types (FIXME -- we should remember the whole
729          function prototype), but the list may define some new types
730          that we have to remember, so we must scan it now.  */
731       while (*p == ';')
732         {
733           ++p;
734           if (parse_stab_type (dhandle, info, &p, (debug_type **) NULL)
735               == DEBUG_TYPE_NULL)
736             return false;
737         }
738
739       break;
740
741     case 'G':
742       /* A global symbol.  The value must be extracted from the symbol
743          table.  */
744       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
745       if (dtype == DEBUG_TYPE_NULL)
746         return false;
747       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_GLOBAL,
748                                   (bfd_vma) -1))
749         return false;
750       break;
751
752       /* This case is faked by a conditional above, when there is no
753          code letter in the dbx data.  Dbx data never actually
754          contains 'l'.  */
755     case 'l':
756     case 's':
757       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
758       if (dtype == DEBUG_TYPE_NULL)
759         return false;
760       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
761                                   value))
762         return false;
763       break;
764
765     case 'p':
766       /* A function parameter.  */
767       if (*p != 'F')
768         dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
769       else
770         {
771         /* pF is a two-letter code that means a function parameter in
772            Fortran.  The type-number specifies the type of the return
773            value.  Translate it into a pointer-to-function type.  */
774           ++p;
775           dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
776           if (dtype != DEBUG_TYPE_NULL)
777             dtype = debug_make_pointer_type (dhandle,
778                                              debug_make_function_type (dhandle,
779                                                                        dtype));
780         }
781       if (dtype == DEBUG_TYPE_NULL)
782         return false;
783       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_STACK,
784                                     value))
785         return false;
786
787       /* FIXME: At this point gdb considers rearranging the parameter
788          address on a big endian machine if it is smaller than an int.
789          We have no way to do that, since we don't really know much
790          about the target.  */
791
792       break;
793
794     case 'P':
795       if (stabtype == N_FUN)
796         {
797           /* Prototype of a function referenced by this file.  */
798           while (*p == ';')
799             {
800               ++p;
801               if (parse_stab_type (dhandle, info, &p, (debug_type **) NULL)
802                   == DEBUG_TYPE_NULL)
803                 return false;
804             }
805           break;
806         }
807       /* Fall through.  */
808     case 'R':
809       /* Parameter which is in a register.  */
810       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
811       if (dtype == DEBUG_TYPE_NULL)
812         return false;
813       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REG,
814                                     value))
815         return false;
816       break;
817
818     case 'r':
819       /* Register variable (either global or local).  */
820       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
821       if (dtype == DEBUG_TYPE_NULL)
822         return false;
823       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_REGISTER,
824                                   value))
825         return false;
826
827       /* FIXME: At this point gdb checks to combine pairs of 'p' and
828          'r' stabs into a single 'P' stab.  */
829
830       break;
831
832     case 'S':
833       /* Static symbol at top level of file */
834       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
835       if (dtype == DEBUG_TYPE_NULL)
836         return false;
837       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_STATIC,
838                                   value))
839         return false;
840       break;
841
842     case 't':
843       /* A typedef.  */
844       dtype = parse_stab_type (dhandle, info, &p, &slot);
845       if (dtype == DEBUG_TYPE_NULL)
846         return false;
847       if (name == NULL)
848         {
849           /* A nameless type.  Nothing to do.  */
850           return true;
851         }
852
853       dtype = debug_name_type (dhandle, name, dtype);
854       if (dtype == DEBUG_TYPE_NULL)
855         return false;
856
857       if (slot != NULL)
858         *slot = dtype;
859
860       break;
861
862     case 'T':
863       /* Struct, union, or enum tag.  For GNU C++, this can be be followed
864          by 't' which means we are typedef'ing it as well.  */
865       if (*p != 't')
866         {
867           synonym = false;
868           /* FIXME: gdb sets synonym to true if the current language
869              is C++.  */
870         }
871       else
872         {
873           synonym = true;
874           ++p;
875         }
876
877       dtype = parse_stab_type (dhandle, info, &p, &slot);
878       if (dtype == DEBUG_TYPE_NULL)
879         return false;
880       if (name == NULL)
881         return true;
882
883       dtype = debug_tag_type (dhandle, name, dtype);
884       if (dtype == DEBUG_TYPE_NULL)
885         return false;
886       if (slot != NULL)
887         *slot = dtype;
888
889       /* See if we have a cross reference to this tag which we can now
890          fill in.  */
891       {
892         register struct stab_tag **pst;
893
894         for (pst = &info->tags; *pst != NULL; pst = &(*pst)->next)
895           {
896             if ((*pst)->name[0] == name[0]
897                 && strcmp ((*pst)->name, name) == 0)
898               {
899                 (*pst)->slot = dtype;
900                 *pst = (*pst)->next;
901                 break;
902               }
903           }
904       }
905
906       if (synonym)
907         {
908           dtype = debug_name_type (dhandle, name, dtype);
909           if (dtype == DEBUG_TYPE_NULL)
910             return false;
911
912           if (slot != NULL)
913             *slot = dtype;
914         }
915
916       break;
917
918     case 'V':
919       /* Static symbol of local scope */
920       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
921       if (dtype == DEBUG_TYPE_NULL)
922         return false;
923       /* FIXME: gdb checks os9k_stabs here.  */
924       if (! stab_record_variable (dhandle, info, name, dtype,
925                                   DEBUG_LOCAL_STATIC, value))
926         return false;
927       break;
928
929     case 'v':
930       /* Reference parameter.  */
931       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
932       if (dtype == DEBUG_TYPE_NULL)
933         return false;
934       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REFERENCE,
935                                     value))
936         return false;
937       break;
938
939     case 'a':
940       /* Reference parameter which is in a register.  */
941       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
942       if (dtype == DEBUG_TYPE_NULL)
943         return false;
944       if (! debug_record_parameter (dhandle, name, dtype, DEBUG_PARM_REF_REG,
945                                     value))
946         return false;
947       break;
948
949     case 'X':
950       /* This is used by Sun FORTRAN for "function result value".
951          Sun claims ("dbx and dbxtool interfaces", 2nd ed)
952          that Pascal uses it too, but when I tried it Pascal used
953          "x:3" (local symbol) instead.  */
954       dtype = parse_stab_type (dhandle, info, &p, (debug_type **) NULL);
955       if (dtype == DEBUG_TYPE_NULL)
956         return false;
957       if (! stab_record_variable (dhandle, info, name, dtype, DEBUG_LOCAL,
958                                   value))
959         return false;
960       break;
961
962     default:
963       bad_stab (string);
964       return false;
965     }
966
967   /* FIXME: gdb converts structure values to structure pointers in a
968      couple of cases, depending upon the target.  */
969
970   return true;
971 }
972
973 /* Parse a stabs type.  */
974
975 static debug_type
976 parse_stab_type (dhandle, info, pp, slotp)
977      PTR dhandle;
978      struct stab_handle *info;
979      const char **pp;
980      debug_type **slotp;
981 {
982   const char *orig;
983   int typenums[2];
984   int size;
985   boolean stringp;
986   int descriptor;
987   debug_type dtype;
988
989   if (slotp != NULL)
990     *slotp = NULL;
991
992   orig = *pp;
993
994   size = -1;
995   stringp = false;
996
997   /* Read type number if present.  The type number may be omitted.
998      for instance in a two-dimensional array declared with type
999      "ar1;1;10;ar1;1;10;4".  */
1000   if (! isdigit ((unsigned char) **pp) && **pp != '(' && **pp != '-')
1001     {
1002       /* 'typenums=' not present, type is anonymous.  Read and return
1003          the definition, but don't put it in the type vector.  */
1004       typenums[0] = typenums[1] = -1;
1005     }
1006   else
1007     {
1008       if (! parse_stab_type_number (pp, typenums))
1009         return DEBUG_TYPE_NULL;
1010
1011       if (**pp != '=')
1012         {
1013           /* Type is not being defined here.  Either it already
1014              exists, or this is a forward reference to it.  */
1015           return stab_find_type (dhandle, info, typenums);
1016         }
1017
1018       /* Only set the slot if the type is being defined.  This means
1019          that the mapping from type numbers to types will only record
1020          the name of the typedef which defines a type.  If we don't do
1021          this, then something like
1022              typedef int foo;
1023              int i;
1024          will record that i is of type foo.  Unfortunately, stabs
1025          information is ambiguous about variable types.  For this code,
1026              typedef int foo;
1027              int i;
1028              foo j;
1029          the stabs information records both i and j as having the same
1030          type.  This could be fixed by patching the compiler.  */
1031       if (slotp != NULL && typenums[0] >= 0 && typenums[1] >= 0)
1032         *slotp = stab_find_slot (info, typenums);
1033
1034       /* Type is being defined here.  */
1035       /* Skip the '='.  */
1036       ++*pp;
1037
1038       while (**pp == '@')
1039         {
1040           const char *p = *pp + 1;
1041           const char *attr;
1042
1043           if (isdigit ((unsigned char) *p) || *p == '(' || *p == '-')
1044             {
1045               /* Member type.  */
1046               break;
1047             }
1048
1049           /* Type attributes.  */
1050           attr = p;
1051
1052           for (; *p != ';'; ++p)
1053             {
1054               if (*p == '\0')
1055                 {
1056                   bad_stab (orig);
1057                   return DEBUG_TYPE_NULL;
1058                 }
1059             }
1060           *pp = p + 1;
1061
1062           switch (*attr)
1063             {
1064             case 's':
1065               size = atoi (attr + 1);
1066               if (size <= 0)
1067                 size = -1;
1068               break;
1069
1070             case 'S':
1071               stringp = true;
1072               break;
1073
1074             default:
1075               /* Ignore unrecognized type attributes, so future
1076                  compilers can invent new ones.  */
1077               break;
1078             }
1079         }
1080     }
1081
1082   descriptor = **pp;
1083   ++*pp;
1084
1085   switch (descriptor)
1086     {
1087     case 'x':
1088       {
1089         enum debug_type_kind code;
1090         const char *q1, *q2, *p;
1091         char *name;
1092         struct stab_tag *st;
1093
1094         /* A cross reference to another type.  */
1095
1096         switch (**pp)
1097           {
1098           case 's':
1099             code = DEBUG_KIND_STRUCT;
1100             break;
1101           case 'u':
1102             code = DEBUG_KIND_UNION;
1103             break;
1104           case 'e':
1105             code = DEBUG_KIND_ENUM;
1106             break;
1107           default:
1108             /* Complain and keep going, so compilers can invent new
1109                cross-reference types.  */
1110             warn_stab (orig, "unrecognized cross reference type");
1111             code = DEBUG_KIND_STRUCT;
1112             break;
1113           }
1114         ++*pp;
1115
1116         q1 = strchr (*pp, '<');
1117         p = strchr (*pp, ':');
1118         if (p == NULL)
1119           {
1120             bad_stab (orig);
1121             return DEBUG_TYPE_NULL;
1122           }
1123         while (q1 != NULL && p > q1 && p[1] == ':')
1124           {
1125             q2 = strchr (q1, '>');
1126             if (q2 == NULL || q2 < p)
1127               break;
1128             p += 2;
1129             p = strchr (p, ':');
1130             if (p == NULL)
1131               {
1132                 bad_stab (orig);
1133                 return DEBUG_TYPE_NULL;
1134               }
1135           }
1136
1137         name = savestring (*pp, p - *pp);
1138
1139         *pp = p + 1;
1140
1141         /* We pass DEBUG_KIND_VOID because we want all tags in the
1142            same namespace.  This is right for C, and I don't know how
1143            to handle other languages.  FIXME.  */
1144         dtype = debug_find_tagged_type (dhandle, name, DEBUG_KIND_VOID);
1145         if (dtype != DEBUG_TYPE_NULL)
1146           {
1147             free (name);
1148             if (typenums[0] != -1)
1149               {
1150                 if (! stab_record_type (dhandle, info, typenums, dtype))
1151                   return DEBUG_TYPE_NULL;
1152               }
1153             return dtype;
1154           }
1155
1156         /* We need to allocate an entry on the undefined tag list.  */
1157         for (st = info->tags; st != NULL; st = st->next)
1158           {
1159             if (st->name[0] == name[0]
1160                 && strcmp (st->name, name) == 0)
1161               break;
1162           }
1163         if (st == NULL)
1164           {
1165             st = (struct stab_tag *) xmalloc (sizeof *st);
1166             memset (st, 0, sizeof *st);
1167
1168             st->next = info->tags;
1169             st->name = name;
1170             st->kind = code;
1171             st->slot = DEBUG_TYPE_NULL;
1172             st->type = debug_make_indirect_type (dhandle, &st->slot, name);
1173             info->tags = st;
1174           }
1175
1176         dtype = st->type;
1177         if (typenums[0] != -1)
1178           {
1179             if (! stab_record_type (dhandle, info, typenums, dtype))
1180               return DEBUG_TYPE_NULL;
1181           }
1182         return dtype;
1183       }
1184       break;
1185
1186     case '-':
1187     case '0':
1188     case '1':
1189     case '2':
1190     case '3':
1191     case '4':
1192     case '5':
1193     case '6':
1194     case '7':
1195     case '8':
1196     case '9':
1197     case '(':
1198       {
1199         const char *hold;
1200         int xtypenums[2];
1201
1202         /* This type is defined as another type.  */
1203
1204         (*pp)--;
1205         hold = *pp;
1206
1207         /* Peek ahead at the number to detect void.  */
1208         if (! parse_stab_type_number (pp, xtypenums))
1209           return DEBUG_TYPE_NULL;
1210
1211         if (typenums[0] == xtypenums[0] && typenums[1] == xtypenums[1])
1212           {
1213             /* This type is being defined as itself, which means that
1214                it is void.  */
1215             dtype = debug_make_void_type (dhandle);
1216           }
1217         else
1218           {
1219             *pp = hold;
1220
1221             /* Go back to the number and have parse_stab_type get it.
1222                This means that we can deal with something like
1223                t(1,2)=(3,4)=... which the Lucid compiler uses.  */
1224             dtype = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
1225             if (dtype == DEBUG_TYPE_NULL)
1226               return DEBUG_TYPE_NULL;
1227           }
1228
1229         if (typenums[0] != -1)
1230           {
1231             if (! stab_record_type (dhandle, info, typenums, dtype))
1232               return DEBUG_TYPE_NULL;
1233           }
1234
1235         break;
1236       }
1237
1238     case '*':
1239       dtype = debug_make_pointer_type (dhandle,
1240                                        parse_stab_type (dhandle, info, pp,
1241                                                         (debug_type **) NULL));
1242       break;
1243
1244     case '&':
1245       /* Reference to another type.  */
1246       dtype = (debug_make_reference_type
1247                (dhandle,
1248                 parse_stab_type (dhandle, info, pp, (debug_type **) NULL)));
1249       break;
1250
1251     case 'f':
1252       /* Function returning another type.  */
1253       /* FIXME: gdb checks os9k_stabs here.  */
1254       dtype = (debug_make_function_type
1255                (dhandle,
1256                 parse_stab_type (dhandle, info, pp, (debug_type **) NULL)));
1257       break;
1258
1259     case 'k':
1260       /* Const qualifier on some type (Sun).  */
1261       /* FIXME: gdb accepts 'c' here if os9k_stabs.  */
1262       dtype = debug_make_const_type (dhandle,
1263                                      parse_stab_type (dhandle, info, pp,
1264                                                       (debug_type **) NULL));
1265       break;
1266
1267     case 'B':
1268       /* Volatile qual on some type (Sun).  */
1269       /* FIXME: gdb accepts 'i' here if os9k_stabs.  */
1270       dtype = (debug_make_volatile_type
1271                (dhandle,
1272                 parse_stab_type (dhandle, info, pp, (debug_type **) NULL)));
1273       break;
1274
1275     case '@':
1276       /* Offset (class & variable) type.  This is used for a pointer
1277          relative to an object.  */
1278       {
1279         debug_type domain;
1280         debug_type memtype;
1281
1282         /* Member type.  */
1283
1284         domain = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
1285         if (domain == DEBUG_TYPE_NULL)
1286           return DEBUG_TYPE_NULL;
1287
1288         if (**pp != ',')
1289           {
1290             bad_stab (orig);
1291             return DEBUG_TYPE_NULL;
1292           }
1293         ++*pp;
1294
1295         memtype = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
1296         if (memtype == DEBUG_TYPE_NULL)
1297           return DEBUG_TYPE_NULL;
1298
1299         dtype = debug_make_offset_type (dhandle, domain, memtype);
1300       }
1301       break;
1302
1303     case '#':
1304       /* Method (class & fn) type.  */
1305       if (**pp == '#')
1306         {
1307           debug_type return_type;
1308
1309           ++*pp;
1310           return_type = parse_stab_type (dhandle, info, pp,
1311                                          (debug_type **) NULL);
1312           if (return_type == DEBUG_TYPE_NULL)
1313             return DEBUG_TYPE_NULL;
1314           if (**pp != ';')
1315             {
1316               bad_stab (orig);
1317               return DEBUG_TYPE_NULL;
1318             }
1319           ++*pp;
1320           dtype = debug_make_method_type (dhandle, return_type,
1321                                           DEBUG_TYPE_NULL, NULL);
1322         }
1323       else
1324         {
1325           debug_type domain;
1326           debug_type return_type;
1327           debug_type *args;
1328           unsigned int n;
1329           unsigned int alloc;
1330
1331           domain = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
1332           if (domain == DEBUG_TYPE_NULL)
1333             return DEBUG_TYPE_NULL;
1334
1335           if (**pp != ',')
1336             {
1337               bad_stab (orig);
1338               return DEBUG_TYPE_NULL;
1339             }
1340           ++*pp;
1341
1342           return_type = parse_stab_type (dhandle, info, pp,
1343                                          (debug_type **) NULL);
1344           if (return_type == DEBUG_TYPE_NULL)
1345             return DEBUG_TYPE_NULL;
1346
1347           alloc = 10;
1348           args = (debug_type *) xmalloc (alloc * sizeof *args);
1349           n = 0;
1350           while (**pp != ';')
1351             {
1352               if (**pp != ',')
1353                 {
1354                   bad_stab (orig);
1355                   return DEBUG_TYPE_NULL;
1356                 }
1357               ++*pp;
1358
1359               if (n + 1 >= alloc)
1360                 {
1361                   alloc += 10;
1362                   args = ((debug_type *)
1363                           xrealloc ((PTR) args, alloc * sizeof *args));
1364                 }
1365
1366               args[n] = parse_stab_type (dhandle, info, pp,
1367                                          (debug_type **) NULL);
1368               if (args[n] == DEBUG_TYPE_NULL)
1369                 return DEBUG_TYPE_NULL;
1370               ++n;
1371             }
1372           ++*pp;
1373
1374           args[n] = DEBUG_TYPE_NULL;
1375
1376           dtype = debug_make_method_type (dhandle, return_type, domain, args);
1377         }
1378       break;
1379
1380     case 'r':
1381       /* Range type.  */
1382       dtype = parse_stab_range_type (dhandle, info, pp, typenums);
1383       break;
1384
1385     case 'b':
1386       /* FIXME: gdb checks os9k_stabs here.  */
1387       /* Sun ACC builtin int type.  */
1388       dtype = parse_stab_sun_builtin_type (dhandle, pp);
1389       break;
1390
1391     case 'R':
1392       /* Sun ACC builtin float type.  */
1393       dtype = parse_stab_sun_floating_type (dhandle, pp);
1394       break;
1395
1396     case 'e':
1397       /* Enumeration type.  */
1398       dtype = parse_stab_enum_type (dhandle, pp);
1399       break;
1400
1401     case 's':
1402     case 'u':
1403       /* Struct or union type.  */
1404       dtype = parse_stab_struct_type (dhandle, info, pp,
1405                                       descriptor == 's', typenums);
1406       break;
1407
1408     case 'a':
1409       /* Array type.  */
1410       if (**pp != 'r')
1411         {
1412           bad_stab (orig);
1413           return DEBUG_TYPE_NULL;
1414         }
1415       ++*pp;
1416
1417       dtype = parse_stab_array_type (dhandle, info, pp, stringp);
1418       break;
1419
1420     case 'S':
1421       dtype = debug_make_set_type (dhandle,
1422                                    parse_stab_type (dhandle, info, pp,
1423                                                     (debug_type **) NULL),
1424                                    stringp);
1425       break;
1426
1427     default:
1428       bad_stab (orig);
1429       return DEBUG_TYPE_NULL;
1430     }
1431
1432   if (dtype == DEBUG_TYPE_NULL)
1433     return DEBUG_TYPE_NULL;
1434
1435   if (typenums[0] != -1)
1436     {
1437       if (! stab_record_type (dhandle, info, typenums, dtype))
1438         return DEBUG_TYPE_NULL;
1439     }
1440
1441   if (size != -1)
1442     {
1443       if (! debug_record_type_size (dhandle, dtype, (unsigned int) size))
1444         return false;
1445     }
1446
1447   return dtype;
1448 }
1449
1450 /* Read a number by which a type is referred to in dbx data, or
1451    perhaps read a pair (FILENUM, TYPENUM) in parentheses.  Just a
1452    single number N is equivalent to (0,N).  Return the two numbers by
1453    storing them in the vector TYPENUMS.  */
1454
1455 static boolean
1456 parse_stab_type_number (pp, typenums)
1457      const char **pp;
1458      int *typenums;
1459 {
1460   const char *orig;
1461
1462   orig = *pp;
1463
1464   if (**pp != '(')
1465     {
1466       typenums[0] = 0;
1467       typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1468     }
1469   else
1470     {
1471       ++*pp;
1472       typenums[0] = (int) parse_number (pp, (boolean *) NULL);
1473       if (**pp != ',')
1474         {
1475           bad_stab (orig);
1476           return false;
1477         }
1478       ++*pp;
1479       typenums[1] = (int) parse_number (pp, (boolean *) NULL);
1480       if (**pp != ')')
1481         {
1482           bad_stab (orig);
1483           return false;
1484         }
1485       ++*pp;
1486     }
1487
1488   return true;
1489 }
1490
1491 /* Parse a range type.  */
1492
1493 static debug_type
1494 parse_stab_range_type (dhandle, info, pp, typenums)
1495      PTR dhandle;
1496      struct stab_handle *info;
1497      const char **pp;
1498      const int *typenums;
1499 {
1500   const char *orig;
1501   int rangenums[2];
1502   boolean self_subrange;
1503   debug_type index_type;
1504   const char *s2, *s3;
1505   bfd_signed_vma n2, n3;
1506   boolean ov2, ov3;
1507
1508   orig = *pp;
1509
1510   index_type = DEBUG_TYPE_NULL;
1511
1512   /* First comes a type we are a subrange of.
1513      In C it is usually 0, 1 or the type being defined.  */
1514   if (! parse_stab_type_number (pp, rangenums))
1515     return DEBUG_TYPE_NULL;
1516
1517   self_subrange = (rangenums[0] == typenums[0]
1518                    && rangenums[1] == typenums[1]);
1519
1520   if (**pp == '=')
1521     {
1522       *pp = orig;
1523       index_type = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
1524       if (index_type == DEBUG_TYPE_NULL)
1525         return DEBUG_TYPE_NULL;
1526     }
1527
1528   if (**pp == ';')
1529     ++*pp;
1530
1531   /* The remaining two operands are usually lower and upper bounds of
1532      the range.  But in some special cases they mean something else.  */
1533   s2 = *pp;
1534   n2 = parse_number (pp, &ov2);
1535   if (**pp != ';')
1536     {
1537       bad_stab (orig);
1538       return DEBUG_TYPE_NULL;
1539     }
1540   ++*pp;
1541
1542   s3 = *pp;
1543   n3 = parse_number (pp, &ov3);
1544   if (**pp != ';')
1545     {
1546       bad_stab (orig);
1547       return DEBUG_TYPE_NULL;
1548     }
1549   ++*pp;
1550
1551   if (ov2 || ov3)
1552     {
1553       /* gcc will emit range stabs for long long types.  Handle this
1554          as a special case.  FIXME: This needs to be more general.  */
1555 #define LLLOW  "01000000000000000000000;"
1556 #define LLHIGH "0777777777777777777777;"
1557 #define ULLHIGH "01777777777777777777777;"
1558       if (index_type == DEBUG_TYPE_NULL)
1559         {
1560           if (strncmp (s2, LLLOW, sizeof LLLOW - 1) == 0
1561               && strncmp (s3, LLHIGH, sizeof LLHIGH - 1) == 0)
1562             return debug_make_int_type (dhandle, 8, false);
1563           if (! ov2
1564               && n2 == 0
1565               && strncmp (s3, ULLHIGH, sizeof ULLHIGH - 1) == 0)
1566             return debug_make_int_type (dhandle, 8, true);
1567         }
1568
1569       warn_stab (orig, "numeric overflow");
1570     }
1571
1572   if (index_type == DEBUG_TYPE_NULL)
1573     {
1574       /* A type defined as a subrange of itself, with both bounds 0,
1575          is void.  */
1576       if (self_subrange && n2 == 0 && n3 == 0)
1577         return debug_make_void_type (dhandle);
1578
1579       /* If n3 is zero and n2 is positive, this is a floating point
1580          type, and n2 is the number of bytes.  */
1581       if (n3 == 0 && n2 > 0)
1582         return debug_make_float_type (dhandle, n2);
1583
1584       /* If the upper bound is -1, this is an unsigned int.  */
1585       if (n2 == 0 && n3 == -1)
1586         {
1587           /* FIXME: The size here really depends upon the target.  */
1588           return debug_make_int_type (dhandle, 4, true);
1589         }
1590
1591       /* A range of 0 to 127 is char.  */
1592       if (self_subrange && n2 == 0 && n3 == 127)
1593         return debug_make_int_type (dhandle, 1, false);
1594
1595       /* FIXME: gdb checks for the language CHILL here.  */
1596
1597       if (n2 == 0)
1598         {
1599           if (n3 < 0)
1600             return debug_make_int_type (dhandle, - n3, true);
1601           else if (n3 == 0xff)
1602             return debug_make_int_type (dhandle, 1, true);
1603           else if (n3 == 0xffff)
1604             return debug_make_int_type (dhandle, 2, true);
1605           /* -1 is used for the upper bound of (4 byte) "unsigned int"
1606              and "unsigned long", and we already checked for that, so
1607              don't need to test for it here.  */
1608         }
1609       else if (n3 == 0
1610                && n2 < 0
1611                && (self_subrange || n2 == -8))
1612         return debug_make_int_type (dhandle, - n2, true);
1613       else if (n2 == - n3 - 1)
1614         {
1615           if (n3 == 0x7f)
1616             return debug_make_int_type (dhandle, 1, false);
1617           else if (n3 == 0x7fff)
1618             return debug_make_int_type (dhandle, 2, false);
1619           else if (n3 == 0x7fffffff)
1620             return debug_make_int_type (dhandle, 4, false);
1621         }
1622     }
1623
1624   /* At this point I don't have the faintest idea how to deal with a
1625      self_subrange type; I'm going to assume that this is used as an
1626      idiom, and that all of them are special cases.  So . . .  */
1627   if (self_subrange)
1628     {
1629       bad_stab (orig);
1630       return DEBUG_TYPE_NULL;
1631     }
1632
1633   index_type = stab_find_type (dhandle, info, rangenums);
1634   if (index_type == DEBUG_TYPE_NULL)
1635     {
1636       /* Does this actually ever happen?  Is that why we are worrying
1637          about dealing with it rather than just calling error_type?  */
1638       warn_stab (orig, "missing index type");
1639       index_type = debug_make_int_type (dhandle, 4, false);
1640     }
1641
1642   return debug_make_range_type (dhandle, index_type, n2, n3);
1643 }
1644
1645 /* Sun's ACC uses a somewhat saner method for specifying the builtin
1646    typedefs in every file (for int, long, etc):
1647
1648         type = b <signed> <width>; <offset>; <nbits>
1649         signed = u or s.  Possible c in addition to u or s (for char?).
1650         offset = offset from high order bit to start bit of type.
1651         width is # bytes in object of this type, nbits is # bits in type.
1652
1653    The width/offset stuff appears to be for small objects stored in
1654    larger ones (e.g. `shorts' in `int' registers).  We ignore it for now,
1655    FIXME.  */
1656
1657 static debug_type
1658 parse_stab_sun_builtin_type (dhandle, pp)
1659      PTR dhandle;
1660      const char **pp;
1661 {
1662   const char *orig;
1663   boolean unsignedp;
1664   bfd_vma bits;
1665
1666   orig = *pp;
1667
1668   switch (**pp)
1669     {
1670     case 's':
1671       unsignedp = false;
1672       break;
1673     case 'u':
1674       unsignedp = true;
1675       break;
1676     default:
1677       bad_stab (orig);
1678       return DEBUG_TYPE_NULL;
1679     }
1680   ++*pp;
1681
1682   /* For some odd reason, all forms of char put a c here.  This is strange
1683      because no other type has this honor.  We can safely ignore this because
1684      we actually determine 'char'acterness by the number of bits specified in
1685      the descriptor.  */
1686   if (**pp == 'c')
1687     ++*pp;
1688
1689   /* The first number appears to be the number of bytes occupied
1690      by this type, except that unsigned short is 4 instead of 2.
1691      Since this information is redundant with the third number,
1692      we will ignore it.  */
1693   (void) parse_number (pp, (boolean *) NULL);
1694   if (**pp != ';')
1695     {
1696       bad_stab (orig);
1697       return DEBUG_TYPE_NULL;
1698     }
1699   ++*pp;
1700
1701   /* The second number is always 0, so ignore it too. */
1702   (void) parse_number (pp, (boolean *) NULL);
1703   if (**pp != ';')
1704     {
1705       bad_stab (orig);
1706       return DEBUG_TYPE_NULL;
1707     }
1708   ++*pp;
1709
1710   /* The third number is the number of bits for this type. */
1711   bits = parse_number (pp, (boolean *) NULL);
1712
1713   /* The type *should* end with a semicolon.  If it are embedded
1714      in a larger type the semicolon may be the only way to know where
1715      the type ends.  If this type is at the end of the stabstring we
1716      can deal with the omitted semicolon (but we don't have to like
1717      it).  Don't bother to complain(), Sun's compiler omits the semicolon
1718      for "void".  */
1719   if (**pp == ';')
1720     ++*pp;
1721
1722   if (bits == 0)
1723     return debug_make_void_type (dhandle);
1724
1725   return debug_make_int_type (dhandle, bits / 8, unsignedp);
1726 }
1727
1728 /* Parse a builtin floating type generated by the Sun compiler.  */
1729
1730 static debug_type
1731 parse_stab_sun_floating_type (dhandle, pp)
1732      PTR dhandle;
1733      const char **pp;
1734 {
1735   const char *orig;
1736   bfd_vma details;
1737   bfd_vma bytes;
1738
1739   orig = *pp;
1740
1741   /* The first number has more details about the type, for example
1742      FN_COMPLEX.  */
1743   details = parse_number (pp, (boolean *) NULL);
1744   if (**pp != ';')
1745     {
1746       bad_stab (orig);
1747       return DEBUG_TYPE_NULL;
1748     }
1749
1750   /* The second number is the number of bytes occupied by this type */
1751   bytes = parse_number (pp, (boolean *) NULL);
1752   if (**pp != ';')
1753     {
1754       bad_stab (orig);
1755       return DEBUG_TYPE_NULL;
1756     }
1757
1758   if (details == NF_COMPLEX
1759       || details == NF_COMPLEX16
1760       || details == NF_COMPLEX32)
1761     return debug_make_complex_type (dhandle, bytes);
1762
1763   return debug_make_float_type (dhandle, bytes);      
1764 }
1765
1766 /* Handle an enum type.  */
1767
1768 static debug_type
1769 parse_stab_enum_type (dhandle, pp)
1770      PTR dhandle;
1771      const char **pp;
1772 {
1773   const char *orig;
1774   const char **names;
1775   bfd_signed_vma *values;
1776   unsigned int n;
1777   unsigned int alloc;
1778
1779   orig = *pp;
1780
1781   /* FIXME: gdb checks os9k_stabs here.  */
1782
1783   /* The aix4 compiler emits an extra field before the enum members;
1784      my guess is it's a type of some sort.  Just ignore it.  */
1785   if (**pp == '-')
1786     {
1787       while (**pp != ':')
1788         ++*pp;
1789       ++*pp;
1790     }
1791
1792   /* Read the value-names and their values.
1793      The input syntax is NAME:VALUE,NAME:VALUE, and so on.
1794      A semicolon or comma instead of a NAME means the end.  */
1795   alloc = 10;
1796   names = (const char **) xmalloc (alloc * sizeof *names);
1797   values = (bfd_signed_vma *) xmalloc (alloc * sizeof *values);
1798   n = 0;
1799   while (**pp != '\0' && **pp != ';' && **pp != ',')
1800     {
1801       const char *p;
1802       char *name;
1803       bfd_signed_vma val;
1804
1805       p = *pp;
1806       while (*p != ':')
1807         ++p;
1808
1809       name = savestring (*pp, p - *pp);
1810
1811       *pp = p + 1;
1812       val = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
1813       if (**pp != ',')
1814         {
1815           bad_stab (orig);
1816           return DEBUG_TYPE_NULL;
1817         }
1818       ++*pp;
1819
1820       if (n + 1 >= alloc)
1821         {
1822           alloc += 10;
1823           names = ((const char **)
1824                    xrealloc ((PTR) names, alloc * sizeof *names));
1825           values = ((bfd_signed_vma *)
1826                     xrealloc ((PTR) values, alloc * sizeof *values));
1827         }
1828
1829       names[n] = name;
1830       values[n] = val;
1831       ++n;
1832     }
1833
1834   names[n] = NULL;
1835   values[n] = 0;
1836
1837   if (**pp == ';')
1838     ++*pp;
1839
1840   return debug_make_enum_type (dhandle, names, values);
1841 }
1842
1843 /* Read the description of a structure (or union type) and return an object
1844    describing the type.
1845
1846    PP points to a character pointer that points to the next unconsumed token
1847    in the the stabs string.  For example, given stabs "A:T4=s4a:1,0,32;;",
1848    *PP will point to "4a:1,0,32;;".  */
1849
1850 static debug_type
1851 parse_stab_struct_type (dhandle, info, pp, structp, typenums)
1852      PTR dhandle;
1853      struct stab_handle *info;
1854      const char **pp;
1855      boolean structp;
1856      const int *typenums;
1857 {
1858   const char *orig;
1859   bfd_vma size;
1860   debug_baseclass *baseclasses;
1861   debug_field *fields;
1862   boolean statics;
1863   debug_method *methods;
1864   debug_type vptrbase;
1865   boolean ownvptr;
1866
1867   orig = *pp;
1868
1869   /* Get the size.  */
1870   size = parse_number (pp, (boolean *) NULL);
1871
1872   /* Get the other information.  */
1873   if (! parse_stab_baseclasses (dhandle, info, pp, &baseclasses)
1874       || ! parse_stab_struct_fields (dhandle, info, pp, &fields, &statics)
1875       || ! parse_stab_members (dhandle, info, pp, &methods)
1876       || ! parse_stab_tilde_field (dhandle, info, pp, typenums, &vptrbase,
1877                                    &ownvptr))
1878     return DEBUG_TYPE_NULL;
1879
1880   if (! statics
1881       && baseclasses == NULL
1882       && methods == NULL
1883       && vptrbase == DEBUG_TYPE_NULL
1884       && ! ownvptr)
1885     return debug_make_struct_type (dhandle, structp, size, fields);
1886
1887   return debug_make_object_type (dhandle, structp, size, fields, baseclasses,
1888                                  methods, vptrbase, ownvptr);
1889 }
1890
1891 /* The stabs for C++ derived classes contain baseclass information which
1892    is marked by a '!' character after the total size.  This function is
1893    called when we encounter the baseclass marker, and slurps up all the
1894    baseclass information.
1895
1896    Immediately following the '!' marker is the number of base classes that
1897    the class is derived from, followed by information for each base class.
1898    For each base class, there are two visibility specifiers, a bit offset
1899    to the base class information within the derived class, a reference to
1900    the type for the base class, and a terminating semicolon.
1901
1902    A typical example, with two base classes, would be "!2,020,19;0264,21;".
1903                                                        ^^ ^ ^ ^  ^ ^  ^
1904         Baseclass information marker __________________|| | | |  | |  |
1905         Number of baseclasses __________________________| | | |  | |  |
1906         Visibility specifiers (2) ________________________| | |  | |  |
1907         Offset in bits from start of class _________________| |  | |  |
1908         Type number for base class ___________________________|  | |  |
1909         Visibility specifiers (2) _______________________________| |  |
1910         Offset in bits from start of class ________________________|  |
1911         Type number of base class ____________________________________|
1912
1913   Return true for success, false for failure.  */
1914
1915 static boolean
1916 parse_stab_baseclasses (dhandle, info, pp, retp)
1917      PTR dhandle;
1918      struct stab_handle *info;
1919      const char **pp;
1920      debug_baseclass **retp;
1921 {
1922   const char *orig;
1923   unsigned int c, i;
1924   debug_baseclass *classes;
1925
1926   *retp = NULL;
1927
1928   orig = *pp;
1929
1930   if (**pp != '!')
1931     {
1932       /* No base classes.  */
1933       return true;
1934     }
1935   ++*pp;
1936
1937   c = (unsigned int) parse_number (pp, (boolean *) NULL);
1938
1939   if (**pp != ',')
1940     {
1941       bad_stab (orig);
1942       return false;
1943     }
1944   ++*pp;
1945
1946   classes = (debug_baseclass *) xmalloc ((c + 1) * sizeof (**retp));
1947
1948   for (i = 0; i < c; i++)
1949     {
1950       boolean virtual;
1951       enum debug_visibility visibility;
1952       bfd_vma bitpos;
1953       debug_type type;
1954
1955       switch (**pp)
1956         {
1957         case '0':
1958           virtual = false;
1959           break;
1960         case '1':
1961           virtual = true;
1962           break;
1963         default:
1964           warn_stab (orig, "unknown virtual character for baseclass");
1965           virtual = false;
1966           break;
1967         }
1968       ++*pp;
1969
1970       switch (**pp)
1971         {
1972         case '0':
1973           visibility = DEBUG_VISIBILITY_PRIVATE;
1974           break;
1975         case '1':
1976           visibility = DEBUG_VISIBILITY_PROTECTED;
1977           break;
1978         case '2':
1979           visibility = DEBUG_VISIBILITY_PUBLIC;
1980           break;
1981         default:
1982           warn_stab (orig, "unknown visibility character for baseclass");
1983           visibility = DEBUG_VISIBILITY_PUBLIC;
1984           break;
1985         }
1986       ++*pp;
1987
1988       /* The remaining value is the bit offset of the portion of the
1989          object corresponding to this baseclass.  Always zero in the
1990          absence of multiple inheritance.  */
1991       bitpos = parse_number (pp, (boolean *) NULL);
1992       if (**pp != ',')
1993         {
1994           bad_stab (orig);
1995           return false;
1996         }
1997       ++*pp;
1998
1999       type = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
2000       if (type == DEBUG_TYPE_NULL)
2001         return false;
2002
2003       classes[i] = debug_make_baseclass (dhandle, type, bitpos, virtual,
2004                                          visibility);
2005       if (classes[i] == DEBUG_BASECLASS_NULL)
2006         return false;
2007
2008       if (**pp != ';')
2009         return false;
2010       ++*pp;
2011     }
2012
2013   classes[i] = DEBUG_BASECLASS_NULL;
2014
2015   *retp = classes;
2016
2017   return true;
2018 }
2019
2020 /* Read struct or class data fields.  They have the form:
2021
2022         NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2023
2024    At the end, we see a semicolon instead of a field.
2025
2026    In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2027    a static field.
2028
2029    The optional VISIBILITY is one of:
2030
2031         '/0'    (VISIBILITY_PRIVATE)
2032         '/1'    (VISIBILITY_PROTECTED)
2033         '/2'    (VISIBILITY_PUBLIC)
2034         '/9'    (VISIBILITY_IGNORE)
2035
2036    or nothing, for C style fields with public visibility.
2037
2038    Returns 1 for success, 0 for failure.  */
2039
2040 static boolean
2041 parse_stab_struct_fields (dhandle, info, pp, retp, staticsp)
2042      PTR dhandle;
2043      struct stab_handle *info;
2044      const char **pp;
2045      debug_field **retp;
2046      boolean *staticsp;
2047 {
2048   const char *orig;
2049   const char *p;
2050   debug_field *fields;
2051   unsigned int c;
2052   unsigned int alloc;
2053
2054   *retp = NULL;
2055   *staticsp = false;
2056
2057   orig = *pp;
2058
2059   c = 0;
2060   alloc = 10;
2061   fields = (debug_field *) xmalloc (alloc * sizeof *fields);
2062   while (**pp != ';')
2063     {
2064       /* FIXME: gdb checks os9k_stabs here.  */
2065
2066       p = *pp;
2067
2068       /* Add 1 to c to leave room for NULL pointer at end.  */
2069       if (c + 1 >= alloc)
2070         {
2071           alloc += 10;
2072           fields = ((debug_field *)
2073                     xrealloc ((PTR) fields, alloc * sizeof *fields));
2074         }
2075
2076       /* If it starts with CPLUS_MARKER it is a special abbreviation,
2077          unless the CPLUS_MARKER is followed by an underscore, in
2078          which case it is just the name of an anonymous type, which we
2079          should handle like any other type name.  We accept either '$'
2080          or '.', because a field name can never contain one of these
2081          characters except as a CPLUS_MARKER.  */
2082
2083       if ((*p == '$' || *p == '.') && p[1] != '_')
2084         {
2085           ++*pp;
2086           if (! parse_stab_cpp_abbrev (dhandle, info, pp, fields + c))
2087             return false;
2088           ++c;
2089           continue;
2090         }
2091
2092       /* Look for the ':' that separates the field name from the field
2093          values.  Data members are delimited by a single ':', while member
2094          functions are delimited by a pair of ':'s.  When we hit the member
2095          functions (if any), terminate scan loop and return. */
2096
2097       p = strchr (p, ':');
2098       if (p == NULL)
2099         {
2100           bad_stab (orig);
2101           return false;
2102         }
2103
2104       if (p[1] == ':')
2105         break;
2106
2107       if (! parse_stab_one_struct_field (dhandle, info, pp, p, fields + c,
2108                                          staticsp))
2109         return false;
2110
2111       ++c;
2112     }
2113
2114   fields[c] = DEBUG_FIELD_NULL;
2115
2116   *retp = fields;
2117
2118   return true;
2119 }
2120
2121 /* Special GNU C++ name.  */
2122
2123 static boolean
2124 parse_stab_cpp_abbrev (dhandle, info, pp, retp)
2125      PTR dhandle;
2126      struct stab_handle *info;
2127      const char **pp;
2128      debug_field *retp;
2129 {
2130   const char *orig;
2131   int cpp_abbrev;
2132   debug_type context;
2133   const char *name;
2134   const char *typename;
2135   debug_type type;
2136   bfd_vma bitpos;
2137
2138   *retp = DEBUG_FIELD_NULL;
2139
2140   orig = *pp;
2141
2142   if (**pp != 'v')
2143     {
2144       bad_stab (*pp);
2145       return false;
2146     }
2147   ++*pp;
2148
2149   cpp_abbrev = **pp;
2150   ++*pp;
2151
2152   /* At this point, *pp points to something like "22:23=*22...", where
2153      the type number before the ':' is the "context" and everything
2154      after is a regular type definition.  Lookup the type, find it's
2155      name, and construct the field name.  */
2156
2157   context = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
2158   if (context == DEBUG_TYPE_NULL)
2159     return false;
2160
2161   switch (cpp_abbrev)
2162     {
2163     case 'f':
2164       /* $vf -- a virtual function table pointer.  */
2165       name = "_vptr$";
2166       break;
2167     case 'b':
2168       /* $vb -- a virtual bsomethingorother */
2169       typename = debug_get_type_name (dhandle, context);
2170       if (typename == NULL)
2171         {
2172           warn_stab (orig, "unnamed $vb type");
2173           typename = "FOO";
2174         }
2175       name = concat ("_vb$", typename, (const char *) NULL);
2176       break;
2177     default:
2178       warn_stab (orig, "unrecognized C++ abbreviation");
2179       name = "INVALID_CPLUSPLUS_ABBREV";
2180       break;
2181     }
2182
2183   if (**pp != ':')
2184     {
2185       bad_stab (orig);
2186       return false;
2187     }
2188   ++*pp;
2189
2190   type = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
2191   if (**pp != ',')
2192     {
2193       bad_stab (orig);
2194       return false;
2195     }
2196   ++*pp;
2197
2198   bitpos = parse_number (pp, (boolean *) NULL);
2199   if (**pp != ';')
2200     {
2201       bad_stab (orig);
2202       return false;
2203     }
2204   ++*pp;
2205
2206   *retp = debug_make_field (dhandle, name, type, bitpos, 0,
2207                             DEBUG_VISIBILITY_PRIVATE);
2208   if (*retp == DEBUG_FIELD_NULL)
2209     return false;
2210
2211   return true;
2212 }
2213
2214 /* Parse a single field in a struct or union.  */
2215
2216 static boolean
2217 parse_stab_one_struct_field (dhandle, info, pp, p, retp, staticsp)
2218      PTR dhandle;
2219      struct stab_handle *info;
2220      const char **pp;
2221      const char *p;
2222      debug_field *retp;
2223      boolean *staticsp;
2224 {
2225   const char *orig;
2226   char *name;
2227   enum debug_visibility visibility;
2228   debug_type type;
2229   bfd_vma bitpos;
2230   bfd_vma bitsize;
2231
2232   orig = *pp;
2233
2234   /* FIXME: gdb checks ARM_DEMANGLING here.  */
2235
2236   name = savestring (*pp, p - *pp);
2237
2238   *pp = p + 1;
2239
2240   if (**pp != '/')
2241     visibility = DEBUG_VISIBILITY_PUBLIC;
2242   else
2243     {
2244       ++*pp;
2245       switch (**pp)
2246         {
2247         case '0':
2248           visibility = DEBUG_VISIBILITY_PRIVATE;
2249           break;
2250         case '1':
2251           visibility = DEBUG_VISIBILITY_PROTECTED;
2252           break;
2253         case '2':
2254           visibility = DEBUG_VISIBILITY_PUBLIC;
2255           break;
2256         default:
2257           warn_stab (orig, "unknown visibility character for field");
2258           visibility = DEBUG_VISIBILITY_PUBLIC;
2259           break;
2260         }
2261       ++*pp;
2262     }
2263
2264   type = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
2265   if (type == DEBUG_TYPE_NULL)
2266     return false;
2267
2268   if (**pp == ':')
2269     {
2270       char *varname;
2271
2272       /* This is a static class member.  */
2273       ++*pp;
2274       p = strchr (*pp, ';');
2275       if (p == NULL)
2276         {
2277           bad_stab (orig);
2278           return false;
2279         }
2280
2281       varname = savestring (*pp, p - *pp);
2282
2283       *pp = p + 1;
2284
2285       *retp = debug_make_static_member (dhandle, name, type, varname,
2286                                         visibility);
2287       *staticsp = true;
2288
2289       return true;
2290     }
2291
2292   if (**pp != ',')
2293     {
2294       bad_stab (orig);
2295       return false;
2296     }
2297   ++*pp;
2298
2299   bitpos = parse_number (pp, (boolean *) NULL);
2300   if (**pp != ',')
2301     {
2302       bad_stab (orig);
2303       return false;
2304     }
2305   ++*pp;
2306
2307   bitsize = parse_number (pp, (boolean *) NULL);
2308   if (**pp != ';')
2309     {
2310       bad_stab (orig);
2311       return false;
2312     }
2313   ++*pp;
2314
2315   if (bitpos == 0 && bitsize == 0)
2316     {
2317       /* This can happen in two cases: (1) at least for gcc 2.4.5 or
2318          so, it is a field which has been optimized out.  The correct
2319          stab for this case is to use VISIBILITY_IGNORE, but that is a
2320          recent invention.  (2) It is a 0-size array.  For example
2321          union { int num; char str[0]; } foo.  Printing "<no value>"
2322          for str in "p foo" is OK, since foo.str (and thus foo.str[3])
2323          will continue to work, and a 0-size array as a whole doesn't
2324          have any contents to print.
2325
2326          I suspect this probably could also happen with gcc -gstabs
2327          (not -gstabs+) for static fields, and perhaps other C++
2328          extensions.  Hopefully few people use -gstabs with gdb, since
2329          it is intended for dbx compatibility.  */
2330       visibility = DEBUG_VISIBILITY_IGNORE;
2331     }
2332
2333   /* FIXME: gdb does some stuff here to mark fields as unpacked.  */
2334
2335   *retp = debug_make_field (dhandle, name, type, bitpos, bitsize, visibility);
2336
2337   return true;
2338 }
2339
2340 /* Read member function stabs info for C++ classes.  The form of each member
2341    function data is:
2342
2343         NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2344
2345    An example with two member functions is:
2346
2347         afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2348
2349    For the case of overloaded operators, the format is op$::*.funcs, where
2350    $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2351    name (such as `+=') and `.' marks the end of the operator name.  */
2352
2353 static boolean
2354 parse_stab_members (dhandle, info, pp, retp)
2355      PTR dhandle;
2356      struct stab_handle *info;
2357      const char **pp;
2358      debug_method **retp;
2359 {
2360   const char *orig;
2361   debug_method *methods;
2362   unsigned int c;
2363   unsigned int alloc;
2364
2365   *retp = NULL;
2366
2367   orig = *pp;
2368
2369   alloc = 0;
2370   methods = NULL;
2371   c = 0;
2372
2373   while (**pp != ';')
2374     {
2375       const char *p;
2376       char *name;
2377       debug_method_variant *variants;
2378       unsigned int cvars;
2379       unsigned int allocvars;
2380       debug_type look_ahead_type;
2381
2382       p = strchr (*pp, ':');
2383       if (p == NULL || p[1] != ':')
2384         break;
2385
2386       /* FIXME: Some systems use something other than '$' here.  */
2387       if ((*pp)[0] != 'o' || (*pp)[1] != 'p' || (*pp)[2] != '$')
2388         {
2389           name = savestring (*pp, p - *pp);
2390           *pp = p + 2;
2391         }
2392       else
2393         {
2394           /* This is a completely wierd case.  In order to stuff in the
2395              names that might contain colons (the usual name delimiter),
2396              Mike Tiemann defined a different name format which is
2397              signalled if the identifier is "op$".  In that case, the
2398              format is "op$::XXXX." where XXXX is the name.  This is
2399              used for names like "+" or "=".  YUUUUUUUK!  FIXME!  */
2400           *pp = p + 2;
2401           for (p = *pp; *p != '.' && *p != '\0'; p++)
2402             ;
2403           if (*p != '.')
2404             {
2405               bad_stab (orig);
2406               return false;
2407             }
2408           name = savestring (*pp, p - *pp);
2409           *pp = p + 1;
2410         }
2411
2412       allocvars = 10;
2413       variants = ((debug_method_variant *)
2414                   xmalloc (allocvars * sizeof *variants));
2415       cvars = 0;
2416
2417       look_ahead_type = DEBUG_TYPE_NULL;
2418
2419       do
2420         {
2421           debug_type type;
2422           char *argtypes;
2423           enum debug_visibility visibility;
2424           boolean constp, volatilep, staticp;
2425           bfd_vma voffset;
2426           debug_type context;
2427
2428           if (look_ahead_type != DEBUG_TYPE_NULL)
2429             {
2430               /* g++ version 1 kludge */
2431               type = look_ahead_type;
2432               look_ahead_type = DEBUG_TYPE_NULL;
2433             }
2434           else
2435             {
2436               type = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
2437               if (type == DEBUG_TYPE_NULL)
2438                 return false;
2439               if (**pp != ':')
2440                 {
2441                   bad_stab (orig);
2442                   return false;
2443                 }
2444             }
2445
2446           ++*pp;
2447           p = strchr (*pp, ';');
2448           if (p == NULL)
2449             {
2450               bad_stab (orig);
2451               return false;
2452             }
2453
2454           /* FIXME: gdb sets is_stub here.  */
2455
2456           argtypes = savestring (*pp, p - *pp);
2457           *pp = p + 1;
2458
2459           switch (**pp)
2460             {
2461             case '0':
2462               visibility = DEBUG_VISIBILITY_PRIVATE;
2463               break;
2464             case '1':
2465               visibility = DEBUG_VISIBILITY_PROTECTED;
2466               break;
2467             default:
2468               visibility = DEBUG_VISIBILITY_PUBLIC;
2469               break;
2470             }
2471           ++*pp;
2472
2473           constp = false;
2474           volatilep = false;
2475           switch (**pp)
2476             {
2477             case 'A':
2478               /* Normal function.  */
2479               ++*pp;
2480               break;
2481             case 'B':
2482               /* const member function.  */
2483               constp = true;
2484               ++*pp;
2485               break;
2486             case 'C':
2487               /* volatile member function.  */
2488               volatilep = true;
2489               ++*pp;
2490               break;
2491             case 'D':
2492               /* const volatile member function.  */
2493               constp = true;
2494               volatilep = true;
2495               ++*pp;
2496               break;
2497             case '*':
2498             case '?':
2499             case '.':
2500               /* File compiled with g++ version 1; no information.  */
2501               break;
2502             default:
2503               warn_stab (orig, "const/volatile indicator missing");
2504               break;
2505             }
2506
2507           staticp = false;
2508           switch (**pp)
2509             {
2510             case '*':
2511               /* virtual member function, followed by index.  The sign
2512                  bit is set to distinguish pointers-to-methods from
2513                  virtual function indicies.  Since the array is in
2514                  words, the quantity must be shifted left by 1 on 16
2515                  bit machine, and by 2 on 32 bit machine, forcing the
2516                  sign bit out, and usable as a valid index into the
2517                  array.  Remove the sign bit here.  */
2518               ++*pp;
2519               voffset = parse_number (pp, (boolean *) NULL);
2520               if (**pp != ';')
2521                 {
2522                   bad_stab (orig);
2523                   return false;
2524                 }
2525               ++*pp;
2526               voffset &= 0x7fffffff;
2527               voffset += 2;
2528
2529               if (**pp == ';' || *pp == '\0')
2530                 {
2531                   /* Must be g++ version 1.  */
2532                   context = DEBUG_TYPE_NULL;
2533                 }
2534               else
2535                 {
2536                   /* Figure out from whence this virtual function
2537                      came.  It may belong to virtual function table of
2538                      one of its baseclasses.  */
2539                     look_ahead_type = parse_stab_type (dhandle, info, pp,
2540                                                        (debug_type **) NULL);
2541                     if (**pp == ':')
2542                       {
2543                         /* g++ version 1 overloaded methods.  */
2544                       }
2545                     else
2546                       {
2547                         context = look_ahead_type;
2548                         look_ahead_type = DEBUG_TYPE_NULL;
2549                         if (**pp != ';')
2550                           {
2551                             bad_stab (orig);
2552                             return false;
2553                           }
2554                         ++*pp;
2555                       }
2556                   }
2557               break;
2558
2559             case '?':
2560               /* static member function.  */
2561               ++*pp;
2562               staticp = true;
2563               voffset = 0;
2564               /* FIXME: gdb sets is_stub here.  */
2565               context = DEBUG_TYPE_NULL;
2566               break;
2567
2568             default:
2569               warn_stab (orig, "member function type missing");
2570               voffset = 0;
2571               context = DEBUG_TYPE_NULL;
2572               break;
2573
2574             case '.':
2575               ++*pp;
2576               voffset = 0;
2577               context = DEBUG_TYPE_NULL;
2578               break;
2579             }
2580
2581           if (cvars + 1 >= allocvars)
2582             {
2583               allocvars += 10;
2584               variants = ((debug_method_variant *)
2585                           xrealloc ((PTR) variants,
2586                                     allocvars * sizeof *variants));
2587             }
2588
2589           if (! staticp)
2590             variants[cvars] = debug_make_method_variant (dhandle, argtypes,
2591                                                          type, visibility,
2592                                                          constp, volatilep,
2593                                                          voffset, context);
2594           else
2595             variants[cvars] = debug_make_static_method_variant (dhandle,
2596                                                                 argtypes,
2597                                                                 type,
2598                                                                 visibility,
2599                                                                 constp,
2600                                                                 volatilep);
2601           if (variants[cvars] == DEBUG_METHOD_VARIANT_NULL)
2602             return false;
2603
2604           ++cvars;
2605         }
2606       while (**pp != ';' && **pp != '\0');
2607
2608       variants[cvars] = DEBUG_METHOD_VARIANT_NULL;
2609
2610       if (**pp != '\0')
2611         ++*pp;
2612
2613       if (c + 1 >= alloc)
2614         {
2615           alloc += 10;
2616           methods = ((debug_method *)
2617                      xrealloc ((PTR) methods, alloc * sizeof *methods));
2618         }
2619
2620       methods[c] = debug_make_method (dhandle, name, variants);
2621
2622       ++c;
2623     }
2624
2625   if (methods != NULL)
2626     methods[c] = DEBUG_METHOD_NULL;
2627
2628   *retp = methods;
2629
2630   return true;
2631 }
2632
2633 /* The tail end of stabs for C++ classes that contain a virtual function
2634    pointer contains a tilde, a %, and a type number.
2635    The type number refers to the base class (possibly this class itself) which
2636    contains the vtable pointer for the current class.
2637
2638    This function is called when we have parsed all the method declarations,
2639    so we can look for the vptr base class info.  */
2640
2641 static boolean
2642 parse_stab_tilde_field (dhandle, info, pp, typenums, retvptrbase, retownvptr)
2643      PTR dhandle;
2644      struct stab_handle *info;
2645      const char **pp;
2646      const int *typenums;
2647      debug_type *retvptrbase;
2648      boolean *retownvptr;
2649 {
2650   const char *orig;
2651   const char *hold;
2652   int vtypenums[2];
2653
2654   *retvptrbase = DEBUG_TYPE_NULL;
2655   *retownvptr = false;
2656
2657   orig = *pp;
2658
2659   /* If we are positioned at a ';', then skip it. */
2660   if (**pp == ';')
2661     ++*pp;
2662
2663   if (**pp != '~')
2664     return true;
2665
2666   ++*pp;
2667
2668   if (**pp == '=' || **pp == '+' || **pp == '-')
2669     {
2670       /* Obsolete flags that used to indicate the presence of
2671          constructors and/or destructors. */
2672       ++*pp;
2673     }
2674
2675   if (**pp != '%')
2676     return true;
2677
2678   ++*pp;
2679
2680   hold = *pp;
2681
2682   /* The next number is the type number of the base class (possibly
2683      our own class) which supplies the vtable for this class.  */
2684   if (! parse_stab_type_number (pp, vtypenums))
2685     return false;
2686
2687   if (vtypenums[0] == typenums[0]
2688       && vtypenums[1] == typenums[1])
2689     *retownvptr = true;
2690   else
2691     {
2692       debug_type vtype;
2693       const char *p;
2694
2695       *pp = hold;
2696
2697       vtype = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
2698       for (p = *pp; *p != ';' && *p != '\0'; p++)
2699         ;
2700       if (*p != ';')
2701         {
2702           bad_stab (orig);
2703           return false;
2704         }
2705
2706       *retvptrbase = vtype;
2707
2708       *pp = p + 1;
2709     }
2710
2711   return true;    
2712 }
2713
2714 /* Read a definition of an array type.  */
2715
2716 static debug_type
2717 parse_stab_array_type (dhandle, info, pp, stringp)
2718      PTR dhandle;
2719      struct stab_handle *info;
2720      const char **pp;
2721      boolean stringp;
2722 {
2723   const char *orig;
2724   debug_type index_type;
2725   boolean adjustable;
2726   bfd_signed_vma lower, upper;
2727   debug_type element_type;
2728
2729   /* Format of an array type:
2730      "ar<index type>;lower;upper;<array_contents_type>".
2731      OS9000: "arlower,upper;<array_contents_type>".
2732
2733      Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
2734      for these, produce a type like float[][].  */
2735
2736   orig = *pp;
2737
2738   /* FIXME: gdb checks os9k_stabs here.  */
2739
2740   index_type = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
2741   if (**pp != ';')
2742     {
2743       bad_stab (orig);
2744       return DEBUG_TYPE_NULL;
2745     }
2746   ++*pp;
2747
2748   adjustable = false;
2749
2750   if (! isdigit ((unsigned char) **pp) && **pp != '-')
2751     {
2752       ++*pp;
2753       adjustable = true;
2754     }
2755
2756   lower = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
2757   if (**pp != ';')
2758     {
2759       bad_stab (orig);
2760       return false;
2761     }
2762   ++*pp;
2763
2764   if (! isdigit ((unsigned char) **pp) && **pp != '-')
2765     {
2766       ++*pp;
2767       adjustable = true;
2768     }
2769
2770   upper = (bfd_signed_vma) parse_number (pp, (boolean *) NULL);
2771   if (**pp != ';')
2772     {
2773       bad_stab (orig);
2774       return false;
2775     }
2776   ++*pp;
2777
2778   element_type = parse_stab_type (dhandle, info, pp, (debug_type **) NULL);
2779   if (element_type == DEBUG_TYPE_NULL)
2780     return false;
2781
2782   if (adjustable)
2783     {
2784       lower = 0;
2785       upper = -1;
2786     }
2787
2788   return debug_make_array_type (dhandle, element_type, index_type, lower,
2789                                 upper, stringp);
2790 }
2791
2792 /* Keep a stack of N_BINCL include files.  */
2793
2794 struct bincl_file
2795 {
2796   struct bincl_file *next;
2797   const char *name;
2798 };
2799
2800 /* Start a new N_BINCL file, pushing it onto the stack.  */
2801
2802 static void
2803 push_bincl (info, name)
2804      struct stab_handle *info;
2805      const char *name;
2806 {
2807   struct bincl_file *n;
2808
2809   n = (struct bincl_file *) xmalloc (sizeof *n);
2810   n->next = info->bincl_stack;
2811   n->name = name;
2812   info->bincl_stack = n;
2813
2814   ++info->files;
2815   info->file_types = ((struct stab_types **)
2816                       xrealloc ((PTR) info->file_types,
2817                                 (info->files
2818                                  * sizeof *info->file_types)));
2819   info->file_types[info->files - 1] = NULL;
2820 }
2821
2822 /* Finish an N_BINCL file, at an N_EINCL, popping the name off the
2823    stack.  */
2824
2825 static const char *
2826 pop_bincl (info)
2827      struct stab_handle *info;
2828 {
2829   struct bincl_file *o;
2830
2831   o = info->bincl_stack;
2832   if (o == NULL)
2833     return info->main_filename;
2834   info->bincl_stack = o->next;
2835   free (o);
2836   if (info->bincl_stack == NULL)
2837     return info->main_filename;
2838   return info->bincl_stack->name;
2839 }
2840
2841 /* Handle a variable definition.  gcc emits variable definitions for a
2842    block before the N_LBRAC, so we must hold onto them until we see
2843    it.  The SunPRO compiler emits variable definitions after the
2844    N_LBRAC, so we can call debug_record_variable immediately.  */
2845
2846 static boolean
2847 stab_record_variable (dhandle, info, name, type, kind, val)
2848      PTR dhandle;
2849      struct stab_handle *info;
2850      const char *name;
2851      debug_type type;
2852      enum debug_var_kind kind;
2853      bfd_vma val;
2854 {
2855   struct stab_pending_var *v;
2856
2857   if (! info->within_function
2858       || (info->gcc_compiled == 0 && info->n_opt_found))
2859     return debug_record_variable (dhandle, name, type, kind, val);
2860
2861   v = (struct stab_pending_var *) xmalloc (sizeof *v);
2862   memset (v, 0, sizeof *v);
2863
2864   v->next = info->pending;
2865   v->name = name;
2866   v->type = type;
2867   v->kind = kind;
2868   v->val = val;
2869   info->pending = v;
2870
2871   return true;
2872 }
2873
2874 /* Emit pending variable definitions.  This is called after we see the
2875    N_LBRAC that starts the block.  */
2876
2877 static boolean
2878 stab_emit_pending_vars (dhandle, info)
2879      PTR dhandle;
2880      struct stab_handle *info;
2881 {
2882   struct stab_pending_var *v;
2883
2884   v = info->pending;
2885   while (v != NULL)
2886     {
2887       struct stab_pending_var *next;
2888
2889       if (! debug_record_variable (dhandle, v->name, v->type, v->kind, v->val))
2890         return false;
2891
2892       next = v->next;
2893       free (v);
2894       v = next;
2895     }
2896
2897   info->pending = NULL;
2898
2899   return true;
2900 }
2901
2902 /* Find the slot for a type in the database.  */
2903
2904 static debug_type *
2905 stab_find_slot (info, typenums)
2906      struct stab_handle *info;
2907      const int *typenums;
2908 {
2909   int filenum;
2910   int index;
2911   struct stab_types **ps;
2912
2913   filenum = typenums[0];
2914   index = typenums[1];
2915
2916   if (filenum < 0 || (unsigned int) filenum >= info->files)
2917     {
2918       fprintf (stderr, "Type file number %d out of range\n", filenum);
2919       return NULL;
2920     }
2921   if (index < 0)
2922     {
2923       fprintf (stderr, "Type index number %d out of range\n", index);
2924       return NULL;
2925     }
2926
2927   ps = info->file_types + filenum;
2928
2929   while (index >= STAB_TYPES_SLOTS)
2930     {
2931       if (*ps == NULL)
2932         {
2933           *ps = (struct stab_types *) xmalloc (sizeof **ps);
2934           memset (*ps, 0, sizeof **ps);
2935         }
2936       ps = &(*ps)->next;
2937       index -= STAB_TYPES_SLOTS;
2938     }
2939   if (*ps == NULL)
2940     {
2941       *ps = (struct stab_types *) xmalloc (sizeof **ps);
2942       memset (*ps, 0, sizeof **ps);
2943     }
2944
2945   return (*ps)->types + index;
2946 }
2947
2948 /* Find a type given a type number.  If the type has not been
2949    allocated yet, create an indirect type.  */
2950
2951 static debug_type
2952 stab_find_type (dhandle, info, typenums)
2953      PTR dhandle;
2954      struct stab_handle *info;
2955      const int *typenums;
2956 {
2957   debug_type *slot;
2958
2959   if (typenums[0] == 0 && typenums[1] < 0)
2960     {
2961       /* A negative type number indicates an XCOFF builtin type.  */
2962       return stab_xcoff_builtin_type (dhandle, info, typenums[1]);
2963     }
2964
2965   slot = stab_find_slot (info, typenums);
2966   if (slot == NULL)
2967     return DEBUG_TYPE_NULL;
2968
2969   if (*slot == DEBUG_TYPE_NULL)
2970     return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
2971
2972   return *slot;
2973 }
2974
2975 /* Record that a given type number refers to a given type.  */
2976
2977 static boolean
2978 stab_record_type (dhandle, info, typenums, type)
2979      PTR dhandle;
2980      struct stab_handle *info;
2981      const int *typenums;
2982      debug_type type;
2983 {
2984   debug_type *slot;
2985
2986   slot = stab_find_slot (info, typenums);
2987   if (slot == NULL)
2988     return false;
2989
2990   /* gdb appears to ignore type redefinitions, so we do as well.  */
2991
2992   *slot = type;
2993
2994   return true;
2995 }
2996
2997 /* Return an XCOFF builtin type.  */
2998
2999 static debug_type
3000 stab_xcoff_builtin_type (dhandle, info, typenum)
3001      PTR dhandle;
3002      struct stab_handle *info;
3003      int typenum;
3004 {
3005   debug_type rettype;
3006   const char *name;
3007
3008   if (typenum >= 0 || typenum < -XCOFF_TYPE_COUNT)
3009     {
3010       fprintf (stderr, "Unrecognized XCOFF type %d\n", typenum);
3011       return DEBUG_TYPE_NULL;
3012     }
3013   if (info->xcoff_types[-typenum] != NULL)
3014     return info->xcoff_types[-typenum];
3015
3016   switch (-typenum)
3017     {
3018     case 1:
3019       /* The size of this and all the other types are fixed, defined
3020          by the debugging format.  */
3021       name = "int";
3022       rettype = debug_make_int_type (dhandle, 4, false);
3023       break;
3024     case 2:
3025       name = "char";
3026       rettype = debug_make_int_type (dhandle, 1, false);
3027       break;
3028     case 3:
3029       name = "short";
3030       rettype = debug_make_int_type (dhandle, 2, false);
3031       break;
3032     case 4:
3033       name = "long";
3034       rettype = debug_make_int_type (dhandle, 4, false);
3035       break;
3036     case 5:
3037       name = "unsigned char";
3038       rettype = debug_make_int_type (dhandle, 1, true);
3039       break;
3040     case 6:
3041       name = "signed char";
3042       rettype = debug_make_int_type (dhandle, 1, false);
3043       break;
3044     case 7:
3045       name = "unsigned short";
3046       rettype = debug_make_int_type (dhandle, 2, true);
3047       break;
3048     case 8:
3049       name = "unsigned int";
3050       rettype = debug_make_int_type (dhandle, 4, true);
3051       break;
3052     case 9:
3053       name = "unsigned";
3054       rettype = debug_make_int_type (dhandle, 4, true);
3055     case 10:
3056       name = "unsigned long";
3057       rettype = debug_make_int_type (dhandle, 4, true);
3058       break;
3059     case 11:
3060       name = "void";
3061       rettype = debug_make_void_type (dhandle);
3062       break;
3063     case 12:
3064       /* IEEE single precision (32 bit).  */
3065       name = "float";
3066       rettype = debug_make_float_type (dhandle, 4);
3067       break;
3068     case 13:
3069       /* IEEE double precision (64 bit).  */
3070       name = "double";
3071       rettype = debug_make_float_type (dhandle, 8);
3072       break;
3073     case 14:
3074       /* This is an IEEE double on the RS/6000, and different machines
3075          with different sizes for "long double" should use different
3076          negative type numbers.  See stabs.texinfo.  */
3077       name = "long double";
3078       rettype = debug_make_float_type (dhandle, 8);
3079       break;
3080     case 15:
3081       name = "integer";
3082       rettype = debug_make_int_type (dhandle, 4, false);
3083       break;
3084     case 16:
3085       name = "boolean";
3086       rettype = debug_make_bool_type (dhandle, 4);
3087       break;
3088     case 17:
3089       name = "short real";
3090       rettype = debug_make_float_type (dhandle, 4);
3091       break;
3092     case 18:
3093       name = "real";
3094       rettype = debug_make_float_type (dhandle, 8);
3095       break;
3096     case 19:
3097       /* FIXME */
3098       name = "stringptr";
3099       rettype = NULL;
3100       break;
3101     case 20:
3102       /* FIXME */
3103       name = "character";
3104       rettype = debug_make_int_type (dhandle, 1, true);
3105       break;
3106     case 21:
3107       name = "logical*1";
3108       rettype = debug_make_bool_type (dhandle, 1);
3109       break;
3110     case 22:
3111       name = "logical*2";
3112       rettype = debug_make_bool_type (dhandle, 2);
3113       break;
3114     case 23:
3115       name = "logical*4";
3116       rettype = debug_make_bool_type (dhandle, 4);
3117       break;
3118     case 24:
3119       name = "logical";
3120       rettype = debug_make_bool_type (dhandle, 4);
3121       break;
3122     case 25:
3123       /* Complex type consisting of two IEEE single precision values.  */
3124       name = "complex";
3125       rettype = debug_make_complex_type (dhandle, 8);
3126       break;
3127     case 26:
3128       /* Complex type consisting of two IEEE double precision values.  */
3129       name = "double complex";
3130       rettype = debug_make_complex_type (dhandle, 16);
3131       break;
3132     case 27:
3133       name = "integer*1";
3134       rettype = debug_make_int_type (dhandle, 1, false);
3135       break;
3136     case 28:
3137       name = "integer*2";
3138       rettype = debug_make_int_type (dhandle, 2, false);
3139       break;
3140     case 29:
3141       name = "integer*4";
3142       rettype = debug_make_int_type (dhandle, 4, false);
3143       break;
3144     case 30:
3145       /* FIXME */
3146       name = "wchar";
3147       rettype = debug_make_int_type (dhandle, 2, false);
3148       break;
3149     case 31:
3150       name = "long long";
3151       rettype = debug_make_int_type (dhandle, 8, false);
3152       break;
3153     case 32:
3154       name = "unsigned long long";
3155       rettype = debug_make_int_type (dhandle, 8, true);
3156       break;
3157     case 33:
3158       name = "logical*8";
3159       rettype = debug_make_bool_type (dhandle, 8);
3160       break;
3161     case 34:
3162       name = "integer*8";
3163       rettype = debug_make_int_type (dhandle, 8, false);
3164       break;
3165     default:
3166       abort ();
3167     }
3168
3169   rettype = debug_name_type (dhandle, name, rettype);
3170
3171   info->xcoff_types[-typenum] = rettype;
3172
3173   return rettype;
3174 }