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