Commit | Line | Data |
---|---|---|
ed3ef339 DE |
1 | /* Scheme interface to types. |
2 | ||
b811d2c2 | 3 | Copyright (C) 2008-2020 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 DE |
29 | #include "bcache.h" |
30 | #include "dwarf2loc.h" | |
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 | |
1913f160 DE |
37 | deleted. |
38 | The typedef for this struct is in guile-internal.h. */ | |
ed3ef339 | 39 | |
1913f160 | 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 | ||
55 | typedef struct | |
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; | |
65 | } field_smob; | |
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 | ||
85 | static const struct objfile_data *tyscm_objfile_data_key; | |
86 | ||
87 | /* Hash table to uniquify global (non-objfile-owned) types. */ | |
88 | static htab_t global_types_map; | |
89 | ||
90 | static struct type *tyscm_get_composite (struct type *type); | |
91 | ||
92 | /* Return the type field of T_SMOB. | |
93 | This exists so that we don't have to export the struct's contents. */ | |
94 | ||
95 | struct type * | |
96 | tyscm_type_smob_type (type_smob *t_smob) | |
97 | { | |
98 | return t_smob->type; | |
99 | } | |
100 | ||
3ab692db PA |
101 | /* Return the name of TYPE in expanded form. If there's an error |
102 | computing the name, throws the gdb exception with scm_throw. */ | |
ed3ef339 | 103 | |
3ab692db PA |
104 | static std::string |
105 | tyscm_type_name (struct type *type) | |
ed3ef339 | 106 | { |
680d7fd5 | 107 | SCM excp; |
a70b8144 | 108 | try |
ed3ef339 | 109 | { |
d7e74731 | 110 | string_file stb; |
ed3ef339 | 111 | |
d7e74731 PA |
112 | LA_PRINT_TYPE (type, "", &stb, -1, 0, &type_print_raw_options); |
113 | return std::move (stb.string ()); | |
ed3ef339 | 114 | } |
230d2906 | 115 | catch (const gdb_exception &except) |
ed3ef339 | 116 | { |
680d7fd5 | 117 | excp = gdbscm_scm_from_gdb_exception (unpack (except)); |
ed3ef339 DE |
118 | } |
119 | ||
680d7fd5 | 120 | gdbscm_throw (excp); |
ed3ef339 DE |
121 | } |
122 | \f | |
123 | /* Administrivia for type smobs. */ | |
124 | ||
125 | /* Helper function to hash a type_smob. */ | |
126 | ||
127 | static hashval_t | |
128 | tyscm_hash_type_smob (const void *p) | |
129 | { | |
9a3c8263 | 130 | const type_smob *t_smob = (const type_smob *) p; |
ed3ef339 DE |
131 | |
132 | return htab_hash_pointer (t_smob->type); | |
133 | } | |
134 | ||
135 | /* Helper function to compute equality of type_smobs. */ | |
136 | ||
137 | static int | |
138 | tyscm_eq_type_smob (const void *ap, const void *bp) | |
139 | { | |
9a3c8263 SM |
140 | const type_smob *a = (const type_smob *) ap; |
141 | const type_smob *b = (const type_smob *) bp; | |
ed3ef339 DE |
142 | |
143 | return (a->type == b->type | |
144 | && a->type != NULL); | |
145 | } | |
146 | ||
147 | /* Return the struct type pointer -> SCM mapping table. | |
148 | If type is owned by an objfile, the mapping table is created if necessary. | |
149 | Otherwise, type is not owned by an objfile, and we use | |
150 | global_types_map. */ | |
151 | ||
152 | static htab_t | |
153 | tyscm_type_map (struct type *type) | |
154 | { | |
155 | struct objfile *objfile = TYPE_OBJFILE (type); | |
156 | htab_t htab; | |
157 | ||
158 | if (objfile == NULL) | |
159 | return global_types_map; | |
160 | ||
9a3c8263 | 161 | htab = (htab_t) objfile_data (objfile, tyscm_objfile_data_key); |
ed3ef339 DE |
162 | if (htab == NULL) |
163 | { | |
164 | htab = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob, | |
165 | tyscm_eq_type_smob); | |
166 | set_objfile_data (objfile, tyscm_objfile_data_key, htab); | |
167 | } | |
168 | ||
169 | return htab; | |
170 | } | |
171 | ||
ed3ef339 DE |
172 | /* The smob "free" function for <gdb:type>. */ |
173 | ||
174 | static size_t | |
175 | tyscm_free_type_smob (SCM self) | |
176 | { | |
177 | type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (self); | |
178 | ||
179 | if (t_smob->type != NULL) | |
180 | { | |
181 | htab_t htab = tyscm_type_map (t_smob->type); | |
182 | ||
183 | gdbscm_clear_eqable_gsmob_ptr_slot (htab, &t_smob->base); | |
184 | } | |
185 | ||
186 | /* Not necessary, done to catch bugs. */ | |
187 | t_smob->type = NULL; | |
188 | ||
189 | return 0; | |
190 | } | |
191 | ||
192 | /* The smob "print" function for <gdb:type>. */ | |
193 | ||
194 | static int | |
195 | tyscm_print_type_smob (SCM self, SCM port, scm_print_state *pstate) | |
196 | { | |
197 | type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (self); | |
3ab692db | 198 | std::string name = tyscm_type_name (t_smob->type); |
ed3ef339 DE |
199 | |
200 | /* pstate->writingp = zero if invoked by display/~A, and nonzero if | |
201 | invoked by write/~S. What to do here may need to evolve. | |
202 | IWBN if we could pass an argument to format that would we could use | |
203 | instead of writingp. */ | |
204 | if (pstate->writingp) | |
205 | gdbscm_printf (port, "#<%s ", type_smob_name); | |
206 | ||
3ab692db | 207 | scm_puts (name.c_str (), port); |
ed3ef339 DE |
208 | |
209 | if (pstate->writingp) | |
210 | scm_puts (">", port); | |
211 | ||
212 | scm_remember_upto_here_1 (self); | |
213 | ||
214 | /* Non-zero means success. */ | |
215 | return 1; | |
216 | } | |
217 | ||
218 | /* The smob "equal?" function for <gdb:type>. */ | |
219 | ||
220 | static SCM | |
221 | tyscm_equal_p_type_smob (SCM type1_scm, SCM type2_scm) | |
222 | { | |
223 | type_smob *type1_smob, *type2_smob; | |
224 | struct type *type1, *type2; | |
894882e3 | 225 | bool result = false; |
ed3ef339 DE |
226 | |
227 | SCM_ASSERT_TYPE (tyscm_is_type (type1_scm), type1_scm, SCM_ARG1, FUNC_NAME, | |
228 | type_smob_name); | |
229 | SCM_ASSERT_TYPE (tyscm_is_type (type2_scm), type2_scm, SCM_ARG2, FUNC_NAME, | |
230 | type_smob_name); | |
231 | type1_smob = (type_smob *) SCM_SMOB_DATA (type1_scm); | |
232 | type2_smob = (type_smob *) SCM_SMOB_DATA (type2_scm); | |
233 | type1 = type1_smob->type; | |
234 | type2 = type2_smob->type; | |
235 | ||
680d7fd5 | 236 | gdbscm_gdb_exception exc {}; |
a70b8144 | 237 | try |
ed3ef339 DE |
238 | { |
239 | result = types_deeply_equal (type1, type2); | |
240 | } | |
230d2906 | 241 | catch (const gdb_exception &except) |
492d29ea | 242 | { |
680d7fd5 | 243 | exc = unpack (except); |
492d29ea | 244 | } |
ed3ef339 | 245 | |
680d7fd5 | 246 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
247 | return scm_from_bool (result); |
248 | } | |
249 | ||
250 | /* Low level routine to create a <gdb:type> object. */ | |
251 | ||
252 | static SCM | |
253 | tyscm_make_type_smob (void) | |
254 | { | |
255 | type_smob *t_smob = (type_smob *) | |
256 | scm_gc_malloc (sizeof (type_smob), type_smob_name); | |
257 | SCM t_scm; | |
258 | ||
259 | /* This must be filled in by the caller. */ | |
260 | t_smob->type = NULL; | |
261 | ||
262 | t_scm = scm_new_smob (type_smob_tag, (scm_t_bits) t_smob); | |
1254eefc | 263 | gdbscm_init_eqable_gsmob (&t_smob->base, t_scm); |
ed3ef339 DE |
264 | |
265 | return t_scm; | |
266 | } | |
267 | ||
268 | /* Return non-zero if SCM is a <gdb:type> object. */ | |
269 | ||
270 | int | |
271 | tyscm_is_type (SCM self) | |
272 | { | |
273 | return SCM_SMOB_PREDICATE (type_smob_tag, self); | |
274 | } | |
275 | ||
276 | /* (type? object) -> boolean */ | |
277 | ||
278 | static SCM | |
279 | gdbscm_type_p (SCM self) | |
280 | { | |
281 | return scm_from_bool (tyscm_is_type (self)); | |
282 | } | |
283 | ||
284 | /* Return the existing object that encapsulates TYPE, or create a new | |
285 | <gdb:type> object. */ | |
286 | ||
287 | SCM | |
288 | tyscm_scm_from_type (struct type *type) | |
289 | { | |
290 | htab_t htab; | |
291 | eqable_gdb_smob **slot; | |
292 | type_smob *t_smob, t_smob_for_lookup; | |
293 | SCM t_scm; | |
294 | ||
295 | /* If we've already created a gsmob for this type, return it. | |
296 | This makes types eq?-able. */ | |
297 | htab = tyscm_type_map (type); | |
298 | t_smob_for_lookup.type = type; | |
299 | slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &t_smob_for_lookup.base); | |
300 | if (*slot != NULL) | |
301 | return (*slot)->containing_scm; | |
302 | ||
303 | t_scm = tyscm_make_type_smob (); | |
304 | t_smob = (type_smob *) SCM_SMOB_DATA (t_scm); | |
305 | t_smob->type = type; | |
1254eefc | 306 | gdbscm_fill_eqable_gsmob_ptr_slot (slot, &t_smob->base); |
ed3ef339 DE |
307 | |
308 | return t_scm; | |
309 | } | |
310 | ||
311 | /* Returns the <gdb:type> object in SELF. | |
312 | Throws an exception if SELF is not a <gdb:type> object. */ | |
313 | ||
314 | static SCM | |
315 | tyscm_get_type_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
316 | { | |
317 | SCM_ASSERT_TYPE (tyscm_is_type (self), self, arg_pos, func_name, | |
318 | type_smob_name); | |
319 | ||
320 | return self; | |
321 | } | |
322 | ||
323 | /* Returns a pointer to the type smob of SELF. | |
324 | Throws an exception if SELF is not a <gdb:type> object. */ | |
325 | ||
326 | type_smob * | |
327 | tyscm_get_type_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
328 | { | |
329 | SCM t_scm = tyscm_get_type_arg_unsafe (self, arg_pos, func_name); | |
330 | type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (t_scm); | |
331 | ||
332 | return t_smob; | |
333 | } | |
334 | ||
a3a5fecc DE |
335 | /* Return the type field of T_SCM, an object of type <gdb:type>. |
336 | This exists so that we don't have to export the struct's contents. */ | |
337 | ||
338 | struct type * | |
339 | tyscm_scm_to_type (SCM t_scm) | |
340 | { | |
341 | type_smob *t_smob; | |
342 | ||
343 | gdb_assert (tyscm_is_type (t_scm)); | |
344 | t_smob = (type_smob *) SCM_SMOB_DATA (t_scm); | |
345 | return t_smob->type; | |
346 | } | |
347 | ||
ed3ef339 DE |
348 | /* Helper function for save_objfile_types to make a deep copy of the type. */ |
349 | ||
350 | static int | |
351 | tyscm_copy_type_recursive (void **slot, void *info) | |
352 | { | |
353 | type_smob *t_smob = (type_smob *) *slot; | |
9a3c8263 | 354 | htab_t copied_types = (htab_t) info; |
ed3ef339 | 355 | struct objfile *objfile = TYPE_OBJFILE (t_smob->type); |
5a1e8c7a DE |
356 | htab_t htab; |
357 | eqable_gdb_smob **new_slot; | |
358 | type_smob t_smob_for_lookup; | |
ed3ef339 DE |
359 | |
360 | gdb_assert (objfile != NULL); | |
361 | ||
362 | htab_empty (copied_types); | |
363 | t_smob->type = copy_type_recursive (objfile, t_smob->type, copied_types); | |
5a1e8c7a DE |
364 | |
365 | /* The eq?-hashtab that the type lived in is going away. | |
366 | Add the type to its new eq?-hashtab: Otherwise if/when the type is later | |
367 | garbage collected we'll assert-fail if the type isn't in the hashtab. | |
368 | PR 16612. | |
369 | ||
370 | Types now live in "arch space", and things like "char" that came from | |
371 | the objfile *could* be considered eq? with the arch "char" type. | |
372 | However, they weren't before the objfile got deleted, so making them | |
373 | eq? now is debatable. */ | |
374 | htab = tyscm_type_map (t_smob->type); | |
375 | t_smob_for_lookup.type = t_smob->type; | |
376 | new_slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &t_smob_for_lookup.base); | |
377 | gdb_assert (*new_slot == NULL); | |
378 | gdbscm_fill_eqable_gsmob_ptr_slot (new_slot, &t_smob->base); | |
379 | ||
ed3ef339 DE |
380 | return 1; |
381 | } | |
382 | ||
383 | /* Called when OBJFILE is about to be deleted. | |
384 | Make a copy of all types associated with OBJFILE. */ | |
385 | ||
386 | static void | |
387 | save_objfile_types (struct objfile *objfile, void *datum) | |
388 | { | |
9a3c8263 | 389 | htab_t htab = (htab_t) datum; |
ed3ef339 DE |
390 | htab_t copied_types; |
391 | ||
392 | if (!gdb_scheme_initialized) | |
393 | return; | |
394 | ||
395 | copied_types = create_copied_types_hash (objfile); | |
396 | ||
397 | if (htab != NULL) | |
398 | { | |
399 | htab_traverse_noresize (htab, tyscm_copy_type_recursive, copied_types); | |
400 | htab_delete (htab); | |
401 | } | |
402 | ||
403 | htab_delete (copied_types); | |
404 | } | |
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. */ | |
515 | gdb_assert (TYPE_FIELDS (type) != NULL); | |
516 | ||
517 | return &TYPE_FIELD (type, f_smob->field_num); | |
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 | ||
532 | return scm_from_int (TYPE_CODE (type)); | |
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; | |
563 | for (i = 0; i < TYPE_NFIELDS (containing_type); ++i) | |
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 | |
e86ca25f TT |
580 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT |
581 | || TYPE_CODE (type) == TYPE_CODE_UNION | |
582 | || TYPE_CODE (type) == TYPE_CODE_ENUM) | |
583 | tagname = TYPE_NAME (type); | |
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 | ||
600 | if (!TYPE_NAME (type)) | |
601 | return SCM_BOOL_F; | |
602 | return gdbscm_scm_from_c_string (TYPE_NAME (type)); | |
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 | ||
641 | return scm_from_long (TYPE_LENGTH (type)); | |
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); |
ed3ef339 DE |
688 | if (TYPE_CODE (type) != TYPE_CODE_PTR |
689 | && TYPE_CODE (type) != TYPE_CODE_REF) | |
690 | break; | |
691 | type = TYPE_TARGET_TYPE (type); | |
692 | } | |
693 | ||
694 | /* If this is not a struct, union, or enum type, raise TypeError | |
695 | exception. */ | |
696 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT | |
697 | && TYPE_CODE (type) != TYPE_CODE_UNION | |
698 | && TYPE_CODE (type) != TYPE_CODE_ENUM) | |
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 | ||
820 | SCM_ASSERT_TYPE (TYPE_CODE (type) == TYPE_CODE_ARRAY | |
821 | || TYPE_CODE (type) == TYPE_CODE_STRING | |
822 | || TYPE_CODE (type) == TYPE_CODE_RANGE, | |
823 | self, SCM_ARG1, FUNC_NAME, _("ranged type")); | |
824 | ||
825 | switch (TYPE_CODE (type)) | |
826 | { | |
827 | case TYPE_CODE_ARRAY: | |
828 | case TYPE_CODE_STRING: | |
829 | low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)); | |
830 | high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (type)); | |
831 | break; | |
832 | case TYPE_CODE_RANGE: | |
833 | low = TYPE_LOW_BOUND (type); | |
834 | high = TYPE_HIGH_BOUND (type); | |
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 | ||
972 | return scm_from_long (TYPE_NFIELDS (type)); | |
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 | |
4c693332 PA |
1000 | for (int i = 0; i < TYPE_NFIELDS (type); i++) |
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 | |
4c693332 PA |
1042 | for (int i = 0; i < TYPE_NFIELDS (type); i++) |
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, | |
1108 | 0, TYPE_NFIELDS (type)), | |
1109 | progress, SCM_ARG1, FUNC_NAME, _("integer")); | |
1110 | field = scm_to_int (progress); | |
1111 | ||
1112 | if (field < TYPE_NFIELDS (type)) | |
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. */ | |
1150 | if (FIELD_TYPE (*field)) | |
1151 | return tyscm_scm_from_type (FIELD_TYPE (*field)); | |
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 | ||
1166 | SCM_ASSERT_TYPE (TYPE_CODE (type) == TYPE_CODE_ENUM, | |
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 | ||
1183 | SCM_ASSERT_TYPE (TYPE_CODE (type) != TYPE_CODE_ENUM, | |
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 | ||
4753d33b | 1225 | if (TYPE_CODE (type) == 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), | |
1321 | X (TYPE_CODE_CHAR), | |
1322 | X (TYPE_CODE_BOOL), | |
1323 | X (TYPE_CODE_COMPLEX), | |
1324 | X (TYPE_CODE_TYPEDEF), | |
1325 | X (TYPE_CODE_NAMESPACE), | |
1326 | X (TYPE_CODE_DECFLOAT), | |
1327 | X (TYPE_CODE_INTERNAL_FUNCTION), | |
1328 | #undef X | |
1329 | ||
1330 | END_INTEGER_CONSTANTS | |
1331 | }; | |
1332 | ||
1333 | static const scheme_function type_functions[] = | |
1334 | { | |
72e02483 | 1335 | { "type?", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_p), |
ed3ef339 DE |
1336 | "\ |
1337 | Return #t if the object is a <gdb:type> object." }, | |
1338 | ||
72e02483 | 1339 | { "lookup-type", 1, 0, 1, as_a_scm_t_subr (gdbscm_lookup_type), |
ed3ef339 DE |
1340 | "\ |
1341 | Return the <gdb:type> object representing string or #f if not found.\n\ | |
1342 | If block is given then the type is looked for in that block.\n\ | |
1343 | \n\ | |
1344 | Arguments: string [#:block <gdb:block>]" }, | |
1345 | ||
72e02483 | 1346 | { "type-code", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_code), |
ed3ef339 DE |
1347 | "\ |
1348 | Return the code of the type" }, | |
1349 | ||
72e02483 | 1350 | { "type-tag", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_tag), |
ed3ef339 DE |
1351 | "\ |
1352 | Return the tag name of the type, or #f if there isn't one." }, | |
1353 | ||
72e02483 | 1354 | { "type-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_name), |
ed3ef339 DE |
1355 | "\ |
1356 | Return the name of the type as a string, or #f if there isn't one." }, | |
1357 | ||
72e02483 | 1358 | { "type-print-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_print_name), |
ed3ef339 DE |
1359 | "\ |
1360 | Return the print name of the type as a string." }, | |
1361 | ||
72e02483 | 1362 | { "type-sizeof", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_sizeof), |
ed3ef339 DE |
1363 | "\ |
1364 | Return the size of the type, in bytes." }, | |
1365 | ||
72e02483 PA |
1366 | { "type-strip-typedefs", 1, 0, 0, |
1367 | as_a_scm_t_subr (gdbscm_type_strip_typedefs), | |
ed3ef339 DE |
1368 | "\ |
1369 | Return a type formed by stripping the type of all typedefs." }, | |
1370 | ||
72e02483 | 1371 | { "type-array", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_array), |
ed3ef339 DE |
1372 | "\ |
1373 | Return a type representing an array of objects of the type.\n\ | |
1374 | \n\ | |
1375 | Arguments: <gdb:type> [low-bound] high-bound\n\ | |
1376 | If low-bound is not provided zero is used.\n\ | |
1377 | N.B. If only the high-bound parameter is specified, it is not\n\ | |
1378 | the array size.\n\ | |
1379 | Valid bounds for array indices are [low-bound,high-bound]." }, | |
1380 | ||
72e02483 | 1381 | { "type-vector", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_vector), |
ed3ef339 DE |
1382 | "\ |
1383 | Return a type representing a vector of objects of the type.\n\ | |
1384 | Vectors differ from arrays in that if the current language has C-style\n\ | |
1385 | arrays, vectors don't decay to a pointer to the first element.\n\ | |
1386 | They are first class values.\n\ | |
1387 | \n\ | |
1388 | Arguments: <gdb:type> [low-bound] high-bound\n\ | |
1389 | If low-bound is not provided zero is used.\n\ | |
1390 | N.B. If only the high-bound parameter is specified, it is not\n\ | |
1391 | the array size.\n\ | |
1392 | Valid bounds for array indices are [low-bound,high-bound]." }, | |
1393 | ||
72e02483 | 1394 | { "type-pointer", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_pointer), |
ed3ef339 DE |
1395 | "\ |
1396 | Return a type of pointer to the type." }, | |
1397 | ||
72e02483 | 1398 | { "type-range", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_range), |
ed3ef339 DE |
1399 | "\ |
1400 | Return (low high) representing the range for the type." }, | |
1401 | ||
72e02483 | 1402 | { "type-reference", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_reference), |
ed3ef339 DE |
1403 | "\ |
1404 | Return a type of reference to the type." }, | |
1405 | ||
72e02483 | 1406 | { "type-target", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_target), |
ed3ef339 DE |
1407 | "\ |
1408 | Return the target type of the type." }, | |
1409 | ||
72e02483 | 1410 | { "type-const", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_const), |
ed3ef339 DE |
1411 | "\ |
1412 | Return a const variant of the type." }, | |
1413 | ||
72e02483 | 1414 | { "type-volatile", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_volatile), |
ed3ef339 DE |
1415 | "\ |
1416 | Return a volatile variant of the type." }, | |
1417 | ||
72e02483 | 1418 | { "type-unqualified", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_unqualified), |
ed3ef339 DE |
1419 | "\ |
1420 | Return a variant of the type without const or volatile attributes." }, | |
1421 | ||
72e02483 | 1422 | { "type-num-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_num_fields), |
ed3ef339 DE |
1423 | "\ |
1424 | Return the number of fields of the type." }, | |
1425 | ||
72e02483 | 1426 | { "type-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_fields), |
ed3ef339 DE |
1427 | "\ |
1428 | Return the list of <gdb:field> objects of fields of the type." }, | |
1429 | ||
72e02483 PA |
1430 | { "make-field-iterator", 1, 0, 0, |
1431 | as_a_scm_t_subr (gdbscm_make_field_iterator), | |
ed3ef339 DE |
1432 | "\ |
1433 | Return a <gdb:iterator> object for iterating over the fields of the type." }, | |
1434 | ||
72e02483 | 1435 | { "type-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_field), |
ed3ef339 DE |
1436 | "\ |
1437 | Return the field named by string of the type.\n\ | |
1438 | \n\ | |
1439 | Arguments: <gdb:type> string" }, | |
1440 | ||
72e02483 | 1441 | { "type-has-field?", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_has_field_p), |
ed3ef339 DE |
1442 | "\ |
1443 | Return #t if the type has field named string.\n\ | |
1444 | \n\ | |
1445 | Arguments: <gdb:type> string" }, | |
1446 | ||
72e02483 | 1447 | { "field?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_p), |
ed3ef339 DE |
1448 | "\ |
1449 | Return #t if the object is a <gdb:field> object." }, | |
1450 | ||
72e02483 | 1451 | { "field-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_name), |
ed3ef339 DE |
1452 | "\ |
1453 | Return the name of the field." }, | |
1454 | ||
72e02483 | 1455 | { "field-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_type), |
ed3ef339 DE |
1456 | "\ |
1457 | Return the type of the field." }, | |
1458 | ||
72e02483 | 1459 | { "field-enumval", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_enumval), |
ed3ef339 DE |
1460 | "\ |
1461 | Return the enum value represented by the field." }, | |
1462 | ||
72e02483 | 1463 | { "field-bitpos", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitpos), |
ed3ef339 DE |
1464 | "\ |
1465 | Return the offset in bits of the field in its containing type." }, | |
1466 | ||
72e02483 | 1467 | { "field-bitsize", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitsize), |
ed3ef339 DE |
1468 | "\ |
1469 | Return the size of the field in bits." }, | |
1470 | ||
72e02483 | 1471 | { "field-artificial?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_artificial_p), |
ed3ef339 DE |
1472 | "\ |
1473 | Return #t if the field is artificial." }, | |
1474 | ||
72e02483 | 1475 | { "field-baseclass?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_baseclass_p), |
ed3ef339 DE |
1476 | "\ |
1477 | Return #t if the field is a baseclass." }, | |
1478 | ||
1479 | END_FUNCTIONS | |
1480 | }; | |
1481 | ||
1482 | void | |
1483 | gdbscm_initialize_types (void) | |
1484 | { | |
1485 | type_smob_tag = gdbscm_make_smob_type (type_smob_name, sizeof (type_smob)); | |
ed3ef339 DE |
1486 | scm_set_smob_free (type_smob_tag, tyscm_free_type_smob); |
1487 | scm_set_smob_print (type_smob_tag, tyscm_print_type_smob); | |
1488 | scm_set_smob_equalp (type_smob_tag, tyscm_equal_p_type_smob); | |
1489 | ||
1490 | field_smob_tag = gdbscm_make_smob_type (field_smob_name, | |
1491 | sizeof (field_smob)); | |
ed3ef339 DE |
1492 | scm_set_smob_print (field_smob_tag, tyscm_print_field_smob); |
1493 | ||
1494 | gdbscm_define_integer_constants (type_integer_constants, 1); | |
1495 | gdbscm_define_functions (type_functions, 1); | |
1496 | ||
1497 | /* This function is "private". */ | |
1498 | tyscm_next_field_x_proc | |
1499 | = scm_c_define_gsubr ("%type-next-field!", 1, 0, 0, | |
72e02483 | 1500 | as_a_scm_t_subr (gdbscm_type_next_field_x)); |
ed3ef339 DE |
1501 | scm_set_procedure_property_x (tyscm_next_field_x_proc, |
1502 | gdbscm_documentation_symbol, | |
1503 | gdbscm_scm_from_c_string ("\ | |
1504 | Internal function to assist the type fields iterator.")); | |
1505 | ||
1506 | block_keyword = scm_from_latin1_keyword ("block"); | |
1507 | ||
1508 | /* Register an objfile "free" callback so we can properly copy types | |
1509 | associated with the objfile when it's about to be deleted. */ | |
1510 | tyscm_objfile_data_key | |
1511 | = register_objfile_data_with_cleanup (save_objfile_types, NULL); | |
1512 | ||
1513 | global_types_map = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob, | |
1514 | tyscm_eq_type_smob); | |
1515 | } |