* complaints.c: New file, code moved from utils.c.
[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 an array type using either a blank type supplied in RESULT_TYPE,
295 or creating a new type. Elements will be of type ELEMENT_TYPE, the
296 indices will be of type INDEX_TYPE, and will range from LOW_BOUND
297 to HIGH_BOUND, inclusive.
298
299 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
300 sure it is TYPE_CODE_UNDEF before we bash it into an array type? */
301
302 struct type *
303 create_array_type (result_type, element_type, index_type, low_bound,
304 high_bound)
305 struct type *result_type;
306 struct type *element_type;
307 struct type *index_type;
308 int low_bound;
309 int high_bound;
310 {
311 struct type *range_type;
312
313 /* Create a blank type if we are not given one to bash on. */
314 if (result_type == NULL)
315 {
316 result_type = alloc_type (TYPE_OBJFILE (element_type));
317 }
318
319 /* Create a range type. */
320 range_type = alloc_type (TYPE_OBJFILE (element_type));
321 TYPE_CODE (range_type) = TYPE_CODE_RANGE;
322 TYPE_TARGET_TYPE (range_type) = index_type;
323 TYPE_LENGTH (range_type) = sizeof (int); /* This should never be needed. */
324 TYPE_NFIELDS (range_type) = 2;
325 TYPE_FIELDS (range_type) = (struct field *)
326 TYPE_ALLOC (range_type, 2 * sizeof (struct field));
327 memset (TYPE_FIELDS (range_type), 0, 2 * sizeof (struct field));
328 TYPE_FIELD_BITPOS (range_type, 0) = low_bound;
329 TYPE_FIELD_BITPOS (range_type, 1) = high_bound;
330 TYPE_FIELD_TYPE (range_type, 0) = builtin_type_int; /* FIXME */
331 TYPE_FIELD_TYPE (range_type, 1) = builtin_type_int; /* FIXME */
332
333 /* Create the array type. */
334 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
335 TYPE_TARGET_TYPE (result_type) = element_type;
336 TYPE_LENGTH (result_type) =
337 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
338 TYPE_NFIELDS (result_type) = 1;
339 TYPE_FIELDS (result_type) = (struct field *)
340 TYPE_ALLOC (result_type, sizeof (struct field));
341 memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
342 TYPE_FIELD_TYPE (result_type, 0) = range_type;
343 TYPE_VPTR_FIELDNO (result_type) = -1;
344
345 return (result_type);
346 }
347
348
349 /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.
350 A MEMBER is a wierd thing -- it amounts to a typed offset into
351 a struct, e.g. "an int at offset 8". A MEMBER TYPE doesn't
352 include the offset (that's the value of the MEMBER itself), but does
353 include the structure type into which it points (for some reason).
354
355 When "smashing" the type, we preserve the objfile that the
356 old type pointed to, since we aren't changing where the type is actually
357 allocated. */
358
359 void
360 smash_to_member_type (type, domain, to_type)
361 struct type *type;
362 struct type *domain;
363 struct type *to_type;
364 {
365 struct objfile *objfile;
366
367 objfile = TYPE_OBJFILE (type);
368
369 memset ((char *) type, 0, sizeof (struct type));
370 TYPE_OBJFILE (type) = objfile;
371 TYPE_TARGET_TYPE (type) = to_type;
372 TYPE_DOMAIN_TYPE (type) = domain;
373 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
374 TYPE_CODE (type) = TYPE_CODE_MEMBER;
375 }
376
377 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
378 METHOD just means `function that gets an extra "this" argument'.
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_method_type (type, domain, to_type, args)
386 struct type *type;
387 struct type *domain;
388 struct type *to_type;
389 struct type **args;
390 {
391 struct objfile *objfile;
392
393 objfile = TYPE_OBJFILE (type);
394
395 memset ((char *) type, 0, sizeof (struct type));
396 TYPE_OBJFILE (type) = objfile;
397 TYPE_TARGET_TYPE (type) = to_type;
398 TYPE_DOMAIN_TYPE (type) = domain;
399 TYPE_ARG_TYPES (type) = args;
400 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
401 TYPE_CODE (type) = TYPE_CODE_METHOD;
402 }
403
404 /* Return a typename for a struct/union/enum type
405 without the tag qualifier. If the type has a NULL name,
406 NULL is returned. */
407
408 char *
409 type_name_no_tag (type)
410 register const struct type *type;
411 {
412 register char *name;
413
414 if ((name = TYPE_NAME (type)) != NULL)
415 {
416 switch (TYPE_CODE (type))
417 {
418 case TYPE_CODE_STRUCT:
419 if(!strncmp (name, "struct ", 7))
420 {
421 name += 7;
422 }
423 break;
424 case TYPE_CODE_UNION:
425 if(!strncmp (name, "union ", 6))
426 {
427 name += 6;
428 }
429 break;
430 case TYPE_CODE_ENUM:
431 if(!strncmp (name, "enum ", 5))
432 {
433 name += 5;
434 }
435 break;
436 default: /* To avoid -Wall warnings */
437 break;
438 }
439 }
440 return (name);
441 }
442
443 /* Lookup a primitive type named NAME.
444 Return zero if NAME is not a primitive type.*/
445
446 struct type *
447 lookup_primitive_typename (name)
448 char *name;
449 {
450 struct type ** const *p;
451
452 for (p = current_language -> la_builtin_type_vector; *p != NULL; p++)
453 {
454 if (!strcmp ((**p) -> name, name))
455 {
456 return (**p);
457 }
458 }
459 return (NULL);
460 }
461
462 /* Lookup a typedef or primitive type named NAME,
463 visible in lexical block BLOCK.
464 If NOERR is nonzero, return zero if NAME is not suitably defined. */
465
466 struct type *
467 lookup_typename (name, block, noerr)
468 char *name;
469 struct block *block;
470 int noerr;
471 {
472 register struct symbol *sym;
473 register struct type *tmp;
474
475 sym = lookup_symbol (name, block, VAR_NAMESPACE, 0, (struct symtab **) NULL);
476 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
477 {
478 tmp = lookup_primitive_typename (name);
479 if (tmp)
480 {
481 return (tmp);
482 }
483 else if (!tmp && noerr)
484 {
485 return (NULL);
486 }
487 else
488 {
489 error ("No type named %s.", name);
490 }
491 }
492 return (SYMBOL_TYPE (sym));
493 }
494
495 struct type *
496 lookup_unsigned_typename (name)
497 char *name;
498 {
499 char *uns = alloca (strlen (name) + 10);
500
501 strcpy (uns, "unsigned ");
502 strcpy (uns + 9, name);
503 return (lookup_typename (uns, (struct block *) NULL, 0));
504 }
505
506 struct type *
507 lookup_signed_typename (name)
508 char *name;
509 {
510 struct type *t;
511 char *uns = alloca (strlen (name) + 8);
512
513 strcpy (uns, "signed ");
514 strcpy (uns + 7, name);
515 t = lookup_typename (uns, (struct block *) NULL, 1);
516 /* If we don't find "signed FOO" just try again with plain "FOO". */
517 if (t != NULL)
518 return t;
519 return lookup_typename (name, (struct block *) NULL, 0);
520 }
521
522 /* Lookup a structure type named "struct NAME",
523 visible in lexical block BLOCK. */
524
525 struct type *
526 lookup_struct (name, block)
527 char *name;
528 struct block *block;
529 {
530 register struct symbol *sym;
531
532 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
533 (struct symtab **) NULL);
534
535 if (sym == NULL)
536 {
537 error ("No struct type named %s.", name);
538 }
539 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
540 {
541 error ("This context has class, union or enum %s, not a struct.", name);
542 }
543 return (SYMBOL_TYPE (sym));
544 }
545
546 /* Lookup a union type named "union NAME",
547 visible in lexical block BLOCK. */
548
549 struct type *
550 lookup_union (name, block)
551 char *name;
552 struct block *block;
553 {
554 register struct symbol *sym;
555
556 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
557 (struct symtab **) NULL);
558
559 if (sym == NULL)
560 {
561 error ("No union type named %s.", name);
562 }
563 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
564 {
565 error ("This context has class, struct or enum %s, not a union.", name);
566 }
567 return (SYMBOL_TYPE (sym));
568 }
569
570 /* Lookup an enum type named "enum NAME",
571 visible in lexical block BLOCK. */
572
573 struct type *
574 lookup_enum (name, block)
575 char *name;
576 struct block *block;
577 {
578 register struct symbol *sym;
579
580 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
581 (struct symtab **) NULL);
582 if (sym == NULL)
583 {
584 error ("No enum type named %s.", name);
585 }
586 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
587 {
588 error ("This context has class, struct or union %s, not an enum.", name);
589 }
590 return (SYMBOL_TYPE (sym));
591 }
592
593 /* Lookup a template type named "template NAME<TYPE>",
594 visible in lexical block BLOCK. */
595
596 struct type *
597 lookup_template_type (name, type, block)
598 char *name;
599 struct type *type;
600 struct block *block;
601 {
602 struct symbol *sym;
603 char *nam = (char*) alloca(strlen(name) + strlen(type->name) + 4);
604 strcpy (nam, name);
605 strcat (nam, "<");
606 strcat (nam, type->name);
607 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
608
609 sym = lookup_symbol (nam, block, VAR_NAMESPACE, 0, (struct symtab **)NULL);
610
611 if (sym == NULL)
612 {
613 error ("No template type named %s.", name);
614 }
615 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
616 {
617 error ("This context has class, union or enum %s, not a struct.", name);
618 }
619 return (SYMBOL_TYPE (sym));
620 }
621
622 /* Given a type TYPE, lookup the type of the component of type named
623 NAME.
624 If NOERR is nonzero, return zero if NAME is not suitably defined. */
625
626 struct type *
627 lookup_struct_elt_type (type, name, noerr)
628 struct type *type;
629 char *name;
630 int noerr;
631 {
632 int i;
633
634 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
635 TYPE_CODE (type) == TYPE_CODE_REF)
636 type = TYPE_TARGET_TYPE (type);
637
638 if (TYPE_CODE (type) != TYPE_CODE_STRUCT &&
639 TYPE_CODE (type) != TYPE_CODE_UNION)
640 {
641 target_terminal_ours ();
642 fflush (stdout);
643 fprintf (stderr, "Type ");
644 type_print (type, "", stderr, -1);
645 error (" is not a structure or union type.");
646 }
647
648 check_stub_type (type);
649
650 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
651 {
652 char *t_field_name = TYPE_FIELD_NAME (type, i);
653
654 if (t_field_name && !strcmp (t_field_name, name))
655 {
656 return TYPE_FIELD_TYPE (type, i);
657 }
658 }
659
660 /* OK, it's not in this class. Recursively check the baseclasses. */
661 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
662 {
663 struct type *t;
664
665 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 0);
666 if (t != NULL)
667 {
668 return t;
669 }
670 }
671
672 if (noerr)
673 {
674 return NULL;
675 }
676
677 target_terminal_ours ();
678 fflush (stdout);
679 fprintf (stderr, "Type ");
680 type_print (type, "", stderr, -1);
681 fprintf (stderr, " has no component named ");
682 fputs_filtered (name, stderr);
683 error (".");
684 return (struct type *)-1; /* For lint */
685 }
686
687 /* This function is really horrible, but to avoid it, there would need
688 to be more filling in of forward references. */
689
690 void
691 fill_in_vptr_fieldno (type)
692 struct type *type;
693 {
694 if (TYPE_VPTR_FIELDNO (type) < 0)
695 {
696 int i;
697 for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
698 {
699 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
700 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
701 {
702 TYPE_VPTR_FIELDNO (type)
703 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
704 TYPE_VPTR_BASETYPE (type)
705 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
706 break;
707 }
708 }
709 }
710 }
711
712 /* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
713
714 If this is a stubbed struct (i.e. declared as struct foo *), see if
715 we can find a full definition in some other file. If so, copy this
716 definition, so we can use it in future. If not, set a flag so we
717 don't waste too much time in future. (FIXME, this doesn't seem
718 to be happening...)
719
720 This used to be coded as a macro, but I don't think it is called
721 often enough to merit such treatment.
722 */
723
724 struct complaint stub_noname_complaint =
725 {"stub type has NULL name", 0, 0};
726
727 void
728 check_stub_type (type)
729 struct type *type;
730 {
731 if (TYPE_FLAGS(type) & TYPE_FLAG_STUB)
732 {
733 char* name = type_name_no_tag (type);
734 struct symbol *sym;
735 if (name == NULL)
736 {
737 complain (&stub_noname_complaint);
738 return;
739 }
740 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
741 (struct symtab **) NULL);
742 if (sym)
743 {
744 memcpy ((char *)type, (char *)SYMBOL_TYPE(sym), sizeof (struct type));
745 }
746 }
747 }
748
749 /* Ugly hack to convert method stubs into method types.
750
751 He ain't kiddin'. This demangles the name of the method into a string
752 including argument types, parses out each argument type, generates
753 a string casting a zero to that type, evaluates the string, and stuffs
754 the resulting type into an argtype vector!!! Then it knows the type
755 of the whole function (including argument types for overloading),
756 which info used to be in the stab's but was removed to hack back
757 the space required for them. */
758
759 void
760 check_stub_method (type, i, j)
761 struct type *type;
762 int i;
763 int j;
764 {
765 struct fn_field *f;
766 char *mangled_name = gdb_mangle_name (type, i, j);
767 char *demangled_name = cplus_demangle (mangled_name,
768 DMGL_PARAMS | DMGL_ANSI);
769 char *argtypetext, *p;
770 int depth = 0, argcount = 1;
771 struct type **argtypes;
772 struct type *mtype;
773
774 if (demangled_name == NULL)
775 {
776 error ("Internal: Cannot demangle mangled name `%s'.", mangled_name);
777 }
778
779 /* Now, read in the parameters that define this type. */
780 argtypetext = strchr (demangled_name, '(') + 1;
781 p = argtypetext;
782 while (*p)
783 {
784 if (*p == '(')
785 {
786 depth += 1;
787 }
788 else if (*p == ')')
789 {
790 depth -= 1;
791 }
792 else if (*p == ',' && depth == 0)
793 {
794 argcount += 1;
795 }
796
797 p += 1;
798 }
799
800 /* We need two more slots: one for the THIS pointer, and one for the
801 NULL [...] or void [end of arglist]. */
802
803 argtypes = (struct type **)
804 TYPE_ALLOC (type, (argcount + 2) * sizeof (struct type *));
805 p = argtypetext;
806 argtypes[0] = lookup_pointer_type (type);
807 argcount = 1;
808
809 if (*p != ')') /* () means no args, skip while */
810 {
811 depth = 0;
812 while (*p)
813 {
814 if (depth <= 0 && (*p == ',' || *p == ')'))
815 {
816 argtypes[argcount] =
817 parse_and_eval_type (argtypetext, p - argtypetext);
818 argcount += 1;
819 argtypetext = p + 1;
820 }
821
822 if (*p == '(')
823 {
824 depth += 1;
825 }
826 else if (*p == ')')
827 {
828 depth -= 1;
829 }
830
831 p += 1;
832 }
833 }
834
835 if (p[-2] != '.') /* Not '...' */
836 {
837 argtypes[argcount] = builtin_type_void; /* List terminator */
838 }
839 else
840 {
841 argtypes[argcount] = NULL; /* Ellist terminator */
842 }
843
844 free (demangled_name);
845
846 f = TYPE_FN_FIELDLIST1 (type, i);
847 TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
848
849 /* Now update the old "stub" type into a real type. */
850 mtype = TYPE_FN_FIELD_TYPE (f, j);
851 TYPE_DOMAIN_TYPE (mtype) = type;
852 TYPE_ARG_TYPES (mtype) = argtypes;
853 TYPE_FLAGS (mtype) &= ~TYPE_FLAG_STUB;
854 TYPE_FN_FIELD_STUB (f, j) = 0;
855 }
856
857 struct cplus_struct_type cplus_struct_default;
858
859 void
860 allocate_cplus_struct_type (type)
861 struct type *type;
862 {
863 if (!HAVE_CPLUS_STRUCT (type))
864 {
865 TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
866 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
867 *(TYPE_CPLUS_SPECIFIC(type)) = cplus_struct_default;
868 }
869 }
870
871 /* Helper function to initialize the standard scalar types.
872
873 If NAME is non-NULL and OBJFILE is non-NULL, then we make a copy
874 of the string pointed to by name in the type_obstack for that objfile,
875 and initialize the type name to that copy. There are places (mipsread.c
876 in particular, where init_type is called with a NULL value for NAME). */
877
878 struct type *
879 init_type (code, length, flags, name, objfile)
880 enum type_code code;
881 int length;
882 int flags;
883 char *name;
884 struct objfile *objfile;
885 {
886 register struct type *type;
887
888 type = alloc_type (objfile);
889 TYPE_CODE (type) = code;
890 TYPE_LENGTH (type) = length;
891 TYPE_FLAGS (type) |= flags;
892 if ((name != NULL) && (objfile != NULL))
893 {
894 TYPE_NAME (type) =
895 obsavestring (name, strlen (name), &objfile -> type_obstack);
896 }
897 else
898 {
899 TYPE_NAME (type) = name;
900 }
901
902 /* C++ fancies. */
903
904 if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
905 {
906 INIT_CPLUS_SPECIFIC (type);
907 }
908 return (type);
909 }
910
911 /* Look up a fundamental type for the specified objfile.
912 May need to construct such a type if this is the first use.
913
914 Some object file formats (ELF, COFF, etc) do not define fundamental
915 types such as "int" or "double". Others (stabs for example), do
916 define fundamental types.
917
918 For the formats which don't provide fundamental types, gdb can create
919 such types, using defaults reasonable for the current language and
920 the current target machine.
921
922 NOTE: This routine is obsolescent. Each debugging format reader
923 should manage it's own fundamental types, either creating them from
924 suitable defaults or reading them from the debugging information,
925 whichever is appropriate. The DWARF reader has already been
926 fixed to do this. Once the other readers are fixed, this routine
927 will go away. Also note that fundamental types should be managed
928 on a compilation unit basis in a multi-language environment, not
929 on a linkage unit basis as is done here. */
930
931
932 struct type *
933 lookup_fundamental_type (objfile, typeid)
934 struct objfile *objfile;
935 int typeid;
936 {
937 register struct type **typep;
938 register int nbytes;
939
940 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
941 {
942 error ("internal error - invalid fundamental type id %d", typeid);
943 }
944
945 /* If this is the first time we need a fundamental type for this objfile
946 then we need to initialize the vector of type pointers. */
947
948 if (objfile -> fundamental_types == NULL)
949 {
950 nbytes = FT_NUM_MEMBERS * sizeof (struct type *);
951 objfile -> fundamental_types = (struct type **)
952 obstack_alloc (&objfile -> type_obstack, nbytes);
953 memset ((char *) objfile -> fundamental_types, 0, nbytes);
954 }
955
956 /* Look for this particular type in the fundamental type vector. If one is
957 not found, create and install one appropriate for the current language. */
958
959 typep = objfile -> fundamental_types + typeid;
960 if (*typep == NULL)
961 {
962 *typep = create_fundamental_type (objfile, typeid);
963 }
964
965 return (*typep);
966 }
967
968 #if MAINTENANCE_CMDS
969
970 static void
971 print_bit_vector (bits, nbits)
972 B_TYPE *bits;
973 int nbits;
974 {
975 int bitno;
976
977 for (bitno = 0; bitno < nbits; bitno++)
978 {
979 if ((bitno % 8) == 0)
980 {
981 puts_filtered (" ");
982 }
983 if (B_TST (bits, bitno))
984 {
985 printf_filtered ("1");
986 }
987 else
988 {
989 printf_filtered ("0");
990 }
991 }
992 }
993
994 /* The args list is a strange beast. It is either terminated by a NULL
995 pointer for varargs functions, or by a pointer to a TYPE_CODE_VOID
996 type for normal fixed argcount functions. (FIXME someday)
997 Also note the first arg should be the "this" pointer, we may not want to
998 include it since we may get into a infinitely recursive situation. */
999
1000 static void
1001 print_arg_types (args, spaces)
1002 struct type **args;
1003 int spaces;
1004 {
1005 if (args != NULL)
1006 {
1007 while (*args != NULL)
1008 {
1009 recursive_dump_type (*args, spaces + 2);
1010 if ((*args++) -> code == TYPE_CODE_VOID)
1011 {
1012 break;
1013 }
1014 }
1015 }
1016 }
1017
1018 static void
1019 dump_fn_fieldlists (type, spaces)
1020 struct type *type;
1021 int spaces;
1022 {
1023 int method_idx;
1024 int overload_idx;
1025 struct fn_field *f;
1026
1027 printfi_filtered (spaces, "fn_fieldlists 0x%x\n",
1028 TYPE_FN_FIELDLISTS (type));
1029 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
1030 {
1031 f = TYPE_FN_FIELDLIST1 (type, method_idx);
1032 printfi_filtered (spaces + 2, "[%d] name '%s' (0x%x) length %d\n",
1033 method_idx,
1034 TYPE_FN_FIELDLIST_NAME (type, method_idx),
1035 TYPE_FN_FIELDLIST_NAME (type, method_idx),
1036 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
1037 for (overload_idx = 0;
1038 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
1039 overload_idx++)
1040 {
1041 printfi_filtered (spaces + 4, "[%d] physname '%s' (0x%x)\n",
1042 overload_idx,
1043 TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
1044 TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
1045 printfi_filtered (spaces + 8, "type 0x%x\n",
1046 TYPE_FN_FIELD_TYPE (f, overload_idx));
1047 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
1048 spaces + 8 + 2);
1049 printfi_filtered (spaces + 8, "args 0x%x\n",
1050 TYPE_FN_FIELD_ARGS (f, overload_idx));
1051 print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx), spaces);
1052 printfi_filtered (spaces + 8, "fcontext 0x%x\n",
1053 TYPE_FN_FIELD_FCONTEXT (f, overload_idx));
1054 printfi_filtered (spaces + 8, "is_const %d\n",
1055 TYPE_FN_FIELD_CONST (f, overload_idx));
1056 printfi_filtered (spaces + 8, "is_volatile %d\n",
1057 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
1058 printfi_filtered (spaces + 8, "is_private %d\n",
1059 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
1060 printfi_filtered (spaces + 8, "is_protected %d\n",
1061 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
1062 printfi_filtered (spaces + 8, "is_stub %d\n",
1063 TYPE_FN_FIELD_STUB (f, overload_idx));
1064 printfi_filtered (spaces + 8, "voffset %u\n",
1065 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
1066 }
1067 }
1068 }
1069
1070 static void
1071 print_cplus_stuff (type, spaces)
1072 struct type *type;
1073 int spaces;
1074 {
1075 int bitno;
1076
1077 printfi_filtered (spaces, "n_baseclasses %d\n",
1078 TYPE_N_BASECLASSES (type));
1079 printfi_filtered (spaces, "nfn_fields %d\n",
1080 TYPE_NFN_FIELDS (type));
1081 printfi_filtered (spaces, "nfn_fields_total %d\n",
1082 TYPE_NFN_FIELDS_TOTAL (type));
1083 if (TYPE_N_BASECLASSES (type) > 0)
1084 {
1085 printfi_filtered (spaces, "virtual_field_bits (%d bits at *0x%x)",
1086 TYPE_N_BASECLASSES (type),
1087 TYPE_FIELD_VIRTUAL_BITS (type));
1088 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
1089 TYPE_N_BASECLASSES (type));
1090 puts_filtered ("\n");
1091 }
1092 if (TYPE_NFIELDS (type) > 0)
1093 {
1094 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
1095 {
1096 printfi_filtered (spaces, "private_field_bits (%d bits at *0x%x)",
1097 TYPE_NFIELDS (type),
1098 TYPE_FIELD_PRIVATE_BITS (type));
1099 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
1100 TYPE_NFIELDS (type));
1101 puts_filtered ("\n");
1102 }
1103 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
1104 {
1105 printfi_filtered (spaces, "protected_field_bits (%d bits at *0x%x)",
1106 TYPE_NFIELDS (type),
1107 TYPE_FIELD_PROTECTED_BITS (type));
1108 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
1109 TYPE_NFIELDS (type));
1110 puts_filtered ("\n");
1111 }
1112 }
1113 if (TYPE_NFN_FIELDS (type) > 0)
1114 {
1115 dump_fn_fieldlists (type, spaces);
1116 }
1117 }
1118
1119 void
1120 recursive_dump_type (type, spaces)
1121 struct type *type;
1122 int spaces;
1123 {
1124 int idx;
1125
1126 printfi_filtered (spaces, "type node 0x%x\n", type);
1127 printfi_filtered (spaces, "name '%s' (0x%x)\n", TYPE_NAME (type),
1128 TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
1129 printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
1130 switch (TYPE_CODE (type))
1131 {
1132 case TYPE_CODE_UNDEF:
1133 printf_filtered ("(TYPE_CODE_UNDEF)");
1134 break;
1135 case TYPE_CODE_PTR:
1136 printf_filtered ("(TYPE_CODE_PTR)");
1137 break;
1138 case TYPE_CODE_ARRAY:
1139 printf_filtered ("(TYPE_CODE_ARRAY)");
1140 break;
1141 case TYPE_CODE_STRUCT:
1142 printf_filtered ("(TYPE_CODE_STRUCT)");
1143 break;
1144 case TYPE_CODE_UNION:
1145 printf_filtered ("(TYPE_CODE_UNION)");
1146 break;
1147 case TYPE_CODE_ENUM:
1148 printf_filtered ("(TYPE_CODE_ENUM)");
1149 break;
1150 case TYPE_CODE_FUNC:
1151 printf_filtered ("(TYPE_CODE_FUNC)");
1152 break;
1153 case TYPE_CODE_INT:
1154 printf_filtered ("(TYPE_CODE_INT)");
1155 break;
1156 case TYPE_CODE_FLT:
1157 printf_filtered ("(TYPE_CODE_FLT)");
1158 break;
1159 case TYPE_CODE_VOID:
1160 printf_filtered ("(TYPE_CODE_VOID)");
1161 break;
1162 case TYPE_CODE_SET:
1163 printf_filtered ("(TYPE_CODE_SET)");
1164 break;
1165 case TYPE_CODE_RANGE:
1166 printf_filtered ("(TYPE_CODE_RANGE)");
1167 break;
1168 case TYPE_CODE_PASCAL_ARRAY:
1169 printf_filtered ("(TYPE_CODE_PASCAL_ARRAY)");
1170 break;
1171 case TYPE_CODE_ERROR:
1172 printf_filtered ("(TYPE_CODE_ERROR)");
1173 break;
1174 case TYPE_CODE_MEMBER:
1175 printf_filtered ("(TYPE_CODE_MEMBER)");
1176 break;
1177 case TYPE_CODE_METHOD:
1178 printf_filtered ("(TYPE_CODE_METHOD)");
1179 break;
1180 case TYPE_CODE_REF:
1181 printf_filtered ("(TYPE_CODE_REF)");
1182 break;
1183 case TYPE_CODE_CHAR:
1184 printf_filtered ("(TYPE_CODE_CHAR)");
1185 break;
1186 case TYPE_CODE_BOOL:
1187 printf_filtered ("(TYPE_CODE_BOOL)");
1188 break;
1189 default:
1190 printf_filtered ("(UNKNOWN TYPE CODE)");
1191 break;
1192 }
1193 puts_filtered ("\n");
1194 printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
1195 printfi_filtered (spaces, "objfile 0x%x\n", TYPE_OBJFILE (type));
1196 printfi_filtered (spaces, "target_type 0x%x\n", TYPE_TARGET_TYPE (type));
1197 if (TYPE_TARGET_TYPE (type) != NULL)
1198 {
1199 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
1200 }
1201 printfi_filtered (spaces, "pointer_type 0x%x\n",
1202 TYPE_POINTER_TYPE (type));
1203 printfi_filtered (spaces, "reference_type 0x%x\n",
1204 TYPE_REFERENCE_TYPE (type));
1205 printfi_filtered (spaces, "function_type 0x%x\n",
1206 TYPE_FUNCTION_TYPE (type));
1207 printfi_filtered (spaces, "flags 0x%x", TYPE_FLAGS (type));
1208 if (TYPE_FLAGS (type) & TYPE_FLAG_UNSIGNED)
1209 {
1210 puts_filtered (" TYPE_FLAG_UNSIGNED");
1211 }
1212 if (TYPE_FLAGS (type) & TYPE_FLAG_SIGNED)
1213 {
1214 puts_filtered (" TYPE_FLAG_SIGNED");
1215 }
1216 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
1217 {
1218 puts_filtered (" TYPE_FLAG_STUB");
1219 }
1220 puts_filtered ("\n");
1221 printfi_filtered (spaces, "nfields %d 0x%x\n", TYPE_NFIELDS (type),
1222 TYPE_FIELDS (type));
1223 for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
1224 {
1225 printfi_filtered (spaces + 2,
1226 "[%d] bitpos %d bitsize %d type 0x%x name '%s' (0x%x)\n",
1227 idx, TYPE_FIELD_BITPOS (type, idx),
1228 TYPE_FIELD_BITSIZE (type, idx),
1229 TYPE_FIELD_TYPE (type, idx),
1230 TYPE_FIELD_NAME (type, idx),
1231 TYPE_FIELD_NAME (type, idx) != NULL
1232 ? TYPE_FIELD_NAME (type, idx)
1233 : "<NULL>");
1234 if (TYPE_FIELD_TYPE (type, idx) != NULL)
1235 {
1236 recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
1237 }
1238 }
1239 printfi_filtered (spaces, "vptr_basetype 0x%x\n",
1240 TYPE_VPTR_BASETYPE (type));
1241 if (TYPE_VPTR_BASETYPE (type) != NULL)
1242 {
1243 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
1244 }
1245 printfi_filtered (spaces, "vptr_fieldno %d\n", TYPE_VPTR_FIELDNO (type));
1246 switch (TYPE_CODE (type))
1247 {
1248 case TYPE_CODE_METHOD:
1249 case TYPE_CODE_FUNC:
1250 printfi_filtered (spaces, "arg_types 0x%x\n", TYPE_ARG_TYPES (type));
1251 print_arg_types (TYPE_ARG_TYPES (type), spaces);
1252 break;
1253
1254 case TYPE_CODE_STRUCT:
1255 printfi_filtered (spaces, "cplus_stuff 0x%x\n",
1256 TYPE_CPLUS_SPECIFIC (type));
1257 print_cplus_stuff (type, spaces);
1258 break;
1259
1260 default:
1261 /* We have to pick one of the union types to be able print and test
1262 the value. Pick cplus_struct_type, even though we know it isn't
1263 any particular one. */
1264 printfi_filtered (spaces, "type_specific 0x%x",
1265 TYPE_CPLUS_SPECIFIC (type));
1266 if (TYPE_CPLUS_SPECIFIC (type) != NULL)
1267 {
1268 printf_filtered (" (unknown data form)");
1269 }
1270 printf_filtered ("\n");
1271 break;
1272
1273 }
1274 }
1275
1276 #endif /* MAINTENANCE_CMDS */