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