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