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