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