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