scm-type.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  1. /* Scheme 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. /* See README file in this directory for implementation notes, coding
  15. conventions, et.al. */
  16. #include "defs.h"
  17. #include "arch-utils.h"
  18. #include "value.h"
  19. #include "gdbtypes.h"
  20. #include "objfiles.h"
  21. #include "language.h"
  22. #include "bcache.h"
  23. #include "dwarf2/loc.h"
  24. #include "typeprint.h"
  25. #include "guile-internal.h"
  26. /* The <gdb:type> smob.
  27. The type is chained with all types associated with its objfile, if any.
  28. This lets us copy the underlying struct type when the objfile is
  29. deleted. */
  30. struct type_smob
  31. {
  32. /* This always appears first.
  33. eqable_gdb_smob is used so that types are eq?-able.
  34. Also, a type object can be associated with an objfile. eqable_gdb_smob
  35. lets us track the lifetime of all types associated with an objfile.
  36. When an objfile is deleted we need to invalidate the type object. */
  37. eqable_gdb_smob base;
  38. /* The GDB type structure this smob is wrapping. */
  39. struct type *type;
  40. };
  41. /* A field smob. */
  42. struct field_smob
  43. {
  44. /* This always appears first. */
  45. gdb_smob base;
  46. /* Backlink to the containing <gdb:type> object. */
  47. SCM type_scm;
  48. /* The field number in TYPE_SCM. */
  49. int field_num;
  50. };
  51. static const char type_smob_name[] = "gdb:type";
  52. static const char field_smob_name[] = "gdb:field";
  53. static const char not_composite_error[] =
  54. N_("type is not a structure, union, or enum type");
  55. /* The tag Guile knows the type smob by. */
  56. static scm_t_bits type_smob_tag;
  57. /* The tag Guile knows the field smob by. */
  58. static scm_t_bits field_smob_tag;
  59. /* The "next" procedure for field iterators. */
  60. static SCM tyscm_next_field_x_proc;
  61. /* Keywords used in argument passing. */
  62. static SCM block_keyword;
  63. static const struct objfile_data *tyscm_objfile_data_key;
  64. /* Hash table to uniquify global (non-objfile-owned) types. */
  65. static htab_t global_types_map;
  66. static struct type *tyscm_get_composite (struct type *type);
  67. /* Return the type field of T_SMOB.
  68. This exists so that we don't have to export the struct's contents. */
  69. struct type *
  70. tyscm_type_smob_type (type_smob *t_smob)
  71. {
  72. return t_smob->type;
  73. }
  74. /* Return the name of TYPE in expanded form. If there's an error
  75. computing the name, throws the gdb exception with scm_throw. */
  76. static std::string
  77. tyscm_type_name (struct type *type)
  78. {
  79. SCM excp;
  80. try
  81. {
  82. string_file stb;
  83. current_language->print_type (type, "", &stb, -1, 0,
  84. &type_print_raw_options);
  85. return stb.release ();
  86. }
  87. catch (const gdb_exception &except)
  88. {
  89. excp = gdbscm_scm_from_gdb_exception (unpack (except));
  90. }
  91. gdbscm_throw (excp);
  92. }
  93. /* Administrivia for type smobs. */
  94. /* Helper function to hash a type_smob. */
  95. static hashval_t
  96. tyscm_hash_type_smob (const void *p)
  97. {
  98. const type_smob *t_smob = (const type_smob *) p;
  99. return htab_hash_pointer (t_smob->type);
  100. }
  101. /* Helper function to compute equality of type_smobs. */
  102. static int
  103. tyscm_eq_type_smob (const void *ap, const void *bp)
  104. {
  105. const type_smob *a = (const type_smob *) ap;
  106. const type_smob *b = (const type_smob *) bp;
  107. return (a->type == b->type
  108. && a->type != NULL);
  109. }
  110. /* Return the struct type pointer -> SCM mapping table.
  111. If type is owned by an objfile, the mapping table is created if necessary.
  112. Otherwise, type is not owned by an objfile, and we use
  113. global_types_map. */
  114. static htab_t
  115. tyscm_type_map (struct type *type)
  116. {
  117. struct objfile *objfile = type->objfile_owner ();
  118. htab_t htab;
  119. if (objfile == NULL)
  120. return global_types_map;
  121. htab = (htab_t) objfile_data (objfile, tyscm_objfile_data_key);
  122. if (htab == NULL)
  123. {
  124. htab = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob,
  125. tyscm_eq_type_smob);
  126. set_objfile_data (objfile, tyscm_objfile_data_key, htab);
  127. }
  128. return htab;
  129. }
  130. /* The smob "free" function for <gdb:type>. */
  131. static size_t
  132. tyscm_free_type_smob (SCM self)
  133. {
  134. type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (self);
  135. if (t_smob->type != NULL)
  136. {
  137. htab_t htab = tyscm_type_map (t_smob->type);
  138. gdbscm_clear_eqable_gsmob_ptr_slot (htab, &t_smob->base);
  139. }
  140. /* Not necessary, done to catch bugs. */
  141. t_smob->type = NULL;
  142. return 0;
  143. }
  144. /* The smob "print" function for <gdb:type>. */
  145. static int
  146. tyscm_print_type_smob (SCM self, SCM port, scm_print_state *pstate)
  147. {
  148. type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (self);
  149. std::string name = tyscm_type_name (t_smob->type);
  150. /* pstate->writingp = zero if invoked by display/~A, and nonzero if
  151. invoked by write/~S. What to do here may need to evolve.
  152. IWBN if we could pass an argument to format that would we could use
  153. instead of writingp. */
  154. if (pstate->writingp)
  155. gdbscm_printf (port, "#<%s ", type_smob_name);
  156. scm_puts (name.c_str (), port);
  157. if (pstate->writingp)
  158. scm_puts (">", port);
  159. scm_remember_upto_here_1 (self);
  160. /* Non-zero means success. */
  161. return 1;
  162. }
  163. /* The smob "equal?" function for <gdb:type>. */
  164. static SCM
  165. tyscm_equal_p_type_smob (SCM type1_scm, SCM type2_scm)
  166. {
  167. type_smob *type1_smob, *type2_smob;
  168. struct type *type1, *type2;
  169. bool result = false;
  170. SCM_ASSERT_TYPE (tyscm_is_type (type1_scm), type1_scm, SCM_ARG1, FUNC_NAME,
  171. type_smob_name);
  172. SCM_ASSERT_TYPE (tyscm_is_type (type2_scm), type2_scm, SCM_ARG2, FUNC_NAME,
  173. type_smob_name);
  174. type1_smob = (type_smob *) SCM_SMOB_DATA (type1_scm);
  175. type2_smob = (type_smob *) SCM_SMOB_DATA (type2_scm);
  176. type1 = type1_smob->type;
  177. type2 = type2_smob->type;
  178. gdbscm_gdb_exception exc {};
  179. try
  180. {
  181. result = types_deeply_equal (type1, type2);
  182. }
  183. catch (const gdb_exception &except)
  184. {
  185. exc = unpack (except);
  186. }
  187. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  188. return scm_from_bool (result);
  189. }
  190. /* Low level routine to create a <gdb:type> object. */
  191. static SCM
  192. tyscm_make_type_smob (void)
  193. {
  194. type_smob *t_smob = (type_smob *)
  195. scm_gc_malloc (sizeof (type_smob), type_smob_name);
  196. SCM t_scm;
  197. /* This must be filled in by the caller. */
  198. t_smob->type = NULL;
  199. t_scm = scm_new_smob (type_smob_tag, (scm_t_bits) t_smob);
  200. gdbscm_init_eqable_gsmob (&t_smob->base, t_scm);
  201. return t_scm;
  202. }
  203. /* Return non-zero if SCM is a <gdb:type> object. */
  204. int
  205. tyscm_is_type (SCM self)
  206. {
  207. return SCM_SMOB_PREDICATE (type_smob_tag, self);
  208. }
  209. /* (type? object) -> boolean */
  210. static SCM
  211. gdbscm_type_p (SCM self)
  212. {
  213. return scm_from_bool (tyscm_is_type (self));
  214. }
  215. /* Return the existing object that encapsulates TYPE, or create a new
  216. <gdb:type> object. */
  217. SCM
  218. tyscm_scm_from_type (struct type *type)
  219. {
  220. htab_t htab;
  221. eqable_gdb_smob **slot;
  222. type_smob *t_smob, t_smob_for_lookup;
  223. SCM t_scm;
  224. /* If we've already created a gsmob for this type, return it.
  225. This makes types eq?-able. */
  226. htab = tyscm_type_map (type);
  227. t_smob_for_lookup.type = type;
  228. slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &t_smob_for_lookup.base);
  229. if (*slot != NULL)
  230. return (*slot)->containing_scm;
  231. t_scm = tyscm_make_type_smob ();
  232. t_smob = (type_smob *) SCM_SMOB_DATA (t_scm);
  233. t_smob->type = type;
  234. gdbscm_fill_eqable_gsmob_ptr_slot (slot, &t_smob->base);
  235. return t_scm;
  236. }
  237. /* Returns the <gdb:type> object in SELF.
  238. Throws an exception if SELF is not a <gdb:type> object. */
  239. static SCM
  240. tyscm_get_type_arg_unsafe (SCM self, int arg_pos, const char *func_name)
  241. {
  242. SCM_ASSERT_TYPE (tyscm_is_type (self), self, arg_pos, func_name,
  243. type_smob_name);
  244. return self;
  245. }
  246. /* Returns a pointer to the type smob of SELF.
  247. Throws an exception if SELF is not a <gdb:type> object. */
  248. type_smob *
  249. tyscm_get_type_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
  250. {
  251. SCM t_scm = tyscm_get_type_arg_unsafe (self, arg_pos, func_name);
  252. type_smob *t_smob = (type_smob *) SCM_SMOB_DATA (t_scm);
  253. return t_smob;
  254. }
  255. /* Return the type field of T_SCM, an object of type <gdb:type>.
  256. This exists so that we don't have to export the struct's contents. */
  257. struct type *
  258. tyscm_scm_to_type (SCM t_scm)
  259. {
  260. type_smob *t_smob;
  261. gdb_assert (tyscm_is_type (t_scm));
  262. t_smob = (type_smob *) SCM_SMOB_DATA (t_scm);
  263. return t_smob->type;
  264. }
  265. /* Helper function for save_objfile_types to make a deep copy of the type. */
  266. static int
  267. tyscm_copy_type_recursive (void **slot, void *info)
  268. {
  269. type_smob *t_smob = (type_smob *) *slot;
  270. htab_t copied_types = (htab_t) info;
  271. struct objfile *objfile = t_smob->type->objfile_owner ();
  272. htab_t htab;
  273. eqable_gdb_smob **new_slot;
  274. type_smob t_smob_for_lookup;
  275. gdb_assert (objfile != NULL);
  276. htab_empty (copied_types);
  277. t_smob->type = copy_type_recursive (objfile, t_smob->type, copied_types);
  278. /* The eq?-hashtab that the type lived in is going away.
  279. Add the type to its new eq?-hashtab: Otherwise if/when the type is later
  280. garbage collected we'll assert-fail if the type isn't in the hashtab.
  281. PR 16612.
  282. Types now live in "arch space", and things like "char" that came from
  283. the objfile *could* be considered eq? with the arch "char" type.
  284. However, they weren't before the objfile got deleted, so making them
  285. eq? now is debatable. */
  286. htab = tyscm_type_map (t_smob->type);
  287. t_smob_for_lookup.type = t_smob->type;
  288. new_slot = gdbscm_find_eqable_gsmob_ptr_slot (htab, &t_smob_for_lookup.base);
  289. gdb_assert (*new_slot == NULL);
  290. gdbscm_fill_eqable_gsmob_ptr_slot (new_slot, &t_smob->base);
  291. return 1;
  292. }
  293. /* Called when OBJFILE is about to be deleted.
  294. Make a copy of all types associated with OBJFILE. */
  295. static void
  296. save_objfile_types (struct objfile *objfile, void *datum)
  297. {
  298. htab_t htab = (htab_t) datum;
  299. if (!gdb_scheme_initialized)
  300. return;
  301. htab_up copied_types = create_copied_types_hash (objfile);
  302. if (htab != NULL)
  303. {
  304. htab_traverse_noresize (htab, tyscm_copy_type_recursive, copied_types.get ());
  305. htab_delete (htab);
  306. }
  307. }
  308. /* Administrivia for field smobs. */
  309. /* The smob "print" function for <gdb:field>. */
  310. static int
  311. tyscm_print_field_smob (SCM self, SCM port, scm_print_state *pstate)
  312. {
  313. field_smob *f_smob = (field_smob *) SCM_SMOB_DATA (self);
  314. gdbscm_printf (port, "#<%s ", field_smob_name);
  315. scm_write (f_smob->type_scm, port);
  316. gdbscm_printf (port, " %d", f_smob->field_num);
  317. scm_puts (">", port);
  318. scm_remember_upto_here_1 (self);
  319. /* Non-zero means success. */
  320. return 1;
  321. }
  322. /* Low level routine to create a <gdb:field> object for field FIELD_NUM
  323. of type TYPE_SCM. */
  324. static SCM
  325. tyscm_make_field_smob (SCM type_scm, int field_num)
  326. {
  327. field_smob *f_smob = (field_smob *)
  328. scm_gc_malloc (sizeof (field_smob), field_smob_name);
  329. SCM result;
  330. f_smob->type_scm = type_scm;
  331. f_smob->field_num = field_num;
  332. result = scm_new_smob (field_smob_tag, (scm_t_bits) f_smob);
  333. gdbscm_init_gsmob (&f_smob->base);
  334. return result;
  335. }
  336. /* Return non-zero if SCM is a <gdb:field> object. */
  337. static int
  338. tyscm_is_field (SCM self)
  339. {
  340. return SCM_SMOB_PREDICATE (field_smob_tag, self);
  341. }
  342. /* (field? object) -> boolean */
  343. static SCM
  344. gdbscm_field_p (SCM self)
  345. {
  346. return scm_from_bool (tyscm_is_field (self));
  347. }
  348. /* Create a new <gdb:field> object that encapsulates field FIELD_NUM
  349. in type TYPE_SCM. */
  350. SCM
  351. tyscm_scm_from_field (SCM type_scm, int field_num)
  352. {
  353. return tyscm_make_field_smob (type_scm, field_num);
  354. }
  355. /* Returns the <gdb:field> object in SELF.
  356. Throws an exception if SELF is not a <gdb:field> object. */
  357. static SCM
  358. tyscm_get_field_arg_unsafe (SCM self, int arg_pos, const char *func_name)
  359. {
  360. SCM_ASSERT_TYPE (tyscm_is_field (self), self, arg_pos, func_name,
  361. field_smob_name);
  362. return self;
  363. }
  364. /* Returns a pointer to the field smob of SELF.
  365. Throws an exception if SELF is not a <gdb:field> object. */
  366. static field_smob *
  367. tyscm_get_field_smob_arg_unsafe (SCM self, int arg_pos, const char *func_name)
  368. {
  369. SCM f_scm = tyscm_get_field_arg_unsafe (self, arg_pos, func_name);
  370. field_smob *f_smob = (field_smob *) SCM_SMOB_DATA (f_scm);
  371. return f_smob;
  372. }
  373. /* Returns a pointer to the type struct in F_SMOB
  374. (the type the field is in). */
  375. static struct type *
  376. tyscm_field_smob_containing_type (field_smob *f_smob)
  377. {
  378. type_smob *t_smob;
  379. gdb_assert (tyscm_is_type (f_smob->type_scm));
  380. t_smob = (type_smob *) SCM_SMOB_DATA (f_smob->type_scm);
  381. return t_smob->type;
  382. }
  383. /* Returns a pointer to the field struct of F_SMOB. */
  384. static struct field *
  385. tyscm_field_smob_to_field (field_smob *f_smob)
  386. {
  387. struct type *type = tyscm_field_smob_containing_type (f_smob);
  388. /* This should be non-NULL by construction. */
  389. gdb_assert (type->fields () != NULL);
  390. return &type->field (f_smob->field_num);
  391. }
  392. /* Type smob accessors. */
  393. /* (type-code <gdb:type>) -> integer
  394. Return the code for this type. */
  395. static SCM
  396. gdbscm_type_code (SCM self)
  397. {
  398. type_smob *t_smob
  399. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  400. struct type *type = t_smob->type;
  401. return scm_from_int (type->code ());
  402. }
  403. /* (type-fields <gdb:type>) -> list
  404. Return a list of all fields. Each element is a <gdb:field> object.
  405. This also supports arrays, we return a field list of one element,
  406. the range type. */
  407. static SCM
  408. gdbscm_type_fields (SCM self)
  409. {
  410. type_smob *t_smob
  411. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  412. struct type *type = t_smob->type;
  413. struct type *containing_type;
  414. SCM containing_type_scm, result;
  415. int i;
  416. containing_type = tyscm_get_composite (type);
  417. if (containing_type == NULL)
  418. gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
  419. _(not_composite_error));
  420. /* If SELF is a typedef or reference, we want the underlying type,
  421. which is what tyscm_get_composite returns. */
  422. if (containing_type == type)
  423. containing_type_scm = self;
  424. else
  425. containing_type_scm = tyscm_scm_from_type (containing_type);
  426. result = SCM_EOL;
  427. for (i = 0; i < containing_type->num_fields (); ++i)
  428. result = scm_cons (tyscm_make_field_smob (containing_type_scm, i), result);
  429. return scm_reverse_x (result, SCM_EOL);
  430. }
  431. /* (type-tag <gdb:type>) -> string
  432. Return the type's tag, or #f. */
  433. static SCM
  434. gdbscm_type_tag (SCM self)
  435. {
  436. type_smob *t_smob
  437. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  438. struct type *type = t_smob->type;
  439. const char *tagname = nullptr;
  440. if (type->code () == TYPE_CODE_STRUCT
  441. || type->code () == TYPE_CODE_UNION
  442. || type->code () == TYPE_CODE_ENUM)
  443. tagname = type->name ();
  444. if (tagname == nullptr)
  445. return SCM_BOOL_F;
  446. return gdbscm_scm_from_c_string (tagname);
  447. }
  448. /* (type-name <gdb:type>) -> string
  449. Return the type's name, or #f. */
  450. static SCM
  451. gdbscm_type_name (SCM self)
  452. {
  453. type_smob *t_smob
  454. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  455. struct type *type = t_smob->type;
  456. if (!type->name ())
  457. return SCM_BOOL_F;
  458. return gdbscm_scm_from_c_string (type->name ());
  459. }
  460. /* (type-print-name <gdb:type>) -> string
  461. Return the print name of type.
  462. TODO: template support elided for now. */
  463. static SCM
  464. gdbscm_type_print_name (SCM self)
  465. {
  466. type_smob *t_smob
  467. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  468. struct type *type = t_smob->type;
  469. std::string thetype = tyscm_type_name (type);
  470. SCM result = gdbscm_scm_from_c_string (thetype.c_str ());
  471. return result;
  472. }
  473. /* (type-sizeof <gdb:type>) -> integer
  474. Return the size of the type represented by SELF, in bytes. */
  475. static SCM
  476. gdbscm_type_sizeof (SCM self)
  477. {
  478. type_smob *t_smob
  479. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  480. struct type *type = t_smob->type;
  481. try
  482. {
  483. check_typedef (type);
  484. }
  485. catch (const gdb_exception &except)
  486. {
  487. }
  488. /* Ignore exceptions. */
  489. return scm_from_long (TYPE_LENGTH (type));
  490. }
  491. /* (type-strip-typedefs <gdb:type>) -> <gdb:type>
  492. Return the type, stripped of typedefs. */
  493. static SCM
  494. gdbscm_type_strip_typedefs (SCM self)
  495. {
  496. type_smob *t_smob
  497. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  498. struct type *type = t_smob->type;
  499. gdbscm_gdb_exception exc {};
  500. try
  501. {
  502. type = check_typedef (type);
  503. }
  504. catch (const gdb_exception &except)
  505. {
  506. exc = unpack (except);
  507. }
  508. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  509. return tyscm_scm_from_type (type);
  510. }
  511. /* Strip typedefs and pointers/reference from a type. Then check that
  512. it is a struct, union, or enum type. If not, return NULL. */
  513. static struct type *
  514. tyscm_get_composite (struct type *type)
  515. {
  516. for (;;)
  517. {
  518. gdbscm_gdb_exception exc {};
  519. try
  520. {
  521. type = check_typedef (type);
  522. }
  523. catch (const gdb_exception &except)
  524. {
  525. exc = unpack (except);
  526. }
  527. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  528. if (type->code () != TYPE_CODE_PTR
  529. && type->code () != TYPE_CODE_REF)
  530. break;
  531. type = TYPE_TARGET_TYPE (type);
  532. }
  533. /* If this is not a struct, union, or enum type, raise TypeError
  534. exception. */
  535. if (type->code () != TYPE_CODE_STRUCT
  536. && type->code () != TYPE_CODE_UNION
  537. && type->code () != TYPE_CODE_ENUM)
  538. return NULL;
  539. return type;
  540. }
  541. /* Helper for tyscm_array and tyscm_vector. */
  542. static SCM
  543. tyscm_array_1 (SCM self, SCM n1_scm, SCM n2_scm, int is_vector,
  544. const char *func_name)
  545. {
  546. type_smob *t_smob
  547. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, func_name);
  548. struct type *type = t_smob->type;
  549. long n1, n2 = 0;
  550. struct type *array = NULL;
  551. gdbscm_parse_function_args (func_name, SCM_ARG2, NULL, "l|l",
  552. n1_scm, &n1, n2_scm, &n2);
  553. if (SCM_UNBNDP (n2_scm))
  554. {
  555. n2 = n1;
  556. n1 = 0;
  557. }
  558. if (n2 < n1 - 1) /* Note: An empty array has n2 == n1 - 1. */
  559. {
  560. gdbscm_out_of_range_error (func_name, SCM_ARG3,
  561. scm_cons (scm_from_long (n1),
  562. scm_from_long (n2)),
  563. _("Array length must not be negative"));
  564. }
  565. gdbscm_gdb_exception exc {};
  566. try
  567. {
  568. array = lookup_array_range_type (type, n1, n2);
  569. if (is_vector)
  570. make_vector_type (array);
  571. }
  572. catch (const gdb_exception &except)
  573. {
  574. exc = unpack (except);
  575. }
  576. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  577. return tyscm_scm_from_type (array);
  578. }
  579. /* (type-array <gdb:type> [low-bound] high-bound) -> <gdb:type>
  580. The array has indices [low-bound,high-bound].
  581. If low-bound is not provided zero is used.
  582. Return an array type.
  583. IWBN if the one argument version specified a size, not the high bound.
  584. It's too easy to pass one argument thinking it is the size of the array.
  585. The current semantics are for compatibility with the Python version.
  586. Later we can add #:size. */
  587. static SCM
  588. gdbscm_type_array (SCM self, SCM n1, SCM n2)
  589. {
  590. return tyscm_array_1 (self, n1, n2, 0, FUNC_NAME);
  591. }
  592. /* (type-vector <gdb:type> [low-bound] high-bound) -> <gdb:type>
  593. The array has indices [low-bound,high-bound].
  594. If low-bound is not provided zero is used.
  595. Return a vector type.
  596. IWBN if the one argument version specified a size, not the high bound.
  597. It's too easy to pass one argument thinking it is the size of the array.
  598. The current semantics are for compatibility with the Python version.
  599. Later we can add #:size. */
  600. static SCM
  601. gdbscm_type_vector (SCM self, SCM n1, SCM n2)
  602. {
  603. return tyscm_array_1 (self, n1, n2, 1, FUNC_NAME);
  604. }
  605. /* (type-pointer <gdb:type>) -> <gdb:type>
  606. Return a <gdb:type> object which represents a pointer to SELF. */
  607. static SCM
  608. gdbscm_type_pointer (SCM self)
  609. {
  610. type_smob *t_smob
  611. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  612. struct type *type = t_smob->type;
  613. gdbscm_gdb_exception exc {};
  614. try
  615. {
  616. type = lookup_pointer_type (type);
  617. }
  618. catch (const gdb_exception &except)
  619. {
  620. exc = unpack (except);
  621. }
  622. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  623. return tyscm_scm_from_type (type);
  624. }
  625. /* (type-range <gdb:type>) -> (low high)
  626. Return the range of a type represented by SELF. The return type is
  627. a list. The first element is the low bound, and the second element
  628. is the high bound. */
  629. static SCM
  630. gdbscm_type_range (SCM self)
  631. {
  632. type_smob *t_smob
  633. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  634. struct type *type = t_smob->type;
  635. SCM low_scm, high_scm;
  636. /* Initialize these to appease GCC warnings. */
  637. LONGEST low = 0, high = 0;
  638. SCM_ASSERT_TYPE (type->code () == TYPE_CODE_ARRAY
  639. || type->code () == TYPE_CODE_STRING
  640. || type->code () == TYPE_CODE_RANGE,
  641. self, SCM_ARG1, FUNC_NAME, _("ranged type"));
  642. switch (type->code ())
  643. {
  644. case TYPE_CODE_ARRAY:
  645. case TYPE_CODE_STRING:
  646. case TYPE_CODE_RANGE:
  647. if (type->bounds ()->low.kind () == PROP_CONST)
  648. low = type->bounds ()->low.const_val ();
  649. else
  650. low = 0;
  651. if (type->bounds ()->high.kind () == PROP_CONST)
  652. high = type->bounds ()->high.const_val ();
  653. else
  654. high = 0;
  655. break;
  656. }
  657. low_scm = gdbscm_scm_from_longest (low);
  658. high_scm = gdbscm_scm_from_longest (high);
  659. return scm_list_2 (low_scm, high_scm);
  660. }
  661. /* (type-reference <gdb:type>) -> <gdb:type>
  662. Return a <gdb:type> object which represents a reference to SELF. */
  663. static SCM
  664. gdbscm_type_reference (SCM self)
  665. {
  666. type_smob *t_smob
  667. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  668. struct type *type = t_smob->type;
  669. gdbscm_gdb_exception exc {};
  670. try
  671. {
  672. type = lookup_lvalue_reference_type (type);
  673. }
  674. catch (const gdb_exception &except)
  675. {
  676. exc = unpack (except);
  677. }
  678. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  679. return tyscm_scm_from_type (type);
  680. }
  681. /* (type-target <gdb:type>) -> <gdb:type>
  682. Return a <gdb:type> object which represents the target type of SELF. */
  683. static SCM
  684. gdbscm_type_target (SCM self)
  685. {
  686. type_smob *t_smob
  687. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  688. struct type *type = t_smob->type;
  689. SCM_ASSERT (TYPE_TARGET_TYPE (type), self, SCM_ARG1, FUNC_NAME);
  690. return tyscm_scm_from_type (TYPE_TARGET_TYPE (type));
  691. }
  692. /* (type-const <gdb:type>) -> <gdb:type>
  693. Return a const-qualified type variant. */
  694. static SCM
  695. gdbscm_type_const (SCM self)
  696. {
  697. type_smob *t_smob
  698. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  699. struct type *type = t_smob->type;
  700. gdbscm_gdb_exception exc {};
  701. try
  702. {
  703. type = make_cv_type (1, 0, type, NULL);
  704. }
  705. catch (const gdb_exception &except)
  706. {
  707. exc = unpack (except);
  708. }
  709. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  710. return tyscm_scm_from_type (type);
  711. }
  712. /* (type-volatile <gdb:type>) -> <gdb:type>
  713. Return a volatile-qualified type variant. */
  714. static SCM
  715. gdbscm_type_volatile (SCM self)
  716. {
  717. type_smob *t_smob
  718. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  719. struct type *type = t_smob->type;
  720. gdbscm_gdb_exception exc {};
  721. try
  722. {
  723. type = make_cv_type (0, 1, type, NULL);
  724. }
  725. catch (const gdb_exception &except)
  726. {
  727. exc = unpack (except);
  728. }
  729. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  730. return tyscm_scm_from_type (type);
  731. }
  732. /* (type-unqualified <gdb:type>) -> <gdb:type>
  733. Return an unqualified type variant. */
  734. static SCM
  735. gdbscm_type_unqualified (SCM self)
  736. {
  737. type_smob *t_smob
  738. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  739. struct type *type = t_smob->type;
  740. gdbscm_gdb_exception exc {};
  741. try
  742. {
  743. type = make_cv_type (0, 0, type, NULL);
  744. }
  745. catch (const gdb_exception &except)
  746. {
  747. exc = unpack (except);
  748. }
  749. GDBSCM_HANDLE_GDB_EXCEPTION (exc);
  750. return tyscm_scm_from_type (type);
  751. }
  752. /* Field related accessors of types. */
  753. /* (type-num-fields <gdb:type>) -> integer
  754. Return number of fields. */
  755. static SCM
  756. gdbscm_type_num_fields (SCM self)
  757. {
  758. type_smob *t_smob
  759. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  760. struct type *type = t_smob->type;
  761. type = tyscm_get_composite (type);
  762. if (type == NULL)
  763. gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
  764. _(not_composite_error));
  765. return scm_from_long (type->num_fields ());
  766. }
  767. /* (type-field <gdb:type> string) -> <gdb:field>
  768. Return the <gdb:field> object for the field named by the argument. */
  769. static SCM
  770. gdbscm_type_field (SCM self, SCM field_scm)
  771. {
  772. type_smob *t_smob
  773. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  774. struct type *type = t_smob->type;
  775. SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME,
  776. _("string"));
  777. /* We want just fields of this type, not of base types, so instead of
  778. using lookup_struct_elt_type, portions of that function are
  779. copied here. */
  780. type = tyscm_get_composite (type);
  781. if (type == NULL)
  782. gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
  783. _(not_composite_error));
  784. {
  785. gdb::unique_xmalloc_ptr<char> field = gdbscm_scm_to_c_string (field_scm);
  786. for (int i = 0; i < type->num_fields (); i++)
  787. {
  788. const char *t_field_name = type->field (i).name ();
  789. if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0))
  790. {
  791. field.reset (nullptr);
  792. return tyscm_make_field_smob (self, i);
  793. }
  794. }
  795. }
  796. gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, field_scm,
  797. _("Unknown field"));
  798. }
  799. /* (type-has-field? <gdb:type> string) -> boolean
  800. Return boolean indicating if type SELF has FIELD_SCM (a string). */
  801. static SCM
  802. gdbscm_type_has_field_p (SCM self, SCM field_scm)
  803. {
  804. type_smob *t_smob
  805. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  806. struct type *type = t_smob->type;
  807. SCM_ASSERT_TYPE (scm_is_string (field_scm), field_scm, SCM_ARG2, FUNC_NAME,
  808. _("string"));
  809. /* We want just fields of this type, not of base types, so instead of
  810. using lookup_struct_elt_type, portions of that function are
  811. copied here. */
  812. type = tyscm_get_composite (type);
  813. if (type == NULL)
  814. gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
  815. _(not_composite_error));
  816. {
  817. gdb::unique_xmalloc_ptr<char> field
  818. = gdbscm_scm_to_c_string (field_scm);
  819. for (int i = 0; i < type->num_fields (); i++)
  820. {
  821. const char *t_field_name = type->field (i).name ();
  822. if (t_field_name && (strcmp_iw (t_field_name, field.get ()) == 0))
  823. return SCM_BOOL_T;
  824. }
  825. }
  826. return SCM_BOOL_F;
  827. }
  828. /* (make-field-iterator <gdb:type>) -> <gdb:iterator>
  829. Make a field iterator object. */
  830. static SCM
  831. gdbscm_make_field_iterator (SCM self)
  832. {
  833. type_smob *t_smob
  834. = tyscm_get_type_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  835. struct type *type = t_smob->type;
  836. struct type *containing_type;
  837. SCM containing_type_scm;
  838. containing_type = tyscm_get_composite (type);
  839. if (containing_type == NULL)
  840. gdbscm_out_of_range_error (FUNC_NAME, SCM_ARG1, self,
  841. _(not_composite_error));
  842. /* If SELF is a typedef or reference, we want the underlying type,
  843. which is what tyscm_get_composite returns. */
  844. if (containing_type == type)
  845. containing_type_scm = self;
  846. else
  847. containing_type_scm = tyscm_scm_from_type (containing_type);
  848. return gdbscm_make_iterator (containing_type_scm, scm_from_int (0),
  849. tyscm_next_field_x_proc);
  850. }
  851. /* (type-next-field! <gdb:iterator>) -> <gdb:field>
  852. Return the next field in the iteration through the list of fields of the
  853. type, or (end-of-iteration).
  854. SELF is a <gdb:iterator> object created by gdbscm_make_field_iterator.
  855. This is the next! <gdb:iterator> function, not exported to the user. */
  856. static SCM
  857. gdbscm_type_next_field_x (SCM self)
  858. {
  859. iterator_smob *i_smob;
  860. type_smob *t_smob;
  861. struct type *type;
  862. SCM it_scm, result, progress, object;
  863. int field;
  864. it_scm = itscm_get_iterator_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  865. i_smob = (iterator_smob *) SCM_SMOB_DATA (it_scm);
  866. object = itscm_iterator_smob_object (i_smob);
  867. progress = itscm_iterator_smob_progress (i_smob);
  868. SCM_ASSERT_TYPE (tyscm_is_type (object), object,
  869. SCM_ARG1, FUNC_NAME, type_smob_name);
  870. t_smob = (type_smob *) SCM_SMOB_DATA (object);
  871. type = t_smob->type;
  872. SCM_ASSERT_TYPE (scm_is_signed_integer (progress,
  873. 0, type->num_fields ()),
  874. progress, SCM_ARG1, FUNC_NAME, _("integer"));
  875. field = scm_to_int (progress);
  876. if (field < type->num_fields ())
  877. {
  878. result = tyscm_make_field_smob (object, field);
  879. itscm_set_iterator_smob_progress_x (i_smob, scm_from_int (field + 1));
  880. return result;
  881. }
  882. return gdbscm_end_of_iteration ();
  883. }
  884. /* Field smob accessors. */
  885. /* (field-name <gdb:field>) -> string
  886. Return the name of this field or #f if there isn't one. */
  887. static SCM
  888. gdbscm_field_name (SCM self)
  889. {
  890. field_smob *f_smob
  891. = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  892. struct field *field = tyscm_field_smob_to_field (f_smob);
  893. if (field->name () != nullptr)
  894. return gdbscm_scm_from_c_string (field->name ());
  895. return SCM_BOOL_F;
  896. }
  897. /* (field-type <gdb:field>) -> <gdb:type>
  898. Return the <gdb:type> object of the field or #f if there isn't one. */
  899. static SCM
  900. gdbscm_field_type (SCM self)
  901. {
  902. field_smob *f_smob
  903. = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  904. struct field *field = tyscm_field_smob_to_field (f_smob);
  905. /* A field can have a NULL type in some situations. */
  906. if (field->type ())
  907. return tyscm_scm_from_type (field->type ());
  908. return SCM_BOOL_F;
  909. }
  910. /* (field-enumval <gdb:field>) -> integer
  911. For enum values, return its value as an integer. */
  912. static SCM
  913. gdbscm_field_enumval (SCM self)
  914. {
  915. field_smob *f_smob
  916. = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  917. struct field *field = tyscm_field_smob_to_field (f_smob);
  918. struct type *type = tyscm_field_smob_containing_type (f_smob);
  919. SCM_ASSERT_TYPE (type->code () == TYPE_CODE_ENUM,
  920. self, SCM_ARG1, FUNC_NAME, _("enum type"));
  921. return scm_from_long (field->loc_enumval ());
  922. }
  923. /* (field-bitpos <gdb:field>) -> integer
  924. For bitfields, return its offset in bits. */
  925. static SCM
  926. gdbscm_field_bitpos (SCM self)
  927. {
  928. field_smob *f_smob
  929. = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  930. struct field *field = tyscm_field_smob_to_field (f_smob);
  931. struct type *type = tyscm_field_smob_containing_type (f_smob);
  932. SCM_ASSERT_TYPE (type->code () != TYPE_CODE_ENUM,
  933. self, SCM_ARG1, FUNC_NAME, _("non-enum type"));
  934. return scm_from_long (field->loc_bitpos ());
  935. }
  936. /* (field-bitsize <gdb:field>) -> integer
  937. Return the size of the field in bits. */
  938. static SCM
  939. gdbscm_field_bitsize (SCM self)
  940. {
  941. field_smob *f_smob
  942. = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  943. struct field *field = tyscm_field_smob_to_field (f_smob);
  944. return scm_from_long (field->loc_bitpos ());
  945. }
  946. /* (field-artificial? <gdb:field>) -> boolean
  947. Return #t if field is artificial. */
  948. static SCM
  949. gdbscm_field_artificial_p (SCM self)
  950. {
  951. field_smob *f_smob
  952. = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  953. struct field *field = tyscm_field_smob_to_field (f_smob);
  954. return scm_from_bool (FIELD_ARTIFICIAL (*field));
  955. }
  956. /* (field-baseclass? <gdb:field>) -> boolean
  957. Return #t if field is a baseclass. */
  958. static SCM
  959. gdbscm_field_baseclass_p (SCM self)
  960. {
  961. field_smob *f_smob
  962. = tyscm_get_field_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
  963. struct type *type = tyscm_field_smob_containing_type (f_smob);
  964. if (type->code () == TYPE_CODE_STRUCT)
  965. return scm_from_bool (f_smob->field_num < TYPE_N_BASECLASSES (type));
  966. return SCM_BOOL_F;
  967. }
  968. /* Return the type named TYPE_NAME in BLOCK.
  969. Returns NULL if not found.
  970. This routine does not throw an error. */
  971. static struct type *
  972. tyscm_lookup_typename (const char *type_name, const struct block *block)
  973. {
  974. struct type *type = NULL;
  975. try
  976. {
  977. if (startswith (type_name, "struct "))
  978. type = lookup_struct (type_name + 7, NULL);
  979. else if (startswith (type_name, "union "))
  980. type = lookup_union (type_name + 6, NULL);
  981. else if (startswith (type_name, "enum "))
  982. type = lookup_enum (type_name + 5, NULL);
  983. else
  984. type = lookup_typename (current_language,
  985. type_name, block, 0);
  986. }
  987. catch (const gdb_exception &except)
  988. {
  989. return NULL;
  990. }
  991. return type;
  992. }
  993. /* (lookup-type name [#:block <gdb:block>]) -> <gdb:type>
  994. TODO: legacy template support left out until needed. */
  995. static SCM
  996. gdbscm_lookup_type (SCM name_scm, SCM rest)
  997. {
  998. SCM keywords[] = { block_keyword, SCM_BOOL_F };
  999. char *name;
  1000. SCM block_scm = SCM_BOOL_F;
  1001. int block_arg_pos = -1;
  1002. const struct block *block = NULL;
  1003. struct type *type;
  1004. gdbscm_parse_function_args (FUNC_NAME, SCM_ARG1, keywords, "s#O",
  1005. name_scm, &name,
  1006. rest, &block_arg_pos, &block_scm);
  1007. if (block_arg_pos != -1)
  1008. {
  1009. SCM exception;
  1010. block = bkscm_scm_to_block (block_scm, block_arg_pos, FUNC_NAME,
  1011. &exception);
  1012. if (block == NULL)
  1013. {
  1014. xfree (name);
  1015. gdbscm_throw (exception);
  1016. }
  1017. }
  1018. type = tyscm_lookup_typename (name, block);
  1019. xfree (name);
  1020. if (type != NULL)
  1021. return tyscm_scm_from_type (type);
  1022. return SCM_BOOL_F;
  1023. }
  1024. /* Initialize the Scheme type code. */
  1025. static const scheme_integer_constant type_integer_constants[] =
  1026. {
  1027. #define X(SYM) { #SYM, SYM }
  1028. X (TYPE_CODE_BITSTRING),
  1029. X (TYPE_CODE_PTR),
  1030. X (TYPE_CODE_ARRAY),
  1031. X (TYPE_CODE_STRUCT),
  1032. X (TYPE_CODE_UNION),
  1033. X (TYPE_CODE_ENUM),
  1034. X (TYPE_CODE_FLAGS),
  1035. X (TYPE_CODE_FUNC),
  1036. X (TYPE_CODE_INT),
  1037. X (TYPE_CODE_FLT),
  1038. X (TYPE_CODE_VOID),
  1039. X (TYPE_CODE_SET),
  1040. X (TYPE_CODE_RANGE),
  1041. X (TYPE_CODE_STRING),
  1042. X (TYPE_CODE_ERROR),
  1043. X (TYPE_CODE_METHOD),
  1044. X (TYPE_CODE_METHODPTR),
  1045. X (TYPE_CODE_MEMBERPTR),
  1046. X (TYPE_CODE_REF),
  1047. X (TYPE_CODE_RVALUE_REF),
  1048. X (TYPE_CODE_CHAR),
  1049. X (TYPE_CODE_BOOL),
  1050. X (TYPE_CODE_COMPLEX),
  1051. X (TYPE_CODE_TYPEDEF),
  1052. X (TYPE_CODE_NAMESPACE),
  1053. X (TYPE_CODE_DECFLOAT),
  1054. X (TYPE_CODE_INTERNAL_FUNCTION),
  1055. #undef X
  1056. END_INTEGER_CONSTANTS
  1057. };
  1058. static const scheme_function type_functions[] =
  1059. {
  1060. { "type?", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_p),
  1061. "\
  1062. Return #t if the object is a <gdb:type> object." },
  1063. { "lookup-type", 1, 0, 1, as_a_scm_t_subr (gdbscm_lookup_type),
  1064. "\
  1065. Return the <gdb:type> object representing string or #f if not found.\n\
  1066. If block is given then the type is looked for in that block.\n\
  1067. \n\
  1068. Arguments: string [#:block <gdb:block>]" },
  1069. { "type-code", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_code),
  1070. "\
  1071. Return the code of the type" },
  1072. { "type-tag", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_tag),
  1073. "\
  1074. Return the tag name of the type, or #f if there isn't one." },
  1075. { "type-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_name),
  1076. "\
  1077. Return the name of the type as a string, or #f if there isn't one." },
  1078. { "type-print-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_print_name),
  1079. "\
  1080. Return the print name of the type as a string." },
  1081. { "type-sizeof", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_sizeof),
  1082. "\
  1083. Return the size of the type, in bytes." },
  1084. { "type-strip-typedefs", 1, 0, 0,
  1085. as_a_scm_t_subr (gdbscm_type_strip_typedefs),
  1086. "\
  1087. Return a type formed by stripping the type of all typedefs." },
  1088. { "type-array", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_array),
  1089. "\
  1090. Return a type representing an array of objects of the type.\n\
  1091. \n\
  1092. Arguments: <gdb:type> [low-bound] high-bound\n\
  1093. If low-bound is not provided zero is used.\n\
  1094. N.B. If only the high-bound parameter is specified, it is not\n\
  1095. the array size.\n\
  1096. Valid bounds for array indices are [low-bound,high-bound]." },
  1097. { "type-vector", 2, 1, 0, as_a_scm_t_subr (gdbscm_type_vector),
  1098. "\
  1099. Return a type representing a vector of objects of the type.\n\
  1100. Vectors differ from arrays in that if the current language has C-style\n\
  1101. arrays, vectors don't decay to a pointer to the first element.\n\
  1102. They are first class values.\n\
  1103. \n\
  1104. Arguments: <gdb:type> [low-bound] high-bound\n\
  1105. If low-bound is not provided zero is used.\n\
  1106. N.B. If only the high-bound parameter is specified, it is not\n\
  1107. the array size.\n\
  1108. Valid bounds for array indices are [low-bound,high-bound]." },
  1109. { "type-pointer", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_pointer),
  1110. "\
  1111. Return a type of pointer to the type." },
  1112. { "type-range", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_range),
  1113. "\
  1114. Return (low high) representing the range for the type." },
  1115. { "type-reference", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_reference),
  1116. "\
  1117. Return a type of reference to the type." },
  1118. { "type-target", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_target),
  1119. "\
  1120. Return the target type of the type." },
  1121. { "type-const", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_const),
  1122. "\
  1123. Return a const variant of the type." },
  1124. { "type-volatile", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_volatile),
  1125. "\
  1126. Return a volatile variant of the type." },
  1127. { "type-unqualified", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_unqualified),
  1128. "\
  1129. Return a variant of the type without const or volatile attributes." },
  1130. { "type-num-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_num_fields),
  1131. "\
  1132. Return the number of fields of the type." },
  1133. { "type-fields", 1, 0, 0, as_a_scm_t_subr (gdbscm_type_fields),
  1134. "\
  1135. Return the list of <gdb:field> objects of fields of the type." },
  1136. { "make-field-iterator", 1, 0, 0,
  1137. as_a_scm_t_subr (gdbscm_make_field_iterator),
  1138. "\
  1139. Return a <gdb:iterator> object for iterating over the fields of the type." },
  1140. { "type-field", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_field),
  1141. "\
  1142. Return the field named by string of the type.\n\
  1143. \n\
  1144. Arguments: <gdb:type> string" },
  1145. { "type-has-field?", 2, 0, 0, as_a_scm_t_subr (gdbscm_type_has_field_p),
  1146. "\
  1147. Return #t if the type has field named string.\n\
  1148. \n\
  1149. Arguments: <gdb:type> string" },
  1150. { "field?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_p),
  1151. "\
  1152. Return #t if the object is a <gdb:field> object." },
  1153. { "field-name", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_name),
  1154. "\
  1155. Return the name of the field." },
  1156. { "field-type", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_type),
  1157. "\
  1158. Return the type of the field." },
  1159. { "field-enumval", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_enumval),
  1160. "\
  1161. Return the enum value represented by the field." },
  1162. { "field-bitpos", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitpos),
  1163. "\
  1164. Return the offset in bits of the field in its containing type." },
  1165. { "field-bitsize", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_bitsize),
  1166. "\
  1167. Return the size of the field in bits." },
  1168. { "field-artificial?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_artificial_p),
  1169. "\
  1170. Return #t if the field is artificial." },
  1171. { "field-baseclass?", 1, 0, 0, as_a_scm_t_subr (gdbscm_field_baseclass_p),
  1172. "\
  1173. Return #t if the field is a baseclass." },
  1174. END_FUNCTIONS
  1175. };
  1176. void
  1177. gdbscm_initialize_types (void)
  1178. {
  1179. type_smob_tag = gdbscm_make_smob_type (type_smob_name, sizeof (type_smob));
  1180. scm_set_smob_free (type_smob_tag, tyscm_free_type_smob);
  1181. scm_set_smob_print (type_smob_tag, tyscm_print_type_smob);
  1182. scm_set_smob_equalp (type_smob_tag, tyscm_equal_p_type_smob);
  1183. field_smob_tag = gdbscm_make_smob_type (field_smob_name,
  1184. sizeof (field_smob));
  1185. scm_set_smob_print (field_smob_tag, tyscm_print_field_smob);
  1186. gdbscm_define_integer_constants (type_integer_constants, 1);
  1187. gdbscm_define_functions (type_functions, 1);
  1188. /* This function is "private". */
  1189. tyscm_next_field_x_proc
  1190. = scm_c_define_gsubr ("%type-next-field!", 1, 0, 0,
  1191. as_a_scm_t_subr (gdbscm_type_next_field_x));
  1192. scm_set_procedure_property_x (tyscm_next_field_x_proc,
  1193. gdbscm_documentation_symbol,
  1194. gdbscm_scm_from_c_string ("\
  1195. Internal function to assist the type fields iterator."));
  1196. block_keyword = scm_from_latin1_keyword ("block");
  1197. global_types_map = gdbscm_create_eqable_gsmob_ptr_map (tyscm_hash_type_smob,
  1198. tyscm_eq_type_smob);
  1199. }
  1200. void _initialize_scm_type ();
  1201. void
  1202. _initialize_scm_type ()
  1203. {
  1204. /* Register an objfile "free" callback so we can properly copy types
  1205. associated with the objfile when it's about to be deleted. */
  1206. tyscm_objfile_data_key
  1207. = register_objfile_data_with_cleanup (save_objfile_types, NULL);
  1208. }