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