* defs.h (STRCMP, STREQ, STREQN): New macros.
[binutils-gdb.git] / gdb / gdbtypes.c
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.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include <string.h>
23 #include "bfd.h"
24 #include "symtab.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "gdbtypes.h"
28 #include "expression.h"
29 #include "language.h"
30 #include "target.h"
31 #include "value.h"
32 #include "demangle.h"
33 #include "complaints.h"
34
35 /* Alloc a new type structure and fill it with some defaults. If
36 OBJFILE is non-NULL, then allocate the space for the type structure
37 in that objfile's type_obstack. */
38
39 struct type *
40 alloc_type (objfile)
41 struct objfile *objfile;
42 {
43 register struct type *type;
44
45 /* Alloc the structure and start off with all fields zeroed. */
46
47 if (objfile == NULL)
48 {
49 type = (struct type *) xmalloc (sizeof (struct type));
50 }
51 else
52 {
53 type = (struct type *) obstack_alloc (&objfile -> type_obstack,
54 sizeof (struct type));
55 }
56 memset ((char *) type, 0, sizeof (struct type));
57
58 /* Initialize the fields that might not be zero. */
59
60 TYPE_CODE (type) = TYPE_CODE_UNDEF;
61 TYPE_OBJFILE (type) = objfile;
62 TYPE_VPTR_FIELDNO (type) = -1;
63
64 return (type);
65 }
66
67 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
68 to a pointer to memory where the pointer type should be stored.
69 If *TYPEPTR is zero, update it to point to the pointer type we return.
70 We allocate new memory if needed. */
71
72 struct type *
73 make_pointer_type (type, typeptr)
74 struct type *type;
75 struct type **typeptr;
76 {
77 register struct type *ntype; /* New type */
78 struct objfile *objfile;
79
80 ntype = TYPE_POINTER_TYPE (type);
81
82 if (ntype)
83 if (typeptr == 0)
84 return ntype; /* Don't care about alloc, and have new type. */
85 else if (*typeptr == 0)
86 {
87 *typeptr = ntype; /* Tracking alloc, and we have new type. */
88 return ntype;
89 }
90
91 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
92 {
93 ntype = alloc_type (TYPE_OBJFILE (type));
94 if (typeptr)
95 *typeptr = ntype;
96 }
97 else /* We have storage, but need to reset it. */
98 {
99 ntype = *typeptr;
100 objfile = TYPE_OBJFILE (ntype);
101 memset ((char *) ntype, 0, sizeof (struct type));
102 TYPE_OBJFILE (ntype) = objfile;
103 }
104
105 TYPE_TARGET_TYPE (ntype) = type;
106 TYPE_POINTER_TYPE (type) = ntype;
107
108 /* FIXME! Assume the machine has only one representation for pointers! */
109
110 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
111 TYPE_CODE (ntype) = TYPE_CODE_PTR;
112
113 /* pointers are unsigned */
114 TYPE_FLAGS (ntype) |= TYPE_FLAG_UNSIGNED;
115
116 if (!TYPE_POINTER_TYPE (type)) /* Remember it, if don't have one. */
117 TYPE_POINTER_TYPE (type) = ntype;
118
119 return ntype;
120 }
121
122 /* Given a type TYPE, return a type of pointers to that type.
123 May need to construct such a type if this is the first use. */
124
125 struct type *
126 lookup_pointer_type (type)
127 struct type *type;
128 {
129 return make_pointer_type (type, (struct type **)0);
130 }
131
132 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero, points
133 to a pointer to memory where the reference type should be stored.
134 If *TYPEPTR is zero, update it to point to the reference type we return.
135 We allocate new memory if needed. */
136
137 struct type *
138 make_reference_type (type, typeptr)
139 struct type *type;
140 struct type **typeptr;
141 {
142 register struct type *ntype; /* New type */
143 struct objfile *objfile;
144
145 ntype = TYPE_REFERENCE_TYPE (type);
146
147 if (ntype)
148 if (typeptr == 0)
149 return ntype; /* Don't care about alloc, and have new type. */
150 else if (*typeptr == 0)
151 {
152 *typeptr = ntype; /* Tracking alloc, and we have new type. */
153 return ntype;
154 }
155
156 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
157 {
158 ntype = alloc_type (TYPE_OBJFILE (type));
159 if (typeptr)
160 *typeptr = ntype;
161 }
162 else /* We have storage, but need to reset it. */
163 {
164 ntype = *typeptr;
165 objfile = TYPE_OBJFILE (ntype);
166 memset ((char *) ntype, 0, sizeof (struct type));
167 TYPE_OBJFILE (ntype) = objfile;
168 }
169
170 TYPE_TARGET_TYPE (ntype) = type;
171 TYPE_REFERENCE_TYPE (type) = ntype;
172
173 /* FIXME! Assume the machine has only one representation for references,
174 and that it matches the (only) representation for pointers! */
175
176 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
177 TYPE_CODE (ntype) = TYPE_CODE_REF;
178
179 if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */
180 TYPE_REFERENCE_TYPE (type) = ntype;
181
182 return ntype;
183 }
184
185 /* Same as above, but caller doesn't care about memory allocation details. */
186
187 struct type *
188 lookup_reference_type (type)
189 struct type *type;
190 {
191 return make_reference_type (type, (struct type **)0);
192 }
193
194 /* Lookup a function type that returns type TYPE. TYPEPTR, if nonzero, points
195 to a pointer to memory where the function type should be stored.
196 If *TYPEPTR is zero, update it to point to the function type we return.
197 We allocate new memory if needed. */
198
199 struct type *
200 make_function_type (type, typeptr)
201 struct type *type;
202 struct type **typeptr;
203 {
204 register struct type *ntype; /* New type */
205 struct objfile *objfile;
206
207 ntype = TYPE_FUNCTION_TYPE (type);
208
209 if (ntype)
210 if (typeptr == 0)
211 return ntype; /* Don't care about alloc, and have new type. */
212 else if (*typeptr == 0)
213 {
214 *typeptr = ntype; /* Tracking alloc, and we have new type. */
215 return ntype;
216 }
217
218 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
219 {
220 ntype = alloc_type (TYPE_OBJFILE (type));
221 if (typeptr)
222 *typeptr = ntype;
223 }
224 else /* We have storage, but need to reset it. */
225 {
226 ntype = *typeptr;
227 objfile = TYPE_OBJFILE (ntype);
228 memset ((char *) ntype, 0, sizeof (struct type));
229 TYPE_OBJFILE (ntype) = objfile;
230 }
231
232 TYPE_TARGET_TYPE (ntype) = type;
233 TYPE_FUNCTION_TYPE (type) = ntype;
234
235 TYPE_LENGTH (ntype) = 1;
236 TYPE_CODE (ntype) = TYPE_CODE_FUNC;
237
238 if (!TYPE_FUNCTION_TYPE (type)) /* Remember it, if don't have one. */
239 TYPE_FUNCTION_TYPE (type) = ntype;
240
241 return ntype;
242 }
243
244
245 /* Given a type TYPE, return a type of functions that return that type.
246 May need to construct such a type if this is the first use. */
247
248 struct type *
249 lookup_function_type (type)
250 struct type *type;
251 {
252 return make_function_type (type, (struct type **)0);
253 }
254
255 /* Implement direct support for MEMBER_TYPE in GNU C++.
256 May need to construct such a type if this is the first use.
257 The TYPE is the type of the member. The DOMAIN is the type
258 of the aggregate that the member belongs to. */
259
260 struct type *
261 lookup_member_type (type, domain)
262 struct type *type;
263 struct type *domain;
264 {
265 register struct type *mtype;
266
267 mtype = alloc_type (TYPE_OBJFILE (type));
268 smash_to_member_type (mtype, domain, type);
269 return (mtype);
270 }
271
272 /* Allocate a stub method whose return type is TYPE.
273 This apparently happens for speed of symbol reading, since parsing
274 out the arguments to the method is cpu-intensive, the way we are doing
275 it. So, we will fill in arguments later.
276 This always returns a fresh type. */
277
278 struct type *
279 allocate_stub_method (type)
280 struct type *type;
281 {
282 struct type *mtype;
283
284 mtype = alloc_type (TYPE_OBJFILE (type));
285 TYPE_TARGET_TYPE (mtype) = type;
286 /* _DOMAIN_TYPE (mtype) = unknown yet */
287 /* _ARG_TYPES (mtype) = unknown yet */
288 TYPE_FLAGS (mtype) = TYPE_FLAG_STUB;
289 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
290 TYPE_LENGTH (mtype) = 1;
291 return (mtype);
292 }
293
294 /* Create a range type using either a blank type supplied in RESULT_TYPE,
295 or creating a new type. Indices will be of type INDEX_TYPE, and will
296 range from LOW_BOUND to HIGH_BOUND, inclusive.
297
298 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
299 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
300
301 struct type *
302 create_range_type (result_type, index_type, low_bound, high_bound)
303 struct type *result_type;
304 struct type *index_type;
305 int low_bound;
306 int high_bound;
307 {
308 if (result_type == NULL)
309 {
310 result_type = alloc_type (TYPE_OBJFILE (index_type));
311 }
312 TYPE_CODE (result_type) = TYPE_CODE_RANGE;
313 TYPE_TARGET_TYPE (result_type) = index_type;
314 TYPE_LENGTH (result_type) = TYPE_LENGTH (index_type);
315 TYPE_NFIELDS (result_type) = 2;
316 TYPE_FIELDS (result_type) = (struct field *)
317 TYPE_ALLOC (result_type, 2 * sizeof (struct field));
318 memset (TYPE_FIELDS (result_type), 0, 2 * sizeof (struct field));
319 TYPE_FIELD_BITPOS (result_type, 0) = low_bound;
320 TYPE_FIELD_BITPOS (result_type, 1) = high_bound;
321 TYPE_FIELD_TYPE (result_type, 0) = builtin_type_int; /* FIXME */
322 TYPE_FIELD_TYPE (result_type, 1) = builtin_type_int; /* FIXME */
323
324 return (result_type);
325 }
326
327
328 /* Create an array type using either a blank type supplied in RESULT_TYPE,
329 or creating a new type. Elements will be of type ELEMENT_TYPE, the
330 indices will be of type RANGE_TYPE.
331
332 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
333 sure it is TYPE_CODE_UNDEF before we bash it into an array type? */
334
335 struct type *
336 create_array_type (result_type, element_type, range_type)
337 struct type *result_type;
338 struct type *element_type;
339 struct type *range_type;
340 {
341 int low_bound;
342 int high_bound;
343
344 if (TYPE_CODE (range_type) != TYPE_CODE_RANGE)
345 {
346 /* FIXME: We only handle range types at the moment. Complain and
347 create a dummy range type to use. */
348 warning ("internal error: array index type must be a range type");
349 range_type = lookup_fundamental_type (TYPE_OBJFILE (range_type),
350 FT_INTEGER);
351 range_type = create_range_type ((struct type *) NULL, range_type, 0, 0);
352 }
353 if (result_type == NULL)
354 {
355 result_type = alloc_type (TYPE_OBJFILE (element_type));
356 }
357 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
358 TYPE_TARGET_TYPE (result_type) = element_type;
359 low_bound = TYPE_FIELD_BITPOS (range_type, 0);
360 high_bound = TYPE_FIELD_BITPOS (range_type, 1);
361 TYPE_LENGTH (result_type) =
362 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
363 TYPE_NFIELDS (result_type) = 1;
364 TYPE_FIELDS (result_type) =
365 (struct field *) TYPE_ALLOC (result_type, sizeof (struct field));
366 memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
367 TYPE_FIELD_TYPE (result_type, 0) = range_type;
368 TYPE_VPTR_FIELDNO (result_type) = -1;
369
370 return (result_type);
371 }
372
373
374 /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.
375 A MEMBER is a wierd thing -- it amounts to a typed offset into
376 a struct, e.g. "an int at offset 8". A MEMBER TYPE doesn't
377 include the offset (that's the value of the MEMBER itself), but does
378 include the structure type into which it points (for some reason).
379
380 When "smashing" the type, we preserve the objfile that the
381 old type pointed to, since we aren't changing where the type is actually
382 allocated. */
383
384 void
385 smash_to_member_type (type, domain, to_type)
386 struct type *type;
387 struct type *domain;
388 struct type *to_type;
389 {
390 struct objfile *objfile;
391
392 objfile = TYPE_OBJFILE (type);
393
394 memset ((char *) type, 0, sizeof (struct type));
395 TYPE_OBJFILE (type) = objfile;
396 TYPE_TARGET_TYPE (type) = to_type;
397 TYPE_DOMAIN_TYPE (type) = domain;
398 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
399 TYPE_CODE (type) = TYPE_CODE_MEMBER;
400 }
401
402 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
403 METHOD just means `function that gets an extra "this" argument'.
404
405 When "smashing" the type, we preserve the objfile that the
406 old type pointed to, since we aren't changing where the type is actually
407 allocated. */
408
409 void
410 smash_to_method_type (type, domain, to_type, args)
411 struct type *type;
412 struct type *domain;
413 struct type *to_type;
414 struct type **args;
415 {
416 struct objfile *objfile;
417
418 objfile = TYPE_OBJFILE (type);
419
420 memset ((char *) type, 0, sizeof (struct type));
421 TYPE_OBJFILE (type) = objfile;
422 TYPE_TARGET_TYPE (type) = to_type;
423 TYPE_DOMAIN_TYPE (type) = domain;
424 TYPE_ARG_TYPES (type) = args;
425 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
426 TYPE_CODE (type) = TYPE_CODE_METHOD;
427 }
428
429 /* Return a typename for a struct/union/enum type
430 without the tag qualifier. If the type has a NULL name,
431 NULL is returned. */
432
433 char *
434 type_name_no_tag (type)
435 register const struct type *type;
436 {
437 register char *name;
438
439 if ((name = TYPE_NAME (type)) != NULL)
440 {
441 switch (TYPE_CODE (type))
442 {
443 case TYPE_CODE_STRUCT:
444 if(!strncmp (name, "struct ", 7))
445 {
446 name += 7;
447 }
448 break;
449 case TYPE_CODE_UNION:
450 if(!strncmp (name, "union ", 6))
451 {
452 name += 6;
453 }
454 break;
455 case TYPE_CODE_ENUM:
456 if(!strncmp (name, "enum ", 5))
457 {
458 name += 5;
459 }
460 break;
461 default: /* To avoid -Wall warnings */
462 break;
463 }
464 }
465 return (name);
466 }
467
468 /* Lookup a primitive type named NAME.
469 Return zero if NAME is not a primitive type.*/
470
471 struct type *
472 lookup_primitive_typename (name)
473 char *name;
474 {
475 struct type ** const *p;
476
477 for (p = current_language -> la_builtin_type_vector; *p != NULL; p++)
478 {
479 if (STREQ ((**p) -> name, name))
480 {
481 return (**p);
482 }
483 }
484 return (NULL);
485 }
486
487 /* Lookup a typedef or primitive type named NAME,
488 visible in lexical block BLOCK.
489 If NOERR is nonzero, return zero if NAME is not suitably defined. */
490
491 struct type *
492 lookup_typename (name, block, noerr)
493 char *name;
494 struct block *block;
495 int noerr;
496 {
497 register struct symbol *sym;
498 register struct type *tmp;
499
500 sym = lookup_symbol (name, block, VAR_NAMESPACE, 0, (struct symtab **) NULL);
501 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
502 {
503 tmp = lookup_primitive_typename (name);
504 if (tmp)
505 {
506 return (tmp);
507 }
508 else if (!tmp && noerr)
509 {
510 return (NULL);
511 }
512 else
513 {
514 error ("No type named %s.", name);
515 }
516 }
517 return (SYMBOL_TYPE (sym));
518 }
519
520 struct type *
521 lookup_unsigned_typename (name)
522 char *name;
523 {
524 char *uns = alloca (strlen (name) + 10);
525
526 strcpy (uns, "unsigned ");
527 strcpy (uns + 9, name);
528 return (lookup_typename (uns, (struct block *) NULL, 0));
529 }
530
531 struct type *
532 lookup_signed_typename (name)
533 char *name;
534 {
535 struct type *t;
536 char *uns = alloca (strlen (name) + 8);
537
538 strcpy (uns, "signed ");
539 strcpy (uns + 7, name);
540 t = lookup_typename (uns, (struct block *) NULL, 1);
541 /* If we don't find "signed FOO" just try again with plain "FOO". */
542 if (t != NULL)
543 return t;
544 return lookup_typename (name, (struct block *) NULL, 0);
545 }
546
547 /* Lookup a structure type named "struct NAME",
548 visible in lexical block BLOCK. */
549
550 struct type *
551 lookup_struct (name, block)
552 char *name;
553 struct block *block;
554 {
555 register struct symbol *sym;
556
557 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
558 (struct symtab **) NULL);
559
560 if (sym == NULL)
561 {
562 error ("No struct type named %s.", name);
563 }
564 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
565 {
566 error ("This context has class, union or enum %s, not a struct.", name);
567 }
568 return (SYMBOL_TYPE (sym));
569 }
570
571 /* Lookup a union type named "union NAME",
572 visible in lexical block BLOCK. */
573
574 struct type *
575 lookup_union (name, block)
576 char *name;
577 struct block *block;
578 {
579 register struct symbol *sym;
580
581 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
582 (struct symtab **) NULL);
583
584 if (sym == NULL)
585 {
586 error ("No union type named %s.", name);
587 }
588 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
589 {
590 error ("This context has class, struct or enum %s, not a union.", name);
591 }
592 return (SYMBOL_TYPE (sym));
593 }
594
595 /* Lookup an enum type named "enum NAME",
596 visible in lexical block BLOCK. */
597
598 struct type *
599 lookup_enum (name, block)
600 char *name;
601 struct block *block;
602 {
603 register struct symbol *sym;
604
605 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
606 (struct symtab **) NULL);
607 if (sym == NULL)
608 {
609 error ("No enum type named %s.", name);
610 }
611 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
612 {
613 error ("This context has class, struct or union %s, not an enum.", name);
614 }
615 return (SYMBOL_TYPE (sym));
616 }
617
618 /* Lookup a template type named "template NAME<TYPE>",
619 visible in lexical block BLOCK. */
620
621 struct type *
622 lookup_template_type (name, type, block)
623 char *name;
624 struct type *type;
625 struct block *block;
626 {
627 struct symbol *sym;
628 char *nam = (char*) alloca(strlen(name) + strlen(type->name) + 4);
629 strcpy (nam, name);
630 strcat (nam, "<");
631 strcat (nam, type->name);
632 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
633
634 sym = lookup_symbol (nam, block, VAR_NAMESPACE, 0, (struct symtab **)NULL);
635
636 if (sym == NULL)
637 {
638 error ("No template type named %s.", name);
639 }
640 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
641 {
642 error ("This context has class, union or enum %s, not a struct.", name);
643 }
644 return (SYMBOL_TYPE (sym));
645 }
646
647 /* Given a type TYPE, lookup the type of the component of type named
648 NAME.
649 If NOERR is nonzero, return zero if NAME is not suitably defined. */
650
651 struct type *
652 lookup_struct_elt_type (type, name, noerr)
653 struct type *type;
654 char *name;
655 int noerr;
656 {
657 int i;
658
659 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
660 TYPE_CODE (type) == TYPE_CODE_REF)
661 type = TYPE_TARGET_TYPE (type);
662
663 if (TYPE_CODE (type) != TYPE_CODE_STRUCT &&
664 TYPE_CODE (type) != TYPE_CODE_UNION)
665 {
666 target_terminal_ours ();
667 fflush (stdout);
668 fprintf (stderr, "Type ");
669 type_print (type, "", stderr, -1);
670 error (" is not a structure or union type.");
671 }
672
673 check_stub_type (type);
674
675 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
676 {
677 char *t_field_name = TYPE_FIELD_NAME (type, i);
678
679 if (t_field_name && STREQ (t_field_name, name))
680 {
681 return TYPE_FIELD_TYPE (type, i);
682 }
683 }
684
685 /* OK, it's not in this class. Recursively check the baseclasses. */
686 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
687 {
688 struct type *t;
689
690 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 0);
691 if (t != NULL)
692 {
693 return t;
694 }
695 }
696
697 if (noerr)
698 {
699 return NULL;
700 }
701
702 target_terminal_ours ();
703 fflush (stdout);
704 fprintf (stderr, "Type ");
705 type_print (type, "", stderr, -1);
706 fprintf (stderr, " has no component named ");
707 fputs_filtered (name, stderr);
708 error (".");
709 return (struct type *)-1; /* For lint */
710 }
711
712 /* This function is really horrible, but to avoid it, there would need
713 to be more filling in of forward references. */
714
715 void
716 fill_in_vptr_fieldno (type)
717 struct type *type;
718 {
719 if (TYPE_VPTR_FIELDNO (type) < 0)
720 {
721 int i;
722 for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
723 {
724 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
725 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
726 {
727 TYPE_VPTR_FIELDNO (type)
728 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
729 TYPE_VPTR_BASETYPE (type)
730 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
731 break;
732 }
733 }
734 }
735 }
736
737 /* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
738
739 If this is a stubbed struct (i.e. declared as struct foo *), see if
740 we can find a full definition in some other file. If so, copy this
741 definition, so we can use it in future. If not, set a flag so we
742 don't waste too much time in future. (FIXME, this doesn't seem
743 to be happening...)
744
745 This used to be coded as a macro, but I don't think it is called
746 often enough to merit such treatment.
747 */
748
749 struct complaint stub_noname_complaint =
750 {"stub type has NULL name", 0, 0};
751
752 void
753 check_stub_type (type)
754 struct type *type;
755 {
756 if (TYPE_FLAGS(type) & TYPE_FLAG_STUB)
757 {
758 char* name = type_name_no_tag (type);
759 struct symbol *sym;
760 if (name == NULL)
761 {
762 complain (&stub_noname_complaint);
763 return;
764 }
765 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
766 (struct symtab **) NULL);
767 if (sym)
768 {
769 memcpy ((char *)type, (char *)SYMBOL_TYPE(sym), sizeof (struct type));
770 }
771 }
772 }
773
774 /* Ugly hack to convert method stubs into method types.
775
776 He ain't kiddin'. This demangles the name of the method into a string
777 including argument types, parses out each argument type, generates
778 a string casting a zero to that type, evaluates the string, and stuffs
779 the resulting type into an argtype vector!!! Then it knows the type
780 of the whole function (including argument types for overloading),
781 which info used to be in the stab's but was removed to hack back
782 the space required for them. */
783
784 void
785 check_stub_method (type, i, j)
786 struct type *type;
787 int i;
788 int j;
789 {
790 struct fn_field *f;
791 char *mangled_name = gdb_mangle_name (type, i, j);
792 char *demangled_name = cplus_demangle (mangled_name,
793 DMGL_PARAMS | DMGL_ANSI);
794 char *argtypetext, *p;
795 int depth = 0, argcount = 1;
796 struct type **argtypes;
797 struct type *mtype;
798
799 if (demangled_name == NULL)
800 {
801 error ("Internal: Cannot demangle mangled name `%s'.", mangled_name);
802 }
803
804 /* Now, read in the parameters that define this type. */
805 argtypetext = strchr (demangled_name, '(') + 1;
806 p = argtypetext;
807 while (*p)
808 {
809 if (*p == '(')
810 {
811 depth += 1;
812 }
813 else if (*p == ')')
814 {
815 depth -= 1;
816 }
817 else if (*p == ',' && depth == 0)
818 {
819 argcount += 1;
820 }
821
822 p += 1;
823 }
824
825 /* We need two more slots: one for the THIS pointer, and one for the
826 NULL [...] or void [end of arglist]. */
827
828 argtypes = (struct type **)
829 TYPE_ALLOC (type, (argcount + 2) * sizeof (struct type *));
830 p = argtypetext;
831 argtypes[0] = lookup_pointer_type (type);
832 argcount = 1;
833
834 if (*p != ')') /* () means no args, skip while */
835 {
836 depth = 0;
837 while (*p)
838 {
839 if (depth <= 0 && (*p == ',' || *p == ')'))
840 {
841 argtypes[argcount] =
842 parse_and_eval_type (argtypetext, p - argtypetext);
843 argcount += 1;
844 argtypetext = p + 1;
845 }
846
847 if (*p == '(')
848 {
849 depth += 1;
850 }
851 else if (*p == ')')
852 {
853 depth -= 1;
854 }
855
856 p += 1;
857 }
858 }
859
860 if (p[-2] != '.') /* Not '...' */
861 {
862 argtypes[argcount] = builtin_type_void; /* List terminator */
863 }
864 else
865 {
866 argtypes[argcount] = NULL; /* Ellist terminator */
867 }
868
869 free (demangled_name);
870
871 f = TYPE_FN_FIELDLIST1 (type, i);
872 TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
873
874 /* Now update the old "stub" type into a real type. */
875 mtype = TYPE_FN_FIELD_TYPE (f, j);
876 TYPE_DOMAIN_TYPE (mtype) = type;
877 TYPE_ARG_TYPES (mtype) = argtypes;
878 TYPE_FLAGS (mtype) &= ~TYPE_FLAG_STUB;
879 TYPE_FN_FIELD_STUB (f, j) = 0;
880 }
881
882 struct cplus_struct_type cplus_struct_default;
883
884 void
885 allocate_cplus_struct_type (type)
886 struct type *type;
887 {
888 if (!HAVE_CPLUS_STRUCT (type))
889 {
890 TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
891 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
892 *(TYPE_CPLUS_SPECIFIC(type)) = cplus_struct_default;
893 }
894 }
895
896 /* Helper function to initialize the standard scalar types.
897
898 If NAME is non-NULL and OBJFILE is non-NULL, then we make a copy
899 of the string pointed to by name in the type_obstack for that objfile,
900 and initialize the type name to that copy. There are places (mipsread.c
901 in particular, where init_type is called with a NULL value for NAME). */
902
903 struct type *
904 init_type (code, length, flags, name, objfile)
905 enum type_code code;
906 int length;
907 int flags;
908 char *name;
909 struct objfile *objfile;
910 {
911 register struct type *type;
912
913 type = alloc_type (objfile);
914 TYPE_CODE (type) = code;
915 TYPE_LENGTH (type) = length;
916 TYPE_FLAGS (type) |= flags;
917 if ((name != NULL) && (objfile != NULL))
918 {
919 TYPE_NAME (type) =
920 obsavestring (name, strlen (name), &objfile -> type_obstack);
921 }
922 else
923 {
924 TYPE_NAME (type) = name;
925 }
926
927 /* C++ fancies. */
928
929 if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
930 {
931 INIT_CPLUS_SPECIFIC (type);
932 }
933 return (type);
934 }
935
936 /* Look up a fundamental type for the specified objfile.
937 May need to construct such a type if this is the first use.
938
939 Some object file formats (ELF, COFF, etc) do not define fundamental
940 types such as "int" or "double". Others (stabs for example), do
941 define fundamental types.
942
943 For the formats which don't provide fundamental types, gdb can create
944 such types, using defaults reasonable for the current language and
945 the current target machine.
946
947 NOTE: This routine is obsolescent. Each debugging format reader
948 should manage it's own fundamental types, either creating them from
949 suitable defaults or reading them from the debugging information,
950 whichever is appropriate. The DWARF reader has already been
951 fixed to do this. Once the other readers are fixed, this routine
952 will go away. Also note that fundamental types should be managed
953 on a compilation unit basis in a multi-language environment, not
954 on a linkage unit basis as is done here. */
955
956
957 struct type *
958 lookup_fundamental_type (objfile, typeid)
959 struct objfile *objfile;
960 int typeid;
961 {
962 register struct type **typep;
963 register int nbytes;
964
965 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
966 {
967 error ("internal error - invalid fundamental type id %d", typeid);
968 }
969
970 /* If this is the first time we need a fundamental type for this objfile
971 then we need to initialize the vector of type pointers. */
972
973 if (objfile -> fundamental_types == NULL)
974 {
975 nbytes = FT_NUM_MEMBERS * sizeof (struct type *);
976 objfile -> fundamental_types = (struct type **)
977 obstack_alloc (&objfile -> type_obstack, nbytes);
978 memset ((char *) objfile -> fundamental_types, 0, nbytes);
979 }
980
981 /* Look for this particular type in the fundamental type vector. If one is
982 not found, create and install one appropriate for the current language. */
983
984 typep = objfile -> fundamental_types + typeid;
985 if (*typep == NULL)
986 {
987 *typep = create_fundamental_type (objfile, typeid);
988 }
989
990 return (*typep);
991 }
992
993 #if MAINTENANCE_CMDS
994
995 static void
996 print_bit_vector (bits, nbits)
997 B_TYPE *bits;
998 int nbits;
999 {
1000 int bitno;
1001
1002 for (bitno = 0; bitno < nbits; bitno++)
1003 {
1004 if ((bitno % 8) == 0)
1005 {
1006 puts_filtered (" ");
1007 }
1008 if (B_TST (bits, bitno))
1009 {
1010 printf_filtered ("1");
1011 }
1012 else
1013 {
1014 printf_filtered ("0");
1015 }
1016 }
1017 }
1018
1019 /* The args list is a strange beast. It is either terminated by a NULL
1020 pointer for varargs functions, or by a pointer to a TYPE_CODE_VOID
1021 type for normal fixed argcount functions. (FIXME someday)
1022 Also note the first arg should be the "this" pointer, we may not want to
1023 include it since we may get into a infinitely recursive situation. */
1024
1025 static void
1026 print_arg_types (args, spaces)
1027 struct type **args;
1028 int spaces;
1029 {
1030 if (args != NULL)
1031 {
1032 while (*args != NULL)
1033 {
1034 recursive_dump_type (*args, spaces + 2);
1035 if ((*args++) -> code == TYPE_CODE_VOID)
1036 {
1037 break;
1038 }
1039 }
1040 }
1041 }
1042
1043 static void
1044 dump_fn_fieldlists (type, spaces)
1045 struct type *type;
1046 int spaces;
1047 {
1048 int method_idx;
1049 int overload_idx;
1050 struct fn_field *f;
1051
1052 printfi_filtered (spaces, "fn_fieldlists 0x%x\n",
1053 TYPE_FN_FIELDLISTS (type));
1054 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
1055 {
1056 f = TYPE_FN_FIELDLIST1 (type, method_idx);
1057 printfi_filtered (spaces + 2, "[%d] name '%s' (0x%x) length %d\n",
1058 method_idx,
1059 TYPE_FN_FIELDLIST_NAME (type, method_idx),
1060 TYPE_FN_FIELDLIST_NAME (type, method_idx),
1061 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
1062 for (overload_idx = 0;
1063 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
1064 overload_idx++)
1065 {
1066 printfi_filtered (spaces + 4, "[%d] physname '%s' (0x%x)\n",
1067 overload_idx,
1068 TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
1069 TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
1070 printfi_filtered (spaces + 8, "type 0x%x\n",
1071 TYPE_FN_FIELD_TYPE (f, overload_idx));
1072 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
1073 spaces + 8 + 2);
1074 printfi_filtered (spaces + 8, "args 0x%x\n",
1075 TYPE_FN_FIELD_ARGS (f, overload_idx));
1076 print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx), spaces);
1077 printfi_filtered (spaces + 8, "fcontext 0x%x\n",
1078 TYPE_FN_FIELD_FCONTEXT (f, overload_idx));
1079 printfi_filtered (spaces + 8, "is_const %d\n",
1080 TYPE_FN_FIELD_CONST (f, overload_idx));
1081 printfi_filtered (spaces + 8, "is_volatile %d\n",
1082 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
1083 printfi_filtered (spaces + 8, "is_private %d\n",
1084 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
1085 printfi_filtered (spaces + 8, "is_protected %d\n",
1086 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
1087 printfi_filtered (spaces + 8, "is_stub %d\n",
1088 TYPE_FN_FIELD_STUB (f, overload_idx));
1089 printfi_filtered (spaces + 8, "voffset %u\n",
1090 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
1091 }
1092 }
1093 }
1094
1095 static void
1096 print_cplus_stuff (type, spaces)
1097 struct type *type;
1098 int spaces;
1099 {
1100 printfi_filtered (spaces, "n_baseclasses %d\n",
1101 TYPE_N_BASECLASSES (type));
1102 printfi_filtered (spaces, "nfn_fields %d\n",
1103 TYPE_NFN_FIELDS (type));
1104 printfi_filtered (spaces, "nfn_fields_total %d\n",
1105 TYPE_NFN_FIELDS_TOTAL (type));
1106 if (TYPE_N_BASECLASSES (type) > 0)
1107 {
1108 printfi_filtered (spaces, "virtual_field_bits (%d bits at *0x%x)",
1109 TYPE_N_BASECLASSES (type),
1110 TYPE_FIELD_VIRTUAL_BITS (type));
1111 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
1112 TYPE_N_BASECLASSES (type));
1113 puts_filtered ("\n");
1114 }
1115 if (TYPE_NFIELDS (type) > 0)
1116 {
1117 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
1118 {
1119 printfi_filtered (spaces, "private_field_bits (%d bits at *0x%x)",
1120 TYPE_NFIELDS (type),
1121 TYPE_FIELD_PRIVATE_BITS (type));
1122 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
1123 TYPE_NFIELDS (type));
1124 puts_filtered ("\n");
1125 }
1126 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
1127 {
1128 printfi_filtered (spaces, "protected_field_bits (%d bits at *0x%x)",
1129 TYPE_NFIELDS (type),
1130 TYPE_FIELD_PROTECTED_BITS (type));
1131 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
1132 TYPE_NFIELDS (type));
1133 puts_filtered ("\n");
1134 }
1135 }
1136 if (TYPE_NFN_FIELDS (type) > 0)
1137 {
1138 dump_fn_fieldlists (type, spaces);
1139 }
1140 }
1141
1142 void
1143 recursive_dump_type (type, spaces)
1144 struct type *type;
1145 int spaces;
1146 {
1147 int idx;
1148
1149 printfi_filtered (spaces, "type node 0x%x\n", type);
1150 printfi_filtered (spaces, "name '%s' (0x%x)\n", TYPE_NAME (type),
1151 TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
1152 printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
1153 switch (TYPE_CODE (type))
1154 {
1155 case TYPE_CODE_UNDEF:
1156 printf_filtered ("(TYPE_CODE_UNDEF)");
1157 break;
1158 case TYPE_CODE_PTR:
1159 printf_filtered ("(TYPE_CODE_PTR)");
1160 break;
1161 case TYPE_CODE_ARRAY:
1162 printf_filtered ("(TYPE_CODE_ARRAY)");
1163 break;
1164 case TYPE_CODE_STRUCT:
1165 printf_filtered ("(TYPE_CODE_STRUCT)");
1166 break;
1167 case TYPE_CODE_UNION:
1168 printf_filtered ("(TYPE_CODE_UNION)");
1169 break;
1170 case TYPE_CODE_ENUM:
1171 printf_filtered ("(TYPE_CODE_ENUM)");
1172 break;
1173 case TYPE_CODE_FUNC:
1174 printf_filtered ("(TYPE_CODE_FUNC)");
1175 break;
1176 case TYPE_CODE_INT:
1177 printf_filtered ("(TYPE_CODE_INT)");
1178 break;
1179 case TYPE_CODE_FLT:
1180 printf_filtered ("(TYPE_CODE_FLT)");
1181 break;
1182 case TYPE_CODE_VOID:
1183 printf_filtered ("(TYPE_CODE_VOID)");
1184 break;
1185 case TYPE_CODE_SET:
1186 printf_filtered ("(TYPE_CODE_SET)");
1187 break;
1188 case TYPE_CODE_RANGE:
1189 printf_filtered ("(TYPE_CODE_RANGE)");
1190 break;
1191 case TYPE_CODE_PASCAL_ARRAY:
1192 printf_filtered ("(TYPE_CODE_PASCAL_ARRAY)");
1193 break;
1194 case TYPE_CODE_ERROR:
1195 printf_filtered ("(TYPE_CODE_ERROR)");
1196 break;
1197 case TYPE_CODE_MEMBER:
1198 printf_filtered ("(TYPE_CODE_MEMBER)");
1199 break;
1200 case TYPE_CODE_METHOD:
1201 printf_filtered ("(TYPE_CODE_METHOD)");
1202 break;
1203 case TYPE_CODE_REF:
1204 printf_filtered ("(TYPE_CODE_REF)");
1205 break;
1206 case TYPE_CODE_CHAR:
1207 printf_filtered ("(TYPE_CODE_CHAR)");
1208 break;
1209 case TYPE_CODE_BOOL:
1210 printf_filtered ("(TYPE_CODE_BOOL)");
1211 break;
1212 default:
1213 printf_filtered ("(UNKNOWN TYPE CODE)");
1214 break;
1215 }
1216 puts_filtered ("\n");
1217 printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
1218 printfi_filtered (spaces, "objfile 0x%x\n", TYPE_OBJFILE (type));
1219 printfi_filtered (spaces, "target_type 0x%x\n", TYPE_TARGET_TYPE (type));
1220 if (TYPE_TARGET_TYPE (type) != NULL)
1221 {
1222 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
1223 }
1224 printfi_filtered (spaces, "pointer_type 0x%x\n",
1225 TYPE_POINTER_TYPE (type));
1226 printfi_filtered (spaces, "reference_type 0x%x\n",
1227 TYPE_REFERENCE_TYPE (type));
1228 printfi_filtered (spaces, "function_type 0x%x\n",
1229 TYPE_FUNCTION_TYPE (type));
1230 printfi_filtered (spaces, "flags 0x%x", TYPE_FLAGS (type));
1231 if (TYPE_FLAGS (type) & TYPE_FLAG_UNSIGNED)
1232 {
1233 puts_filtered (" TYPE_FLAG_UNSIGNED");
1234 }
1235 if (TYPE_FLAGS (type) & TYPE_FLAG_SIGNED)
1236 {
1237 puts_filtered (" TYPE_FLAG_SIGNED");
1238 }
1239 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
1240 {
1241 puts_filtered (" TYPE_FLAG_STUB");
1242 }
1243 puts_filtered ("\n");
1244 printfi_filtered (spaces, "nfields %d 0x%x\n", TYPE_NFIELDS (type),
1245 TYPE_FIELDS (type));
1246 for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
1247 {
1248 printfi_filtered (spaces + 2,
1249 "[%d] bitpos %d bitsize %d type 0x%x name '%s' (0x%x)\n",
1250 idx, TYPE_FIELD_BITPOS (type, idx),
1251 TYPE_FIELD_BITSIZE (type, idx),
1252 TYPE_FIELD_TYPE (type, idx),
1253 TYPE_FIELD_NAME (type, idx),
1254 TYPE_FIELD_NAME (type, idx) != NULL
1255 ? TYPE_FIELD_NAME (type, idx)
1256 : "<NULL>");
1257 if (TYPE_FIELD_TYPE (type, idx) != NULL)
1258 {
1259 recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
1260 }
1261 }
1262 printfi_filtered (spaces, "vptr_basetype 0x%x\n",
1263 TYPE_VPTR_BASETYPE (type));
1264 if (TYPE_VPTR_BASETYPE (type) != NULL)
1265 {
1266 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
1267 }
1268 printfi_filtered (spaces, "vptr_fieldno %d\n", TYPE_VPTR_FIELDNO (type));
1269 switch (TYPE_CODE (type))
1270 {
1271 case TYPE_CODE_METHOD:
1272 case TYPE_CODE_FUNC:
1273 printfi_filtered (spaces, "arg_types 0x%x\n", TYPE_ARG_TYPES (type));
1274 print_arg_types (TYPE_ARG_TYPES (type), spaces);
1275 break;
1276
1277 case TYPE_CODE_STRUCT:
1278 printfi_filtered (spaces, "cplus_stuff 0x%x\n",
1279 TYPE_CPLUS_SPECIFIC (type));
1280 print_cplus_stuff (type, spaces);
1281 break;
1282
1283 default:
1284 /* We have to pick one of the union types to be able print and test
1285 the value. Pick cplus_struct_type, even though we know it isn't
1286 any particular one. */
1287 printfi_filtered (spaces, "type_specific 0x%x",
1288 TYPE_CPLUS_SPECIFIC (type));
1289 if (TYPE_CPLUS_SPECIFIC (type) != NULL)
1290 {
1291 printf_filtered (" (unknown data form)");
1292 }
1293 printf_filtered ("\n");
1294 break;
1295
1296 }
1297 }
1298
1299 #endif /* MAINTENANCE_CMDS */