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