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