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