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