xref: /dflybsd-src/contrib/gdb-7/gdb/python/py-value.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Python interface to values.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2008-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "gdb_assert.h"
225796c8dcSSimon Schubert #include "charset.h"
235796c8dcSSimon Schubert #include "value.h"
245796c8dcSSimon Schubert #include "exceptions.h"
255796c8dcSSimon Schubert #include "language.h"
265796c8dcSSimon Schubert #include "dfp.h"
275796c8dcSSimon Schubert #include "valprint.h"
28c50c785cSJohn Marino #include "infcall.h"
29c50c785cSJohn Marino #include "expression.h"
30c50c785cSJohn Marino #include "cp-abi.h"
31*ef5ccd6cSJohn Marino #include "python.h"
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert #ifdef HAVE_PYTHON
345796c8dcSSimon Schubert 
355796c8dcSSimon Schubert #include "python-internal.h"
365796c8dcSSimon Schubert 
375796c8dcSSimon Schubert /* Even though Python scalar types directly map to host types, we use
38c50c785cSJohn Marino    target types here to remain consistent with the values system in
395796c8dcSSimon Schubert    GDB (which uses target arithmetic).  */
405796c8dcSSimon Schubert 
415796c8dcSSimon Schubert /* Python's integer type corresponds to C's long type.  */
425796c8dcSSimon Schubert #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert /* Python's float type corresponds to C's double type.  */
455796c8dcSSimon Schubert #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert /* Python's long type corresponds to C's long long type.  */
485796c8dcSSimon Schubert #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
495796c8dcSSimon Schubert 
50cf7f2e2dSJohn Marino /* Python's long type corresponds to C's long long type.  Unsigned version.  */
51cf7f2e2dSJohn Marino #define builtin_type_upylong builtin_type \
52cf7f2e2dSJohn Marino   (python_gdbarch)->builtin_unsigned_long_long
53cf7f2e2dSJohn Marino 
545796c8dcSSimon Schubert #define builtin_type_pybool \
555796c8dcSSimon Schubert   language_bool_type (python_language, python_gdbarch)
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert #define builtin_type_pychar \
585796c8dcSSimon Schubert   language_string_char_type (python_language, python_gdbarch)
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert typedef struct value_object {
615796c8dcSSimon Schubert   PyObject_HEAD
625796c8dcSSimon Schubert   struct value_object *next;
635796c8dcSSimon Schubert   struct value_object *prev;
645796c8dcSSimon Schubert   struct value *value;
655796c8dcSSimon Schubert   PyObject *address;
665796c8dcSSimon Schubert   PyObject *type;
67c50c785cSJohn Marino   PyObject *dynamic_type;
685796c8dcSSimon Schubert } value_object;
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert /* List of all values which are currently exposed to Python. It is
715796c8dcSSimon Schubert    maintained so that when an objfile is discarded, preserve_values
725796c8dcSSimon Schubert    can copy the values' types if needed.  */
735796c8dcSSimon Schubert /* This variable is unnecessarily initialized to NULL in order to
745796c8dcSSimon Schubert    work around a linker bug on MacOS.  */
755796c8dcSSimon Schubert static value_object *values_in_python = NULL;
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert /* Called by the Python interpreter when deallocating a value object.  */
785796c8dcSSimon Schubert static void
valpy_dealloc(PyObject * obj)795796c8dcSSimon Schubert valpy_dealloc (PyObject *obj)
805796c8dcSSimon Schubert {
815796c8dcSSimon Schubert   value_object *self = (value_object *) obj;
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert   /* Remove SELF from the global list.  */
845796c8dcSSimon Schubert   if (self->prev)
855796c8dcSSimon Schubert     self->prev->next = self->next;
865796c8dcSSimon Schubert   else
875796c8dcSSimon Schubert     {
885796c8dcSSimon Schubert       gdb_assert (values_in_python == self);
895796c8dcSSimon Schubert       values_in_python = self->next;
905796c8dcSSimon Schubert     }
915796c8dcSSimon Schubert   if (self->next)
925796c8dcSSimon Schubert     self->next->prev = self->prev;
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert   value_free (self->value);
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert   if (self->address)
975796c8dcSSimon Schubert     /* Use braces to appease gcc warning.  *sigh*  */
985796c8dcSSimon Schubert     {
995796c8dcSSimon Schubert       Py_DECREF (self->address);
1005796c8dcSSimon Schubert     }
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert   if (self->type)
1035796c8dcSSimon Schubert     {
1045796c8dcSSimon Schubert       Py_DECREF (self->type);
1055796c8dcSSimon Schubert     }
1065796c8dcSSimon Schubert 
107c50c785cSJohn Marino   Py_XDECREF (self->dynamic_type);
108c50c785cSJohn Marino 
109*ef5ccd6cSJohn Marino   Py_TYPE (self)->tp_free (self);
1105796c8dcSSimon Schubert }
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert /* Helper to push a Value object on the global list.  */
1135796c8dcSSimon Schubert static void
note_value(value_object * value_obj)1145796c8dcSSimon Schubert note_value (value_object *value_obj)
1155796c8dcSSimon Schubert {
1165796c8dcSSimon Schubert   value_obj->next = values_in_python;
1175796c8dcSSimon Schubert   if (value_obj->next)
1185796c8dcSSimon Schubert     value_obj->next->prev = value_obj;
1195796c8dcSSimon Schubert   value_obj->prev = NULL;
1205796c8dcSSimon Schubert   values_in_python = value_obj;
1215796c8dcSSimon Schubert }
1225796c8dcSSimon Schubert 
123c50c785cSJohn Marino /* Called when a new gdb.Value object needs to be allocated.  Returns NULL on
124c50c785cSJohn Marino    error, with a python exception set.  */
1255796c8dcSSimon Schubert static PyObject *
valpy_new(PyTypeObject * subtype,PyObject * args,PyObject * keywords)1265796c8dcSSimon Schubert valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
1275796c8dcSSimon Schubert {
1285796c8dcSSimon Schubert   struct value *value = NULL;   /* Initialize to appease gcc warning.  */
1295796c8dcSSimon Schubert   value_object *value_obj;
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert   if (PyTuple_Size (args) != 1)
1325796c8dcSSimon Schubert     {
1335796c8dcSSimon Schubert       PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
1345796c8dcSSimon Schubert 					  "1 argument"));
1355796c8dcSSimon Schubert       return NULL;
1365796c8dcSSimon Schubert     }
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert   value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
1395796c8dcSSimon Schubert   if (value_obj == NULL)
1405796c8dcSSimon Schubert     {
1415796c8dcSSimon Schubert       PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
1425796c8dcSSimon Schubert 					    "create Value object."));
1435796c8dcSSimon Schubert       return NULL;
1445796c8dcSSimon Schubert     }
1455796c8dcSSimon Schubert 
1465796c8dcSSimon Schubert   value = convert_value_from_python (PyTuple_GetItem (args, 0));
1475796c8dcSSimon Schubert   if (value == NULL)
1485796c8dcSSimon Schubert     {
1495796c8dcSSimon Schubert       subtype->tp_free (value_obj);
1505796c8dcSSimon Schubert       return NULL;
1515796c8dcSSimon Schubert     }
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert   value_obj->value = value;
154*ef5ccd6cSJohn Marino   release_value_or_incref (value);
1555796c8dcSSimon Schubert   value_obj->address = NULL;
1565796c8dcSSimon Schubert   value_obj->type = NULL;
157c50c785cSJohn Marino   value_obj->dynamic_type = NULL;
1585796c8dcSSimon Schubert   note_value (value_obj);
1595796c8dcSSimon Schubert 
1605796c8dcSSimon Schubert   return (PyObject *) value_obj;
1615796c8dcSSimon Schubert }
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert /* Iterate over all the Value objects, calling preserve_one_value on
1645796c8dcSSimon Schubert    each.  */
1655796c8dcSSimon Schubert void
preserve_python_values(struct objfile * objfile,htab_t copied_types)1665796c8dcSSimon Schubert preserve_python_values (struct objfile *objfile, htab_t copied_types)
1675796c8dcSSimon Schubert {
1685796c8dcSSimon Schubert   value_object *iter;
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert   for (iter = values_in_python; iter; iter = iter->next)
1715796c8dcSSimon Schubert     preserve_one_value (iter->value, objfile, copied_types);
1725796c8dcSSimon Schubert }
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert /* Given a value of a pointer type, apply the C unary * operator to it.  */
1755796c8dcSSimon Schubert static PyObject *
valpy_dereference(PyObject * self,PyObject * args)1765796c8dcSSimon Schubert valpy_dereference (PyObject *self, PyObject *args)
1775796c8dcSSimon Schubert {
1785796c8dcSSimon Schubert   volatile struct gdb_exception except;
179*ef5ccd6cSJohn Marino   PyObject *result = NULL;
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
1825796c8dcSSimon Schubert     {
183*ef5ccd6cSJohn Marino       struct value *res_val;
184*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
185*ef5ccd6cSJohn Marino 
1865796c8dcSSimon Schubert       res_val = value_ind (((value_object *) self)->value);
187*ef5ccd6cSJohn Marino       result = value_to_value_object (res_val);
188*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
1895796c8dcSSimon Schubert     }
1905796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
1915796c8dcSSimon Schubert 
192*ef5ccd6cSJohn Marino   return result;
193*ef5ccd6cSJohn Marino }
194*ef5ccd6cSJohn Marino 
195*ef5ccd6cSJohn Marino /* Given a value of a pointer type or a reference type, return the value
196*ef5ccd6cSJohn Marino    referenced. The difference between this function and valpy_dereference is
197*ef5ccd6cSJohn Marino    that the latter applies * unary operator to a value, which need not always
198*ef5ccd6cSJohn Marino    result in the value referenced. For example, for a value which is a reference
199*ef5ccd6cSJohn Marino    to an 'int' pointer ('int *'), valpy_dereference will result in a value of
200*ef5ccd6cSJohn Marino    type 'int' while valpy_referenced_value will result in a value of type
201*ef5ccd6cSJohn Marino    'int *'.  */
202*ef5ccd6cSJohn Marino 
203*ef5ccd6cSJohn Marino static PyObject *
valpy_referenced_value(PyObject * self,PyObject * args)204*ef5ccd6cSJohn Marino valpy_referenced_value (PyObject *self, PyObject *args)
205*ef5ccd6cSJohn Marino {
206*ef5ccd6cSJohn Marino   volatile struct gdb_exception except;
207*ef5ccd6cSJohn Marino   PyObject *result = NULL;
208*ef5ccd6cSJohn Marino 
209*ef5ccd6cSJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
210*ef5ccd6cSJohn Marino     {
211*ef5ccd6cSJohn Marino       struct value *self_val, *res_val;
212*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
213*ef5ccd6cSJohn Marino 
214*ef5ccd6cSJohn Marino       self_val = ((value_object *) self)->value;
215*ef5ccd6cSJohn Marino       switch (TYPE_CODE (check_typedef (value_type (self_val))))
216*ef5ccd6cSJohn Marino         {
217*ef5ccd6cSJohn Marino         case TYPE_CODE_PTR:
218*ef5ccd6cSJohn Marino           res_val = value_ind (self_val);
219*ef5ccd6cSJohn Marino           break;
220*ef5ccd6cSJohn Marino         case TYPE_CODE_REF:
221*ef5ccd6cSJohn Marino           res_val = coerce_ref (self_val);
222*ef5ccd6cSJohn Marino           break;
223*ef5ccd6cSJohn Marino         default:
224*ef5ccd6cSJohn Marino           error(_("Trying to get the referenced value from a value which is "
225*ef5ccd6cSJohn Marino                   "neither a pointer nor a reference."));
226*ef5ccd6cSJohn Marino         }
227*ef5ccd6cSJohn Marino 
228*ef5ccd6cSJohn Marino       result = value_to_value_object (res_val);
229*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
230*ef5ccd6cSJohn Marino     }
231*ef5ccd6cSJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
232*ef5ccd6cSJohn Marino 
233*ef5ccd6cSJohn Marino   return result;
2345796c8dcSSimon Schubert }
2355796c8dcSSimon Schubert 
2365796c8dcSSimon Schubert /* Return "&value".  */
2375796c8dcSSimon Schubert static PyObject *
valpy_get_address(PyObject * self,void * closure)2385796c8dcSSimon Schubert valpy_get_address (PyObject *self, void *closure)
2395796c8dcSSimon Schubert {
2405796c8dcSSimon Schubert   value_object *val_obj = (value_object *) self;
2415796c8dcSSimon Schubert   volatile struct gdb_exception except;
2425796c8dcSSimon Schubert 
2435796c8dcSSimon Schubert   if (!val_obj->address)
2445796c8dcSSimon Schubert     {
2455796c8dcSSimon Schubert       TRY_CATCH (except, RETURN_MASK_ALL)
2465796c8dcSSimon Schubert 	{
247*ef5ccd6cSJohn Marino 	  struct value *res_val;
248*ef5ccd6cSJohn Marino 	  struct cleanup *cleanup
249*ef5ccd6cSJohn Marino 	    = make_cleanup_value_free_to_mark (value_mark ());
250*ef5ccd6cSJohn Marino 
2515796c8dcSSimon Schubert 	  res_val = value_addr (val_obj->value);
252*ef5ccd6cSJohn Marino 	  val_obj->address = value_to_value_object (res_val);
253*ef5ccd6cSJohn Marino 	  do_cleanups (cleanup);
2545796c8dcSSimon Schubert 	}
2555796c8dcSSimon Schubert       if (except.reason < 0)
2565796c8dcSSimon Schubert 	{
2575796c8dcSSimon Schubert 	  val_obj->address = Py_None;
2585796c8dcSSimon Schubert 	  Py_INCREF (Py_None);
2595796c8dcSSimon Schubert 	}
2605796c8dcSSimon Schubert     }
2615796c8dcSSimon Schubert 
262a45ae5f8SJohn Marino   Py_XINCREF (val_obj->address);
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert   return val_obj->address;
2655796c8dcSSimon Schubert }
2665796c8dcSSimon Schubert 
2675796c8dcSSimon Schubert /* Return type of the value.  */
2685796c8dcSSimon Schubert static PyObject *
valpy_get_type(PyObject * self,void * closure)2695796c8dcSSimon Schubert valpy_get_type (PyObject *self, void *closure)
2705796c8dcSSimon Schubert {
2715796c8dcSSimon Schubert   value_object *obj = (value_object *) self;
272cf7f2e2dSJohn Marino 
2735796c8dcSSimon Schubert   if (!obj->type)
2745796c8dcSSimon Schubert     {
2755796c8dcSSimon Schubert       obj->type = type_to_type_object (value_type (obj->value));
2765796c8dcSSimon Schubert       if (!obj->type)
277c50c785cSJohn Marino 	return NULL;
2785796c8dcSSimon Schubert     }
2795796c8dcSSimon Schubert   Py_INCREF (obj->type);
2805796c8dcSSimon Schubert   return obj->type;
2815796c8dcSSimon Schubert }
2825796c8dcSSimon Schubert 
283c50c785cSJohn Marino /* Return dynamic type of the value.  */
284c50c785cSJohn Marino 
285c50c785cSJohn Marino static PyObject *
valpy_get_dynamic_type(PyObject * self,void * closure)286c50c785cSJohn Marino valpy_get_dynamic_type (PyObject *self, void *closure)
287c50c785cSJohn Marino {
288c50c785cSJohn Marino   value_object *obj = (value_object *) self;
289c50c785cSJohn Marino   volatile struct gdb_exception except;
290c50c785cSJohn Marino   struct type *type = NULL;
291c50c785cSJohn Marino 
292c50c785cSJohn Marino   if (obj->dynamic_type != NULL)
293c50c785cSJohn Marino     {
294c50c785cSJohn Marino       Py_INCREF (obj->dynamic_type);
295c50c785cSJohn Marino       return obj->dynamic_type;
296c50c785cSJohn Marino     }
297c50c785cSJohn Marino 
298c50c785cSJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
299c50c785cSJohn Marino     {
300c50c785cSJohn Marino       struct value *val = obj->value;
301*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
302c50c785cSJohn Marino 
303c50c785cSJohn Marino       type = value_type (val);
304c50c785cSJohn Marino       CHECK_TYPEDEF (type);
305c50c785cSJohn Marino 
306c50c785cSJohn Marino       if (((TYPE_CODE (type) == TYPE_CODE_PTR)
307c50c785cSJohn Marino 	   || (TYPE_CODE (type) == TYPE_CODE_REF))
308c50c785cSJohn Marino 	  && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
309c50c785cSJohn Marino 	{
310c50c785cSJohn Marino 	  struct value *target;
311c50c785cSJohn Marino 	  int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
312c50c785cSJohn Marino 
313c50c785cSJohn Marino 	  target = value_ind (val);
314c50c785cSJohn Marino 	  type = value_rtti_type (target, NULL, NULL, NULL);
315c50c785cSJohn Marino 
316c50c785cSJohn Marino 	  if (type)
317c50c785cSJohn Marino 	    {
318c50c785cSJohn Marino 	      if (was_pointer)
319c50c785cSJohn Marino 		type = lookup_pointer_type (type);
320c50c785cSJohn Marino 	      else
321c50c785cSJohn Marino 		type = lookup_reference_type (type);
322c50c785cSJohn Marino 	    }
323c50c785cSJohn Marino 	}
324c50c785cSJohn Marino       else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
325c50c785cSJohn Marino 	type = value_rtti_type (val, NULL, NULL, NULL);
326c50c785cSJohn Marino       else
327c50c785cSJohn Marino 	{
328c50c785cSJohn Marino 	  /* Re-use object's static type.  */
329c50c785cSJohn Marino 	  type = NULL;
330c50c785cSJohn Marino 	}
331*ef5ccd6cSJohn Marino 
332*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
333c50c785cSJohn Marino     }
334c50c785cSJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
335c50c785cSJohn Marino 
336c50c785cSJohn Marino   if (type == NULL)
337c50c785cSJohn Marino     {
338c50c785cSJohn Marino       /* Ensure that the TYPE field is ready.  */
339c50c785cSJohn Marino       if (!valpy_get_type (self, NULL))
340c50c785cSJohn Marino 	return NULL;
341c50c785cSJohn Marino       /* We don't need to incref here, because valpy_get_type already
342c50c785cSJohn Marino 	 did it for us.  */
343c50c785cSJohn Marino       obj->dynamic_type = obj->type;
344c50c785cSJohn Marino     }
345c50c785cSJohn Marino   else
346c50c785cSJohn Marino     obj->dynamic_type = type_to_type_object (type);
347c50c785cSJohn Marino 
348c50c785cSJohn Marino   Py_INCREF (obj->dynamic_type);
349c50c785cSJohn Marino   return obj->dynamic_type;
350c50c785cSJohn Marino }
351c50c785cSJohn Marino 
352cf7f2e2dSJohn Marino /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
353cf7f2e2dSJohn Marino    string.  Return a PyObject representing a lazy_string_object type.
354cf7f2e2dSJohn Marino    A lazy string is a pointer to a string with an optional encoding and
355cf7f2e2dSJohn Marino    length.  If ENCODING is not given, encoding is set to None.  If an
356cf7f2e2dSJohn Marino    ENCODING is provided the encoding parameter is set to ENCODING, but
357cf7f2e2dSJohn Marino    the string is not encoded.  If LENGTH is provided then the length
358cf7f2e2dSJohn Marino    parameter is set to LENGTH, otherwise length will be set to -1 (first
359cf7f2e2dSJohn Marino    null of appropriate with).  */
360cf7f2e2dSJohn Marino static PyObject *
valpy_lazy_string(PyObject * self,PyObject * args,PyObject * kw)361cf7f2e2dSJohn Marino valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
362cf7f2e2dSJohn Marino {
363c50c785cSJohn Marino   gdb_py_longest length = -1;
364cf7f2e2dSJohn Marino   struct value *value = ((value_object *) self)->value;
365cf7f2e2dSJohn Marino   const char *user_encoding = NULL;
366cf7f2e2dSJohn Marino   static char *keywords[] = { "encoding", "length", NULL };
367*ef5ccd6cSJohn Marino   PyObject *str_obj = NULL;
368a45ae5f8SJohn Marino   volatile struct gdb_exception except;
369cf7f2e2dSJohn Marino 
370c50c785cSJohn Marino   if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords,
371cf7f2e2dSJohn Marino 				    &user_encoding, &length))
372cf7f2e2dSJohn Marino     return NULL;
373cf7f2e2dSJohn Marino 
374a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
375a45ae5f8SJohn Marino     {
376*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
377*ef5ccd6cSJohn Marino 
378cf7f2e2dSJohn Marino       if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
379cf7f2e2dSJohn Marino 	value = value_ind (value);
380cf7f2e2dSJohn Marino 
381cf7f2e2dSJohn Marino       str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
382c50c785cSJohn Marino 						 user_encoding,
383c50c785cSJohn Marino 						 value_type (value));
384cf7f2e2dSJohn Marino 
385*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
386*ef5ccd6cSJohn Marino     }
387*ef5ccd6cSJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
388*ef5ccd6cSJohn Marino 
389*ef5ccd6cSJohn Marino   return str_obj;
390cf7f2e2dSJohn Marino }
391cf7f2e2dSJohn Marino 
3925796c8dcSSimon Schubert /* Implementation of gdb.Value.string ([encoding] [, errors]
3935796c8dcSSimon Schubert    [, length]) -> string.  Return Unicode string with value contents.
3945796c8dcSSimon Schubert    If ENCODING is not given, the string is assumed to be encoded in
3955796c8dcSSimon Schubert    the target's charset.  If LENGTH is provided, only fetch string to
3965796c8dcSSimon Schubert    the length provided.  */
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert static PyObject *
valpy_string(PyObject * self,PyObject * args,PyObject * kw)3995796c8dcSSimon Schubert valpy_string (PyObject *self, PyObject *args, PyObject *kw)
4005796c8dcSSimon Schubert {
401cf7f2e2dSJohn Marino   int length = -1;
4025796c8dcSSimon Schubert   gdb_byte *buffer;
4035796c8dcSSimon Schubert   struct value *value = ((value_object *) self)->value;
4045796c8dcSSimon Schubert   volatile struct gdb_exception except;
4055796c8dcSSimon Schubert   PyObject *unicode;
4065796c8dcSSimon Schubert   const char *encoding = NULL;
4075796c8dcSSimon Schubert   const char *errors = NULL;
4085796c8dcSSimon Schubert   const char *user_encoding = NULL;
4095796c8dcSSimon Schubert   const char *la_encoding = NULL;
410cf7f2e2dSJohn Marino   struct type *char_type;
4115796c8dcSSimon Schubert   static char *keywords[] = { "encoding", "errors", "length", NULL };
4125796c8dcSSimon Schubert 
4135796c8dcSSimon Schubert   if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
4145796c8dcSSimon Schubert 				    &user_encoding, &errors, &length))
4155796c8dcSSimon Schubert     return NULL;
4165796c8dcSSimon Schubert 
4175796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
4185796c8dcSSimon Schubert     {
419cf7f2e2dSJohn Marino       LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
4205796c8dcSSimon Schubert     }
4215796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
4225796c8dcSSimon Schubert 
4235796c8dcSSimon Schubert   encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
424cf7f2e2dSJohn Marino   unicode = PyUnicode_Decode (buffer, length * TYPE_LENGTH (char_type),
425cf7f2e2dSJohn Marino 			      encoding, errors);
4265796c8dcSSimon Schubert   xfree (buffer);
4275796c8dcSSimon Schubert 
4285796c8dcSSimon Schubert   return unicode;
4295796c8dcSSimon Schubert }
4305796c8dcSSimon Schubert 
431c50c785cSJohn Marino /* A helper function that implements the various cast operators.  */
432c50c785cSJohn Marino 
4335796c8dcSSimon Schubert static PyObject *
valpy_do_cast(PyObject * self,PyObject * args,enum exp_opcode op)434c50c785cSJohn Marino valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
4355796c8dcSSimon Schubert {
436*ef5ccd6cSJohn Marino   PyObject *type_obj, *result = NULL;
4375796c8dcSSimon Schubert   struct type *type;
4385796c8dcSSimon Schubert   volatile struct gdb_exception except;
4395796c8dcSSimon Schubert 
4405796c8dcSSimon Schubert   if (! PyArg_ParseTuple (args, "O", &type_obj))
4415796c8dcSSimon Schubert     return NULL;
4425796c8dcSSimon Schubert 
4435796c8dcSSimon Schubert   type = type_object_to_type (type_obj);
4445796c8dcSSimon Schubert   if (! type)
4455796c8dcSSimon Schubert     {
446cf7f2e2dSJohn Marino       PyErr_SetString (PyExc_RuntimeError,
447cf7f2e2dSJohn Marino 		       _("Argument must be a type."));
4485796c8dcSSimon Schubert       return NULL;
4495796c8dcSSimon Schubert     }
4505796c8dcSSimon Schubert 
4515796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
4525796c8dcSSimon Schubert     {
453c50c785cSJohn Marino       struct value *val = ((value_object *) self)->value;
454*ef5ccd6cSJohn Marino       struct value *res_val;
455*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
456c50c785cSJohn Marino 
457c50c785cSJohn Marino       if (op == UNOP_DYNAMIC_CAST)
458c50c785cSJohn Marino 	res_val = value_dynamic_cast (type, val);
459c50c785cSJohn Marino       else if (op == UNOP_REINTERPRET_CAST)
460c50c785cSJohn Marino 	res_val = value_reinterpret_cast (type, val);
461c50c785cSJohn Marino       else
462c50c785cSJohn Marino 	{
463c50c785cSJohn Marino 	  gdb_assert (op == UNOP_CAST);
464c50c785cSJohn Marino 	  res_val = value_cast (type, val);
465c50c785cSJohn Marino 	}
466*ef5ccd6cSJohn Marino 
467*ef5ccd6cSJohn Marino       result = value_to_value_object (res_val);
468*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
4695796c8dcSSimon Schubert     }
4705796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
4715796c8dcSSimon Schubert 
472*ef5ccd6cSJohn Marino   return result;
4735796c8dcSSimon Schubert }
4745796c8dcSSimon Schubert 
475c50c785cSJohn Marino /* Implementation of the "cast" method.  */
476c50c785cSJohn Marino 
477c50c785cSJohn Marino static PyObject *
valpy_cast(PyObject * self,PyObject * args)478c50c785cSJohn Marino valpy_cast (PyObject *self, PyObject *args)
479c50c785cSJohn Marino {
480c50c785cSJohn Marino   return valpy_do_cast (self, args, UNOP_CAST);
481c50c785cSJohn Marino }
482c50c785cSJohn Marino 
483c50c785cSJohn Marino /* Implementation of the "dynamic_cast" method.  */
484c50c785cSJohn Marino 
485c50c785cSJohn Marino static PyObject *
valpy_dynamic_cast(PyObject * self,PyObject * args)486c50c785cSJohn Marino valpy_dynamic_cast (PyObject *self, PyObject *args)
487c50c785cSJohn Marino {
488c50c785cSJohn Marino   return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
489c50c785cSJohn Marino }
490c50c785cSJohn Marino 
491c50c785cSJohn Marino /* Implementation of the "reinterpret_cast" method.  */
492c50c785cSJohn Marino 
493c50c785cSJohn Marino static PyObject *
valpy_reinterpret_cast(PyObject * self,PyObject * args)494c50c785cSJohn Marino valpy_reinterpret_cast (PyObject *self, PyObject *args)
495c50c785cSJohn Marino {
496c50c785cSJohn Marino   return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
497c50c785cSJohn Marino }
498c50c785cSJohn Marino 
4995796c8dcSSimon Schubert static Py_ssize_t
valpy_length(PyObject * self)5005796c8dcSSimon Schubert valpy_length (PyObject *self)
5015796c8dcSSimon Schubert {
5025796c8dcSSimon Schubert   /* We don't support getting the number of elements in a struct / class.  */
5035796c8dcSSimon Schubert   PyErr_SetString (PyExc_NotImplementedError,
504cf7f2e2dSJohn Marino 		   _("Invalid operation on gdb.Value."));
5055796c8dcSSimon Schubert   return -1;
5065796c8dcSSimon Schubert }
5075796c8dcSSimon Schubert 
5085796c8dcSSimon Schubert /* Given string name of an element inside structure, return its value
509c50c785cSJohn Marino    object.  Returns NULL on error, with a python exception set.  */
5105796c8dcSSimon Schubert static PyObject *
valpy_getitem(PyObject * self,PyObject * key)5115796c8dcSSimon Schubert valpy_getitem (PyObject *self, PyObject *key)
5125796c8dcSSimon Schubert {
5135796c8dcSSimon Schubert   value_object *self_value = (value_object *) self;
5145796c8dcSSimon Schubert   char *field = NULL;
5155796c8dcSSimon Schubert   volatile struct gdb_exception except;
516*ef5ccd6cSJohn Marino   PyObject *result = NULL;
5175796c8dcSSimon Schubert 
5185796c8dcSSimon Schubert   if (gdbpy_is_string (key))
5195796c8dcSSimon Schubert     {
5205796c8dcSSimon Schubert       field = python_string_to_host_string (key);
5215796c8dcSSimon Schubert       if (field == NULL)
5225796c8dcSSimon Schubert 	return NULL;
5235796c8dcSSimon Schubert     }
5245796c8dcSSimon Schubert 
5255796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
5265796c8dcSSimon Schubert     {
5275796c8dcSSimon Schubert       struct value *tmp = self_value->value;
528*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
529*ef5ccd6cSJohn Marino       struct value *res_val = NULL;
5305796c8dcSSimon Schubert 
5315796c8dcSSimon Schubert       if (field)
5325796c8dcSSimon Schubert 	res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
5335796c8dcSSimon Schubert       else
5345796c8dcSSimon Schubert 	{
5355796c8dcSSimon Schubert 	  /* Assume we are attempting an array access, and let the
5365796c8dcSSimon Schubert 	     value code throw an exception if the index has an invalid
5375796c8dcSSimon Schubert 	     type.  */
5385796c8dcSSimon Schubert 	  struct value *idx = convert_value_from_python (key);
539cf7f2e2dSJohn Marino 
5405796c8dcSSimon Schubert 	  if (idx != NULL)
5415796c8dcSSimon Schubert 	    {
5425796c8dcSSimon Schubert 	      /* Check the value's type is something that can be accessed via
5435796c8dcSSimon Schubert 		 a subscript.  */
5445796c8dcSSimon Schubert 	      struct type *type;
545cf7f2e2dSJohn Marino 
5465796c8dcSSimon Schubert 	      tmp = coerce_ref (tmp);
5475796c8dcSSimon Schubert 	      type = check_typedef (value_type (tmp));
5485796c8dcSSimon Schubert 	      if (TYPE_CODE (type) != TYPE_CODE_ARRAY
5495796c8dcSSimon Schubert 		  && TYPE_CODE (type) != TYPE_CODE_PTR)
550cf7f2e2dSJohn Marino 		  error (_("Cannot subscript requested type."));
5515796c8dcSSimon Schubert 	      else
5525796c8dcSSimon Schubert 		res_val = value_subscript (tmp, value_as_long (idx));
5535796c8dcSSimon Schubert 	    }
5545796c8dcSSimon Schubert 	}
555*ef5ccd6cSJohn Marino 
556*ef5ccd6cSJohn Marino       if (res_val)
557*ef5ccd6cSJohn Marino 	result = value_to_value_object (res_val);
558*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
5595796c8dcSSimon Schubert     }
5605796c8dcSSimon Schubert 
5615796c8dcSSimon Schubert   xfree (field);
5625796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
5635796c8dcSSimon Schubert 
564*ef5ccd6cSJohn Marino   return result;
5655796c8dcSSimon Schubert }
5665796c8dcSSimon Schubert 
5675796c8dcSSimon Schubert static int
valpy_setitem(PyObject * self,PyObject * key,PyObject * value)5685796c8dcSSimon Schubert valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
5695796c8dcSSimon Schubert {
5705796c8dcSSimon Schubert   PyErr_Format (PyExc_NotImplementedError,
5715796c8dcSSimon Schubert 		_("Setting of struct elements is not currently supported."));
5725796c8dcSSimon Schubert   return -1;
5735796c8dcSSimon Schubert }
5745796c8dcSSimon Schubert 
575c50c785cSJohn Marino /* Called by the Python interpreter to perform an inferior function
576c50c785cSJohn Marino    call on the value.  Returns NULL on error, with a python exception set.  */
577c50c785cSJohn Marino static PyObject *
valpy_call(PyObject * self,PyObject * args,PyObject * keywords)578c50c785cSJohn Marino valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
579c50c785cSJohn Marino {
580c50c785cSJohn Marino   Py_ssize_t args_count;
581c50c785cSJohn Marino   volatile struct gdb_exception except;
582c50c785cSJohn Marino   struct value *function = ((value_object *) self)->value;
583c50c785cSJohn Marino   struct value **vargs = NULL;
584a45ae5f8SJohn Marino   struct type *ftype = NULL;
585*ef5ccd6cSJohn Marino   struct value *mark = value_mark ();
586*ef5ccd6cSJohn Marino   PyObject *result = NULL;
587a45ae5f8SJohn Marino 
588a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
589a45ae5f8SJohn Marino     {
590a45ae5f8SJohn Marino       ftype = check_typedef (value_type (function));
591a45ae5f8SJohn Marino     }
592a45ae5f8SJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
593c50c785cSJohn Marino 
594c50c785cSJohn Marino   if (TYPE_CODE (ftype) != TYPE_CODE_FUNC)
595c50c785cSJohn Marino     {
596c50c785cSJohn Marino       PyErr_SetString (PyExc_RuntimeError,
597c50c785cSJohn Marino 		       _("Value is not callable (not TYPE_CODE_FUNC)."));
598c50c785cSJohn Marino       return NULL;
599c50c785cSJohn Marino     }
600c50c785cSJohn Marino 
601a45ae5f8SJohn Marino   if (! PyTuple_Check (args))
602a45ae5f8SJohn Marino     {
603a45ae5f8SJohn Marino       PyErr_SetString (PyExc_TypeError,
604a45ae5f8SJohn Marino 		       _("Inferior arguments must be provided in a tuple."));
605a45ae5f8SJohn Marino       return NULL;
606a45ae5f8SJohn Marino     }
607a45ae5f8SJohn Marino 
608c50c785cSJohn Marino   args_count = PyTuple_Size (args);
609c50c785cSJohn Marino   if (args_count > 0)
610c50c785cSJohn Marino     {
611c50c785cSJohn Marino       int i;
612c50c785cSJohn Marino 
613c50c785cSJohn Marino       vargs = alloca (sizeof (struct value *) * args_count);
614c50c785cSJohn Marino       for (i = 0; i < args_count; i++)
615c50c785cSJohn Marino 	{
616c50c785cSJohn Marino 	  PyObject *item = PyTuple_GetItem (args, i);
617c50c785cSJohn Marino 
618c50c785cSJohn Marino 	  if (item == NULL)
619c50c785cSJohn Marino 	    return NULL;
620c50c785cSJohn Marino 
621c50c785cSJohn Marino 	  vargs[i] = convert_value_from_python (item);
622c50c785cSJohn Marino 	  if (vargs[i] == NULL)
623c50c785cSJohn Marino 	    return NULL;
624c50c785cSJohn Marino 	}
625c50c785cSJohn Marino     }
626c50c785cSJohn Marino 
627c50c785cSJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
628c50c785cSJohn Marino     {
629*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark);
630*ef5ccd6cSJohn Marino       struct value *return_value;
631*ef5ccd6cSJohn Marino 
632c50c785cSJohn Marino       return_value = call_function_by_hand (function, args_count, vargs);
633*ef5ccd6cSJohn Marino       result = value_to_value_object (return_value);
634*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
635c50c785cSJohn Marino     }
636c50c785cSJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
637c50c785cSJohn Marino 
638*ef5ccd6cSJohn Marino   return result;
639c50c785cSJohn Marino }
640c50c785cSJohn Marino 
6415796c8dcSSimon Schubert /* Called by the Python interpreter to obtain string representation
6425796c8dcSSimon Schubert    of the object.  */
6435796c8dcSSimon Schubert static PyObject *
valpy_str(PyObject * self)6445796c8dcSSimon Schubert valpy_str (PyObject *self)
6455796c8dcSSimon Schubert {
6465796c8dcSSimon Schubert   char *s = NULL;
6475796c8dcSSimon Schubert   PyObject *result;
6485796c8dcSSimon Schubert   struct value_print_options opts;
6495796c8dcSSimon Schubert   volatile struct gdb_exception except;
6505796c8dcSSimon Schubert 
6515796c8dcSSimon Schubert   get_user_print_options (&opts);
6525796c8dcSSimon Schubert   opts.deref_ref = 0;
6535796c8dcSSimon Schubert 
6545796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
6555796c8dcSSimon Schubert     {
656a45ae5f8SJohn Marino       struct ui_file *stb = mem_fileopen ();
657a45ae5f8SJohn Marino       struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
658a45ae5f8SJohn Marino 
6595796c8dcSSimon Schubert       common_val_print (((value_object *) self)->value, stb, 0,
6605796c8dcSSimon Schubert 			&opts, python_language);
6615796c8dcSSimon Schubert       s = ui_file_xstrdup (stb, NULL);
6625796c8dcSSimon Schubert 
6635796c8dcSSimon Schubert       do_cleanups (old_chain);
664a45ae5f8SJohn Marino     }
665a45ae5f8SJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
6665796c8dcSSimon Schubert 
6675796c8dcSSimon Schubert   result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
6685796c8dcSSimon Schubert   xfree (s);
6695796c8dcSSimon Schubert 
6705796c8dcSSimon Schubert   return result;
6715796c8dcSSimon Schubert }
6725796c8dcSSimon Schubert 
6735796c8dcSSimon Schubert /* Implements gdb.Value.is_optimized_out.  */
6745796c8dcSSimon Schubert static PyObject *
valpy_get_is_optimized_out(PyObject * self,void * closure)6755796c8dcSSimon Schubert valpy_get_is_optimized_out (PyObject *self, void *closure)
6765796c8dcSSimon Schubert {
6775796c8dcSSimon Schubert   struct value *value = ((value_object *) self)->value;
678a45ae5f8SJohn Marino   int opt = 0;
679a45ae5f8SJohn Marino   volatile struct gdb_exception except;
6805796c8dcSSimon Schubert 
681a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
682a45ae5f8SJohn Marino     {
683a45ae5f8SJohn Marino       opt = value_optimized_out (value);
684a45ae5f8SJohn Marino     }
685a45ae5f8SJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
686a45ae5f8SJohn Marino 
687a45ae5f8SJohn Marino   if (opt)
6885796c8dcSSimon Schubert     Py_RETURN_TRUE;
6895796c8dcSSimon Schubert 
6905796c8dcSSimon Schubert   Py_RETURN_FALSE;
6915796c8dcSSimon Schubert }
6925796c8dcSSimon Schubert 
693a45ae5f8SJohn Marino /* Implements gdb.Value.is_lazy.  */
694a45ae5f8SJohn Marino static PyObject *
valpy_get_is_lazy(PyObject * self,void * closure)695a45ae5f8SJohn Marino valpy_get_is_lazy (PyObject *self, void *closure)
696a45ae5f8SJohn Marino {
697a45ae5f8SJohn Marino   struct value *value = ((value_object *) self)->value;
698a45ae5f8SJohn Marino   int opt = 0;
699a45ae5f8SJohn Marino   volatile struct gdb_exception except;
700a45ae5f8SJohn Marino 
701a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
702a45ae5f8SJohn Marino     {
703a45ae5f8SJohn Marino       opt = value_lazy (value);
704a45ae5f8SJohn Marino     }
705a45ae5f8SJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
706a45ae5f8SJohn Marino 
707a45ae5f8SJohn Marino   if (opt)
708a45ae5f8SJohn Marino     Py_RETURN_TRUE;
709a45ae5f8SJohn Marino 
710a45ae5f8SJohn Marino   Py_RETURN_FALSE;
711a45ae5f8SJohn Marino }
712a45ae5f8SJohn Marino 
713a45ae5f8SJohn Marino /* Implements gdb.Value.fetch_lazy ().  */
714a45ae5f8SJohn Marino static PyObject *
valpy_fetch_lazy(PyObject * self,PyObject * args)715a45ae5f8SJohn Marino valpy_fetch_lazy (PyObject *self, PyObject *args)
716a45ae5f8SJohn Marino {
717a45ae5f8SJohn Marino   struct value *value = ((value_object *) self)->value;
718a45ae5f8SJohn Marino   volatile struct gdb_exception except;
719a45ae5f8SJohn Marino 
720a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
721a45ae5f8SJohn Marino     {
722a45ae5f8SJohn Marino       if (value_lazy (value))
723a45ae5f8SJohn Marino 	value_fetch_lazy (value);
724a45ae5f8SJohn Marino     }
725a45ae5f8SJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
726a45ae5f8SJohn Marino 
727a45ae5f8SJohn Marino   Py_RETURN_NONE;
728a45ae5f8SJohn Marino }
729a45ae5f8SJohn Marino 
730cf7f2e2dSJohn Marino /* Calculate and return the address of the PyObject as the value of
731cf7f2e2dSJohn Marino    the builtin __hash__ call.  */
732cf7f2e2dSJohn Marino static long
valpy_hash(PyObject * self)733cf7f2e2dSJohn Marino valpy_hash (PyObject *self)
734cf7f2e2dSJohn Marino {
735cf7f2e2dSJohn Marino   return (long) (intptr_t) self;
736cf7f2e2dSJohn Marino }
737cf7f2e2dSJohn Marino 
7385796c8dcSSimon Schubert enum valpy_opcode
7395796c8dcSSimon Schubert {
7405796c8dcSSimon Schubert   VALPY_ADD,
7415796c8dcSSimon Schubert   VALPY_SUB,
7425796c8dcSSimon Schubert   VALPY_MUL,
7435796c8dcSSimon Schubert   VALPY_DIV,
7445796c8dcSSimon Schubert   VALPY_REM,
7455796c8dcSSimon Schubert   VALPY_POW,
7465796c8dcSSimon Schubert   VALPY_LSH,
7475796c8dcSSimon Schubert   VALPY_RSH,
7485796c8dcSSimon Schubert   VALPY_BITAND,
7495796c8dcSSimon Schubert   VALPY_BITOR,
7505796c8dcSSimon Schubert   VALPY_BITXOR
7515796c8dcSSimon Schubert };
7525796c8dcSSimon Schubert 
7535796c8dcSSimon Schubert /* If TYPE is a reference, return the target; otherwise return TYPE.  */
7545796c8dcSSimon Schubert #define STRIP_REFERENCE(TYPE) \
7555796c8dcSSimon Schubert   ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
7565796c8dcSSimon Schubert 
7575796c8dcSSimon Schubert /* Returns a value object which is the result of applying the operation
758c50c785cSJohn Marino    specified by OPCODE to the given arguments.  Returns NULL on error, with
759c50c785cSJohn Marino    a python exception set.  */
7605796c8dcSSimon Schubert static PyObject *
valpy_binop(enum valpy_opcode opcode,PyObject * self,PyObject * other)7615796c8dcSSimon Schubert valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
7625796c8dcSSimon Schubert {
7635796c8dcSSimon Schubert   volatile struct gdb_exception except;
764*ef5ccd6cSJohn Marino   PyObject *result = NULL;
7655796c8dcSSimon Schubert 
7665796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
7675796c8dcSSimon Schubert     {
7685796c8dcSSimon Schubert       struct value *arg1, *arg2;
769*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
770*ef5ccd6cSJohn Marino       struct value *res_val = NULL;
7715796c8dcSSimon Schubert 
7725796c8dcSSimon Schubert       /* If the gdb.Value object is the second operand, then it will be passed
7735796c8dcSSimon Schubert 	 to us as the OTHER argument, and SELF will be an entirely different
7745796c8dcSSimon Schubert 	 kind of object, altogether.  Because of this, we can't assume self is
7755796c8dcSSimon Schubert 	 a gdb.Value object and need to convert it from python as well.  */
7765796c8dcSSimon Schubert       arg1 = convert_value_from_python (self);
7775796c8dcSSimon Schubert       if (arg1 == NULL)
7785796c8dcSSimon Schubert 	break;
7795796c8dcSSimon Schubert 
7805796c8dcSSimon Schubert       arg2 = convert_value_from_python (other);
7815796c8dcSSimon Schubert       if (arg2 == NULL)
7825796c8dcSSimon Schubert 	break;
7835796c8dcSSimon Schubert 
7845796c8dcSSimon Schubert       switch (opcode)
7855796c8dcSSimon Schubert 	{
7865796c8dcSSimon Schubert 	case VALPY_ADD:
7875796c8dcSSimon Schubert 	  {
7885796c8dcSSimon Schubert 	    struct type *ltype = value_type (arg1);
7895796c8dcSSimon Schubert 	    struct type *rtype = value_type (arg2);
7905796c8dcSSimon Schubert 
7915796c8dcSSimon Schubert 	    CHECK_TYPEDEF (ltype);
7925796c8dcSSimon Schubert 	    ltype = STRIP_REFERENCE (ltype);
7935796c8dcSSimon Schubert 	    CHECK_TYPEDEF (rtype);
7945796c8dcSSimon Schubert 	    rtype = STRIP_REFERENCE (rtype);
7955796c8dcSSimon Schubert 
7965796c8dcSSimon Schubert 	    if (TYPE_CODE (ltype) == TYPE_CODE_PTR
7975796c8dcSSimon Schubert 		&& is_integral_type (rtype))
7985796c8dcSSimon Schubert 	      res_val = value_ptradd (arg1, value_as_long (arg2));
7995796c8dcSSimon Schubert 	    else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
8005796c8dcSSimon Schubert 		     && is_integral_type (ltype))
8015796c8dcSSimon Schubert 	      res_val = value_ptradd (arg2, value_as_long (arg1));
8025796c8dcSSimon Schubert 	    else
8035796c8dcSSimon Schubert 	      res_val = value_binop (arg1, arg2, BINOP_ADD);
8045796c8dcSSimon Schubert 	  }
8055796c8dcSSimon Schubert 	  break;
8065796c8dcSSimon Schubert 	case VALPY_SUB:
8075796c8dcSSimon Schubert 	  {
8085796c8dcSSimon Schubert 	    struct type *ltype = value_type (arg1);
8095796c8dcSSimon Schubert 	    struct type *rtype = value_type (arg2);
8105796c8dcSSimon Schubert 
8115796c8dcSSimon Schubert 	    CHECK_TYPEDEF (ltype);
8125796c8dcSSimon Schubert 	    ltype = STRIP_REFERENCE (ltype);
8135796c8dcSSimon Schubert 	    CHECK_TYPEDEF (rtype);
8145796c8dcSSimon Schubert 	    rtype = STRIP_REFERENCE (rtype);
8155796c8dcSSimon Schubert 
8165796c8dcSSimon Schubert 	    if (TYPE_CODE (ltype) == TYPE_CODE_PTR
8175796c8dcSSimon Schubert 		&& TYPE_CODE (rtype) == TYPE_CODE_PTR)
8185796c8dcSSimon Schubert 	      /* A ptrdiff_t for the target would be preferable here.  */
8195796c8dcSSimon Schubert 	      res_val = value_from_longest (builtin_type_pyint,
8205796c8dcSSimon Schubert 					    value_ptrdiff (arg1, arg2));
8215796c8dcSSimon Schubert 	    else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
8225796c8dcSSimon Schubert 		     && is_integral_type (rtype))
8235796c8dcSSimon Schubert 	      res_val = value_ptradd (arg1, - value_as_long (arg2));
8245796c8dcSSimon Schubert 	    else
8255796c8dcSSimon Schubert 	      res_val = value_binop (arg1, arg2, BINOP_SUB);
8265796c8dcSSimon Schubert 	  }
8275796c8dcSSimon Schubert 	  break;
8285796c8dcSSimon Schubert 	case VALPY_MUL:
8295796c8dcSSimon Schubert 	  res_val = value_binop (arg1, arg2, BINOP_MUL);
8305796c8dcSSimon Schubert 	  break;
8315796c8dcSSimon Schubert 	case VALPY_DIV:
8325796c8dcSSimon Schubert 	  res_val = value_binop (arg1, arg2, BINOP_DIV);
8335796c8dcSSimon Schubert 	  break;
8345796c8dcSSimon Schubert 	case VALPY_REM:
8355796c8dcSSimon Schubert 	  res_val = value_binop (arg1, arg2, BINOP_REM);
8365796c8dcSSimon Schubert 	  break;
8375796c8dcSSimon Schubert 	case VALPY_POW:
8385796c8dcSSimon Schubert 	  res_val = value_binop (arg1, arg2, BINOP_EXP);
8395796c8dcSSimon Schubert 	  break;
8405796c8dcSSimon Schubert 	case VALPY_LSH:
8415796c8dcSSimon Schubert 	  res_val = value_binop (arg1, arg2, BINOP_LSH);
8425796c8dcSSimon Schubert 	  break;
8435796c8dcSSimon Schubert 	case VALPY_RSH:
8445796c8dcSSimon Schubert 	  res_val = value_binop (arg1, arg2, BINOP_RSH);
8455796c8dcSSimon Schubert 	  break;
8465796c8dcSSimon Schubert 	case VALPY_BITAND:
8475796c8dcSSimon Schubert 	  res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
8485796c8dcSSimon Schubert 	  break;
8495796c8dcSSimon Schubert 	case VALPY_BITOR:
8505796c8dcSSimon Schubert 	  res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
8515796c8dcSSimon Schubert 	  break;
8525796c8dcSSimon Schubert 	case VALPY_BITXOR:
8535796c8dcSSimon Schubert 	  res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
8545796c8dcSSimon Schubert 	  break;
8555796c8dcSSimon Schubert 	}
856*ef5ccd6cSJohn Marino 
857*ef5ccd6cSJohn Marino       if (res_val)
858*ef5ccd6cSJohn Marino 	result = value_to_value_object (res_val);
859*ef5ccd6cSJohn Marino 
860*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
8615796c8dcSSimon Schubert     }
8625796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
8635796c8dcSSimon Schubert 
864*ef5ccd6cSJohn Marino   return result;
8655796c8dcSSimon Schubert }
8665796c8dcSSimon Schubert 
8675796c8dcSSimon Schubert static PyObject *
valpy_add(PyObject * self,PyObject * other)8685796c8dcSSimon Schubert valpy_add (PyObject *self, PyObject *other)
8695796c8dcSSimon Schubert {
8705796c8dcSSimon Schubert   return valpy_binop (VALPY_ADD, self, other);
8715796c8dcSSimon Schubert }
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert static PyObject *
valpy_subtract(PyObject * self,PyObject * other)8745796c8dcSSimon Schubert valpy_subtract (PyObject *self, PyObject *other)
8755796c8dcSSimon Schubert {
8765796c8dcSSimon Schubert   return valpy_binop (VALPY_SUB, self, other);
8775796c8dcSSimon Schubert }
8785796c8dcSSimon Schubert 
8795796c8dcSSimon Schubert static PyObject *
valpy_multiply(PyObject * self,PyObject * other)8805796c8dcSSimon Schubert valpy_multiply (PyObject *self, PyObject *other)
8815796c8dcSSimon Schubert {
8825796c8dcSSimon Schubert   return valpy_binop (VALPY_MUL, self, other);
8835796c8dcSSimon Schubert }
8845796c8dcSSimon Schubert 
8855796c8dcSSimon Schubert static PyObject *
valpy_divide(PyObject * self,PyObject * other)8865796c8dcSSimon Schubert valpy_divide (PyObject *self, PyObject *other)
8875796c8dcSSimon Schubert {
8885796c8dcSSimon Schubert   return valpy_binop (VALPY_DIV, self, other);
8895796c8dcSSimon Schubert }
8905796c8dcSSimon Schubert 
8915796c8dcSSimon Schubert static PyObject *
valpy_remainder(PyObject * self,PyObject * other)8925796c8dcSSimon Schubert valpy_remainder (PyObject *self, PyObject *other)
8935796c8dcSSimon Schubert {
8945796c8dcSSimon Schubert   return valpy_binop (VALPY_REM, self, other);
8955796c8dcSSimon Schubert }
8965796c8dcSSimon Schubert 
8975796c8dcSSimon Schubert static PyObject *
valpy_power(PyObject * self,PyObject * other,PyObject * unused)8985796c8dcSSimon Schubert valpy_power (PyObject *self, PyObject *other, PyObject *unused)
8995796c8dcSSimon Schubert {
9005796c8dcSSimon Schubert   /* We don't support the ternary form of pow.  I don't know how to express
9015796c8dcSSimon Schubert      that, so let's just throw NotImplementedError to at least do something
9025796c8dcSSimon Schubert      about it.  */
9035796c8dcSSimon Schubert   if (unused != Py_None)
9045796c8dcSSimon Schubert     {
9055796c8dcSSimon Schubert       PyErr_SetString (PyExc_NotImplementedError,
9065796c8dcSSimon Schubert 		       "Invalid operation on gdb.Value.");
9075796c8dcSSimon Schubert       return NULL;
9085796c8dcSSimon Schubert     }
9095796c8dcSSimon Schubert 
9105796c8dcSSimon Schubert   return valpy_binop (VALPY_POW, self, other);
9115796c8dcSSimon Schubert }
9125796c8dcSSimon Schubert 
9135796c8dcSSimon Schubert static PyObject *
valpy_negative(PyObject * self)9145796c8dcSSimon Schubert valpy_negative (PyObject *self)
9155796c8dcSSimon Schubert {
9165796c8dcSSimon Schubert   volatile struct gdb_exception except;
917*ef5ccd6cSJohn Marino   PyObject *result = NULL;
9185796c8dcSSimon Schubert 
9195796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
9205796c8dcSSimon Schubert     {
921*ef5ccd6cSJohn Marino       /* Perhaps overkill, but consistency has some virtue.  */
922*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
923*ef5ccd6cSJohn Marino       struct value *val;
924*ef5ccd6cSJohn Marino 
9255796c8dcSSimon Schubert       val = value_neg (((value_object *) self)->value);
926*ef5ccd6cSJohn Marino       result = value_to_value_object (val);
927*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
9285796c8dcSSimon Schubert     }
9295796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
9305796c8dcSSimon Schubert 
931*ef5ccd6cSJohn Marino   return result;
9325796c8dcSSimon Schubert }
9335796c8dcSSimon Schubert 
9345796c8dcSSimon Schubert static PyObject *
valpy_positive(PyObject * self)9355796c8dcSSimon Schubert valpy_positive (PyObject *self)
9365796c8dcSSimon Schubert {
9375796c8dcSSimon Schubert   return value_to_value_object (((value_object *) self)->value);
9385796c8dcSSimon Schubert }
9395796c8dcSSimon Schubert 
9405796c8dcSSimon Schubert static PyObject *
valpy_absolute(PyObject * self)9415796c8dcSSimon Schubert valpy_absolute (PyObject *self)
9425796c8dcSSimon Schubert {
9435796c8dcSSimon Schubert   struct value *value = ((value_object *) self)->value;
944a45ae5f8SJohn Marino   volatile struct gdb_exception except;
945a45ae5f8SJohn Marino   int isabs = 1;
946cf7f2e2dSJohn Marino 
947a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
948a45ae5f8SJohn Marino     {
949*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
950*ef5ccd6cSJohn Marino 
9515796c8dcSSimon Schubert       if (value_less (value, value_zero (value_type (value), not_lval)))
952a45ae5f8SJohn Marino 	isabs = 0;
953*ef5ccd6cSJohn Marino 
954*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
955a45ae5f8SJohn Marino     }
956a45ae5f8SJohn Marino   GDB_PY_HANDLE_EXCEPTION (except);
957a45ae5f8SJohn Marino 
958a45ae5f8SJohn Marino   if (isabs)
9595796c8dcSSimon Schubert     return valpy_positive (self);
960a45ae5f8SJohn Marino   else
961a45ae5f8SJohn Marino     return valpy_negative (self);
9625796c8dcSSimon Schubert }
9635796c8dcSSimon Schubert 
9645796c8dcSSimon Schubert /* Implements boolean evaluation of gdb.Value.  */
9655796c8dcSSimon Schubert static int
valpy_nonzero(PyObject * self)9665796c8dcSSimon Schubert valpy_nonzero (PyObject *self)
9675796c8dcSSimon Schubert {
968a45ae5f8SJohn Marino   volatile struct gdb_exception except;
9695796c8dcSSimon Schubert   value_object *self_value = (value_object *) self;
9705796c8dcSSimon Schubert   struct type *type;
971a45ae5f8SJohn Marino   int nonzero = 0; /* Appease GCC warning.  */
9725796c8dcSSimon Schubert 
973a45ae5f8SJohn Marino   TRY_CATCH (except, RETURN_MASK_ALL)
974a45ae5f8SJohn Marino     {
9755796c8dcSSimon Schubert       type = check_typedef (value_type (self_value->value));
9765796c8dcSSimon Schubert 
9775796c8dcSSimon Schubert       if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
978a45ae5f8SJohn Marino 	nonzero = !!value_as_long (self_value->value);
9795796c8dcSSimon Schubert       else if (TYPE_CODE (type) == TYPE_CODE_FLT)
980a45ae5f8SJohn Marino 	nonzero = value_as_double (self_value->value) != 0;
9815796c8dcSSimon Schubert       else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
982a45ae5f8SJohn Marino 	nonzero = !decimal_is_zero (value_contents (self_value->value),
9835796c8dcSSimon Schubert 				 TYPE_LENGTH (type),
9845796c8dcSSimon Schubert 				 gdbarch_byte_order (get_type_arch (type)));
9855796c8dcSSimon Schubert       else
986c50c785cSJohn Marino 	/* All other values are True.  */
987a45ae5f8SJohn Marino 	nonzero = 1;
988a45ae5f8SJohn Marino     }
989a45ae5f8SJohn Marino   /* This is not documented in the Python documentation, but if this
990a45ae5f8SJohn Marino      function fails, return -1 as slot_nb_nonzero does (the default
991a45ae5f8SJohn Marino      Python nonzero function).  */
992a45ae5f8SJohn Marino   GDB_PY_SET_HANDLE_EXCEPTION (except);
993a45ae5f8SJohn Marino 
994a45ae5f8SJohn Marino   return nonzero;
9955796c8dcSSimon Schubert }
9965796c8dcSSimon Schubert 
9975796c8dcSSimon Schubert /* Implements ~ for value objects.  */
9985796c8dcSSimon Schubert static PyObject *
valpy_invert(PyObject * self)9995796c8dcSSimon Schubert valpy_invert (PyObject *self)
10005796c8dcSSimon Schubert {
10015796c8dcSSimon Schubert   struct value *val = NULL;
10025796c8dcSSimon Schubert   volatile struct gdb_exception except;
10035796c8dcSSimon Schubert 
10045796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
10055796c8dcSSimon Schubert     {
10065796c8dcSSimon Schubert       val = value_complement (((value_object *) self)->value);
10075796c8dcSSimon Schubert     }
10085796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
10095796c8dcSSimon Schubert 
10105796c8dcSSimon Schubert   return value_to_value_object (val);
10115796c8dcSSimon Schubert }
10125796c8dcSSimon Schubert 
10135796c8dcSSimon Schubert /* Implements left shift for value objects.  */
10145796c8dcSSimon Schubert static PyObject *
valpy_lsh(PyObject * self,PyObject * other)10155796c8dcSSimon Schubert valpy_lsh (PyObject *self, PyObject *other)
10165796c8dcSSimon Schubert {
10175796c8dcSSimon Schubert   return valpy_binop (VALPY_LSH, self, other);
10185796c8dcSSimon Schubert }
10195796c8dcSSimon Schubert 
10205796c8dcSSimon Schubert /* Implements right shift for value objects.  */
10215796c8dcSSimon Schubert static PyObject *
valpy_rsh(PyObject * self,PyObject * other)10225796c8dcSSimon Schubert valpy_rsh (PyObject *self, PyObject *other)
10235796c8dcSSimon Schubert {
10245796c8dcSSimon Schubert   return valpy_binop (VALPY_RSH, self, other);
10255796c8dcSSimon Schubert }
10265796c8dcSSimon Schubert 
10275796c8dcSSimon Schubert /* Implements bitwise and for value objects.  */
10285796c8dcSSimon Schubert static PyObject *
valpy_and(PyObject * self,PyObject * other)10295796c8dcSSimon Schubert valpy_and (PyObject *self, PyObject *other)
10305796c8dcSSimon Schubert {
10315796c8dcSSimon Schubert   return valpy_binop (VALPY_BITAND, self, other);
10325796c8dcSSimon Schubert }
10335796c8dcSSimon Schubert 
10345796c8dcSSimon Schubert /* Implements bitwise or for value objects.  */
10355796c8dcSSimon Schubert static PyObject *
valpy_or(PyObject * self,PyObject * other)10365796c8dcSSimon Schubert valpy_or (PyObject *self, PyObject *other)
10375796c8dcSSimon Schubert {
10385796c8dcSSimon Schubert   return valpy_binop (VALPY_BITOR, self, other);
10395796c8dcSSimon Schubert }
10405796c8dcSSimon Schubert 
10415796c8dcSSimon Schubert /* Implements bitwise xor for value objects.  */
10425796c8dcSSimon Schubert static PyObject *
valpy_xor(PyObject * self,PyObject * other)10435796c8dcSSimon Schubert valpy_xor (PyObject *self, PyObject *other)
10445796c8dcSSimon Schubert {
10455796c8dcSSimon Schubert   return valpy_binop (VALPY_BITXOR, self, other);
10465796c8dcSSimon Schubert }
10475796c8dcSSimon Schubert 
1048c50c785cSJohn Marino /* Implements comparison operations for value objects.  Returns NULL on error,
1049c50c785cSJohn Marino    with a python exception set.  */
10505796c8dcSSimon Schubert static PyObject *
valpy_richcompare(PyObject * self,PyObject * other,int op)10515796c8dcSSimon Schubert valpy_richcompare (PyObject *self, PyObject *other, int op)
10525796c8dcSSimon Schubert {
10535796c8dcSSimon Schubert   int result = 0;
10545796c8dcSSimon Schubert   volatile struct gdb_exception except;
10555796c8dcSSimon Schubert 
10565796c8dcSSimon Schubert   if (other == Py_None)
10575796c8dcSSimon Schubert     /* Comparing with None is special.  From what I can tell, in Python
10585796c8dcSSimon Schubert        None is smaller than anything else.  */
10595796c8dcSSimon Schubert     switch (op) {
10605796c8dcSSimon Schubert       case Py_LT:
10615796c8dcSSimon Schubert       case Py_LE:
10625796c8dcSSimon Schubert       case Py_EQ:
10635796c8dcSSimon Schubert 	Py_RETURN_FALSE;
10645796c8dcSSimon Schubert       case Py_NE:
10655796c8dcSSimon Schubert       case Py_GT:
10665796c8dcSSimon Schubert       case Py_GE:
10675796c8dcSSimon Schubert 	Py_RETURN_TRUE;
10685796c8dcSSimon Schubert       default:
10695796c8dcSSimon Schubert 	/* Can't happen.  */
10705796c8dcSSimon Schubert 	PyErr_SetString (PyExc_NotImplementedError,
1071cf7f2e2dSJohn Marino 			 _("Invalid operation on gdb.Value."));
10725796c8dcSSimon Schubert 	return NULL;
10735796c8dcSSimon Schubert     }
10745796c8dcSSimon Schubert 
10755796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
10765796c8dcSSimon Schubert     {
1077*ef5ccd6cSJohn Marino       struct value *value_other, *mark = value_mark ();
1078*ef5ccd6cSJohn Marino       struct cleanup *cleanup;
1079*ef5ccd6cSJohn Marino 
10805796c8dcSSimon Schubert       value_other = convert_value_from_python (other);
10815796c8dcSSimon Schubert       if (value_other == NULL)
10825796c8dcSSimon Schubert 	{
10835796c8dcSSimon Schubert 	  result = -1;
10845796c8dcSSimon Schubert 	  break;
10855796c8dcSSimon Schubert 	}
10865796c8dcSSimon Schubert 
1087*ef5ccd6cSJohn Marino       cleanup = make_cleanup_value_free_to_mark (mark);
1088*ef5ccd6cSJohn Marino 
10895796c8dcSSimon Schubert       switch (op) {
10905796c8dcSSimon Schubert         case Py_LT:
10915796c8dcSSimon Schubert 	  result = value_less (((value_object *) self)->value, value_other);
10925796c8dcSSimon Schubert 	  break;
10935796c8dcSSimon Schubert 	case Py_LE:
10945796c8dcSSimon Schubert 	  result = value_less (((value_object *) self)->value, value_other)
10955796c8dcSSimon Schubert 	    || value_equal (((value_object *) self)->value, value_other);
10965796c8dcSSimon Schubert 	  break;
10975796c8dcSSimon Schubert 	case Py_EQ:
10985796c8dcSSimon Schubert 	  result = value_equal (((value_object *) self)->value, value_other);
10995796c8dcSSimon Schubert 	  break;
11005796c8dcSSimon Schubert 	case Py_NE:
11015796c8dcSSimon Schubert 	  result = !value_equal (((value_object *) self)->value, value_other);
11025796c8dcSSimon Schubert 	  break;
11035796c8dcSSimon Schubert         case Py_GT:
11045796c8dcSSimon Schubert 	  result = value_less (value_other, ((value_object *) self)->value);
11055796c8dcSSimon Schubert 	  break;
11065796c8dcSSimon Schubert 	case Py_GE:
11075796c8dcSSimon Schubert 	  result = value_less (value_other, ((value_object *) self)->value)
11085796c8dcSSimon Schubert 	    || value_equal (((value_object *) self)->value, value_other);
11095796c8dcSSimon Schubert 	  break;
11105796c8dcSSimon Schubert 	default:
11115796c8dcSSimon Schubert 	  /* Can't happen.  */
11125796c8dcSSimon Schubert 	  PyErr_SetString (PyExc_NotImplementedError,
1113cf7f2e2dSJohn Marino 			   _("Invalid operation on gdb.Value."));
11145796c8dcSSimon Schubert 	  result = -1;
11155796c8dcSSimon Schubert 	  break;
11165796c8dcSSimon Schubert       }
1117*ef5ccd6cSJohn Marino 
1118*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
11195796c8dcSSimon Schubert     }
11205796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
11215796c8dcSSimon Schubert 
11225796c8dcSSimon Schubert   /* In this case, the Python exception has already been set.  */
11235796c8dcSSimon Schubert   if (result < 0)
11245796c8dcSSimon Schubert     return NULL;
11255796c8dcSSimon Schubert 
11265796c8dcSSimon Schubert   if (result == 1)
11275796c8dcSSimon Schubert     Py_RETURN_TRUE;
11285796c8dcSSimon Schubert 
11295796c8dcSSimon Schubert   Py_RETURN_FALSE;
11305796c8dcSSimon Schubert }
11315796c8dcSSimon Schubert 
11325796c8dcSSimon Schubert /* Helper function to determine if a type is "int-like".  */
11335796c8dcSSimon Schubert static int
is_intlike(struct type * type,int ptr_ok)11345796c8dcSSimon Schubert is_intlike (struct type *type, int ptr_ok)
11355796c8dcSSimon Schubert {
11365796c8dcSSimon Schubert   return (TYPE_CODE (type) == TYPE_CODE_INT
11375796c8dcSSimon Schubert 	  || TYPE_CODE (type) == TYPE_CODE_ENUM
11385796c8dcSSimon Schubert 	  || TYPE_CODE (type) == TYPE_CODE_BOOL
11395796c8dcSSimon Schubert 	  || TYPE_CODE (type) == TYPE_CODE_CHAR
11405796c8dcSSimon Schubert 	  || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
11415796c8dcSSimon Schubert }
11425796c8dcSSimon Schubert 
1143*ef5ccd6cSJohn Marino #ifndef IS_PY3K
11445796c8dcSSimon Schubert /* Implements conversion to int.  */
11455796c8dcSSimon Schubert static PyObject *
valpy_int(PyObject * self)11465796c8dcSSimon Schubert valpy_int (PyObject *self)
11475796c8dcSSimon Schubert {
11485796c8dcSSimon Schubert   struct value *value = ((value_object *) self)->value;
11495796c8dcSSimon Schubert   struct type *type = value_type (value);
11505796c8dcSSimon Schubert   LONGEST l = 0;
11515796c8dcSSimon Schubert   volatile struct gdb_exception except;
11525796c8dcSSimon Schubert 
11535796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
11545796c8dcSSimon Schubert     {
1155a45ae5f8SJohn Marino       CHECK_TYPEDEF (type);
1156a45ae5f8SJohn Marino       if (!is_intlike (type, 0))
1157a45ae5f8SJohn Marino 	error (_("Cannot convert value to int."));
1158a45ae5f8SJohn Marino 
11595796c8dcSSimon Schubert       l = value_as_long (value);
11605796c8dcSSimon Schubert     }
11615796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
11625796c8dcSSimon Schubert 
1163c50c785cSJohn Marino   return gdb_py_object_from_longest (l);
11645796c8dcSSimon Schubert }
1165*ef5ccd6cSJohn Marino #endif
11665796c8dcSSimon Schubert 
11675796c8dcSSimon Schubert /* Implements conversion to long.  */
11685796c8dcSSimon Schubert static PyObject *
valpy_long(PyObject * self)11695796c8dcSSimon Schubert valpy_long (PyObject *self)
11705796c8dcSSimon Schubert {
11715796c8dcSSimon Schubert   struct value *value = ((value_object *) self)->value;
11725796c8dcSSimon Schubert   struct type *type = value_type (value);
11735796c8dcSSimon Schubert   LONGEST l = 0;
11745796c8dcSSimon Schubert   volatile struct gdb_exception except;
11755796c8dcSSimon Schubert 
11765796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
11775796c8dcSSimon Schubert     {
1178a45ae5f8SJohn Marino       CHECK_TYPEDEF (type);
1179a45ae5f8SJohn Marino 
1180a45ae5f8SJohn Marino       if (!is_intlike (type, 1))
1181a45ae5f8SJohn Marino 	error (_("Cannot convert value to long."));
1182a45ae5f8SJohn Marino 
11835796c8dcSSimon Schubert       l = value_as_long (value);
11845796c8dcSSimon Schubert     }
11855796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
11865796c8dcSSimon Schubert 
1187c50c785cSJohn Marino   return gdb_py_long_from_longest (l);
11885796c8dcSSimon Schubert }
11895796c8dcSSimon Schubert 
11905796c8dcSSimon Schubert /* Implements conversion to float.  */
11915796c8dcSSimon Schubert static PyObject *
valpy_float(PyObject * self)11925796c8dcSSimon Schubert valpy_float (PyObject *self)
11935796c8dcSSimon Schubert {
11945796c8dcSSimon Schubert   struct value *value = ((value_object *) self)->value;
11955796c8dcSSimon Schubert   struct type *type = value_type (value);
11965796c8dcSSimon Schubert   double d = 0;
11975796c8dcSSimon Schubert   volatile struct gdb_exception except;
11985796c8dcSSimon Schubert 
11995796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
12005796c8dcSSimon Schubert     {
1201a45ae5f8SJohn Marino       CHECK_TYPEDEF (type);
1202a45ae5f8SJohn Marino 
1203a45ae5f8SJohn Marino       if (TYPE_CODE (type) != TYPE_CODE_FLT)
1204a45ae5f8SJohn Marino 	error (_("Cannot convert value to float."));
1205a45ae5f8SJohn Marino 
12065796c8dcSSimon Schubert       d = value_as_double (value);
12075796c8dcSSimon Schubert     }
12085796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
12095796c8dcSSimon Schubert 
12105796c8dcSSimon Schubert   return PyFloat_FromDouble (d);
12115796c8dcSSimon Schubert }
12125796c8dcSSimon Schubert 
12135796c8dcSSimon Schubert /* Returns an object for a value which is released from the all_values chain,
12145796c8dcSSimon Schubert    so its lifetime is not bound to the execution of a command.  */
12155796c8dcSSimon Schubert PyObject *
value_to_value_object(struct value * val)12165796c8dcSSimon Schubert value_to_value_object (struct value *val)
12175796c8dcSSimon Schubert {
12185796c8dcSSimon Schubert   value_object *val_obj;
12195796c8dcSSimon Schubert 
12205796c8dcSSimon Schubert   val_obj = PyObject_New (value_object, &value_object_type);
12215796c8dcSSimon Schubert   if (val_obj != NULL)
12225796c8dcSSimon Schubert     {
12235796c8dcSSimon Schubert       val_obj->value = val;
1224*ef5ccd6cSJohn Marino       release_value_or_incref (val);
12255796c8dcSSimon Schubert       val_obj->address = NULL;
12265796c8dcSSimon Schubert       val_obj->type = NULL;
1227c50c785cSJohn Marino       val_obj->dynamic_type = NULL;
12285796c8dcSSimon Schubert       note_value (val_obj);
12295796c8dcSSimon Schubert     }
12305796c8dcSSimon Schubert 
12315796c8dcSSimon Schubert   return (PyObject *) val_obj;
12325796c8dcSSimon Schubert }
12335796c8dcSSimon Schubert 
12345796c8dcSSimon Schubert /* Returns a borrowed reference to the struct value corresponding to
12355796c8dcSSimon Schubert    the given value object.  */
12365796c8dcSSimon Schubert struct value *
value_object_to_value(PyObject * self)12375796c8dcSSimon Schubert value_object_to_value (PyObject *self)
12385796c8dcSSimon Schubert {
12395796c8dcSSimon Schubert   value_object *real;
1240cf7f2e2dSJohn Marino 
12415796c8dcSSimon Schubert   if (! PyObject_TypeCheck (self, &value_object_type))
12425796c8dcSSimon Schubert     return NULL;
12435796c8dcSSimon Schubert   real = (value_object *) self;
12445796c8dcSSimon Schubert   return real->value;
12455796c8dcSSimon Schubert }
12465796c8dcSSimon Schubert 
12475796c8dcSSimon Schubert /* Try to convert a Python value to a gdb value.  If the value cannot
12485796c8dcSSimon Schubert    be converted, set a Python exception and return NULL.  Returns a
12495796c8dcSSimon Schubert    reference to a new value on the all_values chain.  */
12505796c8dcSSimon Schubert 
12515796c8dcSSimon Schubert struct value *
convert_value_from_python(PyObject * obj)12525796c8dcSSimon Schubert convert_value_from_python (PyObject *obj)
12535796c8dcSSimon Schubert {
12545796c8dcSSimon Schubert   struct value *value = NULL; /* -Wall */
12555796c8dcSSimon Schubert   volatile struct gdb_exception except;
12565796c8dcSSimon Schubert   int cmp;
12575796c8dcSSimon Schubert 
12585796c8dcSSimon Schubert   gdb_assert (obj != NULL);
12595796c8dcSSimon Schubert 
12605796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
12615796c8dcSSimon Schubert     {
12625796c8dcSSimon Schubert       if (PyBool_Check (obj))
12635796c8dcSSimon Schubert 	{
12645796c8dcSSimon Schubert 	  cmp = PyObject_IsTrue (obj);
12655796c8dcSSimon Schubert 	  if (cmp >= 0)
12665796c8dcSSimon Schubert 	    value = value_from_longest (builtin_type_pybool, cmp);
12675796c8dcSSimon Schubert 	}
12685796c8dcSSimon Schubert       else if (PyInt_Check (obj))
12695796c8dcSSimon Schubert 	{
12705796c8dcSSimon Schubert 	  long l = PyInt_AsLong (obj);
12715796c8dcSSimon Schubert 
12725796c8dcSSimon Schubert 	  if (! PyErr_Occurred ())
12735796c8dcSSimon Schubert 	    value = value_from_longest (builtin_type_pyint, l);
12745796c8dcSSimon Schubert 	}
12755796c8dcSSimon Schubert       else if (PyLong_Check (obj))
12765796c8dcSSimon Schubert 	{
12775796c8dcSSimon Schubert 	  LONGEST l = PyLong_AsLongLong (obj);
12785796c8dcSSimon Schubert 
1279cf7f2e2dSJohn Marino 	  if (PyErr_Occurred ())
1280cf7f2e2dSJohn Marino 	    {
1281cf7f2e2dSJohn Marino 	      /* If the error was an overflow, we can try converting to
1282cf7f2e2dSJohn Marino 	         ULONGEST instead.  */
1283cf7f2e2dSJohn Marino 	      if (PyErr_ExceptionMatches (PyExc_OverflowError))
1284cf7f2e2dSJohn Marino 		{
1285cf7f2e2dSJohn Marino 		  PyObject *etype, *evalue, *etraceback, *zero;
1286cf7f2e2dSJohn Marino 
1287cf7f2e2dSJohn Marino 		  PyErr_Fetch (&etype, &evalue, &etraceback);
1288cf7f2e2dSJohn Marino 		  zero = PyInt_FromLong (0);
1289cf7f2e2dSJohn Marino 
1290cf7f2e2dSJohn Marino 		  /* Check whether obj is positive.  */
1291cf7f2e2dSJohn Marino 		  if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0)
1292cf7f2e2dSJohn Marino 		    {
1293cf7f2e2dSJohn Marino 		      ULONGEST ul;
1294cf7f2e2dSJohn Marino 
1295cf7f2e2dSJohn Marino 		      ul = PyLong_AsUnsignedLongLong (obj);
12965796c8dcSSimon Schubert 		      if (! PyErr_Occurred ())
1297cf7f2e2dSJohn Marino 			value = value_from_ulongest (builtin_type_upylong, ul);
1298cf7f2e2dSJohn Marino 		    }
1299cf7f2e2dSJohn Marino 		  else
1300cf7f2e2dSJohn Marino 		    /* There's nothing we can do.  */
1301cf7f2e2dSJohn Marino 		    PyErr_Restore (etype, evalue, etraceback);
1302cf7f2e2dSJohn Marino 
1303cf7f2e2dSJohn Marino 		  Py_DECREF (zero);
1304cf7f2e2dSJohn Marino 		}
1305cf7f2e2dSJohn Marino 	    }
1306cf7f2e2dSJohn Marino 	  else
13075796c8dcSSimon Schubert 	    value = value_from_longest (builtin_type_pylong, l);
13085796c8dcSSimon Schubert 	}
13095796c8dcSSimon Schubert       else if (PyFloat_Check (obj))
13105796c8dcSSimon Schubert 	{
13115796c8dcSSimon Schubert 	  double d = PyFloat_AsDouble (obj);
13125796c8dcSSimon Schubert 
13135796c8dcSSimon Schubert 	  if (! PyErr_Occurred ())
13145796c8dcSSimon Schubert 	    value = value_from_double (builtin_type_pyfloat, d);
13155796c8dcSSimon Schubert 	}
13165796c8dcSSimon Schubert       else if (gdbpy_is_string (obj))
13175796c8dcSSimon Schubert 	{
13185796c8dcSSimon Schubert 	  char *s;
13195796c8dcSSimon Schubert 
13205796c8dcSSimon Schubert 	  s = python_string_to_target_string (obj);
13215796c8dcSSimon Schubert 	  if (s != NULL)
13225796c8dcSSimon Schubert 	    {
1323*ef5ccd6cSJohn Marino 	      struct cleanup *old;
1324*ef5ccd6cSJohn Marino 
13255796c8dcSSimon Schubert 	      old = make_cleanup (xfree, s);
13265796c8dcSSimon Schubert 	      value = value_cstring (s, strlen (s), builtin_type_pychar);
13275796c8dcSSimon Schubert 	      do_cleanups (old);
13285796c8dcSSimon Schubert 	    }
13295796c8dcSSimon Schubert 	}
13305796c8dcSSimon Schubert       else if (PyObject_TypeCheck (obj, &value_object_type))
13315796c8dcSSimon Schubert 	value = value_copy (((value_object *) obj)->value);
1332cf7f2e2dSJohn Marino       else if (gdbpy_is_lazy_string (obj))
1333cf7f2e2dSJohn Marino 	{
1334cf7f2e2dSJohn Marino 	  PyObject *result;
1335cf7f2e2dSJohn Marino 
1336c50c785cSJohn Marino 	  result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst,  NULL);
1337cf7f2e2dSJohn Marino 	  value = value_copy (((value_object *) result)->value);
1338cf7f2e2dSJohn Marino 	}
13395796c8dcSSimon Schubert       else
1340*ef5ccd6cSJohn Marino #ifdef IS_PY3K
1341*ef5ccd6cSJohn Marino 	PyErr_Format (PyExc_TypeError,
1342*ef5ccd6cSJohn Marino 		      _("Could not convert Python object: %S."), obj);
1343*ef5ccd6cSJohn Marino #else
1344c50c785cSJohn Marino 	PyErr_Format (PyExc_TypeError,
1345c50c785cSJohn Marino 		      _("Could not convert Python object: %s."),
13465796c8dcSSimon Schubert 		      PyString_AsString (PyObject_Str (obj)));
1347*ef5ccd6cSJohn Marino #endif
13485796c8dcSSimon Schubert     }
13495796c8dcSSimon Schubert   if (except.reason < 0)
13505796c8dcSSimon Schubert     {
13515796c8dcSSimon Schubert       PyErr_Format (except.reason == RETURN_QUIT
13525796c8dcSSimon Schubert 		    ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
13535796c8dcSSimon Schubert 		    "%s", except.message);
13545796c8dcSSimon Schubert       return NULL;
13555796c8dcSSimon Schubert     }
13565796c8dcSSimon Schubert 
13575796c8dcSSimon Schubert   return value;
13585796c8dcSSimon Schubert }
13595796c8dcSSimon Schubert 
13605796c8dcSSimon Schubert /* Returns value object in the ARGth position in GDB's history.  */
13615796c8dcSSimon Schubert PyObject *
gdbpy_history(PyObject * self,PyObject * args)13625796c8dcSSimon Schubert gdbpy_history (PyObject *self, PyObject *args)
13635796c8dcSSimon Schubert {
13645796c8dcSSimon Schubert   int i;
13655796c8dcSSimon Schubert   struct value *res_val = NULL;	  /* Initialize to appease gcc warning.  */
13665796c8dcSSimon Schubert   volatile struct gdb_exception except;
13675796c8dcSSimon Schubert 
13685796c8dcSSimon Schubert   if (!PyArg_ParseTuple (args, "i", &i))
13695796c8dcSSimon Schubert     return NULL;
13705796c8dcSSimon Schubert 
13715796c8dcSSimon Schubert   TRY_CATCH (except, RETURN_MASK_ALL)
13725796c8dcSSimon Schubert     {
13735796c8dcSSimon Schubert       res_val = access_value_history (i);
13745796c8dcSSimon Schubert     }
13755796c8dcSSimon Schubert   GDB_PY_HANDLE_EXCEPTION (except);
13765796c8dcSSimon Schubert 
13775796c8dcSSimon Schubert   return value_to_value_object (res_val);
13785796c8dcSSimon Schubert }
13795796c8dcSSimon Schubert 
1380cf7f2e2dSJohn Marino /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise.  */
1381cf7f2e2dSJohn Marino 
1382cf7f2e2dSJohn Marino int
gdbpy_is_value_object(PyObject * obj)1383cf7f2e2dSJohn Marino gdbpy_is_value_object (PyObject *obj)
1384cf7f2e2dSJohn Marino {
1385cf7f2e2dSJohn Marino   return PyObject_TypeCheck (obj, &value_object_type);
1386cf7f2e2dSJohn Marino }
1387cf7f2e2dSJohn Marino 
13885796c8dcSSimon Schubert void
gdbpy_initialize_values(void)13895796c8dcSSimon Schubert gdbpy_initialize_values (void)
13905796c8dcSSimon Schubert {
13915796c8dcSSimon Schubert   if (PyType_Ready (&value_object_type) < 0)
13925796c8dcSSimon Schubert     return;
13935796c8dcSSimon Schubert 
13945796c8dcSSimon Schubert   Py_INCREF (&value_object_type);
13955796c8dcSSimon Schubert   PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type);
13965796c8dcSSimon Schubert 
13975796c8dcSSimon Schubert   values_in_python = NULL;
13985796c8dcSSimon Schubert }
13995796c8dcSSimon Schubert 
14005796c8dcSSimon Schubert 
14015796c8dcSSimon Schubert 
14025796c8dcSSimon Schubert static PyGetSetDef value_object_getset[] = {
14035796c8dcSSimon Schubert   { "address", valpy_get_address, NULL, "The address of the value.",
14045796c8dcSSimon Schubert     NULL },
14055796c8dcSSimon Schubert   { "is_optimized_out", valpy_get_is_optimized_out, NULL,
1406c50c785cSJohn Marino     "Boolean telling whether the value is optimized "
1407c50c785cSJohn Marino     "out (i.e., not available).",
14085796c8dcSSimon Schubert     NULL },
14095796c8dcSSimon Schubert   { "type", valpy_get_type, NULL, "Type of the value.", NULL },
1410c50c785cSJohn Marino   { "dynamic_type", valpy_get_dynamic_type, NULL,
1411c50c785cSJohn Marino     "Dynamic type of the value.", NULL },
1412a45ae5f8SJohn Marino   { "is_lazy", valpy_get_is_lazy, NULL,
1413a45ae5f8SJohn Marino     "Boolean telling whether the value is lazy (not fetched yet\n\
1414a45ae5f8SJohn Marino from the inferior).  A lazy value is fetched when needed, or when\n\
1415a45ae5f8SJohn Marino the \"fetch_lazy()\" method is called.", NULL },
14165796c8dcSSimon Schubert   {NULL}  /* Sentinel */
14175796c8dcSSimon Schubert };
14185796c8dcSSimon Schubert 
14195796c8dcSSimon Schubert static PyMethodDef value_object_methods[] = {
14205796c8dcSSimon Schubert   { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
1421c50c785cSJohn Marino   { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
1422c50c785cSJohn Marino     "dynamic_cast (gdb.Type) -> gdb.Value\n\
1423c50c785cSJohn Marino Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1424c50c785cSJohn Marino   },
1425c50c785cSJohn Marino   { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
1426c50c785cSJohn Marino     "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1427c50c785cSJohn Marino Cast the value to the supplied type, as if by the C++\n\
1428c50c785cSJohn Marino reinterpret_cast operator."
1429c50c785cSJohn Marino   },
14305796c8dcSSimon Schubert   { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
1431*ef5ccd6cSJohn Marino   { "referenced_value", valpy_referenced_value, METH_NOARGS,
1432*ef5ccd6cSJohn Marino     "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1433c50c785cSJohn Marino   { "lazy_string", (PyCFunction) valpy_lazy_string,
1434c50c785cSJohn Marino     METH_VARARGS | METH_KEYWORDS,
1435cf7f2e2dSJohn Marino     "lazy_string ([encoding]  [, length]) -> lazy_string\n\
1436cf7f2e2dSJohn Marino Return a lazy string representation of the value." },
14375796c8dcSSimon Schubert   { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
14385796c8dcSSimon Schubert     "string ([encoding] [, errors] [, length]) -> string\n\
14395796c8dcSSimon Schubert Return Unicode string representation of the value." },
1440a45ae5f8SJohn Marino   { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
1441a45ae5f8SJohn Marino     "Fetches the value from the inferior, if it was lazy." },
14425796c8dcSSimon Schubert   {NULL}  /* Sentinel */
14435796c8dcSSimon Schubert };
14445796c8dcSSimon Schubert 
14455796c8dcSSimon Schubert static PyNumberMethods value_object_as_number = {
14465796c8dcSSimon Schubert   valpy_add,
14475796c8dcSSimon Schubert   valpy_subtract,
14485796c8dcSSimon Schubert   valpy_multiply,
1449*ef5ccd6cSJohn Marino #ifndef IS_PY3K
14505796c8dcSSimon Schubert   valpy_divide,
1451*ef5ccd6cSJohn Marino #endif
14525796c8dcSSimon Schubert   valpy_remainder,
14535796c8dcSSimon Schubert   NULL,			      /* nb_divmod */
14545796c8dcSSimon Schubert   valpy_power,		      /* nb_power */
14555796c8dcSSimon Schubert   valpy_negative,	      /* nb_negative */
14565796c8dcSSimon Schubert   valpy_positive,	      /* nb_positive */
14575796c8dcSSimon Schubert   valpy_absolute,	      /* nb_absolute */
14585796c8dcSSimon Schubert   valpy_nonzero,	      /* nb_nonzero */
14595796c8dcSSimon Schubert   valpy_invert,		      /* nb_invert */
14605796c8dcSSimon Schubert   valpy_lsh,		      /* nb_lshift */
14615796c8dcSSimon Schubert   valpy_rsh,		      /* nb_rshift */
14625796c8dcSSimon Schubert   valpy_and,		      /* nb_and */
14635796c8dcSSimon Schubert   valpy_xor,		      /* nb_xor */
14645796c8dcSSimon Schubert   valpy_or,		      /* nb_or */
1465*ef5ccd6cSJohn Marino #ifdef IS_PY3K
1466*ef5ccd6cSJohn Marino   valpy_long,		      /* nb_int */
1467*ef5ccd6cSJohn Marino   NULL,			      /* reserved */
1468*ef5ccd6cSJohn Marino #else
14695796c8dcSSimon Schubert   NULL,			      /* nb_coerce */
14705796c8dcSSimon Schubert   valpy_int,		      /* nb_int */
14715796c8dcSSimon Schubert   valpy_long,		      /* nb_long */
1472*ef5ccd6cSJohn Marino #endif
14735796c8dcSSimon Schubert   valpy_float,		      /* nb_float */
1474*ef5ccd6cSJohn Marino #ifndef IS_PY3K
14755796c8dcSSimon Schubert   NULL,			      /* nb_oct */
1476*ef5ccd6cSJohn Marino   NULL,                       /* nb_hex */
1477*ef5ccd6cSJohn Marino #endif
1478*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_add */
1479*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_subtract */
1480*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_multiply */
1481*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_remainder */
1482*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_power */
1483*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_lshift */
1484*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_rshift */
1485*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_and */
1486*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_xor */
1487*ef5ccd6cSJohn Marino   NULL,                       /* nb_inplace_or */
1488*ef5ccd6cSJohn Marino   NULL,                       /* nb_floor_divide */
1489*ef5ccd6cSJohn Marino   valpy_divide                /* nb_true_divide */
14905796c8dcSSimon Schubert };
14915796c8dcSSimon Schubert 
14925796c8dcSSimon Schubert static PyMappingMethods value_object_as_mapping = {
14935796c8dcSSimon Schubert   valpy_length,
14945796c8dcSSimon Schubert   valpy_getitem,
14955796c8dcSSimon Schubert   valpy_setitem
14965796c8dcSSimon Schubert };
14975796c8dcSSimon Schubert 
14985796c8dcSSimon Schubert PyTypeObject value_object_type = {
1499*ef5ccd6cSJohn Marino   PyVarObject_HEAD_INIT (NULL, 0)
15005796c8dcSSimon Schubert   "gdb.Value",			  /*tp_name*/
15015796c8dcSSimon Schubert   sizeof (value_object),	  /*tp_basicsize*/
15025796c8dcSSimon Schubert   0,				  /*tp_itemsize*/
15035796c8dcSSimon Schubert   valpy_dealloc,		  /*tp_dealloc*/
15045796c8dcSSimon Schubert   0,				  /*tp_print*/
15055796c8dcSSimon Schubert   0,				  /*tp_getattr*/
15065796c8dcSSimon Schubert   0,				  /*tp_setattr*/
15075796c8dcSSimon Schubert   0,				  /*tp_compare*/
15085796c8dcSSimon Schubert   0,				  /*tp_repr*/
15095796c8dcSSimon Schubert   &value_object_as_number,	  /*tp_as_number*/
15105796c8dcSSimon Schubert   0,				  /*tp_as_sequence*/
15115796c8dcSSimon Schubert   &value_object_as_mapping,	  /*tp_as_mapping*/
1512cf7f2e2dSJohn Marino   valpy_hash,		          /*tp_hash*/
1513c50c785cSJohn Marino   valpy_call,	                  /*tp_call*/
15145796c8dcSSimon Schubert   valpy_str,			  /*tp_str*/
15155796c8dcSSimon Schubert   0,				  /*tp_getattro*/
15165796c8dcSSimon Schubert   0,				  /*tp_setattro*/
15175796c8dcSSimon Schubert   0,				  /*tp_as_buffer*/
1518c50c785cSJohn Marino   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
1519c50c785cSJohn Marino   | Py_TPFLAGS_BASETYPE,	  /*tp_flags*/
15205796c8dcSSimon Schubert   "GDB value object",		  /* tp_doc */
15215796c8dcSSimon Schubert   0,				  /* tp_traverse */
15225796c8dcSSimon Schubert   0,				  /* tp_clear */
15235796c8dcSSimon Schubert   valpy_richcompare,		  /* tp_richcompare */
15245796c8dcSSimon Schubert   0,				  /* tp_weaklistoffset */
15255796c8dcSSimon Schubert   0,				  /* tp_iter */
15265796c8dcSSimon Schubert   0,				  /* tp_iternext */
15275796c8dcSSimon Schubert   value_object_methods,		  /* tp_methods */
15285796c8dcSSimon Schubert   0,				  /* tp_members */
15295796c8dcSSimon Schubert   value_object_getset,		  /* tp_getset */
15305796c8dcSSimon Schubert   0,				  /* tp_base */
15315796c8dcSSimon Schubert   0,				  /* tp_dict */
15325796c8dcSSimon Schubert   0,				  /* tp_descr_get */
15335796c8dcSSimon Schubert   0,				  /* tp_descr_set */
15345796c8dcSSimon Schubert   0,				  /* tp_dictoffset */
15355796c8dcSSimon Schubert   0,				  /* tp_init */
15365796c8dcSSimon Schubert   0,				  /* tp_alloc */
15375796c8dcSSimon Schubert   valpy_new			  /* tp_new */
15385796c8dcSSimon Schubert };
15395796c8dcSSimon Schubert 
15405796c8dcSSimon Schubert #else
15415796c8dcSSimon Schubert 
15425796c8dcSSimon Schubert void
preserve_python_values(struct objfile * objfile,htab_t copied_types)15435796c8dcSSimon Schubert preserve_python_values (struct objfile *objfile, htab_t copied_types)
15445796c8dcSSimon Schubert {
15455796c8dcSSimon Schubert   /* Nothing.  */
15465796c8dcSSimon Schubert }
15475796c8dcSSimon Schubert 
15485796c8dcSSimon Schubert #endif /* HAVE_PYTHON */
1549