Bump GDB's version number to 15.0.91.DATE-git.
[binutils-gdb.git] / gdb / guile / scm-type.c
blob19324a69810c49bb9ee8bb74930c7d244b6e6e21
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. */
23 #include "top.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_forced_quit &except)
137 quit_force (NULL, 0);
139 catch (const gdb_exception &except)
141 excp = gdbscm_scm_from_gdb_exception (unpack (except));
144 gdbscm_throw (excp);
147 /* Administrivia for type smobs. */
149 /* Helper function to hash a type_smob. */
151 static hashval_t
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. */
161 static int
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
168 && a->type != NULL);
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
174 global_types_map. */
176 static htab_t
177 tyscm_type_map (struct type *type)
179 struct objfile *objfile = type->objfile_owner ();
180 htab_t htab;
182 if (objfile == NULL)
183 return global_types_map;
185 htab = tyscm_objfile_data_key.get (objfile);
186 if (htab == NULL)
188 htab = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob,
189 tyscm_eq_type_smob);
190 tyscm_objfile_data_key.set (objfile, htab);
193 return htab;
196 /* The smob "free" function for <gdb:type>. */
198 static size_t
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. */
211 t_smob->type = NULL;
213 return 0;
216 /* The smob "print" function for <gdb:type>. */
218 static int
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. */
239 return 1;
242 /* The smob "equal?" function for <gdb:type>. */
244 static SCM
245 tyscm_equal_p_type_smob (SCM type1_scm, SCM type2_scm)
247 type_smob *type1_smob, *type2_smob;
248 struct type *type1, *type2;
249 bool result = false;
251 SCM_ASSERT_TYPE (tyscm_is_type (type1_scm), type1_scm, SCM_ARG1, FUNC_NAME,
252 type_smob_name);
253 SCM_ASSERT_TYPE (tyscm_is_type (type2_scm), type2_scm, SCM_ARG2, FUNC_NAME,
254 type_smob_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. */
276 static SCM
277 tyscm_make_type_smob (void)
279 type_smob *t_smob = (type_smob *)
280 scm_gc_malloc (sizeof (type_smob), type_smob_name);
281 SCM t_scm;
283 /* This must be filled in by the caller. */
284 t_smob->type = NULL;
286 t_scm = scm_new_smob (type_smob_tag, (scm_t_bits) t_smob);
287 gdbscm_init_eqable_gsmob (&t_smob->base, t_scm);
289 return 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 */
302 static SCM
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)
314 htab_t htab;
315 eqable_gdb_smob **slot;
316 type_smob *t_smob, t_smob_for_lookup;
317 SCM t_scm;
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);
324 if (*slot != NULL)
325 return (*slot)->containing_scm;
327 t_scm = tyscm_make_type_smob ();
328 t_smob = (type_smob *) SCM_SMOB_DATA (t_scm);
329 t_smob->type = type;
330 gdbscm_fill_eqable_gsmob_ptr_slot (slot, &t_smob->base);
332 return t_scm;
335 /* Returns the <gdb:type> object in SELF.
336 Throws an exception if SELF is not a <gdb:type> object. */
338 static SCM
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,
342 type_smob_name);
344 return self;
347 /* Returns a pointer to the type smob of SELF.
348 Throws an exception if SELF is not a <gdb:type> object. */
350 type_smob *
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);
356 return t_smob;
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. */
362 struct type *
363 tyscm_scm_to_type (SCM t_scm)
365 type_smob *t_smob;
367 gdb_assert (tyscm_is_type (t_scm));
368 t_smob = (type_smob *) SCM_SMOB_DATA (t_scm);
369 return t_smob->type;
372 /* Helper function to make a deep copy of the type. */
374 static int
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;
379 htab_t htab;
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.
389 PR 16612.
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);
401 return 1;
405 /* Administrivia for field smobs. */
407 /* The smob "print" function for <gdb:field>. */
409 static int
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. */
422 return 1;
425 /* Low level routine to create a <gdb:field> object for field FIELD_NUM
426 of type TYPE_SCM. */
428 static SCM
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);
433 SCM result;
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);
440 return result;
443 /* Return non-zero if SCM is a <gdb:field> object. */
445 static int
446 tyscm_is_field (SCM self)
448 return SCM_SMOB_PREDICATE (field_smob_tag, self);
451 /* (field? object) -> boolean */
453 static SCM
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
460 in type TYPE_SCM. */
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. */
471 static SCM
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,
475 field_smob_name);
477 return self;
480 /* Returns a pointer to the field smob of SELF.
481 Throws an exception if SELF is not a <gdb:field> object. */
483 static field_smob *
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);
489 return f_smob;
492 /* Returns a pointer to the type struct in F_SMOB
493 (the type the field is in). */
495 static struct type *
496 tyscm_field_smob_containing_type (field_smob *f_smob)
498 type_smob *t_smob;
500 gdb_assert (tyscm_is_type (f_smob->type_scm));
501 t_smob = (type_smob *) SCM_SMOB_DATA (f_smob->type_scm);
503 return t_smob->type;
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. */
524 static SCM
525 gdbscm_type_code (SCM self)
527 type_smob *t_smob
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,
537 the range type. */
539 static SCM
540 gdbscm_type_fields (SCM self)
542 type_smob *t_smob
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;
547 int i;
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;
558 else
559 containing_type_scm = tyscm_scm_from_type (containing_type);
561 result = SCM_EOL;
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. */
571 static SCM
572 gdbscm_type_tag (SCM self)
574 type_smob *t_smob
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)
585 return SCM_BOOL_F;
586 return gdbscm_scm_from_c_string (tagname);
589 /* (type-name <gdb:type>) -> string
590 Return the type's name, or #f. */
592 static SCM
593 gdbscm_type_name (SCM self)
595 type_smob *t_smob
596 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
597 struct type *type = t_smob->type;
599 if (!type->name ())
600 return SCM_BOOL_F;
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. */
608 static SCM
609 gdbscm_type_print_name (SCM self)
611 type_smob *t_smob
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 ());
617 return result;
620 /* (type-sizeof <gdb:type>) -> integer
621 Return the size of the type represented by SELF, in bytes. */
623 static SCM
624 gdbscm_type_sizeof (SCM self)
626 type_smob *t_smob
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. */
646 static SCM
647 gdbscm_type_strip_typedefs (SCM self)
649 type_smob *t_smob
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. */
670 static struct type *
671 tyscm_get_composite (struct type *type)
674 for (;;)
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)
689 break;
690 type = type->target_type ();
693 /* If this is not a struct, union, or enum type, raise TypeError
694 exception. */
695 if (type->code () != TYPE_CODE_STRUCT
696 && type->code () != TYPE_CODE_UNION
697 && type->code () != TYPE_CODE_ENUM)
698 return NULL;
700 return type;
703 /* Helper for tyscm_array and tyscm_vector. */
705 static SCM
706 tyscm_array_1 (SCM self, SCM n1_scm, SCM n2_scm, int is_vector,
707 const char *func_name)
709 type_smob *t_smob
710 = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, func_name);
711 struct type *type = t_smob->type;
712 long n1, n2 = 0;
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))
720 n2 = n1;
721 n1 = 0;
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),
728 scm_from_long (n2)),
729 _("Array length must not be negative"));
732 gdbscm_gdb_exception exc {};
735 array = lookup_array_range_type (type, n1, n2);
736 if (is_vector)
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. */
758 static SCM
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. */
774 static SCM
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. */
783 static SCM
784 gdbscm_type_pointer (SCM self)
786 type_smob *t_smob
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. */
809 static SCM
810 gdbscm_type_range (SCM self)
812 type_smob *t_smob
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 ();
831 else
832 low = 0;
834 if (type->bounds ()->high.is_constant ())
835 high = type->bounds ()->high.const_val ();
836 else
837 high = 0;
838 break;
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. */
850 static SCM
851 gdbscm_type_reference (SCM self)
853 type_smob *t_smob
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. */
874 static SCM
875 gdbscm_type_target (SCM self)
877 type_smob *t_smob
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. */
889 static SCM
890 gdbscm_type_const (SCM self)
892 type_smob *t_smob
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. */
913 static SCM
914 gdbscm_type_volatile (SCM self)
916 type_smob *t_smob
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. */
937 static SCM
938 gdbscm_type_unqualified (SCM self)
940 type_smob *t_smob
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. */
963 static SCM
964 gdbscm_type_num_fields (SCM self)
966 type_smob *t_smob
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);
971 if (type == NULL)
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. */
981 static SCM
982 gdbscm_type_field (SCM self, SCM field_scm)
984 type_smob *t_smob
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,
989 _("string"));
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
993 copied here. */
995 type = tyscm_get_composite (type);
996 if (type == NULL)
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). */
1022 static SCM
1023 gdbscm_type_has_field_p (SCM self, SCM field_scm)
1025 type_smob *t_smob
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,
1030 _("string"));
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
1034 copied here. */
1036 type = tyscm_get_composite (type);
1037 if (type == NULL)
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))
1050 return SCM_BOOL_T;
1054 return SCM_BOOL_F;
1057 /* (make-field-iterator <gdb:type>) -> <gdb:iterator>
1058 Make a field iterator object. */
1060 static SCM
1061 gdbscm_make_field_iterator (SCM self)
1063 type_smob *t_smob
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;
1078 else
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. */
1091 static SCM
1092 gdbscm_type_next_field_x (SCM self)
1094 iterator_smob *i_smob;
1095 type_smob *t_smob;
1096 struct type *type;
1097 SCM it_scm, result, progress, object;
1098 int field;
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));
1119 return result;
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. */
1130 static SCM
1131 gdbscm_field_name (SCM self)
1133 field_smob *f_smob
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 ());
1139 return SCM_BOOL_F;
1142 /* (field-type <gdb:field>) -> <gdb:type>
1143 Return the <gdb:type> object of the field or #f if there isn't one. */
1145 static SCM
1146 gdbscm_field_type (SCM self)
1148 field_smob *f_smob
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. */
1153 if (field->type ())
1154 return tyscm_scm_from_type (field->type ());
1155 return SCM_BOOL_F;
1158 /* (field-enumval <gdb:field>) -> integer
1159 For enum values, return its value as an integer. */
1161 static SCM
1162 gdbscm_field_enumval (SCM self)
1164 field_smob *f_smob
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. */
1178 static SCM
1179 gdbscm_field_bitpos (SCM self)
1181 field_smob *f_smob
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. */
1195 static SCM
1196 gdbscm_field_bitsize (SCM self)
1198 field_smob *f_smob
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. */
1208 static SCM
1209 gdbscm_field_artificial_p (SCM self)
1211 field_smob *f_smob
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. */
1221 static SCM
1222 gdbscm_field_baseclass_p (SCM self)
1224 field_smob *f_smob
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));
1230 return SCM_BOOL_F;
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);
1250 else
1251 type = lookup_typename (current_language,
1252 type_name, block, 0);
1254 catch (const gdb_exception &except)
1256 return NULL;
1259 return type;
1262 /* (lookup-type name [#:block <gdb:block>]) -> <gdb:type>
1263 TODO: legacy template support left out until needed. */
1265 static SCM
1266 gdbscm_lookup_type (SCM name_scm, SCM rest)
1268 SCM keywords[] = { block_keyword, SCM_BOOL_F };
1269 char *name;
1270 SCM block_scm = SCM_BOOL_F;
1271 int block_arg_pos = -1;
1272 const struct block *block = NULL;
1273 struct type *type;
1275 gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#O",
1276 name_scm, &name,
1277 rest, &block_arg_pos, &block_scm);
1279 if (block_arg_pos != -1)
1281 SCM exception;
1283 block = bkscm_scm_to_block (block_scm, block_arg_pos, FUNC_NAME,
1284 &exception);
1285 if (block == NULL)
1287 xfree (name);
1288 gdbscm_throw (exception);
1291 type = tyscm_lookup_typename (name, block);
1292 xfree (name);
1294 if (type != NULL)
1295 return tyscm_scm_from_type (type);
1296 return SCM_BOOL_F;
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"
1309 #undef OP
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\
1359 the array size.\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\
1372 the array size.\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." },
1460 END_FUNCTIONS
1463 void
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);