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