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