Commit | Line | Data |
---|---|---|
7843261b TJB |
1 | /* Python interface to values. |
2 | ||
4c38e0a4 | 3 | Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc. |
7843261b TJB |
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" | |
08c637de | 21 | #include "gdb_assert.h" |
7843261b TJB |
22 | #include "charset.h" |
23 | #include "value.h" | |
24 | #include "exceptions.h" | |
25 | #include "language.h" | |
26 | #include "dfp.h" | |
79a45b7d | 27 | #include "valprint.h" |
7843261b | 28 | |
7843261b TJB |
29 | #ifdef HAVE_PYTHON |
30 | ||
31 | #include "python-internal.h" | |
32 | ||
33 | /* Even though Python scalar types directly map to host types, we use | |
34 | target types here to remain consistent with the the values system in | |
35 | GDB (which uses target arithmetic). */ | |
36 | ||
37 | /* Python's integer type corresponds to C's long type. */ | |
d452c4bc | 38 | #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long |
7843261b TJB |
39 | |
40 | /* Python's float type corresponds to C's double type. */ | |
d452c4bc | 41 | #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double |
7843261b TJB |
42 | |
43 | /* Python's long type corresponds to C's long long type. */ | |
d452c4bc | 44 | #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long |
7843261b | 45 | |
595939de PM |
46 | /* Python's long type corresponds to C's long long type. Unsigned version. */ |
47 | #define builtin_type_upylong builtin_type \ | |
48 | (python_gdbarch)->builtin_unsigned_long_long | |
49 | ||
7843261b | 50 | #define builtin_type_pybool \ |
d452c4bc | 51 | language_bool_type (python_language, python_gdbarch) |
7843261b | 52 | |
3b7538c0 | 53 | #define builtin_type_pychar \ |
d452c4bc | 54 | language_string_char_type (python_language, python_gdbarch) |
3b7538c0 | 55 | |
4e7a5ef5 | 56 | typedef struct value_object { |
7843261b | 57 | PyObject_HEAD |
4e7a5ef5 TT |
58 | struct value_object *next; |
59 | struct value_object *prev; | |
7843261b | 60 | struct value *value; |
c0c6f777 | 61 | PyObject *address; |
2c74e833 | 62 | PyObject *type; |
7843261b TJB |
63 | } value_object; |
64 | ||
4e7a5ef5 TT |
65 | /* List of all values which are currently exposed to Python. It is |
66 | maintained so that when an objfile is discarded, preserve_values | |
67 | can copy the values' types if needed. */ | |
68 | /* This variable is unnecessarily initialized to NULL in order to | |
69 | work around a linker bug on MacOS. */ | |
70 | static value_object *values_in_python = NULL; | |
71 | ||
7843261b TJB |
72 | /* Called by the Python interpreter when deallocating a value object. */ |
73 | static void | |
74 | valpy_dealloc (PyObject *obj) | |
75 | { | |
76 | value_object *self = (value_object *) obj; | |
77 | ||
4e7a5ef5 TT |
78 | /* Remove SELF from the global list. */ |
79 | if (self->prev) | |
80 | self->prev->next = self->next; | |
81 | else | |
82 | { | |
83 | gdb_assert (values_in_python == self); | |
84 | values_in_python = self->next; | |
85 | } | |
86 | if (self->next) | |
87 | self->next->prev = self->prev; | |
7843261b | 88 | |
77316f4c | 89 | value_free (self->value); |
c0c6f777 TJB |
90 | |
91 | if (self->address) | |
92 | /* Use braces to appease gcc warning. *sigh* */ | |
93 | { | |
94 | Py_DECREF (self->address); | |
95 | } | |
96 | ||
2c74e833 TT |
97 | if (self->type) |
98 | { | |
99 | Py_DECREF (self->type); | |
100 | } | |
101 | ||
7843261b TJB |
102 | self->ob_type->tp_free (self); |
103 | } | |
104 | ||
4e7a5ef5 TT |
105 | /* Helper to push a Value object on the global list. */ |
106 | static void | |
107 | note_value (value_object *value_obj) | |
108 | { | |
109 | value_obj->next = values_in_python; | |
110 | if (value_obj->next) | |
111 | value_obj->next->prev = value_obj; | |
112 | value_obj->prev = NULL; | |
113 | values_in_python = value_obj; | |
114 | } | |
115 | ||
7843261b TJB |
116 | /* Called when a new gdb.Value object needs to be allocated. */ |
117 | static PyObject * | |
118 | valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords) | |
119 | { | |
120 | struct value *value = NULL; /* Initialize to appease gcc warning. */ | |
121 | value_object *value_obj; | |
7843261b TJB |
122 | |
123 | if (PyTuple_Size (args) != 1) | |
124 | { | |
125 | PyErr_SetString (PyExc_TypeError, _("Value object creation takes only " | |
126 | "1 argument")); | |
127 | return NULL; | |
128 | } | |
129 | ||
130 | value_obj = (value_object *) subtype->tp_alloc (subtype, 1); | |
131 | if (value_obj == NULL) | |
132 | { | |
133 | PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to " | |
134 | "create Value object.")); | |
135 | return NULL; | |
136 | } | |
137 | ||
08c637de TJB |
138 | value = convert_value_from_python (PyTuple_GetItem (args, 0)); |
139 | if (value == NULL) | |
7843261b TJB |
140 | { |
141 | subtype->tp_free (value_obj); | |
08c637de | 142 | return NULL; |
7843261b TJB |
143 | } |
144 | ||
145 | value_obj->value = value; | |
4e7a5ef5 | 146 | value_incref (value); |
c0c6f777 | 147 | value_obj->address = NULL; |
2c74e833 | 148 | value_obj->type = NULL; |
4e7a5ef5 | 149 | note_value (value_obj); |
7843261b TJB |
150 | |
151 | return (PyObject *) value_obj; | |
152 | } | |
153 | ||
4e7a5ef5 TT |
154 | /* Iterate over all the Value objects, calling preserve_one_value on |
155 | each. */ | |
156 | void | |
157 | preserve_python_values (struct objfile *objfile, htab_t copied_types) | |
158 | { | |
159 | value_object *iter; | |
160 | ||
161 | for (iter = values_in_python; iter; iter = iter->next) | |
162 | preserve_one_value (iter->value, objfile, copied_types); | |
163 | } | |
164 | ||
7843261b TJB |
165 | /* Given a value of a pointer type, apply the C unary * operator to it. */ |
166 | static PyObject * | |
167 | valpy_dereference (PyObject *self, PyObject *args) | |
168 | { | |
169 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
170 | volatile struct gdb_exception except; | |
171 | ||
172 | TRY_CATCH (except, RETURN_MASK_ALL) | |
173 | { | |
174 | res_val = value_ind (((value_object *) self)->value); | |
175 | } | |
176 | GDB_PY_HANDLE_EXCEPTION (except); | |
177 | ||
178 | return value_to_value_object (res_val); | |
179 | } | |
180 | ||
08c637de TJB |
181 | /* Return "&value". */ |
182 | static PyObject * | |
c0c6f777 | 183 | valpy_get_address (PyObject *self, void *closure) |
08c637de TJB |
184 | { |
185 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
c0c6f777 | 186 | value_object *val_obj = (value_object *) self; |
08c637de TJB |
187 | volatile struct gdb_exception except; |
188 | ||
c0c6f777 | 189 | if (!val_obj->address) |
08c637de | 190 | { |
c0c6f777 TJB |
191 | TRY_CATCH (except, RETURN_MASK_ALL) |
192 | { | |
193 | res_val = value_addr (val_obj->value); | |
194 | } | |
195 | if (except.reason < 0) | |
196 | { | |
197 | val_obj->address = Py_None; | |
198 | Py_INCREF (Py_None); | |
199 | } | |
200 | else | |
201 | val_obj->address = value_to_value_object (res_val); | |
08c637de | 202 | } |
08c637de | 203 | |
c0c6f777 TJB |
204 | Py_INCREF (val_obj->address); |
205 | ||
206 | return val_obj->address; | |
08c637de TJB |
207 | } |
208 | ||
2c74e833 TT |
209 | /* Return type of the value. */ |
210 | static PyObject * | |
211 | valpy_get_type (PyObject *self, void *closure) | |
212 | { | |
213 | value_object *obj = (value_object *) self; | |
d59b6f6c | 214 | |
2c74e833 TT |
215 | if (!obj->type) |
216 | { | |
217 | obj->type = type_to_type_object (value_type (obj->value)); | |
218 | if (!obj->type) | |
219 | { | |
220 | obj->type = Py_None; | |
221 | Py_INCREF (obj->type); | |
222 | } | |
223 | } | |
224 | Py_INCREF (obj->type); | |
225 | return obj->type; | |
226 | } | |
227 | ||
be759fcf PM |
228 | /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) -> |
229 | string. Return a PyObject representing a lazy_string_object type. | |
230 | A lazy string is a pointer to a string with an optional encoding and | |
231 | length. If ENCODING is not given, encoding is set to None. If an | |
232 | ENCODING is provided the encoding parameter is set to ENCODING, but | |
233 | the string is not encoded. If LENGTH is provided then the length | |
234 | parameter is set to LENGTH, otherwise length will be set to -1 (first | |
235 | null of appropriate with). */ | |
236 | static PyObject * | |
237 | valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) | |
238 | { | |
239 | int length = -1; | |
240 | struct value *value = ((value_object *) self)->value; | |
241 | const char *user_encoding = NULL; | |
242 | static char *keywords[] = { "encoding", "length", NULL }; | |
243 | PyObject *str_obj; | |
244 | ||
245 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|si", keywords, | |
246 | &user_encoding, &length)) | |
247 | return NULL; | |
248 | ||
249 | if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR) | |
250 | value = value_ind (value); | |
251 | ||
252 | str_obj = gdbpy_create_lazy_string_object (value_address (value), length, | |
253 | user_encoding, value_type (value)); | |
254 | ||
255 | return (PyObject *) str_obj; | |
256 | } | |
257 | ||
fbb8f299 PM |
258 | /* Implementation of gdb.Value.string ([encoding] [, errors] |
259 | [, length]) -> string. Return Unicode string with value contents. | |
260 | If ENCODING is not given, the string is assumed to be encoded in | |
261 | the target's charset. If LENGTH is provided, only fetch string to | |
262 | the length provided. */ | |
263 | ||
b6cb8e7d | 264 | static PyObject * |
cc924cad | 265 | valpy_string (PyObject *self, PyObject *args, PyObject *kw) |
b6cb8e7d | 266 | { |
f92adf3c | 267 | int length = -1; |
b6cb8e7d TJB |
268 | gdb_byte *buffer; |
269 | struct value *value = ((value_object *) self)->value; | |
270 | volatile struct gdb_exception except; | |
271 | PyObject *unicode; | |
272 | const char *encoding = NULL; | |
273 | const char *errors = NULL; | |
274 | const char *user_encoding = NULL; | |
275 | const char *la_encoding = NULL; | |
96c07c5b | 276 | struct type *char_type; |
31158f0e | 277 | static char *keywords[] = { "encoding", "errors", "length", NULL }; |
b6cb8e7d | 278 | |
fbb8f299 PM |
279 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords, |
280 | &user_encoding, &errors, &length)) | |
b6cb8e7d TJB |
281 | return NULL; |
282 | ||
283 | TRY_CATCH (except, RETURN_MASK_ALL) | |
284 | { | |
96c07c5b | 285 | LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding); |
b6cb8e7d TJB |
286 | } |
287 | GDB_PY_HANDLE_EXCEPTION (except); | |
288 | ||
289 | encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding; | |
96c07c5b TT |
290 | unicode = PyUnicode_Decode (buffer, length * TYPE_LENGTH (char_type), |
291 | encoding, errors); | |
b6cb8e7d TJB |
292 | xfree (buffer); |
293 | ||
294 | return unicode; | |
295 | } | |
296 | ||
2c74e833 TT |
297 | /* Cast a value to a given type. */ |
298 | static PyObject * | |
299 | valpy_cast (PyObject *self, PyObject *args) | |
300 | { | |
301 | PyObject *type_obj; | |
302 | struct type *type; | |
303 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
304 | volatile struct gdb_exception except; | |
305 | ||
306 | if (! PyArg_ParseTuple (args, "O", &type_obj)) | |
307 | return NULL; | |
308 | ||
309 | type = type_object_to_type (type_obj); | |
310 | if (! type) | |
311 | { | |
044c0f87 PM |
312 | PyErr_SetString (PyExc_RuntimeError, |
313 | _("Argument must be a type.")); | |
2c74e833 TT |
314 | return NULL; |
315 | } | |
316 | ||
317 | TRY_CATCH (except, RETURN_MASK_ALL) | |
318 | { | |
319 | res_val = value_cast (type, ((value_object *) self)->value); | |
320 | } | |
321 | GDB_PY_HANDLE_EXCEPTION (except); | |
322 | ||
323 | return value_to_value_object (res_val); | |
324 | } | |
325 | ||
7843261b TJB |
326 | static Py_ssize_t |
327 | valpy_length (PyObject *self) | |
328 | { | |
329 | /* We don't support getting the number of elements in a struct / class. */ | |
330 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 331 | _("Invalid operation on gdb.Value.")); |
7843261b TJB |
332 | return -1; |
333 | } | |
334 | ||
335 | /* Given string name of an element inside structure, return its value | |
336 | object. */ | |
337 | static PyObject * | |
338 | valpy_getitem (PyObject *self, PyObject *key) | |
339 | { | |
340 | value_object *self_value = (value_object *) self; | |
08c637de | 341 | char *field = NULL; |
570e2b1a | 342 | struct value *res_val = NULL; |
7843261b TJB |
343 | volatile struct gdb_exception except; |
344 | ||
08c637de TJB |
345 | if (gdbpy_is_string (key)) |
346 | { | |
347 | field = python_string_to_host_string (key); | |
348 | if (field == NULL) | |
349 | return NULL; | |
350 | } | |
7843261b TJB |
351 | |
352 | TRY_CATCH (except, RETURN_MASK_ALL) | |
353 | { | |
3c0ed299 | 354 | struct value *tmp = self_value->value; |
08c637de TJB |
355 | |
356 | if (field) | |
357 | res_val = value_struct_elt (&tmp, NULL, field, 0, NULL); | |
358 | else | |
359 | { | |
360 | /* Assume we are attempting an array access, and let the | |
361 | value code throw an exception if the index has an invalid | |
362 | type. */ | |
363 | struct value *idx = convert_value_from_python (key); | |
d59b6f6c | 364 | |
570e2b1a | 365 | if (idx != NULL) |
2e4d963f PM |
366 | { |
367 | /* Check the value's type is something that can be accessed via | |
368 | a subscript. */ | |
369 | struct type *type; | |
d59b6f6c | 370 | |
2e4d963f PM |
371 | tmp = coerce_ref (tmp); |
372 | type = check_typedef (value_type (tmp)); | |
373 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY | |
374 | && TYPE_CODE (type) != TYPE_CODE_PTR) | |
044c0f87 | 375 | error( _("Cannot subscript requested type.")); |
2e4d963f PM |
376 | else |
377 | res_val = value_subscript (tmp, value_as_long (idx)); | |
378 | } | |
08c637de | 379 | } |
7843261b | 380 | } |
570e2b1a | 381 | |
06878dd2 | 382 | xfree (field); |
7843261b TJB |
383 | GDB_PY_HANDLE_EXCEPTION (except); |
384 | ||
06878dd2 | 385 | return res_val ? value_to_value_object (res_val) : NULL; |
7843261b TJB |
386 | } |
387 | ||
388 | static int | |
389 | valpy_setitem (PyObject *self, PyObject *key, PyObject *value) | |
390 | { | |
391 | PyErr_Format (PyExc_NotImplementedError, | |
392 | _("Setting of struct elements is not currently supported.")); | |
393 | return -1; | |
394 | } | |
395 | ||
396 | /* Called by the Python interpreter to obtain string representation | |
397 | of the object. */ | |
398 | static PyObject * | |
399 | valpy_str (PyObject *self) | |
400 | { | |
401 | char *s = NULL; | |
7843261b TJB |
402 | struct ui_file *stb; |
403 | struct cleanup *old_chain; | |
404 | PyObject *result; | |
79a45b7d | 405 | struct value_print_options opts; |
7843261b TJB |
406 | volatile struct gdb_exception except; |
407 | ||
79a45b7d TT |
408 | get_user_print_options (&opts); |
409 | opts.deref_ref = 0; | |
410 | ||
7843261b TJB |
411 | stb = mem_fileopen (); |
412 | old_chain = make_cleanup_ui_file_delete (stb); | |
413 | ||
414 | TRY_CATCH (except, RETURN_MASK_ALL) | |
415 | { | |
79a45b7d | 416 | common_val_print (((value_object *) self)->value, stb, 0, |
d452c4bc | 417 | &opts, python_language); |
759ef836 | 418 | s = ui_file_xstrdup (stb, NULL); |
7843261b TJB |
419 | } |
420 | GDB_PY_HANDLE_EXCEPTION (except); | |
421 | ||
422 | do_cleanups (old_chain); | |
423 | ||
424 | result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL); | |
425 | xfree (s); | |
426 | ||
427 | return result; | |
428 | } | |
429 | ||
def2b000 TJB |
430 | /* Implements gdb.Value.is_optimized_out. */ |
431 | static PyObject * | |
432 | valpy_get_is_optimized_out (PyObject *self, void *closure) | |
433 | { | |
434 | struct value *value = ((value_object *) self)->value; | |
435 | ||
436 | if (value_optimized_out (value)) | |
437 | Py_RETURN_TRUE; | |
438 | ||
439 | Py_RETURN_FALSE; | |
440 | } | |
441 | ||
88d4aea7 PM |
442 | /* Calculate and return the address of the PyObject as the value of |
443 | the builtin __hash__ call. */ | |
444 | static long | |
445 | valpy_hash (PyObject *self) | |
446 | { | |
447 | return (long) (intptr_t) self; | |
448 | } | |
449 | ||
7843261b TJB |
450 | enum valpy_opcode |
451 | { | |
452 | VALPY_ADD, | |
453 | VALPY_SUB, | |
454 | VALPY_MUL, | |
455 | VALPY_DIV, | |
456 | VALPY_REM, | |
08c637de TJB |
457 | VALPY_POW, |
458 | VALPY_LSH, | |
459 | VALPY_RSH, | |
460 | VALPY_BITAND, | |
461 | VALPY_BITOR, | |
462 | VALPY_BITXOR | |
7843261b TJB |
463 | }; |
464 | ||
465 | /* If TYPE is a reference, return the target; otherwise return TYPE. */ | |
466 | #define STRIP_REFERENCE(TYPE) \ | |
467 | ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE)) | |
468 | ||
469 | /* Returns a value object which is the result of applying the operation | |
470 | specified by OPCODE to the given arguments. */ | |
471 | static PyObject * | |
472 | valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) | |
473 | { | |
474 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
475 | volatile struct gdb_exception except; | |
476 | ||
477 | TRY_CATCH (except, RETURN_MASK_ALL) | |
478 | { | |
479 | struct value *arg1, *arg2; | |
480 | ||
481 | /* If the gdb.Value object is the second operand, then it will be passed | |
482 | to us as the OTHER argument, and SELF will be an entirely different | |
483 | kind of object, altogether. Because of this, we can't assume self is | |
484 | a gdb.Value object and need to convert it from python as well. */ | |
485 | arg1 = convert_value_from_python (self); | |
08c637de | 486 | if (arg1 == NULL) |
cc924cad | 487 | break; |
08c637de | 488 | |
7843261b | 489 | arg2 = convert_value_from_python (other); |
08c637de | 490 | if (arg2 == NULL) |
cc924cad | 491 | break; |
7843261b TJB |
492 | |
493 | switch (opcode) | |
494 | { | |
495 | case VALPY_ADD: | |
496 | { | |
497 | struct type *ltype = value_type (arg1); | |
498 | struct type *rtype = value_type (arg2); | |
499 | ||
500 | CHECK_TYPEDEF (ltype); | |
501 | ltype = STRIP_REFERENCE (ltype); | |
502 | CHECK_TYPEDEF (rtype); | |
503 | rtype = STRIP_REFERENCE (rtype); | |
504 | ||
2497b498 UW |
505 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR |
506 | && is_integral_type (rtype)) | |
507 | res_val = value_ptradd (arg1, value_as_long (arg2)); | |
508 | else if (TYPE_CODE (rtype) == TYPE_CODE_PTR | |
509 | && is_integral_type (ltype)) | |
510 | res_val = value_ptradd (arg2, value_as_long (arg1)); | |
7843261b TJB |
511 | else |
512 | res_val = value_binop (arg1, arg2, BINOP_ADD); | |
513 | } | |
514 | break; | |
515 | case VALPY_SUB: | |
516 | { | |
517 | struct type *ltype = value_type (arg1); | |
518 | struct type *rtype = value_type (arg2); | |
519 | ||
520 | CHECK_TYPEDEF (ltype); | |
521 | ltype = STRIP_REFERENCE (ltype); | |
522 | CHECK_TYPEDEF (rtype); | |
523 | rtype = STRIP_REFERENCE (rtype); | |
524 | ||
2497b498 UW |
525 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR |
526 | && TYPE_CODE (rtype) == TYPE_CODE_PTR) | |
527 | /* A ptrdiff_t for the target would be preferable here. */ | |
528 | res_val = value_from_longest (builtin_type_pyint, | |
529 | value_ptrdiff (arg1, arg2)); | |
530 | else if (TYPE_CODE (ltype) == TYPE_CODE_PTR | |
531 | && is_integral_type (rtype)) | |
532 | res_val = value_ptradd (arg1, - value_as_long (arg2)); | |
7843261b TJB |
533 | else |
534 | res_val = value_binop (arg1, arg2, BINOP_SUB); | |
535 | } | |
536 | break; | |
537 | case VALPY_MUL: | |
538 | res_val = value_binop (arg1, arg2, BINOP_MUL); | |
539 | break; | |
540 | case VALPY_DIV: | |
541 | res_val = value_binop (arg1, arg2, BINOP_DIV); | |
542 | break; | |
543 | case VALPY_REM: | |
544 | res_val = value_binop (arg1, arg2, BINOP_REM); | |
545 | break; | |
546 | case VALPY_POW: | |
547 | res_val = value_binop (arg1, arg2, BINOP_EXP); | |
548 | break; | |
08c637de TJB |
549 | case VALPY_LSH: |
550 | res_val = value_binop (arg1, arg2, BINOP_LSH); | |
551 | break; | |
552 | case VALPY_RSH: | |
553 | res_val = value_binop (arg1, arg2, BINOP_RSH); | |
554 | break; | |
555 | case VALPY_BITAND: | |
556 | res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND); | |
557 | break; | |
558 | case VALPY_BITOR: | |
559 | res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR); | |
560 | break; | |
561 | case VALPY_BITXOR: | |
562 | res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR); | |
563 | break; | |
7843261b TJB |
564 | } |
565 | } | |
566 | GDB_PY_HANDLE_EXCEPTION (except); | |
567 | ||
cc924cad | 568 | return res_val ? value_to_value_object (res_val) : NULL; |
7843261b TJB |
569 | } |
570 | ||
571 | static PyObject * | |
572 | valpy_add (PyObject *self, PyObject *other) | |
573 | { | |
574 | return valpy_binop (VALPY_ADD, self, other); | |
575 | } | |
576 | ||
577 | static PyObject * | |
578 | valpy_subtract (PyObject *self, PyObject *other) | |
579 | { | |
580 | return valpy_binop (VALPY_SUB, self, other); | |
581 | } | |
582 | ||
583 | static PyObject * | |
584 | valpy_multiply (PyObject *self, PyObject *other) | |
585 | { | |
586 | return valpy_binop (VALPY_MUL, self, other); | |
587 | } | |
588 | ||
589 | static PyObject * | |
590 | valpy_divide (PyObject *self, PyObject *other) | |
591 | { | |
592 | return valpy_binop (VALPY_DIV, self, other); | |
593 | } | |
594 | ||
595 | static PyObject * | |
596 | valpy_remainder (PyObject *self, PyObject *other) | |
597 | { | |
598 | return valpy_binop (VALPY_REM, self, other); | |
599 | } | |
600 | ||
601 | static PyObject * | |
602 | valpy_power (PyObject *self, PyObject *other, PyObject *unused) | |
603 | { | |
604 | /* We don't support the ternary form of pow. I don't know how to express | |
605 | that, so let's just throw NotImplementedError to at least do something | |
606 | about it. */ | |
607 | if (unused != Py_None) | |
608 | { | |
609 | PyErr_SetString (PyExc_NotImplementedError, | |
610 | "Invalid operation on gdb.Value."); | |
611 | return NULL; | |
612 | } | |
613 | ||
614 | return valpy_binop (VALPY_POW, self, other); | |
615 | } | |
616 | ||
617 | static PyObject * | |
618 | valpy_negative (PyObject *self) | |
619 | { | |
620 | struct value *val = NULL; | |
621 | volatile struct gdb_exception except; | |
622 | ||
623 | TRY_CATCH (except, RETURN_MASK_ALL) | |
624 | { | |
625 | val = value_neg (((value_object *) self)->value); | |
626 | } | |
627 | GDB_PY_HANDLE_EXCEPTION (except); | |
628 | ||
629 | return value_to_value_object (val); | |
630 | } | |
631 | ||
632 | static PyObject * | |
633 | valpy_positive (PyObject *self) | |
634 | { | |
4e7a5ef5 | 635 | return value_to_value_object (((value_object *) self)->value); |
7843261b TJB |
636 | } |
637 | ||
638 | static PyObject * | |
639 | valpy_absolute (PyObject *self) | |
640 | { | |
22601c15 | 641 | struct value *value = ((value_object *) self)->value; |
d59b6f6c | 642 | |
22601c15 | 643 | if (value_less (value, value_zero (value_type (value), not_lval))) |
7843261b TJB |
644 | return valpy_negative (self); |
645 | else | |
646 | return valpy_positive (self); | |
647 | } | |
648 | ||
649 | /* Implements boolean evaluation of gdb.Value. */ | |
650 | static int | |
651 | valpy_nonzero (PyObject *self) | |
652 | { | |
653 | value_object *self_value = (value_object *) self; | |
654 | struct type *type; | |
655 | ||
656 | type = check_typedef (value_type (self_value->value)); | |
657 | ||
658 | if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR) | |
659 | return !!value_as_long (self_value->value); | |
660 | else if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
661 | return value_as_double (self_value->value) != 0; | |
662 | else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
663 | return !decimal_is_zero (value_contents (self_value->value), | |
e17a4113 UW |
664 | TYPE_LENGTH (type), |
665 | gdbarch_byte_order (get_type_arch (type))); | |
7843261b TJB |
666 | else |
667 | { | |
668 | PyErr_SetString (PyExc_TypeError, _("Attempted truth testing on invalid " | |
669 | "gdb.Value type.")); | |
670 | return 0; | |
671 | } | |
672 | } | |
673 | ||
08c637de | 674 | /* Implements ~ for value objects. */ |
7843261b | 675 | static PyObject * |
08c637de | 676 | valpy_invert (PyObject *self) |
7843261b | 677 | { |
08c637de | 678 | struct value *val = NULL; |
7843261b TJB |
679 | volatile struct gdb_exception except; |
680 | ||
08c637de | 681 | TRY_CATCH (except, RETURN_MASK_ALL) |
7843261b | 682 | { |
08c637de TJB |
683 | val = value_complement (((value_object *) self)->value); |
684 | } | |
685 | GDB_PY_HANDLE_EXCEPTION (except); | |
7843261b | 686 | |
08c637de TJB |
687 | return value_to_value_object (val); |
688 | } | |
7843261b | 689 | |
08c637de TJB |
690 | /* Implements left shift for value objects. */ |
691 | static PyObject * | |
692 | valpy_lsh (PyObject *self, PyObject *other) | |
693 | { | |
694 | return valpy_binop (VALPY_LSH, self, other); | |
695 | } | |
7843261b | 696 | |
08c637de TJB |
697 | /* Implements right shift for value objects. */ |
698 | static PyObject * | |
699 | valpy_rsh (PyObject *self, PyObject *other) | |
700 | { | |
701 | return valpy_binop (VALPY_RSH, self, other); | |
702 | } | |
7843261b | 703 | |
08c637de TJB |
704 | /* Implements bitwise and for value objects. */ |
705 | static PyObject * | |
706 | valpy_and (PyObject *self, PyObject *other) | |
707 | { | |
708 | return valpy_binop (VALPY_BITAND, self, other); | |
709 | } | |
7843261b | 710 | |
08c637de TJB |
711 | /* Implements bitwise or for value objects. */ |
712 | static PyObject * | |
713 | valpy_or (PyObject *self, PyObject *other) | |
714 | { | |
715 | return valpy_binop (VALPY_BITOR, self, other); | |
716 | } | |
717 | ||
718 | /* Implements bitwise xor for value objects. */ | |
719 | static PyObject * | |
720 | valpy_xor (PyObject *self, PyObject *other) | |
721 | { | |
722 | return valpy_binop (VALPY_BITXOR, self, other); | |
723 | } | |
724 | ||
725 | /* Implements comparison operations for value objects. */ | |
726 | static PyObject * | |
727 | valpy_richcompare (PyObject *self, PyObject *other, int op) | |
728 | { | |
729 | int result = 0; | |
730 | struct value *value_other; | |
731 | volatile struct gdb_exception except; | |
732 | ||
733 | if (other == Py_None) | |
7843261b TJB |
734 | /* Comparing with None is special. From what I can tell, in Python |
735 | None is smaller than anything else. */ | |
736 | switch (op) { | |
737 | case Py_LT: | |
738 | case Py_LE: | |
739 | case Py_EQ: | |
740 | Py_RETURN_FALSE; | |
741 | case Py_NE: | |
742 | case Py_GT: | |
743 | case Py_GE: | |
744 | Py_RETURN_TRUE; | |
745 | default: | |
746 | /* Can't happen. */ | |
747 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 748 | _("Invalid operation on gdb.Value.")); |
7843261b TJB |
749 | return NULL; |
750 | } | |
7843261b TJB |
751 | |
752 | TRY_CATCH (except, RETURN_MASK_ALL) | |
753 | { | |
08c637de TJB |
754 | value_other = convert_value_from_python (other); |
755 | if (value_other == NULL) | |
f02779d8 TT |
756 | { |
757 | result = -1; | |
758 | break; | |
759 | } | |
08c637de | 760 | |
7843261b TJB |
761 | switch (op) { |
762 | case Py_LT: | |
763 | result = value_less (((value_object *) self)->value, value_other); | |
764 | break; | |
765 | case Py_LE: | |
766 | result = value_less (((value_object *) self)->value, value_other) | |
767 | || value_equal (((value_object *) self)->value, value_other); | |
768 | break; | |
769 | case Py_EQ: | |
770 | result = value_equal (((value_object *) self)->value, value_other); | |
771 | break; | |
772 | case Py_NE: | |
773 | result = !value_equal (((value_object *) self)->value, value_other); | |
774 | break; | |
775 | case Py_GT: | |
776 | result = value_less (value_other, ((value_object *) self)->value); | |
777 | break; | |
778 | case Py_GE: | |
779 | result = value_less (value_other, ((value_object *) self)->value) | |
780 | || value_equal (((value_object *) self)->value, value_other); | |
781 | break; | |
782 | default: | |
783 | /* Can't happen. */ | |
784 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 785 | _("Invalid operation on gdb.Value.")); |
f02779d8 TT |
786 | result = -1; |
787 | break; | |
7843261b TJB |
788 | } |
789 | } | |
790 | GDB_PY_HANDLE_EXCEPTION (except); | |
791 | ||
f02779d8 TT |
792 | /* In this case, the Python exception has already been set. */ |
793 | if (result < 0) | |
794 | return NULL; | |
795 | ||
7843261b TJB |
796 | if (result == 1) |
797 | Py_RETURN_TRUE; | |
798 | ||
799 | Py_RETURN_FALSE; | |
800 | } | |
801 | ||
08c637de TJB |
802 | /* Helper function to determine if a type is "int-like". */ |
803 | static int | |
804 | is_intlike (struct type *type, int ptr_ok) | |
805 | { | |
806 | CHECK_TYPEDEF (type); | |
807 | return (TYPE_CODE (type) == TYPE_CODE_INT | |
808 | || TYPE_CODE (type) == TYPE_CODE_ENUM | |
809 | || TYPE_CODE (type) == TYPE_CODE_BOOL | |
810 | || TYPE_CODE (type) == TYPE_CODE_CHAR | |
811 | || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR)); | |
812 | } | |
813 | ||
814 | /* Implements conversion to int. */ | |
815 | static PyObject * | |
816 | valpy_int (PyObject *self) | |
817 | { | |
818 | struct value *value = ((value_object *) self)->value; | |
819 | struct type *type = value_type (value); | |
820 | LONGEST l = 0; | |
821 | volatile struct gdb_exception except; | |
822 | ||
823 | CHECK_TYPEDEF (type); | |
824 | if (!is_intlike (type, 0)) | |
825 | { | |
044c0f87 PM |
826 | PyErr_SetString (PyExc_RuntimeError, |
827 | _("Cannot convert value to int.")); | |
08c637de TJB |
828 | return NULL; |
829 | } | |
830 | ||
831 | TRY_CATCH (except, RETURN_MASK_ALL) | |
832 | { | |
833 | l = value_as_long (value); | |
834 | } | |
835 | GDB_PY_HANDLE_EXCEPTION (except); | |
836 | ||
89fa5381 TT |
837 | #ifdef HAVE_LONG_LONG /* Defined by Python. */ |
838 | /* If we have 'long long', and the value overflows a 'long', use a | |
839 | Python Long; otherwise use a Python Int. */ | |
840 | if (sizeof (l) > sizeof (long) && (l > PyInt_GetMax () | |
841 | || l < (- (LONGEST) PyInt_GetMax ()) - 1)) | |
842 | return PyLong_FromLongLong (l); | |
843 | #endif | |
08c637de TJB |
844 | return PyInt_FromLong (l); |
845 | } | |
846 | ||
847 | /* Implements conversion to long. */ | |
848 | static PyObject * | |
849 | valpy_long (PyObject *self) | |
850 | { | |
851 | struct value *value = ((value_object *) self)->value; | |
852 | struct type *type = value_type (value); | |
853 | LONGEST l = 0; | |
854 | volatile struct gdb_exception except; | |
855 | ||
856 | if (!is_intlike (type, 1)) | |
857 | { | |
044c0f87 PM |
858 | PyErr_SetString (PyExc_RuntimeError, |
859 | _("Cannot convert value to long.")); | |
08c637de TJB |
860 | return NULL; |
861 | } | |
862 | ||
863 | TRY_CATCH (except, RETURN_MASK_ALL) | |
864 | { | |
865 | l = value_as_long (value); | |
866 | } | |
867 | GDB_PY_HANDLE_EXCEPTION (except); | |
868 | ||
89fa5381 TT |
869 | #ifdef HAVE_LONG_LONG /* Defined by Python. */ |
870 | return PyLong_FromLongLong (l); | |
871 | #else | |
08c637de | 872 | return PyLong_FromLong (l); |
89fa5381 | 873 | #endif |
08c637de TJB |
874 | } |
875 | ||
876 | /* Implements conversion to float. */ | |
877 | static PyObject * | |
878 | valpy_float (PyObject *self) | |
879 | { | |
880 | struct value *value = ((value_object *) self)->value; | |
881 | struct type *type = value_type (value); | |
882 | double d = 0; | |
883 | volatile struct gdb_exception except; | |
884 | ||
885 | CHECK_TYPEDEF (type); | |
886 | if (TYPE_CODE (type) != TYPE_CODE_FLT) | |
887 | { | |
044c0f87 PM |
888 | PyErr_SetString (PyExc_RuntimeError, |
889 | _("Cannot convert value to float.")); | |
08c637de TJB |
890 | return NULL; |
891 | } | |
892 | ||
893 | TRY_CATCH (except, RETURN_MASK_ALL) | |
894 | { | |
895 | d = value_as_double (value); | |
896 | } | |
897 | GDB_PY_HANDLE_EXCEPTION (except); | |
898 | ||
899 | return PyFloat_FromDouble (d); | |
900 | } | |
901 | ||
7843261b TJB |
902 | /* Returns an object for a value which is released from the all_values chain, |
903 | so its lifetime is not bound to the execution of a command. */ | |
904 | PyObject * | |
905 | value_to_value_object (struct value *val) | |
906 | { | |
907 | value_object *val_obj; | |
908 | ||
909 | val_obj = PyObject_New (value_object, &value_object_type); | |
910 | if (val_obj != NULL) | |
911 | { | |
912 | val_obj->value = val; | |
4e7a5ef5 | 913 | value_incref (val); |
c0c6f777 | 914 | val_obj->address = NULL; |
2c74e833 | 915 | val_obj->type = NULL; |
4e7a5ef5 | 916 | note_value (val_obj); |
7843261b TJB |
917 | } |
918 | ||
919 | return (PyObject *) val_obj; | |
920 | } | |
921 | ||
4e7a5ef5 TT |
922 | /* Returns a borrowed reference to the struct value corresponding to |
923 | the given value object. */ | |
a6bac58e TT |
924 | struct value * |
925 | value_object_to_value (PyObject *self) | |
926 | { | |
927 | value_object *real; | |
d59b6f6c | 928 | |
a6bac58e TT |
929 | if (! PyObject_TypeCheck (self, &value_object_type)) |
930 | return NULL; | |
931 | real = (value_object *) self; | |
932 | return real->value; | |
933 | } | |
934 | ||
7843261b | 935 | /* Try to convert a Python value to a gdb value. If the value cannot |
4e7a5ef5 TT |
936 | be converted, set a Python exception and return NULL. Returns a |
937 | reference to a new value on the all_values chain. */ | |
7843261b TJB |
938 | |
939 | struct value * | |
940 | convert_value_from_python (PyObject *obj) | |
941 | { | |
942 | struct value *value = NULL; /* -Wall */ | |
7843261b | 943 | struct cleanup *old; |
08c637de TJB |
944 | volatile struct gdb_exception except; |
945 | int cmp; | |
7843261b | 946 | |
08c637de | 947 | gdb_assert (obj != NULL); |
7843261b | 948 | |
08c637de | 949 | TRY_CATCH (except, RETURN_MASK_ALL) |
7843261b | 950 | { |
08c637de TJB |
951 | if (PyBool_Check (obj)) |
952 | { | |
953 | cmp = PyObject_IsTrue (obj); | |
954 | if (cmp >= 0) | |
955 | value = value_from_longest (builtin_type_pybool, cmp); | |
956 | } | |
957 | else if (PyInt_Check (obj)) | |
958 | { | |
959 | long l = PyInt_AsLong (obj); | |
7843261b | 960 | |
08c637de TJB |
961 | if (! PyErr_Occurred ()) |
962 | value = value_from_longest (builtin_type_pyint, l); | |
963 | } | |
964 | else if (PyLong_Check (obj)) | |
965 | { | |
966 | LONGEST l = PyLong_AsLongLong (obj); | |
7843261b | 967 | |
595939de PM |
968 | if (PyErr_Occurred ()) |
969 | { | |
970 | /* If the error was an overflow, we can try converting to | |
971 | ULONGEST instead. */ | |
972 | if (PyErr_ExceptionMatches (PyExc_OverflowError)) | |
973 | { | |
974 | PyObject *etype, *evalue, *etraceback, *zero; | |
975 | ||
976 | PyErr_Fetch (&etype, &evalue, &etraceback); | |
977 | zero = PyInt_FromLong (0); | |
978 | ||
979 | /* Check whether obj is positive. */ | |
980 | if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0) | |
981 | { | |
982 | ULONGEST ul; | |
983 | ||
984 | ul = PyLong_AsUnsignedLongLong (obj); | |
985 | if (! PyErr_Occurred ()) | |
986 | value = value_from_ulongest (builtin_type_upylong, ul); | |
987 | } | |
988 | else | |
989 | /* There's nothing we can do. */ | |
990 | PyErr_Restore (etype, evalue, etraceback); | |
991 | ||
992 | Py_DECREF (zero); | |
993 | } | |
994 | } | |
995 | else | |
08c637de TJB |
996 | value = value_from_longest (builtin_type_pylong, l); |
997 | } | |
998 | else if (PyFloat_Check (obj)) | |
999 | { | |
1000 | double d = PyFloat_AsDouble (obj); | |
7843261b | 1001 | |
08c637de TJB |
1002 | if (! PyErr_Occurred ()) |
1003 | value = value_from_double (builtin_type_pyfloat, d); | |
1004 | } | |
1005 | else if (gdbpy_is_string (obj)) | |
1006 | { | |
1007 | char *s; | |
1008 | ||
1009 | s = python_string_to_target_string (obj); | |
1010 | if (s != NULL) | |
1011 | { | |
1012 | old = make_cleanup (xfree, s); | |
3b7538c0 | 1013 | value = value_cstring (s, strlen (s), builtin_type_pychar); |
08c637de TJB |
1014 | do_cleanups (old); |
1015 | } | |
1016 | } | |
1017 | else if (PyObject_TypeCheck (obj, &value_object_type)) | |
cc924cad | 1018 | value = value_copy (((value_object *) obj)->value); |
be759fcf PM |
1019 | else if (gdbpy_is_lazy_string (obj)) |
1020 | { | |
1021 | PyObject *result; | |
1022 | PyObject *function = PyString_FromString ("value"); | |
d59b6f6c | 1023 | |
be759fcf PM |
1024 | result = PyObject_CallMethodObjArgs (obj, function, NULL); |
1025 | value = value_copy (((value_object *) result)->value); | |
1026 | } | |
08c637de | 1027 | else |
044c0f87 | 1028 | PyErr_Format (PyExc_TypeError, _("Could not convert Python object: %s."), |
08c637de TJB |
1029 | PyString_AsString (PyObject_Str (obj))); |
1030 | } | |
1031 | if (except.reason < 0) | |
1032 | { | |
1033 | PyErr_Format (except.reason == RETURN_QUIT | |
1034 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, | |
1035 | "%s", except.message); | |
1036 | return NULL; | |
1037 | } | |
7843261b TJB |
1038 | |
1039 | return value; | |
1040 | } | |
1041 | ||
1042 | /* Returns value object in the ARGth position in GDB's history. */ | |
1043 | PyObject * | |
08c637de | 1044 | gdbpy_history (PyObject *self, PyObject *args) |
7843261b TJB |
1045 | { |
1046 | int i; | |
1047 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
1048 | volatile struct gdb_exception except; | |
1049 | ||
1050 | if (!PyArg_ParseTuple (args, "i", &i)) | |
1051 | return NULL; | |
1052 | ||
1053 | TRY_CATCH (except, RETURN_MASK_ALL) | |
1054 | { | |
1055 | res_val = access_value_history (i); | |
1056 | } | |
1057 | GDB_PY_HANDLE_EXCEPTION (except); | |
1058 | ||
1059 | return value_to_value_object (res_val); | |
1060 | } | |
1061 | ||
595939de PM |
1062 | /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */ |
1063 | ||
1064 | int | |
1065 | gdbpy_is_value_object (PyObject *obj) | |
1066 | { | |
1067 | return PyObject_TypeCheck (obj, &value_object_type); | |
1068 | } | |
1069 | ||
7843261b TJB |
1070 | void |
1071 | gdbpy_initialize_values (void) | |
1072 | { | |
7843261b TJB |
1073 | if (PyType_Ready (&value_object_type) < 0) |
1074 | return; | |
1075 | ||
1076 | Py_INCREF (&value_object_type); | |
1077 | PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type); | |
1078 | ||
1079 | values_in_python = NULL; | |
1080 | } | |
1081 | ||
2c74e833 TT |
1082 | \f |
1083 | ||
def2b000 | 1084 | static PyGetSetDef value_object_getset[] = { |
c0c6f777 TJB |
1085 | { "address", valpy_get_address, NULL, "The address of the value.", |
1086 | NULL }, | |
def2b000 TJB |
1087 | { "is_optimized_out", valpy_get_is_optimized_out, NULL, |
1088 | "Boolean telling whether the value is optimized out (i.e., not available).", | |
1089 | NULL }, | |
2c74e833 | 1090 | { "type", valpy_get_type, NULL, "Type of the value.", NULL }, |
def2b000 TJB |
1091 | {NULL} /* Sentinel */ |
1092 | }; | |
1093 | ||
f9176c46 | 1094 | static PyMethodDef value_object_methods[] = { |
2c74e833 | 1095 | { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." }, |
f9176c46 | 1096 | { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, |
be759fcf PM |
1097 | { "lazy_string", (PyCFunction) valpy_lazy_string, METH_VARARGS | METH_KEYWORDS, |
1098 | "lazy_string ([encoding] [, length]) -> lazy_string\n\ | |
1099 | Return a lazy string representation of the value." }, | |
cc924cad | 1100 | { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS, |
fbb8f299 | 1101 | "string ([encoding] [, errors] [, length]) -> string\n\ |
cc924cad | 1102 | Return Unicode string representation of the value." }, |
f9176c46 PA |
1103 | {NULL} /* Sentinel */ |
1104 | }; | |
1105 | ||
1106 | static PyNumberMethods value_object_as_number = { | |
1107 | valpy_add, | |
1108 | valpy_subtract, | |
1109 | valpy_multiply, | |
1110 | valpy_divide, | |
1111 | valpy_remainder, | |
1112 | NULL, /* nb_divmod */ | |
1113 | valpy_power, /* nb_power */ | |
1114 | valpy_negative, /* nb_negative */ | |
1115 | valpy_positive, /* nb_positive */ | |
1116 | valpy_absolute, /* nb_absolute */ | |
08c637de TJB |
1117 | valpy_nonzero, /* nb_nonzero */ |
1118 | valpy_invert, /* nb_invert */ | |
1119 | valpy_lsh, /* nb_lshift */ | |
1120 | valpy_rsh, /* nb_rshift */ | |
1121 | valpy_and, /* nb_and */ | |
1122 | valpy_xor, /* nb_xor */ | |
1123 | valpy_or, /* nb_or */ | |
1124 | NULL, /* nb_coerce */ | |
1125 | valpy_int, /* nb_int */ | |
1126 | valpy_long, /* nb_long */ | |
1127 | valpy_float, /* nb_float */ | |
1128 | NULL, /* nb_oct */ | |
1129 | NULL /* nb_hex */ | |
f9176c46 PA |
1130 | }; |
1131 | ||
1132 | static PyMappingMethods value_object_as_mapping = { | |
1133 | valpy_length, | |
1134 | valpy_getitem, | |
1135 | valpy_setitem | |
1136 | }; | |
1137 | ||
1138 | PyTypeObject value_object_type = { | |
1139 | PyObject_HEAD_INIT (NULL) | |
1140 | 0, /*ob_size*/ | |
1141 | "gdb.Value", /*tp_name*/ | |
1142 | sizeof (value_object), /*tp_basicsize*/ | |
1143 | 0, /*tp_itemsize*/ | |
1144 | valpy_dealloc, /*tp_dealloc*/ | |
1145 | 0, /*tp_print*/ | |
1146 | 0, /*tp_getattr*/ | |
1147 | 0, /*tp_setattr*/ | |
1148 | 0, /*tp_compare*/ | |
1149 | 0, /*tp_repr*/ | |
1150 | &value_object_as_number, /*tp_as_number*/ | |
1151 | 0, /*tp_as_sequence*/ | |
1152 | &value_object_as_mapping, /*tp_as_mapping*/ | |
88d4aea7 | 1153 | valpy_hash, /*tp_hash*/ |
f9176c46 PA |
1154 | 0, /*tp_call*/ |
1155 | valpy_str, /*tp_str*/ | |
1156 | 0, /*tp_getattro*/ | |
1157 | 0, /*tp_setattro*/ | |
1158 | 0, /*tp_as_buffer*/ | |
1159 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ | |
1160 | "GDB value object", /* tp_doc */ | |
1161 | 0, /* tp_traverse */ | |
1162 | 0, /* tp_clear */ | |
1163 | valpy_richcompare, /* tp_richcompare */ | |
1164 | 0, /* tp_weaklistoffset */ | |
1165 | 0, /* tp_iter */ | |
1166 | 0, /* tp_iternext */ | |
08c637de TJB |
1167 | value_object_methods, /* tp_methods */ |
1168 | 0, /* tp_members */ | |
def2b000 | 1169 | value_object_getset, /* tp_getset */ |
08c637de TJB |
1170 | 0, /* tp_base */ |
1171 | 0, /* tp_dict */ | |
1172 | 0, /* tp_descr_get */ | |
1173 | 0, /* tp_descr_set */ | |
1174 | 0, /* tp_dictoffset */ | |
1175 | 0, /* tp_init */ | |
1176 | 0, /* tp_alloc */ | |
1177 | valpy_new /* tp_new */ | |
f9176c46 PA |
1178 | }; |
1179 | ||
4e7a5ef5 TT |
1180 | #else |
1181 | ||
1182 | void | |
1183 | preserve_python_values (struct objfile *objfile, htab_t copied_types) | |
1184 | { | |
1185 | /* Nothing. */ | |
1186 | } | |
1187 | ||
7843261b | 1188 | #endif /* HAVE_PYTHON */ |