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