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