]>
Commit | Line | Data |
---|---|---|
ed3ef339 DE |
1 | /* Scheme interface to types. |
2 | ||
1d506c26 | 3 | Copyright (C) 2008-2024 Free Software Foundation, Inc. |
ed3ef339 DE |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | /* See README file in this directory for implementation notes, coding | |
21 | conventions, et.al. */ | |
22 | ||
23 | #include "defs.h" | |
ef0f16cc | 24 | #include "top.h" |
ed3ef339 DE |
25 | #include "arch-utils.h" |
26 | #include "value.h" | |
ed3ef339 DE |
27 | #include "gdbtypes.h" |
28 | #include "objfiles.h" | |
29 | #include "language.h" | |
ed3ef339 | 30 | #include "bcache.h" |
82ca8957 | 31 | #include "dwarf2/loc.h" |
ed3ef339 DE |
32 | #include "typeprint.h" |
33 | #include "guile-internal.h" | |
34 | ||
35 | /* The <gdb:type> smob. | |
36 | The type is chained with all types associated with its objfile, if any. | |
37 | This lets us copy the underlying struct type when the objfile is | |
f99b5177 | 38 | deleted. */ |
ed3ef339 | 39 | |
f99b5177 | 40 | struct type_smob |
ed3ef339 DE |
41 | { |
42 | /* This always appears first. | |
43 | eqable_gdb_smob is used so that types are eq?-able. | |
44 | Also, a type object can be associated with an objfile. eqable_gdb_smob | |
45 | lets us track the lifetime of all types associated with an objfile. | |
46 | When an objfile is deleted we need to invalidate the type object. */ | |
47 | eqable_gdb_smob base; | |
48 | ||
49 | /* The GDB type structure this smob is wrapping. */ | |
50 | struct type *type; | |
1913f160 | 51 | }; |
ed3ef339 DE |
52 | |
53 | /* A field smob. */ | |
54 | ||
f99b5177 | 55 | struct field_smob |
ed3ef339 DE |
56 | { |
57 | /* This always appears first. */ | |
58 | gdb_smob base; | |
59 | ||
60 | /* Backlink to the containing <gdb:type> object. */ | |
61 | SCM type_scm; | |
62 | ||
63 | /* The field number in TYPE_SCM. */ | |
64 | int field_num; | |
f99b5177 | 65 | }; |
ed3ef339 DE |
66 | |
67 | static const char type_smob_name[] = "gdb:type"; | |
68 | static const char field_smob_name[] = "gdb:field"; | |
69 | ||
70 | static const char not_composite_error[] = | |
71 | N_("type is not a structure, union, or enum type"); | |
72 | ||
73 | /* The tag Guile knows the type smob by. */ | |
74 | static scm_t_bits type_smob_tag; | |
75 | ||
76 | /* The tag Guile knows the field smob by. */ | |
77 | static scm_t_bits field_smob_tag; | |
78 | ||
79 | /* The "next" procedure for field iterators. */ | |
80 | static SCM tyscm_next_field_x_proc; | |
81 | ||
82 | /* Keywords used in argument passing. */ | |
83 | static SCM block_keyword; | |
84 | ||
08b8a139 TT |
85 | static int tyscm_copy_type_recursive (void **slot, void *info); |
86 | ||
87 | /* Called when an objfile is about to be deleted. | |
88 | Make a copy of all types associated with OBJFILE. */ | |
89 | ||
90 | struct tyscm_deleter | |
91 | { | |
92 | void operator() (htab_t htab) | |
93 | { | |
94 | if (!gdb_scheme_initialized) | |
95 | return; | |
96 | ||
43cffa64 | 97 | gdb_assert (htab != nullptr); |
08b8a139 | 98 | htab_up copied_types = create_copied_types_hash (); |
43cffa64 TT |
99 | htab_traverse_noresize (htab, tyscm_copy_type_recursive, copied_types.get ()); |
100 | htab_delete (htab); | |
08b8a139 TT |
101 | } |
102 | }; | |
103 | ||
104 | static const registry<objfile>::key<htab, tyscm_deleter> | |
105 | tyscm_objfile_data_key; | |
ed3ef339 DE |
106 | |
107 | /* Hash table to uniquify global (non-objfile-owned) types. */ | |
108 | static htab_t global_types_map; | |
109 | ||
110 | static struct type *tyscm_get_composite (struct type *type); | |
111 | ||
112 | /* Return the type field of T_SMOB. | |
113 | This exists so that we don't have to export the struct's contents. */ | |
114 | ||
115 | struct type * | |
116 | tyscm_type_smob_type (type_smob *t_smob) | |
117 | { | |
118 | return t_smob->type; | |
119 | } | |
120 | ||
3ab692db PA |
121 | /* Return the name of TYPE in expanded form. If there's an error |
122 | computing the name, throws the gdb exception with scm_throw. */ | |
ed3ef339 | 123 | |
3ab692db PA |
124 | static std::string |
125 | tyscm_type_name (struct type *type) | |
ed3ef339 | 126 | { |
680d7fd5 | 127 | SCM excp; |
a70b8144 | 128 | try |
ed3ef339 | 129 | { |
d7e74731 | 130 | string_file stb; |
ed3ef339 | 131 | |
13eb081a TT |
132 | current_language->print_type (type, "", &stb, -1, 0, |
133 | &type_print_raw_options); | |
5d10a204 | 134 | return stb.release (); |
ed3ef339 | 135 | } |
53f1f3d4 KB |
136 | catch (const gdb_exception_forced_quit &except) |
137 | { | |
138 | quit_force (NULL, 0); | |
139 | } | |
230d2906 | 140 | catch (const gdb_exception &except) |
ed3ef339 | 141 | { |
680d7fd5 | 142 | excp = gdbscm_scm_from_gdb_exception (unpack (except)); |
ed3ef339 DE |
143 | } |
144 | ||
680d7fd5 | 145 | gdbscm_throw (excp); |
ed3ef339 DE |
146 | } |
147 | \f | |
148 | /* Administrivia for type smobs. */ | |
149 | ||
150 | /* Helper function to hash a type_smob. */ | |
151 | ||
152 | static hashval_t | |
153 | tyscm_hash_type_smob (const void *p) | |
154 | { | |
9a3c8263 | 155 | const type_smob *t_smob = (const type_smob *) p; |
ed3ef339 DE |
156 | |
157 | return htab_hash_pointer (t_smob->type); | |
158 | } | |
159 | ||
160 | /* Helper function to compute equality of type_smobs. */ | |
161 | ||
162 | static int | |
163 | tyscm_eq_type_smob (const void *ap, const void *bp) | |
164 | { | |
9a3c8263 SM |
165 | const type_smob *a = (const type_smob *) ap; |
166 | const type_smob *b = (const type_smob *) bp; | |
ed3ef339 DE |
167 | |
168 | return (a->type == b->type | |
169 | && a->type != NULL); | |
170 | } | |
171 | ||
172 | /* Return the struct type pointer -> SCM mapping table. | |
173 | If type is owned by an objfile, the mapping table is created if necessary. | |
174 | Otherwise, type is not owned by an objfile, and we use | |
175 | global_types_map. */ | |
176 | ||
177 | static htab_t | |
178 | tyscm_type_map (struct type *type) | |
179 | { | |
6ac37371 | 180 | struct objfile *objfile = type->objfile_owner (); |
ed3ef339 DE |
181 | htab_t htab; |
182 | ||
183 | if (objfile == NULL) | |
184 | return global_types_map; | |
185 | ||
08b8a139 | 186 | htab = tyscm_objfile_data_key.get (objfile); |
ed3ef339 DE |
187 | if (htab == NULL) |
188 | { | |
189 | htab = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob, | |
190 | tyscm_eq_type_smob); | |
08b8a139 | 191 | tyscm_objfile_data_key.set (objfile, htab); |
ed3ef339 DE |
192 | } |
193 | ||
194 | return htab; | |
195 | } | |
196 | ||
ed3ef339 DE |
197 | /* The smob "free" function for <gdb:type>. */ |
198 | ||
199 | static size_t | |
200 | tyscm_free_type_smob (SCM self) | |
201 | { | |
202 | type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (self); | |
203 | ||
204 | if (t_smob->type != NULL) | |
205 | { | |
206 | htab_t htab = tyscm_type_map (t_smob->type); | |
207 | ||
208 | gdbscm_clear_eqable_gsmob_ptr_slot (htab, &t_smob->base); | |
209 | } | |
210 | ||
211 | /* Not necessary, done to catch bugs. */ | |
212 | t_smob->type = NULL; | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
217 | /* The smob "print" function for <gdb:type>. */ | |
218 | ||
219 | static int | |
220 | tyscm_print_type_smob (SCM self, SCM port, scm_print_state *pstate) | |
221 | { | |
222 | type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (self); | |
3ab692db | 223 | std::string name = tyscm_type_name (t_smob->type); |
ed3ef339 DE |
224 | |
225 | /* pstate->writingp = zero if invoked by display/~A, and nonzero if | |
226 | invoked by write/~S. What to do here may need to evolve. | |
227 | IWBN if we could pass an argument to format that would we could use | |
228 | instead of writingp. */ | |
229 | if (pstate->writingp) | |
230 | gdbscm_printf (port, "#<%s ", type_smob_name); | |
231 | ||
3ab692db | 232 | scm_puts (name.c_str (), port); |
ed3ef339 DE |
233 | |
234 | if (pstate->writingp) | |
235 | scm_puts (">", port); | |
236 | ||
237 | scm_remember_upto_here_1 (self); | |
238 | ||
239 | /* Non-zero means success. */ | |
240 | return 1; | |
241 | } | |
242 | ||
243 | /* The smob "equal?" function for <gdb:type>. */ | |
244 | ||
245 | static SCM | |
246 | tyscm_equal_p_type_smob (SCM type1_scm, SCM type2_scm) | |
247 | { | |
248 | type_smob *type1_smob, *type2_smob; | |
249 | struct type *type1, *type2; | |
894882e3 | 250 | bool result = false; |
ed3ef339 DE |
251 | |
252 | SCM_ASSERT_TYPE (tyscm_is_type (type1_scm), type1_scm, SCM_ARG1, FUNC_NAME, | |
253 | type_smob_name); | |
254 | SCM_ASSERT_TYPE (tyscm_is_type (type2_scm), type2_scm, SCM_ARG2, FUNC_NAME, | |
255 | type_smob_name); | |
256 | type1_smob = (type_smob *) SCM_SMOB_DATA (type1_scm); | |
257 | type2_smob = (type_smob *) SCM_SMOB_DATA (type2_scm); | |
258 | type1 = type1_smob->type; | |
259 | type2 = type2_smob->type; | |
260 | ||
680d7fd5 | 261 | gdbscm_gdb_exception exc {}; |
a70b8144 | 262 | try |
ed3ef339 DE |
263 | { |
264 | result = types_deeply_equal (type1, type2); | |
265 | } | |
230d2906 | 266 | catch (const gdb_exception &except) |
492d29ea | 267 | { |
680d7fd5 | 268 | exc = unpack (except); |
492d29ea | 269 | } |
ed3ef339 | 270 | |
680d7fd5 | 271 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
272 | return scm_from_bool (result); |
273 | } | |
274 | ||
275 | /* Low level routine to create a <gdb:type> object. */ | |
276 | ||
277 | static SCM | |
278 | tyscm_make_type_smob (void) | |
279 | { | |
280 | type_smob *t_smob = (type_smob *) | |
281 | scm_gc_malloc (sizeof (type_smob), type_smob_name); | |
282 | SCM t_scm; | |
283 | ||
284 | /* This must be filled in by the caller. */ | |
285 | t_smob->type = NULL; | |
286 | ||
287 | t_scm = scm_new_smob (type_smob_tag, (scm_t_bits) t_smob); | |
1254eefc | 288 | gdbscm_init_eqable_gsmob (&t_smob->base, t_scm); |
ed3ef339 DE |
289 | |
290 | return t_scm; | |
291 | } | |
292 | ||
293 | /* Return non-zero if SCM is a <gdb:type> object. */ | |
294 | ||
295 | int | |
296 | tyscm_is_type (SCM self) | |
297 | { | |
298 | return SCM_SMOB_PREDICATE (type_smob_tag, self); | |
299 | } | |
300 | ||
301 | /* (type? object) -> boolean */ | |
302 | ||
303 | static SCM | |
304 | gdbscm_type_p (SCM self) | |
305 | { | |
306 | return scm_from_bool (tyscm_is_type (self)); | |
307 | } | |
308 | ||
309 | /* Return the existing object that encapsulates TYPE, or create a new | |
310 | <gdb:type> object. */ | |
311 | ||
312 | SCM | |
313 | tyscm_scm_from_type (struct type *type) | |
314 | { | |
315 | htab_t htab; | |
316 | eqable_gdb_smob **slot; | |
317 | type_smob *t_smob, t_smob_for_lookup; | |
318 | SCM t_scm; | |
319 | ||
320 | /* If we've already created a gsmob for this type, return it. | |
321 | This makes types eq?-able. */ | |
322 | htab = tyscm_type_map (type); | |
323 | t_smob_for_lookup.type = type; | |
324 | slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &t_smob_for_lookup.base); | |
325 | if (*slot != NULL) | |
326 | return (*slot)->containing_scm; | |
327 | ||
328 | t_scm = tyscm_make_type_smob (); | |
329 | t_smob = (type_smob *) SCM_SMOB_DATA (t_scm); | |
330 | t_smob->type = type; | |
1254eefc | 331 | gdbscm_fill_eqable_gsmob_ptr_slot (slot, &t_smob->base); |
ed3ef339 DE |
332 | |
333 | return t_scm; | |
334 | } | |
335 | ||
336 | /* Returns the <gdb:type> object in SELF. | |
337 | Throws an exception if SELF is not a <gdb:type> object. */ | |
338 | ||
339 | static SCM | |
340 | tyscm_get_type_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
341 | { | |
342 | SCM_ASSERT_TYPE (tyscm_is_type (self), self, arg_pos, func_name, | |
343 | type_smob_name); | |
344 | ||
345 | return self; | |
346 | } | |
347 | ||
348 | /* Returns a pointer to the type smob of SELF. | |
349 | Throws an exception if SELF is not a <gdb:type> object. */ | |
350 | ||
351 | type_smob * | |
352 | tyscm_get_type_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
353 | { | |
354 | SCM t_scm = tyscm_get_type_arg_unsafe (self, arg_pos, func_name); | |
355 | type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (t_scm); | |
356 | ||
357 | return t_smob; | |
358 | } | |
359 | ||
a3a5fecc DE |
360 | /* Return the type field of T_SCM, an object of type <gdb:type>. |
361 | This exists so that we don't have to export the struct's contents. */ | |
362 | ||
363 | struct type * | |
364 | tyscm_scm_to_type (SCM t_scm) | |
365 | { | |
366 | type_smob *t_smob; | |
367 | ||
368 | gdb_assert (tyscm_is_type (t_scm)); | |
369 | t_smob = (type_smob *) SCM_SMOB_DATA (t_scm); | |
370 | return t_smob->type; | |
371 | } | |
372 | ||
08b8a139 | 373 | /* Helper function to make a deep copy of the type. */ |
ed3ef339 DE |
374 | |
375 | static int | |
376 | tyscm_copy_type_recursive (void **slot, void *info) | |
377 | { | |
378 | type_smob *t_smob = (type_smob *) *slot; | |
9a3c8263 | 379 | htab_t copied_types = (htab_t) info; |
5a1e8c7a DE |
380 | htab_t htab; |
381 | eqable_gdb_smob **new_slot; | |
382 | type_smob t_smob_for_lookup; | |
ed3ef339 | 383 | |
ed3ef339 | 384 | htab_empty (copied_types); |
bde539c2 | 385 | t_smob->type = copy_type_recursive (t_smob->type, copied_types); |
5a1e8c7a DE |
386 | |
387 | /* The eq?-hashtab that the type lived in is going away. | |
388 | Add the type to its new eq?-hashtab: Otherwise if/when the type is later | |
389 | garbage collected we'll assert-fail if the type isn't in the hashtab. | |
390 | PR 16612. | |
391 | ||
392 | Types now live in "arch space", and things like "char" that came from | |
393 | the objfile *could* be considered eq? with the arch "char" type. | |
394 | However, they weren't before the objfile got deleted, so making them | |
395 | eq? now is debatable. */ | |
396 | htab = tyscm_type_map (t_smob->type); | |
397 | t_smob_for_lookup.type = t_smob->type; | |
398 | new_slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &t_smob_for_lookup.base); | |
399 | gdb_assert (*new_slot == NULL); | |
400 | gdbscm_fill_eqable_gsmob_ptr_slot (new_slot, &t_smob->base); | |
401 | ||
ed3ef339 DE |
402 | return 1; |
403 | } | |
404 | ||
ed3ef339 DE |
405 | \f |
406 | /* Administrivia for field smobs. */ | |
407 | ||
ed3ef339 DE |
408 | /* The smob "print" function for <gdb:field>. */ |
409 | ||
410 | static int | |
411 | tyscm_print_field_smob (SCM self, SCM port, scm_print_state *pstate) | |
412 | { | |
413 | field_smob *f_smob = (field_smob *) SCM_SMOB_DATA (self); | |
414 | ||
415 | gdbscm_printf (port, "#<%s ", field_smob_name); | |
416 | scm_write (f_smob->type_scm, port); | |
417 | gdbscm_printf (port, " %d", f_smob->field_num); | |
418 | scm_puts (">", port); | |
419 | ||
420 | scm_remember_upto_here_1 (self); | |
421 | ||
422 | /* Non-zero means success. */ | |
423 | return 1; | |
424 | } | |
425 | ||
426 | /* Low level routine to create a <gdb:field> object for field FIELD_NUM | |
427 | of type TYPE_SCM. */ | |
428 | ||
429 | static SCM | |
430 | tyscm_make_field_smob (SCM type_scm, int field_num) | |
431 | { | |
432 | field_smob *f_smob = (field_smob *) | |
433 | scm_gc_malloc (sizeof (field_smob), field_smob_name); | |
434 | SCM result; | |
435 | ||
436 | f_smob->type_scm = type_scm; | |
437 | f_smob->field_num = field_num; | |
438 | result = scm_new_smob (field_smob_tag, (scm_t_bits) f_smob); | |
439 | gdbscm_init_gsmob (&f_smob->base); | |
440 | ||
441 | return result; | |
442 | } | |
443 | ||
444 | /* Return non-zero if SCM is a <gdb:field> object. */ | |
445 | ||
446 | static int | |
447 | tyscm_is_field (SCM self) | |
448 | { | |
449 | return SCM_SMOB_PREDICATE (field_smob_tag, self); | |
450 | } | |
451 | ||
452 | /* (field? object) -> boolean */ | |
453 | ||
454 | static SCM | |
455 | gdbscm_field_p (SCM self) | |
456 | { | |
457 | return scm_from_bool (tyscm_is_field (self)); | |
458 | } | |
459 | ||
460 | /* Create a new <gdb:field> object that encapsulates field FIELD_NUM | |
461 | in type TYPE_SCM. */ | |
462 | ||
463 | SCM | |
464 | tyscm_scm_from_field (SCM type_scm, int field_num) | |
465 | { | |
466 | return tyscm_make_field_smob (type_scm, field_num); | |
467 | } | |
468 | ||
469 | /* Returns the <gdb:field> object in SELF. | |
470 | Throws an exception if SELF is not a <gdb:field> object. */ | |
471 | ||
472 | static SCM | |
473 | tyscm_get_field_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
474 | { | |
475 | SCM_ASSERT_TYPE (tyscm_is_field (self), self, arg_pos, func_name, | |
476 | field_smob_name); | |
477 | ||
478 | return self; | |
479 | } | |
480 | ||
481 | /* Returns a pointer to the field smob of SELF. | |
482 | Throws an exception if SELF is not a <gdb:field> object. */ | |
483 | ||
484 | static field_smob * | |
485 | tyscm_get_field_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
486 | { | |
487 | SCM f_scm = tyscm_get_field_arg_unsafe (self, arg_pos, func_name); | |
488 | field_smob *f_smob = (field_smob *) SCM_SMOB_DATA (f_scm); | |
489 | ||
490 | return f_smob; | |
491 | } | |
492 | ||
493 | /* Returns a pointer to the type struct in F_SMOB | |
494 | (the type the field is in). */ | |
495 | ||
496 | static struct type * | |
497 | tyscm_field_smob_containing_type (field_smob *f_smob) | |
498 | { | |
499 | type_smob *t_smob; | |
500 | ||
501 | gdb_assert (tyscm_is_type (f_smob->type_scm)); | |
502 | t_smob = (type_smob *) SCM_SMOB_DATA (f_smob->type_scm); | |
503 | ||
504 | return t_smob->type; | |
505 | } | |
506 | ||
507 | /* Returns a pointer to the field struct of F_SMOB. */ | |
508 | ||
509 | static struct field * | |
510 | tyscm_field_smob_to_field (field_smob *f_smob) | |
511 | { | |
512 | struct type *type = tyscm_field_smob_containing_type (f_smob); | |
513 | ||
514 | /* This should be non-NULL by construction. */ | |
80fc5e77 | 515 | gdb_assert (type->fields () != NULL); |
ed3ef339 | 516 | |
ceacbf6e | 517 | return &type->field (f_smob->field_num); |
ed3ef339 DE |
518 | } |
519 | \f | |
520 | /* Type smob accessors. */ | |
521 | ||
522 | /* (type-code <gdb:type>) -> integer | |
523 | Return the code for this type. */ | |
524 | ||
525 | static SCM | |
526 | gdbscm_type_code (SCM self) | |
527 | { | |
528 | type_smob *t_smob | |
529 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
530 | struct type *type = t_smob->type; | |
531 | ||
78134374 | 532 | return scm_from_int (type->code ()); |
ed3ef339 DE |
533 | } |
534 | ||
535 | /* (type-fields <gdb:type>) -> list | |
536 | Return a list of all fields. Each element is a <gdb:field> object. | |
537 | This also supports arrays, we return a field list of one element, | |
538 | the range type. */ | |
539 | ||
540 | static SCM | |
541 | gdbscm_type_fields (SCM self) | |
542 | { | |
543 | type_smob *t_smob | |
544 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
545 | struct type *type = t_smob->type; | |
546 | struct type *containing_type; | |
547 | SCM containing_type_scm, result; | |
548 | int i; | |
549 | ||
550 | containing_type = tyscm_get_composite (type); | |
551 | if (containing_type == NULL) | |
552 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
553 | _(not_composite_error)); | |
554 | ||
555 | /* If SELF is a typedef or reference, we want the underlying type, | |
556 | which is what tyscm_get_composite returns. */ | |
557 | if (containing_type == type) | |
558 | containing_type_scm = self; | |
559 | else | |
560 | containing_type_scm = tyscm_scm_from_type (containing_type); | |
561 | ||
562 | result = SCM_EOL; | |
1f704f76 | 563 | for (i = 0; i < containing_type->num_fields (); ++i) |
ed3ef339 DE |
564 | result = scm_cons (tyscm_make_field_smob (containing_type_scm, i), result); |
565 | ||
566 | return scm_reverse_x (result, SCM_EOL); | |
567 | } | |
568 | ||
569 | /* (type-tag <gdb:type>) -> string | |
570 | Return the type's tag, or #f. */ | |
571 | ||
572 | static SCM | |
573 | gdbscm_type_tag (SCM self) | |
574 | { | |
575 | type_smob *t_smob | |
576 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
577 | struct type *type = t_smob->type; | |
e86ca25f | 578 | const char *tagname = nullptr; |
ed3ef339 | 579 | |
78134374 SM |
580 | if (type->code () == TYPE_CODE_STRUCT |
581 | || type->code () == TYPE_CODE_UNION | |
582 | || type->code () == TYPE_CODE_ENUM) | |
7d93a1e0 | 583 | tagname = type->name (); |
e86ca25f TT |
584 | |
585 | if (tagname == nullptr) | |
ed3ef339 | 586 | return SCM_BOOL_F; |
e86ca25f | 587 | return gdbscm_scm_from_c_string (tagname); |
ed3ef339 DE |
588 | } |
589 | ||
590 | /* (type-name <gdb:type>) -> string | |
591 | Return the type's name, or #f. */ | |
592 | ||
593 | static SCM | |
594 | gdbscm_type_name (SCM self) | |
595 | { | |
596 | type_smob *t_smob | |
597 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
598 | struct type *type = t_smob->type; | |
599 | ||
7d93a1e0 | 600 | if (!type->name ()) |
ed3ef339 | 601 | return SCM_BOOL_F; |
7d93a1e0 | 602 | return gdbscm_scm_from_c_string (type->name ()); |
ed3ef339 DE |
603 | } |
604 | ||
605 | /* (type-print-name <gdb:type>) -> string | |
606 | Return the print name of type. | |
607 | TODO: template support elided for now. */ | |
608 | ||
609 | static SCM | |
610 | gdbscm_type_print_name (SCM self) | |
611 | { | |
612 | type_smob *t_smob | |
613 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
614 | struct type *type = t_smob->type; | |
3ab692db PA |
615 | std::string thetype = tyscm_type_name (type); |
616 | SCM result = gdbscm_scm_from_c_string (thetype.c_str ()); | |
ed3ef339 DE |
617 | |
618 | return result; | |
619 | } | |
620 | ||
621 | /* (type-sizeof <gdb:type>) -> integer | |
622 | Return the size of the type represented by SELF, in bytes. */ | |
623 | ||
624 | static SCM | |
625 | gdbscm_type_sizeof (SCM self) | |
626 | { | |
627 | type_smob *t_smob | |
628 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
629 | struct type *type = t_smob->type; | |
ed3ef339 | 630 | |
a70b8144 | 631 | try |
ed3ef339 DE |
632 | { |
633 | check_typedef (type); | |
634 | } | |
230d2906 | 635 | catch (const gdb_exception &except) |
492d29ea PA |
636 | { |
637 | } | |
492d29ea | 638 | |
ed3ef339 DE |
639 | /* Ignore exceptions. */ |
640 | ||
df86565b | 641 | return scm_from_long (type->length ()); |
ed3ef339 DE |
642 | } |
643 | ||
644 | /* (type-strip-typedefs <gdb:type>) -> <gdb:type> | |
645 | Return the type, stripped of typedefs. */ | |
646 | ||
647 | static SCM | |
648 | gdbscm_type_strip_typedefs (SCM self) | |
649 | { | |
650 | type_smob *t_smob | |
651 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
652 | struct type *type = t_smob->type; | |
ed3ef339 | 653 | |
680d7fd5 | 654 | gdbscm_gdb_exception exc {}; |
a70b8144 | 655 | try |
ed3ef339 DE |
656 | { |
657 | type = check_typedef (type); | |
658 | } | |
230d2906 | 659 | catch (const gdb_exception &except) |
492d29ea | 660 | { |
680d7fd5 | 661 | exc = unpack (except); |
492d29ea | 662 | } |
ed3ef339 | 663 | |
680d7fd5 | 664 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
665 | return tyscm_scm_from_type (type); |
666 | } | |
667 | ||
668 | /* Strip typedefs and pointers/reference from a type. Then check that | |
669 | it is a struct, union, or enum type. If not, return NULL. */ | |
670 | ||
671 | static struct type * | |
672 | tyscm_get_composite (struct type *type) | |
673 | { | |
ed3ef339 DE |
674 | |
675 | for (;;) | |
676 | { | |
680d7fd5 | 677 | gdbscm_gdb_exception exc {}; |
a70b8144 | 678 | try |
ed3ef339 DE |
679 | { |
680 | type = check_typedef (type); | |
681 | } | |
230d2906 | 682 | catch (const gdb_exception &except) |
492d29ea | 683 | { |
680d7fd5 | 684 | exc = unpack (except); |
492d29ea | 685 | } |
ed3ef339 | 686 | |
680d7fd5 | 687 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
78134374 SM |
688 | if (type->code () != TYPE_CODE_PTR |
689 | && type->code () != TYPE_CODE_REF) | |
ed3ef339 | 690 | break; |
27710edb | 691 | type = type->target_type (); |
ed3ef339 DE |
692 | } |
693 | ||
694 | /* If this is not a struct, union, or enum type, raise TypeError | |
695 | exception. */ | |
78134374 SM |
696 | if (type->code () != TYPE_CODE_STRUCT |
697 | && type->code () != TYPE_CODE_UNION | |
698 | && type->code () != TYPE_CODE_ENUM) | |
ed3ef339 DE |
699 | return NULL; |
700 | ||
701 | return type; | |
702 | } | |
703 | ||
704 | /* Helper for tyscm_array and tyscm_vector. */ | |
705 | ||
706 | static SCM | |
707 | tyscm_array_1 (SCM self, SCM n1_scm, SCM n2_scm, int is_vector, | |
708 | const char *func_name) | |
709 | { | |
710 | type_smob *t_smob | |
711 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, func_name); | |
712 | struct type *type = t_smob->type; | |
713 | long n1, n2 = 0; | |
714 | struct type *array = NULL; | |
ed3ef339 DE |
715 | |
716 | gdbscm_parse_function_args (func_name, SCM_ARG2, NULL, "l|l", | |
717 | n1_scm, &n1, n2_scm, &n2); | |
718 | ||
719 | if (SCM_UNBNDP (n2_scm)) | |
720 | { | |
721 | n2 = n1; | |
722 | n1 = 0; | |
723 | } | |
724 | ||
e810d75b | 725 | if (n2 < n1 - 1) /* Note: An empty array has n2 == n1 - 1. */ |
ed3ef339 DE |
726 | { |
727 | gdbscm_out_of_range_error (func_name, SCM_ARG3, | |
728 | scm_cons (scm_from_long (n1), | |
729 | scm_from_long (n2)), | |
730 | _("Array length must not be negative")); | |
731 | } | |
732 | ||
680d7fd5 | 733 | gdbscm_gdb_exception exc {}; |
a70b8144 | 734 | try |
ed3ef339 DE |
735 | { |
736 | array = lookup_array_range_type (type, n1, n2); | |
737 | if (is_vector) | |
738 | make_vector_type (array); | |
739 | } | |
230d2906 | 740 | catch (const gdb_exception &except) |
492d29ea | 741 | { |
680d7fd5 | 742 | exc = unpack (except); |
492d29ea | 743 | } |
ed3ef339 | 744 | |
680d7fd5 | 745 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
746 | return tyscm_scm_from_type (array); |
747 | } | |
748 | ||
749 | /* (type-array <gdb:type> [low-bound] high-bound) -> <gdb:type> | |
750 | The array has indices [low-bound,high-bound]. | |
751 | If low-bound is not provided zero is used. | |
752 | Return an array type. | |
753 | ||
754 | IWBN if the one argument version specified a size, not the high bound. | |
755 | It's too easy to pass one argument thinking it is the size of the array. | |
756 | The current semantics are for compatibility with the Python version. | |
757 | Later we can add #:size. */ | |
758 | ||
759 | static SCM | |
760 | gdbscm_type_array (SCM self, SCM n1, SCM n2) | |
761 | { | |
762 | return tyscm_array_1 (self, n1, n2, 0, FUNC_NAME); | |
763 | } | |
764 | ||
765 | /* (type-vector <gdb:type> [low-bound] high-bound) -> <gdb:type> | |
766 | The array has indices [low-bound,high-bound]. | |
767 | If low-bound is not provided zero is used. | |
768 | Return a vector type. | |
769 | ||
770 | IWBN if the one argument version specified a size, not the high bound. | |
771 | It's too easy to pass one argument thinking it is the size of the array. | |
772 | The current semantics are for compatibility with the Python version. | |
773 | Later we can add #:size. */ | |
774 | ||
775 | static SCM | |
776 | gdbscm_type_vector (SCM self, SCM n1, SCM n2) | |
777 | { | |
778 | return tyscm_array_1 (self, n1, n2, 1, FUNC_NAME); | |
779 | } | |
780 | ||
781 | /* (type-pointer <gdb:type>) -> <gdb:type> | |
782 | Return a <gdb:type> object which represents a pointer to SELF. */ | |
783 | ||
784 | static SCM | |
785 | gdbscm_type_pointer (SCM self) | |
786 | { | |
787 | type_smob *t_smob | |
788 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
789 | struct type *type = t_smob->type; | |
ed3ef339 | 790 | |
680d7fd5 | 791 | gdbscm_gdb_exception exc {}; |
a70b8144 | 792 | try |
ed3ef339 DE |
793 | { |
794 | type = lookup_pointer_type (type); | |
795 | } | |
230d2906 | 796 | catch (const gdb_exception &except) |
492d29ea | 797 | { |
680d7fd5 | 798 | exc = unpack (except); |
492d29ea | 799 | } |
ed3ef339 | 800 | |
680d7fd5 | 801 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
802 | return tyscm_scm_from_type (type); |
803 | } | |
804 | ||
805 | /* (type-range <gdb:type>) -> (low high) | |
806 | Return the range of a type represented by SELF. The return type is | |
807 | a list. The first element is the low bound, and the second element | |
808 | is the high bound. */ | |
809 | ||
810 | static SCM | |
811 | gdbscm_type_range (SCM self) | |
812 | { | |
813 | type_smob *t_smob | |
814 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
815 | struct type *type = t_smob->type; | |
816 | SCM low_scm, high_scm; | |
817 | /* Initialize these to appease GCC warnings. */ | |
818 | LONGEST low = 0, high = 0; | |
819 | ||
78134374 SM |
820 | SCM_ASSERT_TYPE (type->code () == TYPE_CODE_ARRAY |
821 | || type->code () == TYPE_CODE_STRING | |
822 | || type->code () == TYPE_CODE_RANGE, | |
ed3ef339 DE |
823 | self, SCM_ARG1, FUNC_NAME, _("ranged type")); |
824 | ||
78134374 | 825 | switch (type->code ()) |
ed3ef339 DE |
826 | { |
827 | case TYPE_CODE_ARRAY: | |
828 | case TYPE_CODE_STRING: | |
ed3ef339 | 829 | case TYPE_CODE_RANGE: |
9c0fb734 | 830 | if (type->bounds ()->low.is_constant ()) |
e25d6d93 SM |
831 | low = type->bounds ()->low.const_val (); |
832 | else | |
833 | low = 0; | |
834 | ||
9c0fb734 | 835 | if (type->bounds ()->high.is_constant ()) |
e25d6d93 SM |
836 | high = type->bounds ()->high.const_val (); |
837 | else | |
838 | high = 0; | |
ed3ef339 DE |
839 | break; |
840 | } | |
841 | ||
842 | low_scm = gdbscm_scm_from_longest (low); | |
843 | high_scm = gdbscm_scm_from_longest (high); | |
844 | ||
845 | return scm_list_2 (low_scm, high_scm); | |
846 | } | |
847 | ||
848 | /* (type-reference <gdb:type>) -> <gdb:type> | |
849 | Return a <gdb:type> object which represents a reference to SELF. */ | |
850 | ||
851 | static SCM | |
852 | gdbscm_type_reference (SCM self) | |
853 | { | |
854 | type_smob *t_smob | |
855 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
856 | struct type *type = t_smob->type; | |
ed3ef339 | 857 | |
680d7fd5 | 858 | gdbscm_gdb_exception exc {}; |
a70b8144 | 859 | try |
ed3ef339 | 860 | { |
3b224330 | 861 | type = lookup_lvalue_reference_type (type); |
ed3ef339 | 862 | } |
230d2906 | 863 | catch (const gdb_exception &except) |
492d29ea | 864 | { |
680d7fd5 | 865 | exc = unpack (except); |
492d29ea | 866 | } |
ed3ef339 | 867 | |
680d7fd5 | 868 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
869 | return tyscm_scm_from_type (type); |
870 | } | |
871 | ||
872 | /* (type-target <gdb:type>) -> <gdb:type> | |
873 | Return a <gdb:type> object which represents the target type of SELF. */ | |
874 | ||
875 | static SCM | |
876 | gdbscm_type_target (SCM self) | |
877 | { | |
878 | type_smob *t_smob | |
879 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
880 | struct type *type = t_smob->type; | |
881 | ||
27710edb | 882 | SCM_ASSERT (type->target_type (), self, SCM_ARG1, FUNC_NAME); |
ed3ef339 | 883 | |
27710edb | 884 | return tyscm_scm_from_type (type->target_type ()); |
ed3ef339 DE |
885 | } |
886 | ||
887 | /* (type-const <gdb:type>) -> <gdb:type> | |
888 | Return a const-qualified type variant. */ | |
889 | ||
890 | static SCM | |
891 | gdbscm_type_const (SCM self) | |
892 | { | |
893 | type_smob *t_smob | |
894 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
895 | struct type *type = t_smob->type; | |
ed3ef339 | 896 | |
680d7fd5 | 897 | gdbscm_gdb_exception exc {}; |
a70b8144 | 898 | try |
ed3ef339 DE |
899 | { |
900 | type = make_cv_type (1, 0, type, NULL); | |
901 | } | |
230d2906 | 902 | catch (const gdb_exception &except) |
492d29ea | 903 | { |
680d7fd5 | 904 | exc = unpack (except); |
492d29ea | 905 | } |
ed3ef339 | 906 | |
680d7fd5 | 907 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
908 | return tyscm_scm_from_type (type); |
909 | } | |
910 | ||
911 | /* (type-volatile <gdb:type>) -> <gdb:type> | |
912 | Return a volatile-qualified type variant. */ | |
913 | ||
914 | static SCM | |
915 | gdbscm_type_volatile (SCM self) | |
916 | { | |
917 | type_smob *t_smob | |
918 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
919 | struct type *type = t_smob->type; | |
ed3ef339 | 920 | |
680d7fd5 | 921 | gdbscm_gdb_exception exc {}; |
a70b8144 | 922 | try |
ed3ef339 DE |
923 | { |
924 | type = make_cv_type (0, 1, type, NULL); | |
925 | } | |
230d2906 | 926 | catch (const gdb_exception &except) |
492d29ea | 927 | { |
680d7fd5 | 928 | exc = unpack (except); |
492d29ea | 929 | } |
ed3ef339 | 930 | |
680d7fd5 | 931 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
932 | return tyscm_scm_from_type (type); |
933 | } | |
934 | ||
935 | /* (type-unqualified <gdb:type>) -> <gdb:type> | |
936 | Return an unqualified type variant. */ | |
937 | ||
938 | static SCM | |
939 | gdbscm_type_unqualified (SCM self) | |
940 | { | |
941 | type_smob *t_smob | |
942 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
943 | struct type *type = t_smob->type; | |
ed3ef339 | 944 | |
680d7fd5 | 945 | gdbscm_gdb_exception exc {}; |
a70b8144 | 946 | try |
ed3ef339 DE |
947 | { |
948 | type = make_cv_type (0, 0, type, NULL); | |
949 | } | |
230d2906 | 950 | catch (const gdb_exception &except) |
492d29ea | 951 | { |
680d7fd5 | 952 | exc = unpack (except); |
492d29ea | 953 | } |
ed3ef339 | 954 | |
680d7fd5 | 955 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
956 | return tyscm_scm_from_type (type); |
957 | } | |
958 | \f | |
959 | /* Field related accessors of types. */ | |
960 | ||
961 | /* (type-num-fields <gdb:type>) -> integer | |
962 | Return number of fields. */ | |
963 | ||
964 | static SCM | |
965 | gdbscm_type_num_fields (SCM self) | |
966 | { | |
967 | type_smob *t_smob | |
968 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
969 | struct type *type = t_smob->type; | |
970 | ||
971 | type = tyscm_get_composite (type); | |
972 | if (type == NULL) | |
973 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
974 | _(not_composite_error)); | |
975 | ||
1f704f76 | 976 | return scm_from_long (type->num_fields ()); |
ed3ef339 DE |
977 | } |
978 | ||
979 | /* (type-field <gdb:type> string) -> <gdb:field> | |
980 | Return the <gdb:field> object for the field named by the argument. */ | |
981 | ||
982 | static SCM | |
983 | gdbscm_type_field (SCM self, SCM field_scm) | |
984 | { | |
985 | type_smob *t_smob | |
986 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
987 | struct type *type = t_smob->type; | |
ed3ef339 DE |
988 | |
989 | SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME, | |
990 | _("string")); | |
991 | ||
992 | /* We want just fields of this type, not of base types, so instead of | |
993 | using lookup_struct_elt_type, portions of that function are | |
994 | copied here. */ | |
995 | ||
996 | type = tyscm_get_composite (type); | |
997 | if (type == NULL) | |
998 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
999 | _(not_composite_error)); | |
1000 | ||
4c693332 PA |
1001 | { |
1002 | gdb::unique_xmalloc_ptr<char> field = gdbscm_scm_to_c_string (field_scm); | |
ed3ef339 | 1003 | |
1f704f76 | 1004 | for (int i = 0; i < type->num_fields (); i++) |
4c693332 | 1005 | { |
33d16dd9 | 1006 | const char *t_field_name = type->field (i).name (); |
ed3ef339 | 1007 | |
4c693332 PA |
1008 | if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0)) |
1009 | { | |
1010 | field.reset (nullptr); | |
1011 | return tyscm_make_field_smob (self, i); | |
1012 | } | |
1013 | } | |
1014 | } | |
ed3ef339 DE |
1015 | |
1016 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, field_scm, | |
1017 | _("Unknown field")); | |
1018 | } | |
1019 | ||
1020 | /* (type-has-field? <gdb:type> string) -> boolean | |
1021 | Return boolean indicating if type SELF has FIELD_SCM (a string). */ | |
1022 | ||
1023 | static SCM | |
1024 | gdbscm_type_has_field_p (SCM self, SCM field_scm) | |
1025 | { | |
1026 | type_smob *t_smob | |
1027 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1028 | struct type *type = t_smob->type; | |
ed3ef339 DE |
1029 | |
1030 | SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME, | |
1031 | _("string")); | |
1032 | ||
1033 | /* We want just fields of this type, not of base types, so instead of | |
1034 | using lookup_struct_elt_type, portions of that function are | |
1035 | copied here. */ | |
1036 | ||
1037 | type = tyscm_get_composite (type); | |
1038 | if (type == NULL) | |
1039 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
1040 | _(not_composite_error)); | |
1041 | ||
4c693332 PA |
1042 | { |
1043 | gdb::unique_xmalloc_ptr<char> field | |
1044 | = gdbscm_scm_to_c_string (field_scm); | |
ed3ef339 | 1045 | |
1f704f76 | 1046 | for (int i = 0; i < type->num_fields (); i++) |
4c693332 | 1047 | { |
33d16dd9 | 1048 | const char *t_field_name = type->field (i).name (); |
ed3ef339 | 1049 | |
4c693332 | 1050 | if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0)) |
557e56be | 1051 | return SCM_BOOL_T; |
4c693332 PA |
1052 | } |
1053 | } | |
ed3ef339 DE |
1054 | |
1055 | return SCM_BOOL_F; | |
1056 | } | |
1057 | ||
1058 | /* (make-field-iterator <gdb:type>) -> <gdb:iterator> | |
1059 | Make a field iterator object. */ | |
1060 | ||
1061 | static SCM | |
1062 | gdbscm_make_field_iterator (SCM self) | |
1063 | { | |
1064 | type_smob *t_smob | |
1065 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1066 | struct type *type = t_smob->type; | |
1067 | struct type *containing_type; | |
1068 | SCM containing_type_scm; | |
1069 | ||
1070 | containing_type = tyscm_get_composite (type); | |
1071 | if (containing_type == NULL) | |
1072 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
1073 | _(not_composite_error)); | |
1074 | ||
1075 | /* If SELF is a typedef or reference, we want the underlying type, | |
1076 | which is what tyscm_get_composite returns. */ | |
1077 | if (containing_type == type) | |
1078 | containing_type_scm = self; | |
1079 | else | |
1080 | containing_type_scm = tyscm_scm_from_type (containing_type); | |
1081 | ||
1082 | return gdbscm_make_iterator (containing_type_scm, scm_from_int (0), | |
1083 | tyscm_next_field_x_proc); | |
1084 | } | |
1085 | ||
1086 | /* (type-next-field! <gdb:iterator>) -> <gdb:field> | |
1087 | Return the next field in the iteration through the list of fields of the | |
1088 | type, or (end-of-iteration). | |
1089 | SELF is a <gdb:iterator> object created by gdbscm_make_field_iterator. | |
1090 | This is the next! <gdb:iterator> function, not exported to the user. */ | |
1091 | ||
1092 | static SCM | |
1093 | gdbscm_type_next_field_x (SCM self) | |
1094 | { | |
1095 | iterator_smob *i_smob; | |
1096 | type_smob *t_smob; | |
1097 | struct type *type; | |
1098 | SCM it_scm, result, progress, object; | |
798a7429 | 1099 | int field; |
ed3ef339 DE |
1100 | |
1101 | it_scm = itscm_get_iterator_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1102 | i_smob = (iterator_smob *) SCM_SMOB_DATA (it_scm); | |
1103 | object = itscm_iterator_smob_object (i_smob); | |
1104 | progress = itscm_iterator_smob_progress (i_smob); | |
1105 | ||
1106 | SCM_ASSERT_TYPE (tyscm_is_type (object), object, | |
1107 | SCM_ARG1, FUNC_NAME, type_smob_name); | |
1108 | t_smob = (type_smob *) SCM_SMOB_DATA (object); | |
1109 | type = t_smob->type; | |
1110 | ||
1111 | SCM_ASSERT_TYPE (scm_is_signed_integer (progress, | |
1f704f76 | 1112 | 0, type->num_fields ()), |
ed3ef339 DE |
1113 | progress, SCM_ARG1, FUNC_NAME, _("integer")); |
1114 | field = scm_to_int (progress); | |
1115 | ||
1f704f76 | 1116 | if (field < type->num_fields ()) |
ed3ef339 DE |
1117 | { |
1118 | result = tyscm_make_field_smob (object, field); | |
1119 | itscm_set_iterator_smob_progress_x (i_smob, scm_from_int (field + 1)); | |
1120 | return result; | |
1121 | } | |
1122 | ||
1123 | return gdbscm_end_of_iteration (); | |
1124 | } | |
1125 | \f | |
1126 | /* Field smob accessors. */ | |
1127 | ||
1128 | /* (field-name <gdb:field>) -> string | |
1129 | Return the name of this field or #f if there isn't one. */ | |
1130 | ||
1131 | static SCM | |
1132 | gdbscm_field_name (SCM self) | |
1133 | { | |
1134 | field_smob *f_smob | |
1135 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1136 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1137 | ||
33d16dd9 SM |
1138 | if (field->name () != nullptr) |
1139 | return gdbscm_scm_from_c_string (field->name ()); | |
ed3ef339 DE |
1140 | return SCM_BOOL_F; |
1141 | } | |
1142 | ||
1143 | /* (field-type <gdb:field>) -> <gdb:type> | |
1144 | Return the <gdb:type> object of the field or #f if there isn't one. */ | |
1145 | ||
1146 | static SCM | |
1147 | gdbscm_field_type (SCM self) | |
1148 | { | |
1149 | field_smob *f_smob | |
1150 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1151 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1152 | ||
1153 | /* A field can have a NULL type in some situations. */ | |
b6cdac4b SM |
1154 | if (field->type ()) |
1155 | return tyscm_scm_from_type (field->type ()); | |
ed3ef339 DE |
1156 | return SCM_BOOL_F; |
1157 | } | |
1158 | ||
1159 | /* (field-enumval <gdb:field>) -> integer | |
1160 | For enum values, return its value as an integer. */ | |
1161 | ||
1162 | static SCM | |
1163 | gdbscm_field_enumval (SCM self) | |
1164 | { | |
1165 | field_smob *f_smob | |
1166 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1167 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1168 | struct type *type = tyscm_field_smob_containing_type (f_smob); | |
1169 | ||
78134374 | 1170 | SCM_ASSERT_TYPE (type->code () == TYPE_CODE_ENUM, |
ed3ef339 DE |
1171 | self, SCM_ARG1, FUNC_NAME, _("enum type")); |
1172 | ||
5d2038e3 | 1173 | return scm_from_long (field->loc_enumval ()); |
ed3ef339 DE |
1174 | } |
1175 | ||
1176 | /* (field-bitpos <gdb:field>) -> integer | |
1177 | For bitfields, return its offset in bits. */ | |
1178 | ||
1179 | static SCM | |
1180 | gdbscm_field_bitpos (SCM self) | |
1181 | { | |
1182 | field_smob *f_smob | |
1183 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1184 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1185 | struct type *type = tyscm_field_smob_containing_type (f_smob); | |
1186 | ||
78134374 | 1187 | SCM_ASSERT_TYPE (type->code () != TYPE_CODE_ENUM, |
ed3ef339 DE |
1188 | self, SCM_ARG1, FUNC_NAME, _("non-enum type")); |
1189 | ||
3a543e21 | 1190 | return scm_from_long (field->loc_bitpos ()); |
ed3ef339 DE |
1191 | } |
1192 | ||
1193 | /* (field-bitsize <gdb:field>) -> integer | |
1194 | Return the size of the field in bits. */ | |
1195 | ||
1196 | static SCM | |
1197 | gdbscm_field_bitsize (SCM self) | |
1198 | { | |
1199 | field_smob *f_smob | |
1200 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1201 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1202 | ||
3a543e21 | 1203 | return scm_from_long (field->loc_bitpos ()); |
ed3ef339 DE |
1204 | } |
1205 | ||
1206 | /* (field-artificial? <gdb:field>) -> boolean | |
1207 | Return #t if field is artificial. */ | |
1208 | ||
1209 | static SCM | |
1210 | gdbscm_field_artificial_p (SCM self) | |
1211 | { | |
1212 | field_smob *f_smob | |
1213 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1214 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1215 | ||
6c0f7493 | 1216 | return scm_from_bool (field->is_artificial ()); |
ed3ef339 DE |
1217 | } |
1218 | ||
1219 | /* (field-baseclass? <gdb:field>) -> boolean | |
1220 | Return #t if field is a baseclass. */ | |
1221 | ||
1222 | static SCM | |
1223 | gdbscm_field_baseclass_p (SCM self) | |
1224 | { | |
1225 | field_smob *f_smob | |
1226 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
ed3ef339 DE |
1227 | struct type *type = tyscm_field_smob_containing_type (f_smob); |
1228 | ||
78134374 | 1229 | if (type->code () == TYPE_CODE_STRUCT) |
ed3ef339 DE |
1230 | return scm_from_bool (f_smob->field_num < TYPE_N_BASECLASSES (type)); |
1231 | return SCM_BOOL_F; | |
1232 | } | |
1233 | \f | |
1234 | /* Return the type named TYPE_NAME in BLOCK. | |
1235 | Returns NULL if not found. | |
1236 | This routine does not throw an error. */ | |
1237 | ||
1238 | static struct type * | |
1239 | tyscm_lookup_typename (const char *type_name, const struct block *block) | |
1240 | { | |
1241 | struct type *type = NULL; | |
ed3ef339 | 1242 | |
a70b8144 | 1243 | try |
ed3ef339 | 1244 | { |
61012eef | 1245 | if (startswith (type_name, "struct ")) |
ed3ef339 | 1246 | type = lookup_struct (type_name + 7, NULL); |
61012eef | 1247 | else if (startswith (type_name, "union ")) |
ed3ef339 | 1248 | type = lookup_union (type_name + 6, NULL); |
61012eef | 1249 | else if (startswith (type_name, "enum ")) |
ed3ef339 DE |
1250 | type = lookup_enum (type_name + 5, NULL); |
1251 | else | |
b858499d | 1252 | type = lookup_typename (current_language, |
ed3ef339 DE |
1253 | type_name, block, 0); |
1254 | } | |
230d2906 | 1255 | catch (const gdb_exception &except) |
492d29ea PA |
1256 | { |
1257 | return NULL; | |
1258 | } | |
ed3ef339 DE |
1259 | |
1260 | return type; | |
1261 | } | |
1262 | ||
1263 | /* (lookup-type name [#:block <gdb:block>]) -> <gdb:type> | |
1264 | TODO: legacy template support left out until needed. */ | |
1265 | ||
1266 | static SCM | |
1267 | gdbscm_lookup_type (SCM name_scm, SCM rest) | |
1268 | { | |
1269 | SCM keywords[] = { block_keyword, SCM_BOOL_F }; | |
1270 | char *name; | |
1271 | SCM block_scm = SCM_BOOL_F; | |
1272 | int block_arg_pos = -1; | |
1273 | const struct block *block = NULL; | |
1274 | struct type *type; | |
1275 | ||
1276 | gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#O", | |
1277 | name_scm, &name, | |
1278 | rest, &block_arg_pos, &block_scm); | |
1279 | ||
1280 | if (block_arg_pos != -1) | |
1281 | { | |
1282 | SCM exception; | |
1283 | ||
1284 | block = bkscm_scm_to_block (block_scm, block_arg_pos, FUNC_NAME, | |
1285 | &exception); | |
1286 | if (block == NULL) | |
1287 | { | |
1288 | xfree (name); | |
1289 | gdbscm_throw (exception); | |
1290 | } | |
1291 | } | |
1292 | type = tyscm_lookup_typename (name, block); | |
1293 | xfree (name); | |
1294 | ||
1295 | if (type != NULL) | |
1296 | return tyscm_scm_from_type (type); | |
1297 | return SCM_BOOL_F; | |
1298 | } | |
1299 | \f | |
1300 | /* Initialize the Scheme type code. */ | |
1301 | ||
1302 | ||
1303 | static const scheme_integer_constant type_integer_constants[] = | |
1304 | { | |
4881fcd7 TT |
1305 | /* This is kept for backward compatibility. */ |
1306 | { "TYPE_CODE_BITSTRING", -1 }, | |
1307 | ||
1308 | #define OP(SYM) { #SYM, SYM }, | |
1309 | #include "type-codes.def" | |
1310 | #undef OP | |
ed3ef339 DE |
1311 | |
1312 | END_INTEGER_CONSTANTS | |
1313 | }; | |
1314 | ||
1315 | static const scheme_function type_functions[] = | |
1316 | { | |
72e02483 | 1317 | { "type?", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_p), |
ed3ef339 DE |
1318 | "\ |
1319 | Return #t if the object is a <gdb:type> object." }, | |
1320 | ||
72e02483 | 1321 | { "lookup-type", 1, 0, 1, as_a_scm_t_subr (gdbscm_lookup_type), |
ed3ef339 DE |
1322 | "\ |
1323 | Return the <gdb:type> object representing string or #f if not found.\n\ | |
1324 | If block is given then the type is looked for in that block.\n\ | |
1325 | \n\ | |
1326 | Arguments: string [#:block <gdb:block>]" }, | |
1327 | ||
72e02483 | 1328 | { "type-code", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_code), |
ed3ef339 DE |
1329 | "\ |
1330 | Return the code of the type" }, | |
1331 | ||
72e02483 | 1332 | { "type-tag", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_tag), |
ed3ef339 DE |
1333 | "\ |
1334 | Return the tag name of the type, or #f if there isn't one." }, | |
1335 | ||
72e02483 | 1336 | { "type-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_name), |
ed3ef339 DE |
1337 | "\ |
1338 | Return the name of the type as a string, or #f if there isn't one." }, | |
1339 | ||
72e02483 | 1340 | { "type-print-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_print_name), |
ed3ef339 DE |
1341 | "\ |
1342 | Return the print name of the type as a string." }, | |
1343 | ||
72e02483 | 1344 | { "type-sizeof", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_sizeof), |
ed3ef339 DE |
1345 | "\ |
1346 | Return the size of the type, in bytes." }, | |
1347 | ||
72e02483 PA |
1348 | { "type-strip-typedefs", 1, 0, 0, |
1349 | as_a_scm_t_subr (gdbscm_type_strip_typedefs), | |
ed3ef339 DE |
1350 | "\ |
1351 | Return a type formed by stripping the type of all typedefs." }, | |
1352 | ||
72e02483 | 1353 | { "type-array", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_array), |
ed3ef339 DE |
1354 | "\ |
1355 | Return a type representing an array of objects of the type.\n\ | |
1356 | \n\ | |
1357 | Arguments: <gdb:type> [low-bound] high-bound\n\ | |
1358 | If low-bound is not provided zero is used.\n\ | |
1359 | N.B. If only the high-bound parameter is specified, it is not\n\ | |
1360 | the array size.\n\ | |
1361 | Valid bounds for array indices are [low-bound,high-bound]." }, | |
1362 | ||
72e02483 | 1363 | { "type-vector", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_vector), |
ed3ef339 DE |
1364 | "\ |
1365 | Return a type representing a vector of objects of the type.\n\ | |
1366 | Vectors differ from arrays in that if the current language has C-style\n\ | |
1367 | arrays, vectors don't decay to a pointer to the first element.\n\ | |
1368 | They are first class values.\n\ | |
1369 | \n\ | |
1370 | Arguments: <gdb:type> [low-bound] high-bound\n\ | |
1371 | If low-bound is not provided zero is used.\n\ | |
1372 | N.B. If only the high-bound parameter is specified, it is not\n\ | |
1373 | the array size.\n\ | |
1374 | Valid bounds for array indices are [low-bound,high-bound]." }, | |
1375 | ||
72e02483 | 1376 | { "type-pointer", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_pointer), |
ed3ef339 DE |
1377 | "\ |
1378 | Return a type of pointer to the type." }, | |
1379 | ||
72e02483 | 1380 | { "type-range", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_range), |
ed3ef339 DE |
1381 | "\ |
1382 | Return (low high) representing the range for the type." }, | |
1383 | ||
72e02483 | 1384 | { "type-reference", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_reference), |
ed3ef339 DE |
1385 | "\ |
1386 | Return a type of reference to the type." }, | |
1387 | ||
72e02483 | 1388 | { "type-target", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_target), |
ed3ef339 DE |
1389 | "\ |
1390 | Return the target type of the type." }, | |
1391 | ||
72e02483 | 1392 | { "type-const", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_const), |
ed3ef339 DE |
1393 | "\ |
1394 | Return a const variant of the type." }, | |
1395 | ||
72e02483 | 1396 | { "type-volatile", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_volatile), |
ed3ef339 DE |
1397 | "\ |
1398 | Return a volatile variant of the type." }, | |
1399 | ||
72e02483 | 1400 | { "type-unqualified", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_unqualified), |
ed3ef339 DE |
1401 | "\ |
1402 | Return a variant of the type without const or volatile attributes." }, | |
1403 | ||
72e02483 | 1404 | { "type-num-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_num_fields), |
ed3ef339 DE |
1405 | "\ |
1406 | Return the number of fields of the type." }, | |
1407 | ||
72e02483 | 1408 | { "type-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_fields), |
ed3ef339 DE |
1409 | "\ |
1410 | Return the list of <gdb:field> objects of fields of the type." }, | |
1411 | ||
72e02483 PA |
1412 | { "make-field-iterator", 1, 0, 0, |
1413 | as_a_scm_t_subr (gdbscm_make_field_iterator), | |
ed3ef339 DE |
1414 | "\ |
1415 | Return a <gdb:iterator> object for iterating over the fields of the type." }, | |
1416 | ||
72e02483 | 1417 | { "type-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_field), |
ed3ef339 DE |
1418 | "\ |
1419 | Return the field named by string of the type.\n\ | |
1420 | \n\ | |
1421 | Arguments: <gdb:type> string" }, | |
1422 | ||
72e02483 | 1423 | { "type-has-field?", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_has_field_p), |
ed3ef339 DE |
1424 | "\ |
1425 | Return #t if the type has field named string.\n\ | |
1426 | \n\ | |
1427 | Arguments: <gdb:type> string" }, | |
1428 | ||
72e02483 | 1429 | { "field?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_p), |
ed3ef339 DE |
1430 | "\ |
1431 | Return #t if the object is a <gdb:field> object." }, | |
1432 | ||
72e02483 | 1433 | { "field-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_name), |
ed3ef339 DE |
1434 | "\ |
1435 | Return the name of the field." }, | |
1436 | ||
72e02483 | 1437 | { "field-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_type), |
ed3ef339 DE |
1438 | "\ |
1439 | Return the type of the field." }, | |
1440 | ||
72e02483 | 1441 | { "field-enumval", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_enumval), |
ed3ef339 DE |
1442 | "\ |
1443 | Return the enum value represented by the field." }, | |
1444 | ||
72e02483 | 1445 | { "field-bitpos", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitpos), |
ed3ef339 DE |
1446 | "\ |
1447 | Return the offset in bits of the field in its containing type." }, | |
1448 | ||
72e02483 | 1449 | { "field-bitsize", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitsize), |
ed3ef339 DE |
1450 | "\ |
1451 | Return the size of the field in bits." }, | |
1452 | ||
72e02483 | 1453 | { "field-artificial?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_artificial_p), |
ed3ef339 DE |
1454 | "\ |
1455 | Return #t if the field is artificial." }, | |
1456 | ||
72e02483 | 1457 | { "field-baseclass?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_baseclass_p), |
ed3ef339 DE |
1458 | "\ |
1459 | Return #t if the field is a baseclass." }, | |
1460 | ||
1461 | END_FUNCTIONS | |
1462 | }; | |
1463 | ||
1464 | void | |
1465 | gdbscm_initialize_types (void) | |
1466 | { | |
1467 | type_smob_tag = gdbscm_make_smob_type (type_smob_name, sizeof (type_smob)); | |
ed3ef339 DE |
1468 | scm_set_smob_free (type_smob_tag, tyscm_free_type_smob); |
1469 | scm_set_smob_print (type_smob_tag, tyscm_print_type_smob); | |
1470 | scm_set_smob_equalp (type_smob_tag, tyscm_equal_p_type_smob); | |
1471 | ||
1472 | field_smob_tag = gdbscm_make_smob_type (field_smob_name, | |
1473 | sizeof (field_smob)); | |
ed3ef339 DE |
1474 | scm_set_smob_print (field_smob_tag, tyscm_print_field_smob); |
1475 | ||
1476 | gdbscm_define_integer_constants (type_integer_constants, 1); | |
1477 | gdbscm_define_functions (type_functions, 1); | |
1478 | ||
1479 | /* This function is "private". */ | |
1480 | tyscm_next_field_x_proc | |
1481 | = scm_c_define_gsubr ("%type-next-field!", 1, 0, 0, | |
72e02483 | 1482 | as_a_scm_t_subr (gdbscm_type_next_field_x)); |
ed3ef339 DE |
1483 | scm_set_procedure_property_x (tyscm_next_field_x_proc, |
1484 | gdbscm_documentation_symbol, | |
1485 | gdbscm_scm_from_c_string ("\ | |
1486 | Internal function to assist the type fields iterator.")); | |
1487 | ||
1488 | block_keyword = scm_from_latin1_keyword ("block"); | |
1489 | ||
880ae75a AB |
1490 | global_types_map = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob, |
1491 | tyscm_eq_type_smob); | |
1492 | } |