Automatic date update in version.in
[binutils-gdb.git] / gdb / guile / scm-type.c
blob68a5b918e5b8223c4fbbd0c419344e0effc16949
1 /* Scheme interface to types.
3 Copyright (C) 2008-2022 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. */
23 #include "defs.h"
24 #include "arch-utils.h"
25 #include "value.h"
26 #include "gdbtypes.h"
27 #include "objfiles.h"
28 #include "language.h"
29 #include "bcache.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
37 deleted. */
39 struct type_smob
41 /* This always appears first.
42 eqable_gdb_smob is used so that types are eq?-able.
43 Also, a type object can be associated with an objfile. eqable_gdb_smob
44 lets us track the lifetime of all types associated with an objfile.
45 When an objfile is deleted we need to invalidate the type object. */
46 eqable_gdb_smob base;
48 /* The GDB type structure this smob is wrapping. */
49 struct type *type;
52 /* A field smob. */
54 struct field_smob
56 /* This always appears first. */
57 gdb_smob base;
59 /* Backlink to the containing <gdb:type> object. */
60 SCM type_scm;
62 /* The field number in TYPE_SCM. */
63 int field_num;
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. */
89 struct tyscm_deleter
91 void operator() (htab_t htab)
93 if (!gdb_scheme_initialized)
94 return;
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 ());
99 htab_delete (htab);
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. */
114 struct type *
115 tyscm_type_smob_type (type_smob *t_smob)
117 return t_smob->type;
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. */
123 static std::string
124 tyscm_type_name (struct type *type)
126 SCM excp;
129 string_file stb;
131 current_language->print_type (type, "", &stb, -1, 0,
132 &type_print_raw_options);
133 return stb.release ();
135 catch (const gdb_exception &except)
137 excp = gdbscm_scm_from_gdb_exception (unpack (except));
140 gdbscm_throw (excp);
143 /* Administrivia for type smobs. */
145 /* Helper function to hash a type_smob. */
147 static hashval_t
148 tyscm_hash_type_smob (const void *p)
150 const type_smob *t_smob = (const type_smob *) p;
152 return htab_hash_pointer (t_smob->type);
155 /* Helper function to compute equality of type_smobs. */
157 static int
158 tyscm_eq_type_smob (const void *ap, const void *bp)
160 const type_smob *a = (const type_smob *) ap;
161 const type_smob *b = (const type_smob *) bp;
163 return (a->type == b->type
164 && a->type != NULL);
167 /* Return the struct type pointer -> SCM mapping table.
168 If type is owned by an objfile, the mapping table is created if necessary.
169 Otherwise, type is not owned by an objfile, and we use
170 global_types_map. */
172 static htab_t
173 tyscm_type_map (struct type *type)
175 struct objfile *objfile = type->objfile_owner ();
176 htab_t htab;
178 if (objfile == NULL)
179 return global_types_map;
181 htab = tyscm_objfile_data_key.get (objfile);
182 if (htab == NULL)
184 htab = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob,
185 tyscm_eq_type_smob);
186 tyscm_objfile_data_key.set (objfile, htab);
189 return htab;
192 /* The smob "free" function for <gdb:type>. */
194 static size_t
195 tyscm_free_type_smob (SCM self)
197 type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (self);
199 if (t_smob->type != NULL)
201 htab_t htab = tyscm_type_map (t_smob->type);
203 gdbscm_clear_eqable_gsmob_ptr_slot (htab, &t_smob->base);
206 /* Not necessary, done to catch bugs. */
207 t_smob->type = NULL;
209 return 0;
212 /* The smob "print" function for <gdb:type>. */
214 static int
215 tyscm_print_type_smob (SCM self, SCM port, scm_print_state *pstate)
217 type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (self);
218 std::string name = tyscm_type_name (t_smob->type);
220 /* pstate->writingp = zero if invoked by display/~A, and nonzero if
221 invoked by write/~S. What to do here may need to evolve.
222 IWBN if we could pass an argument to format that would we could use
223 instead of writingp. */
224 if (pstate->writingp)
225 gdbscm_printf (port, "#<%s ", type_smob_name);
227 scm_puts (name.c_str (), port);
229 if (pstate->writingp)
230 scm_puts (">", port);
232 scm_remember_upto_here_1 (self);
234 /* Non-zero means success. */
235 return 1;
238 /* The smob "equal?" function for <gdb:type>. */
240 static SCM
241 tyscm_equal_p_type_smob (SCM type1_scm, SCM type2_scm)
243 type_smob *type1_smob, *type2_smob;
244 struct type *type1, *type2;
245 bool result = false;
247 SCM_ASSERT_TYPE (tyscm_is_type (type1_scm), type1_scm, SCM_ARG1, FUNC_NAME,
248 type_smob_name);
249 SCM_ASSERT_TYPE (tyscm_is_type (type2_scm), type2_scm, SCM_ARG2, FUNC_NAME,
250 type_smob_name);
251 type1_smob = (type_smob *) SCM_SMOB_DATA (type1_scm);
252 type2_smob = (type_smob *) SCM_SMOB_DATA (type2_scm);
253 type1 = type1_smob->type;
254 type2 = type2_smob->type;
256 gdbscm_gdb_exception exc {};
259 result = types_deeply_equal (type1, type2);
261 catch (const gdb_exception &except)
263 exc = unpack (except);
266 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
267 return scm_from_bool (result);
270 /* Low level routine to create a <gdb:type> object. */
272 static SCM
273 tyscm_make_type_smob (void)
275 type_smob *t_smob = (type_smob *)
276 scm_gc_malloc (sizeof (type_smob), type_smob_name);
277 SCM t_scm;
279 /* This must be filled in by the caller. */
280 t_smob->type = NULL;
282 t_scm = scm_new_smob (type_smob_tag, (scm_t_bits) t_smob);
283 gdbscm_init_eqable_gsmob (&t_smob->base, t_scm);
285 return t_scm;
288 /* Return non-zero if SCM is a <gdb:type> object. */
291 tyscm_is_type (SCM self)
293 return SCM_SMOB_PREDICATE (type_smob_tag, self);
296 /* (type? object) -> boolean */
298 static SCM
299 gdbscm_type_p (SCM self)
301 return scm_from_bool (tyscm_is_type (self));
304 /* Return the existing object that encapsulates TYPE, or create a new
305 <gdb:type> object. */
308 tyscm_scm_from_type (struct type *type)
310 htab_t htab;
311 eqable_gdb_smob **slot;
312 type_smob *t_smob, t_smob_for_lookup;
313 SCM t_scm;
315 /* If we've already created a gsmob for this type, return it.
316 This makes types eq?-able. */
317 htab = tyscm_type_map (type);
318 t_smob_for_lookup.type = type;
319 slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &t_smob_for_lookup.base);
320 if (*slot != NULL)
321 return (*slot)->containing_scm;
323 t_scm = tyscm_make_type_smob ();
324 t_smob = (type_smob *) SCM_SMOB_DATA (t_scm);
325 t_smob->type = type;
326 gdbscm_fill_eqable_gsmob_ptr_slot (slot, &t_smob->base);
328 return t_scm;
331 /* Returns the <gdb:type> object in SELF.
332 Throws an exception if SELF is not a <gdb:type> object. */
334 static SCM
335 tyscm_get_type_arg_unsafe (SCM self, int arg_pos, const char *func_name)
337 SCM_ASSERT_TYPE (tyscm_is_type (self), self, arg_pos, func_name,
338 type_smob_name);
340 return self;
343 /* Returns a pointer to the type smob of SELF.
344 Throws an exception if SELF is not a <gdb:type> object. */
346 type_smob *
347 tyscm_get_type_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
349 SCM t_scm = tyscm_get_type_arg_unsafe (self, arg_pos, func_name);
350 type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (t_scm);
352 return t_smob;
355 /* Return the type field of T_SCM, an object of type <gdb:type>.
356 This exists so that we don't have to export the struct's contents. */
358 struct type *
359 tyscm_scm_to_type (SCM t_scm)
361 type_smob *t_smob;
363 gdb_assert (tyscm_is_type (t_scm));
364 t_smob = (type_smob *) SCM_SMOB_DATA (t_scm);
365 return t_smob->type;
368 /* Helper function to make a deep copy of the type. */
370 static int
371 tyscm_copy_type_recursive (void **slot, void *info)
373 type_smob *t_smob = (type_smob *) *slot;
374 htab_t copied_types = (htab_t) info;
375 htab_t htab;
376 eqable_gdb_smob **new_slot;
377 type_smob t_smob_for_lookup;
379 htab_empty (copied_types);
380 t_smob->type = copy_type_recursive (t_smob->type, copied_types);
382 /* The eq?-hashtab that the type lived in is going away.
383 Add the type to its new eq?-hashtab: Otherwise if/when the type is later
384 garbage collected we'll assert-fail if the type isn't in the hashtab.
385 PR 16612.
387 Types now live in "arch space", and things like "char" that came from
388 the objfile *could* be considered eq? with the arch "char" type.
389 However, they weren't before the objfile got deleted, so making them
390 eq? now is debatable. */
391 htab = tyscm_type_map (t_smob->type);
392 t_smob_for_lookup.type = t_smob->type;
393 new_slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &t_smob_for_lookup.base);
394 gdb_assert (*new_slot == NULL);
395 gdbscm_fill_eqable_gsmob_ptr_slot (new_slot, &t_smob->base);
397 return 1;
401 /* Administrivia for field smobs. */
403 /* The smob "print" function for <gdb:field>. */
405 static int
406 tyscm_print_field_smob (SCM self, SCM port, scm_print_state *pstate)
408 field_smob *f_smob = (field_smob *) SCM_SMOB_DATA (self);
410 gdbscm_printf (port, "#<%s ", field_smob_name);
411 scm_write (f_smob->type_scm, port);
412 gdbscm_printf (port, " %d", f_smob->field_num);
413 scm_puts (">", port);
415 scm_remember_upto_here_1 (self);
417 /* Non-zero means success. */
418 return 1;
421 /* Low level routine to create a <gdb:field> object for field FIELD_NUM
422 of type TYPE_SCM. */
424 static SCM
425 tyscm_make_field_smob (SCM type_scm, int field_num)
427 field_smob *f_smob = (field_smob *)
428 scm_gc_malloc (sizeof (field_smob), field_smob_name);
429 SCM result;
431 f_smob->type_scm = type_scm;
432 f_smob->field_num = field_num;
433 result = scm_new_smob (field_smob_tag, (scm_t_bits) f_smob);
434 gdbscm_init_gsmob (&f_smob->base);
436 return result;
439 /* Return non-zero if SCM is a <gdb:field> object. */
441 static int
442 tyscm_is_field (SCM self)
444 return SCM_SMOB_PREDICATE (field_smob_tag, self);
447 /* (field? object) -> boolean */
449 static SCM
450 gdbscm_field_p (SCM self)
452 return scm_from_bool (tyscm_is_field (self));
455 /* Create a new <gdb:field> object that encapsulates field FIELD_NUM
456 in type TYPE_SCM. */
459 tyscm_scm_from_field (SCM type_scm, int field_num)
461 return tyscm_make_field_smob (type_scm, field_num);
464 /* Returns the <gdb:field> object in SELF.
465 Throws an exception if SELF is not a <gdb:field> object. */
467 static SCM
468 tyscm_get_field_arg_unsafe (SCM self, int arg_pos, const char *func_name)
470 SCM_ASSERT_TYPE (tyscm_is_field (self), self, arg_pos, func_name,
471 field_smob_name);
473 return self;
476 /* Returns a pointer to the field smob of SELF.
477 Throws an exception if SELF is not a <gdb:field> object. */
479 static field_smob *
480 tyscm_get_field_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
482 SCM f_scm = tyscm_get_field_arg_unsafe (self, arg_pos, func_name);
483 field_smob *f_smob = (field_smob *) SCM_SMOB_DATA (f_scm);
485 return f_smob;
488 /* Returns a pointer to the type struct in F_SMOB
489 (the type the field is in). */
491 static struct type *
492 tyscm_field_smob_containing_type (field_smob *f_smob)
494 type_smob *t_smob;
496 gdb_assert (tyscm_is_type (f_smob->type_scm));
497 t_smob = (type_smob *) SCM_SMOB_DATA (f_smob->type_scm);
499 return t_smob->type;
502 /* Returns a pointer to the field struct of F_SMOB. */
504 static struct field *
505 tyscm_field_smob_to_field (field_smob *f_smob)
507 struct type *type = tyscm_field_smob_containing_type (f_smob);
509 /* This should be non-NULL by construction. */
510 gdb_assert (type->fields () != NULL);
512 return &type->field (f_smob->field_num);
515 /* Type smob accessors. */
517 /* (type-code <gdb:type>) -> integer
518 Return the code for this type. */
520 static SCM
521 gdbscm_type_code (SCM self)
523 type_smob *t_smob
524 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
525 struct type *type = t_smob->type;
527 return scm_from_int (type->code ());
530 /* (type-fields <gdb:type>) -> list
531 Return a list of all fields. Each element is a <gdb:field> object.
532 This also supports arrays, we return a field list of one element,
533 the range type. */
535 static SCM
536 gdbscm_type_fields (SCM self)
538 type_smob *t_smob
539 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
540 struct type *type = t_smob->type;
541 struct type *containing_type;
542 SCM containing_type_scm, result;
543 int i;
545 containing_type = tyscm_get_composite (type);
546 if (containing_type == NULL)
547 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
548 _(not_composite_error));
550 /* If SELF is a typedef or reference, we want the underlying type,
551 which is what tyscm_get_composite returns. */
552 if (containing_type == type)
553 containing_type_scm = self;
554 else
555 containing_type_scm = tyscm_scm_from_type (containing_type);
557 result = SCM_EOL;
558 for (i = 0; i < containing_type->num_fields (); ++i)
559 result = scm_cons (tyscm_make_field_smob (containing_type_scm, i), result);
561 return scm_reverse_x (result, SCM_EOL);
564 /* (type-tag <gdb:type>) -> string
565 Return the type's tag, or #f. */
567 static SCM
568 gdbscm_type_tag (SCM self)
570 type_smob *t_smob
571 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
572 struct type *type = t_smob->type;
573 const char *tagname = nullptr;
575 if (type->code () == TYPE_CODE_STRUCT
576 || type->code () == TYPE_CODE_UNION
577 || type->code () == TYPE_CODE_ENUM)
578 tagname = type->name ();
580 if (tagname == nullptr)
581 return SCM_BOOL_F;
582 return gdbscm_scm_from_c_string (tagname);
585 /* (type-name <gdb:type>) -> string
586 Return the type's name, or #f. */
588 static SCM
589 gdbscm_type_name (SCM self)
591 type_smob *t_smob
592 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
593 struct type *type = t_smob->type;
595 if (!type->name ())
596 return SCM_BOOL_F;
597 return gdbscm_scm_from_c_string (type->name ());
600 /* (type-print-name <gdb:type>) -> string
601 Return the print name of type.
602 TODO: template support elided for now. */
604 static SCM
605 gdbscm_type_print_name (SCM self)
607 type_smob *t_smob
608 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
609 struct type *type = t_smob->type;
610 std::string thetype = tyscm_type_name (type);
611 SCM result = gdbscm_scm_from_c_string (thetype.c_str ());
613 return result;
616 /* (type-sizeof <gdb:type>) -> integer
617 Return the size of the type represented by SELF, in bytes. */
619 static SCM
620 gdbscm_type_sizeof (SCM self)
622 type_smob *t_smob
623 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
624 struct type *type = t_smob->type;
628 check_typedef (type);
630 catch (const gdb_exception &except)
634 /* Ignore exceptions. */
636 return scm_from_long (type->length ());
639 /* (type-strip-typedefs <gdb:type>) -> <gdb:type>
640 Return the type, stripped of typedefs. */
642 static SCM
643 gdbscm_type_strip_typedefs (SCM self)
645 type_smob *t_smob
646 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
647 struct type *type = t_smob->type;
649 gdbscm_gdb_exception exc {};
652 type = check_typedef (type);
654 catch (const gdb_exception &except)
656 exc = unpack (except);
659 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
660 return tyscm_scm_from_type (type);
663 /* Strip typedefs and pointers/reference from a type. Then check that
664 it is a struct, union, or enum type. If not, return NULL. */
666 static struct type *
667 tyscm_get_composite (struct type *type)
670 for (;;)
672 gdbscm_gdb_exception exc {};
675 type = check_typedef (type);
677 catch (const gdb_exception &except)
679 exc = unpack (except);
682 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
683 if (type->code () != TYPE_CODE_PTR
684 && type->code () != TYPE_CODE_REF)
685 break;
686 type = type->target_type ();
689 /* If this is not a struct, union, or enum type, raise TypeError
690 exception. */
691 if (type->code () != TYPE_CODE_STRUCT
692 && type->code () != TYPE_CODE_UNION
693 && type->code () != TYPE_CODE_ENUM)
694 return NULL;
696 return type;
699 /* Helper for tyscm_array and tyscm_vector. */
701 static SCM
702 tyscm_array_1 (SCM self, SCM n1_scm, SCM n2_scm, int is_vector,
703 const char *func_name)
705 type_smob *t_smob
706 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, func_name);
707 struct type *type = t_smob->type;
708 long n1, n2 = 0;
709 struct type *array = NULL;
711 gdbscm_parse_function_args (func_name, SCM_ARG2, NULL, "l|l",
712 n1_scm, &n1, n2_scm, &n2);
714 if (SCM_UNBNDP (n2_scm))
716 n2 = n1;
717 n1 = 0;
720 if (n2 < n1 - 1) /* Note: An empty array has n2 == n1 - 1. */
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"));
728 gdbscm_gdb_exception exc {};
731 array = lookup_array_range_type (type, n1, n2);
732 if (is_vector)
733 make_vector_type (array);
735 catch (const gdb_exception &except)
737 exc = unpack (except);
740 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
741 return tyscm_scm_from_type (array);
744 /* (type-array <gdb:type> [low-bound] high-bound) -> <gdb:type>
745 The array has indices [low-bound,high-bound].
746 If low-bound is not provided zero is used.
747 Return an array type.
749 IWBN if the one argument version specified a size, not the high bound.
750 It's too easy to pass one argument thinking it is the size of the array.
751 The current semantics are for compatibility with the Python version.
752 Later we can add #:size. */
754 static SCM
755 gdbscm_type_array (SCM self, SCM n1, SCM n2)
757 return tyscm_array_1 (self, n1, n2, 0, FUNC_NAME);
760 /* (type-vector <gdb:type> [low-bound] high-bound) -> <gdb:type>
761 The array has indices [low-bound,high-bound].
762 If low-bound is not provided zero is used.
763 Return a vector type.
765 IWBN if the one argument version specified a size, not the high bound.
766 It's too easy to pass one argument thinking it is the size of the array.
767 The current semantics are for compatibility with the Python version.
768 Later we can add #:size. */
770 static SCM
771 gdbscm_type_vector (SCM self, SCM n1, SCM n2)
773 return tyscm_array_1 (self, n1, n2, 1, FUNC_NAME);
776 /* (type-pointer <gdb:type>) -> <gdb:type>
777 Return a <gdb:type> object which represents a pointer to SELF. */
779 static SCM
780 gdbscm_type_pointer (SCM self)
782 type_smob *t_smob
783 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
784 struct type *type = t_smob->type;
786 gdbscm_gdb_exception exc {};
789 type = lookup_pointer_type (type);
791 catch (const gdb_exception &except)
793 exc = unpack (except);
796 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
797 return tyscm_scm_from_type (type);
800 /* (type-range <gdb:type>) -> (low high)
801 Return the range of a type represented by SELF. The return type is
802 a list. The first element is the low bound, and the second element
803 is the high bound. */
805 static SCM
806 gdbscm_type_range (SCM self)
808 type_smob *t_smob
809 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
810 struct type *type = t_smob->type;
811 SCM low_scm, high_scm;
812 /* Initialize these to appease GCC warnings. */
813 LONGEST low = 0, high = 0;
815 SCM_ASSERT_TYPE (type->code () == TYPE_CODE_ARRAY
816 || type->code () == TYPE_CODE_STRING
817 || type->code () == TYPE_CODE_RANGE,
818 self, SCM_ARG1, FUNC_NAME, _("ranged type"));
820 switch (type->code ())
822 case TYPE_CODE_ARRAY:
823 case TYPE_CODE_STRING:
824 case TYPE_CODE_RANGE:
825 if (type->bounds ()->low.kind () == PROP_CONST)
826 low = type->bounds ()->low.const_val ();
827 else
828 low = 0;
830 if (type->bounds ()->high.kind () == PROP_CONST)
831 high = type->bounds ()->high.const_val ();
832 else
833 high = 0;
834 break;
837 low_scm = gdbscm_scm_from_longest (low);
838 high_scm = gdbscm_scm_from_longest (high);
840 return scm_list_2 (low_scm, high_scm);
843 /* (type-reference <gdb:type>) -> <gdb:type>
844 Return a <gdb:type> object which represents a reference to SELF. */
846 static SCM
847 gdbscm_type_reference (SCM self)
849 type_smob *t_smob
850 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
851 struct type *type = t_smob->type;
853 gdbscm_gdb_exception exc {};
856 type = lookup_lvalue_reference_type (type);
858 catch (const gdb_exception &except)
860 exc = unpack (except);
863 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
864 return tyscm_scm_from_type (type);
867 /* (type-target <gdb:type>) -> <gdb:type>
868 Return a <gdb:type> object which represents the target type of SELF. */
870 static SCM
871 gdbscm_type_target (SCM self)
873 type_smob *t_smob
874 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
875 struct type *type = t_smob->type;
877 SCM_ASSERT (type->target_type (), self, SCM_ARG1, FUNC_NAME);
879 return tyscm_scm_from_type (type->target_type ());
882 /* (type-const <gdb:type>) -> <gdb:type>
883 Return a const-qualified type variant. */
885 static SCM
886 gdbscm_type_const (SCM self)
888 type_smob *t_smob
889 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
890 struct type *type = t_smob->type;
892 gdbscm_gdb_exception exc {};
895 type = make_cv_type (1, 0, type, NULL);
897 catch (const gdb_exception &except)
899 exc = unpack (except);
902 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
903 return tyscm_scm_from_type (type);
906 /* (type-volatile <gdb:type>) -> <gdb:type>
907 Return a volatile-qualified type variant. */
909 static SCM
910 gdbscm_type_volatile (SCM self)
912 type_smob *t_smob
913 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
914 struct type *type = t_smob->type;
916 gdbscm_gdb_exception exc {};
919 type = make_cv_type (0, 1, type, NULL);
921 catch (const gdb_exception &except)
923 exc = unpack (except);
926 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
927 return tyscm_scm_from_type (type);
930 /* (type-unqualified <gdb:type>) -> <gdb:type>
931 Return an unqualified type variant. */
933 static SCM
934 gdbscm_type_unqualified (SCM self)
936 type_smob *t_smob
937 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
938 struct type *type = t_smob->type;
940 gdbscm_gdb_exception exc {};
943 type = make_cv_type (0, 0, type, NULL);
945 catch (const gdb_exception &except)
947 exc = unpack (except);
950 GDBSCM_HANDLE_GDB_EXCEPTION (exc);
951 return tyscm_scm_from_type (type);
954 /* Field related accessors of types. */
956 /* (type-num-fields <gdb:type>) -> integer
957 Return number of fields. */
959 static SCM
960 gdbscm_type_num_fields (SCM self)
962 type_smob *t_smob
963 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
964 struct type *type = t_smob->type;
966 type = tyscm_get_composite (type);
967 if (type == NULL)
968 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
969 _(not_composite_error));
971 return scm_from_long (type->num_fields ());
974 /* (type-field <gdb:type> string) -> <gdb:field>
975 Return the <gdb:field> object for the field named by the argument. */
977 static SCM
978 gdbscm_type_field (SCM self, SCM field_scm)
980 type_smob *t_smob
981 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
982 struct type *type = t_smob->type;
984 SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME,
985 _("string"));
987 /* We want just fields of this type, not of base types, so instead of
988 using lookup_struct_elt_type, portions of that function are
989 copied here. */
991 type = tyscm_get_composite (type);
992 if (type == NULL)
993 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
994 _(not_composite_error));
997 gdb::unique_xmalloc_ptr<char> field = gdbscm_scm_to_c_string (field_scm);
999 for (int i = 0; i < type->num_fields (); i++)
1001 const char *t_field_name = type->field (i).name ();
1003 if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0))
1005 field.reset (nullptr);
1006 return tyscm_make_field_smob (self, i);
1011 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, field_scm,
1012 _("Unknown field"));
1015 /* (type-has-field? <gdb:type> string) -> boolean
1016 Return boolean indicating if type SELF has FIELD_SCM (a string). */
1018 static SCM
1019 gdbscm_type_has_field_p (SCM self, SCM field_scm)
1021 type_smob *t_smob
1022 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1023 struct type *type = t_smob->type;
1025 SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME,
1026 _("string"));
1028 /* We want just fields of this type, not of base types, so instead of
1029 using lookup_struct_elt_type, portions of that function are
1030 copied here. */
1032 type = tyscm_get_composite (type);
1033 if (type == NULL)
1034 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
1035 _(not_composite_error));
1038 gdb::unique_xmalloc_ptr<char> field
1039 = gdbscm_scm_to_c_string (field_scm);
1041 for (int i = 0; i < type->num_fields (); i++)
1043 const char *t_field_name = type->field (i).name ();
1045 if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0))
1046 return SCM_BOOL_T;
1050 return SCM_BOOL_F;
1053 /* (make-field-iterator <gdb:type>) -> <gdb:iterator>
1054 Make a field iterator object. */
1056 static SCM
1057 gdbscm_make_field_iterator (SCM self)
1059 type_smob *t_smob
1060 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1061 struct type *type = t_smob->type;
1062 struct type *containing_type;
1063 SCM containing_type_scm;
1065 containing_type = tyscm_get_composite (type);
1066 if (containing_type == NULL)
1067 gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
1068 _(not_composite_error));
1070 /* If SELF is a typedef or reference, we want the underlying type,
1071 which is what tyscm_get_composite returns. */
1072 if (containing_type == type)
1073 containing_type_scm = self;
1074 else
1075 containing_type_scm = tyscm_scm_from_type (containing_type);
1077 return gdbscm_make_iterator (containing_type_scm, scm_from_int (0),
1078 tyscm_next_field_x_proc);
1081 /* (type-next-field! <gdb:iterator>) -> <gdb:field>
1082 Return the next field in the iteration through the list of fields of the
1083 type, or (end-of-iteration).
1084 SELF is a <gdb:iterator> object created by gdbscm_make_field_iterator.
1085 This is the next! <gdb:iterator> function, not exported to the user. */
1087 static SCM
1088 gdbscm_type_next_field_x (SCM self)
1090 iterator_smob *i_smob;
1091 type_smob *t_smob;
1092 struct type *type;
1093 SCM it_scm, result, progress, object;
1094 int field;
1096 it_scm = itscm_get_iterator_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1097 i_smob = (iterator_smob *) SCM_SMOB_DATA (it_scm);
1098 object = itscm_iterator_smob_object (i_smob);
1099 progress = itscm_iterator_smob_progress (i_smob);
1101 SCM_ASSERT_TYPE (tyscm_is_type (object), object,
1102 SCM_ARG1, FUNC_NAME, type_smob_name);
1103 t_smob = (type_smob *) SCM_SMOB_DATA (object);
1104 type = t_smob->type;
1106 SCM_ASSERT_TYPE (scm_is_signed_integer (progress,
1107 0, type->num_fields ()),
1108 progress, SCM_ARG1, FUNC_NAME, _("integer"));
1109 field = scm_to_int (progress);
1111 if (field < type->num_fields ())
1113 result = tyscm_make_field_smob (object, field);
1114 itscm_set_iterator_smob_progress_x (i_smob, scm_from_int (field + 1));
1115 return result;
1118 return gdbscm_end_of_iteration ();
1121 /* Field smob accessors. */
1123 /* (field-name <gdb:field>) -> string
1124 Return the name of this field or #f if there isn't one. */
1126 static SCM
1127 gdbscm_field_name (SCM self)
1129 field_smob *f_smob
1130 = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1131 struct field *field = tyscm_field_smob_to_field (f_smob);
1133 if (field->name () != nullptr)
1134 return gdbscm_scm_from_c_string (field->name ());
1135 return SCM_BOOL_F;
1138 /* (field-type <gdb:field>) -> <gdb:type>
1139 Return the <gdb:type> object of the field or #f if there isn't one. */
1141 static SCM
1142 gdbscm_field_type (SCM self)
1144 field_smob *f_smob
1145 = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1146 struct field *field = tyscm_field_smob_to_field (f_smob);
1148 /* A field can have a NULL type in some situations. */
1149 if (field->type ())
1150 return tyscm_scm_from_type (field->type ());
1151 return SCM_BOOL_F;
1154 /* (field-enumval <gdb:field>) -> integer
1155 For enum values, return its value as an integer. */
1157 static SCM
1158 gdbscm_field_enumval (SCM self)
1160 field_smob *f_smob
1161 = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1162 struct field *field = tyscm_field_smob_to_field (f_smob);
1163 struct type *type = tyscm_field_smob_containing_type (f_smob);
1165 SCM_ASSERT_TYPE (type->code () == TYPE_CODE_ENUM,
1166 self, SCM_ARG1, FUNC_NAME, _("enum type"));
1168 return scm_from_long (field->loc_enumval ());
1171 /* (field-bitpos <gdb:field>) -> integer
1172 For bitfields, return its offset in bits. */
1174 static SCM
1175 gdbscm_field_bitpos (SCM self)
1177 field_smob *f_smob
1178 = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1179 struct field *field = tyscm_field_smob_to_field (f_smob);
1180 struct type *type = tyscm_field_smob_containing_type (f_smob);
1182 SCM_ASSERT_TYPE (type->code () != TYPE_CODE_ENUM,
1183 self, SCM_ARG1, FUNC_NAME, _("non-enum type"));
1185 return scm_from_long (field->loc_bitpos ());
1188 /* (field-bitsize <gdb:field>) -> integer
1189 Return the size of the field in bits. */
1191 static SCM
1192 gdbscm_field_bitsize (SCM self)
1194 field_smob *f_smob
1195 = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1196 struct field *field = tyscm_field_smob_to_field (f_smob);
1198 return scm_from_long (field->loc_bitpos ());
1201 /* (field-artificial? <gdb:field>) -> boolean
1202 Return #t if field is artificial. */
1204 static SCM
1205 gdbscm_field_artificial_p (SCM self)
1207 field_smob *f_smob
1208 = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1209 struct field *field = tyscm_field_smob_to_field (f_smob);
1211 return scm_from_bool (FIELD_ARTIFICIAL (*field));
1214 /* (field-baseclass? <gdb:field>) -> boolean
1215 Return #t if field is a baseclass. */
1217 static SCM
1218 gdbscm_field_baseclass_p (SCM self)
1220 field_smob *f_smob
1221 = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1222 struct type *type = tyscm_field_smob_containing_type (f_smob);
1224 if (type->code () == TYPE_CODE_STRUCT)
1225 return scm_from_bool (f_smob->field_num < TYPE_N_BASECLASSES (type));
1226 return SCM_BOOL_F;
1229 /* Return the type named TYPE_NAME in BLOCK.
1230 Returns NULL if not found.
1231 This routine does not throw an error. */
1233 static struct type *
1234 tyscm_lookup_typename (const char *type_name, const struct block *block)
1236 struct type *type = NULL;
1240 if (startswith (type_name, "struct "))
1241 type = lookup_struct (type_name + 7, NULL);
1242 else if (startswith (type_name, "union "))
1243 type = lookup_union (type_name + 6, NULL);
1244 else if (startswith (type_name, "enum "))
1245 type = lookup_enum (type_name + 5, NULL);
1246 else
1247 type = lookup_typename (current_language,
1248 type_name, block, 0);
1250 catch (const gdb_exception &except)
1252 return NULL;
1255 return type;
1258 /* (lookup-type name [#:block <gdb:block>]) -> <gdb:type>
1259 TODO: legacy template support left out until needed. */
1261 static SCM
1262 gdbscm_lookup_type (SCM name_scm, SCM rest)
1264 SCM keywords[] = { block_keyword, SCM_BOOL_F };
1265 char *name;
1266 SCM block_scm = SCM_BOOL_F;
1267 int block_arg_pos = -1;
1268 const struct block *block = NULL;
1269 struct type *type;
1271 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#O",
1272 name_scm, &name,
1273 rest, &block_arg_pos, &block_scm);
1275 if (block_arg_pos != -1)
1277 SCM exception;
1279 block = bkscm_scm_to_block (block_scm, block_arg_pos, FUNC_NAME,
1280 &exception);
1281 if (block == NULL)
1283 xfree (name);
1284 gdbscm_throw (exception);
1287 type = tyscm_lookup_typename (name, block);
1288 xfree (name);
1290 if (type != NULL)
1291 return tyscm_scm_from_type (type);
1292 return SCM_BOOL_F;
1295 /* Initialize the Scheme type code. */
1298 static const scheme_integer_constant type_integer_constants[] =
1300 #define X(SYM) { #SYM, SYM }
1301 X (TYPE_CODE_BITSTRING),
1302 X (TYPE_CODE_PTR),
1303 X (TYPE_CODE_ARRAY),
1304 X (TYPE_CODE_STRUCT),
1305 X (TYPE_CODE_UNION),
1306 X (TYPE_CODE_ENUM),
1307 X (TYPE_CODE_FLAGS),
1308 X (TYPE_CODE_FUNC),
1309 X (TYPE_CODE_INT),
1310 X (TYPE_CODE_FLT),
1311 X (TYPE_CODE_VOID),
1312 X (TYPE_CODE_SET),
1313 X (TYPE_CODE_RANGE),
1314 X (TYPE_CODE_STRING),
1315 X (TYPE_CODE_ERROR),
1316 X (TYPE_CODE_METHOD),
1317 X (TYPE_CODE_METHODPTR),
1318 X (TYPE_CODE_MEMBERPTR),
1319 X (TYPE_CODE_REF),
1320 X (TYPE_CODE_RVALUE_REF),
1321 X (TYPE_CODE_CHAR),
1322 X (TYPE_CODE_BOOL),
1323 X (TYPE_CODE_COMPLEX),
1324 X (TYPE_CODE_TYPEDEF),
1325 X (TYPE_CODE_NAMESPACE),
1326 X (TYPE_CODE_DECFLOAT),
1327 X (TYPE_CODE_INTERNAL_FUNCTION),
1328 #undef X
1330 END_INTEGER_CONSTANTS
1333 static const scheme_function type_functions[] =
1335 { "type?", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_p),
1337 Return #t if the object is a <gdb:type> object." },
1339 { "lookup-type", 1, 0, 1, as_a_scm_t_subr (gdbscm_lookup_type),
1341 Return the <gdb:type> object representing string or #f if not found.\n\
1342 If block is given then the type is looked for in that block.\n\
1344 Arguments: string [#:block <gdb:block>]" },
1346 { "type-code", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_code),
1348 Return the code of the type" },
1350 { "type-tag", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_tag),
1352 Return the tag name of the type, or #f if there isn't one." },
1354 { "type-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_name),
1356 Return the name of the type as a string, or #f if there isn't one." },
1358 { "type-print-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_print_name),
1360 Return the print name of the type as a string." },
1362 { "type-sizeof", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_sizeof),
1364 Return the size of the type, in bytes." },
1366 { "type-strip-typedefs", 1, 0, 0,
1367 as_a_scm_t_subr (gdbscm_type_strip_typedefs),
1369 Return a type formed by stripping the type of all typedefs." },
1371 { "type-array", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_array),
1373 Return a type representing an array of objects of the type.\n\
1375 Arguments: <gdb:type> [low-bound] high-bound\n\
1376 If low-bound is not provided zero is used.\n\
1377 N.B. If only the high-bound parameter is specified, it is not\n\
1378 the array size.\n\
1379 Valid bounds for array indices are [low-bound,high-bound]." },
1381 { "type-vector", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_vector),
1383 Return a type representing a vector of objects of the type.\n\
1384 Vectors differ from arrays in that if the current language has C-style\n\
1385 arrays, vectors don't decay to a pointer to the first element.\n\
1386 They are first class values.\n\
1388 Arguments: <gdb:type> [low-bound] high-bound\n\
1389 If low-bound is not provided zero is used.\n\
1390 N.B. If only the high-bound parameter is specified, it is not\n\
1391 the array size.\n\
1392 Valid bounds for array indices are [low-bound,high-bound]." },
1394 { "type-pointer", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_pointer),
1396 Return a type of pointer to the type." },
1398 { "type-range", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_range),
1400 Return (low high) representing the range for the type." },
1402 { "type-reference", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_reference),
1404 Return a type of reference to the type." },
1406 { "type-target", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_target),
1408 Return the target type of the type." },
1410 { "type-const", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_const),
1412 Return a const variant of the type." },
1414 { "type-volatile", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_volatile),
1416 Return a volatile variant of the type." },
1418 { "type-unqualified", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_unqualified),
1420 Return a variant of the type without const or volatile attributes." },
1422 { "type-num-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_num_fields),
1424 Return the number of fields of the type." },
1426 { "type-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_fields),
1428 Return the list of <gdb:field> objects of fields of the type." },
1430 { "make-field-iterator", 1, 0, 0,
1431 as_a_scm_t_subr (gdbscm_make_field_iterator),
1433 Return a <gdb:iterator> object for iterating over the fields of the type." },
1435 { "type-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_field),
1437 Return the field named by string of the type.\n\
1439 Arguments: <gdb:type> string" },
1441 { "type-has-field?", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_has_field_p),
1443 Return #t if the type has field named string.\n\
1445 Arguments: <gdb:type> string" },
1447 { "field?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_p),
1449 Return #t if the object is a <gdb:field> object." },
1451 { "field-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_name),
1453 Return the name of the field." },
1455 { "field-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_type),
1457 Return the type of the field." },
1459 { "field-enumval", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_enumval),
1461 Return the enum value represented by the field." },
1463 { "field-bitpos", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitpos),
1465 Return the offset in bits of the field in its containing type." },
1467 { "field-bitsize", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitsize),
1469 Return the size of the field in bits." },
1471 { "field-artificial?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_artificial_p),
1473 Return #t if the field is artificial." },
1475 { "field-baseclass?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_baseclass_p),
1477 Return #t if the field is a baseclass." },
1479 END_FUNCTIONS
1482 void
1483 gdbscm_initialize_types (void)
1485 type_smob_tag = gdbscm_make_smob_type (type_smob_name, sizeof (type_smob));
1486 scm_set_smob_free (type_smob_tag, tyscm_free_type_smob);
1487 scm_set_smob_print (type_smob_tag, tyscm_print_type_smob);
1488 scm_set_smob_equalp (type_smob_tag, tyscm_equal_p_type_smob);
1490 field_smob_tag = gdbscm_make_smob_type (field_smob_name,
1491 sizeof (field_smob));
1492 scm_set_smob_print (field_smob_tag, tyscm_print_field_smob);
1494 gdbscm_define_integer_constants (type_integer_constants, 1);
1495 gdbscm_define_functions (type_functions, 1);
1497 /* This function is "private". */
1498 tyscm_next_field_x_proc
1499 = scm_c_define_gsubr ("%type-next-field!", 1, 0, 0,
1500 as_a_scm_t_subr (gdbscm_type_next_field_x));
1501 scm_set_procedure_property_x (tyscm_next_field_x_proc,
1502 gdbscm_documentation_symbol,
1503 gdbscm_scm_from_c_string ("\
1504 Internal function to assist the type fields iterator."));
1506 block_keyword = scm_from_latin1_keyword ("block");
1508 global_types_map = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob,
1509 tyscm_eq_type_smob);