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