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