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 | |
3b224330 | 372 | type = lookup_lvalue_reference_type (type); |
03f17ccf TT |
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 | |
34b43320 DE |
403 | the string is not encoded. |
404 | If LENGTH is provided then the length parameter is set to LENGTH. | |
405 | Otherwise if the value is an array of known length then the array's length | |
406 | is used. Otherwise the length will be set to -1 (meaning first null of | |
407 | appropriate with). | |
408 | ||
409 | Note: In order to not break any existing uses this allows creating | |
410 | lazy strings from anything. PR 20769. E.g., | |
411 | gdb.parse_and_eval("my_int_variable").lazy_string(). | |
412 | "It's easier to relax restrictions than it is to impose them after the | |
413 | fact." So we should be flagging any unintended uses as errors, but it's | |
414 | perhaps too late for that. */ | |
415 | ||
be759fcf PM |
416 | static PyObject * |
417 | valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw) | |
418 | { | |
74aedc46 | 419 | gdb_py_longest length = -1; |
be759fcf PM |
420 | struct value *value = ((value_object *) self)->value; |
421 | const char *user_encoding = NULL; | |
422 | static char *keywords[] = { "encoding", "length", NULL }; | |
888fe1e1 | 423 | PyObject *str_obj = NULL; |
be759fcf | 424 | |
74aedc46 | 425 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|s" GDB_PY_LL_ARG, keywords, |
be759fcf PM |
426 | &user_encoding, &length)) |
427 | return NULL; | |
428 | ||
34b43320 DE |
429 | if (length < -1) |
430 | { | |
431 | PyErr_SetString (PyExc_ValueError, _("Invalid length.")); | |
432 | return NULL; | |
433 | } | |
434 | ||
492d29ea | 435 | TRY |
f287c1f3 | 436 | { |
eb115069 | 437 | scoped_value_mark free_values; |
34b43320 DE |
438 | struct type *type, *realtype; |
439 | CORE_ADDR addr; | |
440 | ||
441 | type = value_type (value); | |
442 | realtype = check_typedef (type); | |
888fe1e1 | 443 | |
34b43320 DE |
444 | switch (TYPE_CODE (realtype)) |
445 | { | |
446 | case TYPE_CODE_ARRAY: | |
447 | { | |
448 | LONGEST array_length = -1; | |
449 | LONGEST low_bound, high_bound; | |
450 | ||
451 | /* PR 20786: There's no way to specify an array of length zero. | |
452 | Record a length of [0,-1] which is how Ada does it. Anything | |
453 | we do is broken, but this one possible solution. */ | |
454 | if (get_array_bounds (realtype, &low_bound, &high_bound)) | |
455 | array_length = high_bound - low_bound + 1; | |
456 | if (length == -1) | |
457 | length = array_length; | |
458 | else if (array_length == -1) | |
459 | { | |
460 | type = lookup_array_range_type (TYPE_TARGET_TYPE (realtype), | |
461 | 0, length - 1); | |
462 | } | |
463 | else if (length != array_length) | |
464 | { | |
465 | /* We need to create a new array type with the | |
466 | specified length. */ | |
467 | if (length > array_length) | |
468 | error (_("Length is larger than array size.")); | |
469 | type = lookup_array_range_type (TYPE_TARGET_TYPE (realtype), | |
470 | low_bound, | |
471 | low_bound + length - 1); | |
472 | } | |
473 | addr = value_address (value); | |
474 | break; | |
475 | } | |
476 | case TYPE_CODE_PTR: | |
477 | /* If a length is specified we defer creating an array of the | |
478 | specified width until we need to. */ | |
479 | addr = value_as_address (value); | |
480 | break; | |
481 | default: | |
482 | /* Should flag an error here. PR 20769. */ | |
483 | addr = value_address (value); | |
484 | break; | |
485 | } | |
888fe1e1 | 486 | |
34b43320 DE |
487 | str_obj = gdbpy_create_lazy_string_object (addr, length, user_encoding, |
488 | type); | |
f287c1f3 | 489 | } |
492d29ea PA |
490 | CATCH (except, RETURN_MASK_ALL) |
491 | { | |
492 | GDB_PY_HANDLE_EXCEPTION (except); | |
493 | } | |
494 | END_CATCH | |
be759fcf | 495 | |
888fe1e1 | 496 | return str_obj; |
be759fcf PM |
497 | } |
498 | ||
fbb8f299 PM |
499 | /* Implementation of gdb.Value.string ([encoding] [, errors] |
500 | [, length]) -> string. Return Unicode string with value contents. | |
501 | If ENCODING is not given, the string is assumed to be encoded in | |
502 | the target's charset. If LENGTH is provided, only fetch string to | |
503 | the length provided. */ | |
504 | ||
b6cb8e7d | 505 | static PyObject * |
cc924cad | 506 | valpy_string (PyObject *self, PyObject *args, PyObject *kw) |
b6cb8e7d | 507 | { |
f92adf3c | 508 | int length = -1; |
b6cb8e7d TJB |
509 | gdb_byte *buffer; |
510 | struct value *value = ((value_object *) self)->value; | |
b6cb8e7d TJB |
511 | PyObject *unicode; |
512 | const char *encoding = NULL; | |
513 | const char *errors = NULL; | |
514 | const char *user_encoding = NULL; | |
515 | const char *la_encoding = NULL; | |
96c07c5b | 516 | struct type *char_type; |
31158f0e | 517 | static char *keywords[] = { "encoding", "errors", "length", NULL }; |
b6cb8e7d | 518 | |
fbb8f299 PM |
519 | if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords, |
520 | &user_encoding, &errors, &length)) | |
b6cb8e7d TJB |
521 | return NULL; |
522 | ||
492d29ea | 523 | TRY |
b6cb8e7d | 524 | { |
96c07c5b | 525 | LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding); |
b6cb8e7d | 526 | } |
492d29ea PA |
527 | CATCH (except, RETURN_MASK_ALL) |
528 | { | |
529 | GDB_PY_HANDLE_EXCEPTION (except); | |
530 | } | |
531 | END_CATCH | |
b6cb8e7d TJB |
532 | |
533 | encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding; | |
89f6d837 PA |
534 | unicode = PyUnicode_Decode ((const char *) buffer, |
535 | length * TYPE_LENGTH (char_type), | |
96c07c5b | 536 | encoding, errors); |
b6cb8e7d TJB |
537 | xfree (buffer); |
538 | ||
539 | return unicode; | |
540 | } | |
541 | ||
f9ffd4bb TT |
542 | /* A helper function that implements the various cast operators. */ |
543 | ||
2c74e833 | 544 | static PyObject * |
f9ffd4bb | 545 | valpy_do_cast (PyObject *self, PyObject *args, enum exp_opcode op) |
2c74e833 | 546 | { |
888fe1e1 | 547 | PyObject *type_obj, *result = NULL; |
2c74e833 | 548 | struct type *type; |
2c74e833 TT |
549 | |
550 | if (! PyArg_ParseTuple (args, "O", &type_obj)) | |
551 | return NULL; | |
552 | ||
553 | type = type_object_to_type (type_obj); | |
554 | if (! type) | |
555 | { | |
256458bc | 556 | PyErr_SetString (PyExc_RuntimeError, |
044c0f87 | 557 | _("Argument must be a type.")); |
2c74e833 TT |
558 | return NULL; |
559 | } | |
560 | ||
492d29ea | 561 | TRY |
2c74e833 | 562 | { |
f9ffd4bb | 563 | struct value *val = ((value_object *) self)->value; |
888fe1e1 | 564 | struct value *res_val; |
eb115069 | 565 | scoped_value_mark free_values; |
f9ffd4bb TT |
566 | |
567 | if (op == UNOP_DYNAMIC_CAST) | |
568 | res_val = value_dynamic_cast (type, val); | |
569 | else if (op == UNOP_REINTERPRET_CAST) | |
570 | res_val = value_reinterpret_cast (type, val); | |
571 | else | |
572 | { | |
573 | gdb_assert (op == UNOP_CAST); | |
574 | res_val = value_cast (type, val); | |
575 | } | |
888fe1e1 TT |
576 | |
577 | result = value_to_value_object (res_val); | |
2c74e833 | 578 | } |
492d29ea PA |
579 | CATCH (except, RETURN_MASK_ALL) |
580 | { | |
581 | GDB_PY_HANDLE_EXCEPTION (except); | |
582 | } | |
583 | END_CATCH | |
2c74e833 | 584 | |
888fe1e1 | 585 | return result; |
2c74e833 TT |
586 | } |
587 | ||
f9ffd4bb TT |
588 | /* Implementation of the "cast" method. */ |
589 | ||
590 | static PyObject * | |
591 | valpy_cast (PyObject *self, PyObject *args) | |
592 | { | |
593 | return valpy_do_cast (self, args, UNOP_CAST); | |
594 | } | |
595 | ||
596 | /* Implementation of the "dynamic_cast" method. */ | |
597 | ||
598 | static PyObject * | |
599 | valpy_dynamic_cast (PyObject *self, PyObject *args) | |
600 | { | |
601 | return valpy_do_cast (self, args, UNOP_DYNAMIC_CAST); | |
602 | } | |
603 | ||
604 | /* Implementation of the "reinterpret_cast" method. */ | |
605 | ||
606 | static PyObject * | |
607 | valpy_reinterpret_cast (PyObject *self, PyObject *args) | |
608 | { | |
609 | return valpy_do_cast (self, args, UNOP_REINTERPRET_CAST); | |
610 | } | |
611 | ||
7843261b TJB |
612 | static Py_ssize_t |
613 | valpy_length (PyObject *self) | |
614 | { | |
615 | /* We don't support getting the number of elements in a struct / class. */ | |
616 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 617 | _("Invalid operation on gdb.Value.")); |
7843261b TJB |
618 | return -1; |
619 | } | |
620 | ||
a16b0e22 SC |
621 | /* Return 1 if the gdb.Field object FIELD is present in the value V. |
622 | Returns 0 otherwise. If any Python error occurs, -1 is returned. */ | |
623 | ||
624 | static int | |
625 | value_has_field (struct value *v, PyObject *field) | |
626 | { | |
627 | struct type *parent_type, *val_type; | |
628 | enum type_code type_code; | |
7780f186 | 629 | gdbpy_ref<> type_object (PyObject_GetAttrString (field, "parent_type")); |
a16b0e22 SC |
630 | int has_field = 0; |
631 | ||
632 | if (type_object == NULL) | |
633 | return -1; | |
634 | ||
53a0cca3 | 635 | parent_type = type_object_to_type (type_object.get ()); |
a16b0e22 SC |
636 | if (parent_type == NULL) |
637 | { | |
638 | PyErr_SetString (PyExc_TypeError, | |
639 | _("'parent_type' attribute of gdb.Field object is not a" | |
640 | "gdb.Type object.")); | |
641 | return -1; | |
642 | } | |
643 | ||
492d29ea | 644 | TRY |
a16b0e22 SC |
645 | { |
646 | val_type = value_type (v); | |
647 | val_type = check_typedef (val_type); | |
648 | if (TYPE_CODE (val_type) == TYPE_CODE_REF | |
649 | || TYPE_CODE (val_type) == TYPE_CODE_PTR) | |
650 | val_type = check_typedef (TYPE_TARGET_TYPE (val_type)); | |
651 | ||
652 | type_code = TYPE_CODE (val_type); | |
653 | if ((type_code == TYPE_CODE_STRUCT || type_code == TYPE_CODE_UNION) | |
654 | && types_equal (val_type, parent_type)) | |
655 | has_field = 1; | |
656 | else | |
657 | has_field = 0; | |
658 | } | |
492d29ea PA |
659 | CATCH (except, RETURN_MASK_ALL) |
660 | { | |
661 | GDB_PY_SET_HANDLE_EXCEPTION (except); | |
662 | } | |
663 | END_CATCH | |
a16b0e22 SC |
664 | |
665 | return has_field; | |
666 | } | |
667 | ||
668 | /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD. | |
669 | Returns 1 if the flag value is true, 0 if it is false, and -1 if | |
670 | a Python error occurs. */ | |
671 | ||
672 | static int | |
673 | get_field_flag (PyObject *field, const char *flag_name) | |
674 | { | |
7780f186 | 675 | gdbpy_ref<> flag_object (PyObject_GetAttrString (field, flag_name)); |
a16b0e22 SC |
676 | |
677 | if (flag_object == NULL) | |
678 | return -1; | |
679 | ||
53a0cca3 | 680 | return PyObject_IsTrue (flag_object.get ()); |
a16b0e22 SC |
681 | } |
682 | ||
b5b08fb4 SC |
683 | /* Return the "type" attribute of a gdb.Field object. |
684 | Returns NULL on error, with a Python exception set. */ | |
685 | ||
686 | static struct type * | |
687 | get_field_type (PyObject *field) | |
688 | { | |
7780f186 | 689 | gdbpy_ref<> ftype_obj (PyObject_GetAttrString (field, "type")); |
b5b08fb4 SC |
690 | struct type *ftype; |
691 | ||
692 | if (ftype_obj == NULL) | |
693 | return NULL; | |
53a0cca3 | 694 | ftype = type_object_to_type (ftype_obj.get ()); |
b5b08fb4 | 695 | if (ftype == NULL) |
bf7105a4 JB |
696 | PyErr_SetString (PyExc_TypeError, |
697 | _("'type' attribute of gdb.Field object is not a " | |
698 | "gdb.Type object.")); | |
b5b08fb4 SC |
699 | |
700 | return ftype; | |
701 | } | |
702 | ||
a16b0e22 SC |
703 | /* Given string name or a gdb.Field object corresponding to an element inside |
704 | a structure, return its value object. Returns NULL on error, with a python | |
705 | exception set. */ | |
706 | ||
7843261b TJB |
707 | static PyObject * |
708 | valpy_getitem (PyObject *self, PyObject *key) | |
709 | { | |
492d29ea | 710 | struct gdb_exception except = exception_none; |
7843261b | 711 | value_object *self_value = (value_object *) self; |
9b972014 | 712 | gdb::unique_xmalloc_ptr<char> field; |
b5b08fb4 SC |
713 | struct type *base_class_type = NULL, *field_type = NULL; |
714 | long bitpos = -1; | |
888fe1e1 | 715 | PyObject *result = NULL; |
7843261b | 716 | |
08c637de | 717 | if (gdbpy_is_string (key)) |
256458bc | 718 | { |
08c637de TJB |
719 | field = python_string_to_host_string (key); |
720 | if (field == NULL) | |
721 | return NULL; | |
722 | } | |
a16b0e22 SC |
723 | else if (gdbpy_is_field (key)) |
724 | { | |
725 | int is_base_class, valid_field; | |
726 | ||
727 | valid_field = value_has_field (self_value->value, key); | |
728 | if (valid_field < 0) | |
729 | return NULL; | |
730 | else if (valid_field == 0) | |
731 | { | |
732 | PyErr_SetString (PyExc_TypeError, | |
733 | _("Invalid lookup for a field not contained in " | |
734 | "the value.")); | |
735 | ||
736 | return NULL; | |
737 | } | |
738 | ||
739 | is_base_class = get_field_flag (key, "is_base_class"); | |
740 | if (is_base_class < 0) | |
741 | return NULL; | |
742 | else if (is_base_class > 0) | |
743 | { | |
b5b08fb4 SC |
744 | base_class_type = get_field_type (key); |
745 | if (base_class_type == NULL) | |
a16b0e22 SC |
746 | return NULL; |
747 | } | |
748 | else | |
749 | { | |
7780f186 | 750 | gdbpy_ref<> name_obj (PyObject_GetAttrString (key, "name")); |
a16b0e22 SC |
751 | |
752 | if (name_obj == NULL) | |
753 | return NULL; | |
754 | ||
b5b08fb4 SC |
755 | if (name_obj != Py_None) |
756 | { | |
53a0cca3 | 757 | field = python_string_to_host_string (name_obj.get ()); |
b5b08fb4 SC |
758 | if (field == NULL) |
759 | return NULL; | |
760 | } | |
761 | else | |
762 | { | |
b5b08fb4 SC |
763 | if (!PyObject_HasAttrString (key, "bitpos")) |
764 | { | |
765 | PyErr_SetString (PyExc_AttributeError, | |
766 | _("gdb.Field object has no name and no " | |
767 | "'bitpos' attribute.")); | |
768 | ||
769 | return NULL; | |
770 | } | |
7780f186 | 771 | gdbpy_ref<> bitpos_obj (PyObject_GetAttrString (key, "bitpos")); |
b5b08fb4 SC |
772 | if (bitpos_obj == NULL) |
773 | return NULL; | |
53a0cca3 | 774 | if (!gdb_py_int_as_long (bitpos_obj.get (), &bitpos)) |
b5b08fb4 SC |
775 | return NULL; |
776 | ||
777 | field_type = get_field_type (key); | |
778 | if (field_type == NULL) | |
779 | return NULL; | |
780 | } | |
a16b0e22 SC |
781 | } |
782 | } | |
7843261b | 783 | |
492d29ea | 784 | TRY |
7843261b | 785 | { |
3c0ed299 | 786 | struct value *tmp = self_value->value; |
888fe1e1 | 787 | struct value *res_val = NULL; |
eb115069 | 788 | scoped_value_mark free_values; |
08c637de TJB |
789 | |
790 | if (field) | |
9b972014 | 791 | res_val = value_struct_elt (&tmp, NULL, field.get (), NULL, |
5996220c | 792 | "struct/class/union"); |
b5b08fb4 SC |
793 | else if (bitpos >= 0) |
794 | res_val = value_struct_elt_bitpos (&tmp, bitpos, field_type, | |
795 | "struct/class/union"); | |
796 | else if (base_class_type != NULL) | |
a16b0e22 | 797 | { |
b5b08fb4 | 798 | struct type *val_type; |
a16b0e22 SC |
799 | |
800 | val_type = check_typedef (value_type (tmp)); | |
801 | if (TYPE_CODE (val_type) == TYPE_CODE_PTR) | |
802 | res_val = value_cast (lookup_pointer_type (base_class_type), tmp); | |
803 | else if (TYPE_CODE (val_type) == TYPE_CODE_REF) | |
3b224330 AV |
804 | res_val = value_cast (lookup_lvalue_reference_type (base_class_type), |
805 | tmp); | |
a16b0e22 SC |
806 | else |
807 | res_val = value_cast (base_class_type, tmp); | |
808 | } | |
08c637de TJB |
809 | else |
810 | { | |
811 | /* Assume we are attempting an array access, and let the | |
812 | value code throw an exception if the index has an invalid | |
813 | type. */ | |
814 | struct value *idx = convert_value_from_python (key); | |
d59b6f6c | 815 | |
570e2b1a | 816 | if (idx != NULL) |
2e4d963f PM |
817 | { |
818 | /* Check the value's type is something that can be accessed via | |
819 | a subscript. */ | |
820 | struct type *type; | |
d59b6f6c | 821 | |
2e4d963f PM |
822 | tmp = coerce_ref (tmp); |
823 | type = check_typedef (value_type (tmp)); | |
824 | if (TYPE_CODE (type) != TYPE_CODE_ARRAY | |
825 | && TYPE_CODE (type) != TYPE_CODE_PTR) | |
e10abd8f | 826 | error (_("Cannot subscript requested type.")); |
2e4d963f PM |
827 | else |
828 | res_val = value_subscript (tmp, value_as_long (idx)); | |
829 | } | |
08c637de | 830 | } |
888fe1e1 TT |
831 | |
832 | if (res_val) | |
833 | result = value_to_value_object (res_val); | |
7843261b | 834 | } |
492d29ea PA |
835 | CATCH (ex, RETURN_MASK_ALL) |
836 | { | |
837 | except = ex; | |
838 | } | |
839 | END_CATCH | |
570e2b1a | 840 | |
7843261b TJB |
841 | GDB_PY_HANDLE_EXCEPTION (except); |
842 | ||
888fe1e1 | 843 | return result; |
7843261b TJB |
844 | } |
845 | ||
846 | static int | |
847 | valpy_setitem (PyObject *self, PyObject *key, PyObject *value) | |
848 | { | |
849 | PyErr_Format (PyExc_NotImplementedError, | |
850 | _("Setting of struct elements is not currently supported.")); | |
851 | return -1; | |
852 | } | |
853 | ||
5374244e | 854 | /* Called by the Python interpreter to perform an inferior function |
8dc78533 | 855 | call on the value. Returns NULL on error, with a python exception set. */ |
5374244e PM |
856 | static PyObject * |
857 | valpy_call (PyObject *self, PyObject *args, PyObject *keywords) | |
858 | { | |
5374244e | 859 | Py_ssize_t args_count; |
5374244e PM |
860 | struct value *function = ((value_object *) self)->value; |
861 | struct value **vargs = NULL; | |
749fd4ea | 862 | struct type *ftype = NULL; |
888fe1e1 TT |
863 | struct value *mark = value_mark (); |
864 | PyObject *result = NULL; | |
f287c1f3 | 865 | |
492d29ea | 866 | TRY |
f287c1f3 PM |
867 | { |
868 | ftype = check_typedef (value_type (function)); | |
869 | } | |
492d29ea PA |
870 | CATCH (except, RETURN_MASK_ALL) |
871 | { | |
872 | GDB_PY_HANDLE_EXCEPTION (except); | |
873 | } | |
874 | END_CATCH | |
5374244e PM |
875 | |
876 | if (TYPE_CODE (ftype) != TYPE_CODE_FUNC) | |
877 | { | |
878 | PyErr_SetString (PyExc_RuntimeError, | |
879 | _("Value is not callable (not TYPE_CODE_FUNC).")); | |
880 | return NULL; | |
881 | } | |
882 | ||
f287c1f3 PM |
883 | if (! PyTuple_Check (args)) |
884 | { | |
885 | PyErr_SetString (PyExc_TypeError, | |
886 | _("Inferior arguments must be provided in a tuple.")); | |
887 | return NULL; | |
888 | } | |
889 | ||
5374244e PM |
890 | args_count = PyTuple_Size (args); |
891 | if (args_count > 0) | |
892 | { | |
893 | int i; | |
894 | ||
8d749320 | 895 | vargs = XALLOCAVEC (struct value *, args_count); |
5374244e PM |
896 | for (i = 0; i < args_count; i++) |
897 | { | |
898 | PyObject *item = PyTuple_GetItem (args, i); | |
899 | ||
900 | if (item == NULL) | |
901 | return NULL; | |
902 | ||
903 | vargs[i] = convert_value_from_python (item); | |
904 | if (vargs[i] == NULL) | |
905 | return NULL; | |
906 | } | |
907 | } | |
908 | ||
492d29ea | 909 | TRY |
5374244e | 910 | { |
eb115069 | 911 | scoped_value_mark free_values; |
888fe1e1 TT |
912 | struct value *return_value; |
913 | ||
5374244e | 914 | return_value = call_function_by_hand (function, args_count, vargs); |
888fe1e1 | 915 | result = value_to_value_object (return_value); |
5374244e | 916 | } |
492d29ea PA |
917 | CATCH (except, RETURN_MASK_ALL) |
918 | { | |
919 | GDB_PY_HANDLE_EXCEPTION (except); | |
920 | } | |
921 | END_CATCH | |
5374244e | 922 | |
888fe1e1 | 923 | return result; |
5374244e PM |
924 | } |
925 | ||
7843261b TJB |
926 | /* Called by the Python interpreter to obtain string representation |
927 | of the object. */ | |
928 | static PyObject * | |
929 | valpy_str (PyObject *self) | |
930 | { | |
79a45b7d | 931 | struct value_print_options opts; |
7843261b | 932 | |
79a45b7d TT |
933 | get_user_print_options (&opts); |
934 | opts.deref_ref = 0; | |
935 | ||
d7e74731 PA |
936 | string_file stb; |
937 | ||
492d29ea | 938 | TRY |
7843261b | 939 | { |
d7e74731 | 940 | common_val_print (((value_object *) self)->value, &stb, 0, |
d452c4bc | 941 | &opts, python_language); |
7843261b | 942 | } |
492d29ea PA |
943 | CATCH (except, RETURN_MASK_ALL) |
944 | { | |
945 | GDB_PY_HANDLE_EXCEPTION (except); | |
946 | } | |
947 | END_CATCH | |
7843261b | 948 | |
d7e74731 | 949 | return PyUnicode_Decode (stb.c_str (), stb.size (), host_charset (), NULL); |
7843261b TJB |
950 | } |
951 | ||
def2b000 TJB |
952 | /* Implements gdb.Value.is_optimized_out. */ |
953 | static PyObject * | |
954 | valpy_get_is_optimized_out (PyObject *self, void *closure) | |
955 | { | |
956 | struct value *value = ((value_object *) self)->value; | |
f287c1f3 | 957 | int opt = 0; |
def2b000 | 958 | |
492d29ea | 959 | TRY |
f287c1f3 PM |
960 | { |
961 | opt = value_optimized_out (value); | |
962 | } | |
492d29ea PA |
963 | CATCH (except, RETURN_MASK_ALL) |
964 | { | |
965 | GDB_PY_HANDLE_EXCEPTION (except); | |
966 | } | |
967 | END_CATCH | |
f287c1f3 PM |
968 | |
969 | if (opt) | |
def2b000 TJB |
970 | Py_RETURN_TRUE; |
971 | ||
972 | Py_RETURN_FALSE; | |
973 | } | |
974 | ||
913460fc PK |
975 | /* Implements gdb.Value.is_lazy. */ |
976 | static PyObject * | |
977 | valpy_get_is_lazy (PyObject *self, void *closure) | |
978 | { | |
979 | struct value *value = ((value_object *) self)->value; | |
980 | int opt = 0; | |
913460fc | 981 | |
492d29ea | 982 | TRY |
913460fc PK |
983 | { |
984 | opt = value_lazy (value); | |
985 | } | |
492d29ea PA |
986 | CATCH (except, RETURN_MASK_ALL) |
987 | { | |
988 | GDB_PY_HANDLE_EXCEPTION (except); | |
989 | } | |
990 | END_CATCH | |
913460fc PK |
991 | |
992 | if (opt) | |
993 | Py_RETURN_TRUE; | |
994 | ||
995 | Py_RETURN_FALSE; | |
996 | } | |
997 | ||
998 | /* Implements gdb.Value.fetch_lazy (). */ | |
999 | static PyObject * | |
1000 | valpy_fetch_lazy (PyObject *self, PyObject *args) | |
1001 | { | |
1002 | struct value *value = ((value_object *) self)->value; | |
913460fc | 1003 | |
492d29ea | 1004 | TRY |
913460fc PK |
1005 | { |
1006 | if (value_lazy (value)) | |
1007 | value_fetch_lazy (value); | |
1008 | } | |
492d29ea PA |
1009 | CATCH (except, RETURN_MASK_ALL) |
1010 | { | |
1011 | GDB_PY_HANDLE_EXCEPTION (except); | |
1012 | } | |
1013 | END_CATCH | |
913460fc PK |
1014 | |
1015 | Py_RETURN_NONE; | |
1016 | } | |
1017 | ||
88d4aea7 PM |
1018 | /* Calculate and return the address of the PyObject as the value of |
1019 | the builtin __hash__ call. */ | |
881d5d5d | 1020 | static Py_hash_t |
88d4aea7 PM |
1021 | valpy_hash (PyObject *self) |
1022 | { | |
881d5d5d | 1023 | return (intptr_t) self; |
88d4aea7 PM |
1024 | } |
1025 | ||
7843261b TJB |
1026 | enum valpy_opcode |
1027 | { | |
1028 | VALPY_ADD, | |
1029 | VALPY_SUB, | |
1030 | VALPY_MUL, | |
1031 | VALPY_DIV, | |
1032 | VALPY_REM, | |
08c637de TJB |
1033 | VALPY_POW, |
1034 | VALPY_LSH, | |
1035 | VALPY_RSH, | |
1036 | VALPY_BITAND, | |
1037 | VALPY_BITOR, | |
1038 | VALPY_BITXOR | |
7843261b TJB |
1039 | }; |
1040 | ||
1041 | /* If TYPE is a reference, return the target; otherwise return TYPE. */ | |
1042 | #define STRIP_REFERENCE(TYPE) \ | |
1043 | ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE)) | |
1044 | ||
9c6595ab PA |
1045 | /* Helper for valpy_binop. Returns a value object which is the result |
1046 | of applying the operation specified by OPCODE to the given | |
1047 | arguments. Throws a GDB exception on error. */ | |
1048 | ||
7843261b | 1049 | static PyObject * |
9c6595ab | 1050 | valpy_binop_throw (enum valpy_opcode opcode, PyObject *self, PyObject *other) |
7843261b | 1051 | { |
888fe1e1 | 1052 | PyObject *result = NULL; |
7843261b | 1053 | |
9c6595ab | 1054 | struct value *arg1, *arg2; |
9c6595ab PA |
1055 | struct value *res_val = NULL; |
1056 | enum exp_opcode op = OP_NULL; | |
1057 | int handled = 0; | |
1058 | ||
eb115069 TT |
1059 | scoped_value_mark free_values; |
1060 | ||
9c6595ab PA |
1061 | /* If the gdb.Value object is the second operand, then it will be |
1062 | passed to us as the OTHER argument, and SELF will be an entirely | |
1063 | different kind of object, altogether. Because of this, we can't | |
1064 | assume self is a gdb.Value object and need to convert it from | |
1065 | python as well. */ | |
1066 | arg1 = convert_value_from_python (self); | |
1067 | if (arg1 == NULL) | |
eb115069 | 1068 | return NULL; |
08c637de | 1069 | |
9c6595ab PA |
1070 | arg2 = convert_value_from_python (other); |
1071 | if (arg2 == NULL) | |
eb115069 | 1072 | return NULL; |
7843261b | 1073 | |
9c6595ab PA |
1074 | switch (opcode) |
1075 | { | |
1076 | case VALPY_ADD: | |
1077 | { | |
1078 | struct type *ltype = value_type (arg1); | |
1079 | struct type *rtype = value_type (arg2); | |
1080 | ||
1081 | ltype = check_typedef (ltype); | |
1082 | ltype = STRIP_REFERENCE (ltype); | |
1083 | rtype = check_typedef (rtype); | |
1084 | rtype = STRIP_REFERENCE (rtype); | |
1085 | ||
1086 | handled = 1; | |
1087 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR | |
1088 | && is_integral_type (rtype)) | |
1089 | res_val = value_ptradd (arg1, value_as_long (arg2)); | |
1090 | else if (TYPE_CODE (rtype) == TYPE_CODE_PTR | |
1091 | && is_integral_type (ltype)) | |
1092 | res_val = value_ptradd (arg2, value_as_long (arg1)); | |
1093 | else | |
7843261b | 1094 | { |
9c6595ab PA |
1095 | handled = 0; |
1096 | op = BINOP_ADD; | |
7843261b | 1097 | } |
9c6595ab PA |
1098 | } |
1099 | break; | |
1100 | case VALPY_SUB: | |
1101 | { | |
1102 | struct type *ltype = value_type (arg1); | |
1103 | struct type *rtype = value_type (arg2); | |
1104 | ||
1105 | ltype = check_typedef (ltype); | |
1106 | ltype = STRIP_REFERENCE (ltype); | |
1107 | rtype = check_typedef (rtype); | |
1108 | rtype = STRIP_REFERENCE (rtype); | |
1109 | ||
1110 | handled = 1; | |
1111 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR | |
1112 | && TYPE_CODE (rtype) == TYPE_CODE_PTR) | |
1113 | /* A ptrdiff_t for the target would be preferable here. */ | |
1114 | res_val = value_from_longest (builtin_type_pyint, | |
1115 | value_ptrdiff (arg1, arg2)); | |
1116 | else if (TYPE_CODE (ltype) == TYPE_CODE_PTR | |
1117 | && is_integral_type (rtype)) | |
1118 | res_val = value_ptradd (arg1, - value_as_long (arg2)); | |
1119 | else | |
7843261b | 1120 | { |
9c6595ab PA |
1121 | handled = 0; |
1122 | op = BINOP_SUB; | |
7843261b | 1123 | } |
9c6595ab PA |
1124 | } |
1125 | break; | |
1126 | case VALPY_MUL: | |
1127 | op = BINOP_MUL; | |
1128 | break; | |
1129 | case VALPY_DIV: | |
1130 | op = BINOP_DIV; | |
1131 | break; | |
1132 | case VALPY_REM: | |
1133 | op = BINOP_REM; | |
1134 | break; | |
1135 | case VALPY_POW: | |
1136 | op = BINOP_EXP; | |
1137 | break; | |
1138 | case VALPY_LSH: | |
1139 | op = BINOP_LSH; | |
1140 | break; | |
1141 | case VALPY_RSH: | |
1142 | op = BINOP_RSH; | |
1143 | break; | |
1144 | case VALPY_BITAND: | |
1145 | op = BINOP_BITWISE_AND; | |
1146 | break; | |
1147 | case VALPY_BITOR: | |
1148 | op = BINOP_BITWISE_IOR; | |
1149 | break; | |
1150 | case VALPY_BITXOR: | |
1151 | op = BINOP_BITWISE_XOR; | |
1152 | break; | |
1153 | } | |
888fe1e1 | 1154 | |
9c6595ab PA |
1155 | if (!handled) |
1156 | { | |
1157 | if (binop_user_defined_p (op, arg1, arg2)) | |
1158 | res_val = value_x_binop (arg1, arg2, op, OP_NULL, EVAL_NORMAL); | |
1159 | else | |
1160 | res_val = value_binop (arg1, arg2, op); | |
1161 | } | |
f7bd0f78 | 1162 | |
9c6595ab PA |
1163 | if (res_val) |
1164 | result = value_to_value_object (res_val); | |
888fe1e1 | 1165 | |
9c6595ab PA |
1166 | return result; |
1167 | } | |
1168 | ||
1169 | /* Returns a value object which is the result of applying the operation | |
1170 | specified by OPCODE to the given arguments. Returns NULL on error, with | |
1171 | a python exception set. */ | |
1172 | static PyObject * | |
1173 | valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) | |
1174 | { | |
1175 | PyObject *result = NULL; | |
1176 | ||
1177 | TRY | |
1178 | { | |
1179 | result = valpy_binop_throw (opcode, self, other); | |
7843261b | 1180 | } |
492d29ea PA |
1181 | CATCH (except, RETURN_MASK_ALL) |
1182 | { | |
1183 | GDB_PY_HANDLE_EXCEPTION (except); | |
1184 | } | |
1185 | END_CATCH | |
7843261b | 1186 | |
888fe1e1 | 1187 | return result; |
7843261b TJB |
1188 | } |
1189 | ||
1190 | static PyObject * | |
1191 | valpy_add (PyObject *self, PyObject *other) | |
1192 | { | |
1193 | return valpy_binop (VALPY_ADD, self, other); | |
1194 | } | |
1195 | ||
1196 | static PyObject * | |
1197 | valpy_subtract (PyObject *self, PyObject *other) | |
1198 | { | |
1199 | return valpy_binop (VALPY_SUB, self, other); | |
1200 | } | |
1201 | ||
1202 | static PyObject * | |
1203 | valpy_multiply (PyObject *self, PyObject *other) | |
1204 | { | |
1205 | return valpy_binop (VALPY_MUL, self, other); | |
1206 | } | |
1207 | ||
1208 | static PyObject * | |
1209 | valpy_divide (PyObject *self, PyObject *other) | |
1210 | { | |
1211 | return valpy_binop (VALPY_DIV, self, other); | |
1212 | } | |
1213 | ||
1214 | static PyObject * | |
1215 | valpy_remainder (PyObject *self, PyObject *other) | |
1216 | { | |
1217 | return valpy_binop (VALPY_REM, self, other); | |
1218 | } | |
1219 | ||
1220 | static PyObject * | |
1221 | valpy_power (PyObject *self, PyObject *other, PyObject *unused) | |
1222 | { | |
1223 | /* We don't support the ternary form of pow. I don't know how to express | |
1224 | that, so let's just throw NotImplementedError to at least do something | |
1225 | about it. */ | |
1226 | if (unused != Py_None) | |
1227 | { | |
1228 | PyErr_SetString (PyExc_NotImplementedError, | |
1229 | "Invalid operation on gdb.Value."); | |
1230 | return NULL; | |
1231 | } | |
1232 | ||
1233 | return valpy_binop (VALPY_POW, self, other); | |
1234 | } | |
1235 | ||
1236 | static PyObject * | |
1237 | valpy_negative (PyObject *self) | |
1238 | { | |
888fe1e1 | 1239 | PyObject *result = NULL; |
7843261b | 1240 | |
492d29ea | 1241 | TRY |
7843261b | 1242 | { |
888fe1e1 | 1243 | /* Perhaps overkill, but consistency has some virtue. */ |
eb115069 | 1244 | scoped_value_mark free_values; |
888fe1e1 TT |
1245 | struct value *val; |
1246 | ||
7843261b | 1247 | val = value_neg (((value_object *) self)->value); |
888fe1e1 | 1248 | result = value_to_value_object (val); |
7843261b | 1249 | } |
492d29ea PA |
1250 | CATCH (except, RETURN_MASK_ALL) |
1251 | { | |
1252 | GDB_PY_HANDLE_EXCEPTION (except); | |
1253 | } | |
1254 | END_CATCH | |
7843261b | 1255 | |
888fe1e1 | 1256 | return result; |
7843261b TJB |
1257 | } |
1258 | ||
1259 | static PyObject * | |
1260 | valpy_positive (PyObject *self) | |
1261 | { | |
4e7a5ef5 | 1262 | return value_to_value_object (((value_object *) self)->value); |
7843261b TJB |
1263 | } |
1264 | ||
1265 | static PyObject * | |
1266 | valpy_absolute (PyObject *self) | |
1267 | { | |
22601c15 | 1268 | struct value *value = ((value_object *) self)->value; |
f287c1f3 | 1269 | int isabs = 1; |
d59b6f6c | 1270 | |
492d29ea | 1271 | TRY |
f287c1f3 | 1272 | { |
eb115069 | 1273 | scoped_value_mark free_values; |
888fe1e1 | 1274 | |
f287c1f3 PM |
1275 | if (value_less (value, value_zero (value_type (value), not_lval))) |
1276 | isabs = 0; | |
1277 | } | |
492d29ea PA |
1278 | CATCH (except, RETURN_MASK_ALL) |
1279 | { | |
1280 | GDB_PY_HANDLE_EXCEPTION (except); | |
1281 | } | |
1282 | END_CATCH | |
f287c1f3 PM |
1283 | |
1284 | if (isabs) | |
7843261b | 1285 | return valpy_positive (self); |
f287c1f3 PM |
1286 | else |
1287 | return valpy_negative (self); | |
7843261b TJB |
1288 | } |
1289 | ||
1290 | /* Implements boolean evaluation of gdb.Value. */ | |
1291 | static int | |
1292 | valpy_nonzero (PyObject *self) | |
1293 | { | |
492d29ea | 1294 | struct gdb_exception except = exception_none; |
7843261b TJB |
1295 | value_object *self_value = (value_object *) self; |
1296 | struct type *type; | |
f287c1f3 | 1297 | int nonzero = 0; /* Appease GCC warning. */ |
7843261b | 1298 | |
492d29ea | 1299 | TRY |
f287c1f3 | 1300 | { |
5d9c5995 PM |
1301 | type = check_typedef (value_type (self_value->value)); |
1302 | ||
f287c1f3 PM |
1303 | if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR) |
1304 | nonzero = !!value_as_long (self_value->value); | |
1305 | else if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1306 | nonzero = value_as_double (self_value->value) != 0; | |
1307 | else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1308 | nonzero = !decimal_is_zero (value_contents (self_value->value), | |
1309 | TYPE_LENGTH (type), | |
1310 | gdbarch_byte_order (get_type_arch (type))); | |
1311 | else | |
1312 | /* All other values are True. */ | |
1313 | nonzero = 1; | |
1314 | } | |
492d29ea PA |
1315 | CATCH (ex, RETURN_MASK_ALL) |
1316 | { | |
1317 | except = ex; | |
1318 | } | |
1319 | END_CATCH | |
1320 | ||
f287c1f3 PM |
1321 | /* This is not documented in the Python documentation, but if this |
1322 | function fails, return -1 as slot_nb_nonzero does (the default | |
1323 | Python nonzero function). */ | |
1324 | GDB_PY_SET_HANDLE_EXCEPTION (except); | |
1325 | ||
1326 | return nonzero; | |
7843261b TJB |
1327 | } |
1328 | ||
08c637de | 1329 | /* Implements ~ for value objects. */ |
7843261b | 1330 | static PyObject * |
08c637de | 1331 | valpy_invert (PyObject *self) |
7843261b | 1332 | { |
08c637de | 1333 | struct value *val = NULL; |
7843261b | 1334 | |
492d29ea | 1335 | TRY |
7843261b | 1336 | { |
08c637de TJB |
1337 | val = value_complement (((value_object *) self)->value); |
1338 | } | |
492d29ea PA |
1339 | CATCH (except, RETURN_MASK_ALL) |
1340 | { | |
1341 | GDB_PY_HANDLE_EXCEPTION (except); | |
1342 | } | |
1343 | END_CATCH | |
7843261b | 1344 | |
08c637de TJB |
1345 | return value_to_value_object (val); |
1346 | } | |
7843261b | 1347 | |
08c637de TJB |
1348 | /* Implements left shift for value objects. */ |
1349 | static PyObject * | |
1350 | valpy_lsh (PyObject *self, PyObject *other) | |
1351 | { | |
1352 | return valpy_binop (VALPY_LSH, self, other); | |
1353 | } | |
7843261b | 1354 | |
08c637de TJB |
1355 | /* Implements right shift for value objects. */ |
1356 | static PyObject * | |
1357 | valpy_rsh (PyObject *self, PyObject *other) | |
1358 | { | |
1359 | return valpy_binop (VALPY_RSH, self, other); | |
1360 | } | |
7843261b | 1361 | |
08c637de TJB |
1362 | /* Implements bitwise and for value objects. */ |
1363 | static PyObject * | |
1364 | valpy_and (PyObject *self, PyObject *other) | |
1365 | { | |
1366 | return valpy_binop (VALPY_BITAND, self, other); | |
1367 | } | |
7843261b | 1368 | |
08c637de TJB |
1369 | /* Implements bitwise or for value objects. */ |
1370 | static PyObject * | |
1371 | valpy_or (PyObject *self, PyObject *other) | |
1372 | { | |
1373 | return valpy_binop (VALPY_BITOR, self, other); | |
1374 | } | |
1375 | ||
1376 | /* Implements bitwise xor for value objects. */ | |
1377 | static PyObject * | |
1378 | valpy_xor (PyObject *self, PyObject *other) | |
1379 | { | |
1380 | return valpy_binop (VALPY_BITXOR, self, other); | |
1381 | } | |
1382 | ||
9c6595ab PA |
1383 | /* Helper for valpy_richcompare. Implements comparison operations for |
1384 | value objects. Returns true/false on success. Returns -1 with a | |
1385 | Python exception set if a Python error is detected. Throws a GDB | |
1386 | exception on other errors (memory error, etc.). */ | |
1387 | ||
1388 | static int | |
1389 | valpy_richcompare_throw (PyObject *self, PyObject *other, int op) | |
1390 | { | |
1391 | int result; | |
1392 | struct value *value_other; | |
1393 | struct value *value_self; | |
9c6595ab | 1394 | |
eb115069 TT |
1395 | scoped_value_mark free_values; |
1396 | ||
9c6595ab PA |
1397 | value_other = convert_value_from_python (other); |
1398 | if (value_other == NULL) | |
1399 | return -1; | |
1400 | ||
9c6595ab PA |
1401 | value_self = ((value_object *) self)->value; |
1402 | ||
1403 | switch (op) | |
1404 | { | |
1405 | case Py_LT: | |
1406 | result = value_less (value_self, value_other); | |
1407 | break; | |
1408 | case Py_LE: | |
1409 | result = value_less (value_self, value_other) | |
1410 | || value_equal (value_self, value_other); | |
1411 | break; | |
1412 | case Py_EQ: | |
1413 | result = value_equal (value_self, value_other); | |
1414 | break; | |
1415 | case Py_NE: | |
1416 | result = !value_equal (value_self, value_other); | |
1417 | break; | |
1418 | case Py_GT: | |
1419 | result = value_less (value_other, value_self); | |
1420 | break; | |
1421 | case Py_GE: | |
1422 | result = (value_less (value_other, value_self) | |
1423 | || value_equal (value_self, value_other)); | |
1424 | break; | |
1425 | default: | |
1426 | /* Can't happen. */ | |
1427 | PyErr_SetString (PyExc_NotImplementedError, | |
1428 | _("Invalid operation on gdb.Value.")); | |
1429 | result = -1; | |
1430 | break; | |
1431 | } | |
1432 | ||
9c6595ab PA |
1433 | return result; |
1434 | } | |
1435 | ||
1436 | ||
8dc78533 JK |
1437 | /* Implements comparison operations for value objects. Returns NULL on error, |
1438 | with a python exception set. */ | |
08c637de TJB |
1439 | static PyObject * |
1440 | valpy_richcompare (PyObject *self, PyObject *other, int op) | |
1441 | { | |
1442 | int result = 0; | |
08c637de TJB |
1443 | |
1444 | if (other == Py_None) | |
7843261b TJB |
1445 | /* Comparing with None is special. From what I can tell, in Python |
1446 | None is smaller than anything else. */ | |
1447 | switch (op) { | |
1448 | case Py_LT: | |
1449 | case Py_LE: | |
1450 | case Py_EQ: | |
1451 | Py_RETURN_FALSE; | |
1452 | case Py_NE: | |
1453 | case Py_GT: | |
1454 | case Py_GE: | |
1455 | Py_RETURN_TRUE; | |
1456 | default: | |
1457 | /* Can't happen. */ | |
1458 | PyErr_SetString (PyExc_NotImplementedError, | |
044c0f87 | 1459 | _("Invalid operation on gdb.Value.")); |
7843261b TJB |
1460 | return NULL; |
1461 | } | |
7843261b | 1462 | |
492d29ea | 1463 | TRY |
7843261b | 1464 | { |
9c6595ab | 1465 | result = valpy_richcompare_throw (self, other, op); |
7843261b | 1466 | } |
492d29ea PA |
1467 | CATCH (except, RETURN_MASK_ALL) |
1468 | { | |
1469 | GDB_PY_HANDLE_EXCEPTION (except); | |
1470 | } | |
1471 | END_CATCH | |
7843261b | 1472 | |
f02779d8 TT |
1473 | /* In this case, the Python exception has already been set. */ |
1474 | if (result < 0) | |
1475 | return NULL; | |
1476 | ||
7843261b TJB |
1477 | if (result == 1) |
1478 | Py_RETURN_TRUE; | |
1479 | ||
1480 | Py_RETURN_FALSE; | |
1481 | } | |
1482 | ||
9a27f2c6 | 1483 | #ifndef IS_PY3K |
08c637de TJB |
1484 | /* Implements conversion to int. */ |
1485 | static PyObject * | |
1486 | valpy_int (PyObject *self) | |
1487 | { | |
1488 | struct value *value = ((value_object *) self)->value; | |
1489 | struct type *type = value_type (value); | |
1490 | LONGEST l = 0; | |
08c637de | 1491 | |
492d29ea | 1492 | TRY |
08c637de | 1493 | { |
7c245c24 | 1494 | if (!is_integral_type (type)) |
f287c1f3 PM |
1495 | error (_("Cannot convert value to int.")); |
1496 | ||
08c637de TJB |
1497 | l = value_as_long (value); |
1498 | } | |
492d29ea PA |
1499 | CATCH (except, RETURN_MASK_ALL) |
1500 | { | |
1501 | GDB_PY_HANDLE_EXCEPTION (except); | |
1502 | } | |
1503 | END_CATCH | |
08c637de | 1504 | |
74aedc46 | 1505 | return gdb_py_object_from_longest (l); |
08c637de | 1506 | } |
9a27f2c6 | 1507 | #endif |
08c637de TJB |
1508 | |
1509 | /* Implements conversion to long. */ | |
1510 | static PyObject * | |
1511 | valpy_long (PyObject *self) | |
1512 | { | |
1513 | struct value *value = ((value_object *) self)->value; | |
1514 | struct type *type = value_type (value); | |
1515 | LONGEST l = 0; | |
08c637de | 1516 | |
492d29ea | 1517 | TRY |
08c637de | 1518 | { |
f168693b | 1519 | type = check_typedef (type); |
f287c1f3 | 1520 | |
7c245c24 JB |
1521 | if (!is_integral_type (type) |
1522 | && TYPE_CODE (type) != TYPE_CODE_PTR) | |
f287c1f3 PM |
1523 | error (_("Cannot convert value to long.")); |
1524 | ||
08c637de TJB |
1525 | l = value_as_long (value); |
1526 | } | |
492d29ea PA |
1527 | CATCH (except, RETURN_MASK_ALL) |
1528 | { | |
1529 | GDB_PY_HANDLE_EXCEPTION (except); | |
1530 | } | |
1531 | END_CATCH | |
08c637de | 1532 | |
33fa2c6e DE |
1533 | if (TYPE_UNSIGNED (type)) |
1534 | return gdb_py_long_from_ulongest (l); | |
1535 | else | |
1536 | return gdb_py_long_from_longest (l); | |
08c637de TJB |
1537 | } |
1538 | ||
1539 | /* Implements conversion to float. */ | |
1540 | static PyObject * | |
1541 | valpy_float (PyObject *self) | |
1542 | { | |
1543 | struct value *value = ((value_object *) self)->value; | |
1544 | struct type *type = value_type (value); | |
1545 | double d = 0; | |
08c637de | 1546 | |
492d29ea | 1547 | TRY |
08c637de | 1548 | { |
f168693b | 1549 | type = check_typedef (type); |
f287c1f3 PM |
1550 | |
1551 | if (TYPE_CODE (type) != TYPE_CODE_FLT) | |
1552 | error (_("Cannot convert value to float.")); | |
1553 | ||
08c637de TJB |
1554 | d = value_as_double (value); |
1555 | } | |
492d29ea PA |
1556 | CATCH (except, RETURN_MASK_ALL) |
1557 | { | |
1558 | GDB_PY_HANDLE_EXCEPTION (except); | |
1559 | } | |
1560 | END_CATCH | |
08c637de TJB |
1561 | |
1562 | return PyFloat_FromDouble (d); | |
1563 | } | |
1564 | ||
7843261b TJB |
1565 | /* Returns an object for a value which is released from the all_values chain, |
1566 | so its lifetime is not bound to the execution of a command. */ | |
1567 | PyObject * | |
1568 | value_to_value_object (struct value *val) | |
1569 | { | |
1570 | value_object *val_obj; | |
1571 | ||
1572 | val_obj = PyObject_New (value_object, &value_object_type); | |
1573 | if (val_obj != NULL) | |
1574 | { | |
1575 | val_obj->value = val; | |
e848a8a5 | 1576 | release_value_or_incref (val); |
c0c6f777 | 1577 | val_obj->address = NULL; |
2c74e833 | 1578 | val_obj->type = NULL; |
03f17ccf | 1579 | val_obj->dynamic_type = NULL; |
4e7a5ef5 | 1580 | note_value (val_obj); |
7843261b TJB |
1581 | } |
1582 | ||
1583 | return (PyObject *) val_obj; | |
1584 | } | |
1585 | ||
4e7a5ef5 TT |
1586 | /* Returns a borrowed reference to the struct value corresponding to |
1587 | the given value object. */ | |
a6bac58e TT |
1588 | struct value * |
1589 | value_object_to_value (PyObject *self) | |
1590 | { | |
1591 | value_object *real; | |
d59b6f6c | 1592 | |
a6bac58e TT |
1593 | if (! PyObject_TypeCheck (self, &value_object_type)) |
1594 | return NULL; | |
1595 | real = (value_object *) self; | |
1596 | return real->value; | |
1597 | } | |
1598 | ||
7843261b | 1599 | /* Try to convert a Python value to a gdb value. If the value cannot |
4e7a5ef5 TT |
1600 | be converted, set a Python exception and return NULL. Returns a |
1601 | reference to a new value on the all_values chain. */ | |
7843261b TJB |
1602 | |
1603 | struct value * | |
1604 | convert_value_from_python (PyObject *obj) | |
1605 | { | |
1606 | struct value *value = NULL; /* -Wall */ | |
08c637de | 1607 | int cmp; |
7843261b | 1608 | |
08c637de | 1609 | gdb_assert (obj != NULL); |
7843261b | 1610 | |
492d29ea | 1611 | TRY |
7843261b | 1612 | { |
256458bc | 1613 | if (PyBool_Check (obj)) |
08c637de TJB |
1614 | { |
1615 | cmp = PyObject_IsTrue (obj); | |
1616 | if (cmp >= 0) | |
1617 | value = value_from_longest (builtin_type_pybool, cmp); | |
1618 | } | |
06ab7b19 PM |
1619 | /* Make a long logic check first. In Python 3.x, internally, |
1620 | all integers are represented as longs. In Python 2.x, there | |
1621 | is still a differentiation internally between a PyInt and a | |
1622 | PyLong. Explicitly do this long check conversion first. In | |
1623 | GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has | |
1624 | to be done first to ensure we do not lose information in the | |
1625 | conversion process. */ | |
08c637de TJB |
1626 | else if (PyLong_Check (obj)) |
1627 | { | |
1628 | LONGEST l = PyLong_AsLongLong (obj); | |
7843261b | 1629 | |
595939de PM |
1630 | if (PyErr_Occurred ()) |
1631 | { | |
1632 | /* If the error was an overflow, we can try converting to | |
1633 | ULONGEST instead. */ | |
1634 | if (PyErr_ExceptionMatches (PyExc_OverflowError)) | |
1635 | { | |
53a0cca3 | 1636 | PyObject *etype, *evalue, *etraceback; |
595939de PM |
1637 | |
1638 | PyErr_Fetch (&etype, &evalue, &etraceback); | |
7780f186 | 1639 | gdbpy_ref<> zero (PyInt_FromLong (0)); |
595939de PM |
1640 | |
1641 | /* Check whether obj is positive. */ | |
53a0cca3 | 1642 | if (PyObject_RichCompareBool (obj, zero.get (), Py_GT) > 0) |
595939de PM |
1643 | { |
1644 | ULONGEST ul; | |
1645 | ||
1646 | ul = PyLong_AsUnsignedLongLong (obj); | |
1647 | if (! PyErr_Occurred ()) | |
1648 | value = value_from_ulongest (builtin_type_upylong, ul); | |
1649 | } | |
1650 | else | |
1651 | /* There's nothing we can do. */ | |
1652 | PyErr_Restore (etype, evalue, etraceback); | |
595939de PM |
1653 | } |
1654 | } | |
1655 | else | |
08c637de TJB |
1656 | value = value_from_longest (builtin_type_pylong, l); |
1657 | } | |
12c58cd4 | 1658 | #if PY_MAJOR_VERSION == 2 |
06ab7b19 PM |
1659 | else if (PyInt_Check (obj)) |
1660 | { | |
1661 | long l = PyInt_AsLong (obj); | |
1662 | ||
1663 | if (! PyErr_Occurred ()) | |
1664 | value = value_from_longest (builtin_type_pyint, l); | |
1665 | } | |
12c58cd4 | 1666 | #endif |
08c637de TJB |
1667 | else if (PyFloat_Check (obj)) |
1668 | { | |
1669 | double d = PyFloat_AsDouble (obj); | |
7843261b | 1670 | |
08c637de TJB |
1671 | if (! PyErr_Occurred ()) |
1672 | value = value_from_double (builtin_type_pyfloat, d); | |
1673 | } | |
1674 | else if (gdbpy_is_string (obj)) | |
1675 | { | |
9b972014 TT |
1676 | gdb::unique_xmalloc_ptr<char> s |
1677 | = python_string_to_target_string (obj); | |
08c637de | 1678 | if (s != NULL) |
9b972014 TT |
1679 | value = value_cstring (s.get (), strlen (s.get ()), |
1680 | builtin_type_pychar); | |
08c637de TJB |
1681 | } |
1682 | else if (PyObject_TypeCheck (obj, &value_object_type)) | |
cc924cad | 1683 | value = value_copy (((value_object *) obj)->value); |
be759fcf PM |
1684 | else if (gdbpy_is_lazy_string (obj)) |
1685 | { | |
1686 | PyObject *result; | |
d59b6f6c | 1687 | |
fb6a3ed3 | 1688 | result = PyObject_CallMethodObjArgs (obj, gdbpy_value_cst, NULL); |
be759fcf PM |
1689 | value = value_copy (((value_object *) result)->value); |
1690 | } | |
08c637de | 1691 | else |
9a27f2c6 PK |
1692 | #ifdef IS_PY3K |
1693 | PyErr_Format (PyExc_TypeError, | |
1694 | _("Could not convert Python object: %S."), obj); | |
1695 | #else | |
9a2b4c1b MS |
1696 | PyErr_Format (PyExc_TypeError, |
1697 | _("Could not convert Python object: %s."), | |
08c637de | 1698 | PyString_AsString (PyObject_Str (obj))); |
9a27f2c6 | 1699 | #endif |
08c637de | 1700 | } |
492d29ea | 1701 | CATCH (except, RETURN_MASK_ALL) |
08c637de TJB |
1702 | { |
1703 | PyErr_Format (except.reason == RETURN_QUIT | |
1704 | ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, | |
1705 | "%s", except.message); | |
1706 | return NULL; | |
1707 | } | |
492d29ea | 1708 | END_CATCH |
7843261b TJB |
1709 | |
1710 | return value; | |
1711 | } | |
1712 | ||
1713 | /* Returns value object in the ARGth position in GDB's history. */ | |
1714 | PyObject * | |
08c637de | 1715 | gdbpy_history (PyObject *self, PyObject *args) |
7843261b TJB |
1716 | { |
1717 | int i; | |
1718 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
7843261b TJB |
1719 | |
1720 | if (!PyArg_ParseTuple (args, "i", &i)) | |
1721 | return NULL; | |
1722 | ||
492d29ea | 1723 | TRY |
7843261b TJB |
1724 | { |
1725 | res_val = access_value_history (i); | |
1726 | } | |
492d29ea PA |
1727 | CATCH (except, RETURN_MASK_ALL) |
1728 | { | |
1729 | GDB_PY_HANDLE_EXCEPTION (except); | |
1730 | } | |
1731 | END_CATCH | |
7843261b TJB |
1732 | |
1733 | return value_to_value_object (res_val); | |
1734 | } | |
1735 | ||
595939de PM |
1736 | /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */ |
1737 | ||
1738 | int | |
1739 | gdbpy_is_value_object (PyObject *obj) | |
1740 | { | |
1741 | return PyObject_TypeCheck (obj, &value_object_type); | |
1742 | } | |
1743 | ||
999633ed | 1744 | int |
7843261b TJB |
1745 | gdbpy_initialize_values (void) |
1746 | { | |
7843261b | 1747 | if (PyType_Ready (&value_object_type) < 0) |
999633ed | 1748 | return -1; |
7843261b | 1749 | |
aa36459a TT |
1750 | return gdb_pymodule_addobject (gdb_module, "Value", |
1751 | (PyObject *) &value_object_type); | |
7843261b TJB |
1752 | } |
1753 | ||
2c74e833 TT |
1754 | \f |
1755 | ||
def2b000 | 1756 | static PyGetSetDef value_object_getset[] = { |
c0c6f777 TJB |
1757 | { "address", valpy_get_address, NULL, "The address of the value.", |
1758 | NULL }, | |
def2b000 | 1759 | { "is_optimized_out", valpy_get_is_optimized_out, NULL, |
9a2b4c1b MS |
1760 | "Boolean telling whether the value is optimized " |
1761 | "out (i.e., not available).", | |
def2b000 | 1762 | NULL }, |
2c74e833 | 1763 | { "type", valpy_get_type, NULL, "Type of the value.", NULL }, |
03f17ccf TT |
1764 | { "dynamic_type", valpy_get_dynamic_type, NULL, |
1765 | "Dynamic type of the value.", NULL }, | |
913460fc PK |
1766 | { "is_lazy", valpy_get_is_lazy, NULL, |
1767 | "Boolean telling whether the value is lazy (not fetched yet\n\ | |
1768 | from the inferior). A lazy value is fetched when needed, or when\n\ | |
1769 | the \"fetch_lazy()\" method is called.", NULL }, | |
def2b000 TJB |
1770 | {NULL} /* Sentinel */ |
1771 | }; | |
1772 | ||
f9176c46 | 1773 | static PyMethodDef value_object_methods[] = { |
2c74e833 | 1774 | { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." }, |
f9ffd4bb TT |
1775 | { "dynamic_cast", valpy_dynamic_cast, METH_VARARGS, |
1776 | "dynamic_cast (gdb.Type) -> gdb.Value\n\ | |
1777 | Cast the value to the supplied type, as if by the C++ dynamic_cast operator." | |
1778 | }, | |
1779 | { "reinterpret_cast", valpy_reinterpret_cast, METH_VARARGS, | |
1780 | "reinterpret_cast (gdb.Type) -> gdb.Value\n\ | |
1781 | Cast the value to the supplied type, as if by the C++\n\ | |
1782 | reinterpret_cast operator." | |
1783 | }, | |
f9176c46 | 1784 | { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, |
7b282c5a SCR |
1785 | { "referenced_value", valpy_referenced_value, METH_NOARGS, |
1786 | "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." }, | |
4c082a81 SC |
1787 | { "reference_value", valpy_reference_value, METH_NOARGS, |
1788 | "Return a value of type TYPE_CODE_REF referencing this value." }, | |
1789 | { "const_value", valpy_const_value, METH_NOARGS, | |
1790 | "Return a 'const' qualied version of the same value." }, | |
9a2b4c1b MS |
1791 | { "lazy_string", (PyCFunction) valpy_lazy_string, |
1792 | METH_VARARGS | METH_KEYWORDS, | |
be759fcf PM |
1793 | "lazy_string ([encoding] [, length]) -> lazy_string\n\ |
1794 | Return a lazy string representation of the value." }, | |
cc924cad | 1795 | { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS, |
fbb8f299 | 1796 | "string ([encoding] [, errors] [, length]) -> string\n\ |
cc924cad | 1797 | Return Unicode string representation of the value." }, |
256458bc | 1798 | { "fetch_lazy", valpy_fetch_lazy, METH_NOARGS, |
913460fc | 1799 | "Fetches the value from the inferior, if it was lazy." }, |
f9176c46 PA |
1800 | {NULL} /* Sentinel */ |
1801 | }; | |
1802 | ||
1803 | static PyNumberMethods value_object_as_number = { | |
1804 | valpy_add, | |
1805 | valpy_subtract, | |
1806 | valpy_multiply, | |
9a27f2c6 | 1807 | #ifndef IS_PY3K |
f9176c46 | 1808 | valpy_divide, |
9a27f2c6 | 1809 | #endif |
f9176c46 PA |
1810 | valpy_remainder, |
1811 | NULL, /* nb_divmod */ | |
1812 | valpy_power, /* nb_power */ | |
1813 | valpy_negative, /* nb_negative */ | |
1814 | valpy_positive, /* nb_positive */ | |
1815 | valpy_absolute, /* nb_absolute */ | |
08c637de TJB |
1816 | valpy_nonzero, /* nb_nonzero */ |
1817 | valpy_invert, /* nb_invert */ | |
1818 | valpy_lsh, /* nb_lshift */ | |
1819 | valpy_rsh, /* nb_rshift */ | |
1820 | valpy_and, /* nb_and */ | |
1821 | valpy_xor, /* nb_xor */ | |
1822 | valpy_or, /* nb_or */ | |
9a27f2c6 PK |
1823 | #ifdef IS_PY3K |
1824 | valpy_long, /* nb_int */ | |
1825 | NULL, /* reserved */ | |
1826 | #else | |
08c637de TJB |
1827 | NULL, /* nb_coerce */ |
1828 | valpy_int, /* nb_int */ | |
1829 | valpy_long, /* nb_long */ | |
9a27f2c6 | 1830 | #endif |
08c637de | 1831 | valpy_float, /* nb_float */ |
9a27f2c6 | 1832 | #ifndef IS_PY3K |
08c637de | 1833 | NULL, /* nb_oct */ |
9a27f2c6 PK |
1834 | NULL, /* nb_hex */ |
1835 | #endif | |
1836 | NULL, /* nb_inplace_add */ | |
1837 | NULL, /* nb_inplace_subtract */ | |
1838 | NULL, /* nb_inplace_multiply */ | |
e2b7f516 TT |
1839 | #ifndef IS_PY3K |
1840 | NULL, /* nb_inplace_divide */ | |
1841 | #endif | |
9a27f2c6 PK |
1842 | NULL, /* nb_inplace_remainder */ |
1843 | NULL, /* nb_inplace_power */ | |
1844 | NULL, /* nb_inplace_lshift */ | |
1845 | NULL, /* nb_inplace_rshift */ | |
1846 | NULL, /* nb_inplace_and */ | |
1847 | NULL, /* nb_inplace_xor */ | |
1848 | NULL, /* nb_inplace_or */ | |
1849 | NULL, /* nb_floor_divide */ | |
ddae9462 TT |
1850 | valpy_divide, /* nb_true_divide */ |
1851 | NULL, /* nb_inplace_floor_divide */ | |
1852 | NULL, /* nb_inplace_true_divide */ | |
7bd787e8 | 1853 | #ifndef HAVE_LIBPYTHON2_4 |
ddae9462 TT |
1854 | /* This was added in Python 2.5. */ |
1855 | valpy_long, /* nb_index */ | |
7bd787e8 | 1856 | #endif /* HAVE_LIBPYTHON2_4 */ |
f9176c46 PA |
1857 | }; |
1858 | ||
1859 | static PyMappingMethods value_object_as_mapping = { | |
1860 | valpy_length, | |
1861 | valpy_getitem, | |
1862 | valpy_setitem | |
1863 | }; | |
1864 | ||
1865 | PyTypeObject value_object_type = { | |
9a27f2c6 | 1866 | PyVarObject_HEAD_INIT (NULL, 0) |
f9176c46 PA |
1867 | "gdb.Value", /*tp_name*/ |
1868 | sizeof (value_object), /*tp_basicsize*/ | |
1869 | 0, /*tp_itemsize*/ | |
1870 | valpy_dealloc, /*tp_dealloc*/ | |
1871 | 0, /*tp_print*/ | |
1872 | 0, /*tp_getattr*/ | |
1873 | 0, /*tp_setattr*/ | |
1874 | 0, /*tp_compare*/ | |
1875 | 0, /*tp_repr*/ | |
1876 | &value_object_as_number, /*tp_as_number*/ | |
1877 | 0, /*tp_as_sequence*/ | |
1878 | &value_object_as_mapping, /*tp_as_mapping*/ | |
88d4aea7 | 1879 | valpy_hash, /*tp_hash*/ |
5374244e | 1880 | valpy_call, /*tp_call*/ |
f9176c46 PA |
1881 | valpy_str, /*tp_str*/ |
1882 | 0, /*tp_getattro*/ | |
1883 | 0, /*tp_setattro*/ | |
1884 | 0, /*tp_as_buffer*/ | |
9a2b4c1b MS |
1885 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
1886 | | Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
f9176c46 PA |
1887 | "GDB value object", /* tp_doc */ |
1888 | 0, /* tp_traverse */ | |
1889 | 0, /* tp_clear */ | |
1890 | valpy_richcompare, /* tp_richcompare */ | |
1891 | 0, /* tp_weaklistoffset */ | |
1892 | 0, /* tp_iter */ | |
1893 | 0, /* tp_iternext */ | |
08c637de TJB |
1894 | value_object_methods, /* tp_methods */ |
1895 | 0, /* tp_members */ | |
def2b000 | 1896 | value_object_getset, /* tp_getset */ |
08c637de TJB |
1897 | 0, /* tp_base */ |
1898 | 0, /* tp_dict */ | |
1899 | 0, /* tp_descr_get */ | |
1900 | 0, /* tp_descr_set */ | |
1901 | 0, /* tp_dictoffset */ | |
1902 | 0, /* tp_init */ | |
1903 | 0, /* tp_alloc */ | |
1904 | valpy_new /* tp_new */ | |
f9176c46 | 1905 | }; |