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