2012-03-01 Pedro Alves <palves@redhat.com>
[deliverable/binutils-gdb.git] / gdb / python / py-value.c
CommitLineData
7843261b
TJB
1/* Python interface to values.
2
888fe1e1 3 Copyright (C) 2008, 2009, 2010, 2011, 2012 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"
5374244e 28#include "infcall.h"
f9ffd4bb 29#include "expression.h"
03f17ccf 30#include "cp-abi.h"
49a8461d 31#include "python.h"
7843261b 32
7843261b
TJB
33#ifdef HAVE_PYTHON
34
35#include "python-internal.h"
36
37/* Even though Python scalar types directly map to host types, we use
b021a221 38 target types here to remain consistent with the values system in
7843261b
TJB
39 GDB (which uses target arithmetic). */
40
41/* Python's integer type corresponds to C's long type. */
d452c4bc 42#define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
7843261b
TJB
43
44/* Python's float type corresponds to C's double type. */
d452c4bc 45#define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
7843261b
TJB
46
47/* Python's long type corresponds to C's long long type. */
d452c4bc 48#define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
7843261b 49
595939de
PM
50/* Python's long type corresponds to C's long long type. Unsigned version. */
51#define builtin_type_upylong builtin_type \
52 (python_gdbarch)->builtin_unsigned_long_long
53
7843261b 54#define builtin_type_pybool \
d452c4bc 55 language_bool_type (python_language, python_gdbarch)
7843261b 56
3b7538c0 57#define builtin_type_pychar \
d452c4bc 58 language_string_char_type (python_language, python_gdbarch)
3b7538c0 59
4e7a5ef5 60typedef struct value_object {
7843261b 61 PyObject_HEAD
4e7a5ef5
TT
62 struct value_object *next;
63 struct value_object *prev;
7843261b 64 struct value *value;
c0c6f777 65 PyObject *address;
2c74e833 66 PyObject *type;
03f17ccf 67 PyObject *dynamic_type;
7843261b
TJB
68} value_object;
69
4e7a5ef5
TT
70/* List of all values which are currently exposed to Python. It is
71 maintained so that when an objfile is discarded, preserve_values
72 can copy the values' types if needed. */
73/* This variable is unnecessarily initialized to NULL in order to
74 work around a linker bug on MacOS. */
75static value_object *values_in_python = NULL;
76
7843261b
TJB
77/* Called by the Python interpreter when deallocating a value object. */
78static void
79valpy_dealloc (PyObject *obj)
80{
81 value_object *self = (value_object *) obj;
82
4e7a5ef5
TT
83 /* Remove SELF from the global list. */
84 if (self->prev)
85 self->prev->next = self->next;
86 else
87 {
88 gdb_assert (values_in_python == self);
89 values_in_python = self->next;
90 }
91 if (self->next)
92 self->next->prev = self->prev;
7843261b 93
77316f4c 94 value_free (self->value);
c0c6f777
TJB
95
96 if (self->address)
97 /* Use braces to appease gcc warning. *sigh* */
98 {
99 Py_DECREF (self->address);
100 }
101
2c74e833
TT
102 if (self->type)
103 {
104 Py_DECREF (self->type);
105 }
106
03f17ccf
TT
107 Py_XDECREF (self->dynamic_type);
108
7843261b
TJB
109 self->ob_type->tp_free (self);
110}
111
4e7a5ef5
TT
112/* Helper to push a Value object on the global list. */
113static void
114note_value (value_object *value_obj)
115{
116 value_obj->next = values_in_python;
117 if (value_obj->next)
118 value_obj->next->prev = value_obj;
119 value_obj->prev = NULL;
120 values_in_python = value_obj;
121}
122
8dc78533
JK
123/* Called when a new gdb.Value object needs to be allocated. Returns NULL on
124 error, with a python exception set. */
7843261b
TJB
125static PyObject *
126valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
127{
128 struct value *value = NULL; /* Initialize to appease gcc warning. */
129 value_object *value_obj;
7843261b
TJB
130
131 if (PyTuple_Size (args) != 1)
132 {
133 PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
134 "1 argument"));
135 return NULL;
136 }
137
138 value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
139 if (value_obj == NULL)
140 {
141 PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
142 "create Value object."));
143 return NULL;
144 }
145
08c637de
TJB
146 value = convert_value_from_python (PyTuple_GetItem (args, 0));
147 if (value == NULL)
7843261b
TJB
148 {
149 subtype->tp_free (value_obj);
08c637de 150 return NULL;
7843261b
TJB
151 }
152
153 value_obj->value = value;
e848a8a5 154 release_value_or_incref (value);
c0c6f777 155 value_obj->address = NULL;
2c74e833 156 value_obj->type = NULL;
03f17ccf 157 value_obj->dynamic_type = NULL;
4e7a5ef5 158 note_value (value_obj);
7843261b
TJB
159
160 return (PyObject *) value_obj;
161}
162
4e7a5ef5
TT
163/* Iterate over all the Value objects, calling preserve_one_value on
164 each. */
165void
166preserve_python_values (struct objfile *objfile, htab_t copied_types)
167{
168 value_object *iter;
169
170 for (iter = values_in_python; iter; iter = iter->next)
171 preserve_one_value (iter->value, objfile, copied_types);
172}
173
7843261b
TJB
174/* Given a value of a pointer type, apply the C unary * operator to it. */
175static PyObject *
176valpy_dereference (PyObject *self, PyObject *args)
177{
7843261b 178 volatile struct gdb_exception except;
888fe1e1 179 PyObject *result = NULL;
7843261b
TJB
180
181 TRY_CATCH (except, RETURN_MASK_ALL)
182 {
888fe1e1
TT
183 struct value *res_val;
184 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
185
7843261b 186 res_val = value_ind (((value_object *) self)->value);
888fe1e1
TT
187 result = value_to_value_object (res_val);
188 do_cleanups (cleanup);
7843261b
TJB
189 }
190 GDB_PY_HANDLE_EXCEPTION (except);
191
888fe1e1 192 return result;
7843261b
TJB
193}
194
08c637de
TJB
195/* Return "&value". */
196static PyObject *
c0c6f777 197valpy_get_address (PyObject *self, void *closure)
08c637de 198{
c0c6f777 199 value_object *val_obj = (value_object *) self;
08c637de
TJB
200 volatile struct gdb_exception except;
201
c0c6f777 202 if (!val_obj->address)
08c637de 203 {
c0c6f777
TJB
204 TRY_CATCH (except, RETURN_MASK_ALL)
205 {
888fe1e1
TT
206 struct value *res_val;
207 struct cleanup *cleanup
208 = make_cleanup_value_free_to_mark (value_mark ());
209
c0c6f777 210 res_val = value_addr (val_obj->value);
888fe1e1
TT
211 val_obj->address = value_to_value_object (res_val);
212 do_cleanups (cleanup);
c0c6f777
TJB
213 }
214 if (except.reason < 0)
215 {
216 val_obj->address = Py_None;
217 Py_INCREF (Py_None);
218 }
08c637de 219 }
08c637de 220
3fcaed38 221 Py_XINCREF (val_obj->address);
c0c6f777
TJB
222
223 return val_obj->address;
08c637de
TJB
224}
225
2c74e833
TT
226/* Return type of the value. */
227static PyObject *
228valpy_get_type (PyObject *self, void *closure)
229{
230 value_object *obj = (value_object *) self;
d59b6f6c 231
2c74e833
TT
232 if (!obj->type)
233 {
234 obj->type = type_to_type_object (value_type (obj->value));
235 if (!obj->type)
03f17ccf 236 return NULL;
2c74e833
TT
237 }
238 Py_INCREF (obj->type);
239 return obj->type;
240}
241
03f17ccf
TT
242/* Return dynamic type of the value. */
243
244static PyObject *
245valpy_get_dynamic_type (PyObject *self, void *closure)
246{
247 value_object *obj = (value_object *) self;
248 volatile struct gdb_exception except;
249 struct type *type = NULL;
250
251 if (obj->dynamic_type != NULL)
252 {
253 Py_INCREF (obj->dynamic_type);
254 return obj->dynamic_type;
255 }
256
257 TRY_CATCH (except, RETURN_MASK_ALL)
258 {
259 struct value *val = obj->value;
888fe1e1 260 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
03f17ccf
TT
261
262 type = value_type (val);
263 CHECK_TYPEDEF (type);
264
265 if (((TYPE_CODE (type) == TYPE_CODE_PTR)
266 || (TYPE_CODE (type) == TYPE_CODE_REF))
267 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
268 {
269 struct value *target;
270 int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
271
272 target = value_ind (val);
273 type = value_rtti_type (target, NULL, NULL, NULL);
274
275 if (type)
276 {
277 if (was_pointer)
278 type = lookup_pointer_type (type);
279 else
280 type = lookup_reference_type (type);
281 }
282 }
283 else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
284 type = value_rtti_type (val, NULL, NULL, NULL);
285 else
286 {
287 /* Re-use object's static type. */
288 type = NULL;
289 }
888fe1e1
TT
290
291 do_cleanups (cleanup);
03f17ccf
TT
292 }
293 GDB_PY_HANDLE_EXCEPTION (except);
294
295 if (type == NULL)
296 {
297 /* Ensure that the TYPE field is ready. */
298 if (!valpy_get_type (self, NULL))
299 return NULL;
300 /* We don't need to incref here, because valpy_get_type already
301 did it for us. */
302 obj->dynamic_type = obj->type;
303 }
304 else
305 obj->dynamic_type = type_to_type_object (type);
306
307 Py_INCREF (obj->dynamic_type);
308 return obj->dynamic_type;
309}
310
be759fcf
PM
311/* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
312 string. Return a PyObject representing a lazy_string_object type.
313 A lazy string is a pointer to a string with an optional encoding and
314 length. If ENCODING is not given, encoding is set to None. If an
315 ENCODING is provided the encoding parameter is set to ENCODING, but
316 the string is not encoded. If LENGTH is provided then the length
317 parameter is set to LENGTH, otherwise length will be set to -1 (first
318 null of appropriate with). */
319static PyObject *
320valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
321{
74aedc46 322 gdb_py_longest length = -1;
be759fcf
PM
323 struct value *value = ((value_object *) self)->value;
324 const char *user_encoding = NULL;
325 static char *keywords[] = { "encoding", "length", NULL };
888fe1e1 326 PyObject *str_obj = NULL;
f287c1f3 327 volatile struct gdb_exception except;
be759fcf 328
74aedc46 329 if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords,
be759fcf
PM
330 &user_encoding, &length))
331 return NULL;
332
f287c1f3
PM
333 TRY_CATCH (except, RETURN_MASK_ALL)
334 {
888fe1e1
TT
335 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
336
f287c1f3
PM
337 if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
338 value = value_ind (value);
888fe1e1
TT
339
340 str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
341 user_encoding,
342 value_type (value));
343
344 do_cleanups (cleanup);
f287c1f3
PM
345 }
346 GDB_PY_HANDLE_EXCEPTION (except);
be759fcf 347
888fe1e1 348 return str_obj;
be759fcf
PM
349}
350
fbb8f299
PM
351/* Implementation of gdb.Value.string ([encoding] [, errors]
352 [, length]) -> string. Return Unicode string with value contents.
353 If ENCODING is not given, the string is assumed to be encoded in
354 the target's charset. If LENGTH is provided, only fetch string to
355 the length provided. */
356
b6cb8e7d 357static PyObject *
cc924cad 358valpy_string (PyObject *self, PyObject *args, PyObject *kw)
b6cb8e7d 359{
f92adf3c 360 int length = -1;
b6cb8e7d
TJB
361 gdb_byte *buffer;
362 struct value *value = ((value_object *) self)->value;
363 volatile struct gdb_exception except;
364 PyObject *unicode;
365 const char *encoding = NULL;
366 const char *errors = NULL;
367 const char *user_encoding = NULL;
368 const char *la_encoding = NULL;
96c07c5b 369 struct type *char_type;
31158f0e 370 static char *keywords[] = { "encoding", "errors", "length", NULL };
b6cb8e7d 371
fbb8f299
PM
372 if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
373 &user_encoding, &errors, &length))
b6cb8e7d
TJB
374 return NULL;
375
376 TRY_CATCH (except, RETURN_MASK_ALL)
377 {
96c07c5b 378 LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
b6cb8e7d
TJB
379 }
380 GDB_PY_HANDLE_EXCEPTION (except);
381
382 encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
96c07c5b
TT
383 unicode = PyUnicode_Decode (buffer, length * TYPE_LENGTH (char_type),
384 encoding, errors);
b6cb8e7d
TJB
385 xfree (buffer);
386
387 return unicode;
388}
389
f9ffd4bb
TT
390/* A helper function that implements the various cast operators. */
391
2c74e833 392static PyObject *
f9ffd4bb 393valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
2c74e833 394{
888fe1e1 395 PyObject *type_obj, *result = NULL;
2c74e833 396 struct type *type;
2c74e833
TT
397 volatile struct gdb_exception except;
398
399 if (! PyArg_ParseTuple (args, "O", &type_obj))
400 return NULL;
401
402 type = type_object_to_type (type_obj);
403 if (! type)
404 {
044c0f87
PM
405 PyErr_SetString (PyExc_RuntimeError,
406 _("Argument must be a type."));
2c74e833
TT
407 return NULL;
408 }
409
410 TRY_CATCH (except, RETURN_MASK_ALL)
411 {
f9ffd4bb 412 struct value *val = ((value_object *) self)->value;
888fe1e1
TT
413 struct value *res_val;
414 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
f9ffd4bb
TT
415
416 if (op == UNOP_DYNAMIC_CAST)
417 res_val = value_dynamic_cast (type, val);
418 else if (op == UNOP_REINTERPRET_CAST)
419 res_val = value_reinterpret_cast (type, val);
420 else
421 {
422 gdb_assert (op == UNOP_CAST);
423 res_val = value_cast (type, val);
424 }
888fe1e1
TT
425
426 result = value_to_value_object (res_val);
427 do_cleanups (cleanup);
2c74e833
TT
428 }
429 GDB_PY_HANDLE_EXCEPTION (except);
430
888fe1e1 431 return result;
2c74e833
TT
432}
433
f9ffd4bb
TT
434/* Implementation of the "cast" method. */
435
436static PyObject *
437valpy_cast (PyObject *self, PyObject *args)
438{
439 return valpy_do_cast (self, args, UNOP_CAST);
440}
441
442/* Implementation of the "dynamic_cast" method. */
443
444static PyObject *
445valpy_dynamic_cast (PyObject *self, PyObject *args)
446{
447 return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
448}
449
450/* Implementation of the "reinterpret_cast" method. */
451
452static PyObject *
453valpy_reinterpret_cast (PyObject *self, PyObject *args)
454{
455 return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
456}
457
7843261b
TJB
458static Py_ssize_t
459valpy_length (PyObject *self)
460{
461 /* We don't support getting the number of elements in a struct / class. */
462 PyErr_SetString (PyExc_NotImplementedError,
044c0f87 463 _("Invalid operation on gdb.Value."));
7843261b
TJB
464 return -1;
465}
466
467/* Given string name of an element inside structure, return its value
8dc78533 468 object. Returns NULL on error, with a python exception set. */
7843261b
TJB
469static PyObject *
470valpy_getitem (PyObject *self, PyObject *key)
471{
472 value_object *self_value = (value_object *) self;
08c637de 473 char *field = NULL;
7843261b 474 volatile struct gdb_exception except;
888fe1e1 475 PyObject *result = NULL;
7843261b 476
08c637de
TJB
477 if (gdbpy_is_string (key))
478 {
479 field = python_string_to_host_string (key);
480 if (field == NULL)
481 return NULL;
482 }
7843261b
TJB
483
484 TRY_CATCH (except, RETURN_MASK_ALL)
485 {
3c0ed299 486 struct value *tmp = self_value->value;
888fe1e1
TT
487 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
488 struct value *res_val = NULL;
08c637de
TJB
489
490 if (field)
491 res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
492 else
493 {
494 /* Assume we are attempting an array access, and let the
495 value code throw an exception if the index has an invalid
496 type. */
497 struct value *idx = convert_value_from_python (key);
d59b6f6c 498
570e2b1a 499 if (idx != NULL)
2e4d963f
PM
500 {
501 /* Check the value's type is something that can be accessed via
502 a subscript. */
503 struct type *type;
d59b6f6c 504
2e4d963f
PM
505 tmp = coerce_ref (tmp);
506 type = check_typedef (value_type (tmp));
507 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
508 && TYPE_CODE (type) != TYPE_CODE_PTR)
e10abd8f 509 error (_("Cannot subscript requested type."));
2e4d963f
PM
510 else
511 res_val = value_subscript (tmp, value_as_long (idx));
512 }
08c637de 513 }
888fe1e1
TT
514
515 if (res_val)
516 result = value_to_value_object (res_val);
517 do_cleanups (cleanup);
7843261b 518 }
570e2b1a 519
06878dd2 520 xfree (field);
7843261b
TJB
521 GDB_PY_HANDLE_EXCEPTION (except);
522
888fe1e1 523 return result;
7843261b
TJB
524}
525
526static int
527valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
528{
529 PyErr_Format (PyExc_NotImplementedError,
530 _("Setting of struct elements is not currently supported."));
531 return -1;
532}
533
5374244e 534/* Called by the Python interpreter to perform an inferior function
8dc78533 535 call on the value. Returns NULL on error, with a python exception set. */
5374244e
PM
536static PyObject *
537valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
538{
5374244e
PM
539 Py_ssize_t args_count;
540 volatile struct gdb_exception except;
541 struct value *function = ((value_object *) self)->value;
542 struct value **vargs = NULL;
749fd4ea 543 struct type *ftype = NULL;
888fe1e1
TT
544 struct value *mark = value_mark ();
545 PyObject *result = NULL;
f287c1f3
PM
546
547 TRY_CATCH (except, RETURN_MASK_ALL)
548 {
549 ftype = check_typedef (value_type (function));
550 }
551 GDB_PY_HANDLE_EXCEPTION (except);
5374244e
PM
552
553 if (TYPE_CODE (ftype) != TYPE_CODE_FUNC)
554 {
555 PyErr_SetString (PyExc_RuntimeError,
556 _("Value is not callable (not TYPE_CODE_FUNC)."));
557 return NULL;
558 }
559
f287c1f3
PM
560 if (! PyTuple_Check (args))
561 {
562 PyErr_SetString (PyExc_TypeError,
563 _("Inferior arguments must be provided in a tuple."));
564 return NULL;
565 }
566
5374244e
PM
567 args_count = PyTuple_Size (args);
568 if (args_count > 0)
569 {
570 int i;
571
572 vargs = alloca (sizeof (struct value *) * args_count);
573 for (i = 0; i < args_count; i++)
574 {
575 PyObject *item = PyTuple_GetItem (args, i);
576
577 if (item == NULL)
578 return NULL;
579
580 vargs[i] = convert_value_from_python (item);
581 if (vargs[i] == NULL)
582 return NULL;
583 }
584 }
585
586 TRY_CATCH (except, RETURN_MASK_ALL)
587 {
888fe1e1
TT
588 struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark);
589 struct value *return_value;
590
5374244e 591 return_value = call_function_by_hand (function, args_count, vargs);
888fe1e1
TT
592 result = value_to_value_object (return_value);
593 do_cleanups (cleanup);
5374244e
PM
594 }
595 GDB_PY_HANDLE_EXCEPTION (except);
596
888fe1e1 597 return result;
5374244e
PM
598}
599
7843261b
TJB
600/* Called by the Python interpreter to obtain string representation
601 of the object. */
602static PyObject *
603valpy_str (PyObject *self)
604{
605 char *s = NULL;
7843261b 606 PyObject *result;
79a45b7d 607 struct value_print_options opts;
7843261b
TJB
608 volatile struct gdb_exception except;
609
79a45b7d
TT
610 get_user_print_options (&opts);
611 opts.deref_ref = 0;
612
7843261b
TJB
613 TRY_CATCH (except, RETURN_MASK_ALL)
614 {
5fe41fbf
TT
615 struct ui_file *stb = mem_fileopen ();
616 struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
617
79a45b7d 618 common_val_print (((value_object *) self)->value, stb, 0,
d452c4bc 619 &opts, python_language);
759ef836 620 s = ui_file_xstrdup (stb, NULL);
5fe41fbf
TT
621
622 do_cleanups (old_chain);
7843261b
TJB
623 }
624 GDB_PY_HANDLE_EXCEPTION (except);
625
7843261b
TJB
626 result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
627 xfree (s);
628
629 return result;
630}
631
def2b000
TJB
632/* Implements gdb.Value.is_optimized_out. */
633static PyObject *
634valpy_get_is_optimized_out (PyObject *self, void *closure)
635{
636 struct value *value = ((value_object *) self)->value;
f287c1f3
PM
637 int opt = 0;
638 volatile struct gdb_exception except;
def2b000 639
f287c1f3
PM
640 TRY_CATCH (except, RETURN_MASK_ALL)
641 {
642 opt = value_optimized_out (value);
643 }
644 GDB_PY_HANDLE_EXCEPTION (except);
645
646 if (opt)
def2b000
TJB
647 Py_RETURN_TRUE;
648
649 Py_RETURN_FALSE;
650}
651
913460fc
PK
652/* Implements gdb.Value.is_lazy. */
653static PyObject *
654valpy_get_is_lazy (PyObject *self, void *closure)
655{
656 struct value *value = ((value_object *) self)->value;
657 int opt = 0;
658 volatile struct gdb_exception except;
659
660 TRY_CATCH (except, RETURN_MASK_ALL)
661 {
662 opt = value_lazy (value);
663 }
664 GDB_PY_HANDLE_EXCEPTION (except);
665
666 if (opt)
667 Py_RETURN_TRUE;
668
669 Py_RETURN_FALSE;
670}
671
672/* Implements gdb.Value.fetch_lazy (). */
673static PyObject *
674valpy_fetch_lazy (PyObject *self, PyObject *args)
675{
676 struct value *value = ((value_object *) self)->value;
677 volatile struct gdb_exception except;
678
679 TRY_CATCH (except, RETURN_MASK_ALL)
680 {
681 if (value_lazy (value))
682 value_fetch_lazy (value);
683 }
684 GDB_PY_HANDLE_EXCEPTION (except);
685
686 Py_RETURN_NONE;
687}
688
88d4aea7
PM
689/* Calculate and return the address of the PyObject as the value of
690 the builtin __hash__ call. */
691static long
692valpy_hash (PyObject *self)
693{
694 return (long) (intptr_t) self;
695}
696
7843261b
TJB
697enum valpy_opcode
698{
699 VALPY_ADD,
700 VALPY_SUB,
701 VALPY_MUL,
702 VALPY_DIV,
703 VALPY_REM,
08c637de
TJB
704 VALPY_POW,
705 VALPY_LSH,
706 VALPY_RSH,
707 VALPY_BITAND,
708 VALPY_BITOR,
709 VALPY_BITXOR
7843261b
TJB
710};
711
712/* If TYPE is a reference, return the target; otherwise return TYPE. */
713#define STRIP_REFERENCE(TYPE) \
714 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
715
716/* Returns a value object which is the result of applying the operation
8dc78533
JK
717 specified by OPCODE to the given arguments. Returns NULL on error, with
718 a python exception set. */
7843261b
TJB
719static PyObject *
720valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
721{
7843261b 722 volatile struct gdb_exception except;
888fe1e1 723 PyObject *result = NULL;
7843261b
TJB
724
725 TRY_CATCH (except, RETURN_MASK_ALL)
726 {
727 struct value *arg1, *arg2;
888fe1e1 728 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
0f5b7562 729 struct value *res_val = NULL;
7843261b
TJB
730
731 /* If the gdb.Value object is the second operand, then it will be passed
732 to us as the OTHER argument, and SELF will be an entirely different
733 kind of object, altogether. Because of this, we can't assume self is
734 a gdb.Value object and need to convert it from python as well. */
735 arg1 = convert_value_from_python (self);
08c637de 736 if (arg1 == NULL)
cc924cad 737 break;
08c637de 738
7843261b 739 arg2 = convert_value_from_python (other);
08c637de 740 if (arg2 == NULL)
cc924cad 741 break;
7843261b
TJB
742
743 switch (opcode)
744 {
745 case VALPY_ADD:
746 {
747 struct type *ltype = value_type (arg1);
748 struct type *rtype = value_type (arg2);
749
750 CHECK_TYPEDEF (ltype);
751 ltype = STRIP_REFERENCE (ltype);
752 CHECK_TYPEDEF (rtype);
753 rtype = STRIP_REFERENCE (rtype);
754
2497b498
UW
755 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
756 && is_integral_type (rtype))
757 res_val = value_ptradd (arg1, value_as_long (arg2));
758 else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
759 && is_integral_type (ltype))
760 res_val = value_ptradd (arg2, value_as_long (arg1));
7843261b
TJB
761 else
762 res_val = value_binop (arg1, arg2, BINOP_ADD);
763 }
764 break;
765 case VALPY_SUB:
766 {
767 struct type *ltype = value_type (arg1);
768 struct type *rtype = value_type (arg2);
769
770 CHECK_TYPEDEF (ltype);
771 ltype = STRIP_REFERENCE (ltype);
772 CHECK_TYPEDEF (rtype);
773 rtype = STRIP_REFERENCE (rtype);
774
2497b498
UW
775 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
776 && TYPE_CODE (rtype) == TYPE_CODE_PTR)
777 /* A ptrdiff_t for the target would be preferable here. */
778 res_val = value_from_longest (builtin_type_pyint,
779 value_ptrdiff (arg1, arg2));
780 else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
781 && is_integral_type (rtype))
782 res_val = value_ptradd (arg1, - value_as_long (arg2));
7843261b
TJB
783 else
784 res_val = value_binop (arg1, arg2, BINOP_SUB);
785 }
786 break;
787 case VALPY_MUL:
788 res_val = value_binop (arg1, arg2, BINOP_MUL);
789 break;
790 case VALPY_DIV:
791 res_val = value_binop (arg1, arg2, BINOP_DIV);
792 break;
793 case VALPY_REM:
794 res_val = value_binop (arg1, arg2, BINOP_REM);
795 break;
796 case VALPY_POW:
797 res_val = value_binop (arg1, arg2, BINOP_EXP);
798 break;
08c637de
TJB
799 case VALPY_LSH:
800 res_val = value_binop (arg1, arg2, BINOP_LSH);
801 break;
802 case VALPY_RSH:
803 res_val = value_binop (arg1, arg2, BINOP_RSH);
804 break;
805 case VALPY_BITAND:
806 res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
807 break;
808 case VALPY_BITOR:
809 res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
810 break;
811 case VALPY_BITXOR:
812 res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
813 break;
7843261b 814 }
888fe1e1
TT
815
816 if (res_val)
817 result = value_to_value_object (res_val);
818
819 do_cleanups (cleanup);
7843261b
TJB
820 }
821 GDB_PY_HANDLE_EXCEPTION (except);
822
888fe1e1 823 return result;
7843261b
TJB
824}
825
826static PyObject *
827valpy_add (PyObject *self, PyObject *other)
828{
829 return valpy_binop (VALPY_ADD, self, other);
830}
831
832static PyObject *
833valpy_subtract (PyObject *self, PyObject *other)
834{
835 return valpy_binop (VALPY_SUB, self, other);
836}
837
838static PyObject *
839valpy_multiply (PyObject *self, PyObject *other)
840{
841 return valpy_binop (VALPY_MUL, self, other);
842}
843
844static PyObject *
845valpy_divide (PyObject *self, PyObject *other)
846{
847 return valpy_binop (VALPY_DIV, self, other);
848}
849
850static PyObject *
851valpy_remainder (PyObject *self, PyObject *other)
852{
853 return valpy_binop (VALPY_REM, self, other);
854}
855
856static PyObject *
857valpy_power (PyObject *self, PyObject *other, PyObject *unused)
858{
859 /* We don't support the ternary form of pow. I don't know how to express
860 that, so let's just throw NotImplementedError to at least do something
861 about it. */
862 if (unused != Py_None)
863 {
864 PyErr_SetString (PyExc_NotImplementedError,
865 "Invalid operation on gdb.Value.");
866 return NULL;
867 }
868
869 return valpy_binop (VALPY_POW, self, other);
870}
871
872static PyObject *
873valpy_negative (PyObject *self)
874{
7843261b 875 volatile struct gdb_exception except;
888fe1e1 876 PyObject *result = NULL;
7843261b
TJB
877
878 TRY_CATCH (except, RETURN_MASK_ALL)
879 {
888fe1e1
TT
880 /* Perhaps overkill, but consistency has some virtue. */
881 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
882 struct value *val;
883
7843261b 884 val = value_neg (((value_object *) self)->value);
888fe1e1
TT
885 result = value_to_value_object (val);
886 do_cleanups (cleanup);
7843261b
TJB
887 }
888 GDB_PY_HANDLE_EXCEPTION (except);
889
888fe1e1 890 return result;
7843261b
TJB
891}
892
893static PyObject *
894valpy_positive (PyObject *self)
895{
4e7a5ef5 896 return value_to_value_object (((value_object *) self)->value);
7843261b
TJB
897}
898
899static PyObject *
900valpy_absolute (PyObject *self)
901{
22601c15 902 struct value *value = ((value_object *) self)->value;
f287c1f3
PM
903 volatile struct gdb_exception except;
904 int isabs = 1;
d59b6f6c 905
f287c1f3
PM
906 TRY_CATCH (except, RETURN_MASK_ALL)
907 {
888fe1e1
TT
908 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
909
f287c1f3
PM
910 if (value_less (value, value_zero (value_type (value), not_lval)))
911 isabs = 0;
888fe1e1
TT
912
913 do_cleanups (cleanup);
f287c1f3
PM
914 }
915 GDB_PY_HANDLE_EXCEPTION (except);
916
917 if (isabs)
7843261b 918 return valpy_positive (self);
f287c1f3
PM
919 else
920 return valpy_negative (self);
7843261b
TJB
921}
922
923/* Implements boolean evaluation of gdb.Value. */
924static int
925valpy_nonzero (PyObject *self)
926{
f287c1f3 927 volatile struct gdb_exception except;
7843261b
TJB
928 value_object *self_value = (value_object *) self;
929 struct type *type;
f287c1f3 930 int nonzero = 0; /* Appease GCC warning. */
7843261b 931
f287c1f3
PM
932 TRY_CATCH (except, RETURN_MASK_ALL)
933 {
5d9c5995
PM
934 type = check_typedef (value_type (self_value->value));
935
f287c1f3
PM
936 if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
937 nonzero = !!value_as_long (self_value->value);
938 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
939 nonzero = value_as_double (self_value->value) != 0;
940 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
941 nonzero = !decimal_is_zero (value_contents (self_value->value),
942 TYPE_LENGTH (type),
943 gdbarch_byte_order (get_type_arch (type)));
944 else
945 /* All other values are True. */
946 nonzero = 1;
947 }
948 /* This is not documented in the Python documentation, but if this
949 function fails, return -1 as slot_nb_nonzero does (the default
950 Python nonzero function). */
951 GDB_PY_SET_HANDLE_EXCEPTION (except);
952
953 return nonzero;
7843261b
TJB
954}
955
08c637de 956/* Implements ~ for value objects. */
7843261b 957static PyObject *
08c637de 958valpy_invert (PyObject *self)
7843261b 959{
08c637de 960 struct value *val = NULL;
7843261b
TJB
961 volatile struct gdb_exception except;
962
08c637de 963 TRY_CATCH (except, RETURN_MASK_ALL)
7843261b 964 {
08c637de
TJB
965 val = value_complement (((value_object *) self)->value);
966 }
967 GDB_PY_HANDLE_EXCEPTION (except);
7843261b 968
08c637de
TJB
969 return value_to_value_object (val);
970}
7843261b 971
08c637de
TJB
972/* Implements left shift for value objects. */
973static PyObject *
974valpy_lsh (PyObject *self, PyObject *other)
975{
976 return valpy_binop (VALPY_LSH, self, other);
977}
7843261b 978
08c637de
TJB
979/* Implements right shift for value objects. */
980static PyObject *
981valpy_rsh (PyObject *self, PyObject *other)
982{
983 return valpy_binop (VALPY_RSH, self, other);
984}
7843261b 985
08c637de
TJB
986/* Implements bitwise and for value objects. */
987static PyObject *
988valpy_and (PyObject *self, PyObject *other)
989{
990 return valpy_binop (VALPY_BITAND, self, other);
991}
7843261b 992
08c637de
TJB
993/* Implements bitwise or for value objects. */
994static PyObject *
995valpy_or (PyObject *self, PyObject *other)
996{
997 return valpy_binop (VALPY_BITOR, self, other);
998}
999
1000/* Implements bitwise xor for value objects. */
1001static PyObject *
1002valpy_xor (PyObject *self, PyObject *other)
1003{
1004 return valpy_binop (VALPY_BITXOR, self, other);
1005}
1006
8dc78533
JK
1007/* Implements comparison operations for value objects. Returns NULL on error,
1008 with a python exception set. */
08c637de
TJB
1009static PyObject *
1010valpy_richcompare (PyObject *self, PyObject *other, int op)
1011{
1012 int result = 0;
08c637de
TJB
1013 volatile struct gdb_exception except;
1014
1015 if (other == Py_None)
7843261b
TJB
1016 /* Comparing with None is special. From what I can tell, in Python
1017 None is smaller than anything else. */
1018 switch (op) {
1019 case Py_LT:
1020 case Py_LE:
1021 case Py_EQ:
1022 Py_RETURN_FALSE;
1023 case Py_NE:
1024 case Py_GT:
1025 case Py_GE:
1026 Py_RETURN_TRUE;
1027 default:
1028 /* Can't happen. */
1029 PyErr_SetString (PyExc_NotImplementedError,
044c0f87 1030 _("Invalid operation on gdb.Value."));
7843261b
TJB
1031 return NULL;
1032 }
7843261b
TJB
1033
1034 TRY_CATCH (except, RETURN_MASK_ALL)
1035 {
888fe1e1
TT
1036 struct value *value_other, *mark = value_mark ();
1037 struct cleanup *cleanup;
1038
08c637de
TJB
1039 value_other = convert_value_from_python (other);
1040 if (value_other == NULL)
f02779d8
TT
1041 {
1042 result = -1;
1043 break;
1044 }
08c637de 1045
888fe1e1
TT
1046 cleanup = make_cleanup_value_free_to_mark (mark);
1047
7843261b
TJB
1048 switch (op) {
1049 case Py_LT:
1050 result = value_less (((value_object *) self)->value, value_other);
1051 break;
1052 case Py_LE:
1053 result = value_less (((value_object *) self)->value, value_other)
1054 || value_equal (((value_object *) self)->value, value_other);
1055 break;
1056 case Py_EQ:
1057 result = value_equal (((value_object *) self)->value, value_other);
1058 break;
1059 case Py_NE:
1060 result = !value_equal (((value_object *) self)->value, value_other);
1061 break;
1062 case Py_GT:
1063 result = value_less (value_other, ((value_object *) self)->value);
1064 break;
1065 case Py_GE:
1066 result = value_less (value_other, ((value_object *) self)->value)
1067 || value_equal (((value_object *) self)->value, value_other);
1068 break;
1069 default:
1070 /* Can't happen. */
1071 PyErr_SetString (PyExc_NotImplementedError,
044c0f87 1072 _("Invalid operation on gdb.Value."));
f02779d8
TT
1073 result = -1;
1074 break;
7843261b 1075 }
888fe1e1
TT
1076
1077 do_cleanups (cleanup);
7843261b
TJB
1078 }
1079 GDB_PY_HANDLE_EXCEPTION (except);
1080
f02779d8
TT
1081 /* In this case, the Python exception has already been set. */
1082 if (result < 0)
1083 return NULL;
1084
7843261b
TJB
1085 if (result == 1)
1086 Py_RETURN_TRUE;
1087
1088 Py_RETURN_FALSE;
1089}
1090
08c637de
TJB
1091/* Helper function to determine if a type is "int-like". */
1092static int
1093is_intlike (struct type *type, int ptr_ok)
1094{
08c637de
TJB
1095 return (TYPE_CODE (type) == TYPE_CODE_INT
1096 || TYPE_CODE (type) == TYPE_CODE_ENUM
1097 || TYPE_CODE (type) == TYPE_CODE_BOOL
1098 || TYPE_CODE (type) == TYPE_CODE_CHAR
1099 || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
1100}
1101
1102/* Implements conversion to int. */
1103static PyObject *
1104valpy_int (PyObject *self)
1105{
1106 struct value *value = ((value_object *) self)->value;
1107 struct type *type = value_type (value);
1108 LONGEST l = 0;
1109 volatile struct gdb_exception except;
1110
08c637de
TJB
1111 TRY_CATCH (except, RETURN_MASK_ALL)
1112 {
f287c1f3
PM
1113 CHECK_TYPEDEF (type);
1114 if (!is_intlike (type, 0))
1115 error (_("Cannot convert value to int."));
1116
08c637de
TJB
1117 l = value_as_long (value);
1118 }
1119 GDB_PY_HANDLE_EXCEPTION (except);
1120
74aedc46 1121 return gdb_py_object_from_longest (l);
08c637de
TJB
1122}
1123
1124/* Implements conversion to long. */
1125static PyObject *
1126valpy_long (PyObject *self)
1127{
1128 struct value *value = ((value_object *) self)->value;
1129 struct type *type = value_type (value);
1130 LONGEST l = 0;
1131 volatile struct gdb_exception except;
1132
08c637de
TJB
1133 TRY_CATCH (except, RETURN_MASK_ALL)
1134 {
f287c1f3
PM
1135 CHECK_TYPEDEF (type);
1136
1137 if (!is_intlike (type, 1))
1138 error (_("Cannot convert value to long."));
1139
08c637de
TJB
1140 l = value_as_long (value);
1141 }
1142 GDB_PY_HANDLE_EXCEPTION (except);
1143
74aedc46 1144 return gdb_py_long_from_longest (l);
08c637de
TJB
1145}
1146
1147/* Implements conversion to float. */
1148static PyObject *
1149valpy_float (PyObject *self)
1150{
1151 struct value *value = ((value_object *) self)->value;
1152 struct type *type = value_type (value);
1153 double d = 0;
1154 volatile struct gdb_exception except;
1155
08c637de
TJB
1156 TRY_CATCH (except, RETURN_MASK_ALL)
1157 {
f287c1f3
PM
1158 CHECK_TYPEDEF (type);
1159
1160 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1161 error (_("Cannot convert value to float."));
1162
08c637de
TJB
1163 d = value_as_double (value);
1164 }
1165 GDB_PY_HANDLE_EXCEPTION (except);
1166
1167 return PyFloat_FromDouble (d);
1168}
1169
7843261b
TJB
1170/* Returns an object for a value which is released from the all_values chain,
1171 so its lifetime is not bound to the execution of a command. */
1172PyObject *
1173value_to_value_object (struct value *val)
1174{
1175 value_object *val_obj;
1176
1177 val_obj = PyObject_New (value_object, &value_object_type);
1178 if (val_obj != NULL)
1179 {
1180 val_obj->value = val;
e848a8a5 1181 release_value_or_incref (val);
c0c6f777 1182 val_obj->address = NULL;
2c74e833 1183 val_obj->type = NULL;
03f17ccf 1184 val_obj->dynamic_type = NULL;
4e7a5ef5 1185 note_value (val_obj);
7843261b
TJB
1186 }
1187
1188 return (PyObject *) val_obj;
1189}
1190
4e7a5ef5
TT
1191/* Returns a borrowed reference to the struct value corresponding to
1192 the given value object. */
a6bac58e
TT
1193struct value *
1194value_object_to_value (PyObject *self)
1195{
1196 value_object *real;
d59b6f6c 1197
a6bac58e
TT
1198 if (! PyObject_TypeCheck (self, &value_object_type))
1199 return NULL;
1200 real = (value_object *) self;
1201 return real->value;
1202}
1203
7843261b 1204/* Try to convert a Python value to a gdb value. If the value cannot
4e7a5ef5
TT
1205 be converted, set a Python exception and return NULL. Returns a
1206 reference to a new value on the all_values chain. */
7843261b
TJB
1207
1208struct value *
1209convert_value_from_python (PyObject *obj)
1210{
1211 struct value *value = NULL; /* -Wall */
7843261b 1212 struct cleanup *old;
08c637de
TJB
1213 volatile struct gdb_exception except;
1214 int cmp;
7843261b 1215
08c637de 1216 gdb_assert (obj != NULL);
7843261b 1217
08c637de 1218 TRY_CATCH (except, RETURN_MASK_ALL)
7843261b 1219 {
08c637de
TJB
1220 if (PyBool_Check (obj))
1221 {
1222 cmp = PyObject_IsTrue (obj);
1223 if (cmp >= 0)
1224 value = value_from_longest (builtin_type_pybool, cmp);
1225 }
1226 else if (PyInt_Check (obj))
1227 {
1228 long l = PyInt_AsLong (obj);
7843261b 1229
08c637de
TJB
1230 if (! PyErr_Occurred ())
1231 value = value_from_longest (builtin_type_pyint, l);
1232 }
1233 else if (PyLong_Check (obj))
1234 {
1235 LONGEST l = PyLong_AsLongLong (obj);
7843261b 1236
595939de
PM
1237 if (PyErr_Occurred ())
1238 {
1239 /* If the error was an overflow, we can try converting to
1240 ULONGEST instead. */
1241 if (PyErr_ExceptionMatches (PyExc_OverflowError))
1242 {
1243 PyObject *etype, *evalue, *etraceback, *zero;
1244
1245 PyErr_Fetch (&etype, &evalue, &etraceback);
1246 zero = PyInt_FromLong (0);
1247
1248 /* Check whether obj is positive. */
1249 if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0)
1250 {
1251 ULONGEST ul;
1252
1253 ul = PyLong_AsUnsignedLongLong (obj);
1254 if (! PyErr_Occurred ())
1255 value = value_from_ulongest (builtin_type_upylong, ul);
1256 }
1257 else
1258 /* There's nothing we can do. */
1259 PyErr_Restore (etype, evalue, etraceback);
1260
1261 Py_DECREF (zero);
1262 }
1263 }
1264 else
08c637de
TJB
1265 value = value_from_longest (builtin_type_pylong, l);
1266 }
1267 else if (PyFloat_Check (obj))
1268 {
1269 double d = PyFloat_AsDouble (obj);
7843261b 1270
08c637de
TJB
1271 if (! PyErr_Occurred ())
1272 value = value_from_double (builtin_type_pyfloat, d);
1273 }
1274 else if (gdbpy_is_string (obj))
1275 {
1276 char *s;
1277
1278 s = python_string_to_target_string (obj);
1279 if (s != NULL)
1280 {
1281 old = make_cleanup (xfree, s);
3b7538c0 1282 value = value_cstring (s, strlen (s), builtin_type_pychar);
08c637de
TJB
1283 do_cleanups (old);
1284 }
1285 }
1286 else if (PyObject_TypeCheck (obj, &value_object_type))
cc924cad 1287 value = value_copy (((value_object *) obj)->value);
be759fcf
PM
1288 else if (gdbpy_is_lazy_string (obj))
1289 {
1290 PyObject *result;
d59b6f6c 1291
fb6a3ed3 1292 result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst, NULL);
be759fcf
PM
1293 value = value_copy (((value_object *) result)->value);
1294 }
08c637de 1295 else
9a2b4c1b
MS
1296 PyErr_Format (PyExc_TypeError,
1297 _("Could not convert Python object: %s."),
08c637de
TJB
1298 PyString_AsString (PyObject_Str (obj)));
1299 }
1300 if (except.reason < 0)
1301 {
1302 PyErr_Format (except.reason == RETURN_QUIT
1303 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
1304 "%s", except.message);
1305 return NULL;
1306 }
7843261b
TJB
1307
1308 return value;
1309}
1310
1311/* Returns value object in the ARGth position in GDB's history. */
1312PyObject *
08c637de 1313gdbpy_history (PyObject *self, PyObject *args)
7843261b
TJB
1314{
1315 int i;
1316 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
1317 volatile struct gdb_exception except;
1318
1319 if (!PyArg_ParseTuple (args, "i", &i))
1320 return NULL;
1321
1322 TRY_CATCH (except, RETURN_MASK_ALL)
1323 {
1324 res_val = access_value_history (i);
1325 }
1326 GDB_PY_HANDLE_EXCEPTION (except);
1327
1328 return value_to_value_object (res_val);
1329}
1330
595939de
PM
1331/* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1332
1333int
1334gdbpy_is_value_object (PyObject *obj)
1335{
1336 return PyObject_TypeCheck (obj, &value_object_type);
1337}
1338
7843261b
TJB
1339void
1340gdbpy_initialize_values (void)
1341{
7843261b
TJB
1342 if (PyType_Ready (&value_object_type) < 0)
1343 return;
1344
1345 Py_INCREF (&value_object_type);
1346 PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type);
1347
1348 values_in_python = NULL;
1349}
1350
2c74e833
TT
1351\f
1352
def2b000 1353static PyGetSetDef value_object_getset[] = {
c0c6f777
TJB
1354 { "address", valpy_get_address, NULL, "The address of the value.",
1355 NULL },
def2b000 1356 { "is_optimized_out", valpy_get_is_optimized_out, NULL,
9a2b4c1b
MS
1357 "Boolean telling whether the value is optimized "
1358 "out (i.e., not available).",
def2b000 1359 NULL },
2c74e833 1360 { "type", valpy_get_type, NULL, "Type of the value.", NULL },
03f17ccf
TT
1361 { "dynamic_type", valpy_get_dynamic_type, NULL,
1362 "Dynamic type of the value.", NULL },
913460fc
PK
1363 { "is_lazy", valpy_get_is_lazy, NULL,
1364 "Boolean telling whether the value is lazy (not fetched yet\n\
1365from the inferior). A lazy value is fetched when needed, or when\n\
1366the \"fetch_lazy()\" method is called.", NULL },
def2b000
TJB
1367 {NULL} /* Sentinel */
1368};
1369
f9176c46 1370static PyMethodDef value_object_methods[] = {
2c74e833 1371 { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
f9ffd4bb
TT
1372 { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
1373 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1374Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1375 },
1376 { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
1377 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1378Cast the value to the supplied type, as if by the C++\n\
1379reinterpret_cast operator."
1380 },
f9176c46 1381 { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
9a2b4c1b
MS
1382 { "lazy_string", (PyCFunction) valpy_lazy_string,
1383 METH_VARARGS | METH_KEYWORDS,
be759fcf
PM
1384 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1385Return a lazy string representation of the value." },
cc924cad 1386 { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
fbb8f299 1387 "string ([encoding] [, errors] [, length]) -> string\n\
cc924cad 1388Return Unicode string representation of the value." },
913460fc
PK
1389 { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
1390 "Fetches the value from the inferior, if it was lazy." },
f9176c46
PA
1391 {NULL} /* Sentinel */
1392};
1393
1394static PyNumberMethods value_object_as_number = {
1395 valpy_add,
1396 valpy_subtract,
1397 valpy_multiply,
1398 valpy_divide,
1399 valpy_remainder,
1400 NULL, /* nb_divmod */
1401 valpy_power, /* nb_power */
1402 valpy_negative, /* nb_negative */
1403 valpy_positive, /* nb_positive */
1404 valpy_absolute, /* nb_absolute */
08c637de
TJB
1405 valpy_nonzero, /* nb_nonzero */
1406 valpy_invert, /* nb_invert */
1407 valpy_lsh, /* nb_lshift */
1408 valpy_rsh, /* nb_rshift */
1409 valpy_and, /* nb_and */
1410 valpy_xor, /* nb_xor */
1411 valpy_or, /* nb_or */
1412 NULL, /* nb_coerce */
1413 valpy_int, /* nb_int */
1414 valpy_long, /* nb_long */
1415 valpy_float, /* nb_float */
1416 NULL, /* nb_oct */
1417 NULL /* nb_hex */
f9176c46
PA
1418};
1419
1420static PyMappingMethods value_object_as_mapping = {
1421 valpy_length,
1422 valpy_getitem,
1423 valpy_setitem
1424};
1425
1426PyTypeObject value_object_type = {
1427 PyObject_HEAD_INIT (NULL)
1428 0, /*ob_size*/
1429 "gdb.Value", /*tp_name*/
1430 sizeof (value_object), /*tp_basicsize*/
1431 0, /*tp_itemsize*/
1432 valpy_dealloc, /*tp_dealloc*/
1433 0, /*tp_print*/
1434 0, /*tp_getattr*/
1435 0, /*tp_setattr*/
1436 0, /*tp_compare*/
1437 0, /*tp_repr*/
1438 &value_object_as_number, /*tp_as_number*/
1439 0, /*tp_as_sequence*/
1440 &value_object_as_mapping, /*tp_as_mapping*/
88d4aea7 1441 valpy_hash, /*tp_hash*/
5374244e 1442 valpy_call, /*tp_call*/
f9176c46
PA
1443 valpy_str, /*tp_str*/
1444 0, /*tp_getattro*/
1445 0, /*tp_setattro*/
1446 0, /*tp_as_buffer*/
9a2b4c1b
MS
1447 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
1448 | Py_TPFLAGS_BASETYPE, /*tp_flags*/
f9176c46
PA
1449 "GDB value object", /* tp_doc */
1450 0, /* tp_traverse */
1451 0, /* tp_clear */
1452 valpy_richcompare, /* tp_richcompare */
1453 0, /* tp_weaklistoffset */
1454 0, /* tp_iter */
1455 0, /* tp_iternext */
08c637de
TJB
1456 value_object_methods, /* tp_methods */
1457 0, /* tp_members */
def2b000 1458 value_object_getset, /* tp_getset */
08c637de
TJB
1459 0, /* tp_base */
1460 0, /* tp_dict */
1461 0, /* tp_descr_get */
1462 0, /* tp_descr_set */
1463 0, /* tp_dictoffset */
1464 0, /* tp_init */
1465 0, /* tp_alloc */
1466 valpy_new /* tp_new */
f9176c46
PA
1467};
1468
4e7a5ef5
TT
1469#else
1470
1471void
1472preserve_python_values (struct objfile *objfile, htab_t copied_types)
1473{
1474 /* Nothing. */
1475}
1476
7843261b 1477#endif /* HAVE_PYTHON */
This page took 0.419378 seconds and 4 git commands to generate.