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