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