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