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