gdb smob cleanups
[platform/upstream/binutils.git] / gdb / guile / scm-value.c
1 /* Scheme interface to values.
2
3    Copyright (C) 2008-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* See README file in this directory for implementation notes, coding
21    conventions, et.al.  */
22
23 #include "defs.h"
24 #include "arch-utils.h"
25 #include "charset.h"
26 #include "cp-abi.h"
27 #include "gdb_assert.h"
28 #include "infcall.h"
29 #include "symtab.h" /* Needed by language.h.  */
30 #include "language.h"
31 #include "valprint.h"
32 #include "value.h"
33 #include "guile-internal.h"
34
35 /* The <gdb:value> smob.  */
36
37 typedef struct _value_smob
38 {
39   /* This always appears first.  */
40   gdb_smob base;
41
42   /* Doubly linked list of values in values_in_scheme.
43      IWBN to use a chained_gdb_smob instead, which is doable, it just requires
44      a bit more casting than normal.  */
45   struct _value_smob *next;
46   struct _value_smob *prev;
47
48   struct value *value;
49
50   /* These are cached here to avoid making multiple copies of them.
51      Plus computing the dynamic_type can be a bit expensive.
52      We use #f to indicate that the value doesn't exist (e.g. value doesn't
53      have an address), so we need another value to indicate that we haven't
54      computed the value yet.  For this we use SCM_UNDEFINED.  */
55   SCM address;
56   SCM type;
57   SCM dynamic_type;
58 } value_smob;
59
60 static const char value_smob_name[] = "gdb:value";
61
62 /* The tag Guile knows the value smob by.  */
63 static scm_t_bits value_smob_tag;
64
65 /* List of all values which are currently exposed to Scheme. It is
66    maintained so that when an objfile is discarded, preserve_values
67    can copy the values' types if needed.  */
68 static value_smob *values_in_scheme;
69
70 /* Keywords used by Scheme procedures in this file.  */
71 static SCM type_keyword;
72 static SCM encoding_keyword;
73 static SCM errors_keyword;
74 static SCM length_keyword;
75
76 /* Possible #:errors values.  */
77 static SCM error_symbol;
78 static SCM escape_symbol;
79 static SCM substitute_symbol;
80 \f
81 /* Administrivia for value smobs.  */
82
83 /* Iterate over all the <gdb:value> objects, calling preserve_one_value on
84    each.
85    This is the extension_language_ops.preserve_values "method".  */
86
87 void
88 gdbscm_preserve_values (const struct extension_language_defn *extlang,
89                         struct objfile *objfile, htab_t copied_types)
90 {
91   value_smob *iter;
92
93   for (iter = values_in_scheme; iter; iter = iter->next)
94     preserve_one_value (iter->value, objfile, copied_types);
95 }
96
97 /* Helper to add a value_smob to the global list.  */
98
99 static void
100 vlscm_remember_scheme_value (value_smob *v_smob)
101 {
102   v_smob->next = values_in_scheme;
103   if (v_smob->next)
104     v_smob->next->prev = v_smob;
105   v_smob->prev = NULL;
106   values_in_scheme = v_smob;
107 }
108
109 /* Helper to remove a value_smob from the global list.  */
110
111 static void
112 vlscm_forget_value_smob (value_smob *v_smob)
113 {
114   /* Remove SELF from the global list.  */
115   if (v_smob->prev)
116     v_smob->prev->next = v_smob->next;
117   else
118     {
119       gdb_assert (values_in_scheme == v_smob);
120       values_in_scheme = v_smob->next;
121     }
122   if (v_smob->next)
123     v_smob->next->prev = v_smob->prev;
124 }
125
126 /* The smob "mark" function for <gdb:value>.  */
127
128 static SCM
129 vlscm_mark_value_smob (SCM self)
130 {
131   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
132
133   scm_gc_mark (v_smob->address);
134   scm_gc_mark (v_smob->type);
135   return v_smob->dynamic_type;
136 }
137
138 /* The smob "free" function for <gdb:value>.  */
139
140 static size_t
141 vlscm_free_value_smob (SCM self)
142 {
143   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
144
145   vlscm_forget_value_smob (v_smob);
146   value_free (v_smob->value);
147
148   return 0;
149 }
150
151 /* The smob "print" function for <gdb:value>.  */
152
153 static int
154 vlscm_print_value_smob (SCM self, SCM port, scm_print_state *pstate)
155 {
156   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (self);
157   char *s = NULL;
158   struct value_print_options opts;
159   volatile struct gdb_exception except;
160
161   if (pstate->writingp)
162     gdbscm_printf (port, "#<%s ", value_smob_name);
163
164   get_user_print_options (&opts);
165   opts.deref_ref = 0;
166
167   /* pstate->writingp = zero if invoked by display/~A, and nonzero if
168      invoked by write/~S.  What to do here may need to evolve.
169      IWBN if we could pass an argument to format that would we could use
170      instead of writingp.  */
171   opts.raw = !!pstate->writingp;
172
173   TRY_CATCH (except, RETURN_MASK_ALL)
174     {
175       struct ui_file *stb = mem_fileopen ();
176       struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
177
178       common_val_print (v_smob->value, stb, 0, &opts, current_language);
179       s = ui_file_xstrdup (stb, NULL);
180
181       do_cleanups (old_chain);
182     }
183   GDBSCM_HANDLE_GDB_EXCEPTION (except);
184
185   if (s != NULL)
186     {
187       scm_puts (s, port);
188       xfree (s);
189     }
190
191   if (pstate->writingp)
192     scm_puts (">", port);
193
194   scm_remember_upto_here_1 (self);
195
196   /* Non-zero means success.  */
197   return 1;
198 }
199
200 /* The smob "equalp" function for <gdb:value>.  */
201
202 static SCM
203 vlscm_equal_p_value_smob (SCM v1, SCM v2)
204 {
205   const value_smob *v1_smob = (value_smob *) SCM_SMOB_DATA (v1);
206   const value_smob *v2_smob = (value_smob *) SCM_SMOB_DATA (v2);
207   int result = 0;
208   volatile struct gdb_exception except;
209
210   TRY_CATCH (except, RETURN_MASK_ALL)
211     {
212       result = value_equal (v1_smob->value, v2_smob->value);
213     }
214   GDBSCM_HANDLE_GDB_EXCEPTION (except);
215
216   return scm_from_bool (result);
217 }
218
219 /* Low level routine to create a <gdb:value> object.  */
220
221 static SCM
222 vlscm_make_value_smob (void)
223 {
224   value_smob *v_smob = (value_smob *)
225     scm_gc_malloc (sizeof (value_smob), value_smob_name);
226   SCM v_scm;
227
228   /* These must be filled in by the caller.  */
229   v_smob->value = NULL;
230   v_smob->prev = NULL;
231   v_smob->next = NULL;
232
233   /* These are lazily computed.  */
234   v_smob->address = SCM_UNDEFINED;
235   v_smob->type = SCM_UNDEFINED;
236   v_smob->dynamic_type = SCM_UNDEFINED;
237
238   v_scm = scm_new_smob (value_smob_tag, (scm_t_bits) v_smob);
239   gdbscm_init_gsmob (&v_smob->base);
240
241   return v_scm;
242 }
243
244 /* Return non-zero if SCM is a <gdb:value> object.  */
245
246 int
247 vlscm_is_value (SCM scm)
248 {
249   return SCM_SMOB_PREDICATE (value_smob_tag, scm);
250 }
251
252 /* (value? object) -> boolean */
253
254 static SCM
255 gdbscm_value_p (SCM scm)
256 {
257   return scm_from_bool (vlscm_is_value (scm));
258 }
259
260 /* Create a new <gdb:value> object that encapsulates VALUE.
261    The value is released from the all_values chain so its lifetime is not
262    bound to the execution of a command.  */
263
264 SCM
265 vlscm_scm_from_value (struct value *value)
266 {
267   /* N.B. It's important to not cause any side-effects until we know the
268      conversion worked.  */
269   SCM v_scm = vlscm_make_value_smob ();
270   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
271
272   v_smob->value = value;
273   release_value_or_incref (value);
274   vlscm_remember_scheme_value (v_smob);
275
276   return v_scm;
277 }
278
279 /* Returns the <gdb:value> object in SELF.
280    Throws an exception if SELF is not a <gdb:value> object.  */
281
282 static SCM
283 vlscm_get_value_arg_unsafe (SCM self, int arg_pos, const char *func_name)
284 {
285   SCM_ASSERT_TYPE (vlscm_is_value (self), self, arg_pos, func_name,
286                    value_smob_name);
287
288   return self;
289 }
290
291 /* Returns a pointer to the value smob of SELF.
292    Throws an exception if SELF is not a <gdb:value> object.  */
293
294 static value_smob *
295 vlscm_get_value_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
296 {
297   SCM v_scm = vlscm_get_value_arg_unsafe (self, arg_pos, func_name);
298   value_smob *v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
299
300   return v_smob;
301 }
302
303 /* Return the value field of V_SCM, an object of type <gdb:value>.
304    This exists so that we don't have to export the struct's contents.  */
305
306 struct value *
307 vlscm_scm_to_value (SCM v_scm)
308 {
309   value_smob *v_smob;
310
311   gdb_assert (vlscm_is_value (v_scm));
312   v_smob = (value_smob *) SCM_SMOB_DATA (v_scm);
313   return v_smob->value;
314 }
315 \f
316 /* Value methods.  */
317
318 /* (make-value x [#:type type]) -> <gdb:value> */
319
320 static SCM
321 gdbscm_make_value (SCM x, SCM rest)
322 {
323   struct gdbarch *gdbarch = get_current_arch ();
324   const struct language_defn *language = current_language;
325   const SCM keywords[] = { type_keyword, SCM_BOOL_F };
326   int type_arg_pos = -1;
327   SCM type_scm = SCM_UNDEFINED;
328   SCM except_scm, result;
329   type_smob *t_smob;
330   struct type *type = NULL;
331   struct value *value;
332   struct cleanup *cleanups;
333
334   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#O", rest,
335                               &type_arg_pos, &type_scm);
336
337   if (type_arg_pos > 0)
338     {
339       t_smob = tyscm_get_type_smob_arg_unsafe (type_scm, type_arg_pos,
340                                                FUNC_NAME);
341       type = tyscm_type_smob_type (t_smob);
342     }
343
344   cleanups = make_cleanup_value_free_to_mark (value_mark ());
345
346   value = vlscm_convert_typed_value_from_scheme (FUNC_NAME, SCM_ARG1, x,
347                                                  type_arg_pos, type_scm, type,
348                                                  &except_scm,
349                                                  gdbarch, language);
350   if (value == NULL)
351     {
352       do_cleanups (cleanups);
353       gdbscm_throw (except_scm);
354     }
355
356   result = vlscm_scm_from_value (value);
357
358   do_cleanups (cleanups);
359
360   if (gdbscm_is_exception (result))
361     gdbscm_throw (result);
362   return result;
363 }
364
365 /* (make-lazy-value <gdb:type> address) -> <gdb:value> */
366
367 static SCM
368 gdbscm_make_lazy_value (SCM type_scm, SCM address_scm)
369 {
370   type_smob *t_smob;
371   struct type *type;
372   ULONGEST address;
373   struct value *value = NULL;
374   SCM result;
375   struct cleanup *cleanups;
376   volatile struct gdb_exception except;
377
378   t_smob = tyscm_get_type_smob_arg_unsafe (type_scm, SCM_ARG1, FUNC_NAME);
379   type = tyscm_type_smob_type (t_smob);
380
381   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, NULL, "U",
382                               address_scm, &address);
383
384   cleanups = make_cleanup_value_free_to_mark (value_mark ());
385
386   /* There's no (current) need to wrap this in a TRY_CATCH, but for consistency
387      and future-proofing we do.  */
388   TRY_CATCH (except, RETURN_MASK_ALL)
389   {
390     value = value_from_contents_and_address (type, NULL, address);
391   }
392   GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
393
394   result = vlscm_scm_from_value (value);
395
396   do_cleanups (cleanups);
397
398   if (gdbscm_is_exception (result))
399     gdbscm_throw (result);
400   return result;
401 }
402
403 /* (value-optimized-out? <gdb:value>) -> boolean */
404
405 static SCM
406 gdbscm_value_optimized_out_p (SCM self)
407 {
408   value_smob *v_smob
409     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
410   struct value *value = v_smob->value;
411   int opt = 0;
412   volatile struct gdb_exception except;
413
414   TRY_CATCH (except, RETURN_MASK_ALL)
415     {
416       opt = value_optimized_out (value);
417     }
418   GDBSCM_HANDLE_GDB_EXCEPTION (except);
419
420   return scm_from_bool (opt);
421 }
422
423 /* (value-address <gdb:value>) -> integer
424    Returns #f if the value doesn't have one.  */
425
426 static SCM
427 gdbscm_value_address (SCM self)
428 {
429   value_smob *v_smob
430     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
431   struct value *value = v_smob->value;
432
433   if (SCM_UNBNDP (v_smob->address))
434     {
435       struct value *res_val = NULL;
436       struct cleanup *cleanup
437         = make_cleanup_value_free_to_mark (value_mark ());
438       SCM address;
439       volatile struct gdb_exception except;
440
441       TRY_CATCH (except, RETURN_MASK_ALL)
442         {
443           res_val = value_addr (value);
444         }
445       if (except.reason < 0)
446         address = SCM_BOOL_F;
447       else
448         address = vlscm_scm_from_value (res_val);
449
450       do_cleanups (cleanup);
451
452       if (gdbscm_is_exception (address))
453         gdbscm_throw (address);
454
455       v_smob->address = address;
456     }
457
458   return v_smob->address;
459 }
460
461 /* (value-dereference <gdb:value>) -> <gdb:value>
462    Given a value of a pointer type, apply the C unary * operator to it.  */
463
464 static SCM
465 gdbscm_value_dereference (SCM self)
466 {
467   value_smob *v_smob
468     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
469   struct value *value = v_smob->value;
470   SCM result;
471   struct value *res_val = NULL;
472   struct cleanup *cleanups;
473   volatile struct gdb_exception except;
474
475   cleanups = make_cleanup_value_free_to_mark (value_mark ());
476
477   TRY_CATCH (except, RETURN_MASK_ALL)
478     {
479       res_val = value_ind (value);
480     }
481   GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
482
483   result = vlscm_scm_from_value (res_val);
484
485   do_cleanups (cleanups);
486
487   if (gdbscm_is_exception (result))
488     gdbscm_throw (result);
489
490   return result;
491 }
492
493 /* (value-referenced-value <gdb:value>) -> <gdb:value>
494    Given a value of a reference type, return the value referenced.
495    The difference between this function and gdbscm_value_dereference is that
496    the latter applies * unary operator to a value, which need not always
497    result in the value referenced.
498    For example, for a value which is a reference to an 'int' pointer ('int *'),
499    gdbscm_value_dereference will result in a value of type 'int' while
500    gdbscm_value_referenced_value will result in a value of type 'int *'.  */
501
502 static SCM
503 gdbscm_value_referenced_value (SCM self)
504 {
505   value_smob *v_smob
506     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
507   struct value *value = v_smob->value;
508   SCM result;
509   struct value *res_val = NULL;
510   struct cleanup *cleanups;
511   volatile struct gdb_exception except;
512
513   cleanups = make_cleanup_value_free_to_mark (value_mark ());
514
515   TRY_CATCH (except, RETURN_MASK_ALL)
516     {
517       switch (TYPE_CODE (check_typedef (value_type (value))))
518         {
519         case TYPE_CODE_PTR:
520           res_val = value_ind (value);
521           break;
522         case TYPE_CODE_REF:
523           res_val = coerce_ref (value);
524           break;
525         default:
526           error (_("Trying to get the referenced value from a value which is"
527                    " neither a pointer nor a reference"));
528         }
529     }
530   GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
531
532   result = vlscm_scm_from_value (res_val);
533
534   do_cleanups (cleanups);
535
536   if (gdbscm_is_exception (result))
537     gdbscm_throw (result);
538
539   return result;
540 }
541
542 /* (value-type <gdb:value>) -> <gdb:type> */
543
544 static SCM
545 gdbscm_value_type (SCM self)
546 {
547   value_smob *v_smob
548     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
549   struct value *value = v_smob->value;
550
551   if (SCM_UNBNDP (v_smob->type))
552     v_smob->type = tyscm_scm_from_type (value_type (value));
553
554   return v_smob->type;
555 }
556
557 /* (value-dynamic-type <gdb:value>) -> <gdb:type> */
558
559 static SCM
560 gdbscm_value_dynamic_type (SCM self)
561 {
562   value_smob *v_smob
563     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
564   struct value *value = v_smob->value;
565   struct type *type = NULL;
566   volatile struct gdb_exception except;
567
568   if (! SCM_UNBNDP (v_smob->type))
569     return v_smob->dynamic_type;
570
571   TRY_CATCH (except, RETURN_MASK_ALL)
572     {
573       struct cleanup *cleanup
574         = make_cleanup_value_free_to_mark (value_mark ());
575
576       type = value_type (value);
577       CHECK_TYPEDEF (type);
578
579       if (((TYPE_CODE (type) == TYPE_CODE_PTR)
580            || (TYPE_CODE (type) == TYPE_CODE_REF))
581           && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
582         {
583           struct value *target;
584           int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
585
586           if (was_pointer)
587             target = value_ind (value);
588           else
589             target = coerce_ref (value);
590           type = value_rtti_type (target, NULL, NULL, NULL);
591
592           if (type)
593             {
594               if (was_pointer)
595                 type = lookup_pointer_type (type);
596               else
597                 type = lookup_reference_type (type);
598             }
599         }
600       else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
601         type = value_rtti_type (value, NULL, NULL, NULL);
602       else
603         {
604           /* Re-use object's static type.  */
605           type = NULL;
606         }
607
608       do_cleanups (cleanup);
609     }
610   GDBSCM_HANDLE_GDB_EXCEPTION (except);
611
612   if (type == NULL)
613     v_smob->dynamic_type = gdbscm_value_type (self);
614   else
615     v_smob->dynamic_type = tyscm_scm_from_type (type);
616
617   return v_smob->dynamic_type;
618 }
619
620 /* A helper function that implements the various cast operators.  */
621
622 static SCM
623 vlscm_do_cast (SCM self, SCM type_scm, enum exp_opcode op,
624                const char *func_name)
625 {
626   value_smob *v_smob
627     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
628   struct value *value = v_smob->value;
629   type_smob *t_smob
630     = tyscm_get_type_smob_arg_unsafe (type_scm, SCM_ARG2, FUNC_NAME);
631   struct type *type = tyscm_type_smob_type (t_smob);
632   SCM result;
633   struct value *res_val = NULL;
634   struct cleanup *cleanups;
635   volatile struct gdb_exception except;
636
637   cleanups = make_cleanup_value_free_to_mark (value_mark ());
638
639   TRY_CATCH (except, RETURN_MASK_ALL)
640     {
641       if (op == UNOP_DYNAMIC_CAST)
642         res_val = value_dynamic_cast (type, value);
643       else if (op == UNOP_REINTERPRET_CAST)
644         res_val = value_reinterpret_cast (type, value);
645       else
646         {
647           gdb_assert (op == UNOP_CAST);
648           res_val = value_cast (type, value);
649         }
650     }
651   GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
652
653   gdb_assert (res_val != NULL);
654   result = vlscm_scm_from_value (res_val);
655
656   do_cleanups (cleanups);
657
658   if (gdbscm_is_exception (result))
659     gdbscm_throw (result);
660
661   return result;
662 }
663
664 /* (value-cast <gdb:value> <gdb:type>) -> <gdb:value> */
665
666 static SCM
667 gdbscm_value_cast (SCM self, SCM new_type)
668 {
669   return vlscm_do_cast (self, new_type, UNOP_CAST, FUNC_NAME);
670 }
671
672 /* (value-dynamic-cast <gdb:value> <gdb:type>) -> <gdb:value> */
673
674 static SCM
675 gdbscm_value_dynamic_cast (SCM self, SCM new_type)
676 {
677   return vlscm_do_cast (self, new_type, UNOP_DYNAMIC_CAST, FUNC_NAME);
678 }
679
680 /* (value-reinterpret-cast <gdb:value> <gdb:type>) -> <gdb:value> */
681
682 static SCM
683 gdbscm_value_reinterpret_cast (SCM self, SCM new_type)
684 {
685   return vlscm_do_cast (self, new_type, UNOP_REINTERPRET_CAST, FUNC_NAME);
686 }
687
688 /* (value-field <gdb:value> string) -> <gdb:value>
689    Given string name of an element inside structure, return its <gdb:value>
690    object.  */
691
692 static SCM
693 gdbscm_value_field (SCM self, SCM field_scm)
694 {
695   value_smob *v_smob
696     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
697   struct value *value = v_smob->value;
698   char *field = NULL;
699   struct value *res_val = NULL;
700   SCM result;
701   struct cleanup *cleanups;
702   volatile struct gdb_exception except;
703
704   SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME,
705                    _("string"));
706
707   cleanups = make_cleanup_value_free_to_mark (value_mark ());
708
709   field = gdbscm_scm_to_c_string (field_scm);
710   make_cleanup (xfree, field);
711
712   TRY_CATCH (except, RETURN_MASK_ALL)
713     {
714       struct value *tmp = value;
715
716       res_val = value_struct_elt (&tmp, NULL, field, NULL, NULL);
717     }
718   GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
719
720   gdb_assert (res_val != NULL);
721   result = vlscm_scm_from_value (res_val);
722
723   do_cleanups (cleanups);
724
725   if (gdbscm_is_exception (result))
726     gdbscm_throw (result);
727
728   return result;
729 }
730
731 /* (value-subscript <gdb:value> integer|<gdb:value>) -> <gdb:value>
732    Return the specified value in an array.  */
733
734 static SCM
735 gdbscm_value_subscript (SCM self, SCM index_scm)
736 {
737   value_smob *v_smob
738     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
739   struct value *value = v_smob->value;
740   struct value *index = NULL;
741   struct value *res_val = NULL;
742   struct type *type = value_type (value);
743   struct gdbarch *gdbarch;
744   SCM result, except_scm;
745   struct cleanup *cleanups;
746   volatile struct gdb_exception except;
747
748   /* The sequencing here, as everywhere else, is important.
749      We can't have existing cleanups when a Scheme exception is thrown.  */
750
751   SCM_ASSERT (type != NULL, self, SCM_ARG2, FUNC_NAME);
752   gdbarch = get_type_arch (type);
753
754   cleanups = make_cleanup_value_free_to_mark (value_mark ());
755
756   index = vlscm_convert_value_from_scheme (FUNC_NAME, SCM_ARG2, index_scm,
757                                            &except_scm,
758                                            gdbarch, current_language);
759   if (index == NULL)
760     {
761       do_cleanups (cleanups);
762       gdbscm_throw (except_scm);
763     }
764
765   TRY_CATCH (except, RETURN_MASK_ALL)
766     {
767       struct value *tmp = value;
768
769       /* Assume we are attempting an array access, and let the value code
770          throw an exception if the index has an invalid type.
771          Check the value's type is something that can be accessed via
772          a subscript.  */
773       tmp = coerce_ref (tmp);
774       type = check_typedef (value_type (tmp));
775       if (TYPE_CODE (type) != TYPE_CODE_ARRAY
776           && TYPE_CODE (type) != TYPE_CODE_PTR)
777         error (_("Cannot subscript requested type"));
778
779       res_val = value_subscript (tmp, value_as_long (index));
780    }
781   GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
782
783   gdb_assert (res_val != NULL);
784   result = vlscm_scm_from_value (res_val);
785
786   do_cleanups (cleanups);
787
788   if (gdbscm_is_exception (result))
789     gdbscm_throw (result);
790
791   return result;
792 }
793
794 /* (value-call <gdb:value> arg-list) -> <gdb:value>
795    Perform an inferior function call on the value.  */
796
797 static SCM
798 gdbscm_value_call (SCM self, SCM args)
799 {
800   value_smob *v_smob
801     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
802   struct value *function = v_smob->value;
803   struct value *mark = value_mark ();
804   struct type *ftype = NULL;
805   long args_count;
806   struct value **vargs = NULL;
807   SCM result = SCM_BOOL_F;
808   volatile struct gdb_exception except;
809
810   TRY_CATCH (except, RETURN_MASK_ALL)
811     {
812       ftype = check_typedef (value_type (function));
813     }
814   GDBSCM_HANDLE_GDB_EXCEPTION (except);
815
816   SCM_ASSERT_TYPE (TYPE_CODE (ftype) == TYPE_CODE_FUNC, self,
817                    SCM_ARG1, FUNC_NAME,
818                    _("function (value of TYPE_CODE_FUNC)"));
819
820   SCM_ASSERT_TYPE (gdbscm_is_true (scm_list_p (args)), args,
821                    SCM_ARG2, FUNC_NAME, _("list"));
822
823   args_count = scm_ilength (args);
824   if (args_count > 0)
825     {
826       struct gdbarch *gdbarch = get_current_arch ();
827       const struct language_defn *language = current_language;
828       SCM except_scm;
829       long i;
830
831       vargs = alloca (sizeof (struct value *) * args_count);
832       for (i = 0; i < args_count; i++)
833         {
834           SCM arg = scm_car (args);
835
836           vargs[i] = vlscm_convert_value_from_scheme (FUNC_NAME,
837                                                       GDBSCM_ARG_NONE, arg,
838                                                       &except_scm,
839                                                       gdbarch, language);
840           if (vargs[i] == NULL)
841             gdbscm_throw (except_scm);
842
843           args = scm_cdr (args);
844         }
845       gdb_assert (gdbscm_is_true (scm_null_p (args)));
846     }
847
848   TRY_CATCH (except, RETURN_MASK_ALL)
849     {
850       struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark);
851       struct value *return_value;
852
853       return_value = call_function_by_hand (function, args_count, vargs);
854       result = vlscm_scm_from_value (return_value);
855       do_cleanups (cleanup);
856     }
857   GDBSCM_HANDLE_GDB_EXCEPTION (except);
858
859   if (gdbscm_is_exception (result))
860     gdbscm_throw (result);
861
862   return result;
863 }
864
865 /* (value->bytevector <gdb:value>) -> bytevector */
866
867 static SCM
868 gdbscm_value_to_bytevector (SCM self)
869 {
870   value_smob *v_smob
871     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
872   struct value *value = v_smob->value;
873   struct type *type;
874   size_t length = 0;
875   const gdb_byte *contents = NULL;
876   SCM bv;
877   volatile struct gdb_exception except;
878
879   type = value_type (value);
880
881   TRY_CATCH (except, RETURN_MASK_ALL)
882     {
883       CHECK_TYPEDEF (type);
884       length = TYPE_LENGTH (type);
885       contents = value_contents (value);
886     }
887   GDBSCM_HANDLE_GDB_EXCEPTION (except);
888
889   bv = scm_c_make_bytevector (length);
890   memcpy (SCM_BYTEVECTOR_CONTENTS (bv), contents, length);
891
892   return bv;
893 }
894
895 /* Helper function to determine if a type is "int-like".  */
896
897 static int
898 is_intlike (struct type *type, int ptr_ok)
899 {
900   return (TYPE_CODE (type) == TYPE_CODE_INT
901           || TYPE_CODE (type) == TYPE_CODE_ENUM
902           || TYPE_CODE (type) == TYPE_CODE_BOOL
903           || TYPE_CODE (type) == TYPE_CODE_CHAR
904           || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
905 }
906
907 /* (value->bool <gdb:value>) -> boolean
908    Throws an error if the value is not integer-like.  */
909
910 static SCM
911 gdbscm_value_to_bool (SCM self)
912 {
913   value_smob *v_smob
914     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
915   struct value *value = v_smob->value;
916   struct type *type;
917   LONGEST l = 0;
918   volatile struct gdb_exception except;
919
920   type = value_type (value);
921
922   TRY_CATCH (except, RETURN_MASK_ALL)
923     {
924       CHECK_TYPEDEF (type);
925     }
926   GDBSCM_HANDLE_GDB_EXCEPTION (except);
927
928   SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
929                    _("integer-like gdb value"));
930
931   TRY_CATCH (except, RETURN_MASK_ALL)
932     {
933       if (TYPE_CODE (type) == TYPE_CODE_PTR)
934         l = value_as_address (value);
935       else
936         l = value_as_long (value);
937     }
938   GDBSCM_HANDLE_GDB_EXCEPTION (except);
939
940   return scm_from_bool (l != 0);
941 }
942
943 /* (value->integer <gdb:value>) -> integer
944    Throws an error if the value is not integer-like.  */
945
946 static SCM
947 gdbscm_value_to_integer (SCM self)
948 {
949   value_smob *v_smob
950     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
951   struct value *value = v_smob->value;
952   struct type *type;
953   LONGEST l = 0;
954   volatile struct gdb_exception except;
955
956   type = value_type (value);
957
958   TRY_CATCH (except, RETURN_MASK_ALL)
959     {
960       CHECK_TYPEDEF (type);
961     }
962   GDBSCM_HANDLE_GDB_EXCEPTION (except);
963
964   SCM_ASSERT_TYPE (is_intlike (type, 1), self, SCM_ARG1, FUNC_NAME,
965                    _("integer-like gdb value"));
966
967   TRY_CATCH (except, RETURN_MASK_ALL)
968     {
969       if (TYPE_CODE (type) == TYPE_CODE_PTR)
970         l = value_as_address (value);
971       else
972         l = value_as_long (value);
973     }
974   GDBSCM_HANDLE_GDB_EXCEPTION (except);
975
976   if (TYPE_UNSIGNED (type))
977     return gdbscm_scm_from_ulongest (l);
978   else
979     return gdbscm_scm_from_longest (l);
980 }
981
982 /* (value->real <gdb:value>) -> real
983    Throws an error if the value is not a number.  */
984
985 static SCM
986 gdbscm_value_to_real (SCM self)
987 {
988   value_smob *v_smob
989     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
990   struct value *value = v_smob->value;
991   struct type *type;
992   DOUBLEST d = 0;
993   volatile struct gdb_exception except;
994
995   type = value_type (value);
996
997   TRY_CATCH (except, RETURN_MASK_ALL)
998     {
999       CHECK_TYPEDEF (type);
1000     }
1001   GDBSCM_HANDLE_GDB_EXCEPTION (except);
1002
1003   SCM_ASSERT_TYPE (is_intlike (type, 0) || TYPE_CODE (type) == TYPE_CODE_FLT,
1004                    self, SCM_ARG1, FUNC_NAME, _("number"));
1005
1006   TRY_CATCH (except, RETURN_MASK_ALL)
1007     {
1008       d = value_as_double (value);
1009     }
1010   GDBSCM_HANDLE_GDB_EXCEPTION (except);
1011
1012   /* TODO: Is there a better way to check if the value fits?  */
1013   if (d != (double) d)
1014     gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
1015                                _("number can't be converted to a double"));
1016
1017   return scm_from_double (d);
1018 }
1019
1020 /* (value->string <gdb:value>
1021        [#:encoding encoding]
1022        [#:errors #f | 'error | 'substitute]
1023        [#:length length])
1024      -> string
1025    Return Unicode string with value's contents, which must be a string.
1026
1027    If ENCODING is not given, the string is assumed to be encoded in
1028    the target's charset.
1029
1030    ERRORS is one of #f, 'error or 'substitute.
1031    An error setting of #f means use the default, which is
1032    Guile's %default-port-conversion-strategy.  If the default is not one
1033    of 'error or 'substitute, 'substitute is used.
1034    An error setting of "error" causes an exception to be thrown if there's
1035    a decoding error.  An error setting of "substitute" causes invalid
1036    characters to be replaced with "?".
1037
1038    If LENGTH is provided, only fetch string to the length provided.
1039    LENGTH must be a Scheme integer, it can't be a <gdb:value> integer.  */
1040
1041 static SCM
1042 gdbscm_value_to_string (SCM self, SCM rest)
1043 {
1044   value_smob *v_smob
1045     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1046   struct value *value = v_smob->value;
1047   const SCM keywords[] = {
1048     encoding_keyword, errors_keyword, length_keyword, SCM_BOOL_F
1049   };
1050   int encoding_arg_pos = -1, errors_arg_pos = -1, length_arg_pos = -1;
1051   char *encoding = NULL;
1052   SCM errors = SCM_BOOL_F;
1053   int length = -1;
1054   gdb_byte *buffer = NULL;
1055   const char *la_encoding = NULL;
1056   struct type *char_type = NULL;
1057   SCM result;
1058   struct cleanup *cleanups;
1059   volatile struct gdb_exception except;
1060
1061   /* The sequencing here, as everywhere else, is important.
1062      We can't have existing cleanups when a Scheme exception is thrown.  */
1063
1064   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#sOi", rest,
1065                               &encoding_arg_pos, &encoding,
1066                               &errors_arg_pos, &errors,
1067                               &length_arg_pos, &length);
1068
1069   cleanups = make_cleanup (xfree, encoding);
1070
1071   if (errors_arg_pos > 0
1072       && errors != SCM_BOOL_F
1073       && !scm_is_eq (errors, error_symbol)
1074       && !scm_is_eq (errors, substitute_symbol))
1075     {
1076       SCM excp
1077         = gdbscm_make_out_of_range_error (FUNC_NAME, errors_arg_pos, errors,
1078                                           _("invalid error kind"));
1079
1080       do_cleanups (cleanups);
1081       gdbscm_throw (excp);
1082     }
1083   if (errors == SCM_BOOL_F)
1084     errors = scm_port_conversion_strategy (SCM_BOOL_F);
1085   /* We don't assume anything about the result of scm_port_conversion_strategy.
1086      From this point on, if errors is not 'errors, use 'substitute.  */
1087
1088   TRY_CATCH (except, RETURN_MASK_ALL)
1089     {
1090       LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
1091     }
1092   GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
1093
1094   /* If errors is "error" scm_from_stringn may throw a Scheme exception.
1095      Make sure we don't leak.  This is done via scm_dynwind_begin, et.al.  */
1096   discard_cleanups (cleanups);
1097
1098   scm_dynwind_begin (0);
1099
1100   gdbscm_dynwind_xfree (encoding);
1101   gdbscm_dynwind_xfree (buffer);
1102
1103   result = scm_from_stringn ((const char *) buffer,
1104                              length * TYPE_LENGTH (char_type),
1105                              (encoding != NULL && *encoding != '\0'
1106                               ? encoding
1107                               : la_encoding),
1108                              scm_is_eq (errors, error_symbol)
1109                              ? SCM_FAILED_CONVERSION_ERROR
1110                              : SCM_FAILED_CONVERSION_QUESTION_MARK);
1111
1112   scm_dynwind_end ();
1113
1114   return result;
1115 }
1116
1117 /* (value->lazy-string <gdb:value> [#:encoding encoding] [#:length length])
1118      -> <gdb:lazy-string>
1119    Return a Scheme object representing a lazy_string_object type.
1120    A lazy string is a pointer to a string with an optional encoding and length.
1121    If ENCODING is not given, the target's charset is used.
1122    If LENGTH is provided then the length parameter is set to LENGTH, otherwise
1123    length will be set to -1 (first null of appropriate with).
1124    LENGTH must be a Scheme integer, it can't be a <gdb:value> integer.  */
1125
1126 static SCM
1127 gdbscm_value_to_lazy_string (SCM self, SCM rest)
1128 {
1129   value_smob *v_smob
1130     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1131   struct value *value = v_smob->value;
1132   const SCM keywords[] = { encoding_keyword, length_keyword, SCM_BOOL_F };
1133   int encoding_arg_pos = -1, length_arg_pos = -1;
1134   char *encoding = NULL;
1135   int length = -1;
1136   SCM result = SCM_BOOL_F; /* -Wall */
1137   struct cleanup *cleanups;
1138   volatile struct gdb_exception except;
1139
1140   /* The sequencing here, as everywhere else, is important.
1141      We can't have existing cleanups when a Scheme exception is thrown.  */
1142
1143   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG2, keywords, "#si", rest,
1144                               &encoding_arg_pos, &encoding,
1145                               &length_arg_pos, &length);
1146
1147   cleanups = make_cleanup (xfree, encoding);
1148
1149   TRY_CATCH (except, RETURN_MASK_ALL)
1150     {
1151       struct cleanup *inner_cleanup
1152         = make_cleanup_value_free_to_mark (value_mark ());
1153
1154       if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
1155         value = value_ind (value);
1156
1157       result = lsscm_make_lazy_string (value_address (value), length,
1158                                        encoding, value_type (value));
1159
1160       do_cleanups (inner_cleanup);
1161     }
1162   do_cleanups (cleanups);
1163   GDBSCM_HANDLE_GDB_EXCEPTION (except);
1164
1165   if (gdbscm_is_exception (result))
1166     gdbscm_throw (result);
1167
1168   return result;
1169 }
1170
1171 /* (value-lazy? <gdb:value>) -> boolean */
1172
1173 static SCM
1174 gdbscm_value_lazy_p (SCM self)
1175 {
1176   value_smob *v_smob
1177     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1178   struct value *value = v_smob->value;
1179
1180   return scm_from_bool (value_lazy (value));
1181 }
1182
1183 /* (value-fetch-lazy! <gdb:value>) -> unspecified */
1184
1185 static SCM
1186 gdbscm_value_fetch_lazy_x (SCM self)
1187 {
1188   value_smob *v_smob
1189     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1190   struct value *value = v_smob->value;
1191   volatile struct gdb_exception except;
1192
1193   TRY_CATCH (except, RETURN_MASK_ALL)
1194     {
1195       if (value_lazy (value))
1196         value_fetch_lazy (value);
1197     }
1198   GDBSCM_HANDLE_GDB_EXCEPTION (except);
1199
1200   return SCM_UNSPECIFIED;
1201 }
1202
1203 /* (value-print <gdb:value>) -> string */
1204
1205 static SCM
1206 gdbscm_value_print (SCM self)
1207 {
1208   value_smob *v_smob
1209     = vlscm_get_value_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
1210   struct value *value = v_smob->value;
1211   struct value_print_options opts;
1212   char *s = NULL;
1213   SCM result;
1214   volatile struct gdb_exception except;
1215
1216   get_user_print_options (&opts);
1217   opts.deref_ref = 0;
1218
1219   TRY_CATCH (except, RETURN_MASK_ALL)
1220     {
1221       struct ui_file *stb = mem_fileopen ();
1222       struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
1223
1224       common_val_print (value, stb, 0, &opts, current_language);
1225       s = ui_file_xstrdup (stb, NULL);
1226
1227       do_cleanups (old_chain);
1228     }
1229   GDBSCM_HANDLE_GDB_EXCEPTION (except);
1230
1231   /* Use SCM_FAILED_CONVERSION_QUESTION_MARK to ensure this doesn't
1232      throw an error if the encoding fails.
1233      IWBN to use scm_take_locale_string here, but we'd have to temporarily
1234      override the default port conversion handler because contrary to
1235      documentation it doesn't necessarily free the input string.  */
1236   result = scm_from_stringn (s, strlen (s), host_charset (),
1237                              SCM_FAILED_CONVERSION_QUESTION_MARK);
1238   xfree (s);
1239
1240   return result;
1241 }
1242 \f
1243 /* (parse-and-eval string) -> <gdb:value>
1244    Parse a string and evaluate the string as an expression.  */
1245
1246 static SCM
1247 gdbscm_parse_and_eval (SCM expr_scm)
1248 {
1249   char *expr_str;
1250   struct value *res_val = NULL;
1251   SCM result;
1252   struct cleanup *cleanups;
1253   volatile struct gdb_exception except;
1254
1255   /* The sequencing here, as everywhere else, is important.
1256      We can't have existing cleanups when a Scheme exception is thrown.  */
1257
1258   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "s",
1259                               expr_scm, &expr_str);
1260
1261   cleanups = make_cleanup_value_free_to_mark (value_mark ());
1262   make_cleanup (xfree, expr_str);
1263
1264   TRY_CATCH (except, RETURN_MASK_ALL)
1265     {
1266       res_val = parse_and_eval (expr_str);
1267     }
1268   GDBSCM_HANDLE_GDB_EXCEPTION_WITH_CLEANUPS (except, cleanups);
1269
1270   gdb_assert (res_val != NULL);
1271   result = vlscm_scm_from_value (res_val);
1272
1273   do_cleanups (cleanups);
1274
1275   if (gdbscm_is_exception (result))
1276     gdbscm_throw (result);
1277
1278   return result;
1279 }
1280
1281 /* (history-ref integer) -> <gdb:value>
1282    Return the specified value from GDB's value history.  */
1283
1284 static SCM
1285 gdbscm_history_ref (SCM index)
1286 {
1287   int i;
1288   struct value *res_val = NULL; /* Initialize to appease gcc warning.  */
1289   volatile struct gdb_exception except;
1290
1291   gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, NULL, "i", index, &i);
1292
1293   TRY_CATCH (except, RETURN_MASK_ALL)
1294     {
1295       res_val = access_value_history (i);
1296     }
1297   GDBSCM_HANDLE_GDB_EXCEPTION (except);
1298
1299   return vlscm_scm_from_value (res_val);
1300 }
1301
1302 /* (history-append! <gdb:value>) -> index
1303    Append VALUE to GDB's value history.  Return its index in the history.  */
1304
1305 static SCM
1306 gdbscm_history_append_x (SCM value)
1307 {
1308   int res_index = -1;
1309   struct value *v;
1310   volatile struct gdb_exception except;
1311
1312   v = vlscm_scm_to_value (value);
1313
1314   TRY_CATCH (except, RETURN_MASK_ALL)
1315     {
1316       res_index = record_latest_value (v);
1317     }
1318   GDBSCM_HANDLE_GDB_EXCEPTION (except);
1319
1320   return scm_from_int (res_index);
1321 }
1322 \f
1323 /* Initialize the Scheme value code.  */
1324
1325 static const scheme_function value_functions[] =
1326 {
1327   { "value?", 1, 0, 0, gdbscm_value_p,
1328     "\
1329 Return #t if the object is a <gdb:value> object." },
1330
1331   { "make-value", 1, 0, 1, gdbscm_make_value,
1332     "\
1333 Create a <gdb:value> representing object.\n\
1334 Typically this is used to convert numbers and strings to\n\
1335 <gdb:value> objects.\n\
1336 \n\
1337   Arguments: object [#:type <gdb:type>]" },
1338
1339   { "value-optimized-out?", 1, 0, 0, gdbscm_value_optimized_out_p,
1340     "\
1341 Return #t if the value has been optimizd out." },
1342
1343   { "value-address", 1, 0, 0, gdbscm_value_address,
1344     "\
1345 Return the address of the value." },
1346
1347   { "value-type", 1, 0, 0, gdbscm_value_type,
1348     "\
1349 Return the type of the value." },
1350
1351   { "value-dynamic-type", 1, 0, 0, gdbscm_value_dynamic_type,
1352     "\
1353 Return the dynamic type of the value." },
1354
1355   { "value-cast", 2, 0, 0, gdbscm_value_cast,
1356     "\
1357 Cast the value to the supplied type.\n\
1358 \n\
1359   Arguments: <gdb:value> <gdb:type>" },
1360
1361   { "value-dynamic-cast", 2, 0, 0, gdbscm_value_dynamic_cast,
1362     "\
1363 Cast the value to the supplied type, as if by the C++\n\
1364 dynamic_cast operator.\n\
1365 \n\
1366   Arguments: <gdb:value> <gdb:type>" },
1367
1368   { "value-reinterpret-cast", 2, 0, 0, gdbscm_value_reinterpret_cast,
1369     "\
1370 Cast the value to the supplied type, as if by the C++\n\
1371 reinterpret_cast operator.\n\
1372 \n\
1373   Arguments: <gdb:value> <gdb:type>" },
1374
1375   { "value-dereference", 1, 0, 0, gdbscm_value_dereference,
1376     "\
1377 Return the result of applying the C unary * operator to the value." },
1378
1379   { "value-referenced-value", 1, 0, 0, gdbscm_value_referenced_value,
1380     "\
1381 Given a value of a reference type, return the value referenced.\n\
1382 The difference between this function and value-dereference is that\n\
1383 the latter applies * unary operator to a value, which need not always\n\
1384 result in the value referenced.\n\
1385 For example, for a value which is a reference to an 'int' pointer ('int *'),\n\
1386 value-dereference will result in a value of type 'int' while\n\
1387 value-referenced-value will result in a value of type 'int *'." },
1388
1389   { "value-field", 2, 0, 0, gdbscm_value_field,
1390     "\
1391 Return the specified field of the value.\n\
1392 \n\
1393   Arguments: <gdb:value> string" },
1394
1395   { "value-subscript", 2, 0, 0, gdbscm_value_subscript,
1396     "\
1397 Return the value of the array at the specified index.\n\
1398 \n\
1399   Arguments: <gdb:value> integer" },
1400
1401   { "value-call", 2, 0, 0, gdbscm_value_call,
1402     "\
1403 Perform an inferior function call taking the value as a pointer to the\n\
1404 function to call.\n\
1405 Each element of the argument list must be a <gdb:value> object or an object\n\
1406 that can be converted to one.\n\
1407 The result is the value returned by the function.\n\
1408 \n\
1409   Arguments: <gdb:value> arg-list" },
1410
1411   { "value->bool", 1, 0, 0, gdbscm_value_to_bool,
1412     "\
1413 Return the Scheme boolean representing the GDB value.\n\
1414 The value must be \"integer like\".  Pointers are ok." },
1415
1416   { "value->integer", 1, 0, 0, gdbscm_value_to_integer,
1417     "\
1418 Return the Scheme integer representing the GDB value.\n\
1419 The value must be \"integer like\".  Pointers are ok." },
1420
1421   { "value->real", 1, 0, 0, gdbscm_value_to_real,
1422     "\
1423 Return the Scheme real number representing the GDB value.\n\
1424 The value must be a number." },
1425
1426   { "value->bytevector", 1, 0, 0, gdbscm_value_to_bytevector,
1427     "\
1428 Return a Scheme bytevector with the raw contents of the GDB value.\n\
1429 No transformation, endian or otherwise, is performed." },
1430
1431   { "value->string", 1, 0, 1, gdbscm_value_to_string,
1432     "\
1433 Return the Unicode string of the value's contents.\n\
1434 If ENCODING is not given, the string is assumed to be encoded in\n\
1435 the target's charset.\n\
1436 An error setting \"error\" causes an exception to be thrown if there's\n\
1437 a decoding error.  An error setting of \"substitute\" causes invalid\n\
1438 characters to be replaced with \"?\".  The default is \"error\".\n\
1439 If LENGTH is provided, only fetch string to the length provided.\n\
1440 \n\
1441   Arguments: <gdb:value>\n\
1442              [#:encoding encoding] [#:errors \"error\"|\"substitute\"]\n\
1443              [#:length length]" },
1444
1445   { "value->lazy-string", 1, 0, 1, gdbscm_value_to_lazy_string,
1446     "\
1447 Return a Scheme object representing a lazily fetched Unicode string\n\
1448 of the value's contents.\n\
1449 If ENCODING is not given, the string is assumed to be encoded in\n\
1450 the target's charset.\n\
1451 If LENGTH is provided, only fetch string to the length provided.\n\
1452 \n\
1453   Arguments: <gdb:value> [#:encoding encoding] [#:length length]" },
1454
1455   { "value-lazy?", 1, 0, 0, gdbscm_value_lazy_p,
1456     "\
1457 Return #t if the value is lazy (not fetched yet from the inferior).\n\
1458 A lazy value is fetched when needed, or when the value-fetch-lazy! function\n\
1459 is called." },
1460
1461   { "make-lazy-value", 2, 0, 0, gdbscm_make_lazy_value,
1462     "\
1463 Create a <gdb:value> that will be lazily fetched from the target.\n\
1464 \n\
1465   Arguments: <gdb:type> address" },
1466
1467   { "value-fetch-lazy!", 1, 0, 0, gdbscm_value_fetch_lazy_x,
1468     "\
1469 Fetch the value from the inferior, if it was lazy.\n\
1470 The result is \"unspecified\"." },
1471
1472   { "value-print", 1, 0, 0, gdbscm_value_print,
1473     "\
1474 Return the string representation (print form) of the value." },
1475
1476   { "parse-and-eval", 1, 0, 0, gdbscm_parse_and_eval,
1477     "\
1478 Evaluates string in gdb and returns the result as a <gdb:value> object." },
1479
1480   { "history-ref", 1, 0, 0, gdbscm_history_ref,
1481     "\
1482 Return the specified value from GDB's value history." },
1483
1484   { "history-append!", 1, 0, 0, gdbscm_history_append_x,
1485     "\
1486 Append the specified value onto GDB's value history." },
1487
1488   END_FUNCTIONS
1489 };
1490
1491 void
1492 gdbscm_initialize_values (void)
1493 {
1494   value_smob_tag = gdbscm_make_smob_type (value_smob_name,
1495                                           sizeof (value_smob));
1496   scm_set_smob_mark (value_smob_tag, vlscm_mark_value_smob);
1497   scm_set_smob_free (value_smob_tag, vlscm_free_value_smob);
1498   scm_set_smob_print (value_smob_tag, vlscm_print_value_smob);
1499   scm_set_smob_equalp (value_smob_tag, vlscm_equal_p_value_smob);
1500
1501   gdbscm_define_functions (value_functions, 1);
1502
1503   type_keyword = scm_from_latin1_keyword ("type");
1504   encoding_keyword = scm_from_latin1_keyword ("encoding");
1505   errors_keyword = scm_from_latin1_keyword ("errors");
1506   length_keyword = scm_from_latin1_keyword ("length");
1507
1508   error_symbol = scm_from_latin1_symbol ("error");
1509   escape_symbol = scm_from_latin1_symbol ("escape");
1510   substitute_symbol = scm_from_latin1_symbol ("substitute");
1511 }