struct.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /* Copyright 1996-2001,2003-2004,2006-2013,2015,2017-2018,2020
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <alloca.h>
  19. #include <assert.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #define SCM_BUILDING_DEPRECATED_CODE
  23. #include "alist.h"
  24. #include "async.h"
  25. #include "bdw-gc.h"
  26. #include "boolean.h"
  27. #include "chars.h"
  28. #include "deprecation.h"
  29. #include "eq.h"
  30. #include "eval.h"
  31. #include "finalizers.h"
  32. #include "goops.h"
  33. #include "gsubr.h"
  34. #include "hashtab.h"
  35. #include "modules.h"
  36. #include "numbers.h"
  37. #include "pairs.h"
  38. #include "ports.h"
  39. #include "srfi-13.h"
  40. #include "strings.h"
  41. #include "symbols.h"
  42. #include "struct.h"
  43. static SCM required_vtable_fields = SCM_BOOL_F;
  44. static SCM required_applicable_fields = SCM_BOOL_F;
  45. static SCM required_applicable_with_setter_fields = SCM_BOOL_F;
  46. SCM scm_applicable_struct_vtable_vtable;
  47. SCM scm_applicable_struct_with_setter_vtable_vtable;
  48. SCM scm_standard_vtable_vtable;
  49. SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0,
  50. (SCM fields),
  51. "Return a new structure layout object.\n\n"
  52. "@var{fields} must be a string made up of pairs of characters\n"
  53. "strung together. The first character of each pair describes a field\n"
  54. "type, the second a field protection. Allowed types are 'p' for\n"
  55. "GC-protected Scheme data, 'u' for unprotected binary data. \n"
  56. "Allowed protections are 'w' for normal fields or 'h' for \n"
  57. "hidden fields.\n\n"
  58. "Hidden fields are writable, but they will not consume an initializer arg\n"
  59. "passed to @code{make-struct}. They are useful to add slots to a struct\n"
  60. "in a way that preserves backward-compatibility with existing calls to\n"
  61. "@code{make-struct}, especially for derived vtables.")
  62. #define FUNC_NAME s_scm_make_struct_layout
  63. {
  64. SCM new_sym;
  65. scm_t_wchar c;
  66. SCM_VALIDATE_STRING (1, fields);
  67. { /* scope */
  68. size_t len;
  69. int x;
  70. len = scm_i_string_length (fields);
  71. if (len % 2 == 1)
  72. SCM_MISC_ERROR ("odd length field specification: ~S",
  73. scm_list_1 (fields));
  74. for (x = 0; x < len; x += 2)
  75. {
  76. switch (c = scm_i_string_ref (fields, x))
  77. {
  78. case 'u':
  79. case 'p':
  80. break;
  81. default:
  82. SCM_MISC_ERROR ("unrecognized field type: ~S",
  83. scm_list_1 (SCM_MAKE_CHAR (c)));
  84. }
  85. switch (c = scm_i_string_ref (fields, x + 1))
  86. {
  87. case 'w':
  88. case 'h':
  89. case 'r':
  90. break;
  91. default:
  92. SCM_MISC_ERROR ("unrecognized ref specification: ~S",
  93. scm_list_1 (SCM_MAKE_CHAR (c)));
  94. }
  95. }
  96. new_sym = scm_string_to_symbol (fields);
  97. }
  98. scm_remember_upto_here_1 (fields);
  99. return new_sym;
  100. }
  101. #undef FUNC_NAME
  102. static void
  103. set_vtable_access_fields (SCM vtable)
  104. {
  105. size_t len, nfields, bitmask_size, field;
  106. SCM layout;
  107. const char *c_layout;
  108. uint32_t *unboxed_fields;
  109. layout = SCM_VTABLE_LAYOUT (vtable);
  110. c_layout = scm_i_symbol_chars (layout);
  111. len = scm_i_symbol_length (layout);
  112. assert (len % 2 == 0);
  113. nfields = len / 2;
  114. bitmask_size = (nfields + 31U) / 32U;
  115. unboxed_fields =
  116. scm_gc_malloc_pointerless (bitmask_size * sizeof (*unboxed_fields),
  117. "unboxed fields");
  118. memset (unboxed_fields, 0, bitmask_size * sizeof (*unboxed_fields));
  119. /* Update FLAGS according to LAYOUT. */
  120. for (field = 0; field < nfields; field++)
  121. if (c_layout[field*2] == 'u')
  122. unboxed_fields[field/32U] |= 1U << (field%32U);
  123. /* Record computed size of vtable's instances. */
  124. SCM_SET_VTABLE_FLAGS (vtable, 0);
  125. SCM_STRUCT_DATA_SET (vtable, scm_vtable_index_size, len / 2);
  126. SCM_STRUCT_DATA_SET (vtable, scm_vtable_index_unboxed_fields,
  127. (uintptr_t) unboxed_fields);
  128. }
  129. static int
  130. scm_is_valid_vtable_layout (SCM layout)
  131. {
  132. size_t len, n;
  133. const char *c_layout;
  134. c_layout = scm_i_symbol_chars (layout);
  135. len = scm_i_symbol_length (layout);
  136. if (len % 2)
  137. return 0;
  138. for (n = 0; n < len; n += 2)
  139. switch (c_layout[n])
  140. {
  141. case 'u':
  142. case 'p':
  143. switch (c_layout[n+1])
  144. {
  145. case 'w':
  146. case 'h':
  147. break;
  148. case 'r':
  149. scm_c_issue_deprecation_warning
  150. ("Read-only struct fields are deprecated. Implement access "
  151. "control at a higher level instead, as structs no longer "
  152. "enforce field permissions.");
  153. break;
  154. default:
  155. return 0;
  156. }
  157. break;
  158. default:
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. /* Have OBJ, a newly created vtable, inherit flags from VTABLE. VTABLE is a
  164. vtable-vtable and OBJ is an instance of VTABLE. */
  165. void
  166. scm_i_struct_inherit_vtable_magic (SCM vtable, SCM obj)
  167. #define FUNC_NAME "%inherit-vtable-magic"
  168. {
  169. /* Verily, what is the deal here, you ask? Basically, we need to know a couple
  170. of properties of structures at runtime. For example, "is this structure a
  171. vtable of vtables (a metaclass)?"; also, "is this structure applicable?".
  172. Both of these questions also imply a certain layout of the structure. So
  173. instead of checking the layout at runtime, what we do is pre-verify the
  174. layout -- so that at runtime we can just check the applicable flag and
  175. dispatch directly to the Scheme procedure in slot 0. */
  176. SCM olayout;
  177. /* Verify that OBJ is a valid vtable. */
  178. if (! scm_is_valid_vtable_layout (SCM_VTABLE_LAYOUT (obj)))
  179. SCM_MISC_ERROR ("invalid layout for new vtable: ~a",
  180. scm_list_1 (SCM_VTABLE_LAYOUT (obj)));
  181. set_vtable_access_fields (obj);
  182. /* If OBJ's vtable is compatible with the required vtable (class) layout, it
  183. is a metaclass. */
  184. olayout = scm_symbol_to_string (SCM_VTABLE_LAYOUT (obj));
  185. if (scm_is_true (scm_leq_p (scm_string_length (required_vtable_fields),
  186. scm_string_length (olayout)))
  187. && scm_is_true (scm_string_eq (olayout, required_vtable_fields,
  188. scm_from_size_t (0),
  189. scm_string_length (required_vtable_fields),
  190. scm_from_size_t (0),
  191. scm_string_length (required_vtable_fields))))
  192. SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VTABLE);
  193. /* Finally, if OBJ is an applicable class, verify that its vtable is
  194. compatible with the required applicable layout. */
  195. if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SETTER_VTABLE))
  196. {
  197. if (scm_is_false (scm_string_eq (olayout, required_applicable_with_setter_fields,
  198. scm_from_size_t (0),
  199. scm_from_size_t (4),
  200. scm_from_size_t (0),
  201. scm_from_size_t (4))))
  202. SCM_MISC_ERROR ("invalid applicable-with-setter struct layout",
  203. scm_list_1 (olayout));
  204. SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_APPLICABLE | SCM_VTABLE_FLAG_SETTER);
  205. }
  206. else if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_APPLICABLE_VTABLE))
  207. {
  208. if (scm_is_false (scm_string_eq (olayout, required_applicable_fields,
  209. scm_from_size_t (0),
  210. scm_from_size_t (2),
  211. scm_from_size_t (0),
  212. scm_from_size_t (2))))
  213. SCM_MISC_ERROR ("invalid applicable struct layout",
  214. scm_list_1 (olayout));
  215. SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_APPLICABLE);
  216. }
  217. SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VALIDATED);
  218. }
  219. #undef FUNC_NAME
  220. static void
  221. scm_struct_init (SCM handle, SCM layout, size_t n_inits, scm_t_bits *inits)
  222. {
  223. size_t n, n_fields, inits_idx = 0;
  224. n_fields = SCM_STRUCT_SIZE (handle);
  225. for (n = 0; n < n_fields; n++)
  226. {
  227. if (inits_idx == n_inits || scm_i_symbol_ref (layout, n*2+1) == 'h')
  228. {
  229. if (SCM_STRUCT_FIELD_IS_UNBOXED (handle, n))
  230. SCM_STRUCT_DATA_SET (handle, n, 0);
  231. else
  232. SCM_STRUCT_SLOT_SET (handle, n, SCM_BOOL_F);
  233. }
  234. else
  235. {
  236. SCM_STRUCT_DATA_SET (handle, n,
  237. SCM_STRUCT_FIELD_IS_UNBOXED (handle, n)
  238. ? scm_to_uintptr_t (SCM_PACK (inits[inits_idx]))
  239. : inits[inits_idx]);
  240. inits_idx++;
  241. }
  242. }
  243. }
  244. SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0,
  245. (SCM x),
  246. "Return @code{#t} iff @var{x} is a structure object, else\n"
  247. "@code{#f}.")
  248. #define FUNC_NAME s_scm_struct_p
  249. {
  250. return scm_from_bool(SCM_STRUCTP (x));
  251. }
  252. #undef FUNC_NAME
  253. SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0,
  254. (SCM x),
  255. "Return @code{#t} iff @var{x} is a vtable structure.")
  256. #define FUNC_NAME s_scm_struct_vtable_p
  257. {
  258. if (!SCM_STRUCTP (x)
  259. || !SCM_STRUCT_VTABLE_FLAG_IS_SET (x, SCM_VTABLE_FLAG_VTABLE))
  260. return SCM_BOOL_F;
  261. if (!SCM_VTABLE_FLAG_IS_SET (x, SCM_VTABLE_FLAG_VALIDATED))
  262. SCM_MISC_ERROR ("vtable has invalid layout: ~A",
  263. scm_list_1 (SCM_VTABLE_LAYOUT (x)));
  264. return SCM_BOOL_T;
  265. }
  266. #undef FUNC_NAME
  267. /* Finalization: invoke the finalizer of the struct pointed to by PTR. */
  268. static void
  269. struct_finalizer_trampoline (void *ptr, void *unused_data)
  270. {
  271. SCM obj = PTR2SCM (ptr);
  272. scm_t_struct_finalize finalize = SCM_STRUCT_FINALIZER (obj);
  273. if (finalize)
  274. finalize (obj);
  275. }
  276. /* A struct is a sequence of words preceded by a pointer to the struct's
  277. vtable. The vtable reference is tagged with the struct tc3. */
  278. static SCM
  279. scm_i_alloc_struct (scm_t_bits vtable_bits, int n_words)
  280. {
  281. SCM ret;
  282. ret = scm_words (vtable_bits | scm_tc3_struct, n_words + 1);
  283. /* vtable_bits can be 0 when making a vtable vtable */
  284. if (vtable_bits && SCM_VTABLE_INSTANCE_FINALIZER (SCM_PACK (vtable_bits)))
  285. /* Register a finalizer for the newly created instance. */
  286. scm_i_set_finalizer (SCM2PTR (ret), struct_finalizer_trampoline, NULL);
  287. return ret;
  288. }
  289. SCM
  290. scm_c_make_structv (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits *init)
  291. #define FUNC_NAME "make-struct"
  292. {
  293. size_t basic_size;
  294. SCM obj;
  295. SCM_VALIDATE_VTABLE (1, vtable);
  296. basic_size = SCM_VTABLE_SIZE (vtable);
  297. SCM_ASSERT (n_tail == 0, scm_from_size_t (n_tail), 2, FUNC_NAME);
  298. obj = scm_i_alloc_struct (SCM_UNPACK (vtable), basic_size);
  299. scm_struct_init (obj, SCM_VTABLE_LAYOUT (vtable), n_init, init);
  300. /* If we're making a vtable, validate its layout and inherit
  301. flags. However we allow for separation of allocation and
  302. initialization, to humor GOOPS, so only validate if the layout was
  303. passed as an initarg. */
  304. if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE)
  305. && scm_is_true (SCM_VTABLE_LAYOUT (obj)))
  306. scm_i_struct_inherit_vtable_magic (vtable, obj);
  307. return obj;
  308. }
  309. #undef FUNC_NAME
  310. SCM
  311. scm_c_make_struct (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits init, ...)
  312. {
  313. va_list foo;
  314. scm_t_bits *v;
  315. size_t i;
  316. SCM_ASSERT (n_tail == 0, scm_from_size_t (n_tail), 2, "scm_c_make_struct");
  317. v = alloca (sizeof (scm_t_bits) * n_init);
  318. va_start (foo, init);
  319. for (i = 0; i < n_init; i++)
  320. {
  321. v[i] = init;
  322. init = va_arg (foo, scm_t_bits);
  323. }
  324. va_end (foo);
  325. return scm_c_make_structv (vtable, 0, n_init, v);
  326. }
  327. SCM_DEFINE (scm_allocate_struct, "allocate-struct", 2, 0, 0,
  328. (SCM vtable, SCM nfields),
  329. "Allocate a new structure with space for @var{nfields} fields.\n\n"
  330. "@var{vtable} must be a vtable structure (@pxref{Vtables}).\n\n"
  331. "@var{nfields} must be a non-negative integer. Strictly speaking\n"
  332. "@var{nfields} is redundant, as the vtable carries the size\n"
  333. "for its instances. However passing it is useful as a sanity\n"
  334. "check, given that one module can inline a constructor in\n"
  335. "another.\n\n"
  336. "Fields will be initialized with their default values.")
  337. #define FUNC_NAME s_scm_allocate_struct
  338. {
  339. SCM ret;
  340. size_t c_nfields;
  341. SCM_VALIDATE_VTABLE (1, vtable);
  342. c_nfields = scm_to_size_t (nfields);
  343. SCM_ASSERT (SCM_VTABLE_SIZE (vtable) == c_nfields, nfields, 2, FUNC_NAME);
  344. ret = scm_i_alloc_struct (SCM_UNPACK (vtable), c_nfields);
  345. scm_struct_init (ret, SCM_VTABLE_LAYOUT (vtable), 0, NULL);
  346. return ret;
  347. }
  348. #undef FUNC_NAME
  349. SCM_DEFINE (scm_make_struct_simple, "make-struct/simple", 1, 0, 1,
  350. (SCM vtable, SCM init),
  351. "Create a new structure.\n\n"
  352. "@var{vtable} must be a vtable structure (@pxref{Vtables}).\n\n"
  353. "The @var{init1}, @dots{} arguments supply the initial values\n"
  354. "for the structure's fields\n.\n"
  355. "This is a restricted variant of @code{make-struct/no-tail}\n"
  356. "which applies only if the structure has no unboxed fields.\n"
  357. "@code{make-struct/simple} must be called with as many\n"
  358. "@var{init} values as the struct has fields. No finalizer is set\n"
  359. "on the instance, even if the vtable has a non-zero finalizer\n"
  360. "field. No magical vtable fields are inherited.\n\n"
  361. "The advantage of using @code{make-struct/simple} is that the\n"
  362. "compiler can inline it, so it is faster. When in doubt though,\n"
  363. "use @code{make-struct/no-tail}.")
  364. #define FUNC_NAME s_scm_make_struct_simple
  365. {
  366. long i, n_init;
  367. SCM ret;
  368. SCM_VALIDATE_VTABLE (1, vtable);
  369. n_init = scm_ilength (init);
  370. if (n_init != SCM_VTABLE_SIZE (vtable))
  371. SCM_MISC_ERROR ("Wrong number of initializers.", SCM_EOL);
  372. ret = scm_words (SCM_UNPACK (vtable) | scm_tc3_struct, n_init + 1);
  373. for (i = 0; i < n_init; i++, init = scm_cdr (init))
  374. {
  375. SCM_ASSERT (!SCM_VTABLE_FIELD_IS_UNBOXED (vtable, i),
  376. vtable, 1, FUNC_NAME);
  377. SCM_STRUCT_SLOT_SET (ret, i, scm_car (init));
  378. }
  379. return ret;
  380. }
  381. #undef FUNC_NAME
  382. SCM_DEFINE (scm_make_struct_no_tail, "make-struct/no-tail", 1, 0, 1,
  383. (SCM vtable, SCM init),
  384. "Create a new structure.\n\n"
  385. "@var{vtable} must be a vtable structure (@pxref{Vtables}).\n\n"
  386. "The @var{init1}, @dots{} are optional arguments describing how\n"
  387. "successive fields of the structure should be initialized.\n"
  388. "Note that hidden fields (those with protection 'h') have to be\n"
  389. "manually set.\n\n"
  390. "If fewer optional arguments than initializable fields are supplied,\n"
  391. "fields of type 'p' get default value #f while fields of type 'u' are\n"
  392. "initialized to 0.")
  393. #define FUNC_NAME s_scm_make_struct_no_tail
  394. {
  395. size_t i, n_init;
  396. long ilen;
  397. scm_t_bits *v;
  398. SCM_VALIDATE_VTABLE (1, vtable);
  399. ilen = scm_ilength (init);
  400. if (ilen < 0)
  401. SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL);
  402. n_init = (size_t)ilen;
  403. /* best to use alloca, but init could be big, so hack to avoid a possible
  404. stack overflow */
  405. if (n_init < 64)
  406. v = alloca (n_init * sizeof(scm_t_bits));
  407. else
  408. v = scm_gc_malloc (n_init * sizeof(scm_t_bits), "struct");
  409. for (i = 0; i < n_init; i++, init = SCM_CDR (init))
  410. v[i] = SCM_UNPACK (SCM_CAR (init));
  411. return scm_c_make_structv (vtable, 0, n_init, v);
  412. }
  413. #undef FUNC_NAME
  414. SCM
  415. scm_i_make_vtable_vtable (SCM fields)
  416. #define FUNC_NAME "make-vtable-vtable"
  417. {
  418. SCM layout, obj;
  419. size_t n, nfields;
  420. SCM_VALIDATE_STRING (1, fields);
  421. layout = scm_make_struct_layout (fields);
  422. if (!scm_is_valid_vtable_layout (layout))
  423. SCM_MISC_ERROR ("invalid user fields", scm_list_1 (fields));
  424. nfields = scm_i_symbol_length (layout) / 2;
  425. obj = scm_i_alloc_struct (0, nfields);
  426. /* Make it so that the vtable of OBJ is itself. */
  427. SCM_SET_CELL_WORD_0 (obj, SCM_UNPACK (obj) | scm_tc3_struct);
  428. /* Manually initialize fields. */
  429. SCM_STRUCT_SLOT_SET (obj, scm_vtable_index_layout, layout);
  430. set_vtable_access_fields (obj);
  431. SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VTABLE | SCM_VTABLE_FLAG_VALIDATED);
  432. SCM_STRUCT_DATA_SET (obj, scm_vtable_index_instance_finalize, 0);
  433. SCM_STRUCT_SLOT_SET (obj, scm_vtable_index_instance_printer, SCM_BOOL_F);
  434. SCM_STRUCT_SLOT_SET (obj, scm_vtable_index_name, SCM_BOOL_F);
  435. SCM_STRUCT_DATA_SET (obj, scm_vtable_index_reserved_7, 0);
  436. for (n = scm_vtable_offset_user; n < nfields; n++)
  437. if (SCM_STRUCT_FIELD_IS_UNBOXED (obj, n))
  438. SCM_STRUCT_DATA_SET (obj, n, 0);
  439. else
  440. SCM_STRUCT_SLOT_SET (obj, n, SCM_BOOL_F);
  441. return obj;
  442. }
  443. #undef FUNC_NAME
  444. SCM_DEFINE (scm_make_vtable, "make-vtable", 1, 1, 0,
  445. (SCM fields, SCM printer),
  446. "Create a vtable, for creating structures with the given\n"
  447. "@var{fields}.\n"
  448. "\n"
  449. "The optional @var{printer} argument is a function to be called\n"
  450. "@code{(@var{printer} struct port)} on the structures created.\n"
  451. "It should look at @var{struct} and write to @var{port}.")
  452. #define FUNC_NAME s_scm_make_vtable
  453. {
  454. if (SCM_UNBNDP (printer))
  455. printer = SCM_BOOL_F;
  456. return scm_c_make_struct (scm_standard_vtable_vtable, 0, 2,
  457. SCM_UNPACK (scm_make_struct_layout (fields)),
  458. SCM_UNPACK (printer));
  459. }
  460. #undef FUNC_NAME
  461. /* Return true if S1 and S2 are equal structures, i.e., if their vtable and
  462. contents are the same. */
  463. SCM
  464. scm_i_struct_equalp (SCM s1, SCM s2)
  465. #define FUNC_NAME "scm_i_struct_equalp"
  466. {
  467. size_t struct_size, field_num;
  468. SCM_VALIDATE_STRUCT (1, s1);
  469. SCM_VALIDATE_STRUCT (2, s2);
  470. if (!scm_is_eq (SCM_STRUCT_VTABLE (s1), SCM_STRUCT_VTABLE (s2)))
  471. return SCM_BOOL_F;
  472. struct_size = SCM_STRUCT_SIZE (s1);
  473. for (field_num = 0; field_num < struct_size; field_num++)
  474. {
  475. scm_t_bits field1, field2;
  476. field1 = SCM_STRUCT_DATA_REF (s1, field_num);
  477. field2 = SCM_STRUCT_DATA_REF (s2, field_num);
  478. if (field1 != field2) {
  479. if (SCM_STRUCT_FIELD_IS_UNBOXED (s1, field_num))
  480. return SCM_BOOL_F;
  481. /* Having a normal field point to the object itself is a bit
  482. bonkers, but R6RS enums do it, so here we have a horrible
  483. hack. */
  484. if (field1 != SCM_UNPACK (s1) && field2 != SCM_UNPACK (s2))
  485. {
  486. if (scm_is_false
  487. (scm_equal_p (SCM_PACK (field1), SCM_PACK (field2))))
  488. return SCM_BOOL_F;
  489. }
  490. }
  491. }
  492. return SCM_BOOL_T;
  493. }
  494. #undef FUNC_NAME
  495. SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0,
  496. (SCM handle, SCM pos),
  497. "Access the @var{pos}th field of struct associated with\n"
  498. "@var{handle}.\n"
  499. "\n"
  500. "If the field is of type 'p', then it can be set to an arbitrary\n"
  501. "value.\n"
  502. "\n"
  503. "If the field is of type 'u', then it can only be set to a\n"
  504. "non-negative integer value small enough to fit in one machine\n"
  505. "word.")
  506. #define FUNC_NAME s_scm_struct_ref
  507. {
  508. size_t nfields, p;
  509. SCM_VALIDATE_STRUCT (1, handle);
  510. nfields = SCM_STRUCT_SIZE (handle);
  511. p = scm_to_size_t (pos);
  512. SCM_ASSERT_RANGE (2, pos, p < nfields);
  513. SCM_ASSERT (!SCM_STRUCT_FIELD_IS_UNBOXED (handle, p), pos, 2, FUNC_NAME);
  514. return SCM_STRUCT_SLOT_REF (handle, p);
  515. }
  516. #undef FUNC_NAME
  517. SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0,
  518. (SCM handle, SCM pos, SCM val),
  519. "Set the slot of the structure @var{handle} with index @var{pos}\n"
  520. "to @var{val}. Signal an error if the slot can not be written\n"
  521. "to.")
  522. #define FUNC_NAME s_scm_struct_set_x
  523. {
  524. size_t nfields, p;
  525. SCM_VALIDATE_STRUCT (1, handle);
  526. nfields = SCM_STRUCT_SIZE (handle);
  527. p = scm_to_size_t (pos);
  528. SCM_ASSERT_RANGE (2, pos, p < nfields);
  529. SCM_ASSERT (!SCM_STRUCT_FIELD_IS_UNBOXED (handle, p), pos, 2, FUNC_NAME);
  530. SCM_STRUCT_SLOT_SET (handle, p, val);
  531. return val;
  532. }
  533. #undef FUNC_NAME
  534. SCM_DEFINE (scm_struct_ref_unboxed, "struct-ref/unboxed", 2, 0, 0,
  535. (SCM handle, SCM pos),
  536. "Access the @var{pos}th field of struct associated with\n"
  537. "@var{handle}. The field must be of type 'u'.")
  538. #define FUNC_NAME s_scm_struct_ref_unboxed
  539. {
  540. size_t nfields, p;
  541. SCM_VALIDATE_STRUCT (1, handle);
  542. nfields = SCM_STRUCT_SIZE (handle);
  543. p = scm_to_size_t (pos);
  544. SCM_ASSERT_RANGE (2, pos, p < nfields);
  545. SCM_ASSERT (SCM_STRUCT_FIELD_IS_UNBOXED (handle, p), pos, 2, FUNC_NAME);
  546. return scm_from_uintptr_t (SCM_STRUCT_DATA_REF (handle, p));
  547. }
  548. #undef FUNC_NAME
  549. SCM_DEFINE (scm_struct_set_x_unboxed, "struct-set!/unboxed", 3, 0, 0,
  550. (SCM handle, SCM pos, SCM val),
  551. "Set the slot of the structure @var{handle} with index @var{pos}\n"
  552. "to @var{val}. Signal an error if the slot can not be written\n"
  553. "to.")
  554. #define FUNC_NAME s_scm_struct_set_x_unboxed
  555. {
  556. size_t nfields, p;
  557. SCM_VALIDATE_STRUCT (1, handle);
  558. nfields = SCM_STRUCT_SIZE (handle);
  559. p = scm_to_size_t (pos);
  560. SCM_ASSERT_RANGE (2, pos, p < nfields);
  561. SCM_ASSERT (SCM_STRUCT_FIELD_IS_UNBOXED (handle, p), pos, 2, FUNC_NAME);
  562. SCM_STRUCT_DATA_SET (handle, p, scm_to_uintptr_t (val));
  563. return val;
  564. }
  565. #undef FUNC_NAME
  566. SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0,
  567. (SCM handle),
  568. "Return the vtable structure that describes the type of struct\n"
  569. "associated with @var{handle}.")
  570. #define FUNC_NAME s_scm_struct_vtable
  571. {
  572. SCM_VALIDATE_STRUCT (1, handle);
  573. return SCM_STRUCT_VTABLE (handle);
  574. }
  575. #undef FUNC_NAME
  576. /* {Associating names and classes with vtables}
  577. *
  578. * The name of a vtable should probably be stored as a slot. This is
  579. * a backward compatible solution until agreement has been achieved on
  580. * how to associate names with vtables.
  581. */
  582. unsigned long
  583. scm_struct_ihashq (SCM obj, unsigned long n, void *closure)
  584. {
  585. /* The length of the hash table should be a relative prime it's not
  586. necessary to shift down the address. */
  587. return SCM_UNPACK (obj) % n;
  588. }
  589. SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0,
  590. (SCM vtable),
  591. "Return the name of the vtable @var{vtable}.")
  592. #define FUNC_NAME s_scm_struct_vtable_name
  593. {
  594. SCM_VALIDATE_VTABLE (1, vtable);
  595. return SCM_VTABLE_NAME (vtable);
  596. }
  597. #undef FUNC_NAME
  598. SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0,
  599. (SCM vtable, SCM name),
  600. "Set the name of the vtable @var{vtable} to @var{name}.")
  601. #define FUNC_NAME s_scm_set_struct_vtable_name_x
  602. {
  603. SCM_VALIDATE_VTABLE (1, vtable);
  604. SCM_VALIDATE_SYMBOL (2, name);
  605. SCM_SET_VTABLE_NAME (vtable, name);
  606. /* FIXME: remove this, and implement proper struct classes instead.
  607. (Vtables *are* classes.) */
  608. scm_i_define_class_for_vtable (vtable);
  609. return SCM_UNSPECIFIED;
  610. }
  611. #undef FUNC_NAME
  612. void
  613. scm_print_struct (SCM exp, SCM port, scm_print_state *pstate)
  614. {
  615. if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp))))
  616. scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate);
  617. else
  618. {
  619. SCM vtable = SCM_STRUCT_VTABLE (exp);
  620. SCM name = scm_struct_vtable_name (vtable);
  621. scm_puts ("#<", port);
  622. if (scm_is_true (name))
  623. {
  624. scm_display (name, port);
  625. scm_putc (' ', port);
  626. }
  627. else
  628. {
  629. if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE))
  630. scm_puts ("vtable:", port);
  631. else
  632. scm_puts ("struct:", port);
  633. scm_uintprint (SCM_UNPACK (vtable), 16, port);
  634. scm_putc (' ', port);
  635. scm_write (SCM_VTABLE_LAYOUT (vtable), port);
  636. scm_putc (' ', port);
  637. }
  638. scm_uintprint (SCM_UNPACK (exp), 16, port);
  639. /* hackety hack */
  640. if (SCM_STRUCT_APPLICABLE_P (exp))
  641. {
  642. if (scm_is_true (SCM_STRUCT_PROCEDURE (exp)))
  643. {
  644. scm_puts (" proc: ", port);
  645. if (scm_is_true (scm_procedure_p (SCM_STRUCT_PROCEDURE (exp))))
  646. scm_write (SCM_STRUCT_PROCEDURE (exp), port);
  647. else
  648. scm_puts ("(not a procedure?)", port);
  649. }
  650. if (SCM_STRUCT_SETTER_P (exp))
  651. {
  652. scm_puts (" setter: ", port);
  653. scm_write (SCM_STRUCT_SETTER (exp), port);
  654. }
  655. }
  656. scm_putc ('>', port);
  657. }
  658. }
  659. void
  660. scm_init_struct ()
  661. {
  662. SCM name;
  663. required_vtable_fields = scm_from_latin1_string (SCM_VTABLE_BASE_LAYOUT);
  664. scm_c_define ("standard-vtable-fields", required_vtable_fields);
  665. required_applicable_fields = scm_from_latin1_string (SCM_APPLICABLE_BASE_LAYOUT);
  666. required_applicable_with_setter_fields = scm_from_latin1_string (SCM_APPLICABLE_WITH_SETTER_BASE_LAYOUT);
  667. scm_standard_vtable_vtable =
  668. scm_i_make_vtable_vtable (required_vtable_fields);
  669. name = scm_from_utf8_symbol ("<standard-vtable>");
  670. scm_set_struct_vtable_name_x (scm_standard_vtable_vtable, name);
  671. scm_define (name, scm_standard_vtable_vtable);
  672. scm_applicable_struct_vtable_vtable =
  673. scm_c_make_struct (scm_standard_vtable_vtable, 0, 1,
  674. SCM_UNPACK (scm_make_struct_layout (required_vtable_fields)));
  675. name = scm_from_utf8_symbol ("<applicable-struct-vtable>");
  676. SCM_SET_VTABLE_FLAGS (scm_applicable_struct_vtable_vtable,
  677. SCM_VTABLE_FLAG_APPLICABLE_VTABLE);
  678. scm_set_struct_vtable_name_x (scm_applicable_struct_vtable_vtable, name);
  679. scm_define (name, scm_applicable_struct_vtable_vtable);
  680. scm_applicable_struct_with_setter_vtable_vtable =
  681. scm_c_make_struct (scm_standard_vtable_vtable, 0, 1,
  682. SCM_UNPACK (scm_make_struct_layout (required_vtable_fields)));
  683. name = scm_from_utf8_symbol ("<applicable-struct-with-setter-vtable>");
  684. scm_set_struct_vtable_name_x (scm_applicable_struct_with_setter_vtable_vtable, name);
  685. SCM_SET_VTABLE_FLAGS (scm_applicable_struct_with_setter_vtable_vtable,
  686. SCM_VTABLE_FLAG_APPLICABLE_VTABLE | SCM_VTABLE_FLAG_SETTER_VTABLE);
  687. scm_define (name, scm_applicable_struct_with_setter_vtable_vtable);
  688. scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout));
  689. scm_c_define ("vtable-index-printer",
  690. scm_from_int (scm_vtable_index_instance_printer));
  691. scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user));
  692. #include "struct.x"
  693. }