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