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