py-type.c 44 KB

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