]>
Commit | Line | Data |
---|---|---|
2c74e833 TT |
1 | /* Python interface to types. |
2 | ||
4a94e368 | 3 | Copyright (C) 2008-2022 Free Software Foundation, Inc. |
2c74e833 TT |
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 "value.h" | |
2c74e833 TT |
22 | #include "python-internal.h" |
23 | #include "charset.h" | |
24 | #include "gdbtypes.h" | |
25 | #include "cp-support.h" | |
26 | #include "demangle.h" | |
27 | #include "objfiles.h" | |
e6c014f2 | 28 | #include "language.h" |
53342f27 | 29 | #include "typeprint.h" |
67470e9d | 30 | #include "ada-lang.h" |
2c74e833 | 31 | |
f99b5177 | 32 | struct type_object |
2c74e833 TT |
33 | { |
34 | PyObject_HEAD | |
35 | struct type *type; | |
36 | ||
37 | /* If a Type object is associated with an objfile, it is kept on a | |
38 | doubly-linked list, rooted in the objfile. This lets us copy the | |
39 | underlying struct type when the objfile is deleted. */ | |
f99b5177 TT |
40 | struct type_object *prev; |
41 | struct type_object *next; | |
42 | }; | |
2c74e833 | 43 | |
e36122e9 | 44 | extern PyTypeObject type_object_type |
62eec1a5 | 45 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("type_object"); |
2c74e833 TT |
46 | |
47 | /* A Field object. */ | |
f99b5177 | 48 | struct field_object |
2c74e833 TT |
49 | { |
50 | PyObject_HEAD | |
51 | ||
52 | /* Dictionary holding our attributes. */ | |
53 | PyObject *dict; | |
f99b5177 | 54 | }; |
2c74e833 | 55 | |
e36122e9 | 56 | extern PyTypeObject field_object_type |
62eec1a5 | 57 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("field_object"); |
2c74e833 | 58 | |
a73bb892 | 59 | /* A type iterator object. */ |
f99b5177 | 60 | struct typy_iterator_object { |
a73bb892 PK |
61 | PyObject_HEAD |
62 | /* The current field index. */ | |
63 | int field; | |
64 | /* What to return. */ | |
65 | enum gdbpy_iter_kind kind; | |
66 | /* Pointer back to the original source type object. */ | |
f99b5177 TT |
67 | type_object *source; |
68 | }; | |
a73bb892 | 69 | |
e36122e9 | 70 | extern PyTypeObject type_iterator_object_type |
62eec1a5 | 71 | CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("typy_iterator_object"); |
a73bb892 | 72 | |
2c74e833 TT |
73 | /* This is used to initialize various gdb.TYPE_ constants. */ |
74 | struct pyty_code | |
75 | { | |
76 | /* The code. */ | |
4881fcd7 | 77 | int code; |
2c74e833 TT |
78 | /* The name. */ |
79 | const char *name; | |
80 | }; | |
81 | ||
0dab82e9 PK |
82 | /* Forward declarations. */ |
83 | static PyObject *typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind); | |
84 | ||
2c74e833 TT |
85 | static struct pyty_code pyty_codes[] = |
86 | { | |
4881fcd7 TT |
87 | /* This is kept for backward compatibility. */ |
88 | { -1, "TYPE_CODE_BITSTRING" }, | |
89 | ||
90 | #define OP(X) { X, #X }, | |
91 | #include "type-codes.def" | |
92 | #undef OP | |
2c74e833 TT |
93 | }; |
94 | ||
95 | \f | |
96 | ||
97 | static void | |
98 | field_dealloc (PyObject *obj) | |
99 | { | |
100 | field_object *f = (field_object *) obj; | |
d59b6f6c | 101 | |
2c74e833 | 102 | Py_XDECREF (f->dict); |
9a27f2c6 | 103 | Py_TYPE (obj)->tp_free (obj); |
2c74e833 TT |
104 | } |
105 | ||
106 | static PyObject * | |
107 | field_new (void) | |
108 | { | |
88b6faea TT |
109 | gdbpy_ref<field_object> result (PyObject_New (field_object, |
110 | &field_object_type)); | |
d59b6f6c | 111 | |
88b6faea | 112 | if (result != NULL) |
2c74e833 TT |
113 | { |
114 | result->dict = PyDict_New (); | |
115 | if (!result->dict) | |
88b6faea | 116 | return NULL; |
2c74e833 | 117 | } |
88b6faea | 118 | return (PyObject *) result.release (); |
2c74e833 TT |
119 | } |
120 | ||
121 | \f | |
122 | ||
a16b0e22 SC |
123 | /* Return true if OBJ is of type gdb.Field, false otherwise. */ |
124 | ||
125 | int | |
126 | gdbpy_is_field (PyObject *obj) | |
127 | { | |
128 | return PyObject_TypeCheck (obj, &field_object_type); | |
129 | } | |
130 | ||
2c74e833 TT |
131 | /* Return the code for this type. */ |
132 | static PyObject * | |
133 | typy_get_code (PyObject *self, void *closure) | |
134 | { | |
135 | struct type *type = ((type_object *) self)->type; | |
d59b6f6c | 136 | |
47f0e2ff | 137 | return gdb_py_object_from_longest (type->code ()).release (); |
2c74e833 TT |
138 | } |
139 | ||
140 | /* Helper function for typy_fields which converts a single field to a | |
a73bb892 PK |
141 | gdb.Field object. Returns NULL on error. */ |
142 | ||
1b20edf0 | 143 | static gdbpy_ref<> |
2c74e833 TT |
144 | convert_field (struct type *type, int field) |
145 | { | |
7780f186 | 146 | gdbpy_ref<> result (field_new ()); |
2c74e833 | 147 | |
3bb43384 | 148 | if (result == NULL) |
2c74e833 TT |
149 | return NULL; |
150 | ||
7780f186 | 151 | gdbpy_ref<> arg (type_to_type_object (type)); |
a16b0e22 | 152 | if (arg == NULL) |
3bb43384 TT |
153 | return NULL; |
154 | if (PyObject_SetAttrString (result.get (), "parent_type", arg.get ()) < 0) | |
155 | return NULL; | |
a16b0e22 | 156 | |
ceacbf6e | 157 | if (!field_is_static (&type->field (field))) |
2c74e833 | 158 | { |
14e75d8e JK |
159 | const char *attrstring; |
160 | ||
78134374 | 161 | if (type->code () == TYPE_CODE_ENUM) |
14e75d8e | 162 | { |
970db518 | 163 | arg = gdb_py_object_from_longest (type->field (field).loc_enumval ()); |
14e75d8e JK |
164 | attrstring = "enumval"; |
165 | } | |
166 | else | |
167 | { | |
2ad53ea1 | 168 | if (type->field (field).loc_kind () == FIELD_LOC_KIND_DWARF_BLOCK) |
1acda803 TT |
169 | arg = gdbpy_ref<>::new_reference (Py_None); |
170 | else | |
b610c045 | 171 | arg = gdb_py_object_from_longest (type->field (field).loc_bitpos ()); |
14e75d8e JK |
172 | attrstring = "bitpos"; |
173 | } | |
174 | ||
3bb43384 TT |
175 | if (arg == NULL) |
176 | return NULL; | |
2c74e833 | 177 | |
6c28e44a | 178 | if (PyObject_SetAttrString (result.get (), attrstring, arg.get ()) < 0) |
3bb43384 | 179 | return NULL; |
2c74e833 TT |
180 | } |
181 | ||
3bb43384 | 182 | arg.reset (NULL); |
33d16dd9 | 183 | if (type->field (field).name ()) |
b5b08fb4 | 184 | { |
33d16dd9 | 185 | const char *field_name = type->field (field).name (); |
a8f35c2e | 186 | |
b5b08fb4 SC |
187 | if (field_name[0] != '\0') |
188 | { | |
5aee4587 | 189 | arg.reset (PyUnicode_FromString (type->field (field).name ())); |
b5b08fb4 | 190 | if (arg == NULL) |
3bb43384 | 191 | return NULL; |
b5b08fb4 SC |
192 | } |
193 | } | |
194 | if (arg == NULL) | |
1b20edf0 TT |
195 | arg = gdbpy_ref<>::new_reference (Py_None); |
196 | ||
3bb43384 TT |
197 | if (PyObject_SetAttrString (result.get (), "name", arg.get ()) < 0) |
198 | return NULL; | |
2c74e833 | 199 | |
c86acd3f | 200 | arg.reset (PyBool_FromLong (TYPE_FIELD_ARTIFICIAL (type, field))); |
3bb43384 TT |
201 | if (PyObject_SetAttrString (result.get (), "artificial", arg.get ()) < 0) |
202 | return NULL; | |
2c74e833 | 203 | |
78134374 | 204 | if (type->code () == TYPE_CODE_STRUCT) |
c86acd3f | 205 | arg.reset (PyBool_FromLong (field < TYPE_N_BASECLASSES (type))); |
bfd31e71 | 206 | else |
1b20edf0 | 207 | arg = gdbpy_ref<>::new_reference (Py_False); |
3bb43384 TT |
208 | if (PyObject_SetAttrString (result.get (), "is_base_class", arg.get ()) < 0) |
209 | return NULL; | |
210 | ||
062534d4 | 211 | arg = gdb_py_object_from_longest (TYPE_FIELD_BITSIZE (type, field)); |
3bb43384 TT |
212 | if (arg == NULL) |
213 | return NULL; | |
214 | if (PyObject_SetAttrString (result.get (), "bitsize", arg.get ()) < 0) | |
215 | return NULL; | |
2c74e833 TT |
216 | |
217 | /* A field can have a NULL type in some situations. */ | |
940da03e | 218 | if (type->field (field).type () == NULL) |
1b20edf0 | 219 | arg = gdbpy_ref<>::new_reference (Py_None); |
2c74e833 | 220 | else |
940da03e | 221 | arg.reset (type_to_type_object (type->field (field).type ())); |
3bb43384 TT |
222 | if (arg == NULL) |
223 | return NULL; | |
224 | if (PyObject_SetAttrString (result.get (), "type", arg.get ()) < 0) | |
225 | return NULL; | |
2c74e833 | 226 | |
1b20edf0 | 227 | return result; |
2c74e833 TT |
228 | } |
229 | ||
a73bb892 PK |
230 | /* Helper function to return the name of a field, as a gdb.Field object. |
231 | If the field doesn't have a name, None is returned. */ | |
232 | ||
1b20edf0 | 233 | static gdbpy_ref<> |
a73bb892 | 234 | field_name (struct type *type, int field) |
2c74e833 | 235 | { |
1b20edf0 | 236 | gdbpy_ref<> result; |
a73bb892 | 237 | |
33d16dd9 | 238 | if (type->field (field).name ()) |
5aee4587 | 239 | result.reset (PyUnicode_FromString (type->field (field).name ())); |
a73bb892 | 240 | else |
1b20edf0 TT |
241 | result = gdbpy_ref<>::new_reference (Py_None); |
242 | ||
a73bb892 PK |
243 | return result; |
244 | } | |
245 | ||
246 | /* Helper function for Type standard mapping methods. Returns a | |
247 | Python object for field i of the type. "kind" specifies what to | |
248 | return: the name of the field, a gdb.Field object corresponding to | |
249 | the field, or a tuple consisting of field name and gdb.Field | |
250 | object. */ | |
251 | ||
1b20edf0 | 252 | static gdbpy_ref<> |
a73bb892 PK |
253 | make_fielditem (struct type *type, int i, enum gdbpy_iter_kind kind) |
254 | { | |
a73bb892 PK |
255 | switch (kind) |
256 | { | |
257 | case iter_items: | |
3bb43384 | 258 | { |
7780f186 | 259 | gdbpy_ref<> key (field_name (type, i)); |
3bb43384 TT |
260 | if (key == NULL) |
261 | return NULL; | |
1b20edf0 | 262 | gdbpy_ref<> value = convert_field (type, i); |
3bb43384 TT |
263 | if (value == NULL) |
264 | return NULL; | |
7780f186 | 265 | gdbpy_ref<> item (PyTuple_New (2)); |
3bb43384 TT |
266 | if (item == NULL) |
267 | return NULL; | |
268 | PyTuple_SET_ITEM (item.get (), 0, key.release ()); | |
269 | PyTuple_SET_ITEM (item.get (), 1, value.release ()); | |
1b20edf0 | 270 | return item; |
3bb43384 | 271 | } |
a73bb892 | 272 | case iter_keys: |
3bb43384 | 273 | return field_name (type, i); |
a73bb892 | 274 | case iter_values: |
3bb43384 | 275 | return convert_field (type, i); |
a73bb892 | 276 | } |
3bb43384 | 277 | gdb_assert_not_reached ("invalid gdbpy_iter_kind"); |
a73bb892 PK |
278 | } |
279 | ||
280 | /* Return a sequence of all field names, fields, or (name, field) pairs. | |
281 | Each field is a gdb.Field object. */ | |
282 | ||
283 | static PyObject * | |
284 | typy_fields_items (PyObject *self, enum gdbpy_iter_kind kind) | |
285 | { | |
f6b47be4 | 286 | PyObject *py_type = self; |
f6b47be4 DE |
287 | struct type *type = ((type_object *) py_type)->type; |
288 | struct type *checked_type = type; | |
289 | ||
a70b8144 | 290 | try |
f6b47be4 | 291 | { |
f168693b | 292 | checked_type = check_typedef (checked_type); |
f6b47be4 | 293 | } |
230d2906 | 294 | catch (const gdb_exception &except) |
492d29ea PA |
295 | { |
296 | GDB_PY_HANDLE_EXCEPTION (except); | |
297 | } | |
f6b47be4 | 298 | |
2a3c71d6 | 299 | gdbpy_ref<> type_holder; |
f6b47be4 | 300 | if (checked_type != type) |
f6b47be4 | 301 | { |
2a3c71d6 TT |
302 | type_holder.reset (type_to_type_object (checked_type)); |
303 | if (type_holder == nullptr) | |
304 | return nullptr; | |
305 | py_type = type_holder.get (); | |
f6b47be4 | 306 | } |
2a3c71d6 TT |
307 | gdbpy_ref<> iter (typy_make_iter (py_type, kind)); |
308 | if (iter == nullptr) | |
309 | return nullptr; | |
f6b47be4 | 310 | |
2a3c71d6 | 311 | return PySequence_List (iter.get ()); |
a73bb892 PK |
312 | } |
313 | ||
314 | /* Return a sequence of all fields. Each field is a gdb.Field object. */ | |
315 | ||
316 | static PyObject * | |
9cc10fd1 | 317 | typy_values (PyObject *self, PyObject *args) |
a73bb892 PK |
318 | { |
319 | return typy_fields_items (self, iter_values); | |
320 | } | |
321 | ||
9cc10fd1 | 322 | /* Return a sequence of all fields. Each field is a gdb.Field object. |
256458bc | 323 | This method is similar to typy_values, except where the supplied |
9cc10fd1 PK |
324 | gdb.Type is an array, in which case it returns a list of one entry |
325 | which is a gdb.Field object for a range (the array bounds). */ | |
326 | ||
327 | static PyObject * | |
328 | typy_fields (PyObject *self, PyObject *args) | |
329 | { | |
330 | struct type *type = ((type_object *) self)->type; | |
256458bc | 331 | |
78134374 | 332 | if (type->code () != TYPE_CODE_ARRAY) |
9cc10fd1 PK |
333 | return typy_fields_items (self, iter_values); |
334 | ||
335 | /* Array type. Handle this as a special case because the common | |
336 | machinery wants struct or union or enum types. Build a list of | |
337 | one entry which is the range for the array. */ | |
1b20edf0 | 338 | gdbpy_ref<> r = convert_field (type, 0); |
9cc10fd1 PK |
339 | if (r == NULL) |
340 | return NULL; | |
256458bc | 341 | |
3bb43384 | 342 | return Py_BuildValue ("[O]", r.get ()); |
9cc10fd1 PK |
343 | } |
344 | ||
a73bb892 PK |
345 | /* Return a sequence of all field names. Each field is a gdb.Field object. */ |
346 | ||
347 | static PyObject * | |
348 | typy_field_names (PyObject *self, PyObject *args) | |
349 | { | |
350 | return typy_fields_items (self, iter_keys); | |
351 | } | |
352 | ||
256458bc | 353 | /* Return a sequence of all (name, fields) pairs. Each field is a |
a73bb892 PK |
354 | gdb.Field object. */ |
355 | ||
356 | static PyObject * | |
357 | typy_items (PyObject *self, PyObject *args) | |
358 | { | |
359 | return typy_fields_items (self, iter_items); | |
2c74e833 TT |
360 | } |
361 | ||
c0d48811 JB |
362 | /* Return the type's name, or None. */ |
363 | ||
364 | static PyObject * | |
365 | typy_get_name (PyObject *self, void *closure) | |
366 | { | |
367 | struct type *type = ((type_object *) self)->type; | |
368 | ||
7d93a1e0 | 369 | if (type->name () == NULL) |
c0d48811 | 370 | Py_RETURN_NONE; |
67470e9d TT |
371 | /* Ada type names are encoded, but it is better for users to see the |
372 | decoded form. */ | |
373 | if (ADA_TYPE_P (type)) | |
374 | { | |
375 | std::string name = ada_decode (type->name (), false); | |
376 | if (!name.empty ()) | |
5aee4587 | 377 | return PyUnicode_FromString (name.c_str ()); |
67470e9d | 378 | } |
5aee4587 | 379 | return PyUnicode_FromString (type->name ()); |
c0d48811 JB |
380 | } |
381 | ||
2c74e833 TT |
382 | /* Return the type's tag, or None. */ |
383 | static PyObject * | |
384 | typy_get_tag (PyObject *self, void *closure) | |
385 | { | |
386 | struct type *type = ((type_object *) self)->type; | |
e86ca25f | 387 | const char *tagname = nullptr; |
d59b6f6c | 388 | |
78134374 SM |
389 | if (type->code () == TYPE_CODE_STRUCT |
390 | || type->code () == TYPE_CODE_UNION | |
391 | || type->code () == TYPE_CODE_ENUM) | |
7d93a1e0 | 392 | tagname = type->name (); |
e86ca25f TT |
393 | |
394 | if (tagname == nullptr) | |
2c74e833 | 395 | Py_RETURN_NONE; |
5aee4587 | 396 | return PyUnicode_FromString (tagname); |
2c74e833 TT |
397 | } |
398 | ||
e1f2e1a2 CB |
399 | /* Return the type's objfile, or None. */ |
400 | static PyObject * | |
401 | typy_get_objfile (PyObject *self, void *closure) | |
402 | { | |
403 | struct type *type = ((type_object *) self)->type; | |
6ac37371 | 404 | struct objfile *objfile = type->objfile_owner (); |
e1f2e1a2 CB |
405 | |
406 | if (objfile == nullptr) | |
407 | Py_RETURN_NONE; | |
408 | return objfile_to_objfile_object (objfile).release (); | |
409 | } | |
410 | ||
ee6a3d9e AB |
411 | /* Return true if this is a scalar type, otherwise, returns false. */ |
412 | ||
413 | static PyObject * | |
414 | typy_is_scalar (PyObject *self, void *closure) | |
415 | { | |
416 | struct type *type = ((type_object *) self)->type; | |
417 | ||
418 | if (is_scalar_type (type)) | |
419 | Py_RETURN_TRUE; | |
420 | else | |
421 | Py_RETURN_FALSE; | |
422 | } | |
423 | ||
551b380f AB |
424 | /* Return true if this type is signed. Raises a ValueError if this type |
425 | is not a scalar type. */ | |
426 | ||
427 | static PyObject * | |
428 | typy_is_signed (PyObject *self, void *closure) | |
429 | { | |
430 | struct type *type = ((type_object *) self)->type; | |
431 | ||
432 | if (!is_scalar_type (type)) | |
433 | { | |
434 | PyErr_SetString (PyExc_ValueError, | |
435 | _("Type must be a scalar type")); | |
436 | return nullptr; | |
437 | } | |
438 | ||
439 | if (type->is_unsigned ()) | |
440 | Py_RETURN_FALSE; | |
441 | else | |
442 | Py_RETURN_TRUE; | |
443 | } | |
444 | ||
2c74e833 TT |
445 | /* Return the type, stripped of typedefs. */ |
446 | static PyObject * | |
447 | typy_strip_typedefs (PyObject *self, PyObject *args) | |
448 | { | |
449 | struct type *type = ((type_object *) self)->type; | |
5d9c5995 | 450 | |
a70b8144 | 451 | try |
5d9c5995 PM |
452 | { |
453 | type = check_typedef (type); | |
454 | } | |
230d2906 | 455 | catch (const gdb_exception &except) |
492d29ea PA |
456 | { |
457 | GDB_PY_HANDLE_EXCEPTION (except); | |
458 | } | |
2c74e833 | 459 | |
bc9abe4a | 460 | return type_to_type_object (type); |
2c74e833 TT |
461 | } |
462 | ||
9cc10fd1 PK |
463 | /* Strip typedefs and pointers/reference from a type. Then check that |
464 | it is a struct, union, or enum type. If not, raise TypeError. */ | |
465 | ||
466 | static struct type * | |
467 | typy_get_composite (struct type *type) | |
468 | { | |
9cc10fd1 PK |
469 | |
470 | for (;;) | |
471 | { | |
a70b8144 | 472 | try |
9cc10fd1 | 473 | { |
f168693b | 474 | type = check_typedef (type); |
9cc10fd1 | 475 | } |
230d2906 | 476 | catch (const gdb_exception &except) |
492d29ea PA |
477 | { |
478 | GDB_PY_HANDLE_EXCEPTION (except); | |
479 | } | |
9cc10fd1 | 480 | |
809f3be1 | 481 | if (!type->is_pointer_or_reference ()) |
9cc10fd1 | 482 | break; |
27710edb | 483 | type = type->target_type (); |
9cc10fd1 PK |
484 | } |
485 | ||
486 | /* If this is not a struct, union, or enum type, raise TypeError | |
487 | exception. */ | |
78134374 SM |
488 | if (type->code () != TYPE_CODE_STRUCT |
489 | && type->code () != TYPE_CODE_UNION | |
490 | && type->code () != TYPE_CODE_ENUM | |
b3f9469b | 491 | && type->code () != TYPE_CODE_METHOD |
78134374 | 492 | && type->code () != TYPE_CODE_FUNC) |
9cc10fd1 PK |
493 | { |
494 | PyErr_SetString (PyExc_TypeError, | |
bed91f4d | 495 | "Type is not a structure, union, enum, or function type."); |
9cc10fd1 PK |
496 | return NULL; |
497 | } | |
256458bc | 498 | |
9cc10fd1 PK |
499 | return type; |
500 | } | |
501 | ||
a72c3253 | 502 | /* Helper for typy_array and typy_vector. */ |
702c2711 TT |
503 | |
504 | static PyObject * | |
a72c3253 | 505 | typy_array_1 (PyObject *self, PyObject *args, int is_vector) |
702c2711 | 506 | { |
74aedc46 | 507 | long n1, n2; |
702c2711 TT |
508 | PyObject *n2_obj = NULL; |
509 | struct type *array = NULL; | |
510 | struct type *type = ((type_object *) self)->type; | |
702c2711 | 511 | |
74aedc46 | 512 | if (! PyArg_ParseTuple (args, "l|O", &n1, &n2_obj)) |
702c2711 TT |
513 | return NULL; |
514 | ||
515 | if (n2_obj) | |
516 | { | |
5aee4587 | 517 | if (!PyLong_Check (n2_obj)) |
702c2711 TT |
518 | { |
519 | PyErr_SetString (PyExc_RuntimeError, | |
520 | _("Array bound must be an integer")); | |
521 | return NULL; | |
522 | } | |
74aedc46 TT |
523 | |
524 | if (! gdb_py_int_as_long (n2_obj, &n2)) | |
702c2711 TT |
525 | return NULL; |
526 | } | |
527 | else | |
528 | { | |
529 | n2 = n1; | |
530 | n1 = 0; | |
531 | } | |
532 | ||
e810d75b | 533 | if (n2 < n1 - 1) /* Note: An empty array has n2 == n1 - 1. */ |
702c2711 TT |
534 | { |
535 | PyErr_SetString (PyExc_ValueError, | |
536 | _("Array length must not be negative")); | |
537 | return NULL; | |
538 | } | |
539 | ||
a70b8144 | 540 | try |
702c2711 TT |
541 | { |
542 | array = lookup_array_range_type (type, n1, n2); | |
a72c3253 DE |
543 | if (is_vector) |
544 | make_vector_type (array); | |
702c2711 | 545 | } |
230d2906 | 546 | catch (const gdb_exception &except) |
492d29ea PA |
547 | { |
548 | GDB_PY_HANDLE_EXCEPTION (except); | |
549 | } | |
702c2711 TT |
550 | |
551 | return type_to_type_object (array); | |
552 | } | |
553 | ||
a72c3253 DE |
554 | /* Return an array type. */ |
555 | ||
556 | static PyObject * | |
557 | typy_array (PyObject *self, PyObject *args) | |
558 | { | |
559 | return typy_array_1 (self, args, 0); | |
560 | } | |
561 | ||
562 | /* Return a vector type. */ | |
563 | ||
564 | static PyObject * | |
565 | typy_vector (PyObject *self, PyObject *args) | |
566 | { | |
567 | return typy_array_1 (self, args, 1); | |
568 | } | |
569 | ||
2c74e833 TT |
570 | /* Return a Type object which represents a pointer to SELF. */ |
571 | static PyObject * | |
572 | typy_pointer (PyObject *self, PyObject *args) | |
573 | { | |
574 | struct type *type = ((type_object *) self)->type; | |
2c74e833 | 575 | |
a70b8144 | 576 | try |
2c74e833 TT |
577 | { |
578 | type = lookup_pointer_type (type); | |
579 | } | |
230d2906 | 580 | catch (const gdb_exception &except) |
492d29ea PA |
581 | { |
582 | GDB_PY_HANDLE_EXCEPTION (except); | |
583 | } | |
2c74e833 TT |
584 | |
585 | return type_to_type_object (type); | |
586 | } | |
587 | ||
361ae042 PM |
588 | /* Return the range of a type represented by SELF. The return type is |
589 | a tuple. The first element of the tuple contains the low bound, | |
590 | while the second element of the tuple contains the high bound. */ | |
591 | static PyObject * | |
592 | typy_range (PyObject *self, PyObject *args) | |
593 | { | |
594 | struct type *type = ((type_object *) self)->type; | |
8d099ae9 PM |
595 | /* Initialize these to appease GCC warnings. */ |
596 | LONGEST low = 0, high = 0; | |
361ae042 | 597 | |
78134374 SM |
598 | if (type->code () != TYPE_CODE_ARRAY |
599 | && type->code () != TYPE_CODE_STRING | |
600 | && type->code () != TYPE_CODE_RANGE) | |
361ae042 PM |
601 | { |
602 | PyErr_SetString (PyExc_RuntimeError, | |
044c0f87 | 603 | _("This type does not have a range.")); |
361ae042 PM |
604 | return NULL; |
605 | } | |
606 | ||
78134374 | 607 | switch (type->code ()) |
361ae042 PM |
608 | { |
609 | case TYPE_CODE_ARRAY: | |
610 | case TYPE_CODE_STRING: | |
361ae042 | 611 | case TYPE_CODE_RANGE: |
e25d6d93 SM |
612 | if (type->bounds ()->low.kind () == PROP_CONST) |
613 | low = type->bounds ()->low.const_val (); | |
614 | else | |
615 | low = 0; | |
616 | ||
617 | if (type->bounds ()->high.kind () == PROP_CONST) | |
618 | high = type->bounds ()->high.const_val (); | |
619 | else | |
620 | high = 0; | |
361ae042 PM |
621 | break; |
622 | } | |
623 | ||
062534d4 | 624 | gdbpy_ref<> low_bound = gdb_py_object_from_longest (low); |
3bb43384 TT |
625 | if (low_bound == NULL) |
626 | return NULL; | |
361ae042 | 627 | |
062534d4 | 628 | gdbpy_ref<> high_bound = gdb_py_object_from_longest (high); |
3bb43384 TT |
629 | if (high_bound == NULL) |
630 | return NULL; | |
361ae042 | 631 | |
7780f186 | 632 | gdbpy_ref<> result (PyTuple_New (2)); |
3bb43384 TT |
633 | if (result == NULL) |
634 | return NULL; | |
256458bc | 635 | |
3bb43384 TT |
636 | if (PyTuple_SetItem (result.get (), 0, low_bound.release ()) != 0 |
637 | || PyTuple_SetItem (result.get (), 1, high_bound.release ()) != 0) | |
638 | return NULL; | |
639 | return result.release (); | |
361ae042 PM |
640 | } |
641 | ||
2c74e833 TT |
642 | /* Return a Type object which represents a reference to SELF. */ |
643 | static PyObject * | |
644 | typy_reference (PyObject *self, PyObject *args) | |
645 | { | |
646 | struct type *type = ((type_object *) self)->type; | |
2c74e833 | 647 | |
a70b8144 | 648 | try |
2c74e833 | 649 | { |
3b224330 | 650 | type = lookup_lvalue_reference_type (type); |
2c74e833 | 651 | } |
230d2906 | 652 | catch (const gdb_exception &except) |
492d29ea PA |
653 | { |
654 | GDB_PY_HANDLE_EXCEPTION (except); | |
655 | } | |
2c74e833 TT |
656 | |
657 | return type_to_type_object (type); | |
658 | } | |
659 | ||
660 | /* Return a Type object which represents the target type of SELF. */ | |
661 | static PyObject * | |
662 | typy_target (PyObject *self, PyObject *args) | |
663 | { | |
664 | struct type *type = ((type_object *) self)->type; | |
665 | ||
27710edb | 666 | if (!type->target_type ()) |
2c74e833 | 667 | { |
256458bc | 668 | PyErr_SetString (PyExc_RuntimeError, |
044c0f87 | 669 | _("Type does not have a target.")); |
2c74e833 TT |
670 | return NULL; |
671 | } | |
672 | ||
27710edb | 673 | return type_to_type_object (type->target_type ()); |
2c74e833 TT |
674 | } |
675 | ||
676 | /* Return a const-qualified type variant. */ | |
677 | static PyObject * | |
678 | typy_const (PyObject *self, PyObject *args) | |
679 | { | |
680 | struct type *type = ((type_object *) self)->type; | |
2c74e833 | 681 | |
a70b8144 | 682 | try |
2c74e833 TT |
683 | { |
684 | type = make_cv_type (1, 0, type, NULL); | |
685 | } | |
230d2906 | 686 | catch (const gdb_exception &except) |
492d29ea PA |
687 | { |
688 | GDB_PY_HANDLE_EXCEPTION (except); | |
689 | } | |
2c74e833 TT |
690 | |
691 | return type_to_type_object (type); | |
692 | } | |
693 | ||
694 | /* Return a volatile-qualified type variant. */ | |
695 | static PyObject * | |
696 | typy_volatile (PyObject *self, PyObject *args) | |
697 | { | |
698 | struct type *type = ((type_object *) self)->type; | |
2c74e833 | 699 | |
a70b8144 | 700 | try |
2c74e833 TT |
701 | { |
702 | type = make_cv_type (0, 1, type, NULL); | |
703 | } | |
230d2906 | 704 | catch (const gdb_exception &except) |
492d29ea PA |
705 | { |
706 | GDB_PY_HANDLE_EXCEPTION (except); | |
707 | } | |
2c74e833 TT |
708 | |
709 | return type_to_type_object (type); | |
710 | } | |
711 | ||
712 | /* Return an unqualified type variant. */ | |
713 | static PyObject * | |
714 | typy_unqualified (PyObject *self, PyObject *args) | |
715 | { | |
716 | struct type *type = ((type_object *) self)->type; | |
2c74e833 | 717 | |
a70b8144 | 718 | try |
2c74e833 TT |
719 | { |
720 | type = make_cv_type (0, 0, type, NULL); | |
721 | } | |
230d2906 | 722 | catch (const gdb_exception &except) |
492d29ea PA |
723 | { |
724 | GDB_PY_HANDLE_EXCEPTION (except); | |
725 | } | |
2c74e833 TT |
726 | |
727 | return type_to_type_object (type); | |
728 | } | |
729 | ||
730 | /* Return the size of the type represented by SELF, in bytes. */ | |
731 | static PyObject * | |
732 | typy_get_sizeof (PyObject *self, void *closure) | |
733 | { | |
734 | struct type *type = ((type_object *) self)->type; | |
2c74e833 | 735 | |
1acda803 | 736 | bool size_varies = false; |
a70b8144 | 737 | try |
2c74e833 TT |
738 | { |
739 | check_typedef (type); | |
1acda803 TT |
740 | |
741 | size_varies = TYPE_HAS_DYNAMIC_LENGTH (type); | |
2c74e833 | 742 | } |
230d2906 | 743 | catch (const gdb_exception &except) |
492d29ea PA |
744 | { |
745 | } | |
492d29ea | 746 | |
2c74e833 TT |
747 | /* Ignore exceptions. */ |
748 | ||
1acda803 TT |
749 | if (size_varies) |
750 | Py_RETURN_NONE; | |
df86565b | 751 | return gdb_py_object_from_longest (type->length ()).release (); |
2c74e833 TT |
752 | } |
753 | ||
6d7bb824 TT |
754 | /* Return the alignment of the type represented by SELF, in bytes. */ |
755 | static PyObject * | |
756 | typy_get_alignof (PyObject *self, void *closure) | |
757 | { | |
758 | struct type *type = ((type_object *) self)->type; | |
759 | ||
760 | ULONGEST align = 0; | |
a70b8144 | 761 | try |
6d7bb824 TT |
762 | { |
763 | align = type_align (type); | |
764 | } | |
230d2906 | 765 | catch (const gdb_exception &except) |
6d7bb824 TT |
766 | { |
767 | align = 0; | |
768 | } | |
6d7bb824 TT |
769 | |
770 | /* Ignore exceptions. */ | |
771 | ||
12dfa12a | 772 | return gdb_py_object_from_ulongest (align).release (); |
6d7bb824 TT |
773 | } |
774 | ||
1acda803 TT |
775 | /* Return whether or not the type is dynamic. */ |
776 | static PyObject * | |
777 | typy_get_dynamic (PyObject *self, void *closure) | |
778 | { | |
779 | struct type *type = ((type_object *) self)->type; | |
780 | ||
781 | bool result = false; | |
782 | try | |
783 | { | |
784 | result = is_dynamic_type (type); | |
785 | } | |
786 | catch (const gdb_exception &except) | |
787 | { | |
788 | /* Ignore exceptions. */ | |
789 | } | |
790 | ||
791 | if (result) | |
792 | Py_RETURN_TRUE; | |
793 | Py_RETURN_FALSE; | |
794 | } | |
795 | ||
2c74e833 | 796 | static struct type * |
9df2fbc4 | 797 | typy_lookup_typename (const char *type_name, const struct block *block) |
2c74e833 TT |
798 | { |
799 | struct type *type = NULL; | |
d59b6f6c | 800 | |
a70b8144 | 801 | try |
2c74e833 | 802 | { |
61012eef | 803 | if (startswith (type_name, "struct ")) |
2c74e833 | 804 | type = lookup_struct (type_name + 7, NULL); |
61012eef | 805 | else if (startswith (type_name, "union ")) |
2c74e833 | 806 | type = lookup_union (type_name + 6, NULL); |
61012eef | 807 | else if (startswith (type_name, "enum ")) |
2c74e833 TT |
808 | type = lookup_enum (type_name + 5, NULL); |
809 | else | |
1da5d0e6 | 810 | type = lookup_typename (current_language, |
5107b149 | 811 | type_name, block, 0); |
2c74e833 | 812 | } |
230d2906 | 813 | catch (const gdb_exception &except) |
492d29ea PA |
814 | { |
815 | GDB_PY_HANDLE_EXCEPTION (except); | |
816 | } | |
2c74e833 TT |
817 | |
818 | return type; | |
819 | } | |
820 | ||
821 | static struct type * | |
5107b149 | 822 | typy_lookup_type (struct demangle_component *demangled, |
9df2fbc4 | 823 | const struct block *block) |
2c74e833 | 824 | { |
cd829959 | 825 | struct type *type, *rtype = NULL; |
2c74e833 TT |
826 | enum demangle_component_type demangled_type; |
827 | ||
828 | /* Save the type: typy_lookup_type() may (indirectly) overwrite | |
829 | memory pointed by demangled. */ | |
830 | demangled_type = demangled->type; | |
831 | ||
832 | if (demangled_type == DEMANGLE_COMPONENT_POINTER | |
833 | || demangled_type == DEMANGLE_COMPONENT_REFERENCE | |
e4347c89 | 834 | || demangled_type == DEMANGLE_COMPONENT_RVALUE_REFERENCE |
2c74e833 TT |
835 | || demangled_type == DEMANGLE_COMPONENT_CONST |
836 | || demangled_type == DEMANGLE_COMPONENT_VOLATILE) | |
837 | { | |
5107b149 | 838 | type = typy_lookup_type (demangled->u.s_binary.left, block); |
2c74e833 TT |
839 | if (! type) |
840 | return NULL; | |
841 | ||
a70b8144 | 842 | try |
76dce0be | 843 | { |
cd829959 PM |
844 | /* If the demangled_type matches with one of the types |
845 | below, run the corresponding function and save the type | |
846 | to return later. We cannot just return here as we are in | |
847 | an exception handler. */ | |
76dce0be PM |
848 | switch (demangled_type) |
849 | { | |
850 | case DEMANGLE_COMPONENT_REFERENCE: | |
3b224330 | 851 | rtype = lookup_lvalue_reference_type (type); |
cd829959 | 852 | break; |
e4347c89 AV |
853 | case DEMANGLE_COMPONENT_RVALUE_REFERENCE: |
854 | rtype = lookup_rvalue_reference_type (type); | |
855 | break; | |
76dce0be | 856 | case DEMANGLE_COMPONENT_POINTER: |
cd829959 PM |
857 | rtype = lookup_pointer_type (type); |
858 | break; | |
76dce0be | 859 | case DEMANGLE_COMPONENT_CONST: |
cd829959 PM |
860 | rtype = make_cv_type (1, 0, type, NULL); |
861 | break; | |
76dce0be | 862 | case DEMANGLE_COMPONENT_VOLATILE: |
cd829959 PM |
863 | rtype = make_cv_type (0, 1, type, NULL); |
864 | break; | |
76dce0be | 865 | } |
76dce0be | 866 | } |
230d2906 | 867 | catch (const gdb_exception &except) |
492d29ea PA |
868 | { |
869 | GDB_PY_HANDLE_EXCEPTION (except); | |
870 | } | |
2c74e833 | 871 | } |
256458bc | 872 | |
cd829959 PM |
873 | /* If we have a type from the switch statement above, just return |
874 | that. */ | |
875 | if (rtype) | |
876 | return rtype; | |
256458bc | 877 | |
cd829959 | 878 | /* We don't have a type, so lookup the type. */ |
29592bde PA |
879 | gdb::unique_xmalloc_ptr<char> type_name = cp_comp_to_string (demangled, 10); |
880 | return typy_lookup_typename (type_name.get (), block); | |
2c74e833 TT |
881 | } |
882 | ||
326fd672 TT |
883 | /* This is a helper function for typy_template_argument that is used |
884 | when the type does not have template symbols attached. It works by | |
885 | parsing the type name. This happens with compilers, like older | |
886 | versions of GCC, that do not emit DW_TAG_template_*. */ | |
887 | ||
2c74e833 | 888 | static PyObject * |
9df2fbc4 | 889 | typy_legacy_template_argument (struct type *type, const struct block *block, |
326fd672 | 890 | int argno) |
2c74e833 | 891 | { |
326fd672 | 892 | int i; |
2c74e833 | 893 | struct demangle_component *demangled; |
c8b23b3f | 894 | std::unique_ptr<demangle_parse_info> info; |
3513a6bb | 895 | std::string err; |
2c74e833 | 896 | struct type *argtype; |
2c74e833 | 897 | |
7d93a1e0 | 898 | if (type->name () == NULL) |
2c74e833 | 899 | { |
5107b149 | 900 | PyErr_SetString (PyExc_RuntimeError, _("Null type name.")); |
2c74e833 TT |
901 | return NULL; |
902 | } | |
903 | ||
a70b8144 | 904 | try |
5d9c5995 PM |
905 | { |
906 | /* Note -- this is not thread-safe. */ | |
7d93a1e0 | 907 | info = cp_demangled_name_to_comp (type->name (), &err); |
5d9c5995 | 908 | } |
230d2906 | 909 | catch (const gdb_exception &except) |
492d29ea PA |
910 | { |
911 | GDB_PY_HANDLE_EXCEPTION (except); | |
912 | } | |
5d9c5995 | 913 | |
3a93a0c2 | 914 | if (! info) |
2c74e833 | 915 | { |
3513a6bb | 916 | PyErr_SetString (PyExc_RuntimeError, err.c_str ()); |
2c74e833 TT |
917 | return NULL; |
918 | } | |
3a93a0c2 | 919 | demangled = info->tree; |
2c74e833 TT |
920 | |
921 | /* Strip off component names. */ | |
922 | while (demangled->type == DEMANGLE_COMPONENT_QUAL_NAME | |
923 | || demangled->type == DEMANGLE_COMPONENT_LOCAL_NAME) | |
924 | demangled = demangled->u.s_binary.right; | |
925 | ||
926 | if (demangled->type != DEMANGLE_COMPONENT_TEMPLATE) | |
927 | { | |
5107b149 | 928 | PyErr_SetString (PyExc_RuntimeError, _("Type is not a template.")); |
2c74e833 TT |
929 | return NULL; |
930 | } | |
931 | ||
932 | /* Skip from the template to the arguments. */ | |
933 | demangled = demangled->u.s_binary.right; | |
934 | ||
935 | for (i = 0; demangled && i < argno; ++i) | |
936 | demangled = demangled->u.s_binary.right; | |
937 | ||
938 | if (! demangled) | |
939 | { | |
5107b149 | 940 | PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."), |
2c74e833 TT |
941 | argno); |
942 | return NULL; | |
943 | } | |
944 | ||
5107b149 | 945 | argtype = typy_lookup_type (demangled->u.s_binary.left, block); |
2c74e833 TT |
946 | if (! argtype) |
947 | return NULL; | |
948 | ||
949 | return type_to_type_object (argtype); | |
326fd672 TT |
950 | } |
951 | ||
952 | static PyObject * | |
953 | typy_template_argument (PyObject *self, PyObject *args) | |
954 | { | |
955 | int argno; | |
956 | struct type *type = ((type_object *) self)->type; | |
9df2fbc4 | 957 | const struct block *block = NULL; |
326fd672 TT |
958 | PyObject *block_obj = NULL; |
959 | struct symbol *sym; | |
960 | struct value *val = NULL; | |
326fd672 TT |
961 | |
962 | if (! PyArg_ParseTuple (args, "i|O", &argno, &block_obj)) | |
963 | return NULL; | |
964 | ||
fd3ba736 TT |
965 | if (argno < 0) |
966 | { | |
967 | PyErr_SetString (PyExc_RuntimeError, | |
968 | _("Template argument number must be non-negative")); | |
969 | return NULL; | |
970 | } | |
971 | ||
326fd672 TT |
972 | if (block_obj) |
973 | { | |
974 | block = block_object_to_block (block_obj); | |
975 | if (! block) | |
976 | { | |
977 | PyErr_SetString (PyExc_RuntimeError, | |
978 | _("Second argument must be block.")); | |
979 | return NULL; | |
980 | } | |
981 | } | |
982 | ||
a70b8144 | 983 | try |
05d0e1e7 TT |
984 | { |
985 | type = check_typedef (type); | |
aa006118 | 986 | if (TYPE_IS_REFERENCE (type)) |
27710edb | 987 | type = check_typedef (type->target_type ()); |
05d0e1e7 | 988 | } |
230d2906 | 989 | catch (const gdb_exception &except) |
492d29ea PA |
990 | { |
991 | GDB_PY_HANDLE_EXCEPTION (except); | |
992 | } | |
326fd672 TT |
993 | |
994 | /* We might not have DW_TAG_template_*, so try to parse the type's | |
995 | name. This is inefficient if we do not have a template type -- | |
996 | but that is going to wind up as an error anyhow. */ | |
997 | if (! TYPE_N_TEMPLATE_ARGUMENTS (type)) | |
998 | return typy_legacy_template_argument (type, block, argno); | |
999 | ||
1000 | if (argno >= TYPE_N_TEMPLATE_ARGUMENTS (type)) | |
1001 | { | |
1002 | PyErr_Format (PyExc_RuntimeError, _("No argument %d in template."), | |
1003 | argno); | |
1004 | return NULL; | |
1005 | } | |
1006 | ||
1007 | sym = TYPE_TEMPLATE_ARGUMENT (type, argno); | |
66d7f48f | 1008 | if (sym->aclass () == LOC_TYPEDEF) |
5f9c5a63 | 1009 | return type_to_type_object (sym->type ()); |
66d7f48f | 1010 | else if (sym->aclass () == LOC_OPTIMIZED_OUT) |
326fd672 TT |
1011 | { |
1012 | PyErr_Format (PyExc_RuntimeError, | |
1013 | _("Template argument is optimized out")); | |
1014 | return NULL; | |
1015 | } | |
1016 | ||
a70b8144 | 1017 | try |
326fd672 TT |
1018 | { |
1019 | val = value_of_variable (sym, block); | |
1020 | } | |
230d2906 | 1021 | catch (const gdb_exception &except) |
492d29ea PA |
1022 | { |
1023 | GDB_PY_HANDLE_EXCEPTION (except); | |
1024 | } | |
326fd672 TT |
1025 | |
1026 | return value_to_value_object (val); | |
2c74e833 TT |
1027 | } |
1028 | ||
1029 | static PyObject * | |
1030 | typy_str (PyObject *self) | |
1031 | { | |
d7e74731 | 1032 | string_file thetype; |
2c74e833 | 1033 | |
a70b8144 | 1034 | try |
2c74e833 | 1035 | { |
13eb081a TT |
1036 | current_language->print_type (type_object_to_type (self), "", |
1037 | &thetype, -1, 0, | |
1038 | &type_print_raw_options); | |
2c74e833 | 1039 | } |
230d2906 | 1040 | catch (const gdb_exception &except) |
2c74e833 | 1041 | { |
2c74e833 TT |
1042 | GDB_PY_HANDLE_EXCEPTION (except); |
1043 | } | |
1044 | ||
d7e74731 PA |
1045 | return PyUnicode_Decode (thetype.c_str (), thetype.size (), |
1046 | host_charset (), NULL); | |
2c74e833 TT |
1047 | } |
1048 | ||
d839c8a4 TT |
1049 | /* Implement the richcompare method. */ |
1050 | ||
1051 | static PyObject * | |
1052 | typy_richcompare (PyObject *self, PyObject *other, int op) | |
1053 | { | |
894882e3 | 1054 | bool result = false; |
d839c8a4 TT |
1055 | struct type *type1 = type_object_to_type (self); |
1056 | struct type *type2 = type_object_to_type (other); | |
d839c8a4 TT |
1057 | |
1058 | /* We can only compare ourselves to another Type object, and only | |
1059 | for equality or inequality. */ | |
1060 | if (type2 == NULL || (op != Py_EQ && op != Py_NE)) | |
1061 | { | |
1062 | Py_INCREF (Py_NotImplemented); | |
1063 | return Py_NotImplemented; | |
1064 | } | |
1065 | ||
1066 | if (type1 == type2) | |
894882e3 | 1067 | result = true; |
d839c8a4 TT |
1068 | else |
1069 | { | |
a70b8144 | 1070 | try |
d839c8a4 | 1071 | { |
ca092b61 | 1072 | result = types_deeply_equal (type1, type2); |
d839c8a4 | 1073 | } |
230d2906 | 1074 | catch (const gdb_exception &except) |
492d29ea PA |
1075 | { |
1076 | /* If there is a GDB exception, a comparison is not capable | |
1077 | (or trusted), so exit. */ | |
1078 | GDB_PY_HANDLE_EXCEPTION (except); | |
1079 | } | |
d839c8a4 TT |
1080 | } |
1081 | ||
ca092b61 | 1082 | if (op == (result ? Py_EQ : Py_NE)) |
d839c8a4 TT |
1083 | Py_RETURN_TRUE; |
1084 | Py_RETURN_FALSE; | |
1085 | } | |
1086 | ||
2c74e833 TT |
1087 | \f |
1088 | ||
08b8a139 TT |
1089 | /* Deleter that saves types when an objfile is being destroyed. */ |
1090 | struct typy_deleter | |
2c74e833 | 1091 | { |
08b8a139 TT |
1092 | void operator() (type_object *obj) |
1093 | { | |
1094 | if (!gdb_python_initialized) | |
1095 | return; | |
2c74e833 | 1096 | |
08b8a139 TT |
1097 | /* This prevents another thread from freeing the objects we're |
1098 | operating on. */ | |
1099 | gdbpy_enter enter_py; | |
0646da15 | 1100 | |
08b8a139 | 1101 | htab_up copied_types = create_copied_types_hash (); |
2c74e833 | 1102 | |
08b8a139 TT |
1103 | while (obj) |
1104 | { | |
1105 | type_object *next = obj->next; | |
2c74e833 | 1106 | |
08b8a139 | 1107 | htab_empty (copied_types.get ()); |
2c74e833 | 1108 | |
08b8a139 | 1109 | obj->type = copy_type_recursive (obj->type, copied_types.get ()); |
2c74e833 | 1110 | |
08b8a139 TT |
1111 | obj->next = NULL; |
1112 | obj->prev = NULL; | |
2c74e833 | 1113 | |
08b8a139 TT |
1114 | obj = next; |
1115 | } | |
1116 | } | |
1117 | }; | |
2c74e833 | 1118 | |
08b8a139 TT |
1119 | static const registry<objfile>::key<type_object, typy_deleter> |
1120 | typy_objfile_data_key; | |
2c74e833 TT |
1121 | |
1122 | static void | |
1123 | set_type (type_object *obj, struct type *type) | |
1124 | { | |
1125 | obj->type = type; | |
1126 | obj->prev = NULL; | |
6ac37371 | 1127 | if (type != nullptr && type->objfile_owner () != nullptr) |
2c74e833 | 1128 | { |
6ac37371 | 1129 | struct objfile *objfile = type->objfile_owner (); |
2c74e833 | 1130 | |
08b8a139 | 1131 | obj->next = typy_objfile_data_key.get (objfile); |
2c74e833 TT |
1132 | if (obj->next) |
1133 | obj->next->prev = obj; | |
08b8a139 | 1134 | typy_objfile_data_key.set (objfile, obj); |
2c74e833 TT |
1135 | } |
1136 | else | |
1137 | obj->next = NULL; | |
1138 | } | |
1139 | ||
1140 | static void | |
1141 | typy_dealloc (PyObject *obj) | |
1142 | { | |
1143 | type_object *type = (type_object *) obj; | |
1144 | ||
1145 | if (type->prev) | |
1146 | type->prev->next = type->next; | |
6ac37371 | 1147 | else if (type->type != nullptr && type->type->objfile_owner () != nullptr) |
2c74e833 TT |
1148 | { |
1149 | /* Must reset head of list. */ | |
6ac37371 | 1150 | struct objfile *objfile = type->type->objfile_owner (); |
d59b6f6c | 1151 | |
2c74e833 | 1152 | if (objfile) |
08b8a139 | 1153 | typy_objfile_data_key.set (objfile, type->next); |
2c74e833 TT |
1154 | } |
1155 | if (type->next) | |
1156 | type->next->prev = type->prev; | |
1157 | ||
9a27f2c6 | 1158 | Py_TYPE (type)->tp_free (type); |
2c74e833 TT |
1159 | } |
1160 | ||
a73bb892 PK |
1161 | /* Return number of fields ("length" of the field dictionary). */ |
1162 | ||
1163 | static Py_ssize_t | |
1164 | typy_length (PyObject *self) | |
1165 | { | |
1166 | struct type *type = ((type_object *) self)->type; | |
1167 | ||
9cc10fd1 PK |
1168 | type = typy_get_composite (type); |
1169 | if (type == NULL) | |
1170 | return -1; | |
1171 | ||
1f704f76 | 1172 | return type->num_fields (); |
a73bb892 PK |
1173 | } |
1174 | ||
9cc10fd1 | 1175 | /* Implements boolean evaluation of gdb.Type. Handle this like other |
256458bc | 1176 | Python objects that don't have a meaningful truth value -- all |
9cc10fd1 PK |
1177 | values are true. */ |
1178 | ||
1179 | static int | |
1180 | typy_nonzero (PyObject *self) | |
1181 | { | |
1182 | return 1; | |
1183 | } | |
1184 | ||
59fb7612 SS |
1185 | /* Return optimized out value of this type. */ |
1186 | ||
1187 | static PyObject * | |
1188 | typy_optimized_out (PyObject *self, PyObject *args) | |
1189 | { | |
1190 | struct type *type = ((type_object *) self)->type; | |
1191 | ||
1192 | return value_to_value_object (allocate_optimized_out_value (type)); | |
1193 | } | |
1194 | ||
a73bb892 PK |
1195 | /* Return a gdb.Field object for the field named by the argument. */ |
1196 | ||
1197 | static PyObject * | |
1198 | typy_getitem (PyObject *self, PyObject *key) | |
1199 | { | |
1200 | struct type *type = ((type_object *) self)->type; | |
a73bb892 | 1201 | int i; |
76dce0be | 1202 | |
9b972014 | 1203 | gdb::unique_xmalloc_ptr<char> field = python_string_to_host_string (key); |
a73bb892 PK |
1204 | if (field == NULL) |
1205 | return NULL; | |
1206 | ||
256458bc | 1207 | /* We want just fields of this type, not of base types, so instead of |
a73bb892 PK |
1208 | using lookup_struct_elt_type, portions of that function are |
1209 | copied here. */ | |
1210 | ||
9cc10fd1 PK |
1211 | type = typy_get_composite (type); |
1212 | if (type == NULL) | |
1213 | return NULL; | |
256458bc | 1214 | |
1f704f76 | 1215 | for (i = 0; i < type->num_fields (); i++) |
a73bb892 | 1216 | { |
33d16dd9 | 1217 | const char *t_field_name = type->field (i).name (); |
a73bb892 | 1218 | |
9b972014 | 1219 | if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0)) |
1b20edf0 | 1220 | return convert_field (type, i).release (); |
a73bb892 PK |
1221 | } |
1222 | PyErr_SetObject (PyExc_KeyError, key); | |
1223 | return NULL; | |
1224 | } | |
1225 | ||
256458bc | 1226 | /* Implement the "get" method on the type object. This is the |
a73bb892 PK |
1227 | same as getitem if the key is present, but returns the supplied |
1228 | default value or None if the key is not found. */ | |
1229 | ||
1230 | static PyObject * | |
1231 | typy_get (PyObject *self, PyObject *args) | |
1232 | { | |
1233 | PyObject *key, *defval = Py_None, *result; | |
256458bc | 1234 | |
a73bb892 PK |
1235 | if (!PyArg_UnpackTuple (args, "get", 1, 2, &key, &defval)) |
1236 | return NULL; | |
256458bc | 1237 | |
a73bb892 PK |
1238 | result = typy_getitem (self, key); |
1239 | if (result != NULL) | |
1240 | return result; | |
256458bc | 1241 | |
a73bb892 PK |
1242 | /* typy_getitem returned error status. If the exception is |
1243 | KeyError, clear the exception status and return the defval | |
1244 | instead. Otherwise return the exception unchanged. */ | |
1245 | if (!PyErr_ExceptionMatches (PyExc_KeyError)) | |
1246 | return NULL; | |
256458bc | 1247 | |
a73bb892 PK |
1248 | PyErr_Clear (); |
1249 | Py_INCREF (defval); | |
1250 | return defval; | |
1251 | } | |
1252 | ||
1253 | /* Implement the "has_key" method on the type object. */ | |
1254 | ||
1255 | static PyObject * | |
1256 | typy_has_key (PyObject *self, PyObject *args) | |
1257 | { | |
1258 | struct type *type = ((type_object *) self)->type; | |
2ff6b080 | 1259 | const char *field; |
a73bb892 | 1260 | int i; |
76dce0be | 1261 | |
a73bb892 PK |
1262 | if (!PyArg_ParseTuple (args, "s", &field)) |
1263 | return NULL; | |
1264 | ||
256458bc | 1265 | /* We want just fields of this type, not of base types, so instead of |
a73bb892 PK |
1266 | using lookup_struct_elt_type, portions of that function are |
1267 | copied here. */ | |
1268 | ||
9cc10fd1 PK |
1269 | type = typy_get_composite (type); |
1270 | if (type == NULL) | |
1271 | return NULL; | |
a73bb892 | 1272 | |
1f704f76 | 1273 | for (i = 0; i < type->num_fields (); i++) |
a73bb892 | 1274 | { |
33d16dd9 | 1275 | const char *t_field_name = type->field (i).name (); |
a73bb892 PK |
1276 | |
1277 | if (t_field_name && (strcmp_iw (t_field_name, field) == 0)) | |
1278 | Py_RETURN_TRUE; | |
1279 | } | |
1280 | Py_RETURN_FALSE; | |
1281 | } | |
1282 | ||
1283 | /* Make an iterator object to iterate over keys, values, or items. */ | |
1284 | ||
1285 | static PyObject * | |
1286 | typy_make_iter (PyObject *self, enum gdbpy_iter_kind kind) | |
1287 | { | |
1288 | typy_iterator_object *typy_iter_obj; | |
1289 | ||
9cc10fd1 PK |
1290 | /* Check that "self" is a structure or union type. */ |
1291 | if (typy_get_composite (((type_object *) self)->type) == NULL) | |
1292 | return NULL; | |
256458bc | 1293 | |
a73bb892 PK |
1294 | typy_iter_obj = PyObject_New (typy_iterator_object, |
1295 | &type_iterator_object_type); | |
1296 | if (typy_iter_obj == NULL) | |
1297 | return NULL; | |
1298 | ||
1299 | typy_iter_obj->field = 0; | |
1300 | typy_iter_obj->kind = kind; | |
1301 | Py_INCREF (self); | |
1302 | typy_iter_obj->source = (type_object *) self; | |
1303 | ||
1304 | return (PyObject *) typy_iter_obj; | |
1305 | } | |
1306 | ||
1307 | /* iteritems() method. */ | |
1308 | ||
1309 | static PyObject * | |
1310 | typy_iteritems (PyObject *self, PyObject *args) | |
1311 | { | |
1312 | return typy_make_iter (self, iter_items); | |
1313 | } | |
1314 | ||
1315 | /* iterkeys() method. */ | |
1316 | ||
1317 | static PyObject * | |
1318 | typy_iterkeys (PyObject *self, PyObject *args) | |
1319 | { | |
1320 | return typy_make_iter (self, iter_keys); | |
1321 | } | |
1322 | ||
1323 | /* Iterating over the class, same as iterkeys except for the function | |
1324 | signature. */ | |
1325 | ||
1326 | static PyObject * | |
1327 | typy_iter (PyObject *self) | |
1328 | { | |
1329 | return typy_make_iter (self, iter_keys); | |
1330 | } | |
1331 | ||
1332 | /* itervalues() method. */ | |
1333 | ||
1334 | static PyObject * | |
1335 | typy_itervalues (PyObject *self, PyObject *args) | |
1336 | { | |
1337 | return typy_make_iter (self, iter_values); | |
1338 | } | |
1339 | ||
1340 | /* Return a reference to the type iterator. */ | |
1341 | ||
1342 | static PyObject * | |
1343 | typy_iterator_iter (PyObject *self) | |
1344 | { | |
1345 | Py_INCREF (self); | |
1346 | return self; | |
1347 | } | |
1348 | ||
1349 | /* Return the next field in the iteration through the list of fields | |
1350 | of the type. */ | |
1351 | ||
1352 | static PyObject * | |
1353 | typy_iterator_iternext (PyObject *self) | |
1354 | { | |
1355 | typy_iterator_object *iter_obj = (typy_iterator_object *) self; | |
1356 | struct type *type = iter_obj->source->type; | |
256458bc | 1357 | |
1f704f76 | 1358 | if (iter_obj->field < type->num_fields ()) |
a73bb892 | 1359 | { |
1b20edf0 TT |
1360 | gdbpy_ref<> result = make_fielditem (type, iter_obj->field, |
1361 | iter_obj->kind); | |
a73bb892 PK |
1362 | if (result != NULL) |
1363 | iter_obj->field++; | |
1b20edf0 | 1364 | return result.release (); |
a73bb892 PK |
1365 | } |
1366 | ||
1367 | return NULL; | |
1368 | } | |
1369 | ||
1370 | static void | |
1371 | typy_iterator_dealloc (PyObject *obj) | |
1372 | { | |
1373 | typy_iterator_object *iter_obj = (typy_iterator_object *) obj; | |
1374 | ||
1375 | Py_DECREF (iter_obj->source); | |
2e953aca | 1376 | Py_TYPE (obj)->tp_free (obj); |
a73bb892 PK |
1377 | } |
1378 | ||
2c74e833 TT |
1379 | /* Create a new Type referring to TYPE. */ |
1380 | PyObject * | |
1381 | type_to_type_object (struct type *type) | |
1382 | { | |
1383 | type_object *type_obj; | |
1384 | ||
5d63b30a TT |
1385 | try |
1386 | { | |
1387 | /* Try not to let stub types leak out to Python. */ | |
e46d3488 | 1388 | if (type->is_stub ()) |
5d63b30a TT |
1389 | type = check_typedef (type); |
1390 | } | |
1391 | catch (...) | |
1392 | { | |
1393 | /* Just ignore failures in check_typedef. */ | |
1394 | } | |
1395 | ||
2c74e833 TT |
1396 | type_obj = PyObject_New (type_object, &type_object_type); |
1397 | if (type_obj) | |
1398 | set_type (type_obj, type); | |
1399 | ||
1400 | return (PyObject *) type_obj; | |
1401 | } | |
1402 | ||
1403 | struct type * | |
1404 | type_object_to_type (PyObject *obj) | |
1405 | { | |
1406 | if (! PyObject_TypeCheck (obj, &type_object_type)) | |
1407 | return NULL; | |
1408 | return ((type_object *) obj)->type; | |
1409 | } | |
1410 | ||
1411 | \f | |
1412 | ||
1413 | /* Implementation of gdb.lookup_type. */ | |
1414 | PyObject * | |
1415 | gdbpy_lookup_type (PyObject *self, PyObject *args, PyObject *kw) | |
1416 | { | |
2adadf51 | 1417 | static const char *keywords[] = { "name", "block", NULL }; |
ddd49eee | 1418 | const char *type_name = NULL; |
2c74e833 | 1419 | struct type *type = NULL; |
5107b149 | 1420 | PyObject *block_obj = NULL; |
9df2fbc4 | 1421 | const struct block *block = NULL; |
2c74e833 | 1422 | |
2adadf51 PA |
1423 | if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O", keywords, |
1424 | &type_name, &block_obj)) | |
2c74e833 TT |
1425 | return NULL; |
1426 | ||
5107b149 PM |
1427 | if (block_obj) |
1428 | { | |
1429 | block = block_object_to_block (block_obj); | |
1430 | if (! block) | |
1431 | { | |
1432 | PyErr_SetString (PyExc_RuntimeError, | |
1433 | _("'block' argument must be a Block.")); | |
1434 | return NULL; | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | type = typy_lookup_typename (type_name, block); | |
2c74e833 TT |
1439 | if (! type) |
1440 | return NULL; | |
1441 | ||
8833fbf0 | 1442 | return type_to_type_object (type); |
2c74e833 TT |
1443 | } |
1444 | ||
999633ed | 1445 | int |
2c74e833 TT |
1446 | gdbpy_initialize_types (void) |
1447 | { | |
2c74e833 | 1448 | if (PyType_Ready (&type_object_type) < 0) |
999633ed | 1449 | return -1; |
2c74e833 | 1450 | if (PyType_Ready (&field_object_type) < 0) |
999633ed | 1451 | return -1; |
a73bb892 | 1452 | if (PyType_Ready (&type_iterator_object_type) < 0) |
999633ed | 1453 | return -1; |
2c74e833 | 1454 | |
dd1ae8ea | 1455 | for (const auto &item : pyty_codes) |
2c74e833 | 1456 | { |
dd1ae8ea | 1457 | if (PyModule_AddIntConstant (gdb_module, item.name, item.code) < 0) |
999633ed | 1458 | return -1; |
2c74e833 TT |
1459 | } |
1460 | ||
aa36459a TT |
1461 | if (gdb_pymodule_addobject (gdb_module, "Type", |
1462 | (PyObject *) &type_object_type) < 0) | |
999633ed | 1463 | return -1; |
2c74e833 | 1464 | |
aa36459a TT |
1465 | if (gdb_pymodule_addobject (gdb_module, "TypeIterator", |
1466 | (PyObject *) &type_iterator_object_type) < 0) | |
999633ed | 1467 | return -1; |
a73bb892 | 1468 | |
aa36459a TT |
1469 | return gdb_pymodule_addobject (gdb_module, "Field", |
1470 | (PyObject *) &field_object_type); | |
2c74e833 TT |
1471 | } |
1472 | ||
1473 | \f | |
1474 | ||
0d1f4ceb | 1475 | static gdb_PyGetSetDef type_object_getset[] = |
2c74e833 | 1476 | { |
6d7bb824 TT |
1477 | { "alignof", typy_get_alignof, NULL, |
1478 | "The alignment of this type, in bytes.", NULL }, | |
2c74e833 TT |
1479 | { "code", typy_get_code, NULL, |
1480 | "The code for this type.", NULL }, | |
1acda803 TT |
1481 | { "dynamic", typy_get_dynamic, NULL, |
1482 | "Whether this type is dynamic.", NULL }, | |
c0d48811 JB |
1483 | { "name", typy_get_name, NULL, |
1484 | "The name for this type, or None.", NULL }, | |
2c74e833 TT |
1485 | { "sizeof", typy_get_sizeof, NULL, |
1486 | "The size of this type, in bytes.", NULL }, | |
1487 | { "tag", typy_get_tag, NULL, | |
1488 | "The tag name for this type, or None.", NULL }, | |
e1f2e1a2 CB |
1489 | { "objfile", typy_get_objfile, NULL, |
1490 | "The objfile this type was defined in, or None.", NULL }, | |
ee6a3d9e AB |
1491 | { "is_scalar", typy_is_scalar, nullptr, |
1492 | "Is this a scalar type?", nullptr }, | |
551b380f AB |
1493 | { "is_signed", typy_is_signed, nullptr, |
1494 | "Is this an signed type?", nullptr }, | |
2c74e833 TT |
1495 | { NULL } |
1496 | }; | |
1497 | ||
1498 | static PyMethodDef type_object_methods[] = | |
1499 | { | |
702c2711 | 1500 | { "array", typy_array, METH_VARARGS, |
f28c316a DE |
1501 | "array ([LOW_BOUND,] HIGH_BOUND) -> Type\n\ |
1502 | Return a type which represents an array of objects of this type.\n\ | |
1503 | The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\ | |
1504 | If LOW_BOUND is omitted, a value of zero is used." }, | |
a72c3253 DE |
1505 | { "vector", typy_vector, METH_VARARGS, |
1506 | "vector ([LOW_BOUND,] HIGH_BOUND) -> Type\n\ | |
1507 | Return a type which represents a vector of objects of this type.\n\ | |
1508 | The bounds of the array are [LOW_BOUND, HIGH_BOUND] inclusive.\n\ | |
1509 | If LOW_BOUND is omitted, a value of zero is used.\n\ | |
1510 | Vectors differ from arrays in that if the current language has C-style\n\ | |
1511 | arrays, vectors don't decay to a pointer to the first element.\n\ | |
1512 | They are first class values." }, | |
a73bb892 PK |
1513 | { "__contains__", typy_has_key, METH_VARARGS, |
1514 | "T.__contains__(k) -> True if T has a field named k, else False" }, | |
2c74e833 TT |
1515 | { "const", typy_const, METH_NOARGS, |
1516 | "const () -> Type\n\ | |
1517 | Return a const variant of this type." }, | |
59fb7612 SS |
1518 | { "optimized_out", typy_optimized_out, METH_NOARGS, |
1519 | "optimized_out() -> Value\n\ | |
1520 | Return optimized out value of this type." }, | |
2c74e833 | 1521 | { "fields", typy_fields, METH_NOARGS, |
a73bb892 PK |
1522 | "fields () -> list\n\ |
1523 | Return a list holding all the fields of this type.\n\ | |
1524 | Each field is a gdb.Field object." }, | |
1525 | { "get", typy_get, METH_VARARGS, | |
1526 | "T.get(k[,default]) -> returns field named k in T, if it exists;\n\ | |
1527 | otherwise returns default, if supplied, or None if not." }, | |
1528 | { "has_key", typy_has_key, METH_VARARGS, | |
1529 | "T.has_key(k) -> True if T has a field named k, else False" }, | |
1530 | { "items", typy_items, METH_NOARGS, | |
1531 | "items () -> list\n\ | |
1532 | Return a list of (name, field) pairs of this type.\n\ | |
1533 | Each field is a gdb.Field object." }, | |
1534 | { "iteritems", typy_iteritems, METH_NOARGS, | |
1535 | "iteritems () -> an iterator over the (name, field)\n\ | |
1536 | pairs of this type. Each field is a gdb.Field object." }, | |
1537 | { "iterkeys", typy_iterkeys, METH_NOARGS, | |
1538 | "iterkeys () -> an iterator over the field names of this type." }, | |
1539 | { "itervalues", typy_itervalues, METH_NOARGS, | |
1540 | "itervalues () -> an iterator over the fields of this type.\n\ | |
1541 | Each field is a gdb.Field object." }, | |
1542 | { "keys", typy_field_names, METH_NOARGS, | |
1543 | "keys () -> list\n\ | |
1544 | Return a list holding all the fields names of this type." }, | |
2c74e833 TT |
1545 | { "pointer", typy_pointer, METH_NOARGS, |
1546 | "pointer () -> Type\n\ | |
1547 | Return a type of pointer to this type." }, | |
361ae042 PM |
1548 | { "range", typy_range, METH_NOARGS, |
1549 | "range () -> tuple\n\ | |
1550 | Return a tuple containing the lower and upper range for this type."}, | |
2c74e833 TT |
1551 | { "reference", typy_reference, METH_NOARGS, |
1552 | "reference () -> Type\n\ | |
1553 | Return a type of reference to this type." }, | |
1554 | { "strip_typedefs", typy_strip_typedefs, METH_NOARGS, | |
1555 | "strip_typedefs () -> Type\n\ | |
1556 | Return a type formed by stripping this type of all typedefs."}, | |
1557 | { "target", typy_target, METH_NOARGS, | |
1558 | "target () -> Type\n\ | |
1559 | Return the target type of this type." }, | |
1560 | { "template_argument", typy_template_argument, METH_VARARGS, | |
5107b149 | 1561 | "template_argument (arg, [block]) -> Type\n\ |
2c74e833 TT |
1562 | Return the type of a template argument." }, |
1563 | { "unqualified", typy_unqualified, METH_NOARGS, | |
1564 | "unqualified () -> Type\n\ | |
1565 | Return a variant of this type without const or volatile attributes." }, | |
9cc10fd1 | 1566 | { "values", typy_values, METH_NOARGS, |
a73bb892 PK |
1567 | "values () -> list\n\ |
1568 | Return a list holding all the fields of this type.\n\ | |
1569 | Each field is a gdb.Field object." }, | |
2c74e833 TT |
1570 | { "volatile", typy_volatile, METH_NOARGS, |
1571 | "volatile () -> Type\n\ | |
1572 | Return a volatile variant of this type" }, | |
1573 | { NULL } | |
1574 | }; | |
1575 | ||
9cc10fd1 PK |
1576 | static PyNumberMethods type_object_as_number = { |
1577 | NULL, /* nb_add */ | |
1578 | NULL, /* nb_subtract */ | |
1579 | NULL, /* nb_multiply */ | |
9cc10fd1 PK |
1580 | NULL, /* nb_remainder */ |
1581 | NULL, /* nb_divmod */ | |
1582 | NULL, /* nb_power */ | |
1583 | NULL, /* nb_negative */ | |
1584 | NULL, /* nb_positive */ | |
1585 | NULL, /* nb_absolute */ | |
1586 | typy_nonzero, /* nb_nonzero */ | |
1587 | NULL, /* nb_invert */ | |
1588 | NULL, /* nb_lshift */ | |
1589 | NULL, /* nb_rshift */ | |
1590 | NULL, /* nb_and */ | |
1591 | NULL, /* nb_xor */ | |
1592 | NULL, /* nb_or */ | |
9a27f2c6 PK |
1593 | NULL, /* nb_int */ |
1594 | NULL, /* reserved */ | |
9cc10fd1 | 1595 | NULL, /* nb_float */ |
9cc10fd1 PK |
1596 | }; |
1597 | ||
a73bb892 PK |
1598 | static PyMappingMethods typy_mapping = { |
1599 | typy_length, | |
1600 | typy_getitem, | |
1601 | NULL /* no "set" method */ | |
1602 | }; | |
1603 | ||
e36122e9 | 1604 | PyTypeObject type_object_type = |
2c74e833 | 1605 | { |
9a27f2c6 | 1606 | PyVarObject_HEAD_INIT (NULL, 0) |
2c74e833 TT |
1607 | "gdb.Type", /*tp_name*/ |
1608 | sizeof (type_object), /*tp_basicsize*/ | |
1609 | 0, /*tp_itemsize*/ | |
1610 | typy_dealloc, /*tp_dealloc*/ | |
1611 | 0, /*tp_print*/ | |
1612 | 0, /*tp_getattr*/ | |
1613 | 0, /*tp_setattr*/ | |
1614 | 0, /*tp_compare*/ | |
1615 | 0, /*tp_repr*/ | |
9cc10fd1 | 1616 | &type_object_as_number, /*tp_as_number*/ |
2c74e833 | 1617 | 0, /*tp_as_sequence*/ |
a73bb892 | 1618 | &typy_mapping, /*tp_as_mapping*/ |
2c74e833 TT |
1619 | 0, /*tp_hash */ |
1620 | 0, /*tp_call*/ | |
1621 | typy_str, /*tp_str*/ | |
1622 | 0, /*tp_getattro*/ | |
1623 | 0, /*tp_setattro*/ | |
1624 | 0, /*tp_as_buffer*/ | |
0b233e34 | 1625 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
2c74e833 TT |
1626 | "GDB type object", /* tp_doc */ |
1627 | 0, /* tp_traverse */ | |
1628 | 0, /* tp_clear */ | |
d839c8a4 | 1629 | typy_richcompare, /* tp_richcompare */ |
2c74e833 | 1630 | 0, /* tp_weaklistoffset */ |
a73bb892 | 1631 | typy_iter, /* tp_iter */ |
2c74e833 TT |
1632 | 0, /* tp_iternext */ |
1633 | type_object_methods, /* tp_methods */ | |
1634 | 0, /* tp_members */ | |
1635 | type_object_getset, /* tp_getset */ | |
1636 | 0, /* tp_base */ | |
1637 | 0, /* tp_dict */ | |
1638 | 0, /* tp_descr_get */ | |
1639 | 0, /* tp_descr_set */ | |
1640 | 0, /* tp_dictoffset */ | |
1641 | 0, /* tp_init */ | |
1642 | 0, /* tp_alloc */ | |
1643 | 0, /* tp_new */ | |
1644 | }; | |
1645 | ||
0d1f4ceb | 1646 | static gdb_PyGetSetDef field_object_getset[] = |
2e8265fd TT |
1647 | { |
1648 | { "__dict__", gdb_py_generic_dict, NULL, | |
1649 | "The __dict__ for this field.", &field_object_type }, | |
1650 | { NULL } | |
1651 | }; | |
1652 | ||
e36122e9 | 1653 | PyTypeObject field_object_type = |
2c74e833 | 1654 | { |
9a27f2c6 | 1655 | PyVarObject_HEAD_INIT (NULL, 0) |
2c74e833 TT |
1656 | "gdb.Field", /*tp_name*/ |
1657 | sizeof (field_object), /*tp_basicsize*/ | |
1658 | 0, /*tp_itemsize*/ | |
1659 | field_dealloc, /*tp_dealloc*/ | |
1660 | 0, /*tp_print*/ | |
1661 | 0, /*tp_getattr*/ | |
1662 | 0, /*tp_setattr*/ | |
1663 | 0, /*tp_compare*/ | |
1664 | 0, /*tp_repr*/ | |
1665 | 0, /*tp_as_number*/ | |
1666 | 0, /*tp_as_sequence*/ | |
1667 | 0, /*tp_as_mapping*/ | |
1668 | 0, /*tp_hash */ | |
1669 | 0, /*tp_call*/ | |
1670 | 0, /*tp_str*/ | |
1671 | 0, /*tp_getattro*/ | |
1672 | 0, /*tp_setattro*/ | |
1673 | 0, /*tp_as_buffer*/ | |
0b233e34 | 1674 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
2c74e833 TT |
1675 | "GDB field object", /* tp_doc */ |
1676 | 0, /* tp_traverse */ | |
1677 | 0, /* tp_clear */ | |
1678 | 0, /* tp_richcompare */ | |
1679 | 0, /* tp_weaklistoffset */ | |
1680 | 0, /* tp_iter */ | |
1681 | 0, /* tp_iternext */ | |
1682 | 0, /* tp_methods */ | |
1683 | 0, /* tp_members */ | |
2e8265fd | 1684 | field_object_getset, /* tp_getset */ |
2c74e833 TT |
1685 | 0, /* tp_base */ |
1686 | 0, /* tp_dict */ | |
1687 | 0, /* tp_descr_get */ | |
1688 | 0, /* tp_descr_set */ | |
1689 | offsetof (field_object, dict), /* tp_dictoffset */ | |
1690 | 0, /* tp_init */ | |
1691 | 0, /* tp_alloc */ | |
1692 | 0, /* tp_new */ | |
1693 | }; | |
a73bb892 | 1694 | |
e36122e9 | 1695 | PyTypeObject type_iterator_object_type = { |
9a27f2c6 | 1696 | PyVarObject_HEAD_INIT (NULL, 0) |
a73bb892 PK |
1697 | "gdb.TypeIterator", /*tp_name*/ |
1698 | sizeof (typy_iterator_object), /*tp_basicsize*/ | |
1699 | 0, /*tp_itemsize*/ | |
1700 | typy_iterator_dealloc, /*tp_dealloc*/ | |
1701 | 0, /*tp_print*/ | |
1702 | 0, /*tp_getattr*/ | |
1703 | 0, /*tp_setattr*/ | |
1704 | 0, /*tp_compare*/ | |
1705 | 0, /*tp_repr*/ | |
1706 | 0, /*tp_as_number*/ | |
1707 | 0, /*tp_as_sequence*/ | |
1708 | 0, /*tp_as_mapping*/ | |
1709 | 0, /*tp_hash */ | |
1710 | 0, /*tp_call*/ | |
1711 | 0, /*tp_str*/ | |
1712 | 0, /*tp_getattro*/ | |
1713 | 0, /*tp_setattro*/ | |
1714 | 0, /*tp_as_buffer*/ | |
0b233e34 | 1715 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
a73bb892 PK |
1716 | "GDB type iterator object", /*tp_doc */ |
1717 | 0, /*tp_traverse */ | |
1718 | 0, /*tp_clear */ | |
1719 | 0, /*tp_richcompare */ | |
1720 | 0, /*tp_weaklistoffset */ | |
1721 | typy_iterator_iter, /*tp_iter */ | |
1722 | typy_iterator_iternext, /*tp_iternext */ | |
1723 | 0 /*tp_methods */ | |
1724 | }; |