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