* gdbtypes.c (create_array_type): If TYPE_LENGTH (result_type)
[binutils-gdb.git] / gdb / gdbtypes.c
1 /* Support routines for manipulating internal types for GDB.
2 Copyright (C) 1992, 1993, 1994, 1995 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "gdb_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 /* These variables point to the objects
36 representing the predefined C data types. */
37
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;
56
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. */
60
61 struct type *
62 alloc_type (objfile)
63 struct objfile *objfile;
64 {
65 register struct type *type;
66
67 /* Alloc the structure and start off with all fields zeroed. */
68
69 if (objfile == NULL)
70 {
71 type = (struct type *) xmalloc (sizeof (struct type));
72 }
73 else
74 {
75 type = (struct type *) obstack_alloc (&objfile -> type_obstack,
76 sizeof (struct type));
77 OBJSTAT (objfile, n_types++);
78 }
79 memset ((char *) type, 0, sizeof (struct type));
80
81 /* Initialize the fields that might not be zero. */
82
83 TYPE_CODE (type) = TYPE_CODE_UNDEF;
84 TYPE_OBJFILE (type) = objfile;
85 TYPE_VPTR_FIELDNO (type) = -1;
86
87 return (type);
88 }
89
90 /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
91 to a pointer to memory where the pointer type should be stored.
92 If *TYPEPTR is zero, update it to point to the pointer type we return.
93 We allocate new memory if needed. */
94
95 struct type *
96 make_pointer_type (type, typeptr)
97 struct type *type;
98 struct type **typeptr;
99 {
100 register struct type *ntype; /* New type */
101 struct objfile *objfile;
102
103 ntype = TYPE_POINTER_TYPE (type);
104
105 if (ntype)
106 if (typeptr == 0)
107 return ntype; /* Don't care about alloc, and have new type. */
108 else if (*typeptr == 0)
109 {
110 *typeptr = ntype; /* Tracking alloc, and we have new type. */
111 return ntype;
112 }
113
114 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
115 {
116 ntype = alloc_type (TYPE_OBJFILE (type));
117 if (typeptr)
118 *typeptr = ntype;
119 }
120 else /* We have storage, but need to reset it. */
121 {
122 ntype = *typeptr;
123 objfile = TYPE_OBJFILE (ntype);
124 memset ((char *) ntype, 0, sizeof (struct type));
125 TYPE_OBJFILE (ntype) = objfile;
126 }
127
128 TYPE_TARGET_TYPE (ntype) = type;
129 TYPE_POINTER_TYPE (type) = ntype;
130
131 /* FIXME! Assume the machine has only one representation for pointers! */
132
133 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
134 TYPE_CODE (ntype) = TYPE_CODE_PTR;
135
136 /* pointers are unsigned */
137 TYPE_FLAGS (ntype) |= TYPE_FLAG_UNSIGNED;
138
139 if (!TYPE_POINTER_TYPE (type)) /* Remember it, if don't have one. */
140 TYPE_POINTER_TYPE (type) = ntype;
141
142 return ntype;
143 }
144
145 /* Given a type TYPE, return a type of pointers to that type.
146 May need to construct such a type if this is the first use. */
147
148 struct type *
149 lookup_pointer_type (type)
150 struct type *type;
151 {
152 return make_pointer_type (type, (struct type **)0);
153 }
154
155 /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero, points
156 to a pointer to memory where the reference type should be stored.
157 If *TYPEPTR is zero, update it to point to the reference type we return.
158 We allocate new memory if needed. */
159
160 struct type *
161 make_reference_type (type, typeptr)
162 struct type *type;
163 struct type **typeptr;
164 {
165 register struct type *ntype; /* New type */
166 struct objfile *objfile;
167
168 ntype = TYPE_REFERENCE_TYPE (type);
169
170 if (ntype)
171 if (typeptr == 0)
172 return ntype; /* Don't care about alloc, and have new type. */
173 else if (*typeptr == 0)
174 {
175 *typeptr = ntype; /* Tracking alloc, and we have new type. */
176 return ntype;
177 }
178
179 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
180 {
181 ntype = alloc_type (TYPE_OBJFILE (type));
182 if (typeptr)
183 *typeptr = ntype;
184 }
185 else /* We have storage, but need to reset it. */
186 {
187 ntype = *typeptr;
188 objfile = TYPE_OBJFILE (ntype);
189 memset ((char *) ntype, 0, sizeof (struct type));
190 TYPE_OBJFILE (ntype) = objfile;
191 }
192
193 TYPE_TARGET_TYPE (ntype) = type;
194 TYPE_REFERENCE_TYPE (type) = ntype;
195
196 /* FIXME! Assume the machine has only one representation for references,
197 and that it matches the (only) representation for pointers! */
198
199 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
200 TYPE_CODE (ntype) = TYPE_CODE_REF;
201
202 if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */
203 TYPE_REFERENCE_TYPE (type) = ntype;
204
205 return ntype;
206 }
207
208 /* Same as above, but caller doesn't care about memory allocation details. */
209
210 struct type *
211 lookup_reference_type (type)
212 struct type *type;
213 {
214 return make_reference_type (type, (struct type **)0);
215 }
216
217 /* Lookup a function type that returns type TYPE. TYPEPTR, if nonzero, points
218 to a pointer to memory where the function type should be stored.
219 If *TYPEPTR is zero, update it to point to the function type we return.
220 We allocate new memory if needed. */
221
222 struct type *
223 make_function_type (type, typeptr)
224 struct type *type;
225 struct type **typeptr;
226 {
227 register struct type *ntype; /* New type */
228 struct objfile *objfile;
229
230 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
231 {
232 ntype = alloc_type (TYPE_OBJFILE (type));
233 if (typeptr)
234 *typeptr = ntype;
235 }
236 else /* We have storage, but need to reset it. */
237 {
238 ntype = *typeptr;
239 objfile = TYPE_OBJFILE (ntype);
240 memset ((char *) ntype, 0, sizeof (struct type));
241 TYPE_OBJFILE (ntype) = objfile;
242 }
243
244 TYPE_TARGET_TYPE (ntype) = type;
245
246 TYPE_LENGTH (ntype) = 1;
247 TYPE_CODE (ntype) = TYPE_CODE_FUNC;
248
249 return ntype;
250 }
251
252
253 /* Given a type TYPE, return a type of functions that return that type.
254 May need to construct such a type if this is the first use. */
255
256 struct type *
257 lookup_function_type (type)
258 struct type *type;
259 {
260 return make_function_type (type, (struct type **)0);
261 }
262
263 /* Implement direct support for MEMBER_TYPE in GNU C++.
264 May need to construct such a type if this is the first use.
265 The TYPE is the type of the member. The DOMAIN is the type
266 of the aggregate that the member belongs to. */
267
268 struct type *
269 lookup_member_type (type, domain)
270 struct type *type;
271 struct type *domain;
272 {
273 register struct type *mtype;
274
275 mtype = alloc_type (TYPE_OBJFILE (type));
276 smash_to_member_type (mtype, domain, type);
277 return (mtype);
278 }
279
280 /* Allocate a stub method whose return type is TYPE.
281 This apparently happens for speed of symbol reading, since parsing
282 out the arguments to the method is cpu-intensive, the way we are doing
283 it. So, we will fill in arguments later.
284 This always returns a fresh type. */
285
286 struct type *
287 allocate_stub_method (type)
288 struct type *type;
289 {
290 struct type *mtype;
291
292 mtype = alloc_type (TYPE_OBJFILE (type));
293 TYPE_TARGET_TYPE (mtype) = type;
294 /* _DOMAIN_TYPE (mtype) = unknown yet */
295 /* _ARG_TYPES (mtype) = unknown yet */
296 TYPE_FLAGS (mtype) = TYPE_FLAG_STUB;
297 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
298 TYPE_LENGTH (mtype) = 1;
299 return (mtype);
300 }
301
302 /* Create a range type using either a blank type supplied in RESULT_TYPE,
303 or creating a new type, inheriting the objfile from INDEX_TYPE.
304
305 Indices will be of type INDEX_TYPE, and will range from LOW_BOUND to
306 HIGH_BOUND, inclusive.
307
308 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
309 sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
310
311 struct type *
312 create_range_type (result_type, index_type, low_bound, high_bound)
313 struct type *result_type;
314 struct type *index_type;
315 int low_bound;
316 int high_bound;
317 {
318 if (result_type == NULL)
319 {
320 result_type = alloc_type (TYPE_OBJFILE (index_type));
321 }
322 TYPE_CODE (result_type) = TYPE_CODE_RANGE;
323 TYPE_TARGET_TYPE (result_type) = index_type;
324 if (TYPE_FLAGS (index_type) & TYPE_FLAG_STUB)
325 TYPE_FLAGS (result_type) |= TYPE_FLAG_TARGET_STUB;
326 else
327 TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type));
328 TYPE_NFIELDS (result_type) = 2;
329 TYPE_FIELDS (result_type) = (struct field *)
330 TYPE_ALLOC (result_type, 2 * sizeof (struct field));
331 memset (TYPE_FIELDS (result_type), 0, 2 * sizeof (struct field));
332 TYPE_FIELD_BITPOS (result_type, 0) = low_bound;
333 TYPE_FIELD_BITPOS (result_type, 1) = high_bound;
334 TYPE_FIELD_TYPE (result_type, 0) = builtin_type_int; /* FIXME */
335 TYPE_FIELD_TYPE (result_type, 1) = builtin_type_int; /* FIXME */
336
337 return (result_type);
338 }
339
340 /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type TYPE.
341 Return 1 of type is a range type, 0 if it is discrete (and bounds
342 will fit in LONGEST), or -1 otherwise. */
343
344 int
345 get_discrete_bounds (type, lowp, highp)
346 struct type *type;
347 LONGEST *lowp, *highp;
348 {
349 CHECK_TYPEDEF (type);
350 switch (TYPE_CODE (type))
351 {
352 case TYPE_CODE_RANGE:
353 *lowp = TYPE_LOW_BOUND (type);
354 *highp = TYPE_HIGH_BOUND (type);
355 return 1;
356 case TYPE_CODE_ENUM:
357 if (TYPE_NFIELDS (type) > 0)
358 {
359 /* The enums may not be sorted by value, so search all
360 entries */
361 int i;
362
363 *lowp = *highp = TYPE_FIELD_BITPOS (type, 0);
364 for (i = 0; i < TYPE_NFIELDS (type); i++)
365 {
366 if (TYPE_FIELD_BITPOS (type, i) < *lowp)
367 *lowp = TYPE_FIELD_BITPOS (type, i);
368 if (TYPE_FIELD_BITPOS (type, i) > *highp)
369 *highp = TYPE_FIELD_BITPOS (type, i);
370 }
371 }
372 else
373 {
374 *lowp = 0;
375 *highp = -1;
376 }
377 return 0;
378 case TYPE_CODE_BOOL:
379 *lowp = 0;
380 *highp = 1;
381 return 0;
382 case TYPE_CODE_INT:
383 if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */
384 return -1;
385 if (!TYPE_UNSIGNED (type))
386 {
387 *lowp = - (1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
388 *highp = -*lowp - 1;
389 return 0;
390 }
391 /* ... fall through for unsigned ints ... */
392 case TYPE_CODE_CHAR:
393 *lowp = 0;
394 /* This round-about calculation is to avoid shifting by
395 TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work
396 if TYPE_LENGTH (type) == sizeof (LONGEST). */
397 *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
398 *highp = (*highp - 1) | *highp;
399 return 0;
400 default:
401 return -1;
402 }
403 }
404
405 /* Create an array type using either a blank type supplied in RESULT_TYPE,
406 or creating a new type, inheriting the objfile from RANGE_TYPE.
407
408 Elements will be of type ELEMENT_TYPE, the indices will be of type
409 RANGE_TYPE.
410
411 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
412 sure it is TYPE_CODE_UNDEF before we bash it into an array type? */
413
414 struct type *
415 create_array_type (result_type, element_type, range_type)
416 struct type *result_type;
417 struct type *element_type;
418 struct type *range_type;
419 {
420 LONGEST low_bound, high_bound;
421
422 if (result_type == NULL)
423 {
424 result_type = alloc_type (TYPE_OBJFILE (range_type));
425 }
426 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
427 TYPE_TARGET_TYPE (result_type) = element_type;
428 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
429 low_bound = high_bound = 0;
430 CHECK_TYPEDEF (element_type);
431 TYPE_LENGTH (result_type) =
432 TYPE_LENGTH (element_type) * (high_bound - low_bound + 1);
433 TYPE_NFIELDS (result_type) = 1;
434 TYPE_FIELDS (result_type) =
435 (struct field *) TYPE_ALLOC (result_type, sizeof (struct field));
436 memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
437 TYPE_FIELD_TYPE (result_type, 0) = range_type;
438 TYPE_VPTR_FIELDNO (result_type) = -1;
439
440 /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays */
441 if (TYPE_LENGTH (result_type) == 0)
442 TYPE_FLAGS (result_type) |= TYPE_FLAG_TARGET_STUB;
443
444 return (result_type);
445 }
446
447 /* Create a string type using either a blank type supplied in RESULT_TYPE,
448 or creating a new type. String types are similar enough to array of
449 char types that we can use create_array_type to build the basic type
450 and then bash it into a string type.
451
452 For fixed length strings, the range type contains 0 as the lower
453 bound and the length of the string minus one as the upper bound.
454
455 FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
456 sure it is TYPE_CODE_UNDEF before we bash it into a string type? */
457
458 struct type *
459 create_string_type (result_type, range_type)
460 struct type *result_type;
461 struct type *range_type;
462 {
463 result_type = create_array_type (result_type,
464 *current_language->string_char_type,
465 range_type);
466 TYPE_CODE (result_type) = TYPE_CODE_STRING;
467 return (result_type);
468 }
469
470 struct type *
471 create_set_type (result_type, domain_type)
472 struct type *result_type;
473 struct type *domain_type;
474 {
475 LONGEST low_bound, high_bound, bit_length;
476 if (result_type == NULL)
477 {
478 result_type = alloc_type (TYPE_OBJFILE (domain_type));
479 }
480 TYPE_CODE (result_type) = TYPE_CODE_SET;
481 TYPE_NFIELDS (result_type) = 1;
482 TYPE_FIELDS (result_type) = (struct field *)
483 TYPE_ALLOC (result_type, 1 * sizeof (struct field));
484 memset (TYPE_FIELDS (result_type), 0, sizeof (struct field));
485
486 if (! (TYPE_FLAGS (domain_type) & TYPE_FLAG_STUB))
487 {
488 if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
489 low_bound = high_bound = 0;
490 bit_length = high_bound - low_bound + 1;
491 TYPE_LENGTH (result_type)
492 = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
493 }
494 TYPE_FIELD_TYPE (result_type, 0) = domain_type;
495 return (result_type);
496 }
497
498 /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.
499 A MEMBER is a wierd thing -- it amounts to a typed offset into
500 a struct, e.g. "an int at offset 8". A MEMBER TYPE doesn't
501 include the offset (that's the value of the MEMBER itself), but does
502 include the structure type into which it points (for some reason).
503
504 When "smashing" the type, we preserve the objfile that the
505 old type pointed to, since we aren't changing where the type is actually
506 allocated. */
507
508 void
509 smash_to_member_type (type, domain, to_type)
510 struct type *type;
511 struct type *domain;
512 struct type *to_type;
513 {
514 struct objfile *objfile;
515
516 objfile = TYPE_OBJFILE (type);
517
518 memset ((char *) type, 0, sizeof (struct type));
519 TYPE_OBJFILE (type) = objfile;
520 TYPE_TARGET_TYPE (type) = to_type;
521 TYPE_DOMAIN_TYPE (type) = domain;
522 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
523 TYPE_CODE (type) = TYPE_CODE_MEMBER;
524 }
525
526 /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
527 METHOD just means `function that gets an extra "this" argument'.
528
529 When "smashing" the type, we preserve the objfile that the
530 old type pointed to, since we aren't changing where the type is actually
531 allocated. */
532
533 void
534 smash_to_method_type (type, domain, to_type, args)
535 struct type *type;
536 struct type *domain;
537 struct type *to_type;
538 struct type **args;
539 {
540 struct objfile *objfile;
541
542 objfile = TYPE_OBJFILE (type);
543
544 memset ((char *) type, 0, sizeof (struct type));
545 TYPE_OBJFILE (type) = objfile;
546 TYPE_TARGET_TYPE (type) = to_type;
547 TYPE_DOMAIN_TYPE (type) = domain;
548 TYPE_ARG_TYPES (type) = args;
549 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
550 TYPE_CODE (type) = TYPE_CODE_METHOD;
551 }
552
553 /* Return a typename for a struct/union/enum type without "struct ",
554 "union ", or "enum ". If the type has a NULL name, return NULL. */
555
556 char *
557 type_name_no_tag (type)
558 register const struct type *type;
559 {
560 if (TYPE_TAG_NAME (type) != NULL)
561 return TYPE_TAG_NAME (type);
562
563 /* Is there code which expects this to return the name if there is no
564 tag name? My guess is that this is mainly used for C++ in cases where
565 the two will always be the same. */
566 return TYPE_NAME (type);
567 }
568
569 /* Lookup a primitive type named NAME.
570 Return zero if NAME is not a primitive type.*/
571
572 struct type *
573 lookup_primitive_typename (name)
574 char *name;
575 {
576 struct type ** const *p;
577
578 for (p = current_language -> la_builtin_type_vector; *p != NULL; p++)
579 {
580 if (STREQ ((**p) -> name, name))
581 {
582 return (**p);
583 }
584 }
585 return (NULL);
586 }
587
588 /* Lookup a typedef or primitive type named NAME,
589 visible in lexical block BLOCK.
590 If NOERR is nonzero, return zero if NAME is not suitably defined. */
591
592 struct type *
593 lookup_typename (name, block, noerr)
594 char *name;
595 struct block *block;
596 int noerr;
597 {
598 register struct symbol *sym;
599 register struct type *tmp;
600
601 sym = lookup_symbol (name, block, VAR_NAMESPACE, 0, (struct symtab **) NULL);
602 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
603 {
604 tmp = lookup_primitive_typename (name);
605 if (tmp)
606 {
607 return (tmp);
608 }
609 else if (!tmp && noerr)
610 {
611 return (NULL);
612 }
613 else
614 {
615 error ("No type named %s.", name);
616 }
617 }
618 return (SYMBOL_TYPE (sym));
619 }
620
621 struct type *
622 lookup_unsigned_typename (name)
623 char *name;
624 {
625 char *uns = alloca (strlen (name) + 10);
626
627 strcpy (uns, "unsigned ");
628 strcpy (uns + 9, name);
629 return (lookup_typename (uns, (struct block *) NULL, 0));
630 }
631
632 struct type *
633 lookup_signed_typename (name)
634 char *name;
635 {
636 struct type *t;
637 char *uns = alloca (strlen (name) + 8);
638
639 strcpy (uns, "signed ");
640 strcpy (uns + 7, name);
641 t = lookup_typename (uns, (struct block *) NULL, 1);
642 /* If we don't find "signed FOO" just try again with plain "FOO". */
643 if (t != NULL)
644 return t;
645 return lookup_typename (name, (struct block *) NULL, 0);
646 }
647
648 /* Lookup a structure type named "struct NAME",
649 visible in lexical block BLOCK. */
650
651 struct type *
652 lookup_struct (name, block)
653 char *name;
654 struct block *block;
655 {
656 register struct symbol *sym;
657
658 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
659 (struct symtab **) NULL);
660
661 if (sym == NULL)
662 {
663 error ("No struct type named %s.", name);
664 }
665 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
666 {
667 error ("This context has class, union or enum %s, not a struct.", name);
668 }
669 return (SYMBOL_TYPE (sym));
670 }
671
672 /* Lookup a union type named "union NAME",
673 visible in lexical block BLOCK. */
674
675 struct type *
676 lookup_union (name, block)
677 char *name;
678 struct block *block;
679 {
680 register struct symbol *sym;
681
682 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
683 (struct symtab **) NULL);
684
685 if (sym == NULL)
686 {
687 error ("No union type named %s.", name);
688 }
689 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
690 {
691 error ("This context has class, struct or enum %s, not a union.", name);
692 }
693 return (SYMBOL_TYPE (sym));
694 }
695
696 /* Lookup an enum type named "enum NAME",
697 visible in lexical block BLOCK. */
698
699 struct type *
700 lookup_enum (name, block)
701 char *name;
702 struct block *block;
703 {
704 register struct symbol *sym;
705
706 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
707 (struct symtab **) NULL);
708 if (sym == NULL)
709 {
710 error ("No enum type named %s.", name);
711 }
712 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
713 {
714 error ("This context has class, struct or union %s, not an enum.", name);
715 }
716 return (SYMBOL_TYPE (sym));
717 }
718
719 /* Lookup a template type named "template NAME<TYPE>",
720 visible in lexical block BLOCK. */
721
722 struct type *
723 lookup_template_type (name, type, block)
724 char *name;
725 struct type *type;
726 struct block *block;
727 {
728 struct symbol *sym;
729 char *nam = (char*) alloca(strlen(name) + strlen(type->name) + 4);
730 strcpy (nam, name);
731 strcat (nam, "<");
732 strcat (nam, type->name);
733 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
734
735 sym = lookup_symbol (nam, block, VAR_NAMESPACE, 0, (struct symtab **)NULL);
736
737 if (sym == NULL)
738 {
739 error ("No template type named %s.", name);
740 }
741 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
742 {
743 error ("This context has class, union or enum %s, not a struct.", name);
744 }
745 return (SYMBOL_TYPE (sym));
746 }
747
748 /* Given a type TYPE, lookup the type of the component of type named NAME.
749
750 TYPE can be either a struct or union, or a pointer or reference to a struct or
751 union. If it is a pointer or reference, its target type is automatically used.
752 Thus '.' and '->' are interchangable, as specified for the definitions of the
753 expression element types STRUCTOP_STRUCT and STRUCTOP_PTR.
754
755 If NOERR is nonzero, return zero if NAME is not suitably defined.
756 If NAME is the name of a baseclass type, return that type. */
757
758 struct type *
759 lookup_struct_elt_type (type, name, noerr)
760 struct type *type;
761 char *name;
762 int noerr;
763 {
764 int i;
765
766 for (;;)
767 {
768 CHECK_TYPEDEF (type);
769 if (TYPE_CODE (type) != TYPE_CODE_PTR
770 && TYPE_CODE (type) != TYPE_CODE_REF)
771 break;
772 type = TYPE_TARGET_TYPE (type);
773 }
774
775 if (TYPE_CODE (type) != TYPE_CODE_STRUCT &&
776 TYPE_CODE (type) != TYPE_CODE_UNION)
777 {
778 target_terminal_ours ();
779 gdb_flush (gdb_stdout);
780 fprintf_unfiltered (gdb_stderr, "Type ");
781 type_print (type, "", gdb_stderr, -1);
782 error (" is not a structure or union type.");
783 }
784
785 #if 0
786 /* FIXME: This change put in by Michael seems incorrect for the case where
787 the structure tag name is the same as the member name. I.E. when doing
788 "ptype bell->bar" for "struct foo { int bar; int foo; } bell;"
789 Disabled by fnf. */
790 {
791 char *typename;
792
793 typename = type_name_no_tag (type);
794 if (typename != NULL && STREQ (typename, name))
795 return type;
796 }
797 #endif
798
799 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
800 {
801 char *t_field_name = TYPE_FIELD_NAME (type, i);
802
803 if (t_field_name && STREQ (t_field_name, name))
804 {
805 return TYPE_FIELD_TYPE (type, i);
806 }
807 }
808
809 /* OK, it's not in this class. Recursively check the baseclasses. */
810 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
811 {
812 struct type *t;
813
814 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, noerr);
815 if (t != NULL)
816 {
817 return t;
818 }
819 }
820
821 if (noerr)
822 {
823 return NULL;
824 }
825
826 target_terminal_ours ();
827 gdb_flush (gdb_stdout);
828 fprintf_unfiltered (gdb_stderr, "Type ");
829 type_print (type, "", gdb_stderr, -1);
830 fprintf_unfiltered (gdb_stderr, " has no component named ");
831 fputs_filtered (name, gdb_stderr);
832 error (".");
833 return (struct type *)-1; /* For lint */
834 }
835
836 /* If possible, make the vptr_fieldno and vptr_basetype fields of TYPE
837 valid. Callers should be aware that in some cases (for example,
838 the type or one of its baseclasses is a stub type and we are
839 debugging a .o file), this function will not be able to find the virtual
840 function table pointer, and vptr_fieldno will remain -1 and vptr_basetype
841 will remain NULL. */
842
843 void
844 fill_in_vptr_fieldno (type)
845 struct type *type;
846 {
847 CHECK_TYPEDEF (type);
848
849 if (TYPE_VPTR_FIELDNO (type) < 0)
850 {
851 int i;
852
853 /* We must start at zero in case the first (and only) baseclass is
854 virtual (and hence we cannot share the table pointer). */
855 for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
856 {
857 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
858 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
859 {
860 TYPE_VPTR_FIELDNO (type)
861 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
862 TYPE_VPTR_BASETYPE (type)
863 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
864 break;
865 }
866 }
867 }
868 }
869
870 /* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
871
872 If this is a stubbed struct (i.e. declared as struct foo *), see if
873 we can find a full definition in some other file. If so, copy this
874 definition, so we can use it in future. There used to be a comment (but
875 not any code) that if we don't find a full definition, we'd set a flag
876 so we don't spend time in the future checking the same type. That would
877 be a mistake, though--we might load in more symbols which contain a
878 full definition for the type.
879
880 This used to be coded as a macro, but I don't think it is called
881 often enough to merit such treatment. */
882
883 struct complaint stub_noname_complaint =
884 {"stub type has NULL name", 0, 0};
885
886 struct type *
887 check_typedef (type)
888 register struct type *type;
889 {
890 struct type *orig_type = type;
891 while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
892 {
893 if (!TYPE_TARGET_TYPE (type))
894 {
895 char* name;
896 struct symbol *sym;
897
898 /* It is dangerous to call lookup_symbol if we are currently
899 reading a symtab. Infinite recursion is one danger. */
900 if (currently_reading_symtab)
901 return type;
902
903 name = type_name_no_tag (type);
904 /* FIXME: shouldn't we separately check the TYPE_NAME and the
905 TYPE_TAG_NAME, and look in STRUCT_NAMESPACE and/or VAR_NAMESPACE
906 as appropriate? (this code was written before TYPE_NAME and
907 TYPE_TAG_NAME were separate). */
908 if (name == NULL)
909 {
910 complain (&stub_noname_complaint);
911 return type;
912 }
913 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
914 (struct symtab **) NULL);
915 if (sym)
916 TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym);
917 else
918 TYPE_TARGET_TYPE (type) = alloc_type (NULL); /* TYPE_CODE_UNDEF */
919 }
920 type = TYPE_TARGET_TYPE (type);
921 }
922
923 if ((TYPE_FLAGS(type) & TYPE_FLAG_STUB) && ! currently_reading_symtab)
924 {
925 char* name = type_name_no_tag (type);
926 /* FIXME: shouldn't we separately check the TYPE_NAME and the
927 TYPE_TAG_NAME, and look in STRUCT_NAMESPACE and/or VAR_NAMESPACE
928 as appropriate? (this code was written before TYPE_NAME and
929 TYPE_TAG_NAME were separate). */
930 struct symbol *sym;
931 if (name == NULL)
932 {
933 complain (&stub_noname_complaint);
934 return type;
935 }
936 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
937 (struct symtab **) NULL);
938 if (sym)
939 {
940 memcpy ((char *)type,
941 (char *)SYMBOL_TYPE(sym),
942 sizeof (struct type));
943 }
944 }
945
946 if (TYPE_FLAGS (type) & TYPE_FLAG_TARGET_STUB)
947 {
948 struct type *range_type;
949 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
950
951 if (TYPE_FLAGS (target_type) & TYPE_FLAG_STUB)
952 { }
953 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY
954 && TYPE_NFIELDS (type) == 1
955 && (TYPE_CODE (range_type = TYPE_FIELD_TYPE (type, 0))
956 == TYPE_CODE_RANGE))
957 {
958 /* Now recompute the length of the array type, based on its
959 number of elements and the target type's length. */
960 TYPE_LENGTH (type) =
961 ((TYPE_FIELD_BITPOS (range_type, 1)
962 - TYPE_FIELD_BITPOS (range_type, 0)
963 + 1)
964 * TYPE_LENGTH (target_type));
965 TYPE_FLAGS (type) &= ~TYPE_FLAG_TARGET_STUB;
966 }
967 else if (TYPE_CODE (type) == TYPE_CODE_RANGE)
968 {
969 TYPE_LENGTH (type) = TYPE_LENGTH (target_type);
970 TYPE_FLAGS (type) &= ~TYPE_FLAG_TARGET_STUB;
971 }
972 }
973 /* Cache TYPE_LENGTH for future use. */
974 TYPE_LENGTH (orig_type) = TYPE_LENGTH (type);
975 return type;
976 }
977
978 /* New code added to support parsing of Cfront stabs strings */
979 #include <ctype.h>
980 #define INIT_EXTRA { pextras->len=0; pextras->str[0]='\0'; }
981 #define ADD_EXTRA(c) { pextras->str[pextras->len++]=c; }
982 struct extra { char str[128]; int len; }; /* maximum extention is 128! FIXME */
983 void
984 add_name(pextras,n)
985 struct extra * pextras;
986 char * n;
987 {
988 char lenstr[512]; /* FIXME! hardcoded :-( */
989 int nlen, lenstrlen;
990 if ((nlen = (n ? strlen(n) : 0))==0)
991 return;
992 sprintf(pextras->str+pextras->len,"%d%s",nlen,n);
993 pextras->len=strlen(pextras->str);
994 }
995
996 void
997 add_mangled_type(pextras,t)
998 struct extra * pextras;
999 struct type * t;
1000 {
1001 enum type_code tcode;
1002 int tlen, tflags;
1003 char * tname;
1004
1005 tcode = TYPE_CODE(t);
1006 tlen = TYPE_LENGTH(t);
1007 tflags = TYPE_FLAGS(t);
1008 tname = TYPE_NAME(t);
1009 /* args of "..." seem to get mangled as "e" */
1010
1011 switch (tcode)
1012 {
1013 case TYPE_CODE_INT:
1014 if (tflags==1)
1015 ADD_EXTRA('U');
1016 switch (tlen)
1017 {
1018 case 1:
1019 ADD_EXTRA('c');
1020 break;
1021 case 2:
1022 ADD_EXTRA('s');
1023 break;
1024 case 4:
1025 {
1026 char* pname;
1027 if ((pname=strrchr(tname,'l'),pname) && !strcmp(pname,"long"))
1028 ADD_EXTRA('l')
1029 else
1030 ADD_EXTRA('i')
1031 }
1032 break;
1033 default:
1034 {
1035
1036 static struct complaint msg = {"Bad int type code length x%x\n",0,0};
1037
1038 complain (&msg, tlen);
1039
1040 }
1041 }
1042 break;
1043 case TYPE_CODE_FLT:
1044 switch (tlen)
1045 {
1046 case 4:
1047 ADD_EXTRA('f');
1048 break;
1049 case 8:
1050 ADD_EXTRA('d');
1051 break;
1052 case 16:
1053 ADD_EXTRA('r');
1054 break;
1055 default:
1056 {
1057 static struct complaint msg = {"Bad float type code length x%x\n",0,0};
1058 complain (&msg, tlen);
1059 }
1060 }
1061 break;
1062 case TYPE_CODE_REF:
1063 ADD_EXTRA('R');
1064 /* followed by what it's a ref to */
1065 break;
1066 case TYPE_CODE_PTR:
1067 ADD_EXTRA('P');
1068 /* followed by what it's a ptr to */
1069 break;
1070 case TYPE_CODE_TYPEDEF:
1071 {
1072 static struct complaint msg = {"Typedefs in overloaded functions not yet supported\n",0,0};
1073 complain (&msg);
1074 }
1075 /* followed by type bytes & name */
1076 break;
1077 case TYPE_CODE_FUNC:
1078 ADD_EXTRA('F');
1079 /* followed by func's arg '_' & ret types */
1080 break;
1081 case TYPE_CODE_VOID:
1082 ADD_EXTRA('v');
1083 break;
1084 case TYPE_CODE_METHOD:
1085 ADD_EXTRA('M');
1086 /* followed by name of class and func's arg '_' & ret types */
1087 add_name(pextras,tname);
1088 ADD_EXTRA('F'); /* then mangle function */
1089 break;
1090 case TYPE_CODE_STRUCT: /* C struct */
1091 case TYPE_CODE_UNION: /* C union */
1092 case TYPE_CODE_ENUM: /* Enumeration type */
1093 /* followed by name of type */
1094 add_name(pextras,tname);
1095 break;
1096
1097 /* errors possible types/not supported */
1098 case TYPE_CODE_CHAR:
1099 case TYPE_CODE_ARRAY: /* Array type */
1100 case TYPE_CODE_MEMBER: /* Member type */
1101 case TYPE_CODE_BOOL:
1102 case TYPE_CODE_COMPLEX: /* Complex float */
1103 case TYPE_CODE_UNDEF:
1104 case TYPE_CODE_SET: /* Pascal sets */
1105 case TYPE_CODE_RANGE:
1106 case TYPE_CODE_STRING:
1107 case TYPE_CODE_BITSTRING:
1108 case TYPE_CODE_ERROR:
1109 default:
1110 {
1111 static struct complaint msg = {"Unknown type code x%x\n",0,0};
1112 complain (&msg, tcode);
1113 }
1114 }
1115 if (t->target_type)
1116 add_mangled_type(pextras,t->target_type);
1117 }
1118
1119 char *
1120 cfront_mangle_name(type, i, j)
1121 struct type *type;
1122 int i;
1123 int j;
1124 {
1125 struct fn_field *f;
1126 char *mangled_name = gdb_mangle_name (type, i, j);
1127
1128 f = TYPE_FN_FIELDLIST1 (type, i); /* moved from below */
1129
1130 /* kludge to support cfront methods - gdb expects to find "F" for
1131 ARM_mangled names, so when we mangle, we have to add it here */
1132 if (ARM_DEMANGLING)
1133 {
1134 int k;
1135 char * arm_mangled_name;
1136 struct fn_field *method = &f[j];
1137 char *field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1138 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
1139 char *newname = type_name_no_tag (type);
1140
1141 struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
1142 int nargs = TYPE_NFIELDS(ftype); /* number of args */
1143 struct extra extras, * pextras = &extras;
1144 INIT_EXTRA
1145
1146 if (TYPE_FN_FIELD_STATIC_P (f, j)) /* j for sublist within this list */
1147 ADD_EXTRA('S')
1148 ADD_EXTRA('F')
1149 /* add args here! */
1150 if (nargs <= 1) /* no args besides this */
1151 ADD_EXTRA('v')
1152 else {
1153 for (k=1; k<nargs; k++)
1154 {
1155 struct type * t;
1156 t = TYPE_FIELD_TYPE(ftype,k);
1157 add_mangled_type(pextras,t);
1158 }
1159 }
1160 ADD_EXTRA('\0')
1161 printf("add_mangled_type: %s\n",extras.str); /* FIXME */
1162 arm_mangled_name = malloc(strlen(mangled_name)+extras.len);
1163 sprintf(arm_mangled_name,"%s%s",mangled_name,extras.str);
1164 free(mangled_name);
1165 mangled_name = arm_mangled_name;
1166 }
1167 }
1168 #undef ADD_EXTRA
1169 /* End of new code added to support parsing of Cfront stabs strings */
1170
1171 /* Ugly hack to convert method stubs into method types.
1172
1173 He ain't kiddin'. This demangles the name of the method into a string
1174 including argument types, parses out each argument type, generates
1175 a string casting a zero to that type, evaluates the string, and stuffs
1176 the resulting type into an argtype vector!!! Then it knows the type
1177 of the whole function (including argument types for overloading),
1178 which info used to be in the stab's but was removed to hack back
1179 the space required for them. */
1180
1181 void
1182 check_stub_method (type, i, j)
1183 struct type *type;
1184 int i;
1185 int j;
1186 {
1187 struct fn_field *f;
1188 char *mangled_name = gdb_mangle_name (type, i, j);
1189 char *demangled_name = cplus_demangle (mangled_name,
1190 DMGL_PARAMS | DMGL_ANSI);
1191 char *argtypetext, *p;
1192 int depth = 0, argcount = 1;
1193 struct type **argtypes;
1194 struct type *mtype;
1195
1196 /* Make sure we got back a function string that we can use. */
1197 if (demangled_name)
1198 p = strchr (demangled_name, '(');
1199
1200 if (demangled_name == NULL || p == NULL)
1201 error ("Internal: Cannot demangle mangled name `%s'.", mangled_name);
1202
1203 /* Now, read in the parameters that define this type. */
1204 p += 1;
1205 argtypetext = p;
1206 while (*p)
1207 {
1208 if (*p == '(')
1209 {
1210 depth += 1;
1211 }
1212 else if (*p == ')')
1213 {
1214 depth -= 1;
1215 }
1216 else if (*p == ',' && depth == 0)
1217 {
1218 argcount += 1;
1219 }
1220
1221 p += 1;
1222 }
1223
1224 /* We need two more slots: one for the THIS pointer, and one for the
1225 NULL [...] or void [end of arglist]. */
1226
1227 argtypes = (struct type **)
1228 TYPE_ALLOC (type, (argcount + 2) * sizeof (struct type *));
1229 p = argtypetext;
1230 /* FIXME: This is wrong for static member functions. */
1231 argtypes[0] = lookup_pointer_type (type);
1232 argcount = 1;
1233
1234 if (*p != ')') /* () means no args, skip while */
1235 {
1236 depth = 0;
1237 while (*p)
1238 {
1239 if (depth <= 0 && (*p == ',' || *p == ')'))
1240 {
1241 /* Avoid parsing of ellipsis, they will be handled below. */
1242 if (strncmp (argtypetext, "...", p - argtypetext) != 0)
1243 {
1244 argtypes[argcount] =
1245 parse_and_eval_type (argtypetext, p - argtypetext);
1246 argcount += 1;
1247 }
1248 argtypetext = p + 1;
1249 }
1250
1251 if (*p == '(')
1252 {
1253 depth += 1;
1254 }
1255 else if (*p == ')')
1256 {
1257 depth -= 1;
1258 }
1259
1260 p += 1;
1261 }
1262 }
1263
1264 if (p[-2] != '.') /* Not '...' */
1265 {
1266 argtypes[argcount] = builtin_type_void; /* List terminator */
1267 }
1268 else
1269 {
1270 argtypes[argcount] = NULL; /* Ellist terminator */
1271 }
1272
1273 free (demangled_name);
1274
1275 f = TYPE_FN_FIELDLIST1 (type, i);
1276
1277 TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
1278
1279 /* Now update the old "stub" type into a real type. */
1280 mtype = TYPE_FN_FIELD_TYPE (f, j);
1281 TYPE_DOMAIN_TYPE (mtype) = type;
1282 TYPE_ARG_TYPES (mtype) = argtypes;
1283 TYPE_FLAGS (mtype) &= ~TYPE_FLAG_STUB;
1284 TYPE_FN_FIELD_STUB (f, j) = 0;
1285 }
1286
1287 const struct cplus_struct_type cplus_struct_default;
1288
1289 void
1290 allocate_cplus_struct_type (type)
1291 struct type *type;
1292 {
1293 if (!HAVE_CPLUS_STRUCT (type))
1294 {
1295 TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
1296 TYPE_ALLOC (type, sizeof (struct cplus_struct_type));
1297 *(TYPE_CPLUS_SPECIFIC(type)) = cplus_struct_default;
1298 }
1299 }
1300
1301 /* Helper function to initialize the standard scalar types.
1302
1303 If NAME is non-NULL and OBJFILE is non-NULL, then we make a copy
1304 of the string pointed to by name in the type_obstack for that objfile,
1305 and initialize the type name to that copy. There are places (mipsread.c
1306 in particular, where init_type is called with a NULL value for NAME). */
1307
1308 struct type *
1309 init_type (code, length, flags, name, objfile)
1310 enum type_code code;
1311 int length;
1312 int flags;
1313 char *name;
1314 struct objfile *objfile;
1315 {
1316 register struct type *type;
1317
1318 type = alloc_type (objfile);
1319 TYPE_CODE (type) = code;
1320 TYPE_LENGTH (type) = length;
1321 TYPE_FLAGS (type) |= flags;
1322 if ((name != NULL) && (objfile != NULL))
1323 {
1324 TYPE_NAME (type) =
1325 obsavestring (name, strlen (name), &objfile -> type_obstack);
1326 }
1327 else
1328 {
1329 TYPE_NAME (type) = name;
1330 }
1331
1332 /* C++ fancies. */
1333
1334 if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
1335 {
1336 INIT_CPLUS_SPECIFIC (type);
1337 }
1338 return (type);
1339 }
1340
1341 /* Look up a fundamental type for the specified objfile.
1342 May need to construct such a type if this is the first use.
1343
1344 Some object file formats (ELF, COFF, etc) do not define fundamental
1345 types such as "int" or "double". Others (stabs for example), do
1346 define fundamental types.
1347
1348 For the formats which don't provide fundamental types, gdb can create
1349 such types, using defaults reasonable for the current language and
1350 the current target machine.
1351
1352 NOTE: This routine is obsolescent. Each debugging format reader
1353 should manage it's own fundamental types, either creating them from
1354 suitable defaults or reading them from the debugging information,
1355 whichever is appropriate. The DWARF reader has already been
1356 fixed to do this. Once the other readers are fixed, this routine
1357 will go away. Also note that fundamental types should be managed
1358 on a compilation unit basis in a multi-language environment, not
1359 on a linkage unit basis as is done here. */
1360
1361
1362 struct type *
1363 lookup_fundamental_type (objfile, typeid)
1364 struct objfile *objfile;
1365 int typeid;
1366 {
1367 register struct type **typep;
1368 register int nbytes;
1369
1370 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
1371 {
1372 error ("internal error - invalid fundamental type id %d", typeid);
1373 }
1374
1375 /* If this is the first time we need a fundamental type for this objfile
1376 then we need to initialize the vector of type pointers. */
1377
1378 if (objfile -> fundamental_types == NULL)
1379 {
1380 nbytes = FT_NUM_MEMBERS * sizeof (struct type *);
1381 objfile -> fundamental_types = (struct type **)
1382 obstack_alloc (&objfile -> type_obstack, nbytes);
1383 memset ((char *) objfile -> fundamental_types, 0, nbytes);
1384 OBJSTAT (objfile, n_types += FT_NUM_MEMBERS);
1385 }
1386
1387 /* Look for this particular type in the fundamental type vector. If one is
1388 not found, create and install one appropriate for the current language. */
1389
1390 typep = objfile -> fundamental_types + typeid;
1391 if (*typep == NULL)
1392 {
1393 *typep = create_fundamental_type (objfile, typeid);
1394 }
1395
1396 return (*typep);
1397 }
1398
1399 int
1400 can_dereference (t)
1401 struct type *t;
1402 {
1403 /* FIXME: Should we return true for references as well as pointers? */
1404 CHECK_TYPEDEF (t);
1405 return
1406 (t != NULL
1407 && TYPE_CODE (t) == TYPE_CODE_PTR
1408 && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID);
1409 }
1410
1411 /* Chill varying string and arrays are represented as follows:
1412
1413 struct { int __var_length; ELEMENT_TYPE[MAX_SIZE] __var_data};
1414
1415 Return true if TYPE is such a Chill varying type. */
1416
1417 int
1418 chill_varying_type (type)
1419 struct type *type;
1420 {
1421 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
1422 || TYPE_NFIELDS (type) != 2
1423 || strcmp (TYPE_FIELD_NAME (type, 0), "__var_length") != 0)
1424 return 0;
1425 return 1;
1426 }
1427
1428 #if MAINTENANCE_CMDS
1429
1430 static void
1431 print_bit_vector (bits, nbits)
1432 B_TYPE *bits;
1433 int nbits;
1434 {
1435 int bitno;
1436
1437 for (bitno = 0; bitno < nbits; bitno++)
1438 {
1439 if ((bitno % 8) == 0)
1440 {
1441 puts_filtered (" ");
1442 }
1443 if (B_TST (bits, bitno))
1444 {
1445 printf_filtered ("1");
1446 }
1447 else
1448 {
1449 printf_filtered ("0");
1450 }
1451 }
1452 }
1453
1454 /* The args list is a strange beast. It is either terminated by a NULL
1455 pointer for varargs functions, or by a pointer to a TYPE_CODE_VOID
1456 type for normal fixed argcount functions. (FIXME someday)
1457 Also note the first arg should be the "this" pointer, we may not want to
1458 include it since we may get into a infinitely recursive situation. */
1459
1460 static void
1461 print_arg_types (args, spaces)
1462 struct type **args;
1463 int spaces;
1464 {
1465 if (args != NULL)
1466 {
1467 while (*args != NULL)
1468 {
1469 recursive_dump_type (*args, spaces + 2);
1470 if ((*args++) -> code == TYPE_CODE_VOID)
1471 {
1472 break;
1473 }
1474 }
1475 }
1476 }
1477
1478 static void
1479 dump_fn_fieldlists (type, spaces)
1480 struct type *type;
1481 int spaces;
1482 {
1483 int method_idx;
1484 int overload_idx;
1485 struct fn_field *f;
1486
1487 printfi_filtered (spaces, "fn_fieldlists ");
1488 gdb_print_address (TYPE_FN_FIELDLISTS (type), gdb_stdout);
1489 printf_filtered ("\n");
1490 for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++)
1491 {
1492 f = TYPE_FN_FIELDLIST1 (type, method_idx);
1493 printfi_filtered (spaces + 2, "[%d] name '%s' (",
1494 method_idx,
1495 TYPE_FN_FIELDLIST_NAME (type, method_idx));
1496 gdb_print_address (TYPE_FN_FIELDLIST_NAME (type, method_idx),
1497 gdb_stdout);
1498 printf_filtered (") length %d\n",
1499 TYPE_FN_FIELDLIST_LENGTH (type, method_idx));
1500 for (overload_idx = 0;
1501 overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx);
1502 overload_idx++)
1503 {
1504 printfi_filtered (spaces + 4, "[%d] physname '%s' (",
1505 overload_idx,
1506 TYPE_FN_FIELD_PHYSNAME (f, overload_idx));
1507 gdb_print_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx),
1508 gdb_stdout);
1509 printf_filtered (")\n");
1510 printfi_filtered (spaces + 8, "type ");
1511 gdb_print_address (TYPE_FN_FIELD_TYPE (f, overload_idx), gdb_stdout);
1512 printf_filtered ("\n");
1513
1514 recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx),
1515 spaces + 8 + 2);
1516
1517 printfi_filtered (spaces + 8, "args ");
1518 gdb_print_address (TYPE_FN_FIELD_ARGS (f, overload_idx), gdb_stdout);
1519 printf_filtered ("\n");
1520
1521 print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx), spaces);
1522 printfi_filtered (spaces + 8, "fcontext ");
1523 gdb_print_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx),
1524 gdb_stdout);
1525 printf_filtered ("\n");
1526
1527 printfi_filtered (spaces + 8, "is_const %d\n",
1528 TYPE_FN_FIELD_CONST (f, overload_idx));
1529 printfi_filtered (spaces + 8, "is_volatile %d\n",
1530 TYPE_FN_FIELD_VOLATILE (f, overload_idx));
1531 printfi_filtered (spaces + 8, "is_private %d\n",
1532 TYPE_FN_FIELD_PRIVATE (f, overload_idx));
1533 printfi_filtered (spaces + 8, "is_protected %d\n",
1534 TYPE_FN_FIELD_PROTECTED (f, overload_idx));
1535 printfi_filtered (spaces + 8, "is_stub %d\n",
1536 TYPE_FN_FIELD_STUB (f, overload_idx));
1537 printfi_filtered (spaces + 8, "voffset %u\n",
1538 TYPE_FN_FIELD_VOFFSET (f, overload_idx));
1539 }
1540 }
1541 }
1542
1543 static void
1544 print_cplus_stuff (type, spaces)
1545 struct type *type;
1546 int spaces;
1547 {
1548 printfi_filtered (spaces, "n_baseclasses %d\n",
1549 TYPE_N_BASECLASSES (type));
1550 printfi_filtered (spaces, "nfn_fields %d\n",
1551 TYPE_NFN_FIELDS (type));
1552 printfi_filtered (spaces, "nfn_fields_total %d\n",
1553 TYPE_NFN_FIELDS_TOTAL (type));
1554 if (TYPE_N_BASECLASSES (type) > 0)
1555 {
1556 printfi_filtered (spaces, "virtual_field_bits (%d bits at *",
1557 TYPE_N_BASECLASSES (type));
1558 gdb_print_address (TYPE_FIELD_VIRTUAL_BITS (type), gdb_stdout);
1559 printf_filtered (")");
1560
1561 print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type),
1562 TYPE_N_BASECLASSES (type));
1563 puts_filtered ("\n");
1564 }
1565 if (TYPE_NFIELDS (type) > 0)
1566 {
1567 if (TYPE_FIELD_PRIVATE_BITS (type) != NULL)
1568 {
1569 printfi_filtered (spaces, "private_field_bits (%d bits at *",
1570 TYPE_NFIELDS (type));
1571 gdb_print_address (TYPE_FIELD_PRIVATE_BITS (type), gdb_stdout);
1572 printf_filtered (")");
1573 print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type),
1574 TYPE_NFIELDS (type));
1575 puts_filtered ("\n");
1576 }
1577 if (TYPE_FIELD_PROTECTED_BITS (type) != NULL)
1578 {
1579 printfi_filtered (spaces, "protected_field_bits (%d bits at *",
1580 TYPE_NFIELDS (type));
1581 gdb_print_address (TYPE_FIELD_PROTECTED_BITS (type), gdb_stdout);
1582 printf_filtered (")");
1583 print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type),
1584 TYPE_NFIELDS (type));
1585 puts_filtered ("\n");
1586 }
1587 }
1588 if (TYPE_NFN_FIELDS (type) > 0)
1589 {
1590 dump_fn_fieldlists (type, spaces);
1591 }
1592 }
1593
1594 static struct obstack dont_print_type_obstack;
1595
1596 void
1597 recursive_dump_type (type, spaces)
1598 struct type *type;
1599 int spaces;
1600 {
1601 int idx;
1602
1603 if (spaces == 0)
1604 obstack_begin (&dont_print_type_obstack, 0);
1605
1606 if (TYPE_NFIELDS (type) > 0
1607 || (TYPE_CPLUS_SPECIFIC (type) && TYPE_NFN_FIELDS (type) > 0))
1608 {
1609 struct type **first_dont_print
1610 = (struct type **)obstack_base (&dont_print_type_obstack);
1611
1612 int i = (struct type **)obstack_next_free (&dont_print_type_obstack)
1613 - first_dont_print;
1614
1615 while (--i >= 0)
1616 {
1617 if (type == first_dont_print[i])
1618 {
1619 printfi_filtered (spaces, "type node ");
1620 gdb_print_address (type, gdb_stdout);
1621 printf_filtered (" <same as already seen type>\n");
1622 return;
1623 }
1624 }
1625
1626 obstack_ptr_grow (&dont_print_type_obstack, type);
1627 }
1628
1629 printfi_filtered (spaces, "type node ");
1630 gdb_print_address (type, gdb_stdout);
1631 printf_filtered ("\n");
1632 printfi_filtered (spaces, "name '%s' (",
1633 TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>");
1634 gdb_print_address (TYPE_NAME (type), gdb_stdout);
1635 printf_filtered (")\n");
1636 if (TYPE_TAG_NAME (type) != NULL)
1637 {
1638 printfi_filtered (spaces, "tagname '%s' (",
1639 TYPE_TAG_NAME (type));
1640 gdb_print_address (TYPE_TAG_NAME (type), gdb_stdout);
1641 printf_filtered (")\n");
1642 }
1643 printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type));
1644 switch (TYPE_CODE (type))
1645 {
1646 case TYPE_CODE_UNDEF:
1647 printf_filtered ("(TYPE_CODE_UNDEF)");
1648 break;
1649 case TYPE_CODE_PTR:
1650 printf_filtered ("(TYPE_CODE_PTR)");
1651 break;
1652 case TYPE_CODE_ARRAY:
1653 printf_filtered ("(TYPE_CODE_ARRAY)");
1654 break;
1655 case TYPE_CODE_STRUCT:
1656 printf_filtered ("(TYPE_CODE_STRUCT)");
1657 break;
1658 case TYPE_CODE_UNION:
1659 printf_filtered ("(TYPE_CODE_UNION)");
1660 break;
1661 case TYPE_CODE_ENUM:
1662 printf_filtered ("(TYPE_CODE_ENUM)");
1663 break;
1664 case TYPE_CODE_FUNC:
1665 printf_filtered ("(TYPE_CODE_FUNC)");
1666 break;
1667 case TYPE_CODE_INT:
1668 printf_filtered ("(TYPE_CODE_INT)");
1669 break;
1670 case TYPE_CODE_FLT:
1671 printf_filtered ("(TYPE_CODE_FLT)");
1672 break;
1673 case TYPE_CODE_VOID:
1674 printf_filtered ("(TYPE_CODE_VOID)");
1675 break;
1676 case TYPE_CODE_SET:
1677 printf_filtered ("(TYPE_CODE_SET)");
1678 break;
1679 case TYPE_CODE_RANGE:
1680 printf_filtered ("(TYPE_CODE_RANGE)");
1681 break;
1682 case TYPE_CODE_STRING:
1683 printf_filtered ("(TYPE_CODE_STRING)");
1684 break;
1685 case TYPE_CODE_ERROR:
1686 printf_filtered ("(TYPE_CODE_ERROR)");
1687 break;
1688 case TYPE_CODE_MEMBER:
1689 printf_filtered ("(TYPE_CODE_MEMBER)");
1690 break;
1691 case TYPE_CODE_METHOD:
1692 printf_filtered ("(TYPE_CODE_METHOD)");
1693 break;
1694 case TYPE_CODE_REF:
1695 printf_filtered ("(TYPE_CODE_REF)");
1696 break;
1697 case TYPE_CODE_CHAR:
1698 printf_filtered ("(TYPE_CODE_CHAR)");
1699 break;
1700 case TYPE_CODE_BOOL:
1701 printf_filtered ("(TYPE_CODE_BOOL)");
1702 break;
1703 case TYPE_CODE_TYPEDEF:
1704 printf_filtered ("(TYPE_CODE_TYPEDEF)");
1705 break;
1706 default:
1707 printf_filtered ("(UNKNOWN TYPE CODE)");
1708 break;
1709 }
1710 puts_filtered ("\n");
1711 printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type));
1712 printfi_filtered (spaces, "objfile ");
1713 gdb_print_address (TYPE_OBJFILE (type), gdb_stdout);
1714 printf_filtered ("\n");
1715 printfi_filtered (spaces, "target_type ");
1716 gdb_print_address (TYPE_TARGET_TYPE (type), gdb_stdout);
1717 printf_filtered ("\n");
1718 if (TYPE_TARGET_TYPE (type) != NULL)
1719 {
1720 recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2);
1721 }
1722 printfi_filtered (spaces, "pointer_type ");
1723 gdb_print_address (TYPE_POINTER_TYPE (type), gdb_stdout);
1724 printf_filtered ("\n");
1725 printfi_filtered (spaces, "reference_type ");
1726 gdb_print_address (TYPE_REFERENCE_TYPE (type), gdb_stdout);
1727 printf_filtered ("\n");
1728 printfi_filtered (spaces, "flags 0x%x", TYPE_FLAGS (type));
1729 if (TYPE_FLAGS (type) & TYPE_FLAG_UNSIGNED)
1730 {
1731 puts_filtered (" TYPE_FLAG_UNSIGNED");
1732 }
1733 if (TYPE_FLAGS (type) & TYPE_FLAG_STUB)
1734 {
1735 puts_filtered (" TYPE_FLAG_STUB");
1736 }
1737 puts_filtered ("\n");
1738 printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type));
1739 gdb_print_address (TYPE_FIELDS (type), gdb_stdout);
1740 puts_filtered ("\n");
1741 for (idx = 0; idx < TYPE_NFIELDS (type); idx++)
1742 {
1743 printfi_filtered (spaces + 2,
1744 "[%d] bitpos %d bitsize %d type ",
1745 idx, TYPE_FIELD_BITPOS (type, idx),
1746 TYPE_FIELD_BITSIZE (type, idx));
1747 gdb_print_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout);
1748 printf_filtered (" name '%s' (",
1749 TYPE_FIELD_NAME (type, idx) != NULL
1750 ? TYPE_FIELD_NAME (type, idx)
1751 : "<NULL>");
1752 gdb_print_address (TYPE_FIELD_NAME (type, idx), gdb_stdout);
1753 printf_filtered (")\n");
1754 if (TYPE_FIELD_TYPE (type, idx) != NULL)
1755 {
1756 recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4);
1757 }
1758 }
1759 printfi_filtered (spaces, "vptr_basetype ");
1760 gdb_print_address (TYPE_VPTR_BASETYPE (type), gdb_stdout);
1761 puts_filtered ("\n");
1762 if (TYPE_VPTR_BASETYPE (type) != NULL)
1763 {
1764 recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2);
1765 }
1766 printfi_filtered (spaces, "vptr_fieldno %d\n", TYPE_VPTR_FIELDNO (type));
1767 switch (TYPE_CODE (type))
1768 {
1769 case TYPE_CODE_METHOD:
1770 case TYPE_CODE_FUNC:
1771 printfi_filtered (spaces, "arg_types ");
1772 gdb_print_address (TYPE_ARG_TYPES (type), gdb_stdout);
1773 puts_filtered ("\n");
1774 print_arg_types (TYPE_ARG_TYPES (type), spaces);
1775 break;
1776
1777 case TYPE_CODE_STRUCT:
1778 printfi_filtered (spaces, "cplus_stuff ");
1779 gdb_print_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
1780 puts_filtered ("\n");
1781 print_cplus_stuff (type, spaces);
1782 break;
1783
1784 default:
1785 /* We have to pick one of the union types to be able print and test
1786 the value. Pick cplus_struct_type, even though we know it isn't
1787 any particular one. */
1788 printfi_filtered (spaces, "type_specific ");
1789 gdb_print_address (TYPE_CPLUS_SPECIFIC (type), gdb_stdout);
1790 if (TYPE_CPLUS_SPECIFIC (type) != NULL)
1791 {
1792 printf_filtered (" (unknown data form)");
1793 }
1794 printf_filtered ("\n");
1795 break;
1796
1797 }
1798 if (spaces == 0)
1799 obstack_free (&dont_print_type_obstack, NULL);
1800 }
1801
1802 #endif /* MAINTENANCE_CMDS */
1803
1804 void
1805 _initialize_gdbtypes ()
1806 {
1807 builtin_type_void =
1808 init_type (TYPE_CODE_VOID, 1,
1809 0,
1810 "void", (struct objfile *) NULL);
1811 builtin_type_char =
1812 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1813 0,
1814 "char", (struct objfile *) NULL);
1815 builtin_type_signed_char =
1816 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1817 0,
1818 "signed char", (struct objfile *) NULL);
1819 builtin_type_unsigned_char =
1820 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1821 TYPE_FLAG_UNSIGNED,
1822 "unsigned char", (struct objfile *) NULL);
1823 builtin_type_short =
1824 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1825 0,
1826 "short", (struct objfile *) NULL);
1827 builtin_type_unsigned_short =
1828 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1829 TYPE_FLAG_UNSIGNED,
1830 "unsigned short", (struct objfile *) NULL);
1831 builtin_type_int =
1832 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1833 0,
1834 "int", (struct objfile *) NULL);
1835 builtin_type_unsigned_int =
1836 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1837 TYPE_FLAG_UNSIGNED,
1838 "unsigned int", (struct objfile *) NULL);
1839 builtin_type_long =
1840 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1841 0,
1842 "long", (struct objfile *) NULL);
1843 builtin_type_unsigned_long =
1844 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1845 TYPE_FLAG_UNSIGNED,
1846 "unsigned long", (struct objfile *) NULL);
1847 builtin_type_long_long =
1848 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1849 0,
1850 "long long", (struct objfile *) NULL);
1851 builtin_type_unsigned_long_long =
1852 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1853 TYPE_FLAG_UNSIGNED,
1854 "unsigned long long", (struct objfile *) NULL);
1855 builtin_type_float =
1856 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1857 0,
1858 "float", (struct objfile *) NULL);
1859 builtin_type_double =
1860 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1861 0,
1862 "double", (struct objfile *) NULL);
1863 builtin_type_long_double =
1864 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1865 0,
1866 "long double", (struct objfile *) NULL);
1867 builtin_type_complex =
1868 init_type (TYPE_CODE_COMPLEX, 2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1869 0,
1870 "complex", (struct objfile *) NULL);
1871 TYPE_TARGET_TYPE (builtin_type_complex) = builtin_type_float;
1872 builtin_type_double_complex =
1873 init_type (TYPE_CODE_COMPLEX, 2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1874 0,
1875 "double complex", (struct objfile *) NULL);
1876 TYPE_TARGET_TYPE (builtin_type_double_complex) = builtin_type_double;
1877 builtin_type_string =
1878 init_type (TYPE_CODE_STRING, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1879 0,
1880 "string", (struct objfile *) NULL);
1881 }