Remove path name from test case
[binutils-gdb.git] / gdb / stabsread.c
1 /* Support routines for decoding "stabs" debugging information format.
2
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* Support routines for reading and decoding debugging information in
21 the "stabs" format. This format is used by some systems that use
22 COFF or ELF where the stabs data is placed in a special section (as
23 well as with many old systems that used the a.out object file
24 format). Avoid placing any object file format specific code in
25 this file. */
26
27 #include "defs.h"
28 #include "bfd.h"
29 #include "gdbsupport/gdb_obstack.h"
30 #include "symtab.h"
31 #include "gdbtypes.h"
32 #include "expression.h"
33 #include "symfile.h"
34 #include "objfiles.h"
35 #include "aout/stab_gnu.h"
36 #include "libaout.h"
37 #include "aout/aout64.h"
38 #include "gdb-stabs.h"
39 #include "buildsym-legacy.h"
40 #include "complaints.h"
41 #include "demangle.h"
42 #include "gdb-demangle.h"
43 #include "language.h"
44 #include "target-float.h"
45 #include "c-lang.h"
46 #include "cp-abi.h"
47 #include "cp-support.h"
48 #include <ctype.h>
49
50 #include "stabsread.h"
51
52 /* See stabsread.h for these globals. */
53 unsigned int symnum;
54 const char *(*next_symbol_text_func) (struct objfile *);
55 unsigned char processing_gcc_compilation;
56 int within_function;
57 struct symbol *global_sym_chain[HASHSIZE];
58 struct pending_stabs *global_stabs;
59 int previous_stab_code;
60 int *this_object_header_files;
61 int n_this_object_header_files;
62 int n_allocated_this_object_header_files;
63
64 struct stabs_nextfield
65 {
66 struct stabs_nextfield *next;
67
68 /* This is the raw visibility from the stab. It is not checked
69 for being one of the visibilities we recognize, so code which
70 examines this field better be able to deal. */
71 int visibility;
72
73 struct field field;
74 };
75
76 struct next_fnfieldlist
77 {
78 struct next_fnfieldlist *next;
79 struct fn_fieldlist fn_fieldlist;
80 };
81
82 /* The routines that read and process a complete stabs for a C struct or
83 C++ class pass lists of data member fields and lists of member function
84 fields in an instance of a field_info structure, as defined below.
85 This is part of some reorganization of low level C++ support and is
86 expected to eventually go away... (FIXME) */
87
88 struct stab_field_info
89 {
90 struct stabs_nextfield *list = nullptr;
91 struct next_fnfieldlist *fnlist = nullptr;
92
93 auto_obstack obstack;
94 };
95
96 static void
97 read_one_struct_field (struct stab_field_info *, const char **, const char *,
98 struct type *, struct objfile *);
99
100 static struct type *dbx_alloc_type (int[2], struct objfile *);
101
102 static long read_huge_number (const char **, int, int *, int);
103
104 static struct type *error_type (const char **, struct objfile *);
105
106 static void
107 patch_block_stabs (struct pending *, struct pending_stabs *,
108 struct objfile *);
109
110 static int read_type_number (const char **, int *);
111
112 static struct type *read_type (const char **, struct objfile *);
113
114 static struct type *read_range_type (const char **, int[2],
115 int, struct objfile *);
116
117 static struct type *read_sun_builtin_type (const char **,
118 int[2], struct objfile *);
119
120 static struct type *read_sun_floating_type (const char **, int[2],
121 struct objfile *);
122
123 static struct type *read_enum_type (const char **, struct type *, struct objfile *);
124
125 static struct type *rs6000_builtin_type (int, struct objfile *);
126
127 static int
128 read_member_functions (struct stab_field_info *, const char **, struct type *,
129 struct objfile *);
130
131 static int
132 read_struct_fields (struct stab_field_info *, const char **, struct type *,
133 struct objfile *);
134
135 static int
136 read_baseclasses (struct stab_field_info *, const char **, struct type *,
137 struct objfile *);
138
139 static int
140 read_tilde_fields (struct stab_field_info *, const char **, struct type *,
141 struct objfile *);
142
143 static int attach_fn_fields_to_type (struct stab_field_info *, struct type *);
144
145 static int attach_fields_to_type (struct stab_field_info *, struct type *,
146 struct objfile *);
147
148 static struct type *read_struct_type (const char **, struct type *,
149 enum type_code,
150 struct objfile *);
151
152 static struct type *read_array_type (const char **, struct type *,
153 struct objfile *);
154
155 static struct field *read_args (const char **, int, struct objfile *,
156 int *, int *);
157
158 static void add_undefined_type (struct type *, int[2]);
159
160 static int
161 read_cpp_abbrev (struct stab_field_info *, const char **, struct type *,
162 struct objfile *);
163
164 static const char *find_name_end (const char *name);
165
166 static int process_reference (const char **string);
167
168 void stabsread_clear_cache (void);
169
170 static const char vptr_name[] = "_vptr$";
171 static const char vb_name[] = "_vb$";
172
173 static void
174 invalid_cpp_abbrev_complaint (const char *arg1)
175 {
176 complaint (_("invalid C++ abbreviation `%s'"), arg1);
177 }
178
179 static void
180 reg_value_complaint (int regnum, int num_regs, const char *sym)
181 {
182 complaint (_("bad register number %d (max %d) in symbol %s"),
183 regnum, num_regs - 1, sym);
184 }
185
186 static void
187 stabs_general_complaint (const char *arg1)
188 {
189 complaint ("%s", arg1);
190 }
191
192 /* Make a list of forward references which haven't been defined. */
193
194 static struct type **undef_types;
195 static int undef_types_allocated;
196 static int undef_types_length;
197 static struct symbol *current_symbol = NULL;
198
199 /* Make a list of nameless types that are undefined.
200 This happens when another type is referenced by its number
201 before this type is actually defined. For instance "t(0,1)=k(0,2)"
202 and type (0,2) is defined only later. */
203
204 struct nat
205 {
206 int typenums[2];
207 struct type *type;
208 };
209 static struct nat *noname_undefs;
210 static int noname_undefs_allocated;
211 static int noname_undefs_length;
212
213 /* Check for and handle cretinous stabs symbol name continuation! */
214 #define STABS_CONTINUE(pp,objfile) \
215 do { \
216 if (**(pp) == '\\' || (**(pp) == '?' && (*(pp))[1] == '\0')) \
217 *(pp) = next_symbol_text (objfile); \
218 } while (0)
219
220 /* Vector of types defined so far, indexed by their type numbers.
221 (In newer sun systems, dbx uses a pair of numbers in parens,
222 as in "(SUBFILENUM,NUMWITHINSUBFILE)".
223 Then these numbers must be translated through the type_translations
224 hash table to get the index into the type vector.) */
225
226 static struct type **type_vector;
227
228 /* Number of elements allocated for type_vector currently. */
229
230 static int type_vector_length;
231
232 /* Initial size of type vector. Is realloc'd larger if needed, and
233 realloc'd down to the size actually used, when completed. */
234
235 #define INITIAL_TYPE_VECTOR_LENGTH 160
236 \f
237
238 /* Look up a dbx type-number pair. Return the address of the slot
239 where the type for that number-pair is stored.
240 The number-pair is in TYPENUMS.
241
242 This can be used for finding the type associated with that pair
243 or for associating a new type with the pair. */
244
245 static struct type **
246 dbx_lookup_type (int typenums[2], struct objfile *objfile)
247 {
248 int filenum = typenums[0];
249 int index = typenums[1];
250 unsigned old_len;
251 int real_filenum;
252 struct header_file *f;
253 int f_orig_length;
254
255 if (filenum == -1) /* -1,-1 is for temporary types. */
256 return 0;
257
258 if (filenum < 0 || filenum >= n_this_object_header_files)
259 {
260 complaint (_("Invalid symbol data: type number "
261 "(%d,%d) out of range at symtab pos %d."),
262 filenum, index, symnum);
263 goto error_return;
264 }
265
266 if (filenum == 0)
267 {
268 if (index < 0)
269 {
270 /* Caller wants address of address of type. We think
271 that negative (rs6k builtin) types will never appear as
272 "lvalues", (nor should they), so we stuff the real type
273 pointer into a temp, and return its address. If referenced,
274 this will do the right thing. */
275 static struct type *temp_type;
276
277 temp_type = rs6000_builtin_type (index, objfile);
278 return &temp_type;
279 }
280
281 /* Type is defined outside of header files.
282 Find it in this object file's type vector. */
283 if (index >= type_vector_length)
284 {
285 old_len = type_vector_length;
286 if (old_len == 0)
287 {
288 type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
289 type_vector = XNEWVEC (struct type *, type_vector_length);
290 }
291 while (index >= type_vector_length)
292 {
293 type_vector_length *= 2;
294 }
295 type_vector = (struct type **)
296 xrealloc ((char *) type_vector,
297 (type_vector_length * sizeof (struct type *)));
298 memset (&type_vector[old_len], 0,
299 (type_vector_length - old_len) * sizeof (struct type *));
300 }
301 return (&type_vector[index]);
302 }
303 else
304 {
305 real_filenum = this_object_header_files[filenum];
306
307 if (real_filenum >= N_HEADER_FILES (objfile))
308 {
309 static struct type *temp_type;
310
311 warning (_("GDB internal error: bad real_filenum"));
312
313 error_return:
314 temp_type = builtin_type (objfile)->builtin_error;
315 return &temp_type;
316 }
317
318 f = HEADER_FILES (objfile) + real_filenum;
319
320 f_orig_length = f->length;
321 if (index >= f_orig_length)
322 {
323 while (index >= f->length)
324 {
325 f->length *= 2;
326 }
327 f->vector = (struct type **)
328 xrealloc ((char *) f->vector, f->length * sizeof (struct type *));
329 memset (&f->vector[f_orig_length], 0,
330 (f->length - f_orig_length) * sizeof (struct type *));
331 }
332 return (&f->vector[index]);
333 }
334 }
335
336 /* Make sure there is a type allocated for type numbers TYPENUMS
337 and return the type object.
338 This can create an empty (zeroed) type object.
339 TYPENUMS may be (-1, -1) to return a new type object that is not
340 put into the type vector, and so may not be referred to by number. */
341
342 static struct type *
343 dbx_alloc_type (int typenums[2], struct objfile *objfile)
344 {
345 struct type **type_addr;
346
347 if (typenums[0] == -1)
348 {
349 return type_allocator (objfile,
350 get_current_subfile ()->language).new_type ();
351 }
352
353 type_addr = dbx_lookup_type (typenums, objfile);
354
355 /* If we are referring to a type not known at all yet,
356 allocate an empty type for it.
357 We will fill it in later if we find out how. */
358 if (*type_addr == 0)
359 {
360 *type_addr = type_allocator (objfile,
361 get_current_subfile ()->language).new_type ();
362 }
363
364 return (*type_addr);
365 }
366
367 /* Allocate a floating-point type of size BITS. */
368
369 static struct type *
370 dbx_init_float_type (struct objfile *objfile, int bits)
371 {
372 struct gdbarch *gdbarch = objfile->arch ();
373 const struct floatformat **format;
374 struct type *type;
375
376 format = gdbarch_floatformat_for_type (gdbarch, NULL, bits);
377 type_allocator alloc (objfile, get_current_subfile ()->language);
378 if (format)
379 type = init_float_type (alloc, bits, NULL, format);
380 else
381 type = alloc.new_type (TYPE_CODE_ERROR, bits, NULL);
382
383 return type;
384 }
385
386 /* for all the stabs in a given stab vector, build appropriate types
387 and fix their symbols in given symbol vector. */
388
389 static void
390 patch_block_stabs (struct pending *symbols, struct pending_stabs *stabs,
391 struct objfile *objfile)
392 {
393 int ii;
394 char *name;
395 const char *pp;
396 struct symbol *sym;
397
398 if (stabs)
399 {
400 /* for all the stab entries, find their corresponding symbols and
401 patch their types! */
402
403 for (ii = 0; ii < stabs->count; ++ii)
404 {
405 name = stabs->stab[ii];
406 pp = (char *) strchr (name, ':');
407 gdb_assert (pp); /* Must find a ':' or game's over. */
408 while (pp[1] == ':')
409 {
410 pp += 2;
411 pp = (char *) strchr (pp, ':');
412 }
413 sym = find_symbol_in_list (symbols, name, pp - name);
414 if (!sym)
415 {
416 /* FIXME-maybe: it would be nice if we noticed whether
417 the variable was defined *anywhere*, not just whether
418 it is defined in this compilation unit. But neither
419 xlc or GCC seem to need such a definition, and until
420 we do psymtabs (so that the minimal symbols from all
421 compilation units are available now), I'm not sure
422 how to get the information. */
423
424 /* On xcoff, if a global is defined and never referenced,
425 ld will remove it from the executable. There is then
426 a N_GSYM stab for it, but no regular (C_EXT) symbol. */
427 sym = new (&objfile->objfile_obstack) symbol;
428 sym->set_domain (VAR_DOMAIN);
429 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
430 sym->set_linkage_name
431 (obstack_strndup (&objfile->objfile_obstack, name, pp - name));
432 pp += 2;
433 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
434 {
435 /* I don't think the linker does this with functions,
436 so as far as I know this is never executed.
437 But it doesn't hurt to check. */
438 sym->set_type
439 (lookup_function_type (read_type (&pp, objfile)));
440 }
441 else
442 {
443 sym->set_type (read_type (&pp, objfile));
444 }
445 add_symbol_to_list (sym, get_global_symbols ());
446 }
447 else
448 {
449 pp += 2;
450 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
451 {
452 sym->set_type
453 (lookup_function_type (read_type (&pp, objfile)));
454 }
455 else
456 {
457 sym->set_type (read_type (&pp, objfile));
458 }
459 }
460 }
461 }
462 }
463 \f
464
465 /* Read a number by which a type is referred to in dbx data,
466 or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
467 Just a single number N is equivalent to (0,N).
468 Return the two numbers by storing them in the vector TYPENUMS.
469 TYPENUMS will then be used as an argument to dbx_lookup_type.
470
471 Returns 0 for success, -1 for error. */
472
473 static int
474 read_type_number (const char **pp, int *typenums)
475 {
476 int nbits;
477
478 if (**pp == '(')
479 {
480 (*pp)++;
481 typenums[0] = read_huge_number (pp, ',', &nbits, 0);
482 if (nbits != 0)
483 return -1;
484 typenums[1] = read_huge_number (pp, ')', &nbits, 0);
485 if (nbits != 0)
486 return -1;
487 }
488 else
489 {
490 typenums[0] = 0;
491 typenums[1] = read_huge_number (pp, 0, &nbits, 0);
492 if (nbits != 0)
493 return -1;
494 }
495 return 0;
496 }
497 \f
498
499 #define VISIBILITY_PRIVATE '0' /* Stabs character for private field */
500 #define VISIBILITY_PROTECTED '1' /* Stabs character for protected fld */
501 #define VISIBILITY_PUBLIC '2' /* Stabs character for public field */
502 #define VISIBILITY_IGNORE '9' /* Optimized out or zero length */
503
504 /* Structure for storing pointers to reference definitions for fast lookup
505 during "process_later". */
506
507 struct ref_map
508 {
509 const char *stabs;
510 CORE_ADDR value;
511 struct symbol *sym;
512 };
513
514 #define MAX_CHUNK_REFS 100
515 #define REF_CHUNK_SIZE (MAX_CHUNK_REFS * sizeof (struct ref_map))
516 #define REF_MAP_SIZE(ref_chunk) ((ref_chunk) * REF_CHUNK_SIZE)
517
518 static struct ref_map *ref_map;
519
520 /* Ptr to free cell in chunk's linked list. */
521 static int ref_count = 0;
522
523 /* Number of chunks malloced. */
524 static int ref_chunk = 0;
525
526 /* This file maintains a cache of stabs aliases found in the symbol
527 table. If the symbol table changes, this cache must be cleared
528 or we are left holding onto data in invalid obstacks. */
529 void
530 stabsread_clear_cache (void)
531 {
532 ref_count = 0;
533 ref_chunk = 0;
534 }
535
536 /* Create array of pointers mapping refids to symbols and stab strings.
537 Add pointers to reference definition symbols and/or their values as we
538 find them, using their reference numbers as our index.
539 These will be used later when we resolve references. */
540 void
541 ref_add (int refnum, struct symbol *sym, const char *stabs, CORE_ADDR value)
542 {
543 if (ref_count == 0)
544 ref_chunk = 0;
545 if (refnum >= ref_count)
546 ref_count = refnum + 1;
547 if (ref_count > ref_chunk * MAX_CHUNK_REFS)
548 {
549 int new_slots = ref_count - ref_chunk * MAX_CHUNK_REFS;
550 int new_chunks = new_slots / MAX_CHUNK_REFS + 1;
551
552 ref_map = (struct ref_map *)
553 xrealloc (ref_map, REF_MAP_SIZE (ref_chunk + new_chunks));
554 memset (ref_map + ref_chunk * MAX_CHUNK_REFS, 0,
555 new_chunks * REF_CHUNK_SIZE);
556 ref_chunk += new_chunks;
557 }
558 ref_map[refnum].stabs = stabs;
559 ref_map[refnum].sym = sym;
560 ref_map[refnum].value = value;
561 }
562
563 /* Return defined sym for the reference REFNUM. */
564 struct symbol *
565 ref_search (int refnum)
566 {
567 if (refnum < 0 || refnum > ref_count)
568 return 0;
569 return ref_map[refnum].sym;
570 }
571
572 /* Parse a reference id in STRING and return the resulting
573 reference number. Move STRING beyond the reference id. */
574
575 static int
576 process_reference (const char **string)
577 {
578 const char *p;
579 int refnum = 0;
580
581 if (**string != '#')
582 return 0;
583
584 /* Advance beyond the initial '#'. */
585 p = *string + 1;
586
587 /* Read number as reference id. */
588 while (*p && isdigit (*p))
589 {
590 refnum = refnum * 10 + *p - '0';
591 p++;
592 }
593 *string = p;
594 return refnum;
595 }
596
597 /* If STRING defines a reference, store away a pointer to the reference
598 definition for later use. Return the reference number. */
599
600 int
601 symbol_reference_defined (const char **string)
602 {
603 const char *p = *string;
604 int refnum = 0;
605
606 refnum = process_reference (&p);
607
608 /* Defining symbols end in '='. */
609 if (*p == '=')
610 {
611 /* Symbol is being defined here. */
612 *string = p + 1;
613 return refnum;
614 }
615 else
616 {
617 /* Must be a reference. Either the symbol has already been defined,
618 or this is a forward reference to it. */
619 *string = p;
620 return -1;
621 }
622 }
623
624 static int
625 stab_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
626 {
627 int regno = gdbarch_stab_reg_to_regnum (gdbarch, sym->value_longest ());
628
629 if (regno < 0 || regno >= gdbarch_num_cooked_regs (gdbarch))
630 {
631 reg_value_complaint (regno, gdbarch_num_cooked_regs (gdbarch),
632 sym->print_name ());
633
634 regno = gdbarch_sp_regnum (gdbarch); /* Known safe, though useless. */
635 }
636
637 return regno;
638 }
639
640 static const struct symbol_register_ops stab_register_funcs = {
641 stab_reg_to_regnum
642 };
643
644 /* The "aclass" indices for computed symbols. */
645
646 static int stab_register_index;
647 static int stab_regparm_index;
648
649 struct symbol *
650 define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
651 struct objfile *objfile)
652 {
653 struct gdbarch *gdbarch = objfile->arch ();
654 struct symbol *sym;
655 const char *p = find_name_end (string);
656 int deftype;
657 int synonym = 0;
658 int i;
659
660 /* We would like to eliminate nameless symbols, but keep their types.
661 E.g. stab entry ":t10=*2" should produce a type 10, which is a pointer
662 to type 2, but, should not create a symbol to address that type. Since
663 the symbol will be nameless, there is no way any user can refer to it. */
664
665 int nameless;
666
667 /* Ignore syms with empty names. */
668 if (string[0] == 0)
669 return 0;
670
671 /* Ignore old-style symbols from cc -go. */
672 if (p == 0)
673 return 0;
674
675 while (p[1] == ':')
676 {
677 p += 2;
678 p = strchr (p, ':');
679 if (p == NULL)
680 {
681 complaint (
682 _("Bad stabs string '%s'"), string);
683 return NULL;
684 }
685 }
686
687 /* If a nameless stab entry, all we need is the type, not the symbol.
688 e.g. ":t10=*2" or a nameless enum like " :T16=ered:0,green:1,blue:2,;" */
689 nameless = (p == string || ((string[0] == ' ') && (string[1] == ':')));
690
691 current_symbol = sym = new (&objfile->objfile_obstack) symbol;
692
693 if (processing_gcc_compilation)
694 {
695 /* GCC 2.x puts the line number in desc. SunOS apparently puts in the
696 number of bytes occupied by a type or object, which we ignore. */
697 sym->set_line (desc);
698 }
699 else
700 {
701 sym->set_line (0); /* unknown */
702 }
703
704 sym->set_language (get_current_subfile ()->language,
705 &objfile->objfile_obstack);
706
707 if (is_cplus_marker (string[0]))
708 {
709 /* Special GNU C++ names. */
710 switch (string[1])
711 {
712 case 't':
713 sym->set_linkage_name ("this");
714 break;
715
716 case 'v': /* $vtbl_ptr_type */
717 goto normal;
718
719 case 'e':
720 sym->set_linkage_name ("eh_throw");
721 break;
722
723 case '_':
724 /* This was an anonymous type that was never fixed up. */
725 goto normal;
726
727 default:
728 complaint (_("Unknown C++ symbol name `%s'"),
729 string);
730 goto normal; /* Do *something* with it. */
731 }
732 }
733 else
734 {
735 normal:
736 gdb::unique_xmalloc_ptr<char> new_name;
737
738 if (sym->language () == language_cplus)
739 {
740 std::string name (string, p - string);
741 new_name = cp_canonicalize_string (name.c_str ());
742 }
743 else if (sym->language () == language_c)
744 {
745 std::string name (string, p - string);
746 new_name = c_canonicalize_name (name.c_str ());
747 }
748 if (new_name != nullptr)
749 sym->compute_and_set_names (new_name.get (), true, objfile->per_bfd);
750 else
751 sym->compute_and_set_names (gdb::string_view (string, p - string), true,
752 objfile->per_bfd);
753
754 if (sym->language () == language_cplus)
755 cp_scan_for_anonymous_namespaces (get_buildsym_compunit (), sym,
756 objfile);
757
758 }
759 p++;
760
761 /* Determine the type of name being defined. */
762 #if 0
763 /* Getting GDB to correctly skip the symbol on an undefined symbol
764 descriptor and not ever dump core is a very dodgy proposition if
765 we do things this way. I say the acorn RISC machine can just
766 fix their compiler. */
767 /* The Acorn RISC machine's compiler can put out locals that don't
768 start with "234=" or "(3,4)=", so assume anything other than the
769 deftypes we know how to handle is a local. */
770 if (!strchr ("cfFGpPrStTvVXCR", *p))
771 #else
772 if (isdigit (*p) || *p == '(' || *p == '-')
773 #endif
774 deftype = 'l';
775 else
776 deftype = *p++;
777
778 switch (deftype)
779 {
780 case 'c':
781 /* c is a special case, not followed by a type-number.
782 SYMBOL:c=iVALUE for an integer constant symbol.
783 SYMBOL:c=rVALUE for a floating constant symbol.
784 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
785 e.g. "b:c=e6,0" for "const b = blob1"
786 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
787 if (*p != '=')
788 {
789 sym->set_aclass_index (LOC_CONST);
790 sym->set_type (error_type (&p, objfile));
791 sym->set_domain (VAR_DOMAIN);
792 add_symbol_to_list (sym, get_file_symbols ());
793 return sym;
794 }
795 ++p;
796 switch (*p++)
797 {
798 case 'r':
799 {
800 gdb_byte *dbl_valu;
801 struct type *dbl_type;
802
803 dbl_type = builtin_type (objfile)->builtin_double;
804 dbl_valu
805 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
806 dbl_type->length ());
807
808 target_float_from_string (dbl_valu, dbl_type, std::string (p));
809
810 sym->set_type (dbl_type);
811 sym->set_value_bytes (dbl_valu);
812 sym->set_aclass_index (LOC_CONST_BYTES);
813 }
814 break;
815 case 'i':
816 {
817 /* Defining integer constants this way is kind of silly,
818 since 'e' constants allows the compiler to give not
819 only the value, but the type as well. C has at least
820 int, long, unsigned int, and long long as constant
821 types; other languages probably should have at least
822 unsigned as well as signed constants. */
823
824 sym->set_type (builtin_type (objfile)->builtin_long);
825 sym->set_value_longest (atoi (p));
826 sym->set_aclass_index (LOC_CONST);
827 }
828 break;
829
830 case 'c':
831 {
832 sym->set_type (builtin_type (objfile)->builtin_char);
833 sym->set_value_longest (atoi (p));
834 sym->set_aclass_index (LOC_CONST);
835 }
836 break;
837
838 case 's':
839 {
840 struct type *range_type;
841 int ind = 0;
842 char quote = *p++;
843 gdb_byte *string_local = (gdb_byte *) alloca (strlen (p));
844 gdb_byte *string_value;
845
846 if (quote != '\'' && quote != '"')
847 {
848 sym->set_aclass_index (LOC_CONST);
849 sym->set_type (error_type (&p, objfile));
850 sym->set_domain (VAR_DOMAIN);
851 add_symbol_to_list (sym, get_file_symbols ());
852 return sym;
853 }
854
855 /* Find matching quote, rejecting escaped quotes. */
856 while (*p && *p != quote)
857 {
858 if (*p == '\\' && p[1] == quote)
859 {
860 string_local[ind] = (gdb_byte) quote;
861 ind++;
862 p += 2;
863 }
864 else if (*p)
865 {
866 string_local[ind] = (gdb_byte) (*p);
867 ind++;
868 p++;
869 }
870 }
871 if (*p != quote)
872 {
873 sym->set_aclass_index (LOC_CONST);
874 sym->set_type (error_type (&p, objfile));
875 sym->set_domain (VAR_DOMAIN);
876 add_symbol_to_list (sym, get_file_symbols ());
877 return sym;
878 }
879
880 /* NULL terminate the string. */
881 string_local[ind] = 0;
882 type_allocator alloc (objfile, get_current_subfile ()->language);
883 range_type
884 = create_static_range_type (alloc,
885 builtin_type (objfile)->builtin_int,
886 0, ind);
887 sym->set_type
888 (create_array_type (alloc, builtin_type (objfile)->builtin_char,
889 range_type));
890 string_value
891 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, ind + 1);
892 memcpy (string_value, string_local, ind + 1);
893 p++;
894
895 sym->set_value_bytes (string_value);
896 sym->set_aclass_index (LOC_CONST_BYTES);
897 }
898 break;
899
900 case 'e':
901 /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
902 can be represented as integral.
903 e.g. "b:c=e6,0" for "const b = blob1"
904 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
905 {
906 sym->set_aclass_index (LOC_CONST);
907 sym->set_type (read_type (&p, objfile));
908
909 if (*p != ',')
910 {
911 sym->set_type (error_type (&p, objfile));
912 break;
913 }
914 ++p;
915
916 /* If the value is too big to fit in an int (perhaps because
917 it is unsigned), or something like that, we silently get
918 a bogus value. The type and everything else about it is
919 correct. Ideally, we should be using whatever we have
920 available for parsing unsigned and long long values,
921 however. */
922 sym->set_value_longest (atoi (p));
923 }
924 break;
925 default:
926 {
927 sym->set_aclass_index (LOC_CONST);
928 sym->set_type (error_type (&p, objfile));
929 }
930 }
931 sym->set_domain (VAR_DOMAIN);
932 add_symbol_to_list (sym, get_file_symbols ());
933 return sym;
934
935 case 'C':
936 /* The name of a caught exception. */
937 sym->set_type (read_type (&p, objfile));
938 sym->set_aclass_index (LOC_LABEL);
939 sym->set_domain (VAR_DOMAIN);
940 sym->set_value_address (valu);
941 add_symbol_to_list (sym, get_local_symbols ());
942 break;
943
944 case 'f':
945 /* A static function definition. */
946 sym->set_type (read_type (&p, objfile));
947 sym->set_aclass_index (LOC_BLOCK);
948 sym->set_domain (VAR_DOMAIN);
949 add_symbol_to_list (sym, get_file_symbols ());
950 /* fall into process_function_types. */
951
952 process_function_types:
953 /* Function result types are described as the result type in stabs.
954 We need to convert this to the function-returning-type-X type
955 in GDB. E.g. "int" is converted to "function returning int". */
956 if (sym->type ()->code () != TYPE_CODE_FUNC)
957 sym->set_type (lookup_function_type (sym->type ()));
958
959 /* All functions in C++ have prototypes. Stabs does not offer an
960 explicit way to identify prototyped or unprototyped functions,
961 but both GCC and Sun CC emit stabs for the "call-as" type rather
962 than the "declared-as" type for unprototyped functions, so
963 we treat all functions as if they were prototyped. This is used
964 primarily for promotion when calling the function from GDB. */
965 sym->type ()->set_is_prototyped (true);
966
967 /* fall into process_prototype_types. */
968
969 process_prototype_types:
970 /* Sun acc puts declared types of arguments here. */
971 if (*p == ';')
972 {
973 struct type *ftype = sym->type ();
974 int nsemi = 0;
975 int nparams = 0;
976 const char *p1 = p;
977
978 /* Obtain a worst case guess for the number of arguments
979 by counting the semicolons. */
980 while (*p1)
981 {
982 if (*p1++ == ';')
983 nsemi++;
984 }
985
986 /* Allocate parameter information fields and fill them in. */
987 ftype->alloc_fields (nsemi);
988 while (*p++ == ';')
989 {
990 struct type *ptype;
991
992 /* A type number of zero indicates the start of varargs.
993 FIXME: GDB currently ignores vararg functions. */
994 if (p[0] == '0' && p[1] == '\0')
995 break;
996 ptype = read_type (&p, objfile);
997
998 /* The Sun compilers mark integer arguments, which should
999 be promoted to the width of the calling conventions, with
1000 a type which references itself. This type is turned into
1001 a TYPE_CODE_VOID type by read_type, and we have to turn
1002 it back into builtin_int here.
1003 FIXME: Do we need a new builtin_promoted_int_arg ? */
1004 if (ptype->code () == TYPE_CODE_VOID)
1005 ptype = builtin_type (objfile)->builtin_int;
1006 ftype->field (nparams).set_type (ptype);
1007 ftype->field (nparams).set_is_artificial (false);
1008 nparams++;
1009 }
1010 ftype->set_num_fields (nparams);
1011 ftype->set_is_prototyped (true);
1012 }
1013 break;
1014
1015 case 'F':
1016 /* A global function definition. */
1017 sym->set_type (read_type (&p, objfile));
1018 sym->set_aclass_index (LOC_BLOCK);
1019 sym->set_domain (VAR_DOMAIN);
1020 add_symbol_to_list (sym, get_global_symbols ());
1021 goto process_function_types;
1022
1023 case 'G':
1024 /* For a class G (global) symbol, it appears that the
1025 value is not correct. It is necessary to search for the
1026 corresponding linker definition to find the value.
1027 These definitions appear at the end of the namelist. */
1028 sym->set_type (read_type (&p, objfile));
1029 sym->set_aclass_index (LOC_STATIC);
1030 sym->set_domain (VAR_DOMAIN);
1031 /* Don't add symbol references to global_sym_chain.
1032 Symbol references don't have valid names and wont't match up with
1033 minimal symbols when the global_sym_chain is relocated.
1034 We'll fixup symbol references when we fixup the defining symbol. */
1035 if (sym->linkage_name () && sym->linkage_name ()[0] != '#')
1036 {
1037 i = hashname (sym->linkage_name ());
1038 sym->set_value_chain (global_sym_chain[i]);
1039 global_sym_chain[i] = sym;
1040 }
1041 add_symbol_to_list (sym, get_global_symbols ());
1042 break;
1043
1044 /* This case is faked by a conditional above,
1045 when there is no code letter in the dbx data.
1046 Dbx data never actually contains 'l'. */
1047 case 's':
1048 case 'l':
1049 sym->set_type (read_type (&p, objfile));
1050 sym->set_aclass_index (LOC_LOCAL);
1051 sym->set_value_longest (valu);
1052 sym->set_domain (VAR_DOMAIN);
1053 add_symbol_to_list (sym, get_local_symbols ());
1054 break;
1055
1056 case 'p':
1057 if (*p == 'F')
1058 /* pF is a two-letter code that means a function parameter in Fortran.
1059 The type-number specifies the type of the return value.
1060 Translate it into a pointer-to-function type. */
1061 {
1062 p++;
1063 sym->set_type
1064 (lookup_pointer_type
1065 (lookup_function_type (read_type (&p, objfile))));
1066 }
1067 else
1068 sym->set_type (read_type (&p, objfile));
1069
1070 sym->set_aclass_index (LOC_ARG);
1071 sym->set_value_longest (valu);
1072 sym->set_domain (VAR_DOMAIN);
1073 sym->set_is_argument (1);
1074 add_symbol_to_list (sym, get_local_symbols ());
1075
1076 if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
1077 {
1078 /* On little-endian machines, this crud is never necessary,
1079 and, if the extra bytes contain garbage, is harmful. */
1080 break;
1081 }
1082
1083 /* If it's gcc-compiled, if it says `short', believe it. */
1084 if (processing_gcc_compilation
1085 || gdbarch_believe_pcc_promotion (gdbarch))
1086 break;
1087
1088 if (!gdbarch_believe_pcc_promotion (gdbarch))
1089 {
1090 /* If PCC says a parameter is a short or a char, it is
1091 really an int. */
1092 if (sym->type ()->length ()
1093 < gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT
1094 && sym->type ()->code () == TYPE_CODE_INT)
1095 {
1096 sym->set_type
1097 (sym->type ()->is_unsigned ()
1098 ? builtin_type (objfile)->builtin_unsigned_int
1099 : builtin_type (objfile)->builtin_int);
1100 }
1101 break;
1102 }
1103 /* Fall through. */
1104
1105 case 'P':
1106 /* acc seems to use P to declare the prototypes of functions that
1107 are referenced by this file. gdb is not prepared to deal
1108 with this extra information. FIXME, it ought to. */
1109 if (type == N_FUN)
1110 {
1111 sym->set_type (read_type (&p, objfile));
1112 goto process_prototype_types;
1113 }
1114 /*FALLTHROUGH */
1115
1116 case 'R':
1117 /* Parameter which is in a register. */
1118 sym->set_type (read_type (&p, objfile));
1119 sym->set_aclass_index (stab_register_index);
1120 sym->set_is_argument (1);
1121 sym->set_value_longest (valu);
1122 sym->set_domain (VAR_DOMAIN);
1123 add_symbol_to_list (sym, get_local_symbols ());
1124 break;
1125
1126 case 'r':
1127 /* Register variable (either global or local). */
1128 sym->set_type (read_type (&p, objfile));
1129 sym->set_aclass_index (stab_register_index);
1130 sym->set_value_longest (valu);
1131 sym->set_domain (VAR_DOMAIN);
1132 if (within_function)
1133 {
1134 /* Sun cc uses a pair of symbols, one 'p' and one 'r', with
1135 the same name to represent an argument passed in a
1136 register. GCC uses 'P' for the same case. So if we find
1137 such a symbol pair we combine it into one 'P' symbol.
1138 For Sun cc we need to do this regardless of stabs_argument_has_addr, because the compiler puts out
1139 the 'p' symbol even if it never saves the argument onto
1140 the stack.
1141
1142 On most machines, we want to preserve both symbols, so
1143 that we can still get information about what is going on
1144 with the stack (VAX for computing args_printed, using
1145 stack slots instead of saved registers in backtraces,
1146 etc.).
1147
1148 Note that this code illegally combines
1149 main(argc) struct foo argc; { register struct foo argc; }
1150 but this case is considered pathological and causes a warning
1151 from a decent compiler. */
1152
1153 struct pending *local_symbols = *get_local_symbols ();
1154 if (local_symbols
1155 && local_symbols->nsyms > 0
1156 && gdbarch_stabs_argument_has_addr (gdbarch, sym->type ()))
1157 {
1158 struct symbol *prev_sym;
1159
1160 prev_sym = local_symbols->symbol[local_symbols->nsyms - 1];
1161 if ((prev_sym->aclass () == LOC_REF_ARG
1162 || prev_sym->aclass () == LOC_ARG)
1163 && strcmp (prev_sym->linkage_name (),
1164 sym->linkage_name ()) == 0)
1165 {
1166 prev_sym->set_aclass_index (stab_register_index);
1167 /* Use the type from the LOC_REGISTER; that is the type
1168 that is actually in that register. */
1169 prev_sym->set_type (sym->type ());
1170 prev_sym->set_value_longest (sym->value_longest ());
1171 sym = prev_sym;
1172 break;
1173 }
1174 }
1175 add_symbol_to_list (sym, get_local_symbols ());
1176 }
1177 else
1178 add_symbol_to_list (sym, get_file_symbols ());
1179 break;
1180
1181 case 'S':
1182 /* Static symbol at top level of file. */
1183 sym->set_type (read_type (&p, objfile));
1184 sym->set_aclass_index (LOC_STATIC);
1185 sym->set_value_address (valu);
1186 sym->set_domain (VAR_DOMAIN);
1187 add_symbol_to_list (sym, get_file_symbols ());
1188 break;
1189
1190 case 't':
1191 /* In Ada, there is no distinction between typedef and non-typedef;
1192 any type declaration implicitly has the equivalent of a typedef,
1193 and thus 't' is in fact equivalent to 'Tt'.
1194
1195 Therefore, for Ada units, we check the character immediately
1196 before the 't', and if we do not find a 'T', then make sure to
1197 create the associated symbol in the STRUCT_DOMAIN ('t' definitions
1198 will be stored in the VAR_DOMAIN). If the symbol was indeed
1199 defined as 'Tt' then the STRUCT_DOMAIN symbol will be created
1200 elsewhere, so we don't need to take care of that.
1201
1202 This is important to do, because of forward references:
1203 The cleanup of undefined types stored in undef_types only uses
1204 STRUCT_DOMAIN symbols to perform the replacement. */
1205 synonym = (sym->language () == language_ada && p[-2] != 'T');
1206
1207 /* Typedef */
1208 sym->set_type (read_type (&p, objfile));
1209
1210 /* For a nameless type, we don't want a create a symbol, thus we
1211 did not use `sym'. Return without further processing. */
1212 if (nameless)
1213 return NULL;
1214
1215 sym->set_aclass_index (LOC_TYPEDEF);
1216 sym->set_value_longest (valu);
1217 sym->set_domain (VAR_DOMAIN);
1218 /* C++ vagaries: we may have a type which is derived from
1219 a base type which did not have its name defined when the
1220 derived class was output. We fill in the derived class's
1221 base part member's name here in that case. */
1222 if (sym->type ()->name () != NULL)
1223 if ((sym->type ()->code () == TYPE_CODE_STRUCT
1224 || sym->type ()->code () == TYPE_CODE_UNION)
1225 && TYPE_N_BASECLASSES (sym->type ()))
1226 {
1227 int j;
1228
1229 for (j = TYPE_N_BASECLASSES (sym->type ()) - 1; j >= 0; j--)
1230 if (TYPE_BASECLASS_NAME (sym->type (), j) == 0)
1231 sym->type ()->field (j).set_name
1232 (TYPE_BASECLASS (sym->type (), j)->name ());
1233 }
1234
1235 if (sym->type ()->name () == NULL)
1236 {
1237 if ((sym->type ()->code () == TYPE_CODE_PTR
1238 && strcmp (sym->linkage_name (), vtbl_ptr_name))
1239 || sym->type ()->code () == TYPE_CODE_FUNC)
1240 {
1241 /* If we are giving a name to a type such as "pointer to
1242 foo" or "function returning foo", we better not set
1243 the TYPE_NAME. If the program contains "typedef char
1244 *caddr_t;", we don't want all variables of type char
1245 * to print as caddr_t. This is not just a
1246 consequence of GDB's type management; PCC and GCC (at
1247 least through version 2.4) both output variables of
1248 either type char * or caddr_t with the type number
1249 defined in the 't' symbol for caddr_t. If a future
1250 compiler cleans this up it GDB is not ready for it
1251 yet, but if it becomes ready we somehow need to
1252 disable this check (without breaking the PCC/GCC2.4
1253 case).
1254
1255 Sigh.
1256
1257 Fortunately, this check seems not to be necessary
1258 for anything except pointers or functions. */
1259 /* ezannoni: 2000-10-26. This seems to apply for
1260 versions of gcc older than 2.8. This was the original
1261 problem: with the following code gdb would tell that
1262 the type for name1 is caddr_t, and func is char().
1263
1264 typedef char *caddr_t;
1265 char *name2;
1266 struct x
1267 {
1268 char *name1;
1269 } xx;
1270 char *func()
1271 {
1272 }
1273 main () {}
1274 */
1275
1276 /* Pascal accepts names for pointer types. */
1277 if (get_current_subfile ()->language == language_pascal)
1278 sym->type ()->set_name (sym->linkage_name ());
1279 }
1280 else
1281 sym->type ()->set_name (sym->linkage_name ());
1282 }
1283
1284 add_symbol_to_list (sym, get_file_symbols ());
1285
1286 if (synonym)
1287 {
1288 /* Create the STRUCT_DOMAIN clone. */
1289 struct symbol *struct_sym = new (&objfile->objfile_obstack) symbol;
1290
1291 *struct_sym = *sym;
1292 struct_sym->set_aclass_index (LOC_TYPEDEF);
1293 struct_sym->set_value_longest (valu);
1294 struct_sym->set_domain (STRUCT_DOMAIN);
1295 if (sym->type ()->name () == 0)
1296 sym->type ()->set_name
1297 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
1298 (char *) NULL));
1299 add_symbol_to_list (struct_sym, get_file_symbols ());
1300 }
1301
1302 break;
1303
1304 case 'T':
1305 /* Struct, union, or enum tag. For GNU C++, this can be be followed
1306 by 't' which means we are typedef'ing it as well. */
1307 synonym = *p == 't';
1308
1309 if (synonym)
1310 p++;
1311
1312 sym->set_type (read_type (&p, objfile));
1313
1314 /* For a nameless type, we don't want a create a symbol, thus we
1315 did not use `sym'. Return without further processing. */
1316 if (nameless)
1317 return NULL;
1318
1319 sym->set_aclass_index (LOC_TYPEDEF);
1320 sym->set_value_longest (valu);
1321 sym->set_domain (STRUCT_DOMAIN);
1322 if (sym->type ()->name () == 0)
1323 sym->type ()->set_name
1324 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
1325 (char *) NULL));
1326 add_symbol_to_list (sym, get_file_symbols ());
1327
1328 if (synonym)
1329 {
1330 /* Clone the sym and then modify it. */
1331 struct symbol *typedef_sym = new (&objfile->objfile_obstack) symbol;
1332
1333 *typedef_sym = *sym;
1334 typedef_sym->set_aclass_index (LOC_TYPEDEF);
1335 typedef_sym->set_value_longest (valu);
1336 typedef_sym->set_domain (VAR_DOMAIN);
1337 if (sym->type ()->name () == 0)
1338 sym->type ()->set_name
1339 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
1340 (char *) NULL));
1341 add_symbol_to_list (typedef_sym, get_file_symbols ());
1342 }
1343 break;
1344
1345 case 'V':
1346 /* Static symbol of local scope. */
1347 sym->set_type (read_type (&p, objfile));
1348 sym->set_aclass_index (LOC_STATIC);
1349 sym->set_value_address (valu);
1350 sym->set_domain (VAR_DOMAIN);
1351 add_symbol_to_list (sym, get_local_symbols ());
1352 break;
1353
1354 case 'v':
1355 /* Reference parameter */
1356 sym->set_type (read_type (&p, objfile));
1357 sym->set_aclass_index (LOC_REF_ARG);
1358 sym->set_is_argument (1);
1359 sym->set_value_longest (valu);
1360 sym->set_domain (VAR_DOMAIN);
1361 add_symbol_to_list (sym, get_local_symbols ());
1362 break;
1363
1364 case 'a':
1365 /* Reference parameter which is in a register. */
1366 sym->set_type (read_type (&p, objfile));
1367 sym->set_aclass_index (stab_regparm_index);
1368 sym->set_is_argument (1);
1369 sym->set_value_longest (valu);
1370 sym->set_domain (VAR_DOMAIN);
1371 add_symbol_to_list (sym, get_local_symbols ());
1372 break;
1373
1374 case 'X':
1375 /* This is used by Sun FORTRAN for "function result value".
1376 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1377 that Pascal uses it too, but when I tried it Pascal used
1378 "x:3" (local symbol) instead. */
1379 sym->set_type (read_type (&p, objfile));
1380 sym->set_aclass_index (LOC_LOCAL);
1381 sym->set_value_longest (valu);
1382 sym->set_domain (VAR_DOMAIN);
1383 add_symbol_to_list (sym, get_local_symbols ());
1384 break;
1385
1386 default:
1387 sym->set_type (error_type (&p, objfile));
1388 sym->set_aclass_index (LOC_CONST);
1389 sym->set_value_longest (0);
1390 sym->set_domain (VAR_DOMAIN);
1391 add_symbol_to_list (sym, get_file_symbols ());
1392 break;
1393 }
1394
1395 /* Some systems pass variables of certain types by reference instead
1396 of by value, i.e. they will pass the address of a structure (in a
1397 register or on the stack) instead of the structure itself. */
1398
1399 if (gdbarch_stabs_argument_has_addr (gdbarch, sym->type ())
1400 && sym->is_argument ())
1401 {
1402 /* We have to convert LOC_REGISTER to LOC_REGPARM_ADDR (for
1403 variables passed in a register). */
1404 if (sym->aclass () == LOC_REGISTER)
1405 sym->set_aclass_index (LOC_REGPARM_ADDR);
1406 /* Likewise for converting LOC_ARG to LOC_REF_ARG (for the 7th
1407 and subsequent arguments on SPARC, for example). */
1408 else if (sym->aclass () == LOC_ARG)
1409 sym->set_aclass_index (LOC_REF_ARG);
1410 }
1411
1412 return sym;
1413 }
1414
1415 /* Skip rest of this symbol and return an error type.
1416
1417 General notes on error recovery: error_type always skips to the
1418 end of the symbol (modulo cretinous dbx symbol name continuation).
1419 Thus code like this:
1420
1421 if (*(*pp)++ != ';')
1422 return error_type (pp, objfile);
1423
1424 is wrong because if *pp starts out pointing at '\0' (typically as the
1425 result of an earlier error), it will be incremented to point to the
1426 start of the next symbol, which might produce strange results, at least
1427 if you run off the end of the string table. Instead use
1428
1429 if (**pp != ';')
1430 return error_type (pp, objfile);
1431 ++*pp;
1432
1433 or
1434
1435 if (**pp != ';')
1436 foo = error_type (pp, objfile);
1437 else
1438 ++*pp;
1439
1440 And in case it isn't obvious, the point of all this hair is so the compiler
1441 can define new types and new syntaxes, and old versions of the
1442 debugger will be able to read the new symbol tables. */
1443
1444 static struct type *
1445 error_type (const char **pp, struct objfile *objfile)
1446 {
1447 complaint (_("couldn't parse type; debugger out of date?"));
1448 while (1)
1449 {
1450 /* Skip to end of symbol. */
1451 while (**pp != '\0')
1452 {
1453 (*pp)++;
1454 }
1455
1456 /* Check for and handle cretinous dbx symbol name continuation! */
1457 if ((*pp)[-1] == '\\' || (*pp)[-1] == '?')
1458 {
1459 *pp = next_symbol_text (objfile);
1460 }
1461 else
1462 {
1463 break;
1464 }
1465 }
1466 return builtin_type (objfile)->builtin_error;
1467 }
1468 \f
1469
1470 /* Allocate a stub method whose return type is TYPE. This apparently
1471 happens for speed of symbol reading, since parsing out the
1472 arguments to the method is cpu-intensive, the way we are doing it.
1473 So, we will fill in arguments later. This always returns a fresh
1474 type. */
1475
1476 static struct type *
1477 allocate_stub_method (struct type *type)
1478 {
1479 struct type *mtype;
1480
1481 mtype = type_allocator (type).new_type ();
1482 mtype->set_code (TYPE_CODE_METHOD);
1483 mtype->set_length (1);
1484 mtype->set_is_stub (true);
1485 mtype->set_target_type (type);
1486 /* TYPE_SELF_TYPE (mtype) = unknown yet */
1487 return mtype;
1488 }
1489
1490 /* Read type information or a type definition; return the type. Even
1491 though this routine accepts either type information or a type
1492 definition, the distinction is relevant--some parts of stabsread.c
1493 assume that type information starts with a digit, '-', or '(' in
1494 deciding whether to call read_type. */
1495
1496 static struct type *
1497 read_type (const char **pp, struct objfile *objfile)
1498 {
1499 struct type *type = 0;
1500 struct type *type1;
1501 int typenums[2];
1502 char type_descriptor;
1503
1504 /* Size in bits of type if specified by a type attribute, or -1 if
1505 there is no size attribute. */
1506 int type_size = -1;
1507
1508 /* Used to distinguish string and bitstring from char-array and set. */
1509 int is_string = 0;
1510
1511 /* Used to distinguish vector from array. */
1512 int is_vector = 0;
1513
1514 /* Read type number if present. The type number may be omitted.
1515 for instance in a two-dimensional array declared with type
1516 "ar1;1;10;ar1;1;10;4". */
1517 if ((**pp >= '0' && **pp <= '9')
1518 || **pp == '('
1519 || **pp == '-')
1520 {
1521 if (read_type_number (pp, typenums) != 0)
1522 return error_type (pp, objfile);
1523
1524 if (**pp != '=')
1525 {
1526 /* Type is not being defined here. Either it already
1527 exists, or this is a forward reference to it.
1528 dbx_alloc_type handles both cases. */
1529 type = dbx_alloc_type (typenums, objfile);
1530
1531 /* If this is a forward reference, arrange to complain if it
1532 doesn't get patched up by the time we're done
1533 reading. */
1534 if (type->code () == TYPE_CODE_UNDEF)
1535 add_undefined_type (type, typenums);
1536
1537 return type;
1538 }
1539
1540 /* Type is being defined here. */
1541 /* Skip the '='.
1542 Also skip the type descriptor - we get it below with (*pp)[-1]. */
1543 (*pp) += 2;
1544 }
1545 else
1546 {
1547 /* 'typenums=' not present, type is anonymous. Read and return
1548 the definition, but don't put it in the type vector. */
1549 typenums[0] = typenums[1] = -1;
1550 (*pp)++;
1551 }
1552
1553 again:
1554 type_descriptor = (*pp)[-1];
1555 switch (type_descriptor)
1556 {
1557 case 'x':
1558 {
1559 enum type_code code;
1560
1561 /* Used to index through file_symbols. */
1562 struct pending *ppt;
1563 int i;
1564
1565 /* Name including "struct", etc. */
1566 char *type_name;
1567
1568 {
1569 const char *from, *p, *q1, *q2;
1570
1571 /* Set the type code according to the following letter. */
1572 switch ((*pp)[0])
1573 {
1574 case 's':
1575 code = TYPE_CODE_STRUCT;
1576 break;
1577 case 'u':
1578 code = TYPE_CODE_UNION;
1579 break;
1580 case 'e':
1581 code = TYPE_CODE_ENUM;
1582 break;
1583 default:
1584 {
1585 /* Complain and keep going, so compilers can invent new
1586 cross-reference types. */
1587 complaint (_("Unrecognized cross-reference type `%c'"),
1588 (*pp)[0]);
1589 code = TYPE_CODE_STRUCT;
1590 break;
1591 }
1592 }
1593
1594 q1 = strchr (*pp, '<');
1595 p = strchr (*pp, ':');
1596 if (p == NULL)
1597 return error_type (pp, objfile);
1598 if (q1 && p > q1 && p[1] == ':')
1599 {
1600 int nesting_level = 0;
1601
1602 for (q2 = q1; *q2; q2++)
1603 {
1604 if (*q2 == '<')
1605 nesting_level++;
1606 else if (*q2 == '>')
1607 nesting_level--;
1608 else if (*q2 == ':' && nesting_level == 0)
1609 break;
1610 }
1611 p = q2;
1612 if (*p != ':')
1613 return error_type (pp, objfile);
1614 }
1615 type_name = NULL;
1616 if (get_current_subfile ()->language == language_cplus)
1617 {
1618 std::string name (*pp, p - *pp);
1619 gdb::unique_xmalloc_ptr<char> new_name
1620 = cp_canonicalize_string (name.c_str ());
1621 if (new_name != nullptr)
1622 type_name = obstack_strdup (&objfile->objfile_obstack,
1623 new_name.get ());
1624 }
1625 else if (get_current_subfile ()->language == language_c)
1626 {
1627 std::string name (*pp, p - *pp);
1628 gdb::unique_xmalloc_ptr<char> new_name
1629 = c_canonicalize_name (name.c_str ());
1630 if (new_name != nullptr)
1631 type_name = obstack_strdup (&objfile->objfile_obstack,
1632 new_name.get ());
1633 }
1634 if (type_name == NULL)
1635 {
1636 char *to = type_name = (char *)
1637 obstack_alloc (&objfile->objfile_obstack, p - *pp + 1);
1638
1639 /* Copy the name. */
1640 from = *pp + 1;
1641 while (from < p)
1642 *to++ = *from++;
1643 *to = '\0';
1644 }
1645
1646 /* Set the pointer ahead of the name which we just read, and
1647 the colon. */
1648 *pp = p + 1;
1649 }
1650
1651 /* If this type has already been declared, then reuse the same
1652 type, rather than allocating a new one. This saves some
1653 memory. */
1654
1655 for (ppt = *get_file_symbols (); ppt; ppt = ppt->next)
1656 for (i = 0; i < ppt->nsyms; i++)
1657 {
1658 struct symbol *sym = ppt->symbol[i];
1659
1660 if (sym->aclass () == LOC_TYPEDEF
1661 && sym->domain () == STRUCT_DOMAIN
1662 && (sym->type ()->code () == code)
1663 && strcmp (sym->linkage_name (), type_name) == 0)
1664 {
1665 obstack_free (&objfile->objfile_obstack, type_name);
1666 type = sym->type ();
1667 if (typenums[0] != -1)
1668 *dbx_lookup_type (typenums, objfile) = type;
1669 return type;
1670 }
1671 }
1672
1673 /* Didn't find the type to which this refers, so we must
1674 be dealing with a forward reference. Allocate a type
1675 structure for it, and keep track of it so we can
1676 fill in the rest of the fields when we get the full
1677 type. */
1678 type = dbx_alloc_type (typenums, objfile);
1679 type->set_code (code);
1680 type->set_name (type_name);
1681 INIT_CPLUS_SPECIFIC (type);
1682 type->set_is_stub (true);
1683
1684 add_undefined_type (type, typenums);
1685 return type;
1686 }
1687
1688 case '-': /* RS/6000 built-in type */
1689 case '0':
1690 case '1':
1691 case '2':
1692 case '3':
1693 case '4':
1694 case '5':
1695 case '6':
1696 case '7':
1697 case '8':
1698 case '9':
1699 case '(':
1700 (*pp)--;
1701
1702 /* We deal with something like t(1,2)=(3,4)=... which
1703 the Lucid compiler and recent gcc versions (post 2.7.3) use. */
1704
1705 /* Allocate and enter the typedef type first.
1706 This handles recursive types. */
1707 type = dbx_alloc_type (typenums, objfile);
1708 type->set_code (TYPE_CODE_TYPEDEF);
1709 {
1710 struct type *xtype = read_type (pp, objfile);
1711
1712 if (type == xtype)
1713 {
1714 /* It's being defined as itself. That means it is "void". */
1715 type->set_code (TYPE_CODE_VOID);
1716 type->set_length (1);
1717 }
1718 else if (type_size >= 0 || is_string)
1719 {
1720 /* This is the absolute wrong way to construct types. Every
1721 other debug format has found a way around this problem and
1722 the related problems with unnecessarily stubbed types;
1723 someone motivated should attempt to clean up the issue
1724 here as well. Once a type pointed to has been created it
1725 should not be modified.
1726
1727 Well, it's not *absolutely* wrong. Constructing recursive
1728 types (trees, linked lists) necessarily entails modifying
1729 types after creating them. Constructing any loop structure
1730 entails side effects. The Dwarf 2 reader does handle this
1731 more gracefully (it never constructs more than once
1732 instance of a type object, so it doesn't have to copy type
1733 objects wholesale), but it still mutates type objects after
1734 other folks have references to them.
1735
1736 Keep in mind that this circularity/mutation issue shows up
1737 at the source language level, too: C's "incomplete types",
1738 for example. So the proper cleanup, I think, would be to
1739 limit GDB's type smashing to match exactly those required
1740 by the source language. So GDB could have a
1741 "complete_this_type" function, but never create unnecessary
1742 copies of a type otherwise. */
1743 replace_type (type, xtype);
1744 type->set_name (NULL);
1745 }
1746 else
1747 {
1748 type->set_target_is_stub (true);
1749 type->set_target_type (xtype);
1750 }
1751 }
1752 break;
1753
1754 /* In the following types, we must be sure to overwrite any existing
1755 type that the typenums refer to, rather than allocating a new one
1756 and making the typenums point to the new one. This is because there
1757 may already be pointers to the existing type (if it had been
1758 forward-referenced), and we must change it to a pointer, function,
1759 reference, or whatever, *in-place*. */
1760
1761 case '*': /* Pointer to another type */
1762 type1 = read_type (pp, objfile);
1763 type = make_pointer_type (type1, dbx_lookup_type (typenums, objfile));
1764 break;
1765
1766 case '&': /* Reference to another type */
1767 type1 = read_type (pp, objfile);
1768 type = make_reference_type (type1, dbx_lookup_type (typenums, objfile),
1769 TYPE_CODE_REF);
1770 break;
1771
1772 case 'f': /* Function returning another type */
1773 type1 = read_type (pp, objfile);
1774 type = make_function_type (type1, dbx_lookup_type (typenums, objfile));
1775 break;
1776
1777 case 'g': /* Prototyped function. (Sun) */
1778 {
1779 /* Unresolved questions:
1780
1781 - According to Sun's ``STABS Interface Manual'', for 'f'
1782 and 'F' symbol descriptors, a `0' in the argument type list
1783 indicates a varargs function. But it doesn't say how 'g'
1784 type descriptors represent that info. Someone with access
1785 to Sun's toolchain should try it out.
1786
1787 - According to the comment in define_symbol (search for
1788 `process_prototype_types:'), Sun emits integer arguments as
1789 types which ref themselves --- like `void' types. Do we
1790 have to deal with that here, too? Again, someone with
1791 access to Sun's toolchain should try it out and let us
1792 know. */
1793
1794 const char *type_start = (*pp) - 1;
1795 struct type *return_type = read_type (pp, objfile);
1796 struct type *func_type
1797 = make_function_type (return_type,
1798 dbx_lookup_type (typenums, objfile));
1799 struct type_list {
1800 struct type *type;
1801 struct type_list *next;
1802 } *arg_types = 0;
1803 int num_args = 0;
1804
1805 while (**pp && **pp != '#')
1806 {
1807 struct type *arg_type = read_type (pp, objfile);
1808 struct type_list *newobj = XALLOCA (struct type_list);
1809 newobj->type = arg_type;
1810 newobj->next = arg_types;
1811 arg_types = newobj;
1812 num_args++;
1813 }
1814 if (**pp == '#')
1815 ++*pp;
1816 else
1817 {
1818 complaint (_("Prototyped function type didn't "
1819 "end arguments with `#':\n%s"),
1820 type_start);
1821 }
1822
1823 /* If there is just one argument whose type is `void', then
1824 that's just an empty argument list. */
1825 if (arg_types
1826 && ! arg_types->next
1827 && arg_types->type->code () == TYPE_CODE_VOID)
1828 num_args = 0;
1829
1830 func_type->alloc_fields (num_args);
1831 {
1832 int i;
1833 struct type_list *t;
1834
1835 /* We stuck each argument type onto the front of the list
1836 when we read it, so the list is reversed. Build the
1837 fields array right-to-left. */
1838 for (t = arg_types, i = num_args - 1; t; t = t->next, i--)
1839 func_type->field (i).set_type (t->type);
1840 }
1841 func_type->set_num_fields (num_args);
1842 func_type->set_is_prototyped (true);
1843
1844 type = func_type;
1845 break;
1846 }
1847
1848 case 'k': /* Const qualifier on some type (Sun) */
1849 type = read_type (pp, objfile);
1850 type = make_cv_type (1, TYPE_VOLATILE (type), type,
1851 dbx_lookup_type (typenums, objfile));
1852 break;
1853
1854 case 'B': /* Volatile qual on some type (Sun) */
1855 type = read_type (pp, objfile);
1856 type = make_cv_type (TYPE_CONST (type), 1, type,
1857 dbx_lookup_type (typenums, objfile));
1858 break;
1859
1860 case '@':
1861 if (isdigit (**pp) || **pp == '(' || **pp == '-')
1862 { /* Member (class & variable) type */
1863 /* FIXME -- we should be doing smash_to_XXX types here. */
1864
1865 struct type *domain = read_type (pp, objfile);
1866 struct type *memtype;
1867
1868 if (**pp != ',')
1869 /* Invalid member type data format. */
1870 return error_type (pp, objfile);
1871 ++*pp;
1872
1873 memtype = read_type (pp, objfile);
1874 type = dbx_alloc_type (typenums, objfile);
1875 smash_to_memberptr_type (type, domain, memtype);
1876 }
1877 else
1878 /* type attribute */
1879 {
1880 const char *attr = *pp;
1881
1882 /* Skip to the semicolon. */
1883 while (**pp != ';' && **pp != '\0')
1884 ++(*pp);
1885 if (**pp == '\0')
1886 return error_type (pp, objfile);
1887 else
1888 ++ * pp; /* Skip the semicolon. */
1889
1890 switch (*attr)
1891 {
1892 case 's': /* Size attribute */
1893 type_size = atoi (attr + 1);
1894 if (type_size <= 0)
1895 type_size = -1;
1896 break;
1897
1898 case 'S': /* String attribute */
1899 /* FIXME: check to see if following type is array? */
1900 is_string = 1;
1901 break;
1902
1903 case 'V': /* Vector attribute */
1904 /* FIXME: check to see if following type is array? */
1905 is_vector = 1;
1906 break;
1907
1908 default:
1909 /* Ignore unrecognized type attributes, so future compilers
1910 can invent new ones. */
1911 break;
1912 }
1913 ++*pp;
1914 goto again;
1915 }
1916 break;
1917
1918 case '#': /* Method (class & fn) type */
1919 if ((*pp)[0] == '#')
1920 {
1921 /* We'll get the parameter types from the name. */
1922 struct type *return_type;
1923
1924 (*pp)++;
1925 return_type = read_type (pp, objfile);
1926 if (*(*pp)++ != ';')
1927 complaint (_("invalid (minimal) member type "
1928 "data format at symtab pos %d."),
1929 symnum);
1930 type = allocate_stub_method (return_type);
1931 if (typenums[0] != -1)
1932 *dbx_lookup_type (typenums, objfile) = type;
1933 }
1934 else
1935 {
1936 struct type *domain = read_type (pp, objfile);
1937 struct type *return_type;
1938 struct field *args;
1939 int nargs, varargs;
1940
1941 if (**pp != ',')
1942 /* Invalid member type data format. */
1943 return error_type (pp, objfile);
1944 else
1945 ++(*pp);
1946
1947 return_type = read_type (pp, objfile);
1948 args = read_args (pp, ';', objfile, &nargs, &varargs);
1949 if (args == NULL)
1950 return error_type (pp, objfile);
1951 type = dbx_alloc_type (typenums, objfile);
1952 smash_to_method_type (type, domain, return_type, args,
1953 nargs, varargs);
1954 }
1955 break;
1956
1957 case 'r': /* Range type */
1958 type = read_range_type (pp, typenums, type_size, objfile);
1959 if (typenums[0] != -1)
1960 *dbx_lookup_type (typenums, objfile) = type;
1961 break;
1962
1963 case 'b':
1964 {
1965 /* Sun ACC builtin int type */
1966 type = read_sun_builtin_type (pp, typenums, objfile);
1967 if (typenums[0] != -1)
1968 *dbx_lookup_type (typenums, objfile) = type;
1969 }
1970 break;
1971
1972 case 'R': /* Sun ACC builtin float type */
1973 type = read_sun_floating_type (pp, typenums, objfile);
1974 if (typenums[0] != -1)
1975 *dbx_lookup_type (typenums, objfile) = type;
1976 break;
1977
1978 case 'e': /* Enumeration type */
1979 type = dbx_alloc_type (typenums, objfile);
1980 type = read_enum_type (pp, type, objfile);
1981 if (typenums[0] != -1)
1982 *dbx_lookup_type (typenums, objfile) = type;
1983 break;
1984
1985 case 's': /* Struct type */
1986 case 'u': /* Union type */
1987 {
1988 enum type_code type_code = TYPE_CODE_UNDEF;
1989 type = dbx_alloc_type (typenums, objfile);
1990 switch (type_descriptor)
1991 {
1992 case 's':
1993 type_code = TYPE_CODE_STRUCT;
1994 break;
1995 case 'u':
1996 type_code = TYPE_CODE_UNION;
1997 break;
1998 }
1999 type = read_struct_type (pp, type, type_code, objfile);
2000 break;
2001 }
2002
2003 case 'a': /* Array type */
2004 if (**pp != 'r')
2005 return error_type (pp, objfile);
2006 ++*pp;
2007
2008 type = dbx_alloc_type (typenums, objfile);
2009 type = read_array_type (pp, type, objfile);
2010 if (is_string)
2011 type->set_code (TYPE_CODE_STRING);
2012 if (is_vector)
2013 make_vector_type (type);
2014 break;
2015
2016 case 'S': /* Set type */
2017 {
2018 type1 = read_type (pp, objfile);
2019 type_allocator alloc (objfile, get_current_subfile ()->language);
2020 type = create_set_type (alloc, type1);
2021 if (typenums[0] != -1)
2022 *dbx_lookup_type (typenums, objfile) = type;
2023 }
2024 break;
2025
2026 default:
2027 --*pp; /* Go back to the symbol in error. */
2028 /* Particularly important if it was \0! */
2029 return error_type (pp, objfile);
2030 }
2031
2032 if (type == 0)
2033 {
2034 warning (_("GDB internal error, type is NULL in stabsread.c."));
2035 return error_type (pp, objfile);
2036 }
2037
2038 /* Size specified in a type attribute overrides any other size. */
2039 if (type_size != -1)
2040 type->set_length ((type_size + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT);
2041
2042 return type;
2043 }
2044 \f
2045 /* RS/6000 xlc/dbx combination uses a set of builtin types, starting from -1.
2046 Return the proper type node for a given builtin type number. */
2047
2048 static const registry<objfile>::key<struct type *,
2049 gdb::noop_deleter<struct type *>>
2050 rs6000_builtin_type_data;
2051
2052 static struct type *
2053 rs6000_builtin_type (int typenum, struct objfile *objfile)
2054 {
2055 struct type **negative_types = rs6000_builtin_type_data.get (objfile);
2056
2057 /* We recognize types numbered from -NUMBER_RECOGNIZED to -1. */
2058 #define NUMBER_RECOGNIZED 34
2059 struct type *rettype = NULL;
2060
2061 if (typenum >= 0 || typenum < -NUMBER_RECOGNIZED)
2062 {
2063 complaint (_("Unknown builtin type %d"), typenum);
2064 return builtin_type (objfile)->builtin_error;
2065 }
2066
2067 if (!negative_types)
2068 {
2069 /* This includes an empty slot for type number -0. */
2070 negative_types = OBSTACK_CALLOC (&objfile->objfile_obstack,
2071 NUMBER_RECOGNIZED + 1, struct type *);
2072 rs6000_builtin_type_data.set (objfile, negative_types);
2073 }
2074
2075 if (negative_types[-typenum] != NULL)
2076 return negative_types[-typenum];
2077
2078 #if TARGET_CHAR_BIT != 8
2079 #error This code wrong for TARGET_CHAR_BIT not 8
2080 /* These definitions all assume that TARGET_CHAR_BIT is 8. I think
2081 that if that ever becomes not true, the correct fix will be to
2082 make the size in the struct type to be in bits, not in units of
2083 TARGET_CHAR_BIT. */
2084 #endif
2085
2086 type_allocator alloc (objfile, get_current_subfile ()->language);
2087 switch (-typenum)
2088 {
2089 case 1:
2090 /* The size of this and all the other types are fixed, defined
2091 by the debugging format. If there is a type called "int" which
2092 is other than 32 bits, then it should use a new negative type
2093 number (or avoid negative type numbers for that case).
2094 See stabs.texinfo. */
2095 rettype = init_integer_type (alloc, 32, 0, "int");
2096 break;
2097 case 2:
2098 rettype = init_integer_type (alloc, 8, 0, "char");
2099 rettype->set_has_no_signedness (true);
2100 break;
2101 case 3:
2102 rettype = init_integer_type (alloc, 16, 0, "short");
2103 break;
2104 case 4:
2105 rettype = init_integer_type (alloc, 32, 0, "long");
2106 break;
2107 case 5:
2108 rettype = init_integer_type (alloc, 8, 1, "unsigned char");
2109 break;
2110 case 6:
2111 rettype = init_integer_type (alloc, 8, 0, "signed char");
2112 break;
2113 case 7:
2114 rettype = init_integer_type (alloc, 16, 1, "unsigned short");
2115 break;
2116 case 8:
2117 rettype = init_integer_type (alloc, 32, 1, "unsigned int");
2118 break;
2119 case 9:
2120 rettype = init_integer_type (alloc, 32, 1, "unsigned");
2121 break;
2122 case 10:
2123 rettype = init_integer_type (alloc, 32, 1, "unsigned long");
2124 break;
2125 case 11:
2126 rettype = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
2127 break;
2128 case 12:
2129 /* IEEE single precision (32 bit). */
2130 rettype = init_float_type (alloc, 32, "float",
2131 floatformats_ieee_single);
2132 break;
2133 case 13:
2134 /* IEEE double precision (64 bit). */
2135 rettype = init_float_type (alloc, 64, "double",
2136 floatformats_ieee_double);
2137 break;
2138 case 14:
2139 /* This is an IEEE double on the RS/6000, and different machines with
2140 different sizes for "long double" should use different negative
2141 type numbers. See stabs.texinfo. */
2142 rettype = init_float_type (alloc, 64, "long double",
2143 floatformats_ieee_double);
2144 break;
2145 case 15:
2146 rettype = init_integer_type (alloc, 32, 0, "integer");
2147 break;
2148 case 16:
2149 rettype = init_boolean_type (alloc, 32, 1, "boolean");
2150 break;
2151 case 17:
2152 rettype = init_float_type (alloc, 32, "short real",
2153 floatformats_ieee_single);
2154 break;
2155 case 18:
2156 rettype = init_float_type (alloc, 64, "real",
2157 floatformats_ieee_double);
2158 break;
2159 case 19:
2160 rettype = alloc.new_type (TYPE_CODE_ERROR, 0, "stringptr");
2161 break;
2162 case 20:
2163 rettype = init_character_type (alloc, 8, 1, "character");
2164 break;
2165 case 21:
2166 rettype = init_boolean_type (alloc, 8, 1, "logical*1");
2167 break;
2168 case 22:
2169 rettype = init_boolean_type (alloc, 16, 1, "logical*2");
2170 break;
2171 case 23:
2172 rettype = init_boolean_type (alloc, 32, 1, "logical*4");
2173 break;
2174 case 24:
2175 rettype = init_boolean_type (alloc, 32, 1, "logical");
2176 break;
2177 case 25:
2178 /* Complex type consisting of two IEEE single precision values. */
2179 rettype = init_complex_type ("complex",
2180 rs6000_builtin_type (12, objfile));
2181 break;
2182 case 26:
2183 /* Complex type consisting of two IEEE double precision values. */
2184 rettype = init_complex_type ("double complex",
2185 rs6000_builtin_type (13, objfile));
2186 break;
2187 case 27:
2188 rettype = init_integer_type (alloc, 8, 0, "integer*1");
2189 break;
2190 case 28:
2191 rettype = init_integer_type (alloc, 16, 0, "integer*2");
2192 break;
2193 case 29:
2194 rettype = init_integer_type (alloc, 32, 0, "integer*4");
2195 break;
2196 case 30:
2197 rettype = init_character_type (alloc, 16, 0, "wchar");
2198 break;
2199 case 31:
2200 rettype = init_integer_type (alloc, 64, 0, "long long");
2201 break;
2202 case 32:
2203 rettype = init_integer_type (alloc, 64, 1, "unsigned long long");
2204 break;
2205 case 33:
2206 rettype = init_integer_type (alloc, 64, 1, "logical*8");
2207 break;
2208 case 34:
2209 rettype = init_integer_type (alloc, 64, 0, "integer*8");
2210 break;
2211 }
2212 negative_types[-typenum] = rettype;
2213 return rettype;
2214 }
2215 \f
2216 /* This page contains subroutines of read_type. */
2217
2218 /* Wrapper around method_name_from_physname to flag a complaint
2219 if there is an error. */
2220
2221 static char *
2222 stabs_method_name_from_physname (const char *physname)
2223 {
2224 char *method_name;
2225
2226 method_name = method_name_from_physname (physname);
2227
2228 if (method_name == NULL)
2229 {
2230 complaint (_("Method has bad physname %s\n"), physname);
2231 return NULL;
2232 }
2233
2234 return method_name;
2235 }
2236
2237 /* Read member function stabs info for C++ classes. The form of each member
2238 function data is:
2239
2240 NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2241
2242 An example with two member functions is:
2243
2244 afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2245
2246 For the case of overloaded operators, the format is op$::*.funcs, where
2247 $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2248 name (such as `+=') and `.' marks the end of the operator name.
2249
2250 Returns 1 for success, 0 for failure. */
2251
2252 static int
2253 read_member_functions (struct stab_field_info *fip, const char **pp,
2254 struct type *type, struct objfile *objfile)
2255 {
2256 int nfn_fields = 0;
2257 int length = 0;
2258 int i;
2259 struct next_fnfield
2260 {
2261 struct next_fnfield *next;
2262 struct fn_field fn_field;
2263 }
2264 *sublist;
2265 struct type *look_ahead_type;
2266 struct next_fnfieldlist *new_fnlist;
2267 struct next_fnfield *new_sublist;
2268 char *main_fn_name;
2269 const char *p;
2270
2271 /* Process each list until we find something that is not a member function
2272 or find the end of the functions. */
2273
2274 while (**pp != ';')
2275 {
2276 /* We should be positioned at the start of the function name.
2277 Scan forward to find the first ':' and if it is not the
2278 first of a "::" delimiter, then this is not a member function. */
2279 p = *pp;
2280 while (*p != ':')
2281 {
2282 p++;
2283 }
2284 if (p[1] != ':')
2285 {
2286 break;
2287 }
2288
2289 sublist = NULL;
2290 look_ahead_type = NULL;
2291 length = 0;
2292
2293 new_fnlist = OBSTACK_ZALLOC (&fip->obstack, struct next_fnfieldlist);
2294
2295 if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && is_cplus_marker ((*pp)[2]))
2296 {
2297 /* This is a completely wierd case. In order to stuff in the
2298 names that might contain colons (the usual name delimiter),
2299 Mike Tiemann defined a different name format which is
2300 signalled if the identifier is "op$". In that case, the
2301 format is "op$::XXXX." where XXXX is the name. This is
2302 used for names like "+" or "=". YUUUUUUUK! FIXME! */
2303 /* This lets the user type "break operator+".
2304 We could just put in "+" as the name, but that wouldn't
2305 work for "*". */
2306 static char opname[32] = "op$";
2307 char *o = opname + 3;
2308
2309 /* Skip past '::'. */
2310 *pp = p + 2;
2311
2312 STABS_CONTINUE (pp, objfile);
2313 p = *pp;
2314 while (*p != '.')
2315 {
2316 *o++ = *p++;
2317 }
2318 main_fn_name = savestring (opname, o - opname);
2319 /* Skip past '.' */
2320 *pp = p + 1;
2321 }
2322 else
2323 {
2324 main_fn_name = savestring (*pp, p - *pp);
2325 /* Skip past '::'. */
2326 *pp = p + 2;
2327 }
2328 new_fnlist->fn_fieldlist.name = main_fn_name;
2329
2330 do
2331 {
2332 new_sublist = OBSTACK_ZALLOC (&fip->obstack, struct next_fnfield);
2333
2334 /* Check for and handle cretinous dbx symbol name continuation! */
2335 if (look_ahead_type == NULL)
2336 {
2337 /* Normal case. */
2338 STABS_CONTINUE (pp, objfile);
2339
2340 new_sublist->fn_field.type = read_type (pp, objfile);
2341 if (**pp != ':')
2342 {
2343 /* Invalid symtab info for member function. */
2344 return 0;
2345 }
2346 }
2347 else
2348 {
2349 /* g++ version 1 kludge */
2350 new_sublist->fn_field.type = look_ahead_type;
2351 look_ahead_type = NULL;
2352 }
2353
2354 (*pp)++;
2355 p = *pp;
2356 while (*p != ';')
2357 {
2358 p++;
2359 }
2360
2361 /* These are methods, not functions. */
2362 if (new_sublist->fn_field.type->code () == TYPE_CODE_FUNC)
2363 new_sublist->fn_field.type->set_code (TYPE_CODE_METHOD);
2364
2365 /* If this is just a stub, then we don't have the real name here. */
2366 if (new_sublist->fn_field.type->is_stub ())
2367 {
2368 if (!TYPE_SELF_TYPE (new_sublist->fn_field.type))
2369 set_type_self_type (new_sublist->fn_field.type, type);
2370 new_sublist->fn_field.is_stub = 1;
2371 }
2372
2373 new_sublist->fn_field.physname = savestring (*pp, p - *pp);
2374 *pp = p + 1;
2375
2376 /* Set this member function's visibility fields. */
2377 switch (*(*pp)++)
2378 {
2379 case VISIBILITY_PRIVATE:
2380 new_sublist->fn_field.is_private = 1;
2381 break;
2382 case VISIBILITY_PROTECTED:
2383 new_sublist->fn_field.is_protected = 1;
2384 break;
2385 }
2386
2387 STABS_CONTINUE (pp, objfile);
2388 switch (**pp)
2389 {
2390 case 'A': /* Normal functions. */
2391 new_sublist->fn_field.is_const = 0;
2392 new_sublist->fn_field.is_volatile = 0;
2393 (*pp)++;
2394 break;
2395 case 'B': /* `const' member functions. */
2396 new_sublist->fn_field.is_const = 1;
2397 new_sublist->fn_field.is_volatile = 0;
2398 (*pp)++;
2399 break;
2400 case 'C': /* `volatile' member function. */
2401 new_sublist->fn_field.is_const = 0;
2402 new_sublist->fn_field.is_volatile = 1;
2403 (*pp)++;
2404 break;
2405 case 'D': /* `const volatile' member function. */
2406 new_sublist->fn_field.is_const = 1;
2407 new_sublist->fn_field.is_volatile = 1;
2408 (*pp)++;
2409 break;
2410 case '*': /* File compiled with g++ version 1 --
2411 no info. */
2412 case '?':
2413 case '.':
2414 break;
2415 default:
2416 complaint (_("const/volatile indicator missing, got '%c'"),
2417 **pp);
2418 break;
2419 }
2420
2421 switch (*(*pp)++)
2422 {
2423 case '*':
2424 {
2425 int nbits;
2426 /* virtual member function, followed by index.
2427 The sign bit is set to distinguish pointers-to-methods
2428 from virtual function indicies. Since the array is
2429 in words, the quantity must be shifted left by 1
2430 on 16 bit machine, and by 2 on 32 bit machine, forcing
2431 the sign bit out, and usable as a valid index into
2432 the array. Remove the sign bit here. */
2433 new_sublist->fn_field.voffset =
2434 (0x7fffffff & read_huge_number (pp, ';', &nbits, 0)) + 2;
2435 if (nbits != 0)
2436 return 0;
2437
2438 STABS_CONTINUE (pp, objfile);
2439 if (**pp == ';' || **pp == '\0')
2440 {
2441 /* Must be g++ version 1. */
2442 new_sublist->fn_field.fcontext = 0;
2443 }
2444 else
2445 {
2446 /* Figure out from whence this virtual function came.
2447 It may belong to virtual function table of
2448 one of its baseclasses. */
2449 look_ahead_type = read_type (pp, objfile);
2450 if (**pp == ':')
2451 {
2452 /* g++ version 1 overloaded methods. */
2453 }
2454 else
2455 {
2456 new_sublist->fn_field.fcontext = look_ahead_type;
2457 if (**pp != ';')
2458 {
2459 return 0;
2460 }
2461 else
2462 {
2463 ++*pp;
2464 }
2465 look_ahead_type = NULL;
2466 }
2467 }
2468 break;
2469 }
2470 case '?':
2471 /* static member function. */
2472 {
2473 int slen = strlen (main_fn_name);
2474
2475 new_sublist->fn_field.voffset = VOFFSET_STATIC;
2476
2477 /* For static member functions, we can't tell if they
2478 are stubbed, as they are put out as functions, and not as
2479 methods.
2480 GCC v2 emits the fully mangled name if
2481 dbxout.c:flag_minimal_debug is not set, so we have to
2482 detect a fully mangled physname here and set is_stub
2483 accordingly. Fully mangled physnames in v2 start with
2484 the member function name, followed by two underscores.
2485 GCC v3 currently always emits stubbed member functions,
2486 but with fully mangled physnames, which start with _Z. */
2487 if (!(strncmp (new_sublist->fn_field.physname,
2488 main_fn_name, slen) == 0
2489 && new_sublist->fn_field.physname[slen] == '_'
2490 && new_sublist->fn_field.physname[slen + 1] == '_'))
2491 {
2492 new_sublist->fn_field.is_stub = 1;
2493 }
2494 break;
2495 }
2496
2497 default:
2498 /* error */
2499 complaint (_("member function type missing, got '%c'"),
2500 (*pp)[-1]);
2501 /* Normal member function. */
2502 /* Fall through. */
2503
2504 case '.':
2505 /* normal member function. */
2506 new_sublist->fn_field.voffset = 0;
2507 new_sublist->fn_field.fcontext = 0;
2508 break;
2509 }
2510
2511 new_sublist->next = sublist;
2512 sublist = new_sublist;
2513 length++;
2514 STABS_CONTINUE (pp, objfile);
2515 }
2516 while (**pp != ';' && **pp != '\0');
2517
2518 (*pp)++;
2519 STABS_CONTINUE (pp, objfile);
2520
2521 /* Skip GCC 3.X member functions which are duplicates of the callable
2522 constructor/destructor. */
2523 if (strcmp_iw (main_fn_name, "__base_ctor ") == 0
2524 || strcmp_iw (main_fn_name, "__base_dtor ") == 0
2525 || strcmp (main_fn_name, "__deleting_dtor") == 0)
2526 {
2527 xfree (main_fn_name);
2528 }
2529 else
2530 {
2531 int has_destructor = 0, has_other = 0;
2532 int is_v3 = 0;
2533 struct next_fnfield *tmp_sublist;
2534
2535 /* Various versions of GCC emit various mostly-useless
2536 strings in the name field for special member functions.
2537
2538 For stub methods, we need to defer correcting the name
2539 until we are ready to unstub the method, because the current
2540 name string is used by gdb_mangle_name. The only stub methods
2541 of concern here are GNU v2 operators; other methods have their
2542 names correct (see caveat below).
2543
2544 For non-stub methods, in GNU v3, we have a complete physname.
2545 Therefore we can safely correct the name now. This primarily
2546 affects constructors and destructors, whose name will be
2547 __comp_ctor or __comp_dtor instead of Foo or ~Foo. Cast
2548 operators will also have incorrect names; for instance,
2549 "operator int" will be named "operator i" (i.e. the type is
2550 mangled).
2551
2552 For non-stub methods in GNU v2, we have no easy way to
2553 know if we have a complete physname or not. For most
2554 methods the result depends on the platform (if CPLUS_MARKER
2555 can be `$' or `.', it will use minimal debug information, or
2556 otherwise the full physname will be included).
2557
2558 Rather than dealing with this, we take a different approach.
2559 For v3 mangled names, we can use the full physname; for v2,
2560 we use cplus_demangle_opname (which is actually v2 specific),
2561 because the only interesting names are all operators - once again
2562 barring the caveat below. Skip this process if any method in the
2563 group is a stub, to prevent our fouling up the workings of
2564 gdb_mangle_name.
2565
2566 The caveat: GCC 2.95.x (and earlier?) put constructors and
2567 destructors in the same method group. We need to split this
2568 into two groups, because they should have different names.
2569 So for each method group we check whether it contains both
2570 routines whose physname appears to be a destructor (the physnames
2571 for and destructors are always provided, due to quirks in v2
2572 mangling) and routines whose physname does not appear to be a
2573 destructor. If so then we break up the list into two halves.
2574 Even if the constructors and destructors aren't in the same group
2575 the destructor will still lack the leading tilde, so that also
2576 needs to be fixed.
2577
2578 So, to summarize what we expect and handle here:
2579
2580 Given Given Real Real Action
2581 method name physname physname method name
2582
2583 __opi [none] __opi__3Foo operator int opname
2584 [now or later]
2585 Foo _._3Foo _._3Foo ~Foo separate and
2586 rename
2587 operator i _ZN3FoocviEv _ZN3FoocviEv operator int demangle
2588 __comp_ctor _ZN3FooC1ERKS_ _ZN3FooC1ERKS_ Foo demangle
2589 */
2590
2591 tmp_sublist = sublist;
2592 while (tmp_sublist != NULL)
2593 {
2594 if (tmp_sublist->fn_field.physname[0] == '_'
2595 && tmp_sublist->fn_field.physname[1] == 'Z')
2596 is_v3 = 1;
2597
2598 if (is_destructor_name (tmp_sublist->fn_field.physname))
2599 has_destructor++;
2600 else
2601 has_other++;
2602
2603 tmp_sublist = tmp_sublist->next;
2604 }
2605
2606 if (has_destructor && has_other)
2607 {
2608 struct next_fnfieldlist *destr_fnlist;
2609 struct next_fnfield *last_sublist;
2610
2611 /* Create a new fn_fieldlist for the destructors. */
2612
2613 destr_fnlist = OBSTACK_ZALLOC (&fip->obstack,
2614 struct next_fnfieldlist);
2615
2616 destr_fnlist->fn_fieldlist.name
2617 = obconcat (&objfile->objfile_obstack, "~",
2618 new_fnlist->fn_fieldlist.name, (char *) NULL);
2619
2620 destr_fnlist->fn_fieldlist.fn_fields =
2621 XOBNEWVEC (&objfile->objfile_obstack,
2622 struct fn_field, has_destructor);
2623 memset (destr_fnlist->fn_fieldlist.fn_fields, 0,
2624 sizeof (struct fn_field) * has_destructor);
2625 tmp_sublist = sublist;
2626 last_sublist = NULL;
2627 i = 0;
2628 while (tmp_sublist != NULL)
2629 {
2630 if (!is_destructor_name (tmp_sublist->fn_field.physname))
2631 {
2632 tmp_sublist = tmp_sublist->next;
2633 continue;
2634 }
2635
2636 destr_fnlist->fn_fieldlist.fn_fields[i++]
2637 = tmp_sublist->fn_field;
2638 if (last_sublist)
2639 last_sublist->next = tmp_sublist->next;
2640 else
2641 sublist = tmp_sublist->next;
2642 last_sublist = tmp_sublist;
2643 tmp_sublist = tmp_sublist->next;
2644 }
2645
2646 destr_fnlist->fn_fieldlist.length = has_destructor;
2647 destr_fnlist->next = fip->fnlist;
2648 fip->fnlist = destr_fnlist;
2649 nfn_fields++;
2650 length -= has_destructor;
2651 }
2652 else if (is_v3)
2653 {
2654 /* v3 mangling prevents the use of abbreviated physnames,
2655 so we can do this here. There are stubbed methods in v3
2656 only:
2657 - in -gstabs instead of -gstabs+
2658 - or for static methods, which are output as a function type
2659 instead of a method type. */
2660 char *new_method_name =
2661 stabs_method_name_from_physname (sublist->fn_field.physname);
2662
2663 if (new_method_name != NULL
2664 && strcmp (new_method_name,
2665 new_fnlist->fn_fieldlist.name) != 0)
2666 {
2667 new_fnlist->fn_fieldlist.name = new_method_name;
2668 xfree (main_fn_name);
2669 }
2670 else
2671 xfree (new_method_name);
2672 }
2673 else if (has_destructor && new_fnlist->fn_fieldlist.name[0] != '~')
2674 {
2675 new_fnlist->fn_fieldlist.name =
2676 obconcat (&objfile->objfile_obstack,
2677 "~", main_fn_name, (char *)NULL);
2678 xfree (main_fn_name);
2679 }
2680
2681 new_fnlist->fn_fieldlist.fn_fields
2682 = OBSTACK_CALLOC (&objfile->objfile_obstack, length, fn_field);
2683 for (i = length; (i--, sublist); sublist = sublist->next)
2684 {
2685 new_fnlist->fn_fieldlist.fn_fields[i] = sublist->fn_field;
2686 }
2687
2688 new_fnlist->fn_fieldlist.length = length;
2689 new_fnlist->next = fip->fnlist;
2690 fip->fnlist = new_fnlist;
2691 nfn_fields++;
2692 }
2693 }
2694
2695 if (nfn_fields)
2696 {
2697 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2698 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2699 TYPE_ZALLOC (type, sizeof (struct fn_fieldlist) * nfn_fields);
2700 TYPE_NFN_FIELDS (type) = nfn_fields;
2701 }
2702
2703 return 1;
2704 }
2705
2706 /* Special GNU C++ name.
2707
2708 Returns 1 for success, 0 for failure. "failure" means that we can't
2709 keep parsing and it's time for error_type(). */
2710
2711 static int
2712 read_cpp_abbrev (struct stab_field_info *fip, const char **pp,
2713 struct type *type, struct objfile *objfile)
2714 {
2715 const char *p;
2716 const char *name;
2717 char cpp_abbrev;
2718 struct type *context;
2719
2720 p = *pp;
2721 if (*++p == 'v')
2722 {
2723 name = NULL;
2724 cpp_abbrev = *++p;
2725
2726 *pp = p + 1;
2727
2728 /* At this point, *pp points to something like "22:23=*22...",
2729 where the type number before the ':' is the "context" and
2730 everything after is a regular type definition. Lookup the
2731 type, find it's name, and construct the field name. */
2732
2733 context = read_type (pp, objfile);
2734
2735 switch (cpp_abbrev)
2736 {
2737 case 'f': /* $vf -- a virtual function table pointer */
2738 name = context->name ();
2739 if (name == NULL)
2740 {
2741 name = "";
2742 }
2743 fip->list->field.set_name (obconcat (&objfile->objfile_obstack,
2744 vptr_name, name, (char *) NULL));
2745 break;
2746
2747 case 'b': /* $vb -- a virtual bsomethingorother */
2748 name = context->name ();
2749 if (name == NULL)
2750 {
2751 complaint (_("C++ abbreviated type name "
2752 "unknown at symtab pos %d"),
2753 symnum);
2754 name = "FOO";
2755 }
2756 fip->list->field.set_name (obconcat (&objfile->objfile_obstack,
2757 vb_name, name, (char *) NULL));
2758 break;
2759
2760 default:
2761 invalid_cpp_abbrev_complaint (*pp);
2762 fip->list->field.set_name (obconcat (&objfile->objfile_obstack,
2763 "INVALID_CPLUSPLUS_ABBREV",
2764 (char *) NULL));
2765 break;
2766 }
2767
2768 /* At this point, *pp points to the ':'. Skip it and read the
2769 field type. */
2770
2771 p = ++(*pp);
2772 if (p[-1] != ':')
2773 {
2774 invalid_cpp_abbrev_complaint (*pp);
2775 return 0;
2776 }
2777 fip->list->field.set_type (read_type (pp, objfile));
2778 if (**pp == ',')
2779 (*pp)++; /* Skip the comma. */
2780 else
2781 return 0;
2782
2783 {
2784 int nbits;
2785
2786 fip->list->field.set_loc_bitpos (read_huge_number (pp, ';', &nbits, 0));
2787 if (nbits != 0)
2788 return 0;
2789 }
2790 /* This field is unpacked. */
2791 fip->list->field.set_bitsize (0);
2792 fip->list->visibility = VISIBILITY_PRIVATE;
2793 }
2794 else
2795 {
2796 invalid_cpp_abbrev_complaint (*pp);
2797 /* We have no idea what syntax an unrecognized abbrev would have, so
2798 better return 0. If we returned 1, we would need to at least advance
2799 *pp to avoid an infinite loop. */
2800 return 0;
2801 }
2802 return 1;
2803 }
2804
2805 static void
2806 read_one_struct_field (struct stab_field_info *fip, const char **pp,
2807 const char *p, struct type *type,
2808 struct objfile *objfile)
2809 {
2810 struct gdbarch *gdbarch = objfile->arch ();
2811
2812 fip->list->field.set_name
2813 (obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp));
2814 *pp = p + 1;
2815
2816 /* This means we have a visibility for a field coming. */
2817 if (**pp == '/')
2818 {
2819 (*pp)++;
2820 fip->list->visibility = *(*pp)++;
2821 }
2822 else
2823 {
2824 /* normal dbx-style format, no explicit visibility */
2825 fip->list->visibility = VISIBILITY_PUBLIC;
2826 }
2827
2828 fip->list->field.set_type (read_type (pp, objfile));
2829 if (**pp == ':')
2830 {
2831 p = ++(*pp);
2832 #if 0
2833 /* Possible future hook for nested types. */
2834 if (**pp == '!')
2835 {
2836 fip->list->field.bitpos = (long) -2; /* nested type */
2837 p = ++(*pp);
2838 }
2839 else
2840 ...;
2841 #endif
2842 while (*p != ';')
2843 {
2844 p++;
2845 }
2846 /* Static class member. */
2847 fip->list->field.set_loc_physname (savestring (*pp, p - *pp));
2848 *pp = p + 1;
2849 return;
2850 }
2851 else if (**pp != ',')
2852 {
2853 /* Bad structure-type format. */
2854 stabs_general_complaint ("bad structure-type format");
2855 return;
2856 }
2857
2858 (*pp)++; /* Skip the comma. */
2859
2860 {
2861 int nbits;
2862
2863 fip->list->field.set_loc_bitpos (read_huge_number (pp, ',', &nbits, 0));
2864 if (nbits != 0)
2865 {
2866 stabs_general_complaint ("bad structure-type format");
2867 return;
2868 }
2869 fip->list->field.set_bitsize (read_huge_number (pp, ';', &nbits, 0));
2870 if (nbits != 0)
2871 {
2872 stabs_general_complaint ("bad structure-type format");
2873 return;
2874 }
2875 }
2876
2877 if (fip->list->field.loc_bitpos () == 0
2878 && fip->list->field.bitsize () == 0)
2879 {
2880 /* This can happen in two cases: (1) at least for gcc 2.4.5 or so,
2881 it is a field which has been optimized out. The correct stab for
2882 this case is to use VISIBILITY_IGNORE, but that is a recent
2883 invention. (2) It is a 0-size array. For example
2884 union { int num; char str[0]; } foo. Printing _("<no value>" for
2885 str in "p foo" is OK, since foo.str (and thus foo.str[3])
2886 will continue to work, and a 0-size array as a whole doesn't
2887 have any contents to print.
2888
2889 I suspect this probably could also happen with gcc -gstabs (not
2890 -gstabs+) for static fields, and perhaps other C++ extensions.
2891 Hopefully few people use -gstabs with gdb, since it is intended
2892 for dbx compatibility. */
2893
2894 /* Ignore this field. */
2895 fip->list->visibility = VISIBILITY_IGNORE;
2896 }
2897 else
2898 {
2899 /* Detect an unpacked field and mark it as such.
2900 dbx gives a bit size for all fields.
2901 Note that forward refs cannot be packed,
2902 and treat enums as if they had the width of ints. */
2903
2904 struct type *field_type = check_typedef (fip->list->field.type ());
2905
2906 if (field_type->code () != TYPE_CODE_INT
2907 && field_type->code () != TYPE_CODE_RANGE
2908 && field_type->code () != TYPE_CODE_BOOL
2909 && field_type->code () != TYPE_CODE_ENUM)
2910 {
2911 fip->list->field.set_bitsize (0);
2912 }
2913 if ((fip->list->field.bitsize ()
2914 == TARGET_CHAR_BIT * field_type->length ()
2915 || (field_type->code () == TYPE_CODE_ENUM
2916 && (fip->list->field.bitsize ()
2917 == gdbarch_int_bit (gdbarch)))
2918 )
2919 &&
2920 fip->list->field.loc_bitpos () % 8 == 0)
2921 {
2922 fip->list->field.set_bitsize (0);
2923 }
2924 }
2925 }
2926
2927
2928 /* Read struct or class data fields. They have the form:
2929
2930 NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2931
2932 At the end, we see a semicolon instead of a field.
2933
2934 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2935 a static field.
2936
2937 The optional VISIBILITY is one of:
2938
2939 '/0' (VISIBILITY_PRIVATE)
2940 '/1' (VISIBILITY_PROTECTED)
2941 '/2' (VISIBILITY_PUBLIC)
2942 '/9' (VISIBILITY_IGNORE)
2943
2944 or nothing, for C style fields with public visibility.
2945
2946 Returns 1 for success, 0 for failure. */
2947
2948 static int
2949 read_struct_fields (struct stab_field_info *fip, const char **pp,
2950 struct type *type, struct objfile *objfile)
2951 {
2952 const char *p;
2953 struct stabs_nextfield *newobj;
2954
2955 /* We better set p right now, in case there are no fields at all... */
2956
2957 p = *pp;
2958
2959 /* Read each data member type until we find the terminating ';' at the end of
2960 the data member list, or break for some other reason such as finding the
2961 start of the member function list. */
2962 /* Stab string for structure/union does not end with two ';' in
2963 SUN C compiler 5.3 i.e. F6U2, hence check for end of string. */
2964
2965 while (**pp != ';' && **pp != '\0')
2966 {
2967 STABS_CONTINUE (pp, objfile);
2968 /* Get space to record the next field's data. */
2969 newobj = OBSTACK_ZALLOC (&fip->obstack, struct stabs_nextfield);
2970
2971 newobj->next = fip->list;
2972 fip->list = newobj;
2973
2974 /* Get the field name. */
2975 p = *pp;
2976
2977 /* If is starts with CPLUS_MARKER it is a special abbreviation,
2978 unless the CPLUS_MARKER is followed by an underscore, in
2979 which case it is just the name of an anonymous type, which we
2980 should handle like any other type name. */
2981
2982 if (is_cplus_marker (p[0]) && p[1] != '_')
2983 {
2984 if (!read_cpp_abbrev (fip, pp, type, objfile))
2985 return 0;
2986 continue;
2987 }
2988
2989 /* Look for the ':' that separates the field name from the field
2990 values. Data members are delimited by a single ':', while member
2991 functions are delimited by a pair of ':'s. When we hit the member
2992 functions (if any), terminate scan loop and return. */
2993
2994 while (*p != ':' && *p != '\0')
2995 {
2996 p++;
2997 }
2998 if (*p == '\0')
2999 return 0;
3000
3001 /* Check to see if we have hit the member functions yet. */
3002 if (p[1] == ':')
3003 {
3004 break;
3005 }
3006 read_one_struct_field (fip, pp, p, type, objfile);
3007 }
3008 if (p[0] == ':' && p[1] == ':')
3009 {
3010 /* (the deleted) chill the list of fields: the last entry (at
3011 the head) is a partially constructed entry which we now
3012 scrub. */
3013 fip->list = fip->list->next;
3014 }
3015 return 1;
3016 }
3017 /* The stabs for C++ derived classes contain baseclass information which
3018 is marked by a '!' character after the total size. This function is
3019 called when we encounter the baseclass marker, and slurps up all the
3020 baseclass information.
3021
3022 Immediately following the '!' marker is the number of base classes that
3023 the class is derived from, followed by information for each base class.
3024 For each base class, there are two visibility specifiers, a bit offset
3025 to the base class information within the derived class, a reference to
3026 the type for the base class, and a terminating semicolon.
3027
3028 A typical example, with two base classes, would be "!2,020,19;0264,21;".
3029 ^^ ^ ^ ^ ^ ^ ^
3030 Baseclass information marker __________________|| | | | | | |
3031 Number of baseclasses __________________________| | | | | | |
3032 Visibility specifiers (2) ________________________| | | | | |
3033 Offset in bits from start of class _________________| | | | |
3034 Type number for base class ___________________________| | | |
3035 Visibility specifiers (2) _______________________________| | |
3036 Offset in bits from start of class ________________________| |
3037 Type number of base class ____________________________________|
3038
3039 Return 1 for success, 0 for (error-type-inducing) failure. */
3040
3041
3042
3043 static int
3044 read_baseclasses (struct stab_field_info *fip, const char **pp,
3045 struct type *type, struct objfile *objfile)
3046 {
3047 int i;
3048 struct stabs_nextfield *newobj;
3049
3050 if (**pp != '!')
3051 {
3052 return 1;
3053 }
3054 else
3055 {
3056 /* Skip the '!' baseclass information marker. */
3057 (*pp)++;
3058 }
3059
3060 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3061 {
3062 int nbits;
3063
3064 TYPE_N_BASECLASSES (type) = read_huge_number (pp, ',', &nbits, 0);
3065 if (nbits != 0)
3066 return 0;
3067 }
3068
3069 #if 0
3070 /* Some stupid compilers have trouble with the following, so break
3071 it up into simpler expressions. */
3072 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
3073 TYPE_ZALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
3074 #else
3075 {
3076 int num_bytes = B_BYTES (TYPE_N_BASECLASSES (type));
3077 char *pointer;
3078
3079 pointer = (char *) TYPE_ZALLOC (type, num_bytes);
3080 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
3081 }
3082 #endif /* 0 */
3083
3084 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
3085 {
3086 newobj = OBSTACK_ZALLOC (&fip->obstack, struct stabs_nextfield);
3087
3088 newobj->next = fip->list;
3089 fip->list = newobj;
3090 newobj->field.set_bitsize (0); /* This should be an unpacked
3091 field! */
3092
3093 STABS_CONTINUE (pp, objfile);
3094 switch (**pp)
3095 {
3096 case '0':
3097 /* Nothing to do. */
3098 break;
3099 case '1':
3100 SET_TYPE_FIELD_VIRTUAL (type, i);
3101 break;
3102 default:
3103 /* Unknown character. Complain and treat it as non-virtual. */
3104 {
3105 complaint (_("Unknown virtual character `%c' for baseclass"),
3106 **pp);
3107 }
3108 }
3109 ++(*pp);
3110
3111 newobj->visibility = *(*pp)++;
3112 switch (newobj->visibility)
3113 {
3114 case VISIBILITY_PRIVATE:
3115 case VISIBILITY_PROTECTED:
3116 case VISIBILITY_PUBLIC:
3117 break;
3118 default:
3119 /* Bad visibility format. Complain and treat it as
3120 public. */
3121 {
3122 complaint (_("Unknown visibility `%c' for baseclass"),
3123 newobj->visibility);
3124 newobj->visibility = VISIBILITY_PUBLIC;
3125 }
3126 }
3127
3128 {
3129 int nbits;
3130
3131 /* The remaining value is the bit offset of the portion of the object
3132 corresponding to this baseclass. Always zero in the absence of
3133 multiple inheritance. */
3134
3135 newobj->field.set_loc_bitpos (read_huge_number (pp, ',', &nbits, 0));
3136 if (nbits != 0)
3137 return 0;
3138 }
3139
3140 /* The last piece of baseclass information is the type of the
3141 base class. Read it, and remember it's type name as this
3142 field's name. */
3143
3144 newobj->field.set_type (read_type (pp, objfile));
3145 newobj->field.set_name (newobj->field.type ()->name ());
3146
3147 /* Skip trailing ';' and bump count of number of fields seen. */
3148 if (**pp == ';')
3149 (*pp)++;
3150 else
3151 return 0;
3152 }
3153 return 1;
3154 }
3155
3156 /* The tail end of stabs for C++ classes that contain a virtual function
3157 pointer contains a tilde, a %, and a type number.
3158 The type number refers to the base class (possibly this class itself) which
3159 contains the vtable pointer for the current class.
3160
3161 This function is called when we have parsed all the method declarations,
3162 so we can look for the vptr base class info. */
3163
3164 static int
3165 read_tilde_fields (struct stab_field_info *fip, const char **pp,
3166 struct type *type, struct objfile *objfile)
3167 {
3168 const char *p;
3169
3170 STABS_CONTINUE (pp, objfile);
3171
3172 /* If we are positioned at a ';', then skip it. */
3173 if (**pp == ';')
3174 {
3175 (*pp)++;
3176 }
3177
3178 if (**pp == '~')
3179 {
3180 (*pp)++;
3181
3182 if (**pp == '=' || **pp == '+' || **pp == '-')
3183 {
3184 /* Obsolete flags that used to indicate the presence
3185 of constructors and/or destructors. */
3186 (*pp)++;
3187 }
3188
3189 /* Read either a '%' or the final ';'. */
3190 if (*(*pp)++ == '%')
3191 {
3192 /* The next number is the type number of the base class
3193 (possibly our own class) which supplies the vtable for
3194 this class. Parse it out, and search that class to find
3195 its vtable pointer, and install those into TYPE_VPTR_BASETYPE
3196 and TYPE_VPTR_FIELDNO. */
3197
3198 struct type *t;
3199 int i;
3200
3201 t = read_type (pp, objfile);
3202 p = (*pp)++;
3203 while (*p != '\0' && *p != ';')
3204 {
3205 p++;
3206 }
3207 if (*p == '\0')
3208 {
3209 /* Premature end of symbol. */
3210 return 0;
3211 }
3212
3213 set_type_vptr_basetype (type, t);
3214 if (type == t) /* Our own class provides vtbl ptr. */
3215 {
3216 for (i = t->num_fields () - 1;
3217 i >= TYPE_N_BASECLASSES (t);
3218 --i)
3219 {
3220 const char *name = t->field (i).name ();
3221
3222 if (!strncmp (name, vptr_name, sizeof (vptr_name) - 2)
3223 && is_cplus_marker (name[sizeof (vptr_name) - 2]))
3224 {
3225 set_type_vptr_fieldno (type, i);
3226 goto gotit;
3227 }
3228 }
3229 /* Virtual function table field not found. */
3230 complaint (_("virtual function table pointer "
3231 "not found when defining class `%s'"),
3232 type->name ());
3233 return 0;
3234 }
3235 else
3236 {
3237 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
3238 }
3239
3240 gotit:
3241 *pp = p + 1;
3242 }
3243 }
3244 return 1;
3245 }
3246
3247 static int
3248 attach_fn_fields_to_type (struct stab_field_info *fip, struct type *type)
3249 {
3250 int n;
3251
3252 for (n = TYPE_NFN_FIELDS (type);
3253 fip->fnlist != NULL;
3254 fip->fnlist = fip->fnlist->next)
3255 {
3256 --n; /* Circumvent Sun3 compiler bug. */
3257 TYPE_FN_FIELDLISTS (type)[n] = fip->fnlist->fn_fieldlist;
3258 }
3259 return 1;
3260 }
3261
3262 /* Create the vector of fields, and record how big it is.
3263 We need this info to record proper virtual function table information
3264 for this class's virtual functions. */
3265
3266 static int
3267 attach_fields_to_type (struct stab_field_info *fip, struct type *type,
3268 struct objfile *objfile)
3269 {
3270 int nfields = 0;
3271 int non_public_fields = 0;
3272 struct stabs_nextfield *scan;
3273
3274 /* Count up the number of fields that we have, as well as taking note of
3275 whether or not there are any non-public fields, which requires us to
3276 allocate and build the private_field_bits and protected_field_bits
3277 bitfields. */
3278
3279 for (scan = fip->list; scan != NULL; scan = scan->next)
3280 {
3281 nfields++;
3282 if (scan->visibility != VISIBILITY_PUBLIC)
3283 {
3284 non_public_fields++;
3285 }
3286 }
3287
3288 /* Now we know how many fields there are, and whether or not there are any
3289 non-public fields. Record the field count, allocate space for the
3290 array of fields, and create blank visibility bitfields if necessary. */
3291
3292 type->alloc_fields (nfields);
3293
3294 if (non_public_fields)
3295 {
3296 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3297
3298 TYPE_FIELD_PRIVATE_BITS (type) =
3299 (B_TYPE *) TYPE_ZALLOC (type, B_BYTES (nfields));
3300
3301 TYPE_FIELD_PROTECTED_BITS (type) =
3302 (B_TYPE *) TYPE_ZALLOC (type, B_BYTES (nfields));
3303
3304 TYPE_FIELD_IGNORE_BITS (type) =
3305 (B_TYPE *) TYPE_ZALLOC (type, B_BYTES (nfields));
3306 }
3307
3308 /* Copy the saved-up fields into the field vector. Start from the
3309 head of the list, adding to the tail of the field array, so that
3310 they end up in the same order in the array in which they were
3311 added to the list. */
3312
3313 while (nfields-- > 0)
3314 {
3315 type->field (nfields) = fip->list->field;
3316 switch (fip->list->visibility)
3317 {
3318 case VISIBILITY_PRIVATE:
3319 SET_TYPE_FIELD_PRIVATE (type, nfields);
3320 break;
3321
3322 case VISIBILITY_PROTECTED:
3323 SET_TYPE_FIELD_PROTECTED (type, nfields);
3324 break;
3325
3326 case VISIBILITY_IGNORE:
3327 SET_TYPE_FIELD_IGNORE (type, nfields);
3328 break;
3329
3330 case VISIBILITY_PUBLIC:
3331 break;
3332
3333 default:
3334 /* Unknown visibility. Complain and treat it as public. */
3335 {
3336 complaint (_("Unknown visibility `%c' for field"),
3337 fip->list->visibility);
3338 }
3339 break;
3340 }
3341 fip->list = fip->list->next;
3342 }
3343 return 1;
3344 }
3345
3346
3347 /* Complain that the compiler has emitted more than one definition for the
3348 structure type TYPE. */
3349 static void
3350 complain_about_struct_wipeout (struct type *type)
3351 {
3352 const char *name = "";
3353 const char *kind = "";
3354
3355 if (type->name ())
3356 {
3357 name = type->name ();
3358 switch (type->code ())
3359 {
3360 case TYPE_CODE_STRUCT: kind = "struct "; break;
3361 case TYPE_CODE_UNION: kind = "union "; break;
3362 case TYPE_CODE_ENUM: kind = "enum "; break;
3363 default: kind = "";
3364 }
3365 }
3366 else
3367 {
3368 name = "<unknown>";
3369 kind = "";
3370 }
3371
3372 complaint (_("struct/union type gets multiply defined: %s%s"), kind, name);
3373 }
3374
3375 /* Set the length for all variants of a same main_type, which are
3376 connected in the closed chain.
3377
3378 This is something that needs to be done when a type is defined *after*
3379 some cross references to this type have already been read. Consider
3380 for instance the following scenario where we have the following two
3381 stabs entries:
3382
3383 .stabs "t:p(0,21)=*(0,22)=k(0,23)=xsdummy:",160,0,28,-24
3384 .stabs "dummy:T(0,23)=s16x:(0,1),0,3[...]"
3385
3386 A stubbed version of type dummy is created while processing the first
3387 stabs entry. The length of that type is initially set to zero, since
3388 it is unknown at this point. Also, a "constant" variation of type
3389 "dummy" is created as well (this is the "(0,22)=k(0,23)" section of
3390 the stabs line).
3391
3392 The second stabs entry allows us to replace the stubbed definition
3393 with the real definition. However, we still need to adjust the length
3394 of the "constant" variation of that type, as its length was left
3395 untouched during the main type replacement... */
3396
3397 static void
3398 set_length_in_type_chain (struct type *type)
3399 {
3400 struct type *ntype = TYPE_CHAIN (type);
3401
3402 while (ntype != type)
3403 {
3404 if (ntype->length () == 0)
3405 ntype->set_length (type->length ());
3406 else
3407 complain_about_struct_wipeout (ntype);
3408 ntype = TYPE_CHAIN (ntype);
3409 }
3410 }
3411
3412 /* Read the description of a structure (or union type) and return an object
3413 describing the type.
3414
3415 PP points to a character pointer that points to the next unconsumed token
3416 in the stabs string. For example, given stabs "A:T4=s4a:1,0,32;;",
3417 *PP will point to "4a:1,0,32;;".
3418
3419 TYPE points to an incomplete type that needs to be filled in.
3420
3421 OBJFILE points to the current objfile from which the stabs information is
3422 being read. (Note that it is redundant in that TYPE also contains a pointer
3423 to this same objfile, so it might be a good idea to eliminate it. FIXME).
3424 */
3425
3426 static struct type *
3427 read_struct_type (const char **pp, struct type *type, enum type_code type_code,
3428 struct objfile *objfile)
3429 {
3430 struct stab_field_info fi;
3431
3432 /* When describing struct/union/class types in stabs, G++ always drops
3433 all qualifications from the name. So if you've got:
3434 struct A { ... struct B { ... }; ... };
3435 then G++ will emit stabs for `struct A::B' that call it simply
3436 `struct B'. Obviously, if you've got a real top-level definition for
3437 `struct B', or other nested definitions, this is going to cause
3438 problems.
3439
3440 Obviously, GDB can't fix this by itself, but it can at least avoid
3441 scribbling on existing structure type objects when new definitions
3442 appear. */
3443 if (! (type->code () == TYPE_CODE_UNDEF
3444 || type->is_stub ()))
3445 {
3446 complain_about_struct_wipeout (type);
3447
3448 /* It's probably best to return the type unchanged. */
3449 return type;
3450 }
3451
3452 INIT_CPLUS_SPECIFIC (type);
3453 type->set_code (type_code);
3454 type->set_is_stub (false);
3455
3456 /* First comes the total size in bytes. */
3457
3458 {
3459 int nbits;
3460
3461 type->set_length (read_huge_number (pp, 0, &nbits, 0));
3462 if (nbits != 0)
3463 return error_type (pp, objfile);
3464 set_length_in_type_chain (type);
3465 }
3466
3467 /* Now read the baseclasses, if any, read the regular C struct or C++
3468 class member fields, attach the fields to the type, read the C++
3469 member functions, attach them to the type, and then read any tilde
3470 field (baseclass specifier for the class holding the main vtable). */
3471
3472 if (!read_baseclasses (&fi, pp, type, objfile)
3473 || !read_struct_fields (&fi, pp, type, objfile)
3474 || !attach_fields_to_type (&fi, type, objfile)
3475 || !read_member_functions (&fi, pp, type, objfile)
3476 || !attach_fn_fields_to_type (&fi, type)
3477 || !read_tilde_fields (&fi, pp, type, objfile))
3478 {
3479 type = error_type (pp, objfile);
3480 }
3481
3482 return (type);
3483 }
3484
3485 /* Read a definition of an array type,
3486 and create and return a suitable type object.
3487 Also creates a range type which represents the bounds of that
3488 array. */
3489
3490 static struct type *
3491 read_array_type (const char **pp, struct type *type,
3492 struct objfile *objfile)
3493 {
3494 struct type *index_type, *element_type, *range_type;
3495 int lower, upper;
3496 int adjustable = 0;
3497 int nbits;
3498
3499 /* Format of an array type:
3500 "ar<index type>;lower;upper;<array_contents_type>".
3501 OS9000: "arlower,upper;<array_contents_type>".
3502
3503 Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
3504 for these, produce a type like float[][]. */
3505
3506 {
3507 index_type = read_type (pp, objfile);
3508 if (**pp != ';')
3509 /* Improper format of array type decl. */
3510 return error_type (pp, objfile);
3511 ++*pp;
3512 }
3513
3514 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
3515 {
3516 (*pp)++;
3517 adjustable = 1;
3518 }
3519 lower = read_huge_number (pp, ';', &nbits, 0);
3520
3521 if (nbits != 0)
3522 return error_type (pp, objfile);
3523
3524 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
3525 {
3526 (*pp)++;
3527 adjustable = 1;
3528 }
3529 upper = read_huge_number (pp, ';', &nbits, 0);
3530 if (nbits != 0)
3531 return error_type (pp, objfile);
3532
3533 element_type = read_type (pp, objfile);
3534
3535 if (adjustable)
3536 {
3537 lower = 0;
3538 upper = -1;
3539 }
3540
3541 type_allocator alloc (objfile, get_current_subfile ()->language);
3542 range_type =
3543 create_static_range_type (alloc, index_type, lower, upper);
3544 type_allocator smash_alloc (type, type_allocator::SMASH);
3545 type = create_array_type (smash_alloc, element_type, range_type);
3546
3547 return type;
3548 }
3549
3550
3551 /* Read a definition of an enumeration type,
3552 and create and return a suitable type object.
3553 Also defines the symbols that represent the values of the type. */
3554
3555 static struct type *
3556 read_enum_type (const char **pp, struct type *type,
3557 struct objfile *objfile)
3558 {
3559 struct gdbarch *gdbarch = objfile->arch ();
3560 const char *p;
3561 char *name;
3562 long n;
3563 struct symbol *sym;
3564 int nsyms = 0;
3565 struct pending **symlist;
3566 struct pending *osyms, *syms;
3567 int o_nsyms;
3568 int nbits;
3569 int unsigned_enum = 1;
3570
3571 #if 0
3572 /* FIXME! The stabs produced by Sun CC merrily define things that ought
3573 to be file-scope, between N_FN entries, using N_LSYM. What's a mother
3574 to do? For now, force all enum values to file scope. */
3575 if (within_function)
3576 symlist = get_local_symbols ();
3577 else
3578 #endif
3579 symlist = get_file_symbols ();
3580 osyms = *symlist;
3581 o_nsyms = osyms ? osyms->nsyms : 0;
3582
3583 /* The aix4 compiler emits an extra field before the enum members;
3584 my guess is it's a type of some sort. Just ignore it. */
3585 if (**pp == '-')
3586 {
3587 /* Skip over the type. */
3588 while (**pp != ':')
3589 (*pp)++;
3590
3591 /* Skip over the colon. */
3592 (*pp)++;
3593 }
3594
3595 /* Read the value-names and their values.
3596 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
3597 A semicolon or comma instead of a NAME means the end. */
3598 while (**pp && **pp != ';' && **pp != ',')
3599 {
3600 STABS_CONTINUE (pp, objfile);
3601 p = *pp;
3602 while (*p != ':')
3603 p++;
3604 name = obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp);
3605 *pp = p + 1;
3606 n = read_huge_number (pp, ',', &nbits, 0);
3607 if (nbits != 0)
3608 return error_type (pp, objfile);
3609
3610 sym = new (&objfile->objfile_obstack) symbol;
3611 sym->set_linkage_name (name);
3612 sym->set_language (get_current_subfile ()->language,
3613 &objfile->objfile_obstack);
3614 sym->set_aclass_index (LOC_CONST);
3615 sym->set_domain (VAR_DOMAIN);
3616 sym->set_value_longest (n);
3617 if (n < 0)
3618 unsigned_enum = 0;
3619 add_symbol_to_list (sym, symlist);
3620 nsyms++;
3621 }
3622
3623 if (**pp == ';')
3624 (*pp)++; /* Skip the semicolon. */
3625
3626 /* Now fill in the fields of the type-structure. */
3627
3628 type->set_length (gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT);
3629 set_length_in_type_chain (type);
3630 type->set_code (TYPE_CODE_ENUM);
3631 type->set_is_stub (false);
3632 if (unsigned_enum)
3633 type->set_is_unsigned (true);
3634 type->alloc_fields (nsyms);
3635
3636 /* Find the symbols for the values and put them into the type.
3637 The symbols can be found in the symlist that we put them on
3638 to cause them to be defined. osyms contains the old value
3639 of that symlist; everything up to there was defined by us. */
3640 /* Note that we preserve the order of the enum constants, so
3641 that in something like "enum {FOO, LAST_THING=FOO}" we print
3642 FOO, not LAST_THING. */
3643
3644 for (syms = *symlist, n = nsyms - 1; syms; syms = syms->next)
3645 {
3646 int last = syms == osyms ? o_nsyms : 0;
3647 int j = syms->nsyms;
3648
3649 for (; --j >= last; --n)
3650 {
3651 struct symbol *xsym = syms->symbol[j];
3652
3653 xsym->set_type (type);
3654 type->field (n).set_name (xsym->linkage_name ());
3655 type->field (n).set_loc_enumval (xsym->value_longest ());
3656 type->field (n).set_bitsize (0);
3657 }
3658 if (syms == osyms)
3659 break;
3660 }
3661
3662 return type;
3663 }
3664
3665 /* Sun's ACC uses a somewhat saner method for specifying the builtin
3666 typedefs in every file (for int, long, etc):
3667
3668 type = b <signed> <width> <format type>; <offset>; <nbits>
3669 signed = u or s.
3670 optional format type = c or b for char or boolean.
3671 offset = offset from high order bit to start bit of type.
3672 width is # bytes in object of this type, nbits is # bits in type.
3673
3674 The width/offset stuff appears to be for small objects stored in
3675 larger ones (e.g. `shorts' in `int' registers). We ignore it for now,
3676 FIXME. */
3677
3678 static struct type *
3679 read_sun_builtin_type (const char **pp, int typenums[2], struct objfile *objfile)
3680 {
3681 int type_bits;
3682 int nbits;
3683 int unsigned_type;
3684 int boolean_type = 0;
3685
3686 switch (**pp)
3687 {
3688 case 's':
3689 unsigned_type = 0;
3690 break;
3691 case 'u':
3692 unsigned_type = 1;
3693 break;
3694 default:
3695 return error_type (pp, objfile);
3696 }
3697 (*pp)++;
3698
3699 /* For some odd reason, all forms of char put a c here. This is strange
3700 because no other type has this honor. We can safely ignore this because
3701 we actually determine 'char'acterness by the number of bits specified in
3702 the descriptor.
3703 Boolean forms, e.g Fortran logical*X, put a b here. */
3704
3705 if (**pp == 'c')
3706 (*pp)++;
3707 else if (**pp == 'b')
3708 {
3709 boolean_type = 1;
3710 (*pp)++;
3711 }
3712
3713 /* The first number appears to be the number of bytes occupied
3714 by this type, except that unsigned short is 4 instead of 2.
3715 Since this information is redundant with the third number,
3716 we will ignore it. */
3717 read_huge_number (pp, ';', &nbits, 0);
3718 if (nbits != 0)
3719 return error_type (pp, objfile);
3720
3721 /* The second number is always 0, so ignore it too. */
3722 read_huge_number (pp, ';', &nbits, 0);
3723 if (nbits != 0)
3724 return error_type (pp, objfile);
3725
3726 /* The third number is the number of bits for this type. */
3727 type_bits = read_huge_number (pp, 0, &nbits, 0);
3728 if (nbits != 0)
3729 return error_type (pp, objfile);
3730 /* The type *should* end with a semicolon. If it are embedded
3731 in a larger type the semicolon may be the only way to know where
3732 the type ends. If this type is at the end of the stabstring we
3733 can deal with the omitted semicolon (but we don't have to like
3734 it). Don't bother to complain(), Sun's compiler omits the semicolon
3735 for "void". */
3736 if (**pp == ';')
3737 ++(*pp);
3738
3739 type_allocator alloc (objfile, get_current_subfile ()->language);
3740 if (type_bits == 0)
3741 {
3742 struct type *type = alloc.new_type (TYPE_CODE_VOID,
3743 TARGET_CHAR_BIT, nullptr);
3744 if (unsigned_type)
3745 type->set_is_unsigned (true);
3746
3747 return type;
3748 }
3749
3750 if (boolean_type)
3751 return init_boolean_type (alloc, type_bits, unsigned_type, NULL);
3752 else
3753 return init_integer_type (alloc, type_bits, unsigned_type, NULL);
3754 }
3755
3756 static struct type *
3757 read_sun_floating_type (const char **pp, int typenums[2],
3758 struct objfile *objfile)
3759 {
3760 int nbits;
3761 int details;
3762 int nbytes;
3763 struct type *rettype;
3764
3765 /* The first number has more details about the type, for example
3766 FN_COMPLEX. */
3767 details = read_huge_number (pp, ';', &nbits, 0);
3768 if (nbits != 0)
3769 return error_type (pp, objfile);
3770
3771 /* The second number is the number of bytes occupied by this type. */
3772 nbytes = read_huge_number (pp, ';', &nbits, 0);
3773 if (nbits != 0)
3774 return error_type (pp, objfile);
3775
3776 nbits = nbytes * TARGET_CHAR_BIT;
3777
3778 if (details == NF_COMPLEX || details == NF_COMPLEX16
3779 || details == NF_COMPLEX32)
3780 {
3781 rettype = dbx_init_float_type (objfile, nbits / 2);
3782 return init_complex_type (NULL, rettype);
3783 }
3784
3785 return dbx_init_float_type (objfile, nbits);
3786 }
3787
3788 /* Read a number from the string pointed to by *PP.
3789 The value of *PP is advanced over the number.
3790 If END is nonzero, the character that ends the
3791 number must match END, or an error happens;
3792 and that character is skipped if it does match.
3793 If END is zero, *PP is left pointing to that character.
3794
3795 If TWOS_COMPLEMENT_BITS is set to a strictly positive value and if
3796 the number is represented in an octal representation, assume that
3797 it is represented in a 2's complement representation with a size of
3798 TWOS_COMPLEMENT_BITS.
3799
3800 If the number fits in a long, set *BITS to 0 and return the value.
3801 If not, set *BITS to be the number of bits in the number and return 0.
3802
3803 If encounter garbage, set *BITS to -1 and return 0. */
3804
3805 static long
3806 read_huge_number (const char **pp, int end, int *bits,
3807 int twos_complement_bits)
3808 {
3809 const char *p = *pp;
3810 int sign = 1;
3811 int sign_bit = 0;
3812 long n = 0;
3813 int radix = 10;
3814 char overflow = 0;
3815 int nbits = 0;
3816 int c;
3817 long upper_limit;
3818 int twos_complement_representation = 0;
3819
3820 if (*p == '-')
3821 {
3822 sign = -1;
3823 p++;
3824 }
3825
3826 /* Leading zero means octal. GCC uses this to output values larger
3827 than an int (because that would be hard in decimal). */
3828 if (*p == '0')
3829 {
3830 radix = 8;
3831 p++;
3832 }
3833
3834 /* Skip extra zeros. */
3835 while (*p == '0')
3836 p++;
3837
3838 if (sign > 0 && radix == 8 && twos_complement_bits > 0)
3839 {
3840 /* Octal, possibly signed. Check if we have enough chars for a
3841 negative number. */
3842
3843 size_t len;
3844 const char *p1 = p;
3845
3846 while ((c = *p1) >= '0' && c < '8')
3847 p1++;
3848
3849 len = p1 - p;
3850 if (len > twos_complement_bits / 3
3851 || (twos_complement_bits % 3 == 0
3852 && len == twos_complement_bits / 3))
3853 {
3854 /* Ok, we have enough characters for a signed value, check
3855 for signedness by testing if the sign bit is set. */
3856 sign_bit = (twos_complement_bits % 3 + 2) % 3;
3857 c = *p - '0';
3858 if (c & (1 << sign_bit))
3859 {
3860 /* Definitely signed. */
3861 twos_complement_representation = 1;
3862 sign = -1;
3863 }
3864 }
3865 }
3866
3867 upper_limit = LONG_MAX / radix;
3868
3869 while ((c = *p++) >= '0' && c < ('0' + radix))
3870 {
3871 if (n <= upper_limit)
3872 {
3873 if (twos_complement_representation)
3874 {
3875 /* Octal, signed, twos complement representation. In
3876 this case, n is the corresponding absolute value. */
3877 if (n == 0)
3878 {
3879 long sn = c - '0' - ((2 * (c - '0')) | (2 << sign_bit));
3880
3881 n = -sn;
3882 }
3883 else
3884 {
3885 n *= radix;
3886 n -= c - '0';
3887 }
3888 }
3889 else
3890 {
3891 /* unsigned representation */
3892 n *= radix;
3893 n += c - '0'; /* FIXME this overflows anyway. */
3894 }
3895 }
3896 else
3897 overflow = 1;
3898
3899 /* This depends on large values being output in octal, which is
3900 what GCC does. */
3901 if (radix == 8)
3902 {
3903 if (nbits == 0)
3904 {
3905 if (c == '0')
3906 /* Ignore leading zeroes. */
3907 ;
3908 else if (c == '1')
3909 nbits = 1;
3910 else if (c == '2' || c == '3')
3911 nbits = 2;
3912 else
3913 nbits = 3;
3914 }
3915 else
3916 nbits += 3;
3917 }
3918 }
3919 if (end)
3920 {
3921 if (c && c != end)
3922 {
3923 if (bits != NULL)
3924 *bits = -1;
3925 return 0;
3926 }
3927 }
3928 else
3929 --p;
3930
3931 if (radix == 8 && twos_complement_bits > 0 && nbits > twos_complement_bits)
3932 {
3933 /* We were supposed to parse a number with maximum
3934 TWOS_COMPLEMENT_BITS bits, but something went wrong. */
3935 if (bits != NULL)
3936 *bits = -1;
3937 return 0;
3938 }
3939
3940 *pp = p;
3941 if (overflow)
3942 {
3943 if (nbits == 0)
3944 {
3945 /* Large decimal constants are an error (because it is hard to
3946 count how many bits are in them). */
3947 if (bits != NULL)
3948 *bits = -1;
3949 return 0;
3950 }
3951
3952 /* -0x7f is the same as 0x80. So deal with it by adding one to
3953 the number of bits. Two's complement represention octals
3954 can't have a '-' in front. */
3955 if (sign == -1 && !twos_complement_representation)
3956 ++nbits;
3957 if (bits)
3958 *bits = nbits;
3959 }
3960 else
3961 {
3962 if (bits)
3963 *bits = 0;
3964 return n * sign;
3965 }
3966 /* It's *BITS which has the interesting information. */
3967 return 0;
3968 }
3969
3970 static struct type *
3971 read_range_type (const char **pp, int typenums[2], int type_size,
3972 struct objfile *objfile)
3973 {
3974 struct gdbarch *gdbarch = objfile->arch ();
3975 const char *orig_pp = *pp;
3976 int rangenums[2];
3977 long n2, n3;
3978 int n2bits, n3bits;
3979 int self_subrange;
3980 struct type *result_type;
3981 struct type *index_type = NULL;
3982
3983 /* First comes a type we are a subrange of.
3984 In C it is usually 0, 1 or the type being defined. */
3985 if (read_type_number (pp, rangenums) != 0)
3986 return error_type (pp, objfile);
3987 self_subrange = (rangenums[0] == typenums[0] &&
3988 rangenums[1] == typenums[1]);
3989
3990 if (**pp == '=')
3991 {
3992 *pp = orig_pp;
3993 index_type = read_type (pp, objfile);
3994 }
3995
3996 /* A semicolon should now follow; skip it. */
3997 if (**pp == ';')
3998 (*pp)++;
3999
4000 /* The remaining two operands are usually lower and upper bounds
4001 of the range. But in some special cases they mean something else. */
4002 n2 = read_huge_number (pp, ';', &n2bits, type_size);
4003 n3 = read_huge_number (pp, ';', &n3bits, type_size);
4004
4005 if (n2bits == -1 || n3bits == -1)
4006 return error_type (pp, objfile);
4007
4008 type_allocator alloc (objfile, get_current_subfile ()->language);
4009
4010 if (index_type)
4011 goto handle_true_range;
4012
4013 /* If limits are huge, must be large integral type. */
4014 if (n2bits != 0 || n3bits != 0)
4015 {
4016 char got_signed = 0;
4017 char got_unsigned = 0;
4018 /* Number of bits in the type. */
4019 int nbits = 0;
4020
4021 /* If a type size attribute has been specified, the bounds of
4022 the range should fit in this size. If the lower bounds needs
4023 more bits than the upper bound, then the type is signed. */
4024 if (n2bits <= type_size && n3bits <= type_size)
4025 {
4026 if (n2bits == type_size && n2bits > n3bits)
4027 got_signed = 1;
4028 else
4029 got_unsigned = 1;
4030 nbits = type_size;
4031 }
4032 /* Range from 0 to <large number> is an unsigned large integral type. */
4033 else if ((n2bits == 0 && n2 == 0) && n3bits != 0)
4034 {
4035 got_unsigned = 1;
4036 nbits = n3bits;
4037 }
4038 /* Range from <large number> to <large number>-1 is a large signed
4039 integral type. Take care of the case where <large number> doesn't
4040 fit in a long but <large number>-1 does. */
4041 else if ((n2bits != 0 && n3bits != 0 && n2bits == n3bits + 1)
4042 || (n2bits != 0 && n3bits == 0
4043 && (n2bits == sizeof (long) * HOST_CHAR_BIT)
4044 && n3 == LONG_MAX))
4045 {
4046 got_signed = 1;
4047 nbits = n2bits;
4048 }
4049
4050 if (got_signed || got_unsigned)
4051 return init_integer_type (alloc, nbits, got_unsigned, NULL);
4052 else
4053 return error_type (pp, objfile);
4054 }
4055
4056 /* A type defined as a subrange of itself, with bounds both 0, is void. */
4057 if (self_subrange && n2 == 0 && n3 == 0)
4058 return alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, nullptr);
4059
4060 /* If n3 is zero and n2 is positive, we want a floating type, and n2
4061 is the width in bytes.
4062
4063 Fortran programs appear to use this for complex types also. To
4064 distinguish between floats and complex, g77 (and others?) seem
4065 to use self-subranges for the complexes, and subranges of int for
4066 the floats.
4067
4068 Also note that for complexes, g77 sets n2 to the size of one of
4069 the member floats, not the whole complex beast. My guess is that
4070 this was to work well with pre-COMPLEX versions of gdb. */
4071
4072 if (n3 == 0 && n2 > 0)
4073 {
4074 struct type *float_type
4075 = dbx_init_float_type (objfile, n2 * TARGET_CHAR_BIT);
4076
4077 if (self_subrange)
4078 return init_complex_type (NULL, float_type);
4079 else
4080 return float_type;
4081 }
4082
4083 /* If the upper bound is -1, it must really be an unsigned integral. */
4084
4085 else if (n2 == 0 && n3 == -1)
4086 {
4087 int bits = type_size;
4088
4089 if (bits <= 0)
4090 {
4091 /* We don't know its size. It is unsigned int or unsigned
4092 long. GCC 2.3.3 uses this for long long too, but that is
4093 just a GDB 3.5 compatibility hack. */
4094 bits = gdbarch_int_bit (gdbarch);
4095 }
4096
4097 return init_integer_type (alloc, bits, 1, NULL);
4098 }
4099
4100 /* Special case: char is defined (Who knows why) as a subrange of
4101 itself with range 0-127. */
4102 else if (self_subrange && n2 == 0 && n3 == 127)
4103 {
4104 struct type *type = init_integer_type (alloc, TARGET_CHAR_BIT,
4105 0, NULL);
4106 type->set_has_no_signedness (true);
4107 return type;
4108 }
4109 /* We used to do this only for subrange of self or subrange of int. */
4110 else if (n2 == 0)
4111 {
4112 /* -1 is used for the upper bound of (4 byte) "unsigned int" and
4113 "unsigned long", and we already checked for that,
4114 so don't need to test for it here. */
4115
4116 if (n3 < 0)
4117 /* n3 actually gives the size. */
4118 return init_integer_type (alloc, -n3 * TARGET_CHAR_BIT, 1, NULL);
4119
4120 /* Is n3 == 2**(8n)-1 for some integer n? Then it's an
4121 unsigned n-byte integer. But do require n to be a power of
4122 two; we don't want 3- and 5-byte integers flying around. */
4123 {
4124 int bytes;
4125 unsigned long bits;
4126
4127 bits = n3;
4128 for (bytes = 0; (bits & 0xff) == 0xff; bytes++)
4129 bits >>= 8;
4130 if (bits == 0
4131 && ((bytes - 1) & bytes) == 0) /* "bytes is a power of two" */
4132 return init_integer_type (alloc, bytes * TARGET_CHAR_BIT, 1, NULL);
4133 }
4134 }
4135 /* I think this is for Convex "long long". Since I don't know whether
4136 Convex sets self_subrange, I also accept that particular size regardless
4137 of self_subrange. */
4138 else if (n3 == 0 && n2 < 0
4139 && (self_subrange
4140 || n2 == -gdbarch_long_long_bit
4141 (gdbarch) / TARGET_CHAR_BIT))
4142 return init_integer_type (alloc, -n2 * TARGET_CHAR_BIT, 0, NULL);
4143 else if (n2 == -n3 - 1)
4144 {
4145 if (n3 == 0x7f)
4146 return init_integer_type (alloc, 8, 0, NULL);
4147 if (n3 == 0x7fff)
4148 return init_integer_type (alloc, 16, 0, NULL);
4149 if (n3 == 0x7fffffff)
4150 return init_integer_type (alloc, 32, 0, NULL);
4151 }
4152
4153 /* We have a real range type on our hands. Allocate space and
4154 return a real pointer. */
4155 handle_true_range:
4156
4157 if (self_subrange)
4158 index_type = builtin_type (objfile)->builtin_int;
4159 else
4160 index_type = *dbx_lookup_type (rangenums, objfile);
4161 if (index_type == NULL)
4162 {
4163 /* Does this actually ever happen? Is that why we are worrying
4164 about dealing with it rather than just calling error_type? */
4165
4166 complaint (_("base type %d of range type is not defined"), rangenums[1]);
4167
4168 index_type = builtin_type (objfile)->builtin_int;
4169 }
4170
4171 result_type
4172 = create_static_range_type (alloc, index_type, n2, n3);
4173 return (result_type);
4174 }
4175
4176 /* Read in an argument list. This is a list of types, separated by commas
4177 and terminated with END. Return the list of types read in, or NULL
4178 if there is an error. */
4179
4180 static struct field *
4181 read_args (const char **pp, int end, struct objfile *objfile, int *nargsp,
4182 int *varargsp)
4183 {
4184 /* FIXME! Remove this arbitrary limit! */
4185 struct type *types[1024]; /* Allow for fns of 1023 parameters. */
4186 int n = 0, i;
4187 struct field *rval;
4188
4189 while (**pp != end)
4190 {
4191 if (**pp != ',')
4192 /* Invalid argument list: no ','. */
4193 return NULL;
4194 (*pp)++;
4195 STABS_CONTINUE (pp, objfile);
4196 types[n++] = read_type (pp, objfile);
4197 }
4198 (*pp)++; /* get past `end' (the ':' character). */
4199
4200 if (n == 0)
4201 {
4202 /* We should read at least the THIS parameter here. Some broken stabs
4203 output contained `(0,41),(0,42)=@s8;-16;,(0,43),(0,1);' where should
4204 have been present ";-16,(0,43)" reference instead. This way the
4205 excessive ";" marker prematurely stops the parameters parsing. */
4206
4207 complaint (_("Invalid (empty) method arguments"));
4208 *varargsp = 0;
4209 }
4210 else if (types[n - 1]->code () != TYPE_CODE_VOID)
4211 *varargsp = 1;
4212 else
4213 {
4214 n--;
4215 *varargsp = 0;
4216 }
4217
4218 rval = XCNEWVEC (struct field, n);
4219 for (i = 0; i < n; i++)
4220 rval[i].set_type (types[i]);
4221 *nargsp = n;
4222 return rval;
4223 }
4224 \f
4225 /* Common block handling. */
4226
4227 /* List of symbols declared since the last BCOMM. This list is a tail
4228 of local_symbols. When ECOMM is seen, the symbols on the list
4229 are noted so their proper addresses can be filled in later,
4230 using the common block base address gotten from the assembler
4231 stabs. */
4232
4233 static struct pending *common_block;
4234 static int common_block_i;
4235
4236 /* Name of the current common block. We get it from the BCOMM instead of the
4237 ECOMM to match IBM documentation (even though IBM puts the name both places
4238 like everyone else). */
4239 static char *common_block_name;
4240
4241 /* Process a N_BCOMM symbol. The storage for NAME is not guaranteed
4242 to remain after this function returns. */
4243
4244 void
4245 common_block_start (const char *name, struct objfile *objfile)
4246 {
4247 if (common_block_name != NULL)
4248 {
4249 complaint (_("Invalid symbol data: common block within common block"));
4250 }
4251 common_block = *get_local_symbols ();
4252 common_block_i = common_block ? common_block->nsyms : 0;
4253 common_block_name = obstack_strdup (&objfile->objfile_obstack, name);
4254 }
4255
4256 /* Process a N_ECOMM symbol. */
4257
4258 void
4259 common_block_end (struct objfile *objfile)
4260 {
4261 /* Symbols declared since the BCOMM are to have the common block
4262 start address added in when we know it. common_block and
4263 common_block_i point to the first symbol after the BCOMM in
4264 the local_symbols list; copy the list and hang it off the
4265 symbol for the common block name for later fixup. */
4266 int i;
4267 struct symbol *sym;
4268 struct pending *newobj = 0;
4269 struct pending *next;
4270 int j;
4271
4272 if (common_block_name == NULL)
4273 {
4274 complaint (_("ECOMM symbol unmatched by BCOMM"));
4275 return;
4276 }
4277
4278 sym = new (&objfile->objfile_obstack) symbol;
4279 /* Note: common_block_name already saved on objfile_obstack. */
4280 sym->set_linkage_name (common_block_name);
4281 sym->set_aclass_index (LOC_BLOCK);
4282
4283 /* Now we copy all the symbols which have been defined since the BCOMM. */
4284
4285 /* Copy all the struct pendings before common_block. */
4286 for (next = *get_local_symbols ();
4287 next != NULL && next != common_block;
4288 next = next->next)
4289 {
4290 for (j = 0; j < next->nsyms; j++)
4291 add_symbol_to_list (next->symbol[j], &newobj);
4292 }
4293
4294 /* Copy however much of COMMON_BLOCK we need. If COMMON_BLOCK is
4295 NULL, it means copy all the local symbols (which we already did
4296 above). */
4297
4298 if (common_block != NULL)
4299 for (j = common_block_i; j < common_block->nsyms; j++)
4300 add_symbol_to_list (common_block->symbol[j], &newobj);
4301
4302 sym->set_type ((struct type *) newobj);
4303
4304 /* Should we be putting local_symbols back to what it was?
4305 Does it matter? */
4306
4307 i = hashname (sym->linkage_name ());
4308 sym->set_value_chain (global_sym_chain[i]);
4309 global_sym_chain[i] = sym;
4310 common_block_name = NULL;
4311 }
4312
4313 /* Add a common block's start address to the offset of each symbol
4314 declared to be in it (by being between a BCOMM/ECOMM pair that uses
4315 the common block name). */
4316
4317 static void
4318 fix_common_block (struct symbol *sym, CORE_ADDR valu, int section_index)
4319 {
4320 struct pending *next = (struct pending *) sym->type ();
4321
4322 for (; next; next = next->next)
4323 {
4324 int j;
4325
4326 for (j = next->nsyms - 1; j >= 0; j--)
4327 {
4328 next->symbol[j]->set_value_address
4329 (next->symbol[j]->value_address () + valu);
4330 next->symbol[j]->set_section_index (section_index);
4331 }
4332 }
4333 }
4334 \f
4335
4336
4337 /* Add {TYPE, TYPENUMS} to the NONAME_UNDEFS vector.
4338 See add_undefined_type for more details. */
4339
4340 static void
4341 add_undefined_type_noname (struct type *type, int typenums[2])
4342 {
4343 struct nat nat;
4344
4345 nat.typenums[0] = typenums [0];
4346 nat.typenums[1] = typenums [1];
4347 nat.type = type;
4348
4349 if (noname_undefs_length == noname_undefs_allocated)
4350 {
4351 noname_undefs_allocated *= 2;
4352 noname_undefs = (struct nat *)
4353 xrealloc ((char *) noname_undefs,
4354 noname_undefs_allocated * sizeof (struct nat));
4355 }
4356 noname_undefs[noname_undefs_length++] = nat;
4357 }
4358
4359 /* Add TYPE to the UNDEF_TYPES vector.
4360 See add_undefined_type for more details. */
4361
4362 static void
4363 add_undefined_type_1 (struct type *type)
4364 {
4365 if (undef_types_length == undef_types_allocated)
4366 {
4367 undef_types_allocated *= 2;
4368 undef_types = (struct type **)
4369 xrealloc ((char *) undef_types,
4370 undef_types_allocated * sizeof (struct type *));
4371 }
4372 undef_types[undef_types_length++] = type;
4373 }
4374
4375 /* What about types defined as forward references inside of a small lexical
4376 scope? */
4377 /* Add a type to the list of undefined types to be checked through
4378 once this file has been read in.
4379
4380 In practice, we actually maintain two such lists: The first list
4381 (UNDEF_TYPES) is used for types whose name has been provided, and
4382 concerns forward references (eg 'xs' or 'xu' forward references);
4383 the second list (NONAME_UNDEFS) is used for types whose name is
4384 unknown at creation time, because they were referenced through
4385 their type number before the actual type was declared.
4386 This function actually adds the given type to the proper list. */
4387
4388 static void
4389 add_undefined_type (struct type *type, int typenums[2])
4390 {
4391 if (type->name () == NULL)
4392 add_undefined_type_noname (type, typenums);
4393 else
4394 add_undefined_type_1 (type);
4395 }
4396
4397 /* Try to fix all undefined types pushed on the UNDEF_TYPES vector. */
4398
4399 static void
4400 cleanup_undefined_types_noname (struct objfile *objfile)
4401 {
4402 int i;
4403
4404 for (i = 0; i < noname_undefs_length; i++)
4405 {
4406 struct nat nat = noname_undefs[i];
4407 struct type **type;
4408
4409 type = dbx_lookup_type (nat.typenums, objfile);
4410 if (nat.type != *type && (*type)->code () != TYPE_CODE_UNDEF)
4411 {
4412 /* The instance flags of the undefined type are still unset,
4413 and needs to be copied over from the reference type.
4414 Since replace_type expects them to be identical, we need
4415 to set these flags manually before hand. */
4416 nat.type->set_instance_flags ((*type)->instance_flags ());
4417 replace_type (nat.type, *type);
4418 }
4419 }
4420
4421 noname_undefs_length = 0;
4422 }
4423
4424 /* Go through each undefined type, see if it's still undefined, and fix it
4425 up if possible. We have two kinds of undefined types:
4426
4427 TYPE_CODE_ARRAY: Array whose target type wasn't defined yet.
4428 Fix: update array length using the element bounds
4429 and the target type's length.
4430 TYPE_CODE_STRUCT, TYPE_CODE_UNION: Structure whose fields were not
4431 yet defined at the time a pointer to it was made.
4432 Fix: Do a full lookup on the struct/union tag. */
4433
4434 static void
4435 cleanup_undefined_types_1 (void)
4436 {
4437 struct type **type;
4438
4439 /* Iterate over every undefined type, and look for a symbol whose type
4440 matches our undefined type. The symbol matches if:
4441 1. It is a typedef in the STRUCT domain;
4442 2. It has the same name, and same type code;
4443 3. The instance flags are identical.
4444
4445 It is important to check the instance flags, because we have seen
4446 examples where the debug info contained definitions such as:
4447
4448 "foo_t:t30=B31=xefoo_t:"
4449
4450 In this case, we have created an undefined type named "foo_t" whose
4451 instance flags is null (when processing "xefoo_t"), and then created
4452 another type with the same name, but with different instance flags
4453 ('B' means volatile). I think that the definition above is wrong,
4454 since the same type cannot be volatile and non-volatile at the same
4455 time, but we need to be able to cope with it when it happens. The
4456 approach taken here is to treat these two types as different. */
4457
4458 for (type = undef_types; type < undef_types + undef_types_length; type++)
4459 {
4460 switch ((*type)->code ())
4461 {
4462
4463 case TYPE_CODE_STRUCT:
4464 case TYPE_CODE_UNION:
4465 case TYPE_CODE_ENUM:
4466 {
4467 /* Check if it has been defined since. Need to do this here
4468 as well as in check_typedef to deal with the (legitimate in
4469 C though not C++) case of several types with the same name
4470 in different source files. */
4471 if ((*type)->is_stub ())
4472 {
4473 struct pending *ppt;
4474 int i;
4475 /* Name of the type, without "struct" or "union". */
4476 const char *type_name = (*type)->name ();
4477
4478 if (type_name == NULL)
4479 {
4480 complaint (_("need a type name"));
4481 break;
4482 }
4483 for (ppt = *get_file_symbols (); ppt; ppt = ppt->next)
4484 {
4485 for (i = 0; i < ppt->nsyms; i++)
4486 {
4487 struct symbol *sym = ppt->symbol[i];
4488
4489 if (sym->aclass () == LOC_TYPEDEF
4490 && sym->domain () == STRUCT_DOMAIN
4491 && (sym->type ()->code () == (*type)->code ())
4492 && ((*type)->instance_flags ()
4493 == sym->type ()->instance_flags ())
4494 && strcmp (sym->linkage_name (), type_name) == 0)
4495 replace_type (*type, sym->type ());
4496 }
4497 }
4498 }
4499 }
4500 break;
4501
4502 default:
4503 {
4504 complaint (_("forward-referenced types left unresolved, "
4505 "type code %d."),
4506 (*type)->code ());
4507 }
4508 break;
4509 }
4510 }
4511
4512 undef_types_length = 0;
4513 }
4514
4515 /* Try to fix all the undefined types we encountered while processing
4516 this unit. */
4517
4518 void
4519 cleanup_undefined_stabs_types (struct objfile *objfile)
4520 {
4521 cleanup_undefined_types_1 ();
4522 cleanup_undefined_types_noname (objfile);
4523 }
4524
4525 /* See stabsread.h. */
4526
4527 void
4528 scan_file_globals (struct objfile *objfile)
4529 {
4530 int hash;
4531 struct symbol *sym, *prev;
4532 struct objfile *resolve_objfile;
4533
4534 /* SVR4 based linkers copy referenced global symbols from shared
4535 libraries to the main executable.
4536 If we are scanning the symbols for a shared library, try to resolve
4537 them from the minimal symbols of the main executable first. */
4538
4539 if (current_program_space->symfile_object_file
4540 && objfile != current_program_space->symfile_object_file)
4541 resolve_objfile = current_program_space->symfile_object_file;
4542 else
4543 resolve_objfile = objfile;
4544
4545 while (1)
4546 {
4547 /* Avoid expensive loop through all minimal symbols if there are
4548 no unresolved symbols. */
4549 for (hash = 0; hash < HASHSIZE; hash++)
4550 {
4551 if (global_sym_chain[hash])
4552 break;
4553 }
4554 if (hash >= HASHSIZE)
4555 return;
4556
4557 for (minimal_symbol *msymbol : resolve_objfile->msymbols ())
4558 {
4559 QUIT;
4560
4561 /* Skip static symbols. */
4562 switch (msymbol->type ())
4563 {
4564 case mst_file_text:
4565 case mst_file_data:
4566 case mst_file_bss:
4567 continue;
4568 default:
4569 break;
4570 }
4571
4572 prev = NULL;
4573
4574 /* Get the hash index and check all the symbols
4575 under that hash index. */
4576
4577 hash = hashname (msymbol->linkage_name ());
4578
4579 for (sym = global_sym_chain[hash]; sym;)
4580 {
4581 if (strcmp (msymbol->linkage_name (), sym->linkage_name ()) == 0)
4582 {
4583 /* Splice this symbol out of the hash chain and
4584 assign the value we have to it. */
4585 if (prev)
4586 {
4587 prev->set_value_chain (sym->value_chain ());
4588 }
4589 else
4590 {
4591 global_sym_chain[hash] = sym->value_chain ();
4592 }
4593
4594 /* Check to see whether we need to fix up a common block. */
4595 /* Note: this code might be executed several times for
4596 the same symbol if there are multiple references. */
4597 if (sym)
4598 {
4599 if (sym->aclass () == LOC_BLOCK)
4600 fix_common_block
4601 (sym, msymbol->value_address (resolve_objfile),
4602 msymbol->section_index ());
4603 else
4604 sym->set_value_address
4605 (msymbol->value_address (resolve_objfile));
4606 sym->set_section_index (msymbol->section_index ());
4607 }
4608
4609 if (prev)
4610 {
4611 sym = prev->value_chain ();
4612 }
4613 else
4614 {
4615 sym = global_sym_chain[hash];
4616 }
4617 }
4618 else
4619 {
4620 prev = sym;
4621 sym = sym->value_chain ();
4622 }
4623 }
4624 }
4625 if (resolve_objfile == objfile)
4626 break;
4627 resolve_objfile = objfile;
4628 }
4629
4630 /* Change the storage class of any remaining unresolved globals to
4631 LOC_UNRESOLVED and remove them from the chain. */
4632 for (hash = 0; hash < HASHSIZE; hash++)
4633 {
4634 sym = global_sym_chain[hash];
4635 while (sym)
4636 {
4637 prev = sym;
4638 sym = sym->value_chain ();
4639
4640 /* Change the symbol address from the misleading chain value
4641 to address zero. */
4642 prev->set_value_address (0);
4643
4644 /* Complain about unresolved common block symbols. */
4645 if (prev->aclass () == LOC_STATIC)
4646 prev->set_aclass_index (LOC_UNRESOLVED);
4647 else
4648 complaint (_("%s: common block `%s' from "
4649 "global_sym_chain unresolved"),
4650 objfile_name (objfile), prev->print_name ());
4651 }
4652 }
4653 memset (global_sym_chain, 0, sizeof (global_sym_chain));
4654 }
4655
4656 /* Initialize anything that needs initializing when starting to read
4657 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
4658 to a psymtab. */
4659
4660 void
4661 stabsread_init (void)
4662 {
4663 }
4664
4665 /* Initialize anything that needs initializing when a completely new
4666 symbol file is specified (not just adding some symbols from another
4667 file, e.g. a shared library). */
4668
4669 void
4670 stabsread_new_init (void)
4671 {
4672 /* Empty the hash table of global syms looking for values. */
4673 memset (global_sym_chain, 0, sizeof (global_sym_chain));
4674 }
4675
4676 /* Initialize anything that needs initializing at the same time as
4677 start_compunit_symtab() is called. */
4678
4679 void
4680 start_stabs (void)
4681 {
4682 global_stabs = NULL; /* AIX COFF */
4683 /* Leave FILENUM of 0 free for builtin types and this file's types. */
4684 n_this_object_header_files = 1;
4685 type_vector_length = 0;
4686 type_vector = (struct type **) 0;
4687 within_function = 0;
4688
4689 /* FIXME: If common_block_name is not already NULL, we should complain(). */
4690 common_block_name = NULL;
4691 }
4692
4693 /* Call after end_compunit_symtab(). */
4694
4695 void
4696 end_stabs (void)
4697 {
4698 if (type_vector)
4699 {
4700 xfree (type_vector);
4701 }
4702 type_vector = 0;
4703 type_vector_length = 0;
4704 previous_stab_code = 0;
4705 }
4706
4707 void
4708 finish_global_stabs (struct objfile *objfile)
4709 {
4710 if (global_stabs)
4711 {
4712 patch_block_stabs (*get_global_symbols (), global_stabs, objfile);
4713 xfree (global_stabs);
4714 global_stabs = NULL;
4715 }
4716 }
4717
4718 /* Find the end of the name, delimited by a ':', but don't match
4719 ObjC symbols which look like -[Foo bar::]:bla. */
4720 static const char *
4721 find_name_end (const char *name)
4722 {
4723 const char *s = name;
4724
4725 if (s[0] == '-' || *s == '+')
4726 {
4727 /* Must be an ObjC method symbol. */
4728 if (s[1] != '[')
4729 {
4730 error (_("invalid symbol name \"%s\""), name);
4731 }
4732 s = strchr (s, ']');
4733 if (s == NULL)
4734 {
4735 error (_("invalid symbol name \"%s\""), name);
4736 }
4737 return strchr (s, ':');
4738 }
4739 else
4740 {
4741 return strchr (s, ':');
4742 }
4743 }
4744
4745 /* See stabsread.h. */
4746
4747 int
4748 hashname (const char *name)
4749 {
4750 return fast_hash (name, strlen (name)) % HASHSIZE;
4751 }
4752
4753 /* Initializer for this module. */
4754
4755 void _initialize_stabsread ();
4756 void
4757 _initialize_stabsread ()
4758 {
4759 undef_types_allocated = 20;
4760 undef_types_length = 0;
4761 undef_types = XNEWVEC (struct type *, undef_types_allocated);
4762
4763 noname_undefs_allocated = 20;
4764 noname_undefs_length = 0;
4765 noname_undefs = XNEWVEC (struct nat, noname_undefs_allocated);
4766
4767 stab_register_index = register_symbol_register_impl (LOC_REGISTER,
4768 &stab_register_funcs);
4769 stab_regparm_index = register_symbol_register_impl (LOC_REGPARM_ADDR,
4770 &stab_register_funcs);
4771 }