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