1 /* Scheme interface to types.
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 /* See README file in this directory for implementation notes, coding
21 conventions, et.al. */
24 #include "arch-utils.h"
30 #include "dwarf2/loc.h"
31 #include "typeprint.h"
32 #include "guile-internal.h"
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
41 /* This always appears first.
42 eqable_gdb_smob is used so that types are eq?-able.
43 Also, a type object can be associated with an objfile. eqable_gdb_smob
44 lets us track the lifetime of all types associated with an objfile.
45 When an objfile is deleted we need to invalidate the type object. */
48 /* The GDB type structure this smob is wrapping. */
56 /* This always appears first. */
59 /* Backlink to the containing <gdb:type> object. */
62 /* The field number in TYPE_SCM. */
66 static const char type_smob_name
[] = "gdb:type";
67 static const char field_smob_name
[] = "gdb:field";
69 static const char not_composite_error
[] =
70 N_("type is not a structure, union, or enum type");
72 /* The tag Guile knows the type smob by. */
73 static scm_t_bits type_smob_tag
;
75 /* The tag Guile knows the field smob by. */
76 static scm_t_bits field_smob_tag
;
78 /* The "next" procedure for field iterators. */
79 static SCM tyscm_next_field_x_proc
;
81 /* Keywords used in argument passing. */
82 static SCM block_keyword
;
84 static int tyscm_copy_type_recursive (void **slot
, void *info
);
86 /* Called when an objfile is about to be deleted.
87 Make a copy of all types associated with OBJFILE. */
91 void operator() (htab_t htab
)
93 if (!gdb_scheme_initialized
)
96 gdb_assert (htab
!= nullptr);
97 htab_up copied_types
= create_copied_types_hash ();
98 htab_traverse_noresize (htab
, tyscm_copy_type_recursive
, copied_types
.get ());
103 static const registry
<objfile
>::key
<htab
, tyscm_deleter
>
104 tyscm_objfile_data_key
;
106 /* Hash table to uniquify global (non-objfile-owned) types. */
107 static htab_t global_types_map
;
109 static struct type
*tyscm_get_composite (struct type
*type
);
111 /* Return the type field of T_SMOB.
112 This exists so that we don't have to export the struct's contents. */
115 tyscm_type_smob_type (type_smob
*t_smob
)
120 /* Return the name of TYPE in expanded form. If there's an error
121 computing the name, throws the gdb exception with scm_throw. */
124 tyscm_type_name (struct type
*type
)
131 current_language
->print_type (type
, "", &stb
, -1, 0,
132 &type_print_raw_options
);
133 return stb
.release ();
135 catch (const gdb_exception_forced_quit
&except
)
137 quit_force (NULL
, 0);
139 catch (const gdb_exception
&except
)
141 excp
= gdbscm_scm_from_gdb_exception (unpack (except
));
147 /* Administrivia for type smobs. */
149 /* Helper function to hash a type_smob. */
152 tyscm_hash_type_smob (const void *p
)
154 const type_smob
*t_smob
= (const type_smob
*) p
;
156 return htab_hash_pointer (t_smob
->type
);
159 /* Helper function to compute equality of type_smobs. */
162 tyscm_eq_type_smob (const void *ap
, const void *bp
)
164 const type_smob
*a
= (const type_smob
*) ap
;
165 const type_smob
*b
= (const type_smob
*) bp
;
167 return (a
->type
== b
->type
171 /* Return the struct type pointer -> SCM mapping table.
172 If type is owned by an objfile, the mapping table is created if necessary.
173 Otherwise, type is not owned by an objfile, and we use
177 tyscm_type_map (struct type
*type
)
179 struct objfile
*objfile
= type
->objfile_owner ();
183 return global_types_map
;
185 htab
= tyscm_objfile_data_key
.get (objfile
);
188 htab
= gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob
,
190 tyscm_objfile_data_key
.set (objfile
, htab
);
196 /* The smob "free" function for <gdb:type>. */
199 tyscm_free_type_smob (SCM self
)
201 type_smob
*t_smob
= (type_smob
*) SCM_SMOB_DATA (self
);
203 if (t_smob
->type
!= NULL
)
205 htab_t htab
= tyscm_type_map (t_smob
->type
);
207 gdbscm_clear_eqable_gsmob_ptr_slot (htab
, &t_smob
->base
);
210 /* Not necessary, done to catch bugs. */
216 /* The smob "print" function for <gdb:type>. */
219 tyscm_print_type_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
221 type_smob
*t_smob
= (type_smob
*) SCM_SMOB_DATA (self
);
222 std::string name
= tyscm_type_name (t_smob
->type
);
224 /* pstate->writingp = zero if invoked by display/~A, and nonzero if
225 invoked by write/~S. What to do here may need to evolve.
226 IWBN if we could pass an argument to format that would we could use
227 instead of writingp. */
228 if (pstate
->writingp
)
229 gdbscm_printf (port
, "#<%s ", type_smob_name
);
231 scm_puts (name
.c_str (), port
);
233 if (pstate
->writingp
)
234 scm_puts (">", port
);
236 scm_remember_upto_here_1 (self
);
238 /* Non-zero means success. */
242 /* The smob "equal?" function for <gdb:type>. */
245 tyscm_equal_p_type_smob (SCM type1_scm
, SCM type2_scm
)
247 type_smob
*type1_smob
, *type2_smob
;
248 struct type
*type1
, *type2
;
251 SCM_ASSERT_TYPE (tyscm_is_type (type1_scm
), type1_scm
, SCM_ARG1
, FUNC_NAME
,
253 SCM_ASSERT_TYPE (tyscm_is_type (type2_scm
), type2_scm
, SCM_ARG2
, FUNC_NAME
,
255 type1_smob
= (type_smob
*) SCM_SMOB_DATA (type1_scm
);
256 type2_smob
= (type_smob
*) SCM_SMOB_DATA (type2_scm
);
257 type1
= type1_smob
->type
;
258 type2
= type2_smob
->type
;
260 gdbscm_gdb_exception exc
{};
263 result
= types_deeply_equal (type1
, type2
);
265 catch (const gdb_exception
&except
)
267 exc
= unpack (except
);
270 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
271 return scm_from_bool (result
);
274 /* Low level routine to create a <gdb:type> object. */
277 tyscm_make_type_smob (void)
279 type_smob
*t_smob
= (type_smob
*)
280 scm_gc_malloc (sizeof (type_smob
), type_smob_name
);
283 /* This must be filled in by the caller. */
286 t_scm
= scm_new_smob (type_smob_tag
, (scm_t_bits
) t_smob
);
287 gdbscm_init_eqable_gsmob (&t_smob
->base
, t_scm
);
292 /* Return non-zero if SCM is a <gdb:type> object. */
295 tyscm_is_type (SCM self
)
297 return SCM_SMOB_PREDICATE (type_smob_tag
, self
);
300 /* (type? object) -> boolean */
303 gdbscm_type_p (SCM self
)
305 return scm_from_bool (tyscm_is_type (self
));
308 /* Return the existing object that encapsulates TYPE, or create a new
309 <gdb:type> object. */
312 tyscm_scm_from_type (struct type
*type
)
315 eqable_gdb_smob
**slot
;
316 type_smob
*t_smob
, t_smob_for_lookup
;
319 /* If we've already created a gsmob for this type, return it.
320 This makes types eq?-able. */
321 htab
= tyscm_type_map (type
);
322 t_smob_for_lookup
.type
= type
;
323 slot
= gdbscm_find_eqable_gsmob_ptr_slot (htab
, &t_smob_for_lookup
.base
);
325 return (*slot
)->containing_scm
;
327 t_scm
= tyscm_make_type_smob ();
328 t_smob
= (type_smob
*) SCM_SMOB_DATA (t_scm
);
330 gdbscm_fill_eqable_gsmob_ptr_slot (slot
, &t_smob
->base
);
335 /* Returns the <gdb:type> object in SELF.
336 Throws an exception if SELF is not a <gdb:type> object. */
339 tyscm_get_type_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
341 SCM_ASSERT_TYPE (tyscm_is_type (self
), self
, arg_pos
, func_name
,
347 /* Returns a pointer to the type smob of SELF.
348 Throws an exception if SELF is not a <gdb:type> object. */
351 tyscm_get_type_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
353 SCM t_scm
= tyscm_get_type_arg_unsafe (self
, arg_pos
, func_name
);
354 type_smob
*t_smob
= (type_smob
*) SCM_SMOB_DATA (t_scm
);
359 /* Return the type field of T_SCM, an object of type <gdb:type>.
360 This exists so that we don't have to export the struct's contents. */
363 tyscm_scm_to_type (SCM t_scm
)
367 gdb_assert (tyscm_is_type (t_scm
));
368 t_smob
= (type_smob
*) SCM_SMOB_DATA (t_scm
);
372 /* Helper function to make a deep copy of the type. */
375 tyscm_copy_type_recursive (void **slot
, void *info
)
377 type_smob
*t_smob
= (type_smob
*) *slot
;
378 htab_t copied_types
= (htab_t
) info
;
380 eqable_gdb_smob
**new_slot
;
381 type_smob t_smob_for_lookup
;
383 htab_empty (copied_types
);
384 t_smob
->type
= copy_type_recursive (t_smob
->type
, copied_types
);
386 /* The eq?-hashtab that the type lived in is going away.
387 Add the type to its new eq?-hashtab: Otherwise if/when the type is later
388 garbage collected we'll assert-fail if the type isn't in the hashtab.
391 Types now live in "arch space", and things like "char" that came from
392 the objfile *could* be considered eq? with the arch "char" type.
393 However, they weren't before the objfile got deleted, so making them
394 eq? now is debatable. */
395 htab
= tyscm_type_map (t_smob
->type
);
396 t_smob_for_lookup
.type
= t_smob
->type
;
397 new_slot
= gdbscm_find_eqable_gsmob_ptr_slot (htab
, &t_smob_for_lookup
.base
);
398 gdb_assert (*new_slot
== NULL
);
399 gdbscm_fill_eqable_gsmob_ptr_slot (new_slot
, &t_smob
->base
);
405 /* Administrivia for field smobs. */
407 /* The smob "print" function for <gdb:field>. */
410 tyscm_print_field_smob (SCM self
, SCM port
, scm_print_state
*pstate
)
412 field_smob
*f_smob
= (field_smob
*) SCM_SMOB_DATA (self
);
414 gdbscm_printf (port
, "#<%s ", field_smob_name
);
415 scm_write (f_smob
->type_scm
, port
);
416 gdbscm_printf (port
, " %d", f_smob
->field_num
);
417 scm_puts (">", port
);
419 scm_remember_upto_here_1 (self
);
421 /* Non-zero means success. */
425 /* Low level routine to create a <gdb:field> object for field FIELD_NUM
429 tyscm_make_field_smob (SCM type_scm
, int field_num
)
431 field_smob
*f_smob
= (field_smob
*)
432 scm_gc_malloc (sizeof (field_smob
), field_smob_name
);
435 f_smob
->type_scm
= type_scm
;
436 f_smob
->field_num
= field_num
;
437 result
= scm_new_smob (field_smob_tag
, (scm_t_bits
) f_smob
);
438 gdbscm_init_gsmob (&f_smob
->base
);
443 /* Return non-zero if SCM is a <gdb:field> object. */
446 tyscm_is_field (SCM self
)
448 return SCM_SMOB_PREDICATE (field_smob_tag
, self
);
451 /* (field? object) -> boolean */
454 gdbscm_field_p (SCM self
)
456 return scm_from_bool (tyscm_is_field (self
));
459 /* Create a new <gdb:field> object that encapsulates field FIELD_NUM
463 tyscm_scm_from_field (SCM type_scm
, int field_num
)
465 return tyscm_make_field_smob (type_scm
, field_num
);
468 /* Returns the <gdb:field> object in SELF.
469 Throws an exception if SELF is not a <gdb:field> object. */
472 tyscm_get_field_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
474 SCM_ASSERT_TYPE (tyscm_is_field (self
), self
, arg_pos
, func_name
,
480 /* Returns a pointer to the field smob of SELF.
481 Throws an exception if SELF is not a <gdb:field> object. */
484 tyscm_get_field_smob_arg_unsafe (SCM self
, int arg_pos
, const char *func_name
)
486 SCM f_scm
= tyscm_get_field_arg_unsafe (self
, arg_pos
, func_name
);
487 field_smob
*f_smob
= (field_smob
*) SCM_SMOB_DATA (f_scm
);
492 /* Returns a pointer to the type struct in F_SMOB
493 (the type the field is in). */
496 tyscm_field_smob_containing_type (field_smob
*f_smob
)
500 gdb_assert (tyscm_is_type (f_smob
->type_scm
));
501 t_smob
= (type_smob
*) SCM_SMOB_DATA (f_smob
->type_scm
);
506 /* Returns a pointer to the field struct of F_SMOB. */
508 static struct field
*
509 tyscm_field_smob_to_field (field_smob
*f_smob
)
511 struct type
*type
= tyscm_field_smob_containing_type (f_smob
);
513 /* This should be non-NULL by construction. */
514 gdb_assert (type
->fields () != NULL
);
516 return &type
->field (f_smob
->field_num
);
519 /* Type smob accessors. */
521 /* (type-code <gdb:type>) -> integer
522 Return the code for this type. */
525 gdbscm_type_code (SCM self
)
528 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
529 struct type
*type
= t_smob
->type
;
531 return scm_from_int (type
->code ());
534 /* (type-fields <gdb:type>) -> list
535 Return a list of all fields. Each element is a <gdb:field> object.
536 This also supports arrays, we return a field list of one element,
540 gdbscm_type_fields (SCM self
)
543 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
544 struct type
*type
= t_smob
->type
;
545 struct type
*containing_type
;
546 SCM containing_type_scm
, result
;
549 containing_type
= tyscm_get_composite (type
);
550 if (containing_type
== NULL
)
551 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG1
, self
,
552 _(not_composite_error
));
554 /* If SELF is a typedef or reference, we want the underlying type,
555 which is what tyscm_get_composite returns. */
556 if (containing_type
== type
)
557 containing_type_scm
= self
;
559 containing_type_scm
= tyscm_scm_from_type (containing_type
);
562 for (i
= 0; i
< containing_type
->num_fields (); ++i
)
563 result
= scm_cons (tyscm_make_field_smob (containing_type_scm
, i
), result
);
565 return scm_reverse_x (result
, SCM_EOL
);
568 /* (type-tag <gdb:type>) -> string
569 Return the type's tag, or #f. */
572 gdbscm_type_tag (SCM self
)
575 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
576 struct type
*type
= t_smob
->type
;
577 const char *tagname
= nullptr;
579 if (type
->code () == TYPE_CODE_STRUCT
580 || type
->code () == TYPE_CODE_UNION
581 || type
->code () == TYPE_CODE_ENUM
)
582 tagname
= type
->name ();
584 if (tagname
== nullptr)
586 return gdbscm_scm_from_c_string (tagname
);
589 /* (type-name <gdb:type>) -> string
590 Return the type's name, or #f. */
593 gdbscm_type_name (SCM self
)
596 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
597 struct type
*type
= t_smob
->type
;
601 return gdbscm_scm_from_c_string (type
->name ());
604 /* (type-print-name <gdb:type>) -> string
605 Return the print name of type.
606 TODO: template support elided for now. */
609 gdbscm_type_print_name (SCM self
)
612 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
613 struct type
*type
= t_smob
->type
;
614 std::string thetype
= tyscm_type_name (type
);
615 SCM result
= gdbscm_scm_from_c_string (thetype
.c_str ());
620 /* (type-sizeof <gdb:type>) -> integer
621 Return the size of the type represented by SELF, in bytes. */
624 gdbscm_type_sizeof (SCM self
)
627 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
628 struct type
*type
= t_smob
->type
;
632 check_typedef (type
);
634 catch (const gdb_exception
&except
)
638 /* Ignore exceptions. */
640 return scm_from_long (type
->length ());
643 /* (type-strip-typedefs <gdb:type>) -> <gdb:type>
644 Return the type, stripped of typedefs. */
647 gdbscm_type_strip_typedefs (SCM self
)
650 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
651 struct type
*type
= t_smob
->type
;
653 gdbscm_gdb_exception exc
{};
656 type
= check_typedef (type
);
658 catch (const gdb_exception
&except
)
660 exc
= unpack (except
);
663 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
664 return tyscm_scm_from_type (type
);
667 /* Strip typedefs and pointers/reference from a type. Then check that
668 it is a struct, union, or enum type. If not, return NULL. */
671 tyscm_get_composite (struct type
*type
)
676 gdbscm_gdb_exception exc
{};
679 type
= check_typedef (type
);
681 catch (const gdb_exception
&except
)
683 exc
= unpack (except
);
686 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
687 if (type
->code () != TYPE_CODE_PTR
688 && type
->code () != TYPE_CODE_REF
)
690 type
= type
->target_type ();
693 /* If this is not a struct, union, or enum type, raise TypeError
695 if (type
->code () != TYPE_CODE_STRUCT
696 && type
->code () != TYPE_CODE_UNION
697 && type
->code () != TYPE_CODE_ENUM
)
703 /* Helper for tyscm_array and tyscm_vector. */
706 tyscm_array_1 (SCM self
, SCM n1_scm
, SCM n2_scm
, int is_vector
,
707 const char *func_name
)
710 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, func_name
);
711 struct type
*type
= t_smob
->type
;
713 struct type
*array
= NULL
;
715 gdbscm_parse_function_args (func_name
, SCM_ARG2
, NULL
, "l|l",
716 n1_scm
, &n1
, n2_scm
, &n2
);
718 if (SCM_UNBNDP (n2_scm
))
724 if (n2
< n1
- 1) /* Note: An empty array has n2 == n1 - 1. */
726 gdbscm_out_of_range_error (func_name
, SCM_ARG3
,
727 scm_cons (scm_from_long (n1
),
729 _("Array length must not be negative"));
732 gdbscm_gdb_exception exc
{};
735 array
= lookup_array_range_type (type
, n1
, n2
);
737 make_vector_type (array
);
739 catch (const gdb_exception
&except
)
741 exc
= unpack (except
);
744 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
745 return tyscm_scm_from_type (array
);
748 /* (type-array <gdb:type> [low-bound] high-bound) -> <gdb:type>
749 The array has indices [low-bound,high-bound].
750 If low-bound is not provided zero is used.
751 Return an array type.
753 IWBN if the one argument version specified a size, not the high bound.
754 It's too easy to pass one argument thinking it is the size of the array.
755 The current semantics are for compatibility with the Python version.
756 Later we can add #:size. */
759 gdbscm_type_array (SCM self
, SCM n1
, SCM n2
)
761 return tyscm_array_1 (self
, n1
, n2
, 0, FUNC_NAME
);
764 /* (type-vector <gdb:type> [low-bound] high-bound) -> <gdb:type>
765 The array has indices [low-bound,high-bound].
766 If low-bound is not provided zero is used.
767 Return a vector type.
769 IWBN if the one argument version specified a size, not the high bound.
770 It's too easy to pass one argument thinking it is the size of the array.
771 The current semantics are for compatibility with the Python version.
772 Later we can add #:size. */
775 gdbscm_type_vector (SCM self
, SCM n1
, SCM n2
)
777 return tyscm_array_1 (self
, n1
, n2
, 1, FUNC_NAME
);
780 /* (type-pointer <gdb:type>) -> <gdb:type>
781 Return a <gdb:type> object which represents a pointer to SELF. */
784 gdbscm_type_pointer (SCM self
)
787 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
788 struct type
*type
= t_smob
->type
;
790 gdbscm_gdb_exception exc
{};
793 type
= lookup_pointer_type (type
);
795 catch (const gdb_exception
&except
)
797 exc
= unpack (except
);
800 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
801 return tyscm_scm_from_type (type
);
804 /* (type-range <gdb:type>) -> (low high)
805 Return the range of a type represented by SELF. The return type is
806 a list. The first element is the low bound, and the second element
807 is the high bound. */
810 gdbscm_type_range (SCM self
)
813 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
814 struct type
*type
= t_smob
->type
;
815 SCM low_scm
, high_scm
;
816 /* Initialize these to appease GCC warnings. */
817 LONGEST low
= 0, high
= 0;
819 SCM_ASSERT_TYPE (type
->code () == TYPE_CODE_ARRAY
820 || type
->code () == TYPE_CODE_STRING
821 || type
->code () == TYPE_CODE_RANGE
,
822 self
, SCM_ARG1
, FUNC_NAME
, _("ranged type"));
824 switch (type
->code ())
826 case TYPE_CODE_ARRAY
:
827 case TYPE_CODE_STRING
:
828 case TYPE_CODE_RANGE
:
829 if (type
->bounds ()->low
.is_constant ())
830 low
= type
->bounds ()->low
.const_val ();
834 if (type
->bounds ()->high
.is_constant ())
835 high
= type
->bounds ()->high
.const_val ();
841 low_scm
= gdbscm_scm_from_longest (low
);
842 high_scm
= gdbscm_scm_from_longest (high
);
844 return scm_list_2 (low_scm
, high_scm
);
847 /* (type-reference <gdb:type>) -> <gdb:type>
848 Return a <gdb:type> object which represents a reference to SELF. */
851 gdbscm_type_reference (SCM self
)
854 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
855 struct type
*type
= t_smob
->type
;
857 gdbscm_gdb_exception exc
{};
860 type
= lookup_lvalue_reference_type (type
);
862 catch (const gdb_exception
&except
)
864 exc
= unpack (except
);
867 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
868 return tyscm_scm_from_type (type
);
871 /* (type-target <gdb:type>) -> <gdb:type>
872 Return a <gdb:type> object which represents the target type of SELF. */
875 gdbscm_type_target (SCM self
)
878 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
879 struct type
*type
= t_smob
->type
;
881 SCM_ASSERT (type
->target_type (), self
, SCM_ARG1
, FUNC_NAME
);
883 return tyscm_scm_from_type (type
->target_type ());
886 /* (type-const <gdb:type>) -> <gdb:type>
887 Return a const-qualified type variant. */
890 gdbscm_type_const (SCM self
)
893 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
894 struct type
*type
= t_smob
->type
;
896 gdbscm_gdb_exception exc
{};
899 type
= make_cv_type (1, 0, type
, NULL
);
901 catch (const gdb_exception
&except
)
903 exc
= unpack (except
);
906 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
907 return tyscm_scm_from_type (type
);
910 /* (type-volatile <gdb:type>) -> <gdb:type>
911 Return a volatile-qualified type variant. */
914 gdbscm_type_volatile (SCM self
)
917 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
918 struct type
*type
= t_smob
->type
;
920 gdbscm_gdb_exception exc
{};
923 type
= make_cv_type (0, 1, type
, NULL
);
925 catch (const gdb_exception
&except
)
927 exc
= unpack (except
);
930 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
931 return tyscm_scm_from_type (type
);
934 /* (type-unqualified <gdb:type>) -> <gdb:type>
935 Return an unqualified type variant. */
938 gdbscm_type_unqualified (SCM self
)
941 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
942 struct type
*type
= t_smob
->type
;
944 gdbscm_gdb_exception exc
{};
947 type
= make_cv_type (0, 0, type
, NULL
);
949 catch (const gdb_exception
&except
)
951 exc
= unpack (except
);
954 GDBSCM_HANDLE_GDB_EXCEPTION (exc
);
955 return tyscm_scm_from_type (type
);
958 /* Field related accessors of types. */
960 /* (type-num-fields <gdb:type>) -> integer
961 Return number of fields. */
964 gdbscm_type_num_fields (SCM self
)
967 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
968 struct type
*type
= t_smob
->type
;
970 type
= tyscm_get_composite (type
);
972 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG1
, self
,
973 _(not_composite_error
));
975 return scm_from_long (type
->num_fields ());
978 /* (type-field <gdb:type> string) -> <gdb:field>
979 Return the <gdb:field> object for the field named by the argument. */
982 gdbscm_type_field (SCM self
, SCM field_scm
)
985 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
986 struct type
*type
= t_smob
->type
;
988 SCM_ASSERT_TYPE (scm_is_string (field_scm
), field_scm
, SCM_ARG2
, FUNC_NAME
,
991 /* We want just fields of this type, not of base types, so instead of
992 using lookup_struct_elt_type, portions of that function are
995 type
= tyscm_get_composite (type
);
997 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG1
, self
,
998 _(not_composite_error
));
1001 gdb::unique_xmalloc_ptr
<char> field
= gdbscm_scm_to_c_string (field_scm
);
1003 for (int i
= 0; i
< type
->num_fields (); i
++)
1005 const char *t_field_name
= type
->field (i
).name ();
1007 if (t_field_name
&& (strcmp_iw (t_field_name
, field
.get ()) == 0))
1009 field
.reset (nullptr);
1010 return tyscm_make_field_smob (self
, i
);
1015 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG1
, field_scm
,
1016 _("Unknown field"));
1019 /* (type-has-field? <gdb:type> string) -> boolean
1020 Return boolean indicating if type SELF has FIELD_SCM (a string). */
1023 gdbscm_type_has_field_p (SCM self
, SCM field_scm
)
1026 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1027 struct type
*type
= t_smob
->type
;
1029 SCM_ASSERT_TYPE (scm_is_string (field_scm
), field_scm
, SCM_ARG2
, FUNC_NAME
,
1032 /* We want just fields of this type, not of base types, so instead of
1033 using lookup_struct_elt_type, portions of that function are
1036 type
= tyscm_get_composite (type
);
1038 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG1
, self
,
1039 _(not_composite_error
));
1042 gdb::unique_xmalloc_ptr
<char> field
1043 = gdbscm_scm_to_c_string (field_scm
);
1045 for (int i
= 0; i
< type
->num_fields (); i
++)
1047 const char *t_field_name
= type
->field (i
).name ();
1049 if (t_field_name
&& (strcmp_iw (t_field_name
, field
.get ()) == 0))
1057 /* (make-field-iterator <gdb:type>) -> <gdb:iterator>
1058 Make a field iterator object. */
1061 gdbscm_make_field_iterator (SCM self
)
1064 = tyscm_get_type_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1065 struct type
*type
= t_smob
->type
;
1066 struct type
*containing_type
;
1067 SCM containing_type_scm
;
1069 containing_type
= tyscm_get_composite (type
);
1070 if (containing_type
== NULL
)
1071 gdbscm_out_of_range_error (FUNC_NAME
, SCM_ARG1
, self
,
1072 _(not_composite_error
));
1074 /* If SELF is a typedef or reference, we want the underlying type,
1075 which is what tyscm_get_composite returns. */
1076 if (containing_type
== type
)
1077 containing_type_scm
= self
;
1079 containing_type_scm
= tyscm_scm_from_type (containing_type
);
1081 return gdbscm_make_iterator (containing_type_scm
, scm_from_int (0),
1082 tyscm_next_field_x_proc
);
1085 /* (type-next-field! <gdb:iterator>) -> <gdb:field>
1086 Return the next field in the iteration through the list of fields of the
1087 type, or (end-of-iteration).
1088 SELF is a <gdb:iterator> object created by gdbscm_make_field_iterator.
1089 This is the next! <gdb:iterator> function, not exported to the user. */
1092 gdbscm_type_next_field_x (SCM self
)
1094 iterator_smob
*i_smob
;
1097 SCM it_scm
, result
, progress
, object
;
1100 it_scm
= itscm_get_iterator_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1101 i_smob
= (iterator_smob
*) SCM_SMOB_DATA (it_scm
);
1102 object
= itscm_iterator_smob_object (i_smob
);
1103 progress
= itscm_iterator_smob_progress (i_smob
);
1105 SCM_ASSERT_TYPE (tyscm_is_type (object
), object
,
1106 SCM_ARG1
, FUNC_NAME
, type_smob_name
);
1107 t_smob
= (type_smob
*) SCM_SMOB_DATA (object
);
1108 type
= t_smob
->type
;
1110 SCM_ASSERT_TYPE (scm_is_signed_integer (progress
,
1111 0, type
->num_fields ()),
1112 progress
, SCM_ARG1
, FUNC_NAME
, _("integer"));
1113 field
= scm_to_int (progress
);
1115 if (field
< type
->num_fields ())
1117 result
= tyscm_make_field_smob (object
, field
);
1118 itscm_set_iterator_smob_progress_x (i_smob
, scm_from_int (field
+ 1));
1122 return gdbscm_end_of_iteration ();
1125 /* Field smob accessors. */
1127 /* (field-name <gdb:field>) -> string
1128 Return the name of this field or #f if there isn't one. */
1131 gdbscm_field_name (SCM self
)
1134 = tyscm_get_field_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1135 struct field
*field
= tyscm_field_smob_to_field (f_smob
);
1137 if (field
->name () != nullptr)
1138 return gdbscm_scm_from_c_string (field
->name ());
1142 /* (field-type <gdb:field>) -> <gdb:type>
1143 Return the <gdb:type> object of the field or #f if there isn't one. */
1146 gdbscm_field_type (SCM self
)
1149 = tyscm_get_field_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1150 struct field
*field
= tyscm_field_smob_to_field (f_smob
);
1152 /* A field can have a NULL type in some situations. */
1154 return tyscm_scm_from_type (field
->type ());
1158 /* (field-enumval <gdb:field>) -> integer
1159 For enum values, return its value as an integer. */
1162 gdbscm_field_enumval (SCM self
)
1165 = tyscm_get_field_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1166 struct field
*field
= tyscm_field_smob_to_field (f_smob
);
1167 struct type
*type
= tyscm_field_smob_containing_type (f_smob
);
1169 SCM_ASSERT_TYPE (type
->code () == TYPE_CODE_ENUM
,
1170 self
, SCM_ARG1
, FUNC_NAME
, _("enum type"));
1172 return scm_from_long (field
->loc_enumval ());
1175 /* (field-bitpos <gdb:field>) -> integer
1176 For bitfields, return its offset in bits. */
1179 gdbscm_field_bitpos (SCM self
)
1182 = tyscm_get_field_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1183 struct field
*field
= tyscm_field_smob_to_field (f_smob
);
1184 struct type
*type
= tyscm_field_smob_containing_type (f_smob
);
1186 SCM_ASSERT_TYPE (type
->code () != TYPE_CODE_ENUM
,
1187 self
, SCM_ARG1
, FUNC_NAME
, _("non-enum type"));
1189 return scm_from_long (field
->loc_bitpos ());
1192 /* (field-bitsize <gdb:field>) -> integer
1193 Return the size of the field in bits. */
1196 gdbscm_field_bitsize (SCM self
)
1199 = tyscm_get_field_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1200 struct field
*field
= tyscm_field_smob_to_field (f_smob
);
1202 return scm_from_long (field
->loc_bitpos ());
1205 /* (field-artificial? <gdb:field>) -> boolean
1206 Return #t if field is artificial. */
1209 gdbscm_field_artificial_p (SCM self
)
1212 = tyscm_get_field_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1213 struct field
*field
= tyscm_field_smob_to_field (f_smob
);
1215 return scm_from_bool (field
->is_artificial ());
1218 /* (field-baseclass? <gdb:field>) -> boolean
1219 Return #t if field is a baseclass. */
1222 gdbscm_field_baseclass_p (SCM self
)
1225 = tyscm_get_field_smob_arg_unsafe (self
, SCM_ARG1
, FUNC_NAME
);
1226 struct type
*type
= tyscm_field_smob_containing_type (f_smob
);
1228 if (type
->code () == TYPE_CODE_STRUCT
)
1229 return scm_from_bool (f_smob
->field_num
< TYPE_N_BASECLASSES (type
));
1233 /* Return the type named TYPE_NAME in BLOCK.
1234 Returns NULL if not found.
1235 This routine does not throw an error. */
1237 static struct type
*
1238 tyscm_lookup_typename (const char *type_name
, const struct block
*block
)
1240 struct type
*type
= NULL
;
1244 if (startswith (type_name
, "struct "))
1245 type
= lookup_struct (type_name
+ 7, NULL
);
1246 else if (startswith (type_name
, "union "))
1247 type
= lookup_union (type_name
+ 6, NULL
);
1248 else if (startswith (type_name
, "enum "))
1249 type
= lookup_enum (type_name
+ 5, NULL
);
1251 type
= lookup_typename (current_language
,
1252 type_name
, block
, 0);
1254 catch (const gdb_exception
&except
)
1262 /* (lookup-type name [#:block <gdb:block>]) -> <gdb:type>
1263 TODO: legacy template support left out until needed. */
1266 gdbscm_lookup_type (SCM name_scm
, SCM rest
)
1268 SCM keywords
[] = { block_keyword
, SCM_BOOL_F
};
1270 SCM block_scm
= SCM_BOOL_F
;
1271 int block_arg_pos
= -1;
1272 const struct block
*block
= NULL
;
1275 gdbscm_parse_function_args (FUNC_NAME
, SCM_ARG1
, keywords
, "s#O",
1277 rest
, &block_arg_pos
, &block_scm
);
1279 if (block_arg_pos
!= -1)
1283 block
= bkscm_scm_to_block (block_scm
, block_arg_pos
, FUNC_NAME
,
1288 gdbscm_throw (exception
);
1291 type
= tyscm_lookup_typename (name
, block
);
1295 return tyscm_scm_from_type (type
);
1299 /* Initialize the Scheme type code. */
1302 static const scheme_integer_constant type_integer_constants
[] =
1304 /* This is kept for backward compatibility. */
1305 { "TYPE_CODE_BITSTRING", -1 },
1307 #define OP(SYM) { #SYM, SYM },
1308 #include "type-codes.def"
1311 END_INTEGER_CONSTANTS
1314 static const scheme_function type_functions
[] =
1316 { "type?", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_p
),
1318 Return #t if the object is a <gdb:type> object." },
1320 { "lookup-type", 1, 0, 1, as_a_scm_t_subr (gdbscm_lookup_type
),
1322 Return the <gdb:type> object representing string or #f if not found.\n\
1323 If block is given then the type is looked for in that block.\n\
1325 Arguments: string [#:block <gdb:block>]" },
1327 { "type-code", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_code
),
1329 Return the code of the type" },
1331 { "type-tag", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_tag
),
1333 Return the tag name of the type, or #f if there isn't one." },
1335 { "type-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_name
),
1337 Return the name of the type as a string, or #f if there isn't one." },
1339 { "type-print-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_print_name
),
1341 Return the print name of the type as a string." },
1343 { "type-sizeof", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_sizeof
),
1345 Return the size of the type, in bytes." },
1347 { "type-strip-typedefs", 1, 0, 0,
1348 as_a_scm_t_subr (gdbscm_type_strip_typedefs
),
1350 Return a type formed by stripping the type of all typedefs." },
1352 { "type-array", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_array
),
1354 Return a type representing an array of objects of the type.\n\
1356 Arguments: <gdb:type> [low-bound] high-bound\n\
1357 If low-bound is not provided zero is used.\n\
1358 N.B. If only the high-bound parameter is specified, it is not\n\
1360 Valid bounds for array indices are [low-bound,high-bound]." },
1362 { "type-vector", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_vector
),
1364 Return a type representing a vector of objects of the type.\n\
1365 Vectors differ from arrays in that if the current language has C-style\n\
1366 arrays, vectors don't decay to a pointer to the first element.\n\
1367 They are first class values.\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\
1373 Valid bounds for array indices are [low-bound,high-bound]." },
1375 { "type-pointer", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_pointer
),
1377 Return a type of pointer to the type." },
1379 { "type-range", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_range
),
1381 Return (low high) representing the range for the type." },
1383 { "type-reference", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_reference
),
1385 Return a type of reference to the type." },
1387 { "type-target", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_target
),
1389 Return the target type of the type." },
1391 { "type-const", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_const
),
1393 Return a const variant of the type." },
1395 { "type-volatile", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_volatile
),
1397 Return a volatile variant of the type." },
1399 { "type-unqualified", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_unqualified
),
1401 Return a variant of the type without const or volatile attributes." },
1403 { "type-num-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_num_fields
),
1405 Return the number of fields of the type." },
1407 { "type-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_fields
),
1409 Return the list of <gdb:field> objects of fields of the type." },
1411 { "make-field-iterator", 1, 0, 0,
1412 as_a_scm_t_subr (gdbscm_make_field_iterator
),
1414 Return a <gdb:iterator> object for iterating over the fields of the type." },
1416 { "type-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_field
),
1418 Return the field named by string of the type.\n\
1420 Arguments: <gdb:type> string" },
1422 { "type-has-field?", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_has_field_p
),
1424 Return #t if the type has field named string.\n\
1426 Arguments: <gdb:type> string" },
1428 { "field?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_p
),
1430 Return #t if the object is a <gdb:field> object." },
1432 { "field-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_name
),
1434 Return the name of the field." },
1436 { "field-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_type
),
1438 Return the type of the field." },
1440 { "field-enumval", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_enumval
),
1442 Return the enum value represented by the field." },
1444 { "field-bitpos", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitpos
),
1446 Return the offset in bits of the field in its containing type." },
1448 { "field-bitsize", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitsize
),
1450 Return the size of the field in bits." },
1452 { "field-artificial?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_artificial_p
),
1454 Return #t if the field is artificial." },
1456 { "field-baseclass?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_baseclass_p
),
1458 Return #t if the field is a baseclass." },
1464 gdbscm_initialize_types (void)
1466 type_smob_tag
= gdbscm_make_smob_type (type_smob_name
, sizeof (type_smob
));
1467 scm_set_smob_free (type_smob_tag
, tyscm_free_type_smob
);
1468 scm_set_smob_print (type_smob_tag
, tyscm_print_type_smob
);
1469 scm_set_smob_equalp (type_smob_tag
, tyscm_equal_p_type_smob
);
1471 field_smob_tag
= gdbscm_make_smob_type (field_smob_name
,
1472 sizeof (field_smob
));
1473 scm_set_smob_print (field_smob_tag
, tyscm_print_field_smob
);
1475 gdbscm_define_integer_constants (type_integer_constants
, 1);
1476 gdbscm_define_functions (type_functions
, 1);
1478 /* This function is "private". */
1479 tyscm_next_field_x_proc
1480 = scm_c_define_gsubr ("%type-next-field!", 1, 0, 0,
1481 as_a_scm_t_subr (gdbscm_type_next_field_x
));
1482 scm_set_procedure_property_x (tyscm_next_field_x_proc
,
1483 gdbscm_documentation_symbol
,
1484 gdbscm_scm_from_c_string ("\
1485 Internal function to assist the type fields iterator."));
1487 block_keyword
= scm_from_latin1_keyword ("block");
1489 global_types_map
= gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob
,
1490 tyscm_eq_type_smob
);