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