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 | 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 | |
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 | |
391 | if (!gdb_scheme_initialized) | |
392 | return; | |
393 | ||
6108fd18 | 394 | htab_up copied_types = create_copied_types_hash (objfile); |
ed3ef339 DE |
395 | |
396 | if (htab != NULL) | |
397 | { | |
6108fd18 | 398 | htab_traverse_noresize (htab, tyscm_copy_type_recursive, copied_types.get ()); |
ed3ef339 DE |
399 | htab_delete (htab); |
400 | } | |
ed3ef339 DE |
401 | } |
402 | \f | |
403 | /* Administrivia for field smobs. */ | |
404 | ||
ed3ef339 DE |
405 | /* The smob "print" function for <gdb:field>. */ |
406 | ||
407 | static int | |
408 | tyscm_print_field_smob (SCM self, SCM port, scm_print_state *pstate) | |
409 | { | |
410 | field_smob *f_smob = (field_smob *) SCM_SMOB_DATA (self); | |
411 | ||
412 | gdbscm_printf (port, "#<%s ", field_smob_name); | |
413 | scm_write (f_smob->type_scm, port); | |
414 | gdbscm_printf (port, " %d", f_smob->field_num); | |
415 | scm_puts (">", port); | |
416 | ||
417 | scm_remember_upto_here_1 (self); | |
418 | ||
419 | /* Non-zero means success. */ | |
420 | return 1; | |
421 | } | |
422 | ||
423 | /* Low level routine to create a <gdb:field> object for field FIELD_NUM | |
424 | of type TYPE_SCM. */ | |
425 | ||
426 | static SCM | |
427 | tyscm_make_field_smob (SCM type_scm, int field_num) | |
428 | { | |
429 | field_smob *f_smob = (field_smob *) | |
430 | scm_gc_malloc (sizeof (field_smob), field_smob_name); | |
431 | SCM result; | |
432 | ||
433 | f_smob->type_scm = type_scm; | |
434 | f_smob->field_num = field_num; | |
435 | result = scm_new_smob (field_smob_tag, (scm_t_bits) f_smob); | |
436 | gdbscm_init_gsmob (&f_smob->base); | |
437 | ||
438 | return result; | |
439 | } | |
440 | ||
441 | /* Return non-zero if SCM is a <gdb:field> object. */ | |
442 | ||
443 | static int | |
444 | tyscm_is_field (SCM self) | |
445 | { | |
446 | return SCM_SMOB_PREDICATE (field_smob_tag, self); | |
447 | } | |
448 | ||
449 | /* (field? object) -> boolean */ | |
450 | ||
451 | static SCM | |
452 | gdbscm_field_p (SCM self) | |
453 | { | |
454 | return scm_from_bool (tyscm_is_field (self)); | |
455 | } | |
456 | ||
457 | /* Create a new <gdb:field> object that encapsulates field FIELD_NUM | |
458 | in type TYPE_SCM. */ | |
459 | ||
460 | SCM | |
461 | tyscm_scm_from_field (SCM type_scm, int field_num) | |
462 | { | |
463 | return tyscm_make_field_smob (type_scm, field_num); | |
464 | } | |
465 | ||
466 | /* Returns the <gdb:field> object in SELF. | |
467 | Throws an exception if SELF is not a <gdb:field> object. */ | |
468 | ||
469 | static SCM | |
470 | tyscm_get_field_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
471 | { | |
472 | SCM_ASSERT_TYPE (tyscm_is_field (self), self, arg_pos, func_name, | |
473 | field_smob_name); | |
474 | ||
475 | return self; | |
476 | } | |
477 | ||
478 | /* Returns a pointer to the field smob of SELF. | |
479 | Throws an exception if SELF is not a <gdb:field> object. */ | |
480 | ||
481 | static field_smob * | |
482 | tyscm_get_field_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name) | |
483 | { | |
484 | SCM f_scm = tyscm_get_field_arg_unsafe (self, arg_pos, func_name); | |
485 | field_smob *f_smob = (field_smob *) SCM_SMOB_DATA (f_scm); | |
486 | ||
487 | return f_smob; | |
488 | } | |
489 | ||
490 | /* Returns a pointer to the type struct in F_SMOB | |
491 | (the type the field is in). */ | |
492 | ||
493 | static struct type * | |
494 | tyscm_field_smob_containing_type (field_smob *f_smob) | |
495 | { | |
496 | type_smob *t_smob; | |
497 | ||
498 | gdb_assert (tyscm_is_type (f_smob->type_scm)); | |
499 | t_smob = (type_smob *) SCM_SMOB_DATA (f_smob->type_scm); | |
500 | ||
501 | return t_smob->type; | |
502 | } | |
503 | ||
504 | /* Returns a pointer to the field struct of F_SMOB. */ | |
505 | ||
506 | static struct field * | |
507 | tyscm_field_smob_to_field (field_smob *f_smob) | |
508 | { | |
509 | struct type *type = tyscm_field_smob_containing_type (f_smob); | |
510 | ||
511 | /* This should be non-NULL by construction. */ | |
80fc5e77 | 512 | gdb_assert (type->fields () != NULL); |
ed3ef339 | 513 | |
ceacbf6e | 514 | return &type->field (f_smob->field_num); |
ed3ef339 DE |
515 | } |
516 | \f | |
517 | /* Type smob accessors. */ | |
518 | ||
519 | /* (type-code <gdb:type>) -> integer | |
520 | Return the code for this type. */ | |
521 | ||
522 | static SCM | |
523 | gdbscm_type_code (SCM self) | |
524 | { | |
525 | type_smob *t_smob | |
526 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
527 | struct type *type = t_smob->type; | |
528 | ||
78134374 | 529 | return scm_from_int (type->code ()); |
ed3ef339 DE |
530 | } |
531 | ||
532 | /* (type-fields <gdb:type>) -> list | |
533 | Return a list of all fields. Each element is a <gdb:field> object. | |
534 | This also supports arrays, we return a field list of one element, | |
535 | the range type. */ | |
536 | ||
537 | static SCM | |
538 | gdbscm_type_fields (SCM self) | |
539 | { | |
540 | type_smob *t_smob | |
541 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
542 | struct type *type = t_smob->type; | |
543 | struct type *containing_type; | |
544 | SCM containing_type_scm, result; | |
545 | int i; | |
546 | ||
547 | containing_type = tyscm_get_composite (type); | |
548 | if (containing_type == NULL) | |
549 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
550 | _(not_composite_error)); | |
551 | ||
552 | /* If SELF is a typedef or reference, we want the underlying type, | |
553 | which is what tyscm_get_composite returns. */ | |
554 | if (containing_type == type) | |
555 | containing_type_scm = self; | |
556 | else | |
557 | containing_type_scm = tyscm_scm_from_type (containing_type); | |
558 | ||
559 | result = SCM_EOL; | |
1f704f76 | 560 | for (i = 0; i < containing_type->num_fields (); ++i) |
ed3ef339 DE |
561 | result = scm_cons (tyscm_make_field_smob (containing_type_scm, i), result); |
562 | ||
563 | return scm_reverse_x (result, SCM_EOL); | |
564 | } | |
565 | ||
566 | /* (type-tag <gdb:type>) -> string | |
567 | Return the type's tag, or #f. */ | |
568 | ||
569 | static SCM | |
570 | gdbscm_type_tag (SCM self) | |
571 | { | |
572 | type_smob *t_smob | |
573 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
574 | struct type *type = t_smob->type; | |
e86ca25f | 575 | const char *tagname = nullptr; |
ed3ef339 | 576 | |
78134374 SM |
577 | if (type->code () == TYPE_CODE_STRUCT |
578 | || type->code () == TYPE_CODE_UNION | |
579 | || type->code () == TYPE_CODE_ENUM) | |
7d93a1e0 | 580 | tagname = type->name (); |
e86ca25f TT |
581 | |
582 | if (tagname == nullptr) | |
ed3ef339 | 583 | return SCM_BOOL_F; |
e86ca25f | 584 | return gdbscm_scm_from_c_string (tagname); |
ed3ef339 DE |
585 | } |
586 | ||
587 | /* (type-name <gdb:type>) -> string | |
588 | Return the type's name, or #f. */ | |
589 | ||
590 | static SCM | |
591 | gdbscm_type_name (SCM self) | |
592 | { | |
593 | type_smob *t_smob | |
594 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
595 | struct type *type = t_smob->type; | |
596 | ||
7d93a1e0 | 597 | if (!type->name ()) |
ed3ef339 | 598 | return SCM_BOOL_F; |
7d93a1e0 | 599 | return gdbscm_scm_from_c_string (type->name ()); |
ed3ef339 DE |
600 | } |
601 | ||
602 | /* (type-print-name <gdb:type>) -> string | |
603 | Return the print name of type. | |
604 | TODO: template support elided for now. */ | |
605 | ||
606 | static SCM | |
607 | gdbscm_type_print_name (SCM self) | |
608 | { | |
609 | type_smob *t_smob | |
610 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
611 | struct type *type = t_smob->type; | |
3ab692db PA |
612 | std::string thetype = tyscm_type_name (type); |
613 | SCM result = gdbscm_scm_from_c_string (thetype.c_str ()); | |
ed3ef339 DE |
614 | |
615 | return result; | |
616 | } | |
617 | ||
618 | /* (type-sizeof <gdb:type>) -> integer | |
619 | Return the size of the type represented by SELF, in bytes. */ | |
620 | ||
621 | static SCM | |
622 | gdbscm_type_sizeof (SCM self) | |
623 | { | |
624 | type_smob *t_smob | |
625 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
626 | struct type *type = t_smob->type; | |
ed3ef339 | 627 | |
a70b8144 | 628 | try |
ed3ef339 DE |
629 | { |
630 | check_typedef (type); | |
631 | } | |
230d2906 | 632 | catch (const gdb_exception &except) |
492d29ea PA |
633 | { |
634 | } | |
492d29ea | 635 | |
ed3ef339 DE |
636 | /* Ignore exceptions. */ |
637 | ||
638 | return scm_from_long (TYPE_LENGTH (type)); | |
639 | } | |
640 | ||
641 | /* (type-strip-typedefs <gdb:type>) -> <gdb:type> | |
642 | Return the type, stripped of typedefs. */ | |
643 | ||
644 | static SCM | |
645 | gdbscm_type_strip_typedefs (SCM self) | |
646 | { | |
647 | type_smob *t_smob | |
648 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
649 | struct type *type = t_smob->type; | |
ed3ef339 | 650 | |
680d7fd5 | 651 | gdbscm_gdb_exception exc {}; |
a70b8144 | 652 | try |
ed3ef339 DE |
653 | { |
654 | type = check_typedef (type); | |
655 | } | |
230d2906 | 656 | catch (const gdb_exception &except) |
492d29ea | 657 | { |
680d7fd5 | 658 | exc = unpack (except); |
492d29ea | 659 | } |
ed3ef339 | 660 | |
680d7fd5 | 661 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
662 | return tyscm_scm_from_type (type); |
663 | } | |
664 | ||
665 | /* Strip typedefs and pointers/reference from a type. Then check that | |
666 | it is a struct, union, or enum type. If not, return NULL. */ | |
667 | ||
668 | static struct type * | |
669 | tyscm_get_composite (struct type *type) | |
670 | { | |
ed3ef339 DE |
671 | |
672 | for (;;) | |
673 | { | |
680d7fd5 | 674 | gdbscm_gdb_exception exc {}; |
a70b8144 | 675 | try |
ed3ef339 DE |
676 | { |
677 | type = check_typedef (type); | |
678 | } | |
230d2906 | 679 | catch (const gdb_exception &except) |
492d29ea | 680 | { |
680d7fd5 | 681 | exc = unpack (except); |
492d29ea | 682 | } |
ed3ef339 | 683 | |
680d7fd5 | 684 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
78134374 SM |
685 | if (type->code () != TYPE_CODE_PTR |
686 | && type->code () != TYPE_CODE_REF) | |
ed3ef339 DE |
687 | break; |
688 | type = TYPE_TARGET_TYPE (type); | |
689 | } | |
690 | ||
691 | /* If this is not a struct, union, or enum type, raise TypeError | |
692 | exception. */ | |
78134374 SM |
693 | if (type->code () != TYPE_CODE_STRUCT |
694 | && type->code () != TYPE_CODE_UNION | |
695 | && type->code () != TYPE_CODE_ENUM) | |
ed3ef339 DE |
696 | return NULL; |
697 | ||
698 | return type; | |
699 | } | |
700 | ||
701 | /* Helper for tyscm_array and tyscm_vector. */ | |
702 | ||
703 | static SCM | |
704 | tyscm_array_1 (SCM self, SCM n1_scm, SCM n2_scm, int is_vector, | |
705 | const char *func_name) | |
706 | { | |
707 | type_smob *t_smob | |
708 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, func_name); | |
709 | struct type *type = t_smob->type; | |
710 | long n1, n2 = 0; | |
711 | struct type *array = NULL; | |
ed3ef339 DE |
712 | |
713 | gdbscm_parse_function_args (func_name, SCM_ARG2, NULL, "l|l", | |
714 | n1_scm, &n1, n2_scm, &n2); | |
715 | ||
716 | if (SCM_UNBNDP (n2_scm)) | |
717 | { | |
718 | n2 = n1; | |
719 | n1 = 0; | |
720 | } | |
721 | ||
e810d75b | 722 | if (n2 < n1 - 1) /* Note: An empty array has n2 == n1 - 1. */ |
ed3ef339 DE |
723 | { |
724 | gdbscm_out_of_range_error (func_name, SCM_ARG3, | |
725 | scm_cons (scm_from_long (n1), | |
726 | scm_from_long (n2)), | |
727 | _("Array length must not be negative")); | |
728 | } | |
729 | ||
680d7fd5 | 730 | gdbscm_gdb_exception exc {}; |
a70b8144 | 731 | try |
ed3ef339 DE |
732 | { |
733 | array = lookup_array_range_type (type, n1, n2); | |
734 | if (is_vector) | |
735 | make_vector_type (array); | |
736 | } | |
230d2906 | 737 | catch (const gdb_exception &except) |
492d29ea | 738 | { |
680d7fd5 | 739 | exc = unpack (except); |
492d29ea | 740 | } |
ed3ef339 | 741 | |
680d7fd5 | 742 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
743 | return tyscm_scm_from_type (array); |
744 | } | |
745 | ||
746 | /* (type-array <gdb:type> [low-bound] high-bound) -> <gdb:type> | |
747 | The array has indices [low-bound,high-bound]. | |
748 | If low-bound is not provided zero is used. | |
749 | Return an array type. | |
750 | ||
751 | IWBN if the one argument version specified a size, not the high bound. | |
752 | It's too easy to pass one argument thinking it is the size of the array. | |
753 | The current semantics are for compatibility with the Python version. | |
754 | Later we can add #:size. */ | |
755 | ||
756 | static SCM | |
757 | gdbscm_type_array (SCM self, SCM n1, SCM n2) | |
758 | { | |
759 | return tyscm_array_1 (self, n1, n2, 0, FUNC_NAME); | |
760 | } | |
761 | ||
762 | /* (type-vector <gdb:type> [low-bound] high-bound) -> <gdb:type> | |
763 | The array has indices [low-bound,high-bound]. | |
764 | If low-bound is not provided zero is used. | |
765 | Return a vector type. | |
766 | ||
767 | IWBN if the one argument version specified a size, not the high bound. | |
768 | It's too easy to pass one argument thinking it is the size of the array. | |
769 | The current semantics are for compatibility with the Python version. | |
770 | Later we can add #:size. */ | |
771 | ||
772 | static SCM | |
773 | gdbscm_type_vector (SCM self, SCM n1, SCM n2) | |
774 | { | |
775 | return tyscm_array_1 (self, n1, n2, 1, FUNC_NAME); | |
776 | } | |
777 | ||
778 | /* (type-pointer <gdb:type>) -> <gdb:type> | |
779 | Return a <gdb:type> object which represents a pointer to SELF. */ | |
780 | ||
781 | static SCM | |
782 | gdbscm_type_pointer (SCM self) | |
783 | { | |
784 | type_smob *t_smob | |
785 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
786 | struct type *type = t_smob->type; | |
ed3ef339 | 787 | |
680d7fd5 | 788 | gdbscm_gdb_exception exc {}; |
a70b8144 | 789 | try |
ed3ef339 DE |
790 | { |
791 | type = lookup_pointer_type (type); | |
792 | } | |
230d2906 | 793 | catch (const gdb_exception &except) |
492d29ea | 794 | { |
680d7fd5 | 795 | exc = unpack (except); |
492d29ea | 796 | } |
ed3ef339 | 797 | |
680d7fd5 | 798 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
799 | return tyscm_scm_from_type (type); |
800 | } | |
801 | ||
802 | /* (type-range <gdb:type>) -> (low high) | |
803 | Return the range of a type represented by SELF. The return type is | |
804 | a list. The first element is the low bound, and the second element | |
805 | is the high bound. */ | |
806 | ||
807 | static SCM | |
808 | gdbscm_type_range (SCM self) | |
809 | { | |
810 | type_smob *t_smob | |
811 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
812 | struct type *type = t_smob->type; | |
813 | SCM low_scm, high_scm; | |
814 | /* Initialize these to appease GCC warnings. */ | |
815 | LONGEST low = 0, high = 0; | |
816 | ||
78134374 SM |
817 | SCM_ASSERT_TYPE (type->code () == TYPE_CODE_ARRAY |
818 | || type->code () == TYPE_CODE_STRING | |
819 | || type->code () == TYPE_CODE_RANGE, | |
ed3ef339 DE |
820 | self, SCM_ARG1, FUNC_NAME, _("ranged type")); |
821 | ||
78134374 | 822 | switch (type->code ()) |
ed3ef339 DE |
823 | { |
824 | case TYPE_CODE_ARRAY: | |
825 | case TYPE_CODE_STRING: | |
ed3ef339 | 826 | case TYPE_CODE_RANGE: |
5537ddd0 SM |
827 | low = type->bounds ()->low.const_val (); |
828 | high = type->bounds ()->high.const_val (); | |
ed3ef339 DE |
829 | break; |
830 | } | |
831 | ||
832 | low_scm = gdbscm_scm_from_longest (low); | |
833 | high_scm = gdbscm_scm_from_longest (high); | |
834 | ||
835 | return scm_list_2 (low_scm, high_scm); | |
836 | } | |
837 | ||
838 | /* (type-reference <gdb:type>) -> <gdb:type> | |
839 | Return a <gdb:type> object which represents a reference to SELF. */ | |
840 | ||
841 | static SCM | |
842 | gdbscm_type_reference (SCM self) | |
843 | { | |
844 | type_smob *t_smob | |
845 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
846 | struct type *type = t_smob->type; | |
ed3ef339 | 847 | |
680d7fd5 | 848 | gdbscm_gdb_exception exc {}; |
a70b8144 | 849 | try |
ed3ef339 | 850 | { |
3b224330 | 851 | type = lookup_lvalue_reference_type (type); |
ed3ef339 | 852 | } |
230d2906 | 853 | catch (const gdb_exception &except) |
492d29ea | 854 | { |
680d7fd5 | 855 | exc = unpack (except); |
492d29ea | 856 | } |
ed3ef339 | 857 | |
680d7fd5 | 858 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
859 | return tyscm_scm_from_type (type); |
860 | } | |
861 | ||
862 | /* (type-target <gdb:type>) -> <gdb:type> | |
863 | Return a <gdb:type> object which represents the target type of SELF. */ | |
864 | ||
865 | static SCM | |
866 | gdbscm_type_target (SCM self) | |
867 | { | |
868 | type_smob *t_smob | |
869 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
870 | struct type *type = t_smob->type; | |
871 | ||
872 | SCM_ASSERT (TYPE_TARGET_TYPE (type), self, SCM_ARG1, FUNC_NAME); | |
873 | ||
874 | return tyscm_scm_from_type (TYPE_TARGET_TYPE (type)); | |
875 | } | |
876 | ||
877 | /* (type-const <gdb:type>) -> <gdb:type> | |
878 | Return a const-qualified type variant. */ | |
879 | ||
880 | static SCM | |
881 | gdbscm_type_const (SCM self) | |
882 | { | |
883 | type_smob *t_smob | |
884 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
885 | struct type *type = t_smob->type; | |
ed3ef339 | 886 | |
680d7fd5 | 887 | gdbscm_gdb_exception exc {}; |
a70b8144 | 888 | try |
ed3ef339 DE |
889 | { |
890 | type = make_cv_type (1, 0, type, NULL); | |
891 | } | |
230d2906 | 892 | catch (const gdb_exception &except) |
492d29ea | 893 | { |
680d7fd5 | 894 | exc = unpack (except); |
492d29ea | 895 | } |
ed3ef339 | 896 | |
680d7fd5 | 897 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
898 | return tyscm_scm_from_type (type); |
899 | } | |
900 | ||
901 | /* (type-volatile <gdb:type>) -> <gdb:type> | |
902 | Return a volatile-qualified type variant. */ | |
903 | ||
904 | static SCM | |
905 | gdbscm_type_volatile (SCM self) | |
906 | { | |
907 | type_smob *t_smob | |
908 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
909 | struct type *type = t_smob->type; | |
ed3ef339 | 910 | |
680d7fd5 | 911 | gdbscm_gdb_exception exc {}; |
a70b8144 | 912 | try |
ed3ef339 DE |
913 | { |
914 | type = make_cv_type (0, 1, type, NULL); | |
915 | } | |
230d2906 | 916 | catch (const gdb_exception &except) |
492d29ea | 917 | { |
680d7fd5 | 918 | exc = unpack (except); |
492d29ea | 919 | } |
ed3ef339 | 920 | |
680d7fd5 | 921 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
922 | return tyscm_scm_from_type (type); |
923 | } | |
924 | ||
925 | /* (type-unqualified <gdb:type>) -> <gdb:type> | |
926 | Return an unqualified type variant. */ | |
927 | ||
928 | static SCM | |
929 | gdbscm_type_unqualified (SCM self) | |
930 | { | |
931 | type_smob *t_smob | |
932 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
933 | struct type *type = t_smob->type; | |
ed3ef339 | 934 | |
680d7fd5 | 935 | gdbscm_gdb_exception exc {}; |
a70b8144 | 936 | try |
ed3ef339 DE |
937 | { |
938 | type = make_cv_type (0, 0, type, NULL); | |
939 | } | |
230d2906 | 940 | catch (const gdb_exception &except) |
492d29ea | 941 | { |
680d7fd5 | 942 | exc = unpack (except); |
492d29ea | 943 | } |
ed3ef339 | 944 | |
680d7fd5 | 945 | GDBSCM_HANDLE_GDB_EXCEPTION (exc); |
ed3ef339 DE |
946 | return tyscm_scm_from_type (type); |
947 | } | |
948 | \f | |
949 | /* Field related accessors of types. */ | |
950 | ||
951 | /* (type-num-fields <gdb:type>) -> integer | |
952 | Return number of fields. */ | |
953 | ||
954 | static SCM | |
955 | gdbscm_type_num_fields (SCM self) | |
956 | { | |
957 | type_smob *t_smob | |
958 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
959 | struct type *type = t_smob->type; | |
960 | ||
961 | type = tyscm_get_composite (type); | |
962 | if (type == NULL) | |
963 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
964 | _(not_composite_error)); | |
965 | ||
1f704f76 | 966 | return scm_from_long (type->num_fields ()); |
ed3ef339 DE |
967 | } |
968 | ||
969 | /* (type-field <gdb:type> string) -> <gdb:field> | |
970 | Return the <gdb:field> object for the field named by the argument. */ | |
971 | ||
972 | static SCM | |
973 | gdbscm_type_field (SCM self, SCM field_scm) | |
974 | { | |
975 | type_smob *t_smob | |
976 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
977 | struct type *type = t_smob->type; | |
ed3ef339 DE |
978 | |
979 | SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME, | |
980 | _("string")); | |
981 | ||
982 | /* We want just fields of this type, not of base types, so instead of | |
983 | using lookup_struct_elt_type, portions of that function are | |
984 | copied here. */ | |
985 | ||
986 | type = tyscm_get_composite (type); | |
987 | if (type == NULL) | |
988 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
989 | _(not_composite_error)); | |
990 | ||
4c693332 PA |
991 | { |
992 | gdb::unique_xmalloc_ptr<char> field = gdbscm_scm_to_c_string (field_scm); | |
ed3ef339 | 993 | |
1f704f76 | 994 | for (int i = 0; i < type->num_fields (); i++) |
4c693332 PA |
995 | { |
996 | const char *t_field_name = TYPE_FIELD_NAME (type, i); | |
ed3ef339 | 997 | |
4c693332 PA |
998 | if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0)) |
999 | { | |
1000 | field.reset (nullptr); | |
1001 | return tyscm_make_field_smob (self, i); | |
1002 | } | |
1003 | } | |
1004 | } | |
ed3ef339 DE |
1005 | |
1006 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, field_scm, | |
1007 | _("Unknown field")); | |
1008 | } | |
1009 | ||
1010 | /* (type-has-field? <gdb:type> string) -> boolean | |
1011 | Return boolean indicating if type SELF has FIELD_SCM (a string). */ | |
1012 | ||
1013 | static SCM | |
1014 | gdbscm_type_has_field_p (SCM self, SCM field_scm) | |
1015 | { | |
1016 | type_smob *t_smob | |
1017 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1018 | struct type *type = t_smob->type; | |
ed3ef339 DE |
1019 | |
1020 | SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME, | |
1021 | _("string")); | |
1022 | ||
1023 | /* We want just fields of this type, not of base types, so instead of | |
1024 | using lookup_struct_elt_type, portions of that function are | |
1025 | copied here. */ | |
1026 | ||
1027 | type = tyscm_get_composite (type); | |
1028 | if (type == NULL) | |
1029 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
1030 | _(not_composite_error)); | |
1031 | ||
4c693332 PA |
1032 | { |
1033 | gdb::unique_xmalloc_ptr<char> field | |
1034 | = gdbscm_scm_to_c_string (field_scm); | |
ed3ef339 | 1035 | |
1f704f76 | 1036 | for (int i = 0; i < type->num_fields (); i++) |
4c693332 PA |
1037 | { |
1038 | const char *t_field_name = TYPE_FIELD_NAME (type, i); | |
ed3ef339 | 1039 | |
4c693332 | 1040 | if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0)) |
557e56be | 1041 | return SCM_BOOL_T; |
4c693332 PA |
1042 | } |
1043 | } | |
ed3ef339 DE |
1044 | |
1045 | return SCM_BOOL_F; | |
1046 | } | |
1047 | ||
1048 | /* (make-field-iterator <gdb:type>) -> <gdb:iterator> | |
1049 | Make a field iterator object. */ | |
1050 | ||
1051 | static SCM | |
1052 | gdbscm_make_field_iterator (SCM self) | |
1053 | { | |
1054 | type_smob *t_smob | |
1055 | = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1056 | struct type *type = t_smob->type; | |
1057 | struct type *containing_type; | |
1058 | SCM containing_type_scm; | |
1059 | ||
1060 | containing_type = tyscm_get_composite (type); | |
1061 | if (containing_type == NULL) | |
1062 | gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self, | |
1063 | _(not_composite_error)); | |
1064 | ||
1065 | /* If SELF is a typedef or reference, we want the underlying type, | |
1066 | which is what tyscm_get_composite returns. */ | |
1067 | if (containing_type == type) | |
1068 | containing_type_scm = self; | |
1069 | else | |
1070 | containing_type_scm = tyscm_scm_from_type (containing_type); | |
1071 | ||
1072 | return gdbscm_make_iterator (containing_type_scm, scm_from_int (0), | |
1073 | tyscm_next_field_x_proc); | |
1074 | } | |
1075 | ||
1076 | /* (type-next-field! <gdb:iterator>) -> <gdb:field> | |
1077 | Return the next field in the iteration through the list of fields of the | |
1078 | type, or (end-of-iteration). | |
1079 | SELF is a <gdb:iterator> object created by gdbscm_make_field_iterator. | |
1080 | This is the next! <gdb:iterator> function, not exported to the user. */ | |
1081 | ||
1082 | static SCM | |
1083 | gdbscm_type_next_field_x (SCM self) | |
1084 | { | |
1085 | iterator_smob *i_smob; | |
1086 | type_smob *t_smob; | |
1087 | struct type *type; | |
1088 | SCM it_scm, result, progress, object; | |
798a7429 | 1089 | int field; |
ed3ef339 DE |
1090 | |
1091 | it_scm = itscm_get_iterator_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1092 | i_smob = (iterator_smob *) SCM_SMOB_DATA (it_scm); | |
1093 | object = itscm_iterator_smob_object (i_smob); | |
1094 | progress = itscm_iterator_smob_progress (i_smob); | |
1095 | ||
1096 | SCM_ASSERT_TYPE (tyscm_is_type (object), object, | |
1097 | SCM_ARG1, FUNC_NAME, type_smob_name); | |
1098 | t_smob = (type_smob *) SCM_SMOB_DATA (object); | |
1099 | type = t_smob->type; | |
1100 | ||
1101 | SCM_ASSERT_TYPE (scm_is_signed_integer (progress, | |
1f704f76 | 1102 | 0, type->num_fields ()), |
ed3ef339 DE |
1103 | progress, SCM_ARG1, FUNC_NAME, _("integer")); |
1104 | field = scm_to_int (progress); | |
1105 | ||
1f704f76 | 1106 | if (field < type->num_fields ()) |
ed3ef339 DE |
1107 | { |
1108 | result = tyscm_make_field_smob (object, field); | |
1109 | itscm_set_iterator_smob_progress_x (i_smob, scm_from_int (field + 1)); | |
1110 | return result; | |
1111 | } | |
1112 | ||
1113 | return gdbscm_end_of_iteration (); | |
1114 | } | |
1115 | \f | |
1116 | /* Field smob accessors. */ | |
1117 | ||
1118 | /* (field-name <gdb:field>) -> string | |
1119 | Return the name of this field or #f if there isn't one. */ | |
1120 | ||
1121 | static SCM | |
1122 | gdbscm_field_name (SCM self) | |
1123 | { | |
1124 | field_smob *f_smob | |
1125 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1126 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1127 | ||
1128 | if (FIELD_NAME (*field)) | |
1129 | return gdbscm_scm_from_c_string (FIELD_NAME (*field)); | |
1130 | return SCM_BOOL_F; | |
1131 | } | |
1132 | ||
1133 | /* (field-type <gdb:field>) -> <gdb:type> | |
1134 | Return the <gdb:type> object of the field or #f if there isn't one. */ | |
1135 | ||
1136 | static SCM | |
1137 | gdbscm_field_type (SCM self) | |
1138 | { | |
1139 | field_smob *f_smob | |
1140 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1141 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1142 | ||
1143 | /* A field can have a NULL type in some situations. */ | |
b6cdac4b SM |
1144 | if (field->type ()) |
1145 | return tyscm_scm_from_type (field->type ()); | |
ed3ef339 DE |
1146 | return SCM_BOOL_F; |
1147 | } | |
1148 | ||
1149 | /* (field-enumval <gdb:field>) -> integer | |
1150 | For enum values, return its value as an integer. */ | |
1151 | ||
1152 | static SCM | |
1153 | gdbscm_field_enumval (SCM self) | |
1154 | { | |
1155 | field_smob *f_smob | |
1156 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1157 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1158 | struct type *type = tyscm_field_smob_containing_type (f_smob); | |
1159 | ||
78134374 | 1160 | SCM_ASSERT_TYPE (type->code () == TYPE_CODE_ENUM, |
ed3ef339 DE |
1161 | self, SCM_ARG1, FUNC_NAME, _("enum type")); |
1162 | ||
1163 | return scm_from_long (FIELD_ENUMVAL (*field)); | |
1164 | } | |
1165 | ||
1166 | /* (field-bitpos <gdb:field>) -> integer | |
1167 | For bitfields, return its offset in bits. */ | |
1168 | ||
1169 | static SCM | |
1170 | gdbscm_field_bitpos (SCM self) | |
1171 | { | |
1172 | field_smob *f_smob | |
1173 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1174 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1175 | struct type *type = tyscm_field_smob_containing_type (f_smob); | |
1176 | ||
78134374 | 1177 | SCM_ASSERT_TYPE (type->code () != TYPE_CODE_ENUM, |
ed3ef339 DE |
1178 | self, SCM_ARG1, FUNC_NAME, _("non-enum type")); |
1179 | ||
1180 | return scm_from_long (FIELD_BITPOS (*field)); | |
1181 | } | |
1182 | ||
1183 | /* (field-bitsize <gdb:field>) -> integer | |
1184 | Return the size of the field in bits. */ | |
1185 | ||
1186 | static SCM | |
1187 | gdbscm_field_bitsize (SCM self) | |
1188 | { | |
1189 | field_smob *f_smob | |
1190 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1191 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1192 | ||
1193 | return scm_from_long (FIELD_BITPOS (*field)); | |
1194 | } | |
1195 | ||
1196 | /* (field-artificial? <gdb:field>) -> boolean | |
1197 | Return #t if field is artificial. */ | |
1198 | ||
1199 | static SCM | |
1200 | gdbscm_field_artificial_p (SCM self) | |
1201 | { | |
1202 | field_smob *f_smob | |
1203 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
1204 | struct field *field = tyscm_field_smob_to_field (f_smob); | |
1205 | ||
1206 | return scm_from_bool (FIELD_ARTIFICIAL (*field)); | |
1207 | } | |
1208 | ||
1209 | /* (field-baseclass? <gdb:field>) -> boolean | |
1210 | Return #t if field is a baseclass. */ | |
1211 | ||
1212 | static SCM | |
1213 | gdbscm_field_baseclass_p (SCM self) | |
1214 | { | |
1215 | field_smob *f_smob | |
1216 | = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME); | |
ed3ef339 DE |
1217 | struct type *type = tyscm_field_smob_containing_type (f_smob); |
1218 | ||
78134374 | 1219 | if (type->code () == TYPE_CODE_STRUCT) |
ed3ef339 DE |
1220 | return scm_from_bool (f_smob->field_num < TYPE_N_BASECLASSES (type)); |
1221 | return SCM_BOOL_F; | |
1222 | } | |
1223 | \f | |
1224 | /* Return the type named TYPE_NAME in BLOCK. | |
1225 | Returns NULL if not found. | |
1226 | This routine does not throw an error. */ | |
1227 | ||
1228 | static struct type * | |
1229 | tyscm_lookup_typename (const char *type_name, const struct block *block) | |
1230 | { | |
1231 | struct type *type = NULL; | |
ed3ef339 | 1232 | |
a70b8144 | 1233 | try |
ed3ef339 | 1234 | { |
61012eef | 1235 | if (startswith (type_name, "struct ")) |
ed3ef339 | 1236 | type = lookup_struct (type_name + 7, NULL); |
61012eef | 1237 | else if (startswith (type_name, "union ")) |
ed3ef339 | 1238 | type = lookup_union (type_name + 6, NULL); |
61012eef | 1239 | else if (startswith (type_name, "enum ")) |
ed3ef339 DE |
1240 | type = lookup_enum (type_name + 5, NULL); |
1241 | else | |
b858499d | 1242 | type = lookup_typename (current_language, |
ed3ef339 DE |
1243 | type_name, block, 0); |
1244 | } | |
230d2906 | 1245 | catch (const gdb_exception &except) |
492d29ea PA |
1246 | { |
1247 | return NULL; | |
1248 | } | |
ed3ef339 DE |
1249 | |
1250 | return type; | |
1251 | } | |
1252 | ||
1253 | /* (lookup-type name [#:block <gdb:block>]) -> <gdb:type> | |
1254 | TODO: legacy template support left out until needed. */ | |
1255 | ||
1256 | static SCM | |
1257 | gdbscm_lookup_type (SCM name_scm, SCM rest) | |
1258 | { | |
1259 | SCM keywords[] = { block_keyword, SCM_BOOL_F }; | |
1260 | char *name; | |
1261 | SCM block_scm = SCM_BOOL_F; | |
1262 | int block_arg_pos = -1; | |
1263 | const struct block *block = NULL; | |
1264 | struct type *type; | |
1265 | ||
1266 | gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#O", | |
1267 | name_scm, &name, | |
1268 | rest, &block_arg_pos, &block_scm); | |
1269 | ||
1270 | if (block_arg_pos != -1) | |
1271 | { | |
1272 | SCM exception; | |
1273 | ||
1274 | block = bkscm_scm_to_block (block_scm, block_arg_pos, FUNC_NAME, | |
1275 | &exception); | |
1276 | if (block == NULL) | |
1277 | { | |
1278 | xfree (name); | |
1279 | gdbscm_throw (exception); | |
1280 | } | |
1281 | } | |
1282 | type = tyscm_lookup_typename (name, block); | |
1283 | xfree (name); | |
1284 | ||
1285 | if (type != NULL) | |
1286 | return tyscm_scm_from_type (type); | |
1287 | return SCM_BOOL_F; | |
1288 | } | |
1289 | \f | |
1290 | /* Initialize the Scheme type code. */ | |
1291 | ||
1292 | ||
1293 | static const scheme_integer_constant type_integer_constants[] = | |
1294 | { | |
1295 | #define X(SYM) { #SYM, SYM } | |
1296 | X (TYPE_CODE_BITSTRING), | |
1297 | X (TYPE_CODE_PTR), | |
1298 | X (TYPE_CODE_ARRAY), | |
1299 | X (TYPE_CODE_STRUCT), | |
1300 | X (TYPE_CODE_UNION), | |
1301 | X (TYPE_CODE_ENUM), | |
1302 | X (TYPE_CODE_FLAGS), | |
1303 | X (TYPE_CODE_FUNC), | |
1304 | X (TYPE_CODE_INT), | |
1305 | X (TYPE_CODE_FLT), | |
1306 | X (TYPE_CODE_VOID), | |
1307 | X (TYPE_CODE_SET), | |
1308 | X (TYPE_CODE_RANGE), | |
1309 | X (TYPE_CODE_STRING), | |
1310 | X (TYPE_CODE_ERROR), | |
1311 | X (TYPE_CODE_METHOD), | |
1312 | X (TYPE_CODE_METHODPTR), | |
1313 | X (TYPE_CODE_MEMBERPTR), | |
1314 | X (TYPE_CODE_REF), | |
1315 | X (TYPE_CODE_CHAR), | |
1316 | X (TYPE_CODE_BOOL), | |
1317 | X (TYPE_CODE_COMPLEX), | |
1318 | X (TYPE_CODE_TYPEDEF), | |
1319 | X (TYPE_CODE_NAMESPACE), | |
1320 | X (TYPE_CODE_DECFLOAT), | |
1321 | X (TYPE_CODE_INTERNAL_FUNCTION), | |
1322 | #undef X | |
1323 | ||
1324 | END_INTEGER_CONSTANTS | |
1325 | }; | |
1326 | ||
1327 | static const scheme_function type_functions[] = | |
1328 | { | |
72e02483 | 1329 | { "type?", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_p), |
ed3ef339 DE |
1330 | "\ |
1331 | Return #t if the object is a <gdb:type> object." }, | |
1332 | ||
72e02483 | 1333 | { "lookup-type", 1, 0, 1, as_a_scm_t_subr (gdbscm_lookup_type), |
ed3ef339 DE |
1334 | "\ |
1335 | Return the <gdb:type> object representing string or #f if not found.\n\ | |
1336 | If block is given then the type is looked for in that block.\n\ | |
1337 | \n\ | |
1338 | Arguments: string [#:block <gdb:block>]" }, | |
1339 | ||
72e02483 | 1340 | { "type-code", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_code), |
ed3ef339 DE |
1341 | "\ |
1342 | Return the code of the type" }, | |
1343 | ||
72e02483 | 1344 | { "type-tag", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_tag), |
ed3ef339 DE |
1345 | "\ |
1346 | Return the tag name of the type, or #f if there isn't one." }, | |
1347 | ||
72e02483 | 1348 | { "type-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_name), |
ed3ef339 DE |
1349 | "\ |
1350 | Return the name of the type as a string, or #f if there isn't one." }, | |
1351 | ||
72e02483 | 1352 | { "type-print-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_print_name), |
ed3ef339 DE |
1353 | "\ |
1354 | Return the print name of the type as a string." }, | |
1355 | ||
72e02483 | 1356 | { "type-sizeof", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_sizeof), |
ed3ef339 DE |
1357 | "\ |
1358 | Return the size of the type, in bytes." }, | |
1359 | ||
72e02483 PA |
1360 | { "type-strip-typedefs", 1, 0, 0, |
1361 | as_a_scm_t_subr (gdbscm_type_strip_typedefs), | |
ed3ef339 DE |
1362 | "\ |
1363 | Return a type formed by stripping the type of all typedefs." }, | |
1364 | ||
72e02483 | 1365 | { "type-array", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_array), |
ed3ef339 DE |
1366 | "\ |
1367 | Return a type representing an array of objects of the type.\n\ | |
1368 | \n\ | |
1369 | Arguments: <gdb:type> [low-bound] high-bound\n\ | |
1370 | If low-bound is not provided zero is used.\n\ | |
1371 | N.B. If only the high-bound parameter is specified, it is not\n\ | |
1372 | the array size.\n\ | |
1373 | Valid bounds for array indices are [low-bound,high-bound]." }, | |
1374 | ||
72e02483 | 1375 | { "type-vector", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_vector), |
ed3ef339 DE |
1376 | "\ |
1377 | Return a type representing a vector of objects of the type.\n\ | |
1378 | Vectors differ from arrays in that if the current language has C-style\n\ | |
1379 | arrays, vectors don't decay to a pointer to the first element.\n\ | |
1380 | They are first class values.\n\ | |
1381 | \n\ | |
1382 | Arguments: <gdb:type> [low-bound] high-bound\n\ | |
1383 | If low-bound is not provided zero is used.\n\ | |
1384 | N.B. If only the high-bound parameter is specified, it is not\n\ | |
1385 | the array size.\n\ | |
1386 | Valid bounds for array indices are [low-bound,high-bound]." }, | |
1387 | ||
72e02483 | 1388 | { "type-pointer", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_pointer), |
ed3ef339 DE |
1389 | "\ |
1390 | Return a type of pointer to the type." }, | |
1391 | ||
72e02483 | 1392 | { "type-range", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_range), |
ed3ef339 DE |
1393 | "\ |
1394 | Return (low high) representing the range for the type." }, | |
1395 | ||
72e02483 | 1396 | { "type-reference", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_reference), |
ed3ef339 DE |
1397 | "\ |
1398 | Return a type of reference to the type." }, | |
1399 | ||
72e02483 | 1400 | { "type-target", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_target), |
ed3ef339 DE |
1401 | "\ |
1402 | Return the target type of the type." }, | |
1403 | ||
72e02483 | 1404 | { "type-const", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_const), |
ed3ef339 DE |
1405 | "\ |
1406 | Return a const variant of the type." }, | |
1407 | ||
72e02483 | 1408 | { "type-volatile", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_volatile), |
ed3ef339 DE |
1409 | "\ |
1410 | Return a volatile variant of the type." }, | |
1411 | ||
72e02483 | 1412 | { "type-unqualified", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_unqualified), |
ed3ef339 DE |
1413 | "\ |
1414 | Return a variant of the type without const or volatile attributes." }, | |
1415 | ||
72e02483 | 1416 | { "type-num-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_num_fields), |
ed3ef339 DE |
1417 | "\ |
1418 | Return the number of fields of the type." }, | |
1419 | ||
72e02483 | 1420 | { "type-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_fields), |
ed3ef339 DE |
1421 | "\ |
1422 | Return the list of <gdb:field> objects of fields of the type." }, | |
1423 | ||
72e02483 PA |
1424 | { "make-field-iterator", 1, 0, 0, |
1425 | as_a_scm_t_subr (gdbscm_make_field_iterator), | |
ed3ef339 DE |
1426 | "\ |
1427 | Return a <gdb:iterator> object for iterating over the fields of the type." }, | |
1428 | ||
72e02483 | 1429 | { "type-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_field), |
ed3ef339 DE |
1430 | "\ |
1431 | Return the field named by string of the type.\n\ | |
1432 | \n\ | |
1433 | Arguments: <gdb:type> string" }, | |
1434 | ||
72e02483 | 1435 | { "type-has-field?", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_has_field_p), |
ed3ef339 DE |
1436 | "\ |
1437 | Return #t if the type has field named string.\n\ | |
1438 | \n\ | |
1439 | Arguments: <gdb:type> string" }, | |
1440 | ||
72e02483 | 1441 | { "field?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_p), |
ed3ef339 DE |
1442 | "\ |
1443 | Return #t if the object is a <gdb:field> object." }, | |
1444 | ||
72e02483 | 1445 | { "field-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_name), |
ed3ef339 DE |
1446 | "\ |
1447 | Return the name of the field." }, | |
1448 | ||
72e02483 | 1449 | { "field-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_type), |
ed3ef339 DE |
1450 | "\ |
1451 | Return the type of the field." }, | |
1452 | ||
72e02483 | 1453 | { "field-enumval", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_enumval), |
ed3ef339 DE |
1454 | "\ |
1455 | Return the enum value represented by the field." }, | |
1456 | ||
72e02483 | 1457 | { "field-bitpos", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitpos), |
ed3ef339 DE |
1458 | "\ |
1459 | Return the offset in bits of the field in its containing type." }, | |
1460 | ||
72e02483 | 1461 | { "field-bitsize", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitsize), |
ed3ef339 DE |
1462 | "\ |
1463 | Return the size of the field in bits." }, | |
1464 | ||
72e02483 | 1465 | { "field-artificial?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_artificial_p), |
ed3ef339 DE |
1466 | "\ |
1467 | Return #t if the field is artificial." }, | |
1468 | ||
72e02483 | 1469 | { "field-baseclass?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_baseclass_p), |
ed3ef339 DE |
1470 | "\ |
1471 | Return #t if the field is a baseclass." }, | |
1472 | ||
1473 | END_FUNCTIONS | |
1474 | }; | |
1475 | ||
1476 | void | |
1477 | gdbscm_initialize_types (void) | |
1478 | { | |
1479 | type_smob_tag = gdbscm_make_smob_type (type_smob_name, sizeof (type_smob)); | |
ed3ef339 DE |
1480 | scm_set_smob_free (type_smob_tag, tyscm_free_type_smob); |
1481 | scm_set_smob_print (type_smob_tag, tyscm_print_type_smob); | |
1482 | scm_set_smob_equalp (type_smob_tag, tyscm_equal_p_type_smob); | |
1483 | ||
1484 | field_smob_tag = gdbscm_make_smob_type (field_smob_name, | |
1485 | sizeof (field_smob)); | |
ed3ef339 DE |
1486 | scm_set_smob_print (field_smob_tag, tyscm_print_field_smob); |
1487 | ||
1488 | gdbscm_define_integer_constants (type_integer_constants, 1); | |
1489 | gdbscm_define_functions (type_functions, 1); | |
1490 | ||
1491 | /* This function is "private". */ | |
1492 | tyscm_next_field_x_proc | |
1493 | = scm_c_define_gsubr ("%type-next-field!", 1, 0, 0, | |
72e02483 | 1494 | as_a_scm_t_subr (gdbscm_type_next_field_x)); |
ed3ef339 DE |
1495 | scm_set_procedure_property_x (tyscm_next_field_x_proc, |
1496 | gdbscm_documentation_symbol, | |
1497 | gdbscm_scm_from_c_string ("\ | |
1498 | Internal function to assist the type fields iterator.")); | |
1499 | ||
1500 | block_keyword = scm_from_latin1_keyword ("block"); | |
1501 | ||
1502 | /* Register an objfile "free" callback so we can properly copy types | |
1503 | associated with the objfile when it's about to be deleted. */ | |
1504 | tyscm_objfile_data_key | |
1505 | = register_objfile_data_with_cleanup (save_objfile_types, NULL); | |
1506 | ||
1507 | global_types_map = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob, | |
1508 | tyscm_eq_type_smob); | |
1509 | } |