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