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