1 /* Support routines for manipulating internal types for GDB.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3 Contributed by Cygnus Support, using pieces from other GDB modules.
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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #include "expression.h"
33 #include "complaints.h"
35 /* These variables point to the objects
36 representing the predefined C data types. */
38 struct type *builtin_type_void;
39 struct type *builtin_type_char;
40 struct type *builtin_type_short;
41 struct type *builtin_type_int;
42 struct type *builtin_type_long;
43 struct type *builtin_type_long_long;
44 struct type *builtin_type_signed_char;
45 struct type *builtin_type_unsigned_char;
46 struct type *builtin_type_unsigned_short;
47 struct type *builtin_type_unsigned_int;
48 struct type *builtin_type_unsigned_long;
49 struct type *builtin_type_unsigned_long_long;
50 struct type *builtin_type_float;
51 struct type *builtin_type_double;
52 struct type *builtin_type_long_double;
53 struct type *builtin_type_complex;
54 struct type *builtin_type_double_complex;
55 struct type *builtin_type_string;
57 /* Alloc a new type structure and fill it with some defaults. If
58 OBJFILE is non-NULL, then allocate the space for the type structure
59 in that objfile's type_obstack. */
63 struct objfile *objfile;
65 register struct type *type;
67 /* Alloc the structure and start off with all fields zeroed. */
71 type = (struct type *) xmalloc (sizeof (struct type));
75 type = (struct type *) obstack_alloc (&objfile -> type_obstack,
76 sizeof (struct type));
78 memset ((char *) type, 0, sizeof (struct type));
80 /* Initialize the fields that might not be zero. */
82 TYPE_CODE (type) = TYPE_CODE_UNDEF;
83 TYPE_OBJFILE (type) = objfile;
84 TYPE_VPTR_FIELDNO (type) = -1;
89 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
90 to a pointer to memory where the pointer type should be stored.
91 If *TYPEPTR is zero, update it to point to the pointer type we return.
92 We allocate new memory if needed. */
95 make_pointer_type (type, typeptr)
97 struct type **typeptr;
99 register struct type *ntype; /* New type */
100 struct objfile *objfile;
102 ntype = TYPE_POINTER_TYPE (type);
106 return ntype; /* Don't care about alloc, and have new type. */
107 else if (*typeptr == 0)
109 *typeptr = ntype; /* Tracking alloc, and we have new type. */
113 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
115 ntype = alloc_type (TYPE_OBJFILE (type));
119 else /* We have storage, but need to reset it. */
122 objfile = TYPE_OBJFILE (ntype);
123 memset ((char *) ntype, 0, sizeof (struct type));
124 TYPE_OBJFILE (ntype) = objfile;
127 TYPE_TARGET_TYPE (ntype) = type;
128 TYPE_POINTER_TYPE (type) = ntype;
130 /* FIXME! Assume the machine has only one representation for pointers! */
132 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
133 TYPE_CODE (ntype) = TYPE_CODE_PTR;
135 /* pointers are unsigned */
136 TYPE_FLAGS (ntype) |= TYPE_FLAG_UNSIGNED;
138 if (!TYPE_POINTER_TYPE (type)) /* Remember it, if don't have one. */
139 TYPE_POINTER_TYPE (type) = ntype;
144 /* Given a type TYPE, return a type of pointers to that type.
145 May need to construct such a type if this is the first use. */
148 lookup_pointer_type (type)
151 return make_pointer_type (type, (struct type **)0);
154 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero, points
155 to a pointer to memory where the reference type should be stored.
156 If *TYPEPTR is zero, update it to point to the reference type we return.
157 We allocate new memory if needed. */
160 make_reference_type (type, typeptr)
162 struct type **typeptr;
164 register struct type *ntype; /* New type */
165 struct objfile *objfile;
167 ntype = TYPE_REFERENCE_TYPE (type);
171 return ntype; /* Don't care about alloc, and have new type. */
172 else if (*typeptr == 0)
174 *typeptr = ntype; /* Tracking alloc, and we have new type. */
178 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
180 ntype = alloc_type (TYPE_OBJFILE (type));
184 else /* We have storage, but need to reset it. */
187 objfile = TYPE_OBJFILE (ntype);
188 memset ((char *) ntype, 0, sizeof (struct type));
189 TYPE_OBJFILE (ntype) = objfile;
192 TYPE_TARGET_TYPE (ntype) = type;
193 TYPE_REFERENCE_TYPE (type) = ntype;
195 /* FIXME! Assume the machine has only one representation for references,
196 and that it matches the (only) representation for pointers! */
198 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
199 TYPE_CODE (ntype) = TYPE_CODE_REF;
201 if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */
202 TYPE_REFERENCE_TYPE (type) = ntype;
207 /* Same as above, but caller doesn't care about memory allocation details. */
210 lookup_reference_type (type)
213 return make_reference_type (type, (struct type **)0);
216 /* Lookup a function type that returns type TYPE. TYPEPTR, if nonzero, points
217 to a pointer to memory where the function type should be stored.
218 If *TYPEPTR is zero, update it to point to the function type we return.
219 We allocate new memory if needed. */
222 make_function_type (type, typeptr)
224 struct type **typeptr;
226 register struct type *ntype; /* New type */
227 struct objfile *objfile;
229 ntype = TYPE_FUNCTION_TYPE (type);
233 return ntype; /* Don't care about alloc, and have new type. */
234 else if (*typeptr == 0)
236 *typeptr = ntype; /* Tracking alloc, and we have new type. */
240 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
242 ntype = alloc_type (TYPE_OBJFILE (type));
246 else /* We have storage, but need to reset it. */
249 objfile = TYPE_OBJFILE (ntype);
250 memset ((char *) ntype, 0, sizeof (struct type));
251 TYPE_OBJFILE (ntype) = objfile;
254 TYPE_TARGET_TYPE (ntype) = type;
255 TYPE_FUNCTION_TYPE (type) = ntype;
257 TYPE_LENGTH (ntype) = 1;
258 TYPE_CODE (ntype) = TYPE_CODE_FUNC;
260 if (!TYPE_FUNCTION_TYPE (type)) /* Remember it, if don't have one. */
261 TYPE_FUNCTION_TYPE (type) = ntype;
267 /* Given a type TYPE, return a type of functions that return that type.
268 May need to construct such a type if this is the first use. */
271 lookup_function_type (type)
274 return make_function_type (type, (struct type **)0);
277 /* Implement direct support for MEMBER_TYPE in GNU C++.
278 May need to construct such a type if this is the first use.
279 The TYPE is the type of the member. The DOMAIN is the type
280 of the aggregate that the member belongs to. */
283 lookup_member_type (type, domain)
287 register struct type *mtype;
289 mtype = alloc_type (TYPE_OBJFILE (type));
290 smash_to_member_type (mtype, domain, type);
294 /* Allocate a stub method whose return type is TYPE.
295 This apparently happens for speed of symbol reading, since parsing
296 out the arguments to the method is cpu-intensive, the way we are doing
297 it. So, we will fill in arguments later.
298 This always returns a fresh type. */
301 allocate_stub_method (type)
306 mtype = alloc_type (TYPE_OBJFILE (type));
307 TYPE_TARGET_TYPE (mtype) = type;
308 /* _DOMAIN_TYPE (mtype) = unknown yet */
309 /* _ARG_TYPES (mtype) = unknown yet */
310 TYPE_FLAGS (mtype) = TYPE_FLAG_STUB;
311 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
312 TYPE_LENGTH (mtype) = 1;
316 /* Create a range type using either a blank type supplied in RESULT_TYPE,
317 or creating a new type, inheriting the objfile from INDEX_TYPE.
319 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND to
320 HIGH_BOUND, inclusive.
322 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
323 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
326 create_range_type (result_type, index_type, low_bound, high_bound)
327 struct type *result_type;
328 struct type *index_type;
332 if (result_type == NULL)
334 result_type = alloc_type (TYPE_OBJFILE (index_type));
336 TYPE_CODE (result_type) = TYPE_CODE_RANGE;
337 TYPE_TARGET_TYPE (result_type) = index_type;
338 TYPE_LENGTH (result_type) = TYPE_LENGTH (index_type);
339 TYPE_NFIELDS (result_type) = 2;
340 TYPE_FIELDS (result_type) = (struct field *)
341 TYPE_ALLOC (result_type, 2 * sizeof (struct field));
342 memset (TYPE_FIELDS (result_type), 0, 2 * sizeof (struct field));
343 TYPE_FIELD_BITPOS (result_type, 0) = low_bound;
344 TYPE_FIELD_BITPOS (result_type, 1) = high_bound;
345 TYPE_FIELD_TYPE (result_type, 0) = builtin_type_int; /* FIXME */
346 TYPE_FIELD_TYPE (result_type, 1) = builtin_type_int; /* FIXME */
348 return (result_type);
351 /* A lot of code assumes that the "index type" of an array/string/
352 set/bitstring is specifically a range type, though in some languages
353 it can be any discrete type. */
356 force_to_range_type (type)
359 switch (TYPE_CODE (type))
361 case TYPE_CODE_RANGE:
366 int low_bound = TYPE_FIELD_BITPOS (type, 0);
367 int high_bound = TYPE_FIELD_BITPOS (type, TYPE_NFIELDS (type) - 1);
368 struct type *range_type =
369 create_range_type (NULL, type, low_bound, high_bound);
370 TYPE_NAME (range_type) = TYPE_NAME (range_type);
371 TYPE_DUMMY_RANGE (range_type) = 1;
376 struct type *range_type = create_range_type (NULL, type, 0, 1);
377 TYPE_NAME (range_type) = TYPE_NAME (range_type);
378 TYPE_DUMMY_RANGE (range_type) = 1;
383 struct type *range_type = create_range_type (NULL, type, 0, 255);
384 TYPE_NAME (range_type) = TYPE_NAME (range_type);
385 TYPE_DUMMY_RANGE (range_type) = 1;
390 static struct complaint msg =
391 { "array index type must be a discrete type", 0, 0};
394 return create_range_type (NULL, builtin_type_int, 0, 0);
399 /* Create an array type using either a blank type supplied in RESULT_TYPE,
400 or creating a new type, inheriting the objfile from RANGE_TYPE.
402 Elements will be of type ELEMENT_TYPE, the indices will be of type
405 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
406 sure it is TYPE_CODE_UNDEF before we bash it into an array type? */
409 create_array_type (result_type, element_type, range_type)
410 struct type *result_type;
411 struct type *element_type;
412 struct type *range_type;
417 range_type = force_to_range_type (range_type);
418 if (result_type == NULL)
420 result_type = alloc_type (TYPE_OBJFILE (range_type));
422 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
423 TYPE_TARGET_TYPE (result_type) = element_type;
424 low_bound = TYPE_LOW_BOUND (range_type);
425 high_bound = TYPE_HIGH_BOUND (range_type);
426 TYPE_LENGTH (result_type) =
427 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
428 TYPE_NFIELDS (result_type) = 1;
429 TYPE_FIELDS (result_type) =
430 (struct field *) TYPE_ALLOC (result_type, sizeof (struct field));
431 memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
432 TYPE_FIELD_TYPE (result_type, 0) = range_type;
433 TYPE_VPTR_FIELDNO (result_type) = -1;
435 return (result_type);
438 /* Create a string type using either a blank type supplied in RESULT_TYPE,
439 or creating a new type. String types are similar enough to array of
440 char types that we can use create_array_type to build the basic type
441 and then bash it into a string type.
443 For fixed length strings, the range type contains 0 as the lower
444 bound and the length of the string minus one as the upper bound.
446 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
447 sure it is TYPE_CODE_UNDEF before we bash it into a string type? */
450 create_string_type (result_type, range_type)
451 struct type *result_type;
452 struct type *range_type;
454 result_type = create_array_type (result_type, builtin_type_char, range_type);
455 TYPE_CODE (result_type) = TYPE_CODE_STRING;
456 return (result_type);
460 create_set_type (result_type, domain_type)
461 struct type *result_type;
462 struct type *domain_type;
464 int low_bound, high_bound, bit_length;
465 if (result_type == NULL)
467 result_type = alloc_type (TYPE_OBJFILE (domain_type));
469 domain_type = force_to_range_type (domain_type);
470 TYPE_CODE (result_type) = TYPE_CODE_SET;
471 TYPE_NFIELDS (result_type) = 1;
472 TYPE_FIELDS (result_type) = (struct field *)
473 TYPE_ALLOC (result_type, 1 * sizeof (struct field));
474 memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
475 TYPE_FIELD_TYPE (result_type, 0) = domain_type;
476 low_bound = TYPE_LOW_BOUND (domain_type);
477 high_bound = TYPE_HIGH_BOUND (domain_type);
478 bit_length = high_bound - low_bound + 1;
479 if (bit_length <= TARGET_CHAR_BIT)
480 TYPE_LENGTH (result_type) = 1;
481 else if (bit_length <= TARGET_SHORT_BIT)
482 TYPE_LENGTH (result_type) = TARGET_SHORT_BIT / TARGET_CHAR_BIT;
484 TYPE_LENGTH (result_type)
485 = ((bit_length + TARGET_INT_BIT - 1) / TARGET_INT_BIT)
487 return (result_type);
490 /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.
491 A MEMBER is a wierd thing -- it amounts to a typed offset into
492 a struct, e.g. "an int at offset 8". A MEMBER TYPE doesn't
493 include the offset (that's the value of the MEMBER itself), but does
494 include the structure type into which it points (for some reason).
496 When "smashing" the type, we preserve the objfile that the
497 old type pointed to, since we aren't changing where the type is actually
501 smash_to_member_type (type, domain, to_type)
504 struct type *to_type;
506 struct objfile *objfile;
508 objfile = TYPE_OBJFILE (type);
510 memset ((char *) type, 0, sizeof (struct type));
511 TYPE_OBJFILE (type) = objfile;
512 TYPE_TARGET_TYPE (type) = to_type;
513 TYPE_DOMAIN_TYPE (type) = domain;
514 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
515 TYPE_CODE (type) = TYPE_CODE_MEMBER;
518 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
519 METHOD just means `function that gets an extra "this" argument'.
521 When "smashing" the type, we preserve the objfile that the
522 old type pointed to, since we aren't changing where the type is actually
526 smash_to_method_type (type, domain, to_type, args)
529 struct type *to_type;
532 struct objfile *objfile;
534 objfile = TYPE_OBJFILE (type);
536 memset ((char *) type, 0, sizeof (struct type));
537 TYPE_OBJFILE (type) = objfile;
538 TYPE_TARGET_TYPE (type) = to_type;
539 TYPE_DOMAIN_TYPE (type) = domain;
540 TYPE_ARG_TYPES (type) = args;
541 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
542 TYPE_CODE (type) = TYPE_CODE_METHOD;
545 /* Return a typename for a struct/union/enum type without "struct ",
546 "union ", or "enum ". If the type has a NULL name, return NULL. */
549 type_name_no_tag (type)
550 register const struct type *type;
552 if (TYPE_TAG_NAME (type) != NULL)
553 return TYPE_TAG_NAME (type);
555 /* Is there code which expects this to return the name if there is no
556 tag name? My guess is that this is mainly used for C++ in cases where
557 the two will always be the same. */
558 return TYPE_NAME (type);
561 /* Lookup a primitive type named NAME.
562 Return zero if NAME is not a primitive type.*/
565 lookup_primitive_typename (name)
568 struct type ** const *p;
570 for (p = current_language -> la_builtin_type_vector; *p != NULL; p++)
572 if (STREQ ((**p) -> name, name))
580 /* Lookup a typedef or primitive type named NAME,
581 visible in lexical block BLOCK.
582 If NOERR is nonzero, return zero if NAME is not suitably defined. */
585 lookup_typename (name, block, noerr)
590 register struct symbol *sym;
591 register struct type *tmp;
593 sym = lookup_symbol (name, block, VAR_NAMESPACE, 0, (struct symtab **) NULL);
594 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
596 tmp = lookup_primitive_typename (name);
601 else if (!tmp && noerr)
607 error ("No type named %s.", name);
610 return (SYMBOL_TYPE (sym));
614 lookup_unsigned_typename (name)
617 char *uns = alloca (strlen (name) + 10);
619 strcpy (uns, "unsigned ");
620 strcpy (uns + 9, name);
621 return (lookup_typename (uns, (struct block *) NULL, 0));
625 lookup_signed_typename (name)
629 char *uns = alloca (strlen (name) + 8);
631 strcpy (uns, "signed ");
632 strcpy (uns + 7, name);
633 t = lookup_typename (uns, (struct block *) NULL, 1);
634 /* If we don't find "signed FOO" just try again with plain "FOO". */
637 return lookup_typename (name, (struct block *) NULL, 0);
640 /* Lookup a structure type named "struct NAME",
641 visible in lexical block BLOCK. */
644 lookup_struct (name, block)
648 register struct symbol *sym;
650 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
651 (struct symtab **) NULL);
655 error ("No struct type named %s.", name);
657 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
659 error ("This context has class, union or enum %s, not a struct.", name);
661 return (SYMBOL_TYPE (sym));
664 /* Lookup a union type named "union NAME",
665 visible in lexical block BLOCK. */
668 lookup_union (name, block)
672 register struct symbol *sym;
674 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
675 (struct symtab **) NULL);
679 error ("No union type named %s.", name);
681 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
683 error ("This context has class, struct or enum %s, not a union.", name);
685 return (SYMBOL_TYPE (sym));
688 /* Lookup an enum type named "enum NAME",
689 visible in lexical block BLOCK. */
692 lookup_enum (name, block)
696 register struct symbol *sym;
698 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
699 (struct symtab **) NULL);
702 error ("No enum type named %s.", name);
704 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
706 error ("This context has class, struct or union %s, not an enum.", name);
708 return (SYMBOL_TYPE (sym));
711 /* Lookup a template type named "template NAME<TYPE>",
712 visible in lexical block BLOCK. */
715 lookup_template_type (name, type, block)
721 char *nam = (char*) alloca(strlen(name) + strlen(type->name) + 4);
724 strcat (nam, type->name);
725 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
727 sym = lookup_symbol (nam, block, VAR_NAMESPACE, 0, (struct symtab **)NULL);
731 error ("No template type named %s.", name);
733 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
735 error ("This context has class, union or enum %s, not a struct.", name);
737 return (SYMBOL_TYPE (sym));
740 /* Given a type TYPE, lookup the type of the component of type named NAME.
742 TYPE can be either a struct or union, or a pointer or reference to a struct or
743 union. If it is a pointer or reference, its target type is automatically used.
744 Thus '.' and '->' are interchangable, as specified for the definitions of the
745 expression element types STRUCTOP_STRUCT and STRUCTOP_PTR.
747 If NOERR is nonzero, return zero if NAME is not suitably defined.
748 If NAME is the name of a baseclass type, return that type. */
751 lookup_struct_elt_type (type, name, noerr)
758 while (TYPE_CODE (type) == TYPE_CODE_PTR ||
759 TYPE_CODE (type) == TYPE_CODE_REF)
760 type = TYPE_TARGET_TYPE (type);
762 if (TYPE_CODE (type) != TYPE_CODE_STRUCT &&
763 TYPE_CODE (type) != TYPE_CODE_UNION)
765 target_terminal_ours ();
766 gdb_flush (gdb_stdout);
767 fprintf_unfiltered (gdb_stderr, "Type ");
768 type_print (type, "", gdb_stderr, -1);
769 error (" is not a structure or union type.");
772 check_stub_type (type);
775 /* FIXME: This change put in by Michael seems incorrect for the case where
776 the structure tag name is the same as the member name. I.E. when doing
777 "ptype bell->bar" for "struct foo { int bar; int foo; } bell;"
782 typename = type_name_no_tag (type);
783 if (typename != NULL && STREQ (typename, name))
788 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
790 char *t_field_name = TYPE_FIELD_NAME (type, i);
792 if (t_field_name && STREQ (t_field_name, name))
794 return TYPE_FIELD_TYPE (type, i);
798 /* OK, it's not in this class. Recursively check the baseclasses. */
799 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
803 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, noerr);
815 target_terminal_ours ();
816 gdb_flush (gdb_stdout);
817 fprintf_unfiltered (gdb_stderr, "Type ");
818 type_print (type, "", gdb_stderr, -1);
819 fprintf_unfiltered (gdb_stderr, " has no component named ");
820 fputs_filtered (name, gdb_stderr);
822 return (struct type *)-1; /* For lint */
825 /* If possible, make the vptr_fieldno and vptr_basetype fields of TYPE
826 valid. Callers should be aware that in some cases (for example,
827 the type or one of its baseclasses is a stub type and we are
828 debugging a .o file), this function will not be able to find the virtual
829 function table pointer, and vptr_fieldno will remain -1 and vptr_basetype
833 fill_in_vptr_fieldno (type)
836 check_stub_type (type);
838 if (TYPE_VPTR_FIELDNO (type) < 0)
842 /* We must start at zero in case the first (and only) baseclass is
843 virtual (and hence we cannot share the table pointer). */
844 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
846 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
847 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
849 TYPE_VPTR_FIELDNO (type)
850 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
851 TYPE_VPTR_BASETYPE (type)
852 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
859 /* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
861 If this is a stubbed struct (i.e. declared as struct foo *), see if
862 we can find a full definition in some other file. If so, copy this
863 definition, so we can use it in future. There used to be a comment (but
864 not any code) that if we don't find a full definition, we'd set a flag
865 so we don't spend time in the future checking the same type. That would
866 be a mistake, though--we might load in more symbols which contain a
867 full definition for the type.
869 This used to be coded as a macro, but I don't think it is called
870 often enough to merit such treatment. */
872 struct complaint stub_noname_complaint =
873 {"stub type has NULL name", 0, 0};
876 check_stub_type (type)
879 if (TYPE_FLAGS(type) & TYPE_FLAG_STUB)
881 char* name = type_name_no_tag (type);
882 /* FIXME: shouldn't we separately check the TYPE_NAME and the
883 TYPE_TAG_NAME, and look in STRUCT_NAMESPACE and/or VAR_NAMESPACE
884 as appropriate? (this code was written before TYPE_NAME and
885 TYPE_TAG_NAME were separate). */
889 complain (&stub_noname_complaint);
892 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
893 (struct symtab **) NULL);
896 memcpy ((char *)type,
897 (char *)SYMBOL_TYPE(sym),
898 sizeof (struct type));
902 if (TYPE_FLAGS (type) & TYPE_FLAG_TARGET_STUB)
904 struct type *range_type;
906 check_stub_type (TYPE_TARGET_TYPE (type));
907 if (!(TYPE_FLAGS (TYPE_TARGET_TYPE (type)) & TYPE_FLAG_STUB)
908 && TYPE_CODE (type) == TYPE_CODE_ARRAY
909 && TYPE_NFIELDS (type) == 1
910 && (TYPE_CODE (range_type = TYPE_FIELD_TYPE (type, 0))
913 /* Now recompute the length of the array type, based on its
914 number of elements and the target type's length. */
916 ((TYPE_FIELD_BITPOS (range_type, 1)
917 - TYPE_FIELD_BITPOS (range_type, 0)
919 * TYPE_LENGTH (TYPE_TARGET_TYPE (type)));
920 TYPE_FLAGS (type) &= ~TYPE_FLAG_TARGET_STUB;
925 /* Ugly hack to convert method stubs into method types.
927 He ain't kiddin'. This demangles the name of the method into a string
928 including argument types, parses out each argument type, generates
929 a string casting a zero to that type, evaluates the string, and stuffs
930 the resulting type into an argtype vector!!! Then it knows the type
931 of the whole function (including argument types for overloading),
932 which info used to be in the stab's but was removed to hack back
933 the space required for them. */
936 check_stub_method (type, i, j)
942 char *mangled_name = gdb_mangle_name (type, i, j);
943 char *demangled_name = cplus_demangle (mangled_name,
944 DMGL_PARAMS | DMGL_ANSI);
945 char *argtypetext, *p;
946 int depth = 0, argcount = 1;
947 struct type **argtypes;
950 if (demangled_name == NULL)
952 error ("Internal: Cannot demangle mangled name `%s'.", mangled_name);
955 /* Now, read in the parameters that define this type. */
956 argtypetext = strchr (demangled_name, '(') + 1;
968 else if (*p == ',' && depth == 0)
976 /* We need two more slots: one for the THIS pointer, and one for the
977 NULL [...] or void [end of arglist]. */
979 argtypes = (struct type **)
980 TYPE_ALLOC (type, (argcount + 2) * sizeof (struct type *));
982 argtypes[0] = lookup_pointer_type (type);
985 if (*p != ')') /* () means no args, skip while */
990 if (depth <= 0 && (*p == ',' || *p == ')'))
992 /* Avoid parsing of ellipsis, they will be handled below. */
993 if (strncmp (argtypetext, "...", p - argtypetext) != 0)
996 parse_and_eval_type (argtypetext, p - argtypetext);
1015 if (p[-2] != '.') /* Not '...' */
1017 argtypes[argcount] = builtin_type_void; /* List terminator */
1021 argtypes[argcount] = NULL; /* Ellist terminator */
1024 free (demangled_name);
1026 f = TYPE_FN_FIELDLIST1 (type, i);
1027 TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
1029 /* Now update the old "stub" type into a real type. */
1030 mtype = TYPE_FN_FIELD_TYPE (f, j);
1031 TYPE_DOMAIN_TYPE (mtype) = type;
1032 TYPE_ARG_TYPES (mtype) = argtypes;
1033 TYPE_FLAGS (mtype) &= ~TYPE_FLAG_STUB;
1034 TYPE_FN_FIELD_STUB (f, j) = 0;
1037 const struct cplus_struct_type cplus_struct_default;
1040 allocate_cplus_struct_type (type)
1043 if (!HAVE_CPLUS_STRUCT (type))
1045 TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
1046 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
1047 *(TYPE_CPLUS_SPECIFIC(type)) = cplus_struct_default;
1051 /* Helper function to initialize the standard scalar types.
1053 If NAME is non-NULL and OBJFILE is non-NULL, then we make a copy
1054 of the string pointed to by name in the type_obstack for that objfile,
1055 and initialize the type name to that copy. There are places (mipsread.c
1056 in particular, where init_type is called with a NULL value for NAME). */
1059 init_type (code, length, flags, name, objfile)
1060 enum type_code code;
1064 struct objfile *objfile;
1066 register struct type *type;
1068 type = alloc_type (objfile);
1069 TYPE_CODE (type) = code;
1070 TYPE_LENGTH (type) = length;
1071 TYPE_FLAGS (type) |= flags;
1072 if ((name != NULL) && (objfile != NULL))
1075 obsavestring (name, strlen (name), &objfile -> type_obstack);
1079 TYPE_NAME (type) = name;
1084 if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
1086 INIT_CPLUS_SPECIFIC (type);
1091 /* Look up a fundamental type for the specified objfile.
1092 May need to construct such a type if this is the first use.
1094 Some object file formats (ELF, COFF, etc) do not define fundamental
1095 types such as "int" or "double". Others (stabs for example), do
1096 define fundamental types.
1098 For the formats which don't provide fundamental types, gdb can create
1099 such types, using defaults reasonable for the current language and
1100 the current target machine.
1102 NOTE: This routine is obsolescent. Each debugging format reader
1103 should manage it's own fundamental types, either creating them from
1104 suitable defaults or reading them from the debugging information,
1105 whichever is appropriate. The DWARF reader has already been
1106 fixed to do this. Once the other readers are fixed, this routine
1107 will go away. Also note that fundamental types should be managed
1108 on a compilation unit basis in a multi-language environment, not
1109 on a linkage unit basis as is done here. */
1113 lookup_fundamental_type (objfile, typeid)
1114 struct objfile *objfile;
1117 register struct type **typep;
1118 register int nbytes;
1120 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
1122 error ("internal error - invalid fundamental type id %d", typeid);
1125 /* If this is the first time we need a fundamental type for this objfile
1126 then we need to initialize the vector of type pointers. */
1128 if (objfile -> fundamental_types == NULL)
1130 nbytes = FT_NUM_MEMBERS * sizeof (struct type *);
1131 objfile -> fundamental_types = (struct type **)
1132 obstack_alloc (&objfile -> type_obstack, nbytes);
1133 memset ((char *) objfile -> fundamental_types, 0, nbytes);
1136 /* Look for this particular type in the fundamental type vector. If one is
1137 not found, create and install one appropriate for the current language. */
1139 typep = objfile -> fundamental_types + typeid;
1142 *typep = create_fundamental_type (objfile, typeid);
1148 #if MAINTENANCE_CMDS
1151 print_bit_vector (bits, nbits)
1157 for (bitno = 0; bitno < nbits; bitno++)
1159 if ((bitno % 8) == 0)
1161 puts_filtered (" ");
1163 if (B_TST (bits, bitno))
1165 printf_filtered ("1");
1169 printf_filtered ("0");
1174 /* The args list is a strange beast. It is either terminated by a NULL
1175 pointer for varargs functions, or by a pointer to a TYPE_CODE_VOID
1176 type for normal fixed argcount functions. (FIXME someday)
1177 Also note the first arg should be the "this" pointer, we may not want to
1178 include it since we may get into a infinitely recursive situation. */
1181 print_arg_types (args, spaces)
1187 while (*args != NULL)
1189 recursive_dump_type (*args, spaces + 2);
1190 if ((*args++) -> code == TYPE_CODE_VOID)
1199 dump_fn_fieldlists (type, spaces)
1207 printfi_filtered (spaces, "fn_fieldlists ");
1208 gdb_print_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
1209 printf_filtered ("\n");
1210 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
1212 f = TYPE_FN_FIELDLIST1 (type, method_idx);
1213 printfi_filtered (spaces + 2, "[%d] name '%s' (",
1215 TYPE_FN_FIELDLIST_NAME (type, method_idx));
1216 gdb_print_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
1218 printf_filtered (") length %d\n",
1219 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
1220 for (overload_idx = 0;
1221 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
1224 printfi_filtered (spaces + 4, "[%d] physname '%s' (",
1226 TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
1227 gdb_print_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
1229 printf_filtered (")\n");
1230 printfi_filtered (spaces + 8, "type ");
1231 gdb_print_address (TYPE_FN_FIELD_TYPE (f, overload_idx), gdb_stdout);
1232 printf_filtered ("\n");
1234 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
1237 printfi_filtered (spaces + 8, "args ");
1238 gdb_print_address (TYPE_FN_FIELD_ARGS (f, overload_idx), gdb_stdout);
1239 printf_filtered ("\n");
1241 print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx), spaces);
1242 printfi_filtered (spaces + 8, "fcontext ");
1243 gdb_print_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
1245 printf_filtered ("\n");
1247 printfi_filtered (spaces + 8, "is_const %d\n",
1248 TYPE_FN_FIELD_CONST (f, overload_idx));
1249 printfi_filtered (spaces + 8, "is_volatile %d\n",
1250 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
1251 printfi_filtered (spaces + 8, "is_private %d\n",
1252 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
1253 printfi_filtered (spaces + 8, "is_protected %d\n",
1254 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
1255 printfi_filtered (spaces + 8, "is_stub %d\n",
1256 TYPE_FN_FIELD_STUB (f, overload_idx));
1257 printfi_filtered (spaces + 8, "voffset %u\n",
1258 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
1264 print_cplus_stuff (type, spaces)
1268 printfi_filtered (spaces, "n_baseclasses %d\n",
1269 TYPE_N_BASECLASSES (type));
1270 printfi_filtered (spaces, "nfn_fields %d\n",
1271 TYPE_NFN_FIELDS (type));
1272 printfi_filtered (spaces, "nfn_fields_total %d\n",
1273 TYPE_NFN_FIELDS_TOTAL (type));
1274 if (TYPE_N_BASECLASSES (type) > 0)
1276 printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
1277 TYPE_N_BASECLASSES (type));
1278 gdb_print_address (TYPE_FIELD_VIRTUAL_BITS (type), gdb_stdout);
1279 printf_filtered (")");
1281 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
1282 TYPE_N_BASECLASSES (type));
1283 puts_filtered ("\n");
1285 if (TYPE_NFIELDS (type) > 0)
1287 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
1289 printfi_filtered (spaces, "private_field_bits (%d bits at *",
1290 TYPE_NFIELDS (type));
1291 gdb_print_address (TYPE_FIELD_PRIVATE_BITS (type), gdb_stdout);
1292 printf_filtered (")");
1293 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
1294 TYPE_NFIELDS (type));
1295 puts_filtered ("\n");
1297 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
1299 printfi_filtered (spaces, "protected_field_bits (%d bits at *",
1300 TYPE_NFIELDS (type));
1301 gdb_print_address (TYPE_FIELD_PROTECTED_BITS (type), gdb_stdout);
1302 printf_filtered (")");
1303 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
1304 TYPE_NFIELDS (type));
1305 puts_filtered ("\n");
1308 if (TYPE_NFN_FIELDS (type) > 0)
1310 dump_fn_fieldlists (type, spaces);
1315 recursive_dump_type (type, spaces)
1321 printfi_filtered (spaces, "type node ");
1322 gdb_print_address (type, gdb_stdout);
1323 printf_filtered ("\n");
1324 printfi_filtered (spaces, "name '%s' (",
1325 TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
1326 gdb_print_address (TYPE_NAME (type), gdb_stdout);
1327 printf_filtered (")\n");
1328 if (TYPE_TAG_NAME (type) != NULL)
1330 printfi_filtered (spaces, "tagname '%s' (",
1331 TYPE_TAG_NAME (type));
1332 gdb_print_address (TYPE_TAG_NAME (type), gdb_stdout);
1333 printf_filtered (")\n");
1335 printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
1336 switch (TYPE_CODE (type))
1338 case TYPE_CODE_UNDEF:
1339 printf_filtered ("(TYPE_CODE_UNDEF)");
1342 printf_filtered ("(TYPE_CODE_PTR)");
1344 case TYPE_CODE_ARRAY:
1345 printf_filtered ("(TYPE_CODE_ARRAY)");
1347 case TYPE_CODE_STRUCT:
1348 printf_filtered ("(TYPE_CODE_STRUCT)");
1350 case TYPE_CODE_UNION:
1351 printf_filtered ("(TYPE_CODE_UNION)");
1353 case TYPE_CODE_ENUM:
1354 printf_filtered ("(TYPE_CODE_ENUM)");
1356 case TYPE_CODE_FUNC:
1357 printf_filtered ("(TYPE_CODE_FUNC)");
1360 printf_filtered ("(TYPE_CODE_INT)");
1363 printf_filtered ("(TYPE_CODE_FLT)");
1365 case TYPE_CODE_VOID:
1366 printf_filtered ("(TYPE_CODE_VOID)");
1369 printf_filtered ("(TYPE_CODE_SET)");
1371 case TYPE_CODE_RANGE:
1372 printf_filtered ("(TYPE_CODE_RANGE)");
1374 case TYPE_CODE_STRING:
1375 printf_filtered ("(TYPE_CODE_STRING)");
1377 case TYPE_CODE_ERROR:
1378 printf_filtered ("(TYPE_CODE_ERROR)");
1380 case TYPE_CODE_MEMBER:
1381 printf_filtered ("(TYPE_CODE_MEMBER)");
1383 case TYPE_CODE_METHOD:
1384 printf_filtered ("(TYPE_CODE_METHOD)");
1387 printf_filtered ("(TYPE_CODE_REF)");
1389 case TYPE_CODE_CHAR:
1390 printf_filtered ("(TYPE_CODE_CHAR)");
1392 case TYPE_CODE_BOOL:
1393 printf_filtered ("(TYPE_CODE_BOOL)");
1396 printf_filtered ("(UNKNOWN TYPE CODE)");
1399 puts_filtered ("\n");
1400 printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
1401 printfi_filtered (spaces, "objfile ");
1402 gdb_print_address (TYPE_OBJFILE (type), gdb_stdout);
1403 printf_filtered ("\n");
1404 printfi_filtered (spaces, "target_type ");
1405 gdb_print_address (TYPE_TARGET_TYPE (type), gdb_stdout);
1406 printf_filtered ("\n");
1407 if (TYPE_TARGET_TYPE (type) != NULL)
1409 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
1411 printfi_filtered (spaces, "pointer_type ");
1412 gdb_print_address (TYPE_POINTER_TYPE (type), gdb_stdout);
1413 printf_filtered ("\n");
1414 printfi_filtered (spaces, "reference_type ");
1415 gdb_print_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
1416 printf_filtered ("\n");
1417 printfi_filtered (spaces, "function_type ");
1418 gdb_print_address (TYPE_FUNCTION_TYPE (type), gdb_stdout);
1419 printf_filtered ("\n");
1420 printfi_filtered (spaces, "flags 0x%x", TYPE_FLAGS (type));
1421 if (TYPE_FLAGS (type) & TYPE_FLAG_UNSIGNED)
1423 puts_filtered (" TYPE_FLAG_UNSIGNED");
1425 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
1427 puts_filtered (" TYPE_FLAG_STUB");
1429 puts_filtered ("\n");
1430 printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
1431 gdb_print_address (TYPE_FIELDS (type), gdb_stdout);
1432 puts_filtered ("\n");
1433 for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
1435 printfi_filtered (spaces + 2,
1436 "[%d] bitpos %d bitsize %d type ",
1437 idx, TYPE_FIELD_BITPOS (type, idx),
1438 TYPE_FIELD_BITSIZE (type, idx));
1439 gdb_print_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
1440 printf_filtered (" name '%s' (",
1441 TYPE_FIELD_NAME (type, idx) != NULL
1442 ? TYPE_FIELD_NAME (type, idx)
1444 gdb_print_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
1445 printf_filtered (")\n");
1446 if (TYPE_FIELD_TYPE (type, idx) != NULL)
1448 recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
1451 printfi_filtered (spaces, "vptr_basetype ");
1452 gdb_print_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
1453 puts_filtered ("\n");
1454 if (TYPE_VPTR_BASETYPE (type) != NULL)
1456 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
1458 printfi_filtered (spaces, "vptr_fieldno %d\n", TYPE_VPTR_FIELDNO (type));
1459 switch (TYPE_CODE (type))
1461 case TYPE_CODE_METHOD:
1462 case TYPE_CODE_FUNC:
1463 printfi_filtered (spaces, "arg_types ");
1464 gdb_print_address (TYPE_ARG_TYPES (type), gdb_stdout);
1465 puts_filtered ("\n");
1466 print_arg_types (TYPE_ARG_TYPES (type), spaces);
1469 case TYPE_CODE_STRUCT:
1470 printfi_filtered (spaces, "cplus_stuff ");
1471 gdb_print_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
1472 puts_filtered ("\n");
1473 print_cplus_stuff (type, spaces);
1477 /* We have to pick one of the union types to be able print and test
1478 the value. Pick cplus_struct_type, even though we know it isn't
1479 any particular one. */
1480 printfi_filtered (spaces, "type_specific ");
1481 gdb_print_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
1482 if (TYPE_CPLUS_SPECIFIC (type) != NULL)
1484 printf_filtered (" (unknown data form)");
1486 printf_filtered ("\n");
1492 #endif /* MAINTENANCE_CMDS */
1495 _initialize_gdbtypes ()
1498 init_type (TYPE_CODE_VOID, 1,
1500 "void", (struct objfile *) NULL);
1502 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1504 "char", (struct objfile *) NULL);
1505 builtin_type_signed_char =
1506 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1508 "signed char", (struct objfile *) NULL);
1509 builtin_type_unsigned_char =
1510 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1512 "unsigned char", (struct objfile *) NULL);
1513 builtin_type_short =
1514 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1516 "short", (struct objfile *) NULL);
1517 builtin_type_unsigned_short =
1518 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1520 "unsigned short", (struct objfile *) NULL);
1522 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1524 "int", (struct objfile *) NULL);
1525 builtin_type_unsigned_int =
1526 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1528 "unsigned int", (struct objfile *) NULL);
1530 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1532 "long", (struct objfile *) NULL);
1533 builtin_type_unsigned_long =
1534 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1536 "unsigned long", (struct objfile *) NULL);
1537 builtin_type_long_long =
1538 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1540 "long long", (struct objfile *) NULL);
1541 builtin_type_unsigned_long_long =
1542 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1544 "unsigned long long", (struct objfile *) NULL);
1545 builtin_type_float =
1546 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1548 "float", (struct objfile *) NULL);
1549 builtin_type_double =
1550 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1552 "double", (struct objfile *) NULL);
1553 builtin_type_long_double =
1554 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1556 "long double", (struct objfile *) NULL);
1557 builtin_type_complex =
1558 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1560 "complex", (struct objfile *) NULL);
1561 builtin_type_double_complex =
1562 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1564 "double complex", (struct objfile *) NULL);
1565 builtin_type_string =
1566 init_type (TYPE_CODE_STRING, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1568 "string", (struct objfile *) NULL);