struct.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <alloca.h>
  22. #include <assert.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/async.h"
  25. #include "libguile/chars.h"
  26. #include "libguile/eval.h"
  27. #include "libguile/alist.h"
  28. #include "libguile/weaks.h"
  29. #include "libguile/hashtab.h"
  30. #include "libguile/ports.h"
  31. #include "libguile/strings.h"
  32. #include "libguile/srfi-13.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/struct.h"
  35. #include "libguile/eq.h"
  36. #ifdef HAVE_STRING_H
  37. #include <string.h>
  38. #endif
  39. #include "libguile/bdw-gc.h"
  40. /* A needlessly obscure test. */
  41. #define SCM_LAYOUT_TAILP(X) (((X) & 32) == 0) /* R, W or O */
  42. static SCM required_vtable_fields = SCM_BOOL_F;
  43. static SCM required_applicable_fields = SCM_BOOL_F;
  44. static SCM required_applicable_with_setter_fields = SCM_BOOL_F;
  45. SCM scm_applicable_struct_vtable_vtable;
  46. SCM scm_applicable_struct_with_setter_vtable_vtable;
  47. SCM scm_standard_vtable_vtable;
  48. SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0,
  49. (SCM fields),
  50. "Return a new structure layout object.\n\n"
  51. "@var{fields} must be a string made up of pairs of characters\n"
  52. "strung together. The first character of each pair describes a field\n"
  53. "type, the second a field protection. Allowed types are 'p' for\n"
  54. "GC-protected Scheme data, 'u' for unprotected binary data, and 's' for\n"
  55. "a field that points to the structure itself. Allowed protections\n"
  56. "are 'w' for mutable fields, 'h' for hidden fields, 'r' for read-only\n"
  57. "fields, and 'o' for opaque 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.\n\n"
  62. "The last field protection specification may be capitalized to indicate\n"
  63. "that the field is a tail-array.")
  64. #define FUNC_NAME s_scm_make_struct_layout
  65. {
  66. SCM new_sym;
  67. scm_t_wchar c;
  68. SCM_VALIDATE_STRING (1, fields);
  69. { /* scope */
  70. size_t len;
  71. int x;
  72. len = scm_i_string_length (fields);
  73. if (len % 2 == 1)
  74. SCM_MISC_ERROR ("odd length field specification: ~S",
  75. scm_list_1 (fields));
  76. for (x = 0; x < len; x += 2)
  77. {
  78. switch (c = scm_i_string_ref (fields, x))
  79. {
  80. case 'u':
  81. case 'p':
  82. #if 0
  83. case 'i':
  84. case 'd':
  85. #endif
  86. case 's':
  87. break;
  88. default:
  89. SCM_MISC_ERROR ("unrecognized field type: ~S",
  90. scm_list_1 (SCM_MAKE_CHAR (c)));
  91. }
  92. switch (c = scm_i_string_ref (fields, x + 1))
  93. {
  94. case 'w':
  95. case 'h':
  96. if (scm_i_string_ref (fields, x) == 's')
  97. SCM_MISC_ERROR ("self fields not writable", SCM_EOL);
  98. case 'r':
  99. case 'o':
  100. break;
  101. case 'R':
  102. case 'W':
  103. case 'O':
  104. if (scm_i_string_ref (fields, x) == 's')
  105. SCM_MISC_ERROR ("self fields not allowed in tail array",
  106. SCM_EOL);
  107. if (x != len - 2)
  108. SCM_MISC_ERROR ("tail array field must be last field in layout",
  109. SCM_EOL);
  110. break;
  111. default:
  112. SCM_MISC_ERROR ("unrecognized ref specification: ~S",
  113. scm_list_1 (SCM_MAKE_CHAR (c)));
  114. }
  115. #if 0
  116. if (scm_i_string_ref (fields, x, 'd'))
  117. {
  118. if (!scm_i_string_ref (fields, x+2, '-'))
  119. SCM_MISC_ERROR ("missing dash field at position ~A",
  120. scm_list_1 (scm_from_int (x / 2)));
  121. x += 2;
  122. goto recheck_ref;
  123. }
  124. #endif
  125. }
  126. new_sym = scm_string_to_symbol (fields);
  127. }
  128. scm_remember_upto_here_1 (fields);
  129. return new_sym;
  130. }
  131. #undef FUNC_NAME
  132. /* Check whether VTABLE instances have a simple layout (i.e., either only "pr"
  133. or only "pw" fields) and update its flags accordingly. */
  134. static void
  135. set_vtable_layout_flags (SCM vtable)
  136. {
  137. size_t len, field;
  138. SCM layout;
  139. const char *c_layout;
  140. scm_t_bits flags = SCM_VTABLE_FLAG_SIMPLE;
  141. layout = SCM_VTABLE_LAYOUT (vtable);
  142. c_layout = scm_i_symbol_chars (layout);
  143. len = scm_i_symbol_length (layout);
  144. assert (len % 2 == 0);
  145. /* Update FLAGS according to LAYOUT. */
  146. for (field = 0;
  147. field < len && flags & SCM_VTABLE_FLAG_SIMPLE;
  148. field += 2)
  149. {
  150. if (c_layout[field] != 'p')
  151. flags = 0;
  152. else
  153. switch (c_layout[field + 1])
  154. {
  155. case 'w':
  156. case 'W':
  157. if (field == 0)
  158. flags |= SCM_VTABLE_FLAG_SIMPLE_RW;
  159. break;
  160. case 'r':
  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 (GC_PTR ptr, GC_PTR 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. {
  389. /* Register a finalizer for the newly created instance. */
  390. GC_finalization_proc prev_finalizer;
  391. GC_PTR prev_finalizer_data;
  392. GC_REGISTER_FINALIZER_NO_ORDER (SCM2PTR (ret),
  393. struct_finalizer_trampoline,
  394. NULL,
  395. &prev_finalizer,
  396. &prev_finalizer_data);
  397. }
  398. return ret;
  399. }
  400. SCM
  401. scm_c_make_structv (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits *init)
  402. #define FUNC_NAME "make-struct"
  403. {
  404. SCM layout;
  405. size_t basic_size;
  406. SCM obj;
  407. SCM_VALIDATE_VTABLE (1, vtable);
  408. layout = SCM_VTABLE_LAYOUT (vtable);
  409. basic_size = scm_i_symbol_length (layout) / 2;
  410. if (n_tail != 0)
  411. {
  412. SCM layout_str, last_char;
  413. if (basic_size == 0)
  414. {
  415. bad_tail:
  416. SCM_MISC_ERROR ("tail array not allowed unless layout ends R, W, or O", SCM_EOL);
  417. }
  418. layout_str = scm_symbol_to_string (layout);
  419. last_char = scm_string_ref (layout_str,
  420. scm_from_size_t (2 * basic_size - 1));
  421. if (! SCM_LAYOUT_TAILP (SCM_CHAR (last_char)))
  422. goto bad_tail;
  423. }
  424. obj = scm_i_alloc_struct (SCM_STRUCT_DATA (vtable), basic_size + n_tail);
  425. scm_struct_init (obj, layout, n_tail, n_init, init);
  426. /* If we're making a vtable, validate its layout and inherit
  427. flags. However we allow for separation of allocation and
  428. initialization, to humor GOOPS, so only validate if the layout was
  429. passed as an initarg. */
  430. if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE)
  431. && scm_is_true (SCM_VTABLE_LAYOUT (obj)))
  432. scm_i_struct_inherit_vtable_magic (vtable, obj);
  433. return obj;
  434. }
  435. #undef FUNC_NAME
  436. SCM
  437. scm_c_make_struct (SCM vtable, size_t n_tail, size_t n_init, scm_t_bits init, ...)
  438. {
  439. va_list foo;
  440. scm_t_bits *v;
  441. size_t i;
  442. v = alloca (sizeof (scm_t_bits) * n_init);
  443. va_start (foo, init);
  444. for (i = 0; i < n_init; i++)
  445. {
  446. v[i] = init;
  447. init = va_arg (foo, scm_t_bits);
  448. }
  449. va_end (foo);
  450. return scm_c_make_structv (vtable, n_tail, n_init, v);
  451. }
  452. SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1,
  453. (SCM vtable, SCM tail_array_size, SCM init),
  454. "Create a new structure.\n\n"
  455. "@var{type} must be a vtable structure (@pxref{Vtables}).\n\n"
  456. "@var{tail-elts} must be a non-negative integer. If the layout\n"
  457. "specification indicated by @var{type} includes a tail-array,\n"
  458. "this is the number of elements allocated to that array.\n\n"
  459. "The @var{init1}, @dots{} are optional arguments describing how\n"
  460. "successive fields of the structure should be initialized. Only fields\n"
  461. "with protection 'r' or 'w' can be initialized, except for fields of\n"
  462. "type 's', which are automatically initialized to point to the new\n"
  463. "structure itself. Fields with protection 'o' can not be initialized by\n"
  464. "Scheme programs.\n\n"
  465. "If fewer optional arguments than initializable fields are supplied,\n"
  466. "fields of type 'p' get default value #f while fields of type 'u' are\n"
  467. "initialized to 0.\n\n"
  468. "For more information, see the documentation for @code{make-vtable-vtable}.")
  469. #define FUNC_NAME s_scm_make_struct
  470. {
  471. size_t i, n_init;
  472. long ilen;
  473. scm_t_bits *v;
  474. SCM_VALIDATE_VTABLE (1, vtable);
  475. ilen = scm_ilength (init);
  476. if (ilen < 0)
  477. SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL);
  478. n_init = (size_t)ilen;
  479. /* best to use alloca, but init could be big, so hack to avoid a possible
  480. stack overflow */
  481. if (n_init < 64)
  482. v = alloca (n_init * sizeof(scm_t_bits));
  483. else
  484. v = scm_gc_malloc (n_init * sizeof(scm_t_bits), "struct");
  485. for (i = 0; i < n_init; i++, init = SCM_CDR (init))
  486. v[i] = SCM_UNPACK (SCM_CAR (init));
  487. return scm_c_make_structv (vtable, scm_to_size_t (tail_array_size), n_init, v);
  488. }
  489. #undef FUNC_NAME
  490. SCM_DEFINE (scm_make_vtable_vtable, "make-vtable-vtable", 2, 0, 1,
  491. (SCM user_fields, SCM tail_array_size, SCM init),
  492. "Return a new, self-describing vtable structure.\n\n"
  493. "@var{user-fields} is a string describing user defined fields of the\n"
  494. "vtable beginning at index @code{vtable-offset-user}\n"
  495. "(see @code{make-struct-layout}).\n\n"
  496. "@var{tail-size} specifies the size of the tail-array (if any) of\n"
  497. "this vtable.\n\n"
  498. "@var{init1}, @dots{} are the optional initializers for the fields of\n"
  499. "the vtable.\n\n"
  500. "Vtables have one initializable system field---the struct printer.\n"
  501. "This field comes before the user fields in the initializers passed\n"
  502. "to @code{make-vtable-vtable} and @code{make-struct}, and thus works as\n"
  503. "a third optional argument to @code{make-vtable-vtable} and a fourth to\n"
  504. "@code{make-struct} when creating vtables:\n\n"
  505. "If the value is a procedure, it will be called instead of the standard\n"
  506. "printer whenever a struct described by this vtable is printed.\n"
  507. "The procedure will be called with arguments STRUCT and PORT.\n\n"
  508. "The structure of a struct is described by a vtable, so the vtable is\n"
  509. "in essence the type of the struct. The vtable is itself a struct with\n"
  510. "a vtable. This could go on forever if it weren't for the\n"
  511. "vtable-vtables which are self-describing vtables, and thus terminate\n"
  512. "the chain.\n\n"
  513. "There are several potential ways of using structs, but the standard\n"
  514. "one is to use three kinds of structs, together building up a type\n"
  515. "sub-system: one vtable-vtable working as the root and one or several\n"
  516. "\"types\", each with a set of \"instances\". (The vtable-vtable should be\n"
  517. "compared to the class <class> which is the class of itself.)\n\n"
  518. "@lisp\n"
  519. "(define ball-root (make-vtable-vtable \"pr\" 0))\n\n"
  520. "(define (make-ball-type ball-color)\n"
  521. " (make-struct ball-root 0\n"
  522. " (make-struct-layout \"pw\")\n"
  523. " (lambda (ball port)\n"
  524. " (format port \"#<a ~A ball owned by ~A>\"\n"
  525. " (color ball)\n"
  526. " (owner ball)))\n"
  527. " ball-color))\n"
  528. "(define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))\n"
  529. "(define (owner ball) (struct-ref ball 0))\n\n"
  530. "(define red (make-ball-type 'red))\n"
  531. "(define green (make-ball-type 'green))\n\n"
  532. "(define (make-ball type owner) (make-struct type 0 owner))\n\n"
  533. "(define ball (make-ball green 'Nisse))\n"
  534. "ball @result{} #<a green ball owned by Nisse>\n"
  535. "@end lisp")
  536. #define FUNC_NAME s_scm_make_vtable_vtable
  537. {
  538. SCM fields, layout, obj;
  539. size_t basic_size, n_tail, i, n_init;
  540. long ilen;
  541. scm_t_bits *v;
  542. SCM_VALIDATE_STRING (1, user_fields);
  543. ilen = scm_ilength (init);
  544. if (ilen < 0)
  545. SCM_MISC_ERROR ("Rest arguments do not form a proper list.", SCM_EOL);
  546. n_init = (size_t)ilen + 1; /* + 1 for the layout */
  547. /* best to use alloca, but init could be big, so hack to avoid a possible
  548. stack overflow */
  549. if (n_init < 64)
  550. v = alloca (n_init * sizeof(scm_t_bits));
  551. else
  552. v = scm_gc_malloc (n_init * sizeof(scm_t_bits), "struct");
  553. fields = scm_string_append (scm_list_2 (required_vtable_fields,
  554. user_fields));
  555. layout = scm_make_struct_layout (fields);
  556. if (!scm_is_valid_vtable_layout (layout))
  557. SCM_MISC_ERROR ("invalid user fields", scm_list_1 (user_fields));
  558. basic_size = scm_i_symbol_length (layout) / 2;
  559. n_tail = scm_to_size_t (tail_array_size);
  560. i = 0;
  561. v[i++] = SCM_UNPACK (layout);
  562. for (; i < n_init; i++, init = SCM_CDR (init))
  563. v[i] = SCM_UNPACK (SCM_CAR (init));
  564. SCM_CRITICAL_SECTION_START;
  565. obj = scm_i_alloc_struct (NULL, basic_size + n_tail);
  566. /* Make it so that the vtable of OBJ is itself. */
  567. SCM_SET_CELL_WORD_0 (obj, (scm_t_bits) SCM_STRUCT_DATA (obj) | scm_tc3_struct);
  568. SCM_CRITICAL_SECTION_END;
  569. scm_struct_init (obj, layout, n_tail, n_init, v);
  570. SCM_SET_VTABLE_FLAGS (obj,
  571. SCM_VTABLE_FLAG_VTABLE | SCM_VTABLE_FLAG_VALIDATED);
  572. return obj;
  573. }
  574. #undef FUNC_NAME
  575. SCM_DEFINE (scm_make_vtable, "make-vtable", 1, 1, 0,
  576. (SCM fields, SCM printer),
  577. "Create a vtable, for creating structures with the given\n"
  578. "@var{fields}.\n"
  579. "\n"
  580. "The optional @var{printer} argument is a function to be called\n"
  581. "@code{(@var{printer} struct port)} on the structures created.\n"
  582. "It should look at @var{struct} and write to @var{port}.")
  583. #define FUNC_NAME s_scm_make_vtable
  584. {
  585. if (SCM_UNBNDP (printer))
  586. printer = SCM_BOOL_F;
  587. return scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0,
  588. scm_list_2 (scm_make_struct_layout (fields),
  589. printer));
  590. }
  591. #undef FUNC_NAME
  592. /* Return true if S1 and S2 are equal structures, i.e., if their vtable and
  593. contents are the same. Field protections are honored. Thus, it is an
  594. error to test the equality of structures that contain opaque fields. */
  595. SCM
  596. scm_i_struct_equalp (SCM s1, SCM s2)
  597. #define FUNC_NAME "scm_i_struct_equalp"
  598. {
  599. SCM vtable1, vtable2, layout;
  600. size_t struct_size, field_num;
  601. SCM_VALIDATE_STRUCT (1, s1);
  602. SCM_VALIDATE_STRUCT (2, s2);
  603. vtable1 = SCM_STRUCT_VTABLE (s1);
  604. vtable2 = SCM_STRUCT_VTABLE (s2);
  605. if (!scm_is_eq (vtable1, vtable2))
  606. return SCM_BOOL_F;
  607. layout = SCM_STRUCT_LAYOUT (s1);
  608. struct_size = scm_i_symbol_length (layout) / 2;
  609. for (field_num = 0; field_num < struct_size; field_num++)
  610. {
  611. SCM s_field_num;
  612. SCM field1, field2;
  613. /* We have to use `scm_struct_ref ()' here so that fields are accessed
  614. consistently, notably wrt. field types and access rights. */
  615. s_field_num = scm_from_size_t (field_num);
  616. field1 = scm_struct_ref (s1, s_field_num);
  617. field2 = scm_struct_ref (s2, s_field_num);
  618. /* Self-referencing fields (type `s') must be skipped to avoid infinite
  619. recursion. */
  620. if (!(scm_is_eq (field1, s1) && (scm_is_eq (field2, s2))))
  621. if (scm_is_false (scm_equal_p (field1, field2)))
  622. return SCM_BOOL_F;
  623. }
  624. /* FIXME: Tail elements should be tested for equality. */
  625. return SCM_BOOL_T;
  626. }
  627. #undef FUNC_NAME
  628. SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0,
  629. (SCM handle, SCM pos),
  630. "Access the @var{n}th field of @var{struct}.\n\n"
  631. "If the field is of type 'p', then it can be set to an arbitrary value.\n\n"
  632. "If the field is of type 'u', then it can only be set to a non-negative\n"
  633. "integer value small enough to fit in one machine word.")
  634. #define FUNC_NAME s_scm_struct_ref
  635. {
  636. SCM vtable, answer = SCM_UNDEFINED;
  637. scm_t_bits *data;
  638. size_t p;
  639. SCM_VALIDATE_STRUCT (1, handle);
  640. vtable = SCM_STRUCT_VTABLE (handle);
  641. data = SCM_STRUCT_DATA (handle);
  642. p = scm_to_size_t (pos);
  643. if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
  644. && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size)))
  645. /* The fast path: HANDLE is a struct with only "p" fields. */
  646. answer = SCM_PACK (data[p]);
  647. else
  648. {
  649. SCM layout;
  650. size_t layout_len, n_fields;
  651. scm_t_wchar field_type = 0;
  652. layout = SCM_STRUCT_LAYOUT (handle);
  653. layout_len = scm_i_symbol_length (layout);
  654. n_fields = layout_len / 2;
  655. if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1)))
  656. n_fields += data[n_fields - 1];
  657. SCM_ASSERT_RANGE (1, pos, p < n_fields);
  658. if (p * 2 < layout_len)
  659. {
  660. scm_t_wchar ref;
  661. field_type = scm_i_symbol_ref (layout, p * 2);
  662. ref = scm_i_symbol_ref (layout, p * 2 + 1);
  663. if ((ref != 'r') && (ref != 'w') && (ref != 'h'))
  664. {
  665. if ((ref == 'R') || (ref == 'W'))
  666. field_type = 'u';
  667. else
  668. SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
  669. }
  670. }
  671. else if (scm_i_symbol_ref (layout, layout_len - 1) != 'O')
  672. field_type = scm_i_symbol_ref(layout, layout_len - 2);
  673. else
  674. SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
  675. switch (field_type)
  676. {
  677. case 'u':
  678. answer = scm_from_ulong (data[p]);
  679. break;
  680. #if 0
  681. case 'i':
  682. answer = scm_from_long (data[p]);
  683. break;
  684. case 'd':
  685. answer = scm_make_real (*((double *)&(data[p])));
  686. break;
  687. #endif
  688. case 's':
  689. case 'p':
  690. answer = SCM_PACK (data[p]);
  691. break;
  692. default:
  693. SCM_MISC_ERROR ("unrecognized field type: ~S",
  694. scm_list_1 (SCM_MAKE_CHAR (field_type)));
  695. }
  696. }
  697. return answer;
  698. }
  699. #undef FUNC_NAME
  700. SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0,
  701. (SCM handle, SCM pos, SCM val),
  702. "Set the slot of the structure @var{handle} with index @var{pos}\n"
  703. "to @var{val}. Signal an error if the slot can not be written\n"
  704. "to.")
  705. #define FUNC_NAME s_scm_struct_set_x
  706. {
  707. SCM vtable;
  708. scm_t_bits *data;
  709. size_t p;
  710. SCM_VALIDATE_STRUCT (1, handle);
  711. vtable = SCM_STRUCT_VTABLE (handle);
  712. data = SCM_STRUCT_DATA (handle);
  713. p = scm_to_size_t (pos);
  714. if (SCM_LIKELY (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE)
  715. && SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SIMPLE_RW)
  716. && p < SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size)))
  717. /* The fast path: HANDLE is a struct with only "pw" fields. */
  718. data[p] = SCM_UNPACK (val);
  719. else
  720. {
  721. SCM layout;
  722. size_t layout_len, n_fields;
  723. scm_t_wchar field_type = 0;
  724. layout = SCM_STRUCT_LAYOUT (handle);
  725. layout_len = scm_i_symbol_length (layout);
  726. n_fields = layout_len / 2;
  727. if (SCM_LAYOUT_TAILP (scm_i_symbol_ref (layout, layout_len - 1)))
  728. n_fields += data[n_fields - 1];
  729. SCM_ASSERT_RANGE (1, pos, p < n_fields);
  730. if (p * 2 < layout_len)
  731. {
  732. char set_x;
  733. field_type = scm_i_symbol_ref (layout, p * 2);
  734. set_x = scm_i_symbol_ref (layout, p * 2 + 1);
  735. if (set_x != 'w' && set_x != 'h')
  736. SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
  737. }
  738. else if (scm_i_symbol_ref (layout, layout_len - 1) == 'W')
  739. field_type = scm_i_symbol_ref (layout, layout_len - 2);
  740. else
  741. SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
  742. switch (field_type)
  743. {
  744. case 'u':
  745. data[p] = SCM_NUM2ULONG (3, val);
  746. break;
  747. #if 0
  748. case 'i':
  749. data[p] = SCM_NUM2LONG (3, val);
  750. break;
  751. case 'd':
  752. *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3);
  753. break;
  754. #endif
  755. case 'p':
  756. data[p] = SCM_UNPACK (val);
  757. break;
  758. case 's':
  759. SCM_MISC_ERROR ("self fields immutable", SCM_EOL);
  760. default:
  761. SCM_MISC_ERROR ("unrecognized field type: ~S",
  762. scm_list_1 (SCM_MAKE_CHAR (field_type)));
  763. }
  764. }
  765. return val;
  766. }
  767. #undef FUNC_NAME
  768. SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0,
  769. (SCM handle),
  770. "Return the vtable structure that describes the type of @var{struct}.")
  771. #define FUNC_NAME s_scm_struct_vtable
  772. {
  773. SCM_VALIDATE_STRUCT (1, handle);
  774. return SCM_STRUCT_VTABLE (handle);
  775. }
  776. #undef FUNC_NAME
  777. SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0,
  778. (SCM handle),
  779. "Return the vtable tag of the structure @var{handle}.")
  780. #define FUNC_NAME s_scm_struct_vtable_tag
  781. {
  782. SCM_VALIDATE_VTABLE (1, handle);
  783. return scm_from_unsigned_integer
  784. (((scm_t_bits)SCM_STRUCT_DATA (handle)) >> 3);
  785. }
  786. #undef FUNC_NAME
  787. /* {Associating names and classes with vtables}
  788. *
  789. * The name of a vtable should probably be stored as a slot. This is
  790. * a backward compatible solution until agreement has been achieved on
  791. * how to associate names with vtables.
  792. */
  793. unsigned long
  794. scm_struct_ihashq (SCM obj, unsigned long n, void *closure)
  795. {
  796. /* The length of the hash table should be a relative prime it's not
  797. necessary to shift down the address. */
  798. return SCM_UNPACK (obj) % n;
  799. }
  800. SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0,
  801. (SCM vtable),
  802. "Return the name of the vtable @var{vtable}.")
  803. #define FUNC_NAME s_scm_struct_vtable_name
  804. {
  805. SCM_VALIDATE_VTABLE (1, vtable);
  806. return SCM_VTABLE_NAME (vtable);
  807. }
  808. #undef FUNC_NAME
  809. SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0,
  810. (SCM vtable, SCM name),
  811. "Set the name of the vtable @var{vtable} to @var{name}.")
  812. #define FUNC_NAME s_scm_set_struct_vtable_name_x
  813. {
  814. SCM_VALIDATE_VTABLE (1, vtable);
  815. SCM_VALIDATE_SYMBOL (2, name);
  816. SCM_SET_VTABLE_NAME (vtable, name);
  817. /* FIXME: remove this, and implement proper struct classes instead.
  818. (Vtables *are* classes.) */
  819. scm_i_define_class_for_vtable (vtable);
  820. return SCM_UNSPECIFIED;
  821. }
  822. #undef FUNC_NAME
  823. void
  824. scm_print_struct (SCM exp, SCM port, scm_print_state *pstate)
  825. {
  826. if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp))))
  827. scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate);
  828. else
  829. {
  830. SCM vtable = SCM_STRUCT_VTABLE (exp);
  831. SCM name = scm_struct_vtable_name (vtable);
  832. scm_puts ("#<", port);
  833. if (scm_is_true (name))
  834. {
  835. scm_display (name, port);
  836. scm_putc (' ', port);
  837. }
  838. else
  839. {
  840. if (SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_VTABLE))
  841. scm_puts ("vtable:", port);
  842. else
  843. scm_puts ("struct:", port);
  844. scm_uintprint (SCM_UNPACK (vtable), 16, port);
  845. scm_putc (' ', port);
  846. scm_write (SCM_VTABLE_LAYOUT (vtable), port);
  847. scm_putc (' ', port);
  848. }
  849. scm_uintprint (SCM_UNPACK (exp), 16, port);
  850. /* hackety hack */
  851. if (SCM_STRUCT_APPLICABLE_P (exp))
  852. {
  853. if (scm_is_true (SCM_STRUCT_PROCEDURE (exp)))
  854. {
  855. scm_puts (" proc: ", port);
  856. if (scm_is_true (scm_procedure_p (SCM_STRUCT_PROCEDURE (exp))))
  857. scm_write (SCM_STRUCT_PROCEDURE (exp), port);
  858. else
  859. scm_puts ("(not a procedure?)", port);
  860. }
  861. if (SCM_STRUCT_SETTER_P (exp))
  862. {
  863. scm_puts (" setter: ", port);
  864. scm_write (SCM_STRUCT_SETTER (exp), port);
  865. }
  866. }
  867. scm_putc ('>', port);
  868. }
  869. }
  870. void
  871. scm_init_struct ()
  872. {
  873. /* The first word of a struct is equal to `SCM_STRUCT_DATA (vtable) +
  874. scm_tc3_struct', and `SCM_STRUCT_DATA (vtable)' is 2 words after VTABLE by
  875. default. */
  876. GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits) + scm_tc3_struct);
  877. /* In the general case, `SCM_STRUCT_DATA (obj)' points 2 words after the
  878. beginning of a GC-allocated region; that region is different from that of
  879. OBJ once OBJ has undergone class redefinition. */
  880. GC_REGISTER_DISPLACEMENT (2 * sizeof (scm_t_bits));
  881. required_vtable_fields = scm_from_locale_string (SCM_VTABLE_BASE_LAYOUT);
  882. required_applicable_fields = scm_from_locale_string (SCM_APPLICABLE_BASE_LAYOUT);
  883. required_applicable_with_setter_fields = scm_from_locale_string (SCM_APPLICABLE_WITH_SETTER_BASE_LAYOUT);
  884. scm_standard_vtable_vtable =
  885. scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL);
  886. scm_applicable_struct_vtable_vtable =
  887. scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0,
  888. scm_list_1 (scm_make_struct_layout (required_vtable_fields)));
  889. SCM_SET_VTABLE_FLAGS (scm_applicable_struct_vtable_vtable,
  890. SCM_VTABLE_FLAG_APPLICABLE_VTABLE);
  891. scm_c_define ("<applicable-struct-vtable>", scm_applicable_struct_vtable_vtable);
  892. scm_applicable_struct_with_setter_vtable_vtable =
  893. scm_make_struct (scm_standard_vtable_vtable, SCM_INUM0,
  894. scm_list_1 (scm_make_struct_layout (required_vtable_fields)));
  895. SCM_SET_VTABLE_FLAGS (scm_applicable_struct_with_setter_vtable_vtable,
  896. SCM_VTABLE_FLAG_APPLICABLE_VTABLE | SCM_VTABLE_FLAG_SETTER_VTABLE);
  897. scm_c_define ("<applicable-struct-with-setter-vtable>", scm_applicable_struct_with_setter_vtable_vtable);
  898. scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout));
  899. scm_c_define ("vtable-index-printer",
  900. scm_from_int (scm_vtable_index_instance_printer));
  901. scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user));
  902. #include "libguile/struct.x"
  903. }
  904. /*
  905. Local Variables:
  906. c-file-style: "gnu"
  907. End:
  908. */