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