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