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