Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support routines for manipulating internal types for GDB. |
4f2aea11 | 2 | |
e2882c85 | 3 | Copyright (C) 1992-2018 Free Software Foundation, Inc. |
4f2aea11 | 4 | |
c906108c SS |
5 | Contributed by Cygnus Support, using pieces from other GDB modules. |
6 | ||
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
c906108c | 18 | |
c5aa993b | 19 | You should have received a copy of the GNU General Public License |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
21 | |
22 | #include "defs.h" | |
c906108c SS |
23 | #include "bfd.h" |
24 | #include "symtab.h" | |
25 | #include "symfile.h" | |
26 | #include "objfiles.h" | |
27 | #include "gdbtypes.h" | |
28 | #include "expression.h" | |
29 | #include "language.h" | |
30 | #include "target.h" | |
31 | #include "value.h" | |
32 | #include "demangle.h" | |
33 | #include "complaints.h" | |
34 | #include "gdbcmd.h" | |
015a42b4 | 35 | #include "cp-abi.h" |
ae5a43e0 | 36 | #include "hashtab.h" |
8de20a37 | 37 | #include "cp-support.h" |
ca092b61 DE |
38 | #include "bcache.h" |
39 | #include "dwarf2loc.h" | |
80180f79 | 40 | #include "gdbcore.h" |
1841ee5d | 41 | #include "floatformat.h" |
ac3aafc7 | 42 | |
6403aeea SW |
43 | /* Initialize BADNESS constants. */ |
44 | ||
a9d5ef47 | 45 | const struct rank LENGTH_MISMATCH_BADNESS = {100,0}; |
6403aeea | 46 | |
a9d5ef47 SW |
47 | const struct rank TOO_FEW_PARAMS_BADNESS = {100,0}; |
48 | const struct rank INCOMPATIBLE_TYPE_BADNESS = {100,0}; | |
6403aeea | 49 | |
a9d5ef47 | 50 | const struct rank EXACT_MATCH_BADNESS = {0,0}; |
6403aeea | 51 | |
a9d5ef47 SW |
52 | const struct rank INTEGER_PROMOTION_BADNESS = {1,0}; |
53 | const struct rank FLOAT_PROMOTION_BADNESS = {1,0}; | |
54 | const struct rank BASE_PTR_CONVERSION_BADNESS = {1,0}; | |
e15c3eb4 | 55 | const struct rank CV_CONVERSION_BADNESS = {1, 0}; |
a9d5ef47 SW |
56 | const struct rank INTEGER_CONVERSION_BADNESS = {2,0}; |
57 | const struct rank FLOAT_CONVERSION_BADNESS = {2,0}; | |
58 | const struct rank INT_FLOAT_CONVERSION_BADNESS = {2,0}; | |
59 | const struct rank VOID_PTR_CONVERSION_BADNESS = {2,0}; | |
5b4f6e25 | 60 | const struct rank BOOL_CONVERSION_BADNESS = {3,0}; |
a9d5ef47 SW |
61 | const struct rank BASE_CONVERSION_BADNESS = {2,0}; |
62 | const struct rank REFERENCE_CONVERSION_BADNESS = {2,0}; | |
da096638 | 63 | const struct rank NULL_POINTER_CONVERSION_BADNESS = {2,0}; |
a9d5ef47 | 64 | const struct rank NS_POINTER_CONVERSION_BADNESS = {10,0}; |
a451cb65 | 65 | const struct rank NS_INTEGER_POINTER_CONVERSION_BADNESS = {3,0}; |
6403aeea | 66 | |
8da61cc4 | 67 | /* Floatformat pairs. */ |
f9e9243a UW |
68 | const struct floatformat *floatformats_ieee_half[BFD_ENDIAN_UNKNOWN] = { |
69 | &floatformat_ieee_half_big, | |
70 | &floatformat_ieee_half_little | |
71 | }; | |
8da61cc4 DJ |
72 | const struct floatformat *floatformats_ieee_single[BFD_ENDIAN_UNKNOWN] = { |
73 | &floatformat_ieee_single_big, | |
74 | &floatformat_ieee_single_little | |
75 | }; | |
76 | const struct floatformat *floatformats_ieee_double[BFD_ENDIAN_UNKNOWN] = { | |
77 | &floatformat_ieee_double_big, | |
78 | &floatformat_ieee_double_little | |
79 | }; | |
80 | const struct floatformat *floatformats_ieee_double_littlebyte_bigword[BFD_ENDIAN_UNKNOWN] = { | |
81 | &floatformat_ieee_double_big, | |
82 | &floatformat_ieee_double_littlebyte_bigword | |
83 | }; | |
84 | const struct floatformat *floatformats_i387_ext[BFD_ENDIAN_UNKNOWN] = { | |
85 | &floatformat_i387_ext, | |
86 | &floatformat_i387_ext | |
87 | }; | |
88 | const struct floatformat *floatformats_m68881_ext[BFD_ENDIAN_UNKNOWN] = { | |
89 | &floatformat_m68881_ext, | |
90 | &floatformat_m68881_ext | |
91 | }; | |
92 | const struct floatformat *floatformats_arm_ext[BFD_ENDIAN_UNKNOWN] = { | |
93 | &floatformat_arm_ext_big, | |
94 | &floatformat_arm_ext_littlebyte_bigword | |
95 | }; | |
96 | const struct floatformat *floatformats_ia64_spill[BFD_ENDIAN_UNKNOWN] = { | |
97 | &floatformat_ia64_spill_big, | |
98 | &floatformat_ia64_spill_little | |
99 | }; | |
100 | const struct floatformat *floatformats_ia64_quad[BFD_ENDIAN_UNKNOWN] = { | |
101 | &floatformat_ia64_quad_big, | |
102 | &floatformat_ia64_quad_little | |
103 | }; | |
104 | const struct floatformat *floatformats_vax_f[BFD_ENDIAN_UNKNOWN] = { | |
105 | &floatformat_vax_f, | |
106 | &floatformat_vax_f | |
107 | }; | |
108 | const struct floatformat *floatformats_vax_d[BFD_ENDIAN_UNKNOWN] = { | |
109 | &floatformat_vax_d, | |
110 | &floatformat_vax_d | |
111 | }; | |
b14d30e1 | 112 | const struct floatformat *floatformats_ibm_long_double[BFD_ENDIAN_UNKNOWN] = { |
f5aee5ee AM |
113 | &floatformat_ibm_long_double_big, |
114 | &floatformat_ibm_long_double_little | |
b14d30e1 | 115 | }; |
8da61cc4 | 116 | |
2873700e KS |
117 | /* Should opaque types be resolved? */ |
118 | ||
119 | static int opaque_type_resolution = 1; | |
120 | ||
121 | /* A flag to enable printing of debugging information of C++ | |
122 | overloading. */ | |
123 | ||
124 | unsigned int overload_debug = 0; | |
125 | ||
a451cb65 KS |
126 | /* A flag to enable strict type checking. */ |
127 | ||
128 | static int strict_type_checking = 1; | |
129 | ||
2873700e | 130 | /* A function to show whether opaque types are resolved. */ |
5212577a | 131 | |
920d2a44 AC |
132 | static void |
133 | show_opaque_type_resolution (struct ui_file *file, int from_tty, | |
7ba81444 MS |
134 | struct cmd_list_element *c, |
135 | const char *value) | |
920d2a44 | 136 | { |
3e43a32a MS |
137 | fprintf_filtered (file, _("Resolution of opaque struct/class/union types " |
138 | "(if set before loading symbols) is %s.\n"), | |
920d2a44 AC |
139 | value); |
140 | } | |
141 | ||
2873700e | 142 | /* A function to show whether C++ overload debugging is enabled. */ |
5212577a | 143 | |
920d2a44 AC |
144 | static void |
145 | show_overload_debug (struct ui_file *file, int from_tty, | |
146 | struct cmd_list_element *c, const char *value) | |
147 | { | |
7ba81444 MS |
148 | fprintf_filtered (file, _("Debugging of C++ overloading is %s.\n"), |
149 | value); | |
920d2a44 | 150 | } |
c906108c | 151 | |
a451cb65 KS |
152 | /* A function to show the status of strict type checking. */ |
153 | ||
154 | static void | |
155 | show_strict_type_checking (struct ui_file *file, int from_tty, | |
156 | struct cmd_list_element *c, const char *value) | |
157 | { | |
158 | fprintf_filtered (file, _("Strict type checking is %s.\n"), value); | |
159 | } | |
160 | ||
5212577a | 161 | \f |
e9bb382b UW |
162 | /* Allocate a new OBJFILE-associated type structure and fill it |
163 | with some defaults. Space for the type structure is allocated | |
164 | on the objfile's objfile_obstack. */ | |
c906108c SS |
165 | |
166 | struct type * | |
fba45db2 | 167 | alloc_type (struct objfile *objfile) |
c906108c | 168 | { |
52f0bd74 | 169 | struct type *type; |
c906108c | 170 | |
e9bb382b UW |
171 | gdb_assert (objfile != NULL); |
172 | ||
7ba81444 | 173 | /* Alloc the structure and start off with all fields zeroed. */ |
e9bb382b UW |
174 | type = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct type); |
175 | TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (&objfile->objfile_obstack, | |
176 | struct main_type); | |
177 | OBJSTAT (objfile, n_types++); | |
c906108c | 178 | |
e9bb382b UW |
179 | TYPE_OBJFILE_OWNED (type) = 1; |
180 | TYPE_OWNER (type).objfile = objfile; | |
c906108c | 181 | |
7ba81444 | 182 | /* Initialize the fields that might not be zero. */ |
c906108c SS |
183 | |
184 | TYPE_CODE (type) = TYPE_CODE_UNDEF; | |
2fdde8f8 | 185 | TYPE_CHAIN (type) = type; /* Chain back to itself. */ |
c906108c | 186 | |
c16abbde | 187 | return type; |
c906108c SS |
188 | } |
189 | ||
e9bb382b UW |
190 | /* Allocate a new GDBARCH-associated type structure and fill it |
191 | with some defaults. Space for the type structure is allocated | |
8f57eec2 | 192 | on the obstack associated with GDBARCH. */ |
e9bb382b UW |
193 | |
194 | struct type * | |
195 | alloc_type_arch (struct gdbarch *gdbarch) | |
196 | { | |
197 | struct type *type; | |
198 | ||
199 | gdb_assert (gdbarch != NULL); | |
200 | ||
201 | /* Alloc the structure and start off with all fields zeroed. */ | |
202 | ||
8f57eec2 PP |
203 | type = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct type); |
204 | TYPE_MAIN_TYPE (type) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct main_type); | |
e9bb382b UW |
205 | |
206 | TYPE_OBJFILE_OWNED (type) = 0; | |
207 | TYPE_OWNER (type).gdbarch = gdbarch; | |
208 | ||
209 | /* Initialize the fields that might not be zero. */ | |
210 | ||
211 | TYPE_CODE (type) = TYPE_CODE_UNDEF; | |
e9bb382b UW |
212 | TYPE_CHAIN (type) = type; /* Chain back to itself. */ |
213 | ||
214 | return type; | |
215 | } | |
216 | ||
217 | /* If TYPE is objfile-associated, allocate a new type structure | |
218 | associated with the same objfile. If TYPE is gdbarch-associated, | |
219 | allocate a new type structure associated with the same gdbarch. */ | |
220 | ||
221 | struct type * | |
222 | alloc_type_copy (const struct type *type) | |
223 | { | |
224 | if (TYPE_OBJFILE_OWNED (type)) | |
225 | return alloc_type (TYPE_OWNER (type).objfile); | |
226 | else | |
227 | return alloc_type_arch (TYPE_OWNER (type).gdbarch); | |
228 | } | |
229 | ||
230 | /* If TYPE is gdbarch-associated, return that architecture. | |
231 | If TYPE is objfile-associated, return that objfile's architecture. */ | |
232 | ||
233 | struct gdbarch * | |
234 | get_type_arch (const struct type *type) | |
235 | { | |
236 | if (TYPE_OBJFILE_OWNED (type)) | |
237 | return get_objfile_arch (TYPE_OWNER (type).objfile); | |
238 | else | |
239 | return TYPE_OWNER (type).gdbarch; | |
240 | } | |
241 | ||
99ad9427 YQ |
242 | /* See gdbtypes.h. */ |
243 | ||
244 | struct type * | |
245 | get_target_type (struct type *type) | |
246 | { | |
247 | if (type != NULL) | |
248 | { | |
249 | type = TYPE_TARGET_TYPE (type); | |
250 | if (type != NULL) | |
251 | type = check_typedef (type); | |
252 | } | |
253 | ||
254 | return type; | |
255 | } | |
256 | ||
2e056931 SM |
257 | /* See gdbtypes.h. */ |
258 | ||
259 | unsigned int | |
260 | type_length_units (struct type *type) | |
261 | { | |
262 | struct gdbarch *arch = get_type_arch (type); | |
263 | int unit_size = gdbarch_addressable_memory_unit_size (arch); | |
264 | ||
265 | return TYPE_LENGTH (type) / unit_size; | |
266 | } | |
267 | ||
2fdde8f8 DJ |
268 | /* Alloc a new type instance structure, fill it with some defaults, |
269 | and point it at OLDTYPE. Allocate the new type instance from the | |
270 | same place as OLDTYPE. */ | |
271 | ||
272 | static struct type * | |
273 | alloc_type_instance (struct type *oldtype) | |
274 | { | |
275 | struct type *type; | |
276 | ||
277 | /* Allocate the structure. */ | |
278 | ||
e9bb382b | 279 | if (! TYPE_OBJFILE_OWNED (oldtype)) |
41bf6aca | 280 | type = XCNEW (struct type); |
2fdde8f8 | 281 | else |
1deafd4e PA |
282 | type = OBSTACK_ZALLOC (&TYPE_OBJFILE (oldtype)->objfile_obstack, |
283 | struct type); | |
284 | ||
2fdde8f8 DJ |
285 | TYPE_MAIN_TYPE (type) = TYPE_MAIN_TYPE (oldtype); |
286 | ||
287 | TYPE_CHAIN (type) = type; /* Chain back to itself for now. */ | |
288 | ||
c16abbde | 289 | return type; |
2fdde8f8 DJ |
290 | } |
291 | ||
292 | /* Clear all remnants of the previous type at TYPE, in preparation for | |
e9bb382b | 293 | replacing it with something else. Preserve owner information. */ |
5212577a | 294 | |
2fdde8f8 DJ |
295 | static void |
296 | smash_type (struct type *type) | |
297 | { | |
e9bb382b UW |
298 | int objfile_owned = TYPE_OBJFILE_OWNED (type); |
299 | union type_owner owner = TYPE_OWNER (type); | |
300 | ||
2fdde8f8 DJ |
301 | memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type)); |
302 | ||
e9bb382b UW |
303 | /* Restore owner information. */ |
304 | TYPE_OBJFILE_OWNED (type) = objfile_owned; | |
305 | TYPE_OWNER (type) = owner; | |
306 | ||
2fdde8f8 DJ |
307 | /* For now, delete the rings. */ |
308 | TYPE_CHAIN (type) = type; | |
309 | ||
310 | /* For now, leave the pointer/reference types alone. */ | |
311 | } | |
312 | ||
c906108c SS |
313 | /* Lookup a pointer to a type TYPE. TYPEPTR, if nonzero, points |
314 | to a pointer to memory where the pointer type should be stored. | |
315 | If *TYPEPTR is zero, update it to point to the pointer type we return. | |
316 | We allocate new memory if needed. */ | |
317 | ||
318 | struct type * | |
fba45db2 | 319 | make_pointer_type (struct type *type, struct type **typeptr) |
c906108c | 320 | { |
52f0bd74 | 321 | struct type *ntype; /* New type */ |
053cb41b | 322 | struct type *chain; |
c906108c SS |
323 | |
324 | ntype = TYPE_POINTER_TYPE (type); | |
325 | ||
c5aa993b | 326 | if (ntype) |
c906108c | 327 | { |
c5aa993b | 328 | if (typeptr == 0) |
7ba81444 MS |
329 | return ntype; /* Don't care about alloc, |
330 | and have new type. */ | |
c906108c | 331 | else if (*typeptr == 0) |
c5aa993b | 332 | { |
7ba81444 | 333 | *typeptr = ntype; /* Tracking alloc, and have new type. */ |
c906108c | 334 | return ntype; |
c5aa993b | 335 | } |
c906108c SS |
336 | } |
337 | ||
338 | if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */ | |
339 | { | |
e9bb382b | 340 | ntype = alloc_type_copy (type); |
c906108c SS |
341 | if (typeptr) |
342 | *typeptr = ntype; | |
343 | } | |
7ba81444 | 344 | else /* We have storage, but need to reset it. */ |
c906108c SS |
345 | { |
346 | ntype = *typeptr; | |
053cb41b | 347 | chain = TYPE_CHAIN (ntype); |
2fdde8f8 | 348 | smash_type (ntype); |
053cb41b | 349 | TYPE_CHAIN (ntype) = chain; |
c906108c SS |
350 | } |
351 | ||
352 | TYPE_TARGET_TYPE (ntype) = type; | |
353 | TYPE_POINTER_TYPE (type) = ntype; | |
354 | ||
5212577a | 355 | /* FIXME! Assumes the machine has only one representation for pointers! */ |
c906108c | 356 | |
50810684 UW |
357 | TYPE_LENGTH (ntype) |
358 | = gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT; | |
c906108c SS |
359 | TYPE_CODE (ntype) = TYPE_CODE_PTR; |
360 | ||
67b2adb2 | 361 | /* Mark pointers as unsigned. The target converts between pointers |
76e71323 | 362 | and addresses (CORE_ADDRs) using gdbarch_pointer_to_address and |
7ba81444 | 363 | gdbarch_address_to_pointer. */ |
876cecd0 | 364 | TYPE_UNSIGNED (ntype) = 1; |
c5aa993b | 365 | |
053cb41b JB |
366 | /* Update the length of all the other variants of this type. */ |
367 | chain = TYPE_CHAIN (ntype); | |
368 | while (chain != ntype) | |
369 | { | |
370 | TYPE_LENGTH (chain) = TYPE_LENGTH (ntype); | |
371 | chain = TYPE_CHAIN (chain); | |
372 | } | |
373 | ||
c906108c SS |
374 | return ntype; |
375 | } | |
376 | ||
377 | /* Given a type TYPE, return a type of pointers to that type. | |
378 | May need to construct such a type if this is the first use. */ | |
379 | ||
380 | struct type * | |
fba45db2 | 381 | lookup_pointer_type (struct type *type) |
c906108c | 382 | { |
c5aa993b | 383 | return make_pointer_type (type, (struct type **) 0); |
c906108c SS |
384 | } |
385 | ||
7ba81444 MS |
386 | /* Lookup a C++ `reference' to a type TYPE. TYPEPTR, if nonzero, |
387 | points to a pointer to memory where the reference type should be | |
388 | stored. If *TYPEPTR is zero, update it to point to the reference | |
3b224330 AV |
389 | type we return. We allocate new memory if needed. REFCODE denotes |
390 | the kind of reference type to lookup (lvalue or rvalue reference). */ | |
c906108c SS |
391 | |
392 | struct type * | |
3b224330 AV |
393 | make_reference_type (struct type *type, struct type **typeptr, |
394 | enum type_code refcode) | |
c906108c | 395 | { |
52f0bd74 | 396 | struct type *ntype; /* New type */ |
3b224330 | 397 | struct type **reftype; |
1e98b326 | 398 | struct type *chain; |
c906108c | 399 | |
3b224330 AV |
400 | gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF); |
401 | ||
402 | ntype = (refcode == TYPE_CODE_REF ? TYPE_REFERENCE_TYPE (type) | |
403 | : TYPE_RVALUE_REFERENCE_TYPE (type)); | |
c906108c | 404 | |
c5aa993b | 405 | if (ntype) |
c906108c | 406 | { |
c5aa993b | 407 | if (typeptr == 0) |
7ba81444 MS |
408 | return ntype; /* Don't care about alloc, |
409 | and have new type. */ | |
c906108c | 410 | else if (*typeptr == 0) |
c5aa993b | 411 | { |
7ba81444 | 412 | *typeptr = ntype; /* Tracking alloc, and have new type. */ |
c906108c | 413 | return ntype; |
c5aa993b | 414 | } |
c906108c SS |
415 | } |
416 | ||
417 | if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */ | |
418 | { | |
e9bb382b | 419 | ntype = alloc_type_copy (type); |
c906108c SS |
420 | if (typeptr) |
421 | *typeptr = ntype; | |
422 | } | |
7ba81444 | 423 | else /* We have storage, but need to reset it. */ |
c906108c SS |
424 | { |
425 | ntype = *typeptr; | |
1e98b326 | 426 | chain = TYPE_CHAIN (ntype); |
2fdde8f8 | 427 | smash_type (ntype); |
1e98b326 | 428 | TYPE_CHAIN (ntype) = chain; |
c906108c SS |
429 | } |
430 | ||
431 | TYPE_TARGET_TYPE (ntype) = type; | |
3b224330 AV |
432 | reftype = (refcode == TYPE_CODE_REF ? &TYPE_REFERENCE_TYPE (type) |
433 | : &TYPE_RVALUE_REFERENCE_TYPE (type)); | |
434 | ||
435 | *reftype = ntype; | |
c906108c | 436 | |
7ba81444 MS |
437 | /* FIXME! Assume the machine has only one representation for |
438 | references, and that it matches the (only) representation for | |
439 | pointers! */ | |
c906108c | 440 | |
50810684 UW |
441 | TYPE_LENGTH (ntype) = |
442 | gdbarch_ptr_bit (get_type_arch (type)) / TARGET_CHAR_BIT; | |
3b224330 | 443 | TYPE_CODE (ntype) = refcode; |
c5aa993b | 444 | |
3b224330 | 445 | *reftype = ntype; |
c906108c | 446 | |
1e98b326 JB |
447 | /* Update the length of all the other variants of this type. */ |
448 | chain = TYPE_CHAIN (ntype); | |
449 | while (chain != ntype) | |
450 | { | |
451 | TYPE_LENGTH (chain) = TYPE_LENGTH (ntype); | |
452 | chain = TYPE_CHAIN (chain); | |
453 | } | |
454 | ||
c906108c SS |
455 | return ntype; |
456 | } | |
457 | ||
7ba81444 MS |
458 | /* Same as above, but caller doesn't care about memory allocation |
459 | details. */ | |
c906108c SS |
460 | |
461 | struct type * | |
3b224330 AV |
462 | lookup_reference_type (struct type *type, enum type_code refcode) |
463 | { | |
464 | return make_reference_type (type, (struct type **) 0, refcode); | |
465 | } | |
466 | ||
467 | /* Lookup the lvalue reference type for the type TYPE. */ | |
468 | ||
469 | struct type * | |
470 | lookup_lvalue_reference_type (struct type *type) | |
471 | { | |
472 | return lookup_reference_type (type, TYPE_CODE_REF); | |
473 | } | |
474 | ||
475 | /* Lookup the rvalue reference type for the type TYPE. */ | |
476 | ||
477 | struct type * | |
478 | lookup_rvalue_reference_type (struct type *type) | |
c906108c | 479 | { |
3b224330 | 480 | return lookup_reference_type (type, TYPE_CODE_RVALUE_REF); |
c906108c SS |
481 | } |
482 | ||
7ba81444 MS |
483 | /* Lookup a function type that returns type TYPE. TYPEPTR, if |
484 | nonzero, points to a pointer to memory where the function type | |
485 | should be stored. If *TYPEPTR is zero, update it to point to the | |
0c8b41f1 | 486 | function type we return. We allocate new memory if needed. */ |
c906108c SS |
487 | |
488 | struct type * | |
0c8b41f1 | 489 | make_function_type (struct type *type, struct type **typeptr) |
c906108c | 490 | { |
52f0bd74 | 491 | struct type *ntype; /* New type */ |
c906108c SS |
492 | |
493 | if (typeptr == 0 || *typeptr == 0) /* We'll need to allocate one. */ | |
494 | { | |
e9bb382b | 495 | ntype = alloc_type_copy (type); |
c906108c SS |
496 | if (typeptr) |
497 | *typeptr = ntype; | |
498 | } | |
7ba81444 | 499 | else /* We have storage, but need to reset it. */ |
c906108c SS |
500 | { |
501 | ntype = *typeptr; | |
2fdde8f8 | 502 | smash_type (ntype); |
c906108c SS |
503 | } |
504 | ||
505 | TYPE_TARGET_TYPE (ntype) = type; | |
506 | ||
507 | TYPE_LENGTH (ntype) = 1; | |
508 | TYPE_CODE (ntype) = TYPE_CODE_FUNC; | |
c5aa993b | 509 | |
b6cdc2c1 JK |
510 | INIT_FUNC_SPECIFIC (ntype); |
511 | ||
c906108c SS |
512 | return ntype; |
513 | } | |
514 | ||
c906108c SS |
515 | /* Given a type TYPE, return a type of functions that return that type. |
516 | May need to construct such a type if this is the first use. */ | |
517 | ||
518 | struct type * | |
fba45db2 | 519 | lookup_function_type (struct type *type) |
c906108c | 520 | { |
0c8b41f1 | 521 | return make_function_type (type, (struct type **) 0); |
c906108c SS |
522 | } |
523 | ||
71918a86 | 524 | /* Given a type TYPE and argument types, return the appropriate |
a6fb9c08 TT |
525 | function type. If the final type in PARAM_TYPES is NULL, make a |
526 | varargs function. */ | |
71918a86 TT |
527 | |
528 | struct type * | |
529 | lookup_function_type_with_arguments (struct type *type, | |
530 | int nparams, | |
531 | struct type **param_types) | |
532 | { | |
533 | struct type *fn = make_function_type (type, (struct type **) 0); | |
534 | int i; | |
535 | ||
e314d629 | 536 | if (nparams > 0) |
a6fb9c08 | 537 | { |
e314d629 TT |
538 | if (param_types[nparams - 1] == NULL) |
539 | { | |
540 | --nparams; | |
541 | TYPE_VARARGS (fn) = 1; | |
542 | } | |
543 | else if (TYPE_CODE (check_typedef (param_types[nparams - 1])) | |
544 | == TYPE_CODE_VOID) | |
545 | { | |
546 | --nparams; | |
547 | /* Caller should have ensured this. */ | |
548 | gdb_assert (nparams == 0); | |
549 | TYPE_PROTOTYPED (fn) = 1; | |
550 | } | |
54990598 PA |
551 | else |
552 | TYPE_PROTOTYPED (fn) = 1; | |
a6fb9c08 TT |
553 | } |
554 | ||
71918a86 | 555 | TYPE_NFIELDS (fn) = nparams; |
224c3ddb SM |
556 | TYPE_FIELDS (fn) |
557 | = (struct field *) TYPE_ZALLOC (fn, nparams * sizeof (struct field)); | |
71918a86 TT |
558 | for (i = 0; i < nparams; ++i) |
559 | TYPE_FIELD_TYPE (fn, i) = param_types[i]; | |
560 | ||
561 | return fn; | |
562 | } | |
563 | ||
47663de5 MS |
564 | /* Identify address space identifier by name -- |
565 | return the integer flag defined in gdbtypes.h. */ | |
5212577a DE |
566 | |
567 | int | |
50810684 | 568 | address_space_name_to_int (struct gdbarch *gdbarch, char *space_identifier) |
47663de5 | 569 | { |
8b2dbe47 | 570 | int type_flags; |
d8734c88 | 571 | |
7ba81444 | 572 | /* Check for known address space delimiters. */ |
47663de5 | 573 | if (!strcmp (space_identifier, "code")) |
876cecd0 | 574 | return TYPE_INSTANCE_FLAG_CODE_SPACE; |
47663de5 | 575 | else if (!strcmp (space_identifier, "data")) |
876cecd0 | 576 | return TYPE_INSTANCE_FLAG_DATA_SPACE; |
5f11f355 AC |
577 | else if (gdbarch_address_class_name_to_type_flags_p (gdbarch) |
578 | && gdbarch_address_class_name_to_type_flags (gdbarch, | |
579 | space_identifier, | |
580 | &type_flags)) | |
8b2dbe47 | 581 | return type_flags; |
47663de5 | 582 | else |
8a3fe4f8 | 583 | error (_("Unknown address space specifier: \"%s\""), space_identifier); |
47663de5 MS |
584 | } |
585 | ||
586 | /* Identify address space identifier by integer flag as defined in | |
7ba81444 | 587 | gdbtypes.h -- return the string version of the adress space name. */ |
47663de5 | 588 | |
321432c0 | 589 | const char * |
50810684 | 590 | address_space_int_to_name (struct gdbarch *gdbarch, int space_flag) |
47663de5 | 591 | { |
876cecd0 | 592 | if (space_flag & TYPE_INSTANCE_FLAG_CODE_SPACE) |
47663de5 | 593 | return "code"; |
876cecd0 | 594 | else if (space_flag & TYPE_INSTANCE_FLAG_DATA_SPACE) |
47663de5 | 595 | return "data"; |
876cecd0 | 596 | else if ((space_flag & TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL) |
5f11f355 AC |
597 | && gdbarch_address_class_type_flags_to_name_p (gdbarch)) |
598 | return gdbarch_address_class_type_flags_to_name (gdbarch, space_flag); | |
47663de5 MS |
599 | else |
600 | return NULL; | |
601 | } | |
602 | ||
2fdde8f8 | 603 | /* Create a new type with instance flags NEW_FLAGS, based on TYPE. |
ad766c0a JB |
604 | |
605 | If STORAGE is non-NULL, create the new type instance there. | |
606 | STORAGE must be in the same obstack as TYPE. */ | |
47663de5 | 607 | |
b9362cc7 | 608 | static struct type * |
2fdde8f8 DJ |
609 | make_qualified_type (struct type *type, int new_flags, |
610 | struct type *storage) | |
47663de5 MS |
611 | { |
612 | struct type *ntype; | |
613 | ||
614 | ntype = type; | |
5f61c20e JK |
615 | do |
616 | { | |
617 | if (TYPE_INSTANCE_FLAGS (ntype) == new_flags) | |
618 | return ntype; | |
619 | ntype = TYPE_CHAIN (ntype); | |
620 | } | |
621 | while (ntype != type); | |
47663de5 | 622 | |
2fdde8f8 DJ |
623 | /* Create a new type instance. */ |
624 | if (storage == NULL) | |
625 | ntype = alloc_type_instance (type); | |
626 | else | |
627 | { | |
7ba81444 MS |
628 | /* If STORAGE was provided, it had better be in the same objfile |
629 | as TYPE. Otherwise, we can't link it into TYPE's cv chain: | |
630 | if one objfile is freed and the other kept, we'd have | |
631 | dangling pointers. */ | |
ad766c0a JB |
632 | gdb_assert (TYPE_OBJFILE (type) == TYPE_OBJFILE (storage)); |
633 | ||
2fdde8f8 DJ |
634 | ntype = storage; |
635 | TYPE_MAIN_TYPE (ntype) = TYPE_MAIN_TYPE (type); | |
636 | TYPE_CHAIN (ntype) = ntype; | |
637 | } | |
47663de5 MS |
638 | |
639 | /* Pointers or references to the original type are not relevant to | |
2fdde8f8 | 640 | the new type. */ |
47663de5 MS |
641 | TYPE_POINTER_TYPE (ntype) = (struct type *) 0; |
642 | TYPE_REFERENCE_TYPE (ntype) = (struct type *) 0; | |
47663de5 | 643 | |
2fdde8f8 DJ |
644 | /* Chain the new qualified type to the old type. */ |
645 | TYPE_CHAIN (ntype) = TYPE_CHAIN (type); | |
646 | TYPE_CHAIN (type) = ntype; | |
647 | ||
648 | /* Now set the instance flags and return the new type. */ | |
649 | TYPE_INSTANCE_FLAGS (ntype) = new_flags; | |
47663de5 | 650 | |
ab5d3da6 KB |
651 | /* Set length of new type to that of the original type. */ |
652 | TYPE_LENGTH (ntype) = TYPE_LENGTH (type); | |
653 | ||
47663de5 MS |
654 | return ntype; |
655 | } | |
656 | ||
2fdde8f8 DJ |
657 | /* Make an address-space-delimited variant of a type -- a type that |
658 | is identical to the one supplied except that it has an address | |
659 | space attribute attached to it (such as "code" or "data"). | |
660 | ||
7ba81444 MS |
661 | The space attributes "code" and "data" are for Harvard |
662 | architectures. The address space attributes are for architectures | |
663 | which have alternately sized pointers or pointers with alternate | |
664 | representations. */ | |
2fdde8f8 DJ |
665 | |
666 | struct type * | |
667 | make_type_with_address_space (struct type *type, int space_flag) | |
668 | { | |
2fdde8f8 | 669 | int new_flags = ((TYPE_INSTANCE_FLAGS (type) |
876cecd0 TT |
670 | & ~(TYPE_INSTANCE_FLAG_CODE_SPACE |
671 | | TYPE_INSTANCE_FLAG_DATA_SPACE | |
672 | | TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)) | |
2fdde8f8 DJ |
673 | | space_flag); |
674 | ||
675 | return make_qualified_type (type, new_flags, NULL); | |
676 | } | |
c906108c SS |
677 | |
678 | /* Make a "c-v" variant of a type -- a type that is identical to the | |
679 | one supplied except that it may have const or volatile attributes | |
680 | CNST is a flag for setting the const attribute | |
681 | VOLTL is a flag for setting the volatile attribute | |
682 | TYPE is the base type whose variant we are creating. | |
c906108c | 683 | |
ad766c0a JB |
684 | If TYPEPTR and *TYPEPTR are non-zero, then *TYPEPTR points to |
685 | storage to hold the new qualified type; *TYPEPTR and TYPE must be | |
686 | in the same objfile. Otherwise, allocate fresh memory for the new | |
687 | type whereever TYPE lives. If TYPEPTR is non-zero, set it to the | |
688 | new type we construct. */ | |
5212577a | 689 | |
c906108c | 690 | struct type * |
7ba81444 MS |
691 | make_cv_type (int cnst, int voltl, |
692 | struct type *type, | |
693 | struct type **typeptr) | |
c906108c | 694 | { |
52f0bd74 | 695 | struct type *ntype; /* New type */ |
c906108c | 696 | |
2fdde8f8 | 697 | int new_flags = (TYPE_INSTANCE_FLAGS (type) |
308d96ed MS |
698 | & ~(TYPE_INSTANCE_FLAG_CONST |
699 | | TYPE_INSTANCE_FLAG_VOLATILE)); | |
c906108c | 700 | |
c906108c | 701 | if (cnst) |
876cecd0 | 702 | new_flags |= TYPE_INSTANCE_FLAG_CONST; |
c906108c SS |
703 | |
704 | if (voltl) | |
876cecd0 | 705 | new_flags |= TYPE_INSTANCE_FLAG_VOLATILE; |
a02fd225 | 706 | |
2fdde8f8 | 707 | if (typeptr && *typeptr != NULL) |
a02fd225 | 708 | { |
ad766c0a JB |
709 | /* TYPE and *TYPEPTR must be in the same objfile. We can't have |
710 | a C-V variant chain that threads across objfiles: if one | |
711 | objfile gets freed, then the other has a broken C-V chain. | |
712 | ||
713 | This code used to try to copy over the main type from TYPE to | |
714 | *TYPEPTR if they were in different objfiles, but that's | |
715 | wrong, too: TYPE may have a field list or member function | |
716 | lists, which refer to types of their own, etc. etc. The | |
717 | whole shebang would need to be copied over recursively; you | |
718 | can't have inter-objfile pointers. The only thing to do is | |
719 | to leave stub types as stub types, and look them up afresh by | |
720 | name each time you encounter them. */ | |
721 | gdb_assert (TYPE_OBJFILE (*typeptr) == TYPE_OBJFILE (type)); | |
2fdde8f8 DJ |
722 | } |
723 | ||
7ba81444 MS |
724 | ntype = make_qualified_type (type, new_flags, |
725 | typeptr ? *typeptr : NULL); | |
c906108c | 726 | |
2fdde8f8 DJ |
727 | if (typeptr != NULL) |
728 | *typeptr = ntype; | |
a02fd225 | 729 | |
2fdde8f8 | 730 | return ntype; |
a02fd225 | 731 | } |
c906108c | 732 | |
06d66ee9 TT |
733 | /* Make a 'restrict'-qualified version of TYPE. */ |
734 | ||
735 | struct type * | |
736 | make_restrict_type (struct type *type) | |
737 | { | |
738 | return make_qualified_type (type, | |
739 | (TYPE_INSTANCE_FLAGS (type) | |
740 | | TYPE_INSTANCE_FLAG_RESTRICT), | |
741 | NULL); | |
742 | } | |
743 | ||
f1660027 TT |
744 | /* Make a type without const, volatile, or restrict. */ |
745 | ||
746 | struct type * | |
747 | make_unqualified_type (struct type *type) | |
748 | { | |
749 | return make_qualified_type (type, | |
750 | (TYPE_INSTANCE_FLAGS (type) | |
751 | & ~(TYPE_INSTANCE_FLAG_CONST | |
752 | | TYPE_INSTANCE_FLAG_VOLATILE | |
753 | | TYPE_INSTANCE_FLAG_RESTRICT)), | |
754 | NULL); | |
755 | } | |
756 | ||
a2c2acaf MW |
757 | /* Make a '_Atomic'-qualified version of TYPE. */ |
758 | ||
759 | struct type * | |
760 | make_atomic_type (struct type *type) | |
761 | { | |
762 | return make_qualified_type (type, | |
763 | (TYPE_INSTANCE_FLAGS (type) | |
764 | | TYPE_INSTANCE_FLAG_ATOMIC), | |
765 | NULL); | |
766 | } | |
767 | ||
2fdde8f8 DJ |
768 | /* Replace the contents of ntype with the type *type. This changes the |
769 | contents, rather than the pointer for TYPE_MAIN_TYPE (ntype); thus | |
770 | the changes are propogated to all types in the TYPE_CHAIN. | |
dd6bda65 | 771 | |
cda6c68a JB |
772 | In order to build recursive types, it's inevitable that we'll need |
773 | to update types in place --- but this sort of indiscriminate | |
774 | smashing is ugly, and needs to be replaced with something more | |
2fdde8f8 DJ |
775 | controlled. TYPE_MAIN_TYPE is a step in this direction; it's not |
776 | clear if more steps are needed. */ | |
5212577a | 777 | |
dd6bda65 DJ |
778 | void |
779 | replace_type (struct type *ntype, struct type *type) | |
780 | { | |
ab5d3da6 | 781 | struct type *chain; |
dd6bda65 | 782 | |
ad766c0a JB |
783 | /* These two types had better be in the same objfile. Otherwise, |
784 | the assignment of one type's main type structure to the other | |
785 | will produce a type with references to objects (names; field | |
786 | lists; etc.) allocated on an objfile other than its own. */ | |
e46dd0f4 | 787 | gdb_assert (TYPE_OBJFILE (ntype) == TYPE_OBJFILE (type)); |
ad766c0a | 788 | |
2fdde8f8 | 789 | *TYPE_MAIN_TYPE (ntype) = *TYPE_MAIN_TYPE (type); |
dd6bda65 | 790 | |
7ba81444 MS |
791 | /* The type length is not a part of the main type. Update it for |
792 | each type on the variant chain. */ | |
ab5d3da6 | 793 | chain = ntype; |
5f61c20e JK |
794 | do |
795 | { | |
796 | /* Assert that this element of the chain has no address-class bits | |
797 | set in its flags. Such type variants might have type lengths | |
798 | which are supposed to be different from the non-address-class | |
799 | variants. This assertion shouldn't ever be triggered because | |
800 | symbol readers which do construct address-class variants don't | |
801 | call replace_type(). */ | |
802 | gdb_assert (TYPE_ADDRESS_CLASS_ALL (chain) == 0); | |
803 | ||
804 | TYPE_LENGTH (chain) = TYPE_LENGTH (type); | |
805 | chain = TYPE_CHAIN (chain); | |
806 | } | |
807 | while (ntype != chain); | |
ab5d3da6 | 808 | |
2fdde8f8 DJ |
809 | /* Assert that the two types have equivalent instance qualifiers. |
810 | This should be true for at least all of our debug readers. */ | |
811 | gdb_assert (TYPE_INSTANCE_FLAGS (ntype) == TYPE_INSTANCE_FLAGS (type)); | |
dd6bda65 DJ |
812 | } |
813 | ||
c906108c SS |
814 | /* Implement direct support for MEMBER_TYPE in GNU C++. |
815 | May need to construct such a type if this is the first use. | |
816 | The TYPE is the type of the member. The DOMAIN is the type | |
817 | of the aggregate that the member belongs to. */ | |
818 | ||
819 | struct type * | |
0d5de010 | 820 | lookup_memberptr_type (struct type *type, struct type *domain) |
c906108c | 821 | { |
52f0bd74 | 822 | struct type *mtype; |
c906108c | 823 | |
e9bb382b | 824 | mtype = alloc_type_copy (type); |
0d5de010 | 825 | smash_to_memberptr_type (mtype, domain, type); |
c16abbde | 826 | return mtype; |
c906108c SS |
827 | } |
828 | ||
0d5de010 DJ |
829 | /* Return a pointer-to-method type, for a method of type TO_TYPE. */ |
830 | ||
831 | struct type * | |
832 | lookup_methodptr_type (struct type *to_type) | |
833 | { | |
834 | struct type *mtype; | |
835 | ||
e9bb382b | 836 | mtype = alloc_type_copy (to_type); |
0b92b5bb | 837 | smash_to_methodptr_type (mtype, to_type); |
0d5de010 DJ |
838 | return mtype; |
839 | } | |
840 | ||
7ba81444 MS |
841 | /* Allocate a stub method whose return type is TYPE. This apparently |
842 | happens for speed of symbol reading, since parsing out the | |
843 | arguments to the method is cpu-intensive, the way we are doing it. | |
844 | So, we will fill in arguments later. This always returns a fresh | |
845 | type. */ | |
c906108c SS |
846 | |
847 | struct type * | |
fba45db2 | 848 | allocate_stub_method (struct type *type) |
c906108c SS |
849 | { |
850 | struct type *mtype; | |
851 | ||
e9bb382b UW |
852 | mtype = alloc_type_copy (type); |
853 | TYPE_CODE (mtype) = TYPE_CODE_METHOD; | |
854 | TYPE_LENGTH (mtype) = 1; | |
855 | TYPE_STUB (mtype) = 1; | |
c906108c | 856 | TYPE_TARGET_TYPE (mtype) = type; |
4bfb94b8 | 857 | /* TYPE_SELF_TYPE (mtype) = unknown yet */ |
c16abbde | 858 | return mtype; |
c906108c SS |
859 | } |
860 | ||
729efb13 SA |
861 | /* Create a range type with a dynamic range from LOW_BOUND to |
862 | HIGH_BOUND, inclusive. See create_range_type for further details. */ | |
c906108c SS |
863 | |
864 | struct type * | |
729efb13 SA |
865 | create_range_type (struct type *result_type, struct type *index_type, |
866 | const struct dynamic_prop *low_bound, | |
867 | const struct dynamic_prop *high_bound) | |
c906108c SS |
868 | { |
869 | if (result_type == NULL) | |
e9bb382b | 870 | result_type = alloc_type_copy (index_type); |
c906108c SS |
871 | TYPE_CODE (result_type) = TYPE_CODE_RANGE; |
872 | TYPE_TARGET_TYPE (result_type) = index_type; | |
74a9bb82 | 873 | if (TYPE_STUB (index_type)) |
876cecd0 | 874 | TYPE_TARGET_STUB (result_type) = 1; |
c906108c SS |
875 | else |
876 | TYPE_LENGTH (result_type) = TYPE_LENGTH (check_typedef (index_type)); | |
729efb13 | 877 | |
43bbcdc2 PH |
878 | TYPE_RANGE_DATA (result_type) = (struct range_bounds *) |
879 | TYPE_ZALLOC (result_type, sizeof (struct range_bounds)); | |
729efb13 SA |
880 | TYPE_RANGE_DATA (result_type)->low = *low_bound; |
881 | TYPE_RANGE_DATA (result_type)->high = *high_bound; | |
c906108c | 882 | |
729efb13 | 883 | if (low_bound->kind == PROP_CONST && low_bound->data.const_val >= 0) |
876cecd0 | 884 | TYPE_UNSIGNED (result_type) = 1; |
c906108c | 885 | |
45e44d27 JB |
886 | /* Ada allows the declaration of range types whose upper bound is |
887 | less than the lower bound, so checking the lower bound is not | |
888 | enough. Make sure we do not mark a range type whose upper bound | |
889 | is negative as unsigned. */ | |
890 | if (high_bound->kind == PROP_CONST && high_bound->data.const_val < 0) | |
891 | TYPE_UNSIGNED (result_type) = 0; | |
892 | ||
262452ec | 893 | return result_type; |
c906108c SS |
894 | } |
895 | ||
729efb13 SA |
896 | /* Create a range type using either a blank type supplied in |
897 | RESULT_TYPE, or creating a new type, inheriting the objfile from | |
898 | INDEX_TYPE. | |
899 | ||
900 | Indices will be of type INDEX_TYPE, and will range from LOW_BOUND | |
901 | to HIGH_BOUND, inclusive. | |
902 | ||
903 | FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make | |
904 | sure it is TYPE_CODE_UNDEF before we bash it into a range type? */ | |
905 | ||
906 | struct type * | |
907 | create_static_range_type (struct type *result_type, struct type *index_type, | |
908 | LONGEST low_bound, LONGEST high_bound) | |
909 | { | |
910 | struct dynamic_prop low, high; | |
911 | ||
912 | low.kind = PROP_CONST; | |
913 | low.data.const_val = low_bound; | |
914 | ||
915 | high.kind = PROP_CONST; | |
916 | high.data.const_val = high_bound; | |
917 | ||
918 | result_type = create_range_type (result_type, index_type, &low, &high); | |
919 | ||
920 | return result_type; | |
921 | } | |
922 | ||
80180f79 SA |
923 | /* Predicate tests whether BOUNDS are static. Returns 1 if all bounds values |
924 | are static, otherwise returns 0. */ | |
925 | ||
926 | static int | |
927 | has_static_range (const struct range_bounds *bounds) | |
928 | { | |
929 | return (bounds->low.kind == PROP_CONST | |
930 | && bounds->high.kind == PROP_CONST); | |
931 | } | |
932 | ||
933 | ||
7ba81444 MS |
934 | /* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type |
935 | TYPE. Return 1 if type is a range type, 0 if it is discrete (and | |
936 | bounds will fit in LONGEST), or -1 otherwise. */ | |
c906108c SS |
937 | |
938 | int | |
fba45db2 | 939 | get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp) |
c906108c | 940 | { |
f168693b | 941 | type = check_typedef (type); |
c906108c SS |
942 | switch (TYPE_CODE (type)) |
943 | { | |
944 | case TYPE_CODE_RANGE: | |
945 | *lowp = TYPE_LOW_BOUND (type); | |
946 | *highp = TYPE_HIGH_BOUND (type); | |
947 | return 1; | |
948 | case TYPE_CODE_ENUM: | |
949 | if (TYPE_NFIELDS (type) > 0) | |
950 | { | |
951 | /* The enums may not be sorted by value, so search all | |
0963b4bd | 952 | entries. */ |
c906108c SS |
953 | int i; |
954 | ||
14e75d8e | 955 | *lowp = *highp = TYPE_FIELD_ENUMVAL (type, 0); |
c906108c SS |
956 | for (i = 0; i < TYPE_NFIELDS (type); i++) |
957 | { | |
14e75d8e JK |
958 | if (TYPE_FIELD_ENUMVAL (type, i) < *lowp) |
959 | *lowp = TYPE_FIELD_ENUMVAL (type, i); | |
960 | if (TYPE_FIELD_ENUMVAL (type, i) > *highp) | |
961 | *highp = TYPE_FIELD_ENUMVAL (type, i); | |
c906108c SS |
962 | } |
963 | ||
7ba81444 | 964 | /* Set unsigned indicator if warranted. */ |
c5aa993b | 965 | if (*lowp >= 0) |
c906108c | 966 | { |
876cecd0 | 967 | TYPE_UNSIGNED (type) = 1; |
c906108c SS |
968 | } |
969 | } | |
970 | else | |
971 | { | |
972 | *lowp = 0; | |
973 | *highp = -1; | |
974 | } | |
975 | return 0; | |
976 | case TYPE_CODE_BOOL: | |
977 | *lowp = 0; | |
978 | *highp = 1; | |
979 | return 0; | |
980 | case TYPE_CODE_INT: | |
c5aa993b | 981 | if (TYPE_LENGTH (type) > sizeof (LONGEST)) /* Too big */ |
c906108c SS |
982 | return -1; |
983 | if (!TYPE_UNSIGNED (type)) | |
984 | { | |
c5aa993b | 985 | *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1)); |
c906108c SS |
986 | *highp = -*lowp - 1; |
987 | return 0; | |
988 | } | |
7ba81444 | 989 | /* ... fall through for unsigned ints ... */ |
c906108c SS |
990 | case TYPE_CODE_CHAR: |
991 | *lowp = 0; | |
992 | /* This round-about calculation is to avoid shifting by | |
7b83ea04 | 993 | TYPE_LENGTH (type) * TARGET_CHAR_BIT, which will not work |
7ba81444 | 994 | if TYPE_LENGTH (type) == sizeof (LONGEST). */ |
c906108c SS |
995 | *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1); |
996 | *highp = (*highp - 1) | *highp; | |
997 | return 0; | |
998 | default: | |
999 | return -1; | |
1000 | } | |
1001 | } | |
1002 | ||
dbc98a8b KW |
1003 | /* Assuming TYPE is a simple, non-empty array type, compute its upper |
1004 | and lower bound. Save the low bound into LOW_BOUND if not NULL. | |
1005 | Save the high bound into HIGH_BOUND if not NULL. | |
1006 | ||
0963b4bd | 1007 | Return 1 if the operation was successful. Return zero otherwise, |
dbc98a8b KW |
1008 | in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified. |
1009 | ||
1010 | We now simply use get_discrete_bounds call to get the values | |
1011 | of the low and high bounds. | |
1012 | get_discrete_bounds can return three values: | |
1013 | 1, meaning that index is a range, | |
1014 | 0, meaning that index is a discrete type, | |
1015 | or -1 for failure. */ | |
1016 | ||
1017 | int | |
1018 | get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound) | |
1019 | { | |
1020 | struct type *index = TYPE_INDEX_TYPE (type); | |
1021 | LONGEST low = 0; | |
1022 | LONGEST high = 0; | |
1023 | int res; | |
1024 | ||
1025 | if (index == NULL) | |
1026 | return 0; | |
1027 | ||
1028 | res = get_discrete_bounds (index, &low, &high); | |
1029 | if (res == -1) | |
1030 | return 0; | |
1031 | ||
1032 | /* Check if the array bounds are undefined. */ | |
1033 | if (res == 1 | |
1034 | && ((low_bound && TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED (type)) | |
1035 | || (high_bound && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type)))) | |
1036 | return 0; | |
1037 | ||
1038 | if (low_bound) | |
1039 | *low_bound = low; | |
1040 | ||
1041 | if (high_bound) | |
1042 | *high_bound = high; | |
1043 | ||
1044 | return 1; | |
1045 | } | |
1046 | ||
aa715135 JG |
1047 | /* Assuming that TYPE is a discrete type and VAL is a valid integer |
1048 | representation of a value of this type, save the corresponding | |
1049 | position number in POS. | |
1050 | ||
1051 | Its differs from VAL only in the case of enumeration types. In | |
1052 | this case, the position number of the value of the first listed | |
1053 | enumeration literal is zero; the position number of the value of | |
1054 | each subsequent enumeration literal is one more than that of its | |
1055 | predecessor in the list. | |
1056 | ||
1057 | Return 1 if the operation was successful. Return zero otherwise, | |
1058 | in which case the value of POS is unmodified. | |
1059 | */ | |
1060 | ||
1061 | int | |
1062 | discrete_position (struct type *type, LONGEST val, LONGEST *pos) | |
1063 | { | |
1064 | if (TYPE_CODE (type) == TYPE_CODE_ENUM) | |
1065 | { | |
1066 | int i; | |
1067 | ||
1068 | for (i = 0; i < TYPE_NFIELDS (type); i += 1) | |
1069 | { | |
1070 | if (val == TYPE_FIELD_ENUMVAL (type, i)) | |
1071 | { | |
1072 | *pos = i; | |
1073 | return 1; | |
1074 | } | |
1075 | } | |
1076 | /* Invalid enumeration value. */ | |
1077 | return 0; | |
1078 | } | |
1079 | else | |
1080 | { | |
1081 | *pos = val; | |
1082 | return 1; | |
1083 | } | |
1084 | } | |
1085 | ||
7ba81444 MS |
1086 | /* Create an array type using either a blank type supplied in |
1087 | RESULT_TYPE, or creating a new type, inheriting the objfile from | |
1088 | RANGE_TYPE. | |
c906108c SS |
1089 | |
1090 | Elements will be of type ELEMENT_TYPE, the indices will be of type | |
1091 | RANGE_TYPE. | |
1092 | ||
a405673c JB |
1093 | BYTE_STRIDE_PROP, when not NULL, provides the array's byte stride. |
1094 | This byte stride property is added to the resulting array type | |
1095 | as a DYN_PROP_BYTE_STRIDE. As a consequence, the BYTE_STRIDE_PROP | |
1096 | argument can only be used to create types that are objfile-owned | |
1097 | (see add_dyn_prop), meaning that either this function must be called | |
1098 | with an objfile-owned RESULT_TYPE, or an objfile-owned RANGE_TYPE. | |
1099 | ||
1100 | BIT_STRIDE is taken into account only when BYTE_STRIDE_PROP is NULL. | |
dc53a7ad JB |
1101 | If BIT_STRIDE is not zero, build a packed array type whose element |
1102 | size is BIT_STRIDE. Otherwise, ignore this parameter. | |
1103 | ||
7ba81444 MS |
1104 | FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make |
1105 | sure it is TYPE_CODE_UNDEF before we bash it into an array | |
1106 | type? */ | |
c906108c SS |
1107 | |
1108 | struct type * | |
dc53a7ad JB |
1109 | create_array_type_with_stride (struct type *result_type, |
1110 | struct type *element_type, | |
1111 | struct type *range_type, | |
a405673c | 1112 | struct dynamic_prop *byte_stride_prop, |
dc53a7ad | 1113 | unsigned int bit_stride) |
c906108c | 1114 | { |
a405673c JB |
1115 | if (byte_stride_prop != NULL |
1116 | && byte_stride_prop->kind == PROP_CONST) | |
1117 | { | |
1118 | /* The byte stride is actually not dynamic. Pretend we were | |
1119 | called with bit_stride set instead of byte_stride_prop. | |
1120 | This will give us the same result type, while avoiding | |
1121 | the need to handle this as a special case. */ | |
1122 | bit_stride = byte_stride_prop->data.const_val * 8; | |
1123 | byte_stride_prop = NULL; | |
1124 | } | |
1125 | ||
c906108c | 1126 | if (result_type == NULL) |
e9bb382b UW |
1127 | result_type = alloc_type_copy (range_type); |
1128 | ||
c906108c SS |
1129 | TYPE_CODE (result_type) = TYPE_CODE_ARRAY; |
1130 | TYPE_TARGET_TYPE (result_type) = element_type; | |
a405673c JB |
1131 | if (byte_stride_prop == NULL |
1132 | && has_static_range (TYPE_RANGE_DATA (range_type)) | |
b4a7fcab JB |
1133 | && (!type_not_associated (result_type) |
1134 | && !type_not_allocated (result_type))) | |
80180f79 SA |
1135 | { |
1136 | LONGEST low_bound, high_bound; | |
1137 | ||
1138 | if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0) | |
1139 | low_bound = high_bound = 0; | |
f168693b | 1140 | element_type = check_typedef (element_type); |
80180f79 SA |
1141 | /* Be careful when setting the array length. Ada arrays can be |
1142 | empty arrays with the high_bound being smaller than the low_bound. | |
1143 | In such cases, the array length should be zero. */ | |
1144 | if (high_bound < low_bound) | |
1145 | TYPE_LENGTH (result_type) = 0; | |
1146 | else if (bit_stride > 0) | |
1147 | TYPE_LENGTH (result_type) = | |
1148 | (bit_stride * (high_bound - low_bound + 1) + 7) / 8; | |
1149 | else | |
1150 | TYPE_LENGTH (result_type) = | |
1151 | TYPE_LENGTH (element_type) * (high_bound - low_bound + 1); | |
1152 | } | |
ab0d6e0d | 1153 | else |
80180f79 SA |
1154 | { |
1155 | /* This type is dynamic and its length needs to be computed | |
1156 | on demand. In the meantime, avoid leaving the TYPE_LENGTH | |
1157 | undefined by setting it to zero. Although we are not expected | |
1158 | to trust TYPE_LENGTH in this case, setting the size to zero | |
1159 | allows us to avoid allocating objects of random sizes in case | |
1160 | we accidently do. */ | |
1161 | TYPE_LENGTH (result_type) = 0; | |
1162 | } | |
1163 | ||
c906108c SS |
1164 | TYPE_NFIELDS (result_type) = 1; |
1165 | TYPE_FIELDS (result_type) = | |
1deafd4e | 1166 | (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field)); |
262452ec | 1167 | TYPE_INDEX_TYPE (result_type) = range_type; |
a405673c JB |
1168 | if (byte_stride_prop != NULL) |
1169 | add_dyn_prop (DYN_PROP_BYTE_STRIDE, *byte_stride_prop, result_type, | |
1170 | TYPE_OBJFILE (result_type)); | |
1171 | else if (bit_stride > 0) | |
dc53a7ad | 1172 | TYPE_FIELD_BITSIZE (result_type, 0) = bit_stride; |
c906108c | 1173 | |
a9ff5f12 | 1174 | /* TYPE_TARGET_STUB will take care of zero length arrays. */ |
c906108c | 1175 | if (TYPE_LENGTH (result_type) == 0) |
876cecd0 | 1176 | TYPE_TARGET_STUB (result_type) = 1; |
c906108c | 1177 | |
c16abbde | 1178 | return result_type; |
c906108c SS |
1179 | } |
1180 | ||
dc53a7ad JB |
1181 | /* Same as create_array_type_with_stride but with no bit_stride |
1182 | (BIT_STRIDE = 0), thus building an unpacked array. */ | |
1183 | ||
1184 | struct type * | |
1185 | create_array_type (struct type *result_type, | |
1186 | struct type *element_type, | |
1187 | struct type *range_type) | |
1188 | { | |
1189 | return create_array_type_with_stride (result_type, element_type, | |
a405673c | 1190 | range_type, NULL, 0); |
dc53a7ad JB |
1191 | } |
1192 | ||
e3506a9f UW |
1193 | struct type * |
1194 | lookup_array_range_type (struct type *element_type, | |
63375b74 | 1195 | LONGEST low_bound, LONGEST high_bound) |
e3506a9f | 1196 | { |
50810684 | 1197 | struct gdbarch *gdbarch = get_type_arch (element_type); |
e3506a9f UW |
1198 | struct type *index_type = builtin_type (gdbarch)->builtin_int; |
1199 | struct type *range_type | |
0c9c3474 | 1200 | = create_static_range_type (NULL, index_type, low_bound, high_bound); |
d8734c88 | 1201 | |
e3506a9f UW |
1202 | return create_array_type (NULL, element_type, range_type); |
1203 | } | |
1204 | ||
7ba81444 MS |
1205 | /* Create a string type using either a blank type supplied in |
1206 | RESULT_TYPE, or creating a new type. String types are similar | |
1207 | enough to array of char types that we can use create_array_type to | |
1208 | build the basic type and then bash it into a string type. | |
c906108c SS |
1209 | |
1210 | For fixed length strings, the range type contains 0 as the lower | |
1211 | bound and the length of the string minus one as the upper bound. | |
1212 | ||
7ba81444 MS |
1213 | FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make |
1214 | sure it is TYPE_CODE_UNDEF before we bash it into a string | |
1215 | type? */ | |
c906108c SS |
1216 | |
1217 | struct type * | |
3b7538c0 UW |
1218 | create_string_type (struct type *result_type, |
1219 | struct type *string_char_type, | |
7ba81444 | 1220 | struct type *range_type) |
c906108c SS |
1221 | { |
1222 | result_type = create_array_type (result_type, | |
f290d38e | 1223 | string_char_type, |
c906108c SS |
1224 | range_type); |
1225 | TYPE_CODE (result_type) = TYPE_CODE_STRING; | |
c16abbde | 1226 | return result_type; |
c906108c SS |
1227 | } |
1228 | ||
e3506a9f UW |
1229 | struct type * |
1230 | lookup_string_range_type (struct type *string_char_type, | |
63375b74 | 1231 | LONGEST low_bound, LONGEST high_bound) |
e3506a9f UW |
1232 | { |
1233 | struct type *result_type; | |
d8734c88 | 1234 | |
e3506a9f UW |
1235 | result_type = lookup_array_range_type (string_char_type, |
1236 | low_bound, high_bound); | |
1237 | TYPE_CODE (result_type) = TYPE_CODE_STRING; | |
1238 | return result_type; | |
1239 | } | |
1240 | ||
c906108c | 1241 | struct type * |
fba45db2 | 1242 | create_set_type (struct type *result_type, struct type *domain_type) |
c906108c | 1243 | { |
c906108c | 1244 | if (result_type == NULL) |
e9bb382b UW |
1245 | result_type = alloc_type_copy (domain_type); |
1246 | ||
c906108c SS |
1247 | TYPE_CODE (result_type) = TYPE_CODE_SET; |
1248 | TYPE_NFIELDS (result_type) = 1; | |
224c3ddb SM |
1249 | TYPE_FIELDS (result_type) |
1250 | = (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field)); | |
c906108c | 1251 | |
74a9bb82 | 1252 | if (!TYPE_STUB (domain_type)) |
c906108c | 1253 | { |
f9780d5b | 1254 | LONGEST low_bound, high_bound, bit_length; |
d8734c88 | 1255 | |
c906108c SS |
1256 | if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0) |
1257 | low_bound = high_bound = 0; | |
1258 | bit_length = high_bound - low_bound + 1; | |
1259 | TYPE_LENGTH (result_type) | |
1260 | = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT; | |
f9780d5b | 1261 | if (low_bound >= 0) |
876cecd0 | 1262 | TYPE_UNSIGNED (result_type) = 1; |
c906108c SS |
1263 | } |
1264 | TYPE_FIELD_TYPE (result_type, 0) = domain_type; | |
1265 | ||
c16abbde | 1266 | return result_type; |
c906108c SS |
1267 | } |
1268 | ||
ea37ba09 DJ |
1269 | /* Convert ARRAY_TYPE to a vector type. This may modify ARRAY_TYPE |
1270 | and any array types nested inside it. */ | |
1271 | ||
1272 | void | |
1273 | make_vector_type (struct type *array_type) | |
1274 | { | |
1275 | struct type *inner_array, *elt_type; | |
1276 | int flags; | |
1277 | ||
1278 | /* Find the innermost array type, in case the array is | |
1279 | multi-dimensional. */ | |
1280 | inner_array = array_type; | |
1281 | while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY) | |
1282 | inner_array = TYPE_TARGET_TYPE (inner_array); | |
1283 | ||
1284 | elt_type = TYPE_TARGET_TYPE (inner_array); | |
1285 | if (TYPE_CODE (elt_type) == TYPE_CODE_INT) | |
1286 | { | |
2844d6b5 | 1287 | flags = TYPE_INSTANCE_FLAGS (elt_type) | TYPE_INSTANCE_FLAG_NOTTEXT; |
ea37ba09 DJ |
1288 | elt_type = make_qualified_type (elt_type, flags, NULL); |
1289 | TYPE_TARGET_TYPE (inner_array) = elt_type; | |
1290 | } | |
1291 | ||
876cecd0 | 1292 | TYPE_VECTOR (array_type) = 1; |
ea37ba09 DJ |
1293 | } |
1294 | ||
794ac428 | 1295 | struct type * |
ac3aafc7 EZ |
1296 | init_vector_type (struct type *elt_type, int n) |
1297 | { | |
1298 | struct type *array_type; | |
d8734c88 | 1299 | |
e3506a9f | 1300 | array_type = lookup_array_range_type (elt_type, 0, n - 1); |
ea37ba09 | 1301 | make_vector_type (array_type); |
ac3aafc7 EZ |
1302 | return array_type; |
1303 | } | |
1304 | ||
09e2d7c7 DE |
1305 | /* Internal routine called by TYPE_SELF_TYPE to return the type that TYPE |
1306 | belongs to. In c++ this is the class of "this", but TYPE_THIS_TYPE is too | |
1307 | confusing. "self" is a common enough replacement for "this". | |
1308 | TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or | |
1309 | TYPE_CODE_METHOD. */ | |
1310 | ||
1311 | struct type * | |
1312 | internal_type_self_type (struct type *type) | |
1313 | { | |
1314 | switch (TYPE_CODE (type)) | |
1315 | { | |
1316 | case TYPE_CODE_METHODPTR: | |
1317 | case TYPE_CODE_MEMBERPTR: | |
eaaf76ab DE |
1318 | if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE) |
1319 | return NULL; | |
09e2d7c7 DE |
1320 | gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE); |
1321 | return TYPE_MAIN_TYPE (type)->type_specific.self_type; | |
1322 | case TYPE_CODE_METHOD: | |
eaaf76ab DE |
1323 | if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE) |
1324 | return NULL; | |
09e2d7c7 DE |
1325 | gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC); |
1326 | return TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type; | |
1327 | default: | |
1328 | gdb_assert_not_reached ("bad type"); | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | /* Set the type of the class that TYPE belongs to. | |
1333 | In c++ this is the class of "this". | |
1334 | TYPE must be one of TYPE_CODE_METHODPTR, TYPE_CODE_MEMBERPTR, or | |
1335 | TYPE_CODE_METHOD. */ | |
1336 | ||
1337 | void | |
1338 | set_type_self_type (struct type *type, struct type *self_type) | |
1339 | { | |
1340 | switch (TYPE_CODE (type)) | |
1341 | { | |
1342 | case TYPE_CODE_METHODPTR: | |
1343 | case TYPE_CODE_MEMBERPTR: | |
1344 | if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE) | |
1345 | TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_SELF_TYPE; | |
1346 | gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_SELF_TYPE); | |
1347 | TYPE_MAIN_TYPE (type)->type_specific.self_type = self_type; | |
1348 | break; | |
1349 | case TYPE_CODE_METHOD: | |
1350 | if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_NONE) | |
1351 | INIT_FUNC_SPECIFIC (type); | |
1352 | gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FUNC); | |
1353 | TYPE_MAIN_TYPE (type)->type_specific.func_stuff->self_type = self_type; | |
1354 | break; | |
1355 | default: | |
1356 | gdb_assert_not_reached ("bad type"); | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | /* Smash TYPE to be a type of pointers to members of SELF_TYPE with type | |
0d5de010 DJ |
1361 | TO_TYPE. A member pointer is a wierd thing -- it amounts to a |
1362 | typed offset into a struct, e.g. "an int at offset 8". A MEMBER | |
1363 | TYPE doesn't include the offset (that's the value of the MEMBER | |
1364 | itself), but does include the structure type into which it points | |
1365 | (for some reason). | |
c906108c | 1366 | |
7ba81444 MS |
1367 | When "smashing" the type, we preserve the objfile that the old type |
1368 | pointed to, since we aren't changing where the type is actually | |
c906108c SS |
1369 | allocated. */ |
1370 | ||
1371 | void | |
09e2d7c7 | 1372 | smash_to_memberptr_type (struct type *type, struct type *self_type, |
0d5de010 | 1373 | struct type *to_type) |
c906108c | 1374 | { |
2fdde8f8 | 1375 | smash_type (type); |
09e2d7c7 | 1376 | TYPE_CODE (type) = TYPE_CODE_MEMBERPTR; |
c906108c | 1377 | TYPE_TARGET_TYPE (type) = to_type; |
09e2d7c7 | 1378 | set_type_self_type (type, self_type); |
0d5de010 DJ |
1379 | /* Assume that a data member pointer is the same size as a normal |
1380 | pointer. */ | |
50810684 UW |
1381 | TYPE_LENGTH (type) |
1382 | = gdbarch_ptr_bit (get_type_arch (to_type)) / TARGET_CHAR_BIT; | |
c906108c SS |
1383 | } |
1384 | ||
0b92b5bb TT |
1385 | /* Smash TYPE to be a type of pointer to methods type TO_TYPE. |
1386 | ||
1387 | When "smashing" the type, we preserve the objfile that the old type | |
1388 | pointed to, since we aren't changing where the type is actually | |
1389 | allocated. */ | |
1390 | ||
1391 | void | |
1392 | smash_to_methodptr_type (struct type *type, struct type *to_type) | |
1393 | { | |
1394 | smash_type (type); | |
09e2d7c7 | 1395 | TYPE_CODE (type) = TYPE_CODE_METHODPTR; |
0b92b5bb | 1396 | TYPE_TARGET_TYPE (type) = to_type; |
09e2d7c7 | 1397 | set_type_self_type (type, TYPE_SELF_TYPE (to_type)); |
0b92b5bb | 1398 | TYPE_LENGTH (type) = cplus_method_ptr_size (to_type); |
0b92b5bb TT |
1399 | } |
1400 | ||
09e2d7c7 | 1401 | /* Smash TYPE to be a type of method of SELF_TYPE with type TO_TYPE. |
c906108c SS |
1402 | METHOD just means `function that gets an extra "this" argument'. |
1403 | ||
7ba81444 MS |
1404 | When "smashing" the type, we preserve the objfile that the old type |
1405 | pointed to, since we aren't changing where the type is actually | |
c906108c SS |
1406 | allocated. */ |
1407 | ||
1408 | void | |
09e2d7c7 | 1409 | smash_to_method_type (struct type *type, struct type *self_type, |
ad2f7632 DJ |
1410 | struct type *to_type, struct field *args, |
1411 | int nargs, int varargs) | |
c906108c | 1412 | { |
2fdde8f8 | 1413 | smash_type (type); |
09e2d7c7 | 1414 | TYPE_CODE (type) = TYPE_CODE_METHOD; |
c906108c | 1415 | TYPE_TARGET_TYPE (type) = to_type; |
09e2d7c7 | 1416 | set_type_self_type (type, self_type); |
ad2f7632 DJ |
1417 | TYPE_FIELDS (type) = args; |
1418 | TYPE_NFIELDS (type) = nargs; | |
1419 | if (varargs) | |
876cecd0 | 1420 | TYPE_VARARGS (type) = 1; |
c906108c | 1421 | TYPE_LENGTH (type) = 1; /* In practice, this is never needed. */ |
c906108c SS |
1422 | } |
1423 | ||
1424 | /* Return a typename for a struct/union/enum type without "struct ", | |
1425 | "union ", or "enum ". If the type has a NULL name, return NULL. */ | |
1426 | ||
0d5cff50 | 1427 | const char * |
aa1ee363 | 1428 | type_name_no_tag (const struct type *type) |
c906108c SS |
1429 | { |
1430 | if (TYPE_TAG_NAME (type) != NULL) | |
1431 | return TYPE_TAG_NAME (type); | |
1432 | ||
7ba81444 MS |
1433 | /* Is there code which expects this to return the name if there is |
1434 | no tag name? My guess is that this is mainly used for C++ in | |
1435 | cases where the two will always be the same. */ | |
c906108c SS |
1436 | return TYPE_NAME (type); |
1437 | } | |
1438 | ||
d8228535 JK |
1439 | /* A wrapper of type_name_no_tag which calls error if the type is anonymous. |
1440 | Since GCC PR debug/47510 DWARF provides associated information to detect the | |
1441 | anonymous class linkage name from its typedef. | |
1442 | ||
1443 | Parameter TYPE should not yet have CHECK_TYPEDEF applied, this function will | |
1444 | apply it itself. */ | |
1445 | ||
1446 | const char * | |
1447 | type_name_no_tag_or_error (struct type *type) | |
1448 | { | |
1449 | struct type *saved_type = type; | |
1450 | const char *name; | |
1451 | struct objfile *objfile; | |
1452 | ||
f168693b | 1453 | type = check_typedef (type); |
d8228535 JK |
1454 | |
1455 | name = type_name_no_tag (type); | |
1456 | if (name != NULL) | |
1457 | return name; | |
1458 | ||
1459 | name = type_name_no_tag (saved_type); | |
1460 | objfile = TYPE_OBJFILE (saved_type); | |
1461 | error (_("Invalid anonymous type %s [in module %s], GCC PR debug/47510 bug?"), | |
4262abfb JK |
1462 | name ? name : "<anonymous>", |
1463 | objfile ? objfile_name (objfile) : "<arch>"); | |
d8228535 JK |
1464 | } |
1465 | ||
7ba81444 MS |
1466 | /* Lookup a typedef or primitive type named NAME, visible in lexical |
1467 | block BLOCK. If NOERR is nonzero, return zero if NAME is not | |
1468 | suitably defined. */ | |
c906108c SS |
1469 | |
1470 | struct type * | |
e6c014f2 | 1471 | lookup_typename (const struct language_defn *language, |
ddd49eee | 1472 | struct gdbarch *gdbarch, const char *name, |
34eaf542 | 1473 | const struct block *block, int noerr) |
c906108c | 1474 | { |
52f0bd74 | 1475 | struct symbol *sym; |
c906108c | 1476 | |
1994afbf | 1477 | sym = lookup_symbol_in_language (name, block, VAR_DOMAIN, |
d12307c1 | 1478 | language->la_language, NULL).symbol; |
c51fe631 DE |
1479 | if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF) |
1480 | return SYMBOL_TYPE (sym); | |
1481 | ||
c51fe631 DE |
1482 | if (noerr) |
1483 | return NULL; | |
1484 | error (_("No type named %s."), name); | |
c906108c SS |
1485 | } |
1486 | ||
1487 | struct type * | |
e6c014f2 | 1488 | lookup_unsigned_typename (const struct language_defn *language, |
0d5cff50 | 1489 | struct gdbarch *gdbarch, const char *name) |
c906108c | 1490 | { |
224c3ddb | 1491 | char *uns = (char *) alloca (strlen (name) + 10); |
c906108c SS |
1492 | |
1493 | strcpy (uns, "unsigned "); | |
1494 | strcpy (uns + 9, name); | |
e6c014f2 | 1495 | return lookup_typename (language, gdbarch, uns, (struct block *) NULL, 0); |
c906108c SS |
1496 | } |
1497 | ||
1498 | struct type * | |
e6c014f2 | 1499 | lookup_signed_typename (const struct language_defn *language, |
0d5cff50 | 1500 | struct gdbarch *gdbarch, const char *name) |
c906108c SS |
1501 | { |
1502 | struct type *t; | |
224c3ddb | 1503 | char *uns = (char *) alloca (strlen (name) + 8); |
c906108c SS |
1504 | |
1505 | strcpy (uns, "signed "); | |
1506 | strcpy (uns + 7, name); | |
e6c014f2 | 1507 | t = lookup_typename (language, gdbarch, uns, (struct block *) NULL, 1); |
7ba81444 | 1508 | /* If we don't find "signed FOO" just try again with plain "FOO". */ |
c906108c SS |
1509 | if (t != NULL) |
1510 | return t; | |
e6c014f2 | 1511 | return lookup_typename (language, gdbarch, name, (struct block *) NULL, 0); |
c906108c SS |
1512 | } |
1513 | ||
1514 | /* Lookup a structure type named "struct NAME", | |
1515 | visible in lexical block BLOCK. */ | |
1516 | ||
1517 | struct type * | |
270140bd | 1518 | lookup_struct (const char *name, const struct block *block) |
c906108c | 1519 | { |
52f0bd74 | 1520 | struct symbol *sym; |
c906108c | 1521 | |
d12307c1 | 1522 | sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol; |
c906108c SS |
1523 | |
1524 | if (sym == NULL) | |
1525 | { | |
8a3fe4f8 | 1526 | error (_("No struct type named %s."), name); |
c906108c SS |
1527 | } |
1528 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT) | |
1529 | { | |
7ba81444 MS |
1530 | error (_("This context has class, union or enum %s, not a struct."), |
1531 | name); | |
c906108c SS |
1532 | } |
1533 | return (SYMBOL_TYPE (sym)); | |
1534 | } | |
1535 | ||
1536 | /* Lookup a union type named "union NAME", | |
1537 | visible in lexical block BLOCK. */ | |
1538 | ||
1539 | struct type * | |
270140bd | 1540 | lookup_union (const char *name, const struct block *block) |
c906108c | 1541 | { |
52f0bd74 | 1542 | struct symbol *sym; |
c5aa993b | 1543 | struct type *t; |
c906108c | 1544 | |
d12307c1 | 1545 | sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol; |
c906108c SS |
1546 | |
1547 | if (sym == NULL) | |
8a3fe4f8 | 1548 | error (_("No union type named %s."), name); |
c906108c | 1549 | |
c5aa993b | 1550 | t = SYMBOL_TYPE (sym); |
c906108c SS |
1551 | |
1552 | if (TYPE_CODE (t) == TYPE_CODE_UNION) | |
c16abbde | 1553 | return t; |
c906108c | 1554 | |
7ba81444 MS |
1555 | /* If we get here, it's not a union. */ |
1556 | error (_("This context has class, struct or enum %s, not a union."), | |
1557 | name); | |
c906108c SS |
1558 | } |
1559 | ||
c906108c SS |
1560 | /* Lookup an enum type named "enum NAME", |
1561 | visible in lexical block BLOCK. */ | |
1562 | ||
1563 | struct type * | |
270140bd | 1564 | lookup_enum (const char *name, const struct block *block) |
c906108c | 1565 | { |
52f0bd74 | 1566 | struct symbol *sym; |
c906108c | 1567 | |
d12307c1 | 1568 | sym = lookup_symbol (name, block, STRUCT_DOMAIN, 0).symbol; |
c906108c SS |
1569 | if (sym == NULL) |
1570 | { | |
8a3fe4f8 | 1571 | error (_("No enum type named %s."), name); |
c906108c SS |
1572 | } |
1573 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM) | |
1574 | { | |
7ba81444 MS |
1575 | error (_("This context has class, struct or union %s, not an enum."), |
1576 | name); | |
c906108c SS |
1577 | } |
1578 | return (SYMBOL_TYPE (sym)); | |
1579 | } | |
1580 | ||
1581 | /* Lookup a template type named "template NAME<TYPE>", | |
1582 | visible in lexical block BLOCK. */ | |
1583 | ||
1584 | struct type * | |
7ba81444 | 1585 | lookup_template_type (char *name, struct type *type, |
270140bd | 1586 | const struct block *block) |
c906108c SS |
1587 | { |
1588 | struct symbol *sym; | |
7ba81444 MS |
1589 | char *nam = (char *) |
1590 | alloca (strlen (name) + strlen (TYPE_NAME (type)) + 4); | |
d8734c88 | 1591 | |
c906108c SS |
1592 | strcpy (nam, name); |
1593 | strcat (nam, "<"); | |
0004e5a2 | 1594 | strcat (nam, TYPE_NAME (type)); |
0963b4bd | 1595 | strcat (nam, " >"); /* FIXME, extra space still introduced in gcc? */ |
c906108c | 1596 | |
d12307c1 | 1597 | sym = lookup_symbol (nam, block, VAR_DOMAIN, 0).symbol; |
c906108c SS |
1598 | |
1599 | if (sym == NULL) | |
1600 | { | |
8a3fe4f8 | 1601 | error (_("No template type named %s."), name); |
c906108c SS |
1602 | } |
1603 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT) | |
1604 | { | |
7ba81444 MS |
1605 | error (_("This context has class, union or enum %s, not a struct."), |
1606 | name); | |
c906108c SS |
1607 | } |
1608 | return (SYMBOL_TYPE (sym)); | |
1609 | } | |
1610 | ||
7ba81444 MS |
1611 | /* Given a type TYPE, lookup the type of the component of type named |
1612 | NAME. | |
c906108c | 1613 | |
7ba81444 MS |
1614 | TYPE can be either a struct or union, or a pointer or reference to |
1615 | a struct or union. If it is a pointer or reference, its target | |
1616 | type is automatically used. Thus '.' and '->' are interchangable, | |
1617 | as specified for the definitions of the expression element types | |
1618 | STRUCTOP_STRUCT and STRUCTOP_PTR. | |
c906108c SS |
1619 | |
1620 | If NOERR is nonzero, return zero if NAME is not suitably defined. | |
1621 | If NAME is the name of a baseclass type, return that type. */ | |
1622 | ||
1623 | struct type * | |
d7561cbb | 1624 | lookup_struct_elt_type (struct type *type, const char *name, int noerr) |
c906108c SS |
1625 | { |
1626 | int i; | |
1627 | ||
1628 | for (;;) | |
1629 | { | |
f168693b | 1630 | type = check_typedef (type); |
c906108c SS |
1631 | if (TYPE_CODE (type) != TYPE_CODE_PTR |
1632 | && TYPE_CODE (type) != TYPE_CODE_REF) | |
1633 | break; | |
1634 | type = TYPE_TARGET_TYPE (type); | |
1635 | } | |
1636 | ||
687d6395 MS |
1637 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT |
1638 | && TYPE_CODE (type) != TYPE_CODE_UNION) | |
c906108c | 1639 | { |
2f408ecb PA |
1640 | std::string type_name = type_to_string (type); |
1641 | error (_("Type %s is not a structure or union type."), | |
1642 | type_name.c_str ()); | |
c906108c SS |
1643 | } |
1644 | ||
1645 | #if 0 | |
7ba81444 MS |
1646 | /* FIXME: This change put in by Michael seems incorrect for the case |
1647 | where the structure tag name is the same as the member name. | |
0963b4bd | 1648 | I.e. when doing "ptype bell->bar" for "struct foo { int bar; int |
7ba81444 | 1649 | foo; } bell;" Disabled by fnf. */ |
c906108c | 1650 | { |
fe978cb0 | 1651 | char *type_name; |
c906108c | 1652 | |
fe978cb0 PA |
1653 | type_name = type_name_no_tag (type); |
1654 | if (type_name != NULL && strcmp (type_name, name) == 0) | |
c906108c SS |
1655 | return type; |
1656 | } | |
1657 | #endif | |
1658 | ||
1659 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
1660 | { | |
0d5cff50 | 1661 | const char *t_field_name = TYPE_FIELD_NAME (type, i); |
c906108c | 1662 | |
db577aea | 1663 | if (t_field_name && (strcmp_iw (t_field_name, name) == 0)) |
c906108c SS |
1664 | { |
1665 | return TYPE_FIELD_TYPE (type, i); | |
1666 | } | |
f5a010c0 PM |
1667 | else if (!t_field_name || *t_field_name == '\0') |
1668 | { | |
d8734c88 MS |
1669 | struct type *subtype |
1670 | = lookup_struct_elt_type (TYPE_FIELD_TYPE (type, i), name, 1); | |
1671 | ||
f5a010c0 PM |
1672 | if (subtype != NULL) |
1673 | return subtype; | |
1674 | } | |
c906108c SS |
1675 | } |
1676 | ||
1677 | /* OK, it's not in this class. Recursively check the baseclasses. */ | |
1678 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1679 | { | |
1680 | struct type *t; | |
1681 | ||
9733fc94 | 1682 | t = lookup_struct_elt_type (TYPE_BASECLASS (type, i), name, 1); |
c906108c SS |
1683 | if (t != NULL) |
1684 | { | |
1685 | return t; | |
1686 | } | |
1687 | } | |
1688 | ||
1689 | if (noerr) | |
1690 | { | |
1691 | return NULL; | |
1692 | } | |
c5aa993b | 1693 | |
2f408ecb PA |
1694 | std::string type_name = type_to_string (type); |
1695 | error (_("Type %s has no component named %s."), type_name.c_str (), name); | |
c906108c SS |
1696 | } |
1697 | ||
ed3ef339 DE |
1698 | /* Store in *MAX the largest number representable by unsigned integer type |
1699 | TYPE. */ | |
1700 | ||
1701 | void | |
1702 | get_unsigned_type_max (struct type *type, ULONGEST *max) | |
1703 | { | |
1704 | unsigned int n; | |
1705 | ||
f168693b | 1706 | type = check_typedef (type); |
ed3ef339 DE |
1707 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && TYPE_UNSIGNED (type)); |
1708 | gdb_assert (TYPE_LENGTH (type) <= sizeof (ULONGEST)); | |
1709 | ||
1710 | /* Written this way to avoid overflow. */ | |
1711 | n = TYPE_LENGTH (type) * TARGET_CHAR_BIT; | |
1712 | *max = ((((ULONGEST) 1 << (n - 1)) - 1) << 1) | 1; | |
1713 | } | |
1714 | ||
1715 | /* Store in *MIN, *MAX the smallest and largest numbers representable by | |
1716 | signed integer type TYPE. */ | |
1717 | ||
1718 | void | |
1719 | get_signed_type_minmax (struct type *type, LONGEST *min, LONGEST *max) | |
1720 | { | |
1721 | unsigned int n; | |
1722 | ||
f168693b | 1723 | type = check_typedef (type); |
ed3ef339 DE |
1724 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_INT && !TYPE_UNSIGNED (type)); |
1725 | gdb_assert (TYPE_LENGTH (type) <= sizeof (LONGEST)); | |
1726 | ||
1727 | n = TYPE_LENGTH (type) * TARGET_CHAR_BIT; | |
1728 | *min = -((ULONGEST) 1 << (n - 1)); | |
1729 | *max = ((ULONGEST) 1 << (n - 1)) - 1; | |
1730 | } | |
1731 | ||
ae6ae975 DE |
1732 | /* Internal routine called by TYPE_VPTR_FIELDNO to return the value of |
1733 | cplus_stuff.vptr_fieldno. | |
1734 | ||
1735 | cplus_stuff is initialized to cplus_struct_default which does not | |
1736 | set vptr_fieldno to -1 for portability reasons (IWBN to use C99 | |
1737 | designated initializers). We cope with that here. */ | |
1738 | ||
1739 | int | |
1740 | internal_type_vptr_fieldno (struct type *type) | |
1741 | { | |
f168693b | 1742 | type = check_typedef (type); |
ae6ae975 DE |
1743 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT |
1744 | || TYPE_CODE (type) == TYPE_CODE_UNION); | |
1745 | if (!HAVE_CPLUS_STRUCT (type)) | |
1746 | return -1; | |
1747 | return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno; | |
1748 | } | |
1749 | ||
1750 | /* Set the value of cplus_stuff.vptr_fieldno. */ | |
1751 | ||
1752 | void | |
1753 | set_type_vptr_fieldno (struct type *type, int fieldno) | |
1754 | { | |
f168693b | 1755 | type = check_typedef (type); |
ae6ae975 DE |
1756 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT |
1757 | || TYPE_CODE (type) == TYPE_CODE_UNION); | |
1758 | if (!HAVE_CPLUS_STRUCT (type)) | |
1759 | ALLOCATE_CPLUS_STRUCT_TYPE (type); | |
1760 | TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno; | |
1761 | } | |
1762 | ||
1763 | /* Internal routine called by TYPE_VPTR_BASETYPE to return the value of | |
1764 | cplus_stuff.vptr_basetype. */ | |
1765 | ||
1766 | struct type * | |
1767 | internal_type_vptr_basetype (struct type *type) | |
1768 | { | |
f168693b | 1769 | type = check_typedef (type); |
ae6ae975 DE |
1770 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT |
1771 | || TYPE_CODE (type) == TYPE_CODE_UNION); | |
1772 | gdb_assert (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_CPLUS_STUFF); | |
1773 | return TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype; | |
1774 | } | |
1775 | ||
1776 | /* Set the value of cplus_stuff.vptr_basetype. */ | |
1777 | ||
1778 | void | |
1779 | set_type_vptr_basetype (struct type *type, struct type *basetype) | |
1780 | { | |
f168693b | 1781 | type = check_typedef (type); |
ae6ae975 DE |
1782 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT |
1783 | || TYPE_CODE (type) == TYPE_CODE_UNION); | |
1784 | if (!HAVE_CPLUS_STRUCT (type)) | |
1785 | ALLOCATE_CPLUS_STRUCT_TYPE (type); | |
1786 | TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype; | |
1787 | } | |
1788 | ||
81fe8080 DE |
1789 | /* Lookup the vptr basetype/fieldno values for TYPE. |
1790 | If found store vptr_basetype in *BASETYPEP if non-NULL, and return | |
1791 | vptr_fieldno. Also, if found and basetype is from the same objfile, | |
1792 | cache the results. | |
1793 | If not found, return -1 and ignore BASETYPEP. | |
1794 | Callers should be aware that in some cases (for example, | |
c906108c | 1795 | the type or one of its baseclasses is a stub type and we are |
d48cc9dd DJ |
1796 | debugging a .o file, or the compiler uses DWARF-2 and is not GCC), |
1797 | this function will not be able to find the | |
7ba81444 | 1798 | virtual function table pointer, and vptr_fieldno will remain -1 and |
81fe8080 | 1799 | vptr_basetype will remain NULL or incomplete. */ |
c906108c | 1800 | |
81fe8080 DE |
1801 | int |
1802 | get_vptr_fieldno (struct type *type, struct type **basetypep) | |
c906108c | 1803 | { |
f168693b | 1804 | type = check_typedef (type); |
c906108c SS |
1805 | |
1806 | if (TYPE_VPTR_FIELDNO (type) < 0) | |
1807 | { | |
1808 | int i; | |
1809 | ||
7ba81444 MS |
1810 | /* We must start at zero in case the first (and only) baseclass |
1811 | is virtual (and hence we cannot share the table pointer). */ | |
c906108c SS |
1812 | for (i = 0; i < TYPE_N_BASECLASSES (type); i++) |
1813 | { | |
81fe8080 DE |
1814 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); |
1815 | int fieldno; | |
1816 | struct type *basetype; | |
1817 | ||
1818 | fieldno = get_vptr_fieldno (baseclass, &basetype); | |
1819 | if (fieldno >= 0) | |
c906108c | 1820 | { |
81fe8080 | 1821 | /* If the type comes from a different objfile we can't cache |
0963b4bd | 1822 | it, it may have a different lifetime. PR 2384 */ |
5ef73790 | 1823 | if (TYPE_OBJFILE (type) == TYPE_OBJFILE (basetype)) |
81fe8080 | 1824 | { |
ae6ae975 DE |
1825 | set_type_vptr_fieldno (type, fieldno); |
1826 | set_type_vptr_basetype (type, basetype); | |
81fe8080 DE |
1827 | } |
1828 | if (basetypep) | |
1829 | *basetypep = basetype; | |
1830 | return fieldno; | |
c906108c SS |
1831 | } |
1832 | } | |
81fe8080 DE |
1833 | |
1834 | /* Not found. */ | |
1835 | return -1; | |
1836 | } | |
1837 | else | |
1838 | { | |
1839 | if (basetypep) | |
1840 | *basetypep = TYPE_VPTR_BASETYPE (type); | |
1841 | return TYPE_VPTR_FIELDNO (type); | |
c906108c SS |
1842 | } |
1843 | } | |
1844 | ||
44e1a9eb DJ |
1845 | static void |
1846 | stub_noname_complaint (void) | |
1847 | { | |
e2e0b3e5 | 1848 | complaint (&symfile_complaints, _("stub type has NULL name")); |
44e1a9eb DJ |
1849 | } |
1850 | ||
a405673c JB |
1851 | /* Return nonzero if TYPE has a DYN_PROP_BYTE_STRIDE dynamic property |
1852 | attached to it, and that property has a non-constant value. */ | |
1853 | ||
1854 | static int | |
1855 | array_type_has_dynamic_stride (struct type *type) | |
1856 | { | |
1857 | struct dynamic_prop *prop = get_dyn_prop (DYN_PROP_BYTE_STRIDE, type); | |
1858 | ||
1859 | return (prop != NULL && prop->kind != PROP_CONST); | |
1860 | } | |
1861 | ||
d98b7a16 | 1862 | /* Worker for is_dynamic_type. */ |
80180f79 | 1863 | |
d98b7a16 | 1864 | static int |
ee715b5a | 1865 | is_dynamic_type_internal (struct type *type, int top_level) |
80180f79 SA |
1866 | { |
1867 | type = check_typedef (type); | |
1868 | ||
e771e4be PMR |
1869 | /* We only want to recognize references at the outermost level. */ |
1870 | if (top_level && TYPE_CODE (type) == TYPE_CODE_REF) | |
1871 | type = check_typedef (TYPE_TARGET_TYPE (type)); | |
1872 | ||
3cdcd0ce JB |
1873 | /* Types that have a dynamic TYPE_DATA_LOCATION are considered |
1874 | dynamic, even if the type itself is statically defined. | |
1875 | From a user's point of view, this may appear counter-intuitive; | |
1876 | but it makes sense in this context, because the point is to determine | |
1877 | whether any part of the type needs to be resolved before it can | |
1878 | be exploited. */ | |
1879 | if (TYPE_DATA_LOCATION (type) != NULL | |
1880 | && (TYPE_DATA_LOCATION_KIND (type) == PROP_LOCEXPR | |
1881 | || TYPE_DATA_LOCATION_KIND (type) == PROP_LOCLIST)) | |
1882 | return 1; | |
1883 | ||
3f2f83dd KB |
1884 | if (TYPE_ASSOCIATED_PROP (type)) |
1885 | return 1; | |
1886 | ||
1887 | if (TYPE_ALLOCATED_PROP (type)) | |
1888 | return 1; | |
1889 | ||
80180f79 SA |
1890 | switch (TYPE_CODE (type)) |
1891 | { | |
6f8a3220 | 1892 | case TYPE_CODE_RANGE: |
ddb87a81 JB |
1893 | { |
1894 | /* A range type is obviously dynamic if it has at least one | |
1895 | dynamic bound. But also consider the range type to be | |
1896 | dynamic when its subtype is dynamic, even if the bounds | |
1897 | of the range type are static. It allows us to assume that | |
1898 | the subtype of a static range type is also static. */ | |
1899 | return (!has_static_range (TYPE_RANGE_DATA (type)) | |
ee715b5a | 1900 | || is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0)); |
ddb87a81 | 1901 | } |
6f8a3220 | 1902 | |
80180f79 SA |
1903 | case TYPE_CODE_ARRAY: |
1904 | { | |
80180f79 | 1905 | gdb_assert (TYPE_NFIELDS (type) == 1); |
6f8a3220 | 1906 | |
a405673c | 1907 | /* The array is dynamic if either the bounds are dynamic... */ |
ee715b5a | 1908 | if (is_dynamic_type_internal (TYPE_INDEX_TYPE (type), 0)) |
80180f79 | 1909 | return 1; |
a405673c JB |
1910 | /* ... or the elements it contains have a dynamic contents... */ |
1911 | if (is_dynamic_type_internal (TYPE_TARGET_TYPE (type), 0)) | |
1912 | return 1; | |
1913 | /* ... or if it has a dynamic stride... */ | |
1914 | if (array_type_has_dynamic_stride (type)) | |
1915 | return 1; | |
1916 | return 0; | |
80180f79 | 1917 | } |
012370f6 TT |
1918 | |
1919 | case TYPE_CODE_STRUCT: | |
1920 | case TYPE_CODE_UNION: | |
1921 | { | |
1922 | int i; | |
1923 | ||
1924 | for (i = 0; i < TYPE_NFIELDS (type); ++i) | |
1925 | if (!field_is_static (&TYPE_FIELD (type, i)) | |
ee715b5a | 1926 | && is_dynamic_type_internal (TYPE_FIELD_TYPE (type, i), 0)) |
012370f6 TT |
1927 | return 1; |
1928 | } | |
1929 | break; | |
80180f79 | 1930 | } |
92e2a17f TT |
1931 | |
1932 | return 0; | |
80180f79 SA |
1933 | } |
1934 | ||
d98b7a16 TT |
1935 | /* See gdbtypes.h. */ |
1936 | ||
1937 | int | |
1938 | is_dynamic_type (struct type *type) | |
1939 | { | |
ee715b5a | 1940 | return is_dynamic_type_internal (type, 1); |
d98b7a16 TT |
1941 | } |
1942 | ||
df25ebbd | 1943 | static struct type *resolve_dynamic_type_internal |
ee715b5a | 1944 | (struct type *type, struct property_addr_info *addr_stack, int top_level); |
d98b7a16 | 1945 | |
df25ebbd JB |
1946 | /* Given a dynamic range type (dyn_range_type) and a stack of |
1947 | struct property_addr_info elements, return a static version | |
1948 | of that type. */ | |
d190df30 | 1949 | |
80180f79 | 1950 | static struct type * |
df25ebbd JB |
1951 | resolve_dynamic_range (struct type *dyn_range_type, |
1952 | struct property_addr_info *addr_stack) | |
80180f79 SA |
1953 | { |
1954 | CORE_ADDR value; | |
ddb87a81 | 1955 | struct type *static_range_type, *static_target_type; |
80180f79 | 1956 | const struct dynamic_prop *prop; |
80180f79 SA |
1957 | struct dynamic_prop low_bound, high_bound; |
1958 | ||
6f8a3220 | 1959 | gdb_assert (TYPE_CODE (dyn_range_type) == TYPE_CODE_RANGE); |
80180f79 | 1960 | |
6f8a3220 | 1961 | prop = &TYPE_RANGE_DATA (dyn_range_type)->low; |
63e43d3a | 1962 | if (dwarf2_evaluate_property (prop, NULL, addr_stack, &value)) |
80180f79 SA |
1963 | { |
1964 | low_bound.kind = PROP_CONST; | |
1965 | low_bound.data.const_val = value; | |
1966 | } | |
1967 | else | |
1968 | { | |
1969 | low_bound.kind = PROP_UNDEFINED; | |
1970 | low_bound.data.const_val = 0; | |
1971 | } | |
1972 | ||
6f8a3220 | 1973 | prop = &TYPE_RANGE_DATA (dyn_range_type)->high; |
63e43d3a | 1974 | if (dwarf2_evaluate_property (prop, NULL, addr_stack, &value)) |
80180f79 SA |
1975 | { |
1976 | high_bound.kind = PROP_CONST; | |
1977 | high_bound.data.const_val = value; | |
c451ebe5 | 1978 | |
6f8a3220 | 1979 | if (TYPE_RANGE_DATA (dyn_range_type)->flag_upper_bound_is_count) |
c451ebe5 SA |
1980 | high_bound.data.const_val |
1981 | = low_bound.data.const_val + high_bound.data.const_val - 1; | |
80180f79 SA |
1982 | } |
1983 | else | |
1984 | { | |
1985 | high_bound.kind = PROP_UNDEFINED; | |
1986 | high_bound.data.const_val = 0; | |
1987 | } | |
1988 | ||
ddb87a81 JB |
1989 | static_target_type |
1990 | = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (dyn_range_type), | |
ee715b5a | 1991 | addr_stack, 0); |
6f8a3220 | 1992 | static_range_type = create_range_type (copy_type (dyn_range_type), |
ddb87a81 | 1993 | static_target_type, |
6f8a3220 JB |
1994 | &low_bound, &high_bound); |
1995 | TYPE_RANGE_DATA (static_range_type)->flag_bound_evaluated = 1; | |
1996 | return static_range_type; | |
1997 | } | |
1998 | ||
1999 | /* Resolves dynamic bound values of an array type TYPE to static ones. | |
df25ebbd JB |
2000 | ADDR_STACK is a stack of struct property_addr_info to be used |
2001 | if needed during the dynamic resolution. */ | |
6f8a3220 JB |
2002 | |
2003 | static struct type * | |
df25ebbd JB |
2004 | resolve_dynamic_array (struct type *type, |
2005 | struct property_addr_info *addr_stack) | |
6f8a3220 JB |
2006 | { |
2007 | CORE_ADDR value; | |
2008 | struct type *elt_type; | |
2009 | struct type *range_type; | |
2010 | struct type *ary_dim; | |
3f2f83dd | 2011 | struct dynamic_prop *prop; |
a405673c | 2012 | unsigned int bit_stride = 0; |
6f8a3220 JB |
2013 | |
2014 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_ARRAY); | |
2015 | ||
3f2f83dd KB |
2016 | type = copy_type (type); |
2017 | ||
6f8a3220 JB |
2018 | elt_type = type; |
2019 | range_type = check_typedef (TYPE_INDEX_TYPE (elt_type)); | |
df25ebbd | 2020 | range_type = resolve_dynamic_range (range_type, addr_stack); |
6f8a3220 | 2021 | |
3f2f83dd KB |
2022 | /* Resolve allocated/associated here before creating a new array type, which |
2023 | will update the length of the array accordingly. */ | |
2024 | prop = TYPE_ALLOCATED_PROP (type); | |
2025 | if (prop != NULL && dwarf2_evaluate_property (prop, NULL, addr_stack, &value)) | |
2026 | { | |
2027 | TYPE_DYN_PROP_ADDR (prop) = value; | |
2028 | TYPE_DYN_PROP_KIND (prop) = PROP_CONST; | |
2029 | } | |
2030 | prop = TYPE_ASSOCIATED_PROP (type); | |
2031 | if (prop != NULL && dwarf2_evaluate_property (prop, NULL, addr_stack, &value)) | |
2032 | { | |
2033 | TYPE_DYN_PROP_ADDR (prop) = value; | |
2034 | TYPE_DYN_PROP_KIND (prop) = PROP_CONST; | |
2035 | } | |
2036 | ||
80180f79 SA |
2037 | ary_dim = check_typedef (TYPE_TARGET_TYPE (elt_type)); |
2038 | ||
2039 | if (ary_dim != NULL && TYPE_CODE (ary_dim) == TYPE_CODE_ARRAY) | |
d0d84780 | 2040 | elt_type = resolve_dynamic_array (ary_dim, addr_stack); |
80180f79 SA |
2041 | else |
2042 | elt_type = TYPE_TARGET_TYPE (type); | |
2043 | ||
a405673c JB |
2044 | prop = get_dyn_prop (DYN_PROP_BYTE_STRIDE, type); |
2045 | if (prop != NULL) | |
2046 | { | |
2047 | int prop_eval_ok | |
2048 | = dwarf2_evaluate_property (prop, NULL, addr_stack, &value); | |
2049 | ||
2050 | if (prop_eval_ok) | |
2051 | { | |
2052 | remove_dyn_prop (DYN_PROP_BYTE_STRIDE, type); | |
2053 | bit_stride = (unsigned int) (value * 8); | |
2054 | } | |
2055 | else | |
2056 | { | |
2057 | /* Could be a bug in our code, but it could also happen | |
2058 | if the DWARF info is not correct. Issue a warning, | |
2059 | and assume no byte/bit stride (leave bit_stride = 0). */ | |
2060 | warning (_("cannot determine array stride for type %s"), | |
2061 | TYPE_NAME (type) ? TYPE_NAME (type) : "<no name>"); | |
2062 | } | |
2063 | } | |
2064 | else | |
2065 | bit_stride = TYPE_FIELD_BITSIZE (type, 0); | |
2066 | ||
2067 | return create_array_type_with_stride (type, elt_type, range_type, NULL, | |
2068 | bit_stride); | |
80180f79 SA |
2069 | } |
2070 | ||
012370f6 | 2071 | /* Resolve dynamic bounds of members of the union TYPE to static |
df25ebbd JB |
2072 | bounds. ADDR_STACK is a stack of struct property_addr_info |
2073 | to be used if needed during the dynamic resolution. */ | |
012370f6 TT |
2074 | |
2075 | static struct type * | |
df25ebbd JB |
2076 | resolve_dynamic_union (struct type *type, |
2077 | struct property_addr_info *addr_stack) | |
012370f6 TT |
2078 | { |
2079 | struct type *resolved_type; | |
2080 | int i; | |
2081 | unsigned int max_len = 0; | |
2082 | ||
2083 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION); | |
2084 | ||
2085 | resolved_type = copy_type (type); | |
2086 | TYPE_FIELDS (resolved_type) | |
224c3ddb SM |
2087 | = (struct field *) TYPE_ALLOC (resolved_type, |
2088 | TYPE_NFIELDS (resolved_type) | |
2089 | * sizeof (struct field)); | |
012370f6 TT |
2090 | memcpy (TYPE_FIELDS (resolved_type), |
2091 | TYPE_FIELDS (type), | |
2092 | TYPE_NFIELDS (resolved_type) * sizeof (struct field)); | |
2093 | for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i) | |
2094 | { | |
2095 | struct type *t; | |
2096 | ||
2097 | if (field_is_static (&TYPE_FIELD (type, i))) | |
2098 | continue; | |
2099 | ||
d98b7a16 | 2100 | t = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i), |
ee715b5a | 2101 | addr_stack, 0); |
012370f6 TT |
2102 | TYPE_FIELD_TYPE (resolved_type, i) = t; |
2103 | if (TYPE_LENGTH (t) > max_len) | |
2104 | max_len = TYPE_LENGTH (t); | |
2105 | } | |
2106 | ||
2107 | TYPE_LENGTH (resolved_type) = max_len; | |
2108 | return resolved_type; | |
2109 | } | |
2110 | ||
2111 | /* Resolve dynamic bounds of members of the struct TYPE to static | |
df25ebbd JB |
2112 | bounds. ADDR_STACK is a stack of struct property_addr_info to |
2113 | be used if needed during the dynamic resolution. */ | |
012370f6 TT |
2114 | |
2115 | static struct type * | |
df25ebbd JB |
2116 | resolve_dynamic_struct (struct type *type, |
2117 | struct property_addr_info *addr_stack) | |
012370f6 TT |
2118 | { |
2119 | struct type *resolved_type; | |
2120 | int i; | |
6908c509 | 2121 | unsigned resolved_type_bit_length = 0; |
012370f6 TT |
2122 | |
2123 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT); | |
2124 | gdb_assert (TYPE_NFIELDS (type) > 0); | |
2125 | ||
2126 | resolved_type = copy_type (type); | |
2127 | TYPE_FIELDS (resolved_type) | |
224c3ddb SM |
2128 | = (struct field *) TYPE_ALLOC (resolved_type, |
2129 | TYPE_NFIELDS (resolved_type) | |
2130 | * sizeof (struct field)); | |
012370f6 TT |
2131 | memcpy (TYPE_FIELDS (resolved_type), |
2132 | TYPE_FIELDS (type), | |
2133 | TYPE_NFIELDS (resolved_type) * sizeof (struct field)); | |
2134 | for (i = 0; i < TYPE_NFIELDS (resolved_type); ++i) | |
2135 | { | |
6908c509 | 2136 | unsigned new_bit_length; |
df25ebbd | 2137 | struct property_addr_info pinfo; |
012370f6 TT |
2138 | |
2139 | if (field_is_static (&TYPE_FIELD (type, i))) | |
2140 | continue; | |
2141 | ||
6908c509 JB |
2142 | /* As we know this field is not a static field, the field's |
2143 | field_loc_kind should be FIELD_LOC_KIND_BITPOS. Verify | |
2144 | this is the case, but only trigger a simple error rather | |
2145 | than an internal error if that fails. While failing | |
2146 | that verification indicates a bug in our code, the error | |
2147 | is not severe enough to suggest to the user he stops | |
2148 | his debugging session because of it. */ | |
df25ebbd | 2149 | if (TYPE_FIELD_LOC_KIND (type, i) != FIELD_LOC_KIND_BITPOS) |
6908c509 JB |
2150 | error (_("Cannot determine struct field location" |
2151 | " (invalid location kind)")); | |
df25ebbd JB |
2152 | |
2153 | pinfo.type = check_typedef (TYPE_FIELD_TYPE (type, i)); | |
c3345124 | 2154 | pinfo.valaddr = addr_stack->valaddr; |
9920b434 BH |
2155 | pinfo.addr |
2156 | = (addr_stack->addr | |
2157 | + (TYPE_FIELD_BITPOS (resolved_type, i) / TARGET_CHAR_BIT)); | |
df25ebbd JB |
2158 | pinfo.next = addr_stack; |
2159 | ||
2160 | TYPE_FIELD_TYPE (resolved_type, i) | |
2161 | = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i), | |
ee715b5a | 2162 | &pinfo, 0); |
df25ebbd JB |
2163 | gdb_assert (TYPE_FIELD_LOC_KIND (resolved_type, i) |
2164 | == FIELD_LOC_KIND_BITPOS); | |
2165 | ||
6908c509 JB |
2166 | new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i); |
2167 | if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0) | |
2168 | new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i); | |
2169 | else | |
2170 | new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i)) | |
2171 | * TARGET_CHAR_BIT); | |
2172 | ||
2173 | /* Normally, we would use the position and size of the last field | |
2174 | to determine the size of the enclosing structure. But GCC seems | |
2175 | to be encoding the position of some fields incorrectly when | |
2176 | the struct contains a dynamic field that is not placed last. | |
2177 | So we compute the struct size based on the field that has | |
2178 | the highest position + size - probably the best we can do. */ | |
2179 | if (new_bit_length > resolved_type_bit_length) | |
2180 | resolved_type_bit_length = new_bit_length; | |
012370f6 TT |
2181 | } |
2182 | ||
9920b434 BH |
2183 | /* The length of a type won't change for fortran, but it does for C and Ada. |
2184 | For fortran the size of dynamic fields might change over time but not the | |
2185 | type length of the structure. If we adapt it, we run into problems | |
2186 | when calculating the element offset for arrays of structs. */ | |
2187 | if (current_language->la_language != language_fortran) | |
2188 | TYPE_LENGTH (resolved_type) | |
2189 | = (resolved_type_bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT; | |
6908c509 | 2190 | |
9e195661 PMR |
2191 | /* The Ada language uses this field as a cache for static fixed types: reset |
2192 | it as RESOLVED_TYPE must have its own static fixed type. */ | |
2193 | TYPE_TARGET_TYPE (resolved_type) = NULL; | |
2194 | ||
012370f6 TT |
2195 | return resolved_type; |
2196 | } | |
2197 | ||
d98b7a16 | 2198 | /* Worker for resolved_dynamic_type. */ |
80180f79 | 2199 | |
d98b7a16 | 2200 | static struct type * |
df25ebbd | 2201 | resolve_dynamic_type_internal (struct type *type, |
ee715b5a PMR |
2202 | struct property_addr_info *addr_stack, |
2203 | int top_level) | |
80180f79 SA |
2204 | { |
2205 | struct type *real_type = check_typedef (type); | |
6f8a3220 | 2206 | struct type *resolved_type = type; |
d9823cbb | 2207 | struct dynamic_prop *prop; |
3cdcd0ce | 2208 | CORE_ADDR value; |
80180f79 | 2209 | |
ee715b5a | 2210 | if (!is_dynamic_type_internal (real_type, top_level)) |
80180f79 SA |
2211 | return type; |
2212 | ||
5537b577 | 2213 | if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) |
6f8a3220 | 2214 | { |
cac9b138 JK |
2215 | resolved_type = copy_type (type); |
2216 | TYPE_TARGET_TYPE (resolved_type) | |
ee715b5a PMR |
2217 | = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), addr_stack, |
2218 | top_level); | |
5537b577 JK |
2219 | } |
2220 | else | |
2221 | { | |
2222 | /* Before trying to resolve TYPE, make sure it is not a stub. */ | |
2223 | type = real_type; | |
012370f6 | 2224 | |
5537b577 JK |
2225 | switch (TYPE_CODE (type)) |
2226 | { | |
e771e4be PMR |
2227 | case TYPE_CODE_REF: |
2228 | { | |
2229 | struct property_addr_info pinfo; | |
2230 | ||
2231 | pinfo.type = check_typedef (TYPE_TARGET_TYPE (type)); | |
c3345124 JB |
2232 | pinfo.valaddr = NULL; |
2233 | if (addr_stack->valaddr != NULL) | |
2234 | pinfo.addr = extract_typed_address (addr_stack->valaddr, type); | |
2235 | else | |
2236 | pinfo.addr = read_memory_typed_address (addr_stack->addr, type); | |
e771e4be PMR |
2237 | pinfo.next = addr_stack; |
2238 | ||
2239 | resolved_type = copy_type (type); | |
2240 | TYPE_TARGET_TYPE (resolved_type) | |
2241 | = resolve_dynamic_type_internal (TYPE_TARGET_TYPE (type), | |
2242 | &pinfo, top_level); | |
2243 | break; | |
2244 | } | |
2245 | ||
5537b577 | 2246 | case TYPE_CODE_ARRAY: |
df25ebbd | 2247 | resolved_type = resolve_dynamic_array (type, addr_stack); |
5537b577 JK |
2248 | break; |
2249 | ||
2250 | case TYPE_CODE_RANGE: | |
df25ebbd | 2251 | resolved_type = resolve_dynamic_range (type, addr_stack); |
5537b577 JK |
2252 | break; |
2253 | ||
2254 | case TYPE_CODE_UNION: | |
df25ebbd | 2255 | resolved_type = resolve_dynamic_union (type, addr_stack); |
5537b577 JK |
2256 | break; |
2257 | ||
2258 | case TYPE_CODE_STRUCT: | |
df25ebbd | 2259 | resolved_type = resolve_dynamic_struct (type, addr_stack); |
5537b577 JK |
2260 | break; |
2261 | } | |
6f8a3220 | 2262 | } |
80180f79 | 2263 | |
3cdcd0ce JB |
2264 | /* Resolve data_location attribute. */ |
2265 | prop = TYPE_DATA_LOCATION (resolved_type); | |
63e43d3a PMR |
2266 | if (prop != NULL |
2267 | && dwarf2_evaluate_property (prop, NULL, addr_stack, &value)) | |
3cdcd0ce | 2268 | { |
d9823cbb KB |
2269 | TYPE_DYN_PROP_ADDR (prop) = value; |
2270 | TYPE_DYN_PROP_KIND (prop) = PROP_CONST; | |
3cdcd0ce | 2271 | } |
3cdcd0ce | 2272 | |
80180f79 SA |
2273 | return resolved_type; |
2274 | } | |
2275 | ||
d98b7a16 TT |
2276 | /* See gdbtypes.h */ |
2277 | ||
2278 | struct type * | |
c3345124 JB |
2279 | resolve_dynamic_type (struct type *type, const gdb_byte *valaddr, |
2280 | CORE_ADDR addr) | |
d98b7a16 | 2281 | { |
c3345124 JB |
2282 | struct property_addr_info pinfo |
2283 | = {check_typedef (type), valaddr, addr, NULL}; | |
df25ebbd | 2284 | |
ee715b5a | 2285 | return resolve_dynamic_type_internal (type, &pinfo, 1); |
d98b7a16 TT |
2286 | } |
2287 | ||
d9823cbb KB |
2288 | /* See gdbtypes.h */ |
2289 | ||
2290 | struct dynamic_prop * | |
2291 | get_dyn_prop (enum dynamic_prop_node_kind prop_kind, const struct type *type) | |
2292 | { | |
2293 | struct dynamic_prop_list *node = TYPE_DYN_PROP_LIST (type); | |
2294 | ||
2295 | while (node != NULL) | |
2296 | { | |
2297 | if (node->prop_kind == prop_kind) | |
283a9958 | 2298 | return &node->prop; |
d9823cbb KB |
2299 | node = node->next; |
2300 | } | |
2301 | return NULL; | |
2302 | } | |
2303 | ||
2304 | /* See gdbtypes.h */ | |
2305 | ||
2306 | void | |
2307 | add_dyn_prop (enum dynamic_prop_node_kind prop_kind, struct dynamic_prop prop, | |
2308 | struct type *type, struct objfile *objfile) | |
2309 | { | |
2310 | struct dynamic_prop_list *temp; | |
2311 | ||
2312 | gdb_assert (TYPE_OBJFILE_OWNED (type)); | |
2313 | ||
224c3ddb | 2314 | temp = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop_list); |
d9823cbb | 2315 | temp->prop_kind = prop_kind; |
283a9958 | 2316 | temp->prop = prop; |
d9823cbb KB |
2317 | temp->next = TYPE_DYN_PROP_LIST (type); |
2318 | ||
2319 | TYPE_DYN_PROP_LIST (type) = temp; | |
2320 | } | |
2321 | ||
9920b434 BH |
2322 | /* Remove dynamic property from TYPE in case it exists. */ |
2323 | ||
2324 | void | |
2325 | remove_dyn_prop (enum dynamic_prop_node_kind prop_kind, | |
2326 | struct type *type) | |
2327 | { | |
2328 | struct dynamic_prop_list *prev_node, *curr_node; | |
2329 | ||
2330 | curr_node = TYPE_DYN_PROP_LIST (type); | |
2331 | prev_node = NULL; | |
2332 | ||
2333 | while (NULL != curr_node) | |
2334 | { | |
2335 | if (curr_node->prop_kind == prop_kind) | |
2336 | { | |
2337 | /* Update the linked list but don't free anything. | |
2338 | The property was allocated on objstack and it is not known | |
2339 | if we are on top of it. Nevertheless, everything is released | |
2340 | when the complete objstack is freed. */ | |
2341 | if (NULL == prev_node) | |
2342 | TYPE_DYN_PROP_LIST (type) = curr_node->next; | |
2343 | else | |
2344 | prev_node->next = curr_node->next; | |
2345 | ||
2346 | return; | |
2347 | } | |
2348 | ||
2349 | prev_node = curr_node; | |
2350 | curr_node = curr_node->next; | |
2351 | } | |
2352 | } | |
d9823cbb | 2353 | |
92163a10 JK |
2354 | /* Find the real type of TYPE. This function returns the real type, |
2355 | after removing all layers of typedefs, and completing opaque or stub | |
2356 | types. Completion changes the TYPE argument, but stripping of | |
2357 | typedefs does not. | |
2358 | ||
2359 | Instance flags (e.g. const/volatile) are preserved as typedefs are | |
2360 | stripped. If necessary a new qualified form of the underlying type | |
2361 | is created. | |
2362 | ||
2363 | NOTE: This will return a typedef if TYPE_TARGET_TYPE for the typedef has | |
2364 | not been computed and we're either in the middle of reading symbols, or | |
2365 | there was no name for the typedef in the debug info. | |
2366 | ||
9bc118a5 DE |
2367 | NOTE: Lookup of opaque types can throw errors for invalid symbol files. |
2368 | QUITs in the symbol reading code can also throw. | |
2369 | Thus this function can throw an exception. | |
2370 | ||
92163a10 JK |
2371 | If TYPE is a TYPE_CODE_TYPEDEF, its length is updated to the length of |
2372 | the target type. | |
c906108c SS |
2373 | |
2374 | If this is a stubbed struct (i.e. declared as struct foo *), see if | |
0963b4bd | 2375 | we can find a full definition in some other file. If so, copy this |
7ba81444 MS |
2376 | definition, so we can use it in future. There used to be a comment |
2377 | (but not any code) that if we don't find a full definition, we'd | |
2378 | set a flag so we don't spend time in the future checking the same | |
2379 | type. That would be a mistake, though--we might load in more | |
92163a10 | 2380 | symbols which contain a full definition for the type. */ |
c906108c SS |
2381 | |
2382 | struct type * | |
a02fd225 | 2383 | check_typedef (struct type *type) |
c906108c SS |
2384 | { |
2385 | struct type *orig_type = type; | |
92163a10 JK |
2386 | /* While we're removing typedefs, we don't want to lose qualifiers. |
2387 | E.g., const/volatile. */ | |
2388 | int instance_flags = TYPE_INSTANCE_FLAGS (type); | |
a02fd225 | 2389 | |
423c0af8 MS |
2390 | gdb_assert (type); |
2391 | ||
c906108c SS |
2392 | while (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) |
2393 | { | |
2394 | if (!TYPE_TARGET_TYPE (type)) | |
2395 | { | |
0d5cff50 | 2396 | const char *name; |
c906108c SS |
2397 | struct symbol *sym; |
2398 | ||
2399 | /* It is dangerous to call lookup_symbol if we are currently | |
7ba81444 | 2400 | reading a symtab. Infinite recursion is one danger. */ |
c906108c | 2401 | if (currently_reading_symtab) |
92163a10 | 2402 | return make_qualified_type (type, instance_flags, NULL); |
c906108c SS |
2403 | |
2404 | name = type_name_no_tag (type); | |
7ba81444 MS |
2405 | /* FIXME: shouldn't we separately check the TYPE_NAME and |
2406 | the TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or | |
2407 | VAR_DOMAIN as appropriate? (this code was written before | |
2408 | TYPE_NAME and TYPE_TAG_NAME were separate). */ | |
c906108c SS |
2409 | if (name == NULL) |
2410 | { | |
23136709 | 2411 | stub_noname_complaint (); |
92163a10 | 2412 | return make_qualified_type (type, instance_flags, NULL); |
c906108c | 2413 | } |
d12307c1 | 2414 | sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol; |
c906108c SS |
2415 | if (sym) |
2416 | TYPE_TARGET_TYPE (type) = SYMBOL_TYPE (sym); | |
7ba81444 | 2417 | else /* TYPE_CODE_UNDEF */ |
e9bb382b | 2418 | TYPE_TARGET_TYPE (type) = alloc_type_arch (get_type_arch (type)); |
c906108c SS |
2419 | } |
2420 | type = TYPE_TARGET_TYPE (type); | |
c906108c | 2421 | |
92163a10 JK |
2422 | /* Preserve the instance flags as we traverse down the typedef chain. |
2423 | ||
2424 | Handling address spaces/classes is nasty, what do we do if there's a | |
2425 | conflict? | |
2426 | E.g., what if an outer typedef marks the type as class_1 and an inner | |
2427 | typedef marks the type as class_2? | |
2428 | This is the wrong place to do such error checking. We leave it to | |
2429 | the code that created the typedef in the first place to flag the | |
2430 | error. We just pick the outer address space (akin to letting the | |
2431 | outer cast in a chain of casting win), instead of assuming | |
2432 | "it can't happen". */ | |
2433 | { | |
2434 | const int ALL_SPACES = (TYPE_INSTANCE_FLAG_CODE_SPACE | |
2435 | | TYPE_INSTANCE_FLAG_DATA_SPACE); | |
2436 | const int ALL_CLASSES = TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL; | |
2437 | int new_instance_flags = TYPE_INSTANCE_FLAGS (type); | |
2438 | ||
2439 | /* Treat code vs data spaces and address classes separately. */ | |
2440 | if ((instance_flags & ALL_SPACES) != 0) | |
2441 | new_instance_flags &= ~ALL_SPACES; | |
2442 | if ((instance_flags & ALL_CLASSES) != 0) | |
2443 | new_instance_flags &= ~ALL_CLASSES; | |
2444 | ||
2445 | instance_flags |= new_instance_flags; | |
2446 | } | |
2447 | } | |
a02fd225 | 2448 | |
7ba81444 MS |
2449 | /* If this is a struct/class/union with no fields, then check |
2450 | whether a full definition exists somewhere else. This is for | |
2451 | systems where a type definition with no fields is issued for such | |
2452 | types, instead of identifying them as stub types in the first | |
2453 | place. */ | |
c5aa993b | 2454 | |
7ba81444 MS |
2455 | if (TYPE_IS_OPAQUE (type) |
2456 | && opaque_type_resolution | |
2457 | && !currently_reading_symtab) | |
c906108c | 2458 | { |
0d5cff50 | 2459 | const char *name = type_name_no_tag (type); |
c5aa993b | 2460 | struct type *newtype; |
d8734c88 | 2461 | |
c906108c SS |
2462 | if (name == NULL) |
2463 | { | |
23136709 | 2464 | stub_noname_complaint (); |
92163a10 | 2465 | return make_qualified_type (type, instance_flags, NULL); |
c906108c SS |
2466 | } |
2467 | newtype = lookup_transparent_type (name); | |
ad766c0a | 2468 | |
c906108c | 2469 | if (newtype) |
ad766c0a | 2470 | { |
7ba81444 MS |
2471 | /* If the resolved type and the stub are in the same |
2472 | objfile, then replace the stub type with the real deal. | |
2473 | But if they're in separate objfiles, leave the stub | |
2474 | alone; we'll just look up the transparent type every time | |
2475 | we call check_typedef. We can't create pointers between | |
2476 | types allocated to different objfiles, since they may | |
2477 | have different lifetimes. Trying to copy NEWTYPE over to | |
2478 | TYPE's objfile is pointless, too, since you'll have to | |
2479 | move over any other types NEWTYPE refers to, which could | |
2480 | be an unbounded amount of stuff. */ | |
ad766c0a | 2481 | if (TYPE_OBJFILE (newtype) == TYPE_OBJFILE (type)) |
92163a10 JK |
2482 | type = make_qualified_type (newtype, |
2483 | TYPE_INSTANCE_FLAGS (type), | |
2484 | type); | |
ad766c0a JB |
2485 | else |
2486 | type = newtype; | |
2487 | } | |
c906108c | 2488 | } |
7ba81444 MS |
2489 | /* Otherwise, rely on the stub flag being set for opaque/stubbed |
2490 | types. */ | |
74a9bb82 | 2491 | else if (TYPE_STUB (type) && !currently_reading_symtab) |
c906108c | 2492 | { |
0d5cff50 | 2493 | const char *name = type_name_no_tag (type); |
c906108c | 2494 | /* FIXME: shouldn't we separately check the TYPE_NAME and the |
176620f1 | 2495 | TYPE_TAG_NAME, and look in STRUCT_DOMAIN and/or VAR_DOMAIN |
7b83ea04 AC |
2496 | as appropriate? (this code was written before TYPE_NAME and |
2497 | TYPE_TAG_NAME were separate). */ | |
c906108c | 2498 | struct symbol *sym; |
d8734c88 | 2499 | |
c906108c SS |
2500 | if (name == NULL) |
2501 | { | |
23136709 | 2502 | stub_noname_complaint (); |
92163a10 | 2503 | return make_qualified_type (type, instance_flags, NULL); |
c906108c | 2504 | } |
d12307c1 | 2505 | sym = lookup_symbol (name, 0, STRUCT_DOMAIN, 0).symbol; |
c906108c | 2506 | if (sym) |
c26f2453 JB |
2507 | { |
2508 | /* Same as above for opaque types, we can replace the stub | |
92163a10 | 2509 | with the complete type only if they are in the same |
c26f2453 JB |
2510 | objfile. */ |
2511 | if (TYPE_OBJFILE (SYMBOL_TYPE(sym)) == TYPE_OBJFILE (type)) | |
92163a10 JK |
2512 | type = make_qualified_type (SYMBOL_TYPE (sym), |
2513 | TYPE_INSTANCE_FLAGS (type), | |
2514 | type); | |
c26f2453 JB |
2515 | else |
2516 | type = SYMBOL_TYPE (sym); | |
2517 | } | |
c906108c SS |
2518 | } |
2519 | ||
74a9bb82 | 2520 | if (TYPE_TARGET_STUB (type)) |
c906108c | 2521 | { |
c906108c SS |
2522 | struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type)); |
2523 | ||
74a9bb82 | 2524 | if (TYPE_STUB (target_type) || TYPE_TARGET_STUB (target_type)) |
c5aa993b | 2525 | { |
73e2eb35 | 2526 | /* Nothing we can do. */ |
c5aa993b | 2527 | } |
c906108c SS |
2528 | else if (TYPE_CODE (type) == TYPE_CODE_RANGE) |
2529 | { | |
2530 | TYPE_LENGTH (type) = TYPE_LENGTH (target_type); | |
876cecd0 | 2531 | TYPE_TARGET_STUB (type) = 0; |
c906108c SS |
2532 | } |
2533 | } | |
92163a10 JK |
2534 | |
2535 | type = make_qualified_type (type, instance_flags, NULL); | |
2536 | ||
7ba81444 | 2537 | /* Cache TYPE_LENGTH for future use. */ |
c906108c | 2538 | TYPE_LENGTH (orig_type) = TYPE_LENGTH (type); |
92163a10 | 2539 | |
c906108c SS |
2540 | return type; |
2541 | } | |
2542 | ||
7ba81444 | 2543 | /* Parse a type expression in the string [P..P+LENGTH). If an error |
48319d1f | 2544 | occurs, silently return a void type. */ |
c91ecb25 | 2545 | |
b9362cc7 | 2546 | static struct type * |
48319d1f | 2547 | safe_parse_type (struct gdbarch *gdbarch, char *p, int length) |
c91ecb25 ND |
2548 | { |
2549 | struct ui_file *saved_gdb_stderr; | |
34365054 | 2550 | struct type *type = NULL; /* Initialize to keep gcc happy. */ |
c91ecb25 | 2551 | |
7ba81444 | 2552 | /* Suppress error messages. */ |
c91ecb25 | 2553 | saved_gdb_stderr = gdb_stderr; |
d7e74731 | 2554 | gdb_stderr = &null_stream; |
c91ecb25 | 2555 | |
7ba81444 | 2556 | /* Call parse_and_eval_type() without fear of longjmp()s. */ |
492d29ea | 2557 | TRY |
8e7b59a5 KS |
2558 | { |
2559 | type = parse_and_eval_type (p, length); | |
2560 | } | |
492d29ea PA |
2561 | CATCH (except, RETURN_MASK_ERROR) |
2562 | { | |
2563 | type = builtin_type (gdbarch)->builtin_void; | |
2564 | } | |
2565 | END_CATCH | |
c91ecb25 | 2566 | |
7ba81444 | 2567 | /* Stop suppressing error messages. */ |
c91ecb25 ND |
2568 | gdb_stderr = saved_gdb_stderr; |
2569 | ||
2570 | return type; | |
2571 | } | |
2572 | ||
c906108c SS |
2573 | /* Ugly hack to convert method stubs into method types. |
2574 | ||
7ba81444 MS |
2575 | He ain't kiddin'. This demangles the name of the method into a |
2576 | string including argument types, parses out each argument type, | |
2577 | generates a string casting a zero to that type, evaluates the | |
2578 | string, and stuffs the resulting type into an argtype vector!!! | |
2579 | Then it knows the type of the whole function (including argument | |
2580 | types for overloading), which info used to be in the stab's but was | |
2581 | removed to hack back the space required for them. */ | |
c906108c | 2582 | |
de17c821 | 2583 | static void |
fba45db2 | 2584 | check_stub_method (struct type *type, int method_id, int signature_id) |
c906108c | 2585 | { |
50810684 | 2586 | struct gdbarch *gdbarch = get_type_arch (type); |
c906108c SS |
2587 | struct fn_field *f; |
2588 | char *mangled_name = gdb_mangle_name (type, method_id, signature_id); | |
8de20a37 TT |
2589 | char *demangled_name = gdb_demangle (mangled_name, |
2590 | DMGL_PARAMS | DMGL_ANSI); | |
c906108c SS |
2591 | char *argtypetext, *p; |
2592 | int depth = 0, argcount = 1; | |
ad2f7632 | 2593 | struct field *argtypes; |
c906108c SS |
2594 | struct type *mtype; |
2595 | ||
2596 | /* Make sure we got back a function string that we can use. */ | |
2597 | if (demangled_name) | |
2598 | p = strchr (demangled_name, '('); | |
502dcf4e AC |
2599 | else |
2600 | p = NULL; | |
c906108c SS |
2601 | |
2602 | if (demangled_name == NULL || p == NULL) | |
7ba81444 MS |
2603 | error (_("Internal: Cannot demangle mangled name `%s'."), |
2604 | mangled_name); | |
c906108c SS |
2605 | |
2606 | /* Now, read in the parameters that define this type. */ | |
2607 | p += 1; | |
2608 | argtypetext = p; | |
2609 | while (*p) | |
2610 | { | |
070ad9f0 | 2611 | if (*p == '(' || *p == '<') |
c906108c SS |
2612 | { |
2613 | depth += 1; | |
2614 | } | |
070ad9f0 | 2615 | else if (*p == ')' || *p == '>') |
c906108c SS |
2616 | { |
2617 | depth -= 1; | |
2618 | } | |
2619 | else if (*p == ',' && depth == 0) | |
2620 | { | |
2621 | argcount += 1; | |
2622 | } | |
2623 | ||
2624 | p += 1; | |
2625 | } | |
2626 | ||
ad2f7632 | 2627 | /* If we read one argument and it was ``void'', don't count it. */ |
61012eef | 2628 | if (startswith (argtypetext, "(void)")) |
ad2f7632 | 2629 | argcount -= 1; |
c906108c | 2630 | |
ad2f7632 DJ |
2631 | /* We need one extra slot, for the THIS pointer. */ |
2632 | ||
2633 | argtypes = (struct field *) | |
2634 | TYPE_ALLOC (type, (argcount + 1) * sizeof (struct field)); | |
c906108c | 2635 | p = argtypetext; |
4a1970e4 DJ |
2636 | |
2637 | /* Add THIS pointer for non-static methods. */ | |
2638 | f = TYPE_FN_FIELDLIST1 (type, method_id); | |
2639 | if (TYPE_FN_FIELD_STATIC_P (f, signature_id)) | |
2640 | argcount = 0; | |
2641 | else | |
2642 | { | |
ad2f7632 | 2643 | argtypes[0].type = lookup_pointer_type (type); |
4a1970e4 DJ |
2644 | argcount = 1; |
2645 | } | |
c906108c | 2646 | |
0963b4bd | 2647 | if (*p != ')') /* () means no args, skip while. */ |
c906108c SS |
2648 | { |
2649 | depth = 0; | |
2650 | while (*p) | |
2651 | { | |
2652 | if (depth <= 0 && (*p == ',' || *p == ')')) | |
2653 | { | |
ad2f7632 DJ |
2654 | /* Avoid parsing of ellipsis, they will be handled below. |
2655 | Also avoid ``void'' as above. */ | |
2656 | if (strncmp (argtypetext, "...", p - argtypetext) != 0 | |
2657 | && strncmp (argtypetext, "void", p - argtypetext) != 0) | |
c906108c | 2658 | { |
ad2f7632 | 2659 | argtypes[argcount].type = |
48319d1f | 2660 | safe_parse_type (gdbarch, argtypetext, p - argtypetext); |
c906108c SS |
2661 | argcount += 1; |
2662 | } | |
2663 | argtypetext = p + 1; | |
2664 | } | |
2665 | ||
070ad9f0 | 2666 | if (*p == '(' || *p == '<') |
c906108c SS |
2667 | { |
2668 | depth += 1; | |
2669 | } | |
070ad9f0 | 2670 | else if (*p == ')' || *p == '>') |
c906108c SS |
2671 | { |
2672 | depth -= 1; | |
2673 | } | |
2674 | ||
2675 | p += 1; | |
2676 | } | |
2677 | } | |
2678 | ||
c906108c SS |
2679 | TYPE_FN_FIELD_PHYSNAME (f, signature_id) = mangled_name; |
2680 | ||
2681 | /* Now update the old "stub" type into a real type. */ | |
2682 | mtype = TYPE_FN_FIELD_TYPE (f, signature_id); | |
09e2d7c7 DE |
2683 | /* MTYPE may currently be a function (TYPE_CODE_FUNC). |
2684 | We want a method (TYPE_CODE_METHOD). */ | |
2685 | smash_to_method_type (mtype, type, TYPE_TARGET_TYPE (mtype), | |
2686 | argtypes, argcount, p[-2] == '.'); | |
876cecd0 | 2687 | TYPE_STUB (mtype) = 0; |
c906108c | 2688 | TYPE_FN_FIELD_STUB (f, signature_id) = 0; |
ad2f7632 DJ |
2689 | |
2690 | xfree (demangled_name); | |
c906108c SS |
2691 | } |
2692 | ||
7ba81444 MS |
2693 | /* This is the external interface to check_stub_method, above. This |
2694 | function unstubs all of the signatures for TYPE's METHOD_ID method | |
2695 | name. After calling this function TYPE_FN_FIELD_STUB will be | |
2696 | cleared for each signature and TYPE_FN_FIELDLIST_NAME will be | |
2697 | correct. | |
de17c821 DJ |
2698 | |
2699 | This function unfortunately can not die until stabs do. */ | |
2700 | ||
2701 | void | |
2702 | check_stub_method_group (struct type *type, int method_id) | |
2703 | { | |
2704 | int len = TYPE_FN_FIELDLIST_LENGTH (type, method_id); | |
2705 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id); | |
f710f4fc | 2706 | int j, found_stub = 0; |
de17c821 DJ |
2707 | |
2708 | for (j = 0; j < len; j++) | |
2709 | if (TYPE_FN_FIELD_STUB (f, j)) | |
2710 | { | |
2711 | found_stub = 1; | |
2712 | check_stub_method (type, method_id, j); | |
2713 | } | |
2714 | ||
7ba81444 MS |
2715 | /* GNU v3 methods with incorrect names were corrected when we read |
2716 | in type information, because it was cheaper to do it then. The | |
2717 | only GNU v2 methods with incorrect method names are operators and | |
2718 | destructors; destructors were also corrected when we read in type | |
2719 | information. | |
de17c821 DJ |
2720 | |
2721 | Therefore the only thing we need to handle here are v2 operator | |
2722 | names. */ | |
61012eef | 2723 | if (found_stub && !startswith (TYPE_FN_FIELD_PHYSNAME (f, 0), "_Z")) |
de17c821 DJ |
2724 | { |
2725 | int ret; | |
2726 | char dem_opname[256]; | |
2727 | ||
7ba81444 MS |
2728 | ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, |
2729 | method_id), | |
de17c821 DJ |
2730 | dem_opname, DMGL_ANSI); |
2731 | if (!ret) | |
7ba81444 MS |
2732 | ret = cplus_demangle_opname (TYPE_FN_FIELDLIST_NAME (type, |
2733 | method_id), | |
de17c821 DJ |
2734 | dem_opname, 0); |
2735 | if (ret) | |
2736 | TYPE_FN_FIELDLIST_NAME (type, method_id) = xstrdup (dem_opname); | |
2737 | } | |
2738 | } | |
2739 | ||
9655fd1a JK |
2740 | /* Ensure it is in .rodata (if available) by workarounding GCC PR 44690. */ |
2741 | const struct cplus_struct_type cplus_struct_default = { }; | |
c906108c SS |
2742 | |
2743 | void | |
fba45db2 | 2744 | allocate_cplus_struct_type (struct type *type) |
c906108c | 2745 | { |
b4ba55a1 JB |
2746 | if (HAVE_CPLUS_STRUCT (type)) |
2747 | /* Structure was already allocated. Nothing more to do. */ | |
2748 | return; | |
2749 | ||
2750 | TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_CPLUS_STUFF; | |
2751 | TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *) | |
2752 | TYPE_ALLOC (type, sizeof (struct cplus_struct_type)); | |
2753 | *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default; | |
ae6ae975 | 2754 | set_type_vptr_fieldno (type, -1); |
c906108c SS |
2755 | } |
2756 | ||
b4ba55a1 JB |
2757 | const struct gnat_aux_type gnat_aux_default = |
2758 | { NULL }; | |
2759 | ||
2760 | /* Set the TYPE's type-specific kind to TYPE_SPECIFIC_GNAT_STUFF, | |
2761 | and allocate the associated gnat-specific data. The gnat-specific | |
2762 | data is also initialized to gnat_aux_default. */ | |
5212577a | 2763 | |
b4ba55a1 JB |
2764 | void |
2765 | allocate_gnat_aux_type (struct type *type) | |
2766 | { | |
2767 | TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_GNAT_STUFF; | |
2768 | TYPE_GNAT_SPECIFIC (type) = (struct gnat_aux_type *) | |
2769 | TYPE_ALLOC (type, sizeof (struct gnat_aux_type)); | |
2770 | *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default; | |
2771 | } | |
2772 | ||
ae438bc5 UW |
2773 | /* Helper function to initialize a newly allocated type. Set type code |
2774 | to CODE and initialize the type-specific fields accordingly. */ | |
2775 | ||
2776 | static void | |
2777 | set_type_code (struct type *type, enum type_code code) | |
2778 | { | |
2779 | TYPE_CODE (type) = code; | |
2780 | ||
2781 | switch (code) | |
2782 | { | |
2783 | case TYPE_CODE_STRUCT: | |
2784 | case TYPE_CODE_UNION: | |
2785 | case TYPE_CODE_NAMESPACE: | |
2786 | INIT_CPLUS_SPECIFIC (type); | |
2787 | break; | |
2788 | case TYPE_CODE_FLT: | |
2789 | TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT; | |
2790 | break; | |
2791 | case TYPE_CODE_FUNC: | |
2792 | INIT_FUNC_SPECIFIC (type); | |
2793 | break; | |
2794 | } | |
2795 | } | |
2796 | ||
19f392bc UW |
2797 | /* Helper function to verify floating-point format and size. |
2798 | BIT is the type size in bits; if BIT equals -1, the size is | |
2799 | determined by the floatformat. Returns size to be used. */ | |
2800 | ||
2801 | static int | |
0db7851f | 2802 | verify_floatformat (int bit, const struct floatformat *floatformat) |
19f392bc | 2803 | { |
0db7851f | 2804 | gdb_assert (floatformat != NULL); |
9b790ce7 | 2805 | |
19f392bc | 2806 | if (bit == -1) |
0db7851f | 2807 | bit = floatformat->totalsize; |
19f392bc | 2808 | |
0db7851f UW |
2809 | gdb_assert (bit >= 0); |
2810 | gdb_assert (bit >= floatformat->totalsize); | |
19f392bc UW |
2811 | |
2812 | return bit; | |
2813 | } | |
2814 | ||
0db7851f UW |
2815 | /* Return the floating-point format for a floating-point variable of |
2816 | type TYPE. */ | |
2817 | ||
2818 | const struct floatformat * | |
2819 | floatformat_from_type (const struct type *type) | |
2820 | { | |
2821 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT); | |
2822 | gdb_assert (TYPE_FLOATFORMAT (type)); | |
2823 | return TYPE_FLOATFORMAT (type); | |
2824 | } | |
2825 | ||
c906108c SS |
2826 | /* Helper function to initialize the standard scalar types. |
2827 | ||
86f62fd7 TT |
2828 | If NAME is non-NULL, then it is used to initialize the type name. |
2829 | Note that NAME is not copied; it is required to have a lifetime at | |
2830 | least as long as OBJFILE. */ | |
c906108c SS |
2831 | |
2832 | struct type * | |
77b7c781 | 2833 | init_type (struct objfile *objfile, enum type_code code, int bit, |
19f392bc | 2834 | const char *name) |
c906108c | 2835 | { |
52f0bd74 | 2836 | struct type *type; |
c906108c SS |
2837 | |
2838 | type = alloc_type (objfile); | |
ae438bc5 | 2839 | set_type_code (type, code); |
77b7c781 UW |
2840 | gdb_assert ((bit % TARGET_CHAR_BIT) == 0); |
2841 | TYPE_LENGTH (type) = bit / TARGET_CHAR_BIT; | |
86f62fd7 | 2842 | TYPE_NAME (type) = name; |
c906108c | 2843 | |
c16abbde | 2844 | return type; |
c906108c | 2845 | } |
19f392bc | 2846 | |
46a4882b PA |
2847 | /* Allocate a TYPE_CODE_ERROR type structure associated with OBJFILE, |
2848 | to use with variables that have no debug info. NAME is the type | |
2849 | name. */ | |
2850 | ||
2851 | static struct type * | |
2852 | init_nodebug_var_type (struct objfile *objfile, const char *name) | |
2853 | { | |
2854 | return init_type (objfile, TYPE_CODE_ERROR, 0, name); | |
2855 | } | |
2856 | ||
19f392bc UW |
2857 | /* Allocate a TYPE_CODE_INT type structure associated with OBJFILE. |
2858 | BIT is the type size in bits. If UNSIGNED_P is non-zero, set | |
2859 | the type's TYPE_UNSIGNED flag. NAME is the type name. */ | |
2860 | ||
2861 | struct type * | |
2862 | init_integer_type (struct objfile *objfile, | |
2863 | int bit, int unsigned_p, const char *name) | |
2864 | { | |
2865 | struct type *t; | |
2866 | ||
77b7c781 | 2867 | t = init_type (objfile, TYPE_CODE_INT, bit, name); |
19f392bc UW |
2868 | if (unsigned_p) |
2869 | TYPE_UNSIGNED (t) = 1; | |
2870 | ||
2871 | return t; | |
2872 | } | |
2873 | ||
2874 | /* Allocate a TYPE_CODE_CHAR type structure associated with OBJFILE. | |
2875 | BIT is the type size in bits. If UNSIGNED_P is non-zero, set | |
2876 | the type's TYPE_UNSIGNED flag. NAME is the type name. */ | |
2877 | ||
2878 | struct type * | |
2879 | init_character_type (struct objfile *objfile, | |
2880 | int bit, int unsigned_p, const char *name) | |
2881 | { | |
2882 | struct type *t; | |
2883 | ||
77b7c781 | 2884 | t = init_type (objfile, TYPE_CODE_CHAR, bit, name); |
19f392bc UW |
2885 | if (unsigned_p) |
2886 | TYPE_UNSIGNED (t) = 1; | |
2887 | ||
2888 | return t; | |
2889 | } | |
2890 | ||
2891 | /* Allocate a TYPE_CODE_BOOL type structure associated with OBJFILE. | |
2892 | BIT is the type size in bits. If UNSIGNED_P is non-zero, set | |
2893 | the type's TYPE_UNSIGNED flag. NAME is the type name. */ | |
2894 | ||
2895 | struct type * | |
2896 | init_boolean_type (struct objfile *objfile, | |
2897 | int bit, int unsigned_p, const char *name) | |
2898 | { | |
2899 | struct type *t; | |
2900 | ||
77b7c781 | 2901 | t = init_type (objfile, TYPE_CODE_BOOL, bit, name); |
19f392bc UW |
2902 | if (unsigned_p) |
2903 | TYPE_UNSIGNED (t) = 1; | |
2904 | ||
2905 | return t; | |
2906 | } | |
2907 | ||
2908 | /* Allocate a TYPE_CODE_FLT type structure associated with OBJFILE. | |
2909 | BIT is the type size in bits; if BIT equals -1, the size is | |
2910 | determined by the floatformat. NAME is the type name. Set the | |
2911 | TYPE_FLOATFORMAT from FLOATFORMATS. */ | |
2912 | ||
2913 | struct type * | |
2914 | init_float_type (struct objfile *objfile, | |
2915 | int bit, const char *name, | |
2916 | const struct floatformat **floatformats) | |
2917 | { | |
0db7851f UW |
2918 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
2919 | const struct floatformat *fmt = floatformats[gdbarch_byte_order (gdbarch)]; | |
19f392bc UW |
2920 | struct type *t; |
2921 | ||
0db7851f | 2922 | bit = verify_floatformat (bit, fmt); |
77b7c781 | 2923 | t = init_type (objfile, TYPE_CODE_FLT, bit, name); |
0db7851f | 2924 | TYPE_FLOATFORMAT (t) = fmt; |
19f392bc UW |
2925 | |
2926 | return t; | |
2927 | } | |
2928 | ||
2929 | /* Allocate a TYPE_CODE_DECFLOAT type structure associated with OBJFILE. | |
2930 | BIT is the type size in bits. NAME is the type name. */ | |
2931 | ||
2932 | struct type * | |
2933 | init_decfloat_type (struct objfile *objfile, int bit, const char *name) | |
2934 | { | |
2935 | struct type *t; | |
2936 | ||
77b7c781 | 2937 | t = init_type (objfile, TYPE_CODE_DECFLOAT, bit, name); |
19f392bc UW |
2938 | return t; |
2939 | } | |
2940 | ||
2941 | /* Allocate a TYPE_CODE_COMPLEX type structure associated with OBJFILE. | |
2942 | NAME is the type name. TARGET_TYPE is the component float type. */ | |
2943 | ||
2944 | struct type * | |
2945 | init_complex_type (struct objfile *objfile, | |
2946 | const char *name, struct type *target_type) | |
2947 | { | |
2948 | struct type *t; | |
2949 | ||
2950 | t = init_type (objfile, TYPE_CODE_COMPLEX, | |
77b7c781 | 2951 | 2 * TYPE_LENGTH (target_type) * TARGET_CHAR_BIT, name); |
19f392bc UW |
2952 | TYPE_TARGET_TYPE (t) = target_type; |
2953 | return t; | |
2954 | } | |
2955 | ||
2956 | /* Allocate a TYPE_CODE_PTR type structure associated with OBJFILE. | |
2957 | BIT is the pointer type size in bits. NAME is the type name. | |
2958 | TARGET_TYPE is the pointer target type. Always sets the pointer type's | |
2959 | TYPE_UNSIGNED flag. */ | |
2960 | ||
2961 | struct type * | |
2962 | init_pointer_type (struct objfile *objfile, | |
2963 | int bit, const char *name, struct type *target_type) | |
2964 | { | |
2965 | struct type *t; | |
2966 | ||
77b7c781 | 2967 | t = init_type (objfile, TYPE_CODE_PTR, bit, name); |
19f392bc UW |
2968 | TYPE_TARGET_TYPE (t) = target_type; |
2969 | TYPE_UNSIGNED (t) = 1; | |
2970 | return t; | |
2971 | } | |
2972 | ||
5212577a DE |
2973 | \f |
2974 | /* Queries on types. */ | |
c906108c | 2975 | |
c906108c | 2976 | int |
fba45db2 | 2977 | can_dereference (struct type *t) |
c906108c | 2978 | { |
7ba81444 MS |
2979 | /* FIXME: Should we return true for references as well as |
2980 | pointers? */ | |
f168693b | 2981 | t = check_typedef (t); |
c906108c SS |
2982 | return |
2983 | (t != NULL | |
2984 | && TYPE_CODE (t) == TYPE_CODE_PTR | |
2985 | && TYPE_CODE (TYPE_TARGET_TYPE (t)) != TYPE_CODE_VOID); | |
2986 | } | |
2987 | ||
adf40b2e | 2988 | int |
fba45db2 | 2989 | is_integral_type (struct type *t) |
adf40b2e | 2990 | { |
f168693b | 2991 | t = check_typedef (t); |
adf40b2e JM |
2992 | return |
2993 | ((t != NULL) | |
d4f3574e SS |
2994 | && ((TYPE_CODE (t) == TYPE_CODE_INT) |
2995 | || (TYPE_CODE (t) == TYPE_CODE_ENUM) | |
4f2aea11 | 2996 | || (TYPE_CODE (t) == TYPE_CODE_FLAGS) |
d4f3574e SS |
2997 | || (TYPE_CODE (t) == TYPE_CODE_CHAR) |
2998 | || (TYPE_CODE (t) == TYPE_CODE_RANGE) | |
2999 | || (TYPE_CODE (t) == TYPE_CODE_BOOL))); | |
adf40b2e JM |
3000 | } |
3001 | ||
70100014 UW |
3002 | int |
3003 | is_floating_type (struct type *t) | |
3004 | { | |
3005 | t = check_typedef (t); | |
3006 | return | |
3007 | ((t != NULL) | |
3008 | && ((TYPE_CODE (t) == TYPE_CODE_FLT) | |
3009 | || (TYPE_CODE (t) == TYPE_CODE_DECFLOAT))); | |
3010 | } | |
3011 | ||
e09342b5 TJB |
3012 | /* Return true if TYPE is scalar. */ |
3013 | ||
220475ed | 3014 | int |
e09342b5 TJB |
3015 | is_scalar_type (struct type *type) |
3016 | { | |
f168693b | 3017 | type = check_typedef (type); |
e09342b5 TJB |
3018 | |
3019 | switch (TYPE_CODE (type)) | |
3020 | { | |
3021 | case TYPE_CODE_ARRAY: | |
3022 | case TYPE_CODE_STRUCT: | |
3023 | case TYPE_CODE_UNION: | |
3024 | case TYPE_CODE_SET: | |
3025 | case TYPE_CODE_STRING: | |
e09342b5 TJB |
3026 | return 0; |
3027 | default: | |
3028 | return 1; | |
3029 | } | |
3030 | } | |
3031 | ||
3032 | /* Return true if T is scalar, or a composite type which in practice has | |
90e4670f TJB |
3033 | the memory layout of a scalar type. E.g., an array or struct with only |
3034 | one scalar element inside it, or a union with only scalar elements. */ | |
e09342b5 TJB |
3035 | |
3036 | int | |
3037 | is_scalar_type_recursive (struct type *t) | |
3038 | { | |
f168693b | 3039 | t = check_typedef (t); |
e09342b5 TJB |
3040 | |
3041 | if (is_scalar_type (t)) | |
3042 | return 1; | |
3043 | /* Are we dealing with an array or string of known dimensions? */ | |
3044 | else if ((TYPE_CODE (t) == TYPE_CODE_ARRAY | |
3045 | || TYPE_CODE (t) == TYPE_CODE_STRING) && TYPE_NFIELDS (t) == 1 | |
3046 | && TYPE_CODE (TYPE_INDEX_TYPE (t)) == TYPE_CODE_RANGE) | |
3047 | { | |
3048 | LONGEST low_bound, high_bound; | |
3049 | struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (t)); | |
3050 | ||
3051 | get_discrete_bounds (TYPE_INDEX_TYPE (t), &low_bound, &high_bound); | |
3052 | ||
3053 | return high_bound == low_bound && is_scalar_type_recursive (elt_type); | |
3054 | } | |
3055 | /* Are we dealing with a struct with one element? */ | |
3056 | else if (TYPE_CODE (t) == TYPE_CODE_STRUCT && TYPE_NFIELDS (t) == 1) | |
3057 | return is_scalar_type_recursive (TYPE_FIELD_TYPE (t, 0)); | |
3058 | else if (TYPE_CODE (t) == TYPE_CODE_UNION) | |
3059 | { | |
3060 | int i, n = TYPE_NFIELDS (t); | |
3061 | ||
3062 | /* If all elements of the union are scalar, then the union is scalar. */ | |
3063 | for (i = 0; i < n; i++) | |
3064 | if (!is_scalar_type_recursive (TYPE_FIELD_TYPE (t, i))) | |
3065 | return 0; | |
3066 | ||
3067 | return 1; | |
3068 | } | |
3069 | ||
3070 | return 0; | |
3071 | } | |
3072 | ||
6c659fc2 SC |
3073 | /* Return true is T is a class or a union. False otherwise. */ |
3074 | ||
3075 | int | |
3076 | class_or_union_p (const struct type *t) | |
3077 | { | |
3078 | return (TYPE_CODE (t) == TYPE_CODE_STRUCT | |
3079 | || TYPE_CODE (t) == TYPE_CODE_UNION); | |
3080 | } | |
3081 | ||
4e8f195d TT |
3082 | /* A helper function which returns true if types A and B represent the |
3083 | "same" class type. This is true if the types have the same main | |
3084 | type, or the same name. */ | |
3085 | ||
3086 | int | |
3087 | class_types_same_p (const struct type *a, const struct type *b) | |
3088 | { | |
3089 | return (TYPE_MAIN_TYPE (a) == TYPE_MAIN_TYPE (b) | |
3090 | || (TYPE_NAME (a) && TYPE_NAME (b) | |
3091 | && !strcmp (TYPE_NAME (a), TYPE_NAME (b)))); | |
3092 | } | |
3093 | ||
a9d5ef47 SW |
3094 | /* If BASE is an ancestor of DCLASS return the distance between them. |
3095 | otherwise return -1; | |
3096 | eg: | |
3097 | ||
3098 | class A {}; | |
3099 | class B: public A {}; | |
3100 | class C: public B {}; | |
3101 | class D: C {}; | |
3102 | ||
3103 | distance_to_ancestor (A, A, 0) = 0 | |
3104 | distance_to_ancestor (A, B, 0) = 1 | |
3105 | distance_to_ancestor (A, C, 0) = 2 | |
3106 | distance_to_ancestor (A, D, 0) = 3 | |
3107 | ||
3108 | If PUBLIC is 1 then only public ancestors are considered, | |
3109 | and the function returns the distance only if BASE is a public ancestor | |
3110 | of DCLASS. | |
3111 | Eg: | |
3112 | ||
0963b4bd | 3113 | distance_to_ancestor (A, D, 1) = -1. */ |
c906108c | 3114 | |
0526b37a | 3115 | static int |
fe978cb0 | 3116 | distance_to_ancestor (struct type *base, struct type *dclass, int is_public) |
c906108c SS |
3117 | { |
3118 | int i; | |
a9d5ef47 | 3119 | int d; |
c5aa993b | 3120 | |
f168693b SM |
3121 | base = check_typedef (base); |
3122 | dclass = check_typedef (dclass); | |
c906108c | 3123 | |
4e8f195d | 3124 | if (class_types_same_p (base, dclass)) |
a9d5ef47 | 3125 | return 0; |
c906108c SS |
3126 | |
3127 | for (i = 0; i < TYPE_N_BASECLASSES (dclass); i++) | |
4e8f195d | 3128 | { |
fe978cb0 | 3129 | if (is_public && ! BASETYPE_VIA_PUBLIC (dclass, i)) |
0526b37a SW |
3130 | continue; |
3131 | ||
fe978cb0 | 3132 | d = distance_to_ancestor (base, TYPE_BASECLASS (dclass, i), is_public); |
a9d5ef47 SW |
3133 | if (d >= 0) |
3134 | return 1 + d; | |
4e8f195d | 3135 | } |
c906108c | 3136 | |
a9d5ef47 | 3137 | return -1; |
c906108c | 3138 | } |
4e8f195d | 3139 | |
0526b37a SW |
3140 | /* Check whether BASE is an ancestor or base class or DCLASS |
3141 | Return 1 if so, and 0 if not. | |
3142 | Note: If BASE and DCLASS are of the same type, this function | |
3143 | will return 1. So for some class A, is_ancestor (A, A) will | |
3144 | return 1. */ | |
3145 | ||
3146 | int | |
3147 | is_ancestor (struct type *base, struct type *dclass) | |
3148 | { | |
a9d5ef47 | 3149 | return distance_to_ancestor (base, dclass, 0) >= 0; |
0526b37a SW |
3150 | } |
3151 | ||
4e8f195d TT |
3152 | /* Like is_ancestor, but only returns true when BASE is a public |
3153 | ancestor of DCLASS. */ | |
3154 | ||
3155 | int | |
3156 | is_public_ancestor (struct type *base, struct type *dclass) | |
3157 | { | |
a9d5ef47 | 3158 | return distance_to_ancestor (base, dclass, 1) >= 0; |
4e8f195d TT |
3159 | } |
3160 | ||
3161 | /* A helper function for is_unique_ancestor. */ | |
3162 | ||
3163 | static int | |
3164 | is_unique_ancestor_worker (struct type *base, struct type *dclass, | |
3165 | int *offset, | |
8af8e3bc PA |
3166 | const gdb_byte *valaddr, int embedded_offset, |
3167 | CORE_ADDR address, struct value *val) | |
4e8f195d TT |
3168 | { |
3169 | int i, count = 0; | |
3170 | ||
f168693b SM |
3171 | base = check_typedef (base); |
3172 | dclass = check_typedef (dclass); | |
4e8f195d TT |
3173 | |
3174 | for (i = 0; i < TYPE_N_BASECLASSES (dclass) && count < 2; ++i) | |
3175 | { | |
8af8e3bc PA |
3176 | struct type *iter; |
3177 | int this_offset; | |
4e8f195d | 3178 | |
8af8e3bc PA |
3179 | iter = check_typedef (TYPE_BASECLASS (dclass, i)); |
3180 | ||
3181 | this_offset = baseclass_offset (dclass, i, valaddr, embedded_offset, | |
3182 | address, val); | |
4e8f195d TT |
3183 | |
3184 | if (class_types_same_p (base, iter)) | |
3185 | { | |
3186 | /* If this is the first subclass, set *OFFSET and set count | |
3187 | to 1. Otherwise, if this is at the same offset as | |
3188 | previous instances, do nothing. Otherwise, increment | |
3189 | count. */ | |
3190 | if (*offset == -1) | |
3191 | { | |
3192 | *offset = this_offset; | |
3193 | count = 1; | |
3194 | } | |
3195 | else if (this_offset == *offset) | |
3196 | { | |
3197 | /* Nothing. */ | |
3198 | } | |
3199 | else | |
3200 | ++count; | |
3201 | } | |
3202 | else | |
3203 | count += is_unique_ancestor_worker (base, iter, offset, | |
8af8e3bc PA |
3204 | valaddr, |
3205 | embedded_offset + this_offset, | |
3206 | address, val); | |
4e8f195d TT |
3207 | } |
3208 | ||
3209 | return count; | |
3210 | } | |
3211 | ||
3212 | /* Like is_ancestor, but only returns true if BASE is a unique base | |
3213 | class of the type of VAL. */ | |
3214 | ||
3215 | int | |
3216 | is_unique_ancestor (struct type *base, struct value *val) | |
3217 | { | |
3218 | int offset = -1; | |
3219 | ||
3220 | return is_unique_ancestor_worker (base, value_type (val), &offset, | |
8af8e3bc PA |
3221 | value_contents_for_printing (val), |
3222 | value_embedded_offset (val), | |
3223 | value_address (val), val) == 1; | |
4e8f195d TT |
3224 | } |
3225 | ||
c906108c | 3226 | \f |
5212577a | 3227 | /* Overload resolution. */ |
c906108c | 3228 | |
6403aeea SW |
3229 | /* Return the sum of the rank of A with the rank of B. */ |
3230 | ||
3231 | struct rank | |
3232 | sum_ranks (struct rank a, struct rank b) | |
3233 | { | |
3234 | struct rank c; | |
3235 | c.rank = a.rank + b.rank; | |
a9d5ef47 | 3236 | c.subrank = a.subrank + b.subrank; |
6403aeea SW |
3237 | return c; |
3238 | } | |
3239 | ||
3240 | /* Compare rank A and B and return: | |
3241 | 0 if a = b | |
3242 | 1 if a is better than b | |
3243 | -1 if b is better than a. */ | |
3244 | ||
3245 | int | |
3246 | compare_ranks (struct rank a, struct rank b) | |
3247 | { | |
3248 | if (a.rank == b.rank) | |
a9d5ef47 SW |
3249 | { |
3250 | if (a.subrank == b.subrank) | |
3251 | return 0; | |
3252 | if (a.subrank < b.subrank) | |
3253 | return 1; | |
3254 | if (a.subrank > b.subrank) | |
3255 | return -1; | |
3256 | } | |
6403aeea SW |
3257 | |
3258 | if (a.rank < b.rank) | |
3259 | return 1; | |
3260 | ||
0963b4bd | 3261 | /* a.rank > b.rank */ |
6403aeea SW |
3262 | return -1; |
3263 | } | |
c5aa993b | 3264 | |
0963b4bd | 3265 | /* Functions for overload resolution begin here. */ |
c906108c SS |
3266 | |
3267 | /* Compare two badness vectors A and B and return the result. | |
7ba81444 MS |
3268 | 0 => A and B are identical |
3269 | 1 => A and B are incomparable | |
3270 | 2 => A is better than B | |
3271 | 3 => A is worse than B */ | |
c906108c SS |
3272 | |
3273 | int | |
fba45db2 | 3274 | compare_badness (struct badness_vector *a, struct badness_vector *b) |
c906108c SS |
3275 | { |
3276 | int i; | |
3277 | int tmp; | |
c5aa993b JM |
3278 | short found_pos = 0; /* any positives in c? */ |
3279 | short found_neg = 0; /* any negatives in c? */ | |
3280 | ||
3281 | /* differing lengths => incomparable */ | |
c906108c SS |
3282 | if (a->length != b->length) |
3283 | return 1; | |
3284 | ||
c5aa993b JM |
3285 | /* Subtract b from a */ |
3286 | for (i = 0; i < a->length; i++) | |
c906108c | 3287 | { |
6403aeea | 3288 | tmp = compare_ranks (b->rank[i], a->rank[i]); |
c906108c | 3289 | if (tmp > 0) |
c5aa993b | 3290 | found_pos = 1; |
c906108c | 3291 | else if (tmp < 0) |
c5aa993b | 3292 | found_neg = 1; |
c906108c SS |
3293 | } |
3294 | ||
3295 | if (found_pos) | |
3296 | { | |
3297 | if (found_neg) | |
c5aa993b | 3298 | return 1; /* incomparable */ |
c906108c | 3299 | else |
c5aa993b | 3300 | return 3; /* A > B */ |
c906108c | 3301 | } |
c5aa993b JM |
3302 | else |
3303 | /* no positives */ | |
c906108c SS |
3304 | { |
3305 | if (found_neg) | |
c5aa993b | 3306 | return 2; /* A < B */ |
c906108c | 3307 | else |
c5aa993b | 3308 | return 0; /* A == B */ |
c906108c SS |
3309 | } |
3310 | } | |
3311 | ||
7ba81444 MS |
3312 | /* Rank a function by comparing its parameter types (PARMS, length |
3313 | NPARMS), to the types of an argument list (ARGS, length NARGS). | |
3314 | Return a pointer to a badness vector. This has NARGS + 1 | |
3315 | entries. */ | |
c906108c SS |
3316 | |
3317 | struct badness_vector * | |
7ba81444 | 3318 | rank_function (struct type **parms, int nparms, |
da096638 | 3319 | struct value **args, int nargs) |
c906108c SS |
3320 | { |
3321 | int i; | |
8d749320 | 3322 | struct badness_vector *bv = XNEW (struct badness_vector); |
c906108c SS |
3323 | int min_len = nparms < nargs ? nparms : nargs; |
3324 | ||
0963b4bd | 3325 | bv->length = nargs + 1; /* add 1 for the length-match rank. */ |
c4e54771 | 3326 | bv->rank = XNEWVEC (struct rank, nargs + 1); |
c906108c SS |
3327 | |
3328 | /* First compare the lengths of the supplied lists. | |
7ba81444 | 3329 | If there is a mismatch, set it to a high value. */ |
c5aa993b | 3330 | |
c906108c | 3331 | /* pai/1997-06-03 FIXME: when we have debug info about default |
7ba81444 MS |
3332 | arguments and ellipsis parameter lists, we should consider those |
3333 | and rank the length-match more finely. */ | |
c906108c | 3334 | |
6403aeea SW |
3335 | LENGTH_MATCH (bv) = (nargs != nparms) |
3336 | ? LENGTH_MISMATCH_BADNESS | |
3337 | : EXACT_MATCH_BADNESS; | |
c906108c | 3338 | |
0963b4bd | 3339 | /* Now rank all the parameters of the candidate function. */ |
74cc24b0 | 3340 | for (i = 1; i <= min_len; i++) |
da096638 KS |
3341 | bv->rank[i] = rank_one_type (parms[i - 1], value_type (args[i - 1]), |
3342 | args[i - 1]); | |
c906108c | 3343 | |
0963b4bd | 3344 | /* If more arguments than parameters, add dummy entries. */ |
c5aa993b | 3345 | for (i = min_len + 1; i <= nargs; i++) |
c906108c SS |
3346 | bv->rank[i] = TOO_FEW_PARAMS_BADNESS; |
3347 | ||
3348 | return bv; | |
3349 | } | |
3350 | ||
973ccf8b DJ |
3351 | /* Compare the names of two integer types, assuming that any sign |
3352 | qualifiers have been checked already. We do it this way because | |
3353 | there may be an "int" in the name of one of the types. */ | |
3354 | ||
3355 | static int | |
3356 | integer_types_same_name_p (const char *first, const char *second) | |
3357 | { | |
3358 | int first_p, second_p; | |
3359 | ||
7ba81444 MS |
3360 | /* If both are shorts, return 1; if neither is a short, keep |
3361 | checking. */ | |
973ccf8b DJ |
3362 | first_p = (strstr (first, "short") != NULL); |
3363 | second_p = (strstr (second, "short") != NULL); | |
3364 | if (first_p && second_p) | |
3365 | return 1; | |
3366 | if (first_p || second_p) | |
3367 | return 0; | |
3368 | ||
3369 | /* Likewise for long. */ | |
3370 | first_p = (strstr (first, "long") != NULL); | |
3371 | second_p = (strstr (second, "long") != NULL); | |
3372 | if (first_p && second_p) | |
3373 | return 1; | |
3374 | if (first_p || second_p) | |
3375 | return 0; | |
3376 | ||
3377 | /* Likewise for char. */ | |
3378 | first_p = (strstr (first, "char") != NULL); | |
3379 | second_p = (strstr (second, "char") != NULL); | |
3380 | if (first_p && second_p) | |
3381 | return 1; | |
3382 | if (first_p || second_p) | |
3383 | return 0; | |
3384 | ||
3385 | /* They must both be ints. */ | |
3386 | return 1; | |
3387 | } | |
3388 | ||
7062b0a0 SW |
3389 | /* Compares type A to type B returns 1 if the represent the same type |
3390 | 0 otherwise. */ | |
3391 | ||
bd69fc68 | 3392 | int |
7062b0a0 SW |
3393 | types_equal (struct type *a, struct type *b) |
3394 | { | |
3395 | /* Identical type pointers. */ | |
3396 | /* However, this still doesn't catch all cases of same type for b | |
3397 | and a. The reason is that builtin types are different from | |
3398 | the same ones constructed from the object. */ | |
3399 | if (a == b) | |
3400 | return 1; | |
3401 | ||
3402 | /* Resolve typedefs */ | |
3403 | if (TYPE_CODE (a) == TYPE_CODE_TYPEDEF) | |
3404 | a = check_typedef (a); | |
3405 | if (TYPE_CODE (b) == TYPE_CODE_TYPEDEF) | |
3406 | b = check_typedef (b); | |
3407 | ||
3408 | /* If after resolving typedefs a and b are not of the same type | |
3409 | code then they are not equal. */ | |
3410 | if (TYPE_CODE (a) != TYPE_CODE (b)) | |
3411 | return 0; | |
3412 | ||
3413 | /* If a and b are both pointers types or both reference types then | |
3414 | they are equal of the same type iff the objects they refer to are | |
3415 | of the same type. */ | |
3416 | if (TYPE_CODE (a) == TYPE_CODE_PTR | |
3417 | || TYPE_CODE (a) == TYPE_CODE_REF) | |
3418 | return types_equal (TYPE_TARGET_TYPE (a), | |
3419 | TYPE_TARGET_TYPE (b)); | |
3420 | ||
0963b4bd | 3421 | /* Well, damnit, if the names are exactly the same, I'll say they |
7062b0a0 SW |
3422 | are exactly the same. This happens when we generate method |
3423 | stubs. The types won't point to the same address, but they | |
0963b4bd | 3424 | really are the same. */ |
7062b0a0 SW |
3425 | |
3426 | if (TYPE_NAME (a) && TYPE_NAME (b) | |
3427 | && strcmp (TYPE_NAME (a), TYPE_NAME (b)) == 0) | |
3428 | return 1; | |
3429 | ||
3430 | /* Check if identical after resolving typedefs. */ | |
3431 | if (a == b) | |
3432 | return 1; | |
3433 | ||
9ce98649 TT |
3434 | /* Two function types are equal if their argument and return types |
3435 | are equal. */ | |
3436 | if (TYPE_CODE (a) == TYPE_CODE_FUNC) | |
3437 | { | |
3438 | int i; | |
3439 | ||
3440 | if (TYPE_NFIELDS (a) != TYPE_NFIELDS (b)) | |
3441 | return 0; | |
3442 | ||
3443 | if (!types_equal (TYPE_TARGET_TYPE (a), TYPE_TARGET_TYPE (b))) | |
3444 | return 0; | |
3445 | ||
3446 | for (i = 0; i < TYPE_NFIELDS (a); ++i) | |
3447 | if (!types_equal (TYPE_FIELD_TYPE (a, i), TYPE_FIELD_TYPE (b, i))) | |
3448 | return 0; | |
3449 | ||
3450 | return 1; | |
3451 | } | |
3452 | ||
7062b0a0 SW |
3453 | return 0; |
3454 | } | |
ca092b61 DE |
3455 | \f |
3456 | /* Deep comparison of types. */ | |
3457 | ||
3458 | /* An entry in the type-equality bcache. */ | |
3459 | ||
3460 | typedef struct type_equality_entry | |
3461 | { | |
3462 | struct type *type1, *type2; | |
3463 | } type_equality_entry_d; | |
3464 | ||
3465 | DEF_VEC_O (type_equality_entry_d); | |
3466 | ||
3467 | /* A helper function to compare two strings. Returns 1 if they are | |
3468 | the same, 0 otherwise. Handles NULLs properly. */ | |
3469 | ||
3470 | static int | |
3471 | compare_maybe_null_strings (const char *s, const char *t) | |
3472 | { | |
3473 | if (s == NULL && t != NULL) | |
3474 | return 0; | |
3475 | else if (s != NULL && t == NULL) | |
3476 | return 0; | |
3477 | else if (s == NULL && t== NULL) | |
3478 | return 1; | |
3479 | return strcmp (s, t) == 0; | |
3480 | } | |
3481 | ||
3482 | /* A helper function for check_types_worklist that checks two types for | |
3483 | "deep" equality. Returns non-zero if the types are considered the | |
3484 | same, zero otherwise. */ | |
3485 | ||
3486 | static int | |
3487 | check_types_equal (struct type *type1, struct type *type2, | |
3488 | VEC (type_equality_entry_d) **worklist) | |
3489 | { | |
f168693b SM |
3490 | type1 = check_typedef (type1); |
3491 | type2 = check_typedef (type2); | |
ca092b61 DE |
3492 | |
3493 | if (type1 == type2) | |
3494 | return 1; | |
3495 | ||
3496 | if (TYPE_CODE (type1) != TYPE_CODE (type2) | |
3497 | || TYPE_LENGTH (type1) != TYPE_LENGTH (type2) | |
3498 | || TYPE_UNSIGNED (type1) != TYPE_UNSIGNED (type2) | |
3499 | || TYPE_NOSIGN (type1) != TYPE_NOSIGN (type2) | |
3500 | || TYPE_VARARGS (type1) != TYPE_VARARGS (type2) | |
3501 | || TYPE_VECTOR (type1) != TYPE_VECTOR (type2) | |
3502 | || TYPE_NOTTEXT (type1) != TYPE_NOTTEXT (type2) | |
3503 | || TYPE_INSTANCE_FLAGS (type1) != TYPE_INSTANCE_FLAGS (type2) | |
3504 | || TYPE_NFIELDS (type1) != TYPE_NFIELDS (type2)) | |
3505 | return 0; | |
3506 | ||
3507 | if (!compare_maybe_null_strings (TYPE_TAG_NAME (type1), | |
3508 | TYPE_TAG_NAME (type2))) | |
3509 | return 0; | |
3510 | if (!compare_maybe_null_strings (TYPE_NAME (type1), TYPE_NAME (type2))) | |
3511 | return 0; | |
3512 | ||
3513 | if (TYPE_CODE (type1) == TYPE_CODE_RANGE) | |
3514 | { | |
3515 | if (memcmp (TYPE_RANGE_DATA (type1), TYPE_RANGE_DATA (type2), | |
3516 | sizeof (*TYPE_RANGE_DATA (type1))) != 0) | |
3517 | return 0; | |
3518 | } | |
3519 | else | |
3520 | { | |
3521 | int i; | |
3522 | ||
3523 | for (i = 0; i < TYPE_NFIELDS (type1); ++i) | |
3524 | { | |
3525 | const struct field *field1 = &TYPE_FIELD (type1, i); | |
3526 | const struct field *field2 = &TYPE_FIELD (type2, i); | |
3527 | struct type_equality_entry entry; | |
3528 | ||
3529 | if (FIELD_ARTIFICIAL (*field1) != FIELD_ARTIFICIAL (*field2) | |
3530 | || FIELD_BITSIZE (*field1) != FIELD_BITSIZE (*field2) | |
3531 | || FIELD_LOC_KIND (*field1) != FIELD_LOC_KIND (*field2)) | |
3532 | return 0; | |
3533 | if (!compare_maybe_null_strings (FIELD_NAME (*field1), | |
3534 | FIELD_NAME (*field2))) | |
3535 | return 0; | |
3536 | switch (FIELD_LOC_KIND (*field1)) | |
3537 | { | |
3538 | case FIELD_LOC_KIND_BITPOS: | |
3539 | if (FIELD_BITPOS (*field1) != FIELD_BITPOS (*field2)) | |
3540 | return 0; | |
3541 | break; | |
3542 | case FIELD_LOC_KIND_ENUMVAL: | |
3543 | if (FIELD_ENUMVAL (*field1) != FIELD_ENUMVAL (*field2)) | |
3544 | return 0; | |
3545 | break; | |
3546 | case FIELD_LOC_KIND_PHYSADDR: | |
3547 | if (FIELD_STATIC_PHYSADDR (*field1) | |
3548 | != FIELD_STATIC_PHYSADDR (*field2)) | |
3549 | return 0; | |
3550 | break; | |
3551 | case FIELD_LOC_KIND_PHYSNAME: | |
3552 | if (!compare_maybe_null_strings (FIELD_STATIC_PHYSNAME (*field1), | |
3553 | FIELD_STATIC_PHYSNAME (*field2))) | |
3554 | return 0; | |
3555 | break; | |
3556 | case FIELD_LOC_KIND_DWARF_BLOCK: | |
3557 | { | |
3558 | struct dwarf2_locexpr_baton *block1, *block2; | |
3559 | ||
3560 | block1 = FIELD_DWARF_BLOCK (*field1); | |
3561 | block2 = FIELD_DWARF_BLOCK (*field2); | |
3562 | if (block1->per_cu != block2->per_cu | |
3563 | || block1->size != block2->size | |
3564 | || memcmp (block1->data, block2->data, block1->size) != 0) | |
3565 | return 0; | |
3566 | } | |
3567 | break; | |
3568 | default: | |
3569 | internal_error (__FILE__, __LINE__, _("Unsupported field kind " | |
3570 | "%d by check_types_equal"), | |
3571 | FIELD_LOC_KIND (*field1)); | |
3572 | } | |
3573 | ||
3574 | entry.type1 = FIELD_TYPE (*field1); | |
3575 | entry.type2 = FIELD_TYPE (*field2); | |
3576 | VEC_safe_push (type_equality_entry_d, *worklist, &entry); | |
3577 | } | |
3578 | } | |
3579 | ||
3580 | if (TYPE_TARGET_TYPE (type1) != NULL) | |
3581 | { | |
3582 | struct type_equality_entry entry; | |
3583 | ||
3584 | if (TYPE_TARGET_TYPE (type2) == NULL) | |
3585 | return 0; | |
3586 | ||
3587 | entry.type1 = TYPE_TARGET_TYPE (type1); | |
3588 | entry.type2 = TYPE_TARGET_TYPE (type2); | |
3589 | VEC_safe_push (type_equality_entry_d, *worklist, &entry); | |
3590 | } | |
3591 | else if (TYPE_TARGET_TYPE (type2) != NULL) | |
3592 | return 0; | |
3593 | ||
3594 | return 1; | |
3595 | } | |
3596 | ||
3597 | /* Check types on a worklist for equality. Returns zero if any pair | |
3598 | is not equal, non-zero if they are all considered equal. */ | |
3599 | ||
3600 | static int | |
3601 | check_types_worklist (VEC (type_equality_entry_d) **worklist, | |
3602 | struct bcache *cache) | |
3603 | { | |
3604 | while (!VEC_empty (type_equality_entry_d, *worklist)) | |
3605 | { | |
3606 | struct type_equality_entry entry; | |
3607 | int added; | |
3608 | ||
3609 | entry = *VEC_last (type_equality_entry_d, *worklist); | |
3610 | VEC_pop (type_equality_entry_d, *worklist); | |
3611 | ||
3612 | /* If the type pair has already been visited, we know it is | |
3613 | ok. */ | |
3614 | bcache_full (&entry, sizeof (entry), cache, &added); | |
3615 | if (!added) | |
3616 | continue; | |
3617 | ||
3618 | if (check_types_equal (entry.type1, entry.type2, worklist) == 0) | |
3619 | return 0; | |
3620 | } | |
7062b0a0 | 3621 | |
ca092b61 DE |
3622 | return 1; |
3623 | } | |
3624 | ||
3625 | /* Return non-zero if types TYPE1 and TYPE2 are equal, as determined by a | |
3626 | "deep comparison". Otherwise return zero. */ | |
3627 | ||
3628 | int | |
3629 | types_deeply_equal (struct type *type1, struct type *type2) | |
3630 | { | |
6c63c96a | 3631 | struct gdb_exception except = exception_none; |
ca092b61 DE |
3632 | int result = 0; |
3633 | struct bcache *cache; | |
3634 | VEC (type_equality_entry_d) *worklist = NULL; | |
3635 | struct type_equality_entry entry; | |
3636 | ||
3637 | gdb_assert (type1 != NULL && type2 != NULL); | |
3638 | ||
3639 | /* Early exit for the simple case. */ | |
3640 | if (type1 == type2) | |
3641 | return 1; | |
3642 | ||
3643 | cache = bcache_xmalloc (NULL, NULL); | |
3644 | ||
3645 | entry.type1 = type1; | |
3646 | entry.type2 = type2; | |
3647 | VEC_safe_push (type_equality_entry_d, worklist, &entry); | |
3648 | ||
6c63c96a PA |
3649 | /* check_types_worklist calls several nested helper functions, some |
3650 | of which can raise a GDB exception, so we just check and rethrow | |
3651 | here. If there is a GDB exception, a comparison is not capable | |
3652 | (or trusted), so exit. */ | |
492d29ea | 3653 | TRY |
ca092b61 DE |
3654 | { |
3655 | result = check_types_worklist (&worklist, cache); | |
3656 | } | |
6c63c96a | 3657 | CATCH (ex, RETURN_MASK_ALL) |
492d29ea | 3658 | { |
6c63c96a | 3659 | except = ex; |
492d29ea PA |
3660 | } |
3661 | END_CATCH | |
ca092b61 | 3662 | |
6c63c96a PA |
3663 | bcache_xfree (cache); |
3664 | VEC_free (type_equality_entry_d, worklist); | |
3665 | ||
3666 | /* Rethrow if there was a problem. */ | |
3667 | if (except.reason < 0) | |
3668 | throw_exception (except); | |
3669 | ||
ca092b61 DE |
3670 | return result; |
3671 | } | |
3f2f83dd KB |
3672 | |
3673 | /* Allocated status of type TYPE. Return zero if type TYPE is allocated. | |
3674 | Otherwise return one. */ | |
3675 | ||
3676 | int | |
3677 | type_not_allocated (const struct type *type) | |
3678 | { | |
3679 | struct dynamic_prop *prop = TYPE_ALLOCATED_PROP (type); | |
3680 | ||
3681 | return (prop && TYPE_DYN_PROP_KIND (prop) == PROP_CONST | |
3682 | && !TYPE_DYN_PROP_ADDR (prop)); | |
3683 | } | |
3684 | ||
3685 | /* Associated status of type TYPE. Return zero if type TYPE is associated. | |
3686 | Otherwise return one. */ | |
3687 | ||
3688 | int | |
3689 | type_not_associated (const struct type *type) | |
3690 | { | |
3691 | struct dynamic_prop *prop = TYPE_ASSOCIATED_PROP (type); | |
3692 | ||
3693 | return (prop && TYPE_DYN_PROP_KIND (prop) == PROP_CONST | |
3694 | && !TYPE_DYN_PROP_ADDR (prop)); | |
3695 | } | |
ca092b61 | 3696 | \f |
c906108c SS |
3697 | /* Compare one type (PARM) for compatibility with another (ARG). |
3698 | * PARM is intended to be the parameter type of a function; and | |
3699 | * ARG is the supplied argument's type. This function tests if | |
3700 | * the latter can be converted to the former. | |
da096638 | 3701 | * VALUE is the argument's value or NULL if none (or called recursively) |
c906108c SS |
3702 | * |
3703 | * Return 0 if they are identical types; | |
3704 | * Otherwise, return an integer which corresponds to how compatible | |
7ba81444 MS |
3705 | * PARM is to ARG. The higher the return value, the worse the match. |
3706 | * Generally the "bad" conversions are all uniformly assigned a 100. */ | |
c906108c | 3707 | |
6403aeea | 3708 | struct rank |
da096638 | 3709 | rank_one_type (struct type *parm, struct type *arg, struct value *value) |
c906108c | 3710 | { |
a9d5ef47 | 3711 | struct rank rank = {0,0}; |
7062b0a0 | 3712 | |
c906108c SS |
3713 | /* Resolve typedefs */ |
3714 | if (TYPE_CODE (parm) == TYPE_CODE_TYPEDEF) | |
3715 | parm = check_typedef (parm); | |
3716 | if (TYPE_CODE (arg) == TYPE_CODE_TYPEDEF) | |
3717 | arg = check_typedef (arg); | |
3718 | ||
e15c3eb4 | 3719 | if (TYPE_IS_REFERENCE (parm) && value != NULL) |
15c0a2a9 | 3720 | { |
e15c3eb4 KS |
3721 | if (VALUE_LVAL (value) == not_lval) |
3722 | { | |
3723 | /* Rvalues should preferably bind to rvalue references or const | |
3724 | lvalue references. */ | |
3725 | if (TYPE_CODE (parm) == TYPE_CODE_RVALUE_REF) | |
3726 | rank.subrank = REFERENCE_CONVERSION_RVALUE; | |
3727 | else if (TYPE_CONST (TYPE_TARGET_TYPE (parm))) | |
3728 | rank.subrank = REFERENCE_CONVERSION_CONST_LVALUE; | |
3729 | else | |
3730 | return INCOMPATIBLE_TYPE_BADNESS; | |
3731 | return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS); | |
3732 | } | |
3733 | else | |
3734 | { | |
3735 | /* Lvalues should prefer lvalue overloads. */ | |
3736 | if (TYPE_CODE (parm) == TYPE_CODE_RVALUE_REF) | |
3737 | { | |
3738 | rank.subrank = REFERENCE_CONVERSION_RVALUE; | |
3739 | return sum_ranks (rank, REFERENCE_CONVERSION_BADNESS); | |
3740 | } | |
3741 | } | |
15c0a2a9 AV |
3742 | } |
3743 | ||
3744 | if (types_equal (parm, arg)) | |
15c0a2a9 | 3745 | { |
e15c3eb4 KS |
3746 | struct type *t1 = parm; |
3747 | struct type *t2 = arg; | |
15c0a2a9 | 3748 | |
e15c3eb4 KS |
3749 | /* For pointers and references, compare target type. */ |
3750 | if (TYPE_CODE (parm) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (parm)) | |
3751 | { | |
3752 | t1 = TYPE_TARGET_TYPE (parm); | |
3753 | t2 = TYPE_TARGET_TYPE (arg); | |
3754 | } | |
15c0a2a9 | 3755 | |
e15c3eb4 KS |
3756 | /* Make sure they are CV equal, too. */ |
3757 | if (TYPE_CONST (t1) != TYPE_CONST (t2)) | |
3758 | rank.subrank |= CV_CONVERSION_CONST; | |
3759 | if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2)) | |
3760 | rank.subrank |= CV_CONVERSION_VOLATILE; | |
3761 | if (rank.subrank != 0) | |
3762 | return sum_ranks (CV_CONVERSION_BADNESS, rank); | |
3763 | return EXACT_MATCH_BADNESS; | |
15c0a2a9 AV |
3764 | } |
3765 | ||
db577aea | 3766 | /* See through references, since we can almost make non-references |
7ba81444 | 3767 | references. */ |
aa006118 AV |
3768 | |
3769 | if (TYPE_IS_REFERENCE (arg)) | |
da096638 | 3770 | return (sum_ranks (rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL), |
6403aeea | 3771 | REFERENCE_CONVERSION_BADNESS)); |
aa006118 | 3772 | if (TYPE_IS_REFERENCE (parm)) |
da096638 | 3773 | return (sum_ranks (rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL), |
6403aeea | 3774 | REFERENCE_CONVERSION_BADNESS)); |
5d161b24 | 3775 | if (overload_debug) |
7ba81444 MS |
3776 | /* Debugging only. */ |
3777 | fprintf_filtered (gdb_stderr, | |
3778 | "------ Arg is %s [%d], parm is %s [%d]\n", | |
3779 | TYPE_NAME (arg), TYPE_CODE (arg), | |
3780 | TYPE_NAME (parm), TYPE_CODE (parm)); | |
c906108c | 3781 | |
0963b4bd | 3782 | /* x -> y means arg of type x being supplied for parameter of type y. */ |
c906108c SS |
3783 | |
3784 | switch (TYPE_CODE (parm)) | |
3785 | { | |
c5aa993b JM |
3786 | case TYPE_CODE_PTR: |
3787 | switch (TYPE_CODE (arg)) | |
3788 | { | |
3789 | case TYPE_CODE_PTR: | |
7062b0a0 SW |
3790 | |
3791 | /* Allowed pointer conversions are: | |
3792 | (a) pointer to void-pointer conversion. */ | |
3793 | if (TYPE_CODE (TYPE_TARGET_TYPE (parm)) == TYPE_CODE_VOID) | |
c5aa993b | 3794 | return VOID_PTR_CONVERSION_BADNESS; |
7062b0a0 SW |
3795 | |
3796 | /* (b) pointer to ancestor-pointer conversion. */ | |
a9d5ef47 SW |
3797 | rank.subrank = distance_to_ancestor (TYPE_TARGET_TYPE (parm), |
3798 | TYPE_TARGET_TYPE (arg), | |
3799 | 0); | |
3800 | if (rank.subrank >= 0) | |
3801 | return sum_ranks (BASE_PTR_CONVERSION_BADNESS, rank); | |
7062b0a0 SW |
3802 | |
3803 | return INCOMPATIBLE_TYPE_BADNESS; | |
c5aa993b | 3804 | case TYPE_CODE_ARRAY: |
e15c3eb4 KS |
3805 | { |
3806 | struct type *t1 = TYPE_TARGET_TYPE (parm); | |
3807 | struct type *t2 = TYPE_TARGET_TYPE (arg); | |
3808 | ||
3809 | if (types_equal (t1, t2)) | |
3810 | { | |
3811 | /* Make sure they are CV equal. */ | |
3812 | if (TYPE_CONST (t1) != TYPE_CONST (t2)) | |
3813 | rank.subrank |= CV_CONVERSION_CONST; | |
3814 | if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2)) | |
3815 | rank.subrank |= CV_CONVERSION_VOLATILE; | |
3816 | if (rank.subrank != 0) | |
3817 | return sum_ranks (CV_CONVERSION_BADNESS, rank); | |
3818 | return EXACT_MATCH_BADNESS; | |
3819 | } | |
3820 | return INCOMPATIBLE_TYPE_BADNESS; | |
3821 | } | |
c5aa993b | 3822 | case TYPE_CODE_FUNC: |
da096638 | 3823 | return rank_one_type (TYPE_TARGET_TYPE (parm), arg, NULL); |
c5aa993b | 3824 | case TYPE_CODE_INT: |
a451cb65 | 3825 | if (value != NULL && TYPE_CODE (value_type (value)) == TYPE_CODE_INT) |
da096638 | 3826 | { |
a451cb65 KS |
3827 | if (value_as_long (value) == 0) |
3828 | { | |
3829 | /* Null pointer conversion: allow it to be cast to a pointer. | |
3830 | [4.10.1 of C++ standard draft n3290] */ | |
3831 | return NULL_POINTER_CONVERSION_BADNESS; | |
3832 | } | |
3833 | else | |
3834 | { | |
3835 | /* If type checking is disabled, allow the conversion. */ | |
3836 | if (!strict_type_checking) | |
3837 | return NS_INTEGER_POINTER_CONVERSION_BADNESS; | |
3838 | } | |
da096638 KS |
3839 | } |
3840 | /* fall through */ | |
c5aa993b | 3841 | case TYPE_CODE_ENUM: |
4f2aea11 | 3842 | case TYPE_CODE_FLAGS: |
c5aa993b JM |
3843 | case TYPE_CODE_CHAR: |
3844 | case TYPE_CODE_RANGE: | |
3845 | case TYPE_CODE_BOOL: | |
c5aa993b JM |
3846 | default: |
3847 | return INCOMPATIBLE_TYPE_BADNESS; | |
3848 | } | |
3849 | case TYPE_CODE_ARRAY: | |
3850 | switch (TYPE_CODE (arg)) | |
3851 | { | |
3852 | case TYPE_CODE_PTR: | |
3853 | case TYPE_CODE_ARRAY: | |
7ba81444 | 3854 | return rank_one_type (TYPE_TARGET_TYPE (parm), |
da096638 | 3855 | TYPE_TARGET_TYPE (arg), NULL); |
c5aa993b JM |
3856 | default: |
3857 | return INCOMPATIBLE_TYPE_BADNESS; | |
3858 | } | |
3859 | case TYPE_CODE_FUNC: | |
3860 | switch (TYPE_CODE (arg)) | |
3861 | { | |
3862 | case TYPE_CODE_PTR: /* funcptr -> func */ | |
da096638 | 3863 | return rank_one_type (parm, TYPE_TARGET_TYPE (arg), NULL); |
c5aa993b JM |
3864 | default: |
3865 | return INCOMPATIBLE_TYPE_BADNESS; | |
3866 | } | |
3867 | case TYPE_CODE_INT: | |
3868 | switch (TYPE_CODE (arg)) | |
3869 | { | |
3870 | case TYPE_CODE_INT: | |
3871 | if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm)) | |
3872 | { | |
3873 | /* Deal with signed, unsigned, and plain chars and | |
7ba81444 | 3874 | signed and unsigned ints. */ |
c5aa993b JM |
3875 | if (TYPE_NOSIGN (parm)) |
3876 | { | |
0963b4bd | 3877 | /* This case only for character types. */ |
7ba81444 | 3878 | if (TYPE_NOSIGN (arg)) |
6403aeea | 3879 | return EXACT_MATCH_BADNESS; /* plain char -> plain char */ |
7ba81444 MS |
3880 | else /* signed/unsigned char -> plain char */ |
3881 | return INTEGER_CONVERSION_BADNESS; | |
c5aa993b JM |
3882 | } |
3883 | else if (TYPE_UNSIGNED (parm)) | |
3884 | { | |
3885 | if (TYPE_UNSIGNED (arg)) | |
3886 | { | |
7ba81444 MS |
3887 | /* unsigned int -> unsigned int, or |
3888 | unsigned long -> unsigned long */ | |
3889 | if (integer_types_same_name_p (TYPE_NAME (parm), | |
3890 | TYPE_NAME (arg))) | |
6403aeea | 3891 | return EXACT_MATCH_BADNESS; |
7ba81444 MS |
3892 | else if (integer_types_same_name_p (TYPE_NAME (arg), |
3893 | "int") | |
3894 | && integer_types_same_name_p (TYPE_NAME (parm), | |
3895 | "long")) | |
3e43a32a MS |
3896 | /* unsigned int -> unsigned long */ |
3897 | return INTEGER_PROMOTION_BADNESS; | |
c5aa993b | 3898 | else |
3e43a32a MS |
3899 | /* unsigned long -> unsigned int */ |
3900 | return INTEGER_CONVERSION_BADNESS; | |
c5aa993b JM |
3901 | } |
3902 | else | |
3903 | { | |
7ba81444 MS |
3904 | if (integer_types_same_name_p (TYPE_NAME (arg), |
3905 | "long") | |
3906 | && integer_types_same_name_p (TYPE_NAME (parm), | |
3907 | "int")) | |
3e43a32a MS |
3908 | /* signed long -> unsigned int */ |
3909 | return INTEGER_CONVERSION_BADNESS; | |
c5aa993b | 3910 | else |
3e43a32a MS |
3911 | /* signed int/long -> unsigned int/long */ |
3912 | return INTEGER_CONVERSION_BADNESS; | |
c5aa993b JM |
3913 | } |
3914 | } | |
3915 | else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg)) | |
3916 | { | |
7ba81444 MS |
3917 | if (integer_types_same_name_p (TYPE_NAME (parm), |
3918 | TYPE_NAME (arg))) | |
6403aeea | 3919 | return EXACT_MATCH_BADNESS; |
7ba81444 MS |
3920 | else if (integer_types_same_name_p (TYPE_NAME (arg), |
3921 | "int") | |
3922 | && integer_types_same_name_p (TYPE_NAME (parm), | |
3923 | "long")) | |
c5aa993b JM |
3924 | return INTEGER_PROMOTION_BADNESS; |
3925 | else | |
1c5cb38e | 3926 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
3927 | } |
3928 | else | |
1c5cb38e | 3929 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
3930 | } |
3931 | else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm)) | |
3932 | return INTEGER_PROMOTION_BADNESS; | |
3933 | else | |
1c5cb38e | 3934 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b | 3935 | case TYPE_CODE_ENUM: |
4f2aea11 | 3936 | case TYPE_CODE_FLAGS: |
c5aa993b JM |
3937 | case TYPE_CODE_CHAR: |
3938 | case TYPE_CODE_RANGE: | |
3939 | case TYPE_CODE_BOOL: | |
3d567982 TT |
3940 | if (TYPE_DECLARED_CLASS (arg)) |
3941 | return INCOMPATIBLE_TYPE_BADNESS; | |
c5aa993b JM |
3942 | return INTEGER_PROMOTION_BADNESS; |
3943 | case TYPE_CODE_FLT: | |
3944 | return INT_FLOAT_CONVERSION_BADNESS; | |
3945 | case TYPE_CODE_PTR: | |
3946 | return NS_POINTER_CONVERSION_BADNESS; | |
3947 | default: | |
3948 | return INCOMPATIBLE_TYPE_BADNESS; | |
3949 | } | |
3950 | break; | |
3951 | case TYPE_CODE_ENUM: | |
3952 | switch (TYPE_CODE (arg)) | |
3953 | { | |
3954 | case TYPE_CODE_INT: | |
3955 | case TYPE_CODE_CHAR: | |
3956 | case TYPE_CODE_RANGE: | |
3957 | case TYPE_CODE_BOOL: | |
3958 | case TYPE_CODE_ENUM: | |
3d567982 TT |
3959 | if (TYPE_DECLARED_CLASS (parm) || TYPE_DECLARED_CLASS (arg)) |
3960 | return INCOMPATIBLE_TYPE_BADNESS; | |
1c5cb38e | 3961 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
3962 | case TYPE_CODE_FLT: |
3963 | return INT_FLOAT_CONVERSION_BADNESS; | |
3964 | default: | |
3965 | return INCOMPATIBLE_TYPE_BADNESS; | |
3966 | } | |
3967 | break; | |
3968 | case TYPE_CODE_CHAR: | |
3969 | switch (TYPE_CODE (arg)) | |
3970 | { | |
3971 | case TYPE_CODE_RANGE: | |
3972 | case TYPE_CODE_BOOL: | |
3973 | case TYPE_CODE_ENUM: | |
3d567982 TT |
3974 | if (TYPE_DECLARED_CLASS (arg)) |
3975 | return INCOMPATIBLE_TYPE_BADNESS; | |
1c5cb38e | 3976 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
3977 | case TYPE_CODE_FLT: |
3978 | return INT_FLOAT_CONVERSION_BADNESS; | |
3979 | case TYPE_CODE_INT: | |
3980 | if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm)) | |
1c5cb38e | 3981 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
3982 | else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm)) |
3983 | return INTEGER_PROMOTION_BADNESS; | |
3984 | /* >>> !! else fall through !! <<< */ | |
3985 | case TYPE_CODE_CHAR: | |
7ba81444 MS |
3986 | /* Deal with signed, unsigned, and plain chars for C++ and |
3987 | with int cases falling through from previous case. */ | |
c5aa993b JM |
3988 | if (TYPE_NOSIGN (parm)) |
3989 | { | |
3990 | if (TYPE_NOSIGN (arg)) | |
6403aeea | 3991 | return EXACT_MATCH_BADNESS; |
c5aa993b | 3992 | else |
1c5cb38e | 3993 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
3994 | } |
3995 | else if (TYPE_UNSIGNED (parm)) | |
3996 | { | |
3997 | if (TYPE_UNSIGNED (arg)) | |
6403aeea | 3998 | return EXACT_MATCH_BADNESS; |
c5aa993b JM |
3999 | else |
4000 | return INTEGER_PROMOTION_BADNESS; | |
4001 | } | |
4002 | else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg)) | |
6403aeea | 4003 | return EXACT_MATCH_BADNESS; |
c5aa993b | 4004 | else |
1c5cb38e | 4005 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
4006 | default: |
4007 | return INCOMPATIBLE_TYPE_BADNESS; | |
4008 | } | |
4009 | break; | |
4010 | case TYPE_CODE_RANGE: | |
4011 | switch (TYPE_CODE (arg)) | |
4012 | { | |
4013 | case TYPE_CODE_INT: | |
4014 | case TYPE_CODE_CHAR: | |
4015 | case TYPE_CODE_RANGE: | |
4016 | case TYPE_CODE_BOOL: | |
4017 | case TYPE_CODE_ENUM: | |
1c5cb38e | 4018 | return INTEGER_CONVERSION_BADNESS; |
c5aa993b JM |
4019 | case TYPE_CODE_FLT: |
4020 | return INT_FLOAT_CONVERSION_BADNESS; | |
4021 | default: | |
4022 | return INCOMPATIBLE_TYPE_BADNESS; | |
4023 | } | |
4024 | break; | |
4025 | case TYPE_CODE_BOOL: | |
4026 | switch (TYPE_CODE (arg)) | |
4027 | { | |
5b4f6e25 KS |
4028 | /* n3290 draft, section 4.12.1 (conv.bool): |
4029 | ||
4030 | "A prvalue of arithmetic, unscoped enumeration, pointer, or | |
4031 | pointer to member type can be converted to a prvalue of type | |
4032 | bool. A zero value, null pointer value, or null member pointer | |
4033 | value is converted to false; any other value is converted to | |
4034 | true. A prvalue of type std::nullptr_t can be converted to a | |
4035 | prvalue of type bool; the resulting value is false." */ | |
c5aa993b JM |
4036 | case TYPE_CODE_INT: |
4037 | case TYPE_CODE_CHAR: | |
c5aa993b JM |
4038 | case TYPE_CODE_ENUM: |
4039 | case TYPE_CODE_FLT: | |
5b4f6e25 | 4040 | case TYPE_CODE_MEMBERPTR: |
c5aa993b | 4041 | case TYPE_CODE_PTR: |
5b4f6e25 KS |
4042 | return BOOL_CONVERSION_BADNESS; |
4043 | case TYPE_CODE_RANGE: | |
4044 | return INCOMPATIBLE_TYPE_BADNESS; | |
c5aa993b | 4045 | case TYPE_CODE_BOOL: |
6403aeea | 4046 | return EXACT_MATCH_BADNESS; |
c5aa993b JM |
4047 | default: |
4048 | return INCOMPATIBLE_TYPE_BADNESS; | |
4049 | } | |
4050 | break; | |
4051 | case TYPE_CODE_FLT: | |
4052 | switch (TYPE_CODE (arg)) | |
4053 | { | |
4054 | case TYPE_CODE_FLT: | |
4055 | if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm)) | |
4056 | return FLOAT_PROMOTION_BADNESS; | |
4057 | else if (TYPE_LENGTH (arg) == TYPE_LENGTH (parm)) | |
6403aeea | 4058 | return EXACT_MATCH_BADNESS; |
c5aa993b JM |
4059 | else |
4060 | return FLOAT_CONVERSION_BADNESS; | |
4061 | case TYPE_CODE_INT: | |
4062 | case TYPE_CODE_BOOL: | |
4063 | case TYPE_CODE_ENUM: | |
4064 | case TYPE_CODE_RANGE: | |
4065 | case TYPE_CODE_CHAR: | |
4066 | return INT_FLOAT_CONVERSION_BADNESS; | |
4067 | default: | |
4068 | return INCOMPATIBLE_TYPE_BADNESS; | |
4069 | } | |
4070 | break; | |
4071 | case TYPE_CODE_COMPLEX: | |
4072 | switch (TYPE_CODE (arg)) | |
7ba81444 | 4073 | { /* Strictly not needed for C++, but... */ |
c5aa993b JM |
4074 | case TYPE_CODE_FLT: |
4075 | return FLOAT_PROMOTION_BADNESS; | |
4076 | case TYPE_CODE_COMPLEX: | |
6403aeea | 4077 | return EXACT_MATCH_BADNESS; |
c5aa993b JM |
4078 | default: |
4079 | return INCOMPATIBLE_TYPE_BADNESS; | |
4080 | } | |
4081 | break; | |
4082 | case TYPE_CODE_STRUCT: | |
c5aa993b JM |
4083 | switch (TYPE_CODE (arg)) |
4084 | { | |
4085 | case TYPE_CODE_STRUCT: | |
4086 | /* Check for derivation */ | |
a9d5ef47 SW |
4087 | rank.subrank = distance_to_ancestor (parm, arg, 0); |
4088 | if (rank.subrank >= 0) | |
4089 | return sum_ranks (BASE_CONVERSION_BADNESS, rank); | |
c5aa993b JM |
4090 | /* else fall through */ |
4091 | default: | |
4092 | return INCOMPATIBLE_TYPE_BADNESS; | |
4093 | } | |
4094 | break; | |
4095 | case TYPE_CODE_UNION: | |
4096 | switch (TYPE_CODE (arg)) | |
4097 | { | |
4098 | case TYPE_CODE_UNION: | |
4099 | default: | |
4100 | return INCOMPATIBLE_TYPE_BADNESS; | |
4101 | } | |
4102 | break; | |
0d5de010 | 4103 | case TYPE_CODE_MEMBERPTR: |
c5aa993b JM |
4104 | switch (TYPE_CODE (arg)) |
4105 | { | |
4106 | default: | |
4107 | return INCOMPATIBLE_TYPE_BADNESS; | |
4108 | } | |
4109 | break; | |
4110 | case TYPE_CODE_METHOD: | |
4111 | switch (TYPE_CODE (arg)) | |
4112 | { | |
4113 | ||
4114 | default: | |
4115 | return INCOMPATIBLE_TYPE_BADNESS; | |
4116 | } | |
4117 | break; | |
4118 | case TYPE_CODE_REF: | |
4119 | switch (TYPE_CODE (arg)) | |
4120 | { | |
4121 | ||
4122 | default: | |
4123 | return INCOMPATIBLE_TYPE_BADNESS; | |
4124 | } | |
4125 | ||
4126 | break; | |
4127 | case TYPE_CODE_SET: | |
4128 | switch (TYPE_CODE (arg)) | |
4129 | { | |
4130 | /* Not in C++ */ | |
4131 | case TYPE_CODE_SET: | |
7ba81444 | 4132 | return rank_one_type (TYPE_FIELD_TYPE (parm, 0), |
da096638 | 4133 | TYPE_FIELD_TYPE (arg, 0), NULL); |
c5aa993b JM |
4134 | default: |
4135 | return INCOMPATIBLE_TYPE_BADNESS; | |
4136 | } | |
4137 | break; | |
4138 | case TYPE_CODE_VOID: | |
4139 | default: | |
4140 | return INCOMPATIBLE_TYPE_BADNESS; | |
4141 | } /* switch (TYPE_CODE (arg)) */ | |
c906108c SS |
4142 | } |
4143 | ||
0963b4bd | 4144 | /* End of functions for overload resolution. */ |
5212577a DE |
4145 | \f |
4146 | /* Routines to pretty-print types. */ | |
c906108c | 4147 | |
c906108c | 4148 | static void |
fba45db2 | 4149 | print_bit_vector (B_TYPE *bits, int nbits) |
c906108c SS |
4150 | { |
4151 | int bitno; | |
4152 | ||
4153 | for (bitno = 0; bitno < nbits; bitno++) | |
4154 | { | |
4155 | if ((bitno % 8) == 0) | |
4156 | { | |
4157 | puts_filtered (" "); | |
4158 | } | |
4159 | if (B_TST (bits, bitno)) | |
a3f17187 | 4160 | printf_filtered (("1")); |
c906108c | 4161 | else |
a3f17187 | 4162 | printf_filtered (("0")); |
c906108c SS |
4163 | } |
4164 | } | |
4165 | ||
ad2f7632 | 4166 | /* Note the first arg should be the "this" pointer, we may not want to |
7ba81444 MS |
4167 | include it since we may get into a infinitely recursive |
4168 | situation. */ | |
c906108c SS |
4169 | |
4170 | static void | |
4c9e8482 | 4171 | print_args (struct field *args, int nargs, int spaces) |
c906108c SS |
4172 | { |
4173 | if (args != NULL) | |
4174 | { | |
ad2f7632 DJ |
4175 | int i; |
4176 | ||
4177 | for (i = 0; i < nargs; i++) | |
4c9e8482 DE |
4178 | { |
4179 | printfi_filtered (spaces, "[%d] name '%s'\n", i, | |
4180 | args[i].name != NULL ? args[i].name : "<NULL>"); | |
4181 | recursive_dump_type (args[i].type, spaces + 2); | |
4182 | } | |
c906108c SS |
4183 | } |
4184 | } | |
4185 | ||
d6a843b5 JK |
4186 | int |
4187 | field_is_static (struct field *f) | |
4188 | { | |
4189 | /* "static" fields are the fields whose location is not relative | |
4190 | to the address of the enclosing struct. It would be nice to | |
4191 | have a dedicated flag that would be set for static fields when | |
4192 | the type is being created. But in practice, checking the field | |
254e6b9e | 4193 | loc_kind should give us an accurate answer. */ |
d6a843b5 JK |
4194 | return (FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSNAME |
4195 | || FIELD_LOC_KIND (*f) == FIELD_LOC_KIND_PHYSADDR); | |
4196 | } | |
4197 | ||
c906108c | 4198 | static void |
fba45db2 | 4199 | dump_fn_fieldlists (struct type *type, int spaces) |
c906108c SS |
4200 | { |
4201 | int method_idx; | |
4202 | int overload_idx; | |
4203 | struct fn_field *f; | |
4204 | ||
4205 | printfi_filtered (spaces, "fn_fieldlists "); | |
d4f3574e | 4206 | gdb_print_host_address (TYPE_FN_FIELDLISTS (type), gdb_stdout); |
c906108c SS |
4207 | printf_filtered ("\n"); |
4208 | for (method_idx = 0; method_idx < TYPE_NFN_FIELDS (type); method_idx++) | |
4209 | { | |
4210 | f = TYPE_FN_FIELDLIST1 (type, method_idx); | |
4211 | printfi_filtered (spaces + 2, "[%d] name '%s' (", | |
4212 | method_idx, | |
4213 | TYPE_FN_FIELDLIST_NAME (type, method_idx)); | |
d4f3574e SS |
4214 | gdb_print_host_address (TYPE_FN_FIELDLIST_NAME (type, method_idx), |
4215 | gdb_stdout); | |
a3f17187 | 4216 | printf_filtered (_(") length %d\n"), |
c906108c SS |
4217 | TYPE_FN_FIELDLIST_LENGTH (type, method_idx)); |
4218 | for (overload_idx = 0; | |
4219 | overload_idx < TYPE_FN_FIELDLIST_LENGTH (type, method_idx); | |
4220 | overload_idx++) | |
4221 | { | |
4222 | printfi_filtered (spaces + 4, "[%d] physname '%s' (", | |
4223 | overload_idx, | |
4224 | TYPE_FN_FIELD_PHYSNAME (f, overload_idx)); | |
d4f3574e SS |
4225 | gdb_print_host_address (TYPE_FN_FIELD_PHYSNAME (f, overload_idx), |
4226 | gdb_stdout); | |
c906108c SS |
4227 | printf_filtered (")\n"); |
4228 | printfi_filtered (spaces + 8, "type "); | |
7ba81444 MS |
4229 | gdb_print_host_address (TYPE_FN_FIELD_TYPE (f, overload_idx), |
4230 | gdb_stdout); | |
c906108c SS |
4231 | printf_filtered ("\n"); |
4232 | ||
4233 | recursive_dump_type (TYPE_FN_FIELD_TYPE (f, overload_idx), | |
4234 | spaces + 8 + 2); | |
4235 | ||
4236 | printfi_filtered (spaces + 8, "args "); | |
7ba81444 MS |
4237 | gdb_print_host_address (TYPE_FN_FIELD_ARGS (f, overload_idx), |
4238 | gdb_stdout); | |
c906108c | 4239 | printf_filtered ("\n"); |
4c9e8482 DE |
4240 | print_args (TYPE_FN_FIELD_ARGS (f, overload_idx), |
4241 | TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, overload_idx)), | |
4242 | spaces + 8 + 2); | |
c906108c | 4243 | printfi_filtered (spaces + 8, "fcontext "); |
d4f3574e SS |
4244 | gdb_print_host_address (TYPE_FN_FIELD_FCONTEXT (f, overload_idx), |
4245 | gdb_stdout); | |
c906108c SS |
4246 | printf_filtered ("\n"); |
4247 | ||
4248 | printfi_filtered (spaces + 8, "is_const %d\n", | |
4249 | TYPE_FN_FIELD_CONST (f, overload_idx)); | |
4250 | printfi_filtered (spaces + 8, "is_volatile %d\n", | |
4251 | TYPE_FN_FIELD_VOLATILE (f, overload_idx)); | |
4252 | printfi_filtered (spaces + 8, "is_private %d\n", | |
4253 | TYPE_FN_FIELD_PRIVATE (f, overload_idx)); | |
4254 | printfi_filtered (spaces + 8, "is_protected %d\n", | |
4255 | TYPE_FN_FIELD_PROTECTED (f, overload_idx)); | |
4256 | printfi_filtered (spaces + 8, "is_stub %d\n", | |
4257 | TYPE_FN_FIELD_STUB (f, overload_idx)); | |
4258 | printfi_filtered (spaces + 8, "voffset %u\n", | |
4259 | TYPE_FN_FIELD_VOFFSET (f, overload_idx)); | |
4260 | } | |
4261 | } | |
4262 | } | |
4263 | ||
4264 | static void | |
fba45db2 | 4265 | print_cplus_stuff (struct type *type, int spaces) |
c906108c | 4266 | { |
ae6ae975 DE |
4267 | printfi_filtered (spaces, "vptr_fieldno %d\n", TYPE_VPTR_FIELDNO (type)); |
4268 | printfi_filtered (spaces, "vptr_basetype "); | |
4269 | gdb_print_host_address (TYPE_VPTR_BASETYPE (type), gdb_stdout); | |
4270 | puts_filtered ("\n"); | |
4271 | if (TYPE_VPTR_BASETYPE (type) != NULL) | |
4272 | recursive_dump_type (TYPE_VPTR_BASETYPE (type), spaces + 2); | |
4273 | ||
c906108c SS |
4274 | printfi_filtered (spaces, "n_baseclasses %d\n", |
4275 | TYPE_N_BASECLASSES (type)); | |
4276 | printfi_filtered (spaces, "nfn_fields %d\n", | |
4277 | TYPE_NFN_FIELDS (type)); | |
c906108c SS |
4278 | if (TYPE_N_BASECLASSES (type) > 0) |
4279 | { | |
4280 | printfi_filtered (spaces, "virtual_field_bits (%d bits at *", | |
4281 | TYPE_N_BASECLASSES (type)); | |
7ba81444 MS |
4282 | gdb_print_host_address (TYPE_FIELD_VIRTUAL_BITS (type), |
4283 | gdb_stdout); | |
c906108c SS |
4284 | printf_filtered (")"); |
4285 | ||
4286 | print_bit_vector (TYPE_FIELD_VIRTUAL_BITS (type), | |
4287 | TYPE_N_BASECLASSES (type)); | |
4288 | puts_filtered ("\n"); | |
4289 | } | |
4290 | if (TYPE_NFIELDS (type) > 0) | |
4291 | { | |
4292 | if (TYPE_FIELD_PRIVATE_BITS (type) != NULL) | |
4293 | { | |
7ba81444 MS |
4294 | printfi_filtered (spaces, |
4295 | "private_field_bits (%d bits at *", | |
c906108c | 4296 | TYPE_NFIELDS (type)); |
7ba81444 MS |
4297 | gdb_print_host_address (TYPE_FIELD_PRIVATE_BITS (type), |
4298 | gdb_stdout); | |
c906108c SS |
4299 | printf_filtered (")"); |
4300 | print_bit_vector (TYPE_FIELD_PRIVATE_BITS (type), | |
4301 | TYPE_NFIELDS (type)); | |
4302 | puts_filtered ("\n"); | |
4303 | } | |
4304 | if (TYPE_FIELD_PROTECTED_BITS (type) != NULL) | |
4305 | { | |
7ba81444 MS |
4306 | printfi_filtered (spaces, |
4307 | "protected_field_bits (%d bits at *", | |
c906108c | 4308 | TYPE_NFIELDS (type)); |
7ba81444 MS |
4309 | gdb_print_host_address (TYPE_FIELD_PROTECTED_BITS (type), |
4310 | gdb_stdout); | |
c906108c SS |
4311 | printf_filtered (")"); |
4312 | print_bit_vector (TYPE_FIELD_PROTECTED_BITS (type), | |
4313 | TYPE_NFIELDS (type)); | |
4314 | puts_filtered ("\n"); | |
4315 | } | |
4316 | } | |
4317 | if (TYPE_NFN_FIELDS (type) > 0) | |
4318 | { | |
4319 | dump_fn_fieldlists (type, spaces); | |
4320 | } | |
4321 | } | |
4322 | ||
b4ba55a1 JB |
4323 | /* Print the contents of the TYPE's type_specific union, assuming that |
4324 | its type-specific kind is TYPE_SPECIFIC_GNAT_STUFF. */ | |
4325 | ||
4326 | static void | |
4327 | print_gnat_stuff (struct type *type, int spaces) | |
4328 | { | |
4329 | struct type *descriptive_type = TYPE_DESCRIPTIVE_TYPE (type); | |
4330 | ||
8cd00c59 PMR |
4331 | if (descriptive_type == NULL) |
4332 | printfi_filtered (spaces + 2, "no descriptive type\n"); | |
4333 | else | |
4334 | { | |
4335 | printfi_filtered (spaces + 2, "descriptive type\n"); | |
4336 | recursive_dump_type (descriptive_type, spaces + 4); | |
4337 | } | |
b4ba55a1 JB |
4338 | } |
4339 | ||
c906108c SS |
4340 | static struct obstack dont_print_type_obstack; |
4341 | ||
4342 | void | |
fba45db2 | 4343 | recursive_dump_type (struct type *type, int spaces) |
c906108c SS |
4344 | { |
4345 | int idx; | |
4346 | ||
4347 | if (spaces == 0) | |
4348 | obstack_begin (&dont_print_type_obstack, 0); | |
4349 | ||
4350 | if (TYPE_NFIELDS (type) > 0 | |
b4ba55a1 | 4351 | || (HAVE_CPLUS_STRUCT (type) && TYPE_NFN_FIELDS (type) > 0)) |
c906108c SS |
4352 | { |
4353 | struct type **first_dont_print | |
7ba81444 | 4354 | = (struct type **) obstack_base (&dont_print_type_obstack); |
c906108c | 4355 | |
7ba81444 MS |
4356 | int i = (struct type **) |
4357 | obstack_next_free (&dont_print_type_obstack) - first_dont_print; | |
c906108c SS |
4358 | |
4359 | while (--i >= 0) | |
4360 | { | |
4361 | if (type == first_dont_print[i]) | |
4362 | { | |
4363 | printfi_filtered (spaces, "type node "); | |
d4f3574e | 4364 | gdb_print_host_address (type, gdb_stdout); |
a3f17187 | 4365 | printf_filtered (_(" <same as already seen type>\n")); |
c906108c SS |
4366 | return; |
4367 | } | |
4368 | } | |
4369 | ||
4370 | obstack_ptr_grow (&dont_print_type_obstack, type); | |
4371 | } | |
4372 | ||
4373 | printfi_filtered (spaces, "type node "); | |
d4f3574e | 4374 | gdb_print_host_address (type, gdb_stdout); |
c906108c SS |
4375 | printf_filtered ("\n"); |
4376 | printfi_filtered (spaces, "name '%s' (", | |
4377 | TYPE_NAME (type) ? TYPE_NAME (type) : "<NULL>"); | |
d4f3574e | 4378 | gdb_print_host_address (TYPE_NAME (type), gdb_stdout); |
c906108c | 4379 | printf_filtered (")\n"); |
e9e79dd9 FF |
4380 | printfi_filtered (spaces, "tagname '%s' (", |
4381 | TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "<NULL>"); | |
4382 | gdb_print_host_address (TYPE_TAG_NAME (type), gdb_stdout); | |
4383 | printf_filtered (")\n"); | |
c906108c SS |
4384 | printfi_filtered (spaces, "code 0x%x ", TYPE_CODE (type)); |
4385 | switch (TYPE_CODE (type)) | |
4386 | { | |
c5aa993b JM |
4387 | case TYPE_CODE_UNDEF: |
4388 | printf_filtered ("(TYPE_CODE_UNDEF)"); | |
4389 | break; | |
4390 | case TYPE_CODE_PTR: | |
4391 | printf_filtered ("(TYPE_CODE_PTR)"); | |
4392 | break; | |
4393 | case TYPE_CODE_ARRAY: | |
4394 | printf_filtered ("(TYPE_CODE_ARRAY)"); | |
4395 | break; | |
4396 | case TYPE_CODE_STRUCT: | |
4397 | printf_filtered ("(TYPE_CODE_STRUCT)"); | |
4398 | break; | |
4399 | case TYPE_CODE_UNION: | |
4400 | printf_filtered ("(TYPE_CODE_UNION)"); | |
4401 | break; | |
4402 | case TYPE_CODE_ENUM: | |
4403 | printf_filtered ("(TYPE_CODE_ENUM)"); | |
4404 | break; | |
4f2aea11 MK |
4405 | case TYPE_CODE_FLAGS: |
4406 | printf_filtered ("(TYPE_CODE_FLAGS)"); | |
4407 | break; | |
c5aa993b JM |
4408 | case TYPE_CODE_FUNC: |
4409 | printf_filtered ("(TYPE_CODE_FUNC)"); | |
4410 | break; | |
4411 | case TYPE_CODE_INT: | |
4412 | printf_filtered ("(TYPE_CODE_INT)"); | |
4413 | break; | |
4414 | case TYPE_CODE_FLT: | |
4415 | printf_filtered ("(TYPE_CODE_FLT)"); | |
4416 | break; | |
4417 | case TYPE_CODE_VOID: | |
4418 | printf_filtered ("(TYPE_CODE_VOID)"); | |
4419 | break; | |
4420 | case TYPE_CODE_SET: | |
4421 | printf_filtered ("(TYPE_CODE_SET)"); | |
4422 | break; | |
4423 | case TYPE_CODE_RANGE: | |
4424 | printf_filtered ("(TYPE_CODE_RANGE)"); | |
4425 | break; | |
4426 | case TYPE_CODE_STRING: | |
4427 | printf_filtered ("(TYPE_CODE_STRING)"); | |
4428 | break; | |
4429 | case TYPE_CODE_ERROR: | |
4430 | printf_filtered ("(TYPE_CODE_ERROR)"); | |
4431 | break; | |
0d5de010 DJ |
4432 | case TYPE_CODE_MEMBERPTR: |
4433 | printf_filtered ("(TYPE_CODE_MEMBERPTR)"); | |
4434 | break; | |
4435 | case TYPE_CODE_METHODPTR: | |
4436 | printf_filtered ("(TYPE_CODE_METHODPTR)"); | |
c5aa993b JM |
4437 | break; |
4438 | case TYPE_CODE_METHOD: | |
4439 | printf_filtered ("(TYPE_CODE_METHOD)"); | |
4440 | break; | |
4441 | case TYPE_CODE_REF: | |
4442 | printf_filtered ("(TYPE_CODE_REF)"); | |
4443 | break; | |
4444 | case TYPE_CODE_CHAR: | |
4445 | printf_filtered ("(TYPE_CODE_CHAR)"); | |
4446 | break; | |
4447 | case TYPE_CODE_BOOL: | |
4448 | printf_filtered ("(TYPE_CODE_BOOL)"); | |
4449 | break; | |
e9e79dd9 FF |
4450 | case TYPE_CODE_COMPLEX: |
4451 | printf_filtered ("(TYPE_CODE_COMPLEX)"); | |
4452 | break; | |
c5aa993b JM |
4453 | case TYPE_CODE_TYPEDEF: |
4454 | printf_filtered ("(TYPE_CODE_TYPEDEF)"); | |
4455 | break; | |
5c4e30ca DC |
4456 | case TYPE_CODE_NAMESPACE: |
4457 | printf_filtered ("(TYPE_CODE_NAMESPACE)"); | |
4458 | break; | |
c5aa993b JM |
4459 | default: |
4460 | printf_filtered ("(UNKNOWN TYPE CODE)"); | |
4461 | break; | |
c906108c SS |
4462 | } |
4463 | puts_filtered ("\n"); | |
4464 | printfi_filtered (spaces, "length %d\n", TYPE_LENGTH (type)); | |
e9bb382b UW |
4465 | if (TYPE_OBJFILE_OWNED (type)) |
4466 | { | |
4467 | printfi_filtered (spaces, "objfile "); | |
4468 | gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout); | |
4469 | } | |
4470 | else | |
4471 | { | |
4472 | printfi_filtered (spaces, "gdbarch "); | |
4473 | gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout); | |
4474 | } | |
c906108c SS |
4475 | printf_filtered ("\n"); |
4476 | printfi_filtered (spaces, "target_type "); | |
d4f3574e | 4477 | gdb_print_host_address (TYPE_TARGET_TYPE (type), gdb_stdout); |
c906108c SS |
4478 | printf_filtered ("\n"); |
4479 | if (TYPE_TARGET_TYPE (type) != NULL) | |
4480 | { | |
4481 | recursive_dump_type (TYPE_TARGET_TYPE (type), spaces + 2); | |
4482 | } | |
4483 | printfi_filtered (spaces, "pointer_type "); | |
d4f3574e | 4484 | gdb_print_host_address (TYPE_POINTER_TYPE (type), gdb_stdout); |
c906108c SS |
4485 | printf_filtered ("\n"); |
4486 | printfi_filtered (spaces, "reference_type "); | |
d4f3574e | 4487 | gdb_print_host_address (TYPE_REFERENCE_TYPE (type), gdb_stdout); |
c906108c | 4488 | printf_filtered ("\n"); |
2fdde8f8 DJ |
4489 | printfi_filtered (spaces, "type_chain "); |
4490 | gdb_print_host_address (TYPE_CHAIN (type), gdb_stdout); | |
e9e79dd9 | 4491 | printf_filtered ("\n"); |
7ba81444 MS |
4492 | printfi_filtered (spaces, "instance_flags 0x%x", |
4493 | TYPE_INSTANCE_FLAGS (type)); | |
2fdde8f8 DJ |
4494 | if (TYPE_CONST (type)) |
4495 | { | |
a9ff5f12 | 4496 | puts_filtered (" TYPE_CONST"); |
2fdde8f8 DJ |
4497 | } |
4498 | if (TYPE_VOLATILE (type)) | |
4499 | { | |
a9ff5f12 | 4500 | puts_filtered (" TYPE_VOLATILE"); |
2fdde8f8 DJ |
4501 | } |
4502 | if (TYPE_CODE_SPACE (type)) | |
4503 | { | |
a9ff5f12 | 4504 | puts_filtered (" TYPE_CODE_SPACE"); |
2fdde8f8 DJ |
4505 | } |
4506 | if (TYPE_DATA_SPACE (type)) | |
4507 | { | |
a9ff5f12 | 4508 | puts_filtered (" TYPE_DATA_SPACE"); |
2fdde8f8 | 4509 | } |
8b2dbe47 KB |
4510 | if (TYPE_ADDRESS_CLASS_1 (type)) |
4511 | { | |
a9ff5f12 | 4512 | puts_filtered (" TYPE_ADDRESS_CLASS_1"); |
8b2dbe47 KB |
4513 | } |
4514 | if (TYPE_ADDRESS_CLASS_2 (type)) | |
4515 | { | |
a9ff5f12 | 4516 | puts_filtered (" TYPE_ADDRESS_CLASS_2"); |
8b2dbe47 | 4517 | } |
06d66ee9 TT |
4518 | if (TYPE_RESTRICT (type)) |
4519 | { | |
a9ff5f12 | 4520 | puts_filtered (" TYPE_RESTRICT"); |
06d66ee9 | 4521 | } |
a2c2acaf MW |
4522 | if (TYPE_ATOMIC (type)) |
4523 | { | |
a9ff5f12 | 4524 | puts_filtered (" TYPE_ATOMIC"); |
a2c2acaf | 4525 | } |
2fdde8f8 | 4526 | puts_filtered ("\n"); |
876cecd0 TT |
4527 | |
4528 | printfi_filtered (spaces, "flags"); | |
762a036f | 4529 | if (TYPE_UNSIGNED (type)) |
c906108c | 4530 | { |
a9ff5f12 | 4531 | puts_filtered (" TYPE_UNSIGNED"); |
c906108c | 4532 | } |
762a036f FF |
4533 | if (TYPE_NOSIGN (type)) |
4534 | { | |
a9ff5f12 | 4535 | puts_filtered (" TYPE_NOSIGN"); |
762a036f FF |
4536 | } |
4537 | if (TYPE_STUB (type)) | |
c906108c | 4538 | { |
a9ff5f12 | 4539 | puts_filtered (" TYPE_STUB"); |
c906108c | 4540 | } |
762a036f FF |
4541 | if (TYPE_TARGET_STUB (type)) |
4542 | { | |
a9ff5f12 | 4543 | puts_filtered (" TYPE_TARGET_STUB"); |
762a036f | 4544 | } |
762a036f FF |
4545 | if (TYPE_PROTOTYPED (type)) |
4546 | { | |
a9ff5f12 | 4547 | puts_filtered (" TYPE_PROTOTYPED"); |
762a036f FF |
4548 | } |
4549 | if (TYPE_INCOMPLETE (type)) | |
4550 | { | |
a9ff5f12 | 4551 | puts_filtered (" TYPE_INCOMPLETE"); |
762a036f | 4552 | } |
762a036f FF |
4553 | if (TYPE_VARARGS (type)) |
4554 | { | |
a9ff5f12 | 4555 | puts_filtered (" TYPE_VARARGS"); |
762a036f | 4556 | } |
f5f8a009 EZ |
4557 | /* This is used for things like AltiVec registers on ppc. Gcc emits |
4558 | an attribute for the array type, which tells whether or not we | |
4559 | have a vector, instead of a regular array. */ | |
4560 | if (TYPE_VECTOR (type)) | |
4561 | { | |
a9ff5f12 | 4562 | puts_filtered (" TYPE_VECTOR"); |
f5f8a009 | 4563 | } |
876cecd0 TT |
4564 | if (TYPE_FIXED_INSTANCE (type)) |
4565 | { | |
4566 | puts_filtered (" TYPE_FIXED_INSTANCE"); | |
4567 | } | |
4568 | if (TYPE_STUB_SUPPORTED (type)) | |
4569 | { | |
4570 | puts_filtered (" TYPE_STUB_SUPPORTED"); | |
4571 | } | |
4572 | if (TYPE_NOTTEXT (type)) | |
4573 | { | |
4574 | puts_filtered (" TYPE_NOTTEXT"); | |
4575 | } | |
c906108c SS |
4576 | puts_filtered ("\n"); |
4577 | printfi_filtered (spaces, "nfields %d ", TYPE_NFIELDS (type)); | |
d4f3574e | 4578 | gdb_print_host_address (TYPE_FIELDS (type), gdb_stdout); |
c906108c SS |
4579 | puts_filtered ("\n"); |
4580 | for (idx = 0; idx < TYPE_NFIELDS (type); idx++) | |
4581 | { | |
14e75d8e JK |
4582 | if (TYPE_CODE (type) == TYPE_CODE_ENUM) |
4583 | printfi_filtered (spaces + 2, | |
4584 | "[%d] enumval %s type ", | |
4585 | idx, plongest (TYPE_FIELD_ENUMVAL (type, idx))); | |
4586 | else | |
4587 | printfi_filtered (spaces + 2, | |
6b850546 DT |
4588 | "[%d] bitpos %s bitsize %d type ", |
4589 | idx, plongest (TYPE_FIELD_BITPOS (type, idx)), | |
14e75d8e | 4590 | TYPE_FIELD_BITSIZE (type, idx)); |
d4f3574e | 4591 | gdb_print_host_address (TYPE_FIELD_TYPE (type, idx), gdb_stdout); |
c906108c SS |
4592 | printf_filtered (" name '%s' (", |
4593 | TYPE_FIELD_NAME (type, idx) != NULL | |
4594 | ? TYPE_FIELD_NAME (type, idx) | |
4595 | : "<NULL>"); | |
d4f3574e | 4596 | gdb_print_host_address (TYPE_FIELD_NAME (type, idx), gdb_stdout); |
c906108c SS |
4597 | printf_filtered (")\n"); |
4598 | if (TYPE_FIELD_TYPE (type, idx) != NULL) | |
4599 | { | |
4600 | recursive_dump_type (TYPE_FIELD_TYPE (type, idx), spaces + 4); | |
4601 | } | |
4602 | } | |
43bbcdc2 PH |
4603 | if (TYPE_CODE (type) == TYPE_CODE_RANGE) |
4604 | { | |
4605 | printfi_filtered (spaces, "low %s%s high %s%s\n", | |
4606 | plongest (TYPE_LOW_BOUND (type)), | |
4607 | TYPE_LOW_BOUND_UNDEFINED (type) ? " (undefined)" : "", | |
4608 | plongest (TYPE_HIGH_BOUND (type)), | |
3e43a32a MS |
4609 | TYPE_HIGH_BOUND_UNDEFINED (type) |
4610 | ? " (undefined)" : ""); | |
43bbcdc2 | 4611 | } |
c906108c | 4612 | |
b4ba55a1 JB |
4613 | switch (TYPE_SPECIFIC_FIELD (type)) |
4614 | { | |
4615 | case TYPE_SPECIFIC_CPLUS_STUFF: | |
4616 | printfi_filtered (spaces, "cplus_stuff "); | |
4617 | gdb_print_host_address (TYPE_CPLUS_SPECIFIC (type), | |
4618 | gdb_stdout); | |
4619 | puts_filtered ("\n"); | |
4620 | print_cplus_stuff (type, spaces); | |
4621 | break; | |
8da61cc4 | 4622 | |
b4ba55a1 JB |
4623 | case TYPE_SPECIFIC_GNAT_STUFF: |
4624 | printfi_filtered (spaces, "gnat_stuff "); | |
4625 | gdb_print_host_address (TYPE_GNAT_SPECIFIC (type), gdb_stdout); | |
4626 | puts_filtered ("\n"); | |
4627 | print_gnat_stuff (type, spaces); | |
4628 | break; | |
701c159d | 4629 | |
b4ba55a1 JB |
4630 | case TYPE_SPECIFIC_FLOATFORMAT: |
4631 | printfi_filtered (spaces, "floatformat "); | |
0db7851f UW |
4632 | if (TYPE_FLOATFORMAT (type) == NULL |
4633 | || TYPE_FLOATFORMAT (type)->name == NULL) | |
b4ba55a1 JB |
4634 | puts_filtered ("(null)"); |
4635 | else | |
0db7851f | 4636 | puts_filtered (TYPE_FLOATFORMAT (type)->name); |
b4ba55a1 JB |
4637 | puts_filtered ("\n"); |
4638 | break; | |
c906108c | 4639 | |
b6cdc2c1 | 4640 | case TYPE_SPECIFIC_FUNC: |
b4ba55a1 JB |
4641 | printfi_filtered (spaces, "calling_convention %d\n", |
4642 | TYPE_CALLING_CONVENTION (type)); | |
b6cdc2c1 | 4643 | /* tail_call_list is not printed. */ |
b4ba55a1 | 4644 | break; |
09e2d7c7 DE |
4645 | |
4646 | case TYPE_SPECIFIC_SELF_TYPE: | |
4647 | printfi_filtered (spaces, "self_type "); | |
4648 | gdb_print_host_address (TYPE_SELF_TYPE (type), gdb_stdout); | |
4649 | puts_filtered ("\n"); | |
4650 | break; | |
c906108c | 4651 | } |
b4ba55a1 | 4652 | |
c906108c SS |
4653 | if (spaces == 0) |
4654 | obstack_free (&dont_print_type_obstack, NULL); | |
4655 | } | |
5212577a | 4656 | \f |
ae5a43e0 DJ |
4657 | /* Trivial helpers for the libiberty hash table, for mapping one |
4658 | type to another. */ | |
4659 | ||
4660 | struct type_pair | |
4661 | { | |
fe978cb0 | 4662 | struct type *old, *newobj; |
ae5a43e0 DJ |
4663 | }; |
4664 | ||
4665 | static hashval_t | |
4666 | type_pair_hash (const void *item) | |
4667 | { | |
9a3c8263 | 4668 | const struct type_pair *pair = (const struct type_pair *) item; |
d8734c88 | 4669 | |
ae5a43e0 DJ |
4670 | return htab_hash_pointer (pair->old); |
4671 | } | |
4672 | ||
4673 | static int | |
4674 | type_pair_eq (const void *item_lhs, const void *item_rhs) | |
4675 | { | |
9a3c8263 SM |
4676 | const struct type_pair *lhs = (const struct type_pair *) item_lhs; |
4677 | const struct type_pair *rhs = (const struct type_pair *) item_rhs; | |
d8734c88 | 4678 | |
ae5a43e0 DJ |
4679 | return lhs->old == rhs->old; |
4680 | } | |
4681 | ||
4682 | /* Allocate the hash table used by copy_type_recursive to walk | |
4683 | types without duplicates. We use OBJFILE's obstack, because | |
4684 | OBJFILE is about to be deleted. */ | |
4685 | ||
4686 | htab_t | |
4687 | create_copied_types_hash (struct objfile *objfile) | |
4688 | { | |
4689 | return htab_create_alloc_ex (1, type_pair_hash, type_pair_eq, | |
4690 | NULL, &objfile->objfile_obstack, | |
4691 | hashtab_obstack_allocate, | |
4692 | dummy_obstack_deallocate); | |
4693 | } | |
4694 | ||
d9823cbb KB |
4695 | /* Recursively copy (deep copy) a dynamic attribute list of a type. */ |
4696 | ||
4697 | static struct dynamic_prop_list * | |
4698 | copy_dynamic_prop_list (struct obstack *objfile_obstack, | |
4699 | struct dynamic_prop_list *list) | |
4700 | { | |
4701 | struct dynamic_prop_list *copy = list; | |
4702 | struct dynamic_prop_list **node_ptr = © | |
4703 | ||
4704 | while (*node_ptr != NULL) | |
4705 | { | |
4706 | struct dynamic_prop_list *node_copy; | |
4707 | ||
224c3ddb SM |
4708 | node_copy = ((struct dynamic_prop_list *) |
4709 | obstack_copy (objfile_obstack, *node_ptr, | |
4710 | sizeof (struct dynamic_prop_list))); | |
283a9958 | 4711 | node_copy->prop = (*node_ptr)->prop; |
d9823cbb KB |
4712 | *node_ptr = node_copy; |
4713 | ||
4714 | node_ptr = &node_copy->next; | |
4715 | } | |
4716 | ||
4717 | return copy; | |
4718 | } | |
4719 | ||
7ba81444 | 4720 | /* Recursively copy (deep copy) TYPE, if it is associated with |
eed8b28a PP |
4721 | OBJFILE. Return a new type owned by the gdbarch associated with the type, a |
4722 | saved type if we have already visited TYPE (using COPIED_TYPES), or TYPE if | |
4723 | it is not associated with OBJFILE. */ | |
ae5a43e0 DJ |
4724 | |
4725 | struct type * | |
7ba81444 MS |
4726 | copy_type_recursive (struct objfile *objfile, |
4727 | struct type *type, | |
ae5a43e0 DJ |
4728 | htab_t copied_types) |
4729 | { | |
4730 | struct type_pair *stored, pair; | |
4731 | void **slot; | |
4732 | struct type *new_type; | |
4733 | ||
e9bb382b | 4734 | if (! TYPE_OBJFILE_OWNED (type)) |
ae5a43e0 DJ |
4735 | return type; |
4736 | ||
7ba81444 MS |
4737 | /* This type shouldn't be pointing to any types in other objfiles; |
4738 | if it did, the type might disappear unexpectedly. */ | |
ae5a43e0 DJ |
4739 | gdb_assert (TYPE_OBJFILE (type) == objfile); |
4740 | ||
4741 | pair.old = type; | |
4742 | slot = htab_find_slot (copied_types, &pair, INSERT); | |
4743 | if (*slot != NULL) | |
fe978cb0 | 4744 | return ((struct type_pair *) *slot)->newobj; |
ae5a43e0 | 4745 | |
e9bb382b | 4746 | new_type = alloc_type_arch (get_type_arch (type)); |
ae5a43e0 DJ |
4747 | |
4748 | /* We must add the new type to the hash table immediately, in case | |
4749 | we encounter this type again during a recursive call below. */ | |
8d749320 | 4750 | stored = XOBNEW (&objfile->objfile_obstack, struct type_pair); |
ae5a43e0 | 4751 | stored->old = type; |
fe978cb0 | 4752 | stored->newobj = new_type; |
ae5a43e0 DJ |
4753 | *slot = stored; |
4754 | ||
876cecd0 TT |
4755 | /* Copy the common fields of types. For the main type, we simply |
4756 | copy the entire thing and then update specific fields as needed. */ | |
4757 | *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type); | |
e9bb382b UW |
4758 | TYPE_OBJFILE_OWNED (new_type) = 0; |
4759 | TYPE_OWNER (new_type).gdbarch = get_type_arch (type); | |
876cecd0 | 4760 | |
ae5a43e0 DJ |
4761 | if (TYPE_NAME (type)) |
4762 | TYPE_NAME (new_type) = xstrdup (TYPE_NAME (type)); | |
4763 | if (TYPE_TAG_NAME (type)) | |
4764 | TYPE_TAG_NAME (new_type) = xstrdup (TYPE_TAG_NAME (type)); | |
ae5a43e0 DJ |
4765 | |
4766 | TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type); | |
4767 | TYPE_LENGTH (new_type) = TYPE_LENGTH (type); | |
4768 | ||
4769 | /* Copy the fields. */ | |
ae5a43e0 DJ |
4770 | if (TYPE_NFIELDS (type)) |
4771 | { | |
4772 | int i, nfields; | |
4773 | ||
4774 | nfields = TYPE_NFIELDS (type); | |
fc270c35 | 4775 | TYPE_FIELDS (new_type) = XCNEWVEC (struct field, nfields); |
ae5a43e0 DJ |
4776 | for (i = 0; i < nfields; i++) |
4777 | { | |
7ba81444 MS |
4778 | TYPE_FIELD_ARTIFICIAL (new_type, i) = |
4779 | TYPE_FIELD_ARTIFICIAL (type, i); | |
ae5a43e0 DJ |
4780 | TYPE_FIELD_BITSIZE (new_type, i) = TYPE_FIELD_BITSIZE (type, i); |
4781 | if (TYPE_FIELD_TYPE (type, i)) | |
4782 | TYPE_FIELD_TYPE (new_type, i) | |
4783 | = copy_type_recursive (objfile, TYPE_FIELD_TYPE (type, i), | |
4784 | copied_types); | |
4785 | if (TYPE_FIELD_NAME (type, i)) | |
7ba81444 MS |
4786 | TYPE_FIELD_NAME (new_type, i) = |
4787 | xstrdup (TYPE_FIELD_NAME (type, i)); | |
d6a843b5 | 4788 | switch (TYPE_FIELD_LOC_KIND (type, i)) |
ae5a43e0 | 4789 | { |
d6a843b5 JK |
4790 | case FIELD_LOC_KIND_BITPOS: |
4791 | SET_FIELD_BITPOS (TYPE_FIELD (new_type, i), | |
4792 | TYPE_FIELD_BITPOS (type, i)); | |
4793 | break; | |
14e75d8e JK |
4794 | case FIELD_LOC_KIND_ENUMVAL: |
4795 | SET_FIELD_ENUMVAL (TYPE_FIELD (new_type, i), | |
4796 | TYPE_FIELD_ENUMVAL (type, i)); | |
4797 | break; | |
d6a843b5 JK |
4798 | case FIELD_LOC_KIND_PHYSADDR: |
4799 | SET_FIELD_PHYSADDR (TYPE_FIELD (new_type, i), | |
4800 | TYPE_FIELD_STATIC_PHYSADDR (type, i)); | |
4801 | break; | |
4802 | case FIELD_LOC_KIND_PHYSNAME: | |
4803 | SET_FIELD_PHYSNAME (TYPE_FIELD (new_type, i), | |
4804 | xstrdup (TYPE_FIELD_STATIC_PHYSNAME (type, | |
4805 | i))); | |
4806 | break; | |
4807 | default: | |
4808 | internal_error (__FILE__, __LINE__, | |
4809 | _("Unexpected type field location kind: %d"), | |
4810 | TYPE_FIELD_LOC_KIND (type, i)); | |
ae5a43e0 DJ |
4811 | } |
4812 | } | |
4813 | } | |
4814 | ||
0963b4bd | 4815 | /* For range types, copy the bounds information. */ |
43bbcdc2 PH |
4816 | if (TYPE_CODE (type) == TYPE_CODE_RANGE) |
4817 | { | |
8d749320 | 4818 | TYPE_RANGE_DATA (new_type) = XNEW (struct range_bounds); |
43bbcdc2 PH |
4819 | *TYPE_RANGE_DATA (new_type) = *TYPE_RANGE_DATA (type); |
4820 | } | |
4821 | ||
d9823cbb KB |
4822 | if (TYPE_DYN_PROP_LIST (type) != NULL) |
4823 | TYPE_DYN_PROP_LIST (new_type) | |
4824 | = copy_dynamic_prop_list (&objfile->objfile_obstack, | |
4825 | TYPE_DYN_PROP_LIST (type)); | |
4826 | ||
3cdcd0ce | 4827 | |
ae5a43e0 DJ |
4828 | /* Copy pointers to other types. */ |
4829 | if (TYPE_TARGET_TYPE (type)) | |
7ba81444 MS |
4830 | TYPE_TARGET_TYPE (new_type) = |
4831 | copy_type_recursive (objfile, | |
4832 | TYPE_TARGET_TYPE (type), | |
4833 | copied_types); | |
f6b3afbf | 4834 | |
ae5a43e0 DJ |
4835 | /* Maybe copy the type_specific bits. |
4836 | ||
4837 | NOTE drow/2005-12-09: We do not copy the C++-specific bits like | |
4838 | base classes and methods. There's no fundamental reason why we | |
4839 | can't, but at the moment it is not needed. */ | |
4840 | ||
f6b3afbf DE |
4841 | switch (TYPE_SPECIFIC_FIELD (type)) |
4842 | { | |
4843 | case TYPE_SPECIFIC_NONE: | |
4844 | break; | |
4845 | case TYPE_SPECIFIC_FUNC: | |
4846 | INIT_FUNC_SPECIFIC (new_type); | |
4847 | TYPE_CALLING_CONVENTION (new_type) = TYPE_CALLING_CONVENTION (type); | |
4848 | TYPE_NO_RETURN (new_type) = TYPE_NO_RETURN (type); | |
4849 | TYPE_TAIL_CALL_LIST (new_type) = NULL; | |
4850 | break; | |
4851 | case TYPE_SPECIFIC_FLOATFORMAT: | |
4852 | TYPE_FLOATFORMAT (new_type) = TYPE_FLOATFORMAT (type); | |
4853 | break; | |
4854 | case TYPE_SPECIFIC_CPLUS_STUFF: | |
4855 | INIT_CPLUS_SPECIFIC (new_type); | |
4856 | break; | |
4857 | case TYPE_SPECIFIC_GNAT_STUFF: | |
4858 | INIT_GNAT_SPECIFIC (new_type); | |
4859 | break; | |
09e2d7c7 DE |
4860 | case TYPE_SPECIFIC_SELF_TYPE: |
4861 | set_type_self_type (new_type, | |
4862 | copy_type_recursive (objfile, TYPE_SELF_TYPE (type), | |
4863 | copied_types)); | |
4864 | break; | |
f6b3afbf DE |
4865 | default: |
4866 | gdb_assert_not_reached ("bad type_specific_kind"); | |
4867 | } | |
ae5a43e0 DJ |
4868 | |
4869 | return new_type; | |
4870 | } | |
4871 | ||
4af88198 JB |
4872 | /* Make a copy of the given TYPE, except that the pointer & reference |
4873 | types are not preserved. | |
4874 | ||
4875 | This function assumes that the given type has an associated objfile. | |
4876 | This objfile is used to allocate the new type. */ | |
4877 | ||
4878 | struct type * | |
4879 | copy_type (const struct type *type) | |
4880 | { | |
4881 | struct type *new_type; | |
4882 | ||
e9bb382b | 4883 | gdb_assert (TYPE_OBJFILE_OWNED (type)); |
4af88198 | 4884 | |
e9bb382b | 4885 | new_type = alloc_type_copy (type); |
4af88198 JB |
4886 | TYPE_INSTANCE_FLAGS (new_type) = TYPE_INSTANCE_FLAGS (type); |
4887 | TYPE_LENGTH (new_type) = TYPE_LENGTH (type); | |
4888 | memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type), | |
4889 | sizeof (struct main_type)); | |
d9823cbb KB |
4890 | if (TYPE_DYN_PROP_LIST (type) != NULL) |
4891 | TYPE_DYN_PROP_LIST (new_type) | |
4892 | = copy_dynamic_prop_list (&TYPE_OBJFILE (type) -> objfile_obstack, | |
4893 | TYPE_DYN_PROP_LIST (type)); | |
4af88198 JB |
4894 | |
4895 | return new_type; | |
4896 | } | |
5212577a | 4897 | \f |
e9bb382b UW |
4898 | /* Helper functions to initialize architecture-specific types. */ |
4899 | ||
4900 | /* Allocate a type structure associated with GDBARCH and set its | |
4901 | CODE, LENGTH, and NAME fields. */ | |
5212577a | 4902 | |
e9bb382b UW |
4903 | struct type * |
4904 | arch_type (struct gdbarch *gdbarch, | |
77b7c781 | 4905 | enum type_code code, int bit, const char *name) |
e9bb382b UW |
4906 | { |
4907 | struct type *type; | |
4908 | ||
4909 | type = alloc_type_arch (gdbarch); | |
ae438bc5 | 4910 | set_type_code (type, code); |
77b7c781 UW |
4911 | gdb_assert ((bit % TARGET_CHAR_BIT) == 0); |
4912 | TYPE_LENGTH (type) = bit / TARGET_CHAR_BIT; | |
e9bb382b UW |
4913 | |
4914 | if (name) | |
6c214e7c | 4915 | TYPE_NAME (type) = gdbarch_obstack_strdup (gdbarch, name); |
e9bb382b UW |
4916 | |
4917 | return type; | |
4918 | } | |
4919 | ||
4920 | /* Allocate a TYPE_CODE_INT type structure associated with GDBARCH. | |
4921 | BIT is the type size in bits. If UNSIGNED_P is non-zero, set | |
4922 | the type's TYPE_UNSIGNED flag. NAME is the type name. */ | |
5212577a | 4923 | |
e9bb382b UW |
4924 | struct type * |
4925 | arch_integer_type (struct gdbarch *gdbarch, | |
695bfa52 | 4926 | int bit, int unsigned_p, const char *name) |
e9bb382b UW |
4927 | { |
4928 | struct type *t; | |
4929 | ||
77b7c781 | 4930 | t = arch_type (gdbarch, TYPE_CODE_INT, bit, name); |
e9bb382b UW |
4931 | if (unsigned_p) |
4932 | TYPE_UNSIGNED (t) = 1; | |
e9bb382b UW |
4933 | |
4934 | return t; | |
4935 | } | |
4936 | ||
4937 | /* Allocate a TYPE_CODE_CHAR type structure associated with GDBARCH. | |
4938 | BIT is the type size in bits. If UNSIGNED_P is non-zero, set | |
4939 | the type's TYPE_UNSIGNED flag. NAME is the type name. */ | |
5212577a | 4940 | |
e9bb382b UW |
4941 | struct type * |
4942 | arch_character_type (struct gdbarch *gdbarch, | |
695bfa52 | 4943 | int bit, int unsigned_p, const char *name) |
e9bb382b UW |
4944 | { |
4945 | struct type *t; | |
4946 | ||
77b7c781 | 4947 | t = arch_type (gdbarch, TYPE_CODE_CHAR, bit, name); |
e9bb382b UW |
4948 | if (unsigned_p) |
4949 | TYPE_UNSIGNED (t) = 1; | |
4950 | ||
4951 | return t; | |
4952 | } | |
4953 | ||
4954 | /* Allocate a TYPE_CODE_BOOL type structure associated with GDBARCH. | |
4955 | BIT is the type size in bits. If UNSIGNED_P is non-zero, set | |
4956 | the type's TYPE_UNSIGNED flag. NAME is the type name. */ | |
5212577a | 4957 | |
e9bb382b UW |
4958 | struct type * |
4959 | arch_boolean_type (struct gdbarch *gdbarch, | |
695bfa52 | 4960 | int bit, int unsigned_p, const char *name) |
e9bb382b UW |
4961 | { |
4962 | struct type *t; | |
4963 | ||
77b7c781 | 4964 | t = arch_type (gdbarch, TYPE_CODE_BOOL, bit, name); |
e9bb382b UW |
4965 | if (unsigned_p) |
4966 | TYPE_UNSIGNED (t) = 1; | |
4967 | ||
4968 | return t; | |
4969 | } | |
4970 | ||
4971 | /* Allocate a TYPE_CODE_FLT type structure associated with GDBARCH. | |
4972 | BIT is the type size in bits; if BIT equals -1, the size is | |
4973 | determined by the floatformat. NAME is the type name. Set the | |
4974 | TYPE_FLOATFORMAT from FLOATFORMATS. */ | |
5212577a | 4975 | |
27067745 | 4976 | struct type * |
e9bb382b | 4977 | arch_float_type (struct gdbarch *gdbarch, |
695bfa52 TT |
4978 | int bit, const char *name, |
4979 | const struct floatformat **floatformats) | |
8da61cc4 | 4980 | { |
0db7851f | 4981 | const struct floatformat *fmt = floatformats[gdbarch_byte_order (gdbarch)]; |
8da61cc4 DJ |
4982 | struct type *t; |
4983 | ||
0db7851f | 4984 | bit = verify_floatformat (bit, fmt); |
77b7c781 | 4985 | t = arch_type (gdbarch, TYPE_CODE_FLT, bit, name); |
0db7851f | 4986 | TYPE_FLOATFORMAT (t) = fmt; |
b79497cb | 4987 | |
8da61cc4 DJ |
4988 | return t; |
4989 | } | |
4990 | ||
88dfca6c UW |
4991 | /* Allocate a TYPE_CODE_DECFLOAT type structure associated with GDBARCH. |
4992 | BIT is the type size in bits. NAME is the type name. */ | |
4993 | ||
4994 | struct type * | |
4995 | arch_decfloat_type (struct gdbarch *gdbarch, int bit, const char *name) | |
4996 | { | |
4997 | struct type *t; | |
4998 | ||
77b7c781 | 4999 | t = arch_type (gdbarch, TYPE_CODE_DECFLOAT, bit, name); |
88dfca6c UW |
5000 | return t; |
5001 | } | |
5002 | ||
e9bb382b UW |
5003 | /* Allocate a TYPE_CODE_COMPLEX type structure associated with GDBARCH. |
5004 | NAME is the type name. TARGET_TYPE is the component float type. */ | |
5212577a | 5005 | |
27067745 | 5006 | struct type * |
e9bb382b | 5007 | arch_complex_type (struct gdbarch *gdbarch, |
695bfa52 | 5008 | const char *name, struct type *target_type) |
27067745 UW |
5009 | { |
5010 | struct type *t; | |
d8734c88 | 5011 | |
e9bb382b | 5012 | t = arch_type (gdbarch, TYPE_CODE_COMPLEX, |
77b7c781 | 5013 | 2 * TYPE_LENGTH (target_type) * TARGET_CHAR_BIT, name); |
27067745 UW |
5014 | TYPE_TARGET_TYPE (t) = target_type; |
5015 | return t; | |
5016 | } | |
5017 | ||
88dfca6c UW |
5018 | /* Allocate a TYPE_CODE_PTR type structure associated with GDBARCH. |
5019 | BIT is the pointer type size in bits. NAME is the type name. | |
5020 | TARGET_TYPE is the pointer target type. Always sets the pointer type's | |
5021 | TYPE_UNSIGNED flag. */ | |
5022 | ||
5023 | struct type * | |
5024 | arch_pointer_type (struct gdbarch *gdbarch, | |
5025 | int bit, const char *name, struct type *target_type) | |
5026 | { | |
5027 | struct type *t; | |
5028 | ||
77b7c781 | 5029 | t = arch_type (gdbarch, TYPE_CODE_PTR, bit, name); |
88dfca6c UW |
5030 | TYPE_TARGET_TYPE (t) = target_type; |
5031 | TYPE_UNSIGNED (t) = 1; | |
5032 | return t; | |
5033 | } | |
5034 | ||
e9bb382b | 5035 | /* Allocate a TYPE_CODE_FLAGS type structure associated with GDBARCH. |
77b7c781 | 5036 | NAME is the type name. BIT is the size of the flag word in bits. */ |
5212577a | 5037 | |
e9bb382b | 5038 | struct type * |
77b7c781 | 5039 | arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit) |
e9bb382b | 5040 | { |
e9bb382b UW |
5041 | struct type *type; |
5042 | ||
77b7c781 | 5043 | type = arch_type (gdbarch, TYPE_CODE_FLAGS, bit, name); |
e9bb382b | 5044 | TYPE_UNSIGNED (type) = 1; |
81516450 DE |
5045 | TYPE_NFIELDS (type) = 0; |
5046 | /* Pre-allocate enough space assuming every field is one bit. */ | |
224c3ddb | 5047 | TYPE_FIELDS (type) |
77b7c781 | 5048 | = (struct field *) TYPE_ZALLOC (type, bit * sizeof (struct field)); |
e9bb382b UW |
5049 | |
5050 | return type; | |
5051 | } | |
5052 | ||
5053 | /* Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at | |
81516450 DE |
5054 | position BITPOS is called NAME. Pass NAME as "" for fields that |
5055 | should not be printed. */ | |
5056 | ||
5057 | void | |
5058 | append_flags_type_field (struct type *type, int start_bitpos, int nr_bits, | |
695bfa52 | 5059 | struct type *field_type, const char *name) |
81516450 DE |
5060 | { |
5061 | int type_bitsize = TYPE_LENGTH (type) * TARGET_CHAR_BIT; | |
5062 | int field_nr = TYPE_NFIELDS (type); | |
5063 | ||
5064 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLAGS); | |
5065 | gdb_assert (TYPE_NFIELDS (type) + 1 <= type_bitsize); | |
5066 | gdb_assert (start_bitpos >= 0 && start_bitpos < type_bitsize); | |
5067 | gdb_assert (nr_bits >= 1 && nr_bits <= type_bitsize); | |
5068 | gdb_assert (name != NULL); | |
5069 | ||
5070 | TYPE_FIELD_NAME (type, field_nr) = xstrdup (name); | |
5071 | TYPE_FIELD_TYPE (type, field_nr) = field_type; | |
5072 | SET_FIELD_BITPOS (TYPE_FIELD (type, field_nr), start_bitpos); | |
5073 | TYPE_FIELD_BITSIZE (type, field_nr) = nr_bits; | |
5074 | ++TYPE_NFIELDS (type); | |
5075 | } | |
5076 | ||
5077 | /* Special version of append_flags_type_field to add a flag field. | |
5078 | Add field to TYPE_CODE_FLAGS type TYPE to indicate the bit at | |
e9bb382b | 5079 | position BITPOS is called NAME. */ |
5212577a | 5080 | |
e9bb382b | 5081 | void |
695bfa52 | 5082 | append_flags_type_flag (struct type *type, int bitpos, const char *name) |
e9bb382b | 5083 | { |
81516450 | 5084 | struct gdbarch *gdbarch = get_type_arch (type); |
e9bb382b | 5085 | |
81516450 DE |
5086 | append_flags_type_field (type, bitpos, 1, |
5087 | builtin_type (gdbarch)->builtin_bool, | |
5088 | name); | |
e9bb382b UW |
5089 | } |
5090 | ||
5091 | /* Allocate a TYPE_CODE_STRUCT or TYPE_CODE_UNION type structure (as | |
5092 | specified by CODE) associated with GDBARCH. NAME is the type name. */ | |
5212577a | 5093 | |
e9bb382b | 5094 | struct type * |
695bfa52 TT |
5095 | arch_composite_type (struct gdbarch *gdbarch, const char *name, |
5096 | enum type_code code) | |
e9bb382b UW |
5097 | { |
5098 | struct type *t; | |
d8734c88 | 5099 | |
e9bb382b UW |
5100 | gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION); |
5101 | t = arch_type (gdbarch, code, 0, NULL); | |
5102 | TYPE_TAG_NAME (t) = name; | |
5103 | INIT_CPLUS_SPECIFIC (t); | |
5104 | return t; | |
5105 | } | |
5106 | ||
5107 | /* Add new field with name NAME and type FIELD to composite type T. | |
f5dff777 DJ |
5108 | Do not set the field's position or adjust the type's length; |
5109 | the caller should do so. Return the new field. */ | |
5212577a | 5110 | |
f5dff777 | 5111 | struct field * |
695bfa52 | 5112 | append_composite_type_field_raw (struct type *t, const char *name, |
f5dff777 | 5113 | struct type *field) |
e9bb382b UW |
5114 | { |
5115 | struct field *f; | |
d8734c88 | 5116 | |
e9bb382b | 5117 | TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1; |
224c3ddb SM |
5118 | TYPE_FIELDS (t) = XRESIZEVEC (struct field, TYPE_FIELDS (t), |
5119 | TYPE_NFIELDS (t)); | |
e9bb382b UW |
5120 | f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]); |
5121 | memset (f, 0, sizeof f[0]); | |
5122 | FIELD_TYPE (f[0]) = field; | |
5123 | FIELD_NAME (f[0]) = name; | |
f5dff777 DJ |
5124 | return f; |
5125 | } | |
5126 | ||
5127 | /* Add new field with name NAME and type FIELD to composite type T. | |
5128 | ALIGNMENT (if non-zero) specifies the minimum field alignment. */ | |
5212577a | 5129 | |
f5dff777 | 5130 | void |
695bfa52 | 5131 | append_composite_type_field_aligned (struct type *t, const char *name, |
f5dff777 DJ |
5132 | struct type *field, int alignment) |
5133 | { | |
5134 | struct field *f = append_composite_type_field_raw (t, name, field); | |
d8734c88 | 5135 | |
e9bb382b UW |
5136 | if (TYPE_CODE (t) == TYPE_CODE_UNION) |
5137 | { | |
5138 | if (TYPE_LENGTH (t) < TYPE_LENGTH (field)) | |
5139 | TYPE_LENGTH (t) = TYPE_LENGTH (field); | |
5140 | } | |
5141 | else if (TYPE_CODE (t) == TYPE_CODE_STRUCT) | |
5142 | { | |
5143 | TYPE_LENGTH (t) = TYPE_LENGTH (t) + TYPE_LENGTH (field); | |
5144 | if (TYPE_NFIELDS (t) > 1) | |
5145 | { | |
f41f5e61 PA |
5146 | SET_FIELD_BITPOS (f[0], |
5147 | (FIELD_BITPOS (f[-1]) | |
5148 | + (TYPE_LENGTH (FIELD_TYPE (f[-1])) | |
5149 | * TARGET_CHAR_BIT))); | |
e9bb382b UW |
5150 | |
5151 | if (alignment) | |
5152 | { | |
86c3c1fc AB |
5153 | int left; |
5154 | ||
5155 | alignment *= TARGET_CHAR_BIT; | |
5156 | left = FIELD_BITPOS (f[0]) % alignment; | |
d8734c88 | 5157 | |
e9bb382b UW |
5158 | if (left) |
5159 | { | |
f41f5e61 | 5160 | SET_FIELD_BITPOS (f[0], FIELD_BITPOS (f[0]) + (alignment - left)); |
86c3c1fc | 5161 | TYPE_LENGTH (t) += (alignment - left) / TARGET_CHAR_BIT; |
e9bb382b UW |
5162 | } |
5163 | } | |
5164 | } | |
5165 | } | |
5166 | } | |
5167 | ||
5168 | /* Add new field with name NAME and type FIELD to composite type T. */ | |
5212577a | 5169 | |
e9bb382b | 5170 | void |
695bfa52 | 5171 | append_composite_type_field (struct type *t, const char *name, |
e9bb382b UW |
5172 | struct type *field) |
5173 | { | |
5174 | append_composite_type_field_aligned (t, name, field, 0); | |
5175 | } | |
5176 | ||
000177f0 AC |
5177 | static struct gdbarch_data *gdbtypes_data; |
5178 | ||
5179 | const struct builtin_type * | |
5180 | builtin_type (struct gdbarch *gdbarch) | |
5181 | { | |
9a3c8263 | 5182 | return (const struct builtin_type *) gdbarch_data (gdbarch, gdbtypes_data); |
000177f0 AC |
5183 | } |
5184 | ||
5185 | static void * | |
5186 | gdbtypes_post_init (struct gdbarch *gdbarch) | |
5187 | { | |
5188 | struct builtin_type *builtin_type | |
5189 | = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_type); | |
5190 | ||
46bf5051 | 5191 | /* Basic types. */ |
e9bb382b | 5192 | builtin_type->builtin_void |
77b7c781 | 5193 | = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void"); |
e9bb382b UW |
5194 | builtin_type->builtin_char |
5195 | = arch_integer_type (gdbarch, TARGET_CHAR_BIT, | |
5196 | !gdbarch_char_signed (gdbarch), "char"); | |
c413c448 | 5197 | TYPE_NOSIGN (builtin_type->builtin_char) = 1; |
e9bb382b UW |
5198 | builtin_type->builtin_signed_char |
5199 | = arch_integer_type (gdbarch, TARGET_CHAR_BIT, | |
5200 | 0, "signed char"); | |
5201 | builtin_type->builtin_unsigned_char | |
5202 | = arch_integer_type (gdbarch, TARGET_CHAR_BIT, | |
5203 | 1, "unsigned char"); | |
5204 | builtin_type->builtin_short | |
5205 | = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), | |
5206 | 0, "short"); | |
5207 | builtin_type->builtin_unsigned_short | |
5208 | = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), | |
5209 | 1, "unsigned short"); | |
5210 | builtin_type->builtin_int | |
5211 | = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), | |
5212 | 0, "int"); | |
5213 | builtin_type->builtin_unsigned_int | |
5214 | = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), | |
5215 | 1, "unsigned int"); | |
5216 | builtin_type->builtin_long | |
5217 | = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), | |
5218 | 0, "long"); | |
5219 | builtin_type->builtin_unsigned_long | |
5220 | = arch_integer_type (gdbarch, gdbarch_long_bit (gdbarch), | |
5221 | 1, "unsigned long"); | |
5222 | builtin_type->builtin_long_long | |
5223 | = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), | |
5224 | 0, "long long"); | |
5225 | builtin_type->builtin_unsigned_long_long | |
5226 | = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), | |
5227 | 1, "unsigned long long"); | |
70bd8e24 | 5228 | builtin_type->builtin_float |
e9bb382b | 5229 | = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch), |
27067745 | 5230 | "float", gdbarch_float_format (gdbarch)); |
70bd8e24 | 5231 | builtin_type->builtin_double |
e9bb382b | 5232 | = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch), |
27067745 | 5233 | "double", gdbarch_double_format (gdbarch)); |
70bd8e24 | 5234 | builtin_type->builtin_long_double |
e9bb382b | 5235 | = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch), |
27067745 | 5236 | "long double", gdbarch_long_double_format (gdbarch)); |
70bd8e24 | 5237 | builtin_type->builtin_complex |
e9bb382b UW |
5238 | = arch_complex_type (gdbarch, "complex", |
5239 | builtin_type->builtin_float); | |
70bd8e24 | 5240 | builtin_type->builtin_double_complex |
e9bb382b UW |
5241 | = arch_complex_type (gdbarch, "double complex", |
5242 | builtin_type->builtin_double); | |
5243 | builtin_type->builtin_string | |
77b7c781 | 5244 | = arch_type (gdbarch, TYPE_CODE_STRING, TARGET_CHAR_BIT, "string"); |
e9bb382b | 5245 | builtin_type->builtin_bool |
77b7c781 | 5246 | = arch_type (gdbarch, TYPE_CODE_BOOL, TARGET_CHAR_BIT, "bool"); |
000177f0 | 5247 | |
7678ef8f TJB |
5248 | /* The following three are about decimal floating point types, which |
5249 | are 32-bits, 64-bits and 128-bits respectively. */ | |
5250 | builtin_type->builtin_decfloat | |
88dfca6c | 5251 | = arch_decfloat_type (gdbarch, 32, "_Decimal32"); |
7678ef8f | 5252 | builtin_type->builtin_decdouble |
88dfca6c | 5253 | = arch_decfloat_type (gdbarch, 64, "_Decimal64"); |
7678ef8f | 5254 | builtin_type->builtin_declong |
88dfca6c | 5255 | = arch_decfloat_type (gdbarch, 128, "_Decimal128"); |
7678ef8f | 5256 | |
69feb676 | 5257 | /* "True" character types. */ |
e9bb382b UW |
5258 | builtin_type->builtin_true_char |
5259 | = arch_character_type (gdbarch, TARGET_CHAR_BIT, 0, "true character"); | |
5260 | builtin_type->builtin_true_unsigned_char | |
5261 | = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "true character"); | |
69feb676 | 5262 | |
df4df182 | 5263 | /* Fixed-size integer types. */ |
e9bb382b UW |
5264 | builtin_type->builtin_int0 |
5265 | = arch_integer_type (gdbarch, 0, 0, "int0_t"); | |
5266 | builtin_type->builtin_int8 | |
5267 | = arch_integer_type (gdbarch, 8, 0, "int8_t"); | |
5268 | builtin_type->builtin_uint8 | |
5269 | = arch_integer_type (gdbarch, 8, 1, "uint8_t"); | |
5270 | builtin_type->builtin_int16 | |
5271 | = arch_integer_type (gdbarch, 16, 0, "int16_t"); | |
5272 | builtin_type->builtin_uint16 | |
5273 | = arch_integer_type (gdbarch, 16, 1, "uint16_t"); | |
5274 | builtin_type->builtin_int32 | |
5275 | = arch_integer_type (gdbarch, 32, 0, "int32_t"); | |
5276 | builtin_type->builtin_uint32 | |
5277 | = arch_integer_type (gdbarch, 32, 1, "uint32_t"); | |
5278 | builtin_type->builtin_int64 | |
5279 | = arch_integer_type (gdbarch, 64, 0, "int64_t"); | |
5280 | builtin_type->builtin_uint64 | |
5281 | = arch_integer_type (gdbarch, 64, 1, "uint64_t"); | |
5282 | builtin_type->builtin_int128 | |
5283 | = arch_integer_type (gdbarch, 128, 0, "int128_t"); | |
5284 | builtin_type->builtin_uint128 | |
5285 | = arch_integer_type (gdbarch, 128, 1, "uint128_t"); | |
2844d6b5 KW |
5286 | TYPE_INSTANCE_FLAGS (builtin_type->builtin_int8) |= |
5287 | TYPE_INSTANCE_FLAG_NOTTEXT; | |
5288 | TYPE_INSTANCE_FLAGS (builtin_type->builtin_uint8) |= | |
5289 | TYPE_INSTANCE_FLAG_NOTTEXT; | |
df4df182 | 5290 | |
9a22f0d0 PM |
5291 | /* Wide character types. */ |
5292 | builtin_type->builtin_char16 | |
53e710ac | 5293 | = arch_integer_type (gdbarch, 16, 1, "char16_t"); |
9a22f0d0 | 5294 | builtin_type->builtin_char32 |
53e710ac | 5295 | = arch_integer_type (gdbarch, 32, 1, "char32_t"); |
53375380 PA |
5296 | builtin_type->builtin_wchar |
5297 | = arch_integer_type (gdbarch, gdbarch_wchar_bit (gdbarch), | |
5298 | !gdbarch_wchar_signed (gdbarch), "wchar_t"); | |
9a22f0d0 | 5299 | |
46bf5051 | 5300 | /* Default data/code pointer types. */ |
e9bb382b UW |
5301 | builtin_type->builtin_data_ptr |
5302 | = lookup_pointer_type (builtin_type->builtin_void); | |
5303 | builtin_type->builtin_func_ptr | |
5304 | = lookup_pointer_type (lookup_function_type (builtin_type->builtin_void)); | |
0875794a JK |
5305 | builtin_type->builtin_func_func |
5306 | = lookup_function_type (builtin_type->builtin_func_ptr); | |
46bf5051 | 5307 | |
78267919 | 5308 | /* This type represents a GDB internal function. */ |
e9bb382b UW |
5309 | builtin_type->internal_fn |
5310 | = arch_type (gdbarch, TYPE_CODE_INTERNAL_FUNCTION, 0, | |
5311 | "<internal function>"); | |
78267919 | 5312 | |
e81e7f5e SC |
5313 | /* This type represents an xmethod. */ |
5314 | builtin_type->xmethod | |
5315 | = arch_type (gdbarch, TYPE_CODE_XMETHOD, 0, "<xmethod>"); | |
5316 | ||
46bf5051 UW |
5317 | return builtin_type; |
5318 | } | |
5319 | ||
46bf5051 UW |
5320 | /* This set of objfile-based types is intended to be used by symbol |
5321 | readers as basic types. */ | |
5322 | ||
5323 | static const struct objfile_data *objfile_type_data; | |
5324 | ||
5325 | const struct objfile_type * | |
5326 | objfile_type (struct objfile *objfile) | |
5327 | { | |
5328 | struct gdbarch *gdbarch; | |
5329 | struct objfile_type *objfile_type | |
9a3c8263 | 5330 | = (struct objfile_type *) objfile_data (objfile, objfile_type_data); |
46bf5051 UW |
5331 | |
5332 | if (objfile_type) | |
5333 | return objfile_type; | |
5334 | ||
5335 | objfile_type = OBSTACK_CALLOC (&objfile->objfile_obstack, | |
5336 | 1, struct objfile_type); | |
5337 | ||
5338 | /* Use the objfile architecture to determine basic type properties. */ | |
5339 | gdbarch = get_objfile_arch (objfile); | |
5340 | ||
5341 | /* Basic types. */ | |
5342 | objfile_type->builtin_void | |
77b7c781 | 5343 | = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void"); |
46bf5051 | 5344 | objfile_type->builtin_char |
19f392bc UW |
5345 | = init_integer_type (objfile, TARGET_CHAR_BIT, |
5346 | !gdbarch_char_signed (gdbarch), "char"); | |
c413c448 | 5347 | TYPE_NOSIGN (objfile_type->builtin_char) = 1; |
46bf5051 | 5348 | objfile_type->builtin_signed_char |
19f392bc UW |
5349 | = init_integer_type (objfile, TARGET_CHAR_BIT, |
5350 | 0, "signed char"); | |
46bf5051 | 5351 | objfile_type->builtin_unsigned_char |
19f392bc UW |
5352 | = init_integer_type (objfile, TARGET_CHAR_BIT, |
5353 | 1, "unsigned char"); | |
46bf5051 | 5354 | objfile_type->builtin_short |
19f392bc UW |
5355 | = init_integer_type (objfile, gdbarch_short_bit (gdbarch), |
5356 | 0, "short"); | |
46bf5051 | 5357 | objfile_type->builtin_unsigned_short |
19f392bc UW |
5358 | = init_integer_type (objfile, gdbarch_short_bit (gdbarch), |
5359 | 1, "unsigned short"); | |
46bf5051 | 5360 | objfile_type->builtin_int |
19f392bc UW |
5361 | = init_integer_type (objfile, gdbarch_int_bit (gdbarch), |
5362 | 0, "int"); | |
46bf5051 | 5363 | objfile_type->builtin_unsigned_int |
19f392bc UW |
5364 | = init_integer_type (objfile, gdbarch_int_bit (gdbarch), |
5365 | 1, "unsigned int"); | |
46bf5051 | 5366 | objfile_type->builtin_long |
19f392bc UW |
5367 | = init_integer_type (objfile, gdbarch_long_bit (gdbarch), |
5368 | 0, "long"); | |
46bf5051 | 5369 | objfile_type->builtin_unsigned_long |
19f392bc UW |
5370 | = init_integer_type (objfile, gdbarch_long_bit (gdbarch), |
5371 | 1, "unsigned long"); | |
46bf5051 | 5372 | objfile_type->builtin_long_long |
19f392bc UW |
5373 | = init_integer_type (objfile, gdbarch_long_long_bit (gdbarch), |
5374 | 0, "long long"); | |
46bf5051 | 5375 | objfile_type->builtin_unsigned_long_long |
19f392bc UW |
5376 | = init_integer_type (objfile, gdbarch_long_long_bit (gdbarch), |
5377 | 1, "unsigned long long"); | |
46bf5051 | 5378 | objfile_type->builtin_float |
19f392bc UW |
5379 | = init_float_type (objfile, gdbarch_float_bit (gdbarch), |
5380 | "float", gdbarch_float_format (gdbarch)); | |
46bf5051 | 5381 | objfile_type->builtin_double |
19f392bc UW |
5382 | = init_float_type (objfile, gdbarch_double_bit (gdbarch), |
5383 | "double", gdbarch_double_format (gdbarch)); | |
46bf5051 | 5384 | objfile_type->builtin_long_double |
19f392bc UW |
5385 | = init_float_type (objfile, gdbarch_long_double_bit (gdbarch), |
5386 | "long double", gdbarch_long_double_format (gdbarch)); | |
46bf5051 UW |
5387 | |
5388 | /* This type represents a type that was unrecognized in symbol read-in. */ | |
5389 | objfile_type->builtin_error | |
19f392bc | 5390 | = init_type (objfile, TYPE_CODE_ERROR, 0, "<unknown type>"); |
46bf5051 UW |
5391 | |
5392 | /* The following set of types is used for symbols with no | |
5393 | debug information. */ | |
5394 | objfile_type->nodebug_text_symbol | |
77b7c781 | 5395 | = init_type (objfile, TYPE_CODE_FUNC, TARGET_CHAR_BIT, |
19f392bc | 5396 | "<text variable, no debug info>"); |
0875794a | 5397 | objfile_type->nodebug_text_gnu_ifunc_symbol |
77b7c781 | 5398 | = init_type (objfile, TYPE_CODE_FUNC, TARGET_CHAR_BIT, |
19f392bc | 5399 | "<text gnu-indirect-function variable, no debug info>"); |
7022349d | 5400 | /* Ifunc resolvers return a function address. */ |
0875794a | 5401 | TYPE_TARGET_TYPE (objfile_type->nodebug_text_gnu_ifunc_symbol) |
7022349d PA |
5402 | = init_integer_type (objfile, gdbarch_addr_bit (gdbarch), 1, |
5403 | "__IFUNC_RESOLVER_RET"); | |
19f392bc | 5404 | TYPE_GNU_IFUNC (objfile_type->nodebug_text_gnu_ifunc_symbol) = 1; |
0875794a | 5405 | objfile_type->nodebug_got_plt_symbol |
19f392bc UW |
5406 | = init_pointer_type (objfile, gdbarch_addr_bit (gdbarch), |
5407 | "<text from jump slot in .got.plt, no debug info>", | |
5408 | objfile_type->nodebug_text_symbol); | |
46bf5051 | 5409 | objfile_type->nodebug_data_symbol |
46a4882b | 5410 | = init_nodebug_var_type (objfile, "<data variable, no debug info>"); |
46bf5051 | 5411 | objfile_type->nodebug_unknown_symbol |
46a4882b | 5412 | = init_nodebug_var_type (objfile, "<variable (not text or data), no debug info>"); |
46bf5051 | 5413 | objfile_type->nodebug_tls_symbol |
46a4882b | 5414 | = init_nodebug_var_type (objfile, "<thread local variable, no debug info>"); |
000177f0 AC |
5415 | |
5416 | /* NOTE: on some targets, addresses and pointers are not necessarily | |
0a7cfe2c | 5417 | the same. |
000177f0 AC |
5418 | |
5419 | The upshot is: | |
5420 | - gdb's `struct type' always describes the target's | |
5421 | representation. | |
5422 | - gdb's `struct value' objects should always hold values in | |
5423 | target form. | |
5424 | - gdb's CORE_ADDR values are addresses in the unified virtual | |
5425 | address space that the assembler and linker work with. Thus, | |
5426 | since target_read_memory takes a CORE_ADDR as an argument, it | |
5427 | can access any memory on the target, even if the processor has | |
5428 | separate code and data address spaces. | |
5429 | ||
46bf5051 UW |
5430 | In this context, objfile_type->builtin_core_addr is a bit odd: |
5431 | it's a target type for a value the target will never see. It's | |
5432 | only used to hold the values of (typeless) linker symbols, which | |
5433 | are indeed in the unified virtual address space. */ | |
000177f0 | 5434 | |
46bf5051 | 5435 | objfile_type->builtin_core_addr |
19f392bc UW |
5436 | = init_integer_type (objfile, gdbarch_addr_bit (gdbarch), 1, |
5437 | "__CORE_ADDR"); | |
64c50499 | 5438 | |
46bf5051 UW |
5439 | set_objfile_data (objfile, objfile_type_data, objfile_type); |
5440 | return objfile_type; | |
000177f0 AC |
5441 | } |
5442 | ||
c906108c | 5443 | void |
fba45db2 | 5444 | _initialize_gdbtypes (void) |
c906108c | 5445 | { |
5674de60 | 5446 | gdbtypes_data = gdbarch_data_register_post_init (gdbtypes_post_init); |
46bf5051 | 5447 | objfile_type_data = register_objfile_data (); |
5674de60 | 5448 | |
ccce17b0 YQ |
5449 | add_setshow_zuinteger_cmd ("overload", no_class, &overload_debug, |
5450 | _("Set debugging of C++ overloading."), | |
5451 | _("Show debugging of C++ overloading."), | |
5452 | _("When enabled, ranking of the " | |
5453 | "functions is displayed."), | |
5454 | NULL, | |
5455 | show_overload_debug, | |
5456 | &setdebuglist, &showdebuglist); | |
5674de60 | 5457 | |
7ba81444 | 5458 | /* Add user knob for controlling resolution of opaque types. */ |
5674de60 | 5459 | add_setshow_boolean_cmd ("opaque-type-resolution", class_support, |
3e43a32a MS |
5460 | &opaque_type_resolution, |
5461 | _("Set resolution of opaque struct/class/union" | |
5462 | " types (if set before loading symbols)."), | |
5463 | _("Show resolution of opaque struct/class/union" | |
5464 | " types (if set before loading symbols)."), | |
5465 | NULL, NULL, | |
5674de60 UW |
5466 | show_opaque_type_resolution, |
5467 | &setlist, &showlist); | |
a451cb65 KS |
5468 | |
5469 | /* Add an option to permit non-strict type checking. */ | |
5470 | add_setshow_boolean_cmd ("type", class_support, | |
5471 | &strict_type_checking, | |
5472 | _("Set strict type checking."), | |
5473 | _("Show strict type checking."), | |
5474 | NULL, NULL, | |
5475 | show_strict_type_checking, | |
5476 | &setchecklist, &showchecklist); | |
c906108c | 5477 | } |