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