Use unique_xmalloc_ptr in Python code
[deliverable/binutils-gdb.git] / gdb / python / py-value.c
CommitLineData
7843261b
TJB
1/* Python interface to values.
2
618f726f 3 Copyright (C) 2008-2016 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"
21#include "charset.h"
22#include "value.h"
7843261b
TJB
23#include "language.h"
24#include "dfp.h"
79a45b7d 25#include "valprint.h"
5374244e 26#include "infcall.h"
f9ffd4bb 27#include "expression.h"
03f17ccf 28#include "cp-abi.h"
49a8461d 29#include "python.h"
7843261b 30
7843261b
TJB
31#include "python-internal.h"
32
33/* Even though Python scalar types directly map to host types, we use
b021a221 34 target types here to remain consistent with the values system in
7843261b
TJB
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 45
595939de
PM
46/* Python's long type corresponds to C's long long type. Unsigned version. */
47#define builtin_type_upylong builtin_type \
48 (python_gdbarch)->builtin_unsigned_long_long
49
7843261b 50#define builtin_type_pybool \
d452c4bc 51 language_bool_type (python_language, python_gdbarch)
7843261b 52
3b7538c0 53#define builtin_type_pychar \
d452c4bc 54 language_string_char_type (python_language, python_gdbarch)
3b7538c0 55
4e7a5ef5 56typedef struct value_object {
7843261b 57 PyObject_HEAD
4e7a5ef5
TT
58 struct value_object *next;
59 struct value_object *prev;
7843261b 60 struct value *value;
c0c6f777 61 PyObject *address;
2c74e833 62 PyObject *type;
03f17ccf 63 PyObject *dynamic_type;
7843261b
TJB
64} value_object;
65
4e7a5ef5
TT
66/* List of all values which are currently exposed to Python. It is
67 maintained so that when an objfile is discarded, preserve_values
68 can copy the values' types if needed. */
69/* This variable is unnecessarily initialized to NULL in order to
70 work around a linker bug on MacOS. */
71static value_object *values_in_python = NULL;
72
7843261b
TJB
73/* Called by the Python interpreter when deallocating a value object. */
74static void
75valpy_dealloc (PyObject *obj)
76{
77 value_object *self = (value_object *) obj;
78
4e7a5ef5
TT
79 /* Remove SELF from the global list. */
80 if (self->prev)
81 self->prev->next = self->next;
82 else
83 {
84 gdb_assert (values_in_python == self);
85 values_in_python = self->next;
86 }
87 if (self->next)
88 self->next->prev = self->prev;
7843261b 89
77316f4c 90 value_free (self->value);
c0c6f777
TJB
91
92 if (self->address)
93 /* Use braces to appease gcc warning. *sigh* */
94 {
95 Py_DECREF (self->address);
96 }
97
2c74e833
TT
98 if (self->type)
99 {
100 Py_DECREF (self->type);
101 }
102
03f17ccf
TT
103 Py_XDECREF (self->dynamic_type);
104
9a27f2c6 105 Py_TYPE (self)->tp_free (self);
7843261b
TJB
106}
107
4e7a5ef5
TT
108/* Helper to push a Value object on the global list. */
109static void
110note_value (value_object *value_obj)
111{
112 value_obj->next = values_in_python;
113 if (value_obj->next)
114 value_obj->next->prev = value_obj;
115 value_obj->prev = NULL;
116 values_in_python = value_obj;
117}
118
8dc78533
JK
119/* Called when a new gdb.Value object needs to be allocated. Returns NULL on
120 error, with a python exception set. */
7843261b
TJB
121static PyObject *
122valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
123{
124 struct value *value = NULL; /* Initialize to appease gcc warning. */
125 value_object *value_obj;
7843261b
TJB
126
127 if (PyTuple_Size (args) != 1)
128 {
129 PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
130 "1 argument"));
131 return NULL;
132 }
133
134 value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
135 if (value_obj == NULL)
136 {
137 PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
138 "create Value object."));
139 return NULL;
140 }
141
08c637de
TJB
142 value = convert_value_from_python (PyTuple_GetItem (args, 0));
143 if (value == NULL)
7843261b
TJB
144 {
145 subtype->tp_free (value_obj);
08c637de 146 return NULL;
7843261b
TJB
147 }
148
149 value_obj->value = value;
e848a8a5 150 release_value_or_incref (value);
c0c6f777 151 value_obj->address = NULL;
2c74e833 152 value_obj->type = NULL;
03f17ccf 153 value_obj->dynamic_type = NULL;
4e7a5ef5 154 note_value (value_obj);
7843261b
TJB
155
156 return (PyObject *) value_obj;
157}
158
4e7a5ef5
TT
159/* Iterate over all the Value objects, calling preserve_one_value on
160 each. */
161void
6dddc817
DE
162gdbpy_preserve_values (const struct extension_language_defn *extlang,
163 struct objfile *objfile, htab_t copied_types)
4e7a5ef5
TT
164{
165 value_object *iter;
166
167 for (iter = values_in_python; iter; iter = iter->next)
168 preserve_one_value (iter->value, objfile, copied_types);
169}
170
7843261b
TJB
171/* Given a value of a pointer type, apply the C unary * operator to it. */
172static PyObject *
173valpy_dereference (PyObject *self, PyObject *args)
174{
888fe1e1 175 PyObject *result = NULL;
7843261b 176
492d29ea 177 TRY
7843261b 178 {
888fe1e1
TT
179 struct value *res_val;
180 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
181
7843261b 182 res_val = value_ind (((value_object *) self)->value);
888fe1e1
TT
183 result = value_to_value_object (res_val);
184 do_cleanups (cleanup);
7843261b 185 }
492d29ea
PA
186 CATCH (except, RETURN_MASK_ALL)
187 {
188 GDB_PY_HANDLE_EXCEPTION (except);
189 }
190 END_CATCH
7843261b 191
888fe1e1 192 return result;
7843261b
TJB
193}
194
7b282c5a
SCR
195/* Given a value of a pointer type or a reference type, return the value
196 referenced. The difference between this function and valpy_dereference is
197 that the latter applies * unary operator to a value, which need not always
198 result in the value referenced. For example, for a value which is a reference
199 to an 'int' pointer ('int *'), valpy_dereference will result in a value of
200 type 'int' while valpy_referenced_value will result in a value of type
201 'int *'. */
202
203static PyObject *
204valpy_referenced_value (PyObject *self, PyObject *args)
205{
7b282c5a
SCR
206 PyObject *result = NULL;
207
492d29ea 208 TRY
7b282c5a
SCR
209 {
210 struct value *self_val, *res_val;
211 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
212
213 self_val = ((value_object *) self)->value;
214 switch (TYPE_CODE (check_typedef (value_type (self_val))))
215 {
216 case TYPE_CODE_PTR:
217 res_val = value_ind (self_val);
218 break;
219 case TYPE_CODE_REF:
220 res_val = coerce_ref (self_val);
221 break;
222 default:
223 error(_("Trying to get the referenced value from a value which is "
224 "neither a pointer nor a reference."));
225 }
226
227 result = value_to_value_object (res_val);
228 do_cleanups (cleanup);
229 }
492d29ea
PA
230 CATCH (except, RETURN_MASK_ALL)
231 {
232 GDB_PY_HANDLE_EXCEPTION (except);
233 }
234 END_CATCH
7b282c5a
SCR
235
236 return result;
237}
238
4c082a81
SC
239/* Return a value which is a reference to the value. */
240
241static PyObject *
242valpy_reference_value (PyObject *self, PyObject *args)
243{
244 PyObject *result = NULL;
245
246 TRY
247 {
248 struct value *self_val;
249 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
250
251 self_val = ((value_object *) self)->value;
252 result = value_to_value_object (value_ref (self_val));
253
254 do_cleanups (cleanup);
255 }
256 CATCH (except, RETURN_MASK_ALL)
257 {
258 GDB_PY_HANDLE_EXCEPTION (except);
259 }
260 END_CATCH
261
262 return result;
263}
264
265/* Return a "const" qualified version of the value. */
266
267static PyObject *
268valpy_const_value (PyObject *self, PyObject *args)
269{
270 PyObject *result = NULL;
271
272 TRY
273 {
274 struct value *self_val, *res_val;
275 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
276
277 self_val = ((value_object *) self)->value;
278 res_val = make_cv_value (1, 0, self_val);
279 result = value_to_value_object (res_val);
280
281 do_cleanups (cleanup);
282 }
283 CATCH (except, RETURN_MASK_ALL)
284 {
285 GDB_PY_HANDLE_EXCEPTION (except);
286 }
287 END_CATCH
288
289 return result;
290}
291
08c637de
TJB
292/* Return "&value". */
293static PyObject *
c0c6f777 294valpy_get_address (PyObject *self, void *closure)
08c637de 295{
c0c6f777 296 value_object *val_obj = (value_object *) self;
08c637de 297
c0c6f777 298 if (!val_obj->address)
08c637de 299 {
492d29ea 300 TRY
c0c6f777 301 {
888fe1e1
TT
302 struct value *res_val;
303 struct cleanup *cleanup
304 = make_cleanup_value_free_to_mark (value_mark ());
305
c0c6f777 306 res_val = value_addr (val_obj->value);
888fe1e1
TT
307 val_obj->address = value_to_value_object (res_val);
308 do_cleanups (cleanup);
c0c6f777 309 }
492d29ea 310 CATCH (except, RETURN_MASK_ALL)
c0c6f777
TJB
311 {
312 val_obj->address = Py_None;
313 Py_INCREF (Py_None);
314 }
492d29ea 315 END_CATCH
08c637de 316 }
08c637de 317
3fcaed38 318 Py_XINCREF (val_obj->address);
c0c6f777
TJB
319
320 return val_obj->address;
08c637de
TJB
321}
322
2c74e833
TT
323/* Return type of the value. */
324static PyObject *
325valpy_get_type (PyObject *self, void *closure)
326{
327 value_object *obj = (value_object *) self;
d59b6f6c 328
2c74e833
TT
329 if (!obj->type)
330 {
331 obj->type = type_to_type_object (value_type (obj->value));
332 if (!obj->type)
03f17ccf 333 return NULL;
2c74e833
TT
334 }
335 Py_INCREF (obj->type);
336 return obj->type;
337}
338
03f17ccf
TT
339/* Return dynamic type of the value. */
340
341static PyObject *
342valpy_get_dynamic_type (PyObject *self, void *closure)
343{
344 value_object *obj = (value_object *) self;
03f17ccf
TT
345 struct type *type = NULL;
346
347 if (obj->dynamic_type != NULL)
348 {
349 Py_INCREF (obj->dynamic_type);
350 return obj->dynamic_type;
351 }
352
492d29ea 353 TRY
03f17ccf
TT
354 {
355 struct value *val = obj->value;
888fe1e1 356 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
03f17ccf
TT
357
358 type = value_type (val);
f168693b 359 type = check_typedef (type);
03f17ccf
TT
360
361 if (((TYPE_CODE (type) == TYPE_CODE_PTR)
362 || (TYPE_CODE (type) == TYPE_CODE_REF))
4753d33b 363 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT))
03f17ccf
TT
364 {
365 struct value *target;
366 int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
367
7af389b8
SC
368 if (was_pointer)
369 target = value_ind (val);
370 else
371 target = coerce_ref (val);
03f17ccf
TT
372 type = value_rtti_type (target, NULL, NULL, NULL);
373
374 if (type)
375 {
376 if (was_pointer)
377 type = lookup_pointer_type (type);
378 else
379 type = lookup_reference_type (type);
380 }
381 }
4753d33b 382 else if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
03f17ccf
TT
383 type = value_rtti_type (val, NULL, NULL, NULL);
384 else
385 {
386 /* Re-use object's static type. */
387 type = NULL;
388 }
888fe1e1
TT
389
390 do_cleanups (cleanup);
03f17ccf 391 }
492d29ea
PA
392 CATCH (except, RETURN_MASK_ALL)
393 {
394 GDB_PY_HANDLE_EXCEPTION (except);
395 }
396 END_CATCH
03f17ccf
TT
397
398 if (type == NULL)
97b77b39 399 obj->dynamic_type = valpy_get_type (self, NULL);
03f17ccf
TT
400 else
401 obj->dynamic_type = type_to_type_object (type);
402
97b77b39 403 Py_XINCREF (obj->dynamic_type);
03f17ccf
TT
404 return obj->dynamic_type;
405}
406
be759fcf
PM
407/* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
408 string. Return a PyObject representing a lazy_string_object type.
409 A lazy string is a pointer to a string with an optional encoding and
410 length. If ENCODING is not given, encoding is set to None. If an
411 ENCODING is provided the encoding parameter is set to ENCODING, but
412 the string is not encoded. If LENGTH is provided then the length
413 parameter is set to LENGTH, otherwise length will be set to -1 (first
414 null of appropriate with). */
415static PyObject *
416valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
417{
74aedc46 418 gdb_py_longest length = -1;
be759fcf
PM
419 struct value *value = ((value_object *) self)->value;
420 const char *user_encoding = NULL;
421 static char *keywords[] = { "encoding", "length", NULL };
888fe1e1 422 PyObject *str_obj = NULL;
be759fcf 423
74aedc46 424 if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords,
be759fcf
PM
425 &user_encoding, &length))
426 return NULL;
427
492d29ea 428 TRY
f287c1f3 429 {
888fe1e1
TT
430 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
431
f287c1f3
PM
432 if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
433 value = value_ind (value);
888fe1e1
TT
434
435 str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
436 user_encoding,
437 value_type (value));
438
439 do_cleanups (cleanup);
f287c1f3 440 }
492d29ea
PA
441 CATCH (except, RETURN_MASK_ALL)
442 {
443 GDB_PY_HANDLE_EXCEPTION (except);
444 }
445 END_CATCH
be759fcf 446
888fe1e1 447 return str_obj;
be759fcf
PM
448}
449
fbb8f299
PM
450/* Implementation of gdb.Value.string ([encoding] [, errors]
451 [, length]) -> string. Return Unicode string with value contents.
452 If ENCODING is not given, the string is assumed to be encoded in
453 the target's charset. If LENGTH is provided, only fetch string to
454 the length provided. */
455
b6cb8e7d 456static PyObject *
cc924cad 457valpy_string (PyObject *self, PyObject *args, PyObject *kw)
b6cb8e7d 458{
f92adf3c 459 int length = -1;
b6cb8e7d
TJB
460 gdb_byte *buffer;
461 struct value *value = ((value_object *) self)->value;
b6cb8e7d
TJB
462 PyObject *unicode;
463 const char *encoding = NULL;
464 const char *errors = NULL;
465 const char *user_encoding = NULL;
466 const char *la_encoding = NULL;
96c07c5b 467 struct type *char_type;
31158f0e 468 static char *keywords[] = { "encoding", "errors", "length", NULL };
b6cb8e7d 469
fbb8f299
PM
470 if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
471 &user_encoding, &errors, &length))
b6cb8e7d
TJB
472 return NULL;
473
492d29ea 474 TRY
b6cb8e7d 475 {
96c07c5b 476 LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
b6cb8e7d 477 }
492d29ea
PA
478 CATCH (except, RETURN_MASK_ALL)
479 {
480 GDB_PY_HANDLE_EXCEPTION (except);
481 }
482 END_CATCH
b6cb8e7d
TJB
483
484 encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
89f6d837
PA
485 unicode = PyUnicode_Decode ((const char *) buffer,
486 length * TYPE_LENGTH (char_type),
96c07c5b 487 encoding, errors);
b6cb8e7d
TJB
488 xfree (buffer);
489
490 return unicode;
491}
492
f9ffd4bb
TT
493/* A helper function that implements the various cast operators. */
494
2c74e833 495static PyObject *
f9ffd4bb 496valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
2c74e833 497{
888fe1e1 498 PyObject *type_obj, *result = NULL;
2c74e833 499 struct type *type;
2c74e833
TT
500
501 if (! PyArg_ParseTuple (args, "O", &type_obj))
502 return NULL;
503
504 type = type_object_to_type (type_obj);
505 if (! type)
506 {
256458bc 507 PyErr_SetString (PyExc_RuntimeError,
044c0f87 508 _("Argument must be a type."));
2c74e833
TT
509 return NULL;
510 }
511
492d29ea 512 TRY
2c74e833 513 {
f9ffd4bb 514 struct value *val = ((value_object *) self)->value;
888fe1e1
TT
515 struct value *res_val;
516 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
f9ffd4bb
TT
517
518 if (op == UNOP_DYNAMIC_CAST)
519 res_val = value_dynamic_cast (type, val);
520 else if (op == UNOP_REINTERPRET_CAST)
521 res_val = value_reinterpret_cast (type, val);
522 else
523 {
524 gdb_assert (op == UNOP_CAST);
525 res_val = value_cast (type, val);
526 }
888fe1e1
TT
527
528 result = value_to_value_object (res_val);
529 do_cleanups (cleanup);
2c74e833 530 }
492d29ea
PA
531 CATCH (except, RETURN_MASK_ALL)
532 {
533 GDB_PY_HANDLE_EXCEPTION (except);
534 }
535 END_CATCH
2c74e833 536
888fe1e1 537 return result;
2c74e833
TT
538}
539
f9ffd4bb
TT
540/* Implementation of the "cast" method. */
541
542static PyObject *
543valpy_cast (PyObject *self, PyObject *args)
544{
545 return valpy_do_cast (self, args, UNOP_CAST);
546}
547
548/* Implementation of the "dynamic_cast" method. */
549
550static PyObject *
551valpy_dynamic_cast (PyObject *self, PyObject *args)
552{
553 return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
554}
555
556/* Implementation of the "reinterpret_cast" method. */
557
558static PyObject *
559valpy_reinterpret_cast (PyObject *self, PyObject *args)
560{
561 return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
562}
563
7843261b
TJB
564static Py_ssize_t
565valpy_length (PyObject *self)
566{
567 /* We don't support getting the number of elements in a struct / class. */
568 PyErr_SetString (PyExc_NotImplementedError,
044c0f87 569 _("Invalid operation on gdb.Value."));
7843261b
TJB
570 return -1;
571}
572
a16b0e22
SC
573/* Return 1 if the gdb.Field object FIELD is present in the value V.
574 Returns 0 otherwise. If any Python error occurs, -1 is returned. */
575
576static int
577value_has_field (struct value *v, PyObject *field)
578{
579 struct type *parent_type, *val_type;
580 enum type_code type_code;
581 PyObject *type_object = PyObject_GetAttrString (field, "parent_type");
a16b0e22
SC
582 int has_field = 0;
583
584 if (type_object == NULL)
585 return -1;
586
587 parent_type = type_object_to_type (type_object);
588 Py_DECREF (type_object);
589 if (parent_type == NULL)
590 {
591 PyErr_SetString (PyExc_TypeError,
592 _("'parent_type' attribute of gdb.Field object is not a"
593 "gdb.Type object."));
594 return -1;
595 }
596
492d29ea 597 TRY
a16b0e22
SC
598 {
599 val_type = value_type (v);
600 val_type = check_typedef (val_type);
601 if (TYPE_CODE (val_type) == TYPE_CODE_REF
602 || TYPE_CODE (val_type) == TYPE_CODE_PTR)
603 val_type = check_typedef (TYPE_TARGET_TYPE (val_type));
604
605 type_code = TYPE_CODE (val_type);
606 if ((type_code == TYPE_CODE_STRUCT || type_code == TYPE_CODE_UNION)
607 && types_equal (val_type, parent_type))
608 has_field = 1;
609 else
610 has_field = 0;
611 }
492d29ea
PA
612 CATCH (except, RETURN_MASK_ALL)
613 {
614 GDB_PY_SET_HANDLE_EXCEPTION (except);
615 }
616 END_CATCH
a16b0e22
SC
617
618 return has_field;
619}
620
621/* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
622 Returns 1 if the flag value is true, 0 if it is false, and -1 if
623 a Python error occurs. */
624
625static int
626get_field_flag (PyObject *field, const char *flag_name)
627{
628 int flag_value;
5a6c7709 629 PyObject *flag_object = PyObject_GetAttrString (field, flag_name);
a16b0e22
SC
630
631 if (flag_object == NULL)
632 return -1;
633
634 flag_value = PyObject_IsTrue (flag_object);
635 Py_DECREF (flag_object);
636
637 return flag_value;
638}
639
b5b08fb4
SC
640/* Return the "type" attribute of a gdb.Field object.
641 Returns NULL on error, with a Python exception set. */
642
643static struct type *
644get_field_type (PyObject *field)
645{
646 PyObject *ftype_obj = PyObject_GetAttrString (field, "type");
647 struct type *ftype;
648
649 if (ftype_obj == NULL)
650 return NULL;
651 ftype = type_object_to_type (ftype_obj);
652 Py_DECREF (ftype_obj);
653 if (ftype == NULL)
bf7105a4
JB
654 PyErr_SetString (PyExc_TypeError,
655 _("'type' attribute of gdb.Field object is not a "
656 "gdb.Type object."));
b5b08fb4
SC
657
658 return ftype;
659}
660
a16b0e22
SC
661/* Given string name or a gdb.Field object corresponding to an element inside
662 a structure, return its value object. Returns NULL on error, with a python
663 exception set. */
664
7843261b
TJB
665static PyObject *
666valpy_getitem (PyObject *self, PyObject *key)
667{
492d29ea 668 struct gdb_exception except = exception_none;
7843261b 669 value_object *self_value = (value_object *) self;
9b972014 670 gdb::unique_xmalloc_ptr<char> field;
b5b08fb4
SC
671 struct type *base_class_type = NULL, *field_type = NULL;
672 long bitpos = -1;
888fe1e1 673 PyObject *result = NULL;
7843261b 674
08c637de 675 if (gdbpy_is_string (key))
256458bc 676 {
08c637de
TJB
677 field = python_string_to_host_string (key);
678 if (field == NULL)
679 return NULL;
680 }
a16b0e22
SC
681 else if (gdbpy_is_field (key))
682 {
683 int is_base_class, valid_field;
684
685 valid_field = value_has_field (self_value->value, key);
686 if (valid_field < 0)
687 return NULL;
688 else if (valid_field == 0)
689 {
690 PyErr_SetString (PyExc_TypeError,
691 _("Invalid lookup for a field not contained in "
692 "the value."));
693
694 return NULL;
695 }
696
697 is_base_class = get_field_flag (key, "is_base_class");
698 if (is_base_class < 0)
699 return NULL;
700 else if (is_base_class > 0)
701 {
b5b08fb4
SC
702 base_class_type = get_field_type (key);
703 if (base_class_type == NULL)
a16b0e22
SC
704 return NULL;
705 }
706 else
707 {
708 PyObject *name_obj = PyObject_GetAttrString (key, "name");
709
710 if (name_obj == NULL)
711 return NULL;
712
b5b08fb4
SC
713 if (name_obj != Py_None)
714 {
715 field = python_string_to_host_string (name_obj);
716 Py_DECREF (name_obj);
717 if (field == NULL)
718 return NULL;
719 }
720 else
721 {
722 PyObject *bitpos_obj;
723 int valid;
724
725 Py_DECREF (name_obj);
726
727 if (!PyObject_HasAttrString (key, "bitpos"))
728 {
729 PyErr_SetString (PyExc_AttributeError,
730 _("gdb.Field object has no name and no "
731 "'bitpos' attribute."));
732
733 return NULL;
734 }
735 bitpos_obj = PyObject_GetAttrString (key, "bitpos");
736 if (bitpos_obj == NULL)
737 return NULL;
738 valid = gdb_py_int_as_long (bitpos_obj, &bitpos);
739 Py_DECREF (bitpos_obj);
740 if (!valid)
741 return NULL;
742
743 field_type = get_field_type (key);
744 if (field_type == NULL)
745 return NULL;
746 }
a16b0e22
SC
747 }
748 }
7843261b 749
492d29ea 750 TRY
7843261b 751 {
3c0ed299 752 struct value *tmp = self_value->value;
888fe1e1
TT
753 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
754 struct value *res_val = NULL;
08c637de
TJB
755
756 if (field)
9b972014 757 res_val = value_struct_elt (&tmp, NULL, field.get (), NULL,
5996220c 758 "struct/class/union");
b5b08fb4
SC
759 else if (bitpos >= 0)
760 res_val = value_struct_elt_bitpos (&tmp, bitpos, field_type,
761 "struct/class/union");
762 else if (base_class_type != NULL)
a16b0e22 763 {
b5b08fb4 764 struct type *val_type;
a16b0e22
SC
765
766 val_type = check_typedef (value_type (tmp));
767 if (TYPE_CODE (val_type) == TYPE_CODE_PTR)
768 res_val = value_cast (lookup_pointer_type (base_class_type), tmp);
769 else if (TYPE_CODE (val_type) == TYPE_CODE_REF)
770 res_val = value_cast (lookup_reference_type (base_class_type), tmp);
771 else
772 res_val = value_cast (base_class_type, tmp);
773 }
08c637de
TJB
774 else
775 {
776 /* Assume we are attempting an array access, and let the
777 value code throw an exception if the index has an invalid
778 type. */
779 struct value *idx = convert_value_from_python (key);
d59b6f6c 780
570e2b1a 781 if (idx != NULL)
2e4d963f
PM
782 {
783 /* Check the value's type is something that can be accessed via
784 a subscript. */
785 struct type *type;
d59b6f6c 786
2e4d963f
PM
787 tmp = coerce_ref (tmp);
788 type = check_typedef (value_type (tmp));
789 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
790 && TYPE_CODE (type) != TYPE_CODE_PTR)
e10abd8f 791 error (_("Cannot subscript requested type."));
2e4d963f
PM
792 else
793 res_val = value_subscript (tmp, value_as_long (idx));
794 }
08c637de 795 }
888fe1e1
TT
796
797 if (res_val)
798 result = value_to_value_object (res_val);
799 do_cleanups (cleanup);
7843261b 800 }
492d29ea
PA
801 CATCH (ex, RETURN_MASK_ALL)
802 {
803 except = ex;
804 }
805 END_CATCH
570e2b1a 806
7843261b
TJB
807 GDB_PY_HANDLE_EXCEPTION (except);
808
888fe1e1 809 return result;
7843261b
TJB
810}
811
812static int
813valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
814{
815 PyErr_Format (PyExc_NotImplementedError,
816 _("Setting of struct elements is not currently supported."));
817 return -1;
818}
819
5374244e 820/* Called by the Python interpreter to perform an inferior function
8dc78533 821 call on the value. Returns NULL on error, with a python exception set. */
5374244e
PM
822static PyObject *
823valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
824{
5374244e 825 Py_ssize_t args_count;
5374244e
PM
826 struct value *function = ((value_object *) self)->value;
827 struct value **vargs = NULL;
749fd4ea 828 struct type *ftype = NULL;
888fe1e1
TT
829 struct value *mark = value_mark ();
830 PyObject *result = NULL;
f287c1f3 831
492d29ea 832 TRY
f287c1f3
PM
833 {
834 ftype = check_typedef (value_type (function));
835 }
492d29ea
PA
836 CATCH (except, RETURN_MASK_ALL)
837 {
838 GDB_PY_HANDLE_EXCEPTION (except);
839 }
840 END_CATCH
5374244e
PM
841
842 if (TYPE_CODE (ftype) != TYPE_CODE_FUNC)
843 {
844 PyErr_SetString (PyExc_RuntimeError,
845 _("Value is not callable (not TYPE_CODE_FUNC)."));
846 return NULL;
847 }
848
f287c1f3
PM
849 if (! PyTuple_Check (args))
850 {
851 PyErr_SetString (PyExc_TypeError,
852 _("Inferior arguments must be provided in a tuple."));
853 return NULL;
854 }
855
5374244e
PM
856 args_count = PyTuple_Size (args);
857 if (args_count > 0)
858 {
859 int i;
860
8d749320 861 vargs = XALLOCAVEC (struct value *, args_count);
5374244e
PM
862 for (i = 0; i < args_count; i++)
863 {
864 PyObject *item = PyTuple_GetItem (args, i);
865
866 if (item == NULL)
867 return NULL;
868
869 vargs[i] = convert_value_from_python (item);
870 if (vargs[i] == NULL)
871 return NULL;
872 }
873 }
874
492d29ea 875 TRY
5374244e 876 {
888fe1e1
TT
877 struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark);
878 struct value *return_value;
879
5374244e 880 return_value = call_function_by_hand (function, args_count, vargs);
888fe1e1
TT
881 result = value_to_value_object (return_value);
882 do_cleanups (cleanup);
5374244e 883 }
492d29ea
PA
884 CATCH (except, RETURN_MASK_ALL)
885 {
886 GDB_PY_HANDLE_EXCEPTION (except);
887 }
888 END_CATCH
5374244e 889
888fe1e1 890 return result;
5374244e
PM
891}
892
7843261b
TJB
893/* Called by the Python interpreter to obtain string representation
894 of the object. */
895static PyObject *
896valpy_str (PyObject *self)
897{
c92aed16 898 std::string s;
7843261b 899 PyObject *result;
79a45b7d 900 struct value_print_options opts;
7843261b 901
79a45b7d
TT
902 get_user_print_options (&opts);
903 opts.deref_ref = 0;
904
492d29ea 905 TRY
7843261b 906 {
5fe41fbf
TT
907 struct ui_file *stb = mem_fileopen ();
908 struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
909
79a45b7d 910 common_val_print (((value_object *) self)->value, stb, 0,
d452c4bc 911 &opts, python_language);
c92aed16 912 s = ui_file_as_string (stb);
5fe41fbf
TT
913
914 do_cleanups (old_chain);
7843261b 915 }
492d29ea
PA
916 CATCH (except, RETURN_MASK_ALL)
917 {
918 GDB_PY_HANDLE_EXCEPTION (except);
919 }
920 END_CATCH
7843261b 921
c92aed16 922 result = PyUnicode_Decode (s.c_str (), s.length (), host_charset (), NULL);
7843261b
TJB
923
924 return result;
925}
926
def2b000
TJB
927/* Implements gdb.Value.is_optimized_out. */
928static PyObject *
929valpy_get_is_optimized_out (PyObject *self, void *closure)
930{
931 struct value *value = ((value_object *) self)->value;
f287c1f3 932 int opt = 0;
def2b000 933
492d29ea 934 TRY
f287c1f3
PM
935 {
936 opt = value_optimized_out (value);
937 }
492d29ea
PA
938 CATCH (except, RETURN_MASK_ALL)
939 {
940 GDB_PY_HANDLE_EXCEPTION (except);
941 }
942 END_CATCH
f287c1f3
PM
943
944 if (opt)
def2b000
TJB
945 Py_RETURN_TRUE;
946
947 Py_RETURN_FALSE;
948}
949
913460fc
PK
950/* Implements gdb.Value.is_lazy. */
951static PyObject *
952valpy_get_is_lazy (PyObject *self, void *closure)
953{
954 struct value *value = ((value_object *) self)->value;
955 int opt = 0;
913460fc 956
492d29ea 957 TRY
913460fc
PK
958 {
959 opt = value_lazy (value);
960 }
492d29ea
PA
961 CATCH (except, RETURN_MASK_ALL)
962 {
963 GDB_PY_HANDLE_EXCEPTION (except);
964 }
965 END_CATCH
913460fc
PK
966
967 if (opt)
968 Py_RETURN_TRUE;
969
970 Py_RETURN_FALSE;
971}
972
973/* Implements gdb.Value.fetch_lazy (). */
974static PyObject *
975valpy_fetch_lazy (PyObject *self, PyObject *args)
976{
977 struct value *value = ((value_object *) self)->value;
913460fc 978
492d29ea 979 TRY
913460fc
PK
980 {
981 if (value_lazy (value))
982 value_fetch_lazy (value);
983 }
492d29ea
PA
984 CATCH (except, RETURN_MASK_ALL)
985 {
986 GDB_PY_HANDLE_EXCEPTION (except);
987 }
988 END_CATCH
913460fc
PK
989
990 Py_RETURN_NONE;
991}
992
88d4aea7
PM
993/* Calculate and return the address of the PyObject as the value of
994 the builtin __hash__ call. */
881d5d5d 995static Py_hash_t
88d4aea7
PM
996valpy_hash (PyObject *self)
997{
881d5d5d 998 return (intptr_t) self;
88d4aea7
PM
999}
1000
7843261b
TJB
1001enum valpy_opcode
1002{
1003 VALPY_ADD,
1004 VALPY_SUB,
1005 VALPY_MUL,
1006 VALPY_DIV,
1007 VALPY_REM,
08c637de
TJB
1008 VALPY_POW,
1009 VALPY_LSH,
1010 VALPY_RSH,
1011 VALPY_BITAND,
1012 VALPY_BITOR,
1013 VALPY_BITXOR
7843261b
TJB
1014};
1015
1016/* If TYPE is a reference, return the target; otherwise return TYPE. */
1017#define STRIP_REFERENCE(TYPE) \
1018 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
1019
9c6595ab
PA
1020/* Helper for valpy_binop. Returns a value object which is the result
1021 of applying the operation specified by OPCODE to the given
1022 arguments. Throws a GDB exception on error. */
1023
7843261b 1024static PyObject *
9c6595ab 1025valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other)
7843261b 1026{
888fe1e1 1027 PyObject *result = NULL;
7843261b 1028
9c6595ab
PA
1029 struct value *arg1, *arg2;
1030 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1031 struct value *res_val = NULL;
1032 enum exp_opcode op = OP_NULL;
1033 int handled = 0;
1034
1035 /* If the gdb.Value object is the second operand, then it will be
1036 passed to us as the OTHER argument, and SELF will be an entirely
1037 different kind of object, altogether. Because of this, we can't
1038 assume self is a gdb.Value object and need to convert it from
1039 python as well. */
1040 arg1 = convert_value_from_python (self);
1041 if (arg1 == NULL)
7843261b 1042 {
9c6595ab
PA
1043 do_cleanups (cleanup);
1044 return NULL;
1045 }
08c637de 1046
9c6595ab
PA
1047 arg2 = convert_value_from_python (other);
1048 if (arg2 == NULL)
1049 {
1050 do_cleanups (cleanup);
1051 return NULL;
1052 }
7843261b 1053
9c6595ab
PA
1054 switch (opcode)
1055 {
1056 case VALPY_ADD:
1057 {
1058 struct type *ltype = value_type (arg1);
1059 struct type *rtype = value_type (arg2);
1060
1061 ltype = check_typedef (ltype);
1062 ltype = STRIP_REFERENCE (ltype);
1063 rtype = check_typedef (rtype);
1064 rtype = STRIP_REFERENCE (rtype);
1065
1066 handled = 1;
1067 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
1068 && is_integral_type (rtype))
1069 res_val = value_ptradd (arg1, value_as_long (arg2));
1070 else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
1071 && is_integral_type (ltype))
1072 res_val = value_ptradd (arg2, value_as_long (arg1));
1073 else
7843261b 1074 {
9c6595ab
PA
1075 handled = 0;
1076 op = BINOP_ADD;
7843261b 1077 }
9c6595ab
PA
1078 }
1079 break;
1080 case VALPY_SUB:
1081 {
1082 struct type *ltype = value_type (arg1);
1083 struct type *rtype = value_type (arg2);
1084
1085 ltype = check_typedef (ltype);
1086 ltype = STRIP_REFERENCE (ltype);
1087 rtype = check_typedef (rtype);
1088 rtype = STRIP_REFERENCE (rtype);
1089
1090 handled = 1;
1091 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
1092 && TYPE_CODE (rtype) == TYPE_CODE_PTR)
1093 /* A ptrdiff_t for the target would be preferable here. */
1094 res_val = value_from_longest (builtin_type_pyint,
1095 value_ptrdiff (arg1, arg2));
1096 else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
1097 && is_integral_type (rtype))
1098 res_val = value_ptradd (arg1, - value_as_long (arg2));
1099 else
7843261b 1100 {
9c6595ab
PA
1101 handled = 0;
1102 op = BINOP_SUB;
7843261b 1103 }
9c6595ab
PA
1104 }
1105 break;
1106 case VALPY_MUL:
1107 op = BINOP_MUL;
1108 break;
1109 case VALPY_DIV:
1110 op = BINOP_DIV;
1111 break;
1112 case VALPY_REM:
1113 op = BINOP_REM;
1114 break;
1115 case VALPY_POW:
1116 op = BINOP_EXP;
1117 break;
1118 case VALPY_LSH:
1119 op = BINOP_LSH;
1120 break;
1121 case VALPY_RSH:
1122 op = BINOP_RSH;
1123 break;
1124 case VALPY_BITAND:
1125 op = BINOP_BITWISE_AND;
1126 break;
1127 case VALPY_BITOR:
1128 op = BINOP_BITWISE_IOR;
1129 break;
1130 case VALPY_BITXOR:
1131 op = BINOP_BITWISE_XOR;
1132 break;
1133 }
888fe1e1 1134
9c6595ab
PA
1135 if (!handled)
1136 {
1137 if (binop_user_defined_p (op, arg1, arg2))
1138 res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
1139 else
1140 res_val = value_binop (arg1, arg2, op);
1141 }
f7bd0f78 1142
9c6595ab
PA
1143 if (res_val)
1144 result = value_to_value_object (res_val);
888fe1e1 1145
9c6595ab
PA
1146 do_cleanups (cleanup);
1147 return result;
1148}
1149
1150/* Returns a value object which is the result of applying the operation
1151 specified by OPCODE to the given arguments. Returns NULL on error, with
1152 a python exception set. */
1153static PyObject *
1154valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
1155{
1156 PyObject *result = NULL;
1157
1158 TRY
1159 {
1160 result = valpy_binop_throw (opcode, self, other);
7843261b 1161 }
492d29ea
PA
1162 CATCH (except, RETURN_MASK_ALL)
1163 {
1164 GDB_PY_HANDLE_EXCEPTION (except);
1165 }
1166 END_CATCH
7843261b 1167
888fe1e1 1168 return result;
7843261b
TJB
1169}
1170
1171static PyObject *
1172valpy_add (PyObject *self, PyObject *other)
1173{
1174 return valpy_binop (VALPY_ADD, self, other);
1175}
1176
1177static PyObject *
1178valpy_subtract (PyObject *self, PyObject *other)
1179{
1180 return valpy_binop (VALPY_SUB, self, other);
1181}
1182
1183static PyObject *
1184valpy_multiply (PyObject *self, PyObject *other)
1185{
1186 return valpy_binop (VALPY_MUL, self, other);
1187}
1188
1189static PyObject *
1190valpy_divide (PyObject *self, PyObject *other)
1191{
1192 return valpy_binop (VALPY_DIV, self, other);
1193}
1194
1195static PyObject *
1196valpy_remainder (PyObject *self, PyObject *other)
1197{
1198 return valpy_binop (VALPY_REM, self, other);
1199}
1200
1201static PyObject *
1202valpy_power (PyObject *self, PyObject *other, PyObject *unused)
1203{
1204 /* We don't support the ternary form of pow. I don't know how to express
1205 that, so let's just throw NotImplementedError to at least do something
1206 about it. */
1207 if (unused != Py_None)
1208 {
1209 PyErr_SetString (PyExc_NotImplementedError,
1210 "Invalid operation on gdb.Value.");
1211 return NULL;
1212 }
1213
1214 return valpy_binop (VALPY_POW, self, other);
1215}
1216
1217static PyObject *
1218valpy_negative (PyObject *self)
1219{
888fe1e1 1220 PyObject *result = NULL;
7843261b 1221
492d29ea 1222 TRY
7843261b 1223 {
888fe1e1
TT
1224 /* Perhaps overkill, but consistency has some virtue. */
1225 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1226 struct value *val;
1227
7843261b 1228 val = value_neg (((value_object *) self)->value);
888fe1e1
TT
1229 result = value_to_value_object (val);
1230 do_cleanups (cleanup);
7843261b 1231 }
492d29ea
PA
1232 CATCH (except, RETURN_MASK_ALL)
1233 {
1234 GDB_PY_HANDLE_EXCEPTION (except);
1235 }
1236 END_CATCH
7843261b 1237
888fe1e1 1238 return result;
7843261b
TJB
1239}
1240
1241static PyObject *
1242valpy_positive (PyObject *self)
1243{
4e7a5ef5 1244 return value_to_value_object (((value_object *) self)->value);
7843261b
TJB
1245}
1246
1247static PyObject *
1248valpy_absolute (PyObject *self)
1249{
22601c15 1250 struct value *value = ((value_object *) self)->value;
f287c1f3 1251 int isabs = 1;
d59b6f6c 1252
492d29ea 1253 TRY
f287c1f3 1254 {
888fe1e1
TT
1255 struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1256
f287c1f3
PM
1257 if (value_less (value, value_zero (value_type (value), not_lval)))
1258 isabs = 0;
888fe1e1
TT
1259
1260 do_cleanups (cleanup);
f287c1f3 1261 }
492d29ea
PA
1262 CATCH (except, RETURN_MASK_ALL)
1263 {
1264 GDB_PY_HANDLE_EXCEPTION (except);
1265 }
1266 END_CATCH
f287c1f3
PM
1267
1268 if (isabs)
7843261b 1269 return valpy_positive (self);
f287c1f3
PM
1270 else
1271 return valpy_negative (self);
7843261b
TJB
1272}
1273
1274/* Implements boolean evaluation of gdb.Value. */
1275static int
1276valpy_nonzero (PyObject *self)
1277{
492d29ea 1278 struct gdb_exception except = exception_none;
7843261b
TJB
1279 value_object *self_value = (value_object *) self;
1280 struct type *type;
f287c1f3 1281 int nonzero = 0; /* Appease GCC warning. */
7843261b 1282
492d29ea 1283 TRY
f287c1f3 1284 {
5d9c5995
PM
1285 type = check_typedef (value_type (self_value->value));
1286
f287c1f3
PM
1287 if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
1288 nonzero = !!value_as_long (self_value->value);
1289 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1290 nonzero = value_as_double (self_value->value) != 0;
1291 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1292 nonzero = !decimal_is_zero (value_contents (self_value->value),
1293 TYPE_LENGTH (type),
1294 gdbarch_byte_order (get_type_arch (type)));
1295 else
1296 /* All other values are True. */
1297 nonzero = 1;
1298 }
492d29ea
PA
1299 CATCH (ex, RETURN_MASK_ALL)
1300 {
1301 except = ex;
1302 }
1303 END_CATCH
1304
f287c1f3
PM
1305 /* This is not documented in the Python documentation, but if this
1306 function fails, return -1 as slot_nb_nonzero does (the default
1307 Python nonzero function). */
1308 GDB_PY_SET_HANDLE_EXCEPTION (except);
1309
1310 return nonzero;
7843261b
TJB
1311}
1312
08c637de 1313/* Implements ~ for value objects. */
7843261b 1314static PyObject *
08c637de 1315valpy_invert (PyObject *self)
7843261b 1316{
08c637de 1317 struct value *val = NULL;
7843261b 1318
492d29ea 1319 TRY
7843261b 1320 {
08c637de
TJB
1321 val = value_complement (((value_object *) self)->value);
1322 }
492d29ea
PA
1323 CATCH (except, RETURN_MASK_ALL)
1324 {
1325 GDB_PY_HANDLE_EXCEPTION (except);
1326 }
1327 END_CATCH
7843261b 1328
08c637de
TJB
1329 return value_to_value_object (val);
1330}
7843261b 1331
08c637de
TJB
1332/* Implements left shift for value objects. */
1333static PyObject *
1334valpy_lsh (PyObject *self, PyObject *other)
1335{
1336 return valpy_binop (VALPY_LSH, self, other);
1337}
7843261b 1338
08c637de
TJB
1339/* Implements right shift for value objects. */
1340static PyObject *
1341valpy_rsh (PyObject *self, PyObject *other)
1342{
1343 return valpy_binop (VALPY_RSH, self, other);
1344}
7843261b 1345
08c637de
TJB
1346/* Implements bitwise and for value objects. */
1347static PyObject *
1348valpy_and (PyObject *self, PyObject *other)
1349{
1350 return valpy_binop (VALPY_BITAND, self, other);
1351}
7843261b 1352
08c637de
TJB
1353/* Implements bitwise or for value objects. */
1354static PyObject *
1355valpy_or (PyObject *self, PyObject *other)
1356{
1357 return valpy_binop (VALPY_BITOR, self, other);
1358}
1359
1360/* Implements bitwise xor for value objects. */
1361static PyObject *
1362valpy_xor (PyObject *self, PyObject *other)
1363{
1364 return valpy_binop (VALPY_BITXOR, self, other);
1365}
1366
9c6595ab
PA
1367/* Helper for valpy_richcompare. Implements comparison operations for
1368 value objects. Returns true/false on success. Returns -1 with a
1369 Python exception set if a Python error is detected. Throws a GDB
1370 exception on other errors (memory error, etc.). */
1371
1372static int
1373valpy_richcompare_throw (PyObject *self, PyObject *other, int op)
1374{
1375 int result;
1376 struct value *value_other;
1377 struct value *value_self;
1378 struct value *mark = value_mark ();
1379 struct cleanup *cleanup;
1380
1381 value_other = convert_value_from_python (other);
1382 if (value_other == NULL)
1383 return -1;
1384
1385 cleanup = make_cleanup_value_free_to_mark (mark);
1386
1387 value_self = ((value_object *) self)->value;
1388
1389 switch (op)
1390 {
1391 case Py_LT:
1392 result = value_less (value_self, value_other);
1393 break;
1394 case Py_LE:
1395 result = value_less (value_self, value_other)
1396 || value_equal (value_self, value_other);
1397 break;
1398 case Py_EQ:
1399 result = value_equal (value_self, value_other);
1400 break;
1401 case Py_NE:
1402 result = !value_equal (value_self, value_other);
1403 break;
1404 case Py_GT:
1405 result = value_less (value_other, value_self);
1406 break;
1407 case Py_GE:
1408 result = (value_less (value_other, value_self)
1409 || value_equal (value_self, value_other));
1410 break;
1411 default:
1412 /* Can't happen. */
1413 PyErr_SetString (PyExc_NotImplementedError,
1414 _("Invalid operation on gdb.Value."));
1415 result = -1;
1416 break;
1417 }
1418
1419 do_cleanups (cleanup);
1420 return result;
1421}
1422
1423
8dc78533
JK
1424/* Implements comparison operations for value objects. Returns NULL on error,
1425 with a python exception set. */
08c637de
TJB
1426static PyObject *
1427valpy_richcompare (PyObject *self, PyObject *other, int op)
1428{
1429 int result = 0;
08c637de
TJB
1430
1431 if (other == Py_None)
7843261b
TJB
1432 /* Comparing with None is special. From what I can tell, in Python
1433 None is smaller than anything else. */
1434 switch (op) {
1435 case Py_LT:
1436 case Py_LE:
1437 case Py_EQ:
1438 Py_RETURN_FALSE;
1439 case Py_NE:
1440 case Py_GT:
1441 case Py_GE:
1442 Py_RETURN_TRUE;
1443 default:
1444 /* Can't happen. */
1445 PyErr_SetString (PyExc_NotImplementedError,
044c0f87 1446 _("Invalid operation on gdb.Value."));
7843261b
TJB
1447 return NULL;
1448 }
7843261b 1449
492d29ea 1450 TRY
7843261b 1451 {
9c6595ab 1452 result = valpy_richcompare_throw (self, other, op);
7843261b 1453 }
492d29ea
PA
1454 CATCH (except, RETURN_MASK_ALL)
1455 {
1456 GDB_PY_HANDLE_EXCEPTION (except);
1457 }
1458 END_CATCH
7843261b 1459
f02779d8
TT
1460 /* In this case, the Python exception has already been set. */
1461 if (result < 0)
1462 return NULL;
1463
7843261b
TJB
1464 if (result == 1)
1465 Py_RETURN_TRUE;
1466
1467 Py_RETURN_FALSE;
1468}
1469
9a27f2c6 1470#ifndef IS_PY3K
08c637de
TJB
1471/* Implements conversion to int. */
1472static PyObject *
1473valpy_int (PyObject *self)
1474{
1475 struct value *value = ((value_object *) self)->value;
1476 struct type *type = value_type (value);
1477 LONGEST l = 0;
08c637de 1478
492d29ea 1479 TRY
08c637de 1480 {
7c245c24 1481 if (!is_integral_type (type))
f287c1f3
PM
1482 error (_("Cannot convert value to int."));
1483
08c637de
TJB
1484 l = value_as_long (value);
1485 }
492d29ea
PA
1486 CATCH (except, RETURN_MASK_ALL)
1487 {
1488 GDB_PY_HANDLE_EXCEPTION (except);
1489 }
1490 END_CATCH
08c637de 1491
74aedc46 1492 return gdb_py_object_from_longest (l);
08c637de 1493}
9a27f2c6 1494#endif
08c637de
TJB
1495
1496/* Implements conversion to long. */
1497static PyObject *
1498valpy_long (PyObject *self)
1499{
1500 struct value *value = ((value_object *) self)->value;
1501 struct type *type = value_type (value);
1502 LONGEST l = 0;
08c637de 1503
492d29ea 1504 TRY
08c637de 1505 {
f168693b 1506 type = check_typedef (type);
f287c1f3 1507
7c245c24
JB
1508 if (!is_integral_type (type)
1509 && TYPE_CODE (type) != TYPE_CODE_PTR)
f287c1f3
PM
1510 error (_("Cannot convert value to long."));
1511
08c637de
TJB
1512 l = value_as_long (value);
1513 }
492d29ea
PA
1514 CATCH (except, RETURN_MASK_ALL)
1515 {
1516 GDB_PY_HANDLE_EXCEPTION (except);
1517 }
1518 END_CATCH
08c637de 1519
33fa2c6e
DE
1520 if (TYPE_UNSIGNED (type))
1521 return gdb_py_long_from_ulongest (l);
1522 else
1523 return gdb_py_long_from_longest (l);
08c637de
TJB
1524}
1525
1526/* Implements conversion to float. */
1527static PyObject *
1528valpy_float (PyObject *self)
1529{
1530 struct value *value = ((value_object *) self)->value;
1531 struct type *type = value_type (value);
1532 double d = 0;
08c637de 1533
492d29ea 1534 TRY
08c637de 1535 {
f168693b 1536 type = check_typedef (type);
f287c1f3
PM
1537
1538 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1539 error (_("Cannot convert value to float."));
1540
08c637de
TJB
1541 d = value_as_double (value);
1542 }
492d29ea
PA
1543 CATCH (except, RETURN_MASK_ALL)
1544 {
1545 GDB_PY_HANDLE_EXCEPTION (except);
1546 }
1547 END_CATCH
08c637de
TJB
1548
1549 return PyFloat_FromDouble (d);
1550}
1551
7843261b
TJB
1552/* Returns an object for a value which is released from the all_values chain,
1553 so its lifetime is not bound to the execution of a command. */
1554PyObject *
1555value_to_value_object (struct value *val)
1556{
1557 value_object *val_obj;
1558
1559 val_obj = PyObject_New (value_object, &value_object_type);
1560 if (val_obj != NULL)
1561 {
1562 val_obj->value = val;
e848a8a5 1563 release_value_or_incref (val);
c0c6f777 1564 val_obj->address = NULL;
2c74e833 1565 val_obj->type = NULL;
03f17ccf 1566 val_obj->dynamic_type = NULL;
4e7a5ef5 1567 note_value (val_obj);
7843261b
TJB
1568 }
1569
1570 return (PyObject *) val_obj;
1571}
1572
4e7a5ef5
TT
1573/* Returns a borrowed reference to the struct value corresponding to
1574 the given value object. */
a6bac58e
TT
1575struct value *
1576value_object_to_value (PyObject *self)
1577{
1578 value_object *real;
d59b6f6c 1579
a6bac58e
TT
1580 if (! PyObject_TypeCheck (self, &value_object_type))
1581 return NULL;
1582 real = (value_object *) self;
1583 return real->value;
1584}
1585
7843261b 1586/* Try to convert a Python value to a gdb value. If the value cannot
4e7a5ef5
TT
1587 be converted, set a Python exception and return NULL. Returns a
1588 reference to a new value on the all_values chain. */
7843261b
TJB
1589
1590struct value *
1591convert_value_from_python (PyObject *obj)
1592{
1593 struct value *value = NULL; /* -Wall */
08c637de 1594 int cmp;
7843261b 1595
08c637de 1596 gdb_assert (obj != NULL);
7843261b 1597
492d29ea 1598 TRY
7843261b 1599 {
256458bc 1600 if (PyBool_Check (obj))
08c637de
TJB
1601 {
1602 cmp = PyObject_IsTrue (obj);
1603 if (cmp >= 0)
1604 value = value_from_longest (builtin_type_pybool, cmp);
1605 }
06ab7b19
PM
1606 /* Make a long logic check first. In Python 3.x, internally,
1607 all integers are represented as longs. In Python 2.x, there
1608 is still a differentiation internally between a PyInt and a
1609 PyLong. Explicitly do this long check conversion first. In
1610 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
1611 to be done first to ensure we do not lose information in the
1612 conversion process. */
08c637de
TJB
1613 else if (PyLong_Check (obj))
1614 {
1615 LONGEST l = PyLong_AsLongLong (obj);
7843261b 1616
595939de
PM
1617 if (PyErr_Occurred ())
1618 {
1619 /* If the error was an overflow, we can try converting to
1620 ULONGEST instead. */
1621 if (PyErr_ExceptionMatches (PyExc_OverflowError))
1622 {
1623 PyObject *etype, *evalue, *etraceback, *zero;
1624
1625 PyErr_Fetch (&etype, &evalue, &etraceback);
1626 zero = PyInt_FromLong (0);
1627
1628 /* Check whether obj is positive. */
1629 if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0)
1630 {
1631 ULONGEST ul;
1632
1633 ul = PyLong_AsUnsignedLongLong (obj);
1634 if (! PyErr_Occurred ())
1635 value = value_from_ulongest (builtin_type_upylong, ul);
1636 }
1637 else
1638 /* There's nothing we can do. */
1639 PyErr_Restore (etype, evalue, etraceback);
1640
1641 Py_DECREF (zero);
1642 }
1643 }
1644 else
08c637de
TJB
1645 value = value_from_longest (builtin_type_pylong, l);
1646 }
12c58cd4 1647#if PY_MAJOR_VERSION == 2
06ab7b19
PM
1648 else if (PyInt_Check (obj))
1649 {
1650 long l = PyInt_AsLong (obj);
1651
1652 if (! PyErr_Occurred ())
1653 value = value_from_longest (builtin_type_pyint, l);
1654 }
12c58cd4 1655#endif
08c637de
TJB
1656 else if (PyFloat_Check (obj))
1657 {
1658 double d = PyFloat_AsDouble (obj);
7843261b 1659
08c637de
TJB
1660 if (! PyErr_Occurred ())
1661 value = value_from_double (builtin_type_pyfloat, d);
1662 }
1663 else if (gdbpy_is_string (obj))
1664 {
9b972014
TT
1665 gdb::unique_xmalloc_ptr<char> s
1666 = python_string_to_target_string (obj);
08c637de 1667 if (s != NULL)
9b972014
TT
1668 value = value_cstring (s.get (), strlen (s.get ()),
1669 builtin_type_pychar);
08c637de
TJB
1670 }
1671 else if (PyObject_TypeCheck (obj, &value_object_type))
cc924cad 1672 value = value_copy (((value_object *) obj)->value);
be759fcf
PM
1673 else if (gdbpy_is_lazy_string (obj))
1674 {
1675 PyObject *result;
d59b6f6c 1676
fb6a3ed3 1677 result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst, NULL);
be759fcf
PM
1678 value = value_copy (((value_object *) result)->value);
1679 }
08c637de 1680 else
9a27f2c6
PK
1681#ifdef IS_PY3K
1682 PyErr_Format (PyExc_TypeError,
1683 _("Could not convert Python object: %S."), obj);
1684#else
9a2b4c1b
MS
1685 PyErr_Format (PyExc_TypeError,
1686 _("Could not convert Python object: %s."),
08c637de 1687 PyString_AsString (PyObject_Str (obj)));
9a27f2c6 1688#endif
08c637de 1689 }
492d29ea 1690 CATCH (except, RETURN_MASK_ALL)
08c637de
TJB
1691 {
1692 PyErr_Format (except.reason == RETURN_QUIT
1693 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
1694 "%s", except.message);
1695 return NULL;
1696 }
492d29ea 1697 END_CATCH
7843261b
TJB
1698
1699 return value;
1700}
1701
1702/* Returns value object in the ARGth position in GDB's history. */
1703PyObject *
08c637de 1704gdbpy_history (PyObject *self, PyObject *args)
7843261b
TJB
1705{
1706 int i;
1707 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
7843261b
TJB
1708
1709 if (!PyArg_ParseTuple (args, "i", &i))
1710 return NULL;
1711
492d29ea 1712 TRY
7843261b
TJB
1713 {
1714 res_val = access_value_history (i);
1715 }
492d29ea
PA
1716 CATCH (except, RETURN_MASK_ALL)
1717 {
1718 GDB_PY_HANDLE_EXCEPTION (except);
1719 }
1720 END_CATCH
7843261b
TJB
1721
1722 return value_to_value_object (res_val);
1723}
1724
595939de
PM
1725/* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1726
1727int
1728gdbpy_is_value_object (PyObject *obj)
1729{
1730 return PyObject_TypeCheck (obj, &value_object_type);
1731}
1732
999633ed 1733int
7843261b
TJB
1734gdbpy_initialize_values (void)
1735{
7843261b 1736 if (PyType_Ready (&value_object_type) < 0)
999633ed 1737 return -1;
7843261b 1738
aa36459a
TT
1739 return gdb_pymodule_addobject (gdb_module, "Value",
1740 (PyObject *) &value_object_type);
7843261b
TJB
1741}
1742
2c74e833
TT
1743\f
1744
def2b000 1745static PyGetSetDef value_object_getset[] = {
c0c6f777
TJB
1746 { "address", valpy_get_address, NULL, "The address of the value.",
1747 NULL },
def2b000 1748 { "is_optimized_out", valpy_get_is_optimized_out, NULL,
9a2b4c1b
MS
1749 "Boolean telling whether the value is optimized "
1750 "out (i.e., not available).",
def2b000 1751 NULL },
2c74e833 1752 { "type", valpy_get_type, NULL, "Type of the value.", NULL },
03f17ccf
TT
1753 { "dynamic_type", valpy_get_dynamic_type, NULL,
1754 "Dynamic type of the value.", NULL },
913460fc
PK
1755 { "is_lazy", valpy_get_is_lazy, NULL,
1756 "Boolean telling whether the value is lazy (not fetched yet\n\
1757from the inferior). A lazy value is fetched when needed, or when\n\
1758the \"fetch_lazy()\" method is called.", NULL },
def2b000
TJB
1759 {NULL} /* Sentinel */
1760};
1761
f9176c46 1762static PyMethodDef value_object_methods[] = {
2c74e833 1763 { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
f9ffd4bb
TT
1764 { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
1765 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1766Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1767 },
1768 { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
1769 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1770Cast the value to the supplied type, as if by the C++\n\
1771reinterpret_cast operator."
1772 },
f9176c46 1773 { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
7b282c5a
SCR
1774 { "referenced_value", valpy_referenced_value, METH_NOARGS,
1775 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
4c082a81
SC
1776 { "reference_value", valpy_reference_value, METH_NOARGS,
1777 "Return a value of type TYPE_CODE_REF referencing this value." },
1778 { "const_value", valpy_const_value, METH_NOARGS,
1779 "Return a 'const' qualied version of the same value." },
9a2b4c1b
MS
1780 { "lazy_string", (PyCFunction) valpy_lazy_string,
1781 METH_VARARGS | METH_KEYWORDS,
be759fcf
PM
1782 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1783Return a lazy string representation of the value." },
cc924cad 1784 { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
fbb8f299 1785 "string ([encoding] [, errors] [, length]) -> string\n\
cc924cad 1786Return Unicode string representation of the value." },
256458bc 1787 { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
913460fc 1788 "Fetches the value from the inferior, if it was lazy." },
f9176c46
PA
1789 {NULL} /* Sentinel */
1790};
1791
1792static PyNumberMethods value_object_as_number = {
1793 valpy_add,
1794 valpy_subtract,
1795 valpy_multiply,
9a27f2c6 1796#ifndef IS_PY3K
f9176c46 1797 valpy_divide,
9a27f2c6 1798#endif
f9176c46
PA
1799 valpy_remainder,
1800 NULL, /* nb_divmod */
1801 valpy_power, /* nb_power */
1802 valpy_negative, /* nb_negative */
1803 valpy_positive, /* nb_positive */
1804 valpy_absolute, /* nb_absolute */
08c637de
TJB
1805 valpy_nonzero, /* nb_nonzero */
1806 valpy_invert, /* nb_invert */
1807 valpy_lsh, /* nb_lshift */
1808 valpy_rsh, /* nb_rshift */
1809 valpy_and, /* nb_and */
1810 valpy_xor, /* nb_xor */
1811 valpy_or, /* nb_or */
9a27f2c6
PK
1812#ifdef IS_PY3K
1813 valpy_long, /* nb_int */
1814 NULL, /* reserved */
1815#else
08c637de
TJB
1816 NULL, /* nb_coerce */
1817 valpy_int, /* nb_int */
1818 valpy_long, /* nb_long */
9a27f2c6 1819#endif
08c637de 1820 valpy_float, /* nb_float */
9a27f2c6 1821#ifndef IS_PY3K
08c637de 1822 NULL, /* nb_oct */
9a27f2c6
PK
1823 NULL, /* nb_hex */
1824#endif
1825 NULL, /* nb_inplace_add */
1826 NULL, /* nb_inplace_subtract */
1827 NULL, /* nb_inplace_multiply */
e2b7f516
TT
1828#ifndef IS_PY3K
1829 NULL, /* nb_inplace_divide */
1830#endif
9a27f2c6
PK
1831 NULL, /* nb_inplace_remainder */
1832 NULL, /* nb_inplace_power */
1833 NULL, /* nb_inplace_lshift */
1834 NULL, /* nb_inplace_rshift */
1835 NULL, /* nb_inplace_and */
1836 NULL, /* nb_inplace_xor */
1837 NULL, /* nb_inplace_or */
1838 NULL, /* nb_floor_divide */
ddae9462
TT
1839 valpy_divide, /* nb_true_divide */
1840 NULL, /* nb_inplace_floor_divide */
1841 NULL, /* nb_inplace_true_divide */
7bd787e8 1842#ifndef HAVE_LIBPYTHON2_4
ddae9462
TT
1843 /* This was added in Python 2.5. */
1844 valpy_long, /* nb_index */
7bd787e8 1845#endif /* HAVE_LIBPYTHON2_4 */
f9176c46
PA
1846};
1847
1848static PyMappingMethods value_object_as_mapping = {
1849 valpy_length,
1850 valpy_getitem,
1851 valpy_setitem
1852};
1853
1854PyTypeObject value_object_type = {
9a27f2c6 1855 PyVarObject_HEAD_INIT (NULL, 0)
f9176c46
PA
1856 "gdb.Value", /*tp_name*/
1857 sizeof (value_object), /*tp_basicsize*/
1858 0, /*tp_itemsize*/
1859 valpy_dealloc, /*tp_dealloc*/
1860 0, /*tp_print*/
1861 0, /*tp_getattr*/
1862 0, /*tp_setattr*/
1863 0, /*tp_compare*/
1864 0, /*tp_repr*/
1865 &value_object_as_number, /*tp_as_number*/
1866 0, /*tp_as_sequence*/
1867 &value_object_as_mapping, /*tp_as_mapping*/
88d4aea7 1868 valpy_hash, /*tp_hash*/
5374244e 1869 valpy_call, /*tp_call*/
f9176c46
PA
1870 valpy_str, /*tp_str*/
1871 0, /*tp_getattro*/
1872 0, /*tp_setattro*/
1873 0, /*tp_as_buffer*/
9a2b4c1b
MS
1874 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
1875 | Py_TPFLAGS_BASETYPE, /*tp_flags*/
f9176c46
PA
1876 "GDB value object", /* tp_doc */
1877 0, /* tp_traverse */
1878 0, /* tp_clear */
1879 valpy_richcompare, /* tp_richcompare */
1880 0, /* tp_weaklistoffset */
1881 0, /* tp_iter */
1882 0, /* tp_iternext */
08c637de
TJB
1883 value_object_methods, /* tp_methods */
1884 0, /* tp_members */
def2b000 1885 value_object_getset, /* tp_getset */
08c637de
TJB
1886 0, /* tp_base */
1887 0, /* tp_dict */
1888 0, /* tp_descr_get */
1889 0, /* tp_descr_set */
1890 0, /* tp_dictoffset */
1891 0, /* tp_init */
1892 0, /* tp_alloc */
1893 valpy_new /* tp_new */
f9176c46 1894};
This page took 1.032232 seconds and 4 git commands to generate.