Automatic date update in version.in
[binutils-gdb.git] / gdb / stabsread.c
blob754f82406b48ed6285dde6dbbfea2e989ef25c31
1 /* Support routines for decoding "stabs" debugging information format.
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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. */
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>
50 #include "stabsread.h"
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;
64 struct stabs_nextfield
66 struct stabs_nextfield *next;
68 struct field field;
71 struct next_fnfieldlist
73 struct next_fnfieldlist *next;
74 struct fn_fieldlist fn_fieldlist;
77 /* The routines that read and process a complete stabs for a C struct or
78 C++ class pass lists of data member fields and lists of member function
79 fields in an instance of a field_info structure, as defined below.
80 This is part of some reorganization of low level C++ support and is
81 expected to eventually go away... (FIXME) */
83 struct stab_field_info
85 struct stabs_nextfield *list = nullptr;
86 struct next_fnfieldlist *fnlist = nullptr;
88 auto_obstack obstack;
91 static void
92 read_one_struct_field (struct stab_field_info *, const char **, const char *,
93 struct type *, struct objfile *);
95 static struct type *dbx_alloc_type (int[2], struct objfile *);
97 static long read_huge_number (const char **, int, int *, int);
99 static struct type *error_type (const char **, struct objfile *);
101 static void
102 patch_block_stabs (struct pending *, struct pending_stabs *,
103 struct objfile *);
105 static int read_type_number (const char **, int *);
107 static struct type *read_type (const char **, struct objfile *);
109 static struct type *read_range_type (const char **, int[2],
110 int, struct objfile *);
112 static struct type *read_sun_builtin_type (const char **,
113 int[2], struct objfile *);
115 static struct type *read_sun_floating_type (const char **, int[2],
116 struct objfile *);
118 static struct type *read_enum_type (const char **, struct type *, struct objfile *);
120 static struct type *rs6000_builtin_type (int, struct objfile *);
122 static int
123 read_member_functions (struct stab_field_info *, const char **, struct type *,
124 struct objfile *);
126 static int
127 read_struct_fields (struct stab_field_info *, const char **, struct type *,
128 struct objfile *);
130 static int
131 read_baseclasses (struct stab_field_info *, const char **, struct type *,
132 struct objfile *);
134 static int
135 read_tilde_fields (struct stab_field_info *, const char **, struct type *,
136 struct objfile *);
138 static int attach_fn_fields_to_type (struct stab_field_info *, struct type *);
140 static int attach_fields_to_type (struct stab_field_info *, struct type *,
141 struct objfile *);
143 static struct type *read_struct_type (const char **, struct type *,
144 enum type_code,
145 struct objfile *);
147 static struct type *read_array_type (const char **, struct type *,
148 struct objfile *);
150 static struct field *read_args (const char **, int, struct objfile *,
151 int *, int *);
153 static void add_undefined_type (struct type *, int[2]);
155 static int
156 read_cpp_abbrev (struct stab_field_info *, const char **, struct type *,
157 struct objfile *);
159 static const char *find_name_end (const char *name);
161 static int process_reference (const char **string);
163 void stabsread_clear_cache (void);
165 static const char vptr_name[] = "_vptr$";
166 static const char vb_name[] = "_vb$";
168 static void
169 invalid_cpp_abbrev_complaint (const char *arg1)
171 complaint (_("invalid C++ abbreviation `%s'"), arg1);
174 static void
175 reg_value_complaint (int regnum, int num_regs, const char *sym)
177 complaint (_("bad register number %d (max %d) in symbol %s"),
178 regnum, num_regs - 1, sym);
181 static void
182 stabs_general_complaint (const char *arg1)
184 complaint ("%s", arg1);
187 /* Make a list of forward references which haven't been defined. */
189 static struct type **undef_types;
190 static int undef_types_allocated;
191 static int undef_types_length;
192 static struct symbol *current_symbol = NULL;
194 /* Make a list of nameless types that are undefined.
195 This happens when another type is referenced by its number
196 before this type is actually defined. For instance "t(0,1)=k(0,2)"
197 and type (0,2) is defined only later. */
199 struct nat
201 int typenums[2];
202 struct type *type;
204 static struct nat *noname_undefs;
205 static int noname_undefs_allocated;
206 static int noname_undefs_length;
208 /* Check for and handle cretinous stabs symbol name continuation! */
209 #define STABS_CONTINUE(pp,objfile) \
210 do { \
211 if (**(pp) == '\\' || (**(pp) == '?' && (*(pp))[1] == '\0')) \
212 *(pp) = next_symbol_text (objfile); \
213 } while (0)
215 /* Vector of types defined so far, indexed by their type numbers.
216 (In newer sun systems, dbx uses a pair of numbers in parens,
217 as in "(SUBFILENUM,NUMWITHINSUBFILE)".
218 Then these numbers must be translated through the type_translations
219 hash table to get the index into the type vector.) */
221 static struct type **type_vector;
223 /* Number of elements allocated for type_vector currently. */
225 static int type_vector_length;
227 /* Initial size of type vector. Is realloc'd larger if needed, and
228 realloc'd down to the size actually used, when completed. */
230 #define INITIAL_TYPE_VECTOR_LENGTH 160
233 /* Look up a dbx type-number pair. Return the address of the slot
234 where the type for that number-pair is stored.
235 The number-pair is in TYPENUMS.
237 This can be used for finding the type associated with that pair
238 or for associating a new type with the pair. */
240 static struct type **
241 dbx_lookup_type (int typenums[2], struct objfile *objfile)
243 int filenum = typenums[0];
244 int index = typenums[1];
245 unsigned old_len;
246 int real_filenum;
247 struct header_file *f;
248 int f_orig_length;
250 if (filenum == -1) /* -1,-1 is for temporary types. */
251 return 0;
253 if (filenum < 0 || filenum >= n_this_object_header_files)
255 complaint (_("Invalid symbol data: type number "
256 "(%d,%d) out of range at symtab pos %d."),
257 filenum, index, symnum);
258 goto error_return;
261 if (filenum == 0)
263 if (index < 0)
265 /* Caller wants address of address of type. We think
266 that negative (rs6k builtin) types will never appear as
267 "lvalues", (nor should they), so we stuff the real type
268 pointer into a temp, and return its address. If referenced,
269 this will do the right thing. */
270 static struct type *temp_type;
272 temp_type = rs6000_builtin_type (index, objfile);
273 return &temp_type;
276 /* Type is defined outside of header files.
277 Find it in this object file's type vector. */
278 if (index >= type_vector_length)
280 old_len = type_vector_length;
281 if (old_len == 0)
283 type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
284 type_vector = XNEWVEC (struct type *, type_vector_length);
286 while (index >= type_vector_length)
288 type_vector_length *= 2;
290 type_vector = (struct type **)
291 xrealloc ((char *) type_vector,
292 (type_vector_length * sizeof (struct type *)));
293 memset (&type_vector[old_len], 0,
294 (type_vector_length - old_len) * sizeof (struct type *));
296 return (&type_vector[index]);
298 else
300 real_filenum = this_object_header_files[filenum];
302 if (real_filenum >= N_HEADER_FILES (objfile))
304 static struct type *temp_type;
306 warning (_("GDB internal error: bad real_filenum"));
308 error_return:
309 temp_type = builtin_type (objfile)->builtin_error;
310 return &temp_type;
313 f = HEADER_FILES (objfile) + real_filenum;
315 f_orig_length = f->length;
316 if (index >= f_orig_length)
318 while (index >= f->length)
320 f->length *= 2;
322 f->vector = (struct type **)
323 xrealloc ((char *) f->vector, f->length * sizeof (struct type *));
324 memset (&f->vector[f_orig_length], 0,
325 (f->length - f_orig_length) * sizeof (struct type *));
327 return (&f->vector[index]);
331 /* Make sure there is a type allocated for type numbers TYPENUMS
332 and return the type object.
333 This can create an empty (zeroed) type object.
334 TYPENUMS may be (-1, -1) to return a new type object that is not
335 put into the type vector, and so may not be referred to by number. */
337 static struct type *
338 dbx_alloc_type (int typenums[2], struct objfile *objfile)
340 struct type **type_addr;
342 if (typenums[0] == -1)
344 return type_allocator (objfile,
345 get_current_subfile ()->language).new_type ();
348 type_addr = dbx_lookup_type (typenums, objfile);
350 /* If we are referring to a type not known at all yet,
351 allocate an empty type for it.
352 We will fill it in later if we find out how. */
353 if (*type_addr == 0)
355 *type_addr = type_allocator (objfile,
356 get_current_subfile ()->language).new_type ();
359 return (*type_addr);
362 /* Allocate a floating-point type of size BITS. */
364 static struct type *
365 dbx_init_float_type (struct objfile *objfile, int bits)
367 struct gdbarch *gdbarch = objfile->arch ();
368 const struct floatformat **format;
369 struct type *type;
371 format = gdbarch_floatformat_for_type (gdbarch, NULL, bits);
372 type_allocator alloc (objfile, get_current_subfile ()->language);
373 if (format)
374 type = init_float_type (alloc, bits, NULL, format);
375 else
376 type = alloc.new_type (TYPE_CODE_ERROR, bits, NULL);
378 return type;
381 /* for all the stabs in a given stab vector, build appropriate types
382 and fix their symbols in given symbol vector. */
384 static void
385 patch_block_stabs (struct pending *symbols, struct pending_stabs *stabs,
386 struct objfile *objfile)
388 int ii;
389 char *name;
390 const char *pp;
391 struct symbol *sym;
393 if (stabs)
395 /* for all the stab entries, find their corresponding symbols and
396 patch their types! */
398 for (ii = 0; ii < stabs->count; ++ii)
400 name = stabs->stab[ii];
401 pp = (char *) strchr (name, ':');
402 gdb_assert (pp); /* Must find a ':' or game's over. */
403 while (pp[1] == ':')
405 pp += 2;
406 pp = (char *) strchr (pp, ':');
408 sym = find_symbol_in_list (symbols, name, pp - name);
409 if (!sym)
411 /* FIXME-maybe: it would be nice if we noticed whether
412 the variable was defined *anywhere*, not just whether
413 it is defined in this compilation unit. But neither
414 xlc or GCC seem to need such a definition, and until
415 we do psymtabs (so that the minimal symbols from all
416 compilation units are available now), I'm not sure
417 how to get the information. */
419 /* On xcoff, if a global is defined and never referenced,
420 ld will remove it from the executable. There is then
421 a N_GSYM stab for it, but no regular (C_EXT) symbol. */
422 sym = new (&objfile->objfile_obstack) symbol;
423 sym->set_domain (VAR_DOMAIN);
424 sym->set_aclass_index (LOC_OPTIMIZED_OUT);
425 sym->set_linkage_name
426 (obstack_strndup (&objfile->objfile_obstack, name, pp - name));
427 pp += 2;
428 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
430 /* I don't think the linker does this with functions,
431 so as far as I know this is never executed.
432 But it doesn't hurt to check. */
433 sym->set_type
434 (lookup_function_type (read_type (&pp, objfile)));
436 else
438 sym->set_type (read_type (&pp, objfile));
440 add_symbol_to_list (sym, get_global_symbols ());
442 else
444 pp += 2;
445 if (*(pp - 1) == 'F' || *(pp - 1) == 'f')
447 sym->set_type
448 (lookup_function_type (read_type (&pp, objfile)));
450 else
452 sym->set_type (read_type (&pp, objfile));
460 /* Read a number by which a type is referred to in dbx data,
461 or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
462 Just a single number N is equivalent to (0,N).
463 Return the two numbers by storing them in the vector TYPENUMS.
464 TYPENUMS will then be used as an argument to dbx_lookup_type.
466 Returns 0 for success, -1 for error. */
468 static int
469 read_type_number (const char **pp, int *typenums)
471 int nbits;
473 if (**pp == '(')
475 (*pp)++;
476 typenums[0] = read_huge_number (pp, ',', &nbits, 0);
477 if (nbits != 0)
478 return -1;
479 typenums[1] = read_huge_number (pp, ')', &nbits, 0);
480 if (nbits != 0)
481 return -1;
483 else
485 typenums[0] = 0;
486 typenums[1] = read_huge_number (pp, 0, &nbits, 0);
487 if (nbits != 0)
488 return -1;
490 return 0;
494 #define VISIBILITY_PRIVATE '0' /* Stabs character for private field */
495 #define VISIBILITY_PROTECTED '1' /* Stabs character for protected fld */
496 #define VISIBILITY_PUBLIC '2' /* Stabs character for public field */
497 #define VISIBILITY_IGNORE '9' /* Optimized out or zero length */
499 /* Structure for storing pointers to reference definitions for fast lookup
500 during "process_later". */
502 struct ref_map
504 const char *stabs;
505 CORE_ADDR value;
506 struct symbol *sym;
509 #define MAX_CHUNK_REFS 100
510 #define REF_CHUNK_SIZE (MAX_CHUNK_REFS * sizeof (struct ref_map))
511 #define REF_MAP_SIZE(ref_chunk) ((ref_chunk) * REF_CHUNK_SIZE)
513 static struct ref_map *ref_map;
515 /* Ptr to free cell in chunk's linked list. */
516 static int ref_count = 0;
518 /* Number of chunks malloced. */
519 static int ref_chunk = 0;
521 /* This file maintains a cache of stabs aliases found in the symbol
522 table. If the symbol table changes, this cache must be cleared
523 or we are left holding onto data in invalid obstacks. */
524 void
525 stabsread_clear_cache (void)
527 ref_count = 0;
528 ref_chunk = 0;
531 /* Create array of pointers mapping refids to symbols and stab strings.
532 Add pointers to reference definition symbols and/or their values as we
533 find them, using their reference numbers as our index.
534 These will be used later when we resolve references. */
535 void
536 ref_add (int refnum, struct symbol *sym, const char *stabs, CORE_ADDR value)
538 if (ref_count == 0)
539 ref_chunk = 0;
540 if (refnum >= ref_count)
541 ref_count = refnum + 1;
542 if (ref_count > ref_chunk * MAX_CHUNK_REFS)
544 int new_slots = ref_count - ref_chunk * MAX_CHUNK_REFS;
545 int new_chunks = new_slots / MAX_CHUNK_REFS + 1;
547 ref_map = (struct ref_map *)
548 xrealloc (ref_map, REF_MAP_SIZE (ref_chunk + new_chunks));
549 memset (ref_map + ref_chunk * MAX_CHUNK_REFS, 0,
550 new_chunks * REF_CHUNK_SIZE);
551 ref_chunk += new_chunks;
553 ref_map[refnum].stabs = stabs;
554 ref_map[refnum].sym = sym;
555 ref_map[refnum].value = value;
558 /* Return defined sym for the reference REFNUM. */
559 struct symbol *
560 ref_search (int refnum)
562 if (refnum < 0 || refnum > ref_count)
563 return 0;
564 return ref_map[refnum].sym;
567 /* Parse a reference id in STRING and return the resulting
568 reference number. Move STRING beyond the reference id. */
570 static int
571 process_reference (const char **string)
573 const char *p;
574 int refnum = 0;
576 if (**string != '#')
577 return 0;
579 /* Advance beyond the initial '#'. */
580 p = *string + 1;
582 /* Read number as reference id. */
583 while (*p && isdigit (*p))
585 refnum = refnum * 10 + *p - '0';
586 p++;
588 *string = p;
589 return refnum;
592 /* If STRING defines a reference, store away a pointer to the reference
593 definition for later use. Return the reference number. */
596 symbol_reference_defined (const char **string)
598 const char *p = *string;
599 int refnum = 0;
601 refnum = process_reference (&p);
603 /* Defining symbols end in '='. */
604 if (*p == '=')
606 /* Symbol is being defined here. */
607 *string = p + 1;
608 return refnum;
610 else
612 /* Must be a reference. Either the symbol has already been defined,
613 or this is a forward reference to it. */
614 *string = p;
615 return -1;
619 static int
620 stab_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
622 int regno = gdbarch_stab_reg_to_regnum (gdbarch, sym->value_longest ());
624 if (regno < 0 || regno >= gdbarch_num_cooked_regs (gdbarch))
626 reg_value_complaint (regno, gdbarch_num_cooked_regs (gdbarch),
627 sym->print_name ());
629 regno = gdbarch_sp_regnum (gdbarch); /* Known safe, though useless. */
632 return regno;
635 static const struct symbol_register_ops stab_register_funcs = {
636 stab_reg_to_regnum
639 /* The "aclass" indices for computed symbols. */
641 static int stab_register_index;
642 static int stab_regparm_index;
644 struct symbol *
645 define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
646 struct objfile *objfile)
648 struct gdbarch *gdbarch = objfile->arch ();
649 struct symbol *sym;
650 const char *p = find_name_end (string);
651 int deftype;
652 int synonym = 0;
653 int i;
655 /* We would like to eliminate nameless symbols, but keep their types.
656 E.g. stab entry ":t10=*2" should produce a type 10, which is a pointer
657 to type 2, but, should not create a symbol to address that type. Since
658 the symbol will be nameless, there is no way any user can refer to it. */
660 int nameless;
662 /* Ignore syms with empty names. */
663 if (string[0] == 0)
664 return 0;
666 /* Ignore old-style symbols from cc -go. */
667 if (p == 0)
668 return 0;
670 while (p[1] == ':')
672 p += 2;
673 p = strchr (p, ':');
674 if (p == NULL)
676 complaint (
677 _("Bad stabs string '%s'"), string);
678 return NULL;
682 /* If a nameless stab entry, all we need is the type, not the symbol.
683 e.g. ":t10=*2" or a nameless enum like " :T16=ered:0,green:1,blue:2,;" */
684 nameless = (p == string || ((string[0] == ' ') && (string[1] == ':')));
686 current_symbol = sym = new (&objfile->objfile_obstack) symbol;
688 if (processing_gcc_compilation)
690 /* GCC 2.x puts the line number in desc. SunOS apparently puts in the
691 number of bytes occupied by a type or object, which we ignore. */
692 sym->set_line (desc);
694 else
696 sym->set_line (0); /* unknown */
699 sym->set_language (get_current_subfile ()->language,
700 &objfile->objfile_obstack);
702 if (is_cplus_marker (string[0]))
704 /* Special GNU C++ names. */
705 switch (string[1])
707 case 't':
708 sym->set_linkage_name ("this");
709 break;
711 case 'v': /* $vtbl_ptr_type */
712 goto normal;
714 case 'e':
715 sym->set_linkage_name ("eh_throw");
716 break;
718 case '_':
719 /* This was an anonymous type that was never fixed up. */
720 goto normal;
722 default:
723 complaint (_("Unknown C++ symbol name `%s'"),
724 string);
725 goto normal; /* Do *something* with it. */
728 else
730 normal:
731 gdb::unique_xmalloc_ptr<char> new_name;
733 if (sym->language () == language_cplus)
735 std::string name (string, p - string);
736 new_name = cp_canonicalize_string (name.c_str ());
738 else if (sym->language () == language_c)
740 std::string name (string, p - string);
741 new_name = c_canonicalize_name (name.c_str ());
743 if (new_name != nullptr)
744 sym->compute_and_set_names (new_name.get (), true, objfile->per_bfd);
745 else
746 sym->compute_and_set_names (std::string_view (string, p - string), true,
747 objfile->per_bfd);
749 if (sym->language () == language_cplus)
750 cp_scan_for_anonymous_namespaces (get_buildsym_compunit (), sym,
751 objfile);
754 p++;
756 /* Determine the type of name being defined. */
757 #if 0
758 /* Getting GDB to correctly skip the symbol on an undefined symbol
759 descriptor and not ever dump core is a very dodgy proposition if
760 we do things this way. I say the acorn RISC machine can just
761 fix their compiler. */
762 /* The Acorn RISC machine's compiler can put out locals that don't
763 start with "234=" or "(3,4)=", so assume anything other than the
764 deftypes we know how to handle is a local. */
765 if (!strchr ("cfFGpPrStTvVXCR", *p))
766 #else
767 if (isdigit (*p) || *p == '(' || *p == '-')
768 #endif
769 deftype = 'l';
770 else
771 deftype = *p++;
773 switch (deftype)
775 case 'c':
776 /* c is a special case, not followed by a type-number.
777 SYMBOL:c=iVALUE for an integer constant symbol.
778 SYMBOL:c=rVALUE for a floating constant symbol.
779 SYMBOL:c=eTYPE,INTVALUE for an enum constant symbol.
780 e.g. "b:c=e6,0" for "const b = blob1"
781 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
782 if (*p != '=')
784 sym->set_aclass_index (LOC_CONST);
785 sym->set_type (error_type (&p, objfile));
786 sym->set_domain (VAR_DOMAIN);
787 add_symbol_to_list (sym, get_file_symbols ());
788 return sym;
790 ++p;
791 switch (*p++)
793 case 'r':
795 gdb_byte *dbl_valu;
796 struct type *dbl_type;
798 dbl_type = builtin_type (objfile)->builtin_double;
799 dbl_valu
800 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack,
801 dbl_type->length ());
803 target_float_from_string (dbl_valu, dbl_type, std::string (p));
805 sym->set_type (dbl_type);
806 sym->set_value_bytes (dbl_valu);
807 sym->set_aclass_index (LOC_CONST_BYTES);
809 break;
810 case 'i':
812 /* Defining integer constants this way is kind of silly,
813 since 'e' constants allows the compiler to give not
814 only the value, but the type as well. C has at least
815 int, long, unsigned int, and long long as constant
816 types; other languages probably should have at least
817 unsigned as well as signed constants. */
819 sym->set_type (builtin_type (objfile)->builtin_long);
820 sym->set_value_longest (atoi (p));
821 sym->set_aclass_index (LOC_CONST);
823 break;
825 case 'c':
827 sym->set_type (builtin_type (objfile)->builtin_char);
828 sym->set_value_longest (atoi (p));
829 sym->set_aclass_index (LOC_CONST);
831 break;
833 case 's':
835 struct type *range_type;
836 int ind = 0;
837 char quote = *p++;
838 gdb_byte *string_local = (gdb_byte *) alloca (strlen (p));
839 gdb_byte *string_value;
841 if (quote != '\'' && quote != '"')
843 sym->set_aclass_index (LOC_CONST);
844 sym->set_type (error_type (&p, objfile));
845 sym->set_domain (VAR_DOMAIN);
846 add_symbol_to_list (sym, get_file_symbols ());
847 return sym;
850 /* Find matching quote, rejecting escaped quotes. */
851 while (*p && *p != quote)
853 if (*p == '\\' && p[1] == quote)
855 string_local[ind] = (gdb_byte) quote;
856 ind++;
857 p += 2;
859 else if (*p)
861 string_local[ind] = (gdb_byte) (*p);
862 ind++;
863 p++;
866 if (*p != quote)
868 sym->set_aclass_index (LOC_CONST);
869 sym->set_type (error_type (&p, objfile));
870 sym->set_domain (VAR_DOMAIN);
871 add_symbol_to_list (sym, get_file_symbols ());
872 return sym;
875 /* NULL terminate the string. */
876 string_local[ind] = 0;
877 type_allocator alloc (objfile, get_current_subfile ()->language);
878 range_type
879 = create_static_range_type (alloc,
880 builtin_type (objfile)->builtin_int,
881 0, ind);
882 sym->set_type
883 (create_array_type (alloc, builtin_type (objfile)->builtin_char,
884 range_type));
885 string_value
886 = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, ind + 1);
887 memcpy (string_value, string_local, ind + 1);
888 p++;
890 sym->set_value_bytes (string_value);
891 sym->set_aclass_index (LOC_CONST_BYTES);
893 break;
895 case 'e':
896 /* SYMBOL:c=eTYPE,INTVALUE for a constant symbol whose value
897 can be represented as integral.
898 e.g. "b:c=e6,0" for "const b = blob1"
899 (where type 6 is defined by "blobs:t6=eblob1:0,blob2:1,;"). */
901 sym->set_aclass_index (LOC_CONST);
902 sym->set_type (read_type (&p, objfile));
904 if (*p != ',')
906 sym->set_type (error_type (&p, objfile));
907 break;
909 ++p;
911 /* If the value is too big to fit in an int (perhaps because
912 it is unsigned), or something like that, we silently get
913 a bogus value. The type and everything else about it is
914 correct. Ideally, we should be using whatever we have
915 available for parsing unsigned and long long values,
916 however. */
917 sym->set_value_longest (atoi (p));
919 break;
920 default:
922 sym->set_aclass_index (LOC_CONST);
923 sym->set_type (error_type (&p, objfile));
926 sym->set_domain (VAR_DOMAIN);
927 add_symbol_to_list (sym, get_file_symbols ());
928 return sym;
930 case 'C':
931 /* The name of a caught exception. */
932 sym->set_type (read_type (&p, objfile));
933 sym->set_aclass_index (LOC_LABEL);
934 sym->set_domain (VAR_DOMAIN);
935 sym->set_value_address (valu);
936 add_symbol_to_list (sym, get_local_symbols ());
937 break;
939 case 'f':
940 /* A static function definition. */
941 sym->set_type (read_type (&p, objfile));
942 sym->set_aclass_index (LOC_BLOCK);
943 sym->set_domain (VAR_DOMAIN);
944 add_symbol_to_list (sym, get_file_symbols ());
945 /* fall into process_function_types. */
947 process_function_types:
948 /* Function result types are described as the result type in stabs.
949 We need to convert this to the function-returning-type-X type
950 in GDB. E.g. "int" is converted to "function returning int". */
951 if (sym->type ()->code () != TYPE_CODE_FUNC)
952 sym->set_type (lookup_function_type (sym->type ()));
954 /* All functions in C++ have prototypes. Stabs does not offer an
955 explicit way to identify prototyped or unprototyped functions,
956 but both GCC and Sun CC emit stabs for the "call-as" type rather
957 than the "declared-as" type for unprototyped functions, so
958 we treat all functions as if they were prototyped. This is used
959 primarily for promotion when calling the function from GDB. */
960 sym->type ()->set_is_prototyped (true);
962 /* fall into process_prototype_types. */
964 process_prototype_types:
965 /* Sun acc puts declared types of arguments here. */
966 if (*p == ';')
968 struct type *ftype = sym->type ();
969 int nsemi = 0;
970 int nparams = 0;
971 const char *p1 = p;
973 /* Obtain a worst case guess for the number of arguments
974 by counting the semicolons. */
975 while (*p1)
977 if (*p1++ == ';')
978 nsemi++;
981 /* Allocate parameter information fields and fill them in. */
982 ftype->alloc_fields (nsemi);
983 while (*p++ == ';')
985 struct type *ptype;
987 /* A type number of zero indicates the start of varargs.
988 FIXME: GDB currently ignores vararg functions. */
989 if (p[0] == '0' && p[1] == '\0')
990 break;
991 ptype = read_type (&p, objfile);
993 /* The Sun compilers mark integer arguments, which should
994 be promoted to the width of the calling conventions, with
995 a type which references itself. This type is turned into
996 a TYPE_CODE_VOID type by read_type, and we have to turn
997 it back into builtin_int here.
998 FIXME: Do we need a new builtin_promoted_int_arg ? */
999 if (ptype->code () == TYPE_CODE_VOID)
1000 ptype = builtin_type (objfile)->builtin_int;
1001 ftype->field (nparams).set_type (ptype);
1002 ftype->field (nparams).set_is_artificial (false);
1003 nparams++;
1005 ftype->set_num_fields (nparams);
1006 ftype->set_is_prototyped (true);
1008 break;
1010 case 'F':
1011 /* A global function definition. */
1012 sym->set_type (read_type (&p, objfile));
1013 sym->set_aclass_index (LOC_BLOCK);
1014 sym->set_domain (VAR_DOMAIN);
1015 add_symbol_to_list (sym, get_global_symbols ());
1016 goto process_function_types;
1018 case 'G':
1019 /* For a class G (global) symbol, it appears that the
1020 value is not correct. It is necessary to search for the
1021 corresponding linker definition to find the value.
1022 These definitions appear at the end of the namelist. */
1023 sym->set_type (read_type (&p, objfile));
1024 sym->set_aclass_index (LOC_STATIC);
1025 sym->set_domain (VAR_DOMAIN);
1026 /* Don't add symbol references to global_sym_chain.
1027 Symbol references don't have valid names and wont't match up with
1028 minimal symbols when the global_sym_chain is relocated.
1029 We'll fixup symbol references when we fixup the defining symbol. */
1030 if (sym->linkage_name () && sym->linkage_name ()[0] != '#')
1032 i = hashname (sym->linkage_name ());
1033 sym->set_value_chain (global_sym_chain[i]);
1034 global_sym_chain[i] = sym;
1036 add_symbol_to_list (sym, get_global_symbols ());
1037 break;
1039 /* This case is faked by a conditional above,
1040 when there is no code letter in the dbx data.
1041 Dbx data never actually contains 'l'. */
1042 case 's':
1043 case 'l':
1044 sym->set_type (read_type (&p, objfile));
1045 sym->set_aclass_index (LOC_LOCAL);
1046 sym->set_value_longest (valu);
1047 sym->set_domain (VAR_DOMAIN);
1048 add_symbol_to_list (sym, get_local_symbols ());
1049 break;
1051 case 'p':
1052 if (*p == 'F')
1053 /* pF is a two-letter code that means a function parameter in Fortran.
1054 The type-number specifies the type of the return value.
1055 Translate it into a pointer-to-function type. */
1057 p++;
1058 sym->set_type
1059 (lookup_pointer_type
1060 (lookup_function_type (read_type (&p, objfile))));
1062 else
1063 sym->set_type (read_type (&p, objfile));
1065 sym->set_aclass_index (LOC_ARG);
1066 sym->set_value_longest (valu);
1067 sym->set_domain (VAR_DOMAIN);
1068 sym->set_is_argument (1);
1069 add_symbol_to_list (sym, get_local_symbols ());
1071 if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_BIG)
1073 /* On little-endian machines, this crud is never necessary,
1074 and, if the extra bytes contain garbage, is harmful. */
1075 break;
1078 /* If it's gcc-compiled, if it says `short', believe it. */
1079 if (processing_gcc_compilation
1080 || gdbarch_believe_pcc_promotion (gdbarch))
1081 break;
1083 if (!gdbarch_believe_pcc_promotion (gdbarch))
1085 /* If PCC says a parameter is a short or a char, it is
1086 really an int. */
1087 if (sym->type ()->length ()
1088 < gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT
1089 && sym->type ()->code () == TYPE_CODE_INT)
1091 sym->set_type
1092 (sym->type ()->is_unsigned ()
1093 ? builtin_type (objfile)->builtin_unsigned_int
1094 : builtin_type (objfile)->builtin_int);
1096 break;
1098 [[fallthrough]];
1100 case 'P':
1101 /* acc seems to use P to declare the prototypes of functions that
1102 are referenced by this file. gdb is not prepared to deal
1103 with this extra information. FIXME, it ought to. */
1104 if (type == N_FUN)
1106 sym->set_type (read_type (&p, objfile));
1107 goto process_prototype_types;
1109 [[fallthrough]];
1111 case 'R':
1112 /* Parameter which is in a register. */
1113 sym->set_type (read_type (&p, objfile));
1114 sym->set_aclass_index (stab_register_index);
1115 sym->set_is_argument (1);
1116 sym->set_value_longest (valu);
1117 sym->set_domain (VAR_DOMAIN);
1118 add_symbol_to_list (sym, get_local_symbols ());
1119 break;
1121 case 'r':
1122 /* Register variable (either global or local). */
1123 sym->set_type (read_type (&p, objfile));
1124 sym->set_aclass_index (stab_register_index);
1125 sym->set_value_longest (valu);
1126 sym->set_domain (VAR_DOMAIN);
1127 if (within_function)
1129 /* Sun cc uses a pair of symbols, one 'p' and one 'r', with
1130 the same name to represent an argument passed in a
1131 register. GCC uses 'P' for the same case. So if we find
1132 such a symbol pair we combine it into one 'P' symbol.
1133 For Sun cc we need to do this regardless of stabs_argument_has_addr, because the compiler puts out
1134 the 'p' symbol even if it never saves the argument onto
1135 the stack.
1137 On most machines, we want to preserve both symbols, so
1138 that we can still get information about what is going on
1139 with the stack (VAX for computing args_printed, using
1140 stack slots instead of saved registers in backtraces,
1141 etc.).
1143 Note that this code illegally combines
1144 main(argc) struct foo argc; { register struct foo argc; }
1145 but this case is considered pathological and causes a warning
1146 from a decent compiler. */
1148 struct pending *local_symbols = *get_local_symbols ();
1149 if (local_symbols
1150 && local_symbols->nsyms > 0
1151 && gdbarch_stabs_argument_has_addr (gdbarch, sym->type ()))
1153 struct symbol *prev_sym;
1155 prev_sym = local_symbols->symbol[local_symbols->nsyms - 1];
1156 if ((prev_sym->aclass () == LOC_REF_ARG
1157 || prev_sym->aclass () == LOC_ARG)
1158 && strcmp (prev_sym->linkage_name (),
1159 sym->linkage_name ()) == 0)
1161 prev_sym->set_aclass_index (stab_register_index);
1162 /* Use the type from the LOC_REGISTER; that is the type
1163 that is actually in that register. */
1164 prev_sym->set_type (sym->type ());
1165 prev_sym->set_value_longest (sym->value_longest ());
1166 sym = prev_sym;
1167 break;
1170 add_symbol_to_list (sym, get_local_symbols ());
1172 else
1173 add_symbol_to_list (sym, get_file_symbols ());
1174 break;
1176 case 'S':
1177 /* Static symbol at top level of file. */
1178 sym->set_type (read_type (&p, objfile));
1179 sym->set_aclass_index (LOC_STATIC);
1180 sym->set_value_address (valu);
1181 sym->set_domain (VAR_DOMAIN);
1182 add_symbol_to_list (sym, get_file_symbols ());
1183 break;
1185 case 't':
1186 /* In Ada, there is no distinction between typedef and non-typedef;
1187 any type declaration implicitly has the equivalent of a typedef,
1188 and thus 't' is in fact equivalent to 'Tt'.
1190 Therefore, for Ada units, we check the character immediately
1191 before the 't', and if we do not find a 'T', then make sure to
1192 create the associated symbol in the STRUCT_DOMAIN ('t' definitions
1193 will be stored in the VAR_DOMAIN). If the symbol was indeed
1194 defined as 'Tt' then the STRUCT_DOMAIN symbol will be created
1195 elsewhere, so we don't need to take care of that.
1197 This is important to do, because of forward references:
1198 The cleanup of undefined types stored in undef_types only uses
1199 STRUCT_DOMAIN symbols to perform the replacement. */
1200 synonym = (sym->language () == language_ada && p[-2] != 'T');
1202 /* Typedef */
1203 sym->set_type (read_type (&p, objfile));
1205 /* For a nameless type, we don't want a create a symbol, thus we
1206 did not use `sym'. Return without further processing. */
1207 if (nameless)
1208 return NULL;
1210 sym->set_aclass_index (LOC_TYPEDEF);
1211 sym->set_value_longest (valu);
1212 sym->set_domain (VAR_DOMAIN);
1213 /* C++ vagaries: we may have a type which is derived from
1214 a base type which did not have its name defined when the
1215 derived class was output. We fill in the derived class's
1216 base part member's name here in that case. */
1217 if (sym->type ()->name () != NULL)
1218 if ((sym->type ()->code () == TYPE_CODE_STRUCT
1219 || sym->type ()->code () == TYPE_CODE_UNION)
1220 && TYPE_N_BASECLASSES (sym->type ()))
1222 int j;
1224 for (j = TYPE_N_BASECLASSES (sym->type ()) - 1; j >= 0; j--)
1225 if (TYPE_BASECLASS_NAME (sym->type (), j) == 0)
1226 sym->type ()->field (j).set_name
1227 (TYPE_BASECLASS (sym->type (), j)->name ());
1230 if (sym->type ()->name () == NULL)
1232 if ((sym->type ()->code () == TYPE_CODE_PTR
1233 && strcmp (sym->linkage_name (), vtbl_ptr_name))
1234 || sym->type ()->code () == TYPE_CODE_FUNC)
1236 /* If we are giving a name to a type such as "pointer to
1237 foo" or "function returning foo", we better not set
1238 the TYPE_NAME. If the program contains "typedef char
1239 *caddr_t;", we don't want all variables of type char
1240 * to print as caddr_t. This is not just a
1241 consequence of GDB's type management; PCC and GCC (at
1242 least through version 2.4) both output variables of
1243 either type char * or caddr_t with the type number
1244 defined in the 't' symbol for caddr_t. If a future
1245 compiler cleans this up it GDB is not ready for it
1246 yet, but if it becomes ready we somehow need to
1247 disable this check (without breaking the PCC/GCC2.4
1248 case).
1250 Sigh.
1252 Fortunately, this check seems not to be necessary
1253 for anything except pointers or functions. */
1254 /* ezannoni: 2000-10-26. This seems to apply for
1255 versions of gcc older than 2.8. This was the original
1256 problem: with the following code gdb would tell that
1257 the type for name1 is caddr_t, and func is char().
1259 typedef char *caddr_t;
1260 char *name2;
1261 struct x
1263 char *name1;
1264 } xx;
1265 char *func()
1268 main () {}
1271 /* Pascal accepts names for pointer types. */
1272 if (get_current_subfile ()->language == language_pascal)
1273 sym->type ()->set_name (sym->linkage_name ());
1275 else
1276 sym->type ()->set_name (sym->linkage_name ());
1279 add_symbol_to_list (sym, get_file_symbols ());
1281 if (synonym)
1283 /* Create the STRUCT_DOMAIN clone. */
1284 struct symbol *struct_sym = new (&objfile->objfile_obstack) symbol;
1286 *struct_sym = *sym;
1287 struct_sym->set_aclass_index (LOC_TYPEDEF);
1288 struct_sym->set_value_longest (valu);
1289 struct_sym->set_domain (STRUCT_DOMAIN);
1290 if (sym->type ()->name () == 0)
1291 sym->type ()->set_name
1292 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
1293 (char *) NULL));
1294 add_symbol_to_list (struct_sym, get_file_symbols ());
1297 break;
1299 case 'T':
1300 /* Struct, union, or enum tag. For GNU C++, this can be be followed
1301 by 't' which means we are typedef'ing it as well. */
1302 synonym = *p == 't';
1304 if (synonym)
1305 p++;
1307 sym->set_type (read_type (&p, objfile));
1309 /* For a nameless type, we don't want a create a symbol, thus we
1310 did not use `sym'. Return without further processing. */
1311 if (nameless)
1312 return NULL;
1314 sym->set_aclass_index (LOC_TYPEDEF);
1315 sym->set_value_longest (valu);
1316 sym->set_domain (STRUCT_DOMAIN);
1317 if (sym->type ()->name () == 0)
1318 sym->type ()->set_name
1319 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
1320 (char *) NULL));
1321 add_symbol_to_list (sym, get_file_symbols ());
1323 if (synonym)
1325 /* Clone the sym and then modify it. */
1326 struct symbol *typedef_sym = new (&objfile->objfile_obstack) symbol;
1328 *typedef_sym = *sym;
1329 typedef_sym->set_aclass_index (LOC_TYPEDEF);
1330 typedef_sym->set_value_longest (valu);
1331 typedef_sym->set_domain (VAR_DOMAIN);
1332 if (sym->type ()->name () == 0)
1333 sym->type ()->set_name
1334 (obconcat (&objfile->objfile_obstack, sym->linkage_name (),
1335 (char *) NULL));
1336 add_symbol_to_list (typedef_sym, get_file_symbols ());
1338 break;
1340 case 'V':
1341 /* Static symbol of local scope. */
1342 sym->set_type (read_type (&p, objfile));
1343 sym->set_aclass_index (LOC_STATIC);
1344 sym->set_value_address (valu);
1345 sym->set_domain (VAR_DOMAIN);
1346 add_symbol_to_list (sym, get_local_symbols ());
1347 break;
1349 case 'v':
1350 /* Reference parameter */
1351 sym->set_type (read_type (&p, objfile));
1352 sym->set_aclass_index (LOC_REF_ARG);
1353 sym->set_is_argument (1);
1354 sym->set_value_longest (valu);
1355 sym->set_domain (VAR_DOMAIN);
1356 add_symbol_to_list (sym, get_local_symbols ());
1357 break;
1359 case 'a':
1360 /* Reference parameter which is in a register. */
1361 sym->set_type (read_type (&p, objfile));
1362 sym->set_aclass_index (stab_regparm_index);
1363 sym->set_is_argument (1);
1364 sym->set_value_longest (valu);
1365 sym->set_domain (VAR_DOMAIN);
1366 add_symbol_to_list (sym, get_local_symbols ());
1367 break;
1369 case 'X':
1370 /* This is used by Sun FORTRAN for "function result value".
1371 Sun claims ("dbx and dbxtool interfaces", 2nd ed)
1372 that Pascal uses it too, but when I tried it Pascal used
1373 "x:3" (local symbol) instead. */
1374 sym->set_type (read_type (&p, objfile));
1375 sym->set_aclass_index (LOC_LOCAL);
1376 sym->set_value_longest (valu);
1377 sym->set_domain (VAR_DOMAIN);
1378 add_symbol_to_list (sym, get_local_symbols ());
1379 break;
1381 default:
1382 sym->set_type (error_type (&p, objfile));
1383 sym->set_aclass_index (LOC_CONST);
1384 sym->set_value_longest (0);
1385 sym->set_domain (VAR_DOMAIN);
1386 add_symbol_to_list (sym, get_file_symbols ());
1387 break;
1390 /* Some systems pass variables of certain types by reference instead
1391 of by value, i.e. they will pass the address of a structure (in a
1392 register or on the stack) instead of the structure itself. */
1394 if (gdbarch_stabs_argument_has_addr (gdbarch, sym->type ())
1395 && sym->is_argument ())
1397 /* We have to convert LOC_REGISTER to LOC_REGPARM_ADDR (for
1398 variables passed in a register). */
1399 if (sym->aclass () == LOC_REGISTER)
1400 sym->set_aclass_index (LOC_REGPARM_ADDR);
1401 /* Likewise for converting LOC_ARG to LOC_REF_ARG (for the 7th
1402 and subsequent arguments on SPARC, for example). */
1403 else if (sym->aclass () == LOC_ARG)
1404 sym->set_aclass_index (LOC_REF_ARG);
1407 return sym;
1410 /* Skip rest of this symbol and return an error type.
1412 General notes on error recovery: error_type always skips to the
1413 end of the symbol (modulo cretinous dbx symbol name continuation).
1414 Thus code like this:
1416 if (*(*pp)++ != ';')
1417 return error_type (pp, objfile);
1419 is wrong because if *pp starts out pointing at '\0' (typically as the
1420 result of an earlier error), it will be incremented to point to the
1421 start of the next symbol, which might produce strange results, at least
1422 if you run off the end of the string table. Instead use
1424 if (**pp != ';')
1425 return error_type (pp, objfile);
1426 ++*pp;
1430 if (**pp != ';')
1431 foo = error_type (pp, objfile);
1432 else
1433 ++*pp;
1435 And in case it isn't obvious, the point of all this hair is so the compiler
1436 can define new types and new syntaxes, and old versions of the
1437 debugger will be able to read the new symbol tables. */
1439 static struct type *
1440 error_type (const char **pp, struct objfile *objfile)
1442 complaint (_("couldn't parse type; debugger out of date?"));
1443 while (1)
1445 /* Skip to end of symbol. */
1446 while (**pp != '\0')
1448 (*pp)++;
1451 /* Check for and handle cretinous dbx symbol name continuation! */
1452 if ((*pp)[-1] == '\\' || (*pp)[-1] == '?')
1454 *pp = next_symbol_text (objfile);
1456 else
1458 break;
1461 return builtin_type (objfile)->builtin_error;
1465 /* Allocate a stub method whose return type is TYPE. This apparently
1466 happens for speed of symbol reading, since parsing out the
1467 arguments to the method is cpu-intensive, the way we are doing it.
1468 So, we will fill in arguments later. This always returns a fresh
1469 type. */
1471 static struct type *
1472 allocate_stub_method (struct type *type)
1474 struct type *mtype;
1476 mtype = type_allocator (type).new_type ();
1477 mtype->set_code (TYPE_CODE_METHOD);
1478 mtype->set_length (1);
1479 mtype->set_is_stub (true);
1480 mtype->set_target_type (type);
1481 /* TYPE_SELF_TYPE (mtype) = unknown yet */
1482 return mtype;
1485 /* Read type information or a type definition; return the type. Even
1486 though this routine accepts either type information or a type
1487 definition, the distinction is relevant--some parts of stabsread.c
1488 assume that type information starts with a digit, '-', or '(' in
1489 deciding whether to call read_type. */
1491 static struct type *
1492 read_type (const char **pp, struct objfile *objfile)
1494 struct type *type = 0;
1495 struct type *type1;
1496 int typenums[2];
1497 char type_descriptor;
1499 /* Size in bits of type if specified by a type attribute, or -1 if
1500 there is no size attribute. */
1501 int type_size = -1;
1503 /* Used to distinguish string and bitstring from char-array and set. */
1504 int is_string = 0;
1506 /* Used to distinguish vector from array. */
1507 int is_vector = 0;
1509 /* Read type number if present. The type number may be omitted.
1510 for instance in a two-dimensional array declared with type
1511 "ar1;1;10;ar1;1;10;4". */
1512 if ((**pp >= '0' && **pp <= '9')
1513 || **pp == '('
1514 || **pp == '-')
1516 if (read_type_number (pp, typenums) != 0)
1517 return error_type (pp, objfile);
1519 if (**pp != '=')
1521 /* Type is not being defined here. Either it already
1522 exists, or this is a forward reference to it.
1523 dbx_alloc_type handles both cases. */
1524 type = dbx_alloc_type (typenums, objfile);
1526 /* If this is a forward reference, arrange to complain if it
1527 doesn't get patched up by the time we're done
1528 reading. */
1529 if (type->code () == TYPE_CODE_UNDEF)
1530 add_undefined_type (type, typenums);
1532 return type;
1535 /* Type is being defined here. */
1536 /* Skip the '='.
1537 Also skip the type descriptor - we get it below with (*pp)[-1]. */
1538 (*pp) += 2;
1540 else
1542 /* 'typenums=' not present, type is anonymous. Read and return
1543 the definition, but don't put it in the type vector. */
1544 typenums[0] = typenums[1] = -1;
1545 (*pp)++;
1548 again:
1549 type_descriptor = (*pp)[-1];
1550 switch (type_descriptor)
1552 case 'x':
1554 enum type_code code;
1556 /* Used to index through file_symbols. */
1557 struct pending *ppt;
1558 int i;
1560 /* Name including "struct", etc. */
1561 char *type_name;
1564 const char *from, *p, *q1, *q2;
1566 /* Set the type code according to the following letter. */
1567 switch ((*pp)[0])
1569 case 's':
1570 code = TYPE_CODE_STRUCT;
1571 break;
1572 case 'u':
1573 code = TYPE_CODE_UNION;
1574 break;
1575 case 'e':
1576 code = TYPE_CODE_ENUM;
1577 break;
1578 default:
1580 /* Complain and keep going, so compilers can invent new
1581 cross-reference types. */
1582 complaint (_("Unrecognized cross-reference type `%c'"),
1583 (*pp)[0]);
1584 code = TYPE_CODE_STRUCT;
1585 break;
1589 q1 = strchr (*pp, '<');
1590 p = strchr (*pp, ':');
1591 if (p == NULL)
1592 return error_type (pp, objfile);
1593 if (q1 && p > q1 && p[1] == ':')
1595 int nesting_level = 0;
1597 for (q2 = q1; *q2; q2++)
1599 if (*q2 == '<')
1600 nesting_level++;
1601 else if (*q2 == '>')
1602 nesting_level--;
1603 else if (*q2 == ':' && nesting_level == 0)
1604 break;
1606 p = q2;
1607 if (*p != ':')
1608 return error_type (pp, objfile);
1610 type_name = NULL;
1611 if (get_current_subfile ()->language == language_cplus)
1613 std::string name (*pp, p - *pp);
1614 gdb::unique_xmalloc_ptr<char> new_name
1615 = cp_canonicalize_string (name.c_str ());
1616 if (new_name != nullptr)
1617 type_name = obstack_strdup (&objfile->objfile_obstack,
1618 new_name.get ());
1620 else if (get_current_subfile ()->language == language_c)
1622 std::string name (*pp, p - *pp);
1623 gdb::unique_xmalloc_ptr<char> new_name
1624 = c_canonicalize_name (name.c_str ());
1625 if (new_name != nullptr)
1626 type_name = obstack_strdup (&objfile->objfile_obstack,
1627 new_name.get ());
1629 if (type_name == NULL)
1631 char *to = type_name = (char *)
1632 obstack_alloc (&objfile->objfile_obstack, p - *pp + 1);
1634 /* Copy the name. */
1635 from = *pp + 1;
1636 while (from < p)
1637 *to++ = *from++;
1638 *to = '\0';
1641 /* Set the pointer ahead of the name which we just read, and
1642 the colon. */
1643 *pp = p + 1;
1646 /* If this type has already been declared, then reuse the same
1647 type, rather than allocating a new one. This saves some
1648 memory. */
1650 for (ppt = *get_file_symbols (); ppt; ppt = ppt->next)
1651 for (i = 0; i < ppt->nsyms; i++)
1653 struct symbol *sym = ppt->symbol[i];
1655 if (sym->aclass () == LOC_TYPEDEF
1656 && sym->domain () == STRUCT_DOMAIN
1657 && (sym->type ()->code () == code)
1658 && strcmp (sym->linkage_name (), type_name) == 0)
1660 obstack_free (&objfile->objfile_obstack, type_name);
1661 type = sym->type ();
1662 if (typenums[0] != -1)
1663 *dbx_lookup_type (typenums, objfile) = type;
1664 return type;
1668 /* Didn't find the type to which this refers, so we must
1669 be dealing with a forward reference. Allocate a type
1670 structure for it, and keep track of it so we can
1671 fill in the rest of the fields when we get the full
1672 type. */
1673 type = dbx_alloc_type (typenums, objfile);
1674 type->set_code (code);
1675 type->set_name (type_name);
1676 INIT_CPLUS_SPECIFIC (type);
1677 type->set_is_stub (true);
1679 add_undefined_type (type, typenums);
1680 return type;
1683 case '-': /* RS/6000 built-in type */
1684 case '0':
1685 case '1':
1686 case '2':
1687 case '3':
1688 case '4':
1689 case '5':
1690 case '6':
1691 case '7':
1692 case '8':
1693 case '9':
1694 case '(':
1695 (*pp)--;
1697 /* We deal with something like t(1,2)=(3,4)=... which
1698 the Lucid compiler and recent gcc versions (post 2.7.3) use. */
1700 /* Allocate and enter the typedef type first.
1701 This handles recursive types. */
1702 type = dbx_alloc_type (typenums, objfile);
1703 type->set_code (TYPE_CODE_TYPEDEF);
1705 struct type *xtype = read_type (pp, objfile);
1707 if (type == xtype)
1709 /* It's being defined as itself. That means it is "void". */
1710 type->set_code (TYPE_CODE_VOID);
1711 type->set_length (1);
1713 else if (type_size >= 0 || is_string)
1715 /* This is the absolute wrong way to construct types. Every
1716 other debug format has found a way around this problem and
1717 the related problems with unnecessarily stubbed types;
1718 someone motivated should attempt to clean up the issue
1719 here as well. Once a type pointed to has been created it
1720 should not be modified.
1722 Well, it's not *absolutely* wrong. Constructing recursive
1723 types (trees, linked lists) necessarily entails modifying
1724 types after creating them. Constructing any loop structure
1725 entails side effects. The Dwarf 2 reader does handle this
1726 more gracefully (it never constructs more than once
1727 instance of a type object, so it doesn't have to copy type
1728 objects wholesale), but it still mutates type objects after
1729 other folks have references to them.
1731 Keep in mind that this circularity/mutation issue shows up
1732 at the source language level, too: C's "incomplete types",
1733 for example. So the proper cleanup, I think, would be to
1734 limit GDB's type smashing to match exactly those required
1735 by the source language. So GDB could have a
1736 "complete_this_type" function, but never create unnecessary
1737 copies of a type otherwise. */
1738 replace_type (type, xtype);
1739 type->set_name (NULL);
1741 else
1743 type->set_target_is_stub (true);
1744 type->set_target_type (xtype);
1747 break;
1749 /* In the following types, we must be sure to overwrite any existing
1750 type that the typenums refer to, rather than allocating a new one
1751 and making the typenums point to the new one. This is because there
1752 may already be pointers to the existing type (if it had been
1753 forward-referenced), and we must change it to a pointer, function,
1754 reference, or whatever, *in-place*. */
1756 case '*': /* Pointer to another type */
1757 type1 = read_type (pp, objfile);
1758 type = make_pointer_type (type1, dbx_lookup_type (typenums, objfile));
1759 break;
1761 case '&': /* Reference to another type */
1762 type1 = read_type (pp, objfile);
1763 type = make_reference_type (type1, dbx_lookup_type (typenums, objfile),
1764 TYPE_CODE_REF);
1765 break;
1767 case 'f': /* Function returning another type */
1768 type1 = read_type (pp, objfile);
1769 type = make_function_type (type1, dbx_lookup_type (typenums, objfile));
1770 break;
1772 case 'g': /* Prototyped function. (Sun) */
1774 /* Unresolved questions:
1776 - According to Sun's ``STABS Interface Manual'', for 'f'
1777 and 'F' symbol descriptors, a `0' in the argument type list
1778 indicates a varargs function. But it doesn't say how 'g'
1779 type descriptors represent that info. Someone with access
1780 to Sun's toolchain should try it out.
1782 - According to the comment in define_symbol (search for
1783 `process_prototype_types:'), Sun emits integer arguments as
1784 types which ref themselves --- like `void' types. Do we
1785 have to deal with that here, too? Again, someone with
1786 access to Sun's toolchain should try it out and let us
1787 know. */
1789 const char *type_start = (*pp) - 1;
1790 struct type *return_type = read_type (pp, objfile);
1791 struct type *func_type
1792 = make_function_type (return_type,
1793 dbx_lookup_type (typenums, objfile));
1794 struct type_list {
1795 struct type *type;
1796 struct type_list *next;
1797 } *arg_types = 0;
1798 int num_args = 0;
1800 while (**pp && **pp != '#')
1802 struct type *arg_type = read_type (pp, objfile);
1803 struct type_list *newobj = XALLOCA (struct type_list);
1804 newobj->type = arg_type;
1805 newobj->next = arg_types;
1806 arg_types = newobj;
1807 num_args++;
1809 if (**pp == '#')
1810 ++*pp;
1811 else
1813 complaint (_("Prototyped function type didn't "
1814 "end arguments with `#':\n%s"),
1815 type_start);
1818 /* If there is just one argument whose type is `void', then
1819 that's just an empty argument list. */
1820 if (arg_types
1821 && ! arg_types->next
1822 && arg_types->type->code () == TYPE_CODE_VOID)
1823 num_args = 0;
1825 func_type->alloc_fields (num_args);
1827 int i;
1828 struct type_list *t;
1830 /* We stuck each argument type onto the front of the list
1831 when we read it, so the list is reversed. Build the
1832 fields array right-to-left. */
1833 for (t = arg_types, i = num_args - 1; t; t = t->next, i--)
1834 func_type->field (i).set_type (t->type);
1836 func_type->set_num_fields (num_args);
1837 func_type->set_is_prototyped (true);
1839 type = func_type;
1840 break;
1843 case 'k': /* Const qualifier on some type (Sun) */
1844 type = read_type (pp, objfile);
1845 type = make_cv_type (1, TYPE_VOLATILE (type), type,
1846 dbx_lookup_type (typenums, objfile));
1847 break;
1849 case 'B': /* Volatile qual on some type (Sun) */
1850 type = read_type (pp, objfile);
1851 type = make_cv_type (TYPE_CONST (type), 1, type,
1852 dbx_lookup_type (typenums, objfile));
1853 break;
1855 case '@':
1856 if (isdigit (**pp) || **pp == '(' || **pp == '-')
1857 { /* Member (class & variable) type */
1858 /* FIXME -- we should be doing smash_to_XXX types here. */
1860 struct type *domain = read_type (pp, objfile);
1861 struct type *memtype;
1863 if (**pp != ',')
1864 /* Invalid member type data format. */
1865 return error_type (pp, objfile);
1866 ++*pp;
1868 memtype = read_type (pp, objfile);
1869 type = dbx_alloc_type (typenums, objfile);
1870 smash_to_memberptr_type (type, domain, memtype);
1872 else
1873 /* type attribute */
1875 const char *attr = *pp;
1877 /* Skip to the semicolon. */
1878 while (**pp != ';' && **pp != '\0')
1879 ++(*pp);
1880 if (**pp == '\0')
1881 return error_type (pp, objfile);
1882 else
1883 ++ * pp; /* Skip the semicolon. */
1885 switch (*attr)
1887 case 's': /* Size attribute */
1888 type_size = atoi (attr + 1);
1889 if (type_size <= 0)
1890 type_size = -1;
1891 break;
1893 case 'S': /* String attribute */
1894 /* FIXME: check to see if following type is array? */
1895 is_string = 1;
1896 break;
1898 case 'V': /* Vector attribute */
1899 /* FIXME: check to see if following type is array? */
1900 is_vector = 1;
1901 break;
1903 default:
1904 /* Ignore unrecognized type attributes, so future compilers
1905 can invent new ones. */
1906 break;
1908 ++*pp;
1909 goto again;
1911 break;
1913 case '#': /* Method (class & fn) type */
1914 if ((*pp)[0] == '#')
1916 /* We'll get the parameter types from the name. */
1917 struct type *return_type;
1919 (*pp)++;
1920 return_type = read_type (pp, objfile);
1921 if (*(*pp)++ != ';')
1922 complaint (_("invalid (minimal) member type "
1923 "data format at symtab pos %d."),
1924 symnum);
1925 type = allocate_stub_method (return_type);
1926 if (typenums[0] != -1)
1927 *dbx_lookup_type (typenums, objfile) = type;
1929 else
1931 struct type *domain = read_type (pp, objfile);
1932 struct type *return_type;
1933 struct field *args;
1934 int nargs, varargs;
1936 if (**pp != ',')
1937 /* Invalid member type data format. */
1938 return error_type (pp, objfile);
1939 else
1940 ++(*pp);
1942 return_type = read_type (pp, objfile);
1943 args = read_args (pp, ';', objfile, &nargs, &varargs);
1944 if (args == NULL)
1945 return error_type (pp, objfile);
1946 type = dbx_alloc_type (typenums, objfile);
1947 smash_to_method_type (type, domain, return_type, args,
1948 nargs, varargs);
1950 break;
1952 case 'r': /* Range type */
1953 type = read_range_type (pp, typenums, type_size, objfile);
1954 if (typenums[0] != -1)
1955 *dbx_lookup_type (typenums, objfile) = type;
1956 break;
1958 case 'b':
1960 /* Sun ACC builtin int type */
1961 type = read_sun_builtin_type (pp, typenums, objfile);
1962 if (typenums[0] != -1)
1963 *dbx_lookup_type (typenums, objfile) = type;
1965 break;
1967 case 'R': /* Sun ACC builtin float type */
1968 type = read_sun_floating_type (pp, typenums, objfile);
1969 if (typenums[0] != -1)
1970 *dbx_lookup_type (typenums, objfile) = type;
1971 break;
1973 case 'e': /* Enumeration type */
1974 type = dbx_alloc_type (typenums, objfile);
1975 type = read_enum_type (pp, type, objfile);
1976 if (typenums[0] != -1)
1977 *dbx_lookup_type (typenums, objfile) = type;
1978 break;
1980 case 's': /* Struct type */
1981 case 'u': /* Union type */
1983 enum type_code type_code = TYPE_CODE_UNDEF;
1984 type = dbx_alloc_type (typenums, objfile);
1985 switch (type_descriptor)
1987 case 's':
1988 type_code = TYPE_CODE_STRUCT;
1989 break;
1990 case 'u':
1991 type_code = TYPE_CODE_UNION;
1992 break;
1994 type = read_struct_type (pp, type, type_code, objfile);
1995 break;
1998 case 'a': /* Array type */
1999 if (**pp != 'r')
2000 return error_type (pp, objfile);
2001 ++*pp;
2003 type = dbx_alloc_type (typenums, objfile);
2004 type = read_array_type (pp, type, objfile);
2005 if (is_string)
2006 type->set_code (TYPE_CODE_STRING);
2007 if (is_vector)
2008 make_vector_type (type);
2009 break;
2011 case 'S': /* Set type */
2013 type1 = read_type (pp, objfile);
2014 type_allocator alloc (objfile, get_current_subfile ()->language);
2015 type = create_set_type (alloc, type1);
2016 if (typenums[0] != -1)
2017 *dbx_lookup_type (typenums, objfile) = type;
2019 break;
2021 default:
2022 --*pp; /* Go back to the symbol in error. */
2023 /* Particularly important if it was \0! */
2024 return error_type (pp, objfile);
2027 if (type == 0)
2029 warning (_("GDB internal error, type is NULL in stabsread.c."));
2030 return error_type (pp, objfile);
2033 /* Size specified in a type attribute overrides any other size. */
2034 if (type_size != -1)
2035 type->set_length ((type_size + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT);
2037 return type;
2040 /* RS/6000 xlc/dbx combination uses a set of builtin types, starting from -1.
2041 Return the proper type node for a given builtin type number. */
2043 static const registry<objfile>::key<struct type *,
2044 gdb::noop_deleter<struct type *>>
2045 rs6000_builtin_type_data;
2047 static struct type *
2048 rs6000_builtin_type (int typenum, struct objfile *objfile)
2050 struct type **negative_types = rs6000_builtin_type_data.get (objfile);
2052 /* We recognize types numbered from -NUMBER_RECOGNIZED to -1. */
2053 #define NUMBER_RECOGNIZED 34
2054 struct type *rettype = NULL;
2056 if (typenum >= 0 || typenum < -NUMBER_RECOGNIZED)
2058 complaint (_("Unknown builtin type %d"), typenum);
2059 return builtin_type (objfile)->builtin_error;
2062 if (!negative_types)
2064 /* This includes an empty slot for type number -0. */
2065 negative_types = OBSTACK_CALLOC (&objfile->objfile_obstack,
2066 NUMBER_RECOGNIZED + 1, struct type *);
2067 rs6000_builtin_type_data.set (objfile, negative_types);
2070 if (negative_types[-typenum] != NULL)
2071 return negative_types[-typenum];
2073 #if TARGET_CHAR_BIT != 8
2074 #error This code wrong for TARGET_CHAR_BIT not 8
2075 /* These definitions all assume that TARGET_CHAR_BIT is 8. I think
2076 that if that ever becomes not true, the correct fix will be to
2077 make the size in the struct type to be in bits, not in units of
2078 TARGET_CHAR_BIT. */
2079 #endif
2081 type_allocator alloc (objfile, get_current_subfile ()->language);
2082 switch (-typenum)
2084 case 1:
2085 /* The size of this and all the other types are fixed, defined
2086 by the debugging format. If there is a type called "int" which
2087 is other than 32 bits, then it should use a new negative type
2088 number (or avoid negative type numbers for that case).
2089 See stabs.texinfo. */
2090 rettype = init_integer_type (alloc, 32, 0, "int");
2091 break;
2092 case 2:
2093 rettype = init_integer_type (alloc, 8, 0, "char");
2094 rettype->set_has_no_signedness (true);
2095 break;
2096 case 3:
2097 rettype = init_integer_type (alloc, 16, 0, "short");
2098 break;
2099 case 4:
2100 rettype = init_integer_type (alloc, 32, 0, "long");
2101 break;
2102 case 5:
2103 rettype = init_integer_type (alloc, 8, 1, "unsigned char");
2104 break;
2105 case 6:
2106 rettype = init_integer_type (alloc, 8, 0, "signed char");
2107 break;
2108 case 7:
2109 rettype = init_integer_type (alloc, 16, 1, "unsigned short");
2110 break;
2111 case 8:
2112 rettype = init_integer_type (alloc, 32, 1, "unsigned int");
2113 break;
2114 case 9:
2115 rettype = init_integer_type (alloc, 32, 1, "unsigned");
2116 break;
2117 case 10:
2118 rettype = init_integer_type (alloc, 32, 1, "unsigned long");
2119 break;
2120 case 11:
2121 rettype = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
2122 break;
2123 case 12:
2124 /* IEEE single precision (32 bit). */
2125 rettype = init_float_type (alloc, 32, "float",
2126 floatformats_ieee_single);
2127 break;
2128 case 13:
2129 /* IEEE double precision (64 bit). */
2130 rettype = init_float_type (alloc, 64, "double",
2131 floatformats_ieee_double);
2132 break;
2133 case 14:
2134 /* This is an IEEE double on the RS/6000, and different machines with
2135 different sizes for "long double" should use different negative
2136 type numbers. See stabs.texinfo. */
2137 rettype = init_float_type (alloc, 64, "long double",
2138 floatformats_ieee_double);
2139 break;
2140 case 15:
2141 rettype = init_integer_type (alloc, 32, 0, "integer");
2142 break;
2143 case 16:
2144 rettype = init_boolean_type (alloc, 32, 1, "boolean");
2145 break;
2146 case 17:
2147 rettype = init_float_type (alloc, 32, "short real",
2148 floatformats_ieee_single);
2149 break;
2150 case 18:
2151 rettype = init_float_type (alloc, 64, "real",
2152 floatformats_ieee_double);
2153 break;
2154 case 19:
2155 rettype = alloc.new_type (TYPE_CODE_ERROR, 0, "stringptr");
2156 break;
2157 case 20:
2158 rettype = init_character_type (alloc, 8, 1, "character");
2159 break;
2160 case 21:
2161 rettype = init_boolean_type (alloc, 8, 1, "logical*1");
2162 break;
2163 case 22:
2164 rettype = init_boolean_type (alloc, 16, 1, "logical*2");
2165 break;
2166 case 23:
2167 rettype = init_boolean_type (alloc, 32, 1, "logical*4");
2168 break;
2169 case 24:
2170 rettype = init_boolean_type (alloc, 32, 1, "logical");
2171 break;
2172 case 25:
2173 /* Complex type consisting of two IEEE single precision values. */
2174 rettype = init_complex_type ("complex",
2175 rs6000_builtin_type (12, objfile));
2176 break;
2177 case 26:
2178 /* Complex type consisting of two IEEE double precision values. */
2179 rettype = init_complex_type ("double complex",
2180 rs6000_builtin_type (13, objfile));
2181 break;
2182 case 27:
2183 rettype = init_integer_type (alloc, 8, 0, "integer*1");
2184 break;
2185 case 28:
2186 rettype = init_integer_type (alloc, 16, 0, "integer*2");
2187 break;
2188 case 29:
2189 rettype = init_integer_type (alloc, 32, 0, "integer*4");
2190 break;
2191 case 30:
2192 rettype = init_character_type (alloc, 16, 0, "wchar");
2193 break;
2194 case 31:
2195 rettype = init_integer_type (alloc, 64, 0, "long long");
2196 break;
2197 case 32:
2198 rettype = init_integer_type (alloc, 64, 1, "unsigned long long");
2199 break;
2200 case 33:
2201 rettype = init_integer_type (alloc, 64, 1, "logical*8");
2202 break;
2203 case 34:
2204 rettype = init_integer_type (alloc, 64, 0, "integer*8");
2205 break;
2207 negative_types[-typenum] = rettype;
2208 return rettype;
2211 /* This page contains subroutines of read_type. */
2213 /* Wrapper around method_name_from_physname to flag a complaint
2214 if there is an error. */
2216 static char *
2217 stabs_method_name_from_physname (const char *physname)
2219 char *method_name;
2221 method_name = method_name_from_physname (physname);
2223 if (method_name == NULL)
2225 complaint (_("Method has bad physname %s\n"), physname);
2226 return NULL;
2229 return method_name;
2232 /* Read member function stabs info for C++ classes. The form of each member
2233 function data is:
2235 NAME :: TYPENUM[=type definition] ARGS : PHYSNAME ;
2237 An example with two member functions is:
2239 afunc1::20=##15;:i;2A.;afunc2::20:i;2A.;
2241 For the case of overloaded operators, the format is op$::*.funcs, where
2242 $ is the CPLUS_MARKER (usually '$'), `*' holds the place for an operator
2243 name (such as `+=') and `.' marks the end of the operator name.
2245 Returns 1 for success, 0 for failure. */
2247 static int
2248 read_member_functions (struct stab_field_info *fip, const char **pp,
2249 struct type *type, struct objfile *objfile)
2251 int nfn_fields = 0;
2252 int length = 0;
2253 int i;
2254 struct next_fnfield
2256 struct next_fnfield *next;
2257 struct fn_field fn_field;
2259 *sublist;
2260 struct type *look_ahead_type;
2261 struct next_fnfieldlist *new_fnlist;
2262 struct next_fnfield *new_sublist;
2263 char *main_fn_name;
2264 const char *p;
2266 /* Process each list until we find something that is not a member function
2267 or find the end of the functions. */
2269 while (**pp != ';')
2271 /* We should be positioned at the start of the function name.
2272 Scan forward to find the first ':' and if it is not the
2273 first of a "::" delimiter, then this is not a member function. */
2274 p = *pp;
2275 while (*p != ':')
2277 p++;
2279 if (p[1] != ':')
2281 break;
2284 sublist = NULL;
2285 look_ahead_type = NULL;
2286 length = 0;
2288 new_fnlist = OBSTACK_ZALLOC (&fip->obstack, struct next_fnfieldlist);
2290 if ((*pp)[0] == 'o' && (*pp)[1] == 'p' && is_cplus_marker ((*pp)[2]))
2292 /* This is a completely wierd case. In order to stuff in the
2293 names that might contain colons (the usual name delimiter),
2294 Mike Tiemann defined a different name format which is
2295 signalled if the identifier is "op$". In that case, the
2296 format is "op$::XXXX." where XXXX is the name. This is
2297 used for names like "+" or "=". YUUUUUUUK! FIXME! */
2298 /* This lets the user type "break operator+".
2299 We could just put in "+" as the name, but that wouldn't
2300 work for "*". */
2301 static char opname[32] = "op$";
2302 char *o = opname + 3;
2304 /* Skip past '::'. */
2305 *pp = p + 2;
2307 STABS_CONTINUE (pp, objfile);
2308 p = *pp;
2309 while (*p != '.')
2311 *o++ = *p++;
2313 main_fn_name = savestring (opname, o - opname);
2314 /* Skip past '.' */
2315 *pp = p + 1;
2317 else
2319 main_fn_name = savestring (*pp, p - *pp);
2320 /* Skip past '::'. */
2321 *pp = p + 2;
2323 new_fnlist->fn_fieldlist.name = main_fn_name;
2327 new_sublist = OBSTACK_ZALLOC (&fip->obstack, struct next_fnfield);
2329 /* Check for and handle cretinous dbx symbol name continuation! */
2330 if (look_ahead_type == NULL)
2332 /* Normal case. */
2333 STABS_CONTINUE (pp, objfile);
2335 new_sublist->fn_field.type = read_type (pp, objfile);
2336 if (**pp != ':')
2338 /* Invalid symtab info for member function. */
2339 return 0;
2342 else
2344 /* g++ version 1 kludge */
2345 new_sublist->fn_field.type = look_ahead_type;
2346 look_ahead_type = NULL;
2349 (*pp)++;
2350 p = *pp;
2351 while (*p != ';')
2353 p++;
2356 /* These are methods, not functions. */
2357 if (new_sublist->fn_field.type->code () == TYPE_CODE_FUNC)
2358 new_sublist->fn_field.type->set_code (TYPE_CODE_METHOD);
2360 /* If this is just a stub, then we don't have the real name here. */
2361 if (new_sublist->fn_field.type->is_stub ())
2363 if (!TYPE_SELF_TYPE (new_sublist->fn_field.type))
2364 set_type_self_type (new_sublist->fn_field.type, type);
2365 new_sublist->fn_field.is_stub = 1;
2368 new_sublist->fn_field.physname = savestring (*pp, p - *pp);
2369 *pp = p + 1;
2371 /* Set this member function's visibility fields. */
2372 switch (*(*pp)++)
2374 case VISIBILITY_PRIVATE:
2375 new_sublist->fn_field.accessibility = accessibility::PRIVATE;
2376 break;
2377 case VISIBILITY_PROTECTED:
2378 new_sublist->fn_field.accessibility = accessibility::PROTECTED;
2379 break;
2382 STABS_CONTINUE (pp, objfile);
2383 switch (**pp)
2385 case 'A': /* Normal functions. */
2386 new_sublist->fn_field.is_const = 0;
2387 new_sublist->fn_field.is_volatile = 0;
2388 (*pp)++;
2389 break;
2390 case 'B': /* `const' member functions. */
2391 new_sublist->fn_field.is_const = 1;
2392 new_sublist->fn_field.is_volatile = 0;
2393 (*pp)++;
2394 break;
2395 case 'C': /* `volatile' member function. */
2396 new_sublist->fn_field.is_const = 0;
2397 new_sublist->fn_field.is_volatile = 1;
2398 (*pp)++;
2399 break;
2400 case 'D': /* `const volatile' member function. */
2401 new_sublist->fn_field.is_const = 1;
2402 new_sublist->fn_field.is_volatile = 1;
2403 (*pp)++;
2404 break;
2405 case '*': /* File compiled with g++ version 1 --
2406 no info. */
2407 case '?':
2408 case '.':
2409 break;
2410 default:
2411 complaint (_("const/volatile indicator missing, got '%c'"),
2412 **pp);
2413 break;
2416 switch (*(*pp)++)
2418 case '*':
2420 int nbits;
2421 /* virtual member function, followed by index.
2422 The sign bit is set to distinguish pointers-to-methods
2423 from virtual function indicies. Since the array is
2424 in words, the quantity must be shifted left by 1
2425 on 16 bit machine, and by 2 on 32 bit machine, forcing
2426 the sign bit out, and usable as a valid index into
2427 the array. Remove the sign bit here. */
2428 new_sublist->fn_field.voffset =
2429 (0x7fffffff & read_huge_number (pp, ';', &nbits, 0)) + 2;
2430 if (nbits != 0)
2431 return 0;
2433 STABS_CONTINUE (pp, objfile);
2434 if (**pp == ';' || **pp == '\0')
2436 /* Must be g++ version 1. */
2437 new_sublist->fn_field.fcontext = 0;
2439 else
2441 /* Figure out from whence this virtual function came.
2442 It may belong to virtual function table of
2443 one of its baseclasses. */
2444 look_ahead_type = read_type (pp, objfile);
2445 if (**pp == ':')
2447 /* g++ version 1 overloaded methods. */
2449 else
2451 new_sublist->fn_field.fcontext = look_ahead_type;
2452 if (**pp != ';')
2454 return 0;
2456 else
2458 ++*pp;
2460 look_ahead_type = NULL;
2463 break;
2465 case '?':
2466 /* static member function. */
2468 int slen = strlen (main_fn_name);
2470 new_sublist->fn_field.voffset = VOFFSET_STATIC;
2472 /* For static member functions, we can't tell if they
2473 are stubbed, as they are put out as functions, and not as
2474 methods.
2475 GCC v2 emits the fully mangled name if
2476 dbxout.c:flag_minimal_debug is not set, so we have to
2477 detect a fully mangled physname here and set is_stub
2478 accordingly. Fully mangled physnames in v2 start with
2479 the member function name, followed by two underscores.
2480 GCC v3 currently always emits stubbed member functions,
2481 but with fully mangled physnames, which start with _Z. */
2482 if (!(strncmp (new_sublist->fn_field.physname,
2483 main_fn_name, slen) == 0
2484 && new_sublist->fn_field.physname[slen] == '_'
2485 && new_sublist->fn_field.physname[slen + 1] == '_'))
2487 new_sublist->fn_field.is_stub = 1;
2489 break;
2492 default:
2493 /* error */
2494 complaint (_("member function type missing, got '%c'"),
2495 (*pp)[-1]);
2496 /* Normal member function. */
2497 [[fallthrough]];
2499 case '.':
2500 /* normal member function. */
2501 new_sublist->fn_field.voffset = 0;
2502 new_sublist->fn_field.fcontext = 0;
2503 break;
2506 new_sublist->next = sublist;
2507 sublist = new_sublist;
2508 length++;
2509 STABS_CONTINUE (pp, objfile);
2511 while (**pp != ';' && **pp != '\0');
2513 (*pp)++;
2514 STABS_CONTINUE (pp, objfile);
2516 /* Skip GCC 3.X member functions which are duplicates of the callable
2517 constructor/destructor. */
2518 if (strcmp_iw (main_fn_name, "__base_ctor ") == 0
2519 || strcmp_iw (main_fn_name, "__base_dtor ") == 0
2520 || strcmp (main_fn_name, "__deleting_dtor") == 0)
2522 xfree (main_fn_name);
2524 else
2526 int has_destructor = 0, has_other = 0;
2527 int is_v3 = 0;
2528 struct next_fnfield *tmp_sublist;
2530 /* Various versions of GCC emit various mostly-useless
2531 strings in the name field for special member functions.
2533 For stub methods, we need to defer correcting the name
2534 until we are ready to unstub the method, because the current
2535 name string is used by gdb_mangle_name. The only stub methods
2536 of concern here are GNU v2 operators; other methods have their
2537 names correct (see caveat below).
2539 For non-stub methods, in GNU v3, we have a complete physname.
2540 Therefore we can safely correct the name now. This primarily
2541 affects constructors and destructors, whose name will be
2542 __comp_ctor or __comp_dtor instead of Foo or ~Foo. Cast
2543 operators will also have incorrect names; for instance,
2544 "operator int" will be named "operator i" (i.e. the type is
2545 mangled).
2547 For non-stub methods in GNU v2, we have no easy way to
2548 know if we have a complete physname or not. For most
2549 methods the result depends on the platform (if CPLUS_MARKER
2550 can be `$' or `.', it will use minimal debug information, or
2551 otherwise the full physname will be included).
2553 Rather than dealing with this, we take a different approach.
2554 For v3 mangled names, we can use the full physname; for v2,
2555 we use cplus_demangle_opname (which is actually v2 specific),
2556 because the only interesting names are all operators - once again
2557 barring the caveat below. Skip this process if any method in the
2558 group is a stub, to prevent our fouling up the workings of
2559 gdb_mangle_name.
2561 The caveat: GCC 2.95.x (and earlier?) put constructors and
2562 destructors in the same method group. We need to split this
2563 into two groups, because they should have different names.
2564 So for each method group we check whether it contains both
2565 routines whose physname appears to be a destructor (the physnames
2566 for and destructors are always provided, due to quirks in v2
2567 mangling) and routines whose physname does not appear to be a
2568 destructor. If so then we break up the list into two halves.
2569 Even if the constructors and destructors aren't in the same group
2570 the destructor will still lack the leading tilde, so that also
2571 needs to be fixed.
2573 So, to summarize what we expect and handle here:
2575 Given Given Real Real Action
2576 method name physname physname method name
2578 __opi [none] __opi__3Foo operator int opname
2579 [now or later]
2580 Foo _._3Foo _._3Foo ~Foo separate and
2581 rename
2582 operator i _ZN3FoocviEv _ZN3FoocviEv operator int demangle
2583 __comp_ctor _ZN3FooC1ERKS_ _ZN3FooC1ERKS_ Foo demangle
2586 tmp_sublist = sublist;
2587 while (tmp_sublist != NULL)
2589 if (tmp_sublist->fn_field.physname[0] == '_'
2590 && tmp_sublist->fn_field.physname[1] == 'Z')
2591 is_v3 = 1;
2593 if (is_destructor_name (tmp_sublist->fn_field.physname))
2594 has_destructor++;
2595 else
2596 has_other++;
2598 tmp_sublist = tmp_sublist->next;
2601 if (has_destructor && has_other)
2603 struct next_fnfieldlist *destr_fnlist;
2604 struct next_fnfield *last_sublist;
2606 /* Create a new fn_fieldlist for the destructors. */
2608 destr_fnlist = OBSTACK_ZALLOC (&fip->obstack,
2609 struct next_fnfieldlist);
2611 destr_fnlist->fn_fieldlist.name
2612 = obconcat (&objfile->objfile_obstack, "~",
2613 new_fnlist->fn_fieldlist.name, (char *) NULL);
2615 destr_fnlist->fn_fieldlist.fn_fields =
2616 XOBNEWVEC (&objfile->objfile_obstack,
2617 struct fn_field, has_destructor);
2618 memset (destr_fnlist->fn_fieldlist.fn_fields, 0,
2619 sizeof (struct fn_field) * has_destructor);
2620 tmp_sublist = sublist;
2621 last_sublist = NULL;
2622 i = 0;
2623 while (tmp_sublist != NULL)
2625 if (!is_destructor_name (tmp_sublist->fn_field.physname))
2627 tmp_sublist = tmp_sublist->next;
2628 continue;
2631 destr_fnlist->fn_fieldlist.fn_fields[i++]
2632 = tmp_sublist->fn_field;
2633 if (last_sublist)
2634 last_sublist->next = tmp_sublist->next;
2635 else
2636 sublist = tmp_sublist->next;
2637 last_sublist = tmp_sublist;
2638 tmp_sublist = tmp_sublist->next;
2641 destr_fnlist->fn_fieldlist.length = has_destructor;
2642 destr_fnlist->next = fip->fnlist;
2643 fip->fnlist = destr_fnlist;
2644 nfn_fields++;
2645 length -= has_destructor;
2647 else if (is_v3)
2649 /* v3 mangling prevents the use of abbreviated physnames,
2650 so we can do this here. There are stubbed methods in v3
2651 only:
2652 - in -gstabs instead of -gstabs+
2653 - or for static methods, which are output as a function type
2654 instead of a method type. */
2655 char *new_method_name =
2656 stabs_method_name_from_physname (sublist->fn_field.physname);
2658 if (new_method_name != NULL
2659 && strcmp (new_method_name,
2660 new_fnlist->fn_fieldlist.name) != 0)
2662 new_fnlist->fn_fieldlist.name = new_method_name;
2663 xfree (main_fn_name);
2665 else
2666 xfree (new_method_name);
2668 else if (has_destructor && new_fnlist->fn_fieldlist.name[0] != '~')
2670 new_fnlist->fn_fieldlist.name =
2671 obconcat (&objfile->objfile_obstack,
2672 "~", main_fn_name, (char *)NULL);
2673 xfree (main_fn_name);
2676 new_fnlist->fn_fieldlist.fn_fields
2677 = OBSTACK_CALLOC (&objfile->objfile_obstack, length, fn_field);
2678 for (i = length; (i--, sublist); sublist = sublist->next)
2680 new_fnlist->fn_fieldlist.fn_fields[i] = sublist->fn_field;
2683 new_fnlist->fn_fieldlist.length = length;
2684 new_fnlist->next = fip->fnlist;
2685 fip->fnlist = new_fnlist;
2686 nfn_fields++;
2690 if (nfn_fields)
2692 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2693 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2694 TYPE_ZALLOC (type, sizeof (struct fn_fieldlist) * nfn_fields);
2695 TYPE_NFN_FIELDS (type) = nfn_fields;
2698 return 1;
2701 /* Special GNU C++ name.
2703 Returns 1 for success, 0 for failure. "failure" means that we can't
2704 keep parsing and it's time for error_type(). */
2706 static int
2707 read_cpp_abbrev (struct stab_field_info *fip, const char **pp,
2708 struct type *type, struct objfile *objfile)
2710 const char *p;
2711 const char *name;
2712 char cpp_abbrev;
2713 struct type *context;
2715 p = *pp;
2716 if (*++p == 'v')
2718 name = NULL;
2719 cpp_abbrev = *++p;
2721 *pp = p + 1;
2723 /* At this point, *pp points to something like "22:23=*22...",
2724 where the type number before the ':' is the "context" and
2725 everything after is a regular type definition. Lookup the
2726 type, find it's name, and construct the field name. */
2728 context = read_type (pp, objfile);
2730 switch (cpp_abbrev)
2732 case 'f': /* $vf -- a virtual function table pointer */
2733 name = context->name ();
2734 if (name == NULL)
2736 name = "";
2738 fip->list->field.set_name (obconcat (&objfile->objfile_obstack,
2739 vptr_name, name, (char *) NULL));
2740 break;
2742 case 'b': /* $vb -- a virtual bsomethingorother */
2743 name = context->name ();
2744 if (name == NULL)
2746 complaint (_("C++ abbreviated type name "
2747 "unknown at symtab pos %d"),
2748 symnum);
2749 name = "FOO";
2751 fip->list->field.set_name (obconcat (&objfile->objfile_obstack,
2752 vb_name, name, (char *) NULL));
2753 break;
2755 default:
2756 invalid_cpp_abbrev_complaint (*pp);
2757 fip->list->field.set_name (obconcat (&objfile->objfile_obstack,
2758 "INVALID_CPLUSPLUS_ABBREV",
2759 (char *) NULL));
2760 break;
2763 /* At this point, *pp points to the ':'. Skip it and read the
2764 field type. */
2766 p = ++(*pp);
2767 if (p[-1] != ':')
2769 invalid_cpp_abbrev_complaint (*pp);
2770 return 0;
2772 fip->list->field.set_type (read_type (pp, objfile));
2773 if (**pp == ',')
2774 (*pp)++; /* Skip the comma. */
2775 else
2776 return 0;
2779 int nbits;
2781 fip->list->field.set_loc_bitpos (read_huge_number (pp, ';', &nbits, 0));
2782 if (nbits != 0)
2783 return 0;
2785 /* This field is unpacked. */
2786 fip->list->field.set_bitsize (0);
2787 fip->list->field.set_accessibility (accessibility::PRIVATE);
2789 else
2791 invalid_cpp_abbrev_complaint (*pp);
2792 /* We have no idea what syntax an unrecognized abbrev would have, so
2793 better return 0. If we returned 1, we would need to at least advance
2794 *pp to avoid an infinite loop. */
2795 return 0;
2797 return 1;
2800 static void
2801 read_one_struct_field (struct stab_field_info *fip, const char **pp,
2802 const char *p, struct type *type,
2803 struct objfile *objfile)
2805 struct gdbarch *gdbarch = objfile->arch ();
2807 fip->list->field.set_name
2808 (obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp));
2809 *pp = p + 1;
2811 /* This means we have a visibility for a field coming. */
2812 int visibility;
2813 if (**pp == '/')
2815 (*pp)++;
2816 visibility = *(*pp)++;
2818 else
2820 /* normal dbx-style format, no explicit visibility */
2821 visibility = VISIBILITY_PUBLIC;
2824 switch (visibility)
2826 case VISIBILITY_PRIVATE:
2827 fip->list->field.set_accessibility (accessibility::PRIVATE);
2828 break;
2830 case VISIBILITY_PROTECTED:
2831 fip->list->field.set_accessibility (accessibility::PROTECTED);
2832 break;
2834 case VISIBILITY_IGNORE:
2835 fip->list->field.set_ignored ();
2836 break;
2838 case VISIBILITY_PUBLIC:
2839 break;
2841 default:
2842 /* Unknown visibility. Complain and treat it as public. */
2844 complaint (_("Unknown visibility `%c' for field"),
2845 visibility);
2847 break;
2850 fip->list->field.set_type (read_type (pp, objfile));
2851 if (**pp == ':')
2853 p = ++(*pp);
2854 #if 0
2855 /* Possible future hook for nested types. */
2856 if (**pp == '!')
2858 fip->list->field.bitpos = (long) -2; /* nested type */
2859 p = ++(*pp);
2861 else
2862 ...;
2863 #endif
2864 while (*p != ';')
2866 p++;
2868 /* Static class member. */
2869 fip->list->field.set_loc_physname (savestring (*pp, p - *pp));
2870 *pp = p + 1;
2871 return;
2873 else if (**pp != ',')
2875 /* Bad structure-type format. */
2876 stabs_general_complaint ("bad structure-type format");
2877 return;
2880 (*pp)++; /* Skip the comma. */
2883 int nbits;
2885 fip->list->field.set_loc_bitpos (read_huge_number (pp, ',', &nbits, 0));
2886 if (nbits != 0)
2888 stabs_general_complaint ("bad structure-type format");
2889 return;
2891 fip->list->field.set_bitsize (read_huge_number (pp, ';', &nbits, 0));
2892 if (nbits != 0)
2894 stabs_general_complaint ("bad structure-type format");
2895 return;
2899 if (fip->list->field.loc_bitpos () == 0
2900 && fip->list->field.bitsize () == 0)
2902 /* This can happen in two cases: (1) at least for gcc 2.4.5 or so,
2903 it is a field which has been optimized out. The correct stab for
2904 this case is to use VISIBILITY_IGNORE, but that is a recent
2905 invention. (2) It is a 0-size array. For example
2906 union { int num; char str[0]; } foo. Printing _("<no value>" for
2907 str in "p foo" is OK, since foo.str (and thus foo.str[3])
2908 will continue to work, and a 0-size array as a whole doesn't
2909 have any contents to print.
2911 I suspect this probably could also happen with gcc -gstabs (not
2912 -gstabs+) for static fields, and perhaps other C++ extensions.
2913 Hopefully few people use -gstabs with gdb, since it is intended
2914 for dbx compatibility. */
2916 /* Ignore this field. */
2917 fip->list->field.set_ignored ();
2919 else
2921 /* Detect an unpacked field and mark it as such.
2922 dbx gives a bit size for all fields.
2923 Note that forward refs cannot be packed,
2924 and treat enums as if they had the width of ints. */
2926 struct type *field_type = check_typedef (fip->list->field.type ());
2928 if (field_type->code () != TYPE_CODE_INT
2929 && field_type->code () != TYPE_CODE_RANGE
2930 && field_type->code () != TYPE_CODE_BOOL
2931 && field_type->code () != TYPE_CODE_ENUM)
2933 fip->list->field.set_bitsize (0);
2935 if ((fip->list->field.bitsize ()
2936 == TARGET_CHAR_BIT * field_type->length ()
2937 || (field_type->code () == TYPE_CODE_ENUM
2938 && (fip->list->field.bitsize ()
2939 == gdbarch_int_bit (gdbarch)))
2942 fip->list->field.loc_bitpos () % 8 == 0)
2944 fip->list->field.set_bitsize (0);
2950 /* Read struct or class data fields. They have the form:
2952 NAME : [VISIBILITY] TYPENUM , BITPOS , BITSIZE ;
2954 At the end, we see a semicolon instead of a field.
2956 In C++, this may wind up being NAME:?TYPENUM:PHYSNAME; for
2957 a static field.
2959 The optional VISIBILITY is one of:
2961 '/0' (VISIBILITY_PRIVATE)
2962 '/1' (VISIBILITY_PROTECTED)
2963 '/2' (VISIBILITY_PUBLIC)
2964 '/9' (VISIBILITY_IGNORE)
2966 or nothing, for C style fields with public visibility.
2968 Returns 1 for success, 0 for failure. */
2970 static int
2971 read_struct_fields (struct stab_field_info *fip, const char **pp,
2972 struct type *type, struct objfile *objfile)
2974 const char *p;
2975 struct stabs_nextfield *newobj;
2977 /* We better set p right now, in case there are no fields at all... */
2979 p = *pp;
2981 /* Read each data member type until we find the terminating ';' at the end of
2982 the data member list, or break for some other reason such as finding the
2983 start of the member function list. */
2984 /* Stab string for structure/union does not end with two ';' in
2985 SUN C compiler 5.3 i.e. F6U2, hence check for end of string. */
2987 while (**pp != ';' && **pp != '\0')
2989 STABS_CONTINUE (pp, objfile);
2990 /* Get space to record the next field's data. */
2991 newobj = OBSTACK_ZALLOC (&fip->obstack, struct stabs_nextfield);
2993 newobj->next = fip->list;
2994 fip->list = newobj;
2996 /* Get the field name. */
2997 p = *pp;
2999 /* If is starts with CPLUS_MARKER it is a special abbreviation,
3000 unless the CPLUS_MARKER is followed by an underscore, in
3001 which case it is just the name of an anonymous type, which we
3002 should handle like any other type name. */
3004 if (is_cplus_marker (p[0]) && p[1] != '_')
3006 if (!read_cpp_abbrev (fip, pp, type, objfile))
3007 return 0;
3008 continue;
3011 /* Look for the ':' that separates the field name from the field
3012 values. Data members are delimited by a single ':', while member
3013 functions are delimited by a pair of ':'s. When we hit the member
3014 functions (if any), terminate scan loop and return. */
3016 while (*p != ':' && *p != '\0')
3018 p++;
3020 if (*p == '\0')
3021 return 0;
3023 /* Check to see if we have hit the member functions yet. */
3024 if (p[1] == ':')
3026 break;
3028 read_one_struct_field (fip, pp, p, type, objfile);
3030 if (p[0] == ':' && p[1] == ':')
3032 /* (the deleted) chill the list of fields: the last entry (at
3033 the head) is a partially constructed entry which we now
3034 scrub. */
3035 fip->list = fip->list->next;
3037 return 1;
3039 /* The stabs for C++ derived classes contain baseclass information which
3040 is marked by a '!' character after the total size. This function is
3041 called when we encounter the baseclass marker, and slurps up all the
3042 baseclass information.
3044 Immediately following the '!' marker is the number of base classes that
3045 the class is derived from, followed by information for each base class.
3046 For each base class, there are two visibility specifiers, a bit offset
3047 to the base class information within the derived class, a reference to
3048 the type for the base class, and a terminating semicolon.
3050 A typical example, with two base classes, would be "!2,020,19;0264,21;".
3051 ^^ ^ ^ ^ ^ ^ ^
3052 Baseclass information marker __________________|| | | | | | |
3053 Number of baseclasses __________________________| | | | | | |
3054 Visibility specifiers (2) ________________________| | | | | |
3055 Offset in bits from start of class _________________| | | | |
3056 Type number for base class ___________________________| | | |
3057 Visibility specifiers (2) _______________________________| | |
3058 Offset in bits from start of class ________________________| |
3059 Type number of base class ____________________________________|
3061 Return 1 for success, 0 for (error-type-inducing) failure. */
3065 static int
3066 read_baseclasses (struct stab_field_info *fip, const char **pp,
3067 struct type *type, struct objfile *objfile)
3069 int i;
3070 struct stabs_nextfield *newobj;
3072 if (**pp != '!')
3074 return 1;
3076 else
3078 /* Skip the '!' baseclass information marker. */
3079 (*pp)++;
3082 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3084 int nbits;
3086 TYPE_N_BASECLASSES (type) = read_huge_number (pp, ',', &nbits, 0);
3087 if (nbits != 0)
3088 return 0;
3091 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
3093 newobj = OBSTACK_ZALLOC (&fip->obstack, struct stabs_nextfield);
3095 newobj->next = fip->list;
3096 fip->list = newobj;
3097 newobj->field.set_bitsize (0); /* This should be an unpacked
3098 field! */
3100 STABS_CONTINUE (pp, objfile);
3101 switch (**pp)
3103 case '0':
3104 /* Nothing to do. */
3105 break;
3106 case '1':
3107 newobj->field.set_virtual ();
3108 break;
3109 default:
3110 /* Unknown character. Complain and treat it as non-virtual. */
3112 complaint (_("Unknown virtual character `%c' for baseclass"),
3113 **pp);
3116 ++(*pp);
3118 int visibility = *(*pp)++;
3119 switch (visibility)
3121 case VISIBILITY_PRIVATE:
3122 newobj->field.set_accessibility (accessibility::PRIVATE);
3123 break;
3124 case VISIBILITY_PROTECTED:
3125 newobj->field.set_accessibility (accessibility::PROTECTED);
3126 break;
3127 case VISIBILITY_PUBLIC:
3128 break;
3129 default:
3130 /* Bad visibility format. Complain and treat it as
3131 public. */
3133 complaint (_("Unknown visibility `%c' for baseclass"),
3134 visibility);
3139 int nbits;
3141 /* The remaining value is the bit offset of the portion of the object
3142 corresponding to this baseclass. Always zero in the absence of
3143 multiple inheritance. */
3145 newobj->field.set_loc_bitpos (read_huge_number (pp, ',', &nbits, 0));
3146 if (nbits != 0)
3147 return 0;
3150 /* The last piece of baseclass information is the type of the
3151 base class. Read it, and remember it's type name as this
3152 field's name. */
3154 newobj->field.set_type (read_type (pp, objfile));
3155 newobj->field.set_name (newobj->field.type ()->name ());
3157 /* Skip trailing ';' and bump count of number of fields seen. */
3158 if (**pp == ';')
3159 (*pp)++;
3160 else
3161 return 0;
3163 return 1;
3166 /* The tail end of stabs for C++ classes that contain a virtual function
3167 pointer contains a tilde, a %, and a type number.
3168 The type number refers to the base class (possibly this class itself) which
3169 contains the vtable pointer for the current class.
3171 This function is called when we have parsed all the method declarations,
3172 so we can look for the vptr base class info. */
3174 static int
3175 read_tilde_fields (struct stab_field_info *fip, const char **pp,
3176 struct type *type, struct objfile *objfile)
3178 const char *p;
3180 STABS_CONTINUE (pp, objfile);
3182 /* If we are positioned at a ';', then skip it. */
3183 if (**pp == ';')
3185 (*pp)++;
3188 if (**pp == '~')
3190 (*pp)++;
3192 if (**pp == '=' || **pp == '+' || **pp == '-')
3194 /* Obsolete flags that used to indicate the presence
3195 of constructors and/or destructors. */
3196 (*pp)++;
3199 /* Read either a '%' or the final ';'. */
3200 if (*(*pp)++ == '%')
3202 /* The next number is the type number of the base class
3203 (possibly our own class) which supplies the vtable for
3204 this class. Parse it out, and search that class to find
3205 its vtable pointer, and install those into TYPE_VPTR_BASETYPE
3206 and TYPE_VPTR_FIELDNO. */
3208 struct type *t;
3209 int i;
3211 t = read_type (pp, objfile);
3212 p = (*pp)++;
3213 while (*p != '\0' && *p != ';')
3215 p++;
3217 if (*p == '\0')
3219 /* Premature end of symbol. */
3220 return 0;
3223 set_type_vptr_basetype (type, t);
3224 if (type == t) /* Our own class provides vtbl ptr. */
3226 for (i = t->num_fields () - 1;
3227 i >= TYPE_N_BASECLASSES (t);
3228 --i)
3230 const char *name = t->field (i).name ();
3232 if (!strncmp (name, vptr_name, sizeof (vptr_name) - 2)
3233 && is_cplus_marker (name[sizeof (vptr_name) - 2]))
3235 set_type_vptr_fieldno (type, i);
3236 goto gotit;
3239 /* Virtual function table field not found. */
3240 complaint (_("virtual function table pointer "
3241 "not found when defining class `%s'"),
3242 type->name ());
3243 return 0;
3245 else
3247 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
3250 gotit:
3251 *pp = p + 1;
3254 return 1;
3257 static int
3258 attach_fn_fields_to_type (struct stab_field_info *fip, struct type *type)
3260 int n;
3262 for (n = TYPE_NFN_FIELDS (type);
3263 fip->fnlist != NULL;
3264 fip->fnlist = fip->fnlist->next)
3266 --n; /* Circumvent Sun3 compiler bug. */
3267 TYPE_FN_FIELDLISTS (type)[n] = fip->fnlist->fn_fieldlist;
3269 return 1;
3272 /* Create the vector of fields, and record how big it is.
3273 We need this info to record proper virtual function table information
3274 for this class's virtual functions. */
3276 static int
3277 attach_fields_to_type (struct stab_field_info *fip, struct type *type,
3278 struct objfile *objfile)
3280 int nfields = 0;
3281 struct stabs_nextfield *scan;
3283 /* Count up the number of fields that we have. */
3285 for (scan = fip->list; scan != NULL; scan = scan->next)
3286 nfields++;
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. */
3292 type->alloc_fields (nfields);
3294 /* Copy the saved-up fields into the field vector. Start from the
3295 head of the list, adding to the tail of the field array, so that
3296 they end up in the same order in the array in which they were
3297 added to the list. */
3299 while (nfields-- > 0)
3301 type->field (nfields) = fip->list->field;
3302 fip->list = fip->list->next;
3304 return 1;
3308 /* Complain that the compiler has emitted more than one definition for the
3309 structure type TYPE. */
3310 static void
3311 complain_about_struct_wipeout (struct type *type)
3313 const char *name = "";
3314 const char *kind = "";
3316 if (type->name ())
3318 name = type->name ();
3319 switch (type->code ())
3321 case TYPE_CODE_STRUCT: kind = "struct "; break;
3322 case TYPE_CODE_UNION: kind = "union "; break;
3323 case TYPE_CODE_ENUM: kind = "enum "; break;
3324 default: kind = "";
3327 else
3329 name = "<unknown>";
3330 kind = "";
3333 complaint (_("struct/union type gets multiply defined: %s%s"), kind, name);
3336 /* Set the length for all variants of a same main_type, which are
3337 connected in the closed chain.
3339 This is something that needs to be done when a type is defined *after*
3340 some cross references to this type have already been read. Consider
3341 for instance the following scenario where we have the following two
3342 stabs entries:
3344 .stabs "t:p(0,21)=*(0,22)=k(0,23)=xsdummy:",160,0,28,-24
3345 .stabs "dummy:T(0,23)=s16x:(0,1),0,3[...]"
3347 A stubbed version of type dummy is created while processing the first
3348 stabs entry. The length of that type is initially set to zero, since
3349 it is unknown at this point. Also, a "constant" variation of type
3350 "dummy" is created as well (this is the "(0,22)=k(0,23)" section of
3351 the stabs line).
3353 The second stabs entry allows us to replace the stubbed definition
3354 with the real definition. However, we still need to adjust the length
3355 of the "constant" variation of that type, as its length was left
3356 untouched during the main type replacement... */
3358 static void
3359 set_length_in_type_chain (struct type *type)
3361 struct type *ntype = TYPE_CHAIN (type);
3363 while (ntype != type)
3365 if (ntype->length () == 0)
3366 ntype->set_length (type->length ());
3367 else
3368 complain_about_struct_wipeout (ntype);
3369 ntype = TYPE_CHAIN (ntype);
3373 /* Read the description of a structure (or union type) and return an object
3374 describing the type.
3376 PP points to a character pointer that points to the next unconsumed token
3377 in the stabs string. For example, given stabs "A:T4=s4a:1,0,32;;",
3378 *PP will point to "4a:1,0,32;;".
3380 TYPE points to an incomplete type that needs to be filled in.
3382 OBJFILE points to the current objfile from which the stabs information is
3383 being read. (Note that it is redundant in that TYPE also contains a pointer
3384 to this same objfile, so it might be a good idea to eliminate it. FIXME).
3387 static struct type *
3388 read_struct_type (const char **pp, struct type *type, enum type_code type_code,
3389 struct objfile *objfile)
3391 struct stab_field_info fi;
3393 /* When describing struct/union/class types in stabs, G++ always drops
3394 all qualifications from the name. So if you've got:
3395 struct A { ... struct B { ... }; ... };
3396 then G++ will emit stabs for `struct A::B' that call it simply
3397 `struct B'. Obviously, if you've got a real top-level definition for
3398 `struct B', or other nested definitions, this is going to cause
3399 problems.
3401 Obviously, GDB can't fix this by itself, but it can at least avoid
3402 scribbling on existing structure type objects when new definitions
3403 appear. */
3404 if (! (type->code () == TYPE_CODE_UNDEF
3405 || type->is_stub ()))
3407 complain_about_struct_wipeout (type);
3409 /* It's probably best to return the type unchanged. */
3410 return type;
3413 INIT_CPLUS_SPECIFIC (type);
3414 type->set_code (type_code);
3415 type->set_is_stub (false);
3417 /* First comes the total size in bytes. */
3420 int nbits;
3422 type->set_length (read_huge_number (pp, 0, &nbits, 0));
3423 if (nbits != 0)
3424 return error_type (pp, objfile);
3425 set_length_in_type_chain (type);
3428 /* Now read the baseclasses, if any, read the regular C struct or C++
3429 class member fields, attach the fields to the type, read the C++
3430 member functions, attach them to the type, and then read any tilde
3431 field (baseclass specifier for the class holding the main vtable). */
3433 if (!read_baseclasses (&fi, pp, type, objfile)
3434 || !read_struct_fields (&fi, pp, type, objfile)
3435 || !attach_fields_to_type (&fi, type, objfile)
3436 || !read_member_functions (&fi, pp, type, objfile)
3437 || !attach_fn_fields_to_type (&fi, type)
3438 || !read_tilde_fields (&fi, pp, type, objfile))
3440 type = error_type (pp, objfile);
3443 return (type);
3446 /* Read a definition of an array type,
3447 and create and return a suitable type object.
3448 Also creates a range type which represents the bounds of that
3449 array. */
3451 static struct type *
3452 read_array_type (const char **pp, struct type *type,
3453 struct objfile *objfile)
3455 struct type *index_type, *element_type, *range_type;
3456 int lower, upper;
3457 int adjustable = 0;
3458 int nbits;
3460 /* Format of an array type:
3461 "ar<index type>;lower;upper;<array_contents_type>".
3462 OS9000: "arlower,upper;<array_contents_type>".
3464 Fortran adjustable arrays use Adigits or Tdigits for lower or upper;
3465 for these, produce a type like float[][]. */
3468 index_type = read_type (pp, objfile);
3469 if (**pp != ';')
3470 /* Improper format of array type decl. */
3471 return error_type (pp, objfile);
3472 ++*pp;
3475 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
3477 (*pp)++;
3478 adjustable = 1;
3480 lower = read_huge_number (pp, ';', &nbits, 0);
3482 if (nbits != 0)
3483 return error_type (pp, objfile);
3485 if (!(**pp >= '0' && **pp <= '9') && **pp != '-')
3487 (*pp)++;
3488 adjustable = 1;
3490 upper = read_huge_number (pp, ';', &nbits, 0);
3491 if (nbits != 0)
3492 return error_type (pp, objfile);
3494 element_type = read_type (pp, objfile);
3496 if (adjustable)
3498 lower = 0;
3499 upper = -1;
3502 type_allocator alloc (objfile, get_current_subfile ()->language);
3503 range_type =
3504 create_static_range_type (alloc, index_type, lower, upper);
3505 type_allocator smash_alloc (type, type_allocator::SMASH);
3506 type = create_array_type (smash_alloc, element_type, range_type);
3508 return type;
3512 /* Read a definition of an enumeration type,
3513 and create and return a suitable type object.
3514 Also defines the symbols that represent the values of the type. */
3516 static struct type *
3517 read_enum_type (const char **pp, struct type *type,
3518 struct objfile *objfile)
3520 struct gdbarch *gdbarch = objfile->arch ();
3521 const char *p;
3522 char *name;
3523 long n;
3524 struct symbol *sym;
3525 int nsyms = 0;
3526 struct pending **symlist;
3527 struct pending *osyms, *syms;
3528 int o_nsyms;
3529 int nbits;
3530 int unsigned_enum = 1;
3532 #if 0
3533 /* FIXME! The stabs produced by Sun CC merrily define things that ought
3534 to be file-scope, between N_FN entries, using N_LSYM. What's a mother
3535 to do? For now, force all enum values to file scope. */
3536 if (within_function)
3537 symlist = get_local_symbols ();
3538 else
3539 #endif
3540 symlist = get_file_symbols ();
3541 osyms = *symlist;
3542 o_nsyms = osyms ? osyms->nsyms : 0;
3544 /* The aix4 compiler emits an extra field before the enum members;
3545 my guess is it's a type of some sort. Just ignore it. */
3546 if (**pp == '-')
3548 /* Skip over the type. */
3549 while (**pp != ':')
3550 (*pp)++;
3552 /* Skip over the colon. */
3553 (*pp)++;
3556 /* Read the value-names and their values.
3557 The input syntax is NAME:VALUE,NAME:VALUE, and so on.
3558 A semicolon or comma instead of a NAME means the end. */
3559 while (**pp && **pp != ';' && **pp != ',')
3561 STABS_CONTINUE (pp, objfile);
3562 p = *pp;
3563 while (*p != ':')
3564 p++;
3565 name = obstack_strndup (&objfile->objfile_obstack, *pp, p - *pp);
3566 *pp = p + 1;
3567 n = read_huge_number (pp, ',', &nbits, 0);
3568 if (nbits != 0)
3569 return error_type (pp, objfile);
3571 sym = new (&objfile->objfile_obstack) symbol;
3572 sym->set_linkage_name (name);
3573 sym->set_language (get_current_subfile ()->language,
3574 &objfile->objfile_obstack);
3575 sym->set_aclass_index (LOC_CONST);
3576 sym->set_domain (VAR_DOMAIN);
3577 sym->set_value_longest (n);
3578 if (n < 0)
3579 unsigned_enum = 0;
3580 add_symbol_to_list (sym, symlist);
3581 nsyms++;
3584 if (**pp == ';')
3585 (*pp)++; /* Skip the semicolon. */
3587 /* Now fill in the fields of the type-structure. */
3589 type->set_length (gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT);
3590 set_length_in_type_chain (type);
3591 type->set_code (TYPE_CODE_ENUM);
3592 type->set_is_stub (false);
3593 if (unsigned_enum)
3594 type->set_is_unsigned (true);
3595 type->alloc_fields (nsyms);
3597 /* Find the symbols for the values and put them into the type.
3598 The symbols can be found in the symlist that we put them on
3599 to cause them to be defined. osyms contains the old value
3600 of that symlist; everything up to there was defined by us. */
3601 /* Note that we preserve the order of the enum constants, so
3602 that in something like "enum {FOO, LAST_THING=FOO}" we print
3603 FOO, not LAST_THING. */
3605 for (syms = *symlist, n = nsyms - 1; syms; syms = syms->next)
3607 int last = syms == osyms ? o_nsyms : 0;
3608 int j = syms->nsyms;
3610 for (; --j >= last; --n)
3612 struct symbol *xsym = syms->symbol[j];
3614 xsym->set_type (type);
3615 type->field (n).set_name (xsym->linkage_name ());
3616 type->field (n).set_loc_enumval (xsym->value_longest ());
3617 type->field (n).set_bitsize (0);
3619 if (syms == osyms)
3620 break;
3623 return type;
3626 /* Sun's ACC uses a somewhat saner method for specifying the builtin
3627 typedefs in every file (for int, long, etc):
3629 type = b <signed> <width> <format type>; <offset>; <nbits>
3630 signed = u or s.
3631 optional format type = c or b for char or boolean.
3632 offset = offset from high order bit to start bit of type.
3633 width is # bytes in object of this type, nbits is # bits in type.
3635 The width/offset stuff appears to be for small objects stored in
3636 larger ones (e.g. `shorts' in `int' registers). We ignore it for now,
3637 FIXME. */
3639 static struct type *
3640 read_sun_builtin_type (const char **pp, int typenums[2], struct objfile *objfile)
3642 int type_bits;
3643 int nbits;
3644 int unsigned_type;
3645 int boolean_type = 0;
3647 switch (**pp)
3649 case 's':
3650 unsigned_type = 0;
3651 break;
3652 case 'u':
3653 unsigned_type = 1;
3654 break;
3655 default:
3656 return error_type (pp, objfile);
3658 (*pp)++;
3660 /* For some odd reason, all forms of char put a c here. This is strange
3661 because no other type has this honor. We can safely ignore this because
3662 we actually determine 'char'acterness by the number of bits specified in
3663 the descriptor.
3664 Boolean forms, e.g Fortran logical*X, put a b here. */
3666 if (**pp == 'c')
3667 (*pp)++;
3668 else if (**pp == 'b')
3670 boolean_type = 1;
3671 (*pp)++;
3674 /* The first number appears to be the number of bytes occupied
3675 by this type, except that unsigned short is 4 instead of 2.
3676 Since this information is redundant with the third number,
3677 we will ignore it. */
3678 read_huge_number (pp, ';', &nbits, 0);
3679 if (nbits != 0)
3680 return error_type (pp, objfile);
3682 /* The second number is always 0, so ignore it too. */
3683 read_huge_number (pp, ';', &nbits, 0);
3684 if (nbits != 0)
3685 return error_type (pp, objfile);
3687 /* The third number is the number of bits for this type. */
3688 type_bits = read_huge_number (pp, 0, &nbits, 0);
3689 if (nbits != 0)
3690 return error_type (pp, objfile);
3691 /* The type *should* end with a semicolon. If it are embedded
3692 in a larger type the semicolon may be the only way to know where
3693 the type ends. If this type is at the end of the stabstring we
3694 can deal with the omitted semicolon (but we don't have to like
3695 it). Don't bother to complain(), Sun's compiler omits the semicolon
3696 for "void". */
3697 if (**pp == ';')
3698 ++(*pp);
3700 type_allocator alloc (objfile, get_current_subfile ()->language);
3701 if (type_bits == 0)
3703 struct type *type = alloc.new_type (TYPE_CODE_VOID,
3704 TARGET_CHAR_BIT, nullptr);
3705 if (unsigned_type)
3706 type->set_is_unsigned (true);
3708 return type;
3711 if (boolean_type)
3712 return init_boolean_type (alloc, type_bits, unsigned_type, NULL);
3713 else
3714 return init_integer_type (alloc, type_bits, unsigned_type, NULL);
3717 static struct type *
3718 read_sun_floating_type (const char **pp, int typenums[2],
3719 struct objfile *objfile)
3721 int nbits;
3722 int details;
3723 int nbytes;
3724 struct type *rettype;
3726 /* The first number has more details about the type, for example
3727 FN_COMPLEX. */
3728 details = read_huge_number (pp, ';', &nbits, 0);
3729 if (nbits != 0)
3730 return error_type (pp, objfile);
3732 /* The second number is the number of bytes occupied by this type. */
3733 nbytes = read_huge_number (pp, ';', &nbits, 0);
3734 if (nbits != 0)
3735 return error_type (pp, objfile);
3737 nbits = nbytes * TARGET_CHAR_BIT;
3739 if (details == NF_COMPLEX || details == NF_COMPLEX16
3740 || details == NF_COMPLEX32)
3742 rettype = dbx_init_float_type (objfile, nbits / 2);
3743 return init_complex_type (NULL, rettype);
3746 return dbx_init_float_type (objfile, nbits);
3749 /* Read a number from the string pointed to by *PP.
3750 The value of *PP is advanced over the number.
3751 If END is nonzero, the character that ends the
3752 number must match END, or an error happens;
3753 and that character is skipped if it does match.
3754 If END is zero, *PP is left pointing to that character.
3756 If TWOS_COMPLEMENT_BITS is set to a strictly positive value and if
3757 the number is represented in an octal representation, assume that
3758 it is represented in a 2's complement representation with a size of
3759 TWOS_COMPLEMENT_BITS.
3761 If the number fits in a long, set *BITS to 0 and return the value.
3762 If not, set *BITS to be the number of bits in the number and return 0.
3764 If encounter garbage, set *BITS to -1 and return 0. */
3766 static long
3767 read_huge_number (const char **pp, int end, int *bits,
3768 int twos_complement_bits)
3770 const char *p = *pp;
3771 int sign = 1;
3772 int sign_bit = 0;
3773 long n = 0;
3774 int radix = 10;
3775 char overflow = 0;
3776 int nbits = 0;
3777 int c;
3778 long upper_limit;
3779 int twos_complement_representation = 0;
3781 if (*p == '-')
3783 sign = -1;
3784 p++;
3787 /* Leading zero means octal. GCC uses this to output values larger
3788 than an int (because that would be hard in decimal). */
3789 if (*p == '0')
3791 radix = 8;
3792 p++;
3795 /* Skip extra zeros. */
3796 while (*p == '0')
3797 p++;
3799 if (sign > 0 && radix == 8 && twos_complement_bits > 0)
3801 /* Octal, possibly signed. Check if we have enough chars for a
3802 negative number. */
3804 size_t len;
3805 const char *p1 = p;
3807 while ((c = *p1) >= '0' && c < '8')
3808 p1++;
3810 len = p1 - p;
3811 if (len > twos_complement_bits / 3
3812 || (twos_complement_bits % 3 == 0
3813 && len == twos_complement_bits / 3))
3815 /* Ok, we have enough characters for a signed value, check
3816 for signedness by testing if the sign bit is set. */
3817 sign_bit = (twos_complement_bits % 3 + 2) % 3;
3818 c = *p - '0';
3819 if (c & (1 << sign_bit))
3821 /* Definitely signed. */
3822 twos_complement_representation = 1;
3823 sign = -1;
3828 upper_limit = LONG_MAX / radix;
3830 while ((c = *p++) >= '0' && c < ('0' + radix))
3832 if (n <= upper_limit)
3834 if (twos_complement_representation)
3836 /* Octal, signed, twos complement representation. In
3837 this case, n is the corresponding absolute value. */
3838 if (n == 0)
3840 long sn = c - '0' - ((2 * (c - '0')) | (2 << sign_bit));
3842 n = -sn;
3844 else
3846 n *= radix;
3847 n -= c - '0';
3850 else
3852 /* unsigned representation */
3853 n *= radix;
3854 n += c - '0'; /* FIXME this overflows anyway. */
3857 else
3858 overflow = 1;
3860 /* This depends on large values being output in octal, which is
3861 what GCC does. */
3862 if (radix == 8)
3864 if (nbits == 0)
3866 if (c == '0')
3867 /* Ignore leading zeroes. */
3869 else if (c == '1')
3870 nbits = 1;
3871 else if (c == '2' || c == '3')
3872 nbits = 2;
3873 else
3874 nbits = 3;
3876 else
3877 nbits += 3;
3880 if (end)
3882 if (c && c != end)
3884 if (bits != NULL)
3885 *bits = -1;
3886 return 0;
3889 else
3890 --p;
3892 if (radix == 8 && twos_complement_bits > 0 && nbits > twos_complement_bits)
3894 /* We were supposed to parse a number with maximum
3895 TWOS_COMPLEMENT_BITS bits, but something went wrong. */
3896 if (bits != NULL)
3897 *bits = -1;
3898 return 0;
3901 *pp = p;
3902 if (overflow)
3904 if (nbits == 0)
3906 /* Large decimal constants are an error (because it is hard to
3907 count how many bits are in them). */
3908 if (bits != NULL)
3909 *bits = -1;
3910 return 0;
3913 /* -0x7f is the same as 0x80. So deal with it by adding one to
3914 the number of bits. Two's complement represention octals
3915 can't have a '-' in front. */
3916 if (sign == -1 && !twos_complement_representation)
3917 ++nbits;
3918 if (bits)
3919 *bits = nbits;
3921 else
3923 if (bits)
3924 *bits = 0;
3925 return n * sign;
3927 /* It's *BITS which has the interesting information. */
3928 return 0;
3931 static struct type *
3932 read_range_type (const char **pp, int typenums[2], int type_size,
3933 struct objfile *objfile)
3935 struct gdbarch *gdbarch = objfile->arch ();
3936 const char *orig_pp = *pp;
3937 int rangenums[2];
3938 long n2, n3;
3939 int n2bits, n3bits;
3940 int self_subrange;
3941 struct type *result_type;
3942 struct type *index_type = NULL;
3944 /* First comes a type we are a subrange of.
3945 In C it is usually 0, 1 or the type being defined. */
3946 if (read_type_number (pp, rangenums) != 0)
3947 return error_type (pp, objfile);
3948 self_subrange = (rangenums[0] == typenums[0] &&
3949 rangenums[1] == typenums[1]);
3951 if (**pp == '=')
3953 *pp = orig_pp;
3954 index_type = read_type (pp, objfile);
3957 /* A semicolon should now follow; skip it. */
3958 if (**pp == ';')
3959 (*pp)++;
3961 /* The remaining two operands are usually lower and upper bounds
3962 of the range. But in some special cases they mean something else. */
3963 n2 = read_huge_number (pp, ';', &n2bits, type_size);
3964 n3 = read_huge_number (pp, ';', &n3bits, type_size);
3966 if (n2bits == -1 || n3bits == -1)
3967 return error_type (pp, objfile);
3969 type_allocator alloc (objfile, get_current_subfile ()->language);
3971 if (index_type)
3972 goto handle_true_range;
3974 /* If limits are huge, must be large integral type. */
3975 if (n2bits != 0 || n3bits != 0)
3977 char got_signed = 0;
3978 char got_unsigned = 0;
3979 /* Number of bits in the type. */
3980 int nbits = 0;
3982 /* If a type size attribute has been specified, the bounds of
3983 the range should fit in this size. If the lower bounds needs
3984 more bits than the upper bound, then the type is signed. */
3985 if (n2bits <= type_size && n3bits <= type_size)
3987 if (n2bits == type_size && n2bits > n3bits)
3988 got_signed = 1;
3989 else
3990 got_unsigned = 1;
3991 nbits = type_size;
3993 /* Range from 0 to <large number> is an unsigned large integral type. */
3994 else if ((n2bits == 0 && n2 == 0) && n3bits != 0)
3996 got_unsigned = 1;
3997 nbits = n3bits;
3999 /* Range from <large number> to <large number>-1 is a large signed
4000 integral type. Take care of the case where <large number> doesn't
4001 fit in a long but <large number>-1 does. */
4002 else if ((n2bits != 0 && n3bits != 0 && n2bits == n3bits + 1)
4003 || (n2bits != 0 && n3bits == 0
4004 && (n2bits == sizeof (long) * HOST_CHAR_BIT)
4005 && n3 == LONG_MAX))
4007 got_signed = 1;
4008 nbits = n2bits;
4011 if (got_signed || got_unsigned)
4012 return init_integer_type (alloc, nbits, got_unsigned, NULL);
4013 else
4014 return error_type (pp, objfile);
4017 /* A type defined as a subrange of itself, with bounds both 0, is void. */
4018 if (self_subrange && n2 == 0 && n3 == 0)
4019 return alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, nullptr);
4021 /* If n3 is zero and n2 is positive, we want a floating type, and n2
4022 is the width in bytes.
4024 Fortran programs appear to use this for complex types also. To
4025 distinguish between floats and complex, g77 (and others?) seem
4026 to use self-subranges for the complexes, and subranges of int for
4027 the floats.
4029 Also note that for complexes, g77 sets n2 to the size of one of
4030 the member floats, not the whole complex beast. My guess is that
4031 this was to work well with pre-COMPLEX versions of gdb. */
4033 if (n3 == 0 && n2 > 0)
4035 struct type *float_type
4036 = dbx_init_float_type (objfile, n2 * TARGET_CHAR_BIT);
4038 if (self_subrange)
4039 return init_complex_type (NULL, float_type);
4040 else
4041 return float_type;
4044 /* If the upper bound is -1, it must really be an unsigned integral. */
4046 else if (n2 == 0 && n3 == -1)
4048 int bits = type_size;
4050 if (bits <= 0)
4052 /* We don't know its size. It is unsigned int or unsigned
4053 long. GCC 2.3.3 uses this for long long too, but that is
4054 just a GDB 3.5 compatibility hack. */
4055 bits = gdbarch_int_bit (gdbarch);
4058 return init_integer_type (alloc, bits, 1, NULL);
4061 /* Special case: char is defined (Who knows why) as a subrange of
4062 itself with range 0-127. */
4063 else if (self_subrange && n2 == 0 && n3 == 127)
4065 struct type *type = init_integer_type (alloc, TARGET_CHAR_BIT,
4066 0, NULL);
4067 type->set_has_no_signedness (true);
4068 return type;
4070 /* We used to do this only for subrange of self or subrange of int. */
4071 else if (n2 == 0)
4073 /* -1 is used for the upper bound of (4 byte) "unsigned int" and
4074 "unsigned long", and we already checked for that,
4075 so don't need to test for it here. */
4077 if (n3 < 0)
4078 /* n3 actually gives the size. */
4079 return init_integer_type (alloc, -n3 * TARGET_CHAR_BIT, 1, NULL);
4081 /* Is n3 == 2**(8n)-1 for some integer n? Then it's an
4082 unsigned n-byte integer. But do require n to be a power of
4083 two; we don't want 3- and 5-byte integers flying around. */
4085 int bytes;
4086 unsigned long bits;
4088 bits = n3;
4089 for (bytes = 0; (bits & 0xff) == 0xff; bytes++)
4090 bits >>= 8;
4091 if (bits == 0
4092 && ((bytes - 1) & bytes) == 0) /* "bytes is a power of two" */
4093 return init_integer_type (alloc, bytes * TARGET_CHAR_BIT, 1, NULL);
4096 /* I think this is for Convex "long long". Since I don't know whether
4097 Convex sets self_subrange, I also accept that particular size regardless
4098 of self_subrange. */
4099 else if (n3 == 0 && n2 < 0
4100 && (self_subrange
4101 || n2 == -gdbarch_long_long_bit
4102 (gdbarch) / TARGET_CHAR_BIT))
4103 return init_integer_type (alloc, -n2 * TARGET_CHAR_BIT, 0, NULL);
4104 else if (n2 == -n3 - 1)
4106 if (n3 == 0x7f)
4107 return init_integer_type (alloc, 8, 0, NULL);
4108 if (n3 == 0x7fff)
4109 return init_integer_type (alloc, 16, 0, NULL);
4110 if (n3 == 0x7fffffff)
4111 return init_integer_type (alloc, 32, 0, NULL);
4114 /* We have a real range type on our hands. Allocate space and
4115 return a real pointer. */
4116 handle_true_range:
4118 if (self_subrange)
4119 index_type = builtin_type (objfile)->builtin_int;
4120 else
4121 index_type = *dbx_lookup_type (rangenums, objfile);
4122 if (index_type == NULL)
4124 /* Does this actually ever happen? Is that why we are worrying
4125 about dealing with it rather than just calling error_type? */
4127 complaint (_("base type %d of range type is not defined"), rangenums[1]);
4129 index_type = builtin_type (objfile)->builtin_int;
4132 result_type
4133 = create_static_range_type (alloc, index_type, n2, n3);
4134 return (result_type);
4137 /* Read in an argument list. This is a list of types, separated by commas
4138 and terminated with END. Return the list of types read in, or NULL
4139 if there is an error. */
4141 static struct field *
4142 read_args (const char **pp, int end, struct objfile *objfile, int *nargsp,
4143 int *varargsp)
4145 /* FIXME! Remove this arbitrary limit! */
4146 struct type *types[1024]; /* Allow for fns of 1023 parameters. */
4147 int n = 0, i;
4148 struct field *rval;
4150 while (**pp != end)
4152 if (**pp != ',')
4153 /* Invalid argument list: no ','. */
4154 return NULL;
4155 (*pp)++;
4156 STABS_CONTINUE (pp, objfile);
4157 types[n++] = read_type (pp, objfile);
4159 (*pp)++; /* get past `end' (the ':' character). */
4161 if (n == 0)
4163 /* We should read at least the THIS parameter here. Some broken stabs
4164 output contained `(0,41),(0,42)=@s8;-16;,(0,43),(0,1);' where should
4165 have been present ";-16,(0,43)" reference instead. This way the
4166 excessive ";" marker prematurely stops the parameters parsing. */
4168 complaint (_("Invalid (empty) method arguments"));
4169 *varargsp = 0;
4171 else if (types[n - 1]->code () != TYPE_CODE_VOID)
4172 *varargsp = 1;
4173 else
4175 n--;
4176 *varargsp = 0;
4179 rval = XCNEWVEC (struct field, n);
4180 for (i = 0; i < n; i++)
4181 rval[i].set_type (types[i]);
4182 *nargsp = n;
4183 return rval;
4186 /* Common block handling. */
4188 /* List of symbols declared since the last BCOMM. This list is a tail
4189 of local_symbols. When ECOMM is seen, the symbols on the list
4190 are noted so their proper addresses can be filled in later,
4191 using the common block base address gotten from the assembler
4192 stabs. */
4194 static struct pending *common_block;
4195 static int common_block_i;
4197 /* Name of the current common block. We get it from the BCOMM instead of the
4198 ECOMM to match IBM documentation (even though IBM puts the name both places
4199 like everyone else). */
4200 static char *common_block_name;
4202 /* Process a N_BCOMM symbol. The storage for NAME is not guaranteed
4203 to remain after this function returns. */
4205 void
4206 common_block_start (const char *name, struct objfile *objfile)
4208 if (common_block_name != NULL)
4210 complaint (_("Invalid symbol data: common block within common block"));
4212 common_block = *get_local_symbols ();
4213 common_block_i = common_block ? common_block->nsyms : 0;
4214 common_block_name = obstack_strdup (&objfile->objfile_obstack, name);
4217 /* Process a N_ECOMM symbol. */
4219 void
4220 common_block_end (struct objfile *objfile)
4222 /* Symbols declared since the BCOMM are to have the common block
4223 start address added in when we know it. common_block and
4224 common_block_i point to the first symbol after the BCOMM in
4225 the local_symbols list; copy the list and hang it off the
4226 symbol for the common block name for later fixup. */
4227 int i;
4228 struct symbol *sym;
4229 struct pending *newobj = 0;
4230 struct pending *next;
4231 int j;
4233 if (common_block_name == NULL)
4235 complaint (_("ECOMM symbol unmatched by BCOMM"));
4236 return;
4239 sym = new (&objfile->objfile_obstack) symbol;
4240 /* Note: common_block_name already saved on objfile_obstack. */
4241 sym->set_linkage_name (common_block_name);
4242 sym->set_aclass_index (LOC_BLOCK);
4244 /* Now we copy all the symbols which have been defined since the BCOMM. */
4246 /* Copy all the struct pendings before common_block. */
4247 for (next = *get_local_symbols ();
4248 next != NULL && next != common_block;
4249 next = next->next)
4251 for (j = 0; j < next->nsyms; j++)
4252 add_symbol_to_list (next->symbol[j], &newobj);
4255 /* Copy however much of COMMON_BLOCK we need. If COMMON_BLOCK is
4256 NULL, it means copy all the local symbols (which we already did
4257 above). */
4259 if (common_block != NULL)
4260 for (j = common_block_i; j < common_block->nsyms; j++)
4261 add_symbol_to_list (common_block->symbol[j], &newobj);
4263 sym->set_type ((struct type *) newobj);
4265 /* Should we be putting local_symbols back to what it was?
4266 Does it matter? */
4268 i = hashname (sym->linkage_name ());
4269 sym->set_value_chain (global_sym_chain[i]);
4270 global_sym_chain[i] = sym;
4271 common_block_name = NULL;
4274 /* Add a common block's start address to the offset of each symbol
4275 declared to be in it (by being between a BCOMM/ECOMM pair that uses
4276 the common block name). */
4278 static void
4279 fix_common_block (struct symbol *sym, CORE_ADDR valu, int section_index)
4281 struct pending *next = (struct pending *) sym->type ();
4283 for (; next; next = next->next)
4285 int j;
4287 for (j = next->nsyms - 1; j >= 0; j--)
4289 next->symbol[j]->set_value_address
4290 (next->symbol[j]->value_address () + valu);
4291 next->symbol[j]->set_section_index (section_index);
4298 /* Add {TYPE, TYPENUMS} to the NONAME_UNDEFS vector.
4299 See add_undefined_type for more details. */
4301 static void
4302 add_undefined_type_noname (struct type *type, int typenums[2])
4304 struct nat nat;
4306 nat.typenums[0] = typenums [0];
4307 nat.typenums[1] = typenums [1];
4308 nat.type = type;
4310 if (noname_undefs_length == noname_undefs_allocated)
4312 noname_undefs_allocated *= 2;
4313 noname_undefs = (struct nat *)
4314 xrealloc ((char *) noname_undefs,
4315 noname_undefs_allocated * sizeof (struct nat));
4317 noname_undefs[noname_undefs_length++] = nat;
4320 /* Add TYPE to the UNDEF_TYPES vector.
4321 See add_undefined_type for more details. */
4323 static void
4324 add_undefined_type_1 (struct type *type)
4326 if (undef_types_length == undef_types_allocated)
4328 undef_types_allocated *= 2;
4329 undef_types = (struct type **)
4330 xrealloc ((char *) undef_types,
4331 undef_types_allocated * sizeof (struct type *));
4333 undef_types[undef_types_length++] = type;
4336 /* What about types defined as forward references inside of a small lexical
4337 scope? */
4338 /* Add a type to the list of undefined types to be checked through
4339 once this file has been read in.
4341 In practice, we actually maintain two such lists: The first list
4342 (UNDEF_TYPES) is used for types whose name has been provided, and
4343 concerns forward references (eg 'xs' or 'xu' forward references);
4344 the second list (NONAME_UNDEFS) is used for types whose name is
4345 unknown at creation time, because they were referenced through
4346 their type number before the actual type was declared.
4347 This function actually adds the given type to the proper list. */
4349 static void
4350 add_undefined_type (struct type *type, int typenums[2])
4352 if (type->name () == NULL)
4353 add_undefined_type_noname (type, typenums);
4354 else
4355 add_undefined_type_1 (type);
4358 /* Try to fix all undefined types pushed on the UNDEF_TYPES vector. */
4360 static void
4361 cleanup_undefined_types_noname (struct objfile *objfile)
4363 int i;
4365 for (i = 0; i < noname_undefs_length; i++)
4367 struct nat nat = noname_undefs[i];
4368 struct type **type;
4370 type = dbx_lookup_type (nat.typenums, objfile);
4371 if (nat.type != *type && (*type)->code () != TYPE_CODE_UNDEF)
4373 /* The instance flags of the undefined type are still unset,
4374 and needs to be copied over from the reference type.
4375 Since replace_type expects them to be identical, we need
4376 to set these flags manually before hand. */
4377 nat.type->set_instance_flags ((*type)->instance_flags ());
4378 replace_type (nat.type, *type);
4382 noname_undefs_length = 0;
4385 /* Go through each undefined type, see if it's still undefined, and fix it
4386 up if possible. We have two kinds of undefined types:
4388 TYPE_CODE_ARRAY: Array whose target type wasn't defined yet.
4389 Fix: update array length using the element bounds
4390 and the target type's length.
4391 TYPE_CODE_STRUCT, TYPE_CODE_UNION: Structure whose fields were not
4392 yet defined at the time a pointer to it was made.
4393 Fix: Do a full lookup on the struct/union tag. */
4395 static void
4396 cleanup_undefined_types_1 (void)
4398 struct type **type;
4400 /* Iterate over every undefined type, and look for a symbol whose type
4401 matches our undefined type. The symbol matches if:
4402 1. It is a typedef in the STRUCT domain;
4403 2. It has the same name, and same type code;
4404 3. The instance flags are identical.
4406 It is important to check the instance flags, because we have seen
4407 examples where the debug info contained definitions such as:
4409 "foo_t:t30=B31=xefoo_t:"
4411 In this case, we have created an undefined type named "foo_t" whose
4412 instance flags is null (when processing "xefoo_t"), and then created
4413 another type with the same name, but with different instance flags
4414 ('B' means volatile). I think that the definition above is wrong,
4415 since the same type cannot be volatile and non-volatile at the same
4416 time, but we need to be able to cope with it when it happens. The
4417 approach taken here is to treat these two types as different. */
4419 for (type = undef_types; type < undef_types + undef_types_length; type++)
4421 switch ((*type)->code ())
4424 case TYPE_CODE_STRUCT:
4425 case TYPE_CODE_UNION:
4426 case TYPE_CODE_ENUM:
4428 /* Check if it has been defined since. Need to do this here
4429 as well as in check_typedef to deal with the (legitimate in
4430 C though not C++) case of several types with the same name
4431 in different source files. */
4432 if ((*type)->is_stub ())
4434 struct pending *ppt;
4435 int i;
4436 /* Name of the type, without "struct" or "union". */
4437 const char *type_name = (*type)->name ();
4439 if (type_name == NULL)
4441 complaint (_("need a type name"));
4442 break;
4444 for (ppt = *get_file_symbols (); ppt; ppt = ppt->next)
4446 for (i = 0; i < ppt->nsyms; i++)
4448 struct symbol *sym = ppt->symbol[i];
4450 if (sym->aclass () == LOC_TYPEDEF
4451 && sym->domain () == STRUCT_DOMAIN
4452 && (sym->type ()->code () == (*type)->code ())
4453 && ((*type)->instance_flags ()
4454 == sym->type ()->instance_flags ())
4455 && strcmp (sym->linkage_name (), type_name) == 0)
4456 replace_type (*type, sym->type ());
4461 break;
4463 default:
4465 complaint (_("forward-referenced types left unresolved, "
4466 "type code %d."),
4467 (*type)->code ());
4469 break;
4473 undef_types_length = 0;
4476 /* Try to fix all the undefined types we encountered while processing
4477 this unit. */
4479 void
4480 cleanup_undefined_stabs_types (struct objfile *objfile)
4482 cleanup_undefined_types_1 ();
4483 cleanup_undefined_types_noname (objfile);
4486 /* See stabsread.h. */
4488 void
4489 scan_file_globals (struct objfile *objfile)
4491 int hash;
4492 struct symbol *sym, *prev;
4493 struct objfile *resolve_objfile;
4495 /* SVR4 based linkers copy referenced global symbols from shared
4496 libraries to the main executable.
4497 If we are scanning the symbols for a shared library, try to resolve
4498 them from the minimal symbols of the main executable first. */
4500 if (current_program_space->symfile_object_file
4501 && objfile != current_program_space->symfile_object_file)
4502 resolve_objfile = current_program_space->symfile_object_file;
4503 else
4504 resolve_objfile = objfile;
4506 while (1)
4508 /* Avoid expensive loop through all minimal symbols if there are
4509 no unresolved symbols. */
4510 for (hash = 0; hash < HASHSIZE; hash++)
4512 if (global_sym_chain[hash])
4513 break;
4515 if (hash >= HASHSIZE)
4516 return;
4518 for (minimal_symbol *msymbol : resolve_objfile->msymbols ())
4520 QUIT;
4522 /* Skip static symbols. */
4523 switch (msymbol->type ())
4525 case mst_file_text:
4526 case mst_file_data:
4527 case mst_file_bss:
4528 continue;
4529 default:
4530 break;
4533 prev = NULL;
4535 /* Get the hash index and check all the symbols
4536 under that hash index. */
4538 hash = hashname (msymbol->linkage_name ());
4540 for (sym = global_sym_chain[hash]; sym;)
4542 if (strcmp (msymbol->linkage_name (), sym->linkage_name ()) == 0)
4544 /* Splice this symbol out of the hash chain and
4545 assign the value we have to it. */
4546 if (prev)
4548 prev->set_value_chain (sym->value_chain ());
4550 else
4552 global_sym_chain[hash] = sym->value_chain ();
4555 /* Check to see whether we need to fix up a common block. */
4556 /* Note: this code might be executed several times for
4557 the same symbol if there are multiple references. */
4558 if (sym)
4560 if (sym->aclass () == LOC_BLOCK)
4561 fix_common_block
4562 (sym, msymbol->value_address (resolve_objfile),
4563 msymbol->section_index ());
4564 else
4565 sym->set_value_address
4566 (msymbol->value_address (resolve_objfile));
4567 sym->set_section_index (msymbol->section_index ());
4570 if (prev)
4572 sym = prev->value_chain ();
4574 else
4576 sym = global_sym_chain[hash];
4579 else
4581 prev = sym;
4582 sym = sym->value_chain ();
4586 if (resolve_objfile == objfile)
4587 break;
4588 resolve_objfile = objfile;
4591 /* Change the storage class of any remaining unresolved globals to
4592 LOC_UNRESOLVED and remove them from the chain. */
4593 for (hash = 0; hash < HASHSIZE; hash++)
4595 sym = global_sym_chain[hash];
4596 while (sym)
4598 prev = sym;
4599 sym = sym->value_chain ();
4601 /* Change the symbol address from the misleading chain value
4602 to address zero. */
4603 prev->set_value_address (0);
4605 /* Complain about unresolved common block symbols. */
4606 if (prev->aclass () == LOC_STATIC)
4607 prev->set_aclass_index (LOC_UNRESOLVED);
4608 else
4609 complaint (_("%s: common block `%s' from "
4610 "global_sym_chain unresolved"),
4611 objfile_name (objfile), prev->print_name ());
4614 memset (global_sym_chain, 0, sizeof (global_sym_chain));
4617 /* Initialize anything that needs initializing when starting to read
4618 a fresh piece of a symbol file, e.g. reading in the stuff corresponding
4619 to a psymtab. */
4621 void
4622 stabsread_init (void)
4626 /* Initialize anything that needs initializing when a completely new
4627 symbol file is specified (not just adding some symbols from another
4628 file, e.g. a shared library). */
4630 void
4631 stabsread_new_init (void)
4633 /* Empty the hash table of global syms looking for values. */
4634 memset (global_sym_chain, 0, sizeof (global_sym_chain));
4637 /* Initialize anything that needs initializing at the same time as
4638 start_compunit_symtab() is called. */
4640 void
4641 start_stabs (void)
4643 global_stabs = NULL; /* AIX COFF */
4644 /* Leave FILENUM of 0 free for builtin types and this file's types. */
4645 n_this_object_header_files = 1;
4646 type_vector_length = 0;
4647 type_vector = (struct type **) 0;
4648 within_function = 0;
4650 /* FIXME: If common_block_name is not already NULL, we should complain(). */
4651 common_block_name = NULL;
4654 /* Call after end_compunit_symtab(). */
4656 void
4657 end_stabs (void)
4659 if (type_vector)
4661 xfree (type_vector);
4663 type_vector = 0;
4664 type_vector_length = 0;
4665 previous_stab_code = 0;
4668 void
4669 finish_global_stabs (struct objfile *objfile)
4671 if (global_stabs)
4673 patch_block_stabs (*get_global_symbols (), global_stabs, objfile);
4674 xfree (global_stabs);
4675 global_stabs = NULL;
4679 /* Find the end of the name, delimited by a ':', but don't match
4680 ObjC symbols which look like -[Foo bar::]:bla. */
4681 static const char *
4682 find_name_end (const char *name)
4684 const char *s = name;
4686 if (s[0] == '-' || *s == '+')
4688 /* Must be an ObjC method symbol. */
4689 if (s[1] != '[')
4691 error (_("invalid symbol name \"%s\""), name);
4693 s = strchr (s, ']');
4694 if (s == NULL)
4696 error (_("invalid symbol name \"%s\""), name);
4698 return strchr (s, ':');
4700 else
4702 return strchr (s, ':');
4706 /* See stabsread.h. */
4709 hashname (const char *name)
4711 return fast_hash (name, strlen (name)) % HASHSIZE;
4714 /* Initializer for this module. */
4716 void _initialize_stabsread ();
4717 void
4718 _initialize_stabsread ()
4720 undef_types_allocated = 20;
4721 undef_types_length = 0;
4722 undef_types = XNEWVEC (struct type *, undef_types_allocated);
4724 noname_undefs_allocated = 20;
4725 noname_undefs_length = 0;
4726 noname_undefs = XNEWVEC (struct nat, noname_undefs_allocated);
4728 stab_register_index = register_symbol_register_impl (LOC_REGISTER,
4729 &stab_register_funcs);
4730 stab_regparm_index = register_symbol_register_impl (LOC_REGPARM_ADDR,
4731 &stab_register_funcs);