]> Git Repo - binutils.git/blob - gdb/python/py-value.c
2013-12-12 Siva Chandra Reddy <[email protected]>
[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     obj->dynamic_type = valpy_get_type (self, NULL);
338   else
339     obj->dynamic_type = type_to_type_object (type);
340
341   Py_XINCREF (obj->dynamic_type);
342   return obj->dynamic_type;
343 }
344
345 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
346    string.  Return a PyObject representing a lazy_string_object type.
347    A lazy string is a pointer to a string with an optional encoding and
348    length.  If ENCODING is not given, encoding is set to None.  If an
349    ENCODING is provided the encoding parameter is set to ENCODING, but
350    the string is not encoded.  If LENGTH is provided then the length
351    parameter is set to LENGTH, otherwise length will be set to -1 (first
352    null of appropriate with).  */
353 static PyObject *
354 valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
355 {
356   gdb_py_longest length = -1;
357   struct value *value = ((value_object *) self)->value;
358   const char *user_encoding = NULL;
359   static char *keywords[] = { "encoding", "length", NULL };
360   PyObject *str_obj = NULL;
361   volatile struct gdb_exception except;
362
363   if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords,
364                                     &user_encoding, &length))
365     return NULL;
366
367   TRY_CATCH (except, RETURN_MASK_ALL)
368     {
369       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
370
371       if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
372         value = value_ind (value);
373
374       str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
375                                                  user_encoding,
376                                                  value_type (value));
377
378       do_cleanups (cleanup);
379     }
380   GDB_PY_HANDLE_EXCEPTION (except);
381
382   return str_obj;
383 }
384
385 /* Implementation of gdb.Value.string ([encoding] [, errors]
386    [, length]) -> string.  Return Unicode string with value contents.
387    If ENCODING is not given, the string is assumed to be encoded in
388    the target's charset.  If LENGTH is provided, only fetch string to
389    the length provided.  */
390
391 static PyObject *
392 valpy_string (PyObject *self, PyObject *args, PyObject *kw)
393 {
394   int length = -1;
395   gdb_byte *buffer;
396   struct value *value = ((value_object *) self)->value;
397   volatile struct gdb_exception except;
398   PyObject *unicode;
399   const char *encoding = NULL;
400   const char *errors = NULL;
401   const char *user_encoding = NULL;
402   const char *la_encoding = NULL;
403   struct type *char_type;
404   static char *keywords[] = { "encoding", "errors", "length", NULL };
405
406   if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
407                                     &user_encoding, &errors, &length))
408     return NULL;
409
410   TRY_CATCH (except, RETURN_MASK_ALL)
411     {
412       LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
413     }
414   GDB_PY_HANDLE_EXCEPTION (except);
415
416   encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
417   unicode = PyUnicode_Decode ((const char *) buffer,
418                               length * TYPE_LENGTH (char_type),
419                               encoding, errors);
420   xfree (buffer);
421
422   return unicode;
423 }
424
425 /* A helper function that implements the various cast operators.  */
426
427 static PyObject *
428 valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op)
429 {
430   PyObject *type_obj, *result = NULL;
431   struct type *type;
432   volatile struct gdb_exception except;
433
434   if (! PyArg_ParseTuple (args, "O", &type_obj))
435     return NULL;
436
437   type = type_object_to_type (type_obj);
438   if (! type)
439     {
440       PyErr_SetString (PyExc_RuntimeError,
441                        _("Argument must be a type."));
442       return NULL;
443     }
444
445   TRY_CATCH (except, RETURN_MASK_ALL)
446     {
447       struct value *val = ((value_object *) self)->value;
448       struct value *res_val;
449       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
450
451       if (op == UNOP_DYNAMIC_CAST)
452         res_val = value_dynamic_cast (type, val);
453       else if (op == UNOP_REINTERPRET_CAST)
454         res_val = value_reinterpret_cast (type, val);
455       else
456         {
457           gdb_assert (op == UNOP_CAST);
458           res_val = value_cast (type, val);
459         }
460
461       result = value_to_value_object (res_val);
462       do_cleanups (cleanup);
463     }
464   GDB_PY_HANDLE_EXCEPTION (except);
465
466   return result;
467 }
468
469 /* Implementation of the "cast" method.  */
470
471 static PyObject *
472 valpy_cast (PyObject *self, PyObject *args)
473 {
474   return valpy_do_cast (self, args, UNOP_CAST);
475 }
476
477 /* Implementation of the "dynamic_cast" method.  */
478
479 static PyObject *
480 valpy_dynamic_cast (PyObject *self, PyObject *args)
481 {
482   return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST);
483 }
484
485 /* Implementation of the "reinterpret_cast" method.  */
486
487 static PyObject *
488 valpy_reinterpret_cast (PyObject *self, PyObject *args)
489 {
490   return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST);
491 }
492
493 static Py_ssize_t
494 valpy_length (PyObject *self)
495 {
496   /* We don't support getting the number of elements in a struct / class.  */
497   PyErr_SetString (PyExc_NotImplementedError,
498                    _("Invalid operation on gdb.Value."));
499   return -1;
500 }
501
502 /* Return 1 if the gdb.Field object FIELD is present in the value V.
503    Returns 0 otherwise.  If any Python error occurs, -1 is returned.  */
504
505 static int
506 value_has_field (struct value *v, PyObject *field)
507 {
508   struct type *parent_type, *val_type;
509   enum type_code type_code;
510   PyObject *type_object = PyObject_GetAttrString (field, "parent_type");
511   volatile struct gdb_exception except;
512   int has_field = 0;
513
514   if (type_object == NULL)
515     return -1;
516
517   parent_type = type_object_to_type (type_object);
518   Py_DECREF (type_object);
519   if (parent_type == NULL)
520     {
521       PyErr_SetString (PyExc_TypeError,
522                        _("'parent_type' attribute of gdb.Field object is not a"
523                          "gdb.Type object."));
524       return -1;
525     }
526
527   TRY_CATCH (except, RETURN_MASK_ALL)
528     {
529       val_type = value_type (v);
530       val_type = check_typedef (val_type);
531       if (TYPE_CODE (val_type) == TYPE_CODE_REF
532           || TYPE_CODE (val_type) == TYPE_CODE_PTR)
533       val_type = check_typedef (TYPE_TARGET_TYPE (val_type));
534
535       type_code = TYPE_CODE (val_type);
536       if ((type_code == TYPE_CODE_STRUCT || type_code == TYPE_CODE_UNION)
537           && types_equal (val_type, parent_type))
538         has_field = 1;
539       else
540         has_field = 0;
541     }
542   GDB_PY_SET_HANDLE_EXCEPTION (except);
543
544   return has_field;
545 }
546
547 /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
548    Returns 1 if the flag value is true, 0 if it is false, and -1 if
549    a Python error occurs.  */
550
551 static int
552 get_field_flag (PyObject *field, const char *flag_name)
553 {
554   int flag_value;
555   PyObject *flag_object = PyObject_GetAttrString (field, flag_name);
556
557   if (flag_object == NULL)
558     return -1;
559
560   flag_value = PyObject_IsTrue (flag_object);
561   Py_DECREF (flag_object);
562
563   return flag_value;
564 }
565
566 /* Given string name or a gdb.Field object corresponding to an element inside
567    a structure, return its value object.  Returns NULL on error, with a python
568    exception set.  */
569
570 static PyObject *
571 valpy_getitem (PyObject *self, PyObject *key)
572 {
573   value_object *self_value = (value_object *) self;
574   char *field = NULL;
575   PyObject *base_class_type_object = NULL;
576   volatile struct gdb_exception except;
577   PyObject *result = NULL;
578
579   if (gdbpy_is_string (key))
580     {
581       field = python_string_to_host_string (key);
582       if (field == NULL)
583         return NULL;
584     }
585   else if (gdbpy_is_field (key))
586     {
587       int is_base_class, valid_field;
588
589       valid_field = value_has_field (self_value->value, key);
590       if (valid_field < 0)
591         return NULL;
592       else if (valid_field == 0)
593         {
594           PyErr_SetString (PyExc_TypeError,
595                            _("Invalid lookup for a field not contained in "
596                              "the value."));
597
598           return NULL;
599         }
600
601       is_base_class = get_field_flag (key, "is_base_class");
602       if (is_base_class < 0)
603         return NULL;
604       else if (is_base_class > 0)
605         {
606           base_class_type_object = PyObject_GetAttrString (key, "type");
607           if (base_class_type_object == NULL)
608             return NULL;
609         }
610       else
611         {
612           PyObject *name_obj = PyObject_GetAttrString (key, "name");
613
614           if (name_obj == NULL)
615             return NULL;
616
617           field = python_string_to_host_string (name_obj);
618           Py_DECREF (name_obj);
619           if (field == NULL)
620             return NULL;
621         }
622     }
623
624   TRY_CATCH (except, RETURN_MASK_ALL)
625     {
626       struct value *tmp = self_value->value;
627       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
628       struct value *res_val = NULL;
629
630       if (field)
631         res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
632       else if (base_class_type_object != NULL)
633         {
634           struct type *base_class_type, *val_type;
635
636           base_class_type = type_object_to_type (base_class_type_object);
637           Py_DECREF (base_class_type_object);
638           if (base_class_type == NULL)
639             error (_("Field type not an instance of gdb.Type."));
640
641           val_type = check_typedef (value_type (tmp));
642           if (TYPE_CODE (val_type) == TYPE_CODE_PTR)
643             res_val = value_cast (lookup_pointer_type (base_class_type), tmp);
644           else if (TYPE_CODE (val_type) == TYPE_CODE_REF)
645             res_val = value_cast (lookup_reference_type (base_class_type), tmp);
646           else
647             res_val = value_cast (base_class_type, tmp);
648         }
649       else
650         {
651           /* Assume we are attempting an array access, and let the
652              value code throw an exception if the index has an invalid
653              type.  */
654           struct value *idx = convert_value_from_python (key);
655
656           if (idx != NULL)
657             {
658               /* Check the value's type is something that can be accessed via
659                  a subscript.  */
660               struct type *type;
661
662               tmp = coerce_ref (tmp);
663               type = check_typedef (value_type (tmp));
664               if (TYPE_CODE (type) != TYPE_CODE_ARRAY
665                   && TYPE_CODE (type) != TYPE_CODE_PTR)
666                   error (_("Cannot subscript requested type."));
667               else
668                 res_val = value_subscript (tmp, value_as_long (idx));
669             }
670         }
671
672       if (res_val)
673         result = value_to_value_object (res_val);
674       do_cleanups (cleanup);
675     }
676
677   xfree (field);
678   GDB_PY_HANDLE_EXCEPTION (except);
679
680   return result;
681 }
682
683 static int
684 valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
685 {
686   PyErr_Format (PyExc_NotImplementedError,
687                 _("Setting of struct elements is not currently supported."));
688   return -1;
689 }
690
691 /* Called by the Python interpreter to perform an inferior function
692    call on the value.  Returns NULL on error, with a python exception set.  */
693 static PyObject *
694 valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
695 {
696   Py_ssize_t args_count;
697   volatile struct gdb_exception except;
698   struct value *function = ((value_object *) self)->value;
699   struct value **vargs = NULL;
700   struct type *ftype = NULL;
701   struct value *mark = value_mark ();
702   PyObject *result = NULL;
703
704   TRY_CATCH (except, RETURN_MASK_ALL)
705     {
706       ftype = check_typedef (value_type (function));
707     }
708   GDB_PY_HANDLE_EXCEPTION (except);
709
710   if (TYPE_CODE (ftype) != TYPE_CODE_FUNC)
711     {
712       PyErr_SetString (PyExc_RuntimeError,
713                        _("Value is not callable (not TYPE_CODE_FUNC)."));
714       return NULL;
715     }
716
717   if (! PyTuple_Check (args))
718     {
719       PyErr_SetString (PyExc_TypeError,
720                        _("Inferior arguments must be provided in a tuple."));
721       return NULL;
722     }
723
724   args_count = PyTuple_Size (args);
725   if (args_count > 0)
726     {
727       int i;
728
729       vargs = alloca (sizeof (struct value *) * args_count);
730       for (i = 0; i < args_count; i++)
731         {
732           PyObject *item = PyTuple_GetItem (args, i);
733
734           if (item == NULL)
735             return NULL;
736
737           vargs[i] = convert_value_from_python (item);
738           if (vargs[i] == NULL)
739             return NULL;
740         }
741     }
742
743   TRY_CATCH (except, RETURN_MASK_ALL)
744     {
745       struct cleanup *cleanup = make_cleanup_value_free_to_mark (mark);
746       struct value *return_value;
747
748       return_value = call_function_by_hand (function, args_count, vargs);
749       result = value_to_value_object (return_value);
750       do_cleanups (cleanup);
751     }
752   GDB_PY_HANDLE_EXCEPTION (except);
753
754   return result;
755 }
756
757 /* Called by the Python interpreter to obtain string representation
758    of the object.  */
759 static PyObject *
760 valpy_str (PyObject *self)
761 {
762   char *s = NULL;
763   PyObject *result;
764   struct value_print_options opts;
765   volatile struct gdb_exception except;
766
767   get_user_print_options (&opts);
768   opts.deref_ref = 0;
769
770   TRY_CATCH (except, RETURN_MASK_ALL)
771     {
772       struct ui_file *stb = mem_fileopen ();
773       struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
774
775       common_val_print (((value_object *) self)->value, stb, 0,
776                         &opts, python_language);
777       s = ui_file_xstrdup (stb, NULL);
778
779       do_cleanups (old_chain);
780     }
781   GDB_PY_HANDLE_EXCEPTION (except);
782
783   result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
784   xfree (s);
785
786   return result;
787 }
788
789 /* Implements gdb.Value.is_optimized_out.  */
790 static PyObject *
791 valpy_get_is_optimized_out (PyObject *self, void *closure)
792 {
793   struct value *value = ((value_object *) self)->value;
794   int opt = 0;
795   volatile struct gdb_exception except;
796
797   TRY_CATCH (except, RETURN_MASK_ALL)
798     {
799       opt = value_optimized_out (value);
800     }
801   GDB_PY_HANDLE_EXCEPTION (except);
802
803   if (opt)
804     Py_RETURN_TRUE;
805
806   Py_RETURN_FALSE;
807 }
808
809 /* Implements gdb.Value.is_lazy.  */
810 static PyObject *
811 valpy_get_is_lazy (PyObject *self, void *closure)
812 {
813   struct value *value = ((value_object *) self)->value;
814   int opt = 0;
815   volatile struct gdb_exception except;
816
817   TRY_CATCH (except, RETURN_MASK_ALL)
818     {
819       opt = value_lazy (value);
820     }
821   GDB_PY_HANDLE_EXCEPTION (except);
822
823   if (opt)
824     Py_RETURN_TRUE;
825
826   Py_RETURN_FALSE;
827 }
828
829 /* Implements gdb.Value.fetch_lazy ().  */
830 static PyObject *
831 valpy_fetch_lazy (PyObject *self, PyObject *args)
832 {
833   struct value *value = ((value_object *) self)->value;
834   volatile struct gdb_exception except;
835
836   TRY_CATCH (except, RETURN_MASK_ALL)
837     {
838       if (value_lazy (value))
839         value_fetch_lazy (value);
840     }
841   GDB_PY_HANDLE_EXCEPTION (except);
842
843   Py_RETURN_NONE;
844 }
845
846 /* Calculate and return the address of the PyObject as the value of
847    the builtin __hash__ call.  */
848 static long
849 valpy_hash (PyObject *self)
850 {
851   return (long) (intptr_t) self;
852 }
853
854 enum valpy_opcode
855 {
856   VALPY_ADD,
857   VALPY_SUB,
858   VALPY_MUL,
859   VALPY_DIV,
860   VALPY_REM,
861   VALPY_POW,
862   VALPY_LSH,
863   VALPY_RSH,
864   VALPY_BITAND,
865   VALPY_BITOR,
866   VALPY_BITXOR
867 };
868
869 /* If TYPE is a reference, return the target; otherwise return TYPE.  */
870 #define STRIP_REFERENCE(TYPE) \
871   ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
872
873 /* Returns a value object which is the result of applying the operation
874    specified by OPCODE to the given arguments.  Returns NULL on error, with
875    a python exception set.  */
876 static PyObject *
877 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
878 {
879   volatile struct gdb_exception except;
880   PyObject *result = NULL;
881
882   TRY_CATCH (except, RETURN_MASK_ALL)
883     {
884       struct value *arg1, *arg2;
885       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
886       struct value *res_val = NULL;
887
888       /* If the gdb.Value object is the second operand, then it will be passed
889          to us as the OTHER argument, and SELF will be an entirely different
890          kind of object, altogether.  Because of this, we can't assume self is
891          a gdb.Value object and need to convert it from python as well.  */
892       arg1 = convert_value_from_python (self);
893       if (arg1 == NULL)
894         {
895           do_cleanups (cleanup);
896           break;
897         }
898
899       arg2 = convert_value_from_python (other);
900       if (arg2 == NULL)
901         {
902           do_cleanups (cleanup);
903           break;
904         }
905
906       switch (opcode)
907         {
908         case VALPY_ADD:
909           {
910             struct type *ltype = value_type (arg1);
911             struct type *rtype = value_type (arg2);
912
913             CHECK_TYPEDEF (ltype);
914             ltype = STRIP_REFERENCE (ltype);
915             CHECK_TYPEDEF (rtype);
916             rtype = STRIP_REFERENCE (rtype);
917
918             if (TYPE_CODE (ltype) == TYPE_CODE_PTR
919                 && is_integral_type (rtype))
920               res_val = value_ptradd (arg1, value_as_long (arg2));
921             else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
922                      && is_integral_type (ltype))
923               res_val = value_ptradd (arg2, value_as_long (arg1));
924             else
925               res_val = value_binop (arg1, arg2, BINOP_ADD);
926           }
927           break;
928         case VALPY_SUB:
929           {
930             struct type *ltype = value_type (arg1);
931             struct type *rtype = value_type (arg2);
932
933             CHECK_TYPEDEF (ltype);
934             ltype = STRIP_REFERENCE (ltype);
935             CHECK_TYPEDEF (rtype);
936             rtype = STRIP_REFERENCE (rtype);
937
938             if (TYPE_CODE (ltype) == TYPE_CODE_PTR
939                 && TYPE_CODE (rtype) == TYPE_CODE_PTR)
940               /* A ptrdiff_t for the target would be preferable here.  */
941               res_val = value_from_longest (builtin_type_pyint,
942                                             value_ptrdiff (arg1, arg2));
943             else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
944                      && is_integral_type (rtype))
945               res_val = value_ptradd (arg1, - value_as_long (arg2));
946             else
947               res_val = value_binop (arg1, arg2, BINOP_SUB);
948           }
949           break;
950         case VALPY_MUL:
951           res_val = value_binop (arg1, arg2, BINOP_MUL);
952           break;
953         case VALPY_DIV:
954           res_val = value_binop (arg1, arg2, BINOP_DIV);
955           break;
956         case VALPY_REM:
957           res_val = value_binop (arg1, arg2, BINOP_REM);
958           break;
959         case VALPY_POW:
960           res_val = value_binop (arg1, arg2, BINOP_EXP);
961           break;
962         case VALPY_LSH:
963           res_val = value_binop (arg1, arg2, BINOP_LSH);
964           break;
965         case VALPY_RSH:
966           res_val = value_binop (arg1, arg2, BINOP_RSH);
967           break;
968         case VALPY_BITAND:
969           res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
970           break;
971         case VALPY_BITOR:
972           res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
973           break;
974         case VALPY_BITXOR:
975           res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
976           break;
977         }
978
979       if (res_val)
980         result = value_to_value_object (res_val);
981
982       do_cleanups (cleanup);
983     }
984   GDB_PY_HANDLE_EXCEPTION (except);
985
986   return result;
987 }
988
989 static PyObject *
990 valpy_add (PyObject *self, PyObject *other)
991 {
992   return valpy_binop (VALPY_ADD, self, other);
993 }
994
995 static PyObject *
996 valpy_subtract (PyObject *self, PyObject *other)
997 {
998   return valpy_binop (VALPY_SUB, self, other);
999 }
1000
1001 static PyObject *
1002 valpy_multiply (PyObject *self, PyObject *other)
1003 {
1004   return valpy_binop (VALPY_MUL, self, other);
1005 }
1006
1007 static PyObject *
1008 valpy_divide (PyObject *self, PyObject *other)
1009 {
1010   return valpy_binop (VALPY_DIV, self, other);
1011 }
1012
1013 static PyObject *
1014 valpy_remainder (PyObject *self, PyObject *other)
1015 {
1016   return valpy_binop (VALPY_REM, self, other);
1017 }
1018
1019 static PyObject *
1020 valpy_power (PyObject *self, PyObject *other, PyObject *unused)
1021 {
1022   /* We don't support the ternary form of pow.  I don't know how to express
1023      that, so let's just throw NotImplementedError to at least do something
1024      about it.  */
1025   if (unused != Py_None)
1026     {
1027       PyErr_SetString (PyExc_NotImplementedError,
1028                        "Invalid operation on gdb.Value.");
1029       return NULL;
1030     }
1031
1032   return valpy_binop (VALPY_POW, self, other);
1033 }
1034
1035 static PyObject *
1036 valpy_negative (PyObject *self)
1037 {
1038   volatile struct gdb_exception except;
1039   PyObject *result = NULL;
1040
1041   TRY_CATCH (except, RETURN_MASK_ALL)
1042     {
1043       /* Perhaps overkill, but consistency has some virtue.  */
1044       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1045       struct value *val;
1046
1047       val = value_neg (((value_object *) self)->value);
1048       result = value_to_value_object (val);
1049       do_cleanups (cleanup);
1050     }
1051   GDB_PY_HANDLE_EXCEPTION (except);
1052
1053   return result;
1054 }
1055
1056 static PyObject *
1057 valpy_positive (PyObject *self)
1058 {
1059   return value_to_value_object (((value_object *) self)->value);
1060 }
1061
1062 static PyObject *
1063 valpy_absolute (PyObject *self)
1064 {
1065   struct value *value = ((value_object *) self)->value;
1066   volatile struct gdb_exception except;
1067   int isabs = 1;
1068
1069   TRY_CATCH (except, RETURN_MASK_ALL)
1070     {
1071       struct cleanup *cleanup = make_cleanup_value_free_to_mark (value_mark ());
1072
1073       if (value_less (value, value_zero (value_type (value), not_lval)))
1074         isabs = 0;
1075
1076       do_cleanups (cleanup);
1077     }
1078   GDB_PY_HANDLE_EXCEPTION (except);
1079
1080   if (isabs)
1081     return valpy_positive (self);
1082   else
1083     return valpy_negative (self);
1084 }
1085
1086 /* Implements boolean evaluation of gdb.Value.  */
1087 static int
1088 valpy_nonzero (PyObject *self)
1089 {
1090   volatile struct gdb_exception except;
1091   value_object *self_value = (value_object *) self;
1092   struct type *type;
1093   int nonzero = 0; /* Appease GCC warning.  */
1094
1095   TRY_CATCH (except, RETURN_MASK_ALL)
1096     {
1097       type = check_typedef (value_type (self_value->value));
1098
1099       if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
1100         nonzero = !!value_as_long (self_value->value);
1101       else if (TYPE_CODE (type) == TYPE_CODE_FLT)
1102         nonzero = value_as_double (self_value->value) != 0;
1103       else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1104         nonzero = !decimal_is_zero (value_contents (self_value->value),
1105                                  TYPE_LENGTH (type),
1106                                  gdbarch_byte_order (get_type_arch (type)));
1107       else
1108         /* All other values are True.  */
1109         nonzero = 1;
1110     }
1111   /* This is not documented in the Python documentation, but if this
1112      function fails, return -1 as slot_nb_nonzero does (the default
1113      Python nonzero function).  */
1114   GDB_PY_SET_HANDLE_EXCEPTION (except);
1115
1116   return nonzero;
1117 }
1118
1119 /* Implements ~ for value objects.  */
1120 static PyObject *
1121 valpy_invert (PyObject *self)
1122 {
1123   struct value *val = NULL;
1124   volatile struct gdb_exception except;
1125
1126   TRY_CATCH (except, RETURN_MASK_ALL)
1127     {
1128       val = value_complement (((value_object *) self)->value);
1129     }
1130   GDB_PY_HANDLE_EXCEPTION (except);
1131
1132   return value_to_value_object (val);
1133 }
1134
1135 /* Implements left shift for value objects.  */
1136 static PyObject *
1137 valpy_lsh (PyObject *self, PyObject *other)
1138 {
1139   return valpy_binop (VALPY_LSH, self, other);
1140 }
1141
1142 /* Implements right shift for value objects.  */
1143 static PyObject *
1144 valpy_rsh (PyObject *self, PyObject *other)
1145 {
1146   return valpy_binop (VALPY_RSH, self, other);
1147 }
1148
1149 /* Implements bitwise and for value objects.  */
1150 static PyObject *
1151 valpy_and (PyObject *self, PyObject *other)
1152 {
1153   return valpy_binop (VALPY_BITAND, self, other);
1154 }
1155
1156 /* Implements bitwise or for value objects.  */
1157 static PyObject *
1158 valpy_or (PyObject *self, PyObject *other)
1159 {
1160   return valpy_binop (VALPY_BITOR, self, other);
1161 }
1162
1163 /* Implements bitwise xor for value objects.  */
1164 static PyObject *
1165 valpy_xor (PyObject *self, PyObject *other)
1166 {
1167   return valpy_binop (VALPY_BITXOR, self, other);
1168 }
1169
1170 /* Implements comparison operations for value objects.  Returns NULL on error,
1171    with a python exception set.  */
1172 static PyObject *
1173 valpy_richcompare (PyObject *self, PyObject *other, int op)
1174 {
1175   int result = 0;
1176   volatile struct gdb_exception except;
1177
1178   if (other == Py_None)
1179     /* Comparing with None is special.  From what I can tell, in Python
1180        None is smaller than anything else.  */
1181     switch (op) {
1182       case Py_LT:
1183       case Py_LE:
1184       case Py_EQ:
1185         Py_RETURN_FALSE;
1186       case Py_NE:
1187       case Py_GT:
1188       case Py_GE:
1189         Py_RETURN_TRUE;
1190       default:
1191         /* Can't happen.  */
1192         PyErr_SetString (PyExc_NotImplementedError,
1193                          _("Invalid operation on gdb.Value."));
1194         return NULL;
1195     }
1196
1197   TRY_CATCH (except, RETURN_MASK_ALL)
1198     {
1199       struct value *value_other, *mark = value_mark ();
1200       struct cleanup *cleanup;
1201
1202       value_other = convert_value_from_python (other);
1203       if (value_other == NULL)
1204         {
1205           result = -1;
1206           break;
1207         }
1208
1209       cleanup = make_cleanup_value_free_to_mark (mark);
1210
1211       switch (op) {
1212         case Py_LT:
1213           result = value_less (((value_object *) self)->value, value_other);
1214           break;
1215         case Py_LE:
1216           result = value_less (((value_object *) self)->value, value_other)
1217             || value_equal (((value_object *) self)->value, value_other);
1218           break;
1219         case Py_EQ:
1220           result = value_equal (((value_object *) self)->value, value_other);
1221           break;
1222         case Py_NE:
1223           result = !value_equal (((value_object *) self)->value, value_other);
1224           break;
1225         case Py_GT:
1226           result = value_less (value_other, ((value_object *) self)->value);
1227           break;
1228         case Py_GE:
1229           result = value_less (value_other, ((value_object *) self)->value)
1230             || value_equal (((value_object *) self)->value, value_other);
1231           break;
1232         default:
1233           /* Can't happen.  */
1234           PyErr_SetString (PyExc_NotImplementedError,
1235                            _("Invalid operation on gdb.Value."));
1236           result = -1;
1237           break;
1238       }
1239
1240       do_cleanups (cleanup);
1241     }
1242   GDB_PY_HANDLE_EXCEPTION (except);
1243
1244   /* In this case, the Python exception has already been set.  */
1245   if (result < 0)
1246     return NULL;
1247
1248   if (result == 1)
1249     Py_RETURN_TRUE;
1250
1251   Py_RETURN_FALSE;
1252 }
1253
1254 #ifndef IS_PY3K
1255 /* Implements conversion to int.  */
1256 static PyObject *
1257 valpy_int (PyObject *self)
1258 {
1259   struct value *value = ((value_object *) self)->value;
1260   struct type *type = value_type (value);
1261   LONGEST l = 0;
1262   volatile struct gdb_exception except;
1263
1264   TRY_CATCH (except, RETURN_MASK_ALL)
1265     {
1266       if (!is_integral_type (type))
1267         error (_("Cannot convert value to int."));
1268
1269       l = value_as_long (value);
1270     }
1271   GDB_PY_HANDLE_EXCEPTION (except);
1272
1273   return gdb_py_object_from_longest (l);
1274 }
1275 #endif
1276
1277 /* Implements conversion to long.  */
1278 static PyObject *
1279 valpy_long (PyObject *self)
1280 {
1281   struct value *value = ((value_object *) self)->value;
1282   struct type *type = value_type (value);
1283   LONGEST l = 0;
1284   volatile struct gdb_exception except;
1285
1286   TRY_CATCH (except, RETURN_MASK_ALL)
1287     {
1288       CHECK_TYPEDEF (type);
1289
1290       if (!is_integral_type (type)
1291           && TYPE_CODE (type) != TYPE_CODE_PTR)
1292         error (_("Cannot convert value to long."));
1293
1294       l = value_as_long (value);
1295     }
1296   GDB_PY_HANDLE_EXCEPTION (except);
1297
1298   return gdb_py_long_from_longest (l);
1299 }
1300
1301 /* Implements conversion to float.  */
1302 static PyObject *
1303 valpy_float (PyObject *self)
1304 {
1305   struct value *value = ((value_object *) self)->value;
1306   struct type *type = value_type (value);
1307   double d = 0;
1308   volatile struct gdb_exception except;
1309
1310   TRY_CATCH (except, RETURN_MASK_ALL)
1311     {
1312       CHECK_TYPEDEF (type);
1313
1314       if (TYPE_CODE (type) != TYPE_CODE_FLT)
1315         error (_("Cannot convert value to float."));
1316
1317       d = value_as_double (value);
1318     }
1319   GDB_PY_HANDLE_EXCEPTION (except);
1320
1321   return PyFloat_FromDouble (d);
1322 }
1323
1324 /* Returns an object for a value which is released from the all_values chain,
1325    so its lifetime is not bound to the execution of a command.  */
1326 PyObject *
1327 value_to_value_object (struct value *val)
1328 {
1329   value_object *val_obj;
1330
1331   val_obj = PyObject_New (value_object, &value_object_type);
1332   if (val_obj != NULL)
1333     {
1334       val_obj->value = val;
1335       release_value_or_incref (val);
1336       val_obj->address = NULL;
1337       val_obj->type = NULL;
1338       val_obj->dynamic_type = NULL;
1339       note_value (val_obj);
1340     }
1341
1342   return (PyObject *) val_obj;
1343 }
1344
1345 /* Returns a borrowed reference to the struct value corresponding to
1346    the given value object.  */
1347 struct value *
1348 value_object_to_value (PyObject *self)
1349 {
1350   value_object *real;
1351
1352   if (! PyObject_TypeCheck (self, &value_object_type))
1353     return NULL;
1354   real = (value_object *) self;
1355   return real->value;
1356 }
1357
1358 /* Try to convert a Python value to a gdb value.  If the value cannot
1359    be converted, set a Python exception and return NULL.  Returns a
1360    reference to a new value on the all_values chain.  */
1361
1362 struct value *
1363 convert_value_from_python (PyObject *obj)
1364 {
1365   struct value *value = NULL; /* -Wall */
1366   volatile struct gdb_exception except;
1367   int cmp;
1368
1369   gdb_assert (obj != NULL);
1370
1371   TRY_CATCH (except, RETURN_MASK_ALL)
1372     {
1373       if (PyBool_Check (obj))
1374         {
1375           cmp = PyObject_IsTrue (obj);
1376           if (cmp >= 0)
1377             value = value_from_longest (builtin_type_pybool, cmp);
1378         }
1379       /* Make a long logic check first.  In Python 3.x, internally,
1380          all integers are represented as longs.  In Python 2.x, there
1381          is still a differentiation internally between a PyInt and a
1382          PyLong.  Explicitly do this long check conversion first. In
1383          GDB, for Python 3.x, we #ifdef PyInt = PyLong.  This check has
1384          to be done first to ensure we do not lose information in the
1385          conversion process.  */
1386       else if (PyLong_Check (obj))
1387         {
1388           LONGEST l = PyLong_AsLongLong (obj);
1389
1390           if (PyErr_Occurred ())
1391             {
1392               /* If the error was an overflow, we can try converting to
1393                  ULONGEST instead.  */
1394               if (PyErr_ExceptionMatches (PyExc_OverflowError))
1395                 {
1396                   PyObject *etype, *evalue, *etraceback, *zero;
1397
1398                   PyErr_Fetch (&etype, &evalue, &etraceback);
1399                   zero = PyInt_FromLong (0);
1400
1401                   /* Check whether obj is positive.  */
1402                   if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0)
1403                     {
1404                       ULONGEST ul;
1405
1406                       ul = PyLong_AsUnsignedLongLong (obj);
1407                       if (! PyErr_Occurred ())
1408                         value = value_from_ulongest (builtin_type_upylong, ul);
1409                     }
1410                   else
1411                     /* There's nothing we can do.  */
1412                     PyErr_Restore (etype, evalue, etraceback);
1413
1414                   Py_DECREF (zero);
1415                 }
1416             }
1417           else
1418             value = value_from_longest (builtin_type_pylong, l);
1419         }
1420       else if (PyInt_Check (obj))
1421         {
1422           long l = PyInt_AsLong (obj);
1423
1424           if (! PyErr_Occurred ())
1425             value = value_from_longest (builtin_type_pyint, l);
1426         }
1427       else if (PyFloat_Check (obj))
1428         {
1429           double d = PyFloat_AsDouble (obj);
1430
1431           if (! PyErr_Occurred ())
1432             value = value_from_double (builtin_type_pyfloat, d);
1433         }
1434       else if (gdbpy_is_string (obj))
1435         {
1436           char *s;
1437
1438           s = python_string_to_target_string (obj);
1439           if (s != NULL)
1440             {
1441               struct cleanup *old;
1442
1443               old = make_cleanup (xfree, s);
1444               value = value_cstring (s, strlen (s), builtin_type_pychar);
1445               do_cleanups (old);
1446             }
1447         }
1448       else if (PyObject_TypeCheck (obj, &value_object_type))
1449         value = value_copy (((value_object *) obj)->value);
1450       else if (gdbpy_is_lazy_string (obj))
1451         {
1452           PyObject *result;
1453
1454           result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst,  NULL);
1455           value = value_copy (((value_object *) result)->value);
1456         }
1457       else
1458 #ifdef IS_PY3K
1459         PyErr_Format (PyExc_TypeError,
1460                       _("Could not convert Python object: %S."), obj);
1461 #else
1462         PyErr_Format (PyExc_TypeError,
1463                       _("Could not convert Python object: %s."),
1464                       PyString_AsString (PyObject_Str (obj)));
1465 #endif
1466     }
1467   if (except.reason < 0)
1468     {
1469       PyErr_Format (except.reason == RETURN_QUIT
1470                     ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
1471                     "%s", except.message);
1472       return NULL;
1473     }
1474
1475   return value;
1476 }
1477
1478 /* Returns value object in the ARGth position in GDB's history.  */
1479 PyObject *
1480 gdbpy_history (PyObject *self, PyObject *args)
1481 {
1482   int i;
1483   struct value *res_val = NULL;   /* Initialize to appease gcc warning.  */
1484   volatile struct gdb_exception except;
1485
1486   if (!PyArg_ParseTuple (args, "i", &i))
1487     return NULL;
1488
1489   TRY_CATCH (except, RETURN_MASK_ALL)
1490     {
1491       res_val = access_value_history (i);
1492     }
1493   GDB_PY_HANDLE_EXCEPTION (except);
1494
1495   return value_to_value_object (res_val);
1496 }
1497
1498 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise.  */
1499
1500 int
1501 gdbpy_is_value_object (PyObject *obj)
1502 {
1503   return PyObject_TypeCheck (obj, &value_object_type);
1504 }
1505
1506 int
1507 gdbpy_initialize_values (void)
1508 {
1509   if (PyType_Ready (&value_object_type) < 0)
1510     return -1;
1511
1512   return gdb_pymodule_addobject (gdb_module, "Value",
1513                                  (PyObject *) &value_object_type);
1514 }
1515
1516 \f
1517
1518 static PyGetSetDef value_object_getset[] = {
1519   { "address", valpy_get_address, NULL, "The address of the value.",
1520     NULL },
1521   { "is_optimized_out", valpy_get_is_optimized_out, NULL,
1522     "Boolean telling whether the value is optimized "
1523     "out (i.e., not available).",
1524     NULL },
1525   { "type", valpy_get_type, NULL, "Type of the value.", NULL },
1526   { "dynamic_type", valpy_get_dynamic_type, NULL,
1527     "Dynamic type of the value.", NULL },
1528   { "is_lazy", valpy_get_is_lazy, NULL,
1529     "Boolean telling whether the value is lazy (not fetched yet\n\
1530 from the inferior).  A lazy value is fetched when needed, or when\n\
1531 the \"fetch_lazy()\" method is called.", NULL },
1532   {NULL}  /* Sentinel */
1533 };
1534
1535 static PyMethodDef value_object_methods[] = {
1536   { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
1537   { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS,
1538     "dynamic_cast (gdb.Type) -> gdb.Value\n\
1539 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1540   },
1541   { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS,
1542     "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1543 Cast the value to the supplied type, as if by the C++\n\
1544 reinterpret_cast operator."
1545   },
1546   { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
1547   { "referenced_value", valpy_referenced_value, METH_NOARGS,
1548     "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1549   { "lazy_string", (PyCFunction) valpy_lazy_string,
1550     METH_VARARGS | METH_KEYWORDS,
1551     "lazy_string ([encoding]  [, length]) -> lazy_string\n\
1552 Return a lazy string representation of the value." },
1553   { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
1554     "string ([encoding] [, errors] [, length]) -> string\n\
1555 Return Unicode string representation of the value." },
1556   { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
1557     "Fetches the value from the inferior, if it was lazy." },
1558   {NULL}  /* Sentinel */
1559 };
1560
1561 static PyNumberMethods value_object_as_number = {
1562   valpy_add,
1563   valpy_subtract,
1564   valpy_multiply,
1565 #ifndef IS_PY3K
1566   valpy_divide,
1567 #endif
1568   valpy_remainder,
1569   NULL,                       /* nb_divmod */
1570   valpy_power,                /* nb_power */
1571   valpy_negative,             /* nb_negative */
1572   valpy_positive,             /* nb_positive */
1573   valpy_absolute,             /* nb_absolute */
1574   valpy_nonzero,              /* nb_nonzero */
1575   valpy_invert,               /* nb_invert */
1576   valpy_lsh,                  /* nb_lshift */
1577   valpy_rsh,                  /* nb_rshift */
1578   valpy_and,                  /* nb_and */
1579   valpy_xor,                  /* nb_xor */
1580   valpy_or,                   /* nb_or */
1581 #ifdef IS_PY3K
1582   valpy_long,                 /* nb_int */
1583   NULL,                       /* reserved */
1584 #else
1585   NULL,                       /* nb_coerce */
1586   valpy_int,                  /* nb_int */
1587   valpy_long,                 /* nb_long */
1588 #endif
1589   valpy_float,                /* nb_float */
1590 #ifndef IS_PY3K
1591   NULL,                       /* nb_oct */
1592   NULL,                       /* nb_hex */
1593 #endif
1594   NULL,                       /* nb_inplace_add */
1595   NULL,                       /* nb_inplace_subtract */
1596   NULL,                       /* nb_inplace_multiply */
1597   NULL,                       /* nb_inplace_remainder */
1598   NULL,                       /* nb_inplace_power */
1599   NULL,                       /* nb_inplace_lshift */
1600   NULL,                       /* nb_inplace_rshift */
1601   NULL,                       /* nb_inplace_and */
1602   NULL,                       /* nb_inplace_xor */
1603   NULL,                       /* nb_inplace_or */
1604   NULL,                       /* nb_floor_divide */
1605   valpy_divide                /* nb_true_divide */
1606 };
1607
1608 static PyMappingMethods value_object_as_mapping = {
1609   valpy_length,
1610   valpy_getitem,
1611   valpy_setitem
1612 };
1613
1614 PyTypeObject value_object_type = {
1615   PyVarObject_HEAD_INIT (NULL, 0)
1616   "gdb.Value",                    /*tp_name*/
1617   sizeof (value_object),          /*tp_basicsize*/
1618   0,                              /*tp_itemsize*/
1619   valpy_dealloc,                  /*tp_dealloc*/
1620   0,                              /*tp_print*/
1621   0,                              /*tp_getattr*/
1622   0,                              /*tp_setattr*/
1623   0,                              /*tp_compare*/
1624   0,                              /*tp_repr*/
1625   &value_object_as_number,        /*tp_as_number*/
1626   0,                              /*tp_as_sequence*/
1627   &value_object_as_mapping,       /*tp_as_mapping*/
1628   valpy_hash,                     /*tp_hash*/
1629   valpy_call,                     /*tp_call*/
1630   valpy_str,                      /*tp_str*/
1631   0,                              /*tp_getattro*/
1632   0,                              /*tp_setattro*/
1633   0,                              /*tp_as_buffer*/
1634   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES
1635   | Py_TPFLAGS_BASETYPE,          /*tp_flags*/
1636   "GDB value object",             /* tp_doc */
1637   0,                              /* tp_traverse */
1638   0,                              /* tp_clear */
1639   valpy_richcompare,              /* tp_richcompare */
1640   0,                              /* tp_weaklistoffset */
1641   0,                              /* tp_iter */
1642   0,                              /* tp_iternext */
1643   value_object_methods,           /* tp_methods */
1644   0,                              /* tp_members */
1645   value_object_getset,            /* tp_getset */
1646   0,                              /* tp_base */
1647   0,                              /* tp_dict */
1648   0,                              /* tp_descr_get */
1649   0,                              /* tp_descr_set */
1650   0,                              /* tp_dictoffset */
1651   0,                              /* tp_init */
1652   0,                              /* tp_alloc */
1653   valpy_new                       /* tp_new */
1654 };
1655
1656 #else
1657
1658 void
1659 preserve_python_values (struct objfile *objfile, htab_t copied_types)
1660 {
1661   /* Nothing.  */
1662 }
1663
1664 #endif /* HAVE_PYTHON */
This page took 0.120024 seconds and 4 git commands to generate.