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