*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / python / python-value.c
CommitLineData
7843261b
TJB
1/* Python interface to values.
2
0fb0cc75 3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
7843261b
TJB
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#include "defs.h"
08c637de 21#include "gdb_assert.h"
7843261b
TJB
22#include "charset.h"
23#include "value.h"
24#include "exceptions.h"
25#include "language.h"
26#include "dfp.h"
79a45b7d 27#include "valprint.h"
7843261b
TJB
28
29/* List of all values which are currently exposed to Python. It is
30 maintained so that when an objfile is discarded, preserve_values
31 can copy the values' types if needed. This is declared
32 unconditionally to reduce the number of uses of HAVE_PYTHON in the
33 generic code. */
4be15b7a
JB
34/* This variable is unnecessarily initialized to NULL in order to
35 work around a linker bug on MacOS. */
36struct value *values_in_python = NULL;
7843261b
TJB
37
38#ifdef HAVE_PYTHON
39
40#include "python-internal.h"
41
42/* Even though Python scalar types directly map to host types, we use
43 target types here to remain consistent with the the values system in
44 GDB (which uses target arithmetic). */
45
46/* Python's integer type corresponds to C's long type. */
d452c4bc 47#define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
7843261b
TJB
48
49/* Python's float type corresponds to C's double type. */
d452c4bc 50#define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
7843261b
TJB
51
52/* Python's long type corresponds to C's long long type. */
d452c4bc 53#define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
7843261b
TJB
54
55#define builtin_type_pybool \
d452c4bc 56 language_bool_type (python_language, python_gdbarch)
7843261b 57
3b7538c0 58#define builtin_type_pychar \
d452c4bc 59 language_string_char_type (python_language, python_gdbarch)
3b7538c0 60
7843261b
TJB
61typedef struct {
62 PyObject_HEAD
63 struct value *value;
c0c6f777 64 PyObject *address;
2c74e833 65 PyObject *type;
7843261b
TJB
66} value_object;
67
7843261b
TJB
68/* Called by the Python interpreter when deallocating a value object. */
69static void
70valpy_dealloc (PyObject *obj)
71{
72 value_object *self = (value_object *) obj;
73
74 value_remove_from_list (&values_in_python, self->value);
75
77316f4c 76 value_free (self->value);
c0c6f777
TJB
77
78 if (self->address)
79 /* Use braces to appease gcc warning. *sigh* */
80 {
81 Py_DECREF (self->address);
82 }
83
2c74e833
TT
84 if (self->type)
85 {
86 Py_DECREF (self->type);
87 }
88
7843261b
TJB
89 self->ob_type->tp_free (self);
90}
91
92/* Called when a new gdb.Value object needs to be allocated. */
93static PyObject *
94valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
95{
96 struct value *value = NULL; /* Initialize to appease gcc warning. */
97 value_object *value_obj;
7843261b
TJB
98
99 if (PyTuple_Size (args) != 1)
100 {
101 PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
102 "1 argument"));
103 return NULL;
104 }
105
106 value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
107 if (value_obj == NULL)
108 {
109 PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
110 "create Value object."));
111 return NULL;
112 }
113
08c637de
TJB
114 value = convert_value_from_python (PyTuple_GetItem (args, 0));
115 if (value == NULL)
7843261b
TJB
116 {
117 subtype->tp_free (value_obj);
08c637de 118 return NULL;
7843261b
TJB
119 }
120
121 value_obj->value = value;
c0c6f777 122 value_obj->address = NULL;
2c74e833 123 value_obj->type = NULL;
7843261b
TJB
124 release_value (value);
125 value_prepend_to_list (&values_in_python, value);
126
127 return (PyObject *) value_obj;
128}
129
130/* Given a value of a pointer type, apply the C unary * operator to it. */
131static PyObject *
132valpy_dereference (PyObject *self, PyObject *args)
133{
134 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
135 volatile struct gdb_exception except;
136
137 TRY_CATCH (except, RETURN_MASK_ALL)
138 {
139 res_val = value_ind (((value_object *) self)->value);
140 }
141 GDB_PY_HANDLE_EXCEPTION (except);
142
143 return value_to_value_object (res_val);
144}
145
08c637de
TJB
146/* Return "&value". */
147static PyObject *
c0c6f777 148valpy_get_address (PyObject *self, void *closure)
08c637de
TJB
149{
150 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
c0c6f777 151 value_object *val_obj = (value_object *) self;
08c637de
TJB
152 volatile struct gdb_exception except;
153
c0c6f777 154 if (!val_obj->address)
08c637de 155 {
c0c6f777
TJB
156 TRY_CATCH (except, RETURN_MASK_ALL)
157 {
158 res_val = value_addr (val_obj->value);
159 }
160 if (except.reason < 0)
161 {
162 val_obj->address = Py_None;
163 Py_INCREF (Py_None);
164 }
165 else
166 val_obj->address = value_to_value_object (res_val);
08c637de 167 }
08c637de 168
c0c6f777
TJB
169 Py_INCREF (val_obj->address);
170
171 return val_obj->address;
08c637de
TJB
172}
173
2c74e833
TT
174/* Return type of the value. */
175static PyObject *
176valpy_get_type (PyObject *self, void *closure)
177{
178 value_object *obj = (value_object *) self;
179 if (!obj->type)
180 {
181 obj->type = type_to_type_object (value_type (obj->value));
182 if (!obj->type)
183 {
184 obj->type = Py_None;
185 Py_INCREF (obj->type);
186 }
187 }
188 Py_INCREF (obj->type);
189 return obj->type;
190}
191
cc924cad
TJB
192/* Implementation of gdb.Value.string ([encoding] [, errors]) -> string
193 Return Unicode string with value contents. If ENCODING is not given,
194 the string is assumed to be encoded in the target's charset. */
b6cb8e7d 195static PyObject *
cc924cad 196valpy_string (PyObject *self, PyObject *args, PyObject *kw)
b6cb8e7d
TJB
197{
198 int length, ret = 0;
199 gdb_byte *buffer;
200 struct value *value = ((value_object *) self)->value;
201 volatile struct gdb_exception except;
202 PyObject *unicode;
203 const char *encoding = NULL;
204 const char *errors = NULL;
205 const char *user_encoding = NULL;
206 const char *la_encoding = NULL;
cc924cad 207 static char *keywords[] = { "encoding", "errors" };
b6cb8e7d 208
cc924cad
TJB
209 if (!PyArg_ParseTupleAndKeywords (args, kw, "|ss", keywords,
210 &user_encoding, &errors))
b6cb8e7d
TJB
211 return NULL;
212
213 TRY_CATCH (except, RETURN_MASK_ALL)
214 {
215 LA_GET_STRING (value, &buffer, &length, &la_encoding);
216 }
217 GDB_PY_HANDLE_EXCEPTION (except);
218
219 encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
220 unicode = PyUnicode_Decode (buffer, length, encoding, errors);
221 xfree (buffer);
222
223 return unicode;
224}
225
2c74e833
TT
226/* Cast a value to a given type. */
227static PyObject *
228valpy_cast (PyObject *self, PyObject *args)
229{
230 PyObject *type_obj;
231 struct type *type;
232 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
233 volatile struct gdb_exception except;
234
235 if (! PyArg_ParseTuple (args, "O", &type_obj))
236 return NULL;
237
238 type = type_object_to_type (type_obj);
239 if (! type)
240 {
241 PyErr_SetString (PyExc_RuntimeError, "argument must be a Type");
242 return NULL;
243 }
244
245 TRY_CATCH (except, RETURN_MASK_ALL)
246 {
247 res_val = value_cast (type, ((value_object *) self)->value);
248 }
249 GDB_PY_HANDLE_EXCEPTION (except);
250
251 return value_to_value_object (res_val);
252}
253
7843261b
TJB
254static Py_ssize_t
255valpy_length (PyObject *self)
256{
257 /* We don't support getting the number of elements in a struct / class. */
258 PyErr_SetString (PyExc_NotImplementedError,
259 "Invalid operation on gdb.Value.");
260 return -1;
261}
262
263/* Given string name of an element inside structure, return its value
264 object. */
265static PyObject *
266valpy_getitem (PyObject *self, PyObject *key)
267{
268 value_object *self_value = (value_object *) self;
08c637de
TJB
269 char *field = NULL;
270 struct value *idx = NULL;
7843261b 271 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
7843261b
TJB
272 volatile struct gdb_exception except;
273
08c637de
TJB
274 if (gdbpy_is_string (key))
275 {
276 field = python_string_to_host_string (key);
277 if (field == NULL)
278 return NULL;
279 }
7843261b
TJB
280
281 TRY_CATCH (except, RETURN_MASK_ALL)
282 {
3c0ed299 283 struct value *tmp = self_value->value;
08c637de
TJB
284
285 if (field)
286 res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
287 else
288 {
289 /* Assume we are attempting an array access, and let the
290 value code throw an exception if the index has an invalid
291 type. */
292 struct value *idx = convert_value_from_python (key);
293 if (idx == NULL)
294 return NULL;
295
2497b498 296 res_val = value_subscript (tmp, value_as_long (idx));
08c637de 297 }
7843261b 298 }
08c637de
TJB
299 if (field)
300 xfree (field);
7843261b
TJB
301 GDB_PY_HANDLE_EXCEPTION (except);
302
7843261b
TJB
303 return value_to_value_object (res_val);
304}
305
306static int
307valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
308{
309 PyErr_Format (PyExc_NotImplementedError,
310 _("Setting of struct elements is not currently supported."));
311 return -1;
312}
313
314/* Called by the Python interpreter to obtain string representation
315 of the object. */
316static PyObject *
317valpy_str (PyObject *self)
318{
319 char *s = NULL;
320 long dummy;
321 struct ui_file *stb;
322 struct cleanup *old_chain;
323 PyObject *result;
79a45b7d 324 struct value_print_options opts;
7843261b
TJB
325 volatile struct gdb_exception except;
326
79a45b7d
TT
327 get_user_print_options (&opts);
328 opts.deref_ref = 0;
329
7843261b
TJB
330 stb = mem_fileopen ();
331 old_chain = make_cleanup_ui_file_delete (stb);
332
333 TRY_CATCH (except, RETURN_MASK_ALL)
334 {
79a45b7d 335 common_val_print (((value_object *) self)->value, stb, 0,
d452c4bc 336 &opts, python_language);
7843261b
TJB
337 s = ui_file_xstrdup (stb, &dummy);
338 }
339 GDB_PY_HANDLE_EXCEPTION (except);
340
341 do_cleanups (old_chain);
342
343 result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
344 xfree (s);
345
346 return result;
347}
348
def2b000
TJB
349/* Implements gdb.Value.is_optimized_out. */
350static PyObject *
351valpy_get_is_optimized_out (PyObject *self, void *closure)
352{
353 struct value *value = ((value_object *) self)->value;
354
355 if (value_optimized_out (value))
356 Py_RETURN_TRUE;
357
358 Py_RETURN_FALSE;
359}
360
7843261b
TJB
361enum valpy_opcode
362{
363 VALPY_ADD,
364 VALPY_SUB,
365 VALPY_MUL,
366 VALPY_DIV,
367 VALPY_REM,
08c637de
TJB
368 VALPY_POW,
369 VALPY_LSH,
370 VALPY_RSH,
371 VALPY_BITAND,
372 VALPY_BITOR,
373 VALPY_BITXOR
7843261b
TJB
374};
375
376/* If TYPE is a reference, return the target; otherwise return TYPE. */
377#define STRIP_REFERENCE(TYPE) \
378 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
379
380/* Returns a value object which is the result of applying the operation
381 specified by OPCODE to the given arguments. */
382static PyObject *
383valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
384{
385 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
386 volatile struct gdb_exception except;
387
388 TRY_CATCH (except, RETURN_MASK_ALL)
389 {
390 struct value *arg1, *arg2;
391
392 /* If the gdb.Value object is the second operand, then it will be passed
393 to us as the OTHER argument, and SELF will be an entirely different
394 kind of object, altogether. Because of this, we can't assume self is
395 a gdb.Value object and need to convert it from python as well. */
396 arg1 = convert_value_from_python (self);
08c637de 397 if (arg1 == NULL)
cc924cad 398 break;
08c637de 399
7843261b 400 arg2 = convert_value_from_python (other);
08c637de 401 if (arg2 == NULL)
cc924cad 402 break;
7843261b
TJB
403
404 switch (opcode)
405 {
406 case VALPY_ADD:
407 {
408 struct type *ltype = value_type (arg1);
409 struct type *rtype = value_type (arg2);
410
411 CHECK_TYPEDEF (ltype);
412 ltype = STRIP_REFERENCE (ltype);
413 CHECK_TYPEDEF (rtype);
414 rtype = STRIP_REFERENCE (rtype);
415
2497b498
UW
416 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
417 && is_integral_type (rtype))
418 res_val = value_ptradd (arg1, value_as_long (arg2));
419 else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
420 && is_integral_type (ltype))
421 res_val = value_ptradd (arg2, value_as_long (arg1));
7843261b
TJB
422 else
423 res_val = value_binop (arg1, arg2, BINOP_ADD);
424 }
425 break;
426 case VALPY_SUB:
427 {
428 struct type *ltype = value_type (arg1);
429 struct type *rtype = value_type (arg2);
430
431 CHECK_TYPEDEF (ltype);
432 ltype = STRIP_REFERENCE (ltype);
433 CHECK_TYPEDEF (rtype);
434 rtype = STRIP_REFERENCE (rtype);
435
2497b498
UW
436 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
437 && TYPE_CODE (rtype) == TYPE_CODE_PTR)
438 /* A ptrdiff_t for the target would be preferable here. */
439 res_val = value_from_longest (builtin_type_pyint,
440 value_ptrdiff (arg1, arg2));
441 else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
442 && is_integral_type (rtype))
443 res_val = value_ptradd (arg1, - value_as_long (arg2));
7843261b
TJB
444 else
445 res_val = value_binop (arg1, arg2, BINOP_SUB);
446 }
447 break;
448 case VALPY_MUL:
449 res_val = value_binop (arg1, arg2, BINOP_MUL);
450 break;
451 case VALPY_DIV:
452 res_val = value_binop (arg1, arg2, BINOP_DIV);
453 break;
454 case VALPY_REM:
455 res_val = value_binop (arg1, arg2, BINOP_REM);
456 break;
457 case VALPY_POW:
458 res_val = value_binop (arg1, arg2, BINOP_EXP);
459 break;
08c637de
TJB
460 case VALPY_LSH:
461 res_val = value_binop (arg1, arg2, BINOP_LSH);
462 break;
463 case VALPY_RSH:
464 res_val = value_binop (arg1, arg2, BINOP_RSH);
465 break;
466 case VALPY_BITAND:
467 res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
468 break;
469 case VALPY_BITOR:
470 res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
471 break;
472 case VALPY_BITXOR:
473 res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
474 break;
7843261b
TJB
475 }
476 }
477 GDB_PY_HANDLE_EXCEPTION (except);
478
cc924cad 479 return res_val ? value_to_value_object (res_val) : NULL;
7843261b
TJB
480}
481
482static PyObject *
483valpy_add (PyObject *self, PyObject *other)
484{
485 return valpy_binop (VALPY_ADD, self, other);
486}
487
488static PyObject *
489valpy_subtract (PyObject *self, PyObject *other)
490{
491 return valpy_binop (VALPY_SUB, self, other);
492}
493
494static PyObject *
495valpy_multiply (PyObject *self, PyObject *other)
496{
497 return valpy_binop (VALPY_MUL, self, other);
498}
499
500static PyObject *
501valpy_divide (PyObject *self, PyObject *other)
502{
503 return valpy_binop (VALPY_DIV, self, other);
504}
505
506static PyObject *
507valpy_remainder (PyObject *self, PyObject *other)
508{
509 return valpy_binop (VALPY_REM, self, other);
510}
511
512static PyObject *
513valpy_power (PyObject *self, PyObject *other, PyObject *unused)
514{
515 /* We don't support the ternary form of pow. I don't know how to express
516 that, so let's just throw NotImplementedError to at least do something
517 about it. */
518 if (unused != Py_None)
519 {
520 PyErr_SetString (PyExc_NotImplementedError,
521 "Invalid operation on gdb.Value.");
522 return NULL;
523 }
524
525 return valpy_binop (VALPY_POW, self, other);
526}
527
528static PyObject *
529valpy_negative (PyObject *self)
530{
531 struct value *val = NULL;
532 volatile struct gdb_exception except;
533
534 TRY_CATCH (except, RETURN_MASK_ALL)
535 {
536 val = value_neg (((value_object *) self)->value);
537 }
538 GDB_PY_HANDLE_EXCEPTION (except);
539
540 return value_to_value_object (val);
541}
542
543static PyObject *
544valpy_positive (PyObject *self)
545{
546 struct value *copy = value_copy (((value_object *) self)->value);
547
548 return value_to_value_object (copy);
549}
550
551static PyObject *
552valpy_absolute (PyObject *self)
553{
22601c15
UW
554 struct value *value = ((value_object *) self)->value;
555 if (value_less (value, value_zero (value_type (value), not_lval)))
7843261b
TJB
556 return valpy_negative (self);
557 else
558 return valpy_positive (self);
559}
560
561/* Implements boolean evaluation of gdb.Value. */
562static int
563valpy_nonzero (PyObject *self)
564{
565 value_object *self_value = (value_object *) self;
566 struct type *type;
567
568 type = check_typedef (value_type (self_value->value));
569
570 if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
571 return !!value_as_long (self_value->value);
572 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
573 return value_as_double (self_value->value) != 0;
574 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
575 return !decimal_is_zero (value_contents (self_value->value),
e17a4113
UW
576 TYPE_LENGTH (type),
577 gdbarch_byte_order (get_type_arch (type)));
7843261b
TJB
578 else
579 {
580 PyErr_SetString (PyExc_TypeError, _("Attempted truth testing on invalid "
581 "gdb.Value type."));
582 return 0;
583 }
584}
585
08c637de 586/* Implements ~ for value objects. */
7843261b 587static PyObject *
08c637de 588valpy_invert (PyObject *self)
7843261b 589{
08c637de 590 struct value *val = NULL;
7843261b
TJB
591 volatile struct gdb_exception except;
592
08c637de 593 TRY_CATCH (except, RETURN_MASK_ALL)
7843261b 594 {
08c637de
TJB
595 val = value_complement (((value_object *) self)->value);
596 }
597 GDB_PY_HANDLE_EXCEPTION (except);
7843261b 598
08c637de
TJB
599 return value_to_value_object (val);
600}
7843261b 601
08c637de
TJB
602/* Implements left shift for value objects. */
603static PyObject *
604valpy_lsh (PyObject *self, PyObject *other)
605{
606 return valpy_binop (VALPY_LSH, self, other);
607}
7843261b 608
08c637de
TJB
609/* Implements right shift for value objects. */
610static PyObject *
611valpy_rsh (PyObject *self, PyObject *other)
612{
613 return valpy_binop (VALPY_RSH, self, other);
614}
7843261b 615
08c637de
TJB
616/* Implements bitwise and for value objects. */
617static PyObject *
618valpy_and (PyObject *self, PyObject *other)
619{
620 return valpy_binop (VALPY_BITAND, self, other);
621}
7843261b 622
08c637de
TJB
623/* Implements bitwise or for value objects. */
624static PyObject *
625valpy_or (PyObject *self, PyObject *other)
626{
627 return valpy_binop (VALPY_BITOR, self, other);
628}
629
630/* Implements bitwise xor for value objects. */
631static PyObject *
632valpy_xor (PyObject *self, PyObject *other)
633{
634 return valpy_binop (VALPY_BITXOR, self, other);
635}
636
637/* Implements comparison operations for value objects. */
638static PyObject *
639valpy_richcompare (PyObject *self, PyObject *other, int op)
640{
641 int result = 0;
642 struct value *value_other;
643 volatile struct gdb_exception except;
644
645 if (other == Py_None)
7843261b
TJB
646 /* Comparing with None is special. From what I can tell, in Python
647 None is smaller than anything else. */
648 switch (op) {
649 case Py_LT:
650 case Py_LE:
651 case Py_EQ:
652 Py_RETURN_FALSE;
653 case Py_NE:
654 case Py_GT:
655 case Py_GE:
656 Py_RETURN_TRUE;
657 default:
658 /* Can't happen. */
659 PyErr_SetString (PyExc_NotImplementedError,
660 "Invalid operation on gdb.Value.");
661 return NULL;
662 }
7843261b
TJB
663
664 TRY_CATCH (except, RETURN_MASK_ALL)
665 {
08c637de
TJB
666 value_other = convert_value_from_python (other);
667 if (value_other == NULL)
668 return NULL;
669
7843261b
TJB
670 switch (op) {
671 case Py_LT:
672 result = value_less (((value_object *) self)->value, value_other);
673 break;
674 case Py_LE:
675 result = value_less (((value_object *) self)->value, value_other)
676 || value_equal (((value_object *) self)->value, value_other);
677 break;
678 case Py_EQ:
679 result = value_equal (((value_object *) self)->value, value_other);
680 break;
681 case Py_NE:
682 result = !value_equal (((value_object *) self)->value, value_other);
683 break;
684 case Py_GT:
685 result = value_less (value_other, ((value_object *) self)->value);
686 break;
687 case Py_GE:
688 result = value_less (value_other, ((value_object *) self)->value)
689 || value_equal (((value_object *) self)->value, value_other);
690 break;
691 default:
692 /* Can't happen. */
693 PyErr_SetString (PyExc_NotImplementedError,
694 "Invalid operation on gdb.Value.");
695 return NULL;
696 }
697 }
698 GDB_PY_HANDLE_EXCEPTION (except);
699
700 if (result == 1)
701 Py_RETURN_TRUE;
702
703 Py_RETURN_FALSE;
704}
705
08c637de
TJB
706/* Helper function to determine if a type is "int-like". */
707static int
708is_intlike (struct type *type, int ptr_ok)
709{
710 CHECK_TYPEDEF (type);
711 return (TYPE_CODE (type) == TYPE_CODE_INT
712 || TYPE_CODE (type) == TYPE_CODE_ENUM
713 || TYPE_CODE (type) == TYPE_CODE_BOOL
714 || TYPE_CODE (type) == TYPE_CODE_CHAR
715 || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
716}
717
718/* Implements conversion to int. */
719static PyObject *
720valpy_int (PyObject *self)
721{
722 struct value *value = ((value_object *) self)->value;
723 struct type *type = value_type (value);
724 LONGEST l = 0;
725 volatile struct gdb_exception except;
726
727 CHECK_TYPEDEF (type);
728 if (!is_intlike (type, 0))
729 {
730 PyErr_SetString (PyExc_RuntimeError, "cannot convert value to int");
731 return NULL;
732 }
733
734 TRY_CATCH (except, RETURN_MASK_ALL)
735 {
736 l = value_as_long (value);
737 }
738 GDB_PY_HANDLE_EXCEPTION (except);
739
740 return PyInt_FromLong (l);
741}
742
743/* Implements conversion to long. */
744static PyObject *
745valpy_long (PyObject *self)
746{
747 struct value *value = ((value_object *) self)->value;
748 struct type *type = value_type (value);
749 LONGEST l = 0;
750 volatile struct gdb_exception except;
751
752 if (!is_intlike (type, 1))
753 {
754 PyErr_SetString (PyExc_RuntimeError, "cannot convert value to long");
755 return NULL;
756 }
757
758 TRY_CATCH (except, RETURN_MASK_ALL)
759 {
760 l = value_as_long (value);
761 }
762 GDB_PY_HANDLE_EXCEPTION (except);
763
764 return PyLong_FromLong (l);
765}
766
767/* Implements conversion to float. */
768static PyObject *
769valpy_float (PyObject *self)
770{
771 struct value *value = ((value_object *) self)->value;
772 struct type *type = value_type (value);
773 double d = 0;
774 volatile struct gdb_exception except;
775
776 CHECK_TYPEDEF (type);
777 if (TYPE_CODE (type) != TYPE_CODE_FLT)
778 {
779 PyErr_SetString (PyExc_RuntimeError, "cannot convert value to float");
780 return NULL;
781 }
782
783 TRY_CATCH (except, RETURN_MASK_ALL)
784 {
785 d = value_as_double (value);
786 }
787 GDB_PY_HANDLE_EXCEPTION (except);
788
789 return PyFloat_FromDouble (d);
790}
791
7843261b
TJB
792/* Returns an object for a value which is released from the all_values chain,
793 so its lifetime is not bound to the execution of a command. */
794PyObject *
795value_to_value_object (struct value *val)
796{
797 value_object *val_obj;
798
799 val_obj = PyObject_New (value_object, &value_object_type);
800 if (val_obj != NULL)
801 {
802 val_obj->value = val;
c0c6f777 803 val_obj->address = NULL;
2c74e833 804 val_obj->type = NULL;
7843261b
TJB
805 release_value (val);
806 value_prepend_to_list (&values_in_python, val);
807 }
808
809 return (PyObject *) val_obj;
810}
811
a6bac58e
TT
812/* Returns value structure corresponding to the given value object. */
813struct value *
814value_object_to_value (PyObject *self)
815{
816 value_object *real;
817 if (! PyObject_TypeCheck (self, &value_object_type))
818 return NULL;
819 real = (value_object *) self;
820 return real->value;
821}
822
7843261b 823/* Try to convert a Python value to a gdb value. If the value cannot
08c637de 824 be converted, set a Python exception and return NULL. */
7843261b
TJB
825
826struct value *
827convert_value_from_python (PyObject *obj)
828{
829 struct value *value = NULL; /* -Wall */
830 PyObject *target_str, *unicode_str;
831 struct cleanup *old;
08c637de
TJB
832 volatile struct gdb_exception except;
833 int cmp;
7843261b 834
08c637de 835 gdb_assert (obj != NULL);
7843261b 836
08c637de 837 TRY_CATCH (except, RETURN_MASK_ALL)
7843261b 838 {
08c637de
TJB
839 if (PyBool_Check (obj))
840 {
841 cmp = PyObject_IsTrue (obj);
842 if (cmp >= 0)
843 value = value_from_longest (builtin_type_pybool, cmp);
844 }
845 else if (PyInt_Check (obj))
846 {
847 long l = PyInt_AsLong (obj);
7843261b 848
08c637de
TJB
849 if (! PyErr_Occurred ())
850 value = value_from_longest (builtin_type_pyint, l);
851 }
852 else if (PyLong_Check (obj))
853 {
854 LONGEST l = PyLong_AsLongLong (obj);
7843261b 855
08c637de
TJB
856 if (! PyErr_Occurred ())
857 value = value_from_longest (builtin_type_pylong, l);
858 }
859 else if (PyFloat_Check (obj))
860 {
861 double d = PyFloat_AsDouble (obj);
7843261b 862
08c637de
TJB
863 if (! PyErr_Occurred ())
864 value = value_from_double (builtin_type_pyfloat, d);
865 }
866 else if (gdbpy_is_string (obj))
867 {
868 char *s;
869
870 s = python_string_to_target_string (obj);
871 if (s != NULL)
872 {
873 old = make_cleanup (xfree, s);
3b7538c0 874 value = value_cstring (s, strlen (s), builtin_type_pychar);
08c637de
TJB
875 do_cleanups (old);
876 }
877 }
878 else if (PyObject_TypeCheck (obj, &value_object_type))
cc924cad 879 value = value_copy (((value_object *) obj)->value);
08c637de
TJB
880 else
881 PyErr_Format (PyExc_TypeError, _("Could not convert Python object: %s"),
882 PyString_AsString (PyObject_Str (obj)));
883 }
884 if (except.reason < 0)
885 {
886 PyErr_Format (except.reason == RETURN_QUIT
887 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
888 "%s", except.message);
889 return NULL;
890 }
7843261b
TJB
891
892 return value;
893}
894
895/* Returns value object in the ARGth position in GDB's history. */
896PyObject *
08c637de 897gdbpy_history (PyObject *self, PyObject *args)
7843261b
TJB
898{
899 int i;
900 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
901 volatile struct gdb_exception except;
902
903 if (!PyArg_ParseTuple (args, "i", &i))
904 return NULL;
905
906 TRY_CATCH (except, RETURN_MASK_ALL)
907 {
908 res_val = access_value_history (i);
909 }
910 GDB_PY_HANDLE_EXCEPTION (except);
911
912 return value_to_value_object (res_val);
913}
914
915void
916gdbpy_initialize_values (void)
917{
7843261b
TJB
918 if (PyType_Ready (&value_object_type) < 0)
919 return;
920
921 Py_INCREF (&value_object_type);
922 PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type);
923
924 values_in_python = NULL;
925}
926
2c74e833
TT
927\f
928
def2b000 929static PyGetSetDef value_object_getset[] = {
c0c6f777
TJB
930 { "address", valpy_get_address, NULL, "The address of the value.",
931 NULL },
def2b000
TJB
932 { "is_optimized_out", valpy_get_is_optimized_out, NULL,
933 "Boolean telling whether the value is optimized out (i.e., not available).",
934 NULL },
2c74e833 935 { "type", valpy_get_type, NULL, "Type of the value.", NULL },
def2b000
TJB
936 {NULL} /* Sentinel */
937};
938
f9176c46 939static PyMethodDef value_object_methods[] = {
2c74e833 940 { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
f9176c46 941 { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
cc924cad
TJB
942 { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
943 "string ([encoding] [, errors]) -> string\n\
944Return Unicode string representation of the value." },
f9176c46
PA
945 {NULL} /* Sentinel */
946};
947
948static PyNumberMethods value_object_as_number = {
949 valpy_add,
950 valpy_subtract,
951 valpy_multiply,
952 valpy_divide,
953 valpy_remainder,
954 NULL, /* nb_divmod */
955 valpy_power, /* nb_power */
956 valpy_negative, /* nb_negative */
957 valpy_positive, /* nb_positive */
958 valpy_absolute, /* nb_absolute */
08c637de
TJB
959 valpy_nonzero, /* nb_nonzero */
960 valpy_invert, /* nb_invert */
961 valpy_lsh, /* nb_lshift */
962 valpy_rsh, /* nb_rshift */
963 valpy_and, /* nb_and */
964 valpy_xor, /* nb_xor */
965 valpy_or, /* nb_or */
966 NULL, /* nb_coerce */
967 valpy_int, /* nb_int */
968 valpy_long, /* nb_long */
969 valpy_float, /* nb_float */
970 NULL, /* nb_oct */
971 NULL /* nb_hex */
f9176c46
PA
972};
973
974static PyMappingMethods value_object_as_mapping = {
975 valpy_length,
976 valpy_getitem,
977 valpy_setitem
978};
979
980PyTypeObject value_object_type = {
981 PyObject_HEAD_INIT (NULL)
982 0, /*ob_size*/
983 "gdb.Value", /*tp_name*/
984 sizeof (value_object), /*tp_basicsize*/
985 0, /*tp_itemsize*/
986 valpy_dealloc, /*tp_dealloc*/
987 0, /*tp_print*/
988 0, /*tp_getattr*/
989 0, /*tp_setattr*/
990 0, /*tp_compare*/
991 0, /*tp_repr*/
992 &value_object_as_number, /*tp_as_number*/
993 0, /*tp_as_sequence*/
994 &value_object_as_mapping, /*tp_as_mapping*/
995 0, /*tp_hash */
996 0, /*tp_call*/
997 valpy_str, /*tp_str*/
998 0, /*tp_getattro*/
999 0, /*tp_setattro*/
1000 0, /*tp_as_buffer*/
1001 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
1002 "GDB value object", /* tp_doc */
1003 0, /* tp_traverse */
1004 0, /* tp_clear */
1005 valpy_richcompare, /* tp_richcompare */
1006 0, /* tp_weaklistoffset */
1007 0, /* tp_iter */
1008 0, /* tp_iternext */
08c637de
TJB
1009 value_object_methods, /* tp_methods */
1010 0, /* tp_members */
def2b000 1011 value_object_getset, /* tp_getset */
08c637de
TJB
1012 0, /* tp_base */
1013 0, /* tp_dict */
1014 0, /* tp_descr_get */
1015 0, /* tp_descr_set */
1016 0, /* tp_dictoffset */
1017 0, /* tp_init */
1018 0, /* tp_alloc */
1019 valpy_new /* tp_new */
f9176c46
PA
1020};
1021
7843261b 1022#endif /* HAVE_PYTHON */
This page took 0.154119 seconds and 4 git commands to generate.