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