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