Home | History | Annotate | Line # | Download | only in python
py-value.c revision 1.1.1.10
      1       1.1  christos /* Python interface to values.
      2       1.1  christos 
      3   1.1.1.9  christos    Copyright (C) 2008-2024 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.1.9  christos #include "top.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.1.6  christos #include "target-float.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.1.9  christos #include "ada-lang.h"
     31       1.1  christos 
     32       1.1  christos #include "python-internal.h"
     33       1.1  christos 
     34       1.1  christos /* Even though Python scalar types directly map to host types, we use
     35       1.1  christos    target types here to remain consistent with the values system in
     36       1.1  christos    GDB (which uses target arithmetic).  */
     37       1.1  christos 
     38       1.1  christos /* Python's integer type corresponds to C's long type.  */
     39   1.1.1.8  christos #define builtin_type_pyint \
     40   1.1.1.8  christos   builtin_type (gdbpy_enter::get_gdbarch ())->builtin_long
     41       1.1  christos 
     42       1.1  christos /* Python's float type corresponds to C's double type.  */
     43   1.1.1.8  christos #define builtin_type_pyfloat \
     44   1.1.1.8  christos   builtin_type (gdbpy_enter::get_gdbarch ())->builtin_double
     45       1.1  christos 
     46       1.1  christos /* Python's long type corresponds to C's long long type.  */
     47   1.1.1.8  christos #define builtin_type_pylong \
     48   1.1.1.8  christos   builtin_type (gdbpy_enter::get_gdbarch ())->builtin_long_long
     49       1.1  christos 
     50       1.1  christos /* Python's long type corresponds to C's long long type.  Unsigned version.  */
     51       1.1  christos #define builtin_type_upylong builtin_type \
     52   1.1.1.8  christos   (gdbpy_enter::get_gdbarch ())->builtin_unsigned_long_long
     53       1.1  christos 
     54       1.1  christos #define builtin_type_pybool \
     55   1.1.1.8  christos   language_bool_type (current_language, gdbpy_enter::get_gdbarch ())
     56       1.1  christos 
     57   1.1.1.8  christos struct value_object {
     58       1.1  christos   PyObject_HEAD
     59       1.1  christos   struct value_object *next;
     60       1.1  christos   struct value_object *prev;
     61       1.1  christos   struct value *value;
     62       1.1  christos   PyObject *address;
     63       1.1  christos   PyObject *type;
     64       1.1  christos   PyObject *dynamic_type;
     65   1.1.1.9  christos   PyObject *content_bytes;
     66   1.1.1.8  christos };
     67       1.1  christos 
     68       1.1  christos /* List of all values which are currently exposed to Python. It is
     69       1.1  christos    maintained so that when an objfile is discarded, preserve_values
     70       1.1  christos    can copy the values' types if needed.  */
     71       1.1  christos /* This variable is unnecessarily initialized to NULL in order to
     72       1.1  christos    work around a linker bug on MacOS.  */
     73       1.1  christos static value_object *values_in_python = NULL;
     74       1.1  christos 
     75   1.1.1.8  christos /* Clear out an old GDB value stored within SELF, and reset the fields to
     76   1.1.1.8  christos    nullptr.  This should be called when a gdb.Value is deallocated, and
     77   1.1.1.8  christos    also if a gdb.Value is reinitialized with a new value.  */
     78   1.1.1.8  christos 
     79   1.1.1.8  christos static void
     80   1.1.1.8  christos valpy_clear_value (value_object *self)
     81   1.1.1.8  christos {
     82   1.1.1.8  christos   /* Indicate we are no longer interested in the value object.  */
     83   1.1.1.9  christos   self->value->decref ();
     84   1.1.1.8  christos   self->value = nullptr;
     85   1.1.1.8  christos 
     86   1.1.1.8  christos   Py_CLEAR (self->address);
     87   1.1.1.8  christos   Py_CLEAR (self->type);
     88   1.1.1.8  christos   Py_CLEAR (self->dynamic_type);
     89   1.1.1.9  christos   Py_CLEAR (self->content_bytes);
     90   1.1.1.8  christos }
     91   1.1.1.8  christos 
     92       1.1  christos /* Called by the Python interpreter when deallocating a value object.  */
     93       1.1  christos static void
     94       1.1  christos valpy_dealloc (PyObject *obj)
     95       1.1  christos {
     96       1.1  christos   value_object *self = (value_object *) obj;
     97       1.1  christos 
     98   1.1.1.8  christos   /* If SELF failed to initialize correctly then it may not have a value
     99   1.1.1.8  christos      contained within it.  */
    100   1.1.1.8  christos   if (self->value != nullptr)
    101   1.1.1.8  christos     {
    102   1.1.1.8  christos       /* Remove SELF from the global list of values.  */
    103   1.1.1.8  christos       if (self->prev != nullptr)
    104   1.1.1.8  christos 	self->prev->next = self->next;
    105   1.1.1.8  christos       else
    106   1.1.1.8  christos 	{
    107   1.1.1.8  christos 	  gdb_assert (values_in_python == self);
    108   1.1.1.8  christos 	  values_in_python = self->next;
    109   1.1.1.8  christos 	}
    110   1.1.1.8  christos       if (self->next != nullptr)
    111   1.1.1.8  christos 	self->next->prev = self->prev;
    112       1.1  christos 
    113   1.1.1.8  christos       /* Release the value object and any cached Python objects.  */
    114   1.1.1.8  christos       valpy_clear_value (self);
    115   1.1.1.8  christos     }
    116       1.1  christos 
    117       1.1  christos   Py_TYPE (self)->tp_free (self);
    118       1.1  christos }
    119       1.1  christos 
    120   1.1.1.8  christos /* Helper to push a gdb.Value object on to the global list of values.  If
    121   1.1.1.8  christos    VALUE_OBJ is already on the lit then this does nothing.  */
    122   1.1.1.8  christos 
    123       1.1  christos static void
    124       1.1  christos note_value (value_object *value_obj)
    125       1.1  christos {
    126   1.1.1.8  christos   if (value_obj->next == nullptr)
    127   1.1.1.8  christos     {
    128   1.1.1.8  christos       gdb_assert (value_obj->prev == nullptr);
    129   1.1.1.8  christos       value_obj->next = values_in_python;
    130   1.1.1.8  christos       if (value_obj->next != nullptr)
    131   1.1.1.8  christos 	value_obj->next->prev = value_obj;
    132   1.1.1.8  christos       values_in_python = value_obj;
    133   1.1.1.8  christos     }
    134       1.1  christos }
    135       1.1  christos 
    136   1.1.1.6  christos /* Convert a python object OBJ with type TYPE to a gdb value.  The
    137   1.1.1.6  christos    python object in question must conform to the python buffer
    138   1.1.1.6  christos    protocol.  On success, return the converted value, otherwise
    139   1.1.1.9  christos    nullptr.  When REQUIRE_EXACT_SIZE_P is true the buffer OBJ must be the
    140   1.1.1.9  christos    exact length of TYPE.  When REQUIRE_EXACT_SIZE_P is false then the
    141   1.1.1.9  christos    buffer OBJ can be longer than TYPE, in which case only the least
    142   1.1.1.9  christos    significant bytes from the buffer are used.  */
    143   1.1.1.6  christos 
    144   1.1.1.6  christos static struct value *
    145   1.1.1.9  christos convert_buffer_and_type_to_value (PyObject *obj, struct type *type,
    146   1.1.1.9  christos 				  bool require_exact_size_p)
    147   1.1.1.6  christos {
    148   1.1.1.6  christos   Py_buffer_up buffer_up;
    149   1.1.1.6  christos   Py_buffer py_buf;
    150   1.1.1.6  christos 
    151   1.1.1.6  christos   if (PyObject_CheckBuffer (obj)
    152   1.1.1.6  christos       && PyObject_GetBuffer (obj, &py_buf, PyBUF_SIMPLE) == 0)
    153   1.1.1.6  christos     {
    154   1.1.1.6  christos       /* Got a buffer, py_buf, out of obj.  Cause it to be released
    155   1.1.1.8  christos 	 when it goes out of scope.  */
    156   1.1.1.6  christos       buffer_up.reset (&py_buf);
    157   1.1.1.6  christos     }
    158   1.1.1.6  christos   else
    159   1.1.1.6  christos     {
    160   1.1.1.6  christos       PyErr_SetString (PyExc_TypeError,
    161   1.1.1.6  christos 		       _("Object must support the python buffer protocol."));
    162   1.1.1.6  christos       return nullptr;
    163   1.1.1.6  christos     }
    164   1.1.1.6  christos 
    165   1.1.1.9  christos   if (require_exact_size_p && type->length () != py_buf.len)
    166   1.1.1.9  christos     {
    167   1.1.1.9  christos       PyErr_SetString (PyExc_ValueError,
    168   1.1.1.9  christos 		       _("Size of type is not equal to that of buffer object."));
    169   1.1.1.9  christos       return nullptr;
    170   1.1.1.9  christos     }
    171   1.1.1.9  christos   else if (!require_exact_size_p && type->length () > py_buf.len)
    172   1.1.1.6  christos     {
    173   1.1.1.6  christos       PyErr_SetString (PyExc_ValueError,
    174   1.1.1.6  christos 		       _("Size of type is larger than that of buffer object."));
    175   1.1.1.6  christos       return nullptr;
    176   1.1.1.6  christos     }
    177   1.1.1.6  christos 
    178   1.1.1.6  christos   return value_from_contents (type, (const gdb_byte *) py_buf.buf);
    179   1.1.1.6  christos }
    180   1.1.1.6  christos 
    181   1.1.1.8  christos /* Implement gdb.Value.__init__.  */
    182   1.1.1.8  christos 
    183   1.1.1.8  christos static int
    184   1.1.1.8  christos valpy_init (PyObject *self, PyObject *args, PyObject *kwds)
    185       1.1  christos {
    186   1.1.1.6  christos   static const char *keywords[] = { "val", "type", NULL };
    187   1.1.1.6  christos   PyObject *val_obj = nullptr;
    188   1.1.1.6  christos   PyObject *type_obj = nullptr;
    189   1.1.1.6  christos 
    190   1.1.1.8  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kwds, "O|O", keywords,
    191   1.1.1.6  christos 					&val_obj, &type_obj))
    192   1.1.1.8  christos     return -1;
    193   1.1.1.6  christos 
    194   1.1.1.6  christos   struct type *type = nullptr;
    195   1.1.1.8  christos   if (type_obj != nullptr && type_obj != Py_None)
    196       1.1  christos     {
    197   1.1.1.6  christos       type = type_object_to_type (type_obj);
    198   1.1.1.6  christos       if (type == nullptr)
    199   1.1.1.8  christos 	{
    200   1.1.1.6  christos 	  PyErr_SetString (PyExc_TypeError,
    201   1.1.1.6  christos 			   _("type argument must be a gdb.Type."));
    202   1.1.1.8  christos 	  return -1;
    203   1.1.1.6  christos 	}
    204       1.1  christos     }
    205       1.1  christos 
    206   1.1.1.6  christos   struct value *value;
    207   1.1.1.6  christos   if (type == nullptr)
    208   1.1.1.6  christos     value = convert_value_from_python (val_obj);
    209   1.1.1.6  christos   else
    210   1.1.1.9  christos     value = convert_buffer_and_type_to_value (val_obj, type, false);
    211   1.1.1.6  christos   if (value == nullptr)
    212       1.1  christos     {
    213   1.1.1.8  christos       gdb_assert (PyErr_Occurred ());
    214   1.1.1.8  christos       return -1;
    215       1.1  christos     }
    216       1.1  christos 
    217   1.1.1.8  christos   /* There might be a previous value here.  */
    218   1.1.1.8  christos   value_object *value_obj = (value_object *) self;
    219   1.1.1.8  christos   if (value_obj->value != nullptr)
    220   1.1.1.8  christos     valpy_clear_value (value_obj);
    221   1.1.1.8  christos 
    222   1.1.1.8  christos   /* Store the value into this Python object.  */
    223   1.1.1.6  christos   value_obj->value = release_value (value).release ();
    224   1.1.1.8  christos 
    225   1.1.1.8  christos   /* Ensure that this gdb.Value is in the set of all gdb.Value objects.  If
    226   1.1.1.8  christos      we are already in the set then this is call does nothing.  */
    227       1.1  christos   note_value (value_obj);
    228       1.1  christos 
    229   1.1.1.8  christos   return 0;
    230       1.1  christos }
    231       1.1  christos 
    232       1.1  christos /* Iterate over all the Value objects, calling preserve_one_value on
    233       1.1  christos    each.  */
    234       1.1  christos void
    235   1.1.1.2  christos gdbpy_preserve_values (const struct extension_language_defn *extlang,
    236  1.1.1.10  christos 		       struct objfile *objfile,
    237  1.1.1.10  christos 		       copied_types_hash_t &copied_types)
    238       1.1  christos {
    239       1.1  christos   value_object *iter;
    240       1.1  christos 
    241       1.1  christos   for (iter = values_in_python; iter; iter = iter->next)
    242   1.1.1.9  christos     iter->value->preserve (objfile, copied_types);
    243       1.1  christos }
    244       1.1  christos 
    245       1.1  christos /* Given a value of a pointer type, apply the C unary * operator to it.  */
    246       1.1  christos static PyObject *
    247       1.1  christos valpy_dereference (PyObject *self, PyObject *args)
    248       1.1  christos {
    249       1.1  christos   PyObject *result = NULL;
    250       1.1  christos 
    251   1.1.1.7  christos   try
    252       1.1  christos     {
    253       1.1  christos       struct value *res_val;
    254   1.1.1.5  christos       scoped_value_mark free_values;
    255       1.1  christos 
    256       1.1  christos       res_val = value_ind (((value_object *) self)->value);
    257       1.1  christos       result = value_to_value_object (res_val);
    258       1.1  christos     }
    259   1.1.1.7  christos   catch (const gdb_exception &except)
    260   1.1.1.3  christos     {
    261  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    262   1.1.1.3  christos     }
    263       1.1  christos 
    264       1.1  christos   return result;
    265       1.1  christos }
    266       1.1  christos 
    267       1.1  christos /* Given a value of a pointer type or a reference type, return the value
    268       1.1  christos    referenced. The difference between this function and valpy_dereference is
    269       1.1  christos    that the latter applies * unary operator to a value, which need not always
    270       1.1  christos    result in the value referenced. For example, for a value which is a reference
    271       1.1  christos    to an 'int' pointer ('int *'), valpy_dereference will result in a value of
    272       1.1  christos    type 'int' while valpy_referenced_value will result in a value of type
    273       1.1  christos    'int *'.  */
    274       1.1  christos 
    275       1.1  christos static PyObject *
    276       1.1  christos valpy_referenced_value (PyObject *self, PyObject *args)
    277       1.1  christos {
    278       1.1  christos   PyObject *result = NULL;
    279       1.1  christos 
    280   1.1.1.7  christos   try
    281       1.1  christos     {
    282       1.1  christos       struct value *self_val, *res_val;
    283   1.1.1.5  christos       scoped_value_mark free_values;
    284       1.1  christos 
    285       1.1  christos       self_val = ((value_object *) self)->value;
    286   1.1.1.9  christos       switch (check_typedef (self_val->type ())->code ())
    287   1.1.1.8  christos 	{
    288   1.1.1.8  christos 	case TYPE_CODE_PTR:
    289   1.1.1.8  christos 	  res_val = value_ind (self_val);
    290   1.1.1.8  christos 	  break;
    291   1.1.1.8  christos 	case TYPE_CODE_REF:
    292   1.1.1.8  christos 	case TYPE_CODE_RVALUE_REF:
    293   1.1.1.8  christos 	  res_val = coerce_ref (self_val);
    294   1.1.1.8  christos 	  break;
    295   1.1.1.8  christos 	default:
    296   1.1.1.8  christos 	  error(_("Trying to get the referenced value from a value which is "
    297   1.1.1.8  christos 		  "neither a pointer nor a reference."));
    298   1.1.1.8  christos 	}
    299       1.1  christos 
    300       1.1  christos       result = value_to_value_object (res_val);
    301       1.1  christos     }
    302   1.1.1.7  christos   catch (const gdb_exception &except)
    303   1.1.1.3  christos     {
    304  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    305   1.1.1.3  christos     }
    306   1.1.1.3  christos 
    307   1.1.1.3  christos   return result;
    308   1.1.1.3  christos }
    309   1.1.1.3  christos 
    310   1.1.1.3  christos /* Return a value which is a reference to the value.  */
    311   1.1.1.3  christos 
    312   1.1.1.3  christos static PyObject *
    313   1.1.1.5  christos valpy_reference_value (PyObject *self, PyObject *args, enum type_code refcode)
    314   1.1.1.3  christos {
    315   1.1.1.3  christos   PyObject *result = NULL;
    316   1.1.1.3  christos 
    317   1.1.1.7  christos   try
    318   1.1.1.3  christos     {
    319   1.1.1.3  christos       struct value *self_val;
    320   1.1.1.5  christos       scoped_value_mark free_values;
    321   1.1.1.3  christos 
    322   1.1.1.3  christos       self_val = ((value_object *) self)->value;
    323   1.1.1.5  christos       result = value_to_value_object (value_ref (self_val, refcode));
    324   1.1.1.3  christos     }
    325   1.1.1.7  christos   catch (const gdb_exception &except)
    326   1.1.1.3  christos     {
    327  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    328   1.1.1.3  christos     }
    329   1.1.1.3  christos 
    330   1.1.1.3  christos   return result;
    331   1.1.1.3  christos }
    332   1.1.1.3  christos 
    333   1.1.1.5  christos static PyObject *
    334   1.1.1.5  christos valpy_lvalue_reference_value (PyObject *self, PyObject *args)
    335   1.1.1.5  christos {
    336   1.1.1.5  christos   return valpy_reference_value (self, args, TYPE_CODE_REF);
    337   1.1.1.5  christos }
    338   1.1.1.5  christos 
    339   1.1.1.5  christos static PyObject *
    340   1.1.1.5  christos valpy_rvalue_reference_value (PyObject *self, PyObject *args)
    341   1.1.1.5  christos {
    342   1.1.1.5  christos   return valpy_reference_value (self, args, TYPE_CODE_RVALUE_REF);
    343   1.1.1.5  christos }
    344   1.1.1.5  christos 
    345   1.1.1.9  christos /* Implement Value.to_array.  */
    346   1.1.1.9  christos 
    347   1.1.1.9  christos static PyObject *
    348   1.1.1.9  christos valpy_to_array (PyObject *self, PyObject *args)
    349   1.1.1.9  christos {
    350   1.1.1.9  christos   PyObject *result = nullptr;
    351   1.1.1.9  christos 
    352   1.1.1.9  christos   try
    353   1.1.1.9  christos     {
    354   1.1.1.9  christos       struct value *val = ((value_object *) self)->value;
    355   1.1.1.9  christos       struct type *type = check_typedef (val->type ());
    356   1.1.1.9  christos 
    357   1.1.1.9  christos       if (type->code () == TYPE_CODE_ARRAY)
    358   1.1.1.9  christos 	{
    359   1.1.1.9  christos 	  result = self;
    360   1.1.1.9  christos 	  Py_INCREF (result);
    361   1.1.1.9  christos 	}
    362   1.1.1.9  christos       else
    363   1.1.1.9  christos 	{
    364   1.1.1.9  christos 	  val = value_to_array (val);
    365   1.1.1.9  christos 	  if (val == nullptr)
    366   1.1.1.9  christos 	    PyErr_SetString (PyExc_TypeError, _("Value is not array-like."));
    367   1.1.1.9  christos 	  else
    368   1.1.1.9  christos 	    result = value_to_value_object (val);
    369   1.1.1.9  christos 	}
    370   1.1.1.9  christos     }
    371   1.1.1.9  christos   catch (const gdb_exception &except)
    372   1.1.1.9  christos     {
    373  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    374   1.1.1.9  christos     }
    375   1.1.1.9  christos 
    376   1.1.1.9  christos   return result;
    377   1.1.1.9  christos }
    378   1.1.1.9  christos 
    379   1.1.1.3  christos /* Return a "const" qualified version of the value.  */
    380   1.1.1.3  christos 
    381   1.1.1.3  christos static PyObject *
    382   1.1.1.3  christos valpy_const_value (PyObject *self, PyObject *args)
    383   1.1.1.3  christos {
    384   1.1.1.3  christos   PyObject *result = NULL;
    385   1.1.1.3  christos 
    386   1.1.1.7  christos   try
    387   1.1.1.3  christos     {
    388   1.1.1.3  christos       struct value *self_val, *res_val;
    389   1.1.1.5  christos       scoped_value_mark free_values;
    390   1.1.1.3  christos 
    391   1.1.1.3  christos       self_val = ((value_object *) self)->value;
    392   1.1.1.3  christos       res_val = make_cv_value (1, 0, self_val);
    393   1.1.1.3  christos       result = value_to_value_object (res_val);
    394   1.1.1.3  christos     }
    395   1.1.1.7  christos   catch (const gdb_exception &except)
    396   1.1.1.3  christos     {
    397  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    398   1.1.1.3  christos     }
    399       1.1  christos 
    400       1.1  christos   return result;
    401       1.1  christos }
    402       1.1  christos 
    403       1.1  christos /* Return "&value".  */
    404       1.1  christos static PyObject *
    405       1.1  christos valpy_get_address (PyObject *self, void *closure)
    406       1.1  christos {
    407       1.1  christos   value_object *val_obj = (value_object *) self;
    408       1.1  christos 
    409       1.1  christos   if (!val_obj->address)
    410       1.1  christos     {
    411   1.1.1.7  christos       try
    412       1.1  christos 	{
    413       1.1  christos 	  struct value *res_val;
    414   1.1.1.5  christos 	  scoped_value_mark free_values;
    415       1.1  christos 
    416       1.1  christos 	  res_val = value_addr (val_obj->value);
    417       1.1  christos 	  val_obj->address = value_to_value_object (res_val);
    418       1.1  christos 	}
    419   1.1.1.9  christos       catch (const gdb_exception_forced_quit &except)
    420   1.1.1.9  christos 	{
    421   1.1.1.9  christos 	  quit_force (NULL, 0);
    422   1.1.1.9  christos 	}
    423   1.1.1.7  christos       catch (const gdb_exception &except)
    424       1.1  christos 	{
    425       1.1  christos 	  val_obj->address = Py_None;
    426       1.1  christos 	  Py_INCREF (Py_None);
    427       1.1  christos 	}
    428       1.1  christos     }
    429       1.1  christos 
    430       1.1  christos   Py_XINCREF (val_obj->address);
    431       1.1  christos 
    432       1.1  christos   return val_obj->address;
    433       1.1  christos }
    434       1.1  christos 
    435       1.1  christos /* Return type of the value.  */
    436       1.1  christos static PyObject *
    437       1.1  christos valpy_get_type (PyObject *self, void *closure)
    438       1.1  christos {
    439       1.1  christos   value_object *obj = (value_object *) self;
    440       1.1  christos 
    441       1.1  christos   if (!obj->type)
    442       1.1  christos     {
    443   1.1.1.9  christos       obj->type = type_to_type_object (obj->value->type ());
    444       1.1  christos       if (!obj->type)
    445       1.1  christos 	return NULL;
    446       1.1  christos     }
    447       1.1  christos   Py_INCREF (obj->type);
    448       1.1  christos   return obj->type;
    449       1.1  christos }
    450       1.1  christos 
    451       1.1  christos /* Return dynamic type of the value.  */
    452       1.1  christos 
    453       1.1  christos static PyObject *
    454       1.1  christos valpy_get_dynamic_type (PyObject *self, void *closure)
    455       1.1  christos {
    456       1.1  christos   value_object *obj = (value_object *) self;
    457       1.1  christos   struct type *type = NULL;
    458       1.1  christos 
    459       1.1  christos   if (obj->dynamic_type != NULL)
    460       1.1  christos     {
    461       1.1  christos       Py_INCREF (obj->dynamic_type);
    462       1.1  christos       return obj->dynamic_type;
    463       1.1  christos     }
    464       1.1  christos 
    465   1.1.1.7  christos   try
    466       1.1  christos     {
    467       1.1  christos       struct value *val = obj->value;
    468   1.1.1.5  christos       scoped_value_mark free_values;
    469       1.1  christos 
    470   1.1.1.9  christos       type = val->type ();
    471   1.1.1.4  christos       type = check_typedef (type);
    472       1.1  christos 
    473   1.1.1.8  christos       if (type->is_pointer_or_reference ()
    474   1.1.1.8  christos 	  && (type->target_type ()->code () == TYPE_CODE_STRUCT))
    475       1.1  christos 	{
    476       1.1  christos 	  struct value *target;
    477   1.1.1.7  christos 	  int was_pointer = type->code () == TYPE_CODE_PTR;
    478       1.1  christos 
    479   1.1.1.2  christos 	  if (was_pointer)
    480   1.1.1.2  christos 	    target = value_ind (val);
    481   1.1.1.2  christos 	  else
    482   1.1.1.2  christos 	    target = coerce_ref (val);
    483       1.1  christos 	  type = value_rtti_type (target, NULL, NULL, NULL);
    484       1.1  christos 
    485       1.1  christos 	  if (type)
    486       1.1  christos 	    {
    487       1.1  christos 	      if (was_pointer)
    488       1.1  christos 		type = lookup_pointer_type (type);
    489       1.1  christos 	      else
    490   1.1.1.5  christos 		type = lookup_lvalue_reference_type (type);
    491       1.1  christos 	    }
    492       1.1  christos 	}
    493   1.1.1.7  christos       else if (type->code () == TYPE_CODE_STRUCT)
    494       1.1  christos 	type = value_rtti_type (val, NULL, NULL, NULL);
    495       1.1  christos       else
    496       1.1  christos 	{
    497       1.1  christos 	  /* Re-use object's static type.  */
    498       1.1  christos 	  type = NULL;
    499       1.1  christos 	}
    500       1.1  christos     }
    501   1.1.1.7  christos   catch (const gdb_exception &except)
    502   1.1.1.3  christos     {
    503  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    504   1.1.1.3  christos     }
    505       1.1  christos 
    506       1.1  christos   if (type == NULL)
    507       1.1  christos     obj->dynamic_type = valpy_get_type (self, NULL);
    508       1.1  christos   else
    509       1.1  christos     obj->dynamic_type = type_to_type_object (type);
    510       1.1  christos 
    511       1.1  christos   Py_XINCREF (obj->dynamic_type);
    512       1.1  christos   return obj->dynamic_type;
    513       1.1  christos }
    514       1.1  christos 
    515       1.1  christos /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
    516       1.1  christos    string.  Return a PyObject representing a lazy_string_object type.
    517       1.1  christos    A lazy string is a pointer to a string with an optional encoding and
    518       1.1  christos    length.  If ENCODING is not given, encoding is set to None.  If an
    519       1.1  christos    ENCODING is provided the encoding parameter is set to ENCODING, but
    520   1.1.1.5  christos    the string is not encoded.
    521   1.1.1.5  christos    If LENGTH is provided then the length parameter is set to LENGTH.
    522   1.1.1.5  christos    Otherwise if the value is an array of known length then the array's length
    523   1.1.1.5  christos    is used.  Otherwise the length will be set to -1 (meaning first null of
    524  1.1.1.10  christos    appropriate with).  */
    525   1.1.1.5  christos 
    526       1.1  christos static PyObject *
    527       1.1  christos valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
    528       1.1  christos {
    529       1.1  christos   gdb_py_longest length = -1;
    530       1.1  christos   struct value *value = ((value_object *) self)->value;
    531       1.1  christos   const char *user_encoding = NULL;
    532   1.1.1.5  christos   static const char *keywords[] = { "encoding", "length", NULL };
    533       1.1  christos   PyObject *str_obj = NULL;
    534       1.1  christos 
    535   1.1.1.5  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG,
    536   1.1.1.5  christos 					keywords, &user_encoding, &length))
    537       1.1  christos     return NULL;
    538       1.1  christos 
    539   1.1.1.5  christos   if (length < -1)
    540   1.1.1.5  christos     {
    541   1.1.1.5  christos       PyErr_SetString (PyExc_ValueError, _("Invalid length."));
    542   1.1.1.5  christos       return NULL;
    543   1.1.1.5  christos     }
    544   1.1.1.5  christos 
    545   1.1.1.7  christos   try
    546       1.1  christos     {
    547   1.1.1.5  christos       scoped_value_mark free_values;
    548   1.1.1.5  christos       struct type *type, *realtype;
    549   1.1.1.5  christos       CORE_ADDR addr;
    550       1.1  christos 
    551   1.1.1.9  christos       type = value->type ();
    552   1.1.1.5  christos       realtype = check_typedef (type);
    553   1.1.1.5  christos 
    554   1.1.1.7  christos       switch (realtype->code ())
    555   1.1.1.5  christos 	{
    556   1.1.1.5  christos 	case TYPE_CODE_ARRAY:
    557   1.1.1.5  christos 	  {
    558   1.1.1.5  christos 	    LONGEST array_length = -1;
    559   1.1.1.5  christos 	    LONGEST low_bound, high_bound;
    560       1.1  christos 
    561   1.1.1.5  christos 	    /* PR 20786: There's no way to specify an array of length zero.
    562   1.1.1.5  christos 	       Record a length of [0,-1] which is how Ada does it.  Anything
    563   1.1.1.5  christos 	       we do is broken, but this one possible solution.  */
    564   1.1.1.5  christos 	    if (get_array_bounds (realtype, &low_bound, &high_bound))
    565   1.1.1.5  christos 	      array_length = high_bound - low_bound + 1;
    566   1.1.1.5  christos 	    if (length == -1)
    567   1.1.1.5  christos 	      length = array_length;
    568   1.1.1.5  christos 	    else if (array_length == -1)
    569   1.1.1.5  christos 	      {
    570   1.1.1.8  christos 		type = lookup_array_range_type (realtype->target_type (),
    571   1.1.1.5  christos 						0, length - 1);
    572   1.1.1.5  christos 	      }
    573   1.1.1.5  christos 	    else if (length != array_length)
    574   1.1.1.5  christos 	      {
    575   1.1.1.5  christos 		/* We need to create a new array type with the
    576   1.1.1.5  christos 		   specified length.  */
    577   1.1.1.5  christos 		if (length > array_length)
    578   1.1.1.5  christos 		  error (_("Length is larger than array size."));
    579   1.1.1.8  christos 		type = lookup_array_range_type (realtype->target_type (),
    580   1.1.1.5  christos 						low_bound,
    581   1.1.1.5  christos 						low_bound + length - 1);
    582   1.1.1.5  christos 	      }
    583   1.1.1.9  christos 	    addr = value->address ();
    584   1.1.1.5  christos 	    break;
    585   1.1.1.5  christos 	  }
    586   1.1.1.5  christos 	case TYPE_CODE_PTR:
    587   1.1.1.5  christos 	  /* If a length is specified we defer creating an array of the
    588   1.1.1.5  christos 	     specified width until we need to.  */
    589   1.1.1.5  christos 	  addr = value_as_address (value);
    590   1.1.1.5  christos 	  break;
    591   1.1.1.5  christos 	default:
    592  1.1.1.10  christos 	  error (_("Cannot make lazy string from this object"));
    593   1.1.1.5  christos 	}
    594       1.1  christos 
    595   1.1.1.5  christos       str_obj = gdbpy_create_lazy_string_object (addr, length, user_encoding,
    596   1.1.1.5  christos 						 type);
    597       1.1  christos     }
    598   1.1.1.7  christos   catch (const gdb_exception &except)
    599   1.1.1.3  christos     {
    600  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    601   1.1.1.3  christos     }
    602       1.1  christos 
    603       1.1  christos   return str_obj;
    604       1.1  christos }
    605       1.1  christos 
    606       1.1  christos /* Implementation of gdb.Value.string ([encoding] [, errors]
    607       1.1  christos    [, length]) -> string.  Return Unicode string with value contents.
    608       1.1  christos    If ENCODING is not given, the string is assumed to be encoded in
    609       1.1  christos    the target's charset.  If LENGTH is provided, only fetch string to
    610       1.1  christos    the length provided.  */
    611       1.1  christos 
    612       1.1  christos static PyObject *
    613       1.1  christos valpy_string (PyObject *self, PyObject *args, PyObject *kw)
    614       1.1  christos {
    615       1.1  christos   int length = -1;
    616   1.1.1.6  christos   gdb::unique_xmalloc_ptr<gdb_byte> buffer;
    617       1.1  christos   struct value *value = ((value_object *) self)->value;
    618       1.1  christos   const char *encoding = NULL;
    619       1.1  christos   const char *errors = NULL;
    620       1.1  christos   const char *user_encoding = NULL;
    621       1.1  christos   const char *la_encoding = NULL;
    622       1.1  christos   struct type *char_type;
    623   1.1.1.5  christos   static const char *keywords[] = { "encoding", "errors", "length", NULL };
    624       1.1  christos 
    625   1.1.1.5  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
    626   1.1.1.5  christos 					&user_encoding, &errors, &length))
    627       1.1  christos     return NULL;
    628       1.1  christos 
    629   1.1.1.7  christos   try
    630       1.1  christos     {
    631   1.1.1.7  christos       c_get_string (value, &buffer, &length, &char_type, &la_encoding);
    632       1.1  christos     }
    633   1.1.1.7  christos   catch (const gdb_exception &except)
    634   1.1.1.3  christos     {
    635  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    636   1.1.1.3  christos     }
    637       1.1  christos 
    638       1.1  christos   encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
    639   1.1.1.6  christos   return PyUnicode_Decode ((const char *) buffer.get (),
    640   1.1.1.8  christos 			   length * char_type->length (),
    641   1.1.1.6  christos 			   encoding, errors);
    642       1.1  christos }
    643       1.1  christos 
    644   1.1.1.7  christos /* Given a Python object, copy its truth value to a C bool (the value
    645   1.1.1.7  christos    pointed by dest).
    646   1.1.1.7  christos    If src_obj is NULL, then *dest is not modified.
    647   1.1.1.7  christos 
    648   1.1.1.7  christos    Return true in case of success (including src_obj being NULL), false
    649   1.1.1.7  christos    in case of error.  */
    650   1.1.1.7  christos 
    651   1.1.1.7  christos static bool
    652   1.1.1.7  christos copy_py_bool_obj (bool *dest, PyObject *src_obj)
    653   1.1.1.7  christos {
    654   1.1.1.7  christos   if (src_obj)
    655   1.1.1.7  christos     {
    656   1.1.1.7  christos       int cmp = PyObject_IsTrue (src_obj);
    657   1.1.1.7  christos       if (cmp < 0)
    658   1.1.1.7  christos 	return false;
    659   1.1.1.7  christos       *dest = cmp;
    660   1.1.1.7  christos     }
    661   1.1.1.7  christos 
    662   1.1.1.7  christos   return true;
    663   1.1.1.7  christos }
    664   1.1.1.7  christos 
    665   1.1.1.7  christos /* Implementation of gdb.Value.format_string (...) -> string.
    666   1.1.1.7  christos    Return Unicode string with value contents formatted using the
    667   1.1.1.7  christos    keyword-only arguments.  */
    668   1.1.1.7  christos 
    669   1.1.1.7  christos static PyObject *
    670   1.1.1.7  christos valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
    671   1.1.1.7  christos {
    672   1.1.1.7  christos   static const char *keywords[] =
    673   1.1.1.7  christos     {
    674   1.1.1.7  christos       /* Basic C/C++ options.  */
    675   1.1.1.7  christos       "raw",			/* See the /r option to print.  */
    676   1.1.1.7  christos       "pretty_arrays",		/* See set print array on|off.  */
    677   1.1.1.7  christos       "pretty_structs",		/* See set print pretty on|off.  */
    678   1.1.1.7  christos       "array_indexes",		/* See set print array-indexes on|off.  */
    679   1.1.1.7  christos       "symbols",		/* See set print symbol on|off.  */
    680   1.1.1.7  christos       "unions",			/* See set print union on|off.  */
    681   1.1.1.8  christos       "address",		/* See set print address on|off.  */
    682   1.1.1.8  christos       "styling",		/* Should we apply styling.  */
    683   1.1.1.8  christos       "nibbles",		/* See set print nibbles on|off.  */
    684   1.1.1.8  christos       "summary",		/* Summary mode for non-scalars.  */
    685   1.1.1.7  christos       /* C++ options.  */
    686   1.1.1.7  christos       "deref_refs",		/* No corresponding setting.  */
    687   1.1.1.7  christos       "actual_objects",		/* See set print object on|off.  */
    688   1.1.1.7  christos       "static_members",		/* See set print static-members on|off.  */
    689   1.1.1.7  christos       /* C non-bool options.  */
    690   1.1.1.9  christos       "max_characters", 	/* See set print characters N.  */
    691   1.1.1.7  christos       "max_elements", 		/* See set print elements N.  */
    692   1.1.1.7  christos       "max_depth",		/* See set print max-depth N.  */
    693   1.1.1.7  christos       "repeat_threshold",	/* See set print repeats.  */
    694   1.1.1.7  christos       "format",			/* The format passed to the print command.  */
    695   1.1.1.7  christos       NULL
    696   1.1.1.7  christos     };
    697   1.1.1.7  christos 
    698   1.1.1.7  christos   /* This function has too many arguments to be useful as positionals, so
    699   1.1.1.7  christos      the user should specify them all as keyword arguments.
    700   1.1.1.7  christos      Python 3.3 and later have a way to specify it (both in C and Python
    701   1.1.1.7  christos      itself), but we could be compiled with older versions, so we just
    702   1.1.1.7  christos      check that the args tuple is empty.  */
    703   1.1.1.7  christos   Py_ssize_t positional_count = PyObject_Length (args);
    704   1.1.1.7  christos   if (positional_count < 0)
    705   1.1.1.7  christos     return NULL;
    706   1.1.1.7  christos   else if (positional_count > 0)
    707   1.1.1.7  christos     {
    708   1.1.1.7  christos       /* This matches the error message that Python 3.3 raises when
    709   1.1.1.7  christos 	 passing positionals to functions expecting keyword-only
    710   1.1.1.7  christos 	 arguments.  */
    711   1.1.1.7  christos       PyErr_Format (PyExc_TypeError,
    712   1.1.1.7  christos 		    "format_string() takes 0 positional arguments but %zu were given",
    713   1.1.1.7  christos 		    positional_count);
    714   1.1.1.7  christos       return NULL;
    715   1.1.1.7  christos     }
    716   1.1.1.7  christos 
    717   1.1.1.7  christos   struct value_print_options opts;
    718   1.1.1.8  christos   gdbpy_get_print_options (&opts);
    719   1.1.1.9  christos   opts.deref_ref = false;
    720   1.1.1.7  christos 
    721   1.1.1.7  christos   /* We need objects for booleans as the "p" flag for bools is new in
    722   1.1.1.7  christos      Python 3.3.  */
    723   1.1.1.7  christos   PyObject *raw_obj = NULL;
    724   1.1.1.7  christos   PyObject *pretty_arrays_obj = NULL;
    725   1.1.1.7  christos   PyObject *pretty_structs_obj = NULL;
    726   1.1.1.7  christos   PyObject *array_indexes_obj = NULL;
    727   1.1.1.7  christos   PyObject *symbols_obj = NULL;
    728   1.1.1.7  christos   PyObject *unions_obj = NULL;
    729   1.1.1.8  christos   PyObject *address_obj = NULL;
    730   1.1.1.8  christos   PyObject *styling_obj = Py_False;
    731   1.1.1.8  christos   PyObject *nibbles_obj = NULL;
    732   1.1.1.7  christos   PyObject *deref_refs_obj = NULL;
    733   1.1.1.7  christos   PyObject *actual_objects_obj = NULL;
    734   1.1.1.7  christos   PyObject *static_members_obj = NULL;
    735   1.1.1.8  christos   PyObject *summary_obj = NULL;
    736   1.1.1.7  christos   char *format = NULL;
    737   1.1.1.7  christos   if (!gdb_PyArg_ParseTupleAndKeywords (args,
    738   1.1.1.7  christos 					kw,
    739   1.1.1.9  christos 					"|O!O!O!O!O!O!O!O!O!O!O!O!O!IIIIs",
    740   1.1.1.7  christos 					keywords,
    741   1.1.1.7  christos 					&PyBool_Type, &raw_obj,
    742   1.1.1.7  christos 					&PyBool_Type, &pretty_arrays_obj,
    743   1.1.1.7  christos 					&PyBool_Type, &pretty_structs_obj,
    744   1.1.1.7  christos 					&PyBool_Type, &array_indexes_obj,
    745   1.1.1.7  christos 					&PyBool_Type, &symbols_obj,
    746   1.1.1.7  christos 					&PyBool_Type, &unions_obj,
    747   1.1.1.8  christos 					&PyBool_Type, &address_obj,
    748   1.1.1.8  christos 					&PyBool_Type, &styling_obj,
    749   1.1.1.8  christos 					&PyBool_Type, &nibbles_obj,
    750   1.1.1.8  christos 					&PyBool_Type, &summary_obj,
    751   1.1.1.7  christos 					&PyBool_Type, &deref_refs_obj,
    752   1.1.1.7  christos 					&PyBool_Type, &actual_objects_obj,
    753   1.1.1.7  christos 					&PyBool_Type, &static_members_obj,
    754   1.1.1.9  christos 					&opts.print_max_chars,
    755   1.1.1.7  christos 					&opts.print_max,
    756   1.1.1.7  christos 					&opts.max_depth,
    757   1.1.1.7  christos 					&opts.repeat_count_threshold,
    758   1.1.1.7  christos 					&format))
    759   1.1.1.7  christos     return NULL;
    760   1.1.1.7  christos 
    761   1.1.1.7  christos   /* Set boolean arguments.  */
    762   1.1.1.7  christos   if (!copy_py_bool_obj (&opts.raw, raw_obj))
    763   1.1.1.7  christos     return NULL;
    764   1.1.1.7  christos   if (!copy_py_bool_obj (&opts.prettyformat_arrays, pretty_arrays_obj))
    765   1.1.1.7  christos     return NULL;
    766   1.1.1.7  christos   if (!copy_py_bool_obj (&opts.prettyformat_structs, pretty_structs_obj))
    767   1.1.1.7  christos     return NULL;
    768   1.1.1.7  christos   if (!copy_py_bool_obj (&opts.print_array_indexes, array_indexes_obj))
    769   1.1.1.7  christos     return NULL;
    770   1.1.1.7  christos   if (!copy_py_bool_obj (&opts.symbol_print, symbols_obj))
    771   1.1.1.7  christos     return NULL;
    772   1.1.1.7  christos   if (!copy_py_bool_obj (&opts.unionprint, unions_obj))
    773   1.1.1.7  christos     return NULL;
    774   1.1.1.8  christos   if (!copy_py_bool_obj (&opts.addressprint, address_obj))
    775   1.1.1.8  christos     return NULL;
    776   1.1.1.8  christos   if (!copy_py_bool_obj (&opts.nibblesprint, nibbles_obj))
    777   1.1.1.8  christos     return NULL;
    778   1.1.1.7  christos   if (!copy_py_bool_obj (&opts.deref_ref, deref_refs_obj))
    779   1.1.1.7  christos     return NULL;
    780   1.1.1.7  christos   if (!copy_py_bool_obj (&opts.objectprint, actual_objects_obj))
    781   1.1.1.7  christos     return NULL;
    782   1.1.1.7  christos   if (!copy_py_bool_obj (&opts.static_field_print, static_members_obj))
    783   1.1.1.7  christos     return NULL;
    784   1.1.1.8  christos   if (!copy_py_bool_obj (&opts.summary, summary_obj))
    785   1.1.1.8  christos     return nullptr;
    786   1.1.1.7  christos 
    787   1.1.1.7  christos   /* Numeric arguments for which 0 means unlimited (which we represent as
    788   1.1.1.7  christos      UINT_MAX).  Note that the max-depth numeric argument uses -1 as
    789   1.1.1.7  christos      unlimited, and 0 is a valid choice.  */
    790   1.1.1.7  christos   if (opts.print_max == 0)
    791   1.1.1.7  christos     opts.print_max = UINT_MAX;
    792   1.1.1.7  christos   if (opts.repeat_count_threshold == 0)
    793   1.1.1.7  christos     opts.repeat_count_threshold = UINT_MAX;
    794   1.1.1.7  christos 
    795   1.1.1.7  christos   /* Other arguments.  */
    796   1.1.1.7  christos   if (format != NULL)
    797   1.1.1.7  christos     {
    798   1.1.1.7  christos       if (strlen (format) == 1)
    799   1.1.1.7  christos 	opts.format = format[0];
    800   1.1.1.7  christos       else
    801   1.1.1.7  christos 	{
    802   1.1.1.7  christos 	  /* Mimic the message on standard Python ones for similar
    803   1.1.1.7  christos 	     errors.  */
    804   1.1.1.7  christos 	  PyErr_SetString (PyExc_ValueError,
    805   1.1.1.7  christos 			   "a single character is required");
    806   1.1.1.7  christos 	  return NULL;
    807   1.1.1.7  christos 	}
    808   1.1.1.7  christos     }
    809   1.1.1.7  christos 
    810  1.1.1.10  christos   /* We force styling_obj to be a 'bool' when we parse the args above.  */
    811  1.1.1.10  christos   gdb_assert (PyBool_Check (styling_obj));
    812  1.1.1.10  christos   string_file stb (styling_obj == Py_True);
    813   1.1.1.7  christos 
    814   1.1.1.7  christos   try
    815   1.1.1.7  christos     {
    816   1.1.1.7  christos       common_val_print (((value_object *) self)->value, &stb, 0,
    817   1.1.1.8  christos 			&opts, current_language);
    818   1.1.1.7  christos     }
    819   1.1.1.7  christos   catch (const gdb_exception &except)
    820   1.1.1.7  christos     {
    821  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    822   1.1.1.7  christos     }
    823   1.1.1.7  christos 
    824   1.1.1.7  christos   return PyUnicode_Decode (stb.c_str (), stb.size (), host_charset (), NULL);
    825   1.1.1.7  christos }
    826   1.1.1.7  christos 
    827       1.1  christos /* A helper function that implements the various cast operators.  */
    828       1.1  christos 
    829       1.1  christos static PyObject *
    830       1.1  christos valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
    831       1.1  christos {
    832       1.1  christos   PyObject *type_obj, *result = NULL;
    833       1.1  christos   struct type *type;
    834       1.1  christos 
    835       1.1  christos   if (! PyArg_ParseTuple (args, "O", &type_obj))
    836       1.1  christos     return NULL;
    837       1.1  christos 
    838       1.1  christos   type = type_object_to_type (type_obj);
    839       1.1  christos   if (! type)
    840       1.1  christos     {
    841       1.1  christos       PyErr_SetString (PyExc_RuntimeError,
    842       1.1  christos 		       _("Argument must be a type."));
    843       1.1  christos       return NULL;
    844       1.1  christos     }
    845       1.1  christos 
    846   1.1.1.7  christos   try
    847       1.1  christos     {
    848       1.1  christos       struct value *val = ((value_object *) self)->value;
    849       1.1  christos       struct value *res_val;
    850   1.1.1.5  christos       scoped_value_mark free_values;
    851       1.1  christos 
    852       1.1  christos       if (op == UNOP_DYNAMIC_CAST)
    853       1.1  christos 	res_val = value_dynamic_cast (type, val);
    854       1.1  christos       else if (op == UNOP_REINTERPRET_CAST)
    855       1.1  christos 	res_val = value_reinterpret_cast (type, val);
    856       1.1  christos       else
    857       1.1  christos 	{
    858       1.1  christos 	  gdb_assert (op == UNOP_CAST);
    859       1.1  christos 	  res_val = value_cast (type, val);
    860       1.1  christos 	}
    861       1.1  christos 
    862       1.1  christos       result = value_to_value_object (res_val);
    863       1.1  christos     }
    864   1.1.1.7  christos   catch (const gdb_exception &except)
    865   1.1.1.3  christos     {
    866  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
    867   1.1.1.3  christos     }
    868       1.1  christos 
    869       1.1  christos   return result;
    870       1.1  christos }
    871       1.1  christos 
    872       1.1  christos /* Implementation of the "cast" method.  */
    873       1.1  christos 
    874       1.1  christos static PyObject *
    875       1.1  christos valpy_cast (PyObject *self, PyObject *args)
    876       1.1  christos {
    877       1.1  christos   return valpy_do_cast (self, args, UNOP_CAST);
    878       1.1  christos }
    879       1.1  christos 
    880       1.1  christos /* Implementation of the "dynamic_cast" method.  */
    881       1.1  christos 
    882       1.1  christos static PyObject *
    883       1.1  christos valpy_dynamic_cast (PyObject *self, PyObject *args)
    884       1.1  christos {
    885       1.1  christos   return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
    886       1.1  christos }
    887       1.1  christos 
    888       1.1  christos /* Implementation of the "reinterpret_cast" method.  */
    889       1.1  christos 
    890       1.1  christos static PyObject *
    891       1.1  christos valpy_reinterpret_cast (PyObject *self, PyObject *args)
    892       1.1  christos {
    893       1.1  christos   return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
    894       1.1  christos }
    895       1.1  christos 
    896   1.1.1.9  christos /* Assign NEW_VALUE into SELF, handles 'struct value' reference counting,
    897   1.1.1.9  christos    and also clearing the bytes data cached within SELF.  Return true if
    898   1.1.1.9  christos    the assignment was successful, otherwise return false, in which case a
    899   1.1.1.9  christos    Python exception will be set.  */
    900   1.1.1.9  christos 
    901   1.1.1.9  christos static bool
    902   1.1.1.9  christos valpy_assign_core (value_object *self, struct value *new_value)
    903   1.1.1.9  christos {
    904   1.1.1.9  christos   try
    905   1.1.1.9  christos     {
    906   1.1.1.9  christos       new_value = value_assign (self->value, new_value);
    907   1.1.1.9  christos 
    908   1.1.1.9  christos       /* value_as_address returns a new value with the same location
    909   1.1.1.9  christos 	 as the old one.  Ensure that this gdb.Value is updated to
    910   1.1.1.9  christos 	 reflect the new value.  */
    911   1.1.1.9  christos       new_value->incref ();
    912   1.1.1.9  christos       self->value->decref ();
    913   1.1.1.9  christos       Py_CLEAR (self->content_bytes);
    914   1.1.1.9  christos       self->value = new_value;
    915   1.1.1.9  christos     }
    916   1.1.1.9  christos   catch (const gdb_exception &except)
    917   1.1.1.9  christos     {
    918  1.1.1.10  christos       return gdbpy_handle_gdb_exception (false, except);
    919   1.1.1.9  christos     }
    920   1.1.1.9  christos 
    921   1.1.1.9  christos   return true;
    922   1.1.1.9  christos }
    923   1.1.1.9  christos 
    924   1.1.1.9  christos /* Implementation of the "assign" method.  */
    925   1.1.1.9  christos 
    926   1.1.1.9  christos static PyObject *
    927   1.1.1.9  christos valpy_assign (PyObject *self_obj, PyObject *args)
    928   1.1.1.9  christos {
    929   1.1.1.9  christos   PyObject *val_obj;
    930   1.1.1.9  christos 
    931   1.1.1.9  christos   if (! PyArg_ParseTuple (args, "O", &val_obj))
    932   1.1.1.9  christos     return nullptr;
    933   1.1.1.9  christos 
    934   1.1.1.9  christos   struct value *val = convert_value_from_python (val_obj);
    935   1.1.1.9  christos   if (val == nullptr)
    936   1.1.1.9  christos     return nullptr;
    937   1.1.1.9  christos 
    938   1.1.1.9  christos   value_object *self = (value_object *) self_obj;
    939   1.1.1.9  christos   if (!valpy_assign_core (self, val))
    940   1.1.1.9  christos     return nullptr;
    941   1.1.1.9  christos 
    942   1.1.1.9  christos   Py_RETURN_NONE;
    943   1.1.1.9  christos }
    944   1.1.1.9  christos 
    945       1.1  christos static Py_ssize_t
    946       1.1  christos valpy_length (PyObject *self)
    947       1.1  christos {
    948       1.1  christos   /* We don't support getting the number of elements in a struct / class.  */
    949       1.1  christos   PyErr_SetString (PyExc_NotImplementedError,
    950       1.1  christos 		   _("Invalid operation on gdb.Value."));
    951       1.1  christos   return -1;
    952       1.1  christos }
    953       1.1  christos 
    954       1.1  christos /* Return 1 if the gdb.Field object FIELD is present in the value V.
    955       1.1  christos    Returns 0 otherwise.  If any Python error occurs, -1 is returned.  */
    956       1.1  christos 
    957       1.1  christos static int
    958       1.1  christos value_has_field (struct value *v, PyObject *field)
    959       1.1  christos {
    960       1.1  christos   struct type *parent_type, *val_type;
    961       1.1  christos   enum type_code type_code;
    962   1.1.1.5  christos   gdbpy_ref<> type_object (PyObject_GetAttrString (field, "parent_type"));
    963       1.1  christos   int has_field = 0;
    964       1.1  christos 
    965       1.1  christos   if (type_object == NULL)
    966       1.1  christos     return -1;
    967       1.1  christos 
    968   1.1.1.5  christos   parent_type = type_object_to_type (type_object.get ());
    969       1.1  christos   if (parent_type == NULL)
    970       1.1  christos     {
    971       1.1  christos       PyErr_SetString (PyExc_TypeError,
    972       1.1  christos 		       _("'parent_type' attribute of gdb.Field object is not a"
    973       1.1  christos 			 "gdb.Type object."));
    974       1.1  christos       return -1;
    975       1.1  christos     }
    976       1.1  christos 
    977   1.1.1.7  christos   try
    978       1.1  christos     {
    979   1.1.1.9  christos       val_type = v->type ();
    980       1.1  christos       val_type = check_typedef (val_type);
    981   1.1.1.8  christos       if (val_type->is_pointer_or_reference ())
    982   1.1.1.8  christos 	val_type = check_typedef (val_type->target_type ());
    983       1.1  christos 
    984   1.1.1.7  christos       type_code = val_type->code ();
    985       1.1  christos       if ((type_code == TYPE_CODE_STRUCT || type_code == TYPE_CODE_UNION)
    986       1.1  christos 	  && types_equal (val_type, parent_type))
    987       1.1  christos 	has_field = 1;
    988       1.1  christos       else
    989       1.1  christos 	has_field = 0;
    990       1.1  christos     }
    991   1.1.1.7  christos   catch (const gdb_exception &except)
    992   1.1.1.3  christos     {
    993  1.1.1.10  christos       return gdbpy_handle_gdb_exception (-1, except);
    994   1.1.1.3  christos     }
    995       1.1  christos 
    996       1.1  christos   return has_field;
    997       1.1  christos }
    998       1.1  christos 
    999       1.1  christos /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
   1000       1.1  christos    Returns 1 if the flag value is true, 0 if it is false, and -1 if
   1001       1.1  christos    a Python error occurs.  */
   1002       1.1  christos 
   1003       1.1  christos static int
   1004       1.1  christos get_field_flag (PyObject *field, const char *flag_name)
   1005       1.1  christos {
   1006   1.1.1.5  christos   gdbpy_ref<> flag_object (PyObject_GetAttrString (field, flag_name));
   1007       1.1  christos 
   1008       1.1  christos   if (flag_object == NULL)
   1009       1.1  christos     return -1;
   1010       1.1  christos 
   1011   1.1.1.5  christos   return PyObject_IsTrue (flag_object.get ());
   1012       1.1  christos }
   1013       1.1  christos 
   1014       1.1  christos /* Return the "type" attribute of a gdb.Field object.
   1015       1.1  christos    Returns NULL on error, with a Python exception set.  */
   1016       1.1  christos 
   1017       1.1  christos static struct type *
   1018       1.1  christos get_field_type (PyObject *field)
   1019       1.1  christos {
   1020   1.1.1.5  christos   gdbpy_ref<> ftype_obj (PyObject_GetAttrString (field, "type"));
   1021       1.1  christos   struct type *ftype;
   1022       1.1  christos 
   1023       1.1  christos   if (ftype_obj == NULL)
   1024       1.1  christos     return NULL;
   1025   1.1.1.5  christos   ftype = type_object_to_type (ftype_obj.get ());
   1026       1.1  christos   if (ftype == NULL)
   1027       1.1  christos     PyErr_SetString (PyExc_TypeError,
   1028       1.1  christos 		     _("'type' attribute of gdb.Field object is not a "
   1029       1.1  christos 		       "gdb.Type object."));
   1030       1.1  christos 
   1031       1.1  christos   return ftype;
   1032       1.1  christos }
   1033       1.1  christos 
   1034       1.1  christos /* Given string name or a gdb.Field object corresponding to an element inside
   1035       1.1  christos    a structure, return its value object.  Returns NULL on error, with a python
   1036       1.1  christos    exception set.  */
   1037       1.1  christos 
   1038       1.1  christos static PyObject *
   1039       1.1  christos valpy_getitem (PyObject *self, PyObject *key)
   1040       1.1  christos {
   1041       1.1  christos   value_object *self_value = (value_object *) self;
   1042   1.1.1.5  christos   gdb::unique_xmalloc_ptr<char> field;
   1043       1.1  christos   struct type *base_class_type = NULL, *field_type = NULL;
   1044       1.1  christos   long bitpos = -1;
   1045       1.1  christos   PyObject *result = NULL;
   1046       1.1  christos 
   1047       1.1  christos   if (gdbpy_is_string (key))
   1048       1.1  christos     {
   1049       1.1  christos       field = python_string_to_host_string (key);
   1050       1.1  christos       if (field == NULL)
   1051       1.1  christos 	return NULL;
   1052       1.1  christos     }
   1053       1.1  christos   else if (gdbpy_is_field (key))
   1054       1.1  christos     {
   1055       1.1  christos       int is_base_class, valid_field;
   1056       1.1  christos 
   1057       1.1  christos       valid_field = value_has_field (self_value->value, key);
   1058       1.1  christos       if (valid_field < 0)
   1059       1.1  christos 	return NULL;
   1060       1.1  christos       else if (valid_field == 0)
   1061       1.1  christos 	{
   1062       1.1  christos 	  PyErr_SetString (PyExc_TypeError,
   1063       1.1  christos 			   _("Invalid lookup for a field not contained in "
   1064       1.1  christos 			     "the value."));
   1065       1.1  christos 
   1066       1.1  christos 	  return NULL;
   1067       1.1  christos 	}
   1068       1.1  christos 
   1069       1.1  christos       is_base_class = get_field_flag (key, "is_base_class");
   1070       1.1  christos       if (is_base_class < 0)
   1071       1.1  christos 	return NULL;
   1072       1.1  christos       else if (is_base_class > 0)
   1073       1.1  christos 	{
   1074       1.1  christos 	  base_class_type = get_field_type (key);
   1075       1.1  christos 	  if (base_class_type == NULL)
   1076       1.1  christos 	    return NULL;
   1077       1.1  christos 	}
   1078       1.1  christos       else
   1079       1.1  christos 	{
   1080   1.1.1.5  christos 	  gdbpy_ref<> name_obj (PyObject_GetAttrString (key, "name"));
   1081       1.1  christos 
   1082       1.1  christos 	  if (name_obj == NULL)
   1083       1.1  christos 	    return NULL;
   1084       1.1  christos 
   1085       1.1  christos 	  if (name_obj != Py_None)
   1086       1.1  christos 	    {
   1087   1.1.1.5  christos 	      field = python_string_to_host_string (name_obj.get ());
   1088       1.1  christos 	      if (field == NULL)
   1089       1.1  christos 		return NULL;
   1090       1.1  christos 	    }
   1091       1.1  christos 	  else
   1092       1.1  christos 	    {
   1093       1.1  christos 	      if (!PyObject_HasAttrString (key, "bitpos"))
   1094       1.1  christos 		{
   1095       1.1  christos 		  PyErr_SetString (PyExc_AttributeError,
   1096       1.1  christos 				   _("gdb.Field object has no name and no "
   1097   1.1.1.8  christos 				     "'bitpos' attribute."));
   1098       1.1  christos 
   1099       1.1  christos 		  return NULL;
   1100       1.1  christos 		}
   1101   1.1.1.5  christos 	      gdbpy_ref<> bitpos_obj (PyObject_GetAttrString (key, "bitpos"));
   1102       1.1  christos 	      if (bitpos_obj == NULL)
   1103       1.1  christos 		return NULL;
   1104   1.1.1.5  christos 	      if (!gdb_py_int_as_long (bitpos_obj.get (), &bitpos))
   1105       1.1  christos 		return NULL;
   1106       1.1  christos 
   1107       1.1  christos 	      field_type = get_field_type (key);
   1108       1.1  christos 	      if (field_type == NULL)
   1109       1.1  christos 		return NULL;
   1110       1.1  christos 	    }
   1111       1.1  christos 	}
   1112       1.1  christos     }
   1113       1.1  christos 
   1114   1.1.1.7  christos   try
   1115       1.1  christos     {
   1116       1.1  christos       struct value *tmp = self_value->value;
   1117       1.1  christos       struct value *res_val = NULL;
   1118   1.1.1.5  christos       scoped_value_mark free_values;
   1119       1.1  christos 
   1120       1.1  christos       if (field)
   1121   1.1.1.8  christos 	res_val = value_struct_elt (&tmp, {}, field.get (), NULL,
   1122   1.1.1.5  christos 				    "struct/class/union");
   1123       1.1  christos       else if (bitpos >= 0)
   1124       1.1  christos 	res_val = value_struct_elt_bitpos (&tmp, bitpos, field_type,
   1125       1.1  christos 					   "struct/class/union");
   1126       1.1  christos       else if (base_class_type != NULL)
   1127       1.1  christos 	{
   1128       1.1  christos 	  struct type *val_type;
   1129       1.1  christos 
   1130   1.1.1.9  christos 	  val_type = check_typedef (tmp->type ());
   1131   1.1.1.7  christos 	  if (val_type->code () == TYPE_CODE_PTR)
   1132       1.1  christos 	    res_val = value_cast (lookup_pointer_type (base_class_type), tmp);
   1133   1.1.1.7  christos 	  else if (val_type->code () == TYPE_CODE_REF)
   1134   1.1.1.5  christos 	    res_val = value_cast (lookup_lvalue_reference_type (base_class_type),
   1135   1.1.1.8  christos 				  tmp);
   1136   1.1.1.7  christos 	  else if (val_type->code () == TYPE_CODE_RVALUE_REF)
   1137   1.1.1.5  christos 	    res_val = value_cast (lookup_rvalue_reference_type (base_class_type),
   1138   1.1.1.8  christos 				  tmp);
   1139       1.1  christos 	  else
   1140       1.1  christos 	    res_val = value_cast (base_class_type, tmp);
   1141       1.1  christos 	}
   1142       1.1  christos       else
   1143       1.1  christos 	{
   1144       1.1  christos 	  /* Assume we are attempting an array access, and let the
   1145       1.1  christos 	     value code throw an exception if the index has an invalid
   1146       1.1  christos 	     type.  */
   1147       1.1  christos 	  struct value *idx = convert_value_from_python (key);
   1148       1.1  christos 
   1149  1.1.1.10  christos 	  if (idx != NULL
   1150  1.1.1.10  christos 	      && binop_user_defined_p (BINOP_SUBSCRIPT, tmp, idx))
   1151  1.1.1.10  christos 	    res_val = value_x_binop (tmp, idx, BINOP_SUBSCRIPT,
   1152  1.1.1.10  christos 				     OP_NULL, EVAL_NORMAL);
   1153  1.1.1.10  christos 	  else if (idx != NULL)
   1154       1.1  christos 	    {
   1155       1.1  christos 	      /* Check the value's type is something that can be accessed via
   1156       1.1  christos 		 a subscript.  */
   1157       1.1  christos 	      struct type *type;
   1158       1.1  christos 
   1159       1.1  christos 	      tmp = coerce_ref (tmp);
   1160   1.1.1.9  christos 	      type = check_typedef (tmp->type ());
   1161   1.1.1.7  christos 	      if (type->code () != TYPE_CODE_ARRAY
   1162   1.1.1.7  christos 		  && type->code () != TYPE_CODE_PTR)
   1163       1.1  christos 		  error (_("Cannot subscript requested type."));
   1164   1.1.1.9  christos 	      else if (ADA_TYPE_P (type))
   1165   1.1.1.9  christos 		res_val = ada_value_subscript (tmp, 1, &idx);
   1166       1.1  christos 	      else
   1167       1.1  christos 		res_val = value_subscript (tmp, value_as_long (idx));
   1168       1.1  christos 	    }
   1169       1.1  christos 	}
   1170       1.1  christos 
   1171       1.1  christos       if (res_val)
   1172       1.1  christos 	result = value_to_value_object (res_val);
   1173       1.1  christos     }
   1174  1.1.1.10  christos   catch (const gdb_exception &ex)
   1175   1.1.1.3  christos     {
   1176  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, ex);
   1177   1.1.1.3  christos     }
   1178       1.1  christos 
   1179       1.1  christos   return result;
   1180       1.1  christos }
   1181       1.1  christos 
   1182       1.1  christos static int
   1183       1.1  christos valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
   1184       1.1  christos {
   1185       1.1  christos   PyErr_Format (PyExc_NotImplementedError,
   1186       1.1  christos 		_("Setting of struct elements is not currently supported."));
   1187       1.1  christos   return -1;
   1188       1.1  christos }
   1189       1.1  christos 
   1190       1.1  christos /* Called by the Python interpreter to perform an inferior function
   1191       1.1  christos    call on the value.  Returns NULL on error, with a python exception set.  */
   1192       1.1  christos static PyObject *
   1193       1.1  christos valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
   1194       1.1  christos {
   1195       1.1  christos   Py_ssize_t args_count;
   1196       1.1  christos   struct value *function = ((value_object *) self)->value;
   1197       1.1  christos   struct value **vargs = NULL;
   1198       1.1  christos   struct type *ftype = NULL;
   1199       1.1  christos   PyObject *result = NULL;
   1200       1.1  christos 
   1201   1.1.1.7  christos   try
   1202       1.1  christos     {
   1203   1.1.1.9  christos       ftype = check_typedef (function->type ());
   1204       1.1  christos     }
   1205   1.1.1.7  christos   catch (const gdb_exception &except)
   1206   1.1.1.3  christos     {
   1207  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1208   1.1.1.3  christos     }
   1209       1.1  christos 
   1210  1.1.1.10  christos   if (ftype->code () != TYPE_CODE_FUNC && ftype->code () != TYPE_CODE_METHOD
   1211  1.1.1.10  christos       && ftype->code () != TYPE_CODE_INTERNAL_FUNCTION)
   1212       1.1  christos     {
   1213       1.1  christos       PyErr_SetString (PyExc_RuntimeError,
   1214   1.1.1.9  christos 		       _("Value is not callable (not TYPE_CODE_FUNC"
   1215  1.1.1.10  christos 			 " or TYPE_CODE_METHOD"
   1216  1.1.1.10  christos 			 " or TYPE_CODE_INTERNAL_FUNCTION)."));
   1217       1.1  christos       return NULL;
   1218       1.1  christos     }
   1219       1.1  christos 
   1220       1.1  christos   if (! PyTuple_Check (args))
   1221       1.1  christos     {
   1222       1.1  christos       PyErr_SetString (PyExc_TypeError,
   1223       1.1  christos 		       _("Inferior arguments must be provided in a tuple."));
   1224       1.1  christos       return NULL;
   1225       1.1  christos     }
   1226       1.1  christos 
   1227       1.1  christos   args_count = PyTuple_Size (args);
   1228       1.1  christos   if (args_count > 0)
   1229       1.1  christos     {
   1230       1.1  christos       int i;
   1231       1.1  christos 
   1232   1.1.1.4  christos       vargs = XALLOCAVEC (struct value *, args_count);
   1233       1.1  christos       for (i = 0; i < args_count; i++)
   1234       1.1  christos 	{
   1235       1.1  christos 	  PyObject *item = PyTuple_GetItem (args, i);
   1236       1.1  christos 
   1237       1.1  christos 	  if (item == NULL)
   1238       1.1  christos 	    return NULL;
   1239       1.1  christos 
   1240       1.1  christos 	  vargs[i] = convert_value_from_python (item);
   1241       1.1  christos 	  if (vargs[i] == NULL)
   1242       1.1  christos 	    return NULL;
   1243       1.1  christos 	}
   1244       1.1  christos     }
   1245       1.1  christos 
   1246   1.1.1.7  christos   try
   1247       1.1  christos     {
   1248   1.1.1.5  christos       scoped_value_mark free_values;
   1249       1.1  christos 
   1250  1.1.1.10  christos       value *return_value;
   1251  1.1.1.10  christos       if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
   1252  1.1.1.10  christos 	return_value = call_internal_function (gdbpy_enter::get_gdbarch (),
   1253  1.1.1.10  christos 					       current_language,
   1254  1.1.1.10  christos 					       function, args_count, vargs,
   1255  1.1.1.10  christos 					       EVAL_NORMAL);
   1256  1.1.1.10  christos       else
   1257  1.1.1.10  christos 	return_value
   1258  1.1.1.10  christos 	  = call_function_by_hand (function, NULL,
   1259  1.1.1.10  christos 				   gdb::make_array_view (vargs, args_count));
   1260       1.1  christos       result = value_to_value_object (return_value);
   1261       1.1  christos     }
   1262   1.1.1.7  christos   catch (const gdb_exception &except)
   1263   1.1.1.3  christos     {
   1264  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1265   1.1.1.3  christos     }
   1266       1.1  christos 
   1267       1.1  christos   return result;
   1268       1.1  christos }
   1269       1.1  christos 
   1270       1.1  christos /* Called by the Python interpreter to obtain string representation
   1271       1.1  christos    of the object.  */
   1272       1.1  christos static PyObject *
   1273       1.1  christos valpy_str (PyObject *self)
   1274       1.1  christos {
   1275       1.1  christos   struct value_print_options opts;
   1276       1.1  christos 
   1277   1.1.1.8  christos   gdbpy_get_print_options (&opts);
   1278   1.1.1.9  christos   opts.deref_ref = false;
   1279       1.1  christos 
   1280   1.1.1.5  christos   string_file stb;
   1281   1.1.1.5  christos 
   1282   1.1.1.7  christos   try
   1283       1.1  christos     {
   1284   1.1.1.5  christos       common_val_print (((value_object *) self)->value, &stb, 0,
   1285   1.1.1.8  christos 			&opts, current_language);
   1286       1.1  christos     }
   1287   1.1.1.7  christos   catch (const gdb_exception &except)
   1288   1.1.1.3  christos     {
   1289  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1290   1.1.1.3  christos     }
   1291       1.1  christos 
   1292   1.1.1.5  christos   return PyUnicode_Decode (stb.c_str (), stb.size (), host_charset (), NULL);
   1293       1.1  christos }
   1294       1.1  christos 
   1295       1.1  christos /* Implements gdb.Value.is_optimized_out.  */
   1296       1.1  christos static PyObject *
   1297       1.1  christos valpy_get_is_optimized_out (PyObject *self, void *closure)
   1298       1.1  christos {
   1299       1.1  christos   struct value *value = ((value_object *) self)->value;
   1300       1.1  christos   int opt = 0;
   1301       1.1  christos 
   1302   1.1.1.7  christos   try
   1303       1.1  christos     {
   1304   1.1.1.9  christos       opt = value->optimized_out ();
   1305       1.1  christos     }
   1306   1.1.1.7  christos   catch (const gdb_exception &except)
   1307   1.1.1.3  christos     {
   1308  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1309   1.1.1.3  christos     }
   1310       1.1  christos 
   1311       1.1  christos   if (opt)
   1312       1.1  christos     Py_RETURN_TRUE;
   1313       1.1  christos 
   1314       1.1  christos   Py_RETURN_FALSE;
   1315       1.1  christos }
   1316       1.1  christos 
   1317       1.1  christos /* Implements gdb.Value.is_lazy.  */
   1318       1.1  christos static PyObject *
   1319       1.1  christos valpy_get_is_lazy (PyObject *self, void *closure)
   1320       1.1  christos {
   1321       1.1  christos   struct value *value = ((value_object *) self)->value;
   1322       1.1  christos   int opt = 0;
   1323       1.1  christos 
   1324   1.1.1.7  christos   try
   1325       1.1  christos     {
   1326   1.1.1.9  christos       opt = value->lazy ();
   1327       1.1  christos     }
   1328   1.1.1.7  christos   catch (const gdb_exception &except)
   1329   1.1.1.3  christos     {
   1330  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1331   1.1.1.3  christos     }
   1332       1.1  christos 
   1333       1.1  christos   if (opt)
   1334       1.1  christos     Py_RETURN_TRUE;
   1335       1.1  christos 
   1336       1.1  christos   Py_RETURN_FALSE;
   1337       1.1  christos }
   1338       1.1  christos 
   1339   1.1.1.9  christos /* Get gdb.Value.bytes attribute.  */
   1340   1.1.1.9  christos 
   1341   1.1.1.9  christos static PyObject *
   1342   1.1.1.9  christos valpy_get_bytes (PyObject *self, void *closure)
   1343   1.1.1.9  christos {
   1344   1.1.1.9  christos   value_object *value_obj = (value_object *) self;
   1345   1.1.1.9  christos   struct value *value = value_obj->value;
   1346   1.1.1.9  christos 
   1347   1.1.1.9  christos   if (value_obj->content_bytes != nullptr)
   1348   1.1.1.9  christos     {
   1349   1.1.1.9  christos       Py_INCREF (value_obj->content_bytes);
   1350   1.1.1.9  christos       return value_obj->content_bytes;
   1351   1.1.1.9  christos     }
   1352   1.1.1.9  christos 
   1353   1.1.1.9  christos   gdb::array_view<const gdb_byte> contents;
   1354   1.1.1.9  christos   try
   1355   1.1.1.9  christos     {
   1356   1.1.1.9  christos       contents = value->contents ();
   1357   1.1.1.9  christos     }
   1358   1.1.1.9  christos   catch (const gdb_exception &except)
   1359   1.1.1.9  christos     {
   1360  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1361   1.1.1.9  christos     }
   1362   1.1.1.9  christos 
   1363   1.1.1.9  christos   value_obj->content_bytes
   1364   1.1.1.9  christos     =  PyBytes_FromStringAndSize ((const char *) contents.data (),
   1365   1.1.1.9  christos 				  contents.size ());
   1366   1.1.1.9  christos   Py_XINCREF (value_obj->content_bytes);
   1367   1.1.1.9  christos   return value_obj->content_bytes;
   1368   1.1.1.9  christos }
   1369   1.1.1.9  christos 
   1370   1.1.1.9  christos /* Set gdb.Value.bytes attribute.  */
   1371   1.1.1.9  christos 
   1372   1.1.1.9  christos static int
   1373   1.1.1.9  christos valpy_set_bytes (PyObject *self_obj, PyObject *new_value_obj, void *closure)
   1374   1.1.1.9  christos {
   1375   1.1.1.9  christos   value_object *self = (value_object *) self_obj;
   1376   1.1.1.9  christos 
   1377   1.1.1.9  christos   /* Create a new value from the buffer NEW_VALUE_OBJ.  We pass true here
   1378   1.1.1.9  christos      to indicate that NEW_VALUE_OBJ must match exactly the type length.  */
   1379   1.1.1.9  christos   struct value *new_value
   1380   1.1.1.9  christos     = convert_buffer_and_type_to_value (new_value_obj, self->value->type (),
   1381   1.1.1.9  christos 					true);
   1382   1.1.1.9  christos   if (new_value == nullptr)
   1383   1.1.1.9  christos     return -1;
   1384   1.1.1.9  christos 
   1385   1.1.1.9  christos   if (!valpy_assign_core (self, new_value))
   1386   1.1.1.9  christos     return -1;
   1387   1.1.1.9  christos 
   1388   1.1.1.9  christos   return 0;
   1389   1.1.1.9  christos }
   1390   1.1.1.9  christos 
   1391       1.1  christos /* Implements gdb.Value.fetch_lazy ().  */
   1392       1.1  christos static PyObject *
   1393       1.1  christos valpy_fetch_lazy (PyObject *self, PyObject *args)
   1394       1.1  christos {
   1395       1.1  christos   struct value *value = ((value_object *) self)->value;
   1396       1.1  christos 
   1397   1.1.1.7  christos   try
   1398       1.1  christos     {
   1399   1.1.1.9  christos       if (value->lazy ())
   1400   1.1.1.9  christos 	value->fetch_lazy ();
   1401       1.1  christos     }
   1402   1.1.1.7  christos   catch (const gdb_exception &except)
   1403   1.1.1.3  christos     {
   1404  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1405   1.1.1.3  christos     }
   1406       1.1  christos 
   1407       1.1  christos   Py_RETURN_NONE;
   1408       1.1  christos }
   1409       1.1  christos 
   1410       1.1  christos /* Calculate and return the address of the PyObject as the value of
   1411       1.1  christos    the builtin __hash__ call.  */
   1412   1.1.1.3  christos static Py_hash_t
   1413       1.1  christos valpy_hash (PyObject *self)
   1414       1.1  christos {
   1415   1.1.1.3  christos   return (intptr_t) self;
   1416       1.1  christos }
   1417       1.1  christos 
   1418       1.1  christos enum valpy_opcode
   1419       1.1  christos {
   1420       1.1  christos   VALPY_ADD,
   1421       1.1  christos   VALPY_SUB,
   1422       1.1  christos   VALPY_MUL,
   1423       1.1  christos   VALPY_DIV,
   1424       1.1  christos   VALPY_REM,
   1425       1.1  christos   VALPY_POW,
   1426       1.1  christos   VALPY_LSH,
   1427       1.1  christos   VALPY_RSH,
   1428       1.1  christos   VALPY_BITAND,
   1429       1.1  christos   VALPY_BITOR,
   1430       1.1  christos   VALPY_BITXOR
   1431       1.1  christos };
   1432       1.1  christos 
   1433       1.1  christos /* If TYPE is a reference, return the target; otherwise return TYPE.  */
   1434       1.1  christos #define STRIP_REFERENCE(TYPE) \
   1435   1.1.1.8  christos   (TYPE_IS_REFERENCE (TYPE) ? ((TYPE)->target_type ()) : (TYPE))
   1436       1.1  christos 
   1437   1.1.1.4  christos /* Helper for valpy_binop.  Returns a value object which is the result
   1438   1.1.1.4  christos    of applying the operation specified by OPCODE to the given
   1439   1.1.1.4  christos    arguments.  Throws a GDB exception on error.  */
   1440   1.1.1.4  christos 
   1441       1.1  christos static PyObject *
   1442   1.1.1.4  christos valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other)
   1443       1.1  christos {
   1444       1.1  christos   PyObject *result = NULL;
   1445       1.1  christos 
   1446   1.1.1.4  christos   struct value *arg1, *arg2;
   1447   1.1.1.4  christos   struct value *res_val = NULL;
   1448   1.1.1.4  christos   enum exp_opcode op = OP_NULL;
   1449   1.1.1.4  christos   int handled = 0;
   1450   1.1.1.4  christos 
   1451   1.1.1.5  christos   scoped_value_mark free_values;
   1452   1.1.1.5  christos 
   1453   1.1.1.4  christos   /* If the gdb.Value object is the second operand, then it will be
   1454   1.1.1.4  christos      passed to us as the OTHER argument, and SELF will be an entirely
   1455   1.1.1.4  christos      different kind of object, altogether.  Because of this, we can't
   1456   1.1.1.4  christos      assume self is a gdb.Value object and need to convert it from
   1457   1.1.1.4  christos      python as well.  */
   1458   1.1.1.4  christos   arg1 = convert_value_from_python (self);
   1459   1.1.1.4  christos   if (arg1 == NULL)
   1460   1.1.1.5  christos     return NULL;
   1461       1.1  christos 
   1462   1.1.1.4  christos   arg2 = convert_value_from_python (other);
   1463   1.1.1.4  christos   if (arg2 == NULL)
   1464   1.1.1.5  christos     return NULL;
   1465       1.1  christos 
   1466   1.1.1.4  christos   switch (opcode)
   1467   1.1.1.4  christos     {
   1468   1.1.1.4  christos     case VALPY_ADD:
   1469   1.1.1.4  christos       {
   1470   1.1.1.9  christos 	struct type *ltype = arg1->type ();
   1471   1.1.1.9  christos 	struct type *rtype = arg2->type ();
   1472   1.1.1.4  christos 
   1473   1.1.1.4  christos 	ltype = check_typedef (ltype);
   1474   1.1.1.4  christos 	ltype = STRIP_REFERENCE (ltype);
   1475   1.1.1.4  christos 	rtype = check_typedef (rtype);
   1476   1.1.1.4  christos 	rtype = STRIP_REFERENCE (rtype);
   1477   1.1.1.4  christos 
   1478   1.1.1.4  christos 	handled = 1;
   1479   1.1.1.7  christos 	if (ltype->code () == TYPE_CODE_PTR
   1480   1.1.1.4  christos 	    && is_integral_type (rtype))
   1481   1.1.1.4  christos 	  res_val = value_ptradd (arg1, value_as_long (arg2));
   1482   1.1.1.7  christos 	else if (rtype->code () == TYPE_CODE_PTR
   1483   1.1.1.4  christos 		 && is_integral_type (ltype))
   1484   1.1.1.4  christos 	  res_val = value_ptradd (arg2, value_as_long (arg1));
   1485   1.1.1.4  christos 	else
   1486       1.1  christos 	  {
   1487   1.1.1.4  christos 	    handled = 0;
   1488   1.1.1.4  christos 	    op = BINOP_ADD;
   1489       1.1  christos 	  }
   1490   1.1.1.4  christos       }
   1491   1.1.1.4  christos       break;
   1492   1.1.1.4  christos     case VALPY_SUB:
   1493   1.1.1.4  christos       {
   1494   1.1.1.9  christos 	struct type *ltype = arg1->type ();
   1495   1.1.1.9  christos 	struct type *rtype = arg2->type ();
   1496   1.1.1.4  christos 
   1497   1.1.1.4  christos 	ltype = check_typedef (ltype);
   1498   1.1.1.4  christos 	ltype = STRIP_REFERENCE (ltype);
   1499   1.1.1.4  christos 	rtype = check_typedef (rtype);
   1500   1.1.1.4  christos 	rtype = STRIP_REFERENCE (rtype);
   1501   1.1.1.4  christos 
   1502   1.1.1.4  christos 	handled = 1;
   1503   1.1.1.7  christos 	if (ltype->code () == TYPE_CODE_PTR
   1504   1.1.1.7  christos 	    && rtype->code () == TYPE_CODE_PTR)
   1505   1.1.1.4  christos 	  /* A ptrdiff_t for the target would be preferable here.  */
   1506   1.1.1.4  christos 	  res_val = value_from_longest (builtin_type_pyint,
   1507   1.1.1.4  christos 					value_ptrdiff (arg1, arg2));
   1508   1.1.1.7  christos 	else if (ltype->code () == TYPE_CODE_PTR
   1509   1.1.1.4  christos 		 && is_integral_type (rtype))
   1510   1.1.1.4  christos 	  res_val = value_ptradd (arg1, - value_as_long (arg2));
   1511   1.1.1.4  christos 	else
   1512       1.1  christos 	  {
   1513   1.1.1.4  christos 	    handled = 0;
   1514   1.1.1.4  christos 	    op = BINOP_SUB;
   1515       1.1  christos 	  }
   1516   1.1.1.4  christos       }
   1517   1.1.1.4  christos       break;
   1518   1.1.1.4  christos     case VALPY_MUL:
   1519   1.1.1.4  christos       op = BINOP_MUL;
   1520   1.1.1.4  christos       break;
   1521   1.1.1.4  christos     case VALPY_DIV:
   1522   1.1.1.4  christos       op = BINOP_DIV;
   1523   1.1.1.4  christos       break;
   1524   1.1.1.4  christos     case VALPY_REM:
   1525   1.1.1.4  christos       op = BINOP_REM;
   1526   1.1.1.4  christos       break;
   1527   1.1.1.4  christos     case VALPY_POW:
   1528   1.1.1.4  christos       op = BINOP_EXP;
   1529   1.1.1.4  christos       break;
   1530   1.1.1.4  christos     case VALPY_LSH:
   1531   1.1.1.4  christos       op = BINOP_LSH;
   1532   1.1.1.4  christos       break;
   1533   1.1.1.4  christos     case VALPY_RSH:
   1534   1.1.1.4  christos       op = BINOP_RSH;
   1535   1.1.1.4  christos       break;
   1536   1.1.1.4  christos     case VALPY_BITAND:
   1537   1.1.1.4  christos       op = BINOP_BITWISE_AND;
   1538   1.1.1.4  christos       break;
   1539   1.1.1.4  christos     case VALPY_BITOR:
   1540   1.1.1.4  christos       op = BINOP_BITWISE_IOR;
   1541   1.1.1.4  christos       break;
   1542   1.1.1.4  christos     case VALPY_BITXOR:
   1543   1.1.1.4  christos       op = BINOP_BITWISE_XOR;
   1544   1.1.1.4  christos       break;
   1545   1.1.1.4  christos     }
   1546       1.1  christos 
   1547   1.1.1.4  christos   if (!handled)
   1548   1.1.1.4  christos     {
   1549   1.1.1.4  christos       if (binop_user_defined_p (op, arg1, arg2))
   1550   1.1.1.4  christos 	res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL);
   1551   1.1.1.4  christos       else
   1552   1.1.1.4  christos 	res_val = value_binop (arg1, arg2, op);
   1553   1.1.1.4  christos     }
   1554   1.1.1.2  christos 
   1555   1.1.1.4  christos   if (res_val)
   1556   1.1.1.4  christos     result = value_to_value_object (res_val);
   1557       1.1  christos 
   1558   1.1.1.4  christos   return result;
   1559   1.1.1.4  christos }
   1560   1.1.1.4  christos 
   1561   1.1.1.4  christos /* Returns a value object which is the result of applying the operation
   1562   1.1.1.4  christos    specified by OPCODE to the given arguments.  Returns NULL on error, with
   1563   1.1.1.4  christos    a python exception set.  */
   1564   1.1.1.4  christos static PyObject *
   1565   1.1.1.4  christos valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
   1566   1.1.1.4  christos {
   1567   1.1.1.4  christos   PyObject *result = NULL;
   1568   1.1.1.4  christos 
   1569   1.1.1.7  christos   try
   1570   1.1.1.4  christos     {
   1571   1.1.1.4  christos       result = valpy_binop_throw (opcode, self, other);
   1572       1.1  christos     }
   1573   1.1.1.7  christos   catch (const gdb_exception &except)
   1574   1.1.1.3  christos     {
   1575  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1576   1.1.1.3  christos     }
   1577       1.1  christos 
   1578       1.1  christos   return result;
   1579       1.1  christos }
   1580       1.1  christos 
   1581       1.1  christos static PyObject *
   1582       1.1  christos valpy_add (PyObject *self, PyObject *other)
   1583       1.1  christos {
   1584       1.1  christos   return valpy_binop (VALPY_ADD, self, other);
   1585       1.1  christos }
   1586       1.1  christos 
   1587       1.1  christos static PyObject *
   1588       1.1  christos valpy_subtract (PyObject *self, PyObject *other)
   1589       1.1  christos {
   1590       1.1  christos   return valpy_binop (VALPY_SUB, self, other);
   1591       1.1  christos }
   1592       1.1  christos 
   1593       1.1  christos static PyObject *
   1594       1.1  christos valpy_multiply (PyObject *self, PyObject *other)
   1595       1.1  christos {
   1596       1.1  christos   return valpy_binop (VALPY_MUL, self, other);
   1597       1.1  christos }
   1598       1.1  christos 
   1599       1.1  christos static PyObject *
   1600       1.1  christos valpy_divide (PyObject *self, PyObject *other)
   1601       1.1  christos {
   1602       1.1  christos   return valpy_binop (VALPY_DIV, self, other);
   1603       1.1  christos }
   1604       1.1  christos 
   1605       1.1  christos static PyObject *
   1606       1.1  christos valpy_remainder (PyObject *self, PyObject *other)
   1607       1.1  christos {
   1608       1.1  christos   return valpy_binop (VALPY_REM, self, other);
   1609       1.1  christos }
   1610       1.1  christos 
   1611       1.1  christos static PyObject *
   1612       1.1  christos valpy_power (PyObject *self, PyObject *other, PyObject *unused)
   1613       1.1  christos {
   1614       1.1  christos   /* We don't support the ternary form of pow.  I don't know how to express
   1615       1.1  christos      that, so let's just throw NotImplementedError to at least do something
   1616       1.1  christos      about it.  */
   1617       1.1  christos   if (unused != Py_None)
   1618       1.1  christos     {
   1619       1.1  christos       PyErr_SetString (PyExc_NotImplementedError,
   1620       1.1  christos 		       "Invalid operation on gdb.Value.");
   1621       1.1  christos       return NULL;
   1622       1.1  christos     }
   1623       1.1  christos 
   1624       1.1  christos   return valpy_binop (VALPY_POW, self, other);
   1625       1.1  christos }
   1626       1.1  christos 
   1627       1.1  christos static PyObject *
   1628       1.1  christos valpy_negative (PyObject *self)
   1629       1.1  christos {
   1630       1.1  christos   PyObject *result = NULL;
   1631       1.1  christos 
   1632   1.1.1.7  christos   try
   1633       1.1  christos     {
   1634       1.1  christos       /* Perhaps overkill, but consistency has some virtue.  */
   1635   1.1.1.5  christos       scoped_value_mark free_values;
   1636       1.1  christos       struct value *val;
   1637       1.1  christos 
   1638       1.1  christos       val = value_neg (((value_object *) self)->value);
   1639       1.1  christos       result = value_to_value_object (val);
   1640       1.1  christos     }
   1641   1.1.1.7  christos   catch (const gdb_exception &except)
   1642   1.1.1.3  christos     {
   1643  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1644   1.1.1.3  christos     }
   1645       1.1  christos 
   1646       1.1  christos   return result;
   1647       1.1  christos }
   1648       1.1  christos 
   1649       1.1  christos static PyObject *
   1650       1.1  christos valpy_positive (PyObject *self)
   1651       1.1  christos {
   1652       1.1  christos   return value_to_value_object (((value_object *) self)->value);
   1653       1.1  christos }
   1654       1.1  christos 
   1655       1.1  christos static PyObject *
   1656       1.1  christos valpy_absolute (PyObject *self)
   1657       1.1  christos {
   1658       1.1  christos   struct value *value = ((value_object *) self)->value;
   1659       1.1  christos   int isabs = 1;
   1660       1.1  christos 
   1661   1.1.1.7  christos   try
   1662       1.1  christos     {
   1663   1.1.1.5  christos       scoped_value_mark free_values;
   1664       1.1  christos 
   1665   1.1.1.9  christos       if (value_less (value, value::zero (value->type (), not_lval)))
   1666       1.1  christos 	isabs = 0;
   1667       1.1  christos     }
   1668   1.1.1.7  christos   catch (const gdb_exception &except)
   1669   1.1.1.3  christos     {
   1670  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1671   1.1.1.3  christos     }
   1672       1.1  christos 
   1673       1.1  christos   if (isabs)
   1674       1.1  christos     return valpy_positive (self);
   1675       1.1  christos   else
   1676       1.1  christos     return valpy_negative (self);
   1677       1.1  christos }
   1678       1.1  christos 
   1679       1.1  christos /* Implements boolean evaluation of gdb.Value.  */
   1680       1.1  christos static int
   1681       1.1  christos valpy_nonzero (PyObject *self)
   1682       1.1  christos {
   1683       1.1  christos   value_object *self_value = (value_object *) self;
   1684       1.1  christos   struct type *type;
   1685       1.1  christos   int nonzero = 0; /* Appease GCC warning.  */
   1686       1.1  christos 
   1687   1.1.1.7  christos   try
   1688       1.1  christos     {
   1689   1.1.1.9  christos       type = check_typedef (self_value->value->type ());
   1690       1.1  christos 
   1691   1.1.1.7  christos       if (is_integral_type (type) || type->code () == TYPE_CODE_PTR)
   1692       1.1  christos 	nonzero = !!value_as_long (self_value->value);
   1693   1.1.1.6  christos       else if (is_floating_value (self_value->value))
   1694   1.1.1.8  christos 	nonzero = !target_float_is_zero
   1695   1.1.1.9  christos 	  (self_value->value->contents ().data (), type);
   1696       1.1  christos       else
   1697       1.1  christos 	/* All other values are True.  */
   1698       1.1  christos 	nonzero = 1;
   1699       1.1  christos     }
   1700  1.1.1.10  christos   catch (const gdb_exception &ex)
   1701   1.1.1.3  christos     {
   1702   1.1.1.9  christos       /* This is not documented in the Python documentation, but if
   1703   1.1.1.9  christos 	 this function fails, return -1 as slot_nb_nonzero does (the
   1704   1.1.1.9  christos 	 default Python nonzero function).  */
   1705  1.1.1.10  christos       return gdbpy_handle_gdb_exception (-1, ex);
   1706   1.1.1.3  christos     }
   1707   1.1.1.3  christos 
   1708       1.1  christos   return nonzero;
   1709       1.1  christos }
   1710       1.1  christos 
   1711       1.1  christos /* Implements ~ for value objects.  */
   1712       1.1  christos static PyObject *
   1713       1.1  christos valpy_invert (PyObject *self)
   1714       1.1  christos {
   1715   1.1.1.9  christos   PyObject *result = nullptr;
   1716       1.1  christos 
   1717   1.1.1.7  christos   try
   1718       1.1  christos     {
   1719   1.1.1.9  christos       scoped_value_mark free_values;
   1720   1.1.1.9  christos       struct value *val = value_complement (((value_object *) self)->value);
   1721   1.1.1.9  christos       result = value_to_value_object (val);
   1722       1.1  christos     }
   1723   1.1.1.7  christos   catch (const gdb_exception &except)
   1724   1.1.1.3  christos     {
   1725  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1726   1.1.1.3  christos     }
   1727       1.1  christos 
   1728   1.1.1.9  christos   return result;
   1729       1.1  christos }
   1730       1.1  christos 
   1731       1.1  christos /* Implements left shift for value objects.  */
   1732       1.1  christos static PyObject *
   1733       1.1  christos valpy_lsh (PyObject *self, PyObject *other)
   1734       1.1  christos {
   1735       1.1  christos   return valpy_binop (VALPY_LSH, self, other);
   1736       1.1  christos }
   1737       1.1  christos 
   1738       1.1  christos /* Implements right shift for value objects.  */
   1739       1.1  christos static PyObject *
   1740       1.1  christos valpy_rsh (PyObject *self, PyObject *other)
   1741       1.1  christos {
   1742       1.1  christos   return valpy_binop (VALPY_RSH, self, other);
   1743       1.1  christos }
   1744       1.1  christos 
   1745       1.1  christos /* Implements bitwise and for value objects.  */
   1746       1.1  christos static PyObject *
   1747       1.1  christos valpy_and (PyObject *self, PyObject *other)
   1748       1.1  christos {
   1749       1.1  christos   return valpy_binop (VALPY_BITAND, self, other);
   1750       1.1  christos }
   1751       1.1  christos 
   1752       1.1  christos /* Implements bitwise or for value objects.  */
   1753       1.1  christos static PyObject *
   1754       1.1  christos valpy_or (PyObject *self, PyObject *other)
   1755       1.1  christos {
   1756       1.1  christos   return valpy_binop (VALPY_BITOR, self, other);
   1757       1.1  christos }
   1758       1.1  christos 
   1759       1.1  christos /* Implements bitwise xor for value objects.  */
   1760       1.1  christos static PyObject *
   1761       1.1  christos valpy_xor (PyObject *self, PyObject *other)
   1762       1.1  christos {
   1763       1.1  christos   return valpy_binop (VALPY_BITXOR, self, other);
   1764       1.1  christos }
   1765       1.1  christos 
   1766   1.1.1.4  christos /* Helper for valpy_richcompare.  Implements comparison operations for
   1767   1.1.1.4  christos    value objects.  Returns true/false on success.  Returns -1 with a
   1768   1.1.1.4  christos    Python exception set if a Python error is detected.  Throws a GDB
   1769   1.1.1.4  christos    exception on other errors (memory error, etc.).  */
   1770   1.1.1.4  christos 
   1771   1.1.1.4  christos static int
   1772   1.1.1.4  christos valpy_richcompare_throw (PyObject *self, PyObject *other, int op)
   1773   1.1.1.4  christos {
   1774   1.1.1.4  christos   int result;
   1775   1.1.1.4  christos   struct value *value_other;
   1776   1.1.1.4  christos   struct value *value_self;
   1777   1.1.1.5  christos 
   1778   1.1.1.5  christos   scoped_value_mark free_values;
   1779   1.1.1.4  christos 
   1780   1.1.1.4  christos   value_other = convert_value_from_python (other);
   1781   1.1.1.4  christos   if (value_other == NULL)
   1782   1.1.1.4  christos     return -1;
   1783   1.1.1.4  christos 
   1784   1.1.1.4  christos   value_self = ((value_object *) self)->value;
   1785   1.1.1.4  christos 
   1786   1.1.1.4  christos   switch (op)
   1787   1.1.1.4  christos     {
   1788   1.1.1.4  christos     case Py_LT:
   1789   1.1.1.4  christos       result = value_less (value_self, value_other);
   1790   1.1.1.4  christos       break;
   1791   1.1.1.4  christos     case Py_LE:
   1792   1.1.1.4  christos       result = value_less (value_self, value_other)
   1793   1.1.1.4  christos 	|| value_equal (value_self, value_other);
   1794   1.1.1.4  christos       break;
   1795   1.1.1.4  christos     case Py_EQ:
   1796   1.1.1.4  christos       result = value_equal (value_self, value_other);
   1797   1.1.1.4  christos       break;
   1798   1.1.1.4  christos     case Py_NE:
   1799   1.1.1.4  christos       result = !value_equal (value_self, value_other);
   1800   1.1.1.4  christos       break;
   1801   1.1.1.4  christos     case Py_GT:
   1802   1.1.1.4  christos       result = value_less (value_other, value_self);
   1803   1.1.1.4  christos       break;
   1804   1.1.1.4  christos     case Py_GE:
   1805   1.1.1.4  christos       result = (value_less (value_other, value_self)
   1806   1.1.1.4  christos 		|| value_equal (value_self, value_other));
   1807   1.1.1.4  christos       break;
   1808   1.1.1.4  christos     default:
   1809   1.1.1.4  christos       /* Can't happen.  */
   1810   1.1.1.4  christos       PyErr_SetString (PyExc_NotImplementedError,
   1811   1.1.1.4  christos 		       _("Invalid operation on gdb.Value."));
   1812   1.1.1.4  christos       result = -1;
   1813   1.1.1.4  christos       break;
   1814   1.1.1.4  christos     }
   1815   1.1.1.4  christos 
   1816   1.1.1.4  christos   return result;
   1817   1.1.1.4  christos }
   1818   1.1.1.4  christos 
   1819   1.1.1.4  christos 
   1820       1.1  christos /* Implements comparison operations for value objects.  Returns NULL on error,
   1821       1.1  christos    with a python exception set.  */
   1822       1.1  christos static PyObject *
   1823       1.1  christos valpy_richcompare (PyObject *self, PyObject *other, int op)
   1824       1.1  christos {
   1825       1.1  christos   int result = 0;
   1826       1.1  christos 
   1827       1.1  christos   if (other == Py_None)
   1828       1.1  christos     /* Comparing with None is special.  From what I can tell, in Python
   1829       1.1  christos        None is smaller than anything else.  */
   1830       1.1  christos     switch (op) {
   1831       1.1  christos       case Py_LT:
   1832       1.1  christos       case Py_LE:
   1833       1.1  christos       case Py_EQ:
   1834       1.1  christos 	Py_RETURN_FALSE;
   1835       1.1  christos       case Py_NE:
   1836       1.1  christos       case Py_GT:
   1837       1.1  christos       case Py_GE:
   1838       1.1  christos 	Py_RETURN_TRUE;
   1839       1.1  christos       default:
   1840       1.1  christos 	/* Can't happen.  */
   1841       1.1  christos 	PyErr_SetString (PyExc_NotImplementedError,
   1842       1.1  christos 			 _("Invalid operation on gdb.Value."));
   1843       1.1  christos 	return NULL;
   1844       1.1  christos     }
   1845       1.1  christos 
   1846   1.1.1.7  christos   try
   1847       1.1  christos     {
   1848   1.1.1.4  christos       result = valpy_richcompare_throw (self, other, op);
   1849       1.1  christos     }
   1850   1.1.1.7  christos   catch (const gdb_exception &except)
   1851   1.1.1.3  christos     {
   1852  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1853   1.1.1.3  christos     }
   1854       1.1  christos 
   1855       1.1  christos   /* In this case, the Python exception has already been set.  */
   1856       1.1  christos   if (result < 0)
   1857       1.1  christos     return NULL;
   1858       1.1  christos 
   1859       1.1  christos   if (result == 1)
   1860       1.1  christos     Py_RETURN_TRUE;
   1861       1.1  christos 
   1862       1.1  christos   Py_RETURN_FALSE;
   1863       1.1  christos }
   1864       1.1  christos 
   1865       1.1  christos /* Implements conversion to long.  */
   1866       1.1  christos static PyObject *
   1867       1.1  christos valpy_long (PyObject *self)
   1868       1.1  christos {
   1869       1.1  christos   struct value *value = ((value_object *) self)->value;
   1870   1.1.1.9  christos   struct type *type = value->type ();
   1871       1.1  christos   LONGEST l = 0;
   1872       1.1  christos 
   1873   1.1.1.7  christos   try
   1874       1.1  christos     {
   1875   1.1.1.6  christos       if (is_floating_value (value))
   1876   1.1.1.6  christos 	{
   1877   1.1.1.6  christos 	  type = builtin_type_pylong;
   1878   1.1.1.6  christos 	  value = value_cast (type, value);
   1879   1.1.1.6  christos 	}
   1880   1.1.1.6  christos 
   1881   1.1.1.4  christos       type = check_typedef (type);
   1882       1.1  christos 
   1883       1.1  christos       if (!is_integral_type (type)
   1884   1.1.1.7  christos 	  && type->code () != TYPE_CODE_PTR)
   1885       1.1  christos 	error (_("Cannot convert value to long."));
   1886       1.1  christos 
   1887       1.1  christos       l = value_as_long (value);
   1888       1.1  christos     }
   1889   1.1.1.7  christos   catch (const gdb_exception &except)
   1890   1.1.1.3  christos     {
   1891  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1892   1.1.1.3  christos     }
   1893       1.1  christos 
   1894   1.1.1.8  christos   if (type->is_unsigned ())
   1895   1.1.1.8  christos     return gdb_py_object_from_ulongest (l).release ();
   1896   1.1.1.5  christos   else
   1897   1.1.1.8  christos     return gdb_py_object_from_longest (l).release ();
   1898       1.1  christos }
   1899       1.1  christos 
   1900       1.1  christos /* Implements conversion to float.  */
   1901       1.1  christos static PyObject *
   1902       1.1  christos valpy_float (PyObject *self)
   1903       1.1  christos {
   1904       1.1  christos   struct value *value = ((value_object *) self)->value;
   1905   1.1.1.9  christos   struct type *type = value->type ();
   1906       1.1  christos   double d = 0;
   1907       1.1  christos 
   1908   1.1.1.7  christos   try
   1909       1.1  christos     {
   1910   1.1.1.4  christos       type = check_typedef (type);
   1911       1.1  christos 
   1912   1.1.1.7  christos       if (type->code () == TYPE_CODE_FLT && is_floating_value (value))
   1913   1.1.1.9  christos 	d = target_float_to_host_double (value->contents ().data (), type);
   1914   1.1.1.7  christos       else if (type->code () == TYPE_CODE_INT)
   1915   1.1.1.6  christos 	{
   1916   1.1.1.6  christos 	  /* Note that valpy_long accepts TYPE_CODE_PTR and some
   1917   1.1.1.6  christos 	     others here here -- but casting a pointer or bool to a
   1918   1.1.1.6  christos 	     float seems wrong.  */
   1919   1.1.1.6  christos 	  d = value_as_long (value);
   1920   1.1.1.6  christos 	}
   1921   1.1.1.6  christos       else
   1922       1.1  christos 	error (_("Cannot convert value to float."));
   1923       1.1  christos     }
   1924   1.1.1.7  christos   catch (const gdb_exception &except)
   1925   1.1.1.3  christos     {
   1926  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   1927   1.1.1.3  christos     }
   1928       1.1  christos 
   1929       1.1  christos   return PyFloat_FromDouble (d);
   1930       1.1  christos }
   1931       1.1  christos 
   1932   1.1.1.9  christos /* Returns an object for a value, without releasing it from the
   1933   1.1.1.7  christos    all_values chain.  */
   1934   1.1.1.7  christos PyObject *
   1935   1.1.1.9  christos value_to_value_object (struct value *val)
   1936   1.1.1.7  christos {
   1937   1.1.1.7  christos   value_object *val_obj;
   1938   1.1.1.7  christos 
   1939   1.1.1.7  christos   val_obj = PyObject_New (value_object, &value_object_type);
   1940   1.1.1.7  christos   if (val_obj != NULL)
   1941   1.1.1.7  christos     {
   1942   1.1.1.9  christos       val->incref ();
   1943   1.1.1.7  christos       val_obj->value = val;
   1944   1.1.1.8  christos       val_obj->next = nullptr;
   1945   1.1.1.8  christos       val_obj->prev = nullptr;
   1946   1.1.1.7  christos       val_obj->address = NULL;
   1947   1.1.1.7  christos       val_obj->type = NULL;
   1948   1.1.1.7  christos       val_obj->dynamic_type = NULL;
   1949   1.1.1.9  christos       val_obj->content_bytes = nullptr;
   1950   1.1.1.7  christos       note_value (val_obj);
   1951   1.1.1.7  christos     }
   1952   1.1.1.7  christos 
   1953   1.1.1.7  christos   return (PyObject *) val_obj;
   1954   1.1.1.7  christos }
   1955   1.1.1.7  christos 
   1956       1.1  christos /* Returns a borrowed reference to the struct value corresponding to
   1957       1.1  christos    the given value object.  */
   1958       1.1  christos struct value *
   1959       1.1  christos value_object_to_value (PyObject *self)
   1960       1.1  christos {
   1961       1.1  christos   value_object *real;
   1962       1.1  christos 
   1963       1.1  christos   if (! PyObject_TypeCheck (self, &value_object_type))
   1964       1.1  christos     return NULL;
   1965       1.1  christos   real = (value_object *) self;
   1966       1.1  christos   return real->value;
   1967       1.1  christos }
   1968       1.1  christos 
   1969       1.1  christos /* Try to convert a Python value to a gdb value.  If the value cannot
   1970       1.1  christos    be converted, set a Python exception and return NULL.  Returns a
   1971       1.1  christos    reference to a new value on the all_values chain.  */
   1972       1.1  christos 
   1973       1.1  christos struct value *
   1974       1.1  christos convert_value_from_python (PyObject *obj)
   1975       1.1  christos {
   1976       1.1  christos   struct value *value = NULL; /* -Wall */
   1977       1.1  christos   int cmp;
   1978       1.1  christos 
   1979       1.1  christos   gdb_assert (obj != NULL);
   1980       1.1  christos 
   1981   1.1.1.7  christos   try
   1982       1.1  christos     {
   1983       1.1  christos       if (PyBool_Check (obj))
   1984       1.1  christos 	{
   1985  1.1.1.10  christos 	  cmp = obj == Py_True ? 1 : 0;
   1986  1.1.1.10  christos 	  value = value_from_longest (builtin_type_pybool, cmp);
   1987       1.1  christos 	}
   1988       1.1  christos       else if (PyLong_Check (obj))
   1989       1.1  christos 	{
   1990       1.1  christos 	  LONGEST l = PyLong_AsLongLong (obj);
   1991       1.1  christos 
   1992       1.1  christos 	  if (PyErr_Occurred ())
   1993       1.1  christos 	    {
   1994       1.1  christos 	      /* If the error was an overflow, we can try converting to
   1995   1.1.1.8  christos 		 ULONGEST instead.  */
   1996       1.1  christos 	      if (PyErr_ExceptionMatches (PyExc_OverflowError))
   1997       1.1  christos 		{
   1998   1.1.1.6  christos 		  gdbpy_err_fetch fetched_error;
   1999   1.1.1.8  christos 		  gdbpy_ref<> zero = gdb_py_object_from_longest (0);
   2000       1.1  christos 
   2001       1.1  christos 		  /* Check whether obj is positive.  */
   2002   1.1.1.5  christos 		  if (PyObject_RichCompareBool (obj, zero.get (), Py_GT) > 0)
   2003       1.1  christos 		    {
   2004       1.1  christos 		      ULONGEST ul;
   2005       1.1  christos 
   2006       1.1  christos 		      ul = PyLong_AsUnsignedLongLong (obj);
   2007       1.1  christos 		      if (! PyErr_Occurred ())
   2008       1.1  christos 			value = value_from_ulongest (builtin_type_upylong, ul);
   2009       1.1  christos 		    }
   2010       1.1  christos 		  else
   2011   1.1.1.6  christos 		    {
   2012   1.1.1.6  christos 		      /* There's nothing we can do.  */
   2013   1.1.1.6  christos 		      fetched_error.restore ();
   2014   1.1.1.6  christos 		    }
   2015       1.1  christos 		}
   2016       1.1  christos 	    }
   2017       1.1  christos 	  else
   2018       1.1  christos 	    value = value_from_longest (builtin_type_pylong, l);
   2019       1.1  christos 	}
   2020       1.1  christos       else if (PyFloat_Check (obj))
   2021       1.1  christos 	{
   2022       1.1  christos 	  double d = PyFloat_AsDouble (obj);
   2023       1.1  christos 
   2024       1.1  christos 	  if (! PyErr_Occurred ())
   2025   1.1.1.7  christos 	    value = value_from_host_double (builtin_type_pyfloat, d);
   2026       1.1  christos 	}
   2027       1.1  christos       else if (gdbpy_is_string (obj))
   2028       1.1  christos 	{
   2029   1.1.1.5  christos 	  gdb::unique_xmalloc_ptr<char> s
   2030   1.1.1.5  christos 	    = python_string_to_target_string (obj);
   2031       1.1  christos 	  if (s != NULL)
   2032   1.1.1.9  christos 	    value
   2033   1.1.1.9  christos 	      = current_language->value_string (gdbpy_enter::get_gdbarch (),
   2034   1.1.1.9  christos 						s.get (), strlen (s.get ()));
   2035       1.1  christos 	}
   2036       1.1  christos       else if (PyObject_TypeCheck (obj, &value_object_type))
   2037   1.1.1.9  christos 	value = ((value_object *) obj)->value->copy ();
   2038       1.1  christos       else if (gdbpy_is_lazy_string (obj))
   2039       1.1  christos 	{
   2040       1.1  christos 	  PyObject *result;
   2041       1.1  christos 
   2042       1.1  christos 	  result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst,  NULL);
   2043   1.1.1.9  christos 	  value = ((value_object *) result)->value->copy ();
   2044       1.1  christos 	}
   2045       1.1  christos       else
   2046       1.1  christos 	PyErr_Format (PyExc_TypeError,
   2047       1.1  christos 		      _("Could not convert Python object: %S."), obj);
   2048       1.1  christos     }
   2049   1.1.1.7  christos   catch (const gdb_exception &except)
   2050       1.1  christos     {
   2051  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   2052       1.1  christos     }
   2053       1.1  christos 
   2054       1.1  christos   return value;
   2055       1.1  christos }
   2056       1.1  christos 
   2057       1.1  christos /* Returns value object in the ARGth position in GDB's history.  */
   2058       1.1  christos PyObject *
   2059       1.1  christos gdbpy_history (PyObject *self, PyObject *args)
   2060       1.1  christos {
   2061       1.1  christos   int i;
   2062       1.1  christos 
   2063       1.1  christos   if (!PyArg_ParseTuple (args, "i", &i))
   2064       1.1  christos     return NULL;
   2065       1.1  christos 
   2066   1.1.1.9  christos   PyObject *result = nullptr;
   2067   1.1.1.7  christos   try
   2068       1.1  christos     {
   2069   1.1.1.9  christos       scoped_value_mark free_values;
   2070   1.1.1.9  christos       struct value *res_val = access_value_history (i);
   2071   1.1.1.9  christos       result = value_to_value_object (res_val);
   2072       1.1  christos     }
   2073   1.1.1.7  christos   catch (const gdb_exception &except)
   2074   1.1.1.3  christos     {
   2075  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   2076   1.1.1.3  christos     }
   2077       1.1  christos 
   2078   1.1.1.9  christos   return result;
   2079       1.1  christos }
   2080       1.1  christos 
   2081   1.1.1.8  christos /* Add a gdb.Value into GDB's history, and return (as an integer) the
   2082   1.1.1.8  christos    position of the newly added value.  */
   2083   1.1.1.8  christos PyObject *
   2084   1.1.1.8  christos gdbpy_add_history (PyObject *self, PyObject *args)
   2085   1.1.1.8  christos {
   2086   1.1.1.8  christos   PyObject *value_obj;
   2087   1.1.1.8  christos 
   2088   1.1.1.8  christos   if (!PyArg_ParseTuple (args, "O", &value_obj))
   2089   1.1.1.8  christos     return nullptr;
   2090   1.1.1.8  christos 
   2091   1.1.1.8  christos   struct value *value = convert_value_from_python (value_obj);
   2092   1.1.1.8  christos   if (value == nullptr)
   2093   1.1.1.8  christos     return nullptr;
   2094   1.1.1.8  christos 
   2095   1.1.1.8  christos   try
   2096   1.1.1.8  christos     {
   2097   1.1.1.9  christos       int idx = value->record_latest ();
   2098   1.1.1.8  christos       return gdb_py_object_from_longest (idx).release ();
   2099   1.1.1.8  christos     }
   2100   1.1.1.8  christos   catch (const gdb_exception &except)
   2101   1.1.1.8  christos     {
   2102  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   2103   1.1.1.8  christos     }
   2104   1.1.1.8  christos 
   2105   1.1.1.8  christos   return nullptr;
   2106   1.1.1.8  christos }
   2107   1.1.1.8  christos 
   2108   1.1.1.8  christos /* Return an integer, the number of items in GDB's history.  */
   2109   1.1.1.8  christos 
   2110   1.1.1.8  christos PyObject *
   2111   1.1.1.8  christos gdbpy_history_count (PyObject *self, PyObject *args)
   2112   1.1.1.8  christos {
   2113   1.1.1.8  christos   return gdb_py_object_from_ulongest (value_history_count ()).release ();
   2114   1.1.1.8  christos }
   2115   1.1.1.8  christos 
   2116   1.1.1.6  christos /* Return the value of a convenience variable.  */
   2117   1.1.1.6  christos PyObject *
   2118   1.1.1.6  christos gdbpy_convenience_variable (PyObject *self, PyObject *args)
   2119   1.1.1.6  christos {
   2120   1.1.1.6  christos   const char *varname;
   2121   1.1.1.6  christos   struct value *res_val = NULL;
   2122   1.1.1.6  christos 
   2123   1.1.1.6  christos   if (!PyArg_ParseTuple (args, "s", &varname))
   2124   1.1.1.6  christos     return NULL;
   2125   1.1.1.6  christos 
   2126   1.1.1.9  christos   PyObject *result = nullptr;
   2127   1.1.1.9  christos   bool found = false;
   2128   1.1.1.7  christos   try
   2129   1.1.1.6  christos     {
   2130   1.1.1.6  christos       struct internalvar *var = lookup_only_internalvar (varname);
   2131   1.1.1.6  christos 
   2132   1.1.1.6  christos       if (var != NULL)
   2133   1.1.1.6  christos 	{
   2134   1.1.1.9  christos 	  scoped_value_mark free_values;
   2135   1.1.1.8  christos 	  res_val = value_of_internalvar (gdbpy_enter::get_gdbarch (), var);
   2136   1.1.1.9  christos 	  if (res_val->type ()->code () == TYPE_CODE_VOID)
   2137   1.1.1.6  christos 	    res_val = NULL;
   2138   1.1.1.9  christos 	  else
   2139   1.1.1.9  christos 	    {
   2140   1.1.1.9  christos 	      found = true;
   2141   1.1.1.9  christos 	      result = value_to_value_object (res_val);
   2142   1.1.1.9  christos 	    }
   2143   1.1.1.6  christos 	}
   2144   1.1.1.6  christos     }
   2145   1.1.1.7  christos   catch (const gdb_exception &except)
   2146   1.1.1.6  christos     {
   2147  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   2148   1.1.1.6  christos     }
   2149   1.1.1.6  christos 
   2150   1.1.1.9  christos   if (result == nullptr && !found)
   2151   1.1.1.6  christos     Py_RETURN_NONE;
   2152   1.1.1.6  christos 
   2153   1.1.1.9  christos   return result;
   2154   1.1.1.6  christos }
   2155   1.1.1.6  christos 
   2156   1.1.1.6  christos /* Set the value of a convenience variable.  */
   2157   1.1.1.6  christos PyObject *
   2158   1.1.1.6  christos gdbpy_set_convenience_variable (PyObject *self, PyObject *args)
   2159   1.1.1.6  christos {
   2160   1.1.1.6  christos   const char *varname;
   2161   1.1.1.6  christos   PyObject *value_obj;
   2162   1.1.1.6  christos   struct value *value = NULL;
   2163   1.1.1.6  christos 
   2164   1.1.1.6  christos   if (!PyArg_ParseTuple (args, "sO", &varname, &value_obj))
   2165   1.1.1.6  christos     return NULL;
   2166   1.1.1.6  christos 
   2167   1.1.1.6  christos   /* None means to clear the variable.  */
   2168   1.1.1.6  christos   if (value_obj != Py_None)
   2169   1.1.1.6  christos     {
   2170   1.1.1.6  christos       value = convert_value_from_python (value_obj);
   2171   1.1.1.6  christos       if (value == NULL)
   2172   1.1.1.6  christos 	return NULL;
   2173   1.1.1.6  christos     }
   2174   1.1.1.6  christos 
   2175   1.1.1.7  christos   try
   2176   1.1.1.6  christos     {
   2177   1.1.1.6  christos       if (value == NULL)
   2178   1.1.1.6  christos 	{
   2179   1.1.1.6  christos 	  struct internalvar *var = lookup_only_internalvar (varname);
   2180   1.1.1.6  christos 
   2181   1.1.1.6  christos 	  if (var != NULL)
   2182   1.1.1.6  christos 	    clear_internalvar (var);
   2183   1.1.1.6  christos 	}
   2184   1.1.1.6  christos       else
   2185   1.1.1.6  christos 	{
   2186   1.1.1.6  christos 	  struct internalvar *var = lookup_internalvar (varname);
   2187   1.1.1.6  christos 
   2188   1.1.1.6  christos 	  set_internalvar (var, value);
   2189   1.1.1.6  christos 	}
   2190   1.1.1.6  christos     }
   2191   1.1.1.7  christos   catch (const gdb_exception &except)
   2192   1.1.1.6  christos     {
   2193  1.1.1.10  christos       return gdbpy_handle_gdb_exception (nullptr, except);
   2194   1.1.1.6  christos     }
   2195   1.1.1.6  christos 
   2196   1.1.1.6  christos   Py_RETURN_NONE;
   2197   1.1.1.6  christos }
   2198   1.1.1.6  christos 
   2199       1.1  christos /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise.  */
   2200       1.1  christos 
   2201       1.1  christos int
   2202       1.1  christos gdbpy_is_value_object (PyObject *obj)
   2203       1.1  christos {
   2204       1.1  christos   return PyObject_TypeCheck (obj, &value_object_type);
   2205       1.1  christos }
   2206       1.1  christos 
   2207   1.1.1.9  christos static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
   2208       1.1  christos gdbpy_initialize_values (void)
   2209       1.1  christos {
   2210  1.1.1.10  christos   return gdbpy_type_ready (&value_object_type);
   2211       1.1  christos }
   2212       1.1  christos 
   2213   1.1.1.9  christos GDBPY_INITIALIZE_FILE (gdbpy_initialize_values);
   2214   1.1.1.9  christos 
   2215       1.1  christos 
   2216       1.1  christos 
   2218       1.1  christos static gdb_PyGetSetDef value_object_getset[] = {
   2219       1.1  christos   { "address", valpy_get_address, NULL, "The address of the value.",
   2220       1.1  christos     NULL },
   2221       1.1  christos   { "is_optimized_out", valpy_get_is_optimized_out, NULL,
   2222       1.1  christos     "Boolean telling whether the value is optimized "
   2223       1.1  christos     "out (i.e., not available).",
   2224       1.1  christos     NULL },
   2225       1.1  christos   { "type", valpy_get_type, NULL, "Type of the value.", NULL },
   2226       1.1  christos   { "dynamic_type", valpy_get_dynamic_type, NULL,
   2227       1.1  christos     "Dynamic type of the value.", NULL },
   2228       1.1  christos   { "is_lazy", valpy_get_is_lazy, NULL,
   2229       1.1  christos     "Boolean telling whether the value is lazy (not fetched yet\n\
   2230       1.1  christos from the inferior).  A lazy value is fetched when needed, or when\n\
   2231   1.1.1.9  christos the \"fetch_lazy()\" method is called.", NULL },
   2232   1.1.1.9  christos   { "bytes", valpy_get_bytes, valpy_set_bytes,
   2233       1.1  christos     "Return a bytearray containing the bytes of this value.", nullptr },
   2234       1.1  christos   {NULL}  /* Sentinel */
   2235       1.1  christos };
   2236       1.1  christos 
   2237       1.1  christos static PyMethodDef value_object_methods[] = {
   2238       1.1  christos   { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
   2239       1.1  christos   { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
   2240       1.1  christos     "dynamic_cast (gdb.Type) -> gdb.Value\n\
   2241       1.1  christos Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
   2242       1.1  christos   },
   2243       1.1  christos   { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
   2244       1.1  christos     "reinterpret_cast (gdb.Type) -> gdb.Value\n\
   2245       1.1  christos Cast the value to the supplied type, as if by the C++\n\
   2246       1.1  christos reinterpret_cast operator."
   2247       1.1  christos   },
   2248       1.1  christos   { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
   2249       1.1  christos   { "referenced_value", valpy_referenced_value, METH_NOARGS,
   2250   1.1.1.5  christos     "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
   2251   1.1.1.3  christos   { "reference_value", valpy_lvalue_reference_value, METH_NOARGS,
   2252   1.1.1.5  christos     "Return a value of type TYPE_CODE_REF referencing this value." },
   2253   1.1.1.5  christos   { "rvalue_reference_value", valpy_rvalue_reference_value, METH_NOARGS,
   2254   1.1.1.3  christos     "Return a value of type TYPE_CODE_RVALUE_REF referencing this value." },
   2255   1.1.1.9  christos   { "const_value", valpy_const_value, METH_NOARGS,
   2256       1.1  christos     "Return a 'const' qualified version of the same value." },
   2257       1.1  christos   { "lazy_string", (PyCFunction) valpy_lazy_string,
   2258       1.1  christos     METH_VARARGS | METH_KEYWORDS,
   2259       1.1  christos     "lazy_string ([encoding]  [, length]) -> lazy_string\n\
   2260       1.1  christos Return a lazy string representation of the value." },
   2261       1.1  christos   { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
   2262       1.1  christos     "string ([encoding] [, errors] [, length]) -> string\n\
   2263       1.1  christos Return Unicode string representation of the value." },
   2264       1.1  christos   { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
   2265   1.1.1.7  christos     "Fetches the value from the inferior, if it was lazy." },
   2266   1.1.1.7  christos   { "format_string", (PyCFunction) valpy_format_string,
   2267   1.1.1.7  christos     METH_VARARGS | METH_KEYWORDS,
   2268   1.1.1.7  christos     "format_string (...) -> string\n\
   2269   1.1.1.7  christos Return a string representation of the value using the specified\n\
   2270   1.1.1.9  christos formatting options" },
   2271   1.1.1.9  christos   { "assign", (PyCFunction) valpy_assign, METH_VARARGS,
   2272   1.1.1.9  christos     "assign (VAL) -> None\n\
   2273   1.1.1.9  christos Assign VAL to this value." },
   2274   1.1.1.9  christos   { "to_array", valpy_to_array, METH_NOARGS,
   2275   1.1.1.9  christos     "to_array () -> Value\n\
   2276       1.1  christos Return value as an array, if possible." },
   2277       1.1  christos   {NULL}  /* Sentinel */
   2278       1.1  christos };
   2279       1.1  christos 
   2280       1.1  christos static PyNumberMethods value_object_as_number = {
   2281       1.1  christos   valpy_add,
   2282       1.1  christos   valpy_subtract,
   2283       1.1  christos   valpy_multiply,
   2284       1.1  christos   valpy_remainder,
   2285       1.1  christos   NULL,			      /* nb_divmod */
   2286       1.1  christos   valpy_power,		      /* nb_power */
   2287       1.1  christos   valpy_negative,	      /* nb_negative */
   2288       1.1  christos   valpy_positive,	      /* nb_positive */
   2289       1.1  christos   valpy_absolute,	      /* nb_absolute */
   2290       1.1  christos   valpy_nonzero,	      /* nb_nonzero */
   2291       1.1  christos   valpy_invert,		      /* nb_invert */
   2292       1.1  christos   valpy_lsh,		      /* nb_lshift */
   2293       1.1  christos   valpy_rsh,		      /* nb_rshift */
   2294       1.1  christos   valpy_and,		      /* nb_and */
   2295       1.1  christos   valpy_xor,		      /* nb_xor */
   2296       1.1  christos   valpy_or,		      /* nb_or */
   2297       1.1  christos   valpy_long,		      /* nb_int */
   2298       1.1  christos   NULL,			      /* reserved */
   2299       1.1  christos   valpy_float,		      /* nb_float */
   2300       1.1  christos   NULL,                       /* nb_inplace_add */
   2301       1.1  christos   NULL,                       /* nb_inplace_subtract */
   2302       1.1  christos   NULL,                       /* nb_inplace_multiply */
   2303       1.1  christos   NULL,                       /* nb_inplace_remainder */
   2304       1.1  christos   NULL,                       /* nb_inplace_power */
   2305       1.1  christos   NULL,                       /* nb_inplace_lshift */
   2306       1.1  christos   NULL,                       /* nb_inplace_rshift */
   2307       1.1  christos   NULL,                       /* nb_inplace_and */
   2308       1.1  christos   NULL,                       /* nb_inplace_xor */
   2309       1.1  christos   NULL,                       /* nb_inplace_or */
   2310   1.1.1.4  christos   NULL,                       /* nb_floor_divide */
   2311   1.1.1.4  christos   valpy_divide,               /* nb_true_divide */
   2312   1.1.1.4  christos   NULL,			      /* nb_inplace_floor_divide */
   2313   1.1.1.4  christos   NULL,			      /* nb_inplace_true_divide */
   2314       1.1  christos   valpy_long,		      /* nb_index */
   2315       1.1  christos };
   2316       1.1  christos 
   2317       1.1  christos static PyMappingMethods value_object_as_mapping = {
   2318       1.1  christos   valpy_length,
   2319       1.1  christos   valpy_getitem,
   2320       1.1  christos   valpy_setitem
   2321       1.1  christos };
   2322       1.1  christos 
   2323       1.1  christos PyTypeObject value_object_type = {
   2324       1.1  christos   PyVarObject_HEAD_INIT (NULL, 0)
   2325       1.1  christos   "gdb.Value",			  /*tp_name*/
   2326       1.1  christos   sizeof (value_object),	  /*tp_basicsize*/
   2327       1.1  christos   0,				  /*tp_itemsize*/
   2328       1.1  christos   valpy_dealloc,		  /*tp_dealloc*/
   2329       1.1  christos   0,				  /*tp_print*/
   2330       1.1  christos   0,				  /*tp_getattr*/
   2331       1.1  christos   0,				  /*tp_setattr*/
   2332       1.1  christos   0,				  /*tp_compare*/
   2333       1.1  christos   0,				  /*tp_repr*/
   2334       1.1  christos   &value_object_as_number,	  /*tp_as_number*/
   2335       1.1  christos   0,				  /*tp_as_sequence*/
   2336       1.1  christos   &value_object_as_mapping,	  /*tp_as_mapping*/
   2337       1.1  christos   valpy_hash,		          /*tp_hash*/
   2338       1.1  christos   valpy_call,	                  /*tp_call*/
   2339       1.1  christos   valpy_str,			  /*tp_str*/
   2340       1.1  christos   0,				  /*tp_getattro*/
   2341       1.1  christos   0,				  /*tp_setattro*/
   2342       1.1  christos   0,				  /*tp_as_buffer*/
   2343       1.1  christos   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
   2344       1.1  christos   | Py_TPFLAGS_BASETYPE,	  /*tp_flags*/
   2345       1.1  christos   "GDB value object",		  /* tp_doc */
   2346       1.1  christos   0,				  /* tp_traverse */
   2347       1.1  christos   0,				  /* tp_clear */
   2348       1.1  christos   valpy_richcompare,		  /* tp_richcompare */
   2349       1.1  christos   0,				  /* tp_weaklistoffset */
   2350       1.1  christos   0,				  /* tp_iter */
   2351       1.1  christos   0,				  /* tp_iternext */
   2352       1.1  christos   value_object_methods,		  /* tp_methods */
   2353       1.1  christos   0,				  /* tp_members */
   2354       1.1  christos   value_object_getset,		  /* tp_getset */
   2355       1.1  christos   0,				  /* tp_base */
   2356       1.1  christos   0,				  /* tp_dict */
   2357       1.1  christos   0,				  /* tp_descr_get */
   2358       1.1  christos   0,				  /* tp_descr_set */
   2359   1.1.1.8  christos   0,				  /* tp_dictoffset */
   2360       1.1  christos   valpy_init,			  /* tp_init */
   2361   1.1.1.8  christos   0,				  /* tp_alloc */
   2362       1.1  christos   PyType_GenericNew,		  /* tp_new */
   2363                     };
   2364