GDB (API)
/home/stan/gdb/src/gdb/python/py-value.c
Go to the documentation of this file.
00001 /* Python interface to values.
00002 
00003    Copyright (C) 2008-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "gdb_assert.h"
00022 #include "charset.h"
00023 #include "value.h"
00024 #include "exceptions.h"
00025 #include "language.h"
00026 #include "dfp.h"
00027 #include "valprint.h"
00028 #include "infcall.h"
00029 #include "expression.h"
00030 #include "cp-abi.h"
00031 #include "python.h"
00032 
00033 #ifdef HAVE_PYTHON
00034 
00035 #include "python-internal.h"
00036 
00037 /* Even though Python scalar types directly map to host types, we use
00038    target types here to remain consistent with the values system in
00039    GDB (which uses target arithmetic).  */
00040 
00041 /* Python's integer type corresponds to C's long type.  */
00042 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
00043 
00044 /* Python's float type corresponds to C's double type.  */
00045 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
00046 
00047 /* Python's long type corresponds to C's long long type.  */
00048 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
00049 
00050 /* Python's long type corresponds to C's long long type.  Unsigned version.  */
00051 #define builtin_type_upylong builtin_type \
00052   (python_gdbarch)->builtin_unsigned_long_long
00053 
00054 #define builtin_type_pybool \
00055   language_bool_type (python_language, python_gdbarch)
00056 
00057 #define builtin_type_pychar \
00058   language_string_char_type (python_language, python_gdbarch)
00059 
00060 typedef struct value_object {
00061   PyObject_HEAD
00062   struct value_object *next;
00063   struct value_object *prev;
00064   struct value *value;
00065   PyObject *address;
00066   PyObject *type;
00067   PyObject *dynamic_type;
00068 } value_object;
00069 
00070 /* List of all values which are currently exposed to Python. It is
00071    maintained so that when an objfile is discarded, preserve_values
00072    can copy the values' types if needed.  */
00073 /* This variable is unnecessarily initialized to NULL in order to
00074    work around a linker bug on MacOS.  */
00075 static value_object *values_in_python = NULL;
00076 
00077 /* Called by the Python interpreter when deallocating a value object.  */
00078 static void
00079 valpy_dealloc (PyObject *obj)
00080 {
00081   value_object *self = (value_object *) obj;
00082 
00083   /* Remove SELF from the global list.  */
00084   if (self->prev)
00085     self->prev->next = self->next;
00086   else
00087     {
00088       gdb_assert (values_in_python == self);
00089       values_in_python = self->next;
00090     }
00091   if (self->next)
00092     self->next->prev = self->prev;
00093 
00094   value_free (self->value);
00095 
00096   if (self->address)
00097     /* Use braces to appease gcc warning.  *sigh*  */
00098     {
00099       Py_DECREF (self->address);
00100     }
00101 
00102   if (self->type)
00103     {
00104       Py_DECREF (self->type);
00105     }
00106 
00107   Py_XDECREF (self->dynamic_type);
00108 
00109   Py_TYPE (self)->tp_free (self);
00110 }
00111 
00112 /* Helper to push a Value object on the global list.  */
00113 static void
00114 note_value (value_object *value_obj)
00115 {
00116   value_obj->next = values_in_python;
00117   if (value_obj->next)
00118     value_obj->next->prev = value_obj;
00119   value_obj->prev = NULL;
00120   values_in_python = value_obj;
00121 }
00122 
00123 /* Called when a new gdb.Value object needs to be allocated.  Returns NULL on
00124    error, with a python exception set.  */
00125 static PyObject *
00126 valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
00127 {
00128   struct value *value = NULL;   /* Initialize to appease gcc warning.  */
00129   value_object *value_obj;
00130 
00131   if (PyTuple_Size (args) != 1)
00132     {
00133       PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
00134                                           "1 argument"));
00135       return NULL;
00136     }
00137 
00138   value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
00139   if (value_obj == NULL)
00140     {
00141       PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
00142                                             "create Value object."));
00143       return NULL;
00144     }
00145 
00146   value = convert_value_from_python (PyTuple_GetItem (args, 0));
00147   if (value == NULL)
00148     {
00149       subtype->tp_free (value_obj);
00150       return NULL;
00151     }
00152 
00153   value_obj->value = value;
00154   release_value_or_incref (value);
00155   value_obj->address = NULL;
00156   value_obj->type = NULL;
00157   value_obj->dynamic_type = NULL;
00158   note_value (value_obj);
00159 
00160   return (PyObject *) value_obj;
00161 }
00162 
00163 /* Iterate over all the Value objects, calling preserve_one_value on
00164    each.  */
00165 void
00166 preserve_python_values (struct objfile *objfile, htab_t copied_types)
00167 {
00168   value_object *iter;
00169 
00170   for (iter = values_in_python; iter; iter = iter->next)
00171     preserve_one_value (iter->value, objfile, copied_types);
00172 }
00173 
00174 /* Given a value of a pointer type, apply the C unary * operator to it.  */
00175 static PyObject *
00176 valpy_dereference (PyObject *self, PyObject *args)
00177 {
00178   volatile struct gdb_exception except;
00179   PyObject *result = NULL;
00180 
00181   TRY_CATCH (except, RETURN_MASK_ALL)
00182     {
00183       struct value *res_val;
00184       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
00185 
00186       res_val = value_ind (((value_object *) self)->value);
00187       result = value_to_value_object (res_val);
00188       do_cleanups (cleanup);
00189     }
00190   GDB_PY_HANDLE_EXCEPTION (except);
00191 
00192   return result;
00193 }
00194 
00195 /* Given a value of a pointer type or a reference type, return the value
00196    referenced. The difference between this function and valpy_dereference is
00197    that the latter applies * unary operator to a value, which need not always
00198    result in the value referenced. For example, for a value which is a reference
00199    to an 'int' pointer ('int *'), valpy_dereference will result in a value of
00200    type 'int' while valpy_referenced_value will result in a value of type
00201    'int *'.  */
00202 
00203 static PyObject *
00204 valpy_referenced_value (PyObject *self, PyObject *args)
00205 {
00206   volatile struct gdb_exception except;
00207   PyObject *result = NULL;
00208 
00209   TRY_CATCH (except, RETURN_MASK_ALL)
00210     {
00211       struct value *self_val, *res_val;
00212       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
00213 
00214       self_val = ((value_object *) self)->value;
00215       switch (TYPE_CODE (check_typedef (value_type (self_val))))
00216         {
00217         case TYPE_CODE_PTR:
00218           res_val = value_ind (self_val);
00219           break;
00220         case TYPE_CODE_REF:
00221           res_val = coerce_ref (self_val);
00222           break;
00223         default:
00224           error(_("Trying to get the referenced value from a value which is "
00225                   "neither a pointer nor a reference."));
00226         }
00227 
00228       result = value_to_value_object (res_val);
00229       do_cleanups (cleanup);
00230     }
00231   GDB_PY_HANDLE_EXCEPTION (except);
00232 
00233   return result;
00234 }
00235 
00236 /* Return "&value".  */
00237 static PyObject *
00238 valpy_get_address (PyObject *self, void *closure)
00239 {
00240   value_object *val_obj = (value_object *) self;
00241   volatile struct gdb_exception except;
00242 
00243   if (!val_obj->address)
00244     {
00245       TRY_CATCH (except, RETURN_MASK_ALL)
00246         {
00247           struct value *res_val;
00248           struct cleanup *cleanup
00249             = make_cleanup_value_free_to_mark (value_mark ());
00250 
00251           res_val = value_addr (val_obj->value);
00252           val_obj->address = value_to_value_object (res_val);
00253           do_cleanups (cleanup);
00254         }
00255       if (except.reason < 0)
00256         {
00257           val_obj->address = Py_None;
00258           Py_INCREF (Py_None);
00259         }
00260     }
00261 
00262   Py_XINCREF (val_obj->address);
00263 
00264   return val_obj->address;
00265 }
00266 
00267 /* Return type of the value.  */
00268 static PyObject *
00269 valpy_get_type (PyObject *self, void *closure)
00270 {
00271   value_object *obj = (value_object *) self;
00272 
00273   if (!obj->type)
00274     {
00275       obj->type = type_to_type_object (value_type (obj->value));
00276       if (!obj->type)
00277         return NULL;
00278     }
00279   Py_INCREF (obj->type);
00280   return obj->type;
00281 }
00282 
00283 /* Return dynamic type of the value.  */
00284 
00285 static PyObject *
00286 valpy_get_dynamic_type (PyObject *self, void *closure)
00287 {
00288   value_object *obj = (value_object *) self;
00289   volatile struct gdb_exception except;
00290   struct type *type = NULL;
00291 
00292   if (obj->dynamic_type != NULL)
00293     {
00294       Py_INCREF (obj->dynamic_type);
00295       return obj->dynamic_type;
00296     }
00297 
00298   TRY_CATCH (except, RETURN_MASK_ALL)
00299     {
00300       struct value *val = obj->value;
00301       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
00302 
00303       type = value_type (val);
00304       CHECK_TYPEDEF (type);
00305 
00306       if (((TYPE_CODE (type) == TYPE_CODE_PTR)
00307            || (TYPE_CODE (type) == TYPE_CODE_REF))
00308           && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
00309         {
00310           struct value *target;
00311           int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR;
00312 
00313           target = value_ind (val);
00314           type = value_rtti_type (target, NULL, NULL, NULL);
00315 
00316           if (type)
00317             {
00318               if (was_pointer)
00319                 type = lookup_pointer_type (type);
00320               else
00321                 type = lookup_reference_type (type);
00322             }
00323         }
00324       else if (TYPE_CODE (type) == TYPE_CODE_CLASS)
00325         type = value_rtti_type (val, NULL, NULL, NULL);
00326       else
00327         {
00328           /* Re-use object's static type.  */
00329           type = NULL;
00330         }
00331 
00332       do_cleanups (cleanup);
00333     }
00334   GDB_PY_HANDLE_EXCEPTION (except);
00335 
00336   if (type == NULL)
00337     obj->dynamic_type = valpy_get_type (self, NULL);
00338   else
00339     obj->dynamic_type = type_to_type_object (type);
00340 
00341   Py_XINCREF (obj->dynamic_type);
00342   return obj->dynamic_type;
00343 }
00344 
00345 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
00346    string.  Return a PyObject representing a lazy_string_object type.
00347    A lazy string is a pointer to a string with an optional encoding and
00348    length.  If ENCODING is not given, encoding is set to None.  If an
00349    ENCODING is provided the encoding parameter is set to ENCODING, but
00350    the string is not encoded.  If LENGTH is provided then the length
00351    parameter is set to LENGTH, otherwise length will be set to -1 (first
00352    null of appropriate with).  */
00353 static PyObject *
00354 valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
00355 {
00356   gdb_py_longest length = -1;
00357   struct value *value = ((value_object *) self)->value;
00358   const char *user_encoding = NULL;
00359   static char *keywords[] = { "encoding", "length", NULL };
00360   PyObject *str_obj = NULL;
00361   volatile struct gdb_exception except;
00362 
00363   if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords,
00364                                     &user_encoding, &length))
00365     return NULL;
00366 
00367   TRY_CATCH (except, RETURN_MASK_ALL)
00368     {
00369       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
00370 
00371       if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
00372         value = value_ind (value);
00373 
00374       str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
00375                                                  user_encoding,
00376                                                  value_type (value));
00377 
00378       do_cleanups (cleanup);
00379     }
00380   GDB_PY_HANDLE_EXCEPTION (except);
00381 
00382   return str_obj;
00383 }
00384 
00385 /* Implementation of gdb.Value.string ([encoding] [, errors]
00386    [, length]) -> string.  Return Unicode string with value contents.
00387    If ENCODING is not given, the string is assumed to be encoded in
00388    the target's charset.  If LENGTH is provided, only fetch string to
00389    the length provided.  */
00390 
00391 static PyObject *
00392 valpy_string (PyObject *self, PyObject *args, PyObject *kw)
00393 {
00394   int length = -1;
00395   gdb_byte *buffer;
00396   struct value *value = ((value_object *) self)->value;
00397   volatile struct gdb_exception except;
00398   PyObject *unicode;
00399   const char *encoding = NULL;
00400   const char *errors = NULL;
00401   const char *user_encoding = NULL;
00402   const char *la_encoding = NULL;
00403   struct type *char_type;
00404   static char *keywords[] = { "encoding", "errors", "length", NULL };
00405 
00406   if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
00407                                     &user_encoding, &errors, &length))
00408     return NULL;
00409 
00410   TRY_CATCH (except, RETURN_MASK_ALL)
00411     {
00412       LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
00413     }
00414   GDB_PY_HANDLE_EXCEPTION (except);
00415 
00416   encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
00417   unicode = PyUnicode_Decode ((const char *) buffer,
00418                               length * TYPE_LENGTH (char_type),
00419                               encoding, errors);
00420   xfree (buffer);
00421 
00422   return unicode;
00423 }
00424 
00425 /* A helper function that implements the various cast operators.  */
00426 
00427 static PyObject *
00428 valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
00429 {
00430   PyObject *type_obj, *result = NULL;
00431   struct type *type;
00432   volatile struct gdb_exception except;
00433 
00434   if (! PyArg_ParseTuple (args, "O", &type_obj))
00435     return NULL;
00436 
00437   type = type_object_to_type (type_obj);
00438   if (! type)
00439     {
00440       PyErr_SetString (PyExc_RuntimeError, 
00441                        _("Argument must be a type."));
00442       return NULL;
00443     }
00444 
00445   TRY_CATCH (except, RETURN_MASK_ALL)
00446     {
00447       struct value *val = ((value_object *) self)->value;
00448       struct value *res_val;
00449       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
00450 
00451       if (op == UNOP_DYNAMIC_CAST)
00452         res_val = value_dynamic_cast (type, val);
00453       else if (op == UNOP_REINTERPRET_CAST)
00454         res_val = value_reinterpret_cast (type, val);
00455       else
00456         {
00457           gdb_assert (op == UNOP_CAST);
00458           res_val = value_cast (type, val);
00459         }
00460 
00461       result = value_to_value_object (res_val);
00462       do_cleanups (cleanup);
00463     }
00464   GDB_PY_HANDLE_EXCEPTION (except);
00465 
00466   return result;
00467 }
00468 
00469 /* Implementation of the "cast" method.  */
00470 
00471 static PyObject *
00472 valpy_cast (PyObject *self, PyObject *args)
00473 {
00474   return valpy_do_cast (self, args, UNOP_CAST);
00475 }
00476 
00477 /* Implementation of the "dynamic_cast" method.  */
00478 
00479 static PyObject *
00480 valpy_dynamic_cast (PyObject *self, PyObject *args)
00481 {
00482   return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
00483 }
00484 
00485 /* Implementation of the "reinterpret_cast" method.  */
00486 
00487 static PyObject *
00488 valpy_reinterpret_cast (PyObject *self, PyObject *args)
00489 {
00490   return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
00491 }
00492 
00493 static Py_ssize_t
00494 valpy_length (PyObject *self)
00495 {
00496   /* We don't support getting the number of elements in a struct / class.  */
00497   PyErr_SetString (PyExc_NotImplementedError,
00498                    _("Invalid operation on gdb.Value."));
00499   return -1;
00500 }
00501 
00502 /* Given string name of an element inside structure, return its value
00503    object.  Returns NULL on error, with a python exception set.  */
00504 static PyObject *
00505 valpy_getitem (PyObject *self, PyObject *key)
00506 {
00507   value_object *self_value = (value_object *) self;
00508   char *field = NULL;
00509   volatile struct gdb_exception except;
00510   PyObject *result = NULL;
00511 
00512   if (gdbpy_is_string (key))
00513     {  
00514       field = python_string_to_host_string (key);
00515       if (field == NULL)
00516         return NULL;
00517     }
00518 
00519   TRY_CATCH (except, RETURN_MASK_ALL)
00520     {
00521       struct value *tmp = self_value->value;
00522       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
00523       struct value *res_val = NULL;
00524 
00525       if (field)
00526         res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
00527       else
00528         {
00529           /* Assume we are attempting an array access, and let the
00530              value code throw an exception if the index has an invalid
00531              type.  */
00532           struct value *idx = convert_value_from_python (key);
00533 
00534           if (idx != NULL)
00535             {
00536               /* Check the value's type is something that can be accessed via
00537                  a subscript.  */
00538               struct type *type;
00539 
00540               tmp = coerce_ref (tmp);
00541               type = check_typedef (value_type (tmp));
00542               if (TYPE_CODE (type) != TYPE_CODE_ARRAY
00543                   && TYPE_CODE (type) != TYPE_CODE_PTR)
00544                   error (_("Cannot subscript requested type."));
00545               else
00546                 res_val = value_subscript (tmp, value_as_long (idx));
00547             }
00548         }
00549 
00550       if (res_val)
00551         result = value_to_value_object (res_val);
00552       do_cleanups (cleanup);
00553     }
00554 
00555   xfree (field);
00556   GDB_PY_HANDLE_EXCEPTION (except);
00557 
00558   return result;
00559 }
00560 
00561 static int
00562 valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
00563 {
00564   PyErr_Format (PyExc_NotImplementedError,
00565                 _("Setting of struct elements is not currently supported."));
00566   return -1;
00567 }
00568 
00569 /* Called by the Python interpreter to perform an inferior function
00570    call on the value.  Returns NULL on error, with a python exception set.  */
00571 static PyObject *
00572 valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
00573 {
00574   Py_ssize_t args_count;
00575   volatile struct gdb_exception except;
00576   struct value *function = ((value_object *) self)->value;
00577   struct value **vargs = NULL;
00578   struct type *ftype = NULL;
00579   struct value *mark = value_mark ();
00580   PyObject *result = NULL;
00581 
00582   TRY_CATCH (except, RETURN_MASK_ALL)
00583     {
00584       ftype = check_typedef (value_type (function));
00585     }
00586   GDB_PY_HANDLE_EXCEPTION (except);
00587 
00588   if (TYPE_CODE (ftype) != TYPE_CODE_FUNC)
00589     {
00590       PyErr_SetString (PyExc_RuntimeError,
00591                        _("Value is not callable (not TYPE_CODE_FUNC)."));
00592       return NULL;
00593     }
00594 
00595   if (! PyTuple_Check (args))
00596     {
00597       PyErr_SetString (PyExc_TypeError,
00598                        _("Inferior arguments must be provided in a tuple."));
00599       return NULL;
00600     }
00601 
00602   args_count = PyTuple_Size (args);
00603   if (args_count > 0)
00604     {
00605       int i;
00606 
00607       vargs = alloca (sizeof (struct value *) * args_count);
00608       for (i = 0; i < args_count; i++)
00609         {
00610           PyObject *item = PyTuple_GetItem (args, i);
00611 
00612           if (item == NULL)
00613             return NULL;
00614 
00615           vargs[i] = convert_value_from_python (item);
00616           if (vargs[i] == NULL)
00617             return NULL;
00618         }
00619     }
00620 
00621   TRY_CATCH (except, RETURN_MASK_ALL)
00622     {
00623       struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark);
00624       struct value *return_value;
00625 
00626       return_value = call_function_by_hand (function, args_count, vargs);
00627       result = value_to_value_object (return_value);
00628       do_cleanups (cleanup);
00629     }
00630   GDB_PY_HANDLE_EXCEPTION (except);
00631 
00632   return result;
00633 }
00634 
00635 /* Called by the Python interpreter to obtain string representation
00636    of the object.  */
00637 static PyObject *
00638 valpy_str (PyObject *self)
00639 {
00640   char *s = NULL;
00641   PyObject *result;
00642   struct value_print_options opts;
00643   volatile struct gdb_exception except;
00644 
00645   get_user_print_options (&opts);
00646   opts.deref_ref = 0;
00647 
00648   TRY_CATCH (except, RETURN_MASK_ALL)
00649     {
00650       struct ui_file *stb = mem_fileopen ();
00651       struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
00652 
00653       common_val_print (((value_object *) self)->value, stb, 0,
00654                         &opts, python_language);
00655       s = ui_file_xstrdup (stb, NULL);
00656 
00657       do_cleanups (old_chain);
00658     }
00659   GDB_PY_HANDLE_EXCEPTION (except);
00660 
00661   result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
00662   xfree (s);
00663 
00664   return result;
00665 }
00666 
00667 /* Implements gdb.Value.is_optimized_out.  */
00668 static PyObject *
00669 valpy_get_is_optimized_out (PyObject *self, void *closure)
00670 {
00671   struct value *value = ((value_object *) self)->value;
00672   int opt = 0;
00673   volatile struct gdb_exception except;
00674 
00675   TRY_CATCH (except, RETURN_MASK_ALL)
00676     {
00677       opt = value_optimized_out (value);
00678     }
00679   GDB_PY_HANDLE_EXCEPTION (except);
00680 
00681   if (opt)
00682     Py_RETURN_TRUE;
00683 
00684   Py_RETURN_FALSE;
00685 }
00686 
00687 /* Implements gdb.Value.is_lazy.  */
00688 static PyObject *
00689 valpy_get_is_lazy (PyObject *self, void *closure)
00690 {
00691   struct value *value = ((value_object *) self)->value;
00692   int opt = 0;
00693   volatile struct gdb_exception except;
00694 
00695   TRY_CATCH (except, RETURN_MASK_ALL)
00696     {
00697       opt = value_lazy (value);
00698     }
00699   GDB_PY_HANDLE_EXCEPTION (except);
00700 
00701   if (opt)
00702     Py_RETURN_TRUE;
00703 
00704   Py_RETURN_FALSE;
00705 }
00706 
00707 /* Implements gdb.Value.fetch_lazy ().  */
00708 static PyObject *
00709 valpy_fetch_lazy (PyObject *self, PyObject *args)
00710 {
00711   struct value *value = ((value_object *) self)->value;
00712   volatile struct gdb_exception except;
00713 
00714   TRY_CATCH (except, RETURN_MASK_ALL)
00715     {
00716       if (value_lazy (value))
00717         value_fetch_lazy (value);
00718     }
00719   GDB_PY_HANDLE_EXCEPTION (except);
00720 
00721   Py_RETURN_NONE;
00722 }
00723 
00724 /* Calculate and return the address of the PyObject as the value of
00725    the builtin __hash__ call.  */
00726 static long 
00727 valpy_hash (PyObject *self)
00728 {
00729   return (long) (intptr_t) self;
00730 }
00731 
00732 enum valpy_opcode
00733 {
00734   VALPY_ADD,
00735   VALPY_SUB,
00736   VALPY_MUL,
00737   VALPY_DIV,
00738   VALPY_REM,
00739   VALPY_POW,
00740   VALPY_LSH,
00741   VALPY_RSH,
00742   VALPY_BITAND,
00743   VALPY_BITOR,
00744   VALPY_BITXOR
00745 };
00746 
00747 /* If TYPE is a reference, return the target; otherwise return TYPE.  */
00748 #define STRIP_REFERENCE(TYPE) \
00749   ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
00750 
00751 /* Returns a value object which is the result of applying the operation
00752    specified by OPCODE to the given arguments.  Returns NULL on error, with
00753    a python exception set.  */
00754 static PyObject *
00755 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
00756 {
00757   volatile struct gdb_exception except;
00758   PyObject *result = NULL;
00759 
00760   TRY_CATCH (except, RETURN_MASK_ALL)
00761     {
00762       struct value *arg1, *arg2;
00763       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
00764       struct value *res_val = NULL;
00765 
00766       /* If the gdb.Value object is the second operand, then it will be passed
00767          to us as the OTHER argument, and SELF will be an entirely different
00768          kind of object, altogether.  Because of this, we can't assume self is
00769          a gdb.Value object and need to convert it from python as well.  */
00770       arg1 = convert_value_from_python (self);
00771       if (arg1 == NULL)
00772         {
00773           do_cleanups (cleanup);
00774           break;
00775         }
00776 
00777       arg2 = convert_value_from_python (other);
00778       if (arg2 == NULL)
00779         {
00780           do_cleanups (cleanup);
00781           break;
00782         }
00783 
00784       switch (opcode)
00785         {
00786         case VALPY_ADD:
00787           {
00788             struct type *ltype = value_type (arg1);
00789             struct type *rtype = value_type (arg2);
00790 
00791             CHECK_TYPEDEF (ltype);
00792             ltype = STRIP_REFERENCE (ltype);
00793             CHECK_TYPEDEF (rtype);
00794             rtype = STRIP_REFERENCE (rtype);
00795 
00796             if (TYPE_CODE (ltype) == TYPE_CODE_PTR
00797                 && is_integral_type (rtype))
00798               res_val = value_ptradd (arg1, value_as_long (arg2));
00799             else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
00800                      && is_integral_type (ltype))
00801               res_val = value_ptradd (arg2, value_as_long (arg1));
00802             else
00803               res_val = value_binop (arg1, arg2, BINOP_ADD);
00804           }
00805           break;
00806         case VALPY_SUB:
00807           {
00808             struct type *ltype = value_type (arg1);
00809             struct type *rtype = value_type (arg2);
00810 
00811             CHECK_TYPEDEF (ltype);
00812             ltype = STRIP_REFERENCE (ltype);
00813             CHECK_TYPEDEF (rtype);
00814             rtype = STRIP_REFERENCE (rtype);
00815 
00816             if (TYPE_CODE (ltype) == TYPE_CODE_PTR
00817                 && TYPE_CODE (rtype) == TYPE_CODE_PTR)
00818               /* A ptrdiff_t for the target would be preferable here.  */
00819               res_val = value_from_longest (builtin_type_pyint,
00820                                             value_ptrdiff (arg1, arg2));
00821             else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
00822                      && is_integral_type (rtype))
00823               res_val = value_ptradd (arg1, - value_as_long (arg2));
00824             else
00825               res_val = value_binop (arg1, arg2, BINOP_SUB);
00826           }
00827           break;
00828         case VALPY_MUL:
00829           res_val = value_binop (arg1, arg2, BINOP_MUL);
00830           break;
00831         case VALPY_DIV:
00832           res_val = value_binop (arg1, arg2, BINOP_DIV);
00833           break;
00834         case VALPY_REM:
00835           res_val = value_binop (arg1, arg2, BINOP_REM);
00836           break;
00837         case VALPY_POW:
00838           res_val = value_binop (arg1, arg2, BINOP_EXP);
00839           break;
00840         case VALPY_LSH:
00841           res_val = value_binop (arg1, arg2, BINOP_LSH);
00842           break;
00843         case VALPY_RSH:
00844           res_val = value_binop (arg1, arg2, BINOP_RSH);
00845           break;
00846         case VALPY_BITAND:
00847           res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
00848           break;
00849         case VALPY_BITOR:
00850           res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
00851           break;
00852         case VALPY_BITXOR:
00853           res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
00854           break;
00855         }
00856 
00857       if (res_val)
00858         result = value_to_value_object (res_val);
00859 
00860       do_cleanups (cleanup);
00861     }
00862   GDB_PY_HANDLE_EXCEPTION (except);
00863 
00864   return result;
00865 }
00866 
00867 static PyObject *
00868 valpy_add (PyObject *self, PyObject *other)
00869 {
00870   return valpy_binop (VALPY_ADD, self, other);
00871 }
00872 
00873 static PyObject *
00874 valpy_subtract (PyObject *self, PyObject *other)
00875 {
00876   return valpy_binop (VALPY_SUB, self, other);
00877 }
00878 
00879 static PyObject *
00880 valpy_multiply (PyObject *self, PyObject *other)
00881 {
00882   return valpy_binop (VALPY_MUL, self, other);
00883 }
00884 
00885 static PyObject *
00886 valpy_divide (PyObject *self, PyObject *other)
00887 {
00888   return valpy_binop (VALPY_DIV, self, other);
00889 }
00890 
00891 static PyObject *
00892 valpy_remainder (PyObject *self, PyObject *other)
00893 {
00894   return valpy_binop (VALPY_REM, self, other);
00895 }
00896 
00897 static PyObject *
00898 valpy_power (PyObject *self, PyObject *other, PyObject *unused)
00899 {
00900   /* We don't support the ternary form of pow.  I don't know how to express
00901      that, so let's just throw NotImplementedError to at least do something
00902      about it.  */
00903   if (unused != Py_None)
00904     {
00905       PyErr_SetString (PyExc_NotImplementedError,
00906                        "Invalid operation on gdb.Value.");
00907       return NULL;
00908     }
00909 
00910   return valpy_binop (VALPY_POW, self, other);
00911 }
00912 
00913 static PyObject *
00914 valpy_negative (PyObject *self)
00915 {
00916   volatile struct gdb_exception except;
00917   PyObject *result = NULL;
00918 
00919   TRY_CATCH (except, RETURN_MASK_ALL)
00920     {
00921       /* Perhaps overkill, but consistency has some virtue.  */
00922       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
00923       struct value *val;
00924 
00925       val = value_neg (((value_object *) self)->value);
00926       result = value_to_value_object (val);
00927       do_cleanups (cleanup);
00928     }
00929   GDB_PY_HANDLE_EXCEPTION (except);
00930 
00931   return result;
00932 }
00933 
00934 static PyObject *
00935 valpy_positive (PyObject *self)
00936 {
00937   return value_to_value_object (((value_object *) self)->value);
00938 }
00939 
00940 static PyObject *
00941 valpy_absolute (PyObject *self)
00942 {
00943   struct value *value = ((value_object *) self)->value;
00944   volatile struct gdb_exception except;
00945   int isabs = 1;
00946 
00947   TRY_CATCH (except, RETURN_MASK_ALL)
00948     {
00949       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
00950 
00951       if (value_less (value, value_zero (value_type (value), not_lval)))
00952         isabs = 0;
00953 
00954       do_cleanups (cleanup);
00955     }
00956   GDB_PY_HANDLE_EXCEPTION (except);
00957 
00958   if (isabs)
00959     return valpy_positive (self);
00960   else
00961     return valpy_negative (self);
00962 }
00963 
00964 /* Implements boolean evaluation of gdb.Value.  */
00965 static int
00966 valpy_nonzero (PyObject *self)
00967 {
00968   volatile struct gdb_exception except;
00969   value_object *self_value = (value_object *) self;
00970   struct type *type;
00971   int nonzero = 0; /* Appease GCC warning.  */
00972 
00973   TRY_CATCH (except, RETURN_MASK_ALL)
00974     {
00975       type = check_typedef (value_type (self_value->value));
00976 
00977       if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
00978         nonzero = !!value_as_long (self_value->value);
00979       else if (TYPE_CODE (type) == TYPE_CODE_FLT)
00980         nonzero = value_as_double (self_value->value) != 0;
00981       else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
00982         nonzero = !decimal_is_zero (value_contents (self_value->value),
00983                                  TYPE_LENGTH (type),
00984                                  gdbarch_byte_order (get_type_arch (type)));
00985       else
00986         /* All other values are True.  */
00987         nonzero = 1;
00988     }
00989   /* This is not documented in the Python documentation, but if this
00990      function fails, return -1 as slot_nb_nonzero does (the default
00991      Python nonzero function).  */
00992   GDB_PY_SET_HANDLE_EXCEPTION (except);
00993 
00994   return nonzero;
00995 }
00996 
00997 /* Implements ~ for value objects.  */
00998 static PyObject *
00999 valpy_invert (PyObject *self)
01000 {
01001   struct value *val = NULL;
01002   volatile struct gdb_exception except;
01003 
01004   TRY_CATCH (except, RETURN_MASK_ALL)
01005     {
01006       val = value_complement (((value_object *) self)->value);
01007     }
01008   GDB_PY_HANDLE_EXCEPTION (except);
01009 
01010   return value_to_value_object (val);
01011 }
01012 
01013 /* Implements left shift for value objects.  */
01014 static PyObject *
01015 valpy_lsh (PyObject *self, PyObject *other)
01016 {
01017   return valpy_binop (VALPY_LSH, self, other);
01018 }
01019 
01020 /* Implements right shift for value objects.  */
01021 static PyObject *
01022 valpy_rsh (PyObject *self, PyObject *other)
01023 {
01024   return valpy_binop (VALPY_RSH, self, other);
01025 }
01026 
01027 /* Implements bitwise and for value objects.  */
01028 static PyObject *
01029 valpy_and (PyObject *self, PyObject *other)
01030 {
01031   return valpy_binop (VALPY_BITAND, self, other);
01032 }
01033 
01034 /* Implements bitwise or for value objects.  */
01035 static PyObject *
01036 valpy_or (PyObject *self, PyObject *other)
01037 {
01038   return valpy_binop (VALPY_BITOR, self, other);
01039 }
01040 
01041 /* Implements bitwise xor for value objects.  */
01042 static PyObject *
01043 valpy_xor (PyObject *self, PyObject *other)
01044 {
01045   return valpy_binop (VALPY_BITXOR, self, other);
01046 }
01047 
01048 /* Implements comparison operations for value objects.  Returns NULL on error,
01049    with a python exception set.  */
01050 static PyObject *
01051 valpy_richcompare (PyObject *self, PyObject *other, int op)
01052 {
01053   int result = 0;
01054   volatile struct gdb_exception except;
01055 
01056   if (other == Py_None)
01057     /* Comparing with None is special.  From what I can tell, in Python
01058        None is smaller than anything else.  */
01059     switch (op) {
01060       case Py_LT:
01061       case Py_LE:
01062       case Py_EQ:
01063         Py_RETURN_FALSE;
01064       case Py_NE:
01065       case Py_GT:
01066       case Py_GE:
01067         Py_RETURN_TRUE;
01068       default:
01069         /* Can't happen.  */
01070         PyErr_SetString (PyExc_NotImplementedError,
01071                          _("Invalid operation on gdb.Value."));
01072         return NULL;
01073     }
01074 
01075   TRY_CATCH (except, RETURN_MASK_ALL)
01076     {
01077       struct value *value_other, *mark = value_mark ();
01078       struct cleanup *cleanup;
01079 
01080       value_other = convert_value_from_python (other);
01081       if (value_other == NULL)
01082         {
01083           result = -1;
01084           break;
01085         }
01086 
01087       cleanup = make_cleanup_value_free_to_mark (mark);
01088 
01089       switch (op) {
01090         case Py_LT:
01091           result = value_less (((value_object *) self)->value, value_other);
01092           break;
01093         case Py_LE:
01094           result = value_less (((value_object *) self)->value, value_other)
01095             || value_equal (((value_object *) self)->value, value_other);
01096           break;
01097         case Py_EQ:
01098           result = value_equal (((value_object *) self)->value, value_other);
01099           break;
01100         case Py_NE:
01101           result = !value_equal (((value_object *) self)->value, value_other);
01102           break;
01103         case Py_GT:
01104           result = value_less (value_other, ((value_object *) self)->value);
01105           break;
01106         case Py_GE:
01107           result = value_less (value_other, ((value_object *) self)->value)
01108             || value_equal (((value_object *) self)->value, value_other);
01109           break;
01110         default:
01111           /* Can't happen.  */
01112           PyErr_SetString (PyExc_NotImplementedError,
01113                            _("Invalid operation on gdb.Value."));
01114           result = -1;
01115           break;
01116       }
01117 
01118       do_cleanups (cleanup);
01119     }
01120   GDB_PY_HANDLE_EXCEPTION (except);
01121 
01122   /* In this case, the Python exception has already been set.  */
01123   if (result < 0)
01124     return NULL;
01125 
01126   if (result == 1)
01127     Py_RETURN_TRUE;
01128 
01129   Py_RETURN_FALSE;
01130 }
01131 
01132 /* Helper function to determine if a type is "int-like".  */
01133 static int
01134 is_intlike (struct type *type, int ptr_ok)
01135 {
01136   return (TYPE_CODE (type) == TYPE_CODE_INT
01137           || TYPE_CODE (type) == TYPE_CODE_ENUM
01138           || TYPE_CODE (type) == TYPE_CODE_BOOL
01139           || TYPE_CODE (type) == TYPE_CODE_CHAR
01140           || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
01141 }
01142 
01143 #ifndef IS_PY3K
01144 /* Implements conversion to int.  */
01145 static PyObject *
01146 valpy_int (PyObject *self)
01147 {
01148   struct value *value = ((value_object *) self)->value;
01149   struct type *type = value_type (value);
01150   LONGEST l = 0;
01151   volatile struct gdb_exception except;
01152 
01153   TRY_CATCH (except, RETURN_MASK_ALL)
01154     {
01155       CHECK_TYPEDEF (type);
01156       if (!is_intlike (type, 0))
01157         error (_("Cannot convert value to int."));
01158 
01159       l = value_as_long (value);
01160     }
01161   GDB_PY_HANDLE_EXCEPTION (except);
01162 
01163   return gdb_py_object_from_longest (l);
01164 }
01165 #endif
01166 
01167 /* Implements conversion to long.  */
01168 static PyObject *
01169 valpy_long (PyObject *self)
01170 {
01171   struct value *value = ((value_object *) self)->value;
01172   struct type *type = value_type (value);
01173   LONGEST l = 0;
01174   volatile struct gdb_exception except;
01175 
01176   TRY_CATCH (except, RETURN_MASK_ALL)
01177     {
01178       CHECK_TYPEDEF (type);
01179 
01180       if (!is_intlike (type, 1))
01181         error (_("Cannot convert value to long."));
01182 
01183       l = value_as_long (value);
01184     }
01185   GDB_PY_HANDLE_EXCEPTION (except);
01186 
01187   return gdb_py_long_from_longest (l);
01188 }
01189 
01190 /* Implements conversion to float.  */
01191 static PyObject *
01192 valpy_float (PyObject *self)
01193 {
01194   struct value *value = ((value_object *) self)->value;
01195   struct type *type = value_type (value);
01196   double d = 0;
01197   volatile struct gdb_exception except;
01198 
01199   TRY_CATCH (except, RETURN_MASK_ALL)
01200     {
01201       CHECK_TYPEDEF (type);
01202 
01203       if (TYPE_CODE (type) != TYPE_CODE_FLT)
01204         error (_("Cannot convert value to float."));
01205 
01206       d = value_as_double (value);
01207     }
01208   GDB_PY_HANDLE_EXCEPTION (except);
01209 
01210   return PyFloat_FromDouble (d);
01211 }
01212 
01213 /* Returns an object for a value which is released from the all_values chain,
01214    so its lifetime is not bound to the execution of a command.  */
01215 PyObject *
01216 value_to_value_object (struct value *val)
01217 {
01218   value_object *val_obj;
01219 
01220   val_obj = PyObject_New (value_object, &value_object_type);
01221   if (val_obj != NULL)
01222     {
01223       val_obj->value = val;
01224       release_value_or_incref (val);
01225       val_obj->address = NULL;
01226       val_obj->type = NULL;
01227       val_obj->dynamic_type = NULL;
01228       note_value (val_obj);
01229     }
01230 
01231   return (PyObject *) val_obj;
01232 }
01233 
01234 /* Returns a borrowed reference to the struct value corresponding to
01235    the given value object.  */
01236 struct value *
01237 value_object_to_value (PyObject *self)
01238 {
01239   value_object *real;
01240 
01241   if (! PyObject_TypeCheck (self, &value_object_type))
01242     return NULL;
01243   real = (value_object *) self;
01244   return real->value;
01245 }
01246 
01247 /* Try to convert a Python value to a gdb value.  If the value cannot
01248    be converted, set a Python exception and return NULL.  Returns a
01249    reference to a new value on the all_values chain.  */
01250 
01251 struct value *
01252 convert_value_from_python (PyObject *obj)
01253 {
01254   struct value *value = NULL; /* -Wall */
01255   volatile struct gdb_exception except;
01256   int cmp;
01257 
01258   gdb_assert (obj != NULL);
01259 
01260   TRY_CATCH (except, RETURN_MASK_ALL)
01261     {
01262       if (PyBool_Check (obj)) 
01263         {
01264           cmp = PyObject_IsTrue (obj);
01265           if (cmp >= 0)
01266             value = value_from_longest (builtin_type_pybool, cmp);
01267         }
01268       /* Make a long logic check first.  In Python 3.x, internally,
01269          all integers are represented as longs.  In Python 2.x, there
01270          is still a differentiation internally between a PyInt and a
01271          PyLong.  Explicitly do this long check conversion first. In
01272          GDB, for Python 3.x, we #ifdef PyInt = PyLong.  This check has
01273          to be done first to ensure we do not lose information in the
01274          conversion process.  */
01275       else if (PyLong_Check (obj))
01276         {
01277           LONGEST l = PyLong_AsLongLong (obj);
01278 
01279           if (PyErr_Occurred ())
01280             {
01281               /* If the error was an overflow, we can try converting to
01282                  ULONGEST instead.  */
01283               if (PyErr_ExceptionMatches (PyExc_OverflowError))
01284                 {
01285                   PyObject *etype, *evalue, *etraceback, *zero;
01286 
01287                   PyErr_Fetch (&etype, &evalue, &etraceback);
01288                   zero = PyInt_FromLong (0);
01289 
01290                   /* Check whether obj is positive.  */
01291                   if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0)
01292                     {
01293                       ULONGEST ul;
01294 
01295                       ul = PyLong_AsUnsignedLongLong (obj);
01296                       if (! PyErr_Occurred ())
01297                         value = value_from_ulongest (builtin_type_upylong, ul);
01298                     }
01299                   else
01300                     /* There's nothing we can do.  */
01301                     PyErr_Restore (etype, evalue, etraceback);
01302 
01303                   Py_DECREF (zero);
01304                 }
01305             }
01306           else
01307             value = value_from_longest (builtin_type_pylong, l);
01308         }
01309       else if (PyInt_Check (obj))
01310         {
01311           long l = PyInt_AsLong (obj);
01312 
01313           if (! PyErr_Occurred ())
01314             value = value_from_longest (builtin_type_pyint, l);
01315         }
01316       else if (PyFloat_Check (obj))
01317         {
01318           double d = PyFloat_AsDouble (obj);
01319 
01320           if (! PyErr_Occurred ())
01321             value = value_from_double (builtin_type_pyfloat, d);
01322         }
01323       else if (gdbpy_is_string (obj))
01324         {
01325           char *s;
01326 
01327           s = python_string_to_target_string (obj);
01328           if (s != NULL)
01329             {
01330               struct cleanup *old;
01331 
01332               old = make_cleanup (xfree, s);
01333               value = value_cstring (s, strlen (s), builtin_type_pychar);
01334               do_cleanups (old);
01335             }
01336         }
01337       else if (PyObject_TypeCheck (obj, &value_object_type))
01338         value = value_copy (((value_object *) obj)->value);
01339       else if (gdbpy_is_lazy_string (obj))
01340         {
01341           PyObject *result;
01342 
01343           result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst,  NULL);
01344           value = value_copy (((value_object *) result)->value);
01345         }
01346       else
01347 #ifdef IS_PY3K
01348         PyErr_Format (PyExc_TypeError,
01349                       _("Could not convert Python object: %S."), obj);
01350 #else
01351         PyErr_Format (PyExc_TypeError,
01352                       _("Could not convert Python object: %s."),
01353                       PyString_AsString (PyObject_Str (obj)));
01354 #endif
01355     }
01356   if (except.reason < 0)
01357     {
01358       PyErr_Format (except.reason == RETURN_QUIT
01359                     ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
01360                     "%s", except.message);
01361       return NULL;
01362     }
01363 
01364   return value;
01365 }
01366 
01367 /* Returns value object in the ARGth position in GDB's history.  */
01368 PyObject *
01369 gdbpy_history (PyObject *self, PyObject *args)
01370 {
01371   int i;
01372   struct value *res_val = NULL;   /* Initialize to appease gcc warning.  */
01373   volatile struct gdb_exception except;
01374 
01375   if (!PyArg_ParseTuple (args, "i", &i))
01376     return NULL;
01377 
01378   TRY_CATCH (except, RETURN_MASK_ALL)
01379     {
01380       res_val = access_value_history (i);
01381     }
01382   GDB_PY_HANDLE_EXCEPTION (except);
01383 
01384   return value_to_value_object (res_val);
01385 }
01386 
01387 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise.  */
01388 
01389 int
01390 gdbpy_is_value_object (PyObject *obj)
01391 {
01392   return PyObject_TypeCheck (obj, &value_object_type);
01393 }
01394 
01395 int
01396 gdbpy_initialize_values (void)
01397 {
01398   if (PyType_Ready (&value_object_type) < 0)
01399     return -1;
01400 
01401   return gdb_pymodule_addobject (gdb_module, "Value",
01402                                  (PyObject *) &value_object_type);
01403 }
01404 
01405 
01406 
01407 static PyGetSetDef value_object_getset[] = {
01408   { "address", valpy_get_address, NULL, "The address of the value.",
01409     NULL },
01410   { "is_optimized_out", valpy_get_is_optimized_out, NULL,
01411     "Boolean telling whether the value is optimized "
01412     "out (i.e., not available).",
01413     NULL },
01414   { "type", valpy_get_type, NULL, "Type of the value.", NULL },
01415   { "dynamic_type", valpy_get_dynamic_type, NULL,
01416     "Dynamic type of the value.", NULL },
01417   { "is_lazy", valpy_get_is_lazy, NULL,
01418     "Boolean telling whether the value is lazy (not fetched yet\n\
01419 from the inferior).  A lazy value is fetched when needed, or when\n\
01420 the \"fetch_lazy()\" method is called.", NULL },
01421   {NULL}  /* Sentinel */
01422 };
01423 
01424 static PyMethodDef value_object_methods[] = {
01425   { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
01426   { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
01427     "dynamic_cast (gdb.Type) -> gdb.Value\n\
01428 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
01429   },
01430   { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
01431     "reinterpret_cast (gdb.Type) -> gdb.Value\n\
01432 Cast the value to the supplied type, as if by the C++\n\
01433 reinterpret_cast operator."
01434   },
01435   { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
01436   { "referenced_value", valpy_referenced_value, METH_NOARGS,
01437     "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
01438   { "lazy_string", (PyCFunction) valpy_lazy_string,
01439     METH_VARARGS | METH_KEYWORDS,
01440     "lazy_string ([encoding]  [, length]) -> lazy_string\n\
01441 Return a lazy string representation of the value." },
01442   { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
01443     "string ([encoding] [, errors] [, length]) -> string\n\
01444 Return Unicode string representation of the value." },
01445   { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS, 
01446     "Fetches the value from the inferior, if it was lazy." },
01447   {NULL}  /* Sentinel */
01448 };
01449 
01450 static PyNumberMethods value_object_as_number = {
01451   valpy_add,
01452   valpy_subtract,
01453   valpy_multiply,
01454 #ifndef IS_PY3K
01455   valpy_divide,
01456 #endif
01457   valpy_remainder,
01458   NULL,                       /* nb_divmod */
01459   valpy_power,                /* nb_power */
01460   valpy_negative,             /* nb_negative */
01461   valpy_positive,             /* nb_positive */
01462   valpy_absolute,             /* nb_absolute */
01463   valpy_nonzero,              /* nb_nonzero */
01464   valpy_invert,               /* nb_invert */
01465   valpy_lsh,                  /* nb_lshift */
01466   valpy_rsh,                  /* nb_rshift */
01467   valpy_and,                  /* nb_and */
01468   valpy_xor,                  /* nb_xor */
01469   valpy_or,                   /* nb_or */
01470 #ifdef IS_PY3K
01471   valpy_long,                 /* nb_int */
01472   NULL,                       /* reserved */
01473 #else
01474   NULL,                       /* nb_coerce */
01475   valpy_int,                  /* nb_int */
01476   valpy_long,                 /* nb_long */
01477 #endif
01478   valpy_float,                /* nb_float */
01479 #ifndef IS_PY3K
01480   NULL,                       /* nb_oct */
01481   NULL,                       /* nb_hex */
01482 #endif
01483   NULL,                       /* nb_inplace_add */
01484   NULL,                       /* nb_inplace_subtract */
01485   NULL,                       /* nb_inplace_multiply */
01486   NULL,                       /* nb_inplace_remainder */
01487   NULL,                       /* nb_inplace_power */
01488   NULL,                       /* nb_inplace_lshift */
01489   NULL,                       /* nb_inplace_rshift */
01490   NULL,                       /* nb_inplace_and */
01491   NULL,                       /* nb_inplace_xor */
01492   NULL,                       /* nb_inplace_or */
01493   NULL,                       /* nb_floor_divide */
01494   valpy_divide                /* nb_true_divide */
01495 };
01496 
01497 static PyMappingMethods value_object_as_mapping = {
01498   valpy_length,
01499   valpy_getitem,
01500   valpy_setitem
01501 };
01502 
01503 PyTypeObject value_object_type = {
01504   PyVarObject_HEAD_INIT (NULL, 0)
01505   "gdb.Value",                    /*tp_name*/
01506   sizeof (value_object),          /*tp_basicsize*/
01507   0,                              /*tp_itemsize*/
01508   valpy_dealloc,                  /*tp_dealloc*/
01509   0,                              /*tp_print*/
01510   0,                              /*tp_getattr*/
01511   0,                              /*tp_setattr*/
01512   0,                              /*tp_compare*/
01513   0,                              /*tp_repr*/
01514   &value_object_as_number,        /*tp_as_number*/
01515   0,                              /*tp_as_sequence*/
01516   &value_object_as_mapping,       /*tp_as_mapping*/
01517   valpy_hash,                     /*tp_hash*/
01518   valpy_call,                     /*tp_call*/
01519   valpy_str,                      /*tp_str*/
01520   0,                              /*tp_getattro*/
01521   0,                              /*tp_setattro*/
01522   0,                              /*tp_as_buffer*/
01523   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
01524   | Py_TPFLAGS_BASETYPE,          /*tp_flags*/
01525   "GDB value object",             /* tp_doc */
01526   0,                              /* tp_traverse */
01527   0,                              /* tp_clear */
01528   valpy_richcompare,              /* tp_richcompare */
01529   0,                              /* tp_weaklistoffset */
01530   0,                              /* tp_iter */
01531   0,                              /* tp_iternext */
01532   value_object_methods,           /* tp_methods */
01533   0,                              /* tp_members */
01534   value_object_getset,            /* tp_getset */
01535   0,                              /* tp_base */
01536   0,                              /* tp_dict */
01537   0,                              /* tp_descr_get */
01538   0,                              /* tp_descr_set */
01539   0,                              /* tp_dictoffset */
01540   0,                              /* tp_init */
01541   0,                              /* tp_alloc */
01542   valpy_new                       /* tp_new */
01543 };
01544 
01545 #else
01546 
01547 void
01548 preserve_python_values (struct objfile *objfile, htab_t copied_types)
01549 {
01550   /* Nothing.  */
01551 }
01552 
01553 #endif /* HAVE_PYTHON */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines