struct.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /* Copyright (C) 1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include "libguile/_scm.h"
  42. #include "libguile/chars.h"
  43. #include "libguile/eval.h"
  44. #include "libguile/alist.h"
  45. #include "libguile/weaks.h"
  46. #include "libguile/hashtab.h"
  47. #include "libguile/ports.h"
  48. #include "libguile/strings.h"
  49. #include "libguile/validate.h"
  50. #include "libguile/struct.h"
  51. #ifdef HAVE_STRING_H
  52. #include <string.h>
  53. #endif
  54. static SCM required_vtable_fields = SCM_BOOL_F;
  55. SCM scm_struct_table;
  56. SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0,
  57. (SCM fields),
  58. "Return a new structure layout object.\n\n"
  59. "@var{fields} must be a string made up of pairs of characters\n"
  60. "strung together. The first character of each pair describes a field\n"
  61. "type, the second a field protection. Allowed types are 'p' for\n"
  62. "GC-protected Scheme data, 'u' for unprotected binary data, and 's' for\n"
  63. "a field that points to the structure itself. Allowed protections\n"
  64. "are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque\n"
  65. "fields. The last field protection specification may be capitalized to\n"
  66. "indicate that the field is a tail-array.")
  67. #define FUNC_NAME s_scm_make_struct_layout
  68. {
  69. SCM new_sym;
  70. SCM_VALIDATE_STRING (1, fields);
  71. { /* scope */
  72. char * field_desc;
  73. size_t len;
  74. int x;
  75. len = SCM_STRING_LENGTH (fields);
  76. if (len % 2 == 1)
  77. SCM_MISC_ERROR ("odd length field specification: ~S",
  78. scm_list_1 (fields));
  79. field_desc = SCM_STRING_CHARS (fields);
  80. for (x = 0; x < len; x += 2)
  81. {
  82. switch (field_desc[x])
  83. {
  84. case 'u':
  85. case 'p':
  86. #if 0
  87. case 'i':
  88. case 'd':
  89. #endif
  90. case 's':
  91. break;
  92. default:
  93. SCM_MISC_ERROR ("unrecognized field type: ~S",
  94. scm_list_1 (SCM_MAKE_CHAR (field_desc[x])));
  95. }
  96. switch (field_desc[x + 1])
  97. {
  98. case 'w':
  99. if (field_desc[x] == 's')
  100. SCM_MISC_ERROR ("self fields not writable", SCM_EOL);
  101. case 'r':
  102. case 'o':
  103. break;
  104. case 'R':
  105. case 'W':
  106. case 'O':
  107. if (field_desc[x] == 's')
  108. SCM_MISC_ERROR ("self fields not allowed in tail array",
  109. SCM_EOL);
  110. if (x != len - 2)
  111. SCM_MISC_ERROR ("tail array field must be last field in layout",
  112. SCM_EOL);
  113. break;
  114. default:
  115. SCM_MISC_ERROR ("unrecognized ref specification: ~S",
  116. scm_list_1 (SCM_MAKE_CHAR (field_desc[x + 1])));
  117. }
  118. #if 0
  119. if (field_desc[x] == 'd')
  120. {
  121. if (field_desc[x + 2] != '-')
  122. SCM_MISC_ERROR ("missing dash field at position ~A",
  123. scm_list_1 (SCM_MAKINUM (x / 2)));
  124. x += 2;
  125. goto recheck_ref;
  126. }
  127. #endif
  128. }
  129. new_sym = scm_mem2symbol (field_desc, len);
  130. }
  131. return scm_return_first (new_sym, fields);
  132. }
  133. #undef FUNC_NAME
  134. static void
  135. scm_struct_init (SCM handle, SCM layout, scm_t_bits * mem, int tail_elts, SCM inits)
  136. {
  137. unsigned char * fields_desc = (unsigned char *) SCM_SYMBOL_CHARS (layout) - 2;
  138. unsigned char prot = 0;
  139. int n_fields = SCM_SYMBOL_LENGTH (layout) / 2;
  140. int tailp = 0;
  141. while (n_fields)
  142. {
  143. if (!tailp)
  144. {
  145. fields_desc += 2;
  146. prot = fields_desc[1];
  147. if (SCM_LAYOUT_TAILP (prot))
  148. {
  149. tailp = 1;
  150. prot = prot == 'R' ? 'r' : prot == 'W' ? 'w' : 'o';
  151. *mem++ = tail_elts;
  152. n_fields += tail_elts - 1;
  153. if (n_fields == 0)
  154. break;
  155. }
  156. }
  157. switch (*fields_desc)
  158. {
  159. #if 0
  160. case 'i':
  161. if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
  162. *mem = 0;
  163. else
  164. {
  165. *mem = scm_num2long (SCM_CAR (inits), SCM_ARGn, "scm_struct_init");
  166. inits = SCM_CDR (inits);
  167. }
  168. break;
  169. #endif
  170. case 'u':
  171. if ((prot != 'r' && prot != 'w') || SCM_NULLP (inits))
  172. *mem = 0;
  173. else
  174. {
  175. *mem = scm_num2ulong (SCM_CAR (inits),
  176. SCM_ARGn,
  177. "scm_struct_init");
  178. inits = SCM_CDR (inits);
  179. }
  180. break;
  181. case 'p':
  182. if ((prot != 'r' && prot != 'w') || SCM_NULLP (inits))
  183. *mem = SCM_UNPACK (SCM_BOOL_F);
  184. else
  185. {
  186. *mem = SCM_UNPACK (SCM_CAR (inits));
  187. inits = SCM_CDR (inits);
  188. }
  189. break;
  190. #if 0
  191. case 'd':
  192. if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
  193. *((double *)mem) = 0.0;
  194. else
  195. {
  196. *mem = scm_num2dbl (SCM_CAR (inits), "scm_struct_init");
  197. inits = SCM_CDR (inits);
  198. }
  199. fields_desc += 2;
  200. break;
  201. #endif
  202. case 's':
  203. *mem = SCM_UNPACK (handle);
  204. break;
  205. }
  206. n_fields--;
  207. mem++;
  208. }
  209. }
  210. SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0,
  211. (SCM x),
  212. "Return @code{#t} iff @var{x} is a structure object, else\n"
  213. "@code{#f}.")
  214. #define FUNC_NAME s_scm_struct_p
  215. {
  216. return SCM_BOOL(SCM_STRUCTP (x));
  217. }
  218. #undef FUNC_NAME
  219. SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0,
  220. (SCM x),
  221. "Return @code{#t} iff @var{x} is a vtable structure.")
  222. #define FUNC_NAME s_scm_struct_vtable_p
  223. {
  224. SCM layout;
  225. scm_t_bits * mem;
  226. if (!SCM_STRUCTP (x))
  227. return SCM_BOOL_F;
  228. layout = SCM_STRUCT_LAYOUT (x);
  229. if (SCM_SYMBOL_LENGTH (layout) < SCM_STRING_LENGTH (required_vtable_fields))
  230. return SCM_BOOL_F;
  231. if (strncmp (SCM_SYMBOL_CHARS (layout), SCM_STRING_CHARS (required_vtable_fields),
  232. SCM_STRING_LENGTH (required_vtable_fields)))
  233. return SCM_BOOL_F;
  234. mem = SCM_STRUCT_DATA (x);
  235. if (mem[1] != 0)
  236. return SCM_BOOL_F;
  237. return SCM_BOOL (SCM_SYMBOLP (SCM_PACK (mem[0])));
  238. }
  239. #undef FUNC_NAME
  240. /* All struct data must be allocated at an address whose bottom three
  241. bits are zero. This is because the tag for a struct lives in the
  242. bottom three bits of the struct's car, and the upper bits point to
  243. the data of its vtable, which is a struct itself. Thus, if the
  244. address of that data doesn't end in three zeros, tagging it will
  245. destroy the pointer.
  246. This function allocates a block of memory, and returns a pointer at
  247. least scm_struct_n_extra_words words into the block. Furthermore,
  248. it guarantees that that pointer's least three significant bits are
  249. all zero.
  250. The argument n_words should be the number of words that should
  251. appear after the returned address. (That is, it shouldn't include
  252. scm_struct_n_extra_words.)
  253. This function initializes the following fields of the struct:
  254. scm_struct_i_ptr --- the actual start of the block of memory; the
  255. address you should pass to 'free' to dispose of the block.
  256. This field allows us to both guarantee that the returned
  257. address is divisible by eight, and allow the GC to free the
  258. block.
  259. scm_struct_i_n_words --- the number of words allocated to the
  260. block, including the extra fields. This is used by the GC.
  261. Ugh. */
  262. scm_t_bits *
  263. scm_alloc_struct (int n_words, int n_extra, char *who)
  264. {
  265. int size = sizeof (scm_t_bits) * (n_words + n_extra) + 7;
  266. void * block = scm_must_malloc (size, who);
  267. /* Adjust the pointer to hide the extra words. */
  268. scm_t_bits * p = (scm_t_bits *) block + n_extra;
  269. /* Adjust it even further so it's aligned on an eight-byte boundary. */
  270. p = (scm_t_bits *) (((scm_t_bits) p + 7) & ~7);
  271. /* Initialize a few fields as described above. */
  272. p[scm_struct_i_free] = (scm_t_bits) scm_struct_free_standard;
  273. p[scm_struct_i_ptr] = (scm_t_bits) block;
  274. p[scm_struct_i_n_words] = n_words;
  275. p[scm_struct_i_flags] = 0;
  276. return p;
  277. }
  278. size_t
  279. scm_struct_free_0 (scm_t_bits * vtable SCM_UNUSED,
  280. scm_t_bits * data SCM_UNUSED)
  281. {
  282. return 0;
  283. }
  284. size_t
  285. scm_struct_free_light (scm_t_bits * vtable, scm_t_bits * data)
  286. {
  287. scm_must_free (data);
  288. return vtable [scm_struct_i_size] & ~SCM_STRUCTF_MASK;
  289. }
  290. size_t
  291. scm_struct_free_standard (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
  292. {
  293. size_t n = (data[scm_struct_i_n_words] + scm_struct_n_extra_words)
  294. * sizeof (scm_t_bits) + 7;
  295. scm_must_free ((void *) data[scm_struct_i_ptr]);
  296. return n;
  297. }
  298. size_t
  299. scm_struct_free_entity (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
  300. {
  301. size_t n = (data[scm_struct_i_n_words] + scm_struct_entity_n_extra_words)
  302. * sizeof (scm_t_bits) + 7;
  303. scm_must_free ((void *) data[scm_struct_i_ptr]);
  304. return n;
  305. }
  306. static void *
  307. scm_struct_gc_init (void *dummy1 SCM_UNUSED,
  308. void *dummy2 SCM_UNUSED,
  309. void *dummy3 SCM_UNUSED)
  310. {
  311. scm_structs_to_free = SCM_EOL;
  312. return 0;
  313. }
  314. static void *
  315. scm_free_structs (void *dummy1 SCM_UNUSED,
  316. void *dummy2 SCM_UNUSED,
  317. void *dummy3 SCM_UNUSED)
  318. {
  319. SCM newchain = scm_structs_to_free;
  320. do
  321. {
  322. /* Mark vtables in GC chain. GC mark set means delay freeing. */
  323. SCM chain = newchain;
  324. while (!SCM_NULLP (chain))
  325. {
  326. SCM vtable = SCM_STRUCT_VTABLE (chain);
  327. if (SCM_STRUCT_GC_CHAIN (vtable) != 0 && vtable != chain)
  328. SCM_SETGCMARK (vtable);
  329. chain = SCM_STRUCT_GC_CHAIN (chain);
  330. }
  331. /* Free unmarked structs. */
  332. chain = newchain;
  333. newchain = SCM_EOL;
  334. while (!SCM_NULLP (chain))
  335. {
  336. SCM obj = chain;
  337. chain = SCM_STRUCT_GC_CHAIN (chain);
  338. if (SCM_GCMARKP (obj))
  339. {
  340. SCM_CLRGCMARK (obj);
  341. SCM_SET_STRUCT_GC_CHAIN (obj, newchain);
  342. newchain = obj;
  343. }
  344. else
  345. {
  346. scm_t_bits word0 = SCM_CELL_WORD_0 (obj) - scm_tc3_cons_gloc;
  347. /* access as struct */
  348. scm_t_bits * vtable_data = (scm_t_bits *) word0;
  349. scm_t_bits * data = SCM_STRUCT_DATA (obj);
  350. scm_t_struct_free free_struct_data
  351. = ((scm_t_struct_free) vtable_data[scm_struct_i_free]);
  352. SCM_SET_CELL_TYPE (obj, scm_tc_free_cell);
  353. free_struct_data (vtable_data, data);
  354. }
  355. }
  356. }
  357. while (!SCM_NULLP (newchain));
  358. return 0;
  359. }
  360. SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1,
  361. (SCM vtable, SCM tail_array_size, SCM init),
  362. "Create a new structure.\n\n"
  363. "@var{type} must be a vtable structure (@pxref{Vtables}).\n\n"
  364. "@var{tail-elts} must be a non-negative integer. If the layout\n"
  365. "specification indicated by @var{type} includes a tail-array,\n"
  366. "this is the number of elements allocated to that array.\n\n"
  367. "The @var{init1}, @dots{} are optional arguments describing how\n"
  368. "successive fields of the structure should be initialized. Only fields\n"
  369. "with protection 'r' or 'w' can be initialized, except for fields of\n"
  370. "type 's', which are automatically initialized to point to the new\n"
  371. "structure itself; fields with protection 'o' can not be initialized by\n"
  372. "Scheme programs.\n\n"
  373. "If fewer optional arguments than initializable fields are supplied,\n"
  374. "fields of type 'p' get default value #f while fields of type 'u' are\n"
  375. "initialized to 0.\n\n"
  376. "Structs are currently the basic representation for record-like data\n"
  377. "structures in Guile. The plan is to eventually replace them with a\n"
  378. "new representation which will at the same time be easier to use and\n"
  379. "more powerful.\n\n"
  380. "For more information, see the documentation for @code{make-vtable-vtable}.")
  381. #define FUNC_NAME s_scm_make_struct
  382. {
  383. SCM layout;
  384. int basic_size;
  385. int tail_elts;
  386. scm_t_bits * data;
  387. SCM handle;
  388. SCM_VALIDATE_VTABLE (1,vtable);
  389. SCM_VALIDATE_INUM (2,tail_array_size);
  390. SCM_VALIDATE_REST_ARGUMENT (init);
  391. layout = SCM_PACK (SCM_STRUCT_DATA (vtable) [scm_vtable_index_layout]);
  392. basic_size = SCM_SYMBOL_LENGTH (layout) / 2;
  393. tail_elts = SCM_INUM (tail_array_size);
  394. SCM_NEWCELL2 (handle);
  395. SCM_DEFER_INTS;
  396. if (SCM_STRUCT_DATA (vtable)[scm_struct_i_flags] & SCM_STRUCTF_ENTITY)
  397. {
  398. data = scm_alloc_struct (basic_size + tail_elts,
  399. scm_struct_entity_n_extra_words,
  400. "make-struct");
  401. data[scm_struct_i_procedure] = SCM_UNPACK (SCM_BOOL_F);
  402. data[scm_struct_i_setter] = SCM_UNPACK (SCM_BOOL_F);
  403. }
  404. else
  405. data = scm_alloc_struct (basic_size + tail_elts,
  406. scm_struct_n_extra_words,
  407. "make-struct");
  408. SCM_SET_CELL_WORD_1 (handle, data);
  409. SCM_SET_STRUCT_GC_CHAIN (handle, 0);
  410. scm_struct_init (handle, layout, data, tail_elts, init);
  411. SCM_SET_CELL_WORD_0 (handle, (scm_t_bits) SCM_STRUCT_DATA (vtable) + scm_tc3_cons_gloc);
  412. SCM_ALLOW_INTS;
  413. return handle;
  414. }
  415. #undef FUNC_NAME
  416. SCM_DEFINE (scm_make_vtable_vtable, "make-vtable-vtable", 2, 0, 1,
  417. (SCM user_fields, SCM tail_array_size, SCM init),
  418. "Return a new, self-describing vtable structure.\n\n"
  419. "@var{user-fields} is a string describing user defined fields of the\n"
  420. "vtable beginning at index @code{vtable-offset-user}\n"
  421. "(see @code{make-struct-layout}).\n\n"
  422. "@var{tail-size} specifies the size of the tail-array (if any) of\n"
  423. "this vtable.\n\n"
  424. "@var{init1}, @dots{} are the optional initializers for the fields of\n"
  425. "the vtable.\n\n"
  426. "Vtables have one initializable system field---the struct printer.\n"
  427. "This field comes before the user fields in the initializers passed\n"
  428. "to @code{make-vtable-vtable} and @code{make-struct}, and thus works as\n"
  429. "a third optional argument to @code{make-vtable-vtable} and a fourth to\n"
  430. "@code{make-struct} when creating vtables:\n\n"
  431. "If the value is a procedure, it will be called instead of the standard\n"
  432. "printer whenever a struct described by this vtable is printed.\n"
  433. "The procedure will be called with arguments STRUCT and PORT.\n\n"
  434. "The structure of a struct is described by a vtable, so the vtable is\n"
  435. "in essence the type of the struct. The vtable is itself a struct with\n"
  436. "a vtable. This could go on forever if it weren't for the\n"
  437. "vtable-vtables which are self-describing vtables, and thus terminate\n"
  438. "the chain.\n\n"
  439. "There are several potential ways of using structs, but the standard\n"
  440. "one is to use three kinds of structs, together building up a type\n"
  441. "sub-system: one vtable-vtable working as the root and one or several\n"
  442. "\"types\", each with a set of \"instances\". (The vtable-vtable should be\n"
  443. "compared to the class <class> which is the class of itself.)\n\n"
  444. "@lisp\n"
  445. "(define ball-root (make-vtable-vtable \"pr\" 0))\n\n"
  446. "(define (make-ball-type ball-color)\n"
  447. " (make-struct ball-root 0\n"
  448. " (make-struct-layout \"pw\")\n"
  449. " (lambda (ball port)\n"
  450. " (format port \"#<a ~A ball owned by ~A>\"\n"
  451. " (color ball)\n"
  452. " (owner ball)))\n"
  453. " ball-color))\n"
  454. "(define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))\n"
  455. "(define (owner ball) (struct-ref ball 0))\n\n"
  456. "(define red (make-ball-type 'red))\n"
  457. "(define green (make-ball-type 'green))\n\n"
  458. "(define (make-ball type owner) (make-struct type 0 owner))\n\n"
  459. "(define ball (make-ball green 'Nisse))\n"
  460. "ball @result{} #<a green ball owned by Nisse>\n"
  461. "@end lisp")
  462. #define FUNC_NAME s_scm_make_vtable_vtable
  463. {
  464. SCM fields;
  465. SCM layout;
  466. int basic_size;
  467. int tail_elts;
  468. scm_t_bits * data;
  469. SCM handle;
  470. SCM_VALIDATE_STRING (1, user_fields);
  471. SCM_VALIDATE_INUM (2, tail_array_size);
  472. SCM_VALIDATE_REST_ARGUMENT (init);
  473. fields = scm_string_append (scm_list_2 (required_vtable_fields,
  474. user_fields));
  475. layout = scm_make_struct_layout (fields);
  476. basic_size = SCM_SYMBOL_LENGTH (layout) / 2;
  477. tail_elts = SCM_INUM (tail_array_size);
  478. SCM_NEWCELL2 (handle);
  479. SCM_DEFER_INTS;
  480. data = scm_alloc_struct (basic_size + tail_elts,
  481. scm_struct_n_extra_words,
  482. "make-vtable-vtable");
  483. SCM_SET_CELL_WORD_1 (handle, data);
  484. SCM_SET_STRUCT_GC_CHAIN (handle, 0);
  485. data [scm_vtable_index_layout] = SCM_UNPACK (layout);
  486. scm_struct_init (handle, layout, data, tail_elts, scm_cons (layout, init));
  487. SCM_SET_CELL_WORD_0 (handle, (scm_t_bits) data + scm_tc3_cons_gloc);
  488. SCM_ALLOW_INTS;
  489. return handle;
  490. }
  491. #undef FUNC_NAME
  492. SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0,
  493. (SCM handle, SCM pos),
  494. "@deffnx {Scheme Procedure} struct-set! struct n value\n"
  495. "Access (or modify) the @var{n}th field of @var{struct}.\n\n"
  496. "If the field is of type 'p', then it can be set to an arbitrary value.\n\n"
  497. "If the field is of type 'u', then it can only be set to a non-negative\n"
  498. "integer value small enough to fit in one machine word.")
  499. #define FUNC_NAME s_scm_struct_ref
  500. {
  501. SCM answer = SCM_UNDEFINED;
  502. scm_t_bits * data;
  503. SCM layout;
  504. int p;
  505. scm_t_bits n_fields;
  506. char * fields_desc;
  507. char field_type = 0;
  508. SCM_VALIDATE_STRUCT (1,handle);
  509. SCM_VALIDATE_INUM (2,pos);
  510. layout = SCM_STRUCT_LAYOUT (handle);
  511. data = SCM_STRUCT_DATA (handle);
  512. p = SCM_INUM (pos);
  513. fields_desc = SCM_SYMBOL_CHARS (layout);
  514. n_fields = data[scm_struct_i_n_words];
  515. SCM_ASSERT_RANGE(1,pos, p < n_fields);
  516. if (p * 2 < SCM_SYMBOL_LENGTH (layout))
  517. {
  518. char ref;
  519. field_type = fields_desc[p * 2];
  520. ref = fields_desc[p * 2 + 1];
  521. if ((ref != 'r') && (ref != 'w'))
  522. {
  523. if ((ref == 'R') || (ref == 'W'))
  524. field_type = 'u';
  525. else
  526. SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
  527. }
  528. }
  529. else if (fields_desc[SCM_SYMBOL_LENGTH (layout) - 1] != 'O')
  530. field_type = fields_desc[SCM_SYMBOL_LENGTH (layout) - 2];
  531. else
  532. SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
  533. switch (field_type)
  534. {
  535. case 'u':
  536. answer = scm_ulong2num (data[p]);
  537. break;
  538. #if 0
  539. case 'i':
  540. answer = scm_long2num (data[p]);
  541. break;
  542. case 'd':
  543. answer = scm_make_real (*((double *)&(data[p])));
  544. break;
  545. #endif
  546. case 's':
  547. case 'p':
  548. answer = SCM_PACK (data[p]);
  549. break;
  550. default:
  551. SCM_MISC_ERROR ("unrecognized field type: ~S",
  552. scm_list_1 (SCM_MAKE_CHAR (field_type)));
  553. }
  554. return answer;
  555. }
  556. #undef FUNC_NAME
  557. SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0,
  558. (SCM handle, SCM pos, SCM val),
  559. "Set the slot of the structure @var{handle} with index @var{pos}\n"
  560. "to @var{val}. Signal an error if the slot can not be written\n"
  561. "to.")
  562. #define FUNC_NAME s_scm_struct_set_x
  563. {
  564. scm_t_bits * data;
  565. SCM layout;
  566. int p;
  567. int n_fields;
  568. char * fields_desc;
  569. char field_type = 0;
  570. SCM_VALIDATE_STRUCT (1,handle);
  571. SCM_VALIDATE_INUM (2,pos);
  572. layout = SCM_STRUCT_LAYOUT (handle);
  573. data = SCM_STRUCT_DATA (handle);
  574. p = SCM_INUM (pos);
  575. fields_desc = SCM_SYMBOL_CHARS (layout);
  576. n_fields = data[scm_struct_i_n_words];
  577. SCM_ASSERT_RANGE (1,pos, p < n_fields);
  578. if (p * 2 < SCM_SYMBOL_LENGTH (layout))
  579. {
  580. char set_x;
  581. field_type = fields_desc[p * 2];
  582. set_x = fields_desc [p * 2 + 1];
  583. if (set_x != 'w')
  584. SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
  585. }
  586. else if (fields_desc[SCM_SYMBOL_LENGTH (layout) - 1] == 'W')
  587. field_type = fields_desc[SCM_SYMBOL_LENGTH (layout) - 2];
  588. else
  589. SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
  590. switch (field_type)
  591. {
  592. case 'u':
  593. data[p] = SCM_NUM2ULONG (3, val);
  594. break;
  595. #if 0
  596. case 'i':
  597. data[p] = SCM_NUM2LONG (3, val);
  598. break;
  599. case 'd':
  600. *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3);
  601. break;
  602. #endif
  603. case 'p':
  604. data[p] = SCM_UNPACK (val);
  605. break;
  606. case 's':
  607. SCM_MISC_ERROR ("self fields immutable", SCM_EOL);
  608. default:
  609. SCM_MISC_ERROR ("unrecognized field type: ~S",
  610. scm_list_1 (SCM_MAKE_CHAR (field_type)));
  611. }
  612. return val;
  613. }
  614. #undef FUNC_NAME
  615. SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0,
  616. (SCM handle),
  617. "Return the vtable structure that describes the type of @var{struct}.")
  618. #define FUNC_NAME s_scm_struct_vtable
  619. {
  620. SCM_VALIDATE_STRUCT (1,handle);
  621. return SCM_STRUCT_VTABLE (handle);
  622. }
  623. #undef FUNC_NAME
  624. SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0,
  625. (SCM handle),
  626. "Return the vtable tag of the structure @var{handle}.")
  627. #define FUNC_NAME s_scm_struct_vtable_tag
  628. {
  629. SCM_VALIDATE_VTABLE (1,handle);
  630. return scm_long2num ((long) SCM_STRUCT_DATA (handle) >> 3);
  631. }
  632. #undef FUNC_NAME
  633. /* {Associating names and classes with vtables}
  634. *
  635. * The name of a vtable should probably be stored as a slot. This is
  636. * a backward compatible solution until agreement has been achieved on
  637. * how to associate names with vtables.
  638. */
  639. unsigned long
  640. scm_struct_ihashq (SCM obj, unsigned long n)
  641. {
  642. /* The length of the hash table should be a relative prime it's not
  643. necessary to shift down the address. */
  644. return SCM_UNPACK (obj) % n;
  645. }
  646. SCM
  647. scm_struct_create_handle (SCM obj)
  648. {
  649. SCM handle = scm_hash_fn_create_handle_x (scm_struct_table,
  650. obj,
  651. SCM_BOOL_F,
  652. scm_struct_ihashq,
  653. scm_sloppy_assq,
  654. 0);
  655. if (SCM_FALSEP (SCM_CDR (handle)))
  656. SCM_SETCDR (handle, scm_cons (SCM_BOOL_F, SCM_BOOL_F));
  657. return handle;
  658. }
  659. SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0,
  660. (SCM vtable),
  661. "Return the name of the vtable @var{vtable}.")
  662. #define FUNC_NAME s_scm_struct_vtable_name
  663. {
  664. SCM_VALIDATE_VTABLE (1,vtable);
  665. return SCM_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)));
  666. }
  667. #undef FUNC_NAME
  668. SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0,
  669. (SCM vtable, SCM name),
  670. "Set the name of the vtable @var{vtable} to @var{name}.")
  671. #define FUNC_NAME s_scm_set_struct_vtable_name_x
  672. {
  673. SCM_VALIDATE_VTABLE (1,vtable);
  674. SCM_VALIDATE_SYMBOL (2,name);
  675. SCM_SET_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)),
  676. name);
  677. return SCM_UNSPECIFIED;
  678. }
  679. #undef FUNC_NAME
  680. void
  681. scm_print_struct (SCM exp, SCM port, scm_print_state *pstate)
  682. {
  683. if (SCM_NFALSEP (scm_procedure_p (SCM_STRUCT_PRINTER (exp))))
  684. scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate);
  685. else
  686. {
  687. SCM vtable = SCM_STRUCT_VTABLE (exp);
  688. SCM name = scm_struct_vtable_name (vtable);
  689. scm_puts ("#<", port);
  690. if (SCM_NFALSEP (name))
  691. scm_display (name, port);
  692. else
  693. scm_puts ("struct", port);
  694. scm_putc (' ', port);
  695. scm_intprint (SCM_UNPACK (vtable), 16, port);
  696. scm_putc (':', port);
  697. scm_intprint (SCM_UNPACK (exp), 16, port);
  698. scm_putc ('>', port);
  699. }
  700. }
  701. void
  702. scm_struct_prehistory ()
  703. {
  704. scm_c_hook_add (&scm_before_mark_c_hook, scm_struct_gc_init, 0, 0);
  705. scm_c_hook_add (&scm_after_sweep_c_hook, scm_free_structs, 0, 0);
  706. }
  707. void
  708. scm_init_struct ()
  709. {
  710. scm_struct_table
  711. = scm_permanent_object (scm_make_weak_key_hash_table (SCM_MAKINUM (31)));
  712. required_vtable_fields = scm_makfrom0str ("pruosrpw");
  713. scm_permanent_object (required_vtable_fields);
  714. scm_c_define ("vtable-index-layout", SCM_MAKINUM (scm_vtable_index_layout));
  715. scm_c_define ("vtable-index-vtable", SCM_MAKINUM (scm_vtable_index_vtable));
  716. scm_c_define ("vtable-index-printer",
  717. SCM_MAKINUM (scm_vtable_index_printer));
  718. scm_c_define ("vtable-offset-user", SCM_MAKINUM (scm_vtable_offset_user));
  719. #include "libguile/struct.x"
  720. }
  721. /*
  722. Local Variables:
  723. c-file-style: "gnu"
  724. End:
  725. */