Saberlint.
[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 struct minimal_symbol *msymbol;
973 struct symbol *sym, *prev;
974
975 for (msymbol = objfile -> msymbols; msymbol -> name != NULL; msymbol++)
976 {
977 QUIT;
978
979 prev = (struct symbol *) 0;
980
981 /* Get the hash index and check all the symbols
982 under that hash index. */
983
984 hash = hashname (msymbol -> name);
985
986 for (sym = global_sym_chain[hash]; sym;)
987 {
988 if (*(msymbol -> name) == SYMBOL_NAME (sym)[0]
989 && !strcmp(msymbol -> name + 1, SYMBOL_NAME (sym) + 1))
990 {
991 /* Splice this symbol out of the hash chain and
992 assign the value we have to it. */
993 if (prev)
994 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
995 else
996 global_sym_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
997
998 /* Check to see whether we need to fix up a common block. */
999 /* Note: this code might be executed several times for
1000 the same symbol if there are multiple references. */
1001 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
1002 fix_common_block (sym, msymbol -> address);
1003 else
1004 SYMBOL_VALUE_ADDRESS (sym) = msymbol -> address;
1005
1006 if (prev)
1007 sym = SYMBOL_VALUE_CHAIN (prev);
1008 else
1009 sym = global_sym_chain[hash];
1010 }
1011 else
1012 {
1013 prev = sym;
1014 sym = SYMBOL_VALUE_CHAIN (sym);
1015 }
1016 }
1017 }
1018 }
1019
1020 \f
1021 /* Read a number by which a type is referred to in dbx data,
1022 or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
1023 Just a single number N is equivalent to (0,N).
1024 Return the two numbers by storing them in the vector TYPENUMS.
1025 TYPENUMS will then be used as an argument to dbx_lookup_type. */
1026
1027 void
1028 read_type_number (pp, typenums)
1029 register char **pp;
1030 register int *typenums;
1031 {
1032 if (**pp == '(')
1033 {
1034 (*pp)++;
1035 typenums[0] = read_number (pp, ',');
1036 typenums[1] = read_number (pp, ')');
1037 }
1038 else
1039 {
1040 typenums[0] = 0;
1041 typenums[1] = read_number (pp, 0);
1042 }
1043 }
1044 \f
1045 /* To handle GNU C++ typename abbreviation, we need to be able to
1046 fill in a type's name as soon as space for that type is allocated.
1047 `type_synonym_name' is the name of the type being allocated.
1048 It is cleared as soon as it is used (lest all allocated types
1049 get this name). */
1050 static char *type_synonym_name;
1051
1052 /* ARGSUSED */
1053 struct symbol *
1054 define_symbol (valu, string, desc, type, objfile)
1055 unsigned int valu;
1056 char *string;
1057 int desc;
1058 int type;
1059 struct objfile *objfile;
1060 {
1061 register struct symbol *sym;
1062 char *p = (char *) strchr (string, ':');
1063 int deftype;
1064 int synonym = 0;
1065 register int i;
1066 struct type *temptype;
1067
1068 /* Ignore syms with empty names. */
1069 if (string[0] == 0)
1070 return 0;
1071
1072 /* Ignore old-style symbols from cc -go */
1073 if (p == 0)
1074 return 0;
1075
1076 sym = (struct symbol *)obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symbol));
1077
1078 if (processing_gcc_compilation) {
1079 /* GCC 2.x puts the line number in desc. SunOS apparently puts in the
1080 number of bytes occupied by a type or object, which we ignore. */
1081 SYMBOL_LINE(sym) = desc;
1082 } else {
1083 SYMBOL_LINE(sym) = 0; /* unknown */
1084 }
1085
1086 if (string[0] == CPLUS_MARKER)
1087 {
1088 /* Special GNU C++ names. */
1089 switch (string[1])
1090 {
1091 case 't':
1092 SYMBOL_NAME (sym) = obsavestring ("this", strlen ("this"),
1093 &objfile -> symbol_obstack);
1094 break;
1095 case 'v': /* $vtbl_ptr_type */
1096 /* Was: SYMBOL_NAME (sym) = "vptr"; */
1097 goto normal;
1098 case 'e':
1099 SYMBOL_NAME (sym) = obsavestring ("eh_throw", strlen ("eh_throw"),
1100 &objfile -> symbol_obstack);
1101 break;
1102
1103 case '_':
1104 /* This was an anonymous type that was never fixed up. */
1105 goto normal;
1106
1107 default:
1108 abort ();
1109 }
1110 }
1111 else
1112 {
1113 normal:
1114 SYMBOL_NAME (sym)
1115 = (char *) obstack_alloc (&objfile -> symbol_obstack, ((p - string) + 1));
1116 /* Open-coded bcopy--saves function call time. */
1117 {
1118 register char *p1 = string;
1119 register char *p2 = SYMBOL_NAME (sym);
1120 while (p1 != p)
1121 *p2++ = *p1++;
1122 *p2++ = '\0';
1123 }
1124 }
1125 p++;
1126 /* Determine the type of name being defined. */
1127 /* The Acorn RISC machine's compiler can put out locals that don't
1128 start with "234=" or "(3,4)=", so assume anything other than the
1129 deftypes we know how to handle is a local. */
1130 /* (Peter Watkins @ Computervision)
1131 Handle Sun-style local fortran array types 'ar...' .
1132 (gnu@cygnus.com) -- this strchr() handles them properly?
1133 (tiemann@cygnus.com) -- 'C' is for catch. */
1134 if (!strchr ("cfFGpPrStTvVXC", *p))
1135 deftype = 'l';
1136 else
1137 deftype = *p++;
1138
1139 /* c is a special case, not followed by a type-number.
1140 SYMBOL:c=iVALUE for an integer constant symbol.
1141 SYMBOL:c=rVALUE for a floating constant symbol.
1142 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
1143 e.g. "b:c=e6,0" for "const b = blob1"
1144 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
1145 if (deftype == 'c')
1146 {
1147 if (*p++ != '=')
1148 error ("Invalid symbol data at symtab pos %d.", symnum);
1149 switch (*p++)
1150 {
1151 case 'r':
1152 {
1153 double d = atof (p);
1154 char *dbl_valu;
1155
1156 SYMBOL_TYPE (sym) = lookup_fundamental_type (objfile,
1157 FT_DBL_PREC_FLOAT);
1158 dbl_valu = (char *)
1159 obstack_alloc (&objfile -> type_obstack,
1160 sizeof (double));
1161 memcpy (dbl_valu, &d, sizeof (double));
1162 SWAP_TARGET_AND_HOST (dbl_valu, sizeof (double));
1163 SYMBOL_VALUE_BYTES (sym) = dbl_valu;
1164 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
1165 }
1166 break;
1167 case 'i':
1168 {
1169 SYMBOL_TYPE (sym) = lookup_fundamental_type (objfile,
1170 FT_INTEGER);
1171 SYMBOL_VALUE (sym) = atoi (p);
1172 SYMBOL_CLASS (sym) = LOC_CONST;
1173 }
1174 break;
1175 case 'e':
1176 /* SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
1177 e.g. "b:c=e6,0" for "const b = blob1"
1178 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
1179 {
1180 int typenums[2];
1181
1182 read_type_number (&p, typenums);
1183 if (*p++ != ',')
1184 error ("Invalid symbol data: no comma in enum const symbol");
1185
1186 SYMBOL_TYPE (sym) = *dbx_lookup_type (typenums);
1187 SYMBOL_VALUE (sym) = atoi (p);
1188 SYMBOL_CLASS (sym) = LOC_CONST;
1189 }
1190 break;
1191 default:
1192 error ("Invalid symbol data at symtab pos %d.", symnum);
1193 }
1194 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1195 add_symbol_to_list (sym, &file_symbols);
1196 return sym;
1197 }
1198
1199 /* Now usually comes a number that says which data type,
1200 and possibly more stuff to define the type
1201 (all of which is handled by read_type) */
1202
1203 if (deftype == 'p' && *p == 'F')
1204 /* pF is a two-letter code that means a function parameter in Fortran.
1205 The type-number specifies the type of the return value.
1206 Translate it into a pointer-to-function type. */
1207 {
1208 p++;
1209 SYMBOL_TYPE (sym)
1210 = lookup_pointer_type (lookup_function_type (read_type (&p, objfile)));
1211 }
1212 else
1213 {
1214 struct type *type_read;
1215 synonym = *p == 't';
1216
1217 if (synonym)
1218 {
1219 p += 1;
1220 type_synonym_name = obsavestring (SYMBOL_NAME (sym),
1221 strlen (SYMBOL_NAME (sym)),
1222 &objfile -> symbol_obstack);
1223 }
1224
1225 /* Here we save the name of the symbol for read_range_type, which
1226 ends up reading in the basic types. In stabs, unfortunately there
1227 is no distinction between "int" and "long" types except their
1228 names. Until we work out a saner type policy (eliminating most
1229 builtin types and using the names specified in the files), we
1230 save away the name so that far away from here in read_range_type,
1231 we can examine it to decide between "int" and "long". FIXME. */
1232 long_kludge_name = SYMBOL_NAME (sym);
1233 type_read = read_type (&p, objfile);
1234
1235 if ((deftype == 'F' || deftype == 'f')
1236 && TYPE_CODE (type_read) != TYPE_CODE_FUNC)
1237 {
1238 #if 0
1239 /* This code doesn't work -- it needs to realloc and can't. */
1240 struct type *new = (struct type *)
1241 obstack_alloc (&objfile -> type_obstack,
1242 sizeof (struct type));
1243
1244 /* Generate a template for the type of this function. The
1245 types of the arguments will be added as we read the symbol
1246 table. */
1247 *new = *lookup_function_type (type_read);
1248 SYMBOL_TYPE(sym) = new;
1249 TYPE_OBJFILE (new) = objfile;
1250 in_function_type = new;
1251 #else
1252 SYMBOL_TYPE (sym) = lookup_function_type (type_read);
1253 #endif
1254 }
1255 else
1256 SYMBOL_TYPE (sym) = type_read;
1257 }
1258
1259 switch (deftype)
1260 {
1261 case 'C':
1262 /* The name of a caught exception. */
1263 SYMBOL_CLASS (sym) = LOC_LABEL;
1264 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1265 SYMBOL_VALUE_ADDRESS (sym) = valu;
1266 add_symbol_to_list (sym, &local_symbols);
1267 break;
1268
1269 case 'f':
1270 SYMBOL_CLASS (sym) = LOC_BLOCK;
1271 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1272 add_symbol_to_list (sym, &file_symbols);
1273 break;
1274
1275 case 'F':
1276 SYMBOL_CLASS (sym) = LOC_BLOCK;
1277 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1278 add_symbol_to_list (sym, &global_symbols);
1279 break;
1280
1281 case 'G':
1282 /* For a class G (global) symbol, it appears that the
1283 value is not correct. It is necessary to search for the
1284 corresponding linker definition to find the value.
1285 These definitions appear at the end of the namelist. */
1286 i = hashname (SYMBOL_NAME (sym));
1287 SYMBOL_VALUE_CHAIN (sym) = global_sym_chain[i];
1288 global_sym_chain[i] = sym;
1289 SYMBOL_CLASS (sym) = LOC_STATIC;
1290 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1291 add_symbol_to_list (sym, &global_symbols);
1292 break;
1293
1294 /* This case is faked by a conditional above,
1295 when there is no code letter in the dbx data.
1296 Dbx data never actually contains 'l'. */
1297 case 'l':
1298 SYMBOL_CLASS (sym) = LOC_LOCAL;
1299 SYMBOL_VALUE (sym) = valu;
1300 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1301 add_symbol_to_list (sym, &local_symbols);
1302 break;
1303
1304 case 'p':
1305 /* Normally this is a parameter, a LOC_ARG. On the i960, it
1306 can also be a LOC_LOCAL_ARG depending on symbol type. */
1307 #ifndef DBX_PARM_SYMBOL_CLASS
1308 #define DBX_PARM_SYMBOL_CLASS(type) LOC_ARG
1309 #endif
1310 SYMBOL_CLASS (sym) = DBX_PARM_SYMBOL_CLASS (type);
1311 SYMBOL_VALUE (sym) = valu;
1312 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1313 #if 0
1314 /* This doesn't work yet. */
1315 add_param_to_type (&in_function_type, sym);
1316 #endif
1317 add_symbol_to_list (sym, &local_symbols);
1318
1319 /* If it's gcc-compiled, if it says `short', believe it. */
1320 if (processing_gcc_compilation || BELIEVE_PCC_PROMOTION)
1321 break;
1322
1323 #if defined(BELIEVE_PCC_PROMOTION_TYPE)
1324 /* This macro is defined on machines (e.g. sparc) where
1325 we should believe the type of a PCC 'short' argument,
1326 but shouldn't believe the address (the address is
1327 the address of the corresponding int). Note that
1328 this is only different from the BELIEVE_PCC_PROMOTION
1329 case on big-endian machines.
1330
1331 My guess is that this correction, as opposed to changing
1332 the parameter to an 'int' (as done below, for PCC
1333 on most machines), is the right thing to do
1334 on all machines, but I don't want to risk breaking
1335 something that already works. On most PCC machines,
1336 the sparc problem doesn't come up because the calling
1337 function has to zero the top bytes (not knowing whether
1338 the called function wants an int or a short), so there
1339 is no practical difference between an int and a short
1340 (except perhaps what happens when the GDB user types
1341 "print short_arg = 0x10000;").
1342
1343 Hacked for SunOS 4.1 by gnu@cygnus.com. In 4.1, the compiler
1344 actually produces the correct address (we don't need to fix it
1345 up). I made this code adapt so that it will offset the symbol
1346 if it was pointing at an int-aligned location and not
1347 otherwise. This way you can use the same gdb for 4.0.x and
1348 4.1 systems.
1349
1350 If the parameter is shorter than an int, and is integral
1351 (e.g. char, short, or unsigned equivalent), and is claimed to
1352 be passed on an integer boundary, don't believe it! Offset the
1353 parameter's address to the tail-end of that integer. */
1354
1355 temptype = lookup_fundamental_type (objfile, FT_INTEGER);
1356 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1357 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
1358 && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (temptype))
1359 {
1360 SYMBOL_VALUE (sym) += TYPE_LENGTH (temptype)
1361 - TYPE_LENGTH (SYMBOL_TYPE (sym));
1362 }
1363 break;
1364
1365 #else /* no BELIEVE_PCC_PROMOTION_TYPE. */
1366
1367 /* If PCC says a parameter is a short or a char,
1368 it is really an int. */
1369 temptype = lookup_fundamental_type (objfile, FT_INTEGER);
1370 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1371 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1372 {
1373 SYMBOL_TYPE (sym) = TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1374 ? lookup_fundamental_type (objfile, FT_UNSIGNED_INTEGER)
1375 : temptype;
1376 }
1377 break;
1378
1379 #endif /* no BELIEVE_PCC_PROMOTION_TYPE. */
1380
1381 case 'P':
1382 SYMBOL_CLASS (sym) = LOC_REGPARM;
1383 SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (valu);
1384 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1385 add_symbol_to_list (sym, &local_symbols);
1386 break;
1387
1388 case 'r':
1389 SYMBOL_CLASS (sym) = LOC_REGISTER;
1390 SYMBOL_VALUE (sym) = STAB_REG_TO_REGNUM (valu);
1391 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1392 add_symbol_to_list (sym, &local_symbols);
1393 break;
1394
1395 case 'S':
1396 /* Static symbol at top level of file */
1397 SYMBOL_CLASS (sym) = LOC_STATIC;
1398 SYMBOL_VALUE_ADDRESS (sym) = valu;
1399 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1400 add_symbol_to_list (sym, &file_symbols);
1401 break;
1402
1403 case 't':
1404 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1405 SYMBOL_VALUE (sym) = valu;
1406 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1407 if (TYPE_NAME (SYMBOL_TYPE (sym)) == NULL)
1408 TYPE_NAME (SYMBOL_TYPE (sym)) =
1409 obsavestring (SYMBOL_NAME (sym),
1410 strlen (SYMBOL_NAME (sym)),
1411 &objfile -> symbol_obstack);
1412 /* C++ vagaries: we may have a type which is derived from
1413 a base type which did not have its name defined when the
1414 derived class was output. We fill in the derived class's
1415 base part member's name here in that case. */
1416 else if ((TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
1417 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_UNION)
1418 && TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)))
1419 {
1420 int j;
1421 for (j = TYPE_N_BASECLASSES (SYMBOL_TYPE (sym)) - 1; j >= 0; j--)
1422 if (TYPE_BASECLASS_NAME (SYMBOL_TYPE (sym), j) == 0)
1423 TYPE_BASECLASS_NAME (SYMBOL_TYPE (sym), j) =
1424 type_name_no_tag (TYPE_BASECLASS (SYMBOL_TYPE (sym), j));
1425 }
1426
1427 add_symbol_to_list (sym, &file_symbols);
1428 break;
1429
1430 case 'T':
1431 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1432 SYMBOL_VALUE (sym) = valu;
1433 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1434 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1435 TYPE_NAME (SYMBOL_TYPE (sym))
1436 = obconcat (&objfile -> type_obstack, "",
1437 (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM
1438 ? "enum "
1439 : (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
1440 ? "struct " : "union ")),
1441 SYMBOL_NAME (sym));
1442 add_symbol_to_list (sym, &file_symbols);
1443
1444 if (synonym)
1445 {
1446 register struct symbol *typedef_sym = (struct symbol *)
1447 obstack_alloc (&objfile -> type_obstack,
1448 sizeof (struct symbol));
1449 SYMBOL_NAME (typedef_sym) = SYMBOL_NAME (sym);
1450 SYMBOL_TYPE (typedef_sym) = SYMBOL_TYPE (sym);
1451
1452 SYMBOL_CLASS (typedef_sym) = LOC_TYPEDEF;
1453 SYMBOL_VALUE (typedef_sym) = valu;
1454 SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE;
1455 add_symbol_to_list (typedef_sym, &file_symbols);
1456 }
1457 break;
1458
1459 case 'V':
1460 /* Static symbol of local scope */
1461 SYMBOL_CLASS (sym) = LOC_STATIC;
1462 SYMBOL_VALUE_ADDRESS (sym) = valu;
1463 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1464 add_symbol_to_list (sym, &local_symbols);
1465 break;
1466
1467 case 'v':
1468 /* Reference parameter */
1469 SYMBOL_CLASS (sym) = LOC_REF_ARG;
1470 SYMBOL_VALUE (sym) = valu;
1471 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1472 add_symbol_to_list (sym, &local_symbols);
1473 break;
1474
1475 case 'X':
1476 /* This is used by Sun FORTRAN for "function result value".
1477 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1478 that Pascal uses it too, but when I tried it Pascal used
1479 "x:3" (local symbol) instead. */
1480 SYMBOL_CLASS (sym) = LOC_LOCAL;
1481 SYMBOL_VALUE (sym) = valu;
1482 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1483 add_symbol_to_list (sym, &local_symbols);
1484 break;
1485
1486 default:
1487 error ("Invalid symbol data: unknown symbol-type code `%c' at symtab pos %d.", deftype, symnum);
1488 }
1489 return sym;
1490 }
1491 \f
1492 /* What about types defined as forward references inside of a small lexical
1493 scope? */
1494 /* Add a type to the list of undefined types to be checked through
1495 once this file has been read in. */
1496 void
1497 add_undefined_type (type)
1498 struct type *type;
1499 {
1500 if (undef_types_length == undef_types_allocated)
1501 {
1502 undef_types_allocated *= 2;
1503 undef_types = (struct type **)
1504 xrealloc ((char *) undef_types,
1505 undef_types_allocated * sizeof (struct type *));
1506 }
1507 undef_types[undef_types_length++] = type;
1508 }
1509
1510 /* Go through each undefined type, see if it's still undefined, and fix it
1511 up if possible. We have two kinds of undefined types:
1512
1513 TYPE_CODE_ARRAY: Array whose target type wasn't defined yet.
1514 Fix: update array length using the element bounds
1515 and the target type's length.
1516 TYPE_CODE_STRUCT, TYPE_CODE_UNION: Structure whose fields were not
1517 yet defined at the time a pointer to it was made.
1518 Fix: Do a full lookup on the struct/union tag. */
1519 static void
1520 cleanup_undefined_types ()
1521 {
1522 struct type **type;
1523
1524 for (type = undef_types; type < undef_types + undef_types_length; type++) {
1525 switch (TYPE_CODE (*type)) {
1526
1527 case TYPE_CODE_STRUCT:
1528 case TYPE_CODE_UNION:
1529 case TYPE_CODE_ENUM:
1530 {
1531 /* Reasonable test to see if it's been defined since. */
1532 if (TYPE_NFIELDS (*type) == 0)
1533 {
1534 struct pending *ppt;
1535 int i;
1536 /* Name of the type, without "struct" or "union" */
1537 char *typename = TYPE_NAME (*type);
1538
1539 if (!strncmp (typename, "struct ", 7))
1540 typename += 7;
1541 if (!strncmp (typename, "union ", 6))
1542 typename += 6;
1543 if (!strncmp (typename, "enum ", 5))
1544 typename += 5;
1545
1546 for (ppt = file_symbols; ppt; ppt = ppt->next)
1547 for (i = 0; i < ppt->nsyms; i++)
1548 {
1549 struct symbol *sym = ppt->symbol[i];
1550
1551 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
1552 && SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE
1553 && (TYPE_CODE (SYMBOL_TYPE (sym)) ==
1554 TYPE_CODE (*type))
1555 && !strcmp (SYMBOL_NAME (sym), typename))
1556 memcpy (*type, SYMBOL_TYPE (sym), sizeof (struct type));
1557 }
1558 }
1559 else
1560 /* It has been defined; don't mark it as a stub. */
1561 TYPE_FLAGS (*type) &= ~TYPE_FLAG_STUB;
1562 }
1563 break;
1564
1565 case TYPE_CODE_ARRAY:
1566 {
1567 struct type *range_type;
1568 int lower, upper;
1569
1570 if (TYPE_LENGTH (*type) != 0) /* Better be unknown */
1571 goto badtype;
1572 if (TYPE_NFIELDS (*type) != 1)
1573 goto badtype;
1574 range_type = TYPE_FIELD_TYPE (*type, 0);
1575 if (TYPE_CODE (range_type) != TYPE_CODE_RANGE)
1576 goto badtype;
1577
1578 /* Now recompute the length of the array type, based on its
1579 number of elements and the target type's length. */
1580 lower = TYPE_FIELD_BITPOS (range_type, 0);
1581 upper = TYPE_FIELD_BITPOS (range_type, 1);
1582 TYPE_LENGTH (*type) = (upper - lower + 1)
1583 * TYPE_LENGTH (TYPE_TARGET_TYPE (*type));
1584 }
1585 break;
1586
1587 default:
1588 badtype:
1589 error ("GDB internal error. cleanup_undefined_types with bad\
1590 type %d.", TYPE_CODE (*type));
1591 break;
1592 }
1593 }
1594 undef_types_length = 0;
1595 }
1596 \f
1597 /* Skip rest of this symbol and return an error type.
1598
1599 General notes on error recovery: error_type always skips to the
1600 end of the symbol (modulo cretinous dbx symbol name continuation).
1601 Thus code like this:
1602
1603 if (*(*pp)++ != ';')
1604 return error_type (pp);
1605
1606 is wrong because if *pp starts out pointing at '\0' (typically as the
1607 result of an earlier error), it will be incremented to point to the
1608 start of the next symbol, which might produce strange results, at least
1609 if you run off the end of the string table. Instead use
1610
1611 if (**pp != ';')
1612 return error_type (pp);
1613 ++*pp;
1614
1615 or
1616
1617 if (**pp != ';')
1618 foo = error_type (pp);
1619 else
1620 ++*pp;
1621
1622 And in case it isn't obvious, the point of all this hair is so the compiler
1623 can define new types and new syntaxes, and old versions of the
1624 debugger will be able to read the new symbol tables. */
1625
1626 struct type *
1627 error_type (pp)
1628 char **pp;
1629 {
1630 complain (&error_type_complaint, 0);
1631 while (1)
1632 {
1633 /* Skip to end of symbol. */
1634 while (**pp != '\0')
1635 (*pp)++;
1636
1637 /* Check for and handle cretinous dbx symbol name continuation! */
1638 if ((*pp)[-1] == '\\')
1639 *pp = next_symbol_text ();
1640 else
1641 break;
1642 }
1643 return builtin_type_error;
1644 }
1645 \f
1646 /* Read a dbx type reference or definition;
1647 return the type that is meant.
1648 This can be just a number, in which case it references
1649 a type already defined and placed in type_vector.
1650 Or the number can be followed by an =, in which case
1651 it means to define a new type according to the text that
1652 follows the =. */
1653
1654 struct type *
1655 read_type (pp, objfile)
1656 register char **pp;
1657 struct objfile *objfile;
1658 {
1659 register struct type *type = 0;
1660 struct type *type1;
1661 int typenums[2];
1662 int xtypenums[2];
1663
1664 /* Read type number if present. The type number may be omitted.
1665 for instance in a two-dimensional array declared with type
1666 "ar1;1;10;ar1;1;10;4". */
1667 if ((**pp >= '0' && **pp <= '9')
1668 || **pp == '(')
1669 {
1670 read_type_number (pp, typenums);
1671
1672 /* Type is not being defined here. Either it already exists,
1673 or this is a forward reference to it. dbx_alloc_type handles
1674 both cases. */
1675 if (**pp != '=')
1676 return dbx_alloc_type (typenums, objfile);
1677
1678 /* Type is being defined here. */
1679 #if 0 /* Callers aren't prepared for a NULL result! FIXME -- metin! */
1680 {
1681 struct type *tt;
1682
1683 /* if such a type already exists, this is an unnecessary duplication
1684 of the stab string, which is common in (RS/6000) xlc generated
1685 objects. In that case, simply return NULL and let the caller take
1686 care of it. */
1687
1688 tt = *dbx_lookup_type (typenums);
1689 if (tt && tt->length && tt->code)
1690 return NULL;
1691 }
1692 #endif
1693
1694 *pp += 2;
1695 }
1696 else
1697 {
1698 /* 'typenums=' not present, type is anonymous. Read and return
1699 the definition, but don't put it in the type vector. */
1700 typenums[0] = typenums[1] = -1;
1701 *pp += 1;
1702 }
1703
1704 switch ((*pp)[-1])
1705 {
1706 case 'x':
1707 {
1708 enum type_code code;
1709
1710 /* Used to index through file_symbols. */
1711 struct pending *ppt;
1712 int i;
1713
1714 /* Name including "struct", etc. */
1715 char *type_name;
1716
1717 /* Name without "struct", etc. */
1718 char *type_name_only;
1719
1720 {
1721 char *prefix;
1722 char *from, *to;
1723
1724 /* Set the type code according to the following letter. */
1725 switch ((*pp)[0])
1726 {
1727 case 's':
1728 code = TYPE_CODE_STRUCT;
1729 prefix = "struct ";
1730 break;
1731 case 'u':
1732 code = TYPE_CODE_UNION;
1733 prefix = "union ";
1734 break;
1735 case 'e':
1736 code = TYPE_CODE_ENUM;
1737 prefix = "enum ";
1738 break;
1739 default:
1740 return error_type (pp);
1741 }
1742
1743 to = type_name = (char *)
1744 obstack_alloc (&objfile -> type_obstack,
1745 (strlen (prefix) +
1746 ((char *) strchr (*pp, ':') - (*pp)) + 1));
1747
1748 /* Copy the prefix. */
1749 from = prefix;
1750 while (*to++ = *from++)
1751 ;
1752 to--;
1753
1754 type_name_only = to;
1755
1756 /* Copy the name. */
1757 from = *pp + 1;
1758 while ((*to++ = *from++) != ':')
1759 ;
1760 *--to = '\0';
1761
1762 /* Set the pointer ahead of the name which we just read. */
1763 *pp = from;
1764
1765 #if 0
1766 /* The following hack is clearly wrong, because it doesn't
1767 check whether we are in a baseclass. I tried to reproduce
1768 the case that it is trying to fix, but I couldn't get
1769 g++ to put out a cross reference to a basetype. Perhaps
1770 it doesn't do it anymore. */
1771 /* Note: for C++, the cross reference may be to a base type which
1772 has not yet been seen. In this case, we skip to the comma,
1773 which will mark the end of the base class name. (The ':'
1774 at the end of the base class name will be skipped as well.)
1775 But sometimes (ie. when the cross ref is the last thing on
1776 the line) there will be no ','. */
1777 from = (char *) strchr (*pp, ',');
1778 if (from)
1779 *pp = from;
1780 #endif /* 0 */
1781 }
1782
1783 /* Now check to see whether the type has already been declared. */
1784 /* This is necessary at least in the case where the
1785 program says something like
1786 struct foo bar[5];
1787 The compiler puts out a cross-reference; we better find
1788 set the length of the structure correctly so we can
1789 set the length of the array. */
1790 for (ppt = file_symbols; ppt; ppt = ppt->next)
1791 for (i = 0; i < ppt->nsyms; i++)
1792 {
1793 struct symbol *sym = ppt->symbol[i];
1794
1795 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF
1796 && SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE
1797 && (TYPE_CODE (SYMBOL_TYPE (sym)) == code)
1798 && !strcmp (SYMBOL_NAME (sym), type_name_only))
1799 {
1800 obstack_free (&objfile -> type_obstack, type_name);
1801 type = SYMBOL_TYPE (sym);
1802 return type;
1803 }
1804 }
1805
1806 /* Didn't find the type to which this refers, so we must
1807 be dealing with a forward reference. Allocate a type
1808 structure for it, and keep track of it so we can
1809 fill in the rest of the fields when we get the full
1810 type. */
1811 type = dbx_alloc_type (typenums, objfile);
1812 TYPE_CODE (type) = code;
1813 TYPE_NAME (type) = type_name;
1814 INIT_CPLUS_SPECIFIC(type);
1815 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
1816
1817 add_undefined_type (type);
1818 return type;
1819 }
1820
1821 case '-': /* RS/6000 built-in type */
1822 (*pp)--;
1823 type = builtin_type (pp); /* (in xcoffread.c) */
1824 goto after_digits;
1825
1826 case '0':
1827 case '1':
1828 case '2':
1829 case '3':
1830 case '4':
1831 case '5':
1832 case '6':
1833 case '7':
1834 case '8':
1835 case '9':
1836 case '(':
1837 (*pp)--;
1838 read_type_number (pp, xtypenums);
1839 type = *dbx_lookup_type (xtypenums);
1840 /* fall through */
1841
1842 after_digits:
1843 if (type == 0)
1844 type = lookup_fundamental_type (objfile, FT_VOID);
1845 if (typenums[0] != -1)
1846 *dbx_lookup_type (typenums) = type;
1847 break;
1848
1849 case '*':
1850 type1 = read_type (pp, objfile);
1851 /* FIXME -- we should be doing smash_to_XXX types here. */
1852 #if 0
1853 /* postponed type decoration should be allowed. */
1854 if (typenums[1] > 0 && typenums[1] < type_vector_length &&
1855 (type = type_vector[typenums[1]])) {
1856 smash_to_pointer_type (type, type1);
1857 break;
1858 }
1859 #endif
1860 type = lookup_pointer_type (type1);
1861 if (typenums[0] != -1)
1862 *dbx_lookup_type (typenums) = type;
1863 break;
1864
1865 case '@':
1866 {
1867 struct type *domain = read_type (pp, objfile);
1868 struct type *memtype;
1869
1870 if (**pp != ',')
1871 /* Invalid member type data format. */
1872 return error_type (pp);
1873 ++*pp;
1874
1875 memtype = read_type (pp, objfile);
1876 type = dbx_alloc_type (typenums, objfile);
1877 smash_to_member_type (type, domain, memtype);
1878 }
1879 break;
1880
1881 case '#':
1882 if ((*pp)[0] == '#')
1883 {
1884 /* We'll get the parameter types from the name. */
1885 struct type *return_type;
1886
1887 *pp += 1;
1888 return_type = read_type (pp, objfile);
1889 if (*(*pp)++ != ';')
1890 complain (&invalid_member_complaint, (char *) symnum);
1891 type = allocate_stub_method (return_type);
1892 if (typenums[0] != -1)
1893 *dbx_lookup_type (typenums) = type;
1894 }
1895 else
1896 {
1897 struct type *domain = read_type (pp, objfile);
1898 struct type *return_type;
1899 struct type **args;
1900
1901 if (*(*pp)++ != ',')
1902 error ("invalid member type data format, at symtab pos %d.",
1903 symnum);
1904
1905 return_type = read_type (pp, objfile);
1906 args = read_args (pp, ';', objfile);
1907 type = dbx_alloc_type (typenums, objfile);
1908 smash_to_method_type (type, domain, return_type, args);
1909 }
1910 break;
1911
1912 case '&':
1913 type1 = read_type (pp, objfile);
1914 type = lookup_reference_type (type1);
1915 if (typenums[0] != -1)
1916 *dbx_lookup_type (typenums) = type;
1917 break;
1918
1919 case 'f':
1920 type1 = read_type (pp, objfile);
1921 type = lookup_function_type (type1);
1922 if (typenums[0] != -1)
1923 *dbx_lookup_type (typenums) = type;
1924 break;
1925
1926 case 'r':
1927 type = read_range_type (pp, typenums, objfile);
1928 if (typenums[0] != -1)
1929 *dbx_lookup_type (typenums) = type;
1930 break;
1931
1932 case 'e':
1933 type = dbx_alloc_type (typenums, objfile);
1934 type = read_enum_type (pp, type, objfile);
1935 *dbx_lookup_type (typenums) = type;
1936 break;
1937
1938 case 's':
1939 type = dbx_alloc_type (typenums, objfile);
1940 TYPE_NAME (type) = type_synonym_name;
1941 type_synonym_name = 0;
1942 type = read_struct_type (pp, type, objfile);
1943 break;
1944
1945 case 'u':
1946 type = dbx_alloc_type (typenums, objfile);
1947 TYPE_NAME (type) = type_synonym_name;
1948 type_synonym_name = 0;
1949 type = read_struct_type (pp, type, objfile);
1950 TYPE_CODE (type) = TYPE_CODE_UNION;
1951 break;
1952
1953 case 'a':
1954 if (**pp != 'r')
1955 return error_type (pp);
1956 ++*pp;
1957
1958 type = dbx_alloc_type (typenums, objfile);
1959 type = read_array_type (pp, type, objfile);
1960 break;
1961
1962 default:
1963 --*pp; /* Go back to the symbol in error */
1964 /* Particularly important if it was \0! */
1965 return error_type (pp);
1966 }
1967
1968 if (type == 0)
1969 abort ();
1970
1971 #if 0
1972 /* If this is an overriding temporary alteration for a header file's
1973 contents, and this type number is unknown in the global definition,
1974 put this type into the global definition at this type number. */
1975 if (header_file_prev_index >= 0)
1976 {
1977 register struct type **tp
1978 = explicit_lookup_type (header_file_prev_index, typenums[1]);
1979 if (*tp == 0)
1980 *tp = type;
1981 }
1982 #endif
1983 return type;
1984 }
1985 \f
1986 /* This page contains subroutines of read_type. */
1987
1988 /* Read the description of a structure (or union type)
1989 and return an object describing the type. */
1990
1991 static struct type *
1992 read_struct_type (pp, type, objfile)
1993 char **pp;
1994 register struct type *type;
1995 struct objfile *objfile;
1996 {
1997 /* Total number of methods defined in this class.
1998 If the class defines two `f' methods, and one `g' method,
1999 then this will have the value 3. */
2000 int total_length = 0;
2001
2002 struct nextfield
2003 {
2004 struct nextfield *next;
2005 int visibility; /* 0=public, 1=protected, 2=public */
2006 struct field field;
2007 };
2008
2009 struct next_fnfield
2010 {
2011 struct next_fnfield *next;
2012 struct fn_field fn_field;
2013 };
2014
2015 struct next_fnfieldlist
2016 {
2017 struct next_fnfieldlist *next;
2018 struct fn_fieldlist fn_fieldlist;
2019 };
2020
2021 register struct nextfield *list = 0;
2022 struct nextfield *new;
2023 register char *p;
2024 int nfields = 0;
2025 int non_public_fields = 0;
2026 register int n;
2027
2028 register struct next_fnfieldlist *mainlist = 0;
2029 int nfn_fields = 0;
2030
2031 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2032 INIT_CPLUS_SPECIFIC(type);
2033
2034 /* First comes the total size in bytes. */
2035
2036 TYPE_LENGTH (type) = read_number (pp, 0);
2037
2038 /* C++: Now, if the class is a derived class, then the next character
2039 will be a '!', followed by the number of base classes derived from.
2040 Each element in the list contains visibility information,
2041 the offset of this base class in the derived structure,
2042 and then the base type. */
2043 if (**pp == '!')
2044 {
2045 int i, n_baseclasses, offset;
2046 struct type *baseclass;
2047 int via_public;
2048
2049 /* Nonzero if it is a virtual baseclass, i.e.,
2050
2051 struct A{};
2052 struct B{};
2053 struct C : public B, public virtual A {};
2054
2055 B is a baseclass of C; A is a virtual baseclass for C. This is a C++
2056 2.0 language feature. */
2057 int via_virtual;
2058
2059 *pp += 1;
2060
2061 ALLOCATE_CPLUS_STRUCT_TYPE(type);
2062
2063 n_baseclasses = read_number (pp, ',');
2064 TYPE_FIELD_VIRTUAL_BITS (type) =
2065 (B_TYPE *) obstack_alloc (&objfile -> type_obstack,
2066 B_BYTES (n_baseclasses));
2067 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), n_baseclasses);
2068
2069 for (i = 0; i < n_baseclasses; i++)
2070 {
2071 if (**pp == '\\')
2072 *pp = next_symbol_text ();
2073
2074 switch (**pp)
2075 {
2076 case '0':
2077 via_virtual = 0;
2078 break;
2079 case '1':
2080 via_virtual = 1;
2081 break;
2082 default:
2083 /* Bad visibility format. */
2084 return error_type (pp);
2085 }
2086 ++*pp;
2087
2088 switch (**pp)
2089 {
2090 case '0':
2091 via_public = 0;
2092 non_public_fields++;
2093 break;
2094 case '2':
2095 via_public = 2;
2096 break;
2097 default:
2098 /* Bad visibility format. */
2099 return error_type (pp);
2100 }
2101 if (via_virtual)
2102 SET_TYPE_FIELD_VIRTUAL (type, i);
2103 ++*pp;
2104
2105 /* Offset of the portion of the object corresponding to
2106 this baseclass. Always zero in the absence of
2107 multiple inheritance. */
2108 offset = read_number (pp, ',');
2109 baseclass = read_type (pp, objfile);
2110 *pp += 1; /* skip trailing ';' */
2111
2112 /* Make this baseclass visible for structure-printing purposes. */
2113 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2114 new->next = list;
2115 list = new;
2116 list->visibility = via_public;
2117 list->field.type = baseclass;
2118 list->field.name = type_name_no_tag (baseclass);
2119 list->field.bitpos = offset;
2120 list->field.bitsize = 0; /* this should be an unpacked field! */
2121 nfields++;
2122 }
2123 TYPE_N_BASECLASSES (type) = n_baseclasses;
2124 }
2125
2126 /* Now come the fields, as NAME:?TYPENUM,BITPOS,BITSIZE; for each one.
2127 At the end, we see a semicolon instead of a field.
2128
2129 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2130 a static field.
2131
2132 The `?' is a placeholder for one of '/2' (public visibility),
2133 '/1' (protected visibility), '/0' (private visibility), or nothing
2134 (C style symbol table, public visibility). */
2135
2136 /* We better set p right now, in case there are no fields at all... */
2137 p = *pp;
2138
2139 while (**pp != ';')
2140 {
2141 /* Check for and handle cretinous dbx symbol name continuation! */
2142 if (**pp == '\\') *pp = next_symbol_text ();
2143
2144 /* Get space to record the next field's data. */
2145 new = (struct nextfield *) alloca (sizeof (struct nextfield));
2146 new->next = list;
2147 list = new;
2148
2149 /* Get the field name. */
2150 p = *pp;
2151 if (*p == CPLUS_MARKER)
2152 {
2153 /* Special GNU C++ name. */
2154 if (*++p == 'v')
2155 {
2156 const char *prefix;
2157 char *name = 0;
2158 struct type *context;
2159
2160 switch (*++p)
2161 {
2162 case 'f':
2163 prefix = vptr_name;
2164 break;
2165 case 'b':
2166 prefix = vb_name;
2167 break;
2168 default:
2169 complain (&invalid_cpp_abbrev_complaint, *pp);
2170 prefix = "INVALID_C++_ABBREV";
2171 break;
2172 }
2173 *pp = p + 1;
2174 context = read_type (pp, objfile);
2175 name = type_name_no_tag (context);
2176 if (name == 0)
2177 {
2178 complain (&invalid_cpp_type_complaint, (char *) symnum);
2179 TYPE_NAME (context) = name;
2180 }
2181 list->field.name = obconcat (&objfile -> type_obstack,
2182 prefix, name, "");
2183 p = ++(*pp);
2184 if (p[-1] != ':')
2185 complain (&invalid_cpp_abbrev_complaint, *pp);
2186 list->field.type = read_type (pp, objfile);
2187 (*pp)++; /* Skip the comma. */
2188 list->field.bitpos = read_number (pp, ';');
2189 /* This field is unpacked. */
2190 list->field.bitsize = 0;
2191 list->visibility = 0; /* private */
2192 non_public_fields++;
2193 }
2194 /* GNU C++ anonymous type. */
2195 else if (*p == '_')
2196 break;
2197 else
2198 complain (&invalid_cpp_abbrev_complaint, *pp);
2199
2200 nfields++;
2201 continue;
2202 }
2203
2204 while (*p != ':') p++;
2205 list->field.name = obsavestring (*pp, p - *pp,
2206 &objfile -> type_obstack);
2207
2208 /* C++: Check to see if we have hit the methods yet. */
2209 if (p[1] == ':')
2210 break;
2211
2212 *pp = p + 1;
2213
2214 /* This means we have a visibility for a field coming. */
2215 if (**pp == '/')
2216 {
2217 switch (*++*pp)
2218 {
2219 case '0':
2220 list->visibility = 0; /* private */
2221 non_public_fields++;
2222 *pp += 1;
2223 break;
2224
2225 case '1':
2226 list->visibility = 1; /* protected */
2227 non_public_fields++;
2228 *pp += 1;
2229 break;
2230
2231 case '2':
2232 list->visibility = 2; /* public */
2233 *pp += 1;
2234 break;
2235 }
2236 }
2237 else /* normal dbx-style format. */
2238 list->visibility = 2; /* public */
2239
2240 list->field.type = read_type (pp, objfile);
2241 if (**pp == ':')
2242 {
2243 /* Static class member. */
2244 list->field.bitpos = (long)-1;
2245 p = ++(*pp);
2246 while (*p != ';') p++;
2247 list->field.bitsize = (long) savestring (*pp, p - *pp);
2248 *pp = p + 1;
2249 nfields++;
2250 continue;
2251 }
2252 else if (**pp != ',')
2253 /* Bad structure-type format. */
2254 return error_type (pp);
2255
2256 (*pp)++; /* Skip the comma. */
2257 list->field.bitpos = read_number (pp, ',');
2258 list->field.bitsize = read_number (pp, ';');
2259
2260 #if 0
2261 /* FIXME-tiemann: Can't the compiler put out something which
2262 lets us distinguish these? (or maybe just not put out anything
2263 for the field). What is the story here? What does the compiler
2264 really do? Also, patch gdb.texinfo for this case; I document
2265 it as a possible problem there. Search for "DBX-style". */
2266
2267 /* This is wrong because this is identical to the symbols
2268 produced for GCC 0-size arrays. For example:
2269 typedef union {
2270 int num;
2271 char str[0];
2272 } foo;
2273 The code which dumped core in such circumstances should be
2274 fixed not to dump core. */
2275
2276 /* g++ -g0 can put out bitpos & bitsize zero for a static
2277 field. This does not give us any way of getting its
2278 class, so we can't know its name. But we can just
2279 ignore the field so we don't dump core and other nasty
2280 stuff. */
2281 if (list->field.bitpos == 0
2282 && list->field.bitsize == 0)
2283 {
2284 complain (&dbx_class_complaint, 0);
2285 /* Ignore this field. */
2286 list = list->next;
2287 }
2288 else
2289 #endif /* 0 */
2290 {
2291 /* Detect an unpacked field and mark it as such.
2292 dbx gives a bit size for all fields.
2293 Note that forward refs cannot be packed,
2294 and treat enums as if they had the width of ints. */
2295 if (TYPE_CODE (list->field.type) != TYPE_CODE_INT
2296 && TYPE_CODE (list->field.type) != TYPE_CODE_ENUM)
2297 list->field.bitsize = 0;
2298 if ((list->field.bitsize == 8 * TYPE_LENGTH (list->field.type)
2299 || (TYPE_CODE (list->field.type) == TYPE_CODE_ENUM
2300 && (list->field.bitsize
2301 == 8 * TYPE_LENGTH (lookup_fundamental_type (objfile, FT_INTEGER)))
2302 )
2303 )
2304 &&
2305 list->field.bitpos % 8 == 0)
2306 list->field.bitsize = 0;
2307 nfields++;
2308 }
2309 }
2310
2311 if (p[1] == ':')
2312 /* chill the list of fields: the last entry (at the head)
2313 is a partially constructed entry which we now scrub. */
2314 list = list->next;
2315
2316 /* Now create the vector of fields, and record how big it is.
2317 We need this info to record proper virtual function table information
2318 for this class's virtual functions. */
2319
2320 TYPE_NFIELDS (type) = nfields;
2321 TYPE_FIELDS (type) = (struct field *)
2322 obstack_alloc (&objfile -> type_obstack, sizeof (struct field) * nfields);
2323
2324 if (non_public_fields)
2325 {
2326 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2327
2328 TYPE_FIELD_PRIVATE_BITS (type) =
2329 (B_TYPE *) obstack_alloc (&objfile -> type_obstack,
2330 B_BYTES (nfields));
2331 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
2332
2333 TYPE_FIELD_PROTECTED_BITS (type) =
2334 (B_TYPE *) obstack_alloc (&objfile -> type_obstack,
2335 B_BYTES (nfields));
2336 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
2337 }
2338
2339 /* Copy the saved-up fields into the field vector. */
2340
2341 for (n = nfields; list; list = list->next)
2342 {
2343 n -= 1;
2344 TYPE_FIELD (type, n) = list->field;
2345 if (list->visibility == 0)
2346 SET_TYPE_FIELD_PRIVATE (type, n);
2347 else if (list->visibility == 1)
2348 SET_TYPE_FIELD_PROTECTED (type, n);
2349 }
2350
2351 /* Now come the method fields, as NAME::methods
2352 where each method is of the form TYPENUM,ARGS,...:PHYSNAME;
2353 At the end, we see a semicolon instead of a field.
2354
2355 For the case of overloaded operators, the format is
2356 op$::*.methods, where $ is the CPLUS_MARKER (usually '$'),
2357 `*' holds the place for an operator name (such as `+=')
2358 and `.' marks the end of the operator name. */
2359 if (p[1] == ':')
2360 {
2361 /* Now, read in the methods. To simplify matters, we
2362 "unread" the name that has been read, so that we can
2363 start from the top. */
2364
2365 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2366 /* For each list of method lists... */
2367 do
2368 {
2369 int i;
2370 struct next_fnfield *sublist = 0;
2371 struct type *look_ahead_type = NULL;
2372 int length = 0;
2373 struct next_fnfieldlist *new_mainlist =
2374 (struct next_fnfieldlist *)alloca (sizeof (struct next_fnfieldlist));
2375 char *main_fn_name;
2376
2377 p = *pp;
2378
2379 /* read in the name. */
2380 while (*p != ':') p++;
2381 if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && (*pp)[2] == CPLUS_MARKER)
2382 {
2383 /* This is a completely wierd case. In order to stuff in the
2384 names that might contain colons (the usual name delimiter),
2385 Mike Tiemann defined a different name format which is
2386 signalled if the identifier is "op$". In that case, the
2387 format is "op$::XXXX." where XXXX is the name. This is
2388 used for names like "+" or "=". YUUUUUUUK! FIXME! */
2389 /* This lets the user type "break operator+".
2390 We could just put in "+" as the name, but that wouldn't
2391 work for "*". */
2392 static char opname[32] = {'o', 'p', CPLUS_MARKER};
2393 char *o = opname + 3;
2394
2395 /* Skip past '::'. */
2396 *pp = p + 2;
2397 if (**pp == '\\') *pp = next_symbol_text ();
2398 p = *pp;
2399 while (*p != '.')
2400 *o++ = *p++;
2401 main_fn_name = savestring (opname, o - opname);
2402 /* Skip past '.' */
2403 *pp = p + 1;
2404 }
2405 else
2406 {
2407 main_fn_name = savestring (*pp, p - *pp);
2408 /* Skip past '::'. */
2409 *pp = p + 2;
2410 }
2411 new_mainlist->fn_fieldlist.name = main_fn_name;
2412
2413 do
2414 {
2415 struct next_fnfield *new_sublist =
2416 (struct next_fnfield *)alloca (sizeof (struct next_fnfield));
2417
2418 /* Check for and handle cretinous dbx symbol name continuation! */
2419 if (look_ahead_type == NULL) /* Normal case. */
2420 {
2421 if (**pp == '\\') *pp = next_symbol_text ();
2422
2423 new_sublist->fn_field.type = read_type (pp, objfile);
2424 if (**pp != ':')
2425 /* Invalid symtab info for method. */
2426 return error_type (pp);
2427 }
2428 else
2429 { /* g++ version 1 kludge */
2430 new_sublist->fn_field.type = look_ahead_type;
2431 look_ahead_type = NULL;
2432 }
2433
2434 *pp += 1;
2435 p = *pp;
2436 while (*p != ';') p++;
2437
2438 /* If this is just a stub, then we don't have the
2439 real name here. */
2440 if (TYPE_FLAGS (new_sublist->fn_field.type) & TYPE_FLAG_STUB)
2441 new_sublist->fn_field.is_stub = 1;
2442 new_sublist->fn_field.physname = savestring (*pp, p - *pp);
2443 *pp = p + 1;
2444
2445 /* Set this method's visibility fields. */
2446 switch (*(*pp)++ - '0')
2447 {
2448 case 0:
2449 new_sublist->fn_field.is_private = 1;
2450 break;
2451 case 1:
2452 new_sublist->fn_field.is_protected = 1;
2453 break;
2454 }
2455
2456 if (**pp == '\\') *pp = next_symbol_text ();
2457 switch (**pp)
2458 {
2459 case 'A': /* Normal functions. */
2460 new_sublist->fn_field.is_const = 0;
2461 new_sublist->fn_field.is_volatile = 0;
2462 (*pp)++;
2463 break;
2464 case 'B': /* `const' member functions. */
2465 new_sublist->fn_field.is_const = 1;
2466 new_sublist->fn_field.is_volatile = 0;
2467 (*pp)++;
2468 break;
2469 case 'C': /* `volatile' member function. */
2470 new_sublist->fn_field.is_const = 0;
2471 new_sublist->fn_field.is_volatile = 1;
2472 (*pp)++;
2473 break;
2474 case 'D': /* `const volatile' member function. */
2475 new_sublist->fn_field.is_const = 1;
2476 new_sublist->fn_field.is_volatile = 1;
2477 (*pp)++;
2478 break;
2479 case '*': /* File compiled with g++ version 1 -- no info */
2480 case '?':
2481 case '.':
2482 break;
2483 default:
2484 complain (&const_vol_complaint, (char *) (long) **pp);
2485 break;
2486 }
2487
2488 switch (*(*pp)++)
2489 {
2490 case '*':
2491 /* virtual member function, followed by index. */
2492 /* The sign bit is set to distinguish pointers-to-methods
2493 from virtual function indicies. Since the array is
2494 in words, the quantity must be shifted left by 1
2495 on 16 bit machine, and by 2 on 32 bit machine, forcing
2496 the sign bit out, and usable as a valid index into
2497 the array. Remove the sign bit here. */
2498 new_sublist->fn_field.voffset =
2499 (0x7fffffff & read_number (pp, ';')) + 2;
2500
2501 if (**pp == '\\') *pp = next_symbol_text ();
2502
2503 if (**pp == ';' || **pp == '\0')
2504 /* Must be g++ version 1. */
2505 new_sublist->fn_field.fcontext = 0;
2506 else
2507 {
2508 /* Figure out from whence this virtual function came.
2509 It may belong to virtual function table of
2510 one of its baseclasses. */
2511 look_ahead_type = read_type (pp, objfile);
2512 if (**pp == ':')
2513 { /* g++ version 1 overloaded methods. */ }
2514 else
2515 {
2516 new_sublist->fn_field.fcontext = look_ahead_type;
2517 if (**pp != ';')
2518 return error_type (pp);
2519 else
2520 ++*pp;
2521 look_ahead_type = NULL;
2522 }
2523 }
2524 break;
2525
2526 case '?':
2527 /* static member function. */
2528 new_sublist->fn_field.voffset = VOFFSET_STATIC;
2529 if (strncmp (new_sublist->fn_field.physname,
2530 main_fn_name, strlen (main_fn_name)))
2531 new_sublist->fn_field.is_stub = 1;
2532 break;
2533
2534 default:
2535 /* error */
2536 complain (&member_fn_complaint, (char *) (long) (*pp)[-1]);
2537 /* Fall through into normal member function. */
2538
2539 case '.':
2540 /* normal member function. */
2541 new_sublist->fn_field.voffset = 0;
2542 new_sublist->fn_field.fcontext = 0;
2543 break;
2544 }
2545
2546 new_sublist->next = sublist;
2547 sublist = new_sublist;
2548 length++;
2549 if (**pp == '\\') *pp = next_symbol_text ();
2550 }
2551 while (**pp != ';' && **pp != '\0');
2552
2553 *pp += 1;
2554
2555 new_mainlist->fn_fieldlist.fn_fields =
2556 (struct fn_field *) obstack_alloc (&objfile -> type_obstack,
2557 sizeof (struct fn_field) * length);
2558 for (i = length; (i--, sublist); sublist = sublist->next)
2559 new_mainlist->fn_fieldlist.fn_fields[i] = sublist->fn_field;
2560
2561 new_mainlist->fn_fieldlist.length = length;
2562 new_mainlist->next = mainlist;
2563 mainlist = new_mainlist;
2564 nfn_fields++;
2565 total_length += length;
2566 }
2567 while (**pp != ';');
2568 }
2569
2570 *pp += 1;
2571
2572
2573 if (nfn_fields)
2574 {
2575 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2576 obstack_alloc (&objfile -> type_obstack,
2577 sizeof (struct fn_fieldlist) * nfn_fields);
2578 TYPE_NFN_FIELDS (type) = nfn_fields;
2579 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
2580 }
2581
2582 {
2583 int i;
2584 for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
2585 TYPE_NFN_FIELDS_TOTAL (type) +=
2586 TYPE_NFN_FIELDS_TOTAL (TYPE_BASECLASS (type, i));
2587 }
2588
2589 for (n = nfn_fields; mainlist; mainlist = mainlist->next) {
2590 --n; /* Circumvent Sun3 compiler bug */
2591 TYPE_FN_FIELDLISTS (type)[n] = mainlist->fn_fieldlist;
2592 }
2593
2594 if (**pp == '~')
2595 {
2596 *pp += 1;
2597
2598 if (**pp == '=' || **pp == '+' || **pp == '-')
2599 {
2600 /* Obsolete flags that used to indicate the presence
2601 of constructors and/or destructors. */
2602 *pp += 1;
2603 }
2604
2605 /* Read either a '%' or the final ';'. */
2606 if (*(*pp)++ == '%')
2607 {
2608 /* We'd like to be able to derive the vtable pointer field
2609 from the type information, but when it's inherited, that's
2610 hard. A reason it's hard is because we may read in the
2611 info about a derived class before we read in info about
2612 the base class that provides the vtable pointer field.
2613 Once the base info has been read, we could fill in the info
2614 for the derived classes, but for the fact that by then,
2615 we don't remember who needs what. */
2616
2617 int predicted_fieldno = -1;
2618
2619 /* Now we must record the virtual function table pointer's
2620 field information. */
2621
2622 struct type *t;
2623 int i;
2624
2625
2626 #if 0
2627 {
2628 /* In version 2, we derive the vfield ourselves. */
2629 for (n = 0; n < nfields; n++)
2630 {
2631 if (! strncmp (TYPE_FIELD_NAME (type, n), vptr_name,
2632 sizeof (vptr_name) -1))
2633 {
2634 predicted_fieldno = n;
2635 break;
2636 }
2637 }
2638 if (predicted_fieldno < 0)
2639 for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
2640 if (! TYPE_FIELD_VIRTUAL (type, n)
2641 && TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, n)) >= 0)
2642 {
2643 predicted_fieldno = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, n));
2644 break;
2645 }
2646 }
2647 #endif
2648
2649 t = read_type (pp, objfile);
2650 p = (*pp)++;
2651 while (*p != '\0' && *p != ';')
2652 p++;
2653 if (*p == '\0')
2654 /* Premature end of symbol. */
2655 return error_type (pp);
2656
2657 TYPE_VPTR_BASETYPE (type) = t;
2658 if (type == t)
2659 {
2660 if (TYPE_FIELD_NAME (t, TYPE_N_BASECLASSES (t)) == 0)
2661 {
2662 /* FIXME-tiemann: what's this? */
2663 #if 0
2664 TYPE_VPTR_FIELDNO (type) = i = TYPE_N_BASECLASSES (t);
2665 #else
2666 error_type (pp);
2667 #endif
2668 }
2669 else for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); --i)
2670 if (! strncmp (TYPE_FIELD_NAME (t, i), vptr_name,
2671 sizeof (vptr_name) -1))
2672 {
2673 TYPE_VPTR_FIELDNO (type) = i;
2674 break;
2675 }
2676 if (i < 0)
2677 /* Virtual function table field not found. */
2678 return error_type (pp);
2679 }
2680 else
2681 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
2682
2683 #if 0
2684 if (TYPE_VPTR_FIELDNO (type) != predicted_fieldno)
2685 error ("TYPE_VPTR_FIELDNO miscalculated");
2686 #endif
2687
2688 *pp = p + 1;
2689 }
2690 }
2691
2692 return type;
2693 }
2694
2695 /* Read a definition of an array type,
2696 and create and return a suitable type object.
2697 Also creates a range type which represents the bounds of that
2698 array. */
2699 static struct type *
2700 read_array_type (pp, type, objfile)
2701 register char **pp;
2702 register struct type *type;
2703 struct objfile *objfile;
2704 {
2705 struct type *index_type, *element_type, *range_type;
2706 int lower, upper;
2707 int adjustable = 0;
2708
2709 /* Format of an array type:
2710 "ar<index type>;lower;upper;<array_contents_type>". Put code in
2711 to handle this.
2712
2713 Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
2714 for these, produce a type like float[][]. */
2715
2716 index_type = read_type (pp, objfile);
2717 if (**pp != ';')
2718 /* Improper format of array type decl. */
2719 return error_type (pp);
2720 ++*pp;
2721
2722 if (!(**pp >= '0' && **pp <= '9'))
2723 {
2724 *pp += 1;
2725 adjustable = 1;
2726 }
2727 lower = read_number (pp, ';');
2728
2729 if (!(**pp >= '0' && **pp <= '9'))
2730 {
2731 *pp += 1;
2732 adjustable = 1;
2733 }
2734 upper = read_number (pp, ';');
2735
2736 element_type = read_type (pp, objfile);
2737
2738 if (adjustable)
2739 {
2740 lower = 0;
2741 upper = -1;
2742 }
2743
2744 {
2745 /* Create range type. */
2746 range_type = (struct type *)
2747 obstack_alloc (&objfile -> type_obstack, sizeof (struct type));
2748 bzero (range_type, sizeof (struct type));
2749 TYPE_OBJFILE (range_type) = objfile;
2750 TYPE_CODE (range_type) = TYPE_CODE_RANGE;
2751 TYPE_TARGET_TYPE (range_type) = index_type;
2752
2753 /* This should never be needed. */
2754 TYPE_LENGTH (range_type) = sizeof (int);
2755
2756 TYPE_NFIELDS (range_type) = 2;
2757 TYPE_FIELDS (range_type) =
2758 (struct field *) obstack_alloc (&objfile -> type_obstack,
2759 2 * sizeof (struct field));
2760 TYPE_FIELD_BITPOS (range_type, 0) = lower;
2761 TYPE_FIELD_BITPOS (range_type, 1) = upper;
2762 }
2763
2764 TYPE_CODE (type) = TYPE_CODE_ARRAY;
2765 TYPE_TARGET_TYPE (type) = element_type;
2766 TYPE_LENGTH (type) = (upper - lower + 1) * TYPE_LENGTH (element_type);
2767 TYPE_NFIELDS (type) = 1;
2768 TYPE_FIELDS (type) =
2769 (struct field *) obstack_alloc (&objfile -> type_obstack,
2770 sizeof (struct field));
2771 TYPE_FIELD_TYPE (type, 0) = range_type;
2772
2773 /* If we have an array whose element type is not yet known, but whose
2774 bounds *are* known, record it to be adjusted at the end of the file. */
2775 if (TYPE_LENGTH (element_type) == 0 && !adjustable)
2776 add_undefined_type (type);
2777
2778 return type;
2779 }
2780
2781
2782 /* Read a definition of an enumeration type,
2783 and create and return a suitable type object.
2784 Also defines the symbols that represent the values of the type. */
2785
2786 static struct type *
2787 read_enum_type (pp, type, objfile)
2788 register char **pp;
2789 register struct type *type;
2790 struct objfile *objfile;
2791 {
2792 register char *p;
2793 char *name;
2794 register long n;
2795 register struct symbol *sym;
2796 int nsyms = 0;
2797 struct pending **symlist;
2798 struct pending *osyms, *syms;
2799 int o_nsyms;
2800
2801 if (within_function)
2802 symlist = &local_symbols;
2803 else
2804 symlist = &file_symbols;
2805 osyms = *symlist;
2806 o_nsyms = osyms ? osyms->nsyms : 0;
2807
2808 /* Read the value-names and their values.
2809 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
2810 A semicolon or comma instead of a NAME means the end. */
2811 while (**pp && **pp != ';' && **pp != ',')
2812 {
2813 /* Check for and handle cretinous dbx symbol name continuation! */
2814 if (**pp == '\\') *pp = next_symbol_text ();
2815
2816 p = *pp;
2817 while (*p != ':') p++;
2818 name = obsavestring (*pp, p - *pp, &objfile -> symbol_obstack);
2819 *pp = p + 1;
2820 n = read_number (pp, ',');
2821
2822 sym = (struct symbol *) obstack_alloc (&objfile -> symbol_obstack, sizeof (struct symbol));
2823 bzero (sym, sizeof (struct symbol));
2824 SYMBOL_NAME (sym) = name;
2825 SYMBOL_CLASS (sym) = LOC_CONST;
2826 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2827 SYMBOL_VALUE (sym) = n;
2828 add_symbol_to_list (sym, symlist);
2829 nsyms++;
2830 }
2831
2832 if (**pp == ';')
2833 (*pp)++; /* Skip the semicolon. */
2834
2835 /* Now fill in the fields of the type-structure. */
2836
2837 TYPE_LENGTH (type) = sizeof (int);
2838 TYPE_CODE (type) = TYPE_CODE_ENUM;
2839 TYPE_NFIELDS (type) = nsyms;
2840 TYPE_FIELDS (type) = (struct field *)
2841 obstack_alloc (&objfile -> type_obstack,
2842 sizeof (struct field) * nsyms);
2843
2844 /* Find the symbols for the values and put them into the type.
2845 The symbols can be found in the symlist that we put them on
2846 to cause them to be defined. osyms contains the old value
2847 of that symlist; everything up to there was defined by us. */
2848 /* Note that we preserve the order of the enum constants, so
2849 that in something like "enum {FOO, LAST_THING=FOO}" we print
2850 FOO, not LAST_THING. */
2851
2852 for (syms = *symlist, n = 0; syms; syms = syms->next)
2853 {
2854 int j = 0;
2855 if (syms == osyms)
2856 j = o_nsyms;
2857 for (; j < syms->nsyms; j++,n++)
2858 {
2859 struct symbol *xsym = syms->symbol[j];
2860 SYMBOL_TYPE (xsym) = type;
2861 TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
2862 TYPE_FIELD_VALUE (type, n) = 0;
2863 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
2864 TYPE_FIELD_BITSIZE (type, n) = 0;
2865 }
2866 if (syms == osyms)
2867 break;
2868 }
2869
2870 #if 0
2871 /* This screws up perfectly good C programs with enums. FIXME. */
2872 /* Is this Modula-2's BOOLEAN type? Flag it as such if so. */
2873 if(TYPE_NFIELDS(type) == 2 &&
2874 ((!strcmp(TYPE_FIELD_NAME(type,0),"TRUE") &&
2875 !strcmp(TYPE_FIELD_NAME(type,1),"FALSE")) ||
2876 (!strcmp(TYPE_FIELD_NAME(type,1),"TRUE") &&
2877 !strcmp(TYPE_FIELD_NAME(type,0),"FALSE"))))
2878 TYPE_CODE(type) = TYPE_CODE_BOOL;
2879 #endif
2880
2881 return type;
2882 }
2883
2884 /* Read a number from the string pointed to by *PP.
2885 The value of *PP is advanced over the number.
2886 If END is nonzero, the character that ends the
2887 number must match END, or an error happens;
2888 and that character is skipped if it does match.
2889 If END is zero, *PP is left pointing to that character.
2890
2891 If the number fits in a long, set *VALUE and set *BITS to 0.
2892 If not, set *BITS to be the number of bits in the number.
2893
2894 If encounter garbage, set *BITS to -1. */
2895
2896 static void
2897 read_huge_number (pp, end, valu, bits)
2898 char **pp;
2899 int end;
2900 long *valu;
2901 int *bits;
2902 {
2903 char *p = *pp;
2904 int sign = 1;
2905 long n = 0;
2906 int radix = 10;
2907 char overflow = 0;
2908 int nbits = 0;
2909 int c;
2910 long upper_limit;
2911
2912 if (*p == '-')
2913 {
2914 sign = -1;
2915 p++;
2916 }
2917
2918 /* Leading zero means octal. GCC uses this to output values larger
2919 than an int (because that would be hard in decimal). */
2920 if (*p == '0')
2921 {
2922 radix = 8;
2923 p++;
2924 }
2925
2926 upper_limit = LONG_MAX / radix;
2927 while ((c = *p++) >= '0' && c <= ('0' + radix))
2928 {
2929 if (n <= upper_limit)
2930 {
2931 n *= radix;
2932 n += c - '0'; /* FIXME this overflows anyway */
2933 }
2934 else
2935 overflow = 1;
2936
2937 /* This depends on large values being output in octal, which is
2938 what GCC does. */
2939 if (radix == 8)
2940 {
2941 if (nbits == 0)
2942 {
2943 if (c == '0')
2944 /* Ignore leading zeroes. */
2945 ;
2946 else if (c == '1')
2947 nbits = 1;
2948 else if (c == '2' || c == '3')
2949 nbits = 2;
2950 else
2951 nbits = 3;
2952 }
2953 else
2954 nbits += 3;
2955 }
2956 }
2957 if (end)
2958 {
2959 if (c && c != end)
2960 {
2961 if (bits != NULL)
2962 *bits = -1;
2963 return;
2964 }
2965 }
2966 else
2967 --p;
2968
2969 *pp = p;
2970 if (overflow)
2971 {
2972 if (nbits == 0)
2973 {
2974 /* Large decimal constants are an error (because it is hard to
2975 count how many bits are in them). */
2976 if (bits != NULL)
2977 *bits = -1;
2978 return;
2979 }
2980
2981 /* -0x7f is the same as 0x80. So deal with it by adding one to
2982 the number of bits. */
2983 if (sign == -1)
2984 ++nbits;
2985 if (bits)
2986 *bits = nbits;
2987 }
2988 else
2989 {
2990 if (valu)
2991 *valu = n * sign;
2992 if (bits)
2993 *bits = 0;
2994 }
2995 }
2996
2997 #define MAX_OF_C_TYPE(t) ((1 << (sizeof (t)*8 - 1)) - 1)
2998 #define MIN_OF_C_TYPE(t) (-(1 << (sizeof (t)*8 - 1)))
2999
3000 static struct type *
3001 read_range_type (pp, typenums, objfile)
3002 char **pp;
3003 int typenums[2];
3004 struct objfile *objfile;
3005 {
3006 int rangenums[2];
3007 long n2, n3;
3008 int n2bits, n3bits;
3009 int self_subrange;
3010 struct type *result_type;
3011
3012 /* First comes a type we are a subrange of.
3013 In C it is usually 0, 1 or the type being defined. */
3014 read_type_number (pp, rangenums);
3015 self_subrange = (rangenums[0] == typenums[0] &&
3016 rangenums[1] == typenums[1]);
3017
3018 /* A semicolon should now follow; skip it. */
3019 if (**pp == ';')
3020 (*pp)++;
3021
3022 /* The remaining two operands are usually lower and upper bounds
3023 of the range. But in some special cases they mean something else. */
3024 read_huge_number (pp, ';', &n2, &n2bits);
3025 read_huge_number (pp, ';', &n3, &n3bits);
3026
3027 if (n2bits == -1 || n3bits == -1)
3028 return error_type (pp);
3029
3030 /* If limits are huge, must be large integral type. */
3031 if (n2bits != 0 || n3bits != 0)
3032 {
3033 char got_signed = 0;
3034 char got_unsigned = 0;
3035 /* Number of bits in the type. */
3036 int nbits;
3037
3038 /* Range from 0 to <large number> is an unsigned large integral type. */
3039 if ((n2bits == 0 && n2 == 0) && n3bits != 0)
3040 {
3041 got_unsigned = 1;
3042 nbits = n3bits;
3043 }
3044 /* Range from <large number> to <large number>-1 is a large signed
3045 integral type. */
3046 else if (n2bits != 0 && n3bits != 0 && n2bits == n3bits + 1)
3047 {
3048 got_signed = 1;
3049 nbits = n2bits;
3050 }
3051
3052 /* Check for "long long". */
3053 if (got_signed && nbits == TARGET_LONG_LONG_BIT)
3054 return (lookup_fundamental_type (objfile, FT_LONG_LONG));
3055 if (got_unsigned && nbits == TARGET_LONG_LONG_BIT)
3056 return (lookup_fundamental_type (objfile, FT_UNSIGNED_LONG_LONG));
3057
3058 if (got_signed || got_unsigned)
3059 {
3060 result_type = (struct type *)
3061 obstack_alloc (&objfile -> type_obstack,
3062 sizeof (struct type));
3063 bzero (result_type, sizeof (struct type));
3064 TYPE_OBJFILE (result_type) = objfile;
3065 TYPE_LENGTH (result_type) = nbits / TARGET_CHAR_BIT;
3066 TYPE_CODE (result_type) = TYPE_CODE_INT;
3067 if (got_unsigned)
3068 TYPE_FLAGS (result_type) |= TYPE_FLAG_UNSIGNED;
3069 return result_type;
3070 }
3071 else
3072 return error_type (pp);
3073 }
3074
3075 /* A type defined as a subrange of itself, with bounds both 0, is void. */
3076 if (self_subrange && n2 == 0 && n3 == 0)
3077 return (lookup_fundamental_type (objfile, FT_VOID));
3078
3079 /* If n3 is zero and n2 is not, we want a floating type,
3080 and n2 is the width in bytes.
3081
3082 Fortran programs appear to use this for complex types also,
3083 and they give no way to distinguish between double and single-complex!
3084 We don't have complex types, so we would lose on all fortran files!
3085 So return type `double' for all of those. It won't work right
3086 for the complex values, but at least it makes the file loadable.
3087
3088 FIXME, we may be able to distinguish these by their names. FIXME. */
3089
3090 if (n3 == 0 && n2 > 0)
3091 {
3092 if (n2 == sizeof (float))
3093 return (lookup_fundamental_type (objfile, FT_FLOAT));
3094 return (lookup_fundamental_type (objfile, FT_DBL_PREC_FLOAT));
3095 }
3096
3097 /* If the upper bound is -1, it must really be an unsigned int. */
3098
3099 else if (n2 == 0 && n3 == -1)
3100 {
3101 /* FIXME -- the only way to distinguish `unsigned int' from `unsigned
3102 long' is to look at its name! */
3103 if (
3104 long_kludge_name && ((long_kludge_name[0] == 'u' /* unsigned */ &&
3105 long_kludge_name[9] == 'l' /* long */)
3106 || (long_kludge_name[0] == 'l' /* long unsigned */)))
3107 return (lookup_fundamental_type (objfile, FT_UNSIGNED_LONG));
3108 else
3109 return (lookup_fundamental_type (objfile, FT_UNSIGNED_INTEGER));
3110 }
3111
3112 /* Special case: char is defined (Who knows why) as a subrange of
3113 itself with range 0-127. */
3114 else if (self_subrange && n2 == 0 && n3 == 127)
3115 return (lookup_fundamental_type (objfile, FT_CHAR));
3116
3117 /* Assumptions made here: Subrange of self is equivalent to subrange
3118 of int. FIXME: Host and target type-sizes assumed the same. */
3119 /* FIXME: This is the *only* place in GDB that depends on comparing
3120 some type to a builtin type with ==. Fix it! */
3121 else if (n2 == 0
3122 && (self_subrange ||
3123 *dbx_lookup_type (rangenums) == lookup_fundamental_type (objfile, FT_INTEGER)))
3124 {
3125 /* an unsigned type */
3126 #ifdef LONG_LONG
3127 if (n3 == - sizeof (long long))
3128 return (lookup_fundamental_type (objfile, FT_UNSIGNED_LONG_LONG));
3129 #endif
3130 /* FIXME -- the only way to distinguish `unsigned int' from `unsigned
3131 long' is to look at its name! */
3132 if (n3 == (unsigned long)~0L &&
3133 long_kludge_name && ((long_kludge_name[0] == 'u' /* unsigned */ &&
3134 long_kludge_name[9] == 'l' /* long */)
3135 || (long_kludge_name[0] == 'l' /* long unsigned */)))
3136 return (lookup_fundamental_type (objfile, FT_UNSIGNED_LONG));
3137 if (n3 == (unsigned int)~0L)
3138 return (lookup_fundamental_type (objfile, FT_UNSIGNED_INTEGER));
3139 if (n3 == (unsigned short)~0L)
3140 return (lookup_fundamental_type (objfile, FT_UNSIGNED_SHORT));
3141 if (n3 == (unsigned char)~0L)
3142 return (lookup_fundamental_type (objfile, FT_UNSIGNED_CHAR));
3143 }
3144 #ifdef LONG_LONG
3145 else if (n3 == 0 && n2 == -sizeof (long long))
3146 return (lookup_fundamental_type (objfile, FT_LONG_LONG));
3147 #endif
3148 else if (n2 == -n3 -1)
3149 {
3150 /* a signed type */
3151 /* FIXME -- the only way to distinguish `int' from `long' is to look
3152 at its name! */
3153 if ((n3 == (1 << (8 * sizeof (long) - 1)) - 1) &&
3154 long_kludge_name && long_kludge_name[0] == 'l' /* long */)
3155 return (lookup_fundamental_type (objfile, FT_LONG));
3156 if (n3 == (1 << (8 * sizeof (int) - 1)) - 1)
3157 return (lookup_fundamental_type (objfile, FT_INTEGER));
3158 if (n3 == (1 << (8 * sizeof (short) - 1)) - 1)
3159 return (lookup_fundamental_type (objfile, FT_SHORT));
3160 if (n3 == (1 << (8 * sizeof (char) - 1)) - 1)
3161 return (lookup_fundamental_type (objfile, FT_CHAR));
3162 }
3163
3164 /* We have a real range type on our hands. Allocate space and
3165 return a real pointer. */
3166
3167 /* At this point I don't have the faintest idea how to deal with
3168 a self_subrange type; I'm going to assume that this is used
3169 as an idiom, and that all of them are special cases. So . . . */
3170 if (self_subrange)
3171 return error_type (pp);
3172
3173 result_type = (struct type *)
3174 obstack_alloc (&objfile -> type_obstack, sizeof (struct type));
3175 bzero (result_type, sizeof (struct type));
3176 TYPE_OBJFILE (result_type) = objfile;
3177
3178 TYPE_CODE (result_type) = TYPE_CODE_RANGE;
3179
3180 TYPE_TARGET_TYPE (result_type) = *dbx_lookup_type(rangenums);
3181 if (TYPE_TARGET_TYPE (result_type) == 0) {
3182 complain (&range_type_base_complaint, (char *) rangenums[1]);
3183 TYPE_TARGET_TYPE (result_type) = lookup_fundamental_type (objfile, FT_INTEGER);
3184 }
3185
3186 TYPE_NFIELDS (result_type) = 2;
3187 TYPE_FIELDS (result_type) =
3188 (struct field *) obstack_alloc (&objfile -> type_obstack,
3189 2 * sizeof (struct field));
3190 bzero (TYPE_FIELDS (result_type), 2 * sizeof (struct field));
3191 TYPE_FIELD_BITPOS (result_type, 0) = n2;
3192 TYPE_FIELD_BITPOS (result_type, 1) = n3;
3193
3194 TYPE_LENGTH (result_type) = TYPE_LENGTH (TYPE_TARGET_TYPE (result_type));
3195
3196 return result_type;
3197 }
3198
3199 /* Read a number from the string pointed to by *PP.
3200 The value of *PP is advanced over the number.
3201 If END is nonzero, the character that ends the
3202 number must match END, or an error happens;
3203 and that character is skipped if it does match.
3204 If END is zero, *PP is left pointing to that character. */
3205
3206 long
3207 read_number (pp, end)
3208 char **pp;
3209 int end;
3210 {
3211 register char *p = *pp;
3212 register long n = 0;
3213 register int c;
3214 int sign = 1;
3215
3216 /* Handle an optional leading minus sign. */
3217
3218 if (*p == '-')
3219 {
3220 sign = -1;
3221 p++;
3222 }
3223
3224 /* Read the digits, as far as they go. */
3225
3226 while ((c = *p++) >= '0' && c <= '9')
3227 {
3228 n *= 10;
3229 n += c - '0';
3230 }
3231 if (end)
3232 {
3233 if (c && c != end)
3234 error ("Invalid symbol data: invalid character \\%03o at symbol pos %d.", c, symnum);
3235 }
3236 else
3237 --p;
3238
3239 *pp = p;
3240 return n * sign;
3241 }
3242
3243 /* Read in an argument list. This is a list of types, separated by commas
3244 and terminated with END. Return the list of types read in, or (struct type
3245 **)-1 if there is an error. */
3246 static struct type **
3247 read_args (pp, end, objfile)
3248 char **pp;
3249 int end;
3250 struct objfile *objfile;
3251 {
3252 /* FIXME! Remove this arbitrary limit! */
3253 struct type *types[1024], **rval; /* allow for fns of 1023 parameters */
3254 int n = 0;
3255
3256 while (**pp != end)
3257 {
3258 if (**pp != ',')
3259 /* Invalid argument list: no ','. */
3260 return (struct type **)-1;
3261 *pp += 1;
3262
3263 /* Check for and handle cretinous dbx symbol name continuation! */
3264 if (**pp == '\\')
3265 *pp = next_symbol_text ();
3266
3267 types[n++] = read_type (pp, objfile);
3268 }
3269 *pp += 1; /* get past `end' (the ':' character) */
3270
3271 if (n == 1)
3272 {
3273 rval = (struct type **) xmalloc (2 * sizeof (struct type *));
3274 }
3275 else if (TYPE_CODE (types[n-1]) != TYPE_CODE_VOID)
3276 {
3277 rval = (struct type **) xmalloc ((n + 1) * sizeof (struct type *));
3278 bzero (rval + n, sizeof (struct type *));
3279 }
3280 else
3281 {
3282 rval = (struct type **) xmalloc (n * sizeof (struct type *));
3283 }
3284 memcpy (rval, types, n * sizeof (struct type *));
3285 return rval;
3286 }
3287
3288 /* Add a common block's start address to the offset of each symbol
3289 declared to be in it (by being between a BCOMM/ECOMM pair that uses
3290 the common block name). */
3291
3292 static void
3293 fix_common_block (sym, valu)
3294 struct symbol *sym;
3295 int valu;
3296 {
3297 struct pending *next = (struct pending *) SYMBOL_NAMESPACE (sym);
3298 for ( ; next; next = next->next)
3299 {
3300 register int j;
3301 for (j = next->nsyms - 1; j >= 0; j--)
3302 SYMBOL_VALUE_ADDRESS (next->symbol[j]) += valu;
3303 }
3304 }
3305
3306 /* Initializer for this module */
3307 void
3308 _initialize_buildsym ()
3309 {
3310 undef_types_allocated = 20;
3311 undef_types_length = 0;
3312 undef_types = (struct type **) xmalloc (undef_types_allocated *
3313 sizeof (struct type *));
3314 }