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