struct.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2007,
  2. * 2008, 2009, 2010, 2011, 2012, 2013, 2015 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <alloca.h>
  23. #include <assert.h>
  24. #define SCM_BUILDING_DEPRECATED_CODE
  25. #include "libguile/_scm.h"
  26. #include "libguile/async.h"
  27. #include "libguile/chars.h"
  28. #include "libguile/eval.h"
  29. #include "libguile/alist.h"
  30. #include "libguile/hashtab.h"
  31. #include "libguile/ports.h"
  32. #include "libguile/strings.h"
  33. #include "libguile/srfi-13.h"
  34. #include "libguile/validate.h"
  35. #include "libguile/struct.h"
  36. #include "libguile/eq.h"
  37. #ifdef HAVE_STRING_H
  38. #include <string.h>
  39. #endif
  40. #include "libguile/bdw-gc.h"
  41. /* A needlessly obscure test. */
  42. #define SCM_LAYOUT_TAILP(X) (((X) & 32) == 0) /* R, W or O */
  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, and 's' for\n"
  56. "a field that points to the structure itself. Allowed protections\n"
  57. "are 'w' for mutable fields, 'h' for hidden fields, 'r' for read-only\n"
  58. "fields, and 'o' for opaque fields.\n\n"
  59. "Hidden fields are writable, but they will not consume an initializer arg\n"
  60. "passed to @code{make-struct}. They are useful to add slots to a struct\n"
  61. "in a way that preserves backward-compatibility with existing calls to\n"
  62. "@code{make-struct}, especially for derived vtables.\n\n"
  63. "The last field protection specification may be capitalized to indicate\n"
  64. "that the field is a tail-array.")
  65. #define FUNC_NAME s_scm_make_struct_layout
  66. {
  67. SCM new_sym;
  68. scm_t_wchar c;
  69. SCM_VALIDATE_STRING (1, fields);
  70. { /* scope */
  71. size_t len;
  72. int x;
  73. len = scm_i_string_length (fields);
  74. if (len % 2 == 1)
  75. SCM_MISC_ERROR ("odd length field specification: ~S",
  76. scm_list_1 (fields));
  77. for (x = 0; x < len; x += 2)
  78. {
  79. switch (c = scm_i_string_ref (fields, x))
  80. {
  81. case 'u':
  82. case 'p':
  83. #if 0
  84. case 'i':
  85. case 'd':
  86. #endif
  87. case 's':
  88. break;
  89. default:
  90. SCM_MISC_ERROR ("unrecognized field type: ~S",
  91. scm_list_1 (SCM_MAKE_CHAR (c)));
  92. }
  93. switch (c = scm_i_string_ref (fields, x + 1))
  94. {
  95. case 'w':
  96. case 'h':
  97. if (scm_i_string_ref (fields, x) == 's')
  98. SCM_MISC_ERROR ("self fields not writable", SCM_EOL);
  99. case 'r':
  100. case 'o':
  101. break;
  102. case 'R':
  103. case 'W':
  104. case 'O':
  105. if (scm_i_string_ref (fields, x) == 's')
  106. SCM_MISC_ERROR ("self fields not allowed in tail array",
  107. SCM_EOL);
  108. if (x != len - 2)
  109. SCM_MISC_ERROR ("tail array field must be last field in layout",
  110. SCM_EOL);
  111. break;
  112. default:
  113. SCM_MISC_ERROR ("unrecognized ref specification: ~S",
  114. scm_list_1 (SCM_MAKE_CHAR (c)));
  115. }
  116. #if 0
  117. if (scm_i_string_ref (fields, x, 'd'))
  118. {
  119. if (!scm_i_string_ref (fields, x+2, '-'))
  120. SCM_MISC_ERROR ("missing dash field at position ~A",
  121. scm_list_1 (scm_from_int (x / 2)));
  122. x += 2;
  123. goto recheck_ref;
  124. }
  125. #endif
  126. }
  127. new_sym = scm_string_to_symbol (fields);
  128. }
  129. scm_remember_upto_here_1 (fields);
  130. return new_sym;
  131. }
  132. #undef FUNC_NAME
  133. /* Check whether VTABLE instances have a simple layout (i.e., either
  134. only "pr" or only "pw" fields and no tail array) and update its flags
  135. accordingly. */
  136. static void
  137. set_vtable_layout_flags (SCM vtable)
  138. {
  139. size_t len, field;
  140. SCM layout;
  141. const char *c_layout;
  142. scm_t_bits flags = SCM_VTABLE_FLAG_SIMPLE;
  143. layout = SCM_VTABLE_LAYOUT (vtable);
  144. c_layout = scm_i_symbol_chars (layout);
  145. len = scm_i_symbol_length (layout);
  146. assert (len % 2 == 0);
  147. /* Update FLAGS according to LAYOUT. */
  148. for (field = 0;
  149. field < len && flags & SCM_VTABLE_FLAG_SIMPLE;
  150. field += 2)
  151. {
  152. if (c_layout[field] != 'p')
  153. flags = 0;
  154. else
  155. switch (c_layout[field + 1])
  156. {
  157. case 'w':
  158. if (field == 0)
  159. flags |= SCM_VTABLE_FLAG_SIMPLE_RW;
  160. break;
  161. case 'r':
  162. flags &= ~SCM_VTABLE_FLAG_SIMPLE_RW;
  163. break;
  164. default:
  165. flags = 0;
  166. }
  167. }
  168. if (flags & SCM_VTABLE_FLAG_SIMPLE)
  169. {
  170. /* VTABLE is simple so update its flags and record the size of its
  171. instances. */
  172. SCM_SET_VTABLE_FLAGS (vtable, flags);
  173. SCM_STRUCT_DATA_SET (vtable, scm_vtable_index_size, len / 2);
  174. }
  175. }
  176. static int
  177. scm_is_valid_vtable_layout (SCM layout)
  178. {
  179. size_t len, n;
  180. const char *c_layout;
  181. c_layout = scm_i_symbol_chars (layout);
  182. len = scm_i_symbol_length (layout);
  183. if (len % 2)
  184. return 0;
  185. for (n = 0; n < len; n += 2)
  186. switch (c_layout[n])
  187. {
  188. case 'u':
  189. case 'p':
  190. case 's':
  191. switch (c_layout[n+1])
  192. {
  193. case 'W':
  194. case 'R':
  195. case 'O':
  196. if (n + 2 != len)
  197. return 0;
  198. case 'w':
  199. case 'h':
  200. case 'r':
  201. case 'o':
  202. break;
  203. default:
  204. return 0;
  205. }
  206. break;
  207. default:
  208. return 0;
  209. }
  210. return 1;
  211. }
  212. /* Have OBJ, a newly created vtable, inherit flags from VTABLE. VTABLE is a
  213. vtable-vtable and OBJ is an instance of VTABLE. */
  214. void
  215. scm_i_struct_inherit_vtable_magic (SCM vtable, SCM obj)
  216. #define FUNC_NAME "%inherit-vtable-magic"
  217. {
  218. /* Verily, what is the deal here, you ask? Basically, we need to know a couple
  219. of properties of structures at runtime. For example, "is this structure a
  220. vtable of vtables (a metaclass)?"; also, "is this structure applicable?".
  221. Both of these questions also imply a certain layout of the structure. So
  222. instead of checking the layout at runtime, what we do is pre-verify the
  223. layout -- so that at runtime we can just check the applicable flag and
  224. dispatch directly to the Scheme procedure in slot 0. */
  225. SCM olayout;
  226. /* Verify that OBJ is a valid vtable. */
  227. if (! scm_is_valid_vtable_layout (SCM_VTABLE_LAYOUT (obj)))
  228. SCM_MISC_ERROR ("invalid layout for new vtable: ~a",
  229. scm_list_1 (SCM_VTABLE_LAYOUT (obj)));
  230. set_vtable_layout_flags (obj);
  231. /* If OBJ's vtable is compatible with the required vtable (class) layout, it
  232. is a metaclass. */
  233. olayout = scm_symbol_to_string (SCM_VTABLE_LAYOUT (obj));
  234. if (scm_is_true (scm_leq_p (scm_string_length (required_vtable_fields),
  235. scm_string_length (olayout)))
  236. && scm_is_true (scm_string_eq (olayout, required_vtable_fields,
  237. scm_from_size_t (0),
  238. scm_string_length (required_vtable_fields),
  239. scm_from_size_t (0),
  240. scm_string_length (required_vtable_fields))))
  241. SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VTABLE);
  242. /* Finally, if OBJ is an applicable class, verify that its vtable is
  243. compatible with the required applicable layout. */
  244. if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SETTER_VTABLE))
  245. {
  246. if (scm_is_false (scm_string_eq (olayout, required_applicable_with_setter_fields,
  247. scm_from_size_t (0),
  248. scm_from_size_t (4),
  249. scm_from_size_t (0),
  250. scm_from_size_t (4))))
  251. SCM_MISC_ERROR ("invalid applicable-with-setter struct layout",
  252. scm_list_1 (olayout));
  253. SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_APPLICABLE | SCM_VTABLE_FLAG_SETTER);
  254. }
  255. else if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_APPLICABLE_VTABLE))
  256. {
  257. if (scm_is_false (scm_string_eq (olayout, required_applicable_fields,
  258. scm_from_size_t (0),
  259. scm_from_size_t (2),
  260. scm_from_size_t (0),
  261. scm_from_size_t (2))))
  262. SCM_MISC_ERROR ("invalid applicable struct layout",
  263. scm_list_1 (olayout));
  264. SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_APPLICABLE);
  265. }
  266. SCM_SET_VTABLE_FLAGS (obj, SCM_VTABLE_FLAG_VALIDATED);
  267. }
  268. #undef FUNC_NAME
  269. static void
  270. scm_struct_init (SCM handle, SCM layout, size_t n_tail,
  271. size_t n_inits, scm_t_bits *inits)
  272. {
  273. SCM vtable;
  274. scm_t_bits *mem;
  275. vtable = SCM_STRUCT_VTABLE (handle);
  276. mem = SCM_STRUCT_DATA (handle);
  277. if (SCM_UNPACK (vtable) != 0
  278. && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
  279. && n_tail == 0
  280. && n_inits == SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size))
  281. /* The fast path: HANDLE has N_INITS "p" fields. */
  282. memcpy (mem, inits, n_inits * sizeof (SCM));
  283. else
  284. {
  285. scm_t_wchar prot = 0;
  286. int n_fields = scm_i_symbol_length (layout) / 2;
  287. int tailp = 0;
  288. int i;
  289. size_t inits_idx = 0;
  290. i = -2;
  291. while (n_fields)
  292. {
  293. if (!tailp)
  294. {
  295. i += 2;
  296. prot = scm_i_symbol_ref (layout, i+1);
  297. if (SCM_LAYOUT_TAILP (prot))
  298. {
  299. tailp = 1;
  300. prot = prot == 'R' ? 'r' : prot == 'W' ? 'w' : 'o';
  301. *mem++ = (scm_t_bits)n_tail;
  302. n_fields += n_tail - 1;
  303. if (n_fields == 0)
  304. break;
  305. }
  306. }
  307. switch (scm_i_symbol_ref (layout, i))
  308. {
  309. case 'u':
  310. if ((prot != 'r' && prot != 'w') || inits_idx == n_inits)
  311. *mem = 0;
  312. else
  313. {
  314. *mem = scm_to_ulong (SCM_PACK (inits[inits_idx]));
  315. inits_idx++;
  316. }
  317. break;
  318. case 'p':
  319. if ((prot != 'r' && prot != 'w') || inits_idx == n_inits)
  320. *mem = SCM_UNPACK (SCM_BOOL_F);
  321. else
  322. {
  323. *mem = inits[inits_idx];
  324. inits_idx++;
  325. }
  326. break;
  327. case 's':
  328. *mem = SCM_UNPACK (handle);
  329. break;
  330. }
  331. n_fields--;
  332. mem++;
  333. }
  334. }
  335. }
  336. SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0,
  337. (SCM x),
  338. "Return @code{#t} iff @var{x} is a structure object, else\n"
  339. "@code{#f}.")
  340. #define FUNC_NAME s_scm_struct_p
  341. {
  342. return scm_from_bool(SCM_STRUCTP (x));
  343. }
  344. #undef FUNC_NAME
  345. SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0,
  346. (SCM x),
  347. "Return @code{#t} iff @var{x} is a vtable structure.")
  348. #define FUNC_NAME s_scm_struct_vtable_p
  349. {
  350. if (!SCM_STRUCTP (x)
  351. || !SCM_STRUCT_VTABLE_FLAG_IS_SET (x, SCM_VTABLE_FLAG_VTABLE))
  352. return SCM_BOOL_F;
  353. if (!SCM_VTABLE_FLAG_IS_SET (x, SCM_VTABLE_FLAG_VALIDATED))
  354. SCM_MISC_ERROR ("vtable has invalid layout: ~A",
  355. scm_list_1 (SCM_VTABLE_LAYOUT (x)));
  356. return SCM_BOOL_T;
  357. }
  358. #undef FUNC_NAME
  359. /* Finalization: invoke the finalizer of the struct pointed to by PTR. */
  360. static void
  361. struct_finalizer_trampoline (void *ptr, void *unused_data)
  362. {
  363. SCM obj = PTR2SCM (ptr);
  364. scm_t_struct_finalize finalize = SCM_STRUCT_FINALIZER (obj);
  365. if (finalize)
  366. finalize (obj);
  367. }
  368. /* All struct data must be allocated at an address whose bottom three
  369. bits are zero. This is because the tag for a struct lives in the
  370. bottom three bits of the struct's car, and the upper bits point to
  371. the data of its vtable, which is a struct itself. Thus, if the
  372. address of that data doesn't end in three zeros, tagging it will
  373. destroy the pointer.
  374. I suppose we should make it clear here that, the data must be 8-byte aligned,
  375. *within* the struct, and the struct itself should be 8-byte aligned. In
  376. practice we ensure this because the data starts two words into a struct.
  377. This function allocates an 8-byte aligned block of memory, whose first word
  378. points to the given vtable data, then a data pointer, then n_words of data.
  379. */
  380. SCM
  381. scm_i_alloc_struct (scm_t_bits *vtable_data, int n_words)
  382. {
  383. SCM ret;
  384. ret = scm_words ((scm_t_bits)vtable_data | scm_tc3_struct, n_words + 2);
  385. SCM_SET_CELL_WORD_1 (ret, (scm_t_bits)SCM_CELL_OBJECT_LOC (ret, 2));
  386. /* vtable_data can be null when making a vtable vtable */
  387. if (vtable_data && vtable_data[scm_vtable_index_instance_finalize])
  388. /* Register a finalizer for the newly created instance. */
  389. scm_i_set_finalizer (SCM2PTR (ret), struct_finalizer_trampoline, NULL);
  390. return ret;
  391. }
  392. SCM
  393. scm_c_make_structv (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits *init)
  394. #define FUNC_NAME "make-struct"
  395. {
  396. SCM layout;
  397. size_t basic_size;
  398. SCM obj;
  399. SCM_VALIDATE_VTABLE (1, vtable);
  400. layout = SCM_VTABLE_LAYOUT (vtable);
  401. basic_size = scm_i_symbol_length (layout) / 2;
  402. if (n_tail != 0)
  403. {
  404. SCM layout_str, last_char;
  405. if (basic_size == 0)
  406. {
  407. bad_tail:
  408. SCM_MISC_ERROR ("tail array not allowed unless layout ends R, W, or O", SCM_EOL);
  409. }
  410. layout_str = scm_symbol_to_string (layout);
  411. last_char = scm_string_ref (layout_str,
  412. scm_from_size_t (2 * basic_size - 1));
  413. if (! SCM_LAYOUT_TAILP (SCM_CHAR (last_char)))
  414. goto bad_tail;
  415. }
  416. obj = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), basic_size + n_tail);
  417. scm_struct_init (obj, layout, n_tail, n_init, init);
  418. /* If we're making a vtable, validate its layout and inherit
  419. flags. However we allow for separation of allocation and
  420. initialization, to humor GOOPS, so only validate if the layout was
  421. passed as an initarg. */
  422. if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE)
  423. && scm_is_true (SCM_VTABLE_LAYOUT (obj)))
  424. scm_i_struct_inherit_vtable_magic (vtable, obj);
  425. return obj;
  426. }
  427. #undef FUNC_NAME
  428. SCM
  429. scm_c_make_struct (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits init, ...)
  430. {
  431. va_list foo;
  432. scm_t_bits *v;
  433. size_t i;
  434. v = alloca (sizeof (scm_t_bits) * n_init);
  435. va_start (foo, init);
  436. for (i = 0; i < n_init; i++)
  437. {
  438. v[i] = init;
  439. init = va_arg (foo, scm_t_bits);
  440. }
  441. va_end (foo);
  442. return scm_c_make_structv (vtable, n_tail, n_init, v);
  443. }
  444. SCM_DEFINE (scm_allocate_struct, "allocate-struct", 2, 0, 0,
  445. (SCM vtable, SCM nfields),
  446. "Allocate a new structure with space for @var{nfields} fields.\n\n"
  447. "@var{vtable} must be a vtable structure (@pxref{Vtables}).\n\n"
  448. "@var{nfields} must be a non-negative integer. Strictly speaking\n"
  449. "@var{nfields} is redundant, as the vtable carries the size\n"
  450. "for its instances. However passing it is useful as a sanity\n"
  451. "check, given that one module can inline a constructor in\n"
  452. "another.\n\n"
  453. "Fields will be initialized with their default values.")
  454. #define FUNC_NAME s_scm_allocate_struct
  455. {
  456. SCM ret;
  457. size_t c_nfields;
  458. SCM_VALIDATE_VTABLE (1, vtable);
  459. c_nfields = scm_to_size_t (nfields);
  460. SCM_ASSERT (SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size) == c_nfields,
  461. nfields, 2, FUNC_NAME);
  462. ret = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), c_nfields);
  463. if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)))
  464. {
  465. size_t n;
  466. for (n = 0; n < c_nfields; n++)
  467. SCM_STRUCT_DATA_SET (ret, n, SCM_UNPACK (SCM_BOOL_F));
  468. }
  469. else
  470. scm_struct_init (ret, SCM_VTABLE_LAYOUT (vtable), 0, 0, NULL);
  471. return ret;
  472. }
  473. #undef FUNC_NAME
  474. SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1,
  475. (SCM vtable, SCM tail_array_size, SCM init),
  476. "Create a new structure.\n\n"
  477. "@var{vtable} must be a vtable structure (@pxref{Vtables}).\n\n"
  478. "@var{tail_array_size} must be a non-negative integer. If the layout\n"
  479. "specification indicated by @var{vtable} includes a tail-array,\n"
  480. "this is the number of elements allocated to that array.\n\n"
  481. "The @var{init1}, @dots{} are optional arguments describing how\n"
  482. "successive fields of the structure should be initialized. Only fields\n"
  483. "with protection 'r' or 'w' can be initialized, except for fields of\n"
  484. "type 's', which are automatically initialized to point to the new\n"
  485. "structure itself. Fields with protection 'o' can not be initialized by\n"
  486. "Scheme programs.\n\n"
  487. "If fewer optional arguments than initializable fields are supplied,\n"
  488. "fields of type 'p' get default value #f while fields of type 'u' are\n"
  489. "initialized to 0.")
  490. #define FUNC_NAME s_scm_make_struct
  491. {
  492. size_t i, n_init;
  493. long ilen;
  494. scm_t_bits *v;
  495. SCM_VALIDATE_VTABLE (1, vtable);
  496. ilen = scm_ilength (init);
  497. if (ilen < 0)
  498. SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL);
  499. n_init = (size_t)ilen;
  500. /* best to use alloca, but init could be big, so hack to avoid a possible
  501. stack overflow */
  502. if (n_init < 64)
  503. v = alloca (n_init * sizeof(scm_t_bits));
  504. else
  505. v = scm_gc_malloc (n_init * sizeof(scm_t_bits), "struct");
  506. for (i = 0; i < n_init; i++, init = SCM_CDR (init))
  507. v[i] = SCM_UNPACK (SCM_CAR (init));
  508. return scm_c_make_structv (vtable, scm_to_size_t (tail_array_size), n_init, v);
  509. }
  510. #undef FUNC_NAME
  511. SCM
  512. scm_i_make_vtable_vtable (SCM fields)
  513. #define FUNC_NAME "make-vtable-vtable"
  514. {
  515. SCM layout, obj;
  516. size_t basic_size;
  517. scm_t_bits v;
  518. SCM_VALIDATE_STRING (1, fields);
  519. layout = scm_make_struct_layout (fields);
  520. if (!scm_is_valid_vtable_layout (layout))
  521. SCM_MISC_ERROR ("invalid user fields", scm_list_1 (fields));
  522. basic_size = scm_i_symbol_length (layout) / 2;
  523. obj = scm_i_alloc_struct (NULL, basic_size);
  524. /* Make it so that the vtable of OBJ is itself. */
  525. SCM_SET_CELL_WORD_0 (obj, (scm_t_bits) SCM_STRUCT_DATA (obj) | scm_tc3_struct);
  526. v = SCM_UNPACK (layout);
  527. scm_struct_init (obj, layout, 0, 1, &v);
  528. SCM_SET_VTABLE_FLAGS (obj,
  529. SCM_VTABLE_FLAG_VTABLE | SCM_VTABLE_FLAG_VALIDATED);
  530. return obj;
  531. }
  532. #undef FUNC_NAME
  533. SCM_DEFINE (scm_make_vtable, "make-vtable", 1, 1, 0,
  534. (SCM fields, SCM printer),
  535. "Create a vtable, for creating structures with the given\n"
  536. "@var{fields}.\n"
  537. "\n"
  538. "The optional @var{printer} argument is a function to be called\n"
  539. "@code{(@var{printer} struct port)} on the structures created.\n"
  540. "It should look at @var{struct} and write to @var{port}.")
  541. #define FUNC_NAME s_scm_make_vtable
  542. {
  543. if (SCM_UNBNDP (printer))
  544. printer = SCM_BOOL_F;
  545. return scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0,
  546. scm_list_2 (scm_make_struct_layout (fields),
  547. printer));
  548. }
  549. #undef FUNC_NAME
  550. /* Return true if S1 and S2 are equal structures, i.e., if their vtable and
  551. contents are the same. Field protections are honored. Thus, it is an
  552. error to test the equality of structures that contain opaque fields. */
  553. SCM
  554. scm_i_struct_equalp (SCM s1, SCM s2)
  555. #define FUNC_NAME "scm_i_struct_equalp"
  556. {
  557. SCM vtable1, vtable2, layout;
  558. size_t struct_size, field_num;
  559. SCM_VALIDATE_STRUCT (1, s1);
  560. SCM_VALIDATE_STRUCT (2, s2);
  561. vtable1 = SCM_STRUCT_VTABLE (s1);
  562. vtable2 = SCM_STRUCT_VTABLE (s2);
  563. if (!scm_is_eq (vtable1, vtable2))
  564. return SCM_BOOL_F;
  565. layout = SCM_STRUCT_LAYOUT (s1);
  566. struct_size = scm_i_symbol_length (layout) / 2;
  567. for (field_num = 0; field_num < struct_size; field_num++)
  568. {
  569. SCM s_field_num;
  570. SCM field1, field2;
  571. /* We have to use `scm_struct_ref ()' here so that fields are accessed
  572. consistently, notably wrt. field types and access rights. */
  573. s_field_num = scm_from_size_t (field_num);
  574. field1 = scm_struct_ref (s1, s_field_num);
  575. field2 = scm_struct_ref (s2, s_field_num);
  576. /* Self-referencing fields (type `s') must be skipped to avoid infinite
  577. recursion. */
  578. if (!(scm_is_eq (field1, s1) && (scm_is_eq (field2, s2))))
  579. if (scm_is_false (scm_equal_p (field1, field2)))
  580. return SCM_BOOL_F;
  581. }
  582. /* FIXME: Tail elements should be tested for equality. */
  583. return SCM_BOOL_T;
  584. }
  585. #undef FUNC_NAME
  586. SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0,
  587. (SCM handle, SCM pos),
  588. "Access the @var{pos}th field of struct associated with\n"
  589. "@var{handle}.\n"
  590. "\n"
  591. "If the field is of type 'p', then it can be set to an arbitrary\n"
  592. "value.\n"
  593. "\n"
  594. "If the field is of type 'u', then it can only be set to a\n"
  595. "non-negative integer value small enough to fit in one machine\n"
  596. "word.")
  597. #define FUNC_NAME s_scm_struct_ref
  598. {
  599. SCM vtable, answer = SCM_UNDEFINED;
  600. scm_t_bits *data;
  601. size_t p;
  602. SCM_VALIDATE_STRUCT (1, handle);
  603. vtable = SCM_STRUCT_VTABLE (handle);
  604. data = SCM_STRUCT_DATA (handle);
  605. p = scm_to_size_t (pos);
  606. if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
  607. && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size)))
  608. /* The fast path: HANDLE is a struct with only "p" fields. */
  609. answer = SCM_PACK (data[p]);
  610. else
  611. {
  612. SCM layout;
  613. size_t layout_len, n_fields;
  614. scm_t_wchar field_type = 0;
  615. layout = SCM_STRUCT_LAYOUT (handle);
  616. layout_len = scm_i_symbol_length (layout);
  617. n_fields = layout_len / 2;
  618. if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1)))
  619. n_fields += data[n_fields - 1];
  620. SCM_ASSERT_RANGE (1, pos, p < n_fields);
  621. if (p * 2 < layout_len)
  622. {
  623. scm_t_wchar ref;
  624. field_type = scm_i_symbol_ref (layout, p * 2);
  625. ref = scm_i_symbol_ref (layout, p * 2 + 1);
  626. if ((ref != 'r') && (ref != 'w') && (ref != 'h'))
  627. {
  628. if ((ref == 'R') || (ref == 'W'))
  629. field_type = 'u';
  630. else
  631. SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
  632. }
  633. }
  634. else if (scm_i_symbol_ref (layout, layout_len - 1) != 'O')
  635. field_type = scm_i_symbol_ref(layout, layout_len - 2);
  636. else
  637. SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
  638. switch (field_type)
  639. {
  640. case 'u':
  641. answer = scm_from_ulong (data[p]);
  642. break;
  643. #if 0
  644. case 'i':
  645. answer = scm_from_long (data[p]);
  646. break;
  647. case 'd':
  648. answer = scm_make_real (*((double *)&(data[p])));
  649. break;
  650. #endif
  651. case 's':
  652. case 'p':
  653. answer = SCM_PACK (data[p]);
  654. break;
  655. default:
  656. SCM_MISC_ERROR ("unrecognized field type: ~S",
  657. scm_list_1 (SCM_MAKE_CHAR (field_type)));
  658. }
  659. }
  660. return answer;
  661. }
  662. #undef FUNC_NAME
  663. SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0,
  664. (SCM handle, SCM pos, SCM val),
  665. "Set the slot of the structure @var{handle} with index @var{pos}\n"
  666. "to @var{val}. Signal an error if the slot can not be written\n"
  667. "to.")
  668. #define FUNC_NAME s_scm_struct_set_x
  669. {
  670. SCM vtable;
  671. scm_t_bits *data;
  672. size_t p;
  673. SCM_VALIDATE_STRUCT (1, handle);
  674. vtable = SCM_STRUCT_VTABLE (handle);
  675. data = SCM_STRUCT_DATA (handle);
  676. p = scm_to_size_t (pos);
  677. if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
  678. && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE_RW)
  679. && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size)))
  680. /* The fast path: HANDLE is a struct with only "pw" fields. */
  681. data[p] = SCM_UNPACK (val);
  682. else
  683. {
  684. SCM layout;
  685. size_t layout_len, n_fields;
  686. scm_t_wchar field_type = 0;
  687. layout = SCM_STRUCT_LAYOUT (handle);
  688. layout_len = scm_i_symbol_length (layout);
  689. n_fields = layout_len / 2;
  690. if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1)))
  691. n_fields += data[n_fields - 1];
  692. SCM_ASSERT_RANGE (1, pos, p < n_fields);
  693. if (p * 2 < layout_len)
  694. {
  695. char set_x;
  696. field_type = scm_i_symbol_ref (layout, p * 2);
  697. set_x = scm_i_symbol_ref (layout, p * 2 + 1);
  698. if (set_x != 'w' && set_x != 'h')
  699. SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
  700. }
  701. else if (scm_i_symbol_ref (layout, layout_len - 1) == 'W')
  702. field_type = scm_i_symbol_ref (layout, layout_len - 2);
  703. else
  704. SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
  705. switch (field_type)
  706. {
  707. case 'u':
  708. data[p] = SCM_NUM2ULONG (3, val);
  709. break;
  710. #if 0
  711. case 'i':
  712. data[p] = SCM_NUM2LONG (3, val);
  713. break;
  714. case 'd':
  715. *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3);
  716. break;
  717. #endif
  718. case 'p':
  719. data[p] = SCM_UNPACK (val);
  720. break;
  721. case 's':
  722. SCM_MISC_ERROR ("self fields immutable", SCM_EOL);
  723. default:
  724. SCM_MISC_ERROR ("unrecognized field type: ~S",
  725. scm_list_1 (SCM_MAKE_CHAR (field_type)));
  726. }
  727. }
  728. return val;
  729. }
  730. #undef FUNC_NAME
  731. SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0,
  732. (SCM handle),
  733. "Return the vtable structure that describes the type of struct\n"
  734. "associated with @var{handle}.")
  735. #define FUNC_NAME s_scm_struct_vtable
  736. {
  737. SCM_VALIDATE_STRUCT (1, handle);
  738. return SCM_STRUCT_VTABLE (handle);
  739. }
  740. #undef FUNC_NAME
  741. /* {Associating names and classes with vtables}
  742. *
  743. * The name of a vtable should probably be stored as a slot. This is
  744. * a backward compatible solution until agreement has been achieved on
  745. * how to associate names with vtables.
  746. */
  747. unsigned long
  748. scm_struct_ihashq (SCM obj, unsigned long n, void *closure)
  749. {
  750. /* The length of the hash table should be a relative prime it's not
  751. necessary to shift down the address. */
  752. return SCM_UNPACK (obj) % n;
  753. }
  754. SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0,
  755. (SCM vtable),
  756. "Return the name of the vtable @var{vtable}.")
  757. #define FUNC_NAME s_scm_struct_vtable_name
  758. {
  759. SCM_VALIDATE_VTABLE (1, vtable);
  760. return SCM_VTABLE_NAME (vtable);
  761. }
  762. #undef FUNC_NAME
  763. SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0,
  764. (SCM vtable, SCM name),
  765. "Set the name of the vtable @var{vtable} to @var{name}.")
  766. #define FUNC_NAME s_scm_set_struct_vtable_name_x
  767. {
  768. SCM_VALIDATE_VTABLE (1, vtable);
  769. SCM_VALIDATE_SYMBOL (2, name);
  770. SCM_SET_VTABLE_NAME (vtable, name);
  771. /* FIXME: remove this, and implement proper struct classes instead.
  772. (Vtables *are* classes.) */
  773. scm_i_define_class_for_vtable (vtable);
  774. return SCM_UNSPECIFIED;
  775. }
  776. #undef FUNC_NAME
  777. void
  778. scm_print_struct (SCM exp, SCM port, scm_print_state *pstate)
  779. {
  780. if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp))))
  781. scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate);
  782. else
  783. {
  784. SCM vtable = SCM_STRUCT_VTABLE (exp);
  785. SCM name = scm_struct_vtable_name (vtable);
  786. scm_puts_unlocked ("#<", port);
  787. if (scm_is_true (name))
  788. {
  789. scm_display (name, port);
  790. scm_putc_unlocked (' ', port);
  791. }
  792. else
  793. {
  794. if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE))
  795. scm_puts_unlocked ("vtable:", port);
  796. else
  797. scm_puts_unlocked ("struct:", port);
  798. scm_uintprint (SCM_UNPACK (vtable), 16, port);
  799. scm_putc_unlocked (' ', port);
  800. scm_write (SCM_VTABLE_LAYOUT (vtable), port);
  801. scm_putc_unlocked (' ', port);
  802. }
  803. scm_uintprint (SCM_UNPACK (exp), 16, port);
  804. /* hackety hack */
  805. if (SCM_STRUCT_APPLICABLE_P (exp))
  806. {
  807. if (scm_is_true (SCM_STRUCT_PROCEDURE (exp)))
  808. {
  809. scm_puts_unlocked (" proc: ", port);
  810. if (scm_is_true (scm_procedure_p (SCM_STRUCT_PROCEDURE (exp))))
  811. scm_write (SCM_STRUCT_PROCEDURE (exp), port);
  812. else
  813. scm_puts_unlocked ("(not a procedure?)", port);
  814. }
  815. if (SCM_STRUCT_SETTER_P (exp))
  816. {
  817. scm_puts_unlocked (" setter: ", port);
  818. scm_write (SCM_STRUCT_SETTER (exp), port);
  819. }
  820. }
  821. scm_putc_unlocked ('>', port);
  822. }
  823. }
  824. void
  825. scm_init_struct ()
  826. {
  827. SCM name;
  828. /* The first word of a struct is equal to `SCM_STRUCT_DATA (vtable) +
  829. scm_tc3_struct', and `SCM_STRUCT_DATA (vtable)' is 2 words after VTABLE by
  830. default. */
  831. GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits) + scm_tc3_struct);
  832. /* In the general case, `SCM_STRUCT_DATA (obj)' points 2 words after the
  833. beginning of a GC-allocated region; that region is different from that of
  834. OBJ once OBJ has undergone class redefinition. */
  835. GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits));
  836. required_vtable_fields = scm_from_locale_string (SCM_VTABLE_BASE_LAYOUT);
  837. scm_c_define ("standard-vtable-fields", required_vtable_fields);
  838. required_applicable_fields = scm_from_locale_string (SCM_APPLICABLE_BASE_LAYOUT);
  839. required_applicable_with_setter_fields = scm_from_locale_string (SCM_APPLICABLE_WITH_SETTER_BASE_LAYOUT);
  840. scm_standard_vtable_vtable =
  841. scm_i_make_vtable_vtable (required_vtable_fields);
  842. name = scm_from_utf8_symbol ("<standard-vtable>");
  843. scm_set_struct_vtable_name_x (scm_standard_vtable_vtable, name);
  844. scm_define (name, scm_standard_vtable_vtable);
  845. scm_applicable_struct_vtable_vtable =
  846. scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0,
  847. scm_list_1 (scm_make_struct_layout (required_vtable_fields)));
  848. name = scm_from_utf8_symbol ("<applicable-struct-vtable>");
  849. SCM_SET_VTABLE_FLAGS (scm_applicable_struct_vtable_vtable,
  850. SCM_VTABLE_FLAG_APPLICABLE_VTABLE);
  851. scm_set_struct_vtable_name_x (scm_applicable_struct_vtable_vtable, name);
  852. scm_define (name, scm_applicable_struct_vtable_vtable);
  853. scm_applicable_struct_with_setter_vtable_vtable =
  854. scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0,
  855. scm_list_1 (scm_make_struct_layout (required_vtable_fields)));
  856. name = scm_from_utf8_symbol ("<applicable-struct-with-setter-vtable>");
  857. scm_set_struct_vtable_name_x (scm_applicable_struct_with_setter_vtable_vtable, name);
  858. SCM_SET_VTABLE_FLAGS (scm_applicable_struct_with_setter_vtable_vtable,
  859. SCM_VTABLE_FLAG_APPLICABLE_VTABLE | SCM_VTABLE_FLAG_SETTER_VTABLE);
  860. scm_define (name, scm_applicable_struct_with_setter_vtable_vtable);
  861. scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout));
  862. scm_c_define ("vtable-index-printer",
  863. scm_from_int (scm_vtable_index_instance_printer));
  864. scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user));
  865. #include "libguile/struct.x"
  866. }
  867. /*
  868. Local Variables:
  869. c-file-style: "gnu"
  870. End:
  871. */