Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support routines for manipulating internal types for GDB. |
4f2aea11 | 2 | |
9b254dd1 | 3 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, |
4c38e0a4 JB |
4 | 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
5 | Free Software Foundation, Inc. | |
4f2aea11 | 6 | |
c906108c SS |
7 | Contributed by Cygnus Support, using pieces from other GDB modules. |
8 | ||
c5aa993b | 9 | This file is part of GDB. |
c906108c | 10 | |
c5aa993b JM |
11 | This program is free software; you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 13 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 14 | (at your option) any later version. |
c906108c | 15 | |
c5aa993b JM |
16 | This program is distributed in the hope that it will be useful, |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | GNU General Public License for more details. | |
c906108c | 20 | |
c5aa993b | 21 | You should have received a copy of the GNU General Public License |
a9762ec7 | 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
23 | |
24 | #include "defs.h" | |
25 | #include "gdb_string.h" | |
26 | #include "bfd.h" | |
27 | #include "symtab.h" | |
28 | #include "symfile.h" | |
29 | #include "objfiles.h" | |
30 | #include "gdbtypes.h" | |
31 | #include "expression.h" | |
32 | #include "language.h" | |
33 | #include "target.h" | |
34 | #include "value.h" | |
35 | #include "demangle.h" | |
36 | #include "complaints.h" | |
37 | #include "gdbcmd.h" | |
c91ecb25 | 38 | #include "wrapper.h" |
015a42b4 | 39 | #include "cp-abi.h" |
a02fd225 | 40 | #include "gdb_assert.h" |
ae5a43e0 | 41 | #include "hashtab.h" |
c906108c | 42 | |
ac3aafc7 | 43 | |
8da61cc4 DJ |
44 | /* Floatformat pairs. */ |
45 | const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = { | |
46 | &floatformat_ieee_single_big, | |
47 | &floatformat_ieee_single_little | |
48 | }; | |
49 | const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = { | |
50 | &floatformat_ieee_double_big, | |
51 | &floatformat_ieee_double_little | |
52 | }; | |
53 | const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = { | |
54 | &floatformat_ieee_double_big, | |
55 | &floatformat_ieee_double_littlebyte_bigword | |
56 | }; | |
57 | const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = { | |
58 | &floatformat_i387_ext, | |
59 | &floatformat_i387_ext | |
60 | }; | |
61 | const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = { | |
62 | &floatformat_m68881_ext, | |
63 | &floatformat_m68881_ext | |
64 | }; | |
65 | const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = { | |
66 | &floatformat_arm_ext_big, | |
67 | &floatformat_arm_ext_littlebyte_bigword | |
68 | }; | |
69 | const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = { | |
70 | &floatformat_ia64_spill_big, | |
71 | &floatformat_ia64_spill_little | |
72 | }; | |
73 | const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = { | |
74 | &floatformat_ia64_quad_big, | |
75 | &floatformat_ia64_quad_little | |
76 | }; | |
77 | const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = { | |
78 | &floatformat_vax_f, | |
79 | &floatformat_vax_f | |
80 | }; | |
81 | const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = { | |
82 | &floatformat_vax_d, | |
83 | &floatformat_vax_d | |
84 | }; | |
b14d30e1 JM |
85 | const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = { |
86 | &floatformat_ibm_long_double, | |
87 | &floatformat_ibm_long_double | |
88 | }; | |
8da61cc4 | 89 | |
8da61cc4 | 90 | |
c906108c | 91 | int opaque_type_resolution = 1; |
920d2a44 AC |
92 | static void |
93 | show_opaque_type_resolution (struct ui_file *file, int from_tty, | |
7ba81444 MS |
94 | struct cmd_list_element *c, |
95 | const char *value) | |
920d2a44 AC |
96 | { |
97 | fprintf_filtered (file, _("\ | |
98 | Resolution of opaque struct/class/union types (if set before loading symbols) is %s.\n"), | |
99 | value); | |
100 | } | |
101 | ||
5d161b24 | 102 | int overload_debug = 0; |
920d2a44 AC |
103 | static void |
104 | show_overload_debug (struct ui_file *file, int from_tty, | |
105 | struct cmd_list_element *c, const char *value) | |
106 | { | |
7ba81444 MS |
107 | fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"), |
108 | value); | |
920d2a44 | 109 | } |
c906108c | 110 | |
c5aa993b JM |
111 | struct extra |
112 | { | |
113 | char str[128]; | |
114 | int len; | |
7ba81444 | 115 | }; /* Maximum extension is 128! FIXME */ |
c906108c | 116 | |
a14ed312 | 117 | static void print_bit_vector (B_TYPE *, int); |
ad2f7632 | 118 | static void print_arg_types (struct field *, int, int); |
a14ed312 KB |
119 | static void dump_fn_fieldlists (struct type *, int); |
120 | static void print_cplus_stuff (struct type *, int); | |
7a292a7a | 121 | |
c906108c | 122 | |
e9bb382b UW |
123 | /* Allocate a new OBJFILE-associated type structure and fill it |
124 | with some defaults. Space for the type structure is allocated | |
125 | on the objfile's objfile_obstack. */ | |
c906108c SS |
126 | |
127 | struct type * | |
fba45db2 | 128 | alloc_type (struct objfile *objfile) |
c906108c | 129 | { |
52f0bd74 | 130 | struct type *type; |
c906108c | 131 | |
e9bb382b UW |
132 | gdb_assert (objfile != NULL); |
133 | ||
7ba81444 | 134 | /* Alloc the structure and start off with all fields zeroed. */ |
e9bb382b UW |
135 | type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type); |
136 | TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack, | |
137 | struct main_type); | |
138 | OBJSTAT (objfile, n_types++); | |
c906108c | 139 | |
e9bb382b UW |
140 | TYPE_OBJFILE_OWNED (type) = 1; |
141 | TYPE_OWNER (type).objfile = objfile; | |
c906108c | 142 | |
7ba81444 | 143 | /* Initialize the fields that might not be zero. */ |
c906108c SS |
144 | |
145 | TYPE_CODE (type) = TYPE_CODE_UNDEF; | |
c906108c | 146 | TYPE_VPTR_FIELDNO (type) = -1; |
2fdde8f8 | 147 | TYPE_CHAIN (type) = type; /* Chain back to itself. */ |
c906108c | 148 | |
c16abbde | 149 | return type; |
c906108c SS |
150 | } |
151 | ||
e9bb382b UW |
152 | /* Allocate a new GDBARCH-associated type structure and fill it |
153 | with some defaults. Space for the type structure is allocated | |
154 | on the heap. */ | |
155 | ||
156 | struct type * | |
157 | alloc_type_arch (struct gdbarch *gdbarch) | |
158 | { | |
159 | struct type *type; | |
160 | ||
161 | gdb_assert (gdbarch != NULL); | |
162 | ||
163 | /* Alloc the structure and start off with all fields zeroed. */ | |
164 | ||
165 | type = XZALLOC (struct type); | |
166 | TYPE_MAIN_TYPE (type) = XZALLOC (struct main_type); | |
167 | ||
168 | TYPE_OBJFILE_OWNED (type) = 0; | |
169 | TYPE_OWNER (type).gdbarch = gdbarch; | |
170 | ||
171 | /* Initialize the fields that might not be zero. */ | |
172 | ||
173 | TYPE_CODE (type) = TYPE_CODE_UNDEF; | |
174 | TYPE_VPTR_FIELDNO (type) = -1; | |
175 | TYPE_CHAIN (type) = type; /* Chain back to itself. */ | |
176 | ||
177 | return type; | |
178 | } | |
179 | ||
180 | /* If TYPE is objfile-associated, allocate a new type structure | |
181 | associated with the same objfile. If TYPE is gdbarch-associated, | |
182 | allocate a new type structure associated with the same gdbarch. */ | |
183 | ||
184 | struct type * | |
185 | alloc_type_copy (const struct type *type) | |
186 | { | |
187 | if (TYPE_OBJFILE_OWNED (type)) | |
188 | return alloc_type (TYPE_OWNER (type).objfile); | |
189 | else | |
190 | return alloc_type_arch (TYPE_OWNER (type).gdbarch); | |
191 | } | |
192 | ||
193 | /* If TYPE is gdbarch-associated, return that architecture. | |
194 | If TYPE is objfile-associated, return that objfile's architecture. */ | |
195 | ||
196 | struct gdbarch * | |
197 | get_type_arch (const struct type *type) | |
198 | { | |
199 | if (TYPE_OBJFILE_OWNED (type)) | |
200 | return get_objfile_arch (TYPE_OWNER (type).objfile); | |
201 | else | |
202 | return TYPE_OWNER (type).gdbarch; | |
203 | } | |
204 | ||
205 | ||
2fdde8f8 DJ |
206 | /* Alloc a new type instance structure, fill it with some defaults, |
207 | and point it at OLDTYPE. Allocate the new type instance from the | |
208 | same place as OLDTYPE. */ | |
209 | ||
210 | static struct type * | |
211 | alloc_type_instance (struct type *oldtype) | |
212 | { | |
213 | struct type *type; | |
214 | ||
215 | /* Allocate the structure. */ | |
216 | ||
e9bb382b | 217 | if (! TYPE_OBJFILE_OWNED (oldtype)) |
1deafd4e | 218 | type = XZALLOC (struct type); |
2fdde8f8 | 219 | else |
1deafd4e PA |
220 | type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack, |
221 | struct type); | |
222 | ||
2fdde8f8 DJ |
223 | TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype); |
224 | ||
225 | TYPE_CHAIN (type) = type; /* Chain back to itself for now. */ | |
226 | ||
c16abbde | 227 | return type; |
2fdde8f8 DJ |
228 | } |
229 | ||
230 | /* Clear all remnants of the previous type at TYPE, in preparation for | |
e9bb382b | 231 | replacing it with something else. Preserve owner information. */ |
2fdde8f8 DJ |
232 | static void |
233 | smash_type (struct type *type) | |
234 | { | |
e9bb382b UW |
235 | int objfile_owned = TYPE_OBJFILE_OWNED (type); |
236 | union type_owner owner = TYPE_OWNER (type); | |
237 | ||
2fdde8f8 DJ |
238 | memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type)); |
239 | ||
e9bb382b UW |
240 | /* Restore owner information. */ |
241 | TYPE_OBJFILE_OWNED (type) = objfile_owned; | |
242 | TYPE_OWNER (type) = owner; | |
243 | ||
2fdde8f8 DJ |
244 | /* For now, delete the rings. */ |
245 | TYPE_CHAIN (type) = type; | |
246 | ||
247 | /* For now, leave the pointer/reference types alone. */ | |
248 | } | |
249 | ||
c906108c SS |
250 | /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points |
251 | to a pointer to memory where the pointer type should be stored. | |
252 | If *TYPEPTR is zero, update it to point to the pointer type we return. | |
253 | We allocate new memory if needed. */ | |
254 | ||
255 | struct type * | |
fba45db2 | 256 | make_pointer_type (struct type *type, struct type **typeptr) |
c906108c | 257 | { |
52f0bd74 | 258 | struct type *ntype; /* New type */ |
053cb41b | 259 | struct type *chain; |
c906108c SS |
260 | |
261 | ntype = TYPE_POINTER_TYPE (type); | |
262 | ||
c5aa993b | 263 | if (ntype) |
c906108c | 264 | { |
c5aa993b | 265 | if (typeptr == 0) |
7ba81444 MS |
266 | return ntype; /* Don't care about alloc, |
267 | and have new type. */ | |
c906108c | 268 | else if (*typeptr == 0) |
c5aa993b | 269 | { |
7ba81444 | 270 | *typeptr = ntype; /* Tracking alloc, and have new type. */ |
c906108c | 271 | return ntype; |
c5aa993b | 272 | } |
c906108c SS |
273 | } |
274 | ||
275 | if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */ | |
276 | { | |
e9bb382b | 277 | ntype = alloc_type_copy (type); |
c906108c SS |
278 | if (typeptr) |
279 | *typeptr = ntype; | |
280 | } | |
7ba81444 | 281 | else /* We have storage, but need to reset it. */ |
c906108c SS |
282 | { |
283 | ntype = *typeptr; | |
053cb41b | 284 | chain = TYPE_CHAIN (ntype); |
2fdde8f8 | 285 | smash_type (ntype); |
053cb41b | 286 | TYPE_CHAIN (ntype) = chain; |
c906108c SS |
287 | } |
288 | ||
289 | TYPE_TARGET_TYPE (ntype) = type; | |
290 | TYPE_POINTER_TYPE (type) = ntype; | |
291 | ||
7ba81444 MS |
292 | /* FIXME! Assume the machine has only one representation for |
293 | pointers! */ | |
c906108c | 294 | |
50810684 UW |
295 | TYPE_LENGTH (ntype) |
296 | = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT; | |
c906108c SS |
297 | TYPE_CODE (ntype) = TYPE_CODE_PTR; |
298 | ||
67b2adb2 | 299 | /* Mark pointers as unsigned. The target converts between pointers |
76e71323 | 300 | and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and |
7ba81444 | 301 | gdbarch_address_to_pointer. */ |
876cecd0 | 302 | TYPE_UNSIGNED (ntype) = 1; |
c5aa993b | 303 | |
c906108c SS |
304 | if (!TYPE_POINTER_TYPE (type)) /* Remember it, if don't have one. */ |
305 | TYPE_POINTER_TYPE (type) = ntype; | |
306 | ||
053cb41b JB |
307 | /* Update the length of all the other variants of this type. */ |
308 | chain = TYPE_CHAIN (ntype); | |
309 | while (chain != ntype) | |
310 | { | |
311 | TYPE_LENGTH (chain) = TYPE_LENGTH (ntype); | |
312 | chain = TYPE_CHAIN (chain); | |
313 | } | |
314 | ||
c906108c SS |
315 | return ntype; |
316 | } | |
317 | ||
318 | /* Given a type TYPE, return a type of pointers to that type. | |
319 | May need to construct such a type if this is the first use. */ | |
320 | ||
321 | struct type * | |
fba45db2 | 322 | lookup_pointer_type (struct type *type) |
c906108c | 323 | { |
c5aa993b | 324 | return make_pointer_type (type, (struct type **) 0); |
c906108c SS |
325 | } |
326 | ||
7ba81444 MS |
327 | /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero, |
328 | points to a pointer to memory where the reference type should be | |
329 | stored. If *TYPEPTR is zero, update it to point to the reference | |
330 | type we return. We allocate new memory if needed. */ | |
c906108c SS |
331 | |
332 | struct type * | |
fba45db2 | 333 | make_reference_type (struct type *type, struct type **typeptr) |
c906108c | 334 | { |
52f0bd74 | 335 | struct type *ntype; /* New type */ |
1e98b326 | 336 | struct type *chain; |
c906108c SS |
337 | |
338 | ntype = TYPE_REFERENCE_TYPE (type); | |
339 | ||
c5aa993b | 340 | if (ntype) |
c906108c | 341 | { |
c5aa993b | 342 | if (typeptr == 0) |
7ba81444 MS |
343 | return ntype; /* Don't care about alloc, |
344 | and have new type. */ | |
c906108c | 345 | else if (*typeptr == 0) |
c5aa993b | 346 | { |
7ba81444 | 347 | *typeptr = ntype; /* Tracking alloc, and have new type. */ |
c906108c | 348 | return ntype; |
c5aa993b | 349 | } |
c906108c SS |
350 | } |
351 | ||
352 | if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */ | |
353 | { | |
e9bb382b | 354 | ntype = alloc_type_copy (type); |
c906108c SS |
355 | if (typeptr) |
356 | *typeptr = ntype; | |
357 | } | |
7ba81444 | 358 | else /* We have storage, but need to reset it. */ |
c906108c SS |
359 | { |
360 | ntype = *typeptr; | |
1e98b326 | 361 | chain = TYPE_CHAIN (ntype); |
2fdde8f8 | 362 | smash_type (ntype); |
1e98b326 | 363 | TYPE_CHAIN (ntype) = chain; |
c906108c SS |
364 | } |
365 | ||
366 | TYPE_TARGET_TYPE (ntype) = type; | |
367 | TYPE_REFERENCE_TYPE (type) = ntype; | |
368 | ||
7ba81444 MS |
369 | /* FIXME! Assume the machine has only one representation for |
370 | references, and that it matches the (only) representation for | |
371 | pointers! */ | |
c906108c | 372 | |
50810684 UW |
373 | TYPE_LENGTH (ntype) = |
374 | gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT; | |
c906108c | 375 | TYPE_CODE (ntype) = TYPE_CODE_REF; |
c5aa993b | 376 | |
c906108c SS |
377 | if (!TYPE_REFERENCE_TYPE (type)) /* Remember it, if don't have one. */ |
378 | TYPE_REFERENCE_TYPE (type) = ntype; | |
379 | ||
1e98b326 JB |
380 | /* Update the length of all the other variants of this type. */ |
381 | chain = TYPE_CHAIN (ntype); | |
382 | while (chain != ntype) | |
383 | { | |
384 | TYPE_LENGTH (chain) = TYPE_LENGTH (ntype); | |
385 | chain = TYPE_CHAIN (chain); | |
386 | } | |
387 | ||
c906108c SS |
388 | return ntype; |
389 | } | |
390 | ||
7ba81444 MS |
391 | /* Same as above, but caller doesn't care about memory allocation |
392 | details. */ | |
c906108c SS |
393 | |
394 | struct type * | |
fba45db2 | 395 | lookup_reference_type (struct type *type) |
c906108c | 396 | { |
c5aa993b | 397 | return make_reference_type (type, (struct type **) 0); |
c906108c SS |
398 | } |
399 | ||
7ba81444 MS |
400 | /* Lookup a function type that returns type TYPE. TYPEPTR, if |
401 | nonzero, points to a pointer to memory where the function type | |
402 | should be stored. If *TYPEPTR is zero, update it to point to the | |
0c8b41f1 | 403 | function type we return. We allocate new memory if needed. */ |
c906108c SS |
404 | |
405 | struct type * | |
0c8b41f1 | 406 | make_function_type (struct type *type, struct type **typeptr) |
c906108c | 407 | { |
52f0bd74 | 408 | struct type *ntype; /* New type */ |
c906108c SS |
409 | |
410 | if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */ | |
411 | { | |
e9bb382b | 412 | ntype = alloc_type_copy (type); |
c906108c SS |
413 | if (typeptr) |
414 | *typeptr = ntype; | |
415 | } | |
7ba81444 | 416 | else /* We have storage, but need to reset it. */ |
c906108c SS |
417 | { |
418 | ntype = *typeptr; | |
2fdde8f8 | 419 | smash_type (ntype); |
c906108c SS |
420 | } |
421 | ||
422 | TYPE_TARGET_TYPE (ntype) = type; | |
423 | ||
424 | TYPE_LENGTH (ntype) = 1; | |
425 | TYPE_CODE (ntype) = TYPE_CODE_FUNC; | |
c5aa993b | 426 | |
c906108c SS |
427 | return ntype; |
428 | } | |
429 | ||
430 | ||
431 | /* Given a type TYPE, return a type of functions that return that type. | |
432 | May need to construct such a type if this is the first use. */ | |
433 | ||
434 | struct type * | |
fba45db2 | 435 | lookup_function_type (struct type *type) |
c906108c | 436 | { |
0c8b41f1 | 437 | return make_function_type (type, (struct type **) 0); |
c906108c SS |
438 | } |
439 | ||
47663de5 MS |
440 | /* Identify address space identifier by name -- |
441 | return the integer flag defined in gdbtypes.h. */ | |
442 | extern int | |
50810684 | 443 | address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier) |
47663de5 | 444 | { |
8b2dbe47 | 445 | int type_flags; |
7ba81444 | 446 | /* Check for known address space delimiters. */ |
47663de5 | 447 | if (!strcmp (space_identifier, "code")) |
876cecd0 | 448 | return TYPE_INSTANCE_FLAG_CODE_SPACE; |
47663de5 | 449 | else if (!strcmp (space_identifier, "data")) |
876cecd0 | 450 | return TYPE_INSTANCE_FLAG_DATA_SPACE; |
5f11f355 AC |
451 | else if (gdbarch_address_class_name_to_type_flags_p (gdbarch) |
452 | && gdbarch_address_class_name_to_type_flags (gdbarch, | |
453 | space_identifier, | |
454 | &type_flags)) | |
8b2dbe47 | 455 | return type_flags; |
47663de5 | 456 | else |
8a3fe4f8 | 457 | error (_("Unknown address space specifier: \"%s\""), space_identifier); |
47663de5 MS |
458 | } |
459 | ||
460 | /* Identify address space identifier by integer flag as defined in | |
7ba81444 | 461 | gdbtypes.h -- return the string version of the adress space name. */ |
47663de5 | 462 | |
321432c0 | 463 | const char * |
50810684 | 464 | address_space_int_to_name (struct gdbarch *gdbarch, int space_flag) |
47663de5 | 465 | { |
876cecd0 | 466 | if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE) |
47663de5 | 467 | return "code"; |
876cecd0 | 468 | else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE) |
47663de5 | 469 | return "data"; |
876cecd0 | 470 | else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL) |
5f11f355 AC |
471 | && gdbarch_address_class_type_flags_to_name_p (gdbarch)) |
472 | return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag); | |
47663de5 MS |
473 | else |
474 | return NULL; | |
475 | } | |
476 | ||
2fdde8f8 | 477 | /* Create a new type with instance flags NEW_FLAGS, based on TYPE. |
ad766c0a JB |
478 | |
479 | If STORAGE is non-NULL, create the new type instance there. | |
480 | STORAGE must be in the same obstack as TYPE. */ | |
47663de5 | 481 | |
b9362cc7 | 482 | static struct type * |
2fdde8f8 DJ |
483 | make_qualified_type (struct type *type, int new_flags, |
484 | struct type *storage) | |
47663de5 MS |
485 | { |
486 | struct type *ntype; | |
487 | ||
488 | ntype = type; | |
5f61c20e JK |
489 | do |
490 | { | |
491 | if (TYPE_INSTANCE_FLAGS (ntype) == new_flags) | |
492 | return ntype; | |
493 | ntype = TYPE_CHAIN (ntype); | |
494 | } | |
495 | while (ntype != type); | |
47663de5 | 496 | |
2fdde8f8 DJ |
497 | /* Create a new type instance. */ |
498 | if (storage == NULL) | |
499 | ntype = alloc_type_instance (type); | |
500 | else | |
501 | { | |
7ba81444 MS |
502 | /* If STORAGE was provided, it had better be in the same objfile |
503 | as TYPE. Otherwise, we can't link it into TYPE's cv chain: | |
504 | if one objfile is freed and the other kept, we'd have | |
505 | dangling pointers. */ | |
ad766c0a JB |
506 | gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage)); |
507 | ||
2fdde8f8 DJ |
508 | ntype = storage; |
509 | TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type); | |
510 | TYPE_CHAIN (ntype) = ntype; | |
511 | } | |
47663de5 MS |
512 | |
513 | /* Pointers or references to the original type are not relevant to | |
2fdde8f8 | 514 | the new type. */ |
47663de5 MS |
515 | TYPE_POINTER_TYPE (ntype) = (struct type *) 0; |
516 | TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0; | |
47663de5 | 517 | |
2fdde8f8 DJ |
518 | /* Chain the new qualified type to the old type. */ |
519 | TYPE_CHAIN (ntype) = TYPE_CHAIN (type); | |
520 | TYPE_CHAIN (type) = ntype; | |
521 | ||
522 | /* Now set the instance flags and return the new type. */ | |
523 | TYPE_INSTANCE_FLAGS (ntype) = new_flags; | |
47663de5 | 524 | |
ab5d3da6 KB |
525 | /* Set length of new type to that of the original type. */ |
526 | TYPE_LENGTH (ntype) = TYPE_LENGTH (type); | |
527 | ||
47663de5 MS |
528 | return ntype; |
529 | } | |
530 | ||
2fdde8f8 DJ |
531 | /* Make an address-space-delimited variant of a type -- a type that |
532 | is identical to the one supplied except that it has an address | |
533 | space attribute attached to it (such as "code" or "data"). | |
534 | ||
7ba81444 MS |
535 | The space attributes "code" and "data" are for Harvard |
536 | architectures. The address space attributes are for architectures | |
537 | which have alternately sized pointers or pointers with alternate | |
538 | representations. */ | |
2fdde8f8 DJ |
539 | |
540 | struct type * | |
541 | make_type_with_address_space (struct type *type, int space_flag) | |
542 | { | |
543 | struct type *ntype; | |
544 | int new_flags = ((TYPE_INSTANCE_FLAGS (type) | |
876cecd0 TT |
545 | & ~(TYPE_INSTANCE_FLAG_CODE_SPACE |
546 | | TYPE_INSTANCE_FLAG_DATA_SPACE | |
547 | | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)) | |
2fdde8f8 DJ |
548 | | space_flag); |
549 | ||
550 | return make_qualified_type (type, new_flags, NULL); | |
551 | } | |
c906108c SS |
552 | |
553 | /* Make a "c-v" variant of a type -- a type that is identical to the | |
554 | one supplied except that it may have const or volatile attributes | |
555 | CNST is a flag for setting the const attribute | |
556 | VOLTL is a flag for setting the volatile attribute | |
557 | TYPE is the base type whose variant we are creating. | |
c906108c | 558 | |
ad766c0a JB |
559 | If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to |
560 | storage to hold the new qualified type; *TYPEPTR and TYPE must be | |
561 | in the same objfile. Otherwise, allocate fresh memory for the new | |
562 | type whereever TYPE lives. If TYPEPTR is non-zero, set it to the | |
563 | new type we construct. */ | |
c906108c | 564 | struct type * |
7ba81444 MS |
565 | make_cv_type (int cnst, int voltl, |
566 | struct type *type, | |
567 | struct type **typeptr) | |
c906108c | 568 | { |
52f0bd74 AC |
569 | struct type *ntype; /* New type */ |
570 | struct type *tmp_type = type; /* tmp type */ | |
c906108c SS |
571 | struct objfile *objfile; |
572 | ||
2fdde8f8 | 573 | int new_flags = (TYPE_INSTANCE_FLAGS (type) |
876cecd0 | 574 | & ~(TYPE_INSTANCE_FLAG_CONST | TYPE_INSTANCE_FLAG_VOLATILE)); |
c906108c | 575 | |
c906108c | 576 | if (cnst) |
876cecd0 | 577 | new_flags |= TYPE_INSTANCE_FLAG_CONST; |
c906108c SS |
578 | |
579 | if (voltl) | |
876cecd0 | 580 | new_flags |= TYPE_INSTANCE_FLAG_VOLATILE; |
a02fd225 | 581 | |
2fdde8f8 | 582 | if (typeptr && *typeptr != NULL) |
a02fd225 | 583 | { |
ad766c0a JB |
584 | /* TYPE and *TYPEPTR must be in the same objfile. We can't have |
585 | a C-V variant chain that threads across objfiles: if one | |
586 | objfile gets freed, then the other has a broken C-V chain. | |
587 | ||
588 | This code used to try to copy over the main type from TYPE to | |
589 | *TYPEPTR if they were in different objfiles, but that's | |
590 | wrong, too: TYPE may have a field list or member function | |
591 | lists, which refer to types of their own, etc. etc. The | |
592 | whole shebang would need to be copied over recursively; you | |
593 | can't have inter-objfile pointers. The only thing to do is | |
594 | to leave stub types as stub types, and look them up afresh by | |
595 | name each time you encounter them. */ | |
596 | gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type)); | |
2fdde8f8 DJ |
597 | } |
598 | ||
7ba81444 MS |
599 | ntype = make_qualified_type (type, new_flags, |
600 | typeptr ? *typeptr : NULL); | |
c906108c | 601 | |
2fdde8f8 DJ |
602 | if (typeptr != NULL) |
603 | *typeptr = ntype; | |
a02fd225 | 604 | |
2fdde8f8 | 605 | return ntype; |
a02fd225 | 606 | } |
c906108c | 607 | |
2fdde8f8 DJ |
608 | /* Replace the contents of ntype with the type *type. This changes the |
609 | contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus | |
610 | the changes are propogated to all types in the TYPE_CHAIN. | |
dd6bda65 | 611 | |
cda6c68a JB |
612 | In order to build recursive types, it's inevitable that we'll need |
613 | to update types in place --- but this sort of indiscriminate | |
614 | smashing is ugly, and needs to be replaced with something more | |
2fdde8f8 DJ |
615 | controlled. TYPE_MAIN_TYPE is a step in this direction; it's not |
616 | clear if more steps are needed. */ | |
dd6bda65 DJ |
617 | void |
618 | replace_type (struct type *ntype, struct type *type) | |
619 | { | |
ab5d3da6 | 620 | struct type *chain; |
dd6bda65 | 621 | |
ad766c0a JB |
622 | /* These two types had better be in the same objfile. Otherwise, |
623 | the assignment of one type's main type structure to the other | |
624 | will produce a type with references to objects (names; field | |
625 | lists; etc.) allocated on an objfile other than its own. */ | |
626 | gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (ntype)); | |
627 | ||
2fdde8f8 | 628 | *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type); |
dd6bda65 | 629 | |
7ba81444 MS |
630 | /* The type length is not a part of the main type. Update it for |
631 | each type on the variant chain. */ | |
ab5d3da6 | 632 | chain = ntype; |
5f61c20e JK |
633 | do |
634 | { | |
635 | /* Assert that this element of the chain has no address-class bits | |
636 | set in its flags. Such type variants might have type lengths | |
637 | which are supposed to be different from the non-address-class | |
638 | variants. This assertion shouldn't ever be triggered because | |
639 | symbol readers which do construct address-class variants don't | |
640 | call replace_type(). */ | |
641 | gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0); | |
642 | ||
643 | TYPE_LENGTH (chain) = TYPE_LENGTH (type); | |
644 | chain = TYPE_CHAIN (chain); | |
645 | } | |
646 | while (ntype != chain); | |
ab5d3da6 | 647 | |
2fdde8f8 DJ |
648 | /* Assert that the two types have equivalent instance qualifiers. |
649 | This should be true for at least all of our debug readers. */ | |
650 | gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type)); | |
dd6bda65 DJ |
651 | } |
652 | ||
c906108c SS |
653 | /* Implement direct support for MEMBER_TYPE in GNU C++. |
654 | May need to construct such a type if this is the first use. | |
655 | The TYPE is the type of the member. The DOMAIN is the type | |
656 | of the aggregate that the member belongs to. */ | |
657 | ||
658 | struct type * | |
0d5de010 | 659 | lookup_memberptr_type (struct type *type, struct type *domain) |
c906108c | 660 | { |
52f0bd74 | 661 | struct type *mtype; |
c906108c | 662 | |
e9bb382b | 663 | mtype = alloc_type_copy (type); |
0d5de010 | 664 | smash_to_memberptr_type (mtype, domain, type); |
c16abbde | 665 | return mtype; |
c906108c SS |
666 | } |
667 | ||
0d5de010 DJ |
668 | /* Return a pointer-to-method type, for a method of type TO_TYPE. */ |
669 | ||
670 | struct type * | |
671 | lookup_methodptr_type (struct type *to_type) | |
672 | { | |
673 | struct type *mtype; | |
674 | ||
e9bb382b | 675 | mtype = alloc_type_copy (to_type); |
0b92b5bb | 676 | smash_to_methodptr_type (mtype, to_type); |
0d5de010 DJ |
677 | return mtype; |
678 | } | |
679 | ||
7ba81444 MS |
680 | /* Allocate a stub method whose return type is TYPE. This apparently |
681 | happens for speed of symbol reading, since parsing out the | |
682 | arguments to the method is cpu-intensive, the way we are doing it. | |
683 | So, we will fill in arguments later. This always returns a fresh | |
684 | type. */ | |
c906108c SS |
685 | |
686 | struct type * | |
fba45db2 | 687 | allocate_stub_method (struct type *type) |
c906108c SS |
688 | { |
689 | struct type *mtype; | |
690 | ||
e9bb382b UW |
691 | mtype = alloc_type_copy (type); |
692 | TYPE_CODE (mtype) = TYPE_CODE_METHOD; | |
693 | TYPE_LENGTH (mtype) = 1; | |
694 | TYPE_STUB (mtype) = 1; | |
c906108c SS |
695 | TYPE_TARGET_TYPE (mtype) = type; |
696 | /* _DOMAIN_TYPE (mtype) = unknown yet */ | |
c16abbde | 697 | return mtype; |
c906108c SS |
698 | } |
699 | ||
7ba81444 MS |
700 | /* Create a range type using either a blank type supplied in |
701 | RESULT_TYPE, or creating a new type, inheriting the objfile from | |
702 | INDEX_TYPE. | |
c906108c | 703 | |
7ba81444 MS |
704 | Indices will be of type INDEX_TYPE, and will range from LOW_BOUND |
705 | to HIGH_BOUND, inclusive. | |
c906108c | 706 | |
7ba81444 MS |
707 | FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make |
708 | sure it is TYPE_CODE_UNDEF before we bash it into a range type? */ | |
c906108c SS |
709 | |
710 | struct type * | |
fba45db2 | 711 | create_range_type (struct type *result_type, struct type *index_type, |
43bbcdc2 | 712 | LONGEST low_bound, LONGEST high_bound) |
c906108c SS |
713 | { |
714 | if (result_type == NULL) | |
e9bb382b | 715 | result_type = alloc_type_copy (index_type); |
c906108c SS |
716 | TYPE_CODE (result_type) = TYPE_CODE_RANGE; |
717 | TYPE_TARGET_TYPE (result_type) = index_type; | |
74a9bb82 | 718 | if (TYPE_STUB (index_type)) |
876cecd0 | 719 | TYPE_TARGET_STUB (result_type) = 1; |
c906108c SS |
720 | else |
721 | TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type)); | |
43bbcdc2 PH |
722 | TYPE_RANGE_DATA (result_type) = (struct range_bounds *) |
723 | TYPE_ZALLOC (result_type, sizeof (struct range_bounds)); | |
262452ec JK |
724 | TYPE_LOW_BOUND (result_type) = low_bound; |
725 | TYPE_HIGH_BOUND (result_type) = high_bound; | |
c906108c | 726 | |
c5aa993b | 727 | if (low_bound >= 0) |
876cecd0 | 728 | TYPE_UNSIGNED (result_type) = 1; |
c906108c | 729 | |
262452ec | 730 | return result_type; |
c906108c SS |
731 | } |
732 | ||
7ba81444 MS |
733 | /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type |
734 | TYPE. Return 1 if type is a range type, 0 if it is discrete (and | |
735 | bounds will fit in LONGEST), or -1 otherwise. */ | |
c906108c SS |
736 | |
737 | int | |
fba45db2 | 738 | get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp) |
c906108c SS |
739 | { |
740 | CHECK_TYPEDEF (type); | |
741 | switch (TYPE_CODE (type)) | |
742 | { | |
743 | case TYPE_CODE_RANGE: | |
744 | *lowp = TYPE_LOW_BOUND (type); | |
745 | *highp = TYPE_HIGH_BOUND (type); | |
746 | return 1; | |
747 | case TYPE_CODE_ENUM: | |
748 | if (TYPE_NFIELDS (type) > 0) | |
749 | { | |
750 | /* The enums may not be sorted by value, so search all | |
751 | entries */ | |
752 | int i; | |
753 | ||
754 | *lowp = *highp = TYPE_FIELD_BITPOS (type, 0); | |
755 | for (i = 0; i < TYPE_NFIELDS (type); i++) | |
756 | { | |
757 | if (TYPE_FIELD_BITPOS (type, i) < *lowp) | |
758 | *lowp = TYPE_FIELD_BITPOS (type, i); | |
759 | if (TYPE_FIELD_BITPOS (type, i) > *highp) | |
760 | *highp = TYPE_FIELD_BITPOS (type, i); | |
761 | } | |
762 | ||
7ba81444 | 763 | /* Set unsigned indicator if warranted. */ |
c5aa993b | 764 | if (*lowp >= 0) |
c906108c | 765 | { |
876cecd0 | 766 | TYPE_UNSIGNED (type) = 1; |
c906108c SS |
767 | } |
768 | } | |
769 | else | |
770 | { | |
771 | *lowp = 0; | |
772 | *highp = -1; | |
773 | } | |
774 | return 0; | |
775 | case TYPE_CODE_BOOL: | |
776 | *lowp = 0; | |
777 | *highp = 1; | |
778 | return 0; | |
779 | case TYPE_CODE_INT: | |
c5aa993b | 780 | if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */ |
c906108c SS |
781 | return -1; |
782 | if (!TYPE_UNSIGNED (type)) | |
783 | { | |
c5aa993b | 784 | *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1)); |
c906108c SS |
785 | *highp = -*lowp - 1; |
786 | return 0; | |
787 | } | |
7ba81444 | 788 | /* ... fall through for unsigned ints ... */ |
c906108c SS |
789 | case TYPE_CODE_CHAR: |
790 | *lowp = 0; | |
791 | /* This round-about calculation is to avoid shifting by | |
7b83ea04 | 792 | TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work |
7ba81444 | 793 | if TYPE_LENGTH (type) == sizeof (LONGEST). */ |
c906108c SS |
794 | *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1); |
795 | *highp = (*highp - 1) | *highp; | |
796 | return 0; | |
797 | default: | |
798 | return -1; | |
799 | } | |
800 | } | |
801 | ||
7ba81444 MS |
802 | /* Create an array type using either a blank type supplied in |
803 | RESULT_TYPE, or creating a new type, inheriting the objfile from | |
804 | RANGE_TYPE. | |
c906108c SS |
805 | |
806 | Elements will be of type ELEMENT_TYPE, the indices will be of type | |
807 | RANGE_TYPE. | |
808 | ||
7ba81444 MS |
809 | FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make |
810 | sure it is TYPE_CODE_UNDEF before we bash it into an array | |
811 | type? */ | |
c906108c SS |
812 | |
813 | struct type * | |
7ba81444 MS |
814 | create_array_type (struct type *result_type, |
815 | struct type *element_type, | |
fba45db2 | 816 | struct type *range_type) |
c906108c SS |
817 | { |
818 | LONGEST low_bound, high_bound; | |
819 | ||
820 | if (result_type == NULL) | |
e9bb382b UW |
821 | result_type = alloc_type_copy (range_type); |
822 | ||
c906108c SS |
823 | TYPE_CODE (result_type) = TYPE_CODE_ARRAY; |
824 | TYPE_TARGET_TYPE (result_type) = element_type; | |
825 | if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0) | |
826 | low_bound = high_bound = 0; | |
827 | CHECK_TYPEDEF (element_type); | |
ab0d6e0d JB |
828 | /* Be careful when setting the array length. Ada arrays can be |
829 | empty arrays with the high_bound being smaller than the low_bound. | |
830 | In such cases, the array length should be zero. */ | |
831 | if (high_bound < low_bound) | |
832 | TYPE_LENGTH (result_type) = 0; | |
833 | else | |
834 | TYPE_LENGTH (result_type) = | |
835 | TYPE_LENGTH (element_type) * (high_bound - low_bound + 1); | |
c906108c SS |
836 | TYPE_NFIELDS (result_type) = 1; |
837 | TYPE_FIELDS (result_type) = | |
1deafd4e | 838 | (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field)); |
262452ec | 839 | TYPE_INDEX_TYPE (result_type) = range_type; |
c906108c SS |
840 | TYPE_VPTR_FIELDNO (result_type) = -1; |
841 | ||
842 | /* TYPE_FLAG_TARGET_STUB will take care of zero length arrays */ | |
843 | if (TYPE_LENGTH (result_type) == 0) | |
876cecd0 | 844 | TYPE_TARGET_STUB (result_type) = 1; |
c906108c | 845 | |
c16abbde | 846 | return result_type; |
c906108c SS |
847 | } |
848 | ||
e3506a9f UW |
849 | struct type * |
850 | lookup_array_range_type (struct type *element_type, | |
851 | int low_bound, int high_bound) | |
852 | { | |
50810684 | 853 | struct gdbarch *gdbarch = get_type_arch (element_type); |
e3506a9f UW |
854 | struct type *index_type = builtin_type (gdbarch)->builtin_int; |
855 | struct type *range_type | |
856 | = create_range_type (NULL, index_type, low_bound, high_bound); | |
857 | return create_array_type (NULL, element_type, range_type); | |
858 | } | |
859 | ||
7ba81444 MS |
860 | /* Create a string type using either a blank type supplied in |
861 | RESULT_TYPE, or creating a new type. String types are similar | |
862 | enough to array of char types that we can use create_array_type to | |
863 | build the basic type and then bash it into a string type. | |
c906108c SS |
864 | |
865 | For fixed length strings, the range type contains 0 as the lower | |
866 | bound and the length of the string minus one as the upper bound. | |
867 | ||
7ba81444 MS |
868 | FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make |
869 | sure it is TYPE_CODE_UNDEF before we bash it into a string | |
870 | type? */ | |
c906108c SS |
871 | |
872 | struct type * | |
3b7538c0 UW |
873 | create_string_type (struct type *result_type, |
874 | struct type *string_char_type, | |
7ba81444 | 875 | struct type *range_type) |
c906108c SS |
876 | { |
877 | result_type = create_array_type (result_type, | |
f290d38e | 878 | string_char_type, |
c906108c SS |
879 | range_type); |
880 | TYPE_CODE (result_type) = TYPE_CODE_STRING; | |
c16abbde | 881 | return result_type; |
c906108c SS |
882 | } |
883 | ||
e3506a9f UW |
884 | struct type * |
885 | lookup_string_range_type (struct type *string_char_type, | |
886 | int low_bound, int high_bound) | |
887 | { | |
888 | struct type *result_type; | |
889 | result_type = lookup_array_range_type (string_char_type, | |
890 | low_bound, high_bound); | |
891 | TYPE_CODE (result_type) = TYPE_CODE_STRING; | |
892 | return result_type; | |
893 | } | |
894 | ||
c906108c | 895 | struct type * |
fba45db2 | 896 | create_set_type (struct type *result_type, struct type *domain_type) |
c906108c | 897 | { |
c906108c | 898 | if (result_type == NULL) |
e9bb382b UW |
899 | result_type = alloc_type_copy (domain_type); |
900 | ||
c906108c SS |
901 | TYPE_CODE (result_type) = TYPE_CODE_SET; |
902 | TYPE_NFIELDS (result_type) = 1; | |
1deafd4e | 903 | TYPE_FIELDS (result_type) = TYPE_ZALLOC (result_type, sizeof (struct field)); |
c906108c | 904 | |
74a9bb82 | 905 | if (!TYPE_STUB (domain_type)) |
c906108c | 906 | { |
f9780d5b | 907 | LONGEST low_bound, high_bound, bit_length; |
c906108c SS |
908 | if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0) |
909 | low_bound = high_bound = 0; | |
910 | bit_length = high_bound - low_bound + 1; | |
911 | TYPE_LENGTH (result_type) | |
912 | = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT; | |
f9780d5b | 913 | if (low_bound >= 0) |
876cecd0 | 914 | TYPE_UNSIGNED (result_type) = 1; |
c906108c SS |
915 | } |
916 | TYPE_FIELD_TYPE (result_type, 0) = domain_type; | |
917 | ||
c16abbde | 918 | return result_type; |
c906108c SS |
919 | } |
920 | ||
ea37ba09 DJ |
921 | /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE |
922 | and any array types nested inside it. */ | |
923 | ||
924 | void | |
925 | make_vector_type (struct type *array_type) | |
926 | { | |
927 | struct type *inner_array, *elt_type; | |
928 | int flags; | |
929 | ||
930 | /* Find the innermost array type, in case the array is | |
931 | multi-dimensional. */ | |
932 | inner_array = array_type; | |
933 | while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY) | |
934 | inner_array = TYPE_TARGET_TYPE (inner_array); | |
935 | ||
936 | elt_type = TYPE_TARGET_TYPE (inner_array); | |
937 | if (TYPE_CODE (elt_type) == TYPE_CODE_INT) | |
938 | { | |
939 | flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_FLAG_NOTTEXT; | |
940 | elt_type = make_qualified_type (elt_type, flags, NULL); | |
941 | TYPE_TARGET_TYPE (inner_array) = elt_type; | |
942 | } | |
943 | ||
876cecd0 | 944 | TYPE_VECTOR (array_type) = 1; |
ea37ba09 DJ |
945 | } |
946 | ||
794ac428 | 947 | struct type * |
ac3aafc7 EZ |
948 | init_vector_type (struct type *elt_type, int n) |
949 | { | |
950 | struct type *array_type; | |
e3506a9f | 951 | array_type = lookup_array_range_type (elt_type, 0, n - 1); |
ea37ba09 | 952 | make_vector_type (array_type); |
ac3aafc7 EZ |
953 | return array_type; |
954 | } | |
955 | ||
0d5de010 DJ |
956 | /* Smash TYPE to be a type of pointers to members of DOMAIN with type |
957 | TO_TYPE. A member pointer is a wierd thing -- it amounts to a | |
958 | typed offset into a struct, e.g. "an int at offset 8". A MEMBER | |
959 | TYPE doesn't include the offset (that's the value of the MEMBER | |
960 | itself), but does include the structure type into which it points | |
961 | (for some reason). | |
c906108c | 962 | |
7ba81444 MS |
963 | When "smashing" the type, we preserve the objfile that the old type |
964 | pointed to, since we aren't changing where the type is actually | |
c906108c SS |
965 | allocated. */ |
966 | ||
967 | void | |
0d5de010 DJ |
968 | smash_to_memberptr_type (struct type *type, struct type *domain, |
969 | struct type *to_type) | |
c906108c | 970 | { |
2fdde8f8 | 971 | smash_type (type); |
c906108c SS |
972 | TYPE_TARGET_TYPE (type) = to_type; |
973 | TYPE_DOMAIN_TYPE (type) = domain; | |
0d5de010 DJ |
974 | /* Assume that a data member pointer is the same size as a normal |
975 | pointer. */ | |
50810684 UW |
976 | TYPE_LENGTH (type) |
977 | = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT; | |
0d5de010 | 978 | TYPE_CODE (type) = TYPE_CODE_MEMBERPTR; |
c906108c SS |
979 | } |
980 | ||
0b92b5bb TT |
981 | /* Smash TYPE to be a type of pointer to methods type TO_TYPE. |
982 | ||
983 | When "smashing" the type, we preserve the objfile that the old type | |
984 | pointed to, since we aren't changing where the type is actually | |
985 | allocated. */ | |
986 | ||
987 | void | |
988 | smash_to_methodptr_type (struct type *type, struct type *to_type) | |
989 | { | |
990 | smash_type (type); | |
991 | TYPE_TARGET_TYPE (type) = to_type; | |
992 | TYPE_DOMAIN_TYPE (type) = TYPE_DOMAIN_TYPE (to_type); | |
993 | TYPE_LENGTH (type) = cplus_method_ptr_size (to_type); | |
994 | TYPE_CODE (type) = TYPE_CODE_METHODPTR; | |
995 | } | |
996 | ||
c906108c SS |
997 | /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE. |
998 | METHOD just means `function that gets an extra "this" argument'. | |
999 | ||
7ba81444 MS |
1000 | When "smashing" the type, we preserve the objfile that the old type |
1001 | pointed to, since we aren't changing where the type is actually | |
c906108c SS |
1002 | allocated. */ |
1003 | ||
1004 | void | |
fba45db2 | 1005 | smash_to_method_type (struct type *type, struct type *domain, |
ad2f7632 DJ |
1006 | struct type *to_type, struct field *args, |
1007 | int nargs, int varargs) | |
c906108c | 1008 | { |
2fdde8f8 | 1009 | smash_type (type); |
c906108c SS |
1010 | TYPE_TARGET_TYPE (type) = to_type; |
1011 | TYPE_DOMAIN_TYPE (type) = domain; | |
ad2f7632 DJ |
1012 | TYPE_FIELDS (type) = args; |
1013 | TYPE_NFIELDS (type) = nargs; | |
1014 | if (varargs) | |
876cecd0 | 1015 | TYPE_VARARGS (type) = 1; |
c906108c SS |
1016 | TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */ |
1017 | TYPE_CODE (type) = TYPE_CODE_METHOD; | |
1018 | } | |
1019 | ||
1020 | /* Return a typename for a struct/union/enum type without "struct ", | |
1021 | "union ", or "enum ". If the type has a NULL name, return NULL. */ | |
1022 | ||
1023 | char * | |
aa1ee363 | 1024 | type_name_no_tag (const struct type *type) |
c906108c SS |
1025 | { |
1026 | if (TYPE_TAG_NAME (type) != NULL) | |
1027 | return TYPE_TAG_NAME (type); | |
1028 | ||
7ba81444 MS |
1029 | /* Is there code which expects this to return the name if there is |
1030 | no tag name? My guess is that this is mainly used for C++ in | |
1031 | cases where the two will always be the same. */ | |
c906108c SS |
1032 | return TYPE_NAME (type); |
1033 | } | |
1034 | ||
7ba81444 MS |
1035 | /* Lookup a typedef or primitive type named NAME, visible in lexical |
1036 | block BLOCK. If NOERR is nonzero, return zero if NAME is not | |
1037 | suitably defined. */ | |
c906108c SS |
1038 | |
1039 | struct type * | |
e6c014f2 UW |
1040 | lookup_typename (const struct language_defn *language, |
1041 | struct gdbarch *gdbarch, char *name, | |
1042 | struct block *block, int noerr) | |
c906108c | 1043 | { |
52f0bd74 AC |
1044 | struct symbol *sym; |
1045 | struct type *tmp; | |
c906108c | 1046 | |
2570f2b7 | 1047 | sym = lookup_symbol (name, block, VAR_DOMAIN, 0); |
c906108c SS |
1048 | if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF) |
1049 | { | |
e6c014f2 | 1050 | tmp = language_lookup_primitive_type_by_name (language, gdbarch, name); |
c906108c SS |
1051 | if (tmp) |
1052 | { | |
c16abbde | 1053 | return tmp; |
c906108c SS |
1054 | } |
1055 | else if (!tmp && noerr) | |
1056 | { | |
c16abbde | 1057 | return NULL; |
c906108c SS |
1058 | } |
1059 | else | |
1060 | { | |
8a3fe4f8 | 1061 | error (_("No type named %s."), name); |
c906108c SS |
1062 | } |
1063 | } | |
1064 | return (SYMBOL_TYPE (sym)); | |
1065 | } | |
1066 | ||
1067 | struct type * | |
e6c014f2 UW |
1068 | lookup_unsigned_typename (const struct language_defn *language, |
1069 | struct gdbarch *gdbarch, char *name) | |
c906108c SS |
1070 | { |
1071 | char *uns = alloca (strlen (name) + 10); | |
1072 | ||
1073 | strcpy (uns, "unsigned "); | |
1074 | strcpy (uns + 9, name); | |
e6c014f2 | 1075 | return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0); |
c906108c SS |
1076 | } |
1077 | ||
1078 | struct type * | |
e6c014f2 UW |
1079 | lookup_signed_typename (const struct language_defn *language, |
1080 | struct gdbarch *gdbarch, char *name) | |
c906108c SS |
1081 | { |
1082 | struct type *t; | |
1083 | char *uns = alloca (strlen (name) + 8); | |
1084 | ||
1085 | strcpy (uns, "signed "); | |
1086 | strcpy (uns + 7, name); | |
e6c014f2 | 1087 | t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1); |
7ba81444 | 1088 | /* If we don't find "signed FOO" just try again with plain "FOO". */ |
c906108c SS |
1089 | if (t != NULL) |
1090 | return t; | |
e6c014f2 | 1091 | return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0); |
c906108c SS |
1092 | } |
1093 | ||
1094 | /* Lookup a structure type named "struct NAME", | |
1095 | visible in lexical block BLOCK. */ | |
1096 | ||
1097 | struct type * | |
fba45db2 | 1098 | lookup_struct (char *name, struct block *block) |
c906108c | 1099 | { |
52f0bd74 | 1100 | struct symbol *sym; |
c906108c | 1101 | |
2570f2b7 | 1102 | sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0); |
c906108c SS |
1103 | |
1104 | if (sym == NULL) | |
1105 | { | |
8a3fe4f8 | 1106 | error (_("No struct type named %s."), name); |
c906108c SS |
1107 | } |
1108 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT) | |
1109 | { | |
7ba81444 MS |
1110 | error (_("This context has class, union or enum %s, not a struct."), |
1111 | name); | |
c906108c SS |
1112 | } |
1113 | return (SYMBOL_TYPE (sym)); | |
1114 | } | |
1115 | ||
1116 | /* Lookup a union type named "union NAME", | |
1117 | visible in lexical block BLOCK. */ | |
1118 | ||
1119 | struct type * | |
fba45db2 | 1120 | lookup_union (char *name, struct block *block) |
c906108c | 1121 | { |
52f0bd74 | 1122 | struct symbol *sym; |
c5aa993b | 1123 | struct type *t; |
c906108c | 1124 | |
2570f2b7 | 1125 | sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0); |
c906108c SS |
1126 | |
1127 | if (sym == NULL) | |
8a3fe4f8 | 1128 | error (_("No union type named %s."), name); |
c906108c | 1129 | |
c5aa993b | 1130 | t = SYMBOL_TYPE (sym); |
c906108c SS |
1131 | |
1132 | if (TYPE_CODE (t) == TYPE_CODE_UNION) | |
c16abbde | 1133 | return t; |
c906108c | 1134 | |
7ba81444 MS |
1135 | /* If we get here, it's not a union. */ |
1136 | error (_("This context has class, struct or enum %s, not a union."), | |
1137 | name); | |
c906108c SS |
1138 | } |
1139 | ||
1140 | ||
1141 | /* Lookup an enum type named "enum NAME", | |
1142 | visible in lexical block BLOCK. */ | |
1143 | ||
1144 | struct type * | |
fba45db2 | 1145 | lookup_enum (char *name, struct block *block) |
c906108c | 1146 | { |
52f0bd74 | 1147 | struct symbol *sym; |
c906108c | 1148 | |
2570f2b7 | 1149 | sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0); |
c906108c SS |
1150 | if (sym == NULL) |
1151 | { | |
8a3fe4f8 | 1152 | error (_("No enum type named %s."), name); |
c906108c SS |
1153 | } |
1154 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM) | |
1155 | { | |
7ba81444 MS |
1156 | error (_("This context has class, struct or union %s, not an enum."), |
1157 | name); | |
c906108c SS |
1158 | } |
1159 | return (SYMBOL_TYPE (sym)); | |
1160 | } | |
1161 | ||
1162 | /* Lookup a template type named "template NAME<TYPE>", | |
1163 | visible in lexical block BLOCK. */ | |
1164 | ||
1165 | struct type * | |
7ba81444 MS |
1166 | lookup_template_type (char *name, struct type *type, |
1167 | struct block *block) | |
c906108c SS |
1168 | { |
1169 | struct symbol *sym; | |
7ba81444 MS |
1170 | char *nam = (char *) |
1171 | alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4); | |
c906108c SS |
1172 | strcpy (nam, name); |
1173 | strcat (nam, "<"); | |
0004e5a2 | 1174 | strcat (nam, TYPE_NAME (type)); |
7ba81444 | 1175 | strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */ |
c906108c | 1176 | |
2570f2b7 | 1177 | sym = lookup_symbol (nam, block, VAR_DOMAIN, 0); |
c906108c SS |
1178 | |
1179 | if (sym == NULL) | |
1180 | { | |
8a3fe4f8 | 1181 | error (_("No template type named %s."), name); |
c906108c SS |
1182 | } |
1183 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT) | |
1184 | { | |
7ba81444 MS |
1185 | error (_("This context has class, union or enum %s, not a struct."), |
1186 | name); | |
c906108c SS |
1187 | } |
1188 | return (SYMBOL_TYPE (sym)); | |
1189 | } | |
1190 | ||
7ba81444 MS |
1191 | /* Given a type TYPE, lookup the type of the component of type named |
1192 | NAME. | |
c906108c | 1193 | |
7ba81444 MS |
1194 | TYPE can be either a struct or union, or a pointer or reference to |
1195 | a struct or union. If it is a pointer or reference, its target | |
1196 | type is automatically used. Thus '.' and '->' are interchangable, | |
1197 | as specified for the definitions of the expression element types | |
1198 | STRUCTOP_STRUCT and STRUCTOP_PTR. | |
c906108c SS |
1199 | |
1200 | If NOERR is nonzero, return zero if NAME is not suitably defined. | |
1201 | If NAME is the name of a baseclass type, return that type. */ | |
1202 | ||
1203 | struct type * | |
fba45db2 | 1204 | lookup_struct_elt_type (struct type *type, char *name, int noerr) |
c906108c SS |
1205 | { |
1206 | int i; | |
1207 | ||
1208 | for (;;) | |
1209 | { | |
1210 | CHECK_TYPEDEF (type); | |
1211 | if (TYPE_CODE (type) != TYPE_CODE_PTR | |
1212 | && TYPE_CODE (type) != TYPE_CODE_REF) | |
1213 | break; | |
1214 | type = TYPE_TARGET_TYPE (type); | |
1215 | } | |
1216 | ||
687d6395 MS |
1217 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT |
1218 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
c906108c SS |
1219 | { |
1220 | target_terminal_ours (); | |
1221 | gdb_flush (gdb_stdout); | |
1222 | fprintf_unfiltered (gdb_stderr, "Type "); | |
1223 | type_print (type, "", gdb_stderr, -1); | |
8a3fe4f8 | 1224 | error (_(" is not a structure or union type.")); |
c906108c SS |
1225 | } |
1226 | ||
1227 | #if 0 | |
7ba81444 MS |
1228 | /* FIXME: This change put in by Michael seems incorrect for the case |
1229 | where the structure tag name is the same as the member name. | |
1230 | I.E. when doing "ptype bell->bar" for "struct foo { int bar; int | |
1231 | foo; } bell;" Disabled by fnf. */ | |
c906108c SS |
1232 | { |
1233 | char *typename; | |
1234 | ||
1235 | typename = type_name_no_tag (type); | |
762f08a3 | 1236 | if (typename != NULL && strcmp (typename, name) == 0) |
c906108c SS |
1237 | return type; |
1238 | } | |
1239 | #endif | |
1240 | ||
1241 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
1242 | { | |
1243 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
1244 | ||
db577aea | 1245 | if (t_field_name && (strcmp_iw (t_field_name, name) == 0)) |
c906108c SS |
1246 | { |
1247 | return TYPE_FIELD_TYPE (type, i); | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | /* OK, it's not in this class. Recursively check the baseclasses. */ | |
1252 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1253 | { | |
1254 | struct type *t; | |
1255 | ||
9733fc94 | 1256 | t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1); |
c906108c SS |
1257 | if (t != NULL) |
1258 | { | |
1259 | return t; | |
1260 | } | |
1261 | } | |
1262 | ||
1263 | if (noerr) | |
1264 | { | |
1265 | return NULL; | |
1266 | } | |
c5aa993b | 1267 | |
c906108c SS |
1268 | target_terminal_ours (); |
1269 | gdb_flush (gdb_stdout); | |
1270 | fprintf_unfiltered (gdb_stderr, "Type "); | |
1271 | type_print (type, "", gdb_stderr, -1); | |
1272 | fprintf_unfiltered (gdb_stderr, " has no component named "); | |
1273 | fputs_filtered (name, gdb_stderr); | |
8a3fe4f8 | 1274 | error ((".")); |
c5aa993b | 1275 | return (struct type *) -1; /* For lint */ |
c906108c SS |
1276 | } |
1277 | ||
81fe8080 DE |
1278 | /* Lookup the vptr basetype/fieldno values for TYPE. |
1279 | If found store vptr_basetype in *BASETYPEP if non-NULL, and return | |
1280 | vptr_fieldno. Also, if found and basetype is from the same objfile, | |
1281 | cache the results. | |
1282 | If not found, return -1 and ignore BASETYPEP. | |
1283 | Callers should be aware that in some cases (for example, | |
c906108c | 1284 | the type or one of its baseclasses is a stub type and we are |
d48cc9dd DJ |
1285 | debugging a .o file, or the compiler uses DWARF-2 and is not GCC), |
1286 | this function will not be able to find the | |
7ba81444 | 1287 | virtual function table pointer, and vptr_fieldno will remain -1 and |
81fe8080 | 1288 | vptr_basetype will remain NULL or incomplete. */ |
c906108c | 1289 | |
81fe8080 DE |
1290 | int |
1291 | get_vptr_fieldno (struct type *type, struct type **basetypep) | |
c906108c SS |
1292 | { |
1293 | CHECK_TYPEDEF (type); | |
1294 | ||
1295 | if (TYPE_VPTR_FIELDNO (type) < 0) | |
1296 | { | |
1297 | int i; | |
1298 | ||
7ba81444 MS |
1299 | /* We must start at zero in case the first (and only) baseclass |
1300 | is virtual (and hence we cannot share the table pointer). */ | |
c906108c SS |
1301 | for (i = 0; i < TYPE_N_BASECLASSES (type); i++) |
1302 | { | |
81fe8080 DE |
1303 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); |
1304 | int fieldno; | |
1305 | struct type *basetype; | |
1306 | ||
1307 | fieldno = get_vptr_fieldno (baseclass, &basetype); | |
1308 | if (fieldno >= 0) | |
c906108c | 1309 | { |
81fe8080 DE |
1310 | /* If the type comes from a different objfile we can't cache |
1311 | it, it may have a different lifetime. PR 2384 */ | |
5ef73790 | 1312 | if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype)) |
81fe8080 DE |
1313 | { |
1314 | TYPE_VPTR_FIELDNO (type) = fieldno; | |
1315 | TYPE_VPTR_BASETYPE (type) = basetype; | |
1316 | } | |
1317 | if (basetypep) | |
1318 | *basetypep = basetype; | |
1319 | return fieldno; | |
c906108c SS |
1320 | } |
1321 | } | |
81fe8080 DE |
1322 | |
1323 | /* Not found. */ | |
1324 | return -1; | |
1325 | } | |
1326 | else | |
1327 | { | |
1328 | if (basetypep) | |
1329 | *basetypep = TYPE_VPTR_BASETYPE (type); | |
1330 | return TYPE_VPTR_FIELDNO (type); | |
c906108c SS |
1331 | } |
1332 | } | |
1333 | ||
44e1a9eb DJ |
1334 | static void |
1335 | stub_noname_complaint (void) | |
1336 | { | |
e2e0b3e5 | 1337 | complaint (&symfile_complaints, _("stub type has NULL name")); |
44e1a9eb DJ |
1338 | } |
1339 | ||
c906108c SS |
1340 | /* Added by Bryan Boreham, Kewill, Sun Sep 17 18:07:17 1989. |
1341 | ||
1342 | If this is a stubbed struct (i.e. declared as struct foo *), see if | |
1343 | we can find a full definition in some other file. If so, copy this | |
7ba81444 MS |
1344 | definition, so we can use it in future. There used to be a comment |
1345 | (but not any code) that if we don't find a full definition, we'd | |
1346 | set a flag so we don't spend time in the future checking the same | |
1347 | type. That would be a mistake, though--we might load in more | |
1348 | symbols which contain a full definition for the type. | |
c906108c | 1349 | |
7b83ea04 | 1350 | This used to be coded as a macro, but I don't think it is called |
c54eabfa | 1351 | often enough to merit such treatment. |
c906108c | 1352 | |
c54eabfa | 1353 | Find the real type of TYPE. This function returns the real type, |
7ba81444 MS |
1354 | after removing all layers of typedefs and completing opaque or stub |
1355 | types. Completion changes the TYPE argument, but stripping of | |
c54eabfa JK |
1356 | typedefs does not. |
1357 | ||
1358 | If TYPE is a TYPE_CODE_TYPEDEF, its length is (also) set to the length of | |
1359 | the target type instead of zero. However, in the case of TYPE_CODE_TYPEDEF | |
1360 | check_typedef can still return different type than the original TYPE | |
1361 | pointer. */ | |
c906108c SS |
1362 | |
1363 | struct type * | |
a02fd225 | 1364 | check_typedef (struct type *type) |
c906108c SS |
1365 | { |
1366 | struct type *orig_type = type; | |
a02fd225 DJ |
1367 | int is_const, is_volatile; |
1368 | ||
423c0af8 MS |
1369 | gdb_assert (type); |
1370 | ||
c906108c SS |
1371 | while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) |
1372 | { | |
1373 | if (!TYPE_TARGET_TYPE (type)) | |
1374 | { | |
c5aa993b | 1375 | char *name; |
c906108c SS |
1376 | struct symbol *sym; |
1377 | ||
1378 | /* It is dangerous to call lookup_symbol if we are currently | |
7ba81444 | 1379 | reading a symtab. Infinite recursion is one danger. */ |
c906108c SS |
1380 | if (currently_reading_symtab) |
1381 | return type; | |
1382 | ||
1383 | name = type_name_no_tag (type); | |
7ba81444 MS |
1384 | /* FIXME: shouldn't we separately check the TYPE_NAME and |
1385 | the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or | |
1386 | VAR_DOMAIN as appropriate? (this code was written before | |
1387 | TYPE_NAME and TYPE_TAG_NAME were separate). */ | |
c906108c SS |
1388 | if (name == NULL) |
1389 | { | |
23136709 | 1390 | stub_noname_complaint (); |
c906108c SS |
1391 | return type; |
1392 | } | |
2570f2b7 | 1393 | sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0); |
c906108c SS |
1394 | if (sym) |
1395 | TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym); | |
7ba81444 | 1396 | else /* TYPE_CODE_UNDEF */ |
e9bb382b | 1397 | TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type)); |
c906108c SS |
1398 | } |
1399 | type = TYPE_TARGET_TYPE (type); | |
1400 | } | |
1401 | ||
a02fd225 DJ |
1402 | is_const = TYPE_CONST (type); |
1403 | is_volatile = TYPE_VOLATILE (type); | |
1404 | ||
7ba81444 MS |
1405 | /* If this is a struct/class/union with no fields, then check |
1406 | whether a full definition exists somewhere else. This is for | |
1407 | systems where a type definition with no fields is issued for such | |
1408 | types, instead of identifying them as stub types in the first | |
1409 | place. */ | |
c5aa993b | 1410 | |
7ba81444 MS |
1411 | if (TYPE_IS_OPAQUE (type) |
1412 | && opaque_type_resolution | |
1413 | && !currently_reading_symtab) | |
c906108c | 1414 | { |
c5aa993b JM |
1415 | char *name = type_name_no_tag (type); |
1416 | struct type *newtype; | |
c906108c SS |
1417 | if (name == NULL) |
1418 | { | |
23136709 | 1419 | stub_noname_complaint (); |
c906108c SS |
1420 | return type; |
1421 | } | |
1422 | newtype = lookup_transparent_type (name); | |
ad766c0a | 1423 | |
c906108c | 1424 | if (newtype) |
ad766c0a | 1425 | { |
7ba81444 MS |
1426 | /* If the resolved type and the stub are in the same |
1427 | objfile, then replace the stub type with the real deal. | |
1428 | But if they're in separate objfiles, leave the stub | |
1429 | alone; we'll just look up the transparent type every time | |
1430 | we call check_typedef. We can't create pointers between | |
1431 | types allocated to different objfiles, since they may | |
1432 | have different lifetimes. Trying to copy NEWTYPE over to | |
1433 | TYPE's objfile is pointless, too, since you'll have to | |
1434 | move over any other types NEWTYPE refers to, which could | |
1435 | be an unbounded amount of stuff. */ | |
ad766c0a JB |
1436 | if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type)) |
1437 | make_cv_type (is_const, is_volatile, newtype, &type); | |
1438 | else | |
1439 | type = newtype; | |
1440 | } | |
c906108c | 1441 | } |
7ba81444 MS |
1442 | /* Otherwise, rely on the stub flag being set for opaque/stubbed |
1443 | types. */ | |
74a9bb82 | 1444 | else if (TYPE_STUB (type) && !currently_reading_symtab) |
c906108c | 1445 | { |
c5aa993b | 1446 | char *name = type_name_no_tag (type); |
c906108c | 1447 | /* FIXME: shouldn't we separately check the TYPE_NAME and the |
176620f1 | 1448 | TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN |
7b83ea04 AC |
1449 | as appropriate? (this code was written before TYPE_NAME and |
1450 | TYPE_TAG_NAME were separate). */ | |
c906108c SS |
1451 | struct symbol *sym; |
1452 | if (name == NULL) | |
1453 | { | |
23136709 | 1454 | stub_noname_complaint (); |
c906108c SS |
1455 | return type; |
1456 | } | |
2570f2b7 | 1457 | sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0); |
c906108c | 1458 | if (sym) |
c26f2453 JB |
1459 | { |
1460 | /* Same as above for opaque types, we can replace the stub | |
1461 | with the complete type only if they are int the same | |
1462 | objfile. */ | |
1463 | if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type)) | |
7ba81444 MS |
1464 | make_cv_type (is_const, is_volatile, |
1465 | SYMBOL_TYPE (sym), &type); | |
c26f2453 JB |
1466 | else |
1467 | type = SYMBOL_TYPE (sym); | |
1468 | } | |
c906108c SS |
1469 | } |
1470 | ||
74a9bb82 | 1471 | if (TYPE_TARGET_STUB (type)) |
c906108c SS |
1472 | { |
1473 | struct type *range_type; | |
1474 | struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type)); | |
1475 | ||
74a9bb82 | 1476 | if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type)) |
c5aa993b | 1477 | { |
7ba81444 | 1478 | /* Empty. */ |
c5aa993b | 1479 | } |
c906108c SS |
1480 | else if (TYPE_CODE (type) == TYPE_CODE_ARRAY |
1481 | && TYPE_NFIELDS (type) == 1 | |
262452ec | 1482 | && (TYPE_CODE (range_type = TYPE_INDEX_TYPE (type)) |
c906108c SS |
1483 | == TYPE_CODE_RANGE)) |
1484 | { | |
1485 | /* Now recompute the length of the array type, based on its | |
ab0d6e0d JB |
1486 | number of elements and the target type's length. |
1487 | Watch out for Ada null Ada arrays where the high bound | |
43bbcdc2 PH |
1488 | is smaller than the low bound. */ |
1489 | const LONGEST low_bound = TYPE_LOW_BOUND (range_type); | |
1490 | const LONGEST high_bound = TYPE_HIGH_BOUND (range_type); | |
1491 | ULONGEST len; | |
1492 | ||
ab0d6e0d | 1493 | if (high_bound < low_bound) |
43bbcdc2 PH |
1494 | len = 0; |
1495 | else { | |
1496 | /* For now, we conservatively take the array length to be 0 | |
1497 | if its length exceeds UINT_MAX. The code below assumes | |
1498 | that for x < 0, (ULONGEST) x == -x + ULONGEST_MAX + 1, | |
1499 | which is technically not guaranteed by C, but is usually true | |
1500 | (because it would be true if x were unsigned with its | |
1501 | high-order bit on). It uses the fact that | |
1502 | high_bound-low_bound is always representable in | |
1503 | ULONGEST and that if high_bound-low_bound+1 overflows, | |
1504 | it overflows to 0. We must change these tests if we | |
1505 | decide to increase the representation of TYPE_LENGTH | |
1506 | from unsigned int to ULONGEST. */ | |
1507 | ULONGEST ulow = low_bound, uhigh = high_bound; | |
1508 | ULONGEST tlen = TYPE_LENGTH (target_type); | |
1509 | ||
1510 | len = tlen * (uhigh - ulow + 1); | |
1511 | if (tlen == 0 || (len / tlen - 1 + ulow) != uhigh | |
1512 | || len > UINT_MAX) | |
1513 | len = 0; | |
1514 | } | |
1515 | TYPE_LENGTH (type) = len; | |
876cecd0 | 1516 | TYPE_TARGET_STUB (type) = 0; |
c906108c SS |
1517 | } |
1518 | else if (TYPE_CODE (type) == TYPE_CODE_RANGE) | |
1519 | { | |
1520 | TYPE_LENGTH (type) = TYPE_LENGTH (target_type); | |
876cecd0 | 1521 | TYPE_TARGET_STUB (type) = 0; |
c906108c SS |
1522 | } |
1523 | } | |
7ba81444 | 1524 | /* Cache TYPE_LENGTH for future use. */ |
c906108c SS |
1525 | TYPE_LENGTH (orig_type) = TYPE_LENGTH (type); |
1526 | return type; | |
1527 | } | |
1528 | ||
7ba81444 | 1529 | /* Parse a type expression in the string [P..P+LENGTH). If an error |
48319d1f | 1530 | occurs, silently return a void type. */ |
c91ecb25 | 1531 | |
b9362cc7 | 1532 | static struct type * |
48319d1f | 1533 | safe_parse_type (struct gdbarch *gdbarch, char *p, int length) |
c91ecb25 ND |
1534 | { |
1535 | struct ui_file *saved_gdb_stderr; | |
1536 | struct type *type; | |
1537 | ||
7ba81444 | 1538 | /* Suppress error messages. */ |
c91ecb25 ND |
1539 | saved_gdb_stderr = gdb_stderr; |
1540 | gdb_stderr = ui_file_new (); | |
1541 | ||
7ba81444 | 1542 | /* Call parse_and_eval_type() without fear of longjmp()s. */ |
c91ecb25 | 1543 | if (!gdb_parse_and_eval_type (p, length, &type)) |
48319d1f | 1544 | type = builtin_type (gdbarch)->builtin_void; |
c91ecb25 | 1545 | |
7ba81444 | 1546 | /* Stop suppressing error messages. */ |
c91ecb25 ND |
1547 | ui_file_delete (gdb_stderr); |
1548 | gdb_stderr = saved_gdb_stderr; | |
1549 | ||
1550 | return type; | |
1551 | } | |
1552 | ||
c906108c SS |
1553 | /* Ugly hack to convert method stubs into method types. |
1554 | ||
7ba81444 MS |
1555 | He ain't kiddin'. This demangles the name of the method into a |
1556 | string including argument types, parses out each argument type, | |
1557 | generates a string casting a zero to that type, evaluates the | |
1558 | string, and stuffs the resulting type into an argtype vector!!! | |
1559 | Then it knows the type of the whole function (including argument | |
1560 | types for overloading), which info used to be in the stab's but was | |
1561 | removed to hack back the space required for them. */ | |
c906108c | 1562 | |
de17c821 | 1563 | static void |
fba45db2 | 1564 | check_stub_method (struct type *type, int method_id, int signature_id) |
c906108c | 1565 | { |
50810684 | 1566 | struct gdbarch *gdbarch = get_type_arch (type); |
c906108c SS |
1567 | struct fn_field *f; |
1568 | char *mangled_name = gdb_mangle_name (type, method_id, signature_id); | |
1569 | char *demangled_name = cplus_demangle (mangled_name, | |
1570 | DMGL_PARAMS | DMGL_ANSI); | |
1571 | char *argtypetext, *p; | |
1572 | int depth = 0, argcount = 1; | |
ad2f7632 | 1573 | struct field *argtypes; |
c906108c SS |
1574 | struct type *mtype; |
1575 | ||
1576 | /* Make sure we got back a function string that we can use. */ | |
1577 | if (demangled_name) | |
1578 | p = strchr (demangled_name, '('); | |
502dcf4e AC |
1579 | else |
1580 | p = NULL; | |
c906108c SS |
1581 | |
1582 | if (demangled_name == NULL || p == NULL) | |
7ba81444 MS |
1583 | error (_("Internal: Cannot demangle mangled name `%s'."), |
1584 | mangled_name); | |
c906108c SS |
1585 | |
1586 | /* Now, read in the parameters that define this type. */ | |
1587 | p += 1; | |
1588 | argtypetext = p; | |
1589 | while (*p) | |
1590 | { | |
070ad9f0 | 1591 | if (*p == '(' || *p == '<') |
c906108c SS |
1592 | { |
1593 | depth += 1; | |
1594 | } | |
070ad9f0 | 1595 | else if (*p == ')' || *p == '>') |
c906108c SS |
1596 | { |
1597 | depth -= 1; | |
1598 | } | |
1599 | else if (*p == ',' && depth == 0) | |
1600 | { | |
1601 | argcount += 1; | |
1602 | } | |
1603 | ||
1604 | p += 1; | |
1605 | } | |
1606 | ||
ad2f7632 DJ |
1607 | /* If we read one argument and it was ``void'', don't count it. */ |
1608 | if (strncmp (argtypetext, "(void)", 6) == 0) | |
1609 | argcount -= 1; | |
c906108c | 1610 | |
ad2f7632 DJ |
1611 | /* We need one extra slot, for the THIS pointer. */ |
1612 | ||
1613 | argtypes = (struct field *) | |
1614 | TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field)); | |
c906108c | 1615 | p = argtypetext; |
4a1970e4 DJ |
1616 | |
1617 | /* Add THIS pointer for non-static methods. */ | |
1618 | f = TYPE_FN_FIELDLIST1 (type, method_id); | |
1619 | if (TYPE_FN_FIELD_STATIC_P (f, signature_id)) | |
1620 | argcount = 0; | |
1621 | else | |
1622 | { | |
ad2f7632 | 1623 | argtypes[0].type = lookup_pointer_type (type); |
4a1970e4 DJ |
1624 | argcount = 1; |
1625 | } | |
c906108c | 1626 | |
c5aa993b | 1627 | if (*p != ')') /* () means no args, skip while */ |
c906108c SS |
1628 | { |
1629 | depth = 0; | |
1630 | while (*p) | |
1631 | { | |
1632 | if (depth <= 0 && (*p == ',' || *p == ')')) | |
1633 | { | |
ad2f7632 DJ |
1634 | /* Avoid parsing of ellipsis, they will be handled below. |
1635 | Also avoid ``void'' as above. */ | |
1636 | if (strncmp (argtypetext, "...", p - argtypetext) != 0 | |
1637 | && strncmp (argtypetext, "void", p - argtypetext) != 0) | |
c906108c | 1638 | { |
ad2f7632 | 1639 | argtypes[argcount].type = |
48319d1f | 1640 | safe_parse_type (gdbarch, argtypetext, p - argtypetext); |
c906108c SS |
1641 | argcount += 1; |
1642 | } | |
1643 | argtypetext = p + 1; | |
1644 | } | |
1645 | ||
070ad9f0 | 1646 | if (*p == '(' || *p == '<') |
c906108c SS |
1647 | { |
1648 | depth += 1; | |
1649 | } | |
070ad9f0 | 1650 | else if (*p == ')' || *p == '>') |
c906108c SS |
1651 | { |
1652 | depth -= 1; | |
1653 | } | |
1654 | ||
1655 | p += 1; | |
1656 | } | |
1657 | } | |
1658 | ||
c906108c SS |
1659 | TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name; |
1660 | ||
1661 | /* Now update the old "stub" type into a real type. */ | |
1662 | mtype = TYPE_FN_FIELD_TYPE (f, signature_id); | |
1663 | TYPE_DOMAIN_TYPE (mtype) = type; | |
ad2f7632 DJ |
1664 | TYPE_FIELDS (mtype) = argtypes; |
1665 | TYPE_NFIELDS (mtype) = argcount; | |
876cecd0 | 1666 | TYPE_STUB (mtype) = 0; |
c906108c | 1667 | TYPE_FN_FIELD_STUB (f, signature_id) = 0; |
ad2f7632 | 1668 | if (p[-2] == '.') |
876cecd0 | 1669 | TYPE_VARARGS (mtype) = 1; |
ad2f7632 DJ |
1670 | |
1671 | xfree (demangled_name); | |
c906108c SS |
1672 | } |
1673 | ||
7ba81444 MS |
1674 | /* This is the external interface to check_stub_method, above. This |
1675 | function unstubs all of the signatures for TYPE's METHOD_ID method | |
1676 | name. After calling this function TYPE_FN_FIELD_STUB will be | |
1677 | cleared for each signature and TYPE_FN_FIELDLIST_NAME will be | |
1678 | correct. | |
de17c821 DJ |
1679 | |
1680 | This function unfortunately can not die until stabs do. */ | |
1681 | ||
1682 | void | |
1683 | check_stub_method_group (struct type *type, int method_id) | |
1684 | { | |
1685 | int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id); | |
1686 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id); | |
f710f4fc | 1687 | int j, found_stub = 0; |
de17c821 DJ |
1688 | |
1689 | for (j = 0; j < len; j++) | |
1690 | if (TYPE_FN_FIELD_STUB (f, j)) | |
1691 | { | |
1692 | found_stub = 1; | |
1693 | check_stub_method (type, method_id, j); | |
1694 | } | |
1695 | ||
7ba81444 MS |
1696 | /* GNU v3 methods with incorrect names were corrected when we read |
1697 | in type information, because it was cheaper to do it then. The | |
1698 | only GNU v2 methods with incorrect method names are operators and | |
1699 | destructors; destructors were also corrected when we read in type | |
1700 | information. | |
de17c821 DJ |
1701 | |
1702 | Therefore the only thing we need to handle here are v2 operator | |
1703 | names. */ | |
1704 | if (found_stub && strncmp (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z", 2) != 0) | |
1705 | { | |
1706 | int ret; | |
1707 | char dem_opname[256]; | |
1708 | ||
7ba81444 MS |
1709 | ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, |
1710 | method_id), | |
de17c821 DJ |
1711 | dem_opname, DMGL_ANSI); |
1712 | if (!ret) | |
7ba81444 MS |
1713 | ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, |
1714 | method_id), | |
de17c821 DJ |
1715 | dem_opname, 0); |
1716 | if (ret) | |
1717 | TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname); | |
1718 | } | |
1719 | } | |
1720 | ||
c906108c SS |
1721 | const struct cplus_struct_type cplus_struct_default; |
1722 | ||
1723 | void | |
fba45db2 | 1724 | allocate_cplus_struct_type (struct type *type) |
c906108c | 1725 | { |
b4ba55a1 JB |
1726 | if (HAVE_CPLUS_STRUCT (type)) |
1727 | /* Structure was already allocated. Nothing more to do. */ | |
1728 | return; | |
1729 | ||
1730 | TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF; | |
1731 | TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *) | |
1732 | TYPE_ALLOC (type, sizeof (struct cplus_struct_type)); | |
1733 | *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default; | |
c906108c SS |
1734 | } |
1735 | ||
b4ba55a1 JB |
1736 | const struct gnat_aux_type gnat_aux_default = |
1737 | { NULL }; | |
1738 | ||
1739 | /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF, | |
1740 | and allocate the associated gnat-specific data. The gnat-specific | |
1741 | data is also initialized to gnat_aux_default. */ | |
1742 | void | |
1743 | allocate_gnat_aux_type (struct type *type) | |
1744 | { | |
1745 | TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF; | |
1746 | TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *) | |
1747 | TYPE_ALLOC (type, sizeof (struct gnat_aux_type)); | |
1748 | *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default; | |
1749 | } | |
1750 | ||
1751 | ||
c906108c SS |
1752 | /* Helper function to initialize the standard scalar types. |
1753 | ||
e9bb382b UW |
1754 | If NAME is non-NULL, then we make a copy of the string pointed |
1755 | to by name in the objfile_obstack for that objfile, and initialize | |
1756 | the type name to that copy. There are places (mipsread.c in particular), | |
1757 | where init_type is called with a NULL value for NAME). */ | |
c906108c SS |
1758 | |
1759 | struct type * | |
7ba81444 MS |
1760 | init_type (enum type_code code, int length, int flags, |
1761 | char *name, struct objfile *objfile) | |
c906108c | 1762 | { |
52f0bd74 | 1763 | struct type *type; |
c906108c SS |
1764 | |
1765 | type = alloc_type (objfile); | |
1766 | TYPE_CODE (type) = code; | |
1767 | TYPE_LENGTH (type) = length; | |
876cecd0 TT |
1768 | |
1769 | gdb_assert (!(flags & (TYPE_FLAG_MIN - 1))); | |
1770 | if (flags & TYPE_FLAG_UNSIGNED) | |
1771 | TYPE_UNSIGNED (type) = 1; | |
1772 | if (flags & TYPE_FLAG_NOSIGN) | |
1773 | TYPE_NOSIGN (type) = 1; | |
1774 | if (flags & TYPE_FLAG_STUB) | |
1775 | TYPE_STUB (type) = 1; | |
1776 | if (flags & TYPE_FLAG_TARGET_STUB) | |
1777 | TYPE_TARGET_STUB (type) = 1; | |
1778 | if (flags & TYPE_FLAG_STATIC) | |
1779 | TYPE_STATIC (type) = 1; | |
1780 | if (flags & TYPE_FLAG_PROTOTYPED) | |
1781 | TYPE_PROTOTYPED (type) = 1; | |
1782 | if (flags & TYPE_FLAG_INCOMPLETE) | |
1783 | TYPE_INCOMPLETE (type) = 1; | |
1784 | if (flags & TYPE_FLAG_VARARGS) | |
1785 | TYPE_VARARGS (type) = 1; | |
1786 | if (flags & TYPE_FLAG_VECTOR) | |
1787 | TYPE_VECTOR (type) = 1; | |
1788 | if (flags & TYPE_FLAG_STUB_SUPPORTED) | |
1789 | TYPE_STUB_SUPPORTED (type) = 1; | |
1790 | if (flags & TYPE_FLAG_NOTTEXT) | |
1791 | TYPE_NOTTEXT (type) = 1; | |
1792 | if (flags & TYPE_FLAG_FIXED_INSTANCE) | |
1793 | TYPE_FIXED_INSTANCE (type) = 1; | |
1794 | ||
e9bb382b UW |
1795 | if (name) |
1796 | TYPE_NAME (type) = obsavestring (name, strlen (name), | |
1797 | &objfile->objfile_obstack); | |
c906108c SS |
1798 | |
1799 | /* C++ fancies. */ | |
1800 | ||
973ccf8b | 1801 | if (name && strcmp (name, "char") == 0) |
876cecd0 | 1802 | TYPE_NOSIGN (type) = 1; |
973ccf8b | 1803 | |
b4ba55a1 | 1804 | switch (code) |
c906108c | 1805 | { |
b4ba55a1 JB |
1806 | case TYPE_CODE_STRUCT: |
1807 | case TYPE_CODE_UNION: | |
1808 | case TYPE_CODE_NAMESPACE: | |
1809 | INIT_CPLUS_SPECIFIC (type); | |
1810 | break; | |
1811 | case TYPE_CODE_FLT: | |
1812 | TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT; | |
1813 | break; | |
1814 | case TYPE_CODE_FUNC: | |
1815 | TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CALLING_CONVENTION; | |
1816 | break; | |
c906108c | 1817 | } |
c16abbde | 1818 | return type; |
c906108c SS |
1819 | } |
1820 | ||
c906108c | 1821 | int |
fba45db2 | 1822 | can_dereference (struct type *t) |
c906108c | 1823 | { |
7ba81444 MS |
1824 | /* FIXME: Should we return true for references as well as |
1825 | pointers? */ | |
c906108c SS |
1826 | CHECK_TYPEDEF (t); |
1827 | return | |
1828 | (t != NULL | |
1829 | && TYPE_CODE (t) == TYPE_CODE_PTR | |
1830 | && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID); | |
1831 | } | |
1832 | ||
adf40b2e | 1833 | int |
fba45db2 | 1834 | is_integral_type (struct type *t) |
adf40b2e JM |
1835 | { |
1836 | CHECK_TYPEDEF (t); | |
1837 | return | |
1838 | ((t != NULL) | |
d4f3574e SS |
1839 | && ((TYPE_CODE (t) == TYPE_CODE_INT) |
1840 | || (TYPE_CODE (t) == TYPE_CODE_ENUM) | |
4f2aea11 | 1841 | || (TYPE_CODE (t) == TYPE_CODE_FLAGS) |
d4f3574e SS |
1842 | || (TYPE_CODE (t) == TYPE_CODE_CHAR) |
1843 | || (TYPE_CODE (t) == TYPE_CODE_RANGE) | |
1844 | || (TYPE_CODE (t) == TYPE_CODE_BOOL))); | |
adf40b2e JM |
1845 | } |
1846 | ||
4e8f195d TT |
1847 | /* A helper function which returns true if types A and B represent the |
1848 | "same" class type. This is true if the types have the same main | |
1849 | type, or the same name. */ | |
1850 | ||
1851 | int | |
1852 | class_types_same_p (const struct type *a, const struct type *b) | |
1853 | { | |
1854 | return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b) | |
1855 | || (TYPE_NAME (a) && TYPE_NAME (b) | |
1856 | && !strcmp (TYPE_NAME (a), TYPE_NAME (b)))); | |
1857 | } | |
1858 | ||
7b83ea04 | 1859 | /* Check whether BASE is an ancestor or base class or DCLASS |
c906108c SS |
1860 | Return 1 if so, and 0 if not. |
1861 | Note: callers may want to check for identity of the types before | |
1862 | calling this function -- identical types are considered to satisfy | |
7ba81444 | 1863 | the ancestor relationship even if they're identical. */ |
c906108c SS |
1864 | |
1865 | int | |
fba45db2 | 1866 | is_ancestor (struct type *base, struct type *dclass) |
c906108c SS |
1867 | { |
1868 | int i; | |
c5aa993b | 1869 | |
c906108c SS |
1870 | CHECK_TYPEDEF (base); |
1871 | CHECK_TYPEDEF (dclass); | |
1872 | ||
4e8f195d | 1873 | if (class_types_same_p (base, dclass)) |
6b1ba9a0 | 1874 | return 1; |
c906108c SS |
1875 | |
1876 | for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++) | |
4e8f195d TT |
1877 | { |
1878 | if (is_ancestor (base, TYPE_BASECLASS (dclass, i))) | |
1879 | return 1; | |
1880 | } | |
c906108c SS |
1881 | |
1882 | return 0; | |
1883 | } | |
4e8f195d TT |
1884 | |
1885 | /* Like is_ancestor, but only returns true when BASE is a public | |
1886 | ancestor of DCLASS. */ | |
1887 | ||
1888 | int | |
1889 | is_public_ancestor (struct type *base, struct type *dclass) | |
1890 | { | |
1891 | int i; | |
1892 | ||
1893 | CHECK_TYPEDEF (base); | |
1894 | CHECK_TYPEDEF (dclass); | |
1895 | ||
1896 | if (class_types_same_p (base, dclass)) | |
1897 | return 1; | |
1898 | ||
1899 | for (i = 0; i < TYPE_N_BASECLASSES (dclass); ++i) | |
1900 | { | |
1901 | if (! BASETYPE_VIA_PUBLIC (dclass, i)) | |
1902 | continue; | |
1903 | if (is_public_ancestor (base, TYPE_BASECLASS (dclass, i))) | |
1904 | return 1; | |
1905 | } | |
1906 | ||
1907 | return 0; | |
1908 | } | |
1909 | ||
1910 | /* A helper function for is_unique_ancestor. */ | |
1911 | ||
1912 | static int | |
1913 | is_unique_ancestor_worker (struct type *base, struct type *dclass, | |
1914 | int *offset, | |
1915 | const bfd_byte *contents, CORE_ADDR address) | |
1916 | { | |
1917 | int i, count = 0; | |
1918 | ||
1919 | CHECK_TYPEDEF (base); | |
1920 | CHECK_TYPEDEF (dclass); | |
1921 | ||
1922 | for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i) | |
1923 | { | |
1924 | struct type *iter = check_typedef (TYPE_BASECLASS (dclass, i)); | |
1925 | int this_offset = baseclass_offset (dclass, i, contents, address); | |
1926 | ||
1927 | if (this_offset == -1) | |
1928 | error (_("virtual baseclass botch")); | |
1929 | ||
1930 | if (class_types_same_p (base, iter)) | |
1931 | { | |
1932 | /* If this is the first subclass, set *OFFSET and set count | |
1933 | to 1. Otherwise, if this is at the same offset as | |
1934 | previous instances, do nothing. Otherwise, increment | |
1935 | count. */ | |
1936 | if (*offset == -1) | |
1937 | { | |
1938 | *offset = this_offset; | |
1939 | count = 1; | |
1940 | } | |
1941 | else if (this_offset == *offset) | |
1942 | { | |
1943 | /* Nothing. */ | |
1944 | } | |
1945 | else | |
1946 | ++count; | |
1947 | } | |
1948 | else | |
1949 | count += is_unique_ancestor_worker (base, iter, offset, | |
1950 | contents + this_offset, | |
1951 | address + this_offset); | |
1952 | } | |
1953 | ||
1954 | return count; | |
1955 | } | |
1956 | ||
1957 | /* Like is_ancestor, but only returns true if BASE is a unique base | |
1958 | class of the type of VAL. */ | |
1959 | ||
1960 | int | |
1961 | is_unique_ancestor (struct type *base, struct value *val) | |
1962 | { | |
1963 | int offset = -1; | |
1964 | ||
1965 | return is_unique_ancestor_worker (base, value_type (val), &offset, | |
1966 | value_contents (val), | |
1967 | value_address (val)) == 1; | |
1968 | } | |
1969 | ||
c906108c SS |
1970 | \f |
1971 | ||
c5aa993b | 1972 | |
c906108c SS |
1973 | /* Functions for overload resolution begin here */ |
1974 | ||
1975 | /* Compare two badness vectors A and B and return the result. | |
7ba81444 MS |
1976 | 0 => A and B are identical |
1977 | 1 => A and B are incomparable | |
1978 | 2 => A is better than B | |
1979 | 3 => A is worse than B */ | |
c906108c SS |
1980 | |
1981 | int | |
fba45db2 | 1982 | compare_badness (struct badness_vector *a, struct badness_vector *b) |
c906108c SS |
1983 | { |
1984 | int i; | |
1985 | int tmp; | |
c5aa993b JM |
1986 | short found_pos = 0; /* any positives in c? */ |
1987 | short found_neg = 0; /* any negatives in c? */ | |
1988 | ||
1989 | /* differing lengths => incomparable */ | |
c906108c SS |
1990 | if (a->length != b->length) |
1991 | return 1; | |
1992 | ||
c5aa993b JM |
1993 | /* Subtract b from a */ |
1994 | for (i = 0; i < a->length; i++) | |
c906108c SS |
1995 | { |
1996 | tmp = a->rank[i] - b->rank[i]; | |
1997 | if (tmp > 0) | |
c5aa993b | 1998 | found_pos = 1; |
c906108c | 1999 | else if (tmp < 0) |
c5aa993b | 2000 | found_neg = 1; |
c906108c SS |
2001 | } |
2002 | ||
2003 | if (found_pos) | |
2004 | { | |
2005 | if (found_neg) | |
c5aa993b | 2006 | return 1; /* incomparable */ |
c906108c | 2007 | else |
c5aa993b | 2008 | return 3; /* A > B */ |
c906108c | 2009 | } |
c5aa993b JM |
2010 | else |
2011 | /* no positives */ | |
c906108c SS |
2012 | { |
2013 | if (found_neg) | |
c5aa993b | 2014 | return 2; /* A < B */ |
c906108c | 2015 | else |
c5aa993b | 2016 | return 0; /* A == B */ |
c906108c SS |
2017 | } |
2018 | } | |
2019 | ||
7ba81444 MS |
2020 | /* Rank a function by comparing its parameter types (PARMS, length |
2021 | NPARMS), to the types of an argument list (ARGS, length NARGS). | |
2022 | Return a pointer to a badness vector. This has NARGS + 1 | |
2023 | entries. */ | |
c906108c SS |
2024 | |
2025 | struct badness_vector * | |
7ba81444 MS |
2026 | rank_function (struct type **parms, int nparms, |
2027 | struct type **args, int nargs) | |
c906108c SS |
2028 | { |
2029 | int i; | |
c5aa993b | 2030 | struct badness_vector *bv; |
c906108c SS |
2031 | int min_len = nparms < nargs ? nparms : nargs; |
2032 | ||
2033 | bv = xmalloc (sizeof (struct badness_vector)); | |
c5aa993b | 2034 | bv->length = nargs + 1; /* add 1 for the length-match rank */ |
c906108c SS |
2035 | bv->rank = xmalloc ((nargs + 1) * sizeof (int)); |
2036 | ||
2037 | /* First compare the lengths of the supplied lists. | |
7ba81444 | 2038 | If there is a mismatch, set it to a high value. */ |
c5aa993b | 2039 | |
c906108c | 2040 | /* pai/1997-06-03 FIXME: when we have debug info about default |
7ba81444 MS |
2041 | arguments and ellipsis parameter lists, we should consider those |
2042 | and rank the length-match more finely. */ | |
c906108c SS |
2043 | |
2044 | LENGTH_MATCH (bv) = (nargs != nparms) ? LENGTH_MISMATCH_BADNESS : 0; | |
2045 | ||
2046 | /* Now rank all the parameters of the candidate function */ | |
74cc24b0 DB |
2047 | for (i = 1; i <= min_len; i++) |
2048 | bv->rank[i] = rank_one_type (parms[i-1], args[i-1]); | |
c906108c | 2049 | |
c5aa993b JM |
2050 | /* If more arguments than parameters, add dummy entries */ |
2051 | for (i = min_len + 1; i <= nargs; i++) | |
c906108c SS |
2052 | bv->rank[i] = TOO_FEW_PARAMS_BADNESS; |
2053 | ||
2054 | return bv; | |
2055 | } | |
2056 | ||
973ccf8b DJ |
2057 | /* Compare the names of two integer types, assuming that any sign |
2058 | qualifiers have been checked already. We do it this way because | |
2059 | there may be an "int" in the name of one of the types. */ | |
2060 | ||
2061 | static int | |
2062 | integer_types_same_name_p (const char *first, const char *second) | |
2063 | { | |
2064 | int first_p, second_p; | |
2065 | ||
7ba81444 MS |
2066 | /* If both are shorts, return 1; if neither is a short, keep |
2067 | checking. */ | |
973ccf8b DJ |
2068 | first_p = (strstr (first, "short") != NULL); |
2069 | second_p = (strstr (second, "short") != NULL); | |
2070 | if (first_p && second_p) | |
2071 | return 1; | |
2072 | if (first_p || second_p) | |
2073 | return 0; | |
2074 | ||
2075 | /* Likewise for long. */ | |
2076 | first_p = (strstr (first, "long") != NULL); | |
2077 | second_p = (strstr (second, "long") != NULL); | |
2078 | if (first_p && second_p) | |
2079 | return 1; | |
2080 | if (first_p || second_p) | |
2081 | return 0; | |
2082 | ||
2083 | /* Likewise for char. */ | |
2084 | first_p = (strstr (first, "char") != NULL); | |
2085 | second_p = (strstr (second, "char") != NULL); | |
2086 | if (first_p && second_p) | |
2087 | return 1; | |
2088 | if (first_p || second_p) | |
2089 | return 0; | |
2090 | ||
2091 | /* They must both be ints. */ | |
2092 | return 1; | |
2093 | } | |
2094 | ||
c906108c SS |
2095 | /* Compare one type (PARM) for compatibility with another (ARG). |
2096 | * PARM is intended to be the parameter type of a function; and | |
2097 | * ARG is the supplied argument's type. This function tests if | |
2098 | * the latter can be converted to the former. | |
2099 | * | |
2100 | * Return 0 if they are identical types; | |
2101 | * Otherwise, return an integer which corresponds to how compatible | |
7ba81444 MS |
2102 | * PARM is to ARG. The higher the return value, the worse the match. |
2103 | * Generally the "bad" conversions are all uniformly assigned a 100. */ | |
c906108c SS |
2104 | |
2105 | int | |
fba45db2 | 2106 | rank_one_type (struct type *parm, struct type *arg) |
c906108c | 2107 | { |
7ba81444 | 2108 | /* Identical type pointers. */ |
c906108c | 2109 | /* However, this still doesn't catch all cases of same type for arg |
7ba81444 MS |
2110 | and param. The reason is that builtin types are different from |
2111 | the same ones constructed from the object. */ | |
c906108c SS |
2112 | if (parm == arg) |
2113 | return 0; | |
2114 | ||
2115 | /* Resolve typedefs */ | |
2116 | if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF) | |
2117 | parm = check_typedef (parm); | |
2118 | if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF) | |
2119 | arg = check_typedef (arg); | |
2120 | ||
070ad9f0 | 2121 | /* |
7ba81444 MS |
2122 | Well, damnit, if the names are exactly the same, I'll say they |
2123 | are exactly the same. This happens when we generate method | |
2124 | stubs. The types won't point to the same address, but they | |
070ad9f0 DB |
2125 | really are the same. |
2126 | */ | |
2127 | ||
687d6395 MS |
2128 | if (TYPE_NAME (parm) && TYPE_NAME (arg) |
2129 | && !strcmp (TYPE_NAME (parm), TYPE_NAME (arg))) | |
7ba81444 | 2130 | return 0; |
070ad9f0 | 2131 | |
7ba81444 | 2132 | /* Check if identical after resolving typedefs. */ |
c906108c SS |
2133 | if (parm == arg) |
2134 | return 0; | |
2135 | ||
db577aea | 2136 | /* See through references, since we can almost make non-references |
7ba81444 | 2137 | references. */ |
db577aea | 2138 | if (TYPE_CODE (arg) == TYPE_CODE_REF) |
6b1ba9a0 | 2139 | return (rank_one_type (parm, TYPE_TARGET_TYPE (arg)) |
db577aea AC |
2140 | + REFERENCE_CONVERSION_BADNESS); |
2141 | if (TYPE_CODE (parm) == TYPE_CODE_REF) | |
6b1ba9a0 | 2142 | return (rank_one_type (TYPE_TARGET_TYPE (parm), arg) |
db577aea | 2143 | + REFERENCE_CONVERSION_BADNESS); |
5d161b24 | 2144 | if (overload_debug) |
7ba81444 MS |
2145 | /* Debugging only. */ |
2146 | fprintf_filtered (gdb_stderr, | |
2147 | "------ Arg is %s [%d], parm is %s [%d]\n", | |
2148 | TYPE_NAME (arg), TYPE_CODE (arg), | |
2149 | TYPE_NAME (parm), TYPE_CODE (parm)); | |
c906108c SS |
2150 | |
2151 | /* x -> y means arg of type x being supplied for parameter of type y */ | |
2152 | ||
2153 | switch (TYPE_CODE (parm)) | |
2154 | { | |
c5aa993b JM |
2155 | case TYPE_CODE_PTR: |
2156 | switch (TYPE_CODE (arg)) | |
2157 | { | |
2158 | case TYPE_CODE_PTR: | |
66c53f2b KS |
2159 | if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID |
2160 | && TYPE_CODE (TYPE_TARGET_TYPE (arg)) != TYPE_CODE_VOID) | |
c5aa993b JM |
2161 | return VOID_PTR_CONVERSION_BADNESS; |
2162 | else | |
7ba81444 MS |
2163 | return rank_one_type (TYPE_TARGET_TYPE (parm), |
2164 | TYPE_TARGET_TYPE (arg)); | |
c5aa993b | 2165 | case TYPE_CODE_ARRAY: |
7ba81444 MS |
2166 | return rank_one_type (TYPE_TARGET_TYPE (parm), |
2167 | TYPE_TARGET_TYPE (arg)); | |
c5aa993b JM |
2168 | case TYPE_CODE_FUNC: |
2169 | return rank_one_type (TYPE_TARGET_TYPE (parm), arg); | |
2170 | case TYPE_CODE_INT: | |
2171 | case TYPE_CODE_ENUM: | |
4f2aea11 | 2172 | case TYPE_CODE_FLAGS: |
c5aa993b JM |
2173 | case TYPE_CODE_CHAR: |
2174 | case TYPE_CODE_RANGE: | |
2175 | case TYPE_CODE_BOOL: | |
2176 | return POINTER_CONVERSION_BADNESS; | |
2177 | default: | |
2178 | return INCOMPATIBLE_TYPE_BADNESS; | |
2179 | } | |
2180 | case TYPE_CODE_ARRAY: | |
2181 | switch (TYPE_CODE (arg)) | |
2182 | { | |
2183 | case TYPE_CODE_PTR: | |
2184 | case TYPE_CODE_ARRAY: | |
7ba81444 MS |
2185 | return rank_one_type (TYPE_TARGET_TYPE (parm), |
2186 | TYPE_TARGET_TYPE (arg)); | |
c5aa993b JM |
2187 | default: |
2188 | return INCOMPATIBLE_TYPE_BADNESS; | |
2189 | } | |
2190 | case TYPE_CODE_FUNC: | |
2191 | switch (TYPE_CODE (arg)) | |
2192 | { | |
2193 | case TYPE_CODE_PTR: /* funcptr -> func */ | |
2194 | return rank_one_type (parm, TYPE_TARGET_TYPE (arg)); | |
2195 | default: | |
2196 | return INCOMPATIBLE_TYPE_BADNESS; | |
2197 | } | |
2198 | case TYPE_CODE_INT: | |
2199 | switch (TYPE_CODE (arg)) | |
2200 | { | |
2201 | case TYPE_CODE_INT: | |
2202 | if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm)) | |
2203 | { | |
2204 | /* Deal with signed, unsigned, and plain chars and | |
7ba81444 | 2205 | signed and unsigned ints. */ |
c5aa993b JM |
2206 | if (TYPE_NOSIGN (parm)) |
2207 | { | |
2208 | /* This case only for character types */ | |
7ba81444 MS |
2209 | if (TYPE_NOSIGN (arg)) |
2210 | return 0; /* plain char -> plain char */ | |
2211 | else /* signed/unsigned char -> plain char */ | |
2212 | return INTEGER_CONVERSION_BADNESS; | |
c5aa993b JM |
2213 | } |
2214 | else if (TYPE_UNSIGNED (parm)) | |
2215 | { | |
2216 | if (TYPE_UNSIGNED (arg)) | |
2217 | { | |
7ba81444 MS |
2218 | /* unsigned int -> unsigned int, or |
2219 | unsigned long -> unsigned long */ | |
2220 | if (integer_types_same_name_p (TYPE_NAME (parm), | |
2221 | TYPE_NAME (arg))) | |
973ccf8b | 2222 | return 0; |
7ba81444 MS |
2223 | else if (integer_types_same_name_p (TYPE_NAME (arg), |
2224 | "int") | |
2225 | && integer_types_same_name_p (TYPE_NAME (parm), | |
2226 | "long")) | |
c5aa993b JM |
2227 | return INTEGER_PROMOTION_BADNESS; /* unsigned int -> unsigned long */ |
2228 | else | |
1c5cb38e | 2229 | return INTEGER_CONVERSION_BADNESS; /* unsigned long -> unsigned int */ |
c5aa993b JM |
2230 | } |
2231 | else | |
2232 | { | |
7ba81444 MS |
2233 | if (integer_types_same_name_p (TYPE_NAME (arg), |
2234 | "long") | |
2235 | && integer_types_same_name_p (TYPE_NAME (parm), | |
2236 | "int")) | |
1c5cb38e | 2237 | return INTEGER_CONVERSION_BADNESS; /* signed long -> unsigned int */ |
c5aa993b JM |
2238 | else |
2239 | return INTEGER_CONVERSION_BADNESS; /* signed int/long -> unsigned int/long */ | |
2240 | } | |
2241 | } | |
2242 | else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg)) | |
2243 | { | |
7ba81444 MS |
2244 | if (integer_types_same_name_p (TYPE_NAME (parm), |
2245 | TYPE_NAME (arg))) | |
c5aa993b | 2246 | return 0; |
7ba81444 MS |
2247 | else if (integer_types_same_name_p (TYPE_NAME (arg), |
2248 | "int") | |
2249 | && integer_types_same_name_p (TYPE_NAME (parm), | |
2250 | "long")) | |
c5aa993b JM |
2251 | return INTEGER_PROMOTION_BADNESS; |
2252 | else | |
1c5cb38e | 2253 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
2254 | } |
2255 | else | |
1c5cb38e | 2256 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
2257 | } |
2258 | else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm)) | |
2259 | return INTEGER_PROMOTION_BADNESS; | |
2260 | else | |
1c5cb38e | 2261 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b | 2262 | case TYPE_CODE_ENUM: |
4f2aea11 | 2263 | case TYPE_CODE_FLAGS: |
c5aa993b JM |
2264 | case TYPE_CODE_CHAR: |
2265 | case TYPE_CODE_RANGE: | |
2266 | case TYPE_CODE_BOOL: | |
2267 | return INTEGER_PROMOTION_BADNESS; | |
2268 | case TYPE_CODE_FLT: | |
2269 | return INT_FLOAT_CONVERSION_BADNESS; | |
2270 | case TYPE_CODE_PTR: | |
2271 | return NS_POINTER_CONVERSION_BADNESS; | |
2272 | default: | |
2273 | return INCOMPATIBLE_TYPE_BADNESS; | |
2274 | } | |
2275 | break; | |
2276 | case TYPE_CODE_ENUM: | |
2277 | switch (TYPE_CODE (arg)) | |
2278 | { | |
2279 | case TYPE_CODE_INT: | |
2280 | case TYPE_CODE_CHAR: | |
2281 | case TYPE_CODE_RANGE: | |
2282 | case TYPE_CODE_BOOL: | |
2283 | case TYPE_CODE_ENUM: | |
1c5cb38e | 2284 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
2285 | case TYPE_CODE_FLT: |
2286 | return INT_FLOAT_CONVERSION_BADNESS; | |
2287 | default: | |
2288 | return INCOMPATIBLE_TYPE_BADNESS; | |
2289 | } | |
2290 | break; | |
2291 | case TYPE_CODE_CHAR: | |
2292 | switch (TYPE_CODE (arg)) | |
2293 | { | |
2294 | case TYPE_CODE_RANGE: | |
2295 | case TYPE_CODE_BOOL: | |
2296 | case TYPE_CODE_ENUM: | |
1c5cb38e | 2297 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
2298 | case TYPE_CODE_FLT: |
2299 | return INT_FLOAT_CONVERSION_BADNESS; | |
2300 | case TYPE_CODE_INT: | |
2301 | if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm)) | |
1c5cb38e | 2302 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
2303 | else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm)) |
2304 | return INTEGER_PROMOTION_BADNESS; | |
2305 | /* >>> !! else fall through !! <<< */ | |
2306 | case TYPE_CODE_CHAR: | |
7ba81444 MS |
2307 | /* Deal with signed, unsigned, and plain chars for C++ and |
2308 | with int cases falling through from previous case. */ | |
c5aa993b JM |
2309 | if (TYPE_NOSIGN (parm)) |
2310 | { | |
2311 | if (TYPE_NOSIGN (arg)) | |
2312 | return 0; | |
2313 | else | |
1c5cb38e | 2314 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
2315 | } |
2316 | else if (TYPE_UNSIGNED (parm)) | |
2317 | { | |
2318 | if (TYPE_UNSIGNED (arg)) | |
2319 | return 0; | |
2320 | else | |
2321 | return INTEGER_PROMOTION_BADNESS; | |
2322 | } | |
2323 | else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg)) | |
2324 | return 0; | |
2325 | else | |
1c5cb38e | 2326 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
2327 | default: |
2328 | return INCOMPATIBLE_TYPE_BADNESS; | |
2329 | } | |
2330 | break; | |
2331 | case TYPE_CODE_RANGE: | |
2332 | switch (TYPE_CODE (arg)) | |
2333 | { | |
2334 | case TYPE_CODE_INT: | |
2335 | case TYPE_CODE_CHAR: | |
2336 | case TYPE_CODE_RANGE: | |
2337 | case TYPE_CODE_BOOL: | |
2338 | case TYPE_CODE_ENUM: | |
1c5cb38e | 2339 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
2340 | case TYPE_CODE_FLT: |
2341 | return INT_FLOAT_CONVERSION_BADNESS; | |
2342 | default: | |
2343 | return INCOMPATIBLE_TYPE_BADNESS; | |
2344 | } | |
2345 | break; | |
2346 | case TYPE_CODE_BOOL: | |
2347 | switch (TYPE_CODE (arg)) | |
2348 | { | |
2349 | case TYPE_CODE_INT: | |
2350 | case TYPE_CODE_CHAR: | |
2351 | case TYPE_CODE_RANGE: | |
2352 | case TYPE_CODE_ENUM: | |
2353 | case TYPE_CODE_FLT: | |
2354 | case TYPE_CODE_PTR: | |
2355 | return BOOLEAN_CONVERSION_BADNESS; | |
2356 | case TYPE_CODE_BOOL: | |
2357 | return 0; | |
2358 | default: | |
2359 | return INCOMPATIBLE_TYPE_BADNESS; | |
2360 | } | |
2361 | break; | |
2362 | case TYPE_CODE_FLT: | |
2363 | switch (TYPE_CODE (arg)) | |
2364 | { | |
2365 | case TYPE_CODE_FLT: | |
2366 | if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm)) | |
2367 | return FLOAT_PROMOTION_BADNESS; | |
2368 | else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm)) | |
2369 | return 0; | |
2370 | else | |
2371 | return FLOAT_CONVERSION_BADNESS; | |
2372 | case TYPE_CODE_INT: | |
2373 | case TYPE_CODE_BOOL: | |
2374 | case TYPE_CODE_ENUM: | |
2375 | case TYPE_CODE_RANGE: | |
2376 | case TYPE_CODE_CHAR: | |
2377 | return INT_FLOAT_CONVERSION_BADNESS; | |
2378 | default: | |
2379 | return INCOMPATIBLE_TYPE_BADNESS; | |
2380 | } | |
2381 | break; | |
2382 | case TYPE_CODE_COMPLEX: | |
2383 | switch (TYPE_CODE (arg)) | |
7ba81444 | 2384 | { /* Strictly not needed for C++, but... */ |
c5aa993b JM |
2385 | case TYPE_CODE_FLT: |
2386 | return FLOAT_PROMOTION_BADNESS; | |
2387 | case TYPE_CODE_COMPLEX: | |
2388 | return 0; | |
2389 | default: | |
2390 | return INCOMPATIBLE_TYPE_BADNESS; | |
2391 | } | |
2392 | break; | |
2393 | case TYPE_CODE_STRUCT: | |
c906108c | 2394 | /* currently same as TYPE_CODE_CLASS */ |
c5aa993b JM |
2395 | switch (TYPE_CODE (arg)) |
2396 | { | |
2397 | case TYPE_CODE_STRUCT: | |
2398 | /* Check for derivation */ | |
2399 | if (is_ancestor (parm, arg)) | |
2400 | return BASE_CONVERSION_BADNESS; | |
2401 | /* else fall through */ | |
2402 | default: | |
2403 | return INCOMPATIBLE_TYPE_BADNESS; | |
2404 | } | |
2405 | break; | |
2406 | case TYPE_CODE_UNION: | |
2407 | switch (TYPE_CODE (arg)) | |
2408 | { | |
2409 | case TYPE_CODE_UNION: | |
2410 | default: | |
2411 | return INCOMPATIBLE_TYPE_BADNESS; | |
2412 | } | |
2413 | break; | |
0d5de010 | 2414 | case TYPE_CODE_MEMBERPTR: |
c5aa993b JM |
2415 | switch (TYPE_CODE (arg)) |
2416 | { | |
2417 | default: | |
2418 | return INCOMPATIBLE_TYPE_BADNESS; | |
2419 | } | |
2420 | break; | |
2421 | case TYPE_CODE_METHOD: | |
2422 | switch (TYPE_CODE (arg)) | |
2423 | { | |
2424 | ||
2425 | default: | |
2426 | return INCOMPATIBLE_TYPE_BADNESS; | |
2427 | } | |
2428 | break; | |
2429 | case TYPE_CODE_REF: | |
2430 | switch (TYPE_CODE (arg)) | |
2431 | { | |
2432 | ||
2433 | default: | |
2434 | return INCOMPATIBLE_TYPE_BADNESS; | |
2435 | } | |
2436 | ||
2437 | break; | |
2438 | case TYPE_CODE_SET: | |
2439 | switch (TYPE_CODE (arg)) | |
2440 | { | |
2441 | /* Not in C++ */ | |
2442 | case TYPE_CODE_SET: | |
7ba81444 MS |
2443 | return rank_one_type (TYPE_FIELD_TYPE (parm, 0), |
2444 | TYPE_FIELD_TYPE (arg, 0)); | |
c5aa993b JM |
2445 | default: |
2446 | return INCOMPATIBLE_TYPE_BADNESS; | |
2447 | } | |
2448 | break; | |
2449 | case TYPE_CODE_VOID: | |
2450 | default: | |
2451 | return INCOMPATIBLE_TYPE_BADNESS; | |
2452 | } /* switch (TYPE_CODE (arg)) */ | |
c906108c SS |
2453 | } |
2454 | ||
c5aa993b JM |
2455 | |
2456 | /* End of functions for overload resolution */ | |
c906108c | 2457 | |
c906108c | 2458 | static void |
fba45db2 | 2459 | print_bit_vector (B_TYPE *bits, int nbits) |
c906108c SS |
2460 | { |
2461 | int bitno; | |
2462 | ||
2463 | for (bitno = 0; bitno < nbits; bitno++) | |
2464 | { | |
2465 | if ((bitno % 8) == 0) | |
2466 | { | |
2467 | puts_filtered (" "); | |
2468 | } | |
2469 | if (B_TST (bits, bitno)) | |
a3f17187 | 2470 | printf_filtered (("1")); |
c906108c | 2471 | else |
a3f17187 | 2472 | printf_filtered (("0")); |
c906108c SS |
2473 | } |
2474 | } | |
2475 | ||
ad2f7632 | 2476 | /* Note the first arg should be the "this" pointer, we may not want to |
7ba81444 MS |
2477 | include it since we may get into a infinitely recursive |
2478 | situation. */ | |
c906108c SS |
2479 | |
2480 | static void | |
ad2f7632 | 2481 | print_arg_types (struct field *args, int nargs, int spaces) |
c906108c SS |
2482 | { |
2483 | if (args != NULL) | |
2484 | { | |
ad2f7632 DJ |
2485 | int i; |
2486 | ||
2487 | for (i = 0; i < nargs; i++) | |
2488 | recursive_dump_type (args[i].type, spaces + 2); | |
c906108c SS |
2489 | } |
2490 | } | |
2491 | ||
d6a843b5 JK |
2492 | int |
2493 | field_is_static (struct field *f) | |
2494 | { | |
2495 | /* "static" fields are the fields whose location is not relative | |
2496 | to the address of the enclosing struct. It would be nice to | |
2497 | have a dedicated flag that would be set for static fields when | |
2498 | the type is being created. But in practice, checking the field | |
2499 | loc_kind should give us an accurate answer (at least as long as | |
2500 | we assume that DWARF block locations are not going to be used | |
2501 | for static fields). FIXME? */ | |
2502 | return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME | |
2503 | || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR); | |
2504 | } | |
2505 | ||
c906108c | 2506 | static void |
fba45db2 | 2507 | dump_fn_fieldlists (struct type *type, int spaces) |
c906108c SS |
2508 | { |
2509 | int method_idx; | |
2510 | int overload_idx; | |
2511 | struct fn_field *f; | |
2512 | ||
2513 | printfi_filtered (spaces, "fn_fieldlists "); | |
d4f3574e | 2514 | gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout); |
c906108c SS |
2515 | printf_filtered ("\n"); |
2516 | for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++) | |
2517 | { | |
2518 | f = TYPE_FN_FIELDLIST1 (type, method_idx); | |
2519 | printfi_filtered (spaces + 2, "[%d] name '%s' (", | |
2520 | method_idx, | |
2521 | TYPE_FN_FIELDLIST_NAME (type, method_idx)); | |
d4f3574e SS |
2522 | gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx), |
2523 | gdb_stdout); | |
a3f17187 | 2524 | printf_filtered (_(") length %d\n"), |
c906108c SS |
2525 | TYPE_FN_FIELDLIST_LENGTH (type, method_idx)); |
2526 | for (overload_idx = 0; | |
2527 | overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx); | |
2528 | overload_idx++) | |
2529 | { | |
2530 | printfi_filtered (spaces + 4, "[%d] physname '%s' (", | |
2531 | overload_idx, | |
2532 | TYPE_FN_FIELD_PHYSNAME (f, overload_idx)); | |
d4f3574e SS |
2533 | gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx), |
2534 | gdb_stdout); | |
c906108c SS |
2535 | printf_filtered (")\n"); |
2536 | printfi_filtered (spaces + 8, "type "); | |
7ba81444 MS |
2537 | gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), |
2538 | gdb_stdout); | |
c906108c SS |
2539 | printf_filtered ("\n"); |
2540 | ||
2541 | recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx), | |
2542 | spaces + 8 + 2); | |
2543 | ||
2544 | printfi_filtered (spaces + 8, "args "); | |
7ba81444 MS |
2545 | gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), |
2546 | gdb_stdout); | |
c906108c SS |
2547 | printf_filtered ("\n"); |
2548 | ||
ad2f7632 | 2549 | print_arg_types (TYPE_FN_FIELD_ARGS (f, overload_idx), |
7ba81444 MS |
2550 | TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, |
2551 | overload_idx)), | |
ad2f7632 | 2552 | spaces); |
c906108c | 2553 | printfi_filtered (spaces + 8, "fcontext "); |
d4f3574e SS |
2554 | gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx), |
2555 | gdb_stdout); | |
c906108c SS |
2556 | printf_filtered ("\n"); |
2557 | ||
2558 | printfi_filtered (spaces + 8, "is_const %d\n", | |
2559 | TYPE_FN_FIELD_CONST (f, overload_idx)); | |
2560 | printfi_filtered (spaces + 8, "is_volatile %d\n", | |
2561 | TYPE_FN_FIELD_VOLATILE (f, overload_idx)); | |
2562 | printfi_filtered (spaces + 8, "is_private %d\n", | |
2563 | TYPE_FN_FIELD_PRIVATE (f, overload_idx)); | |
2564 | printfi_filtered (spaces + 8, "is_protected %d\n", | |
2565 | TYPE_FN_FIELD_PROTECTED (f, overload_idx)); | |
2566 | printfi_filtered (spaces + 8, "is_stub %d\n", | |
2567 | TYPE_FN_FIELD_STUB (f, overload_idx)); | |
2568 | printfi_filtered (spaces + 8, "voffset %u\n", | |
2569 | TYPE_FN_FIELD_VOFFSET (f, overload_idx)); | |
2570 | } | |
2571 | } | |
2572 | } | |
2573 | ||
2574 | static void | |
fba45db2 | 2575 | print_cplus_stuff (struct type *type, int spaces) |
c906108c SS |
2576 | { |
2577 | printfi_filtered (spaces, "n_baseclasses %d\n", | |
2578 | TYPE_N_BASECLASSES (type)); | |
2579 | printfi_filtered (spaces, "nfn_fields %d\n", | |
2580 | TYPE_NFN_FIELDS (type)); | |
2581 | printfi_filtered (spaces, "nfn_fields_total %d\n", | |
2582 | TYPE_NFN_FIELDS_TOTAL (type)); | |
2583 | if (TYPE_N_BASECLASSES (type) > 0) | |
2584 | { | |
2585 | printfi_filtered (spaces, "virtual_field_bits (%d bits at *", | |
2586 | TYPE_N_BASECLASSES (type)); | |
7ba81444 MS |
2587 | gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), |
2588 | gdb_stdout); | |
c906108c SS |
2589 | printf_filtered (")"); |
2590 | ||
2591 | print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type), | |
2592 | TYPE_N_BASECLASSES (type)); | |
2593 | puts_filtered ("\n"); | |
2594 | } | |
2595 | if (TYPE_NFIELDS (type) > 0) | |
2596 | { | |
2597 | if (TYPE_FIELD_PRIVATE_BITS (type) != NULL) | |
2598 | { | |
7ba81444 MS |
2599 | printfi_filtered (spaces, |
2600 | "private_field_bits (%d bits at *", | |
c906108c | 2601 | TYPE_NFIELDS (type)); |
7ba81444 MS |
2602 | gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), |
2603 | gdb_stdout); | |
c906108c SS |
2604 | printf_filtered (")"); |
2605 | print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type), | |
2606 | TYPE_NFIELDS (type)); | |
2607 | puts_filtered ("\n"); | |
2608 | } | |
2609 | if (TYPE_FIELD_PROTECTED_BITS (type) != NULL) | |
2610 | { | |
7ba81444 MS |
2611 | printfi_filtered (spaces, |
2612 | "protected_field_bits (%d bits at *", | |
c906108c | 2613 | TYPE_NFIELDS (type)); |
7ba81444 MS |
2614 | gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), |
2615 | gdb_stdout); | |
c906108c SS |
2616 | printf_filtered (")"); |
2617 | print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type), | |
2618 | TYPE_NFIELDS (type)); | |
2619 | puts_filtered ("\n"); | |
2620 | } | |
2621 | } | |
2622 | if (TYPE_NFN_FIELDS (type) > 0) | |
2623 | { | |
2624 | dump_fn_fieldlists (type, spaces); | |
2625 | } | |
2626 | } | |
2627 | ||
b4ba55a1 JB |
2628 | /* Print the contents of the TYPE's type_specific union, assuming that |
2629 | its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */ | |
2630 | ||
2631 | static void | |
2632 | print_gnat_stuff (struct type *type, int spaces) | |
2633 | { | |
2634 | struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type); | |
2635 | ||
2636 | recursive_dump_type (descriptive_type, spaces + 2); | |
2637 | } | |
2638 | ||
c906108c SS |
2639 | static struct obstack dont_print_type_obstack; |
2640 | ||
2641 | void | |
fba45db2 | 2642 | recursive_dump_type (struct type *type, int spaces) |
c906108c SS |
2643 | { |
2644 | int idx; | |
2645 | ||
2646 | if (spaces == 0) | |
2647 | obstack_begin (&dont_print_type_obstack, 0); | |
2648 | ||
2649 | if (TYPE_NFIELDS (type) > 0 | |
b4ba55a1 | 2650 | || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0)) |
c906108c SS |
2651 | { |
2652 | struct type **first_dont_print | |
7ba81444 | 2653 | = (struct type **) obstack_base (&dont_print_type_obstack); |
c906108c | 2654 | |
7ba81444 MS |
2655 | int i = (struct type **) |
2656 | obstack_next_free (&dont_print_type_obstack) - first_dont_print; | |
c906108c SS |
2657 | |
2658 | while (--i >= 0) | |
2659 | { | |
2660 | if (type == first_dont_print[i]) | |
2661 | { | |
2662 | printfi_filtered (spaces, "type node "); | |
d4f3574e | 2663 | gdb_print_host_address (type, gdb_stdout); |
a3f17187 | 2664 | printf_filtered (_(" <same as already seen type>\n")); |
c906108c SS |
2665 | return; |
2666 | } | |
2667 | } | |
2668 | ||
2669 | obstack_ptr_grow (&dont_print_type_obstack, type); | |
2670 | } | |
2671 | ||
2672 | printfi_filtered (spaces, "type node "); | |
d4f3574e | 2673 | gdb_print_host_address (type, gdb_stdout); |
c906108c SS |
2674 | printf_filtered ("\n"); |
2675 | printfi_filtered (spaces, "name '%s' (", | |
2676 | TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>"); | |
d4f3574e | 2677 | gdb_print_host_address (TYPE_NAME (type), gdb_stdout); |
c906108c | 2678 | printf_filtered (")\n"); |
e9e79dd9 FF |
2679 | printfi_filtered (spaces, "tagname '%s' (", |
2680 | TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>"); | |
2681 | gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout); | |
2682 | printf_filtered (")\n"); | |
c906108c SS |
2683 | printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type)); |
2684 | switch (TYPE_CODE (type)) | |
2685 | { | |
c5aa993b JM |
2686 | case TYPE_CODE_UNDEF: |
2687 | printf_filtered ("(TYPE_CODE_UNDEF)"); | |
2688 | break; | |
2689 | case TYPE_CODE_PTR: | |
2690 | printf_filtered ("(TYPE_CODE_PTR)"); | |
2691 | break; | |
2692 | case TYPE_CODE_ARRAY: | |
2693 | printf_filtered ("(TYPE_CODE_ARRAY)"); | |
2694 | break; | |
2695 | case TYPE_CODE_STRUCT: | |
2696 | printf_filtered ("(TYPE_CODE_STRUCT)"); | |
2697 | break; | |
2698 | case TYPE_CODE_UNION: | |
2699 | printf_filtered ("(TYPE_CODE_UNION)"); | |
2700 | break; | |
2701 | case TYPE_CODE_ENUM: | |
2702 | printf_filtered ("(TYPE_CODE_ENUM)"); | |
2703 | break; | |
4f2aea11 MK |
2704 | case TYPE_CODE_FLAGS: |
2705 | printf_filtered ("(TYPE_CODE_FLAGS)"); | |
2706 | break; | |
c5aa993b JM |
2707 | case TYPE_CODE_FUNC: |
2708 | printf_filtered ("(TYPE_CODE_FUNC)"); | |
2709 | break; | |
2710 | case TYPE_CODE_INT: | |
2711 | printf_filtered ("(TYPE_CODE_INT)"); | |
2712 | break; | |
2713 | case TYPE_CODE_FLT: | |
2714 | printf_filtered ("(TYPE_CODE_FLT)"); | |
2715 | break; | |
2716 | case TYPE_CODE_VOID: | |
2717 | printf_filtered ("(TYPE_CODE_VOID)"); | |
2718 | break; | |
2719 | case TYPE_CODE_SET: | |
2720 | printf_filtered ("(TYPE_CODE_SET)"); | |
2721 | break; | |
2722 | case TYPE_CODE_RANGE: | |
2723 | printf_filtered ("(TYPE_CODE_RANGE)"); | |
2724 | break; | |
2725 | case TYPE_CODE_STRING: | |
2726 | printf_filtered ("(TYPE_CODE_STRING)"); | |
2727 | break; | |
e9e79dd9 FF |
2728 | case TYPE_CODE_BITSTRING: |
2729 | printf_filtered ("(TYPE_CODE_BITSTRING)"); | |
2730 | break; | |
c5aa993b JM |
2731 | case TYPE_CODE_ERROR: |
2732 | printf_filtered ("(TYPE_CODE_ERROR)"); | |
2733 | break; | |
0d5de010 DJ |
2734 | case TYPE_CODE_MEMBERPTR: |
2735 | printf_filtered ("(TYPE_CODE_MEMBERPTR)"); | |
2736 | break; | |
2737 | case TYPE_CODE_METHODPTR: | |
2738 | printf_filtered ("(TYPE_CODE_METHODPTR)"); | |
c5aa993b JM |
2739 | break; |
2740 | case TYPE_CODE_METHOD: | |
2741 | printf_filtered ("(TYPE_CODE_METHOD)"); | |
2742 | break; | |
2743 | case TYPE_CODE_REF: | |
2744 | printf_filtered ("(TYPE_CODE_REF)"); | |
2745 | break; | |
2746 | case TYPE_CODE_CHAR: | |
2747 | printf_filtered ("(TYPE_CODE_CHAR)"); | |
2748 | break; | |
2749 | case TYPE_CODE_BOOL: | |
2750 | printf_filtered ("(TYPE_CODE_BOOL)"); | |
2751 | break; | |
e9e79dd9 FF |
2752 | case TYPE_CODE_COMPLEX: |
2753 | printf_filtered ("(TYPE_CODE_COMPLEX)"); | |
2754 | break; | |
c5aa993b JM |
2755 | case TYPE_CODE_TYPEDEF: |
2756 | printf_filtered ("(TYPE_CODE_TYPEDEF)"); | |
2757 | break; | |
5c4e30ca DC |
2758 | case TYPE_CODE_NAMESPACE: |
2759 | printf_filtered ("(TYPE_CODE_NAMESPACE)"); | |
2760 | break; | |
c5aa993b JM |
2761 | default: |
2762 | printf_filtered ("(UNKNOWN TYPE CODE)"); | |
2763 | break; | |
c906108c SS |
2764 | } |
2765 | puts_filtered ("\n"); | |
2766 | printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type)); | |
e9bb382b UW |
2767 | if (TYPE_OBJFILE_OWNED (type)) |
2768 | { | |
2769 | printfi_filtered (spaces, "objfile "); | |
2770 | gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout); | |
2771 | } | |
2772 | else | |
2773 | { | |
2774 | printfi_filtered (spaces, "gdbarch "); | |
2775 | gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout); | |
2776 | } | |
c906108c SS |
2777 | printf_filtered ("\n"); |
2778 | printfi_filtered (spaces, "target_type "); | |
d4f3574e | 2779 | gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout); |
c906108c SS |
2780 | printf_filtered ("\n"); |
2781 | if (TYPE_TARGET_TYPE (type) != NULL) | |
2782 | { | |
2783 | recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2); | |
2784 | } | |
2785 | printfi_filtered (spaces, "pointer_type "); | |
d4f3574e | 2786 | gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout); |
c906108c SS |
2787 | printf_filtered ("\n"); |
2788 | printfi_filtered (spaces, "reference_type "); | |
d4f3574e | 2789 | gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout); |
c906108c | 2790 | printf_filtered ("\n"); |
2fdde8f8 DJ |
2791 | printfi_filtered (spaces, "type_chain "); |
2792 | gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout); | |
e9e79dd9 | 2793 | printf_filtered ("\n"); |
7ba81444 MS |
2794 | printfi_filtered (spaces, "instance_flags 0x%x", |
2795 | TYPE_INSTANCE_FLAGS (type)); | |
2fdde8f8 DJ |
2796 | if (TYPE_CONST (type)) |
2797 | { | |
2798 | puts_filtered (" TYPE_FLAG_CONST"); | |
2799 | } | |
2800 | if (TYPE_VOLATILE (type)) | |
2801 | { | |
2802 | puts_filtered (" TYPE_FLAG_VOLATILE"); | |
2803 | } | |
2804 | if (TYPE_CODE_SPACE (type)) | |
2805 | { | |
2806 | puts_filtered (" TYPE_FLAG_CODE_SPACE"); | |
2807 | } | |
2808 | if (TYPE_DATA_SPACE (type)) | |
2809 | { | |
2810 | puts_filtered (" TYPE_FLAG_DATA_SPACE"); | |
2811 | } | |
8b2dbe47 KB |
2812 | if (TYPE_ADDRESS_CLASS_1 (type)) |
2813 | { | |
2814 | puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_1"); | |
2815 | } | |
2816 | if (TYPE_ADDRESS_CLASS_2 (type)) | |
2817 | { | |
2818 | puts_filtered (" TYPE_FLAG_ADDRESS_CLASS_2"); | |
2819 | } | |
2fdde8f8 | 2820 | puts_filtered ("\n"); |
876cecd0 TT |
2821 | |
2822 | printfi_filtered (spaces, "flags"); | |
762a036f | 2823 | if (TYPE_UNSIGNED (type)) |
c906108c SS |
2824 | { |
2825 | puts_filtered (" TYPE_FLAG_UNSIGNED"); | |
2826 | } | |
762a036f FF |
2827 | if (TYPE_NOSIGN (type)) |
2828 | { | |
2829 | puts_filtered (" TYPE_FLAG_NOSIGN"); | |
2830 | } | |
2831 | if (TYPE_STUB (type)) | |
c906108c SS |
2832 | { |
2833 | puts_filtered (" TYPE_FLAG_STUB"); | |
2834 | } | |
762a036f FF |
2835 | if (TYPE_TARGET_STUB (type)) |
2836 | { | |
2837 | puts_filtered (" TYPE_FLAG_TARGET_STUB"); | |
2838 | } | |
2839 | if (TYPE_STATIC (type)) | |
2840 | { | |
2841 | puts_filtered (" TYPE_FLAG_STATIC"); | |
2842 | } | |
762a036f FF |
2843 | if (TYPE_PROTOTYPED (type)) |
2844 | { | |
2845 | puts_filtered (" TYPE_FLAG_PROTOTYPED"); | |
2846 | } | |
2847 | if (TYPE_INCOMPLETE (type)) | |
2848 | { | |
2849 | puts_filtered (" TYPE_FLAG_INCOMPLETE"); | |
2850 | } | |
762a036f FF |
2851 | if (TYPE_VARARGS (type)) |
2852 | { | |
2853 | puts_filtered (" TYPE_FLAG_VARARGS"); | |
2854 | } | |
f5f8a009 EZ |
2855 | /* This is used for things like AltiVec registers on ppc. Gcc emits |
2856 | an attribute for the array type, which tells whether or not we | |
2857 | have a vector, instead of a regular array. */ | |
2858 | if (TYPE_VECTOR (type)) | |
2859 | { | |
2860 | puts_filtered (" TYPE_FLAG_VECTOR"); | |
2861 | } | |
876cecd0 TT |
2862 | if (TYPE_FIXED_INSTANCE (type)) |
2863 | { | |
2864 | puts_filtered (" TYPE_FIXED_INSTANCE"); | |
2865 | } | |
2866 | if (TYPE_STUB_SUPPORTED (type)) | |
2867 | { | |
2868 | puts_filtered (" TYPE_STUB_SUPPORTED"); | |
2869 | } | |
2870 | if (TYPE_NOTTEXT (type)) | |
2871 | { | |
2872 | puts_filtered (" TYPE_NOTTEXT"); | |
2873 | } | |
c906108c SS |
2874 | puts_filtered ("\n"); |
2875 | printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type)); | |
d4f3574e | 2876 | gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout); |
c906108c SS |
2877 | puts_filtered ("\n"); |
2878 | for (idx = 0; idx < TYPE_NFIELDS (type); idx++) | |
2879 | { | |
2880 | printfi_filtered (spaces + 2, | |
2881 | "[%d] bitpos %d bitsize %d type ", | |
2882 | idx, TYPE_FIELD_BITPOS (type, idx), | |
2883 | TYPE_FIELD_BITSIZE (type, idx)); | |
d4f3574e | 2884 | gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout); |
c906108c SS |
2885 | printf_filtered (" name '%s' (", |
2886 | TYPE_FIELD_NAME (type, idx) != NULL | |
2887 | ? TYPE_FIELD_NAME (type, idx) | |
2888 | : "<NULL>"); | |
d4f3574e | 2889 | gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout); |
c906108c SS |
2890 | printf_filtered (")\n"); |
2891 | if (TYPE_FIELD_TYPE (type, idx) != NULL) | |
2892 | { | |
2893 | recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4); | |
2894 | } | |
2895 | } | |
43bbcdc2 PH |
2896 | if (TYPE_CODE (type) == TYPE_CODE_RANGE) |
2897 | { | |
2898 | printfi_filtered (spaces, "low %s%s high %s%s\n", | |
2899 | plongest (TYPE_LOW_BOUND (type)), | |
2900 | TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "", | |
2901 | plongest (TYPE_HIGH_BOUND (type)), | |
2902 | TYPE_HIGH_BOUND_UNDEFINED (type) ? " (undefined)" : ""); | |
2903 | } | |
c906108c | 2904 | printfi_filtered (spaces, "vptr_basetype "); |
d4f3574e | 2905 | gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout); |
c906108c SS |
2906 | puts_filtered ("\n"); |
2907 | if (TYPE_VPTR_BASETYPE (type) != NULL) | |
2908 | { | |
2909 | recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2); | |
2910 | } | |
7ba81444 MS |
2911 | printfi_filtered (spaces, "vptr_fieldno %d\n", |
2912 | TYPE_VPTR_FIELDNO (type)); | |
c906108c | 2913 | |
b4ba55a1 JB |
2914 | switch (TYPE_SPECIFIC_FIELD (type)) |
2915 | { | |
2916 | case TYPE_SPECIFIC_CPLUS_STUFF: | |
2917 | printfi_filtered (spaces, "cplus_stuff "); | |
2918 | gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), | |
2919 | gdb_stdout); | |
2920 | puts_filtered ("\n"); | |
2921 | print_cplus_stuff (type, spaces); | |
2922 | break; | |
8da61cc4 | 2923 | |
b4ba55a1 JB |
2924 | case TYPE_SPECIFIC_GNAT_STUFF: |
2925 | printfi_filtered (spaces, "gnat_stuff "); | |
2926 | gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout); | |
2927 | puts_filtered ("\n"); | |
2928 | print_gnat_stuff (type, spaces); | |
2929 | break; | |
701c159d | 2930 | |
b4ba55a1 JB |
2931 | case TYPE_SPECIFIC_FLOATFORMAT: |
2932 | printfi_filtered (spaces, "floatformat "); | |
2933 | if (TYPE_FLOATFORMAT (type) == NULL) | |
2934 | puts_filtered ("(null)"); | |
2935 | else | |
2936 | { | |
2937 | puts_filtered ("{ "); | |
2938 | if (TYPE_FLOATFORMAT (type)[0] == NULL | |
2939 | || TYPE_FLOATFORMAT (type)[0]->name == NULL) | |
2940 | puts_filtered ("(null)"); | |
2941 | else | |
2942 | puts_filtered (TYPE_FLOATFORMAT (type)[0]->name); | |
2943 | ||
2944 | puts_filtered (", "); | |
2945 | if (TYPE_FLOATFORMAT (type)[1] == NULL | |
2946 | || TYPE_FLOATFORMAT (type)[1]->name == NULL) | |
2947 | puts_filtered ("(null)"); | |
2948 | else | |
2949 | puts_filtered (TYPE_FLOATFORMAT (type)[1]->name); | |
2950 | ||
2951 | puts_filtered (" }"); | |
2952 | } | |
2953 | puts_filtered ("\n"); | |
2954 | break; | |
c906108c | 2955 | |
b4ba55a1 JB |
2956 | case TYPE_SPECIFIC_CALLING_CONVENTION: |
2957 | printfi_filtered (spaces, "calling_convention %d\n", | |
2958 | TYPE_CALLING_CONVENTION (type)); | |
2959 | break; | |
c906108c | 2960 | } |
b4ba55a1 | 2961 | |
c906108c SS |
2962 | if (spaces == 0) |
2963 | obstack_free (&dont_print_type_obstack, NULL); | |
2964 | } | |
2965 | ||
ae5a43e0 DJ |
2966 | /* Trivial helpers for the libiberty hash table, for mapping one |
2967 | type to another. */ | |
2968 | ||
2969 | struct type_pair | |
2970 | { | |
2971 | struct type *old, *new; | |
2972 | }; | |
2973 | ||
2974 | static hashval_t | |
2975 | type_pair_hash (const void *item) | |
2976 | { | |
2977 | const struct type_pair *pair = item; | |
2978 | return htab_hash_pointer (pair->old); | |
2979 | } | |
2980 | ||
2981 | static int | |
2982 | type_pair_eq (const void *item_lhs, const void *item_rhs) | |
2983 | { | |
2984 | const struct type_pair *lhs = item_lhs, *rhs = item_rhs; | |
2985 | return lhs->old == rhs->old; | |
2986 | } | |
2987 | ||
2988 | /* Allocate the hash table used by copy_type_recursive to walk | |
2989 | types without duplicates. We use OBJFILE's obstack, because | |
2990 | OBJFILE is about to be deleted. */ | |
2991 | ||
2992 | htab_t | |
2993 | create_copied_types_hash (struct objfile *objfile) | |
2994 | { | |
2995 | return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq, | |
2996 | NULL, &objfile->objfile_obstack, | |
2997 | hashtab_obstack_allocate, | |
2998 | dummy_obstack_deallocate); | |
2999 | } | |
3000 | ||
7ba81444 MS |
3001 | /* Recursively copy (deep copy) TYPE, if it is associated with |
3002 | OBJFILE. Return a new type allocated using malloc, a saved type if | |
3003 | we have already visited TYPE (using COPIED_TYPES), or TYPE if it is | |
3004 | not associated with OBJFILE. */ | |
ae5a43e0 DJ |
3005 | |
3006 | struct type * | |
7ba81444 MS |
3007 | copy_type_recursive (struct objfile *objfile, |
3008 | struct type *type, | |
ae5a43e0 DJ |
3009 | htab_t copied_types) |
3010 | { | |
3011 | struct type_pair *stored, pair; | |
3012 | void **slot; | |
3013 | struct type *new_type; | |
3014 | ||
e9bb382b | 3015 | if (! TYPE_OBJFILE_OWNED (type)) |
ae5a43e0 DJ |
3016 | return type; |
3017 | ||
7ba81444 MS |
3018 | /* This type shouldn't be pointing to any types in other objfiles; |
3019 | if it did, the type might disappear unexpectedly. */ | |
ae5a43e0 DJ |
3020 | gdb_assert (TYPE_OBJFILE (type) == objfile); |
3021 | ||
3022 | pair.old = type; | |
3023 | slot = htab_find_slot (copied_types, &pair, INSERT); | |
3024 | if (*slot != NULL) | |
3025 | return ((struct type_pair *) *slot)->new; | |
3026 | ||
e9bb382b | 3027 | new_type = alloc_type_arch (get_type_arch (type)); |
ae5a43e0 DJ |
3028 | |
3029 | /* We must add the new type to the hash table immediately, in case | |
3030 | we encounter this type again during a recursive call below. */ | |
d87ecdfb | 3031 | stored = obstack_alloc (&objfile->objfile_obstack, sizeof (struct type_pair)); |
ae5a43e0 DJ |
3032 | stored->old = type; |
3033 | stored->new = new_type; | |
3034 | *slot = stored; | |
3035 | ||
876cecd0 TT |
3036 | /* Copy the common fields of types. For the main type, we simply |
3037 | copy the entire thing and then update specific fields as needed. */ | |
3038 | *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type); | |
e9bb382b UW |
3039 | TYPE_OBJFILE_OWNED (new_type) = 0; |
3040 | TYPE_OWNER (new_type).gdbarch = get_type_arch (type); | |
876cecd0 | 3041 | |
ae5a43e0 DJ |
3042 | if (TYPE_NAME (type)) |
3043 | TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type)); | |
3044 | if (TYPE_TAG_NAME (type)) | |
3045 | TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type)); | |
ae5a43e0 DJ |
3046 | |
3047 | TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type); | |
3048 | TYPE_LENGTH (new_type) = TYPE_LENGTH (type); | |
3049 | ||
3050 | /* Copy the fields. */ | |
ae5a43e0 DJ |
3051 | if (TYPE_NFIELDS (type)) |
3052 | { | |
3053 | int i, nfields; | |
3054 | ||
3055 | nfields = TYPE_NFIELDS (type); | |
1deafd4e | 3056 | TYPE_FIELDS (new_type) = XCALLOC (nfields, struct field); |
ae5a43e0 DJ |
3057 | for (i = 0; i < nfields; i++) |
3058 | { | |
7ba81444 MS |
3059 | TYPE_FIELD_ARTIFICIAL (new_type, i) = |
3060 | TYPE_FIELD_ARTIFICIAL (type, i); | |
ae5a43e0 DJ |
3061 | TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i); |
3062 | if (TYPE_FIELD_TYPE (type, i)) | |
3063 | TYPE_FIELD_TYPE (new_type, i) | |
3064 | = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i), | |
3065 | copied_types); | |
3066 | if (TYPE_FIELD_NAME (type, i)) | |
7ba81444 MS |
3067 | TYPE_FIELD_NAME (new_type, i) = |
3068 | xstrdup (TYPE_FIELD_NAME (type, i)); | |
d6a843b5 | 3069 | switch (TYPE_FIELD_LOC_KIND (type, i)) |
ae5a43e0 | 3070 | { |
d6a843b5 JK |
3071 | case FIELD_LOC_KIND_BITPOS: |
3072 | SET_FIELD_BITPOS (TYPE_FIELD (new_type, i), | |
3073 | TYPE_FIELD_BITPOS (type, i)); | |
3074 | break; | |
3075 | case FIELD_LOC_KIND_PHYSADDR: | |
3076 | SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i), | |
3077 | TYPE_FIELD_STATIC_PHYSADDR (type, i)); | |
3078 | break; | |
3079 | case FIELD_LOC_KIND_PHYSNAME: | |
3080 | SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i), | |
3081 | xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type, | |
3082 | i))); | |
3083 | break; | |
3084 | default: | |
3085 | internal_error (__FILE__, __LINE__, | |
3086 | _("Unexpected type field location kind: %d"), | |
3087 | TYPE_FIELD_LOC_KIND (type, i)); | |
ae5a43e0 DJ |
3088 | } |
3089 | } | |
3090 | } | |
3091 | ||
43bbcdc2 PH |
3092 | /* For range types, copy the bounds information. */ |
3093 | if (TYPE_CODE (type) == TYPE_CODE_RANGE) | |
3094 | { | |
3095 | TYPE_RANGE_DATA (new_type) = xmalloc (sizeof (struct range_bounds)); | |
3096 | *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type); | |
3097 | } | |
3098 | ||
ae5a43e0 DJ |
3099 | /* Copy pointers to other types. */ |
3100 | if (TYPE_TARGET_TYPE (type)) | |
7ba81444 MS |
3101 | TYPE_TARGET_TYPE (new_type) = |
3102 | copy_type_recursive (objfile, | |
3103 | TYPE_TARGET_TYPE (type), | |
3104 | copied_types); | |
ae5a43e0 | 3105 | if (TYPE_VPTR_BASETYPE (type)) |
7ba81444 MS |
3106 | TYPE_VPTR_BASETYPE (new_type) = |
3107 | copy_type_recursive (objfile, | |
3108 | TYPE_VPTR_BASETYPE (type), | |
3109 | copied_types); | |
ae5a43e0 DJ |
3110 | /* Maybe copy the type_specific bits. |
3111 | ||
3112 | NOTE drow/2005-12-09: We do not copy the C++-specific bits like | |
3113 | base classes and methods. There's no fundamental reason why we | |
3114 | can't, but at the moment it is not needed. */ | |
3115 | ||
3116 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
d5d6fca5 | 3117 | TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type); |
ae5a43e0 DJ |
3118 | else if (TYPE_CODE (type) == TYPE_CODE_STRUCT |
3119 | || TYPE_CODE (type) == TYPE_CODE_UNION | |
ae5a43e0 DJ |
3120 | || TYPE_CODE (type) == TYPE_CODE_NAMESPACE) |
3121 | INIT_CPLUS_SPECIFIC (new_type); | |
3122 | ||
3123 | return new_type; | |
3124 | } | |
3125 | ||
4af88198 JB |
3126 | /* Make a copy of the given TYPE, except that the pointer & reference |
3127 | types are not preserved. | |
3128 | ||
3129 | This function assumes that the given type has an associated objfile. | |
3130 | This objfile is used to allocate the new type. */ | |
3131 | ||
3132 | struct type * | |
3133 | copy_type (const struct type *type) | |
3134 | { | |
3135 | struct type *new_type; | |
3136 | ||
e9bb382b | 3137 | gdb_assert (TYPE_OBJFILE_OWNED (type)); |
4af88198 | 3138 | |
e9bb382b | 3139 | new_type = alloc_type_copy (type); |
4af88198 JB |
3140 | TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type); |
3141 | TYPE_LENGTH (new_type) = TYPE_LENGTH (type); | |
3142 | memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type), | |
3143 | sizeof (struct main_type)); | |
3144 | ||
3145 | return new_type; | |
3146 | } | |
3147 | ||
e9bb382b UW |
3148 | |
3149 | /* Helper functions to initialize architecture-specific types. */ | |
3150 | ||
3151 | /* Allocate a type structure associated with GDBARCH and set its | |
3152 | CODE, LENGTH, and NAME fields. */ | |
3153 | struct type * | |
3154 | arch_type (struct gdbarch *gdbarch, | |
3155 | enum type_code code, int length, char *name) | |
3156 | { | |
3157 | struct type *type; | |
3158 | ||
3159 | type = alloc_type_arch (gdbarch); | |
3160 | TYPE_CODE (type) = code; | |
3161 | TYPE_LENGTH (type) = length; | |
3162 | ||
3163 | if (name) | |
3164 | TYPE_NAME (type) = xstrdup (name); | |
3165 | ||
3166 | return type; | |
3167 | } | |
3168 | ||
3169 | /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH. | |
3170 | BIT is the type size in bits. If UNSIGNED_P is non-zero, set | |
3171 | the type's TYPE_UNSIGNED flag. NAME is the type name. */ | |
3172 | struct type * | |
3173 | arch_integer_type (struct gdbarch *gdbarch, | |
3174 | int bit, int unsigned_p, char *name) | |
3175 | { | |
3176 | struct type *t; | |
3177 | ||
3178 | t = arch_type (gdbarch, TYPE_CODE_INT, bit / TARGET_CHAR_BIT, name); | |
3179 | if (unsigned_p) | |
3180 | TYPE_UNSIGNED (t) = 1; | |
3181 | if (name && strcmp (name, "char") == 0) | |
3182 | TYPE_NOSIGN (t) = 1; | |
3183 | ||
3184 | return t; | |
3185 | } | |
3186 | ||
3187 | /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH. | |
3188 | BIT is the type size in bits. If UNSIGNED_P is non-zero, set | |
3189 | the type's TYPE_UNSIGNED flag. NAME is the type name. */ | |
3190 | struct type * | |
3191 | arch_character_type (struct gdbarch *gdbarch, | |
3192 | int bit, int unsigned_p, char *name) | |
3193 | { | |
3194 | struct type *t; | |
3195 | ||
3196 | t = arch_type (gdbarch, TYPE_CODE_CHAR, bit / TARGET_CHAR_BIT, name); | |
3197 | if (unsigned_p) | |
3198 | TYPE_UNSIGNED (t) = 1; | |
3199 | ||
3200 | return t; | |
3201 | } | |
3202 | ||
3203 | /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH. | |
3204 | BIT is the type size in bits. If UNSIGNED_P is non-zero, set | |
3205 | the type's TYPE_UNSIGNED flag. NAME is the type name. */ | |
3206 | struct type * | |
3207 | arch_boolean_type (struct gdbarch *gdbarch, | |
3208 | int bit, int unsigned_p, char *name) | |
3209 | { | |
3210 | struct type *t; | |
3211 | ||
3212 | t = arch_type (gdbarch, TYPE_CODE_BOOL, bit / TARGET_CHAR_BIT, name); | |
3213 | if (unsigned_p) | |
3214 | TYPE_UNSIGNED (t) = 1; | |
3215 | ||
3216 | return t; | |
3217 | } | |
3218 | ||
3219 | /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH. | |
3220 | BIT is the type size in bits; if BIT equals -1, the size is | |
3221 | determined by the floatformat. NAME is the type name. Set the | |
3222 | TYPE_FLOATFORMAT from FLOATFORMATS. */ | |
27067745 | 3223 | struct type * |
e9bb382b UW |
3224 | arch_float_type (struct gdbarch *gdbarch, |
3225 | int bit, char *name, const struct floatformat **floatformats) | |
8da61cc4 DJ |
3226 | { |
3227 | struct type *t; | |
3228 | ||
3229 | if (bit == -1) | |
3230 | { | |
3231 | gdb_assert (floatformats != NULL); | |
3232 | gdb_assert (floatformats[0] != NULL && floatformats[1] != NULL); | |
3233 | bit = floatformats[0]->totalsize; | |
3234 | } | |
3235 | gdb_assert (bit >= 0); | |
3236 | ||
e9bb382b | 3237 | t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name); |
8da61cc4 DJ |
3238 | TYPE_FLOATFORMAT (t) = floatformats; |
3239 | return t; | |
3240 | } | |
3241 | ||
e9bb382b UW |
3242 | /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH. |
3243 | NAME is the type name. TARGET_TYPE is the component float type. */ | |
27067745 | 3244 | struct type * |
e9bb382b UW |
3245 | arch_complex_type (struct gdbarch *gdbarch, |
3246 | char *name, struct type *target_type) | |
27067745 UW |
3247 | { |
3248 | struct type *t; | |
e9bb382b UW |
3249 | t = arch_type (gdbarch, TYPE_CODE_COMPLEX, |
3250 | 2 * TYPE_LENGTH (target_type), name); | |
27067745 UW |
3251 | TYPE_TARGET_TYPE (t) = target_type; |
3252 | return t; | |
3253 | } | |
3254 | ||
e9bb382b | 3255 | /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH. |
eb90ce83 | 3256 | NAME is the type name. LENGTH is the size of the flag word in bytes. */ |
e9bb382b UW |
3257 | struct type * |
3258 | arch_flags_type (struct gdbarch *gdbarch, char *name, int length) | |
3259 | { | |
3260 | int nfields = length * TARGET_CHAR_BIT; | |
3261 | struct type *type; | |
3262 | ||
3263 | type = arch_type (gdbarch, TYPE_CODE_FLAGS, length, name); | |
3264 | TYPE_UNSIGNED (type) = 1; | |
3265 | TYPE_NFIELDS (type) = nfields; | |
3266 | TYPE_FIELDS (type) = TYPE_ZALLOC (type, nfields * sizeof (struct field)); | |
3267 | ||
3268 | return type; | |
3269 | } | |
3270 | ||
3271 | /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at | |
3272 | position BITPOS is called NAME. */ | |
3273 | void | |
3274 | append_flags_type_flag (struct type *type, int bitpos, char *name) | |
3275 | { | |
3276 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS); | |
3277 | gdb_assert (bitpos < TYPE_NFIELDS (type)); | |
3278 | gdb_assert (bitpos >= 0); | |
3279 | ||
3280 | if (name) | |
3281 | { | |
3282 | TYPE_FIELD_NAME (type, bitpos) = xstrdup (name); | |
3283 | TYPE_FIELD_BITPOS (type, bitpos) = bitpos; | |
3284 | } | |
3285 | else | |
3286 | { | |
3287 | /* Don't show this field to the user. */ | |
3288 | TYPE_FIELD_BITPOS (type, bitpos) = -1; | |
3289 | } | |
3290 | } | |
3291 | ||
3292 | /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as | |
3293 | specified by CODE) associated with GDBARCH. NAME is the type name. */ | |
3294 | struct type * | |
3295 | arch_composite_type (struct gdbarch *gdbarch, char *name, enum type_code code) | |
3296 | { | |
3297 | struct type *t; | |
3298 | gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION); | |
3299 | t = arch_type (gdbarch, code, 0, NULL); | |
3300 | TYPE_TAG_NAME (t) = name; | |
3301 | INIT_CPLUS_SPECIFIC (t); | |
3302 | return t; | |
3303 | } | |
3304 | ||
3305 | /* Add new field with name NAME and type FIELD to composite type T. | |
f5dff777 DJ |
3306 | Do not set the field's position or adjust the type's length; |
3307 | the caller should do so. Return the new field. */ | |
3308 | struct field * | |
3309 | append_composite_type_field_raw (struct type *t, char *name, | |
3310 | struct type *field) | |
e9bb382b UW |
3311 | { |
3312 | struct field *f; | |
3313 | TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1; | |
3314 | TYPE_FIELDS (t) = xrealloc (TYPE_FIELDS (t), | |
3315 | sizeof (struct field) * TYPE_NFIELDS (t)); | |
3316 | f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]); | |
3317 | memset (f, 0, sizeof f[0]); | |
3318 | FIELD_TYPE (f[0]) = field; | |
3319 | FIELD_NAME (f[0]) = name; | |
f5dff777 DJ |
3320 | return f; |
3321 | } | |
3322 | ||
3323 | /* Add new field with name NAME and type FIELD to composite type T. | |
3324 | ALIGNMENT (if non-zero) specifies the minimum field alignment. */ | |
3325 | void | |
3326 | append_composite_type_field_aligned (struct type *t, char *name, | |
3327 | struct type *field, int alignment) | |
3328 | { | |
3329 | struct field *f = append_composite_type_field_raw (t, name, field); | |
e9bb382b UW |
3330 | if (TYPE_CODE (t) == TYPE_CODE_UNION) |
3331 | { | |
3332 | if (TYPE_LENGTH (t) < TYPE_LENGTH (field)) | |
3333 | TYPE_LENGTH (t) = TYPE_LENGTH (field); | |
3334 | } | |
3335 | else if (TYPE_CODE (t) == TYPE_CODE_STRUCT) | |
3336 | { | |
3337 | TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field); | |
3338 | if (TYPE_NFIELDS (t) > 1) | |
3339 | { | |
3340 | FIELD_BITPOS (f[0]) = (FIELD_BITPOS (f[-1]) | |
3341 | + (TYPE_LENGTH (FIELD_TYPE (f[-1])) | |
3342 | * TARGET_CHAR_BIT)); | |
3343 | ||
3344 | if (alignment) | |
3345 | { | |
3346 | int left = FIELD_BITPOS (f[0]) % (alignment * TARGET_CHAR_BIT); | |
3347 | if (left) | |
3348 | { | |
3349 | FIELD_BITPOS (f[0]) += left; | |
3350 | TYPE_LENGTH (t) += left / TARGET_CHAR_BIT; | |
3351 | } | |
3352 | } | |
3353 | } | |
3354 | } | |
3355 | } | |
3356 | ||
3357 | /* Add new field with name NAME and type FIELD to composite type T. */ | |
3358 | void | |
3359 | append_composite_type_field (struct type *t, char *name, | |
3360 | struct type *field) | |
3361 | { | |
3362 | append_composite_type_field_aligned (t, name, field, 0); | |
3363 | } | |
3364 | ||
3365 | ||
000177f0 AC |
3366 | static struct gdbarch_data *gdbtypes_data; |
3367 | ||
3368 | const struct builtin_type * | |
3369 | builtin_type (struct gdbarch *gdbarch) | |
3370 | { | |
3371 | return gdbarch_data (gdbarch, gdbtypes_data); | |
3372 | } | |
3373 | ||
3374 | static void * | |
3375 | gdbtypes_post_init (struct gdbarch *gdbarch) | |
3376 | { | |
3377 | struct builtin_type *builtin_type | |
3378 | = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type); | |
3379 | ||
46bf5051 | 3380 | /* Basic types. */ |
e9bb382b UW |
3381 | builtin_type->builtin_void |
3382 | = arch_type (gdbarch, TYPE_CODE_VOID, 1, "void"); | |
3383 | builtin_type->builtin_char | |
3384 | = arch_integer_type (gdbarch, TARGET_CHAR_BIT, | |
3385 | !gdbarch_char_signed (gdbarch), "char"); | |
3386 | builtin_type->builtin_signed_char | |
3387 | = arch_integer_type (gdbarch, TARGET_CHAR_BIT, | |
3388 | 0, "signed char"); | |
3389 | builtin_type->builtin_unsigned_char | |
3390 | = arch_integer_type (gdbarch, TARGET_CHAR_BIT, | |
3391 | 1, "unsigned char"); | |
3392 | builtin_type->builtin_short | |
3393 | = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), | |
3394 | 0, "short"); | |
3395 | builtin_type->builtin_unsigned_short | |
3396 | = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), | |
3397 | 1, "unsigned short"); | |
3398 | builtin_type->builtin_int | |
3399 | = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), | |
3400 | 0, "int"); | |
3401 | builtin_type->builtin_unsigned_int | |
3402 | = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), | |
3403 | 1, "unsigned int"); | |
3404 | builtin_type->builtin_long | |
3405 | = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), | |
3406 | 0, "long"); | |
3407 | builtin_type->builtin_unsigned_long | |
3408 | = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), | |
3409 | 1, "unsigned long"); | |
3410 | builtin_type->builtin_long_long | |
3411 | = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), | |
3412 | 0, "long long"); | |
3413 | builtin_type->builtin_unsigned_long_long | |
3414 | = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), | |
3415 | 1, "unsigned long long"); | |
70bd8e24 | 3416 | builtin_type->builtin_float |
e9bb382b | 3417 | = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch), |
27067745 | 3418 | "float", gdbarch_float_format (gdbarch)); |
70bd8e24 | 3419 | builtin_type->builtin_double |
e9bb382b | 3420 | = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch), |
27067745 | 3421 | "double", gdbarch_double_format (gdbarch)); |
70bd8e24 | 3422 | builtin_type->builtin_long_double |
e9bb382b | 3423 | = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch), |
27067745 | 3424 | "long double", gdbarch_long_double_format (gdbarch)); |
70bd8e24 | 3425 | builtin_type->builtin_complex |
e9bb382b UW |
3426 | = arch_complex_type (gdbarch, "complex", |
3427 | builtin_type->builtin_float); | |
70bd8e24 | 3428 | builtin_type->builtin_double_complex |
e9bb382b UW |
3429 | = arch_complex_type (gdbarch, "double complex", |
3430 | builtin_type->builtin_double); | |
3431 | builtin_type->builtin_string | |
3432 | = arch_type (gdbarch, TYPE_CODE_STRING, 1, "string"); | |
3433 | builtin_type->builtin_bool | |
3434 | = arch_type (gdbarch, TYPE_CODE_BOOL, 1, "bool"); | |
000177f0 | 3435 | |
7678ef8f TJB |
3436 | /* The following three are about decimal floating point types, which |
3437 | are 32-bits, 64-bits and 128-bits respectively. */ | |
3438 | builtin_type->builtin_decfloat | |
e9bb382b | 3439 | = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 32 / 8, "_Decimal32"); |
7678ef8f | 3440 | builtin_type->builtin_decdouble |
e9bb382b | 3441 | = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 64 / 8, "_Decimal64"); |
7678ef8f | 3442 | builtin_type->builtin_declong |
e9bb382b | 3443 | = arch_type (gdbarch, TYPE_CODE_DECFLOAT, 128 / 8, "_Decimal128"); |
7678ef8f | 3444 | |
69feb676 | 3445 | /* "True" character types. */ |
e9bb382b UW |
3446 | builtin_type->builtin_true_char |
3447 | = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character"); | |
3448 | builtin_type->builtin_true_unsigned_char | |
3449 | = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character"); | |
69feb676 | 3450 | |
df4df182 | 3451 | /* Fixed-size integer types. */ |
e9bb382b UW |
3452 | builtin_type->builtin_int0 |
3453 | = arch_integer_type (gdbarch, 0, 0, "int0_t"); | |
3454 | builtin_type->builtin_int8 | |
3455 | = arch_integer_type (gdbarch, 8, 0, "int8_t"); | |
3456 | builtin_type->builtin_uint8 | |
3457 | = arch_integer_type (gdbarch, 8, 1, "uint8_t"); | |
3458 | builtin_type->builtin_int16 | |
3459 | = arch_integer_type (gdbarch, 16, 0, "int16_t"); | |
3460 | builtin_type->builtin_uint16 | |
3461 | = arch_integer_type (gdbarch, 16, 1, "uint16_t"); | |
3462 | builtin_type->builtin_int32 | |
3463 | = arch_integer_type (gdbarch, 32, 0, "int32_t"); | |
3464 | builtin_type->builtin_uint32 | |
3465 | = arch_integer_type (gdbarch, 32, 1, "uint32_t"); | |
3466 | builtin_type->builtin_int64 | |
3467 | = arch_integer_type (gdbarch, 64, 0, "int64_t"); | |
3468 | builtin_type->builtin_uint64 | |
3469 | = arch_integer_type (gdbarch, 64, 1, "uint64_t"); | |
3470 | builtin_type->builtin_int128 | |
3471 | = arch_integer_type (gdbarch, 128, 0, "int128_t"); | |
3472 | builtin_type->builtin_uint128 | |
3473 | = arch_integer_type (gdbarch, 128, 1, "uint128_t"); | |
3474 | TYPE_NOTTEXT (builtin_type->builtin_int8) = 1; | |
3475 | TYPE_NOTTEXT (builtin_type->builtin_uint8) = 1; | |
df4df182 | 3476 | |
9a22f0d0 PM |
3477 | /* Wide character types. */ |
3478 | builtin_type->builtin_char16 | |
3479 | = arch_integer_type (gdbarch, 16, 0, "char16_t"); | |
3480 | builtin_type->builtin_char32 | |
3481 | = arch_integer_type (gdbarch, 32, 0, "char32_t"); | |
3482 | ||
3483 | ||
46bf5051 | 3484 | /* Default data/code pointer types. */ |
e9bb382b UW |
3485 | builtin_type->builtin_data_ptr |
3486 | = lookup_pointer_type (builtin_type->builtin_void); | |
3487 | builtin_type->builtin_func_ptr | |
3488 | = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void)); | |
46bf5051 | 3489 | |
78267919 | 3490 | /* This type represents a GDB internal function. */ |
e9bb382b UW |
3491 | builtin_type->internal_fn |
3492 | = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0, | |
3493 | "<internal function>"); | |
78267919 | 3494 | |
46bf5051 UW |
3495 | return builtin_type; |
3496 | } | |
3497 | ||
3498 | ||
3499 | /* This set of objfile-based types is intended to be used by symbol | |
3500 | readers as basic types. */ | |
3501 | ||
3502 | static const struct objfile_data *objfile_type_data; | |
3503 | ||
3504 | const struct objfile_type * | |
3505 | objfile_type (struct objfile *objfile) | |
3506 | { | |
3507 | struct gdbarch *gdbarch; | |
3508 | struct objfile_type *objfile_type | |
3509 | = objfile_data (objfile, objfile_type_data); | |
3510 | ||
3511 | if (objfile_type) | |
3512 | return objfile_type; | |
3513 | ||
3514 | objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack, | |
3515 | 1, struct objfile_type); | |
3516 | ||
3517 | /* Use the objfile architecture to determine basic type properties. */ | |
3518 | gdbarch = get_objfile_arch (objfile); | |
3519 | ||
3520 | /* Basic types. */ | |
3521 | objfile_type->builtin_void | |
3522 | = init_type (TYPE_CODE_VOID, 1, | |
3523 | 0, | |
3524 | "void", objfile); | |
3525 | ||
3526 | objfile_type->builtin_char | |
3527 | = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
3528 | (TYPE_FLAG_NOSIGN | |
3529 | | (gdbarch_char_signed (gdbarch) ? 0 : TYPE_FLAG_UNSIGNED)), | |
3530 | "char", objfile); | |
3531 | objfile_type->builtin_signed_char | |
3532 | = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
3533 | 0, | |
3534 | "signed char", objfile); | |
3535 | objfile_type->builtin_unsigned_char | |
3536 | = init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, | |
3537 | TYPE_FLAG_UNSIGNED, | |
3538 | "unsigned char", objfile); | |
3539 | objfile_type->builtin_short | |
3540 | = init_type (TYPE_CODE_INT, | |
3541 | gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT, | |
3542 | 0, "short", objfile); | |
3543 | objfile_type->builtin_unsigned_short | |
3544 | = init_type (TYPE_CODE_INT, | |
3545 | gdbarch_short_bit (gdbarch) / TARGET_CHAR_BIT, | |
3546 | TYPE_FLAG_UNSIGNED, "unsigned short", objfile); | |
3547 | objfile_type->builtin_int | |
3548 | = init_type (TYPE_CODE_INT, | |
3549 | gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT, | |
3550 | 0, "int", objfile); | |
3551 | objfile_type->builtin_unsigned_int | |
3552 | = init_type (TYPE_CODE_INT, | |
3553 | gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT, | |
3554 | TYPE_FLAG_UNSIGNED, "unsigned int", objfile); | |
3555 | objfile_type->builtin_long | |
3556 | = init_type (TYPE_CODE_INT, | |
3557 | gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT, | |
3558 | 0, "long", objfile); | |
3559 | objfile_type->builtin_unsigned_long | |
3560 | = init_type (TYPE_CODE_INT, | |
3561 | gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT, | |
3562 | TYPE_FLAG_UNSIGNED, "unsigned long", objfile); | |
3563 | objfile_type->builtin_long_long | |
3564 | = init_type (TYPE_CODE_INT, | |
3565 | gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT, | |
3566 | 0, "long long", objfile); | |
3567 | objfile_type->builtin_unsigned_long_long | |
3568 | = init_type (TYPE_CODE_INT, | |
3569 | gdbarch_long_long_bit (gdbarch) / TARGET_CHAR_BIT, | |
3570 | TYPE_FLAG_UNSIGNED, "unsigned long long", objfile); | |
3571 | ||
3572 | objfile_type->builtin_float | |
3573 | = init_type (TYPE_CODE_FLT, | |
3574 | gdbarch_float_bit (gdbarch) / TARGET_CHAR_BIT, | |
3575 | 0, "float", objfile); | |
3576 | TYPE_FLOATFORMAT (objfile_type->builtin_float) | |
3577 | = gdbarch_float_format (gdbarch); | |
3578 | objfile_type->builtin_double | |
3579 | = init_type (TYPE_CODE_FLT, | |
3580 | gdbarch_double_bit (gdbarch) / TARGET_CHAR_BIT, | |
3581 | 0, "double", objfile); | |
3582 | TYPE_FLOATFORMAT (objfile_type->builtin_double) | |
3583 | = gdbarch_double_format (gdbarch); | |
3584 | objfile_type->builtin_long_double | |
3585 | = init_type (TYPE_CODE_FLT, | |
3586 | gdbarch_long_double_bit (gdbarch) / TARGET_CHAR_BIT, | |
3587 | 0, "long double", objfile); | |
3588 | TYPE_FLOATFORMAT (objfile_type->builtin_long_double) | |
3589 | = gdbarch_long_double_format (gdbarch); | |
3590 | ||
3591 | /* This type represents a type that was unrecognized in symbol read-in. */ | |
3592 | objfile_type->builtin_error | |
3593 | = init_type (TYPE_CODE_ERROR, 0, 0, "<unknown type>", objfile); | |
3594 | ||
3595 | /* The following set of types is used for symbols with no | |
3596 | debug information. */ | |
3597 | objfile_type->nodebug_text_symbol | |
3598 | = init_type (TYPE_CODE_FUNC, 1, 0, | |
3599 | "<text variable, no debug info>", objfile); | |
3600 | TYPE_TARGET_TYPE (objfile_type->nodebug_text_symbol) | |
3601 | = objfile_type->builtin_int; | |
3602 | objfile_type->nodebug_data_symbol | |
3603 | = init_type (TYPE_CODE_INT, | |
3604 | gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0, | |
3605 | "<data variable, no debug info>", objfile); | |
3606 | objfile_type->nodebug_unknown_symbol | |
3607 | = init_type (TYPE_CODE_INT, 1, 0, | |
3608 | "<variable (not text or data), no debug info>", objfile); | |
3609 | objfile_type->nodebug_tls_symbol | |
3610 | = init_type (TYPE_CODE_INT, | |
3611 | gdbarch_int_bit (gdbarch) / HOST_CHAR_BIT, 0, | |
3612 | "<thread local variable, no debug info>", objfile); | |
000177f0 AC |
3613 | |
3614 | /* NOTE: on some targets, addresses and pointers are not necessarily | |
3615 | the same --- for example, on the D10V, pointers are 16 bits long, | |
3616 | but addresses are 32 bits long. See doc/gdbint.texinfo, | |
3617 | ``Pointers Are Not Always Addresses''. | |
3618 | ||
3619 | The upshot is: | |
3620 | - gdb's `struct type' always describes the target's | |
3621 | representation. | |
3622 | - gdb's `struct value' objects should always hold values in | |
3623 | target form. | |
3624 | - gdb's CORE_ADDR values are addresses in the unified virtual | |
3625 | address space that the assembler and linker work with. Thus, | |
3626 | since target_read_memory takes a CORE_ADDR as an argument, it | |
3627 | can access any memory on the target, even if the processor has | |
3628 | separate code and data address spaces. | |
3629 | ||
3630 | So, for example: | |
3631 | - If v is a value holding a D10V code pointer, its contents are | |
3632 | in target form: a big-endian address left-shifted two bits. | |
3633 | - If p is a D10V pointer type, TYPE_LENGTH (p) == 2, just as | |
3634 | sizeof (void *) == 2 on the target. | |
3635 | ||
46bf5051 UW |
3636 | In this context, objfile_type->builtin_core_addr is a bit odd: |
3637 | it's a target type for a value the target will never see. It's | |
3638 | only used to hold the values of (typeless) linker symbols, which | |
3639 | are indeed in the unified virtual address space. */ | |
000177f0 | 3640 | |
46bf5051 UW |
3641 | objfile_type->builtin_core_addr |
3642 | = init_type (TYPE_CODE_INT, | |
3643 | gdbarch_addr_bit (gdbarch) / 8, | |
3644 | TYPE_FLAG_UNSIGNED, "__CORE_ADDR", objfile); | |
64c50499 | 3645 | |
46bf5051 UW |
3646 | set_objfile_data (objfile, objfile_type_data, objfile_type); |
3647 | return objfile_type; | |
000177f0 AC |
3648 | } |
3649 | ||
46bf5051 | 3650 | |
a14ed312 | 3651 | extern void _initialize_gdbtypes (void); |
c906108c | 3652 | void |
fba45db2 | 3653 | _initialize_gdbtypes (void) |
c906108c | 3654 | { |
5674de60 | 3655 | gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init); |
46bf5051 | 3656 | objfile_type_data = register_objfile_data (); |
5674de60 | 3657 | |
85c07804 AC |
3658 | add_setshow_zinteger_cmd ("overload", no_class, &overload_debug, _("\ |
3659 | Set debugging of C++ overloading."), _("\ | |
3660 | Show debugging of C++ overloading."), _("\ | |
3661 | When enabled, ranking of the functions is displayed."), | |
3662 | NULL, | |
920d2a44 | 3663 | show_overload_debug, |
85c07804 | 3664 | &setdebuglist, &showdebuglist); |
5674de60 | 3665 | |
7ba81444 | 3666 | /* Add user knob for controlling resolution of opaque types. */ |
5674de60 UW |
3667 | add_setshow_boolean_cmd ("opaque-type-resolution", class_support, |
3668 | &opaque_type_resolution, _("\ | |
3669 | Set resolution of opaque struct/class/union types (if set before loading symbols)."), _("\ | |
3670 | Show resolution of opaque struct/class/union types (if set before loading symbols)."), NULL, | |
3671 | NULL, | |
3672 | show_opaque_type_resolution, | |
3673 | &setlist, &showlist); | |
c906108c | 3674 | } |