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