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