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