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