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