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