Commit | Line | Data |
---|---|---|
7843261b TJB |
1 | /* Python interface to values. |
2 | ||
61baf725 | 3 | Copyright (C) 2008-2017 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" | |
21 | #include "charset.h" | |
22 | #include "value.h" | |
7843261b TJB |
23 | #include "language.h" |
24 | #include "dfp.h" | |
79a45b7d | 25 | #include "valprint.h" |
5374244e | 26 | #include "infcall.h" |
f9ffd4bb | 27 | #include "expression.h" |
03f17ccf | 28 | #include "cp-abi.h" |
49a8461d | 29 | #include "python.h" |
7843261b | 30 | |
7843261b | 31 | #include "python-internal.h" |
53a0cca3 | 32 | #include "py-ref.h" |
7843261b TJB |
33 | |
34 | /* Even though Python scalar types directly map to host types, we use | |
b021a221 | 35 | target types here to remain consistent with the values system in |
7843261b TJB |
36 | GDB (which uses target arithmetic). */ |
37 | ||
38 | /* Python's integer type corresponds to C's long type. */ | |
d452c4bc | 39 | #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long |
7843261b TJB |
40 | |
41 | /* Python's float type corresponds to C's double type. */ | |
d452c4bc | 42 | #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double |
7843261b TJB |
43 | |
44 | /* Python's long type corresponds to C's long long type. */ | |
d452c4bc | 45 | #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long |
7843261b | 46 | |
595939de PM |
47 | /* Python's long type corresponds to C's long long type. Unsigned version. */ |
48 | #define builtin_type_upylong builtin_type \ | |
49 | (python_gdbarch)->builtin_unsigned_long_long | |
50 | ||
7843261b | 51 | #define builtin_type_pybool \ |
d452c4bc | 52 | language_bool_type (python_language, python_gdbarch) |
7843261b | 53 | |
3b7538c0 | 54 | #define builtin_type_pychar \ |
d452c4bc | 55 | language_string_char_type (python_language, python_gdbarch) |
3b7538c0 | 56 | |
4e7a5ef5 | 57 | typedef struct value_object { |
7843261b | 58 | PyObject_HEAD |
4e7a5ef5 TT |
59 | struct value_object *next; |
60 | struct value_object *prev; | |
7843261b | 61 | struct value *value; |
c0c6f777 | 62 | PyObject *address; |
2c74e833 | 63 | PyObject *type; |
03f17ccf | 64 | PyObject *dynamic_type; |
7843261b TJB |
65 | } value_object; |
66 | ||
4e7a5ef5 TT |
67 | /* List of all values which are currently exposed to Python. It is |
68 | maintained so that when an objfile is discarded, preserve_values | |
69 | can copy the values' types if needed. */ | |
70 | /* This variable is unnecessarily initialized to NULL in order to | |
71 | work around a linker bug on MacOS. */ | |
72 | static value_object *values_in_python = NULL; | |
73 | ||
7843261b TJB |
74 | /* Called by the Python interpreter when deallocating a value object. */ |
75 | static void | |
76 | valpy_dealloc (PyObject *obj) | |
77 | { | |
78 | value_object *self = (value_object *) obj; | |
79 | ||
4e7a5ef5 TT |
80 | /* Remove SELF from the global list. */ |
81 | if (self->prev) | |
82 | self->prev->next = self->next; | |
83 | else | |
84 | { | |
85 | gdb_assert (values_in_python == self); | |
86 | values_in_python = self->next; | |
87 | } | |
88 | if (self->next) | |
89 | self->next->prev = self->prev; | |
7843261b | 90 | |
77316f4c | 91 | value_free (self->value); |
c0c6f777 TJB |
92 | |
93 | if (self->address) | |
94 | /* Use braces to appease gcc warning. *sigh* */ | |
95 | { | |
96 | Py_DECREF (self->address); | |
97 | } | |
98 | ||
2c74e833 TT |
99 | if (self->type) |
100 | { | |
101 | Py_DECREF (self->type); | |
102 | } | |
103 | ||
03f17ccf TT |
104 | Py_XDECREF (self->dynamic_type); |
105 | ||
9a27f2c6 | 106 | Py_TYPE (self)->tp_free (self); |
7843261b TJB |
107 | } |
108 | ||
4e7a5ef5 TT |
109 | /* Helper to push a Value object on the global list. */ |
110 | static void | |
111 | note_value (value_object *value_obj) | |
112 | { | |
113 | value_obj->next = values_in_python; | |
114 | if (value_obj->next) | |
115 | value_obj->next->prev = value_obj; | |
116 | value_obj->prev = NULL; | |
117 | values_in_python = value_obj; | |
118 | } | |
119 | ||
8dc78533 JK |
120 | /* Called when a new gdb.Value object needs to be allocated. Returns NULL on |
121 | error, with a python exception set. */ | |
7843261b TJB |
122 | static PyObject * |
123 | valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords) | |
124 | { | |
125 | struct value *value = NULL; /* Initialize to appease gcc warning. */ | |
126 | value_object *value_obj; | |
7843261b TJB |
127 | |
128 | if (PyTuple_Size (args) != 1) | |
129 | { | |
130 | PyErr_SetString (PyExc_TypeError, _("Value object creation takes only " | |
131 | "1 argument")); | |
132 | return NULL; | |
133 | } | |
134 | ||
135 | value_obj = (value_object *) subtype->tp_alloc (subtype, 1); | |
136 | if (value_obj == NULL) | |
137 | { | |
138 | PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to " | |
139 | "create Value object.")); | |
140 | return NULL; | |
141 | } | |
142 | ||
08c637de TJB |
143 | value = convert_value_from_python (PyTuple_GetItem (args, 0)); |
144 | if (value == NULL) | |
7843261b TJB |
145 | { |
146 | subtype->tp_free (value_obj); | |
08c637de | 147 | return NULL; |
7843261b TJB |
148 | } |
149 | ||
150 | value_obj->value = value; | |
e848a8a5 | 151 | release_value_or_incref (value); |
c0c6f777 | 152 | value_obj->address = NULL; |
2c74e833 | 153 | value_obj->type = NULL; |
03f17ccf | 154 | value_obj->dynamic_type = NULL; |
4e7a5ef5 | 155 | note_value (value_obj); |
7843261b TJB |
156 | |
157 | return (PyObject *) value_obj; | |
158 | } | |
159 | ||
4e7a5ef5 TT |
160 | /* Iterate over all the Value objects, calling preserve_one_value on |
161 | each. */ | |
162 | void | |
6dddc817 DE |
163 | gdbpy_preserve_values (const struct extension_language_defn *extlang, |
164 | struct objfile *objfile, htab_t copied_types) | |
4e7a5ef5 TT |
165 | { |
166 | value_object *iter; | |
167 | ||
168 | for (iter = values_in_python; iter; iter = iter->next) | |
169 | preserve_one_value (iter->value, objfile, copied_types); | |
170 | } | |
171 | ||
7843261b TJB |
172 | /* Given a value of a pointer type, apply the C unary * operator to it. */ |
173 | static PyObject * | |
174 | valpy_dereference (PyObject *self, PyObject *args) | |
175 | { | |
888fe1e1 | 176 | PyObject *result = NULL; |
7843261b | 177 | |
492d29ea | 178 | TRY |
7843261b | 179 | { |
888fe1e1 | 180 | struct value *res_val; |
eb115069 | 181 | scoped_value_mark free_values; |
888fe1e1 | 182 | |
7843261b | 183 | res_val = value_ind (((value_object *) self)->value); |
888fe1e1 | 184 | result = value_to_value_object (res_val); |
7843261b | 185 | } |
492d29ea PA |
186 | CATCH (except, RETURN_MASK_ALL) |
187 | { | |
188 | GDB_PY_HANDLE_EXCEPTION (except); | |
189 | } | |
190 | END_CATCH | |
7843261b | 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 | { | |
7b282c5a SCR |
206 | PyObject *result = NULL; |
207 | ||
492d29ea | 208 | TRY |
7b282c5a SCR |
209 | { |
210 | struct value *self_val, *res_val; | |
eb115069 | 211 | scoped_value_mark free_values; |
7b282c5a SCR |
212 | |
213 | self_val = ((value_object *) self)->value; | |
214 | switch (TYPE_CODE (check_typedef (value_type (self_val)))) | |
215 | { | |
216 | case TYPE_CODE_PTR: | |
217 | res_val = value_ind (self_val); | |
218 | break; | |
219 | case TYPE_CODE_REF: | |
220 | res_val = coerce_ref (self_val); | |
221 | break; | |
222 | default: | |
223 | error(_("Trying to get the referenced value from a value which is " | |
224 | "neither a pointer nor a reference.")); | |
225 | } | |
226 | ||
227 | result = value_to_value_object (res_val); | |
7b282c5a | 228 | } |
492d29ea PA |
229 | CATCH (except, RETURN_MASK_ALL) |
230 | { | |
231 | GDB_PY_HANDLE_EXCEPTION (except); | |
232 | } | |
233 | END_CATCH | |
7b282c5a SCR |
234 | |
235 | return result; | |
236 | } | |
237 | ||
4c082a81 SC |
238 | /* Return a value which is a reference to the value. */ |
239 | ||
240 | static PyObject * | |
241 | valpy_reference_value (PyObject *self, PyObject *args) | |
242 | { | |
243 | PyObject *result = NULL; | |
244 | ||
245 | TRY | |
246 | { | |
247 | struct value *self_val; | |
eb115069 | 248 | scoped_value_mark free_values; |
4c082a81 SC |
249 | |
250 | self_val = ((value_object *) self)->value; | |
251 | result = value_to_value_object (value_ref (self_val)); | |
4c082a81 SC |
252 | } |
253 | CATCH (except, RETURN_MASK_ALL) | |
254 | { | |
255 | GDB_PY_HANDLE_EXCEPTION (except); | |
256 | } | |
257 | END_CATCH | |
258 | ||
259 | return result; | |
260 | } | |
261 | ||
262 | /* Return a "const" qualified version of the value. */ | |
263 | ||
264 | static PyObject * | |
265 | valpy_const_value (PyObject *self, PyObject *args) | |
266 | { | |
267 | PyObject *result = NULL; | |
268 | ||
269 | TRY | |
270 | { | |
271 | struct value *self_val, *res_val; | |
eb115069 | 272 | scoped_value_mark free_values; |
4c082a81 SC |
273 | |
274 | self_val = ((value_object *) self)->value; | |
275 | res_val = make_cv_value (1, 0, self_val); | |
276 | result = value_to_value_object (res_val); | |
4c082a81 SC |
277 | } |
278 | CATCH (except, RETURN_MASK_ALL) | |
279 | { | |
280 | GDB_PY_HANDLE_EXCEPTION (except); | |
281 | } | |
282 | END_CATCH | |
283 | ||
284 | return result; | |
285 | } | |
286 | ||
08c637de TJB |
287 | /* Return "&value". */ |
288 | static PyObject * | |
c0c6f777 | 289 | valpy_get_address (PyObject *self, void *closure) |
08c637de | 290 | { |
c0c6f777 | 291 | value_object *val_obj = (value_object *) self; |
08c637de | 292 | |
c0c6f777 | 293 | if (!val_obj->address) |
08c637de | 294 | { |
492d29ea | 295 | TRY |
c0c6f777 | 296 | { |
888fe1e1 | 297 | struct value *res_val; |
eb115069 | 298 | scoped_value_mark free_values; |
888fe1e1 | 299 | |
c0c6f777 | 300 | res_val = value_addr (val_obj->value); |
888fe1e1 | 301 | val_obj->address = value_to_value_object (res_val); |
c0c6f777 | 302 | } |
492d29ea | 303 | CATCH (except, RETURN_MASK_ALL) |
c0c6f777 TJB |
304 | { |
305 | val_obj->address = Py_None; | |
306 | Py_INCREF (Py_None); | |
307 | } | |
492d29ea | 308 | END_CATCH |
08c637de | 309 | } |
08c637de | 310 | |
3fcaed38 | 311 | Py_XINCREF (val_obj->address); |
c0c6f777 TJB |
312 | |
313 | return val_obj->address; | |
08c637de TJB |
314 | } |
315 | ||
2c74e833 TT |
316 | /* Return type of the value. */ |
317 | static PyObject * | |
318 | valpy_get_type (PyObject *self, void *closure) | |
319 | { | |
320 | value_object *obj = (value_object *) self; | |
d59b6f6c | 321 | |
2c74e833 TT |
322 | if (!obj->type) |
323 | { | |
324 | obj->type = type_to_type_object (value_type (obj->value)); | |
325 | if (!obj->type) | |
03f17ccf | 326 | return NULL; |
2c74e833 TT |
327 | } |
328 | Py_INCREF (obj->type); | |
329 | return obj->type; | |
330 | } | |
331 | ||
03f17ccf TT |
332 | /* Return dynamic type of the value. */ |
333 | ||
334 | static PyObject * | |
335 | valpy_get_dynamic_type (PyObject *self, void *closure) | |
336 | { | |
337 | value_object *obj = (value_object *) self; | |
03f17ccf TT |
338 | struct type *type = NULL; |
339 | ||
340 | if (obj->dynamic_type != NULL) | |
341 | { | |
342 | Py_INCREF (obj->dynamic_type); | |
343 | return obj->dynamic_type; | |
344 | } | |
345 | ||
492d29ea | 346 | TRY |
03f17ccf TT |
347 | { |
348 | struct value *val = obj->value; | |
eb115069 | 349 | scoped_value_mark free_values; |
03f17ccf TT |
350 | |
351 | type = value_type (val); | |
f168693b | 352 | type = check_typedef (type); |
03f17ccf TT |
353 | |
354 | if (((TYPE_CODE (type) == TYPE_CODE_PTR) | |
355 | || (TYPE_CODE (type) == TYPE_CODE_REF)) | |
4753d33b | 356 | && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT)) |
03f17ccf TT |
357 | { |
358 | struct value *target; | |
359 | int was_pointer = TYPE_CODE (type) == TYPE_CODE_PTR; | |
360 | ||
7af389b8 SC |
361 | if (was_pointer) |
362 | target = value_ind (val); | |
363 | else | |
364 | target = coerce_ref (val); | |
03f17ccf TT |
365 | type = value_rtti_type (target, NULL, NULL, NULL); |
366 | ||
367 | if (type) | |
368 | { | |
369 | if (was_pointer) | |
370 | type = lookup_pointer_type (type); | |
371 | else | |
372 | type = lookup_reference_type (type); | |
373 | } | |
374 | } | |
4753d33b | 375 | else if (TYPE_CODE (type) == TYPE_CODE_STRUCT) |
03f17ccf TT |
376 | type = value_rtti_type (val, NULL, NULL, NULL); |
377 | else | |
378 | { | |
379 | /* Re-use object's static type. */ | |
380 | type = NULL; | |
381 | } | |
382 | } | |
492d29ea PA |
383 | CATCH (except, RETURN_MASK_ALL) |
384 | { | |
385 | GDB_PY_HANDLE_EXCEPTION (except); | |
386 | } | |
387 | END_CATCH | |
03f17ccf TT |
388 | |
389 | if (type == NULL) | |
97b77b39 | 390 | obj->dynamic_type = valpy_get_type (self, NULL); |
03f17ccf TT |
391 | else |
392 | obj->dynamic_type = type_to_type_object (type); | |
393 | ||
97b77b39 | 394 | Py_XINCREF (obj->dynamic_type); |
03f17ccf TT |
395 | return obj->dynamic_type; |
396 | } | |
397 | ||
be759fcf PM |
398 | /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) -> |
399 | string. Return a PyObject representing a lazy_string_object type. | |
400 | A lazy string is a pointer to a string with an optional encoding and | |
401 | length. If ENCODING is not given, encoding is set to None. If an | |
402 | ENCODING is provided the encoding parameter is set to ENCODING, but | |
403 | the string is not encoded. If LENGTH is provided then the length | |
404 | parameter is set to LENGTH, otherwise length will be set to -1 (first | |
405 | null of appropriate with). */ | |
406 | static PyObject * | |
407 | valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) | |
408 | { | |
74aedc46 | 409 | gdb_py_longest length = -1; |
be759fcf PM |
410 | struct value *value = ((value_object *) self)->value; |
411 | const char *user_encoding = NULL; | |
412 | static char *keywords[] = { "encoding", "length", NULL }; | |
888fe1e1 | 413 | PyObject *str_obj = NULL; |
be759fcf | 414 | |
74aedc46 | 415 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords, |
be759fcf PM |
416 | &user_encoding, &length)) |
417 | return NULL; | |
418 | ||
492d29ea | 419 | TRY |
f287c1f3 | 420 | { |
eb115069 | 421 | scoped_value_mark free_values; |
888fe1e1 | 422 | |
f287c1f3 PM |
423 | if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR) |
424 | value = value_ind (value); | |
888fe1e1 TT |
425 | |
426 | str_obj = gdbpy_create_lazy_string_object (value_address (value), length, | |
427 | user_encoding, | |
428 | value_type (value)); | |
f287c1f3 | 429 | } |
492d29ea PA |
430 | CATCH (except, RETURN_MASK_ALL) |
431 | { | |
432 | GDB_PY_HANDLE_EXCEPTION (except); | |
433 | } | |
434 | END_CATCH | |
be759fcf | 435 | |
888fe1e1 | 436 | return str_obj; |
be759fcf PM |
437 | } |
438 | ||
fbb8f299 PM |
439 | /* Implementation of gdb.Value.string ([encoding] [, errors] |
440 | [, length]) -> string. Return Unicode string with value contents. | |
441 | If ENCODING is not given, the string is assumed to be encoded in | |
442 | the target's charset. If LENGTH is provided, only fetch string to | |
443 | the length provided. */ | |
444 | ||
b6cb8e7d | 445 | static PyObject * |
cc924cad | 446 | valpy_string (PyObject *self, PyObject *args, PyObject *kw) |
b6cb8e7d | 447 | { |
f92adf3c | 448 | int length = -1; |
b6cb8e7d TJB |
449 | gdb_byte *buffer; |
450 | struct value *value = ((value_object *) self)->value; | |
b6cb8e7d TJB |
451 | PyObject *unicode; |
452 | const char *encoding = NULL; | |
453 | const char *errors = NULL; | |
454 | const char *user_encoding = NULL; | |
455 | const char *la_encoding = NULL; | |
96c07c5b | 456 | struct type *char_type; |
31158f0e | 457 | static char *keywords[] = { "encoding", "errors", "length", NULL }; |
b6cb8e7d | 458 | |
fbb8f299 PM |
459 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords, |
460 | &user_encoding, &errors, &length)) | |
b6cb8e7d TJB |
461 | return NULL; |
462 | ||
492d29ea | 463 | TRY |
b6cb8e7d | 464 | { |
96c07c5b | 465 | LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding); |
b6cb8e7d | 466 | } |
492d29ea PA |
467 | CATCH (except, RETURN_MASK_ALL) |
468 | { | |
469 | GDB_PY_HANDLE_EXCEPTION (except); | |
470 | } | |
471 | END_CATCH | |
b6cb8e7d TJB |
472 | |
473 | encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding; | |
89f6d837 PA |
474 | unicode = PyUnicode_Decode ((const char *) buffer, |
475 | length * TYPE_LENGTH (char_type), | |
96c07c5b | 476 | encoding, errors); |
b6cb8e7d TJB |
477 | xfree (buffer); |
478 | ||
479 | return unicode; | |
480 | } | |
481 | ||
f9ffd4bb TT |
482 | /* A helper function that implements the various cast operators. */ |
483 | ||
2c74e833 | 484 | static PyObject * |
f9ffd4bb | 485 | valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) |
2c74e833 | 486 | { |
888fe1e1 | 487 | PyObject *type_obj, *result = NULL; |
2c74e833 | 488 | struct type *type; |
2c74e833 TT |
489 | |
490 | if (! PyArg_ParseTuple (args, "O", &type_obj)) | |
491 | return NULL; | |
492 | ||
493 | type = type_object_to_type (type_obj); | |
494 | if (! type) | |
495 | { | |
256458bc | 496 | PyErr_SetString (PyExc_RuntimeError, |
044c0f87 | 497 | _("Argument must be a type.")); |
2c74e833 TT |
498 | return NULL; |
499 | } | |
500 | ||
492d29ea | 501 | TRY |
2c74e833 | 502 | { |
f9ffd4bb | 503 | struct value *val = ((value_object *) self)->value; |
888fe1e1 | 504 | struct value *res_val; |
eb115069 | 505 | scoped_value_mark free_values; |
f9ffd4bb TT |
506 | |
507 | if (op == UNOP_DYNAMIC_CAST) | |
508 | res_val = value_dynamic_cast (type, val); | |
509 | else if (op == UNOP_REINTERPRET_CAST) | |
510 | res_val = value_reinterpret_cast (type, val); | |
511 | else | |
512 | { | |
513 | gdb_assert (op == UNOP_CAST); | |
514 | res_val = value_cast (type, val); | |
515 | } | |
888fe1e1 TT |
516 | |
517 | result = value_to_value_object (res_val); | |
2c74e833 | 518 | } |
492d29ea PA |
519 | CATCH (except, RETURN_MASK_ALL) |
520 | { | |
521 | GDB_PY_HANDLE_EXCEPTION (except); | |
522 | } | |
523 | END_CATCH | |
2c74e833 | 524 | |
888fe1e1 | 525 | return result; |
2c74e833 TT |
526 | } |
527 | ||
f9ffd4bb TT |
528 | /* Implementation of the "cast" method. */ |
529 | ||
530 | static PyObject * | |
531 | valpy_cast (PyObject *self, PyObject *args) | |
532 | { | |
533 | return valpy_do_cast (self, args, UNOP_CAST); | |
534 | } | |
535 | ||
536 | /* Implementation of the "dynamic_cast" method. */ | |
537 | ||
538 | static PyObject * | |
539 | valpy_dynamic_cast (PyObject *self, PyObject *args) | |
540 | { | |
541 | return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST); | |
542 | } | |
543 | ||
544 | /* Implementation of the "reinterpret_cast" method. */ | |
545 | ||
546 | static PyObject * | |
547 | valpy_reinterpret_cast (PyObject *self, PyObject *args) | |
548 | { | |
549 | return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST); | |
550 | } | |
551 | ||
7843261b TJB |
552 | static Py_ssize_t |
553 | valpy_length (PyObject *self) | |
554 | { | |
555 | /* We don't support getting the number of elements in a struct / class. */ | |
556 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 557 | _("Invalid operation on gdb.Value.")); |
7843261b TJB |
558 | return -1; |
559 | } | |
560 | ||
a16b0e22 SC |
561 | /* Return 1 if the gdb.Field object FIELD is present in the value V. |
562 | Returns 0 otherwise. If any Python error occurs, -1 is returned. */ | |
563 | ||
564 | static int | |
565 | value_has_field (struct value *v, PyObject *field) | |
566 | { | |
567 | struct type *parent_type, *val_type; | |
568 | enum type_code type_code; | |
53a0cca3 | 569 | gdbpy_ref type_object (PyObject_GetAttrString (field, "parent_type")); |
a16b0e22 SC |
570 | int has_field = 0; |
571 | ||
572 | if (type_object == NULL) | |
573 | return -1; | |
574 | ||
53a0cca3 | 575 | parent_type = type_object_to_type (type_object.get ()); |
a16b0e22 SC |
576 | if (parent_type == NULL) |
577 | { | |
578 | PyErr_SetString (PyExc_TypeError, | |
579 | _("'parent_type' attribute of gdb.Field object is not a" | |
580 | "gdb.Type object.")); | |
581 | return -1; | |
582 | } | |
583 | ||
492d29ea | 584 | TRY |
a16b0e22 SC |
585 | { |
586 | val_type = value_type (v); | |
587 | val_type = check_typedef (val_type); | |
588 | if (TYPE_CODE (val_type) == TYPE_CODE_REF | |
589 | || TYPE_CODE (val_type) == TYPE_CODE_PTR) | |
590 | val_type = check_typedef (TYPE_TARGET_TYPE (val_type)); | |
591 | ||
592 | type_code = TYPE_CODE (val_type); | |
593 | if ((type_code == TYPE_CODE_STRUCT || type_code == TYPE_CODE_UNION) | |
594 | && types_equal (val_type, parent_type)) | |
595 | has_field = 1; | |
596 | else | |
597 | has_field = 0; | |
598 | } | |
492d29ea PA |
599 | CATCH (except, RETURN_MASK_ALL) |
600 | { | |
601 | GDB_PY_SET_HANDLE_EXCEPTION (except); | |
602 | } | |
603 | END_CATCH | |
a16b0e22 SC |
604 | |
605 | return has_field; | |
606 | } | |
607 | ||
608 | /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD. | |
609 | Returns 1 if the flag value is true, 0 if it is false, and -1 if | |
610 | a Python error occurs. */ | |
611 | ||
612 | static int | |
613 | get_field_flag (PyObject *field, const char *flag_name) | |
614 | { | |
53a0cca3 | 615 | gdbpy_ref flag_object (PyObject_GetAttrString (field, flag_name)); |
a16b0e22 SC |
616 | |
617 | if (flag_object == NULL) | |
618 | return -1; | |
619 | ||
53a0cca3 | 620 | return PyObject_IsTrue (flag_object.get ()); |
a16b0e22 SC |
621 | } |
622 | ||
b5b08fb4 SC |
623 | /* Return the "type" attribute of a gdb.Field object. |
624 | Returns NULL on error, with a Python exception set. */ | |
625 | ||
626 | static struct type * | |
627 | get_field_type (PyObject *field) | |
628 | { | |
53a0cca3 | 629 | gdbpy_ref ftype_obj (PyObject_GetAttrString (field, "type")); |
b5b08fb4 SC |
630 | struct type *ftype; |
631 | ||
632 | if (ftype_obj == NULL) | |
633 | return NULL; | |
53a0cca3 | 634 | ftype = type_object_to_type (ftype_obj.get ()); |
b5b08fb4 | 635 | if (ftype == NULL) |
bf7105a4 JB |
636 | PyErr_SetString (PyExc_TypeError, |
637 | _("'type' attribute of gdb.Field object is not a " | |
638 | "gdb.Type object.")); | |
b5b08fb4 SC |
639 | |
640 | return ftype; | |
641 | } | |
642 | ||
a16b0e22 SC |
643 | /* Given string name or a gdb.Field object corresponding to an element inside |
644 | a structure, return its value object. Returns NULL on error, with a python | |
645 | exception set. */ | |
646 | ||
7843261b TJB |
647 | static PyObject * |
648 | valpy_getitem (PyObject *self, PyObject *key) | |
649 | { | |
492d29ea | 650 | struct gdb_exception except = exception_none; |
7843261b | 651 | value_object *self_value = (value_object *) self; |
9b972014 | 652 | gdb::unique_xmalloc_ptr<char> field; |
b5b08fb4 SC |
653 | struct type *base_class_type = NULL, *field_type = NULL; |
654 | long bitpos = -1; | |
888fe1e1 | 655 | PyObject *result = NULL; |
7843261b | 656 | |
08c637de | 657 | if (gdbpy_is_string (key)) |
256458bc | 658 | { |
08c637de TJB |
659 | field = python_string_to_host_string (key); |
660 | if (field == NULL) | |
661 | return NULL; | |
662 | } | |
a16b0e22 SC |
663 | else if (gdbpy_is_field (key)) |
664 | { | |
665 | int is_base_class, valid_field; | |
666 | ||
667 | valid_field = value_has_field (self_value->value, key); | |
668 | if (valid_field < 0) | |
669 | return NULL; | |
670 | else if (valid_field == 0) | |
671 | { | |
672 | PyErr_SetString (PyExc_TypeError, | |
673 | _("Invalid lookup for a field not contained in " | |
674 | "the value.")); | |
675 | ||
676 | return NULL; | |
677 | } | |
678 | ||
679 | is_base_class = get_field_flag (key, "is_base_class"); | |
680 | if (is_base_class < 0) | |
681 | return NULL; | |
682 | else if (is_base_class > 0) | |
683 | { | |
b5b08fb4 SC |
684 | base_class_type = get_field_type (key); |
685 | if (base_class_type == NULL) | |
a16b0e22 SC |
686 | return NULL; |
687 | } | |
688 | else | |
689 | { | |
53a0cca3 | 690 | gdbpy_ref name_obj (PyObject_GetAttrString (key, "name")); |
a16b0e22 SC |
691 | |
692 | if (name_obj == NULL) | |
693 | return NULL; | |
694 | ||
b5b08fb4 SC |
695 | if (name_obj != Py_None) |
696 | { | |
53a0cca3 | 697 | field = python_string_to_host_string (name_obj.get ()); |
b5b08fb4 SC |
698 | if (field == NULL) |
699 | return NULL; | |
700 | } | |
701 | else | |
702 | { | |
b5b08fb4 SC |
703 | if (!PyObject_HasAttrString (key, "bitpos")) |
704 | { | |
705 | PyErr_SetString (PyExc_AttributeError, | |
706 | _("gdb.Field object has no name and no " | |
707 | "'bitpos' attribute.")); | |
708 | ||
709 | return NULL; | |
710 | } | |
53a0cca3 | 711 | gdbpy_ref bitpos_obj (PyObject_GetAttrString (key, "bitpos")); |
b5b08fb4 SC |
712 | if (bitpos_obj == NULL) |
713 | return NULL; | |
53a0cca3 | 714 | if (!gdb_py_int_as_long (bitpos_obj.get (), &bitpos)) |
b5b08fb4 SC |
715 | return NULL; |
716 | ||
717 | field_type = get_field_type (key); | |
718 | if (field_type == NULL) | |
719 | return NULL; | |
720 | } | |
a16b0e22 SC |
721 | } |
722 | } | |
7843261b | 723 | |
492d29ea | 724 | TRY |
7843261b | 725 | { |
3c0ed299 | 726 | struct value *tmp = self_value->value; |
888fe1e1 | 727 | struct value *res_val = NULL; |
eb115069 | 728 | scoped_value_mark free_values; |
08c637de TJB |
729 | |
730 | if (field) | |
9b972014 | 731 | res_val = value_struct_elt (&tmp, NULL, field.get (), NULL, |
5996220c | 732 | "struct/class/union"); |
b5b08fb4 SC |
733 | else if (bitpos >= 0) |
734 | res_val = value_struct_elt_bitpos (&tmp, bitpos, field_type, | |
735 | "struct/class/union"); | |
736 | else if (base_class_type != NULL) | |
a16b0e22 | 737 | { |
b5b08fb4 | 738 | struct type *val_type; |
a16b0e22 SC |
739 | |
740 | val_type = check_typedef (value_type (tmp)); | |
741 | if (TYPE_CODE (val_type) == TYPE_CODE_PTR) | |
742 | res_val = value_cast (lookup_pointer_type (base_class_type), tmp); | |
743 | else if (TYPE_CODE (val_type) == TYPE_CODE_REF) | |
744 | res_val = value_cast (lookup_reference_type (base_class_type), tmp); | |
745 | else | |
746 | res_val = value_cast (base_class_type, tmp); | |
747 | } | |
08c637de TJB |
748 | else |
749 | { | |
750 | /* Assume we are attempting an array access, and let the | |
751 | value code throw an exception if the index has an invalid | |
752 | type. */ | |
753 | struct value *idx = convert_value_from_python (key); | |
d59b6f6c | 754 | |
570e2b1a | 755 | if (idx != NULL) |
2e4d963f PM |
756 | { |
757 | /* Check the value's type is something that can be accessed via | |
758 | a subscript. */ | |
759 | struct type *type; | |
d59b6f6c | 760 | |
2e4d963f PM |
761 | tmp = coerce_ref (tmp); |
762 | type = check_typedef (value_type (tmp)); | |
763 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY | |
764 | && TYPE_CODE (type) != TYPE_CODE_PTR) | |
e10abd8f | 765 | error (_("Cannot subscript requested type.")); |
2e4d963f PM |
766 | else |
767 | res_val = value_subscript (tmp, value_as_long (idx)); | |
768 | } | |
08c637de | 769 | } |
888fe1e1 TT |
770 | |
771 | if (res_val) | |
772 | result = value_to_value_object (res_val); | |
7843261b | 773 | } |
492d29ea PA |
774 | CATCH (ex, RETURN_MASK_ALL) |
775 | { | |
776 | except = ex; | |
777 | } | |
778 | END_CATCH | |
570e2b1a | 779 | |
7843261b TJB |
780 | GDB_PY_HANDLE_EXCEPTION (except); |
781 | ||
888fe1e1 | 782 | return result; |
7843261b TJB |
783 | } |
784 | ||
785 | static int | |
786 | valpy_setitem (PyObject *self, PyObject *key, PyObject *value) | |
787 | { | |
788 | PyErr_Format (PyExc_NotImplementedError, | |
789 | _("Setting of struct elements is not currently supported.")); | |
790 | return -1; | |
791 | } | |
792 | ||
5374244e | 793 | /* Called by the Python interpreter to perform an inferior function |
8dc78533 | 794 | call on the value. Returns NULL on error, with a python exception set. */ |
5374244e PM |
795 | static PyObject * |
796 | valpy_call (PyObject *self, PyObject *args, PyObject *keywords) | |
797 | { | |
5374244e | 798 | Py_ssize_t args_count; |
5374244e PM |
799 | struct value *function = ((value_object *) self)->value; |
800 | struct value **vargs = NULL; | |
749fd4ea | 801 | struct type *ftype = NULL; |
888fe1e1 TT |
802 | struct value *mark = value_mark (); |
803 | PyObject *result = NULL; | |
f287c1f3 | 804 | |
492d29ea | 805 | TRY |
f287c1f3 PM |
806 | { |
807 | ftype = check_typedef (value_type (function)); | |
808 | } | |
492d29ea PA |
809 | CATCH (except, RETURN_MASK_ALL) |
810 | { | |
811 | GDB_PY_HANDLE_EXCEPTION (except); | |
812 | } | |
813 | END_CATCH | |
5374244e PM |
814 | |
815 | if (TYPE_CODE (ftype) != TYPE_CODE_FUNC) | |
816 | { | |
817 | PyErr_SetString (PyExc_RuntimeError, | |
818 | _("Value is not callable (not TYPE_CODE_FUNC).")); | |
819 | return NULL; | |
820 | } | |
821 | ||
f287c1f3 PM |
822 | if (! PyTuple_Check (args)) |
823 | { | |
824 | PyErr_SetString (PyExc_TypeError, | |
825 | _("Inferior arguments must be provided in a tuple.")); | |
826 | return NULL; | |
827 | } | |
828 | ||
5374244e PM |
829 | args_count = PyTuple_Size (args); |
830 | if (args_count > 0) | |
831 | { | |
832 | int i; | |
833 | ||
8d749320 | 834 | vargs = XALLOCAVEC (struct value *, args_count); |
5374244e PM |
835 | for (i = 0; i < args_count; i++) |
836 | { | |
837 | PyObject *item = PyTuple_GetItem (args, i); | |
838 | ||
839 | if (item == NULL) | |
840 | return NULL; | |
841 | ||
842 | vargs[i] = convert_value_from_python (item); | |
843 | if (vargs[i] == NULL) | |
844 | return NULL; | |
845 | } | |
846 | } | |
847 | ||
492d29ea | 848 | TRY |
5374244e | 849 | { |
eb115069 | 850 | scoped_value_mark free_values; |
888fe1e1 TT |
851 | struct value *return_value; |
852 | ||
5374244e | 853 | return_value = call_function_by_hand (function, args_count, vargs); |
888fe1e1 | 854 | result = value_to_value_object (return_value); |
5374244e | 855 | } |
492d29ea PA |
856 | CATCH (except, RETURN_MASK_ALL) |
857 | { | |
858 | GDB_PY_HANDLE_EXCEPTION (except); | |
859 | } | |
860 | END_CATCH | |
5374244e | 861 | |
888fe1e1 | 862 | return result; |
5374244e PM |
863 | } |
864 | ||
7843261b TJB |
865 | /* Called by the Python interpreter to obtain string representation |
866 | of the object. */ | |
867 | static PyObject * | |
868 | valpy_str (PyObject *self) | |
869 | { | |
c92aed16 | 870 | std::string s; |
7843261b | 871 | PyObject *result; |
79a45b7d | 872 | struct value_print_options opts; |
7843261b | 873 | |
79a45b7d TT |
874 | get_user_print_options (&opts); |
875 | opts.deref_ref = 0; | |
876 | ||
492d29ea | 877 | TRY |
7843261b | 878 | { |
5fe41fbf TT |
879 | struct ui_file *stb = mem_fileopen (); |
880 | struct cleanup *old_chain = make_cleanup_ui_file_delete (stb); | |
881 | ||
79a45b7d | 882 | common_val_print (((value_object *) self)->value, stb, 0, |
d452c4bc | 883 | &opts, python_language); |
c92aed16 | 884 | s = ui_file_as_string (stb); |
5fe41fbf TT |
885 | |
886 | do_cleanups (old_chain); | |
7843261b | 887 | } |
492d29ea PA |
888 | CATCH (except, RETURN_MASK_ALL) |
889 | { | |
890 | GDB_PY_HANDLE_EXCEPTION (except); | |
891 | } | |
892 | END_CATCH | |
7843261b | 893 | |
c92aed16 | 894 | result = PyUnicode_Decode (s.c_str (), s.length (), host_charset (), NULL); |
7843261b TJB |
895 | |
896 | return result; | |
897 | } | |
898 | ||
def2b000 TJB |
899 | /* Implements gdb.Value.is_optimized_out. */ |
900 | static PyObject * | |
901 | valpy_get_is_optimized_out (PyObject *self, void *closure) | |
902 | { | |
903 | struct value *value = ((value_object *) self)->value; | |
f287c1f3 | 904 | int opt = 0; |
def2b000 | 905 | |
492d29ea | 906 | TRY |
f287c1f3 PM |
907 | { |
908 | opt = value_optimized_out (value); | |
909 | } | |
492d29ea PA |
910 | CATCH (except, RETURN_MASK_ALL) |
911 | { | |
912 | GDB_PY_HANDLE_EXCEPTION (except); | |
913 | } | |
914 | END_CATCH | |
f287c1f3 PM |
915 | |
916 | if (opt) | |
def2b000 TJB |
917 | Py_RETURN_TRUE; |
918 | ||
919 | Py_RETURN_FALSE; | |
920 | } | |
921 | ||
913460fc PK |
922 | /* Implements gdb.Value.is_lazy. */ |
923 | static PyObject * | |
924 | valpy_get_is_lazy (PyObject *self, void *closure) | |
925 | { | |
926 | struct value *value = ((value_object *) self)->value; | |
927 | int opt = 0; | |
913460fc | 928 | |
492d29ea | 929 | TRY |
913460fc PK |
930 | { |
931 | opt = value_lazy (value); | |
932 | } | |
492d29ea PA |
933 | CATCH (except, RETURN_MASK_ALL) |
934 | { | |
935 | GDB_PY_HANDLE_EXCEPTION (except); | |
936 | } | |
937 | END_CATCH | |
913460fc PK |
938 | |
939 | if (opt) | |
940 | Py_RETURN_TRUE; | |
941 | ||
942 | Py_RETURN_FALSE; | |
943 | } | |
944 | ||
945 | /* Implements gdb.Value.fetch_lazy (). */ | |
946 | static PyObject * | |
947 | valpy_fetch_lazy (PyObject *self, PyObject *args) | |
948 | { | |
949 | struct value *value = ((value_object *) self)->value; | |
913460fc | 950 | |
492d29ea | 951 | TRY |
913460fc PK |
952 | { |
953 | if (value_lazy (value)) | |
954 | value_fetch_lazy (value); | |
955 | } | |
492d29ea PA |
956 | CATCH (except, RETURN_MASK_ALL) |
957 | { | |
958 | GDB_PY_HANDLE_EXCEPTION (except); | |
959 | } | |
960 | END_CATCH | |
913460fc PK |
961 | |
962 | Py_RETURN_NONE; | |
963 | } | |
964 | ||
88d4aea7 PM |
965 | /* Calculate and return the address of the PyObject as the value of |
966 | the builtin __hash__ call. */ | |
881d5d5d | 967 | static Py_hash_t |
88d4aea7 PM |
968 | valpy_hash (PyObject *self) |
969 | { | |
881d5d5d | 970 | return (intptr_t) self; |
88d4aea7 PM |
971 | } |
972 | ||
7843261b TJB |
973 | enum valpy_opcode |
974 | { | |
975 | VALPY_ADD, | |
976 | VALPY_SUB, | |
977 | VALPY_MUL, | |
978 | VALPY_DIV, | |
979 | VALPY_REM, | |
08c637de TJB |
980 | VALPY_POW, |
981 | VALPY_LSH, | |
982 | VALPY_RSH, | |
983 | VALPY_BITAND, | |
984 | VALPY_BITOR, | |
985 | VALPY_BITXOR | |
7843261b TJB |
986 | }; |
987 | ||
988 | /* If TYPE is a reference, return the target; otherwise return TYPE. */ | |
989 | #define STRIP_REFERENCE(TYPE) \ | |
990 | ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE)) | |
991 | ||
9c6595ab PA |
992 | /* Helper for valpy_binop. Returns a value object which is the result |
993 | of applying the operation specified by OPCODE to the given | |
994 | arguments. Throws a GDB exception on error. */ | |
995 | ||
7843261b | 996 | static PyObject * |
9c6595ab | 997 | valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other) |
7843261b | 998 | { |
888fe1e1 | 999 | PyObject *result = NULL; |
7843261b | 1000 | |
9c6595ab | 1001 | struct value *arg1, *arg2; |
9c6595ab PA |
1002 | struct value *res_val = NULL; |
1003 | enum exp_opcode op = OP_NULL; | |
1004 | int handled = 0; | |
1005 | ||
eb115069 TT |
1006 | scoped_value_mark free_values; |
1007 | ||
9c6595ab PA |
1008 | /* If the gdb.Value object is the second operand, then it will be |
1009 | passed to us as the OTHER argument, and SELF will be an entirely | |
1010 | different kind of object, altogether. Because of this, we can't | |
1011 | assume self is a gdb.Value object and need to convert it from | |
1012 | python as well. */ | |
1013 | arg1 = convert_value_from_python (self); | |
1014 | if (arg1 == NULL) | |
eb115069 | 1015 | return NULL; |
08c637de | 1016 | |
9c6595ab PA |
1017 | arg2 = convert_value_from_python (other); |
1018 | if (arg2 == NULL) | |
eb115069 | 1019 | return NULL; |
7843261b | 1020 | |
9c6595ab PA |
1021 | switch (opcode) |
1022 | { | |
1023 | case VALPY_ADD: | |
1024 | { | |
1025 | struct type *ltype = value_type (arg1); | |
1026 | struct type *rtype = value_type (arg2); | |
1027 | ||
1028 | ltype = check_typedef (ltype); | |
1029 | ltype = STRIP_REFERENCE (ltype); | |
1030 | rtype = check_typedef (rtype); | |
1031 | rtype = STRIP_REFERENCE (rtype); | |
1032 | ||
1033 | handled = 1; | |
1034 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR | |
1035 | && is_integral_type (rtype)) | |
1036 | res_val = value_ptradd (arg1, value_as_long (arg2)); | |
1037 | else if (TYPE_CODE (rtype) == TYPE_CODE_PTR | |
1038 | && is_integral_type (ltype)) | |
1039 | res_val = value_ptradd (arg2, value_as_long (arg1)); | |
1040 | else | |
7843261b | 1041 | { |
9c6595ab PA |
1042 | handled = 0; |
1043 | op = BINOP_ADD; | |
7843261b | 1044 | } |
9c6595ab PA |
1045 | } |
1046 | break; | |
1047 | case VALPY_SUB: | |
1048 | { | |
1049 | struct type *ltype = value_type (arg1); | |
1050 | struct type *rtype = value_type (arg2); | |
1051 | ||
1052 | ltype = check_typedef (ltype); | |
1053 | ltype = STRIP_REFERENCE (ltype); | |
1054 | rtype = check_typedef (rtype); | |
1055 | rtype = STRIP_REFERENCE (rtype); | |
1056 | ||
1057 | handled = 1; | |
1058 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR | |
1059 | && TYPE_CODE (rtype) == TYPE_CODE_PTR) | |
1060 | /* A ptrdiff_t for the target would be preferable here. */ | |
1061 | res_val = value_from_longest (builtin_type_pyint, | |
1062 | value_ptrdiff (arg1, arg2)); | |
1063 | else if (TYPE_CODE (ltype) == TYPE_CODE_PTR | |
1064 | && is_integral_type (rtype)) | |
1065 | res_val = value_ptradd (arg1, - value_as_long (arg2)); | |
1066 | else | |
7843261b | 1067 | { |
9c6595ab PA |
1068 | handled = 0; |
1069 | op = BINOP_SUB; | |
7843261b | 1070 | } |
9c6595ab PA |
1071 | } |
1072 | break; | |
1073 | case VALPY_MUL: | |
1074 | op = BINOP_MUL; | |
1075 | break; | |
1076 | case VALPY_DIV: | |
1077 | op = BINOP_DIV; | |
1078 | break; | |
1079 | case VALPY_REM: | |
1080 | op = BINOP_REM; | |
1081 | break; | |
1082 | case VALPY_POW: | |
1083 | op = BINOP_EXP; | |
1084 | break; | |
1085 | case VALPY_LSH: | |
1086 | op = BINOP_LSH; | |
1087 | break; | |
1088 | case VALPY_RSH: | |
1089 | op = BINOP_RSH; | |
1090 | break; | |
1091 | case VALPY_BITAND: | |
1092 | op = BINOP_BITWISE_AND; | |
1093 | break; | |
1094 | case VALPY_BITOR: | |
1095 | op = BINOP_BITWISE_IOR; | |
1096 | break; | |
1097 | case VALPY_BITXOR: | |
1098 | op = BINOP_BITWISE_XOR; | |
1099 | break; | |
1100 | } | |
888fe1e1 | 1101 | |
9c6595ab PA |
1102 | if (!handled) |
1103 | { | |
1104 | if (binop_user_defined_p (op, arg1, arg2)) | |
1105 | res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL); | |
1106 | else | |
1107 | res_val = value_binop (arg1, arg2, op); | |
1108 | } | |
f7bd0f78 | 1109 | |
9c6595ab PA |
1110 | if (res_val) |
1111 | result = value_to_value_object (res_val); | |
888fe1e1 | 1112 | |
9c6595ab PA |
1113 | return result; |
1114 | } | |
1115 | ||
1116 | /* Returns a value object which is the result of applying the operation | |
1117 | specified by OPCODE to the given arguments. Returns NULL on error, with | |
1118 | a python exception set. */ | |
1119 | static PyObject * | |
1120 | valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) | |
1121 | { | |
1122 | PyObject *result = NULL; | |
1123 | ||
1124 | TRY | |
1125 | { | |
1126 | result = valpy_binop_throw (opcode, self, other); | |
7843261b | 1127 | } |
492d29ea PA |
1128 | CATCH (except, RETURN_MASK_ALL) |
1129 | { | |
1130 | GDB_PY_HANDLE_EXCEPTION (except); | |
1131 | } | |
1132 | END_CATCH | |
7843261b | 1133 | |
888fe1e1 | 1134 | return result; |
7843261b TJB |
1135 | } |
1136 | ||
1137 | static PyObject * | |
1138 | valpy_add (PyObject *self, PyObject *other) | |
1139 | { | |
1140 | return valpy_binop (VALPY_ADD, self, other); | |
1141 | } | |
1142 | ||
1143 | static PyObject * | |
1144 | valpy_subtract (PyObject *self, PyObject *other) | |
1145 | { | |
1146 | return valpy_binop (VALPY_SUB, self, other); | |
1147 | } | |
1148 | ||
1149 | static PyObject * | |
1150 | valpy_multiply (PyObject *self, PyObject *other) | |
1151 | { | |
1152 | return valpy_binop (VALPY_MUL, self, other); | |
1153 | } | |
1154 | ||
1155 | static PyObject * | |
1156 | valpy_divide (PyObject *self, PyObject *other) | |
1157 | { | |
1158 | return valpy_binop (VALPY_DIV, self, other); | |
1159 | } | |
1160 | ||
1161 | static PyObject * | |
1162 | valpy_remainder (PyObject *self, PyObject *other) | |
1163 | { | |
1164 | return valpy_binop (VALPY_REM, self, other); | |
1165 | } | |
1166 | ||
1167 | static PyObject * | |
1168 | valpy_power (PyObject *self, PyObject *other, PyObject *unused) | |
1169 | { | |
1170 | /* We don't support the ternary form of pow. I don't know how to express | |
1171 | that, so let's just throw NotImplementedError to at least do something | |
1172 | about it. */ | |
1173 | if (unused != Py_None) | |
1174 | { | |
1175 | PyErr_SetString (PyExc_NotImplementedError, | |
1176 | "Invalid operation on gdb.Value."); | |
1177 | return NULL; | |
1178 | } | |
1179 | ||
1180 | return valpy_binop (VALPY_POW, self, other); | |
1181 | } | |
1182 | ||
1183 | static PyObject * | |
1184 | valpy_negative (PyObject *self) | |
1185 | { | |
888fe1e1 | 1186 | PyObject *result = NULL; |
7843261b | 1187 | |
492d29ea | 1188 | TRY |
7843261b | 1189 | { |
888fe1e1 | 1190 | /* Perhaps overkill, but consistency has some virtue. */ |
eb115069 | 1191 | scoped_value_mark free_values; |
888fe1e1 TT |
1192 | struct value *val; |
1193 | ||
7843261b | 1194 | val = value_neg (((value_object *) self)->value); |
888fe1e1 | 1195 | result = value_to_value_object (val); |
7843261b | 1196 | } |
492d29ea PA |
1197 | CATCH (except, RETURN_MASK_ALL) |
1198 | { | |
1199 | GDB_PY_HANDLE_EXCEPTION (except); | |
1200 | } | |
1201 | END_CATCH | |
7843261b | 1202 | |
888fe1e1 | 1203 | return result; |
7843261b TJB |
1204 | } |
1205 | ||
1206 | static PyObject * | |
1207 | valpy_positive (PyObject *self) | |
1208 | { | |
4e7a5ef5 | 1209 | return value_to_value_object (((value_object *) self)->value); |
7843261b TJB |
1210 | } |
1211 | ||
1212 | static PyObject * | |
1213 | valpy_absolute (PyObject *self) | |
1214 | { | |
22601c15 | 1215 | struct value *value = ((value_object *) self)->value; |
f287c1f3 | 1216 | int isabs = 1; |
d59b6f6c | 1217 | |
492d29ea | 1218 | TRY |
f287c1f3 | 1219 | { |
eb115069 | 1220 | scoped_value_mark free_values; |
888fe1e1 | 1221 | |
f287c1f3 PM |
1222 | if (value_less (value, value_zero (value_type (value), not_lval))) |
1223 | isabs = 0; | |
1224 | } | |
492d29ea PA |
1225 | CATCH (except, RETURN_MASK_ALL) |
1226 | { | |
1227 | GDB_PY_HANDLE_EXCEPTION (except); | |
1228 | } | |
1229 | END_CATCH | |
f287c1f3 PM |
1230 | |
1231 | if (isabs) | |
7843261b | 1232 | return valpy_positive (self); |
f287c1f3 PM |
1233 | else |
1234 | return valpy_negative (self); | |
7843261b TJB |
1235 | } |
1236 | ||
1237 | /* Implements boolean evaluation of gdb.Value. */ | |
1238 | static int | |
1239 | valpy_nonzero (PyObject *self) | |
1240 | { | |
492d29ea | 1241 | struct gdb_exception except = exception_none; |
7843261b TJB |
1242 | value_object *self_value = (value_object *) self; |
1243 | struct type *type; | |
f287c1f3 | 1244 | int nonzero = 0; /* Appease GCC warning. */ |
7843261b | 1245 | |
492d29ea | 1246 | TRY |
f287c1f3 | 1247 | { |
5d9c5995 PM |
1248 | type = check_typedef (value_type (self_value->value)); |
1249 | ||
f287c1f3 PM |
1250 | if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR) |
1251 | nonzero = !!value_as_long (self_value->value); | |
1252 | else if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1253 | nonzero = value_as_double (self_value->value) != 0; | |
1254 | else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1255 | nonzero = !decimal_is_zero (value_contents (self_value->value), | |
1256 | TYPE_LENGTH (type), | |
1257 | gdbarch_byte_order (get_type_arch (type))); | |
1258 | else | |
1259 | /* All other values are True. */ | |
1260 | nonzero = 1; | |
1261 | } | |
492d29ea PA |
1262 | CATCH (ex, RETURN_MASK_ALL) |
1263 | { | |
1264 | except = ex; | |
1265 | } | |
1266 | END_CATCH | |
1267 | ||
f287c1f3 PM |
1268 | /* This is not documented in the Python documentation, but if this |
1269 | function fails, return -1 as slot_nb_nonzero does (the default | |
1270 | Python nonzero function). */ | |
1271 | GDB_PY_SET_HANDLE_EXCEPTION (except); | |
1272 | ||
1273 | return nonzero; | |
7843261b TJB |
1274 | } |
1275 | ||
08c637de | 1276 | /* Implements ~ for value objects. */ |
7843261b | 1277 | static PyObject * |
08c637de | 1278 | valpy_invert (PyObject *self) |
7843261b | 1279 | { |
08c637de | 1280 | struct value *val = NULL; |
7843261b | 1281 | |
492d29ea | 1282 | TRY |
7843261b | 1283 | { |
08c637de TJB |
1284 | val = value_complement (((value_object *) self)->value); |
1285 | } | |
492d29ea PA |
1286 | CATCH (except, RETURN_MASK_ALL) |
1287 | { | |
1288 | GDB_PY_HANDLE_EXCEPTION (except); | |
1289 | } | |
1290 | END_CATCH | |
7843261b | 1291 | |
08c637de TJB |
1292 | return value_to_value_object (val); |
1293 | } | |
7843261b | 1294 | |
08c637de TJB |
1295 | /* Implements left shift for value objects. */ |
1296 | static PyObject * | |
1297 | valpy_lsh (PyObject *self, PyObject *other) | |
1298 | { | |
1299 | return valpy_binop (VALPY_LSH, self, other); | |
1300 | } | |
7843261b | 1301 | |
08c637de TJB |
1302 | /* Implements right shift for value objects. */ |
1303 | static PyObject * | |
1304 | valpy_rsh (PyObject *self, PyObject *other) | |
1305 | { | |
1306 | return valpy_binop (VALPY_RSH, self, other); | |
1307 | } | |
7843261b | 1308 | |
08c637de TJB |
1309 | /* Implements bitwise and for value objects. */ |
1310 | static PyObject * | |
1311 | valpy_and (PyObject *self, PyObject *other) | |
1312 | { | |
1313 | return valpy_binop (VALPY_BITAND, self, other); | |
1314 | } | |
7843261b | 1315 | |
08c637de TJB |
1316 | /* Implements bitwise or for value objects. */ |
1317 | static PyObject * | |
1318 | valpy_or (PyObject *self, PyObject *other) | |
1319 | { | |
1320 | return valpy_binop (VALPY_BITOR, self, other); | |
1321 | } | |
1322 | ||
1323 | /* Implements bitwise xor for value objects. */ | |
1324 | static PyObject * | |
1325 | valpy_xor (PyObject *self, PyObject *other) | |
1326 | { | |
1327 | return valpy_binop (VALPY_BITXOR, self, other); | |
1328 | } | |
1329 | ||
9c6595ab PA |
1330 | /* Helper for valpy_richcompare. Implements comparison operations for |
1331 | value objects. Returns true/false on success. Returns -1 with a | |
1332 | Python exception set if a Python error is detected. Throws a GDB | |
1333 | exception on other errors (memory error, etc.). */ | |
1334 | ||
1335 | static int | |
1336 | valpy_richcompare_throw (PyObject *self, PyObject *other, int op) | |
1337 | { | |
1338 | int result; | |
1339 | struct value *value_other; | |
1340 | struct value *value_self; | |
9c6595ab PA |
1341 | struct cleanup *cleanup; |
1342 | ||
eb115069 TT |
1343 | scoped_value_mark free_values; |
1344 | ||
9c6595ab PA |
1345 | value_other = convert_value_from_python (other); |
1346 | if (value_other == NULL) | |
1347 | return -1; | |
1348 | ||
9c6595ab PA |
1349 | value_self = ((value_object *) self)->value; |
1350 | ||
1351 | switch (op) | |
1352 | { | |
1353 | case Py_LT: | |
1354 | result = value_less (value_self, value_other); | |
1355 | break; | |
1356 | case Py_LE: | |
1357 | result = value_less (value_self, value_other) | |
1358 | || value_equal (value_self, value_other); | |
1359 | break; | |
1360 | case Py_EQ: | |
1361 | result = value_equal (value_self, value_other); | |
1362 | break; | |
1363 | case Py_NE: | |
1364 | result = !value_equal (value_self, value_other); | |
1365 | break; | |
1366 | case Py_GT: | |
1367 | result = value_less (value_other, value_self); | |
1368 | break; | |
1369 | case Py_GE: | |
1370 | result = (value_less (value_other, value_self) | |
1371 | || value_equal (value_self, value_other)); | |
1372 | break; | |
1373 | default: | |
1374 | /* Can't happen. */ | |
1375 | PyErr_SetString (PyExc_NotImplementedError, | |
1376 | _("Invalid operation on gdb.Value.")); | |
1377 | result = -1; | |
1378 | break; | |
1379 | } | |
1380 | ||
9c6595ab PA |
1381 | return result; |
1382 | } | |
1383 | ||
1384 | ||
8dc78533 JK |
1385 | /* Implements comparison operations for value objects. Returns NULL on error, |
1386 | with a python exception set. */ | |
08c637de TJB |
1387 | static PyObject * |
1388 | valpy_richcompare (PyObject *self, PyObject *other, int op) | |
1389 | { | |
1390 | int result = 0; | |
08c637de TJB |
1391 | |
1392 | if (other == Py_None) | |
7843261b TJB |
1393 | /* Comparing with None is special. From what I can tell, in Python |
1394 | None is smaller than anything else. */ | |
1395 | switch (op) { | |
1396 | case Py_LT: | |
1397 | case Py_LE: | |
1398 | case Py_EQ: | |
1399 | Py_RETURN_FALSE; | |
1400 | case Py_NE: | |
1401 | case Py_GT: | |
1402 | case Py_GE: | |
1403 | Py_RETURN_TRUE; | |
1404 | default: | |
1405 | /* Can't happen. */ | |
1406 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 1407 | _("Invalid operation on gdb.Value.")); |
7843261b TJB |
1408 | return NULL; |
1409 | } | |
7843261b | 1410 | |
492d29ea | 1411 | TRY |
7843261b | 1412 | { |
9c6595ab | 1413 | result = valpy_richcompare_throw (self, other, op); |
7843261b | 1414 | } |
492d29ea PA |
1415 | CATCH (except, RETURN_MASK_ALL) |
1416 | { | |
1417 | GDB_PY_HANDLE_EXCEPTION (except); | |
1418 | } | |
1419 | END_CATCH | |
7843261b | 1420 | |
f02779d8 TT |
1421 | /* In this case, the Python exception has already been set. */ |
1422 | if (result < 0) | |
1423 | return NULL; | |
1424 | ||
7843261b TJB |
1425 | if (result == 1) |
1426 | Py_RETURN_TRUE; | |
1427 | ||
1428 | Py_RETURN_FALSE; | |
1429 | } | |
1430 | ||
9a27f2c6 | 1431 | #ifndef IS_PY3K |
08c637de TJB |
1432 | /* Implements conversion to int. */ |
1433 | static PyObject * | |
1434 | valpy_int (PyObject *self) | |
1435 | { | |
1436 | struct value *value = ((value_object *) self)->value; | |
1437 | struct type *type = value_type (value); | |
1438 | LONGEST l = 0; | |
08c637de | 1439 | |
492d29ea | 1440 | TRY |
08c637de | 1441 | { |
7c245c24 | 1442 | if (!is_integral_type (type)) |
f287c1f3 PM |
1443 | error (_("Cannot convert value to int.")); |
1444 | ||
08c637de TJB |
1445 | l = value_as_long (value); |
1446 | } | |
492d29ea PA |
1447 | CATCH (except, RETURN_MASK_ALL) |
1448 | { | |
1449 | GDB_PY_HANDLE_EXCEPTION (except); | |
1450 | } | |
1451 | END_CATCH | |
08c637de | 1452 | |
74aedc46 | 1453 | return gdb_py_object_from_longest (l); |
08c637de | 1454 | } |
9a27f2c6 | 1455 | #endif |
08c637de TJB |
1456 | |
1457 | /* Implements conversion to long. */ | |
1458 | static PyObject * | |
1459 | valpy_long (PyObject *self) | |
1460 | { | |
1461 | struct value *value = ((value_object *) self)->value; | |
1462 | struct type *type = value_type (value); | |
1463 | LONGEST l = 0; | |
08c637de | 1464 | |
492d29ea | 1465 | TRY |
08c637de | 1466 | { |
f168693b | 1467 | type = check_typedef (type); |
f287c1f3 | 1468 | |
7c245c24 JB |
1469 | if (!is_integral_type (type) |
1470 | && TYPE_CODE (type) != TYPE_CODE_PTR) | |
f287c1f3 PM |
1471 | error (_("Cannot convert value to long.")); |
1472 | ||
08c637de TJB |
1473 | l = value_as_long (value); |
1474 | } | |
492d29ea PA |
1475 | CATCH (except, RETURN_MASK_ALL) |
1476 | { | |
1477 | GDB_PY_HANDLE_EXCEPTION (except); | |
1478 | } | |
1479 | END_CATCH | |
08c637de | 1480 | |
33fa2c6e DE |
1481 | if (TYPE_UNSIGNED (type)) |
1482 | return gdb_py_long_from_ulongest (l); | |
1483 | else | |
1484 | return gdb_py_long_from_longest (l); | |
08c637de TJB |
1485 | } |
1486 | ||
1487 | /* Implements conversion to float. */ | |
1488 | static PyObject * | |
1489 | valpy_float (PyObject *self) | |
1490 | { | |
1491 | struct value *value = ((value_object *) self)->value; | |
1492 | struct type *type = value_type (value); | |
1493 | double d = 0; | |
08c637de | 1494 | |
492d29ea | 1495 | TRY |
08c637de | 1496 | { |
f168693b | 1497 | type = check_typedef (type); |
f287c1f3 PM |
1498 | |
1499 | if (TYPE_CODE (type) != TYPE_CODE_FLT) | |
1500 | error (_("Cannot convert value to float.")); | |
1501 | ||
08c637de TJB |
1502 | d = value_as_double (value); |
1503 | } | |
492d29ea PA |
1504 | CATCH (except, RETURN_MASK_ALL) |
1505 | { | |
1506 | GDB_PY_HANDLE_EXCEPTION (except); | |
1507 | } | |
1508 | END_CATCH | |
08c637de TJB |
1509 | |
1510 | return PyFloat_FromDouble (d); | |
1511 | } | |
1512 | ||
7843261b TJB |
1513 | /* Returns an object for a value which is released from the all_values chain, |
1514 | so its lifetime is not bound to the execution of a command. */ | |
1515 | PyObject * | |
1516 | value_to_value_object (struct value *val) | |
1517 | { | |
1518 | value_object *val_obj; | |
1519 | ||
1520 | val_obj = PyObject_New (value_object, &value_object_type); | |
1521 | if (val_obj != NULL) | |
1522 | { | |
1523 | val_obj->value = val; | |
e848a8a5 | 1524 | release_value_or_incref (val); |
c0c6f777 | 1525 | val_obj->address = NULL; |
2c74e833 | 1526 | val_obj->type = NULL; |
03f17ccf | 1527 | val_obj->dynamic_type = NULL; |
4e7a5ef5 | 1528 | note_value (val_obj); |
7843261b TJB |
1529 | } |
1530 | ||
1531 | return (PyObject *) val_obj; | |
1532 | } | |
1533 | ||
4e7a5ef5 TT |
1534 | /* Returns a borrowed reference to the struct value corresponding to |
1535 | the given value object. */ | |
a6bac58e TT |
1536 | struct value * |
1537 | value_object_to_value (PyObject *self) | |
1538 | { | |
1539 | value_object *real; | |
d59b6f6c | 1540 | |
a6bac58e TT |
1541 | if (! PyObject_TypeCheck (self, &value_object_type)) |
1542 | return NULL; | |
1543 | real = (value_object *) self; | |
1544 | return real->value; | |
1545 | } | |
1546 | ||
7843261b | 1547 | /* Try to convert a Python value to a gdb value. If the value cannot |
4e7a5ef5 TT |
1548 | be converted, set a Python exception and return NULL. Returns a |
1549 | reference to a new value on the all_values chain. */ | |
7843261b TJB |
1550 | |
1551 | struct value * | |
1552 | convert_value_from_python (PyObject *obj) | |
1553 | { | |
1554 | struct value *value = NULL; /* -Wall */ | |
08c637de | 1555 | int cmp; |
7843261b | 1556 | |
08c637de | 1557 | gdb_assert (obj != NULL); |
7843261b | 1558 | |
492d29ea | 1559 | TRY |
7843261b | 1560 | { |
256458bc | 1561 | if (PyBool_Check (obj)) |
08c637de TJB |
1562 | { |
1563 | cmp = PyObject_IsTrue (obj); | |
1564 | if (cmp >= 0) | |
1565 | value = value_from_longest (builtin_type_pybool, cmp); | |
1566 | } | |
06ab7b19 PM |
1567 | /* Make a long logic check first. In Python 3.x, internally, |
1568 | all integers are represented as longs. In Python 2.x, there | |
1569 | is still a differentiation internally between a PyInt and a | |
1570 | PyLong. Explicitly do this long check conversion first. In | |
1571 | GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has | |
1572 | to be done first to ensure we do not lose information in the | |
1573 | conversion process. */ | |
08c637de TJB |
1574 | else if (PyLong_Check (obj)) |
1575 | { | |
1576 | LONGEST l = PyLong_AsLongLong (obj); | |
7843261b | 1577 | |
595939de PM |
1578 | if (PyErr_Occurred ()) |
1579 | { | |
1580 | /* If the error was an overflow, we can try converting to | |
1581 | ULONGEST instead. */ | |
1582 | if (PyErr_ExceptionMatches (PyExc_OverflowError)) | |
1583 | { | |
53a0cca3 | 1584 | PyObject *etype, *evalue, *etraceback; |
595939de PM |
1585 | |
1586 | PyErr_Fetch (&etype, &evalue, &etraceback); | |
53a0cca3 | 1587 | gdbpy_ref zero (PyInt_FromLong (0)); |
595939de PM |
1588 | |
1589 | /* Check whether obj is positive. */ | |
53a0cca3 | 1590 | if (PyObject_RichCompareBool (obj, zero.get (), Py_GT) > 0) |
595939de PM |
1591 | { |
1592 | ULONGEST ul; | |
1593 | ||
1594 | ul = PyLong_AsUnsignedLongLong (obj); | |
1595 | if (! PyErr_Occurred ()) | |
1596 | value = value_from_ulongest (builtin_type_upylong, ul); | |
1597 | } | |
1598 | else | |
1599 | /* There's nothing we can do. */ | |
1600 | PyErr_Restore (etype, evalue, etraceback); | |
595939de PM |
1601 | } |
1602 | } | |
1603 | else | |
08c637de TJB |
1604 | value = value_from_longest (builtin_type_pylong, l); |
1605 | } | |
12c58cd4 | 1606 | #if PY_MAJOR_VERSION == 2 |
06ab7b19 PM |
1607 | else if (PyInt_Check (obj)) |
1608 | { | |
1609 | long l = PyInt_AsLong (obj); | |
1610 | ||
1611 | if (! PyErr_Occurred ()) | |
1612 | value = value_from_longest (builtin_type_pyint, l); | |
1613 | } | |
12c58cd4 | 1614 | #endif |
08c637de TJB |
1615 | else if (PyFloat_Check (obj)) |
1616 | { | |
1617 | double d = PyFloat_AsDouble (obj); | |
7843261b | 1618 | |
08c637de TJB |
1619 | if (! PyErr_Occurred ()) |
1620 | value = value_from_double (builtin_type_pyfloat, d); | |
1621 | } | |
1622 | else if (gdbpy_is_string (obj)) | |
1623 | { | |
9b972014 TT |
1624 | gdb::unique_xmalloc_ptr<char> s |
1625 | = python_string_to_target_string (obj); | |
08c637de | 1626 | if (s != NULL) |
9b972014 TT |
1627 | value = value_cstring (s.get (), strlen (s.get ()), |
1628 | builtin_type_pychar); | |
08c637de TJB |
1629 | } |
1630 | else if (PyObject_TypeCheck (obj, &value_object_type)) | |
cc924cad | 1631 | value = value_copy (((value_object *) obj)->value); |
be759fcf PM |
1632 | else if (gdbpy_is_lazy_string (obj)) |
1633 | { | |
1634 | PyObject *result; | |
d59b6f6c | 1635 | |
fb6a3ed3 | 1636 | result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst, NULL); |
be759fcf PM |
1637 | value = value_copy (((value_object *) result)->value); |
1638 | } | |
08c637de | 1639 | else |
9a27f2c6 PK |
1640 | #ifdef IS_PY3K |
1641 | PyErr_Format (PyExc_TypeError, | |
1642 | _("Could not convert Python object: %S."), obj); | |
1643 | #else | |
9a2b4c1b MS |
1644 | PyErr_Format (PyExc_TypeError, |
1645 | _("Could not convert Python object: %s."), | |
08c637de | 1646 | PyString_AsString (PyObject_Str (obj))); |
9a27f2c6 | 1647 | #endif |
08c637de | 1648 | } |
492d29ea | 1649 | CATCH (except, RETURN_MASK_ALL) |
08c637de TJB |
1650 | { |
1651 | PyErr_Format (except.reason == RETURN_QUIT | |
1652 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, | |
1653 | "%s", except.message); | |
1654 | return NULL; | |
1655 | } | |
492d29ea | 1656 | END_CATCH |
7843261b TJB |
1657 | |
1658 | return value; | |
1659 | } | |
1660 | ||
1661 | /* Returns value object in the ARGth position in GDB's history. */ | |
1662 | PyObject * | |
08c637de | 1663 | gdbpy_history (PyObject *self, PyObject *args) |
7843261b TJB |
1664 | { |
1665 | int i; | |
1666 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
7843261b TJB |
1667 | |
1668 | if (!PyArg_ParseTuple (args, "i", &i)) | |
1669 | return NULL; | |
1670 | ||
492d29ea | 1671 | TRY |
7843261b TJB |
1672 | { |
1673 | res_val = access_value_history (i); | |
1674 | } | |
492d29ea PA |
1675 | CATCH (except, RETURN_MASK_ALL) |
1676 | { | |
1677 | GDB_PY_HANDLE_EXCEPTION (except); | |
1678 | } | |
1679 | END_CATCH | |
7843261b TJB |
1680 | |
1681 | return value_to_value_object (res_val); | |
1682 | } | |
1683 | ||
595939de PM |
1684 | /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */ |
1685 | ||
1686 | int | |
1687 | gdbpy_is_value_object (PyObject *obj) | |
1688 | { | |
1689 | return PyObject_TypeCheck (obj, &value_object_type); | |
1690 | } | |
1691 | ||
999633ed | 1692 | int |
7843261b TJB |
1693 | gdbpy_initialize_values (void) |
1694 | { | |
7843261b | 1695 | if (PyType_Ready (&value_object_type) < 0) |
999633ed | 1696 | return -1; |
7843261b | 1697 | |
aa36459a TT |
1698 | return gdb_pymodule_addobject (gdb_module, "Value", |
1699 | (PyObject *) &value_object_type); | |
7843261b TJB |
1700 | } |
1701 | ||
2c74e833 TT |
1702 | \f |
1703 | ||
def2b000 | 1704 | static PyGetSetDef value_object_getset[] = { |
c0c6f777 TJB |
1705 | { "address", valpy_get_address, NULL, "The address of the value.", |
1706 | NULL }, | |
def2b000 | 1707 | { "is_optimized_out", valpy_get_is_optimized_out, NULL, |
9a2b4c1b MS |
1708 | "Boolean telling whether the value is optimized " |
1709 | "out (i.e., not available).", | |
def2b000 | 1710 | NULL }, |
2c74e833 | 1711 | { "type", valpy_get_type, NULL, "Type of the value.", NULL }, |
03f17ccf TT |
1712 | { "dynamic_type", valpy_get_dynamic_type, NULL, |
1713 | "Dynamic type of the value.", NULL }, | |
913460fc PK |
1714 | { "is_lazy", valpy_get_is_lazy, NULL, |
1715 | "Boolean telling whether the value is lazy (not fetched yet\n\ | |
1716 | from the inferior). A lazy value is fetched when needed, or when\n\ | |
1717 | the \"fetch_lazy()\" method is called.", NULL }, | |
def2b000 TJB |
1718 | {NULL} /* Sentinel */ |
1719 | }; | |
1720 | ||
f9176c46 | 1721 | static PyMethodDef value_object_methods[] = { |
2c74e833 | 1722 | { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." }, |
f9ffd4bb TT |
1723 | { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS, |
1724 | "dynamic_cast (gdb.Type) -> gdb.Value\n\ | |
1725 | Cast the value to the supplied type, as if by the C++ dynamic_cast operator." | |
1726 | }, | |
1727 | { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS, | |
1728 | "reinterpret_cast (gdb.Type) -> gdb.Value\n\ | |
1729 | Cast the value to the supplied type, as if by the C++\n\ | |
1730 | reinterpret_cast operator." | |
1731 | }, | |
f9176c46 | 1732 | { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, |
7b282c5a SCR |
1733 | { "referenced_value", valpy_referenced_value, METH_NOARGS, |
1734 | "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." }, | |
4c082a81 SC |
1735 | { "reference_value", valpy_reference_value, METH_NOARGS, |
1736 | "Return a value of type TYPE_CODE_REF referencing this value." }, | |
1737 | { "const_value", valpy_const_value, METH_NOARGS, | |
1738 | "Return a 'const' qualied version of the same value." }, | |
9a2b4c1b MS |
1739 | { "lazy_string", (PyCFunction) valpy_lazy_string, |
1740 | METH_VARARGS | METH_KEYWORDS, | |
be759fcf PM |
1741 | "lazy_string ([encoding] [, length]) -> lazy_string\n\ |
1742 | Return a lazy string representation of the value." }, | |
cc924cad | 1743 | { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS, |
fbb8f299 | 1744 | "string ([encoding] [, errors] [, length]) -> string\n\ |
cc924cad | 1745 | Return Unicode string representation of the value." }, |
256458bc | 1746 | { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS, |
913460fc | 1747 | "Fetches the value from the inferior, if it was lazy." }, |
f9176c46 PA |
1748 | {NULL} /* Sentinel */ |
1749 | }; | |
1750 | ||
1751 | static PyNumberMethods value_object_as_number = { | |
1752 | valpy_add, | |
1753 | valpy_subtract, | |
1754 | valpy_multiply, | |
9a27f2c6 | 1755 | #ifndef IS_PY3K |
f9176c46 | 1756 | valpy_divide, |
9a27f2c6 | 1757 | #endif |
f9176c46 PA |
1758 | valpy_remainder, |
1759 | NULL, /* nb_divmod */ | |
1760 | valpy_power, /* nb_power */ | |
1761 | valpy_negative, /* nb_negative */ | |
1762 | valpy_positive, /* nb_positive */ | |
1763 | valpy_absolute, /* nb_absolute */ | |
08c637de TJB |
1764 | valpy_nonzero, /* nb_nonzero */ |
1765 | valpy_invert, /* nb_invert */ | |
1766 | valpy_lsh, /* nb_lshift */ | |
1767 | valpy_rsh, /* nb_rshift */ | |
1768 | valpy_and, /* nb_and */ | |
1769 | valpy_xor, /* nb_xor */ | |
1770 | valpy_or, /* nb_or */ | |
9a27f2c6 PK |
1771 | #ifdef IS_PY3K |
1772 | valpy_long, /* nb_int */ | |
1773 | NULL, /* reserved */ | |
1774 | #else | |
08c637de TJB |
1775 | NULL, /* nb_coerce */ |
1776 | valpy_int, /* nb_int */ | |
1777 | valpy_long, /* nb_long */ | |
9a27f2c6 | 1778 | #endif |
08c637de | 1779 | valpy_float, /* nb_float */ |
9a27f2c6 | 1780 | #ifndef IS_PY3K |
08c637de | 1781 | NULL, /* nb_oct */ |
9a27f2c6 PK |
1782 | NULL, /* nb_hex */ |
1783 | #endif | |
1784 | NULL, /* nb_inplace_add */ | |
1785 | NULL, /* nb_inplace_subtract */ | |
1786 | NULL, /* nb_inplace_multiply */ | |
e2b7f516 TT |
1787 | #ifndef IS_PY3K |
1788 | NULL, /* nb_inplace_divide */ | |
1789 | #endif | |
9a27f2c6 PK |
1790 | NULL, /* nb_inplace_remainder */ |
1791 | NULL, /* nb_inplace_power */ | |
1792 | NULL, /* nb_inplace_lshift */ | |
1793 | NULL, /* nb_inplace_rshift */ | |
1794 | NULL, /* nb_inplace_and */ | |
1795 | NULL, /* nb_inplace_xor */ | |
1796 | NULL, /* nb_inplace_or */ | |
1797 | NULL, /* nb_floor_divide */ | |
ddae9462 TT |
1798 | valpy_divide, /* nb_true_divide */ |
1799 | NULL, /* nb_inplace_floor_divide */ | |
1800 | NULL, /* nb_inplace_true_divide */ | |
7bd787e8 | 1801 | #ifndef HAVE_LIBPYTHON2_4 |
ddae9462 TT |
1802 | /* This was added in Python 2.5. */ |
1803 | valpy_long, /* nb_index */ | |
7bd787e8 | 1804 | #endif /* HAVE_LIBPYTHON2_4 */ |
f9176c46 PA |
1805 | }; |
1806 | ||
1807 | static PyMappingMethods value_object_as_mapping = { | |
1808 | valpy_length, | |
1809 | valpy_getitem, | |
1810 | valpy_setitem | |
1811 | }; | |
1812 | ||
1813 | PyTypeObject value_object_type = { | |
9a27f2c6 | 1814 | PyVarObject_HEAD_INIT (NULL, 0) |
f9176c46 PA |
1815 | "gdb.Value", /*tp_name*/ |
1816 | sizeof (value_object), /*tp_basicsize*/ | |
1817 | 0, /*tp_itemsize*/ | |
1818 | valpy_dealloc, /*tp_dealloc*/ | |
1819 | 0, /*tp_print*/ | |
1820 | 0, /*tp_getattr*/ | |
1821 | 0, /*tp_setattr*/ | |
1822 | 0, /*tp_compare*/ | |
1823 | 0, /*tp_repr*/ | |
1824 | &value_object_as_number, /*tp_as_number*/ | |
1825 | 0, /*tp_as_sequence*/ | |
1826 | &value_object_as_mapping, /*tp_as_mapping*/ | |
88d4aea7 | 1827 | valpy_hash, /*tp_hash*/ |
5374244e | 1828 | valpy_call, /*tp_call*/ |
f9176c46 PA |
1829 | valpy_str, /*tp_str*/ |
1830 | 0, /*tp_getattro*/ | |
1831 | 0, /*tp_setattro*/ | |
1832 | 0, /*tp_as_buffer*/ | |
9a2b4c1b MS |
1833 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
1834 | | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
f9176c46 PA |
1835 | "GDB value object", /* tp_doc */ |
1836 | 0, /* tp_traverse */ | |
1837 | 0, /* tp_clear */ | |
1838 | valpy_richcompare, /* tp_richcompare */ | |
1839 | 0, /* tp_weaklistoffset */ | |
1840 | 0, /* tp_iter */ | |
1841 | 0, /* tp_iternext */ | |
08c637de TJB |
1842 | value_object_methods, /* tp_methods */ |
1843 | 0, /* tp_members */ | |
def2b000 | 1844 | value_object_getset, /* tp_getset */ |
08c637de TJB |
1845 | 0, /* tp_base */ |
1846 | 0, /* tp_dict */ | |
1847 | 0, /* tp_descr_get */ | |
1848 | 0, /* tp_descr_set */ | |
1849 | 0, /* tp_dictoffset */ | |
1850 | 0, /* tp_init */ | |
1851 | 0, /* tp_alloc */ | |
1852 | valpy_new /* tp_new */ | |
f9176c46 | 1853 | }; |