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