Lint
[deliverable/binutils-gdb.git] / gdb / gdbtypes.c
CommitLineData
1ab3bf1b
JG
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
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
1ab3bf1b 21#include "defs.h"
93fe4e33 22#include <string.h>
1ab3bf1b
JG
23#include "bfd.h"
24#include "symtab.h"
25#include "symfile.h"
5e2e79f8 26#include "objfiles.h"
1ab3bf1b
JG
27#include "gdbtypes.h"
28#include "expression.h"
29#include "language.h"
30#include "target.h"
31#include "value.h"
8f793aa5 32#include "demangle.h"
1ab3bf1b
JG
33
34/* Alloc a new type structure and fill it with some defaults. If
35 OBJFILE is non-NULL, then allocate the space for the type structure
36 in that objfile's type_obstack. */
37
38struct type *
39alloc_type (objfile)
40 struct objfile *objfile;
41{
42 register struct type *type;
43
44 /* Alloc the structure and start off with all fields zeroed. */
45
46 if (objfile == NULL)
47 {
48 type = (struct type *) xmalloc (sizeof (struct type));
49 }
50 else
51 {
52 type = (struct type *) obstack_alloc (&objfile -> type_obstack,
53 sizeof (struct type));
54 }
93fe4e33 55 (void) memset ((char *)type, 0, sizeof (struct type));
1ab3bf1b
JG
56
57 /* Initialize the fields that might not be zero. */
58
59 TYPE_CODE (type) = TYPE_CODE_UNDEF;
60 TYPE_OBJFILE (type) = objfile;
61 TYPE_VPTR_FIELDNO (type) = -1;
62
63 return (type);
64}
65
ea1549b3
JG
66/* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points
67 to a pointer to memory where the pointer type should be stored.
68 If *TYPEPTR is zero, update it to point to the pointer type we return.
69 We allocate new memory if needed. */
70
71struct type *
72make_pointer_type (type, typeptr)
73 struct type *type;
74 struct type **typeptr;
75{
76 register struct type *ntype; /* New type */
77 struct objfile *objfile;
78
79 ntype = TYPE_POINTER_TYPE (type);
80
81 if (ntype)
82 if (typeptr == 0)
83 return ntype; /* Don't care about alloc, and have new type. */
84 else if (*typeptr == 0)
85 {
86 *typeptr = ntype; /* Tracking alloc, and we have new type. */
87 return ntype;
88 }
89
90 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
91 {
92 ntype = alloc_type (TYPE_OBJFILE (type));
93 if (typeptr)
94 *typeptr = ntype;
95 }
96 else /* We have storage, but need to reset it. */
97 {
98 ntype = *typeptr;
99 objfile = TYPE_OBJFILE (ntype);
100 memset ((char *)ntype, 0, sizeof (struct type));
101 TYPE_OBJFILE (ntype) = objfile;
102 }
103
104 TYPE_TARGET_TYPE (ntype) = type;
105 TYPE_POINTER_TYPE (type) = ntype;
106
107 /* FIXME! Assume the machine has only one representation for pointers! */
108
109 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
110 TYPE_CODE (ntype) = TYPE_CODE_PTR;
111
112 /* pointers are unsigned */
113 TYPE_FLAGS (ntype) |= TYPE_FLAG_UNSIGNED;
114
115 if (!TYPE_POINTER_TYPE (type)) /* Remember it, if don't have one. */
116 TYPE_POINTER_TYPE (type) = ntype;
117
118 return ntype;
119}
120
1ab3bf1b
JG
121/* Given a type TYPE, return a type of pointers to that type.
122 May need to construct such a type if this is the first use. */
123
124struct type *
125lookup_pointer_type (type)
126 struct type *type;
127{
ea1549b3
JG
128 return make_pointer_type (type, (struct type **)0);
129}
130
131/* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero, points
132 to a pointer to memory where the reference type should be stored.
133 If *TYPEPTR is zero, update it to point to the reference type we return.
134 We allocate new memory if needed. */
135
136struct type *
137make_reference_type (type, typeptr)
138 struct type *type;
139 struct type **typeptr;
140{
141 register struct type *ntype; /* New type */
142 struct objfile *objfile;
143
144 ntype = TYPE_REFERENCE_TYPE (type);
1ab3bf1b 145
ea1549b3
JG
146 if (ntype)
147 if (typeptr == 0)
148 return ntype; /* Don't care about alloc, and have new type. */
149 else if (*typeptr == 0)
150 {
151 *typeptr = ntype; /* Tracking alloc, and we have new type. */
152 return ntype;
153 }
154
155 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
156 {
157 ntype = alloc_type (TYPE_OBJFILE (type));
158 if (typeptr)
159 *typeptr = ntype;
160 }
161 else /* We have storage, but need to reset it. */
1ab3bf1b 162 {
ea1549b3
JG
163 ntype = *typeptr;
164 objfile = TYPE_OBJFILE (ntype);
165 memset ((char *)ntype, 0, sizeof (struct type));
166 TYPE_OBJFILE (ntype) = objfile;
1ab3bf1b 167 }
ea1549b3
JG
168
169 TYPE_TARGET_TYPE (ntype) = type;
170 TYPE_REFERENCE_TYPE (type) = ntype;
171
172 /* FIXME! Assume the machine has only one representation for references,
173 and that it matches the (only) representation for pointers! */
174
175 TYPE_LENGTH (ntype) = TARGET_PTR_BIT / TARGET_CHAR_BIT;
176 TYPE_CODE (ntype) = TYPE_CODE_REF;
177
178 if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */
179 TYPE_REFERENCE_TYPE (type) = ntype;
180
181 return ntype;
1ab3bf1b
JG
182}
183
ea1549b3
JG
184/* Same as above, but caller doesn't care about memory allocation details. */
185
1ab3bf1b
JG
186struct type *
187lookup_reference_type (type)
188 struct type *type;
189{
ea1549b3
JG
190 return make_reference_type (type, (struct type **)0);
191}
192
193/* Lookup a function type that returns type TYPE. TYPEPTR, if nonzero, points
194 to a pointer to memory where the function type should be stored.
195 If *TYPEPTR is zero, update it to point to the function type we return.
196 We allocate new memory if needed. */
1ab3bf1b 197
ea1549b3
JG
198struct type *
199make_function_type (type, typeptr)
200 struct type *type;
201 struct type **typeptr;
202{
203 register struct type *ntype; /* New type */
204 struct objfile *objfile;
205
206 ntype = TYPE_FUNCTION_TYPE (type);
207
208 if (ntype)
209 if (typeptr == 0)
210 return ntype; /* Don't care about alloc, and have new type. */
211 else if (*typeptr == 0)
212 {
213 *typeptr = ntype; /* Tracking alloc, and we have new type. */
214 return ntype;
215 }
216
217 if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */
1ab3bf1b 218 {
ea1549b3
JG
219 ntype = alloc_type (TYPE_OBJFILE (type));
220 if (typeptr)
221 *typeptr = ntype;
1ab3bf1b 222 }
ea1549b3
JG
223 else /* We have storage, but need to reset it. */
224 {
225 ntype = *typeptr;
226 objfile = TYPE_OBJFILE (ntype);
227 memset ((char *)ntype, 0, sizeof (struct type));
228 TYPE_OBJFILE (ntype) = objfile;
229 }
230
231 TYPE_TARGET_TYPE (ntype) = type;
232 TYPE_FUNCTION_TYPE (type) = ntype;
233
234 TYPE_LENGTH (ntype) = 1;
235 TYPE_CODE (ntype) = TYPE_CODE_FUNC;
236
237 if (!TYPE_FUNCTION_TYPE (type)) /* Remember it, if don't have one. */
238 TYPE_FUNCTION_TYPE (type) = ntype;
239
240 return ntype;
1ab3bf1b
JG
241}
242
ea1549b3 243
1ab3bf1b
JG
244/* Given a type TYPE, return a type of functions that return that type.
245 May need to construct such a type if this is the first use. */
246
247struct type *
248lookup_function_type (type)
249 struct type *type;
250{
ea1549b3 251 return make_function_type (type, (struct type **)0);
1ab3bf1b
JG
252}
253
254/* Implement direct support for MEMBER_TYPE in GNU C++.
255 May need to construct such a type if this is the first use.
256 The TYPE is the type of the member. The DOMAIN is the type
257 of the aggregate that the member belongs to. */
258
259struct type *
260lookup_member_type (type, domain)
261 struct type *type;
262 struct type *domain;
263{
264 register struct type *mtype;
265
266 mtype = alloc_type (TYPE_OBJFILE (type));
267 smash_to_member_type (mtype, domain, type);
268 return (mtype);
269}
270
271/* Allocate a stub method whose return type is TYPE.
272 This apparently happens for speed of symbol reading, since parsing
273 out the arguments to the method is cpu-intensive, the way we are doing
274 it. So, we will fill in arguments later.
275 This always returns a fresh type. */
276
277struct type *
278allocate_stub_method (type)
279 struct type *type;
280{
281 struct type *mtype;
282
283 mtype = alloc_type (TYPE_OBJFILE (type));
284 TYPE_TARGET_TYPE (mtype) = type;
285 /* _DOMAIN_TYPE (mtype) = unknown yet */
286 /* _ARG_TYPES (mtype) = unknown yet */
287 TYPE_FLAGS (mtype) = TYPE_FLAG_STUB;
288 TYPE_CODE (mtype) = TYPE_CODE_METHOD;
289 TYPE_LENGTH (mtype) = 1;
290 return (mtype);
291}
292
293/* Create an array type. Elements will be of type TYPE, and there will
294 be NUM of them.
295
296 Eventually this should be extended to take two more arguments which
297 specify the bounds of the array and the type of the index.
298 It should also be changed to be a "lookup" function, with the
299 appropriate data structures added to the type field.
300 Then read array type should call here. */
301
302struct type *
303create_array_type (element_type, number)
304 struct type *element_type;
305 int number;
306{
307 struct type *result_type;
308 struct type *range_type;
309
310 result_type = alloc_type (TYPE_OBJFILE (element_type));
311
312 TYPE_CODE (result_type) = TYPE_CODE_ARRAY;
313 TYPE_TARGET_TYPE (result_type) = element_type;
314 TYPE_LENGTH (result_type) = number * TYPE_LENGTH (element_type);
315 TYPE_NFIELDS (result_type) = 1;
316 TYPE_FIELDS (result_type) = (struct field *)
317 obstack_alloc (&TYPE_OBJFILE (result_type) -> type_obstack,
318 sizeof (struct field));
319
320 {
321 /* Create range type. */
322 range_type = alloc_type (TYPE_OBJFILE (result_type));
323 TYPE_CODE (range_type) = TYPE_CODE_RANGE;
324 TYPE_TARGET_TYPE (range_type) = builtin_type_int; /* FIXME */
325
326 /* This should never be needed. */
327 TYPE_LENGTH (range_type) = sizeof (int);
328
329 TYPE_NFIELDS (range_type) = 2;
330 TYPE_FIELDS (range_type) = (struct field *)
331 obstack_alloc (&TYPE_OBJFILE (range_type) -> type_obstack,
332 2 * sizeof (struct field));
333 TYPE_FIELD_BITPOS (range_type, 0) = 0; /* FIXME */
334 TYPE_FIELD_BITPOS (range_type, 1) = number-1; /* FIXME */
335 TYPE_FIELD_TYPE (range_type, 0) = builtin_type_int; /* FIXME */
336 TYPE_FIELD_TYPE (range_type, 1) = builtin_type_int; /* FIXME */
337 }
338 TYPE_FIELD_TYPE(result_type,0)=range_type;
339 TYPE_VPTR_FIELDNO (result_type) = -1;
340
341 return (result_type);
342}
343
344
345/* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE.
346 A MEMBER is a wierd thing -- it amounts to a typed offset into
347 a struct, e.g. "an int at offset 8". A MEMBER TYPE doesn't
348 include the offset (that's the value of the MEMBER itself), but does
349 include the structure type into which it points (for some reason).
350
c2e4669f 351 When "smashing" the type, we preserve the objfile that the
1ab3bf1b 352 old type pointed to, since we aren't changing where the type is actually
c2e4669f 353 allocated. */
1ab3bf1b
JG
354
355void
356smash_to_member_type (type, domain, to_type)
357 struct type *type;
358 struct type *domain;
359 struct type *to_type;
360{
361 struct objfile *objfile;
362
363 objfile = TYPE_OBJFILE (type);
364
93fe4e33 365 (void) memset ((char *)type, 0, sizeof (struct type));
1ab3bf1b
JG
366 TYPE_OBJFILE (type) = objfile;
367 TYPE_TARGET_TYPE (type) = to_type;
368 TYPE_DOMAIN_TYPE (type) = domain;
369 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
370 TYPE_CODE (type) = TYPE_CODE_MEMBER;
371}
372
373/* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE.
374 METHOD just means `function that gets an extra "this" argument'.
375
c2e4669f 376 When "smashing" the type, we preserve the objfile that the
1ab3bf1b 377 old type pointed to, since we aren't changing where the type is actually
c2e4669f 378 allocated. */
1ab3bf1b
JG
379
380void
381smash_to_method_type (type, domain, to_type, args)
382 struct type *type;
383 struct type *domain;
384 struct type *to_type;
385 struct type **args;
386{
387 struct objfile *objfile;
388
389 objfile = TYPE_OBJFILE (type);
390
93fe4e33 391 (void) memset ((char *)type, 0, sizeof (struct type));
1ab3bf1b
JG
392 TYPE_OBJFILE (type) = objfile;
393 TYPE_TARGET_TYPE (type) = to_type;
394 TYPE_DOMAIN_TYPE (type) = domain;
395 TYPE_ARG_TYPES (type) = args;
396 TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */
397 TYPE_CODE (type) = TYPE_CODE_METHOD;
398}
399
400/* Return a typename for a struct/union/enum type
401 without the tag qualifier. If the type has a NULL name,
402 NULL is returned. */
403
404char *
405type_name_no_tag (type)
406 register const struct type *type;
407{
408 register char *name;
409
410 if ((name = TYPE_NAME (type)) != NULL)
411 {
412 switch (TYPE_CODE (type))
413 {
414 case TYPE_CODE_STRUCT:
415 if(!strncmp (name, "struct ", 7))
416 {
417 name += 7;
418 }
419 break;
420 case TYPE_CODE_UNION:
421 if(!strncmp (name, "union ", 6))
422 {
423 name += 6;
424 }
425 break;
426 case TYPE_CODE_ENUM:
427 if(!strncmp (name, "enum ", 5))
428 {
429 name += 5;
430 }
431 break;
ac88ca20
JG
432 default: /* To avoid -Wall warnings */
433 break;
1ab3bf1b
JG
434 }
435 }
436 return (name);
437}
438
439/* Lookup a primitive type named NAME.
440 Return zero if NAME is not a primitive type.*/
441
442struct type *
443lookup_primitive_typename (name)
444 char *name;
445{
446 struct type ** const *p;
447
448 for (p = current_language -> la_builtin_type_vector; *p != NULL; p++)
449 {
450 if (!strcmp ((**p) -> name, name))
451 {
452 return (**p);
453 }
454 }
455 return (NULL);
456}
457
458/* Lookup a typedef or primitive type named NAME,
459 visible in lexical block BLOCK.
460 If NOERR is nonzero, return zero if NAME is not suitably defined. */
461
462struct type *
463lookup_typename (name, block, noerr)
464 char *name;
465 struct block *block;
466 int noerr;
467{
468 register struct symbol *sym;
469 register struct type *tmp;
470
471 sym = lookup_symbol (name, block, VAR_NAMESPACE, 0, (struct symtab **) NULL);
472 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
473 {
474 tmp = lookup_primitive_typename (name);
475 if (tmp)
476 {
477 return (tmp);
478 }
479 else if (!tmp && noerr)
480 {
481 return (NULL);
482 }
483 else
484 {
485 error ("No type named %s.", name);
486 }
487 }
488 return (SYMBOL_TYPE (sym));
489}
490
491struct type *
492lookup_unsigned_typename (name)
493 char *name;
494{
495 char *uns = alloca (strlen (name) + 10);
496
497 strcpy (uns, "unsigned ");
498 strcpy (uns + 9, name);
499 return (lookup_typename (uns, (struct block *) NULL, 0));
500}
501
502/* Lookup a structure type named "struct NAME",
503 visible in lexical block BLOCK. */
504
505struct type *
506lookup_struct (name, block)
507 char *name;
508 struct block *block;
509{
510 register struct symbol *sym;
511
512 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
513 (struct symtab **) NULL);
514
515 if (sym == NULL)
516 {
517 error ("No struct type named %s.", name);
518 }
519 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
520 {
521 error ("This context has class, union or enum %s, not a struct.", name);
522 }
523 return (SYMBOL_TYPE (sym));
524}
525
526/* Lookup a union type named "union NAME",
527 visible in lexical block BLOCK. */
528
529struct type *
530lookup_union (name, block)
531 char *name;
532 struct block *block;
533{
534 register struct symbol *sym;
535
536 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
537 (struct symtab **) NULL);
538
539 if (sym == NULL)
540 {
541 error ("No union type named %s.", name);
542 }
543 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
544 {
545 error ("This context has class, struct or enum %s, not a union.", name);
546 }
547 return (SYMBOL_TYPE (sym));
548}
549
550/* Lookup an enum type named "enum NAME",
551 visible in lexical block BLOCK. */
552
553struct type *
554lookup_enum (name, block)
555 char *name;
556 struct block *block;
557{
558 register struct symbol *sym;
559
560 sym = lookup_symbol (name, block, STRUCT_NAMESPACE, 0,
561 (struct symtab **) NULL);
562 if (sym == NULL)
563 {
564 error ("No enum type named %s.", name);
565 }
566 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
567 {
568 error ("This context has class, struct or union %s, not an enum.", name);
569 }
570 return (SYMBOL_TYPE (sym));
571}
572
573/* Lookup a template type named "template NAME<TYPE>",
574 visible in lexical block BLOCK. */
575
576struct type *
577lookup_template_type (name, type, block)
578 char *name;
579 struct type *type;
580 struct block *block;
581{
582 struct symbol *sym;
583 char *nam = (char*) alloca(strlen(name) + strlen(type->name) + 4);
584 strcpy (nam, name);
585 strcat (nam, "<");
586 strcat (nam, type->name);
587 strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */
588
589 sym = lookup_symbol (nam, block, VAR_NAMESPACE, 0, (struct symtab **)NULL);
590
591 if (sym == NULL)
592 {
593 error ("No template type named %s.", name);
594 }
595 if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
596 {
597 error ("This context has class, union or enum %s, not a struct.", name);
598 }
599 return (SYMBOL_TYPE (sym));
600}
601
602/* Given a type TYPE, lookup the type of the component of type named
603 NAME.
604 If NOERR is nonzero, return zero if NAME is not suitably defined. */
605
606struct type *
607lookup_struct_elt_type (type, name, noerr)
608 struct type *type;
609 char *name;
610 int noerr;
611{
612 int i;
613
614 if (TYPE_CODE (type) != TYPE_CODE_STRUCT &&
615 TYPE_CODE (type) != TYPE_CODE_UNION)
616 {
617 target_terminal_ours ();
618 fflush (stdout);
619 fprintf (stderr, "Type ");
620 type_print (type, "", stderr, -1);
621 error (" is not a structure or union type.");
622 }
623
624 check_stub_type (type);
625
626 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
627 {
628 char *t_field_name = TYPE_FIELD_NAME (type, i);
629
630 if (t_field_name && !strcmp (t_field_name, name))
631 {
632 return TYPE_FIELD_TYPE (type, i);
633 }
634 }
635
636 /* OK, it's not in this class. Recursively check the baseclasses. */
637 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
638 {
639 struct type *t;
640
641 t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 0);
642 if (t != NULL)
643 {
644 return t;
645 }
646 }
647
648 if (noerr)
649 {
650 return NULL;
651 }
652
653 target_terminal_ours ();
654 fflush (stdout);
655 fprintf (stderr, "Type ");
656 type_print (type, "", stderr, -1);
657 fprintf (stderr, " has no component named ");
658 fputs_filtered (name, stderr);
659 error (".");
660 return (struct type *)-1; /* For lint */
661}
662
663/* This function is really horrible, but to avoid it, there would need
664 to be more filling in of forward references. */
665
666void
667fill_in_vptr_fieldno (type)
668 struct type *type;
669{
670 if (TYPE_VPTR_FIELDNO (type) < 0)
671 {
672 int i;
673 for (i = 1; i < TYPE_N_BASECLASSES (type); i++)
674 {
675 fill_in_vptr_fieldno (TYPE_BASECLASS (type, i));
676 if (TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i)) >= 0)
677 {
678 TYPE_VPTR_FIELDNO (type)
679 = TYPE_VPTR_FIELDNO (TYPE_BASECLASS (type, i));
680 TYPE_VPTR_BASETYPE (type)
681 = TYPE_VPTR_BASETYPE (TYPE_BASECLASS (type, i));
682 break;
683 }
684 }
685 }
686}
687
688/* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989.
689
690 If this is a stubbed struct (i.e. declared as struct foo *), see if
691 we can find a full definition in some other file. If so, copy this
692 definition, so we can use it in future. If not, set a flag so we
693 don't waste too much time in future. (FIXME, this doesn't seem
694 to be happening...)
695
696 This used to be coded as a macro, but I don't think it is called
697 often enough to merit such treatment.
698*/
699
700struct complaint stub_noname_complaint =
701 {"stub type has NULL name", 0, 0};
702
703void
704check_stub_type (type)
705 struct type *type;
706{
707 if (TYPE_FLAGS(type) & TYPE_FLAG_STUB)
708 {
709 char* name = type_name_no_tag (type);
710 struct symbol *sym;
711 if (name == NULL)
712 {
713 complain (&stub_noname_complaint, 0);
714 return;
715 }
716 sym = lookup_symbol (name, 0, STRUCT_NAMESPACE, 0,
717 (struct symtab **) NULL);
718 if (sym)
719 {
93fe4e33 720 memcpy ((char *)type, (char *)SYMBOL_TYPE(sym), sizeof (struct type));
1ab3bf1b
JG
721 }
722 }
723}
724
725/* Ugly hack to convert method stubs into method types.
726
727 He ain't kiddin'. This demangles the name of the method into a string
728 including argument types, parses out each argument type, generates
729 a string casting a zero to that type, evaluates the string, and stuffs
730 the resulting type into an argtype vector!!! Then it knows the type
731 of the whole function (including argument types for overloading),
732 which info used to be in the stab's but was removed to hack back
733 the space required for them. */
734
735void
736check_stub_method (type, i, j)
737 struct type *type;
738 int i;
739 int j;
740{
741 struct fn_field *f;
742 char *mangled_name = gdb_mangle_name (type, i, j);
8f793aa5 743 char *demangled_name = cplus_demangle (mangled_name, DMGL_PARAMS);
1ab3bf1b
JG
744 char *argtypetext, *p;
745 int depth = 0, argcount = 1;
746 struct type **argtypes;
747 struct type *mtype;
748
749 if (demangled_name == NULL)
750 {
751 error ("Internal: Cannot demangle mangled name `%s'.", mangled_name);
752 }
753
754 /* Now, read in the parameters that define this type. */
755 argtypetext = strchr (demangled_name, '(') + 1;
756 p = argtypetext;
757 while (*p)
758 {
759 if (*p == '(')
760 {
761 depth += 1;
762 }
763 else if (*p == ')')
764 {
765 depth -= 1;
766 }
767 else if (*p == ',' && depth == 0)
768 {
769 argcount += 1;
770 }
771
772 p += 1;
773 }
774
775 /* We need two more slots: one for the THIS pointer, and one for the
776 NULL [...] or void [end of arglist]. */
777
778 argtypes = (struct type **)
779 obstack_alloc (&TYPE_OBJFILE (type) -> type_obstack,
780 (argcount+2) * sizeof (struct type *));
781 p = argtypetext;
782 argtypes[0] = lookup_pointer_type (type);
783 argcount = 1;
784
785 if (*p != ')') /* () means no args, skip while */
786 {
787 depth = 0;
788 while (*p)
789 {
790 if (depth <= 0 && (*p == ',' || *p == ')'))
791 {
792 argtypes[argcount] =
793 parse_and_eval_type (argtypetext, p - argtypetext);
794 argcount += 1;
795 argtypetext = p + 1;
796 }
797
798 if (*p == '(')
799 {
800 depth += 1;
801 }
802 else if (*p == ')')
803 {
804 depth -= 1;
805 }
806
807 p += 1;
808 }
809 }
810
811 if (p[-2] != '.') /* ... */
812 {
813 argtypes[argcount] = builtin_type_void; /* Ellist terminator */
814 }
815 else
816 {
817 argtypes[argcount] = NULL; /* List terminator */
818 }
819
820 free (demangled_name);
821
822 f = TYPE_FN_FIELDLIST1 (type, i);
823 TYPE_FN_FIELD_PHYSNAME (f, j) = mangled_name;
824
825 /* Now update the old "stub" type into a real type. */
826 mtype = TYPE_FN_FIELD_TYPE (f, j);
827 TYPE_DOMAIN_TYPE (mtype) = type;
828 TYPE_ARG_TYPES (mtype) = argtypes;
829 TYPE_FLAGS (mtype) &= ~TYPE_FLAG_STUB;
830 TYPE_FN_FIELD_STUB (f, j) = 0;
831}
832
833const struct cplus_struct_type cplus_struct_default;
834
835void
836allocate_cplus_struct_type (type)
837 struct type *type;
838{
839 if (!HAVE_CPLUS_STRUCT (type))
840 {
841 TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
842 obstack_alloc (&current_objfile -> type_obstack,
843 sizeof (struct cplus_struct_type));
844 *(TYPE_CPLUS_SPECIFIC(type)) = cplus_struct_default;
845 }
846}
847
848/* Helper function to initialize the standard scalar types. */
849
850struct type *
851init_type (code, length, flags, name, objfile)
852 enum type_code code;
853 int length;
854 int flags;
855 char *name;
856 struct objfile *objfile;
857{
858 register struct type *type;
859
860 type = alloc_type (objfile);
861 TYPE_CODE (type) = code;
862 TYPE_LENGTH (type) = length;
863 TYPE_FLAGS (type) |= flags;
864 TYPE_NAME (type) = name;
865
866 /* C++ fancies. */
867
868 if (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
869 {
870 INIT_CPLUS_SPECIFIC (type);
871 }
872 return (type);
873}
874
875/* Look up a fundamental type for the specified objfile.
876 May need to construct such a type if this is the first use.
877
878 Some object file formats (ELF, COFF, etc) do not define fundamental
879 types such as "int" or "double". Others (stabs for example), do
880 define fundamental types.
881
882 For the formats which don't provide fundamental types, gdb can create
883 such types, using defaults reasonable for the current target machine. */
884
885struct type *
886lookup_fundamental_type (objfile, typeid)
887 struct objfile *objfile;
888 int typeid;
889{
890 register struct type *type = NULL;
891 register struct type **typep;
892 register int nbytes;
893
894 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
895 {
896 error ("internal error - invalid fundamental type id %d", typeid);
897 }
898 else
899 {
900 /* If this is the first time we */
901 if (objfile -> fundamental_types == NULL)
902 {
903 nbytes = FT_NUM_MEMBERS * sizeof (struct type *);
904 objfile -> fundamental_types = (struct type **)
905 obstack_alloc (&objfile -> type_obstack, nbytes);
93fe4e33 906 (void) memset ((char *)objfile -> fundamental_types, 0, nbytes);
1ab3bf1b
JG
907 }
908 typep = objfile -> fundamental_types + typeid;
909 if ((type = *typep) == NULL)
910 {
911 switch (typeid)
912 {
913 default:
914 error ("internal error: unhandled type id %d", typeid);
915 break;
916 case FT_VOID:
917 type = init_type (TYPE_CODE_VOID,
918 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1867b3be 919 0,
1ab3bf1b
JG
920 "void", objfile);
921 break;
922 case FT_BOOLEAN:
923 type = init_type (TYPE_CODE_INT,
924 TARGET_INT_BIT / TARGET_CHAR_BIT,
1867b3be 925 TYPE_FLAG_UNSIGNED,
318bf84f 926 "boolean", objfile);
1ab3bf1b
JG
927 break;
928 case FT_STRING:
929 type = init_type (TYPE_CODE_PASCAL_ARRAY,
930 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1867b3be 931 0,
318bf84f 932 "string", objfile);
1ab3bf1b
JG
933 break;
934 case FT_CHAR:
935 type = init_type (TYPE_CODE_INT,
936 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1867b3be 937 0,
318bf84f 938 "char", objfile);
1ab3bf1b
JG
939 break;
940 case FT_SIGNED_CHAR:
941 type = init_type (TYPE_CODE_INT,
942 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1867b3be 943 TYPE_FLAG_SIGNED,
318bf84f 944 "signed char", objfile);
1ab3bf1b
JG
945 break;
946 case FT_UNSIGNED_CHAR:
947 type = init_type (TYPE_CODE_INT,
948 TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1867b3be 949 TYPE_FLAG_UNSIGNED,
318bf84f 950 "unsigned char", objfile);
1ab3bf1b
JG
951 break;
952 case FT_SHORT:
953 type = init_type (TYPE_CODE_INT,
954 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1867b3be 955 0,
318bf84f 956 "short", objfile);
1ab3bf1b
JG
957 break;
958 case FT_SIGNED_SHORT:
959 type = init_type (TYPE_CODE_INT,
960 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1867b3be 961 TYPE_FLAG_SIGNED,
318bf84f 962 "signed short", objfile);
1ab3bf1b
JG
963 break;
964 case FT_UNSIGNED_SHORT:
965 type = init_type (TYPE_CODE_INT,
966 TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1867b3be 967 TYPE_FLAG_UNSIGNED,
318bf84f 968 "unsigned short", objfile);
1ab3bf1b
JG
969 break;
970 case FT_INTEGER:
971 type = init_type (TYPE_CODE_INT,
972 TARGET_INT_BIT / TARGET_CHAR_BIT,
1867b3be 973 0,
318bf84f 974 "int", objfile);
1ab3bf1b
JG
975 break;
976 case FT_SIGNED_INTEGER:
977 type = init_type (TYPE_CODE_INT,
978 TARGET_INT_BIT / TARGET_CHAR_BIT,
1867b3be 979 TYPE_FLAG_SIGNED,
318bf84f 980 "signed int", objfile);
1ab3bf1b
JG
981 break;
982 case FT_UNSIGNED_INTEGER:
983 type = init_type (TYPE_CODE_INT,
984 TARGET_INT_BIT / TARGET_CHAR_BIT,
1867b3be 985 TYPE_FLAG_UNSIGNED,
318bf84f 986 "unsigned int", objfile);
1ab3bf1b 987 break;
4a11eef2
FF
988 case FT_FIXED_DECIMAL:
989 type = init_type (TYPE_CODE_INT,
990 TARGET_INT_BIT / TARGET_CHAR_BIT,
1867b3be 991 0,
4a11eef2
FF
992 "fixed decimal", objfile);
993 break;
1ab3bf1b
JG
994 case FT_LONG:
995 type = init_type (TYPE_CODE_INT,
996 TARGET_LONG_BIT / TARGET_CHAR_BIT,
1867b3be 997 0,
318bf84f 998 "long", objfile);
1ab3bf1b
JG
999 break;
1000 case FT_SIGNED_LONG:
1001 type = init_type (TYPE_CODE_INT,
1002 TARGET_LONG_BIT / TARGET_CHAR_BIT,
1867b3be 1003 TYPE_FLAG_SIGNED,
318bf84f 1004 "signed long", objfile);
1ab3bf1b
JG
1005 break;
1006 case FT_UNSIGNED_LONG:
1007 type = init_type (TYPE_CODE_INT,
1008 TARGET_LONG_BIT / TARGET_CHAR_BIT,
1867b3be 1009 TYPE_FLAG_UNSIGNED,
318bf84f 1010 "unsigned long", objfile);
1ab3bf1b
JG
1011 break;
1012 case FT_LONG_LONG:
1013 type = init_type (TYPE_CODE_INT,
1014 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1867b3be 1015 0,
318bf84f 1016 "long long", objfile);
1ab3bf1b
JG
1017 break;
1018 case FT_SIGNED_LONG_LONG:
1019 type = init_type (TYPE_CODE_INT,
1020 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1867b3be 1021 TYPE_FLAG_SIGNED,
318bf84f 1022 "signed long long", objfile);
1ab3bf1b
JG
1023 break;
1024 case FT_UNSIGNED_LONG_LONG:
1025 type = init_type (TYPE_CODE_INT,
1026 TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1867b3be 1027 TYPE_FLAG_UNSIGNED,
4a11eef2 1028 "unsigned long long", objfile);
1ab3bf1b
JG
1029 break;
1030 case FT_FLOAT:
1031 type = init_type (TYPE_CODE_FLT,
1032 TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1867b3be 1033 0,
318bf84f 1034 "float", objfile);
1ab3bf1b
JG
1035 break;
1036 case FT_DBL_PREC_FLOAT:
1037 type = init_type (TYPE_CODE_FLT,
1038 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1867b3be 1039 0,
318bf84f 1040 "double", objfile);
1ab3bf1b 1041 break;
4a11eef2
FF
1042 case FT_FLOAT_DECIMAL:
1043 type = init_type (TYPE_CODE_FLT,
1044 TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1867b3be 1045 0,
4a11eef2
FF
1046 "floating decimal", objfile);
1047 break;
1ab3bf1b
JG
1048 case FT_EXT_PREC_FLOAT:
1049 type = init_type (TYPE_CODE_FLT,
1050 TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1867b3be 1051 0,
318bf84f 1052 "long double", objfile);
1ab3bf1b
JG
1053 break;
1054 case FT_COMPLEX:
1055 type = init_type (TYPE_CODE_FLT,
1056 TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1867b3be 1057 0,
318bf84f 1058 "complex", objfile);
1ab3bf1b
JG
1059 break;
1060 case FT_DBL_PREC_COMPLEX:
1061 type = init_type (TYPE_CODE_FLT,
1062 TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1867b3be 1063 0,
318bf84f 1064 "double complex", objfile);
1ab3bf1b
JG
1065 break;
1066 case FT_EXT_PREC_COMPLEX:
1067 type = init_type (TYPE_CODE_FLT,
1068 TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1867b3be 1069 0,
4a11eef2 1070 "long double complex", objfile);
1ab3bf1b
JG
1071 break;
1072 }
1073 /* Install the newly created type in the objfile's fundamental_types
1074 vector. */
1075 *typep = type;
1076 }
1077 }
1078 return (type);
1079}
1080
This page took 0.072038 seconds and 4 git commands to generate.