Replace free() with xfree().
[external/binutils.git] / gdb / stabsread.c
1 /* Support routines for decoding "stabs" debugging information format.
2    Copyright 1986, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 1998
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
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,
20    Boston, MA 02111-1307, USA.  */
21
22 /* Support routines for reading and decoding debugging information in
23    the "stabs" format.  This format is used with many systems that use
24    the a.out object file format, as well as some systems that use
25    COFF or ELF where the stabs data is placed in a special section.
26    Avoid placing any object file format specific code in this file. */
27
28 #include "defs.h"
29 #include "gdb_string.h"
30 #include "bfd.h"
31 #include "obstack.h"
32 #include "symtab.h"
33 #include "gdbtypes.h"
34 #include "expression.h"
35 #include "symfile.h"
36 #include "objfiles.h"
37 #include "aout/stab_gnu.h"      /* We always use GNU stabs, not native */
38 #include "libaout.h"
39 #include "aout/aout64.h"
40 #include "gdb-stabs.h"
41 #include "buildsym.h"
42 #include "complaints.h"
43 #include "demangle.h"
44 #include "language.h"
45
46 #include <ctype.h>
47
48 /* Ask stabsread.h to define the vars it normally declares `extern'.  */
49 #define EXTERN
50 /**/
51 #include "stabsread.h"          /* Our own declarations */
52 #undef  EXTERN
53
54 extern void _initialize_stabsread (void);
55
56 /* The routines that read and process a complete stabs for a C struct or 
57    C++ class pass lists of data member fields and lists of member function
58    fields in an instance of a field_info structure, as defined below.
59    This is part of some reorganization of low level C++ support and is
60    expected to eventually go away... (FIXME) */
61
62 struct field_info
63   {
64     struct nextfield
65       {
66         struct nextfield *next;
67
68         /* This is the raw visibility from the stab.  It is not checked
69            for being one of the visibilities we recognize, so code which
70            examines this field better be able to deal.  */
71         int visibility;
72
73         struct field field;
74       }
75      *list;
76     struct next_fnfieldlist
77       {
78         struct next_fnfieldlist *next;
79         struct fn_fieldlist fn_fieldlist;
80       }
81      *fnlist;
82   };
83
84 static void
85 read_one_struct_field (struct field_info *, char **, char *,
86                        struct type *, struct objfile *);
87
88 static char *get_substring (char **, int);
89
90 static struct type *dbx_alloc_type (int[2], struct objfile *);
91
92 static long read_huge_number (char **, int, int *);
93
94 static struct type *error_type (char **, struct objfile *);
95
96 static void
97 patch_block_stabs (struct pending *, struct pending_stabs *,
98                    struct objfile *);
99
100 static void fix_common_block (struct symbol *, int);
101
102 static int read_type_number (char **, int *);
103
104 static struct type *read_range_type (char **, int[2], struct objfile *);
105
106 static struct type *read_sun_builtin_type (char **, int[2], struct objfile *);
107
108 static struct type *read_sun_floating_type (char **, int[2],
109                                             struct objfile *);
110
111 static struct type *read_enum_type (char **, struct type *, struct objfile *);
112
113 static struct type *rs6000_builtin_type (int);
114
115 static int
116 read_member_functions (struct field_info *, char **, struct type *,
117                        struct objfile *);
118
119 static int
120 read_struct_fields (struct field_info *, char **, struct type *,
121                     struct objfile *);
122
123 static int
124 read_baseclasses (struct field_info *, char **, struct type *,
125                   struct objfile *);
126
127 static int
128 read_tilde_fields (struct field_info *, char **, struct type *,
129                    struct objfile *);
130
131 static int attach_fn_fields_to_type (struct field_info *, struct type *);
132
133 static int
134 attach_fields_to_type (struct field_info *, struct type *, struct objfile *);
135
136 static struct type *read_struct_type (char **, struct type *,
137                                       struct objfile *);
138
139 static struct type *read_array_type (char **, struct type *,
140                                      struct objfile *);
141
142 static struct type **read_args (char **, int, struct objfile *);
143
144 static int
145 read_cpp_abbrev (struct field_info *, char **, struct type *,
146                  struct objfile *);
147
148 /* new functions added for cfront support */
149
150 static int
151 copy_cfront_struct_fields (struct field_info *, struct type *,
152                            struct objfile *);
153
154 static char *get_cfront_method_physname (char *);
155
156 static int
157 read_cfront_baseclasses (struct field_info *, char **,
158                          struct type *, struct objfile *);
159
160 static int
161 read_cfront_static_fields (struct field_info *, char **,
162                            struct type *, struct objfile *);
163 static int
164 read_cfront_member_functions (struct field_info *, char **,
165                               struct type *, struct objfile *);
166
167 /* end new functions added for cfront support */
168
169 static void
170 add_live_range (struct objfile *, struct symbol *, CORE_ADDR, CORE_ADDR);
171
172 static int resolve_live_range (struct objfile *, struct symbol *, char *);
173
174 static int process_reference (char **string);
175
176 static CORE_ADDR ref_search_value (int refnum);
177
178 static int
179 resolve_symbol_reference (struct objfile *, struct symbol *, char *);
180
181 void stabsread_clear_cache (void);
182
183 static const char vptr_name[] =
184 {'_', 'v', 'p', 't', 'r', CPLUS_MARKER, '\0'};
185 static const char vb_name[] =
186 {'_', 'v', 'b', CPLUS_MARKER, '\0'};
187
188 /* Define this as 1 if a pcc declaration of a char or short argument
189    gives the correct address.  Otherwise assume pcc gives the
190    address of the corresponding int, which is not the same on a
191    big-endian machine.  */
192
193 #if !defined (BELIEVE_PCC_PROMOTION)
194 #define BELIEVE_PCC_PROMOTION 0
195 #endif
196 #if !defined (BELIEVE_PCC_PROMOTION_TYPE)
197 #define BELIEVE_PCC_PROMOTION_TYPE 0
198 #endif
199
200 static struct complaint invalid_cpp_abbrev_complaint =
201 {"invalid C++ abbreviation `%s'", 0, 0};
202
203 static struct complaint invalid_cpp_type_complaint =
204 {"C++ abbreviated type name unknown at symtab pos %d", 0, 0};
205
206 static struct complaint member_fn_complaint =
207 {"member function type missing, got '%c'", 0, 0};
208
209 static struct complaint const_vol_complaint =
210 {"const/volatile indicator missing, got '%c'", 0, 0};
211
212 static struct complaint error_type_complaint =
213 {"debug info mismatch between compiler and debugger", 0, 0};
214
215 static struct complaint invalid_member_complaint =
216 {"invalid (minimal) member type data format at symtab pos %d.", 0, 0};
217
218 static struct complaint range_type_base_complaint =
219 {"base type %d of range type is not defined", 0, 0};
220
221 static struct complaint reg_value_complaint =
222 {"register number %d too large (max %d) in symbol %s", 0, 0};
223
224 static struct complaint vtbl_notfound_complaint =
225 {"virtual function table pointer not found when defining class `%s'", 0, 0};
226
227 static struct complaint unrecognized_cplus_name_complaint =
228 {"Unknown C++ symbol name `%s'", 0, 0};
229
230 static struct complaint rs6000_builtin_complaint =
231 {"Unknown builtin type %d", 0, 0};
232
233 static struct complaint unresolved_sym_chain_complaint =
234 {"%s: common block `%s' from global_sym_chain unresolved", 0, 0};
235
236 static struct complaint stabs_general_complaint =
237 {"%s", 0, 0};
238
239 static struct complaint lrs_general_complaint =
240 {"%s", 0, 0};
241
242 /* Make a list of forward references which haven't been defined.  */
243
244 static struct type **undef_types;
245 static int undef_types_allocated;
246 static int undef_types_length;
247 static struct symbol *current_symbol = NULL;
248
249 /* Check for and handle cretinous stabs symbol name continuation!  */
250 #define STABS_CONTINUE(pp,objfile)                              \
251   do {                                                  \
252     if (**(pp) == '\\' || (**(pp) == '?' && (*(pp))[1] == '\0')) \
253       *(pp) = next_symbol_text (objfile);       \
254   } while (0)
255 \f
256 /* FIXME: These probably should be our own types (like rs6000_builtin_type
257    has its own types) rather than builtin_type_*.  */
258 static struct type **os9k_type_vector[] =
259 {
260   0,
261   &builtin_type_int,
262   &builtin_type_char,
263   &builtin_type_long,
264   &builtin_type_short,
265   &builtin_type_unsigned_char,
266   &builtin_type_unsigned_short,
267   &builtin_type_unsigned_long,
268   &builtin_type_unsigned_int,
269   &builtin_type_float,
270   &builtin_type_double,
271   &builtin_type_void,
272   &builtin_type_long_double
273 };
274
275 static void os9k_init_type_vector (struct type **);
276
277 static void
278 os9k_init_type_vector (struct type **tv)
279 {
280   unsigned int i;
281   for (i = 0; i < sizeof (os9k_type_vector) / sizeof (struct type **); i++)
282     tv[i] = (os9k_type_vector[i] == 0 ? 0 : *(os9k_type_vector[i]));
283 }
284
285 /* Look up a dbx type-number pair.  Return the address of the slot
286    where the type for that number-pair is stored.
287    The number-pair is in TYPENUMS.
288
289    This can be used for finding the type associated with that pair
290    or for associating a new type with the pair.  */
291
292 struct type **
293 dbx_lookup_type (int typenums[2])
294 {
295   register int filenum = typenums[0];
296   register int index = typenums[1];
297   unsigned old_len;
298   register int real_filenum;
299   register struct header_file *f;
300   int f_orig_length;
301
302   if (filenum == -1)            /* -1,-1 is for temporary types.  */
303     return 0;
304
305   if (filenum < 0 || filenum >= n_this_object_header_files)
306     {
307       static struct complaint msg =
308       {"\
309 Invalid symbol data: type number (%d,%d) out of range at symtab pos %d.",
310        0, 0};
311       complain (&msg, filenum, index, symnum);
312       goto error_return;
313     }
314
315   if (filenum == 0)
316     {
317       if (index < 0)
318         {
319           /* Caller wants address of address of type.  We think
320              that negative (rs6k builtin) types will never appear as
321              "lvalues", (nor should they), so we stuff the real type
322              pointer into a temp, and return its address.  If referenced,
323              this will do the right thing.  */
324           static struct type *temp_type;
325
326           temp_type = rs6000_builtin_type (index);
327           return &temp_type;
328         }
329
330       /* Type is defined outside of header files.
331          Find it in this object file's type vector.  */
332       if (index >= type_vector_length)
333         {
334           old_len = type_vector_length;
335           if (old_len == 0)
336             {
337               type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
338               type_vector = (struct type **)
339                 xmalloc (type_vector_length * sizeof (struct type *));
340             }
341           while (index >= type_vector_length)
342             {
343               type_vector_length *= 2;
344             }
345           type_vector = (struct type **)
346             xrealloc ((char *) type_vector,
347                       (type_vector_length * sizeof (struct type *)));
348           memset (&type_vector[old_len], 0,
349                   (type_vector_length - old_len) * sizeof (struct type *));
350
351           if (os9k_stabs)
352             /* Deal with OS9000 fundamental types.  */
353             os9k_init_type_vector (type_vector);
354         }
355       return (&type_vector[index]);
356     }
357   else
358     {
359       real_filenum = this_object_header_files[filenum];
360
361       if (real_filenum >= N_HEADER_FILES (current_objfile))
362         {
363           struct type *temp_type;
364           struct type **temp_type_p;
365
366           warning ("GDB internal error: bad real_filenum");
367
368         error_return:
369           temp_type = init_type (TYPE_CODE_ERROR, 0, 0, NULL, NULL);
370           temp_type_p = (struct type **) xmalloc (sizeof (struct type *));
371           *temp_type_p = temp_type;
372           return temp_type_p;
373         }
374
375       f = HEADER_FILES (current_objfile) + real_filenum;
376
377       f_orig_length = f->length;
378       if (index >= f_orig_length)
379         {
380           while (index >= f->length)
381             {
382               f->length *= 2;
383             }
384           f->vector = (struct type **)
385             xrealloc ((char *) f->vector, f->length * sizeof (struct type *));
386           memset (&f->vector[f_orig_length], 0,
387                   (f->length - f_orig_length) * sizeof (struct type *));
388         }
389       return (&f->vector[index]);
390     }
391 }
392
393 /* Make sure there is a type allocated for type numbers TYPENUMS
394    and return the type object.
395    This can create an empty (zeroed) type object.
396    TYPENUMS may be (-1, -1) to return a new type object that is not
397    put into the type vector, and so may not be referred to by number. */
398
399 static struct type *
400 dbx_alloc_type (int typenums[2], struct objfile *objfile)
401 {
402   register struct type **type_addr;
403
404   if (typenums[0] == -1)
405     {
406       return (alloc_type (objfile));
407     }
408
409   type_addr = dbx_lookup_type (typenums);
410
411   /* If we are referring to a type not known at all yet,
412      allocate an empty type for it.
413      We will fill it in later if we find out how.  */
414   if (*type_addr == 0)
415     {
416       *type_addr = alloc_type (objfile);
417     }
418
419   return (*type_addr);
420 }
421
422 /* for all the stabs in a given stab vector, build appropriate types 
423    and fix their symbols in given symbol vector. */
424
425 static void
426 patch_block_stabs (struct pending *symbols, struct pending_stabs *stabs,
427                    struct objfile *objfile)
428 {
429   int ii;
430   char *name;
431   char *pp;
432   struct symbol *sym;
433
434   if (stabs)
435     {
436
437       /* for all the stab entries, find their corresponding symbols and 
438          patch their types! */
439
440       for (ii = 0; ii < stabs->count; ++ii)
441         {
442           name = stabs->stab[ii];
443           pp = (char *) strchr (name, ':');
444           while (pp[1] == ':')
445             {
446               pp += 2;
447               pp = (char *) strchr (pp, ':');
448             }
449           sym = find_symbol_in_list (symbols, name, pp - name);
450           if (!sym)
451             {
452               /* FIXME-maybe: it would be nice if we noticed whether
453                  the variable was defined *anywhere*, not just whether
454                  it is defined in this compilation unit.  But neither
455                  xlc or GCC seem to need such a definition, and until
456                  we do psymtabs (so that the minimal symbols from all
457                  compilation units are available now), I'm not sure
458                  how to get the information.  */
459
460               /* On xcoff, if a global is defined and never referenced,
461                  ld will remove it from the executable.  There is then
462                  a N_GSYM stab for it, but no regular (C_EXT) symbol.  */
463               sym = (struct symbol *)
464                 obstack_alloc (&objfile->symbol_obstack,
465                                sizeof (struct symbol));
466
467               memset (sym, 0, sizeof (struct symbol));
468               SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
469               SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
470               SYMBOL_NAME (sym) =
471                 obsavestring (name, pp - name, &objfile->symbol_obstack);
472               pp += 2;
473               if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
474                 {
475                   /* I don't think the linker does this with functions,
476                      so as far as I know this is never executed.
477                      But it doesn't hurt to check.  */
478                   SYMBOL_TYPE (sym) =
479                     lookup_function_type (read_type (&pp, objfile));
480                 }
481               else
482                 {
483                   SYMBOL_TYPE (sym) = read_type (&pp, objfile);
484                 }
485               add_symbol_to_list (sym, &global_symbols);
486             }
487           else
488             {
489               pp += 2;
490               if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
491                 {
492                   SYMBOL_TYPE (sym) =
493                     lookup_function_type (read_type (&pp, objfile));
494                 }
495               else
496                 {
497                   SYMBOL_TYPE (sym) = read_type (&pp, objfile);
498                 }
499             }
500         }
501     }
502 }
503 \f
504
505 /* Read a number by which a type is referred to in dbx data,
506    or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
507    Just a single number N is equivalent to (0,N).
508    Return the two numbers by storing them in the vector TYPENUMS.
509    TYPENUMS will then be used as an argument to dbx_lookup_type.
510
511    Returns 0 for success, -1 for error.  */
512
513 static int
514 read_type_number (register char **pp, register int *typenums)
515 {
516   int nbits;
517   if (**pp == '(')
518     {
519       (*pp)++;
520       typenums[0] = read_huge_number (pp, ',', &nbits);
521       if (nbits != 0)
522         return -1;
523       typenums[1] = read_huge_number (pp, ')', &nbits);
524       if (nbits != 0)
525         return -1;
526     }
527   else
528     {
529       typenums[0] = 0;
530       typenums[1] = read_huge_number (pp, 0, &nbits);
531       if (nbits != 0)
532         return -1;
533     }
534   return 0;
535 }
536 \f
537
538 #define VISIBILITY_PRIVATE      '0'     /* Stabs character for private field */
539 #define VISIBILITY_PROTECTED    '1'     /* Stabs character for protected fld */
540 #define VISIBILITY_PUBLIC       '2'     /* Stabs character for public field */
541 #define VISIBILITY_IGNORE       '9'     /* Optimized out or zero length */
542
543 #define CFRONT_VISIBILITY_PRIVATE       '2'     /* Stabs character for private field */
544 #define CFRONT_VISIBILITY_PUBLIC        '1'     /* Stabs character for public field */
545
546 /* This code added to support parsing of ARM/Cfront stabs strings */
547
548 /* Get substring from string up to char c, advance string pointer past
549    suibstring. */
550
551 static char *
552 get_substring (char **p, int c)
553 {
554   char *str;
555   str = *p;
556   *p = strchr (*p, c);
557   if (*p)
558     {
559       **p = 0;
560       (*p)++;
561     }
562   else
563     str = 0;
564   return str;
565 }
566
567 /* Physname gets strcat'd onto sname in order to recreate the mangled
568    name (see funtion gdb_mangle_name in gdbtypes.c).  For cfront, make
569    the physname look like that of g++ - take out the initial mangling
570    eg: for sname="a" and fname="foo__1aFPFs_i" return "FPFs_i" */
571
572 static char *
573 get_cfront_method_physname (char *fname)
574 {
575   int len = 0;
576   /* FIXME would like to make this generic for g++ too, but 
577      that is already handled in read_member_funcctions */
578   char *p = fname;
579
580   /* search ahead to find the start of the mangled suffix */
581   if (*p == '_' && *(p + 1) == '_')     /* compiler generated; probably a ctor/dtor */
582     p += 2;
583   while (p && (unsigned) ((p + 1) - fname) < strlen (fname) && *(p + 1) != '_')
584     p = strchr (p, '_');
585   if (!(p && *p == '_' && *(p + 1) == '_'))
586     error ("Invalid mangled function name %s", fname);
587   p += 2;                       /* advance past '__' */
588
589   /* struct name length and name of type should come next; advance past it */
590   while (isdigit (*p))
591     {
592       len = len * 10 + (*p - '0');
593       p++;
594     }
595   p += len;
596
597   return p;
598 }
599
600 /* Read base classes within cfront class definition.
601    eg: A:ZcA;1@Bpub v2@Bvirpri;__ct__1AFv func__1AFv *sfunc__1AFv ;as__1A ;;
602    ^^^^^^^^^^^^^^^^^^
603
604    A:ZcA;;foopri__1AFv foopro__1AFv __ct__1AFv __ct__1AFRC1A foopub__1AFv ;;;
605    ^
606  */
607
608 static int
609 read_cfront_baseclasses (struct field_info *fip, char **pp, struct type *type,
610                          struct objfile *objfile)
611 {
612   static struct complaint msg_unknown =
613   {"\
614          Unsupported token in stabs string %s.\n",
615    0, 0};
616   static struct complaint msg_notfound =
617   {"\
618                    Unable to find base type for %s.\n",
619    0, 0};
620   int bnum = 0;
621   char *p;
622   int i;
623   struct nextfield *new;
624
625   if (**pp == ';')              /* no base classes; return */
626     {
627       ++(*pp);
628       return 1;
629     }
630
631   /* first count base classes so we can allocate space before parsing */
632   for (p = *pp; p && *p && *p != ';'; p++)
633     {
634       if (*p == ' ')
635         bnum++;
636     }
637   bnum++;                       /* add one more for last one */
638
639   /* now parse the base classes until we get to the start of the methods 
640      (code extracted and munged from read_baseclasses) */
641   ALLOCATE_CPLUS_STRUCT_TYPE (type);
642   TYPE_N_BASECLASSES (type) = bnum;
643
644   /* allocate space */
645   {
646     int num_bytes = B_BYTES (TYPE_N_BASECLASSES (type));
647     char *pointer;
648
649     pointer = (char *) TYPE_ALLOC (type, num_bytes);
650     TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
651   }
652   B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
653
654   for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
655     {
656       new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
657       make_cleanup (xfree, new);
658       memset (new, 0, sizeof (struct nextfield));
659       new->next = fip->list;
660       fip->list = new;
661       FIELD_BITSIZE (new->field) = 0;   /* this should be an unpacked field! */
662
663       STABS_CONTINUE (pp, objfile);
664
665       /* virtual?  eg: v2@Bvir */
666       if (**pp == 'v')
667         {
668           SET_TYPE_FIELD_VIRTUAL (type, i);
669           ++(*pp);
670         }
671
672       /* access?  eg: 2@Bvir */
673       /* Note: protected inheritance not supported in cfront */
674       switch (*(*pp)++)
675         {
676         case CFRONT_VISIBILITY_PRIVATE:
677           new->visibility = VISIBILITY_PRIVATE;
678           break;
679         case CFRONT_VISIBILITY_PUBLIC:
680           new->visibility = VISIBILITY_PUBLIC;
681           break;
682         default:
683           /* Bad visibility format.  Complain and treat it as
684              public.  */
685           {
686             static struct complaint msg =
687             {
688               "Unknown visibility `%c' for baseclass", 0, 0};
689             complain (&msg, new->visibility);
690             new->visibility = VISIBILITY_PUBLIC;
691           }
692         }
693
694       /* "@" comes next - eg: @Bvir */
695       if (**pp != '@')
696         {
697           complain (&msg_unknown, *pp);
698           return 1;
699         }
700       ++(*pp);
701
702
703       /* Set the bit offset of the portion of the object corresponding 
704          to this baseclass.  Always zero in the absence of
705          multiple inheritance.  */
706       /* Unable to read bit position from stabs;
707          Assuming no multiple inheritance for now FIXME! */
708       /* We may have read this in the structure definition;
709          now we should fixup the members to be the actual base classes */
710       FIELD_BITPOS (new->field) = 0;
711
712       /* Get the base class name and type */
713       {
714         char *bname;            /* base class name */
715         struct symbol *bsym;    /* base class */
716         char *p1, *p2;
717         p1 = strchr (*pp, ' ');
718         p2 = strchr (*pp, ';');
719         if (p1 < p2)
720           bname = get_substring (pp, ' ');
721         else
722           bname = get_substring (pp, ';');
723         if (!bname || !*bname)
724           {
725             complain (&msg_unknown, *pp);
726             return 1;
727           }
728         /* FIXME! attach base info to type */
729         bsym = lookup_symbol (bname, 0, STRUCT_NAMESPACE, 0, 0);        /*demangled_name */
730         if (bsym)
731           {
732             new->field.type = SYMBOL_TYPE (bsym);
733             new->field.name = type_name_no_tag (new->field.type);
734           }
735         else
736           {
737             complain (&msg_notfound, *pp);
738             return 1;
739           }
740       }
741
742       /* If more base classes to parse, loop again.
743          We ate the last ' ' or ';' in get_substring,
744          so on exit we will have skipped the trailing ';' */
745       /* if invalid, return 0; add code to detect  - FIXME! */
746     }
747   return 1;
748 }
749
750 /* read cfront member functions.
751    pp points to string starting with list of functions
752    eg: A:ZcA;1@Bpub v2@Bvirpri;__ct__1AFv func__1AFv *sfunc__1AFv ;as__1A ;;
753    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
754    A:ZcA;;foopri__1AFv foopro__1AFv __ct__1AFv __ct__1AFRC1A foopub__1AFv ;;;
755    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
756  */
757
758 static int
759 read_cfront_member_functions (struct field_info *fip, char **pp,
760                               struct type *type, struct objfile *objfile)
761 {
762   /* This code extracted from read_member_functions 
763      so as to do the similar thing for our funcs */
764
765   int nfn_fields = 0;
766   int length = 0;
767   /* Total number of member functions defined in this class.  If the class
768      defines two `f' functions, and one `g' function, then this will have
769      the value 3.  */
770   int total_length = 0;
771   int i;
772   struct next_fnfield
773     {
774       struct next_fnfield *next;
775       struct fn_field fn_field;
776     }
777    *sublist;
778   struct type *look_ahead_type;
779   struct next_fnfieldlist *new_fnlist;
780   struct next_fnfield *new_sublist;
781   char *main_fn_name;
782   char *fname;
783   struct symbol *ref_func = 0;
784
785   /* Process each list until we find the end of the member functions.
786      eg: p = "__ct__1AFv foo__1AFv ;;;" */
787
788   STABS_CONTINUE (pp, objfile); /* handle \\ */
789
790   while (**pp != ';' && (fname = get_substring (pp, ' '), fname))
791     {
792       int is_static = 0;
793       int sublist_count = 0;
794       char *pname;
795       if (fname[0] == '*')      /* static member */
796         {
797           is_static = 1;
798           sublist_count++;
799           fname++;
800         }
801       ref_func = lookup_symbol (fname, 0, VAR_NAMESPACE, 0, 0);         /* demangled name */
802       if (!ref_func)
803         {
804           static struct complaint msg =
805           {"\
806                 Unable to find function symbol for %s\n",
807            0, 0};
808           complain (&msg, fname);
809           continue;
810         }
811       sublist = NULL;
812       look_ahead_type = NULL;
813       length = 0;
814
815       new_fnlist = (struct next_fnfieldlist *)
816         xmalloc (sizeof (struct next_fnfieldlist));
817       make_cleanup (xfree, new_fnlist);
818       memset (new_fnlist, 0, sizeof (struct next_fnfieldlist));
819
820       /* The following is code to work around cfront generated stabs.
821          The stabs contains full mangled name for each field.
822          We try to demangle the name and extract the field name out of it.  */
823       {
824         char *dem, *dem_p, *dem_args;
825         int dem_len;
826         dem = cplus_demangle (fname, DMGL_ANSI | DMGL_PARAMS);
827         if (dem != NULL)
828           {
829             dem_p = strrchr (dem, ':');
830             if (dem_p != 0 && *(dem_p - 1) == ':')
831               dem_p++;
832             /* get rid of args */
833             dem_args = strchr (dem_p, '(');
834             if (dem_args == NULL)
835               dem_len = strlen (dem_p);
836             else
837               dem_len = dem_args - dem_p;
838             main_fn_name =
839               obsavestring (dem_p, dem_len, &objfile->type_obstack);
840           }
841         else
842           {
843             main_fn_name =
844               obsavestring (fname, strlen (fname), &objfile->type_obstack);
845           }
846       }                         /* end of code for cfront work around */
847
848       new_fnlist->fn_fieldlist.name = main_fn_name;
849
850 /*-------------------------------------------------*/
851       /* Set up the sublists
852          Sublists are stuff like args, static, visibility, etc.
853          so in ARM, we have to set that info some other way.
854          Multiple sublists happen if overloading
855          eg: foo::26=##1;:;2A.;
856          In g++, we'd loop here thru all the sublists...  */
857
858       new_sublist =
859         (struct next_fnfield *) xmalloc (sizeof (struct next_fnfield));
860       make_cleanup (xfree, new_sublist);
861       memset (new_sublist, 0, sizeof (struct next_fnfield));
862
863       /* eat 1; from :;2A.; */
864       new_sublist->fn_field.type = SYMBOL_TYPE (ref_func);      /* normally takes a read_type */
865       /* Make this type look like a method stub for gdb */
866       TYPE_FLAGS (new_sublist->fn_field.type) |= TYPE_FLAG_STUB;
867       TYPE_CODE (new_sublist->fn_field.type) = TYPE_CODE_METHOD;
868
869       /* If this is just a stub, then we don't have the real name here. */
870       if (TYPE_FLAGS (new_sublist->fn_field.type) & TYPE_FLAG_STUB)
871         {
872           if (!TYPE_DOMAIN_TYPE (new_sublist->fn_field.type))
873             TYPE_DOMAIN_TYPE (new_sublist->fn_field.type) = type;
874           new_sublist->fn_field.is_stub = 1;
875         }
876
877       /* physname used later in mangling; eg PFs_i,5 for foo__1aFPFs_i 
878          physname gets strcat'd in order to recreate the onto mangled name */
879       pname = get_cfront_method_physname (fname);
880       new_sublist->fn_field.physname = savestring (pname, strlen (pname));
881
882
883       /* Set this member function's visibility fields. 
884          Unable to distinguish access from stabs definition!
885          Assuming public for now.  FIXME!
886          (for private, set new_sublist->fn_field.is_private = 1,
887          for public, set new_sublist->fn_field.is_protected = 1) */
888
889       /* Unable to distinguish const/volatile from stabs definition!
890          Assuming normal for now.  FIXME! */
891
892       new_sublist->fn_field.is_const = 0;
893       new_sublist->fn_field.is_volatile = 0;    /* volatile not implemented in cfront */
894
895       /* Set virtual/static function info
896          How to get vtable offsets ? 
897          Assuming normal for now FIXME!! 
898          For vtables, figure out from whence this virtual function came.
899          It may belong to virtual function table of
900          one of its baseclasses.
901          set:
902          new_sublist -> fn_field.voffset = vtable offset,
903          new_sublist -> fn_field.fcontext = look_ahead_type;
904          where look_ahead_type is type of baseclass */
905       if (is_static)
906         new_sublist->fn_field.voffset = VOFFSET_STATIC;
907       else                      /* normal member function.  */
908         new_sublist->fn_field.voffset = 0;
909       new_sublist->fn_field.fcontext = 0;
910
911
912       /* Prepare new sublist */
913       new_sublist->next = sublist;
914       sublist = new_sublist;
915       length++;
916
917       /* In g++, we loop thu sublists - now we set from functions. */
918       new_fnlist->fn_fieldlist.fn_fields = (struct fn_field *)
919         obstack_alloc (&objfile->type_obstack,
920                        sizeof (struct fn_field) * length);
921       memset (new_fnlist->fn_fieldlist.fn_fields, 0,
922               sizeof (struct fn_field) * length);
923       for (i = length; (i--, sublist); sublist = sublist->next)
924         {
925           new_fnlist->fn_fieldlist.fn_fields[i] = sublist->fn_field;
926         }
927
928       new_fnlist->fn_fieldlist.length = length;
929       new_fnlist->next = fip->fnlist;
930       fip->fnlist = new_fnlist;
931       nfn_fields++;
932       total_length += length;
933       STABS_CONTINUE (pp, objfile);     /* handle \\ */
934     }                           /* end of loop */
935
936   if (nfn_fields)
937     {
938       /* type should already have space */
939       TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
940         TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * nfn_fields);
941       memset (TYPE_FN_FIELDLISTS (type), 0,
942               sizeof (struct fn_fieldlist) * nfn_fields);
943       TYPE_NFN_FIELDS (type) = nfn_fields;
944       TYPE_NFN_FIELDS_TOTAL (type) = total_length;
945     }
946
947   /* end of scope for reading member func */
948
949   /* eg: ";;" */
950
951   /* Skip trailing ';' and bump count of number of fields seen */
952   if (**pp == ';')
953     (*pp)++;
954   else
955     return 0;
956   return 1;
957 }
958
959 /* This routine fixes up partial cfront types that were created
960    while parsing the stabs.  The main need for this function is
961    to add information such as methods to classes.
962    Examples of "p": "sA;;__ct__1AFv foo__1AFv ;;;" */
963 int
964 resolve_cfront_continuation (struct objfile *objfile, struct symbol *sym,
965                              char *p)
966 {
967   struct symbol *ref_sym = 0;
968   char *sname;
969   /* snarfed from read_struct_type */
970   struct field_info fi;
971   struct type *type;
972   struct cleanup *back_to;
973
974   /* Need to make sure that fi isn't gunna conflict with struct 
975      in case struct already had some fnfs */
976   fi.list = NULL;
977   fi.fnlist = NULL;
978   back_to = make_cleanup (null_cleanup, 0);
979
980   /* We only accept structs, classes and unions at the moment. 
981      Other continuation types include t (typedef), r (long dbl), ... 
982      We may want to add support for them as well; 
983      right now they are handled by duplicating the symbol information 
984      into the type information (see define_symbol) */
985   if (*p != 's'                 /* structs */
986       && *p != 'c'              /* class */
987       && *p != 'u')             /* union */
988     return 0;                   /* only handle C++ types */
989   p++;
990
991   /* Get symbol typs name and validate 
992      eg: p = "A;;__ct__1AFv foo__1AFv ;;;" */
993   sname = get_substring (&p, ';');
994   if (!sname || strcmp (sname, SYMBOL_NAME (sym)))
995     error ("Internal error: base symbol type name does not match\n");
996
997   /* Find symbol's internal gdb reference using demangled_name.
998      This is the real sym that we want; 
999      sym was a temp hack to make debugger happy */
1000   ref_sym = lookup_symbol (SYMBOL_NAME (sym), 0, STRUCT_NAMESPACE, 0, 0);
1001   type = SYMBOL_TYPE (ref_sym);
1002
1003
1004   /* Now read the baseclasses, if any, read the regular C struct or C++
1005      class member fields, attach the fields to the type, read the C++
1006      member functions, attach them to the type, and then read any tilde
1007      field (baseclass specifier for the class holding the main vtable). */
1008
1009   if (!read_cfront_baseclasses (&fi, &p, type, objfile)
1010   /* g++ does this next, but cfront already did this: 
1011      || !read_struct_fields (&fi, &p, type, objfile) */
1012       || !copy_cfront_struct_fields (&fi, type, objfile)
1013       || !read_cfront_member_functions (&fi, &p, type, objfile)
1014       || !read_cfront_static_fields (&fi, &p, type, objfile)
1015       || !attach_fields_to_type (&fi, type, objfile)
1016       || !attach_fn_fields_to_type (&fi, type)
1017   /* g++ does this next, but cfront doesn't seem to have this: 
1018      || !read_tilde_fields (&fi, &p, type, objfile) */
1019     )
1020     {
1021       type = error_type (&p, objfile);
1022     }
1023
1024   do_cleanups (back_to);
1025   return 0;
1026 }
1027 /* End of code added to support parsing of ARM/Cfront stabs strings */
1028
1029
1030 /* This routine fixes up symbol references/aliases to point to the original
1031    symbol definition.  Returns 0 on failure, non-zero on success.  */
1032
1033 static int
1034 resolve_symbol_reference (struct objfile *objfile, struct symbol *sym, char *p)
1035 {
1036   int refnum;
1037   struct symbol *ref_sym = 0;
1038   struct alias_list *alias;
1039
1040   /* If this is not a symbol reference return now.  */
1041   if (*p != '#')
1042     return 0;
1043
1044   /* Use "#<num>" as the name; we'll fix the name later.
1045      We stored the original symbol name as "#<id>=<name>"
1046      so we can now search for "#<id>" to resolving the reference.
1047      We'll fix the names later by removing the "#<id>" or "#<id>=" */
1048
1049 /*---------------------------------------------------------*/
1050   /* Get the reference id number, and 
1051      advance p past the names so we can parse the rest. 
1052      eg: id=2 for p : "2=", "2=z:r(0,1)" "2:r(0,1);l(#5,#6),l(#7,#4)" */
1053 /*---------------------------------------------------------*/
1054
1055   /* This gets reference name from string.  sym may not have a name. */
1056
1057   /* Get the reference number associated with the reference id in the
1058      gdb stab string.  From that reference number, get the main/primary
1059      symbol for this alias.  */
1060   refnum = process_reference (&p);
1061   ref_sym = ref_search (refnum);
1062   if (!ref_sym)
1063     {
1064       complain (&lrs_general_complaint, "symbol for reference not found");
1065       return 0;
1066     }
1067
1068   /* Parse the stab of the referencing symbol
1069      now that we have the referenced symbol.
1070      Add it as a new symbol and a link back to the referenced symbol.
1071      eg: p : "=", "=z:r(0,1)" ":r(0,1);l(#5,#6),l(#7,#4)" */
1072
1073
1074   /* If the stab symbol table and string contain:
1075      RSYM   0      5      00000000 868    #15=z:r(0,1)
1076      LBRAC  0      0      00000000 899    #5=
1077      SLINE  0      16     00000003 923    #6=
1078      Then the same symbols can be later referenced by:
1079      RSYM   0      5      00000000 927    #15:r(0,1);l(#5,#6)
1080      This is used in live range splitting to:
1081      1) specify that a symbol (#15) is actually just a new storage 
1082      class for a symbol (#15=z) which was previously defined.
1083      2) specify that the beginning and ending ranges for a symbol 
1084      (#15) are the values of the beginning (#5) and ending (#6) 
1085      symbols. */
1086
1087   /* Read number as reference id.
1088      eg: p : "=", "=z:r(0,1)" ":r(0,1);l(#5,#6),l(#7,#4)" */
1089   /* FIXME! Might I want to use SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
1090      in case of "l(0,0)"? */
1091
1092 /*--------------------------------------------------*/
1093   /* Add this symbol to the reference list.           */
1094 /*--------------------------------------------------*/
1095
1096   alias = (struct alias_list *) obstack_alloc (&objfile->type_obstack,
1097                                                sizeof (struct alias_list));
1098   if (!alias)
1099     {
1100       complain (&lrs_general_complaint, "Unable to allocate alias list memory");
1101       return 0;
1102     }
1103
1104   alias->next = 0;
1105   alias->sym = sym;
1106
1107   if (!SYMBOL_ALIASES (ref_sym))
1108     {
1109       SYMBOL_ALIASES (ref_sym) = alias;
1110     }
1111   else
1112     {
1113       struct alias_list *temp;
1114
1115       /* Get to the end of the list.  */
1116       for (temp = SYMBOL_ALIASES (ref_sym);
1117            temp->next;
1118            temp = temp->next)
1119         ;
1120       temp->next = alias;
1121     }
1122
1123   /* Want to fix up name so that other functions (eg. valops)
1124      will correctly print the name.
1125      Don't add_symbol_to_list so that lookup_symbol won't find it.
1126      nope... needed for fixups. */
1127   SYMBOL_NAME (sym) = SYMBOL_NAME (ref_sym);
1128
1129   /* Done!  */
1130   return 1;
1131 }
1132
1133 /* Structure for storing pointers to reference definitions for fast lookup 
1134    during "process_later". */
1135
1136 struct ref_map
1137 {
1138   char *stabs;
1139   CORE_ADDR value;
1140   struct symbol *sym;
1141 };
1142
1143 #define MAX_CHUNK_REFS 100
1144 #define REF_CHUNK_SIZE (MAX_CHUNK_REFS * sizeof (struct ref_map))
1145 #define REF_MAP_SIZE(ref_chunk) ((ref_chunk) * REF_CHUNK_SIZE)
1146
1147 static struct ref_map *ref_map;
1148
1149 /* Ptr to free cell in chunk's linked list. */
1150 static int ref_count = 0;
1151
1152 /* Number of chunks malloced. */
1153 static int ref_chunk = 0;
1154
1155 /* This file maintains a cache of stabs aliases found in the symbol
1156    table. If the symbol table changes, this cache must be cleared
1157    or we are left holding onto data in invalid obstacks. */
1158 void
1159 stabsread_clear_cache (void)
1160 {
1161   ref_count = 0;
1162   ref_chunk = 0;
1163 }
1164
1165 /* Create array of pointers mapping refids to symbols and stab strings.
1166    Add pointers to reference definition symbols and/or their values as we 
1167    find them, using their reference numbers as our index. 
1168    These will be used later when we resolve references. */
1169 void
1170 ref_add (int refnum, struct symbol *sym, char *stabs, CORE_ADDR value)
1171 {
1172   if (ref_count == 0)
1173     ref_chunk = 0;
1174   if (refnum >= ref_count)
1175     ref_count = refnum + 1;
1176   if (ref_count > ref_chunk * MAX_CHUNK_REFS)
1177     {
1178       int new_slots = ref_count - ref_chunk * MAX_CHUNK_REFS;
1179       int new_chunks = new_slots / MAX_CHUNK_REFS + 1;
1180       ref_map = (struct ref_map *)
1181         xrealloc (ref_map, REF_MAP_SIZE (ref_chunk + new_chunks));
1182       memset (ref_map + ref_chunk * MAX_CHUNK_REFS, 0, new_chunks * REF_CHUNK_SIZE);
1183       ref_chunk += new_chunks;
1184     }
1185   ref_map[refnum].stabs = stabs;
1186   ref_map[refnum].sym = sym;
1187   ref_map[refnum].value = value;
1188 }
1189
1190 /* Return defined sym for the reference REFNUM.  */
1191 struct symbol *
1192 ref_search (int refnum)
1193 {
1194   if (refnum < 0 || refnum > ref_count)
1195     return 0;
1196   return ref_map[refnum].sym;
1197 }
1198
1199 /* Return value for the reference REFNUM.  */
1200
1201 static CORE_ADDR
1202 ref_search_value (int refnum)
1203 {
1204   if (refnum < 0 || refnum > ref_count)
1205     return 0;
1206   return ref_map[refnum].value;
1207 }
1208
1209 /* Parse a reference id in STRING and return the resulting
1210    reference number.  Move STRING beyond the reference id.  */
1211
1212 static int
1213 process_reference (char **string)
1214 {
1215   char *p;
1216   int refnum = 0;
1217
1218   if (**string != '#')
1219     return 0;
1220
1221   /* Advance beyond the initial '#'.  */
1222   p = *string + 1;
1223
1224   /* Read number as reference id. */
1225   while (*p && isdigit (*p))
1226     {
1227       refnum = refnum * 10 + *p - '0';
1228       p++;
1229     }
1230   *string = p;
1231   return refnum;
1232 }
1233
1234 /* If STRING defines a reference, store away a pointer to the reference 
1235    definition for later use.  Return the reference number.  */
1236
1237 int
1238 symbol_reference_defined (char **string)
1239 {
1240   char *p = *string;
1241   int refnum = 0;
1242
1243   refnum = process_reference (&p);
1244
1245   /* Defining symbols end in '=' */
1246   if (*p == '=')
1247     {
1248       /* Symbol is being defined here. */
1249       *string = p + 1;
1250       return refnum;
1251     }
1252   else
1253     {
1254       /* Must be a reference.   Either the symbol has already been defined,
1255          or this is a forward reference to it.  */
1256       *string = p;
1257       return -1;
1258     }
1259 }
1260
1261 /* ARGSUSED */
1262 struct symbol *
1263 define_symbol (CORE_ADDR valu, char *string, int desc, int type,
1264                struct objfile *objfile)
1265 {
1266   register struct symbol *sym;
1267   char *p = (char *) strchr (string, ':');
1268   int deftype;
1269   int synonym = 0;
1270   register int i;
1271
1272   /* We would like to eliminate nameless symbols, but keep their types.
1273      E.g. stab entry ":t10=*2" should produce a type 10, which is a pointer
1274      to type 2, but, should not create a symbol to address that type. Since
1275      the symbol will be nameless, there is no way any user can refer to it. */
1276
1277   int nameless;
1278
1279   /* Ignore syms with empty names.  */
1280   if (string[0] == 0)
1281     return 0;
1282
1283   /* Ignore old-style symbols from cc -go  */
1284   if (p == 0)
1285     return 0;
1286
1287   while (p[1] == ':')
1288     {
1289       p += 2;
1290       p = strchr (p, ':');
1291     }
1292
1293   /* If a nameless stab entry, all we need is the type, not the symbol.
1294      e.g. ":t10=*2" or a nameless enum like " :T16=ered:0,green:1,blue:2,;" */
1295   nameless = (p == string || ((string[0] == ' ') && (string[1] == ':')));
1296
1297   current_symbol = sym = (struct symbol *)
1298     obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
1299   memset (sym, 0, sizeof (struct symbol));
1300
1301   switch (type & N_TYPE)
1302     {
1303     case N_TEXT:
1304       SYMBOL_SECTION (sym) = SECT_OFF_TEXT (objfile);
1305       break;
1306     case N_DATA:
1307       SYMBOL_SECTION (sym) = SECT_OFF_DATA (objfile);
1308       break;
1309     case N_BSS:
1310       SYMBOL_SECTION (sym) = SECT_OFF_BSS (objfile);
1311       break;
1312     }
1313
1314   if (processing_gcc_compilation)
1315     {
1316       /* GCC 2.x puts the line number in desc.  SunOS apparently puts in the
1317          number of bytes occupied by a type or object, which we ignore.  */
1318       SYMBOL_LINE (sym) = desc;
1319     }
1320   else
1321     {
1322       SYMBOL_LINE (sym) = 0;    /* unknown */
1323     }
1324
1325   if (is_cplus_marker (string[0]))
1326     {
1327       /* Special GNU C++ names.  */
1328       switch (string[1])
1329         {
1330         case 't':
1331           SYMBOL_NAME (sym) = obsavestring ("this", strlen ("this"),
1332                                             &objfile->symbol_obstack);
1333           break;
1334
1335         case 'v':               /* $vtbl_ptr_type */
1336           /* Was: SYMBOL_NAME (sym) = "vptr"; */
1337           goto normal;
1338
1339         case 'e':
1340           SYMBOL_NAME (sym) = obsavestring ("eh_throw", strlen ("eh_throw"),
1341                                             &objfile->symbol_obstack);
1342           break;
1343
1344         case '_':
1345           /* This was an anonymous type that was never fixed up.  */
1346           goto normal;
1347
1348 #ifdef STATIC_TRANSFORM_NAME
1349         case 'X':
1350           /* SunPRO (3.0 at least) static variable encoding.  */
1351           goto normal;
1352 #endif
1353
1354         default:
1355           complain (&unrecognized_cplus_name_complaint, string);
1356           goto normal;          /* Do *something* with it */
1357         }
1358     }
1359   else if (string[0] == '#')
1360     {
1361       /* Special GNU C extension for referencing symbols.  */
1362       char *s;
1363       int refnum, nlen;
1364
1365       /* If STRING defines a new reference id, then add it to the
1366          reference map.  Else it must be referring to a previously
1367          defined symbol, so add it to the alias list of the previously
1368          defined symbol.  */
1369       s = string;
1370       refnum = symbol_reference_defined (&s);
1371       if (refnum >= 0)
1372         ref_add (refnum, sym, string, SYMBOL_VALUE (sym));
1373       else if (!resolve_symbol_reference (objfile, sym, string))
1374         return NULL;
1375
1376       /* S..P contains the name of the symbol.  We need to store
1377          the correct name into SYMBOL_NAME.  */
1378       nlen = p - s;
1379       if (refnum >= 0)
1380         {
1381           if (nlen > 0)
1382             {
1383               SYMBOL_NAME (sym) = (char *)
1384                 obstack_alloc (&objfile->symbol_obstack, nlen);
1385               strncpy (SYMBOL_NAME (sym), s, nlen);
1386               SYMBOL_NAME (sym)[nlen] = '\0';
1387               SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
1388             }
1389           else
1390             /* FIXME! Want SYMBOL_NAME (sym) = 0;
1391                Get error if leave name 0.  So give it something. */
1392             {
1393               nlen = p - string;
1394               SYMBOL_NAME (sym) = (char *)
1395                 obstack_alloc (&objfile->symbol_obstack, nlen);
1396               strncpy (SYMBOL_NAME (sym), string, nlen);
1397               SYMBOL_NAME (sym)[nlen] = '\0';
1398               SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
1399             }
1400         }
1401       /* Advance STRING beyond the reference id.  */
1402       string = s;
1403     }
1404   else
1405     {
1406     normal:
1407       SYMBOL_LANGUAGE (sym) = current_subfile->language;
1408       SYMBOL_NAME (sym) = (char *)
1409         obstack_alloc (&objfile->symbol_obstack, ((p - string) + 1));
1410       /* Open-coded memcpy--saves function call time.  */
1411       /* FIXME:  Does it really?  Try replacing with simple strcpy and
1412          try it on an executable with a large symbol table. */
1413       /* FIXME: considering that gcc can open code memcpy anyway, I
1414          doubt it.  xoxorich. */
1415       {
1416         register char *p1 = string;
1417         register char *p2 = SYMBOL_NAME (sym);
1418         while (p1 != p)
1419           {
1420             *p2++ = *p1++;
1421           }
1422         *p2++ = '\0';
1423       }
1424
1425       /* If this symbol is from a C++ compilation, then attempt to cache the
1426          demangled form for future reference.  This is a typical time versus
1427          space tradeoff, that was decided in favor of time because it sped up
1428          C++ symbol lookups by a factor of about 20. */
1429
1430       SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
1431     }
1432   p++;
1433
1434   /* Determine the type of name being defined.  */
1435 #if 0
1436   /* Getting GDB to correctly skip the symbol on an undefined symbol
1437      descriptor and not ever dump core is a very dodgy proposition if
1438      we do things this way.  I say the acorn RISC machine can just
1439      fix their compiler.  */
1440   /* The Acorn RISC machine's compiler can put out locals that don't
1441      start with "234=" or "(3,4)=", so assume anything other than the
1442      deftypes we know how to handle is a local.  */
1443   if (!strchr ("cfFGpPrStTvVXCR", *p))
1444 #else
1445   if (isdigit (*p) || *p == '(' || *p == '-')
1446 #endif
1447     deftype = 'l';
1448   else
1449     deftype = *p++;
1450
1451   switch (deftype)
1452     {
1453     case 'c':
1454       /* c is a special case, not followed by a type-number.
1455          SYMBOL:c=iVALUE for an integer constant symbol.
1456          SYMBOL:c=rVALUE for a floating constant symbol.
1457          SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
1458          e.g. "b:c=e6,0" for "const b = blob1"
1459          (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
1460       if (*p != '=')
1461         {
1462           SYMBOL_CLASS (sym) = LOC_CONST;
1463           SYMBOL_TYPE (sym) = error_type (&p, objfile);
1464           SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1465           add_symbol_to_list (sym, &file_symbols);
1466           return sym;
1467         }
1468       ++p;
1469       switch (*p++)
1470         {
1471         case 'r':
1472           {
1473             double d = atof (p);
1474             char *dbl_valu;
1475
1476             /* FIXME-if-picky-about-floating-accuracy: Should be using
1477                target arithmetic to get the value.  real.c in GCC
1478                probably has the necessary code.  */
1479
1480             /* FIXME: lookup_fundamental_type is a hack.  We should be
1481                creating a type especially for the type of float constants.
1482                Problem is, what type should it be?
1483
1484                Also, what should the name of this type be?  Should we
1485                be using 'S' constants (see stabs.texinfo) instead?  */
1486
1487             SYMBOL_TYPE (sym) = lookup_fundamental_type (objfile,
1488                                                          FT_DBL_PREC_FLOAT);
1489             dbl_valu = (char *)
1490               obstack_alloc (&objfile->symbol_obstack,
1491                              TYPE_LENGTH (SYMBOL_TYPE (sym)));
1492             store_floating (dbl_valu, TYPE_LENGTH (SYMBOL_TYPE (sym)), d);
1493             SYMBOL_VALUE_BYTES (sym) = dbl_valu;
1494             SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
1495           }
1496           break;
1497         case 'i':
1498           {
1499             /* Defining integer constants this way is kind of silly,
1500                since 'e' constants allows the compiler to give not
1501                only the value, but the type as well.  C has at least
1502                int, long, unsigned int, and long long as constant
1503                types; other languages probably should have at least
1504                unsigned as well as signed constants.  */
1505
1506             /* We just need one int constant type for all objfiles.
1507                It doesn't depend on languages or anything (arguably its
1508                name should be a language-specific name for a type of
1509                that size, but I'm inclined to say that if the compiler
1510                wants a nice name for the type, it can use 'e').  */
1511             static struct type *int_const_type;
1512
1513             /* Yes, this is as long as a *host* int.  That is because we
1514                use atoi.  */
1515             if (int_const_type == NULL)
1516               int_const_type =
1517                 init_type (TYPE_CODE_INT,
1518                            sizeof (int) * HOST_CHAR_BIT / TARGET_CHAR_BIT, 0,
1519                            "integer constant",
1520                              (struct objfile *) NULL);
1521             SYMBOL_TYPE (sym) = int_const_type;
1522             SYMBOL_VALUE (sym) = atoi (p);
1523             SYMBOL_CLASS (sym) = LOC_CONST;
1524           }
1525           break;
1526         case 'e':
1527           /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
1528              can be represented as integral.
1529              e.g. "b:c=e6,0" for "const b = blob1"
1530              (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;").  */
1531           {
1532             SYMBOL_CLASS (sym) = LOC_CONST;
1533             SYMBOL_TYPE (sym) = read_type (&p, objfile);
1534
1535             if (*p != ',')
1536               {
1537                 SYMBOL_TYPE (sym) = error_type (&p, objfile);
1538                 break;
1539               }
1540             ++p;
1541
1542             /* If the value is too big to fit in an int (perhaps because
1543                it is unsigned), or something like that, we silently get
1544                a bogus value.  The type and everything else about it is
1545                correct.  Ideally, we should be using whatever we have
1546                available for parsing unsigned and long long values,
1547                however.  */
1548             SYMBOL_VALUE (sym) = atoi (p);
1549           }
1550           break;
1551         default:
1552           {
1553             SYMBOL_CLASS (sym) = LOC_CONST;
1554             SYMBOL_TYPE (sym) = error_type (&p, objfile);
1555           }
1556         }
1557       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1558       add_symbol_to_list (sym, &file_symbols);
1559       return sym;
1560
1561     case 'C':
1562       /* The name of a caught exception.  */
1563       SYMBOL_TYPE (sym) = read_type (&p, objfile);
1564       SYMBOL_CLASS (sym) = LOC_LABEL;
1565       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1566       SYMBOL_VALUE_ADDRESS (sym) = valu;
1567       add_symbol_to_list (sym, &local_symbols);
1568       break;
1569
1570     case 'f':
1571       /* A static function definition.  */
1572       SYMBOL_TYPE (sym) = read_type (&p, objfile);
1573       SYMBOL_CLASS (sym) = LOC_BLOCK;
1574       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1575       add_symbol_to_list (sym, &file_symbols);
1576       /* fall into process_function_types.  */
1577
1578     process_function_types:
1579       /* Function result types are described as the result type in stabs.
1580          We need to convert this to the function-returning-type-X type
1581          in GDB.  E.g. "int" is converted to "function returning int".  */
1582       if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_FUNC)
1583         SYMBOL_TYPE (sym) = lookup_function_type (SYMBOL_TYPE (sym));
1584
1585       /* All functions in C++ have prototypes.  */
1586       if (SYMBOL_LANGUAGE (sym) == language_cplus)
1587         TYPE_FLAGS (SYMBOL_TYPE (sym)) |= TYPE_FLAG_PROTOTYPED;
1588
1589       /* fall into process_prototype_types */
1590
1591     process_prototype_types:
1592       /* Sun acc puts declared types of arguments here.  */
1593       if (*p == ';')
1594         {
1595           struct type *ftype = SYMBOL_TYPE (sym);
1596           int nsemi = 0;
1597           int nparams = 0;
1598           char *p1 = p;
1599
1600           /* Obtain a worst case guess for the number of arguments
1601              by counting the semicolons.  */
1602           while (*p1)
1603             {
1604               if (*p1++ == ';')
1605                 nsemi++;
1606             }
1607
1608           /* Allocate parameter information fields and fill them in. */
1609           TYPE_FIELDS (ftype) = (struct field *)
1610             TYPE_ALLOC (ftype, nsemi * sizeof (struct field));
1611           while (*p++ == ';')
1612             {
1613               struct type *ptype;
1614
1615               /* A type number of zero indicates the start of varargs.
1616                  FIXME: GDB currently ignores vararg functions.  */
1617               if (p[0] == '0' && p[1] == '\0')
1618                 break;
1619               ptype = read_type (&p, objfile);
1620
1621               /* The Sun compilers mark integer arguments, which should
1622                  be promoted to the width of the calling conventions, with
1623                  a type which references itself. This type is turned into
1624                  a TYPE_CODE_VOID type by read_type, and we have to turn
1625                  it back into builtin_type_int here.
1626                  FIXME: Do we need a new builtin_type_promoted_int_arg ?  */
1627               if (TYPE_CODE (ptype) == TYPE_CODE_VOID)
1628                 ptype = builtin_type_int;
1629               TYPE_FIELD_TYPE (ftype, nparams++) = ptype;
1630             }
1631           TYPE_NFIELDS (ftype) = nparams;
1632           TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
1633         }
1634       break;
1635
1636     case 'F':
1637       /* A global function definition.  */
1638       SYMBOL_TYPE (sym) = read_type (&p, objfile);
1639       SYMBOL_CLASS (sym) = LOC_BLOCK;
1640       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1641       add_symbol_to_list (sym, &global_symbols);
1642       goto process_function_types;
1643
1644     case 'G':
1645       /* For a class G (global) symbol, it appears that the
1646          value is not correct.  It is necessary to search for the
1647          corresponding linker definition to find the value.
1648          These definitions appear at the end of the namelist.  */
1649       SYMBOL_TYPE (sym) = read_type (&p, objfile);
1650       SYMBOL_CLASS (sym) = LOC_STATIC;
1651       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1652       /* Don't add symbol references to global_sym_chain.
1653          Symbol references don't have valid names and wont't match up with
1654          minimal symbols when the global_sym_chain is relocated.
1655          We'll fixup symbol references when we fixup the defining symbol.  */
1656       if (SYMBOL_NAME (sym) && SYMBOL_NAME (sym)[0] != '#')
1657         {
1658           i = hashname (SYMBOL_NAME (sym));
1659           SYMBOL_VALUE_CHAIN (sym) = global_sym_chain[i];
1660           global_sym_chain[i] = sym;
1661         }
1662       add_symbol_to_list (sym, &global_symbols);
1663       break;
1664
1665       /* This case is faked by a conditional above,
1666          when there is no code letter in the dbx data.
1667          Dbx data never actually contains 'l'.  */
1668     case 's':
1669     case 'l':
1670       SYMBOL_TYPE (sym) = read_type (&p, objfile);
1671       SYMBOL_CLASS (sym) = LOC_LOCAL;
1672       SYMBOL_VALUE (sym) = valu;
1673       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1674       add_symbol_to_list (sym, &local_symbols);
1675       break;
1676
1677     case 'p':
1678       if (*p == 'F')
1679         /* pF is a two-letter code that means a function parameter in Fortran.
1680            The type-number specifies the type of the return value.
1681            Translate it into a pointer-to-function type.  */
1682         {
1683           p++;
1684           SYMBOL_TYPE (sym)
1685             = lookup_pointer_type
1686             (lookup_function_type (read_type (&p, objfile)));
1687         }
1688       else
1689         SYMBOL_TYPE (sym) = read_type (&p, objfile);
1690
1691       /* Normally this is a parameter, a LOC_ARG.  On the i960, it
1692          can also be a LOC_LOCAL_ARG depending on symbol type.  */
1693 #ifndef DBX_PARM_SYMBOL_CLASS
1694 #define DBX_PARM_SYMBOL_CLASS(type)     LOC_ARG
1695 #endif
1696
1697       SYMBOL_CLASS (sym) = DBX_PARM_SYMBOL_CLASS (type);
1698       SYMBOL_VALUE (sym) = valu;
1699       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1700       add_symbol_to_list (sym, &local_symbols);
1701
1702       if (TARGET_BYTE_ORDER != BIG_ENDIAN)
1703         {
1704           /* On little-endian machines, this crud is never necessary,
1705              and, if the extra bytes contain garbage, is harmful.  */
1706           break;
1707         }
1708
1709       /* If it's gcc-compiled, if it says `short', believe it.  */
1710       if (processing_gcc_compilation || BELIEVE_PCC_PROMOTION)
1711         break;
1712
1713       if (!BELIEVE_PCC_PROMOTION)
1714         {
1715           /* This is the signed type which arguments get promoted to.  */
1716           static struct type *pcc_promotion_type;
1717           /* This is the unsigned type which arguments get promoted to.  */
1718           static struct type *pcc_unsigned_promotion_type;
1719
1720           /* Call it "int" because this is mainly C lossage.  */
1721           if (pcc_promotion_type == NULL)
1722             pcc_promotion_type =
1723               init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1724                          0, "int", NULL);
1725
1726           if (pcc_unsigned_promotion_type == NULL)
1727             pcc_unsigned_promotion_type =
1728               init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1729                          TYPE_FLAG_UNSIGNED, "unsigned int", NULL);
1730
1731           if (BELIEVE_PCC_PROMOTION_TYPE)
1732             {
1733               /* This is defined on machines (e.g. sparc) where we
1734                  should believe the type of a PCC 'short' argument,
1735                  but shouldn't believe the address (the address is the
1736                  address of the corresponding int).
1737
1738                  My guess is that this correction, as opposed to
1739                  changing the parameter to an 'int' (as done below,
1740                  for PCC on most machines), is the right thing to do
1741                  on all machines, but I don't want to risk breaking
1742                  something that already works.  On most PCC machines,
1743                  the sparc problem doesn't come up because the calling
1744                  function has to zero the top bytes (not knowing
1745                  whether the called function wants an int or a short),
1746                  so there is little practical difference between an
1747                  int and a short (except perhaps what happens when the
1748                  GDB user types "print short_arg = 0x10000;").
1749
1750                  Hacked for SunOS 4.1 by gnu@cygnus.com.  In 4.1, the
1751                  compiler actually produces the correct address (we
1752                  don't need to fix it up).  I made this code adapt so
1753                  that it will offset the symbol if it was pointing at
1754                  an int-aligned location and not otherwise.  This way
1755                  you can use the same gdb for 4.0.x and 4.1 systems.
1756
1757                  If the parameter is shorter than an int, and is
1758                  integral (e.g. char, short, or unsigned equivalent),
1759                  and is claimed to be passed on an integer boundary,
1760                  don't believe it!  Offset the parameter's address to
1761                  the tail-end of that integer.  */
1762
1763               if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (pcc_promotion_type)
1764                   && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
1765               && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (pcc_promotion_type))
1766                 {
1767                   SYMBOL_VALUE (sym) += TYPE_LENGTH (pcc_promotion_type)
1768                     - TYPE_LENGTH (SYMBOL_TYPE (sym));
1769                 }
1770               break;
1771             }
1772           else
1773             {
1774               /* If PCC says a parameter is a short or a char,
1775                  it is really an int.  */
1776               if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (pcc_promotion_type)
1777                   && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1778                 {
1779                   SYMBOL_TYPE (sym) =
1780                     TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1781                     ? pcc_unsigned_promotion_type
1782                     : pcc_promotion_type;
1783                 }
1784               break;
1785             }
1786         }
1787
1788     case 'P':
1789       /* acc seems to use P to declare the prototypes of functions that
1790          are referenced by this file.  gdb is not prepared to deal
1791          with this extra information.  FIXME, it ought to.  */
1792       if (type == N_FUN)
1793         {
1794           SYMBOL_TYPE (sym) = read_type (&p, objfile);
1795           goto process_prototype_types;
1796         }
1797       /*FALLTHROUGH */
1798
1799     case 'R':
1800       /* Parameter which is in a register.  */
1801       SYMBOL_TYPE (sym) = read_type (&p, objfile);
1802       SYMBOL_CLASS (sym) = LOC_REGPARM;
1803       SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (valu);
1804       if (SYMBOL_VALUE (sym) >= NUM_REGS)
1805         {
1806           complain (&reg_value_complaint, SYMBOL_VALUE (sym), NUM_REGS,
1807                     SYMBOL_SOURCE_NAME (sym));
1808           SYMBOL_VALUE (sym) = SP_REGNUM;       /* Known safe, though useless */
1809         }
1810       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1811       add_symbol_to_list (sym, &local_symbols);
1812       break;
1813
1814     case 'r':
1815       /* Register variable (either global or local).  */
1816       SYMBOL_TYPE (sym) = read_type (&p, objfile);
1817       SYMBOL_CLASS (sym) = LOC_REGISTER;
1818       SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (valu);
1819       if (SYMBOL_VALUE (sym) >= NUM_REGS)
1820         {
1821           complain (&reg_value_complaint, SYMBOL_VALUE (sym), NUM_REGS,
1822                     SYMBOL_SOURCE_NAME (sym));
1823           SYMBOL_VALUE (sym) = SP_REGNUM;       /* Known safe, though useless */
1824         }
1825       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1826       if (within_function)
1827         {
1828           /* Sun cc uses a pair of symbols, one 'p' and one 'r' with the same
1829              name to represent an argument passed in a register.
1830              GCC uses 'P' for the same case.  So if we find such a symbol pair
1831              we combine it into one 'P' symbol.  For Sun cc we need to do this
1832              regardless of REG_STRUCT_HAS_ADDR, because the compiler puts out
1833              the 'p' symbol even if it never saves the argument onto the stack.
1834
1835              On most machines, we want to preserve both symbols, so that
1836              we can still get information about what is going on with the
1837              stack (VAX for computing args_printed, using stack slots instead
1838              of saved registers in backtraces, etc.).
1839
1840              Note that this code illegally combines
1841              main(argc) struct foo argc; { register struct foo argc; }
1842              but this case is considered pathological and causes a warning
1843              from a decent compiler.  */
1844
1845           if (local_symbols
1846               && local_symbols->nsyms > 0
1847 #ifndef USE_REGISTER_NOT_ARG
1848               && REG_STRUCT_HAS_ADDR_P ()
1849               && REG_STRUCT_HAS_ADDR (processing_gcc_compilation,
1850                                       SYMBOL_TYPE (sym))
1851               && (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
1852                   || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_UNION
1853                   || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_SET
1854                   || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_BITSTRING)
1855 #endif
1856             )
1857             {
1858               struct symbol *prev_sym;
1859               prev_sym = local_symbols->symbol[local_symbols->nsyms - 1];
1860               if ((SYMBOL_CLASS (prev_sym) == LOC_REF_ARG
1861                    || SYMBOL_CLASS (prev_sym) == LOC_ARG)
1862                   && STREQ (SYMBOL_NAME (prev_sym), SYMBOL_NAME (sym)))
1863                 {
1864                   SYMBOL_CLASS (prev_sym) = LOC_REGPARM;
1865                   /* Use the type from the LOC_REGISTER; that is the type
1866                      that is actually in that register.  */
1867                   SYMBOL_TYPE (prev_sym) = SYMBOL_TYPE (sym);
1868                   SYMBOL_VALUE (prev_sym) = SYMBOL_VALUE (sym);
1869                   sym = prev_sym;
1870                   break;
1871                 }
1872             }
1873           add_symbol_to_list (sym, &local_symbols);
1874         }
1875       else
1876         add_symbol_to_list (sym, &file_symbols);
1877       break;
1878
1879     case 'S':
1880       /* Static symbol at top level of file */
1881       SYMBOL_TYPE (sym) = read_type (&p, objfile);
1882       SYMBOL_CLASS (sym) = LOC_STATIC;
1883       SYMBOL_VALUE_ADDRESS (sym) = valu;
1884 #ifdef STATIC_TRANSFORM_NAME
1885       if (IS_STATIC_TRANSFORM_NAME (SYMBOL_NAME (sym)))
1886         {
1887           struct minimal_symbol *msym;
1888           msym = lookup_minimal_symbol (SYMBOL_NAME (sym), NULL, objfile);
1889           if (msym != NULL)
1890             {
1891               SYMBOL_NAME (sym) = STATIC_TRANSFORM_NAME (SYMBOL_NAME (sym));
1892               SYMBOL_VALUE_ADDRESS (sym) = SYMBOL_VALUE_ADDRESS (msym);
1893             }
1894         }
1895 #endif
1896       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1897       add_symbol_to_list (sym, &file_symbols);
1898       break;
1899
1900     case 't':
1901       SYMBOL_TYPE (sym) = read_type (&p, objfile);
1902
1903       /* For a nameless type, we don't want a create a symbol, thus we
1904          did not use `sym'. Return without further processing. */
1905       if (nameless)
1906         return NULL;
1907
1908       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1909       SYMBOL_VALUE (sym) = valu;
1910       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1911       /* C++ vagaries: we may have a type which is derived from
1912          a base type which did not have its name defined when the
1913          derived class was output.  We fill in the derived class's
1914          base part member's name here in that case.  */
1915       if (TYPE_NAME (SYMBOL_TYPE (sym)) != NULL)
1916         if ((TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
1917              || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_UNION)
1918             && TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)))
1919           {
1920             int j;
1921             for (j = TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)) - 1; j >= 0; j--)
1922               if (TYPE_BASECLASS_NAME (SYMBOL_TYPE (sym), j) == 0)
1923                 TYPE_BASECLASS_NAME (SYMBOL_TYPE (sym), j) =
1924                   type_name_no_tag (TYPE_BASECLASS (SYMBOL_TYPE (sym), j));
1925           }
1926
1927       if (TYPE_NAME (SYMBOL_TYPE (sym)) == NULL)
1928         {
1929           /* gcc-2.6 or later (when using -fvtable-thunks)
1930              emits a unique named type for a vtable entry.
1931              Some gdb code depends on that specific name. */
1932           extern const char vtbl_ptr_name[];
1933
1934           if ((TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1935                && strcmp (SYMBOL_NAME (sym), vtbl_ptr_name))
1936               || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1937             {
1938               /* If we are giving a name to a type such as "pointer to
1939                  foo" or "function returning foo", we better not set
1940                  the TYPE_NAME.  If the program contains "typedef char
1941                  *caddr_t;", we don't want all variables of type char
1942                  * to print as caddr_t.  This is not just a
1943                  consequence of GDB's type management; PCC and GCC (at
1944                  least through version 2.4) both output variables of
1945                  either type char * or caddr_t with the type number
1946                  defined in the 't' symbol for caddr_t.  If a future
1947                  compiler cleans this up it GDB is not ready for it
1948                  yet, but if it becomes ready we somehow need to
1949                  disable this check (without breaking the PCC/GCC2.4
1950                  case).
1951
1952                  Sigh.
1953
1954                  Fortunately, this check seems not to be necessary
1955                  for anything except pointers or functions.  */
1956               /* ezannoni: 2000-10-26. This seems to apply for
1957                  versions of gcc older than 2.8. This was the original
1958                  problem: with the following code gdb would tell that
1959                  the type for name1 is caddr_t, and func is char()
1960                  typedef char *caddr_t;
1961                  char *name2;
1962                  struct x
1963                  {
1964                  char *name1;
1965                  } xx;
1966                  char *func()
1967                  {
1968                  }
1969                  main () {}
1970                  */
1971
1972               /* Pascal accepts names for pointer types. */
1973               if (current_subfile->language == language_pascal)
1974                 {
1975                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NAME (sym);
1976                 }
1977             }
1978           else
1979             TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NAME (sym);
1980         }
1981
1982       add_symbol_to_list (sym, &file_symbols);
1983       break;
1984
1985     case 'T':
1986       /* Struct, union, or enum tag.  For GNU C++, this can be be followed
1987          by 't' which means we are typedef'ing it as well.  */
1988       synonym = *p == 't';
1989
1990       if (synonym)
1991         p++;
1992       /* The semantics of C++ state that "struct foo { ... }" also defines 
1993          a typedef for "foo".  Unfortunately, cfront never makes the typedef
1994          when translating C++ into C.  We make the typedef here so that
1995          "ptype foo" works as expected for cfront translated code.  */
1996       else if (current_subfile->language == language_cplus)
1997         synonym = 1;
1998
1999       SYMBOL_TYPE (sym) = read_type (&p, objfile);
2000
2001       /* For a nameless type, we don't want a create a symbol, thus we
2002          did not use `sym'. Return without further processing. */
2003       if (nameless)
2004         return NULL;
2005
2006       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
2007       SYMBOL_VALUE (sym) = valu;
2008       SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
2009       if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
2010         TYPE_TAG_NAME (SYMBOL_TYPE (sym))
2011           = obconcat (&objfile->type_obstack, "", "", SYMBOL_NAME (sym));
2012       add_symbol_to_list (sym, &file_symbols);
2013
2014       if (synonym)
2015         {
2016           /* Clone the sym and then modify it. */
2017           register struct symbol *typedef_sym = (struct symbol *)
2018           obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
2019           *typedef_sym = *sym;
2020           SYMBOL_CLASS (typedef_sym) = LOC_TYPEDEF;
2021           SYMBOL_VALUE (typedef_sym) = valu;
2022           SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE;
2023           if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
2024             TYPE_NAME (SYMBOL_TYPE (sym))
2025               = obconcat (&objfile->type_obstack, "", "", SYMBOL_NAME (sym));
2026           add_symbol_to_list (typedef_sym, &file_symbols);
2027         }
2028       break;
2029
2030     case 'V':
2031       /* Static symbol of local scope */
2032       SYMBOL_TYPE (sym) = read_type (&p, objfile);
2033       SYMBOL_CLASS (sym) = LOC_STATIC;
2034       SYMBOL_VALUE_ADDRESS (sym) = valu;
2035 #ifdef STATIC_TRANSFORM_NAME
2036       if (IS_STATIC_TRANSFORM_NAME (SYMBOL_NAME (sym)))
2037         {
2038           struct minimal_symbol *msym;
2039           msym = lookup_minimal_symbol (SYMBOL_NAME (sym), NULL, objfile);
2040           if (msym != NULL)
2041             {
2042               SYMBOL_NAME (sym) = STATIC_TRANSFORM_NAME (SYMBOL_NAME (sym));
2043               SYMBOL_VALUE_ADDRESS (sym) = SYMBOL_VALUE_ADDRESS (msym);
2044             }
2045         }
2046 #endif
2047       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2048       if (os9k_stabs)
2049         add_symbol_to_list (sym, &global_symbols);
2050       else
2051         add_symbol_to_list (sym, &local_symbols);
2052       break;
2053
2054     case 'v':
2055       /* Reference parameter */
2056       SYMBOL_TYPE (sym) = read_type (&p, objfile);
2057       SYMBOL_CLASS (sym) = LOC_REF_ARG;
2058       SYMBOL_VALUE (sym) = valu;
2059       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2060       add_symbol_to_list (sym, &local_symbols);
2061       break;
2062
2063     case 'a':
2064       /* Reference parameter which is in a register.  */
2065       SYMBOL_TYPE (sym) = read_type (&p, objfile);
2066       SYMBOL_CLASS (sym) = LOC_REGPARM_ADDR;
2067       SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (valu);
2068       if (SYMBOL_VALUE (sym) >= NUM_REGS)
2069         {
2070           complain (&reg_value_complaint, SYMBOL_VALUE (sym), NUM_REGS,
2071                     SYMBOL_SOURCE_NAME (sym));
2072           SYMBOL_VALUE (sym) = SP_REGNUM;       /* Known safe, though useless */
2073         }
2074       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2075       add_symbol_to_list (sym, &local_symbols);
2076       break;
2077
2078     case 'X':
2079       /* This is used by Sun FORTRAN for "function result value".
2080          Sun claims ("dbx and dbxtool interfaces", 2nd ed)
2081          that Pascal uses it too, but when I tried it Pascal used
2082          "x:3" (local symbol) instead.  */
2083       SYMBOL_TYPE (sym) = read_type (&p, objfile);
2084       SYMBOL_CLASS (sym) = LOC_LOCAL;
2085       SYMBOL_VALUE (sym) = valu;
2086       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2087       add_symbol_to_list (sym, &local_symbols);
2088       break;
2089
2090       /* New code added to support cfront stabs strings.
2091          Note: case 'P' already handled above */
2092     case 'Z':
2093       /* Cfront type continuation coming up!
2094          Find the original definition and add to it.
2095          We'll have to do this for the typedef too,
2096          since we cloned the symbol to define a type in read_type.
2097          Stabs info examples:
2098          __1C :Ztl 
2099          foo__1CFv :ZtF (first def foo__1CFv:F(0,3);(0,24))
2100          C:ZsC;;__ct__1CFv func1__1CFv func2__1CFv ... ;;;
2101          where C is the name of the class.
2102          Unfortunately, we can't lookup the original symbol yet 'cuz 
2103          we haven't finished reading all the symbols.
2104          Instead, we save it for processing later */
2105       process_later (sym, p, resolve_cfront_continuation);
2106       SYMBOL_TYPE (sym) = error_type (&p, objfile);     /* FIXME! change later */
2107       SYMBOL_CLASS (sym) = LOC_CONST;
2108       SYMBOL_VALUE (sym) = 0;
2109       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2110       /* Don't add to list - we'll delete it later when 
2111          we add the continuation to the real sym */
2112       return sym;
2113       /* End of new code added to support cfront stabs strings */
2114
2115     default:
2116       SYMBOL_TYPE (sym) = error_type (&p, objfile);
2117       SYMBOL_CLASS (sym) = LOC_CONST;
2118       SYMBOL_VALUE (sym) = 0;
2119       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2120       add_symbol_to_list (sym, &file_symbols);
2121       break;
2122     }
2123
2124   /* When passing structures to a function, some systems sometimes pass
2125      the address in a register, not the structure itself. */
2126
2127   if (REG_STRUCT_HAS_ADDR_P ()
2128       && REG_STRUCT_HAS_ADDR (processing_gcc_compilation, SYMBOL_TYPE (sym))
2129       && (SYMBOL_CLASS (sym) == LOC_REGPARM || SYMBOL_CLASS (sym) == LOC_ARG))
2130     {
2131       struct type *symbol_type = check_typedef (SYMBOL_TYPE (sym));
2132
2133       if ((TYPE_CODE (symbol_type) == TYPE_CODE_STRUCT)
2134           || (TYPE_CODE (symbol_type) == TYPE_CODE_UNION)
2135           || (TYPE_CODE (symbol_type) == TYPE_CODE_BITSTRING)
2136           || (TYPE_CODE (symbol_type) == TYPE_CODE_SET))
2137         {
2138           /* If REG_STRUCT_HAS_ADDR yields non-zero we have to convert
2139              LOC_REGPARM to LOC_REGPARM_ADDR for structures and unions. */
2140           if (SYMBOL_CLASS (sym) == LOC_REGPARM)
2141             SYMBOL_CLASS (sym) = LOC_REGPARM_ADDR;
2142           /* Likewise for converting LOC_ARG to LOC_REF_ARG (for the 7th
2143              and subsequent arguments on the sparc, for example).  */
2144           else if (SYMBOL_CLASS (sym) == LOC_ARG)
2145             SYMBOL_CLASS (sym) = LOC_REF_ARG;
2146         }
2147     }
2148
2149   /* Is there more to parse?  For example LRS/alias information?  */
2150   while (*p && *p == ';')
2151     {
2152       p++;
2153       if (*p && p[0] == 'l' && p[1] == '(')
2154         {
2155           /* GNU extensions for live range splitting may be appended to 
2156              the end of the stab string.  eg. "l(#1,#2);l(#3,#5)" */
2157
2158           /* Resolve the live range and add it to SYM's live range list.  */
2159           if (!resolve_live_range (objfile, sym, p))
2160             return NULL;
2161
2162           /* Find end of live range info. */
2163           p = strchr (p, ')');
2164           if (!*p || *p != ')')
2165             {
2166               complain (&lrs_general_complaint, "live range format not recognized");
2167               return NULL;
2168             }
2169           p++;
2170         }
2171     }
2172   return sym;
2173 }
2174
2175 /* Add the live range found in P to the symbol SYM in objfile OBJFILE.  Returns
2176    non-zero on success, zero otherwise.  */
2177
2178 static int
2179 resolve_live_range (struct objfile *objfile, struct symbol *sym, char *p)
2180 {
2181   int refnum;
2182   CORE_ADDR start, end;
2183
2184   /* Sanity check the beginning of the stabs string.  */
2185   if (!*p || *p != 'l')
2186     {
2187       complain (&lrs_general_complaint, "live range string 1");
2188       return 0;
2189     }
2190   p++;
2191
2192   if (!*p || *p != '(')
2193     {
2194       complain (&lrs_general_complaint, "live range string 2");
2195       return 0;
2196     }
2197   p++;
2198
2199   /* Get starting value of range and advance P past the reference id.
2200
2201      ?!? In theory, the process_reference should never fail, but we should
2202      catch that case just in case the compiler scrogged the stabs.  */
2203   refnum = process_reference (&p);
2204   start = ref_search_value (refnum);
2205   if (!start)
2206     {
2207       complain (&lrs_general_complaint, "Live range symbol not found 1");
2208       return 0;
2209     }
2210
2211   if (!*p || *p != ',')
2212     {
2213       complain (&lrs_general_complaint, "live range string 3");
2214       return 0;
2215     }
2216   p++;
2217
2218   /* Get ending value of range and advance P past the reference id.
2219
2220      ?!? In theory, the process_reference should never fail, but we should
2221      catch that case just in case the compiler scrogged the stabs.  */
2222   refnum = process_reference (&p);
2223   end = ref_search_value (refnum);
2224   if (!end)
2225     {
2226       complain (&lrs_general_complaint, "Live range symbol not found 2");
2227       return 0;
2228     }
2229
2230   if (!*p || *p != ')')
2231     {
2232       complain (&lrs_general_complaint, "live range string 4");
2233       return 0;
2234     }
2235
2236   /* Now that we know the bounds of the range, add it to the
2237      symbol.  */
2238   add_live_range (objfile, sym, start, end);
2239
2240   return 1;
2241 }
2242
2243 /* Add a new live range defined by START and END to the symbol SYM
2244    in objfile OBJFILE.  */
2245
2246 static void
2247 add_live_range (struct objfile *objfile, struct symbol *sym, CORE_ADDR start,
2248                 CORE_ADDR end)
2249 {
2250   struct range_list *r, *rs;
2251
2252   if (start >= end)
2253     {
2254       complain (&lrs_general_complaint, "end of live range follows start");
2255       return;
2256     }
2257
2258   /* Alloc new live range structure. */
2259   r = (struct range_list *)
2260     obstack_alloc (&objfile->type_obstack,
2261                    sizeof (struct range_list));
2262   r->start = start;
2263   r->end = end;
2264   r->next = 0;
2265
2266   /* Append this range to the symbol's range list. */
2267   if (!SYMBOL_RANGES (sym))
2268     SYMBOL_RANGES (sym) = r;
2269   else
2270     {
2271       /* Get the last range for the symbol. */
2272       for (rs = SYMBOL_RANGES (sym); rs->next; rs = rs->next)
2273         ;
2274       rs->next = r;
2275     }
2276 }
2277 \f
2278
2279 /* Skip rest of this symbol and return an error type.
2280
2281    General notes on error recovery:  error_type always skips to the
2282    end of the symbol (modulo cretinous dbx symbol name continuation).
2283    Thus code like this:
2284
2285    if (*(*pp)++ != ';')
2286    return error_type (pp, objfile);
2287
2288    is wrong because if *pp starts out pointing at '\0' (typically as the
2289    result of an earlier error), it will be incremented to point to the
2290    start of the next symbol, which might produce strange results, at least
2291    if you run off the end of the string table.  Instead use
2292
2293    if (**pp != ';')
2294    return error_type (pp, objfile);
2295    ++*pp;
2296
2297    or
2298
2299    if (**pp != ';')
2300    foo = error_type (pp, objfile);
2301    else
2302    ++*pp;
2303
2304    And in case it isn't obvious, the point of all this hair is so the compiler
2305    can define new types and new syntaxes, and old versions of the
2306    debugger will be able to read the new symbol tables.  */
2307
2308 static struct type *
2309 error_type (char **pp, struct objfile *objfile)
2310 {
2311   complain (&error_type_complaint);
2312   while (1)
2313     {
2314       /* Skip to end of symbol.  */
2315       while (**pp != '\0')
2316         {
2317           (*pp)++;
2318         }
2319
2320       /* Check for and handle cretinous dbx symbol name continuation!  */
2321       if ((*pp)[-1] == '\\' || (*pp)[-1] == '?')
2322         {
2323           *pp = next_symbol_text (objfile);
2324         }
2325       else
2326         {
2327           break;
2328         }
2329     }
2330   return (builtin_type_error);
2331 }
2332 \f
2333
2334 /* Read type information or a type definition; return the type.  Even
2335    though this routine accepts either type information or a type
2336    definition, the distinction is relevant--some parts of stabsread.c
2337    assume that type information starts with a digit, '-', or '(' in
2338    deciding whether to call read_type.  */
2339
2340 struct type *
2341 read_type (register char **pp, struct objfile *objfile)
2342 {
2343   register struct type *type = 0;
2344   struct type *type1;
2345   int typenums[2];
2346   char type_descriptor;
2347
2348   /* Size in bits of type if specified by a type attribute, or -1 if
2349      there is no size attribute.  */
2350   int type_size = -1;
2351
2352   /* Used to distinguish string and bitstring from char-array and set. */
2353   int is_string = 0;
2354
2355   /* Read type number if present.  The type number may be omitted.
2356      for instance in a two-dimensional array declared with type
2357      "ar1;1;10;ar1;1;10;4".  */
2358   if ((**pp >= '0' && **pp <= '9')
2359       || **pp == '('
2360       || **pp == '-')
2361     {
2362       if (read_type_number (pp, typenums) != 0)
2363         return error_type (pp, objfile);
2364
2365       /* Type is not being defined here.  Either it already exists,
2366          or this is a forward reference to it.  dbx_alloc_type handles
2367          both cases.  */
2368       if (**pp != '=')
2369         return dbx_alloc_type (typenums, objfile);
2370
2371       /* Type is being defined here.  */
2372       /* Skip the '='.
2373          Also skip the type descriptor - we get it below with (*pp)[-1].  */
2374       (*pp) += 2;
2375     }
2376   else
2377     {
2378       /* 'typenums=' not present, type is anonymous.  Read and return
2379          the definition, but don't put it in the type vector.  */
2380       typenums[0] = typenums[1] = -1;
2381       (*pp)++;
2382     }
2383
2384 again:
2385   type_descriptor = (*pp)[-1];
2386   switch (type_descriptor)
2387     {
2388     case 'x':
2389       {
2390         enum type_code code;
2391
2392         /* Used to index through file_symbols.  */
2393         struct pending *ppt;
2394         int i;
2395
2396         /* Name including "struct", etc.  */
2397         char *type_name;
2398
2399         {
2400           char *from, *to, *p, *q1, *q2;
2401
2402           /* Set the type code according to the following letter.  */
2403           switch ((*pp)[0])
2404             {
2405             case 's':
2406               code = TYPE_CODE_STRUCT;
2407               break;
2408             case 'u':
2409               code = TYPE_CODE_UNION;
2410               break;
2411             case 'e':
2412               code = TYPE_CODE_ENUM;
2413               break;
2414             default:
2415               {
2416                 /* Complain and keep going, so compilers can invent new
2417                    cross-reference types.  */
2418                 static struct complaint msg =
2419                 {"Unrecognized cross-reference type `%c'", 0, 0};
2420                 complain (&msg, (*pp)[0]);
2421                 code = TYPE_CODE_STRUCT;
2422                 break;
2423               }
2424             }
2425
2426           q1 = strchr (*pp, '<');
2427           p = strchr (*pp, ':');
2428           if (p == NULL)
2429             return error_type (pp, objfile);
2430           if (q1 && p > q1 && p[1] == ':')
2431             {
2432               int nesting_level = 0;
2433               for (q2 = q1; *q2; q2++)
2434                 {
2435                   if (*q2 == '<')
2436                     nesting_level++;
2437                   else if (*q2 == '>')
2438                     nesting_level--;
2439                   else if (*q2 == ':' && nesting_level == 0)
2440                     break;
2441                 }
2442               p = q2;
2443               if (*p != ':')
2444                 return error_type (pp, objfile);
2445             }
2446           to = type_name =
2447             (char *) obstack_alloc (&objfile->type_obstack, p - *pp + 1);
2448
2449           /* Copy the name.  */
2450           from = *pp + 1;
2451           while (from < p)
2452             *to++ = *from++;
2453           *to = '\0';
2454
2455           /* Set the pointer ahead of the name which we just read, and
2456              the colon.  */
2457           *pp = from + 1;
2458         }
2459
2460         /* Now check to see whether the type has already been
2461            declared.  This was written for arrays of cross-referenced
2462            types before we had TYPE_CODE_TARGET_STUBBED, so I'm pretty
2463            sure it is not necessary anymore.  But it might be a good
2464            idea, to save a little memory.  */
2465
2466         for (ppt = file_symbols; ppt; ppt = ppt->next)
2467           for (i = 0; i < ppt->nsyms; i++)
2468             {
2469               struct symbol *sym = ppt->symbol[i];
2470
2471               if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2472                   && SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE
2473                   && (TYPE_CODE (SYMBOL_TYPE (sym)) == code)
2474                   && STREQ (SYMBOL_NAME (sym), type_name))
2475                 {
2476                   obstack_free (&objfile->type_obstack, type_name);
2477                   type = SYMBOL_TYPE (sym);
2478                   return type;
2479                 }
2480             }
2481
2482         /* Didn't find the type to which this refers, so we must
2483            be dealing with a forward reference.  Allocate a type
2484            structure for it, and keep track of it so we can
2485            fill in the rest of the fields when we get the full
2486            type.  */
2487         type = dbx_alloc_type (typenums, objfile);
2488         TYPE_CODE (type) = code;
2489         TYPE_TAG_NAME (type) = type_name;
2490         INIT_CPLUS_SPECIFIC (type);
2491         TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
2492
2493         add_undefined_type (type);
2494         return type;
2495       }
2496
2497     case '-':                   /* RS/6000 built-in type */
2498     case '0':
2499     case '1':
2500     case '2':
2501     case '3':
2502     case '4':
2503     case '5':
2504     case '6':
2505     case '7':
2506     case '8':
2507     case '9':
2508     case '(':
2509       (*pp)--;
2510
2511       /* We deal with something like t(1,2)=(3,4)=... which
2512          the Lucid compiler and recent gcc versions (post 2.7.3) use. */
2513
2514       /* Allocate and enter the typedef type first.
2515          This handles recursive types. */
2516       type = dbx_alloc_type (typenums, objfile);
2517       TYPE_CODE (type) = TYPE_CODE_TYPEDEF;
2518       {
2519         struct type *xtype = read_type (pp, objfile);
2520         if (type == xtype)
2521           {
2522             /* It's being defined as itself.  That means it is "void".  */
2523             TYPE_CODE (type) = TYPE_CODE_VOID;
2524             TYPE_LENGTH (type) = 1;
2525           }
2526         else if (type_size >= 0 || is_string)
2527           {
2528             *type = *xtype;
2529             TYPE_NAME (type) = NULL;
2530             TYPE_TAG_NAME (type) = NULL;
2531           }
2532         else
2533           {
2534             TYPE_FLAGS (type) |= TYPE_FLAG_TARGET_STUB;
2535             TYPE_TARGET_TYPE (type) = xtype;
2536           }
2537       }
2538       break;
2539
2540       /* In the following types, we must be sure to overwrite any existing
2541          type that the typenums refer to, rather than allocating a new one
2542          and making the typenums point to the new one.  This is because there
2543          may already be pointers to the existing type (if it had been
2544          forward-referenced), and we must change it to a pointer, function,
2545          reference, or whatever, *in-place*.  */
2546
2547     case '*':
2548       type1 = read_type (pp, objfile);
2549       type = make_pointer_type (type1, dbx_lookup_type (typenums));
2550       break;
2551
2552     case '&':                   /* Reference to another type */
2553       type1 = read_type (pp, objfile);
2554       type = make_reference_type (type1, dbx_lookup_type (typenums));
2555       break;
2556
2557     case 'f':                   /* Function returning another type */
2558       if (os9k_stabs && **pp == '(')
2559         {
2560           /* Function prototype; parse it.
2561              We must conditionalize this on os9k_stabs because otherwise
2562              it could be confused with a Sun-style (1,3) typenumber
2563              (I think).  */
2564           struct type *t;
2565           ++*pp;
2566           while (**pp != ')')
2567             {
2568               t = read_type (pp, objfile);
2569               if (**pp == ',')
2570                 ++ * pp;
2571             }
2572         }
2573       type1 = read_type (pp, objfile);
2574       type = make_function_type (type1, dbx_lookup_type (typenums));
2575       break;
2576
2577     case 'k':                   /* Const qualifier on some type (Sun) */
2578     case 'c':                   /* Const qualifier on some type (OS9000) */
2579       /* Because 'c' means other things to AIX and 'k' is perfectly good,
2580          only accept 'c' in the os9k_stabs case.  */
2581       if (type_descriptor == 'c' && !os9k_stabs)
2582         return error_type (pp, objfile);
2583       type = read_type (pp, objfile);
2584       /* FIXME! For now, we ignore const and volatile qualifiers.  */
2585       break;
2586
2587     case 'B':                   /* Volatile qual on some type (Sun) */
2588     case 'i':                   /* Volatile qual on some type (OS9000) */
2589       /* Because 'i' means other things to AIX and 'B' is perfectly good,
2590          only accept 'i' in the os9k_stabs case.  */
2591       if (type_descriptor == 'i' && !os9k_stabs)
2592         return error_type (pp, objfile);
2593       type = read_type (pp, objfile);
2594       /* FIXME! For now, we ignore const and volatile qualifiers.  */
2595       break;
2596
2597     case '@':
2598       if (isdigit (**pp) || **pp == '(' || **pp == '-')
2599         {                       /* Member (class & variable) type */
2600           /* FIXME -- we should be doing smash_to_XXX types here.  */
2601
2602           struct type *domain = read_type (pp, objfile);
2603           struct type *memtype;
2604
2605           if (**pp != ',')
2606             /* Invalid member type data format.  */
2607             return error_type (pp, objfile);
2608           ++*pp;
2609
2610           memtype = read_type (pp, objfile);
2611           type = dbx_alloc_type (typenums, objfile);
2612           smash_to_member_type (type, domain, memtype);
2613         }
2614       else
2615         /* type attribute */
2616         {
2617           char *attr = *pp;
2618           /* Skip to the semicolon.  */
2619           while (**pp != ';' && **pp != '\0')
2620             ++(*pp);
2621           if (**pp == '\0')
2622             return error_type (pp, objfile);
2623           else
2624             ++ * pp;            /* Skip the semicolon.  */
2625
2626           switch (*attr)
2627             {
2628             case 's':
2629               type_size = atoi (attr + 1);
2630               if (type_size <= 0)
2631                 type_size = -1;
2632               break;
2633
2634             case 'S':
2635               is_string = 1;
2636               break;
2637
2638             default:
2639               /* Ignore unrecognized type attributes, so future compilers
2640                  can invent new ones.  */
2641               break;
2642             }
2643           ++*pp;
2644           goto again;
2645         }
2646       break;
2647
2648     case '#':                   /* Method (class & fn) type */
2649       if ((*pp)[0] == '#')
2650         {
2651           /* We'll get the parameter types from the name.  */
2652           struct type *return_type;
2653
2654           (*pp)++;
2655           return_type = read_type (pp, objfile);
2656           if (*(*pp)++ != ';')
2657             complain (&invalid_member_complaint, symnum);
2658           type = allocate_stub_method (return_type);
2659           if (typenums[0] != -1)
2660             *dbx_lookup_type (typenums) = type;
2661         }
2662       else
2663         {
2664           struct type *domain = read_type (pp, objfile);
2665           struct type *return_type;
2666           struct type **args;
2667
2668           if (**pp != ',')
2669             /* Invalid member type data format.  */
2670             return error_type (pp, objfile);
2671           else
2672             ++(*pp);
2673
2674           return_type = read_type (pp, objfile);
2675           args = read_args (pp, ';', objfile);
2676           type = dbx_alloc_type (typenums, objfile);
2677           smash_to_method_type (type, domain, return_type, args);
2678         }
2679       break;
2680
2681     case 'r':                   /* Range type */
2682       type = read_range_type (pp, typenums, objfile);
2683       if (typenums[0] != -1)
2684         *dbx_lookup_type (typenums) = type;
2685       break;
2686
2687     case 'b':
2688       if (os9k_stabs)
2689         /* Const and volatile qualified type.  */
2690         type = read_type (pp, objfile);
2691       else
2692         {
2693           /* Sun ACC builtin int type */
2694           type = read_sun_builtin_type (pp, typenums, objfile);
2695           if (typenums[0] != -1)
2696             *dbx_lookup_type (typenums) = type;
2697         }
2698       break;
2699
2700     case 'R':                   /* Sun ACC builtin float type */
2701       type = read_sun_floating_type (pp, typenums, objfile);
2702       if (typenums[0] != -1)
2703         *dbx_lookup_type (typenums) = type;
2704       break;
2705
2706     case 'e':                   /* Enumeration type */
2707       type = dbx_alloc_type (typenums, objfile);
2708       type = read_enum_type (pp, type, objfile);
2709       if (typenums[0] != -1)
2710         *dbx_lookup_type (typenums) = type;
2711       break;
2712
2713     case 's':                   /* Struct type */
2714     case 'u':                   /* Union type */
2715       type = dbx_alloc_type (typenums, objfile);
2716       switch (type_descriptor)
2717         {
2718         case 's':
2719           TYPE_CODE (type) = TYPE_CODE_STRUCT;
2720           break;
2721         case 'u':
2722           TYPE_CODE (type) = TYPE_CODE_UNION;
2723           break;
2724         }
2725       type = read_struct_type (pp, type, objfile);
2726       break;
2727
2728     case 'a':                   /* Array type */
2729       if (**pp != 'r')
2730         return error_type (pp, objfile);
2731       ++*pp;
2732
2733       type = dbx_alloc_type (typenums, objfile);
2734       type = read_array_type (pp, type, objfile);
2735       if (is_string)
2736         TYPE_CODE (type) = TYPE_CODE_STRING;
2737       break;
2738
2739     case 'S':
2740       type1 = read_type (pp, objfile);
2741       type = create_set_type ((struct type *) NULL, type1);
2742       if (is_string)
2743         TYPE_CODE (type) = TYPE_CODE_BITSTRING;
2744       if (typenums[0] != -1)
2745         *dbx_lookup_type (typenums) = type;
2746       break;
2747
2748     default:
2749       --*pp;                    /* Go back to the symbol in error */
2750       /* Particularly important if it was \0! */
2751       return error_type (pp, objfile);
2752     }
2753
2754   if (type == 0)
2755     {
2756       warning ("GDB internal error, type is NULL in stabsread.c\n");
2757       return error_type (pp, objfile);
2758     }
2759
2760   /* Size specified in a type attribute overrides any other size.  */
2761   if (type_size != -1)
2762     TYPE_LENGTH (type) = (type_size + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
2763
2764   return type;
2765 }
2766 \f
2767 /* RS/6000 xlc/dbx combination uses a set of builtin types, starting from -1.
2768    Return the proper type node for a given builtin type number. */
2769
2770 static struct type *
2771 rs6000_builtin_type (int typenum)
2772 {
2773   /* We recognize types numbered from -NUMBER_RECOGNIZED to -1.  */
2774 #define NUMBER_RECOGNIZED 34
2775   /* This includes an empty slot for type number -0.  */
2776   static struct type *negative_types[NUMBER_RECOGNIZED + 1];
2777   struct type *rettype = NULL;
2778
2779   if (typenum >= 0 || typenum < -NUMBER_RECOGNIZED)
2780     {
2781       complain (&rs6000_builtin_complaint, typenum);
2782       return builtin_type_error;
2783     }
2784   if (negative_types[-typenum] != NULL)
2785     return negative_types[-typenum];
2786
2787 #if TARGET_CHAR_BIT != 8
2788 #error This code wrong for TARGET_CHAR_BIT not 8
2789   /* These definitions all assume that TARGET_CHAR_BIT is 8.  I think
2790      that if that ever becomes not true, the correct fix will be to
2791      make the size in the struct type to be in bits, not in units of
2792      TARGET_CHAR_BIT.  */
2793 #endif
2794
2795   switch (-typenum)
2796     {
2797     case 1:
2798       /* The size of this and all the other types are fixed, defined
2799          by the debugging format.  If there is a type called "int" which
2800          is other than 32 bits, then it should use a new negative type
2801          number (or avoid negative type numbers for that case).
2802          See stabs.texinfo.  */
2803       rettype = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
2804       break;
2805     case 2:
2806       rettype = init_type (TYPE_CODE_INT, 1, 0, "char", NULL);
2807       break;
2808     case 3:
2809       rettype = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
2810       break;
2811     case 4:
2812       rettype = init_type (TYPE_CODE_INT, 4, 0, "long", NULL);
2813       break;
2814     case 5:
2815       rettype = init_type (TYPE_CODE_INT, 1, TYPE_FLAG_UNSIGNED,
2816                            "unsigned char", NULL);
2817       break;
2818     case 6:
2819       rettype = init_type (TYPE_CODE_INT, 1, 0, "signed char", NULL);
2820       break;
2821     case 7:
2822       rettype = init_type (TYPE_CODE_INT, 2, TYPE_FLAG_UNSIGNED,
2823                            "unsigned short", NULL);
2824       break;
2825     case 8:
2826       rettype = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_UNSIGNED,
2827                            "unsigned int", NULL);
2828       break;
2829     case 9:
2830       rettype = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_UNSIGNED,
2831                            "unsigned", NULL);
2832     case 10:
2833       rettype = init_type (TYPE_CODE_INT, 4, TYPE_FLAG_UNSIGNED,
2834                            "unsigned long", NULL);
2835       break;
2836     case 11:
2837       rettype = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
2838       break;
2839     case 12:
2840       /* IEEE single precision (32 bit).  */
2841       rettype = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
2842       break;
2843     case 13:
2844       /* IEEE double precision (64 bit).  */
2845       rettype = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
2846       break;
2847     case 14:
2848       /* This is an IEEE double on the RS/6000, and different machines with
2849          different sizes for "long double" should use different negative
2850          type numbers.  See stabs.texinfo.  */
2851       rettype = init_type (TYPE_CODE_FLT, 8, 0, "long double", NULL);
2852       break;
2853     case 15:
2854       rettype = init_type (TYPE_CODE_INT, 4, 0, "integer", NULL);
2855       break;
2856     case 16:
2857       rettype = init_type (TYPE_CODE_BOOL, 4, TYPE_FLAG_UNSIGNED,
2858                            "boolean", NULL);
2859       break;
2860     case 17:
2861       rettype = init_type (TYPE_CODE_FLT, 4, 0, "short real", NULL);
2862       break;
2863     case 18:
2864       rettype = init_type (TYPE_CODE_FLT, 8, 0, "real", NULL);
2865       break;
2866     case 19:
2867       rettype = init_type (TYPE_CODE_ERROR, 0, 0, "stringptr", NULL);
2868       break;
2869     case 20:
2870       rettype = init_type (TYPE_CODE_CHAR, 1, TYPE_FLAG_UNSIGNED,
2871                            "character", NULL);
2872       break;
2873     case 21:
2874       rettype = init_type (TYPE_CODE_BOOL, 1, TYPE_FLAG_UNSIGNED,
2875                            "logical*1", NULL);
2876       break;
2877     case 22:
2878       rettype = init_type (TYPE_CODE_BOOL, 2, TYPE_FLAG_UNSIGNED,
2879                            "logical*2", NULL);
2880       break;
2881     case 23:
2882       rettype = init_type (TYPE_CODE_BOOL, 4, TYPE_FLAG_UNSIGNED,
2883                            "logical*4", NULL);
2884       break;
2885     case 24:
2886       rettype = init_type (TYPE_CODE_BOOL, 4, TYPE_FLAG_UNSIGNED,
2887                            "logical", NULL);
2888       break;
2889     case 25:
2890       /* Complex type consisting of two IEEE single precision values.  */
2891       rettype = init_type (TYPE_CODE_COMPLEX, 8, 0, "complex", NULL);
2892       break;
2893     case 26:
2894       /* Complex type consisting of two IEEE double precision values.  */
2895       rettype = init_type (TYPE_CODE_COMPLEX, 16, 0, "double complex", NULL);
2896       break;
2897     case 27:
2898       rettype = init_type (TYPE_CODE_INT, 1, 0, "integer*1", NULL);
2899       break;
2900     case 28:
2901       rettype = init_type (TYPE_CODE_INT, 2, 0, "integer*2", NULL);
2902       break;
2903     case 29:
2904       rettype = init_type (TYPE_CODE_INT, 4, 0, "integer*4", NULL);
2905       break;
2906     case 30:
2907       rettype = init_type (TYPE_CODE_CHAR, 2, 0, "wchar", NULL);
2908       break;
2909     case 31:
2910       rettype = init_type (TYPE_CODE_INT, 8, 0, "long long", NULL);
2911       break;
2912     case 32:
2913       rettype = init_type (TYPE_CODE_INT, 8, TYPE_FLAG_UNSIGNED,
2914                            "unsigned long long", NULL);
2915       break;
2916     case 33:
2917       rettype = init_type (TYPE_CODE_INT, 8, TYPE_FLAG_UNSIGNED,
2918                            "logical*8", NULL);
2919       break;
2920     case 34:
2921       rettype = init_type (TYPE_CODE_INT, 8, 0, "integer*8", NULL);
2922       break;
2923     }
2924   negative_types[-typenum] = rettype;
2925   return rettype;
2926 }
2927 \f
2928 /* This page contains subroutines of read_type.  */
2929
2930 /* Read member function stabs info for C++ classes.  The form of each member
2931    function data is:
2932
2933    NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2934
2935    An example with two member functions is:
2936
2937    afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2938
2939    For the case of overloaded operators, the format is op$::*.funcs, where
2940    $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2941    name (such as `+=') and `.' marks the end of the operator name.
2942
2943    Returns 1 for success, 0 for failure.  */
2944
2945 static int
2946 read_member_functions (struct field_info *fip, char **pp, struct type *type,
2947                        struct objfile *objfile)
2948 {
2949   int nfn_fields = 0;
2950   int length = 0;
2951   /* Total number of member functions defined in this class.  If the class
2952      defines two `f' functions, and one `g' function, then this will have
2953      the value 3.  */
2954   int total_length = 0;
2955   int i;
2956   struct next_fnfield
2957     {
2958       struct next_fnfield *next;
2959       struct fn_field fn_field;
2960     }
2961    *sublist;
2962   struct type *look_ahead_type;
2963   struct next_fnfieldlist *new_fnlist;
2964   struct next_fnfield *new_sublist;
2965   char *main_fn_name;
2966   register char *p;
2967
2968   /* Process each list until we find something that is not a member function
2969      or find the end of the functions. */
2970
2971   while (**pp != ';')
2972     {
2973       /* We should be positioned at the start of the function name.
2974          Scan forward to find the first ':' and if it is not the
2975          first of a "::" delimiter, then this is not a member function. */
2976       p = *pp;
2977       while (*p != ':')
2978         {
2979           p++;
2980         }
2981       if (p[1] != ':')
2982         {
2983           break;
2984         }
2985
2986       sublist = NULL;
2987       look_ahead_type = NULL;
2988       length = 0;
2989
2990       new_fnlist = (struct next_fnfieldlist *)
2991         xmalloc (sizeof (struct next_fnfieldlist));
2992       make_cleanup (xfree, new_fnlist);
2993       memset (new_fnlist, 0, sizeof (struct next_fnfieldlist));
2994
2995       if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && is_cplus_marker ((*pp)[2]))
2996         {
2997           /* This is a completely wierd case.  In order to stuff in the
2998              names that might contain colons (the usual name delimiter),
2999              Mike Tiemann defined a different name format which is
3000              signalled if the identifier is "op$".  In that case, the
3001              format is "op$::XXXX." where XXXX is the name.  This is
3002              used for names like "+" or "=".  YUUUUUUUK!  FIXME!  */
3003           /* This lets the user type "break operator+".
3004              We could just put in "+" as the name, but that wouldn't
3005              work for "*".  */
3006           static char opname[32] =
3007           {'o', 'p', CPLUS_MARKER};
3008           char *o = opname + 3;
3009
3010           /* Skip past '::'.  */
3011           *pp = p + 2;
3012
3013           STABS_CONTINUE (pp, objfile);
3014           p = *pp;
3015           while (*p != '.')
3016             {
3017               *o++ = *p++;
3018             }
3019           main_fn_name = savestring (opname, o - opname);
3020           /* Skip past '.'  */
3021           *pp = p + 1;
3022         }
3023       else
3024         {
3025           main_fn_name = savestring (*pp, p - *pp);
3026           /* Skip past '::'.  */
3027           *pp = p + 2;
3028         }
3029       new_fnlist->fn_fieldlist.name = main_fn_name;
3030
3031       do
3032         {
3033           new_sublist =
3034             (struct next_fnfield *) xmalloc (sizeof (struct next_fnfield));
3035           make_cleanup (xfree, new_sublist);
3036           memset (new_sublist, 0, sizeof (struct next_fnfield));
3037
3038           /* Check for and handle cretinous dbx symbol name continuation!  */
3039           if (look_ahead_type == NULL)
3040             {
3041               /* Normal case. */
3042               STABS_CONTINUE (pp, objfile);
3043
3044               new_sublist->fn_field.type = read_type (pp, objfile);
3045               if (**pp != ':')
3046                 {
3047                   /* Invalid symtab info for member function.  */
3048                   return 0;
3049                 }
3050             }
3051           else
3052             {
3053               /* g++ version 1 kludge */
3054               new_sublist->fn_field.type = look_ahead_type;
3055               look_ahead_type = NULL;
3056             }
3057
3058           (*pp)++;
3059           p = *pp;
3060           while (*p != ';')
3061             {
3062               p++;
3063             }
3064
3065           /* If this is just a stub, then we don't have the real name here. */
3066
3067           if (TYPE_FLAGS (new_sublist->fn_field.type) & TYPE_FLAG_STUB)
3068             {
3069               if (!TYPE_DOMAIN_TYPE (new_sublist->fn_field.type))
3070                 TYPE_DOMAIN_TYPE (new_sublist->fn_field.type) = type;
3071               new_sublist->fn_field.is_stub = 1;
3072             }
3073           new_sublist->fn_field.physname = savestring (*pp, p - *pp);
3074           *pp = p + 1;
3075
3076           /* Set this member function's visibility fields.  */
3077           switch (*(*pp)++)
3078             {
3079             case VISIBILITY_PRIVATE:
3080               new_sublist->fn_field.is_private = 1;
3081               break;
3082             case VISIBILITY_PROTECTED:
3083               new_sublist->fn_field.is_protected = 1;
3084               break;
3085             }
3086
3087           STABS_CONTINUE (pp, objfile);
3088           switch (**pp)
3089             {
3090             case 'A':           /* Normal functions. */
3091               new_sublist->fn_field.is_const = 0;
3092               new_sublist->fn_field.is_volatile = 0;
3093               (*pp)++;
3094               break;
3095             case 'B':           /* `const' member functions. */
3096               new_sublist->fn_field.is_const = 1;
3097               new_sublist->fn_field.is_volatile = 0;
3098               (*pp)++;
3099               break;
3100             case 'C':           /* `volatile' member function. */
3101               new_sublist->fn_field.is_const = 0;
3102               new_sublist->fn_field.is_volatile = 1;
3103               (*pp)++;
3104               break;
3105             case 'D':           /* `const volatile' member function. */
3106               new_sublist->fn_field.is_const = 1;
3107               new_sublist->fn_field.is_volatile = 1;
3108               (*pp)++;
3109               break;
3110             case '*':           /* File compiled with g++ version 1 -- no info */
3111             case '?':
3112             case '.':
3113               break;
3114             default:
3115               complain (&const_vol_complaint, **pp);
3116               break;
3117             }
3118
3119           switch (*(*pp)++)
3120             {
3121             case '*':
3122               {
3123                 int nbits;
3124                 /* virtual member function, followed by index.
3125                    The sign bit is set to distinguish pointers-to-methods
3126                    from virtual function indicies.  Since the array is
3127                    in words, the quantity must be shifted left by 1
3128                    on 16 bit machine, and by 2 on 32 bit machine, forcing
3129                    the sign bit out, and usable as a valid index into
3130                    the array.  Remove the sign bit here.  */
3131                 new_sublist->fn_field.voffset =
3132                   (0x7fffffff & read_huge_number (pp, ';', &nbits)) + 2;
3133                 if (nbits != 0)
3134                   return 0;
3135
3136                 STABS_CONTINUE (pp, objfile);
3137                 if (**pp == ';' || **pp == '\0')
3138                   {
3139                     /* Must be g++ version 1.  */
3140                     new_sublist->fn_field.fcontext = 0;
3141                   }
3142                 else
3143                   {
3144                     /* Figure out from whence this virtual function came.
3145                        It may belong to virtual function table of
3146                        one of its baseclasses.  */
3147                     look_ahead_type = read_type (pp, objfile);
3148                     if (**pp == ':')
3149                       {
3150                         /* g++ version 1 overloaded methods. */
3151                       }
3152                     else
3153                       {
3154                         new_sublist->fn_field.fcontext = look_ahead_type;
3155                         if (**pp != ';')
3156                           {
3157                             return 0;
3158                           }
3159                         else
3160                           {
3161                             ++*pp;
3162                           }
3163                         look_ahead_type = NULL;
3164                       }
3165                   }
3166                 break;
3167               }
3168             case '?':
3169               /* static member function.  */
3170               new_sublist->fn_field.voffset = VOFFSET_STATIC;
3171               if (strncmp (new_sublist->fn_field.physname,
3172                            main_fn_name, strlen (main_fn_name)))
3173                 {
3174                   new_sublist->fn_field.is_stub = 1;
3175                 }
3176               break;
3177
3178             default:
3179               /* error */
3180               complain (&member_fn_complaint, (*pp)[-1]);
3181               /* Fall through into normal member function.  */
3182
3183             case '.':
3184               /* normal member function.  */
3185               new_sublist->fn_field.voffset = 0;
3186               new_sublist->fn_field.fcontext = 0;
3187               break;
3188             }
3189
3190           new_sublist->next = sublist;
3191           sublist = new_sublist;
3192           length++;
3193           STABS_CONTINUE (pp, objfile);
3194         }
3195       while (**pp != ';' && **pp != '\0');
3196
3197       (*pp)++;
3198
3199       new_fnlist->fn_fieldlist.fn_fields = (struct fn_field *)
3200         obstack_alloc (&objfile->type_obstack,
3201                        sizeof (struct fn_field) * length);
3202       memset (new_fnlist->fn_fieldlist.fn_fields, 0,
3203               sizeof (struct fn_field) * length);
3204       for (i = length; (i--, sublist); sublist = sublist->next)
3205         {
3206           new_fnlist->fn_fieldlist.fn_fields[i] = sublist->fn_field;
3207         }
3208
3209       new_fnlist->fn_fieldlist.length = length;
3210       new_fnlist->next = fip->fnlist;
3211       fip->fnlist = new_fnlist;
3212       nfn_fields++;
3213       total_length += length;
3214       STABS_CONTINUE (pp, objfile);
3215     }
3216
3217   if (nfn_fields)
3218     {
3219       ALLOCATE_CPLUS_STRUCT_TYPE (type);
3220       TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
3221         TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * nfn_fields);
3222       memset (TYPE_FN_FIELDLISTS (type), 0,
3223               sizeof (struct fn_fieldlist) * nfn_fields);
3224       TYPE_NFN_FIELDS (type) = nfn_fields;
3225       TYPE_NFN_FIELDS_TOTAL (type) = total_length;
3226     }
3227
3228   return 1;
3229 }
3230
3231 /* Special GNU C++ name.
3232
3233    Returns 1 for success, 0 for failure.  "failure" means that we can't
3234    keep parsing and it's time for error_type().  */
3235
3236 static int
3237 read_cpp_abbrev (struct field_info *fip, char **pp, struct type *type,
3238                  struct objfile *objfile)
3239 {
3240   register char *p;
3241   char *name;
3242   char cpp_abbrev;
3243   struct type *context;
3244
3245   p = *pp;
3246   if (*++p == 'v')
3247     {
3248       name = NULL;
3249       cpp_abbrev = *++p;
3250
3251       *pp = p + 1;
3252
3253       /* At this point, *pp points to something like "22:23=*22...",
3254          where the type number before the ':' is the "context" and
3255          everything after is a regular type definition.  Lookup the
3256          type, find it's name, and construct the field name. */
3257
3258       context = read_type (pp, objfile);
3259
3260       switch (cpp_abbrev)
3261         {
3262         case 'f':               /* $vf -- a virtual function table pointer */
3263           fip->list->field.name =
3264             obconcat (&objfile->type_obstack, vptr_name, "", "");
3265           break;
3266
3267         case 'b':               /* $vb -- a virtual bsomethingorother */
3268           name = type_name_no_tag (context);
3269           if (name == NULL)
3270             {
3271               complain (&invalid_cpp_type_complaint, symnum);
3272               name = "FOO";
3273             }
3274           fip->list->field.name =
3275             obconcat (&objfile->type_obstack, vb_name, name, "");
3276           break;
3277
3278         default:
3279           complain (&invalid_cpp_abbrev_complaint, *pp);
3280           fip->list->field.name =
3281             obconcat (&objfile->type_obstack,
3282                       "INVALID_CPLUSPLUS_ABBREV", "", "");
3283           break;
3284         }
3285
3286       /* At this point, *pp points to the ':'.  Skip it and read the
3287          field type. */
3288
3289       p = ++(*pp);
3290       if (p[-1] != ':')
3291         {
3292           complain (&invalid_cpp_abbrev_complaint, *pp);
3293           return 0;
3294         }
3295       fip->list->field.type = read_type (pp, objfile);
3296       if (**pp == ',')
3297         (*pp)++;                /* Skip the comma.  */
3298       else
3299         return 0;
3300
3301       {
3302         int nbits;
3303         FIELD_BITPOS (fip->list->field) = read_huge_number (pp, ';', &nbits);
3304         if (nbits != 0)
3305           return 0;
3306       }
3307       /* This field is unpacked.  */
3308       FIELD_BITSIZE (fip->list->field) = 0;
3309       fip->list->visibility = VISIBILITY_PRIVATE;
3310     }
3311   else
3312     {
3313       complain (&invalid_cpp_abbrev_complaint, *pp);
3314       /* We have no idea what syntax an unrecognized abbrev would have, so
3315          better return 0.  If we returned 1, we would need to at least advance
3316          *pp to avoid an infinite loop.  */
3317       return 0;
3318     }
3319   return 1;
3320 }
3321
3322 static void
3323 read_one_struct_field (struct field_info *fip, char **pp, char *p,
3324                        struct type *type, struct objfile *objfile)
3325 {
3326   /* The following is code to work around cfront generated stabs.
3327      The stabs contains full mangled name for each field.
3328      We try to demangle the name and extract the field name out of it.
3329    */
3330   if (ARM_DEMANGLING && current_subfile->language == language_cplus)
3331     {
3332       char save_p;
3333       char *dem, *dem_p;
3334       save_p = *p;
3335       *p = '\0';
3336       dem = cplus_demangle (*pp, DMGL_ANSI | DMGL_PARAMS);
3337       if (dem != NULL)
3338         {
3339           dem_p = strrchr (dem, ':');
3340           if (dem_p != 0 && *(dem_p - 1) == ':')
3341             dem_p++;
3342           FIELD_NAME (fip->list->field) =
3343             obsavestring (dem_p, strlen (dem_p), &objfile->type_obstack);
3344         }
3345       else
3346         {
3347           FIELD_NAME (fip->list->field) =
3348             obsavestring (*pp, p - *pp, &objfile->type_obstack);
3349         }
3350       *p = save_p;
3351     }
3352   /* end of code for cfront work around */
3353
3354   else
3355     fip->list->field.name =
3356       obsavestring (*pp, p - *pp, &objfile->type_obstack);
3357   *pp = p + 1;
3358
3359   /* This means we have a visibility for a field coming. */
3360   if (**pp == '/')
3361     {
3362       (*pp)++;
3363       fip->list->visibility = *(*pp)++;
3364     }
3365   else
3366     {
3367       /* normal dbx-style format, no explicit visibility */
3368       fip->list->visibility = VISIBILITY_PUBLIC;
3369     }
3370
3371   fip->list->field.type = read_type (pp, objfile);
3372   if (**pp == ':')
3373     {
3374       p = ++(*pp);
3375 #if 0
3376       /* Possible future hook for nested types. */
3377       if (**pp == '!')
3378         {
3379           fip->list->field.bitpos = (long) -2;  /* nested type */
3380           p = ++(*pp);
3381         }
3382       else
3383         ...;
3384 #endif
3385       while (*p != ';')
3386         {
3387           p++;
3388         }
3389       /* Static class member.  */
3390       SET_FIELD_PHYSNAME (fip->list->field, savestring (*pp, p - *pp));
3391       *pp = p + 1;
3392       return;
3393     }
3394   else if (**pp != ',')
3395     {
3396       /* Bad structure-type format.  */
3397       complain (&stabs_general_complaint, "bad structure-type format");
3398       return;
3399     }
3400
3401   (*pp)++;                      /* Skip the comma.  */
3402
3403   {
3404     int nbits;
3405     FIELD_BITPOS (fip->list->field) = read_huge_number (pp, ',', &nbits);
3406     if (nbits != 0)
3407       {
3408         complain (&stabs_general_complaint, "bad structure-type format");
3409         return;
3410       }
3411     FIELD_BITSIZE (fip->list->field) = read_huge_number (pp, ';', &nbits);
3412     if (nbits != 0)
3413       {
3414         complain (&stabs_general_complaint, "bad structure-type format");
3415         return;
3416       }
3417   }
3418
3419   if (FIELD_BITPOS (fip->list->field) == 0
3420       && FIELD_BITSIZE (fip->list->field) == 0)
3421     {
3422       /* This can happen in two cases: (1) at least for gcc 2.4.5 or so,
3423          it is a field which has been optimized out.  The correct stab for
3424          this case is to use VISIBILITY_IGNORE, but that is a recent
3425          invention.  (2) It is a 0-size array.  For example
3426          union { int num; char str[0]; } foo.  Printing "<no value>" for
3427          str in "p foo" is OK, since foo.str (and thus foo.str[3])
3428          will continue to work, and a 0-size array as a whole doesn't
3429          have any contents to print.
3430
3431          I suspect this probably could also happen with gcc -gstabs (not
3432          -gstabs+) for static fields, and perhaps other C++ extensions.
3433          Hopefully few people use -gstabs with gdb, since it is intended
3434          for dbx compatibility.  */
3435
3436       /* Ignore this field.  */
3437       fip->list->visibility = VISIBILITY_IGNORE;
3438     }
3439   else
3440     {
3441       /* Detect an unpacked field and mark it as such.
3442          dbx gives a bit size for all fields.
3443          Note that forward refs cannot be packed,
3444          and treat enums as if they had the width of ints.  */
3445
3446       struct type *field_type = check_typedef (FIELD_TYPE (fip->list->field));
3447
3448       if (TYPE_CODE (field_type) != TYPE_CODE_INT
3449           && TYPE_CODE (field_type) != TYPE_CODE_RANGE
3450           && TYPE_CODE (field_type) != TYPE_CODE_BOOL
3451           && TYPE_CODE (field_type) != TYPE_CODE_ENUM)
3452         {
3453           FIELD_BITSIZE (fip->list->field) = 0;
3454         }
3455       if ((FIELD_BITSIZE (fip->list->field)
3456            == TARGET_CHAR_BIT * TYPE_LENGTH (field_type)
3457            || (TYPE_CODE (field_type) == TYPE_CODE_ENUM
3458                && FIELD_BITSIZE (fip->list->field) == TARGET_INT_BIT)
3459           )
3460           &&
3461           FIELD_BITPOS (fip->list->field) % 8 == 0)
3462         {
3463           FIELD_BITSIZE (fip->list->field) = 0;
3464         }
3465     }
3466 }
3467
3468
3469 /* Read struct or class data fields.  They have the form:
3470
3471    NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
3472
3473    At the end, we see a semicolon instead of a field.
3474
3475    In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
3476    a static field.
3477
3478    The optional VISIBILITY is one of:
3479
3480    '/0' (VISIBILITY_PRIVATE)
3481    '/1' (VISIBILITY_PROTECTED)
3482    '/2' (VISIBILITY_PUBLIC)
3483    '/9' (VISIBILITY_IGNORE)
3484
3485    or nothing, for C style fields with public visibility.
3486
3487    Returns 1 for success, 0 for failure.  */
3488
3489 static int
3490 read_struct_fields (struct field_info *fip, char **pp, struct type *type,
3491                     struct objfile *objfile)
3492 {
3493   register char *p;
3494   struct nextfield *new;
3495
3496   /* We better set p right now, in case there are no fields at all...    */
3497
3498   p = *pp;
3499
3500   /* Read each data member type until we find the terminating ';' at the end of
3501      the data member list, or break for some other reason such as finding the
3502      start of the member function list. */
3503
3504   while (**pp != ';')
3505     {
3506       if (os9k_stabs && **pp == ',')
3507         break;
3508       STABS_CONTINUE (pp, objfile);
3509       /* Get space to record the next field's data.  */
3510       new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
3511       make_cleanup (xfree, new);
3512       memset (new, 0, sizeof (struct nextfield));
3513       new->next = fip->list;
3514       fip->list = new;
3515
3516       /* Get the field name.  */
3517       p = *pp;
3518
3519       /* If is starts with CPLUS_MARKER it is a special abbreviation,
3520          unless the CPLUS_MARKER is followed by an underscore, in
3521          which case it is just the name of an anonymous type, which we
3522          should handle like any other type name.  */
3523
3524       if (is_cplus_marker (p[0]) && p[1] != '_')
3525         {
3526           if (!read_cpp_abbrev (fip, pp, type, objfile))
3527             return 0;
3528           continue;
3529         }
3530
3531       /* Look for the ':' that separates the field name from the field
3532          values.  Data members are delimited by a single ':', while member
3533          functions are delimited by a pair of ':'s.  When we hit the member
3534          functions (if any), terminate scan loop and return. */
3535
3536       while (*p != ':' && *p != '\0')
3537         {
3538           p++;
3539         }
3540       if (*p == '\0')
3541         return 0;
3542
3543       /* Check to see if we have hit the member functions yet.  */
3544       if (p[1] == ':')
3545         {
3546           break;
3547         }
3548       read_one_struct_field (fip, pp, p, type, objfile);
3549     }
3550   if (p[0] == ':' && p[1] == ':')
3551     {
3552       /* chill the list of fields: the last entry (at the head) is a
3553          partially constructed entry which we now scrub. */
3554       fip->list = fip->list->next;
3555     }
3556   return 1;
3557 }
3558 /* *INDENT-OFF* */
3559 /* The stabs for C++ derived classes contain baseclass information which
3560    is marked by a '!' character after the total size.  This function is
3561    called when we encounter the baseclass marker, and slurps up all the
3562    baseclass information.
3563
3564    Immediately following the '!' marker is the number of base classes that
3565    the class is derived from, followed by information for each base class.
3566    For each base class, there are two visibility specifiers, a bit offset
3567    to the base class information within the derived class, a reference to
3568    the type for the base class, and a terminating semicolon.
3569
3570    A typical example, with two base classes, would be "!2,020,19;0264,21;".
3571                                                        ^^ ^ ^ ^  ^ ^  ^
3572         Baseclass information marker __________________|| | | |  | |  |
3573         Number of baseclasses __________________________| | | |  | |  |
3574         Visibility specifiers (2) ________________________| | |  | |  |
3575         Offset in bits from start of class _________________| |  | |  |
3576         Type number for base class ___________________________|  | |  |
3577         Visibility specifiers (2) _______________________________| |  |
3578         Offset in bits from start of class ________________________|  |
3579         Type number of base class ____________________________________|
3580
3581   Return 1 for success, 0 for (error-type-inducing) failure.  */
3582 /* *INDENT-ON* */
3583
3584
3585
3586 static int
3587 read_baseclasses (struct field_info *fip, char **pp, struct type *type,
3588                   struct objfile *objfile)
3589 {
3590   int i;
3591   struct nextfield *new;
3592
3593   if (**pp != '!')
3594     {
3595       return 1;
3596     }
3597   else
3598     {
3599       /* Skip the '!' baseclass information marker. */
3600       (*pp)++;
3601     }
3602
3603   ALLOCATE_CPLUS_STRUCT_TYPE (type);
3604   {
3605     int nbits;
3606     TYPE_N_BASECLASSES (type) = read_huge_number (pp, ',', &nbits);
3607     if (nbits != 0)
3608       return 0;
3609   }
3610
3611 #if 0
3612   /* Some stupid compilers have trouble with the following, so break
3613      it up into simpler expressions.  */
3614   TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
3615     TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
3616 #else
3617   {
3618     int num_bytes = B_BYTES (TYPE_N_BASECLASSES (type));
3619     char *pointer;
3620
3621     pointer = (char *) TYPE_ALLOC (type, num_bytes);
3622     TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
3623   }
3624 #endif /* 0 */
3625
3626   B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
3627
3628   for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
3629     {
3630       new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
3631       make_cleanup (xfree, new);
3632       memset (new, 0, sizeof (struct nextfield));
3633       new->next = fip->list;
3634       fip->list = new;
3635       FIELD_BITSIZE (new->field) = 0;   /* this should be an unpacked field! */
3636
3637       STABS_CONTINUE (pp, objfile);
3638       switch (**pp)
3639         {
3640         case '0':
3641           /* Nothing to do. */
3642           break;
3643         case '1':
3644           SET_TYPE_FIELD_VIRTUAL (type, i);
3645           break;
3646         default:
3647           /* Unknown character.  Complain and treat it as non-virtual.  */
3648           {
3649             static struct complaint msg =
3650             {
3651               "Unknown virtual character `%c' for baseclass", 0, 0};
3652             complain (&msg, **pp);
3653           }
3654         }
3655       ++(*pp);
3656
3657       new->visibility = *(*pp)++;
3658       switch (new->visibility)
3659         {
3660         case VISIBILITY_PRIVATE:
3661         case VISIBILITY_PROTECTED:
3662         case VISIBILITY_PUBLIC:
3663           break;
3664         default:
3665           /* Bad visibility format.  Complain and treat it as
3666              public.  */
3667           {
3668             static struct complaint msg =
3669             {
3670               "Unknown visibility `%c' for baseclass", 0, 0
3671             };
3672             complain (&msg, new->visibility);
3673             new->visibility = VISIBILITY_PUBLIC;
3674           }
3675         }
3676
3677       {
3678         int nbits;
3679
3680         /* The remaining value is the bit offset of the portion of the object
3681            corresponding to this baseclass.  Always zero in the absence of
3682            multiple inheritance.  */
3683
3684         FIELD_BITPOS (new->field) = read_huge_number (pp, ',', &nbits);
3685         if (nbits != 0)
3686           return 0;
3687       }
3688
3689       /* The last piece of baseclass information is the type of the
3690          base class.  Read it, and remember it's type name as this
3691          field's name. */
3692
3693       new->field.type = read_type (pp, objfile);
3694       new->field.name = type_name_no_tag (new->field.type);
3695
3696       /* skip trailing ';' and bump count of number of fields seen */
3697       if (**pp == ';')
3698         (*pp)++;
3699       else
3700         return 0;
3701     }
3702   return 1;
3703 }
3704
3705 /* The tail end of stabs for C++ classes that contain a virtual function
3706    pointer contains a tilde, a %, and a type number.
3707    The type number refers to the base class (possibly this class itself) which
3708    contains the vtable pointer for the current class.
3709
3710    This function is called when we have parsed all the method declarations,
3711    so we can look for the vptr base class info.  */
3712
3713 static int
3714 read_tilde_fields (struct field_info *fip, char **pp, struct type *type,
3715                    struct objfile *objfile)
3716 {
3717   register char *p;
3718
3719   STABS_CONTINUE (pp, objfile);
3720
3721   /* If we are positioned at a ';', then skip it. */
3722   if (**pp == ';')
3723     {
3724       (*pp)++;
3725     }
3726
3727   if (**pp == '~')
3728     {
3729       (*pp)++;
3730
3731       if (**pp == '=' || **pp == '+' || **pp == '-')
3732         {
3733           /* Obsolete flags that used to indicate the presence
3734              of constructors and/or destructors. */
3735           (*pp)++;
3736         }
3737
3738       /* Read either a '%' or the final ';'.  */
3739       if (*(*pp)++ == '%')
3740         {
3741           /* The next number is the type number of the base class
3742              (possibly our own class) which supplies the vtable for
3743              this class.  Parse it out, and search that class to find
3744              its vtable pointer, and install those into TYPE_VPTR_BASETYPE
3745              and TYPE_VPTR_FIELDNO.  */
3746
3747           struct type *t;
3748           int i;
3749
3750           t = read_type (pp, objfile);
3751           p = (*pp)++;
3752           while (*p != '\0' && *p != ';')
3753             {
3754               p++;
3755             }
3756           if (*p == '\0')
3757             {
3758               /* Premature end of symbol.  */
3759               return 0;
3760             }
3761
3762           TYPE_VPTR_BASETYPE (type) = t;
3763           if (type == t)        /* Our own class provides vtbl ptr */
3764             {
3765               for (i = TYPE_NFIELDS (t) - 1;
3766                    i >= TYPE_N_BASECLASSES (t);
3767                    --i)
3768                 {
3769                   if (!strncmp (TYPE_FIELD_NAME (t, i), vptr_name,
3770                                 sizeof (vptr_name) - 1))
3771                     {
3772                       TYPE_VPTR_FIELDNO (type) = i;
3773                       goto gotit;
3774                     }
3775                 }
3776               /* Virtual function table field not found.  */
3777               complain (&vtbl_notfound_complaint, TYPE_NAME (type));
3778               return 0;
3779             }
3780           else
3781             {
3782               TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
3783             }
3784
3785         gotit:
3786           *pp = p + 1;
3787         }
3788     }
3789   return 1;
3790 }
3791
3792 static int
3793 attach_fn_fields_to_type (struct field_info *fip, register struct type *type)
3794 {
3795   register int n;
3796
3797   for (n = TYPE_NFN_FIELDS (type);
3798        fip->fnlist != NULL;
3799        fip->fnlist = fip->fnlist->next)
3800     {
3801       --n;                      /* Circumvent Sun3 compiler bug */
3802       TYPE_FN_FIELDLISTS (type)[n] = fip->fnlist->fn_fieldlist;
3803     }
3804   return 1;
3805 }
3806
3807 /* read cfront class static data.
3808    pp points to string starting with the list of static data
3809    eg: A:ZcA;1@Bpub v2@Bvirpri;__ct__1AFv func__1AFv *sfunc__1AFv ;as__1A ;;
3810    ^^^^^^^^
3811
3812    A:ZcA;;foopri__1AFv foopro__1AFv __ct__1AFv __ct__1AFRC1A foopub__1AFv ;;;
3813    ^
3814  */
3815
3816 static int
3817 read_cfront_static_fields (struct field_info *fip, char **pp, struct type *type,
3818                            struct objfile *objfile)
3819 {
3820   struct nextfield *new;
3821   struct type *stype;
3822   char *sname;
3823   struct symbol *ref_static = 0;
3824
3825   if (**pp == ';')              /* no static data; return */
3826     {
3827       ++(*pp);
3828       return 1;
3829     }
3830
3831   /* Process each field in the list until we find the terminating ";" */
3832
3833   /* eg: p = "as__1A ;;;" */
3834   STABS_CONTINUE (pp, objfile); /* handle \\ */
3835   while (**pp != ';' && (sname = get_substring (pp, ' '), sname))
3836     {
3837       ref_static = lookup_symbol (sname, 0, VAR_NAMESPACE, 0, 0);       /*demangled_name */
3838       if (!ref_static)
3839         {
3840           static struct complaint msg =
3841           {"\
3842                 Unable to find symbol for static data field %s\n",
3843            0, 0};
3844           complain (&msg, sname);
3845           continue;
3846         }
3847       stype = SYMBOL_TYPE (ref_static);
3848
3849       /* allocate a new fip */
3850       new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
3851       make_cleanup (xfree, new);
3852       memset (new, 0, sizeof (struct nextfield));
3853       new->next = fip->list;
3854       fip->list = new;
3855
3856       /* set visibility */
3857       /* FIXME! no way to tell visibility from stabs??? */
3858       new->visibility = VISIBILITY_PUBLIC;
3859
3860       /* set field info into fip */
3861       fip->list->field.type = stype;
3862
3863       /* set bitpos & bitsize */
3864       SET_FIELD_PHYSNAME (fip->list->field, savestring (sname, strlen (sname)));
3865
3866       /* set name field */
3867       /* The following is code to work around cfront generated stabs.
3868          The stabs contains full mangled name for each field.
3869          We try to demangle the name and extract the field name out of it.
3870        */
3871       if (ARM_DEMANGLING)
3872         {
3873           char *dem, *dem_p;
3874           dem = cplus_demangle (sname, DMGL_ANSI | DMGL_PARAMS);
3875           if (dem != NULL)
3876             {
3877               dem_p = strrchr (dem, ':');
3878               if (dem_p != 0 && *(dem_p - 1) == ':')
3879                 dem_p++;
3880               fip->list->field.name =
3881                 obsavestring (dem_p, strlen (dem_p), &objfile->type_obstack);
3882             }
3883           else
3884             {
3885               fip->list->field.name =
3886                 obsavestring (sname, strlen (sname), &objfile->type_obstack);
3887             }
3888         }                       /* end of code for cfront work around */
3889     }                           /* loop again for next static field */
3890   return 1;
3891 }
3892
3893 /* Copy structure fields to fip so attach_fields_to_type will work.
3894    type has already been created with the initial instance data fields.
3895    Now we want to be able to add the other members to the class,
3896    so we want to add them back to the fip and reattach them again
3897    once we have collected all the class members. */
3898
3899 static int
3900 copy_cfront_struct_fields (struct field_info *fip, struct type *type,
3901                            struct objfile *objfile)
3902 {
3903   int nfields = TYPE_NFIELDS (type);
3904   int i;
3905   struct nextfield *new;
3906
3907   /* Copy the fields into the list of fips and reset the types 
3908      to remove the old fields */
3909
3910   for (i = 0; i < nfields; i++)
3911     {
3912       /* allocate a new fip */
3913       new = (struct nextfield *) xmalloc (sizeof (struct nextfield));
3914       make_cleanup (xfree, new);
3915       memset (new, 0, sizeof (struct nextfield));
3916       new->next = fip->list;
3917       fip->list = new;
3918
3919       /* copy field info into fip */
3920       new->field = TYPE_FIELD (type, i);
3921       /* set visibility */
3922       if (TYPE_FIELD_PROTECTED (type, i))
3923         new->visibility = VISIBILITY_PROTECTED;
3924       else if (TYPE_FIELD_PRIVATE (type, i))
3925         new->visibility = VISIBILITY_PRIVATE;
3926       else
3927         new->visibility = VISIBILITY_PUBLIC;
3928     }
3929   /* Now delete the fields from the type since we will be 
3930      allocing new space once we get the rest of the fields 
3931      in attach_fields_to_type.
3932      The pointer TYPE_FIELDS(type) is left dangling but should 
3933      be freed later by objstack_free */
3934   TYPE_FIELDS (type) = 0;
3935   TYPE_NFIELDS (type) = 0;
3936
3937   return 1;
3938 }
3939
3940 /* Create the vector of fields, and record how big it is.
3941    We need this info to record proper virtual function table information
3942    for this class's virtual functions.  */
3943
3944 static int
3945 attach_fields_to_type (struct field_info *fip, register struct type *type,
3946                        struct objfile *objfile)
3947 {
3948   register int nfields = 0;
3949   register int non_public_fields = 0;
3950   register struct nextfield *scan;
3951
3952   /* Count up the number of fields that we have, as well as taking note of
3953      whether or not there are any non-public fields, which requires us to
3954      allocate and build the private_field_bits and protected_field_bits
3955      bitfields. */
3956
3957   for (scan = fip->list; scan != NULL; scan = scan->next)
3958     {
3959       nfields++;
3960       if (scan->visibility != VISIBILITY_PUBLIC)
3961         {
3962           non_public_fields++;
3963         }
3964     }
3965
3966   /* Now we know how many fields there are, and whether or not there are any
3967      non-public fields.  Record the field count, allocate space for the
3968      array of fields, and create blank visibility bitfields if necessary. */
3969
3970   TYPE_NFIELDS (type) = nfields;
3971   TYPE_FIELDS (type) = (struct field *)
3972     TYPE_ALLOC (type, sizeof (struct field) * nfields);
3973   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
3974
3975   if (non_public_fields)
3976     {
3977       ALLOCATE_CPLUS_STRUCT_TYPE (type);
3978
3979       TYPE_FIELD_PRIVATE_BITS (type) =
3980         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3981       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
3982
3983       TYPE_FIELD_PROTECTED_BITS (type) =
3984         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3985       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
3986
3987       TYPE_FIELD_IGNORE_BITS (type) =
3988         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3989       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
3990     }
3991
3992   /* Copy the saved-up fields into the field vector.  Start from the head
3993      of the list, adding to the tail of the field array, so that they end
3994      up in the same order in the array in which they were added to the list. */
3995
3996   while (nfields-- > 0)
3997     {
3998       TYPE_FIELD (type, nfields) = fip->list->field;
3999       switch (fip->list->visibility)
4000         {
4001         case VISIBILITY_PRIVATE:
4002           SET_TYPE_FIELD_PRIVATE (type, nfields);
4003           break;
4004
4005         case VISIBILITY_PROTECTED:
4006           SET_TYPE_FIELD_PROTECTED (type, nfields);
4007           break;
4008
4009         case VISIBILITY_IGNORE:
4010           SET_TYPE_FIELD_IGNORE (type, nfields);
4011           break;
4012
4013         case VISIBILITY_PUBLIC:
4014           break;
4015
4016         default:
4017           /* Unknown visibility.  Complain and treat it as public.  */
4018           {
4019             static struct complaint msg =
4020             {
4021               "Unknown visibility `%c' for field", 0, 0};
4022             complain (&msg, fip->list->visibility);
4023           }
4024           break;
4025         }
4026       fip->list = fip->list->next;
4027     }
4028   return 1;
4029 }
4030
4031 /* Read the description of a structure (or union type) and return an object
4032    describing the type.
4033
4034    PP points to a character pointer that points to the next unconsumed token
4035    in the the stabs string.  For example, given stabs "A:T4=s4a:1,0,32;;",
4036    *PP will point to "4a:1,0,32;;".
4037
4038    TYPE points to an incomplete type that needs to be filled in.
4039
4040    OBJFILE points to the current objfile from which the stabs information is
4041    being read.  (Note that it is redundant in that TYPE also contains a pointer
4042    to this same objfile, so it might be a good idea to eliminate it.  FIXME). 
4043  */
4044
4045 static struct type *
4046 read_struct_type (char **pp, struct type *type, struct objfile *objfile)
4047 {
4048   struct cleanup *back_to;
4049   struct field_info fi;
4050
4051   fi.list = NULL;
4052   fi.fnlist = NULL;
4053
4054   back_to = make_cleanup (null_cleanup, 0);
4055
4056   INIT_CPLUS_SPECIFIC (type);
4057   TYPE_FLAGS (type) &= ~TYPE_FLAG_STUB;
4058
4059   /* First comes the total size in bytes.  */
4060
4061   {
4062     int nbits;
4063     TYPE_LENGTH (type) = read_huge_number (pp, 0, &nbits);
4064     if (nbits != 0)
4065       return error_type (pp, objfile);
4066   }
4067
4068   /* Now read the baseclasses, if any, read the regular C struct or C++
4069      class member fields, attach the fields to the type, read the C++
4070      member functions, attach them to the type, and then read any tilde
4071      field (baseclass specifier for the class holding the main vtable). */
4072
4073   if (!read_baseclasses (&fi, pp, type, objfile)
4074       || !read_struct_fields (&fi, pp, type, objfile)
4075       || !attach_fields_to_type (&fi, type, objfile)
4076       || !read_member_functions (&fi, pp, type, objfile)
4077       || !attach_fn_fields_to_type (&fi, type)
4078       || !read_tilde_fields (&fi, pp, type, objfile))
4079     {
4080       type = error_type (pp, objfile);
4081     }
4082
4083   do_cleanups (back_to);
4084   return (type);
4085 }
4086
4087 /* Read a definition of an array type,
4088    and create and return a suitable type object.
4089    Also creates a range type which represents the bounds of that
4090    array.  */
4091
4092 static struct type *
4093 read_array_type (register char **pp, register struct type *type,
4094                  struct objfile *objfile)
4095 {
4096   struct type *index_type, *element_type, *range_type;
4097   int lower, upper;
4098   int adjustable = 0;
4099   int nbits;
4100
4101   /* Format of an array type:
4102      "ar<index type>;lower;upper;<array_contents_type>".
4103      OS9000: "arlower,upper;<array_contents_type>".
4104
4105      Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
4106      for these, produce a type like float[][].  */
4107
4108   if (os9k_stabs)
4109     index_type = builtin_type_int;
4110   else
4111     {
4112       index_type = read_type (pp, objfile);
4113       if (**pp != ';')
4114         /* Improper format of array type decl.  */
4115         return error_type (pp, objfile);
4116       ++*pp;
4117     }
4118
4119   if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
4120     {
4121       (*pp)++;
4122       adjustable = 1;
4123     }
4124   lower = read_huge_number (pp, os9k_stabs ? ',' : ';', &nbits);
4125   if (nbits != 0)
4126     return error_type (pp, objfile);
4127
4128   if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
4129     {
4130       (*pp)++;
4131       adjustable = 1;
4132     }
4133   upper = read_huge_number (pp, ';', &nbits);
4134   if (nbits != 0)
4135     return error_type (pp, objfile);
4136
4137   element_type = read_type (pp, objfile);
4138
4139   if (adjustable)
4140     {
4141       lower = 0;
4142       upper = -1;
4143     }
4144
4145   range_type =
4146     create_range_type ((struct type *) NULL, index_type, lower, upper);
4147   type = create_array_type (type, element_type, range_type);
4148
4149   return type;
4150 }
4151
4152
4153 /* Read a definition of an enumeration type,
4154    and create and return a suitable type object.
4155    Also defines the symbols that represent the values of the type.  */
4156
4157 static struct type *
4158 read_enum_type (register char **pp, register struct type *type,
4159                 struct objfile *objfile)
4160 {
4161   register char *p;
4162   char *name;
4163   register long n;
4164   register struct symbol *sym;
4165   int nsyms = 0;
4166   struct pending **symlist;
4167   struct pending *osyms, *syms;
4168   int o_nsyms;
4169   int nbits;
4170   int unsigned_enum = 1;
4171
4172 #if 0
4173   /* FIXME!  The stabs produced by Sun CC merrily define things that ought
4174      to be file-scope, between N_FN entries, using N_LSYM.  What's a mother
4175      to do?  For now, force all enum values to file scope.  */
4176   if (within_function)
4177     symlist = &local_symbols;
4178   else
4179 #endif
4180     symlist = &file_symbols;
4181   osyms = *symlist;
4182   o_nsyms = osyms ? osyms->nsyms : 0;
4183
4184   if (os9k_stabs)
4185     {
4186       /* Size.  Perhaps this does not have to be conditionalized on
4187          os9k_stabs (assuming the name of an enum constant can't start
4188          with a digit).  */
4189       read_huge_number (pp, 0, &nbits);
4190       if (nbits != 0)
4191         return error_type (pp, objfile);
4192     }
4193
4194   /* The aix4 compiler emits an extra field before the enum members;
4195      my guess is it's a type of some sort.  Just ignore it.  */
4196   if (**pp == '-')
4197     {
4198       /* Skip over the type.  */
4199       while (**pp != ':')
4200         (*pp)++;
4201
4202       /* Skip over the colon.  */
4203       (*pp)++;
4204     }
4205
4206   /* Read the value-names and their values.
4207      The input syntax is NAME:VALUE,NAME:VALUE, and so on.
4208      A semicolon or comma instead of a NAME means the end.  */
4209   while (**pp && **pp != ';' && **pp != ',')
4210     {
4211       STABS_CONTINUE (pp, objfile);
4212       p = *pp;
4213       while (*p != ':')
4214         p++;
4215       name = obsavestring (*pp, p - *pp, &objfile->symbol_obstack);
4216       *pp = p + 1;
4217       n = read_huge_number (pp, ',', &nbits);
4218       if (nbits != 0)
4219         return error_type (pp, objfile);
4220
4221       sym = (struct symbol *)
4222         obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
4223       memset (sym, 0, sizeof (struct symbol));
4224       SYMBOL_NAME (sym) = name;
4225       SYMBOL_LANGUAGE (sym) = current_subfile->language;
4226       SYMBOL_CLASS (sym) = LOC_CONST;
4227       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4228       SYMBOL_VALUE (sym) = n;
4229       if (n < 0)
4230         unsigned_enum = 0;
4231       add_symbol_to_list (sym, symlist);
4232       nsyms++;
4233     }
4234
4235   if (**pp == ';')
4236     (*pp)++;                    /* Skip the semicolon.  */
4237
4238   /* Now fill in the fields of the type-structure.  */
4239
4240   TYPE_LENGTH (type) = TARGET_INT_BIT / HOST_CHAR_BIT;
4241   TYPE_CODE (type) = TYPE_CODE_ENUM;
4242   TYPE_FLAGS (type) &= ~TYPE_FLAG_STUB;
4243   if (unsigned_enum)
4244     TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
4245   TYPE_NFIELDS (type) = nsyms;
4246   TYPE_FIELDS (type) = (struct field *)
4247     TYPE_ALLOC (type, sizeof (struct field) * nsyms);
4248   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nsyms);
4249
4250   /* Find the symbols for the values and put them into the type.
4251      The symbols can be found in the symlist that we put them on
4252      to cause them to be defined.  osyms contains the old value
4253      of that symlist; everything up to there was defined by us.  */
4254   /* Note that we preserve the order of the enum constants, so
4255      that in something like "enum {FOO, LAST_THING=FOO}" we print
4256      FOO, not LAST_THING.  */
4257
4258   for (syms = *symlist, n = nsyms - 1; syms; syms = syms->next)
4259     {
4260       int last = syms == osyms ? o_nsyms : 0;
4261       int j = syms->nsyms;
4262       for (; --j >= last; --n)
4263         {
4264           struct symbol *xsym = syms->symbol[j];
4265           SYMBOL_TYPE (xsym) = type;
4266           TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
4267           TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
4268           TYPE_FIELD_BITSIZE (type, n) = 0;
4269         }
4270       if (syms == osyms)
4271         break;
4272     }
4273
4274   return type;
4275 }
4276
4277 /* Sun's ACC uses a somewhat saner method for specifying the builtin
4278    typedefs in every file (for int, long, etc):
4279
4280    type = b <signed> <width> <format type>; <offset>; <nbits>
4281    signed = u or s.
4282    optional format type = c or b for char or boolean.
4283    offset = offset from high order bit to start bit of type.
4284    width is # bytes in object of this type, nbits is # bits in type.
4285
4286    The width/offset stuff appears to be for small objects stored in
4287    larger ones (e.g. `shorts' in `int' registers).  We ignore it for now,
4288    FIXME.  */
4289
4290 static struct type *
4291 read_sun_builtin_type (char **pp, int typenums[2], struct objfile *objfile)
4292 {
4293   int type_bits;
4294   int nbits;
4295   int signed_type;
4296   enum type_code code = TYPE_CODE_INT;
4297
4298   switch (**pp)
4299     {
4300     case 's':
4301       signed_type = 1;
4302       break;
4303     case 'u':
4304       signed_type = 0;
4305       break;
4306     default:
4307       return error_type (pp, objfile);
4308     }
4309   (*pp)++;
4310
4311   /* For some odd reason, all forms of char put a c here.  This is strange
4312      because no other type has this honor.  We can safely ignore this because
4313      we actually determine 'char'acterness by the number of bits specified in
4314      the descriptor.
4315      Boolean forms, e.g Fortran logical*X, put a b here.  */
4316
4317   if (**pp == 'c')
4318     (*pp)++;
4319   else if (**pp == 'b')
4320     {
4321       code = TYPE_CODE_BOOL;
4322       (*pp)++;
4323     }
4324
4325   /* The first number appears to be the number of bytes occupied
4326      by this type, except that unsigned short is 4 instead of 2.
4327      Since this information is redundant with the third number,
4328      we will ignore it.  */
4329   read_huge_number (pp, ';', &nbits);
4330   if (nbits != 0)
4331     return error_type (pp, objfile);
4332
4333   /* The second number is always 0, so ignore it too. */
4334   read_huge_number (pp, ';', &nbits);
4335   if (nbits != 0)
4336     return error_type (pp, objfile);
4337
4338   /* The third number is the number of bits for this type. */
4339   type_bits = read_huge_number (pp, 0, &nbits);
4340   if (nbits != 0)
4341     return error_type (pp, objfile);
4342   /* The type *should* end with a semicolon.  If it are embedded
4343      in a larger type the semicolon may be the only way to know where
4344      the type ends.  If this type is at the end of the stabstring we
4345      can deal with the omitted semicolon (but we don't have to like
4346      it).  Don't bother to complain(), Sun's compiler omits the semicolon
4347      for "void".  */
4348   if (**pp == ';')
4349     ++(*pp);
4350
4351   if (type_bits == 0)
4352     return init_type (TYPE_CODE_VOID, 1,
4353                       signed_type ? 0 : TYPE_FLAG_UNSIGNED, (char *) NULL,
4354                       objfile);
4355   else
4356     return init_type (code,
4357                       type_bits / TARGET_CHAR_BIT,
4358                       signed_type ? 0 : TYPE_FLAG_UNSIGNED, (char *) NULL,
4359                       objfile);
4360 }
4361
4362 static struct type *
4363 read_sun_floating_type (char **pp, int typenums[2], struct objfile *objfile)
4364 {
4365   int nbits;
4366   int details;
4367   int nbytes;
4368
4369   /* The first number has more details about the type, for example
4370      FN_COMPLEX.  */
4371   details = read_huge_number (pp, ';', &nbits);
4372   if (nbits != 0)
4373     return error_type (pp, objfile);
4374
4375   /* The second number is the number of bytes occupied by this type */
4376   nbytes = read_huge_number (pp, ';', &nbits);
4377   if (nbits != 0)
4378     return error_type (pp, objfile);
4379
4380   if (details == NF_COMPLEX || details == NF_COMPLEX16
4381       || details == NF_COMPLEX32)
4382     /* This is a type we can't handle, but we do know the size.
4383        We also will be able to give it a name.  */
4384     return init_type (TYPE_CODE_COMPLEX, nbytes, 0, NULL, objfile);
4385
4386   return init_type (TYPE_CODE_FLT, nbytes, 0, NULL, objfile);
4387 }
4388
4389 /* Read a number from the string pointed to by *PP.
4390    The value of *PP is advanced over the number.
4391    If END is nonzero, the character that ends the
4392    number must match END, or an error happens;
4393    and that character is skipped if it does match.
4394    If END is zero, *PP is left pointing to that character.
4395
4396    If the number fits in a long, set *BITS to 0 and return the value.
4397    If not, set *BITS to be the number of bits in the number and return 0.
4398
4399    If encounter garbage, set *BITS to -1 and return 0.  */
4400
4401 static long
4402 read_huge_number (char **pp, int end, int *bits)
4403 {
4404   char *p = *pp;
4405   int sign = 1;
4406   long n = 0;
4407   int radix = 10;
4408   char overflow = 0;
4409   int nbits = 0;
4410   int c;
4411   long upper_limit;
4412
4413   if (*p == '-')
4414     {
4415       sign = -1;
4416       p++;
4417     }
4418
4419   /* Leading zero means octal.  GCC uses this to output values larger
4420      than an int (because that would be hard in decimal).  */
4421   if (*p == '0')
4422     {
4423       radix = 8;
4424       p++;
4425     }
4426
4427   if (os9k_stabs)
4428     upper_limit = ULONG_MAX / radix;
4429   else
4430     upper_limit = LONG_MAX / radix;
4431
4432   while ((c = *p++) >= '0' && c < ('0' + radix))
4433     {
4434       if (n <= upper_limit)
4435         {
4436           n *= radix;
4437           n += c - '0';         /* FIXME this overflows anyway */
4438         }
4439       else
4440         overflow = 1;
4441
4442       /* This depends on large values being output in octal, which is
4443          what GCC does. */
4444       if (radix == 8)
4445         {
4446           if (nbits == 0)
4447             {
4448               if (c == '0')
4449                 /* Ignore leading zeroes.  */
4450                 ;
4451               else if (c == '1')
4452                 nbits = 1;
4453               else if (c == '2' || c == '3')
4454                 nbits = 2;
4455               else
4456                 nbits = 3;
4457             }
4458           else
4459             nbits += 3;
4460         }
4461     }
4462   if (end)
4463     {
4464       if (c && c != end)
4465         {
4466           if (bits != NULL)
4467             *bits = -1;
4468           return 0;
4469         }
4470     }
4471   else
4472     --p;
4473
4474   *pp = p;
4475   if (overflow)
4476     {
4477       if (nbits == 0)
4478         {
4479           /* Large decimal constants are an error (because it is hard to
4480              count how many bits are in them).  */
4481           if (bits != NULL)
4482             *bits = -1;
4483           return 0;
4484         }
4485
4486       /* -0x7f is the same as 0x80.  So deal with it by adding one to
4487          the number of bits.  */
4488       if (sign == -1)
4489         ++nbits;
4490       if (bits)
4491         *bits = nbits;
4492     }
4493   else
4494     {
4495       if (bits)
4496         *bits = 0;
4497       return n * sign;
4498     }
4499   /* It's *BITS which has the interesting information.  */
4500   return 0;
4501 }
4502
4503 static struct type *
4504 read_range_type (char **pp, int typenums[2], struct objfile *objfile)
4505 {
4506   char *orig_pp = *pp;
4507   int rangenums[2];
4508   long n2, n3;
4509   int n2bits, n3bits;
4510   int self_subrange;
4511   struct type *result_type;
4512   struct type *index_type = NULL;
4513
4514   /* First comes a type we are a subrange of.
4515      In C it is usually 0, 1 or the type being defined.  */
4516   if (read_type_number (pp, rangenums) != 0)
4517     return error_type (pp, objfile);
4518   self_subrange = (rangenums[0] == typenums[0] &&
4519                    rangenums[1] == typenums[1]);
4520
4521   if (**pp == '=')
4522     {
4523       *pp = orig_pp;
4524       index_type = read_type (pp, objfile);
4525     }
4526
4527   /* A semicolon should now follow; skip it.  */
4528   if (**pp == ';')
4529     (*pp)++;
4530
4531   /* The remaining two operands are usually lower and upper bounds
4532      of the range.  But in some special cases they mean something else.  */
4533   n2 = read_huge_number (pp, ';', &n2bits);
4534   n3 = read_huge_number (pp, ';', &n3bits);
4535
4536   if (n2bits == -1 || n3bits == -1)
4537     return error_type (pp, objfile);
4538
4539   if (index_type)
4540     goto handle_true_range;
4541
4542   /* If limits are huge, must be large integral type.  */
4543   if (n2bits != 0 || n3bits != 0)
4544     {
4545       char got_signed = 0;
4546       char got_unsigned = 0;
4547       /* Number of bits in the type.  */
4548       int nbits = 0;
4549
4550       /* Range from 0 to <large number> is an unsigned large integral type.  */
4551       if ((n2bits == 0 && n2 == 0) && n3bits != 0)
4552         {
4553           got_unsigned = 1;
4554           nbits = n3bits;
4555         }
4556       /* Range from <large number> to <large number>-1 is a large signed
4557          integral type.  Take care of the case where <large number> doesn't
4558          fit in a long but <large number>-1 does.  */
4559       else if ((n2bits != 0 && n3bits != 0 && n2bits == n3bits + 1)
4560                || (n2bits != 0 && n3bits == 0
4561                    && (n2bits == sizeof (long) * HOST_CHAR_BIT)
4562                    && n3 == LONG_MAX))
4563         {
4564           got_signed = 1;
4565           nbits = n2bits;
4566         }
4567
4568       if (got_signed || got_unsigned)
4569         {
4570           return init_type (TYPE_CODE_INT, nbits / TARGET_CHAR_BIT,
4571                             got_unsigned ? TYPE_FLAG_UNSIGNED : 0, NULL,
4572                             objfile);
4573         }
4574       else
4575         return error_type (pp, objfile);
4576     }
4577
4578   /* A type defined as a subrange of itself, with bounds both 0, is void.  */
4579   if (self_subrange && n2 == 0 && n3 == 0)
4580     return init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
4581
4582   /* If n3 is zero and n2 is positive, we want a floating type, and n2
4583      is the width in bytes.
4584
4585      Fortran programs appear to use this for complex types also.  To
4586      distinguish between floats and complex, g77 (and others?)  seem
4587      to use self-subranges for the complexes, and subranges of int for
4588      the floats.
4589
4590      Also note that for complexes, g77 sets n2 to the size of one of
4591      the member floats, not the whole complex beast.  My guess is that
4592      this was to work well with pre-COMPLEX versions of gdb. */
4593
4594   if (n3 == 0 && n2 > 0)
4595     {
4596       struct type *float_type
4597         = init_type (TYPE_CODE_FLT, n2, 0, NULL, objfile);
4598
4599       if (self_subrange)
4600         {
4601           struct type *complex_type = 
4602             init_type (TYPE_CODE_COMPLEX, 2 * n2, 0, NULL, objfile);
4603           TYPE_TARGET_TYPE (complex_type) = float_type;
4604           return complex_type;
4605         }
4606       else
4607         return float_type;
4608     }
4609
4610   /* If the upper bound is -1, it must really be an unsigned int.  */
4611
4612   else if (n2 == 0 && n3 == -1)
4613     {
4614       /* It is unsigned int or unsigned long.  */
4615       /* GCC 2.3.3 uses this for long long too, but that is just a GDB 3.5
4616          compatibility hack.  */
4617       return init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
4618                         TYPE_FLAG_UNSIGNED, NULL, objfile);
4619     }
4620
4621   /* Special case: char is defined (Who knows why) as a subrange of
4622      itself with range 0-127.  */
4623   else if (self_subrange && n2 == 0 && n3 == 127)
4624     return init_type (TYPE_CODE_INT, 1, 0, NULL, objfile);
4625
4626   else if (current_symbol && SYMBOL_LANGUAGE (current_symbol) == language_chill
4627            && !self_subrange)
4628     goto handle_true_range;
4629
4630   /* We used to do this only for subrange of self or subrange of int.  */
4631   else if (n2 == 0)
4632     {
4633       /* -1 is used for the upper bound of (4 byte) "unsigned int" and
4634          "unsigned long", and we already checked for that,
4635          so don't need to test for it here.  */
4636
4637       if (n3 < 0)
4638         /* n3 actually gives the size.  */
4639         return init_type (TYPE_CODE_INT, -n3, TYPE_FLAG_UNSIGNED,
4640                           NULL, objfile);
4641
4642       /* Is n3 == 2**(8n)-1 for some integer n?  Then it's an
4643          unsigned n-byte integer.  But do require n to be a power of
4644          two; we don't want 3- and 5-byte integers flying around.  */
4645       {
4646         int bytes;
4647         unsigned long bits;
4648
4649         bits = n3;
4650         for (bytes = 0; (bits & 0xff) == 0xff; bytes++)
4651           bits >>= 8;
4652         if (bits == 0
4653             && ((bytes - 1) & bytes) == 0) /* "bytes is a power of two" */
4654           return init_type (TYPE_CODE_INT, bytes, TYPE_FLAG_UNSIGNED, NULL,
4655                             objfile);
4656       }
4657     }
4658   /* I think this is for Convex "long long".  Since I don't know whether
4659      Convex sets self_subrange, I also accept that particular size regardless
4660      of self_subrange.  */
4661   else if (n3 == 0 && n2 < 0
4662            && (self_subrange
4663                || n2 == -TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT))
4664     return init_type (TYPE_CODE_INT, -n2, 0, NULL, objfile);
4665   else if (n2 == -n3 - 1)
4666     {
4667       if (n3 == 0x7f)
4668         return init_type (TYPE_CODE_INT, 1, 0, NULL, objfile);
4669       if (n3 == 0x7fff)
4670         return init_type (TYPE_CODE_INT, 2, 0, NULL, objfile);
4671       if (n3 == 0x7fffffff)
4672         return init_type (TYPE_CODE_INT, 4, 0, NULL, objfile);
4673     }
4674
4675   /* We have a real range type on our hands.  Allocate space and
4676      return a real pointer.  */
4677 handle_true_range:
4678
4679   if (self_subrange)
4680     index_type = builtin_type_int;
4681   else
4682     index_type = *dbx_lookup_type (rangenums);
4683   if (index_type == NULL)
4684     {
4685       /* Does this actually ever happen?  Is that why we are worrying
4686          about dealing with it rather than just calling error_type?  */
4687
4688       static struct type *range_type_index;
4689
4690       complain (&range_type_base_complaint, rangenums[1]);
4691       if (range_type_index == NULL)
4692         range_type_index =
4693           init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
4694                      0, "range type index type", NULL);
4695       index_type = range_type_index;
4696     }
4697
4698   result_type = create_range_type ((struct type *) NULL, index_type, n2, n3);
4699   return (result_type);
4700 }
4701
4702 /* Read in an argument list.  This is a list of types, separated by commas
4703    and terminated with END.  Return the list of types read in, or (struct type
4704    **)-1 if there is an error.  */
4705
4706 static struct type **
4707 read_args (char **pp, int end, struct objfile *objfile)
4708 {
4709   /* FIXME!  Remove this arbitrary limit!  */
4710   struct type *types[1024], **rval;     /* allow for fns of 1023 parameters */
4711   int n = 0;
4712
4713   while (**pp != end)
4714     {
4715       if (**pp != ',')
4716         /* Invalid argument list: no ','.  */
4717         return (struct type **) -1;
4718       (*pp)++;
4719       STABS_CONTINUE (pp, objfile);
4720       types[n++] = read_type (pp, objfile);
4721     }
4722   (*pp)++;                      /* get past `end' (the ':' character) */
4723
4724   if (n == 1)
4725     {
4726       rval = (struct type **) xmalloc (2 * sizeof (struct type *));
4727     }
4728   else if (TYPE_CODE (types[n - 1]) != TYPE_CODE_VOID)
4729     {
4730       rval = (struct type **) xmalloc ((n + 1) * sizeof (struct type *));
4731       memset (rval + n, 0, sizeof (struct type *));
4732     }
4733   else
4734     {
4735       rval = (struct type **) xmalloc (n * sizeof (struct type *));
4736     }
4737   memcpy (rval, types, n * sizeof (struct type *));
4738   return rval;
4739 }
4740 \f
4741 /* Common block handling.  */
4742
4743 /* List of symbols declared since the last BCOMM.  This list is a tail
4744    of local_symbols.  When ECOMM is seen, the symbols on the list
4745    are noted so their proper addresses can be filled in later,
4746    using the common block base address gotten from the assembler
4747    stabs.  */
4748
4749 static struct pending *common_block;
4750 static int common_block_i;
4751
4752 /* Name of the current common block.  We get it from the BCOMM instead of the
4753    ECOMM to match IBM documentation (even though IBM puts the name both places
4754    like everyone else).  */
4755 static char *common_block_name;
4756
4757 /* Process a N_BCOMM symbol.  The storage for NAME is not guaranteed
4758    to remain after this function returns.  */
4759
4760 void
4761 common_block_start (char *name, struct objfile *objfile)
4762 {
4763   if (common_block_name != NULL)
4764     {
4765       static struct complaint msg =
4766       {
4767         "Invalid symbol data: common block within common block",
4768         0, 0};
4769       complain (&msg);
4770     }
4771   common_block = local_symbols;
4772   common_block_i = local_symbols ? local_symbols->nsyms : 0;
4773   common_block_name = obsavestring (name, strlen (name),
4774                                     &objfile->symbol_obstack);
4775 }
4776
4777 /* Process a N_ECOMM symbol.  */
4778
4779 void
4780 common_block_end (struct objfile *objfile)
4781 {
4782   /* Symbols declared since the BCOMM are to have the common block
4783      start address added in when we know it.  common_block and
4784      common_block_i point to the first symbol after the BCOMM in
4785      the local_symbols list; copy the list and hang it off the
4786      symbol for the common block name for later fixup.  */
4787   int i;
4788   struct symbol *sym;
4789   struct pending *new = 0;
4790   struct pending *next;
4791   int j;
4792
4793   if (common_block_name == NULL)
4794     {
4795       static struct complaint msg =
4796       {"ECOMM symbol unmatched by BCOMM", 0, 0};
4797       complain (&msg);
4798       return;
4799     }
4800
4801   sym = (struct symbol *)
4802     obstack_alloc (&objfile->symbol_obstack, sizeof (struct symbol));
4803   memset (sym, 0, sizeof (struct symbol));
4804   /* Note: common_block_name already saved on symbol_obstack */
4805   SYMBOL_NAME (sym) = common_block_name;
4806   SYMBOL_CLASS (sym) = LOC_BLOCK;
4807
4808   /* Now we copy all the symbols which have been defined since the BCOMM.  */
4809
4810   /* Copy all the struct pendings before common_block.  */
4811   for (next = local_symbols;
4812        next != NULL && next != common_block;
4813        next = next->next)
4814     {
4815       for (j = 0; j < next->nsyms; j++)
4816         add_symbol_to_list (next->symbol[j], &new);
4817     }
4818
4819   /* Copy however much of COMMON_BLOCK we need.  If COMMON_BLOCK is
4820      NULL, it means copy all the local symbols (which we already did
4821      above).  */
4822
4823   if (common_block != NULL)
4824     for (j = common_block_i; j < common_block->nsyms; j++)
4825       add_symbol_to_list (common_block->symbol[j], &new);
4826
4827   SYMBOL_TYPE (sym) = (struct type *) new;
4828
4829   /* Should we be putting local_symbols back to what it was?
4830      Does it matter?  */
4831
4832   i = hashname (SYMBOL_NAME (sym));
4833   SYMBOL_VALUE_CHAIN (sym) = global_sym_chain[i];
4834   global_sym_chain[i] = sym;
4835   common_block_name = NULL;
4836 }
4837
4838 /* Add a common block's start address to the offset of each symbol
4839    declared to be in it (by being between a BCOMM/ECOMM pair that uses
4840    the common block name).  */
4841
4842 static void
4843 fix_common_block (struct symbol *sym, int valu)
4844 {
4845   struct pending *next = (struct pending *) SYMBOL_TYPE (sym);
4846   for (; next; next = next->next)
4847     {
4848       register int j;
4849       for (j = next->nsyms - 1; j >= 0; j--)
4850         SYMBOL_VALUE_ADDRESS (next->symbol[j]) += valu;
4851     }
4852 }
4853 \f
4854
4855
4856 /* What about types defined as forward references inside of a small lexical
4857    scope?  */
4858 /* Add a type to the list of undefined types to be checked through
4859    once this file has been read in.  */
4860
4861 void
4862 add_undefined_type (struct type *type)
4863 {
4864   if (undef_types_length == undef_types_allocated)
4865     {
4866       undef_types_allocated *= 2;
4867       undef_types = (struct type **)
4868         xrealloc ((char *) undef_types,
4869                   undef_types_allocated * sizeof (struct type *));
4870     }
4871   undef_types[undef_types_length++] = type;
4872 }
4873
4874 /* Go through each undefined type, see if it's still undefined, and fix it
4875    up if possible.  We have two kinds of undefined types:
4876
4877    TYPE_CODE_ARRAY:  Array whose target type wasn't defined yet.
4878    Fix:  update array length using the element bounds
4879    and the target type's length.
4880    TYPE_CODE_STRUCT, TYPE_CODE_UNION:  Structure whose fields were not
4881    yet defined at the time a pointer to it was made.
4882    Fix:  Do a full lookup on the struct/union tag.  */
4883 void
4884 cleanup_undefined_types (void)
4885 {
4886   struct type **type;
4887
4888   for (type = undef_types; type < undef_types + undef_types_length; type++)
4889     {
4890       switch (TYPE_CODE (*type))
4891         {
4892
4893         case TYPE_CODE_STRUCT:
4894         case TYPE_CODE_UNION:
4895         case TYPE_CODE_ENUM:
4896           {
4897             /* Check if it has been defined since.  Need to do this here
4898                as well as in check_typedef to deal with the (legitimate in
4899                C though not C++) case of several types with the same name
4900                in different source files.  */
4901             if (TYPE_FLAGS (*type) & TYPE_FLAG_STUB)
4902               {
4903                 struct pending *ppt;
4904                 int i;
4905                 /* Name of the type, without "struct" or "union" */
4906                 char *typename = TYPE_TAG_NAME (*type);
4907
4908                 if (typename == NULL)
4909                   {
4910                     static struct complaint msg =
4911                     {"need a type name", 0, 0};
4912                     complain (&msg);
4913                     break;
4914                   }
4915                 for (ppt = file_symbols; ppt; ppt = ppt->next)
4916                   {
4917                     for (i = 0; i < ppt->nsyms; i++)
4918                       {
4919                         struct symbol *sym = ppt->symbol[i];
4920
4921                         if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
4922                             && SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE
4923                             && (TYPE_CODE (SYMBOL_TYPE (sym)) ==
4924                                 TYPE_CODE (*type))
4925                             && STREQ (SYMBOL_NAME (sym), typename))
4926                           {
4927                             memcpy (*type, SYMBOL_TYPE (sym),
4928                                     sizeof (struct type));
4929                           }
4930                       }
4931                   }
4932               }
4933           }
4934           break;
4935
4936         default:
4937           {
4938             static struct complaint msg =
4939             {"\
4940 GDB internal error.  cleanup_undefined_types with bad type %d.", 0, 0};
4941             complain (&msg, TYPE_CODE (*type));
4942           }
4943           break;
4944         }
4945     }
4946
4947   undef_types_length = 0;
4948 }
4949
4950 /* Scan through all of the global symbols defined in the object file,
4951    assigning values to the debugging symbols that need to be assigned
4952    to.  Get these symbols from the minimal symbol table.  */
4953
4954 void
4955 scan_file_globals (struct objfile *objfile)
4956 {
4957   int hash;
4958   struct minimal_symbol *msymbol;
4959   struct symbol *sym, *prev, *rsym;
4960   struct objfile *resolve_objfile;
4961
4962   /* SVR4 based linkers copy referenced global symbols from shared
4963      libraries to the main executable.
4964      If we are scanning the symbols for a shared library, try to resolve
4965      them from the minimal symbols of the main executable first.  */
4966
4967   if (symfile_objfile && objfile != symfile_objfile)
4968     resolve_objfile = symfile_objfile;
4969   else
4970     resolve_objfile = objfile;
4971
4972   while (1)
4973     {
4974       /* Avoid expensive loop through all minimal symbols if there are
4975          no unresolved symbols.  */
4976       for (hash = 0; hash < HASHSIZE; hash++)
4977         {
4978           if (global_sym_chain[hash])
4979             break;
4980         }
4981       if (hash >= HASHSIZE)
4982         return;
4983
4984       for (msymbol = resolve_objfile->msymbols;
4985            msymbol && SYMBOL_NAME (msymbol) != NULL;
4986            msymbol++)
4987         {
4988           QUIT;
4989
4990           /* Skip static symbols.  */
4991           switch (MSYMBOL_TYPE (msymbol))
4992             {
4993             case mst_file_text:
4994             case mst_file_data:
4995             case mst_file_bss:
4996               continue;
4997             default:
4998               break;
4999             }
5000
5001           prev = NULL;
5002
5003           /* Get the hash index and check all the symbols
5004              under that hash index. */
5005
5006           hash = hashname (SYMBOL_NAME (msymbol));
5007
5008           for (sym = global_sym_chain[hash]; sym;)
5009             {
5010               if (SYMBOL_NAME (msymbol)[0] == SYMBOL_NAME (sym)[0] &&
5011                   STREQ (SYMBOL_NAME (msymbol) + 1, SYMBOL_NAME (sym) + 1))
5012                 {
5013
5014                   struct alias_list *aliases;
5015
5016                   /* Splice this symbol out of the hash chain and
5017                      assign the value we have to it. */
5018                   if (prev)
5019                     {
5020                       SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
5021                     }
5022                   else
5023                     {
5024                       global_sym_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
5025                     }
5026
5027                   /* Check to see whether we need to fix up a common block.  */
5028                   /* Note: this code might be executed several times for
5029                      the same symbol if there are multiple references.  */
5030
5031                   /* If symbol has aliases, do minimal symbol fixups for each.
5032                      These live aliases/references weren't added to 
5033                      global_sym_chain hash but may also need to be fixed up. */
5034                   /* FIXME: Maybe should have added aliases to the global chain,                     resolved symbol name, then treated aliases as normal 
5035                      symbols?  Still, we wouldn't want to add_to_list. */
5036                   /* Now do the same for each alias of this symbol */
5037                   rsym = sym;
5038                   aliases = SYMBOL_ALIASES (sym);
5039                   while (rsym)
5040                     {
5041                       if (SYMBOL_CLASS (rsym) == LOC_BLOCK)
5042                         {
5043                           fix_common_block (rsym,
5044                                             SYMBOL_VALUE_ADDRESS (msymbol));
5045                         }
5046                       else
5047                         {
5048                           SYMBOL_VALUE_ADDRESS (rsym)
5049                             = SYMBOL_VALUE_ADDRESS (msymbol);
5050                         }
5051                       SYMBOL_SECTION (rsym) = SYMBOL_SECTION (msymbol);
5052                       if (aliases)
5053                         {
5054                           rsym = aliases->sym;
5055                           aliases = aliases->next;
5056                         }
5057                       else
5058                         rsym = NULL;
5059                     }
5060
5061
5062                   if (prev)
5063                     {
5064                       sym = SYMBOL_VALUE_CHAIN (prev);
5065                     }
5066                   else
5067                     {
5068                       sym = global_sym_chain[hash];
5069                     }
5070                 }
5071               else
5072                 {
5073                   prev = sym;
5074                   sym = SYMBOL_VALUE_CHAIN (sym);
5075                 }
5076             }
5077         }
5078       if (resolve_objfile == objfile)
5079         break;
5080       resolve_objfile = objfile;
5081     }
5082
5083   /* Change the storage class of any remaining unresolved globals to
5084      LOC_UNRESOLVED and remove them from the chain.  */
5085   for (hash = 0; hash < HASHSIZE; hash++)
5086     {
5087       sym = global_sym_chain[hash];
5088       while (sym)
5089         {
5090           prev = sym;
5091           sym = SYMBOL_VALUE_CHAIN (sym);
5092
5093           /* Change the symbol address from the misleading chain value
5094              to address zero.  */
5095           SYMBOL_VALUE_ADDRESS (prev) = 0;
5096
5097           /* Complain about unresolved common block symbols.  */
5098           if (SYMBOL_CLASS (prev) == LOC_STATIC)
5099             SYMBOL_CLASS (prev) = LOC_UNRESOLVED;
5100           else
5101             complain (&unresolved_sym_chain_complaint,
5102                       objfile->name, SYMBOL_NAME (prev));
5103         }
5104     }
5105   memset (global_sym_chain, 0, sizeof (global_sym_chain));
5106 }
5107
5108 /* Initialize anything that needs initializing when starting to read
5109    a fresh piece of a symbol file, e.g. reading in the stuff corresponding
5110    to a psymtab.  */
5111
5112 void
5113 stabsread_init (void)
5114 {
5115 }
5116
5117 /* Initialize anything that needs initializing when a completely new
5118    symbol file is specified (not just adding some symbols from another
5119    file, e.g. a shared library).  */
5120
5121 void
5122 stabsread_new_init (void)
5123 {
5124   /* Empty the hash table of global syms looking for values.  */
5125   memset (global_sym_chain, 0, sizeof (global_sym_chain));
5126 }
5127
5128 /* Initialize anything that needs initializing at the same time as
5129    start_symtab() is called. */
5130
5131 void
5132 start_stabs (void)
5133 {
5134   global_stabs = NULL;          /* AIX COFF */
5135   /* Leave FILENUM of 0 free for builtin types and this file's types.  */
5136   n_this_object_header_files = 1;
5137   type_vector_length = 0;
5138   type_vector = (struct type **) 0;
5139
5140   /* FIXME: If common_block_name is not already NULL, we should complain().  */
5141   common_block_name = NULL;
5142
5143   os9k_stabs = 0;
5144 }
5145
5146 /* Call after end_symtab() */
5147
5148 void
5149 end_stabs (void)
5150 {
5151   if (type_vector)
5152     {
5153       xfree (type_vector);
5154     }
5155   type_vector = 0;
5156   type_vector_length = 0;
5157   previous_stab_code = 0;
5158 }
5159
5160 void
5161 finish_global_stabs (struct objfile *objfile)
5162 {
5163   if (global_stabs)
5164     {
5165       patch_block_stabs (global_symbols, global_stabs, objfile);
5166       xfree (global_stabs);
5167       global_stabs = NULL;
5168     }
5169 }
5170
5171 /* Initializer for this module */
5172
5173 void
5174 _initialize_stabsread (void)
5175 {
5176   undef_types_allocated = 20;
5177   undef_types_length = 0;
5178   undef_types = (struct type **)
5179     xmalloc (undef_types_allocated * sizeof (struct type *));
5180 }