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