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