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