goops.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /* Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2008,2009,2010,2011,2012,2013,2014,2015
  2. * 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. /* This software is a derivative work of other copyrighted softwares; the
  20. * copyright notices of these softwares are placed in the file COPYRIGHTS
  21. *
  22. * This file is based upon stklos.c from the STk distribution by
  23. * Erick Gallesio <eg@unice.fr>.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. # include <config.h>
  27. #endif
  28. #include "libguile/_scm.h"
  29. #include "libguile/async.h"
  30. #include "libguile/chars.h"
  31. #include "libguile/dynwind.h"
  32. #include "libguile/eval.h"
  33. #include "libguile/gsubr.h"
  34. #include "libguile/hashtab.h"
  35. #include "libguile/keywords.h"
  36. #include "libguile/macros.h"
  37. #include "libguile/modules.h"
  38. #include "libguile/ports.h"
  39. #include "libguile/procprop.h"
  40. #include "libguile/programs.h"
  41. #include "libguile/smob.h"
  42. #include "libguile/strings.h"
  43. #include "libguile/strports.h"
  44. #include "libguile/vectors.h"
  45. #include "libguile/validate.h"
  46. #include "libguile/goops.h"
  47. /* Port classes */
  48. #define SCM_IN_PCLASS_INDEX 0
  49. #define SCM_OUT_PCLASS_INDEX SCM_I_MAX_PORT_TYPE_COUNT
  50. #define SCM_INOUT_PCLASS_INDEX (2 * SCM_I_MAX_PORT_TYPE_COUNT)
  51. /* Objects have identity, so references to classes and instances are by
  52. value, not by reference. Redefinition of a class or modification of
  53. an instance causes in-place update; you can think of GOOPS as
  54. building in its own indirection, and for that reason referring to
  55. GOOPS values by variable reference is unnecessary.
  56. References to ordinary procedures is by reference (by variable),
  57. though, as in the rest of Guile. */
  58. SCM_KEYWORD (k_name, "name");
  59. SCM_KEYWORD (k_setter, "setter");
  60. SCM_SYMBOL (sym_redefined, "redefined");
  61. SCM_GLOBAL_SYMBOL (scm_sym_args, "args");
  62. static int goops_loaded_p = 0;
  63. static SCM var_make_standard_class = SCM_BOOL_F;
  64. static SCM var_change_class = SCM_BOOL_F;
  65. static SCM var_make = SCM_BOOL_F;
  66. static SCM var_inherit_applicable = SCM_BOOL_F;
  67. static SCM var_class_name = SCM_BOOL_F;
  68. static SCM var_class_direct_supers = SCM_BOOL_F;
  69. static SCM var_class_direct_slots = SCM_BOOL_F;
  70. static SCM var_class_direct_subclasses = SCM_BOOL_F;
  71. static SCM var_class_direct_methods = SCM_BOOL_F;
  72. static SCM var_class_precedence_list = SCM_BOOL_F;
  73. static SCM var_class_slots = SCM_BOOL_F;
  74. static SCM var_generic_function_methods = SCM_BOOL_F;
  75. static SCM var_method_generic_function = SCM_BOOL_F;
  76. static SCM var_method_specializers = SCM_BOOL_F;
  77. static SCM var_method_procedure = SCM_BOOL_F;
  78. static SCM var_slot_ref = SCM_BOOL_F;
  79. static SCM var_slot_set_x = SCM_BOOL_F;
  80. static SCM var_slot_bound_p = SCM_BOOL_F;
  81. static SCM var_slot_exists_p = SCM_BOOL_F;
  82. /* These variables are filled in by the object system when loaded. */
  83. static SCM class_boolean, class_char, class_pair;
  84. static SCM class_procedure, class_string, class_symbol;
  85. static SCM class_primitive_generic;
  86. static SCM class_vector, class_null;
  87. static SCM class_integer, class_real, class_complex, class_fraction;
  88. static SCM class_unknown;
  89. static SCM class_top, class_object, class_class;
  90. static SCM class_applicable;
  91. static SCM class_applicable_struct, class_applicable_struct_with_setter;
  92. static SCM class_generic, class_generic_with_setter;
  93. static SCM class_accessor;
  94. static SCM class_extended_generic, class_extended_generic_with_setter;
  95. static SCM class_extended_accessor;
  96. static SCM class_method;
  97. static SCM class_accessor_method;
  98. static SCM class_procedure_class;
  99. static SCM class_applicable_struct_class;
  100. static SCM class_applicable_struct_with_setter_class;
  101. static SCM class_number, class_list;
  102. static SCM class_keyword;
  103. static SCM class_port, class_input_output_port;
  104. static SCM class_input_port, class_output_port;
  105. static SCM class_foreign_slot;
  106. static SCM class_self, class_protected;
  107. static SCM class_hidden, class_opaque, class_read_only;
  108. static SCM class_protected_hidden, class_protected_opaque, class_protected_read_only;
  109. static SCM class_scm;
  110. static SCM class_int, class_float, class_double;
  111. static SCM class_foreign;
  112. static SCM class_hashtable;
  113. static SCM class_fluid;
  114. static SCM class_dynamic_state;
  115. static SCM class_frame;
  116. static SCM class_keyword;
  117. static SCM class_vm_cont;
  118. static SCM class_bytevector;
  119. static SCM class_uvec;
  120. static SCM class_array;
  121. static SCM class_bitvector;
  122. static SCM vtable_class_map = SCM_BOOL_F;
  123. /* Port classes. Allocate 3 times the maximum number of port types so that
  124. input ports, output ports, and in/out ports can be stored at different
  125. offsets. See `SCM_IN_PCLASS_INDEX' et al. */
  126. SCM scm_i_port_class[3 * SCM_I_MAX_PORT_TYPE_COUNT];
  127. /* SMOB classes. */
  128. SCM scm_i_smob_class[SCM_I_MAX_SMOB_TYPE_COUNT];
  129. SCM scm_module_goops;
  130. static SCM scm_sys_make_vtable_vtable (SCM layout);
  131. static SCM scm_sys_init_layout_x (SCM class, SCM layout);
  132. static SCM scm_sys_clear_fields_x (SCM obj, SCM unbound);
  133. static SCM scm_sys_goops_early_init (void);
  134. static SCM scm_sys_goops_loaded (void);
  135. SCM_DEFINE (scm_sys_make_vtable_vtable, "%make-vtable-vtable", 1, 0, 0,
  136. (SCM layout),
  137. "")
  138. #define FUNC_NAME s_scm_sys_make_vtable_vtable
  139. {
  140. return scm_i_make_vtable_vtable (layout);
  141. }
  142. #undef FUNC_NAME
  143. SCM
  144. scm_make_standard_class (SCM meta, SCM name, SCM dsupers, SCM dslots)
  145. {
  146. return scm_call_4 (scm_variable_ref (var_make_standard_class),
  147. meta, name, dsupers, dslots);
  148. }
  149. SCM_DEFINE (scm_sys_init_layout_x, "%init-layout!", 2, 0, 0,
  150. (SCM class, SCM layout),
  151. "")
  152. #define FUNC_NAME s_scm_sys_init_layout_x
  153. {
  154. SCM_VALIDATE_INSTANCE (1, class);
  155. SCM_ASSERT (!scm_is_symbol (SCM_VTABLE_LAYOUT (class)), class, 1, FUNC_NAME);
  156. SCM_VALIDATE_STRING (2, layout);
  157. SCM_SET_VTABLE_LAYOUT (class, scm_make_struct_layout (layout));
  158. scm_i_struct_inherit_vtable_magic (SCM_CLASS_OF (class), class);
  159. SCM_SET_CLASS_FLAGS (class, SCM_CLASSF_GOOPS_OR_VALID);
  160. return SCM_UNSPECIFIED;
  161. }
  162. #undef FUNC_NAME
  163. /* This function is used for efficient type dispatch. */
  164. SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
  165. (SCM x),
  166. "Return the class of @var{x}.")
  167. #define FUNC_NAME s_scm_class_of
  168. {
  169. switch (SCM_ITAG3 (x))
  170. {
  171. case scm_tc3_int_1:
  172. case scm_tc3_int_2:
  173. return class_integer;
  174. case scm_tc3_imm24:
  175. if (SCM_CHARP (x))
  176. return class_char;
  177. else if (scm_is_bool (x))
  178. return class_boolean;
  179. else if (scm_is_null (x))
  180. return class_null;
  181. else
  182. return class_unknown;
  183. case scm_tc3_cons:
  184. switch (SCM_TYP7 (x))
  185. {
  186. case scm_tcs_cons_nimcar:
  187. return class_pair;
  188. case scm_tc7_symbol:
  189. return class_symbol;
  190. case scm_tc7_vector:
  191. case scm_tc7_wvect:
  192. return class_vector;
  193. case scm_tc7_pointer:
  194. return class_foreign;
  195. case scm_tc7_hashtable:
  196. return class_hashtable;
  197. case scm_tc7_fluid:
  198. return class_fluid;
  199. case scm_tc7_dynamic_state:
  200. return class_dynamic_state;
  201. case scm_tc7_frame:
  202. return class_frame;
  203. case scm_tc7_keyword:
  204. return class_keyword;
  205. case scm_tc7_vm_cont:
  206. return class_vm_cont;
  207. case scm_tc7_bytevector:
  208. if (SCM_BYTEVECTOR_ELEMENT_TYPE (x) == SCM_ARRAY_ELEMENT_TYPE_VU8)
  209. return class_bytevector;
  210. else
  211. return class_uvec;
  212. case scm_tc7_array:
  213. return class_array;
  214. case scm_tc7_bitvector:
  215. return class_bitvector;
  216. case scm_tc7_string:
  217. return class_string;
  218. case scm_tc7_number:
  219. switch SCM_TYP16 (x) {
  220. case scm_tc16_big:
  221. return class_integer;
  222. case scm_tc16_real:
  223. return class_real;
  224. case scm_tc16_complex:
  225. return class_complex;
  226. case scm_tc16_fraction:
  227. return class_fraction;
  228. }
  229. case scm_tc7_program:
  230. if (SCM_PROGRAM_IS_PRIMITIVE_GENERIC (x)
  231. && SCM_UNPACK (*SCM_SUBR_GENERIC (x)))
  232. return class_primitive_generic;
  233. else
  234. return class_procedure;
  235. case scm_tc7_smob:
  236. {
  237. scm_t_bits type = SCM_TYP16 (x);
  238. if (type != scm_tc16_port_with_ps)
  239. return scm_i_smob_class[SCM_TC2SMOBNUM (type)];
  240. x = SCM_PORT_WITH_PS_PORT (x);
  241. /* fall through to ports */
  242. }
  243. case scm_tc7_port:
  244. return scm_i_port_class[(SCM_WRTNG & SCM_CELL_WORD_0 (x)
  245. ? (SCM_RDNG & SCM_CELL_WORD_0 (x)
  246. ? SCM_INOUT_PCLASS_INDEX | SCM_PTOBNUM (x)
  247. : SCM_OUT_PCLASS_INDEX | SCM_PTOBNUM (x))
  248. : SCM_IN_PCLASS_INDEX | SCM_PTOBNUM (x))];
  249. case scm_tcs_struct:
  250. if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS_VALID)
  251. /* A GOOPS object with a valid class. */
  252. return SCM_CLASS_OF (x);
  253. else if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS)
  254. /* A GOOPS object whose class might have been redefined. */
  255. {
  256. SCM class = SCM_CLASS_OF (x);
  257. SCM new_class = scm_slot_ref (class, sym_redefined);
  258. if (!scm_is_false (new_class))
  259. scm_change_object_class (x, class, new_class);
  260. /* Re-load class from instance. */
  261. return SCM_CLASS_OF (x);
  262. }
  263. else
  264. return scm_i_define_class_for_vtable (SCM_CLASS_OF (x));
  265. default:
  266. if (scm_is_pair (x))
  267. return class_pair;
  268. else
  269. return class_unknown;
  270. }
  271. case scm_tc3_struct:
  272. case scm_tc3_tc7_1:
  273. case scm_tc3_tc7_2:
  274. /* case scm_tc3_unused: */
  275. /* Never reached */
  276. break;
  277. }
  278. return class_unknown;
  279. }
  280. #undef FUNC_NAME
  281. SCM_DEFINE (scm_instance_p, "instance?", 1, 0, 0,
  282. (SCM obj),
  283. "Return @code{#t} if @var{obj} is an instance.")
  284. #define FUNC_NAME s_scm_instance_p
  285. {
  286. return scm_from_bool (SCM_INSTANCEP (obj));
  287. }
  288. #undef FUNC_NAME
  289. int
  290. scm_is_generic (SCM x)
  291. {
  292. return SCM_INSTANCEP (x) && SCM_SUBCLASSP (SCM_CLASS_OF (x), class_generic);
  293. }
  294. int
  295. scm_is_method (SCM x)
  296. {
  297. return SCM_INSTANCEP (x) && SCM_SUBCLASSP (SCM_CLASS_OF (x), class_method);
  298. }
  299. SCM
  300. scm_class_name (SCM obj)
  301. {
  302. return scm_call_1 (scm_variable_ref (var_class_name), obj);
  303. }
  304. SCM
  305. scm_class_direct_supers (SCM obj)
  306. {
  307. return scm_call_1 (scm_variable_ref (var_class_direct_supers), obj);
  308. }
  309. SCM
  310. scm_class_direct_slots (SCM obj)
  311. {
  312. return scm_call_1 (scm_variable_ref (var_class_direct_slots), obj);
  313. }
  314. SCM
  315. scm_class_direct_subclasses (SCM obj)
  316. {
  317. return scm_call_1 (scm_variable_ref (var_class_direct_subclasses), obj);
  318. }
  319. SCM
  320. scm_class_direct_methods (SCM obj)
  321. {
  322. return scm_call_1 (scm_variable_ref (var_class_direct_methods), obj);
  323. }
  324. SCM
  325. scm_class_precedence_list (SCM obj)
  326. {
  327. return scm_call_1 (scm_variable_ref (var_class_precedence_list), obj);
  328. }
  329. SCM
  330. scm_class_slots (SCM obj)
  331. {
  332. return scm_call_1 (scm_variable_ref (var_class_slots), obj);
  333. }
  334. SCM_DEFINE (scm_generic_function_name, "generic-function-name", 1, 0, 0,
  335. (SCM obj),
  336. "Return the name of the generic function @var{obj}.")
  337. #define FUNC_NAME s_scm_generic_function_name
  338. {
  339. SCM_VALIDATE_GENERIC (1, obj);
  340. return scm_procedure_property (obj, scm_sym_name);
  341. }
  342. #undef FUNC_NAME
  343. SCM
  344. scm_generic_function_methods (SCM obj)
  345. {
  346. return scm_call_1 (scm_variable_ref (var_generic_function_methods), obj);
  347. }
  348. SCM
  349. scm_method_generic_function (SCM obj)
  350. {
  351. return scm_call_1 (scm_variable_ref (var_method_generic_function), obj);
  352. }
  353. SCM
  354. scm_method_specializers (SCM obj)
  355. {
  356. return scm_call_1 (scm_variable_ref (var_method_specializers), obj);
  357. }
  358. SCM
  359. scm_method_procedure (SCM obj)
  360. {
  361. return scm_call_1 (scm_variable_ref (var_method_procedure), obj);
  362. }
  363. SCM
  364. scm_slot_ref (SCM obj, SCM slot_name)
  365. {
  366. return scm_call_2 (scm_variable_ref (var_slot_ref), obj, slot_name);
  367. }
  368. SCM
  369. scm_slot_set_x (SCM obj, SCM slot_name, SCM value)
  370. {
  371. return scm_call_3 (scm_variable_ref (var_slot_set_x), obj, slot_name, value);
  372. }
  373. SCM
  374. scm_slot_bound_p (SCM obj, SCM slot_name)
  375. {
  376. return scm_call_2 (scm_variable_ref (var_slot_bound_p), obj, slot_name);
  377. }
  378. SCM
  379. scm_slot_exists_p (SCM obj, SCM slot_name)
  380. {
  381. return scm_call_2 (scm_variable_ref (var_slot_exists_p), obj, slot_name);
  382. }
  383. SCM_DEFINE (scm_sys_clear_fields_x, "%clear-fields!", 2, 0, 0,
  384. (SCM obj, SCM unbound),
  385. "")
  386. #define FUNC_NAME s_scm_sys_clear_fields_x
  387. {
  388. scm_t_signed_bits n, i;
  389. SCM vtable, layout;
  390. SCM_VALIDATE_STRUCT (1, obj);
  391. vtable = SCM_STRUCT_VTABLE (obj);
  392. n = SCM_STRUCT_DATA_REF (vtable, scm_vtable_index_size);
  393. layout = SCM_VTABLE_LAYOUT (vtable);
  394. /* Set all SCM-holding slots to the GOOPS unbound value. */
  395. for (i = 0; i < n; i++)
  396. if (scm_i_symbol_ref (layout, i*2) == 'p')
  397. SCM_STRUCT_SLOT_SET (obj, i, unbound);
  398. return SCM_UNSPECIFIED;
  399. }
  400. #undef FUNC_NAME
  401. SCM_DEFINE (scm_sys_modify_instance, "%modify-instance", 2, 0, 0,
  402. (SCM old, SCM new),
  403. "Used by change-class to modify objects in place.")
  404. #define FUNC_NAME s_scm_sys_modify_instance
  405. {
  406. SCM_VALIDATE_INSTANCE (1, old);
  407. SCM_VALIDATE_INSTANCE (2, new);
  408. /* Exchange the data contained in old and new. We exchange rather than
  409. * scratch the old value with new to be correct with GC.
  410. * See "Class redefinition protocol above".
  411. */
  412. SCM_CRITICAL_SECTION_START;
  413. {
  414. scm_t_bits word0, word1;
  415. word0 = SCM_CELL_WORD_0 (old);
  416. word1 = SCM_CELL_WORD_1 (old);
  417. SCM_SET_CELL_WORD_0 (old, SCM_CELL_WORD_0 (new));
  418. SCM_SET_CELL_WORD_1 (old, SCM_CELL_WORD_1 (new));
  419. SCM_SET_CELL_WORD_0 (new, word0);
  420. SCM_SET_CELL_WORD_1 (new, word1);
  421. }
  422. SCM_CRITICAL_SECTION_END;
  423. return SCM_UNSPECIFIED;
  424. }
  425. #undef FUNC_NAME
  426. SCM_DEFINE (scm_sys_modify_class, "%modify-class", 2, 0, 0,
  427. (SCM old, SCM new),
  428. "")
  429. #define FUNC_NAME s_scm_sys_modify_class
  430. {
  431. SCM_VALIDATE_CLASS (1, old);
  432. SCM_VALIDATE_CLASS (2, new);
  433. SCM_CRITICAL_SECTION_START;
  434. {
  435. scm_t_bits word0, word1;
  436. word0 = SCM_CELL_WORD_0 (old);
  437. word1 = SCM_CELL_WORD_1 (old);
  438. SCM_SET_CELL_WORD_0 (old, SCM_CELL_WORD_0 (new));
  439. SCM_SET_CELL_WORD_1 (old, SCM_CELL_WORD_1 (new));
  440. SCM_STRUCT_DATA (old)[scm_vtable_index_self] = SCM_UNPACK (old);
  441. SCM_SET_CELL_WORD_0 (new, word0);
  442. SCM_SET_CELL_WORD_1 (new, word1);
  443. SCM_STRUCT_DATA (new)[scm_vtable_index_self] = SCM_UNPACK (new);
  444. }
  445. SCM_CRITICAL_SECTION_END;
  446. return SCM_UNSPECIFIED;
  447. }
  448. #undef FUNC_NAME
  449. /* When instances change class, they finally get a new body, but
  450. * before that, they go through purgatory in hell. Odd as it may
  451. * seem, this data structure saves us from eternal suffering in
  452. * infinite recursions.
  453. */
  454. static scm_t_bits **hell;
  455. static long n_hell = 1; /* one place for the evil one himself */
  456. static long hell_size = 4;
  457. static SCM hell_mutex;
  458. static long
  459. burnin (SCM o)
  460. {
  461. long i;
  462. for (i = 1; i < n_hell; ++i)
  463. if (SCM_STRUCT_DATA (o) == hell[i])
  464. return i;
  465. return 0;
  466. }
  467. static void
  468. go_to_hell (void *o)
  469. {
  470. SCM obj = *(SCM*)o;
  471. scm_lock_mutex (hell_mutex);
  472. if (n_hell >= hell_size)
  473. {
  474. hell_size *= 2;
  475. hell = scm_realloc (hell, hell_size * sizeof(*hell));
  476. }
  477. hell[n_hell++] = SCM_STRUCT_DATA (obj);
  478. scm_unlock_mutex (hell_mutex);
  479. }
  480. static void
  481. go_to_heaven (void *o)
  482. {
  483. SCM obj = *(SCM*)o;
  484. scm_lock_mutex (hell_mutex);
  485. hell[burnin (obj)] = hell[--n_hell];
  486. scm_unlock_mutex (hell_mutex);
  487. }
  488. static SCM
  489. purgatory (SCM obj, SCM new_class)
  490. {
  491. return scm_call_2 (SCM_VARIABLE_REF (var_change_class), obj, new_class);
  492. }
  493. /* This function calls the generic function change-class for all
  494. * instances which aren't currently undergoing class change.
  495. */
  496. void
  497. scm_change_object_class (SCM obj, SCM old_class SCM_UNUSED, SCM new_class)
  498. {
  499. if (!burnin (obj))
  500. {
  501. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  502. scm_dynwind_rewind_handler (go_to_hell, &obj, SCM_F_WIND_EXPLICITLY);
  503. scm_dynwind_unwind_handler (go_to_heaven, &obj, SCM_F_WIND_EXPLICITLY);
  504. purgatory (obj, new_class);
  505. scm_dynwind_end ();
  506. }
  507. }
  508. /* Primitive generics: primitives that can dispatch to generics if their
  509. arguments fail to apply. */
  510. SCM_DEFINE (scm_generic_capability_p, "generic-capability?", 1, 0, 0,
  511. (SCM proc),
  512. "")
  513. #define FUNC_NAME s_scm_generic_capability_p
  514. {
  515. SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
  516. proc, SCM_ARG1, FUNC_NAME);
  517. return (SCM_PRIMITIVE_GENERIC_P (proc) ? SCM_BOOL_T : SCM_BOOL_F);
  518. }
  519. #undef FUNC_NAME
  520. SCM_DEFINE (scm_enable_primitive_generic_x, "enable-primitive-generic!", 0, 0, 1,
  521. (SCM subrs),
  522. "")
  523. #define FUNC_NAME s_scm_enable_primitive_generic_x
  524. {
  525. SCM_VALIDATE_REST_ARGUMENT (subrs);
  526. while (!scm_is_null (subrs))
  527. {
  528. SCM subr = SCM_CAR (subrs);
  529. SCM_ASSERT (SCM_PRIMITIVE_GENERIC_P (subr), subr, SCM_ARGn, FUNC_NAME);
  530. SCM_SET_SUBR_GENERIC (subr,
  531. scm_make (scm_list_3 (class_generic,
  532. k_name,
  533. SCM_SUBR_NAME (subr))));
  534. subrs = SCM_CDR (subrs);
  535. }
  536. return SCM_UNSPECIFIED;
  537. }
  538. #undef FUNC_NAME
  539. SCM_DEFINE (scm_set_primitive_generic_x, "set-primitive-generic!", 2, 0, 0,
  540. (SCM subr, SCM generic),
  541. "")
  542. #define FUNC_NAME s_scm_set_primitive_generic_x
  543. {
  544. SCM_ASSERT (SCM_PRIMITIVE_GENERIC_P (subr), subr, SCM_ARG1, FUNC_NAME);
  545. SCM_ASSERT (SCM_GENERICP (generic), generic, SCM_ARG2, FUNC_NAME);
  546. SCM_SET_SUBR_GENERIC (subr, generic);
  547. return SCM_UNSPECIFIED;
  548. }
  549. #undef FUNC_NAME
  550. SCM_DEFINE (scm_primitive_generic_generic, "primitive-generic-generic", 1, 0, 0,
  551. (SCM subr),
  552. "")
  553. #define FUNC_NAME s_scm_primitive_generic_generic
  554. {
  555. if (SCM_PRIMITIVE_GENERIC_P (subr))
  556. {
  557. if (!SCM_UNPACK (*SCM_SUBR_GENERIC (subr)))
  558. scm_enable_primitive_generic_x (scm_list_1 (subr));
  559. return *SCM_SUBR_GENERIC (subr);
  560. }
  561. SCM_WRONG_TYPE_ARG (SCM_ARG1, subr);
  562. }
  563. #undef FUNC_NAME
  564. SCM
  565. scm_wta_dispatch_0 (SCM gf, const char *subr)
  566. {
  567. if (!SCM_UNPACK (gf))
  568. scm_error_num_args_subr (subr);
  569. return scm_call_0 (gf);
  570. }
  571. SCM
  572. scm_wta_dispatch_1 (SCM gf, SCM a1, int pos, const char *subr)
  573. {
  574. if (!SCM_UNPACK (gf))
  575. scm_wrong_type_arg (subr, pos, a1);
  576. return scm_call_1 (gf, a1);
  577. }
  578. SCM
  579. scm_wta_dispatch_2 (SCM gf, SCM a1, SCM a2, int pos, const char *subr)
  580. {
  581. if (!SCM_UNPACK (gf))
  582. scm_wrong_type_arg (subr, pos, (pos == SCM_ARG1) ? a1 : a2);
  583. return scm_call_2 (gf, a1, a2);
  584. }
  585. SCM
  586. scm_wta_dispatch_n (SCM gf, SCM args, int pos, const char *subr)
  587. {
  588. if (!SCM_UNPACK (gf))
  589. scm_wrong_type_arg (subr, pos, scm_list_ref (args, scm_from_int (pos)));
  590. return scm_apply_0 (gf, args);
  591. }
  592. SCM
  593. scm_make (SCM args)
  594. {
  595. return scm_apply_0 (scm_variable_ref (var_make), args);
  596. }
  597. /* SMOB, struct, and port classes. */
  598. static SCM
  599. make_class_name (const char *prefix, const char *type_name, const char *suffix)
  600. {
  601. if (!type_name)
  602. type_name = "";
  603. return scm_string_to_symbol (scm_string_append
  604. (scm_list_3 (scm_from_utf8_string (prefix),
  605. scm_from_utf8_string (type_name),
  606. scm_from_utf8_string (suffix))));
  607. }
  608. SCM
  609. scm_make_extended_class (char const *type_name, int applicablep)
  610. {
  611. SCM name, meta, supers;
  612. name = make_class_name ("<", type_name, ">");
  613. meta = class_class;
  614. if (applicablep)
  615. supers = scm_list_1 (class_applicable);
  616. else
  617. supers = scm_list_1 (class_top);
  618. return scm_make_standard_class (meta, name, supers, SCM_EOL);
  619. }
  620. void
  621. scm_i_inherit_applicable (SCM c)
  622. {
  623. scm_call_1 (scm_variable_ref (var_inherit_applicable), c);
  624. }
  625. static void
  626. create_smob_classes (void)
  627. {
  628. long i;
  629. for (i = 0; i < SCM_I_MAX_SMOB_TYPE_COUNT; ++i)
  630. scm_i_smob_class[i] = SCM_BOOL_F;
  631. for (i = 0; i < scm_numsmob; ++i)
  632. if (scm_is_false (scm_i_smob_class[i]))
  633. scm_i_smob_class[i] = scm_make_extended_class (SCM_SMOBNAME (i),
  634. scm_smobs[i].apply != 0);
  635. }
  636. void
  637. scm_make_port_classes (long ptobnum, char *type_name)
  638. {
  639. SCM name, meta, super, supers;
  640. meta = class_class;
  641. name = make_class_name ("<", type_name, "-port>");
  642. supers = scm_list_1 (class_port);
  643. super = scm_make_standard_class (meta, name, supers, SCM_EOL);
  644. name = make_class_name ("<", type_name, "-input-port>");
  645. supers = scm_list_2 (super, class_input_port);
  646. scm_i_port_class[SCM_IN_PCLASS_INDEX + ptobnum]
  647. = scm_make_standard_class (meta, name, supers, SCM_EOL);
  648. name = make_class_name ("<", type_name, "-output-port>");
  649. supers = scm_list_2 (super, class_output_port);
  650. scm_i_port_class[SCM_OUT_PCLASS_INDEX + ptobnum]
  651. = scm_make_standard_class (meta, name, supers, SCM_EOL);
  652. name = make_class_name ("<", type_name, "-input-output-port>");
  653. supers = scm_list_2 (super, class_input_output_port);
  654. scm_i_port_class[SCM_INOUT_PCLASS_INDEX + ptobnum]
  655. = scm_make_standard_class (meta, name, supers, SCM_EOL);
  656. }
  657. static void
  658. create_port_classes (void)
  659. {
  660. long i;
  661. for (i = scm_c_num_port_types () - 1; i >= 0; i--)
  662. scm_make_port_classes (i, SCM_PTOBNAME (i));
  663. }
  664. SCM
  665. scm_i_define_class_for_vtable (SCM vtable)
  666. {
  667. SCM class;
  668. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  669. if (scm_is_false (vtable_class_map))
  670. vtable_class_map = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  671. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  672. if (scm_is_false (scm_struct_vtable_p (vtable)))
  673. abort ();
  674. class = scm_weak_table_refq (vtable_class_map, vtable, SCM_BOOL_F);
  675. if (scm_is_false (class))
  676. {
  677. if (SCM_UNPACK (class_class))
  678. {
  679. SCM name, meta, supers;
  680. name = SCM_VTABLE_NAME (vtable);
  681. if (scm_is_symbol (name))
  682. name = scm_string_to_symbol
  683. (scm_string_append
  684. (scm_list_3 (scm_from_latin1_string ("<"),
  685. scm_symbol_to_string (name),
  686. scm_from_latin1_string (">"))));
  687. else
  688. name = scm_from_latin1_symbol ("<>");
  689. if (SCM_STRUCT_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SETTER))
  690. {
  691. meta = class_applicable_struct_with_setter_class;
  692. supers = scm_list_1 (class_applicable_struct_with_setter);
  693. }
  694. else if (SCM_STRUCT_VTABLE_FLAG_IS_SET (vtable,
  695. SCM_VTABLE_FLAG_APPLICABLE))
  696. {
  697. meta = class_applicable_struct_class;
  698. supers = scm_list_1 (class_applicable_struct);
  699. }
  700. else
  701. {
  702. meta = class_class;
  703. supers = scm_list_1 (class_top);
  704. }
  705. return scm_make_standard_class (meta, name, supers, SCM_EOL);
  706. }
  707. else
  708. /* `create_struct_classes' will fill this in later. */
  709. class = SCM_BOOL_F;
  710. /* Don't worry about races. This only happens when creating a
  711. vtable, which happens by definition in one thread. */
  712. scm_weak_table_putq_x (vtable_class_map, vtable, class);
  713. }
  714. return class;
  715. }
  716. static SCM
  717. make_struct_class (void *closure SCM_UNUSED,
  718. SCM vtable, SCM data, SCM prev SCM_UNUSED)
  719. {
  720. if (scm_is_false (data))
  721. scm_i_define_class_for_vtable (vtable);
  722. return SCM_UNSPECIFIED;
  723. }
  724. static void
  725. create_struct_classes (void)
  726. {
  727. /* FIXME: take the vtable_class_map while initializing goops? */
  728. scm_internal_hash_fold (make_struct_class, 0, SCM_BOOL_F,
  729. vtable_class_map);
  730. }
  731. void
  732. scm_load_goops ()
  733. {
  734. if (!goops_loaded_p)
  735. scm_c_resolve_module ("oop goops");
  736. }
  737. SCM
  738. scm_ensure_accessor (SCM name)
  739. {
  740. SCM var, gf;
  741. var = scm_module_variable (scm_current_module (), name);
  742. if (SCM_VARIABLEP (var) && !SCM_UNBNDP (SCM_VARIABLE_REF (var)))
  743. gf = SCM_VARIABLE_REF (var);
  744. else
  745. gf = SCM_BOOL_F;
  746. if (!SCM_IS_A_P (gf, class_accessor))
  747. {
  748. gf = scm_make (scm_list_3 (class_generic, k_name, name));
  749. gf = scm_make (scm_list_5 (class_accessor,
  750. k_name, name, k_setter, gf));
  751. }
  752. return gf;
  753. }
  754. SCM_DEFINE (scm_sys_goops_early_init, "%goops-early-init", 0, 0, 0,
  755. (),
  756. "")
  757. #define FUNC_NAME s_scm_sys_goops_early_init
  758. {
  759. var_make_standard_class = scm_c_lookup ("make-standard-class");
  760. var_make = scm_c_lookup ("make");
  761. var_inherit_applicable = scm_c_lookup ("inherit-applicable!");
  762. /* For SCM_SUBCLASSP. */
  763. var_class_precedence_list = scm_c_lookup ("class-precedence-list");
  764. var_slot_ref = scm_c_lookup ("slot-ref");
  765. var_slot_set_x = scm_c_lookup ("slot-set!");
  766. var_slot_bound_p = scm_c_lookup ("slot-bound?");
  767. var_slot_exists_p = scm_c_lookup ("slot-exists?");
  768. class_class = scm_variable_ref (scm_c_lookup ("<class>"));
  769. class_top = scm_variable_ref (scm_c_lookup ("<top>"));
  770. class_object = scm_variable_ref (scm_c_lookup ("<object>"));
  771. class_foreign_slot = scm_variable_ref (scm_c_lookup ("<foreign-slot>"));
  772. class_protected = scm_variable_ref (scm_c_lookup ("<protected-slot>"));
  773. class_hidden = scm_variable_ref (scm_c_lookup ("<hidden-slot>"));
  774. class_opaque = scm_variable_ref (scm_c_lookup ("<opaque-slot>"));
  775. class_read_only = scm_variable_ref (scm_c_lookup ("<read-only-slot>"));
  776. class_self = scm_variable_ref (scm_c_lookup ("<self-slot>"));
  777. class_protected_opaque = scm_variable_ref (scm_c_lookup ("<protected-opaque-slot>"));
  778. class_protected_hidden = scm_variable_ref (scm_c_lookup ("<protected-hidden-slot>"));
  779. class_protected_read_only = scm_variable_ref (scm_c_lookup ("<protected-read-only-slot>"));
  780. class_scm = scm_variable_ref (scm_c_lookup ("<scm-slot>"));
  781. class_int = scm_variable_ref (scm_c_lookup ("<int-slot>"));
  782. class_float = scm_variable_ref (scm_c_lookup ("<float-slot>"));
  783. class_double = scm_variable_ref (scm_c_lookup ("<double-slot>"));
  784. /* Applicables */
  785. class_procedure_class = scm_variable_ref (scm_c_lookup ("<procedure-class>"));
  786. class_applicable_struct_class = scm_variable_ref (scm_c_lookup ("<applicable-struct-class>"));
  787. class_applicable_struct_with_setter_class =
  788. scm_variable_ref (scm_c_lookup ("<applicable-struct-with-setter-class>"));
  789. class_method = scm_variable_ref (scm_c_lookup ("<method>"));
  790. class_accessor_method = scm_variable_ref (scm_c_lookup ("<accessor-method>"));
  791. class_applicable = scm_variable_ref (scm_c_lookup ("<applicable>"));
  792. class_applicable_struct = scm_variable_ref (scm_c_lookup ("<applicable-struct>"));
  793. class_applicable_struct_with_setter = scm_variable_ref (scm_c_lookup ("<applicable-struct-with-setter>"));
  794. class_generic = scm_variable_ref (scm_c_lookup ("<generic>"));
  795. class_extended_generic = scm_variable_ref (scm_c_lookup ("<extended-generic>"));
  796. class_generic_with_setter = scm_variable_ref (scm_c_lookup ("<generic-with-setter>"));
  797. class_accessor = scm_variable_ref (scm_c_lookup ("<accessor>"));
  798. class_extended_generic_with_setter = scm_variable_ref (scm_c_lookup ("<extended-generic-with-setter>"));
  799. class_extended_accessor = scm_variable_ref (scm_c_lookup ("<extended-accessor>"));
  800. /* Primitive types classes */
  801. class_boolean = scm_variable_ref (scm_c_lookup ("<boolean>"));
  802. class_char = scm_variable_ref (scm_c_lookup ("<char>"));
  803. class_list = scm_variable_ref (scm_c_lookup ("<list>"));
  804. class_pair = scm_variable_ref (scm_c_lookup ("<pair>"));
  805. class_null = scm_variable_ref (scm_c_lookup ("<null>"));
  806. class_string = scm_variable_ref (scm_c_lookup ("<string>"));
  807. class_symbol = scm_variable_ref (scm_c_lookup ("<symbol>"));
  808. class_vector = scm_variable_ref (scm_c_lookup ("<vector>"));
  809. class_foreign = scm_variable_ref (scm_c_lookup ("<foreign>"));
  810. class_hashtable = scm_variable_ref (scm_c_lookup ("<hashtable>"));
  811. class_fluid = scm_variable_ref (scm_c_lookup ("<fluid>"));
  812. class_dynamic_state = scm_variable_ref (scm_c_lookup ("<dynamic-state>"));
  813. class_frame = scm_variable_ref (scm_c_lookup ("<frame>"));
  814. class_keyword = scm_variable_ref (scm_c_lookup ("<keyword>"));
  815. class_vm_cont = scm_variable_ref (scm_c_lookup ("<vm-continuation>"));
  816. class_bytevector = scm_variable_ref (scm_c_lookup ("<bytevector>"));
  817. class_uvec = scm_variable_ref (scm_c_lookup ("<uvec>"));
  818. class_array = scm_variable_ref (scm_c_lookup ("<array>"));
  819. class_bitvector = scm_variable_ref (scm_c_lookup ("<bitvector>"));
  820. class_number = scm_variable_ref (scm_c_lookup ("<number>"));
  821. class_complex = scm_variable_ref (scm_c_lookup ("<complex>"));
  822. class_real = scm_variable_ref (scm_c_lookup ("<real>"));
  823. class_integer = scm_variable_ref (scm_c_lookup ("<integer>"));
  824. class_fraction = scm_variable_ref (scm_c_lookup ("<fraction>"));
  825. class_keyword = scm_variable_ref (scm_c_lookup ("<keyword>"));
  826. class_unknown = scm_variable_ref (scm_c_lookup ("<unknown>"));
  827. class_procedure = scm_variable_ref (scm_c_lookup ("<procedure>"));
  828. class_primitive_generic = scm_variable_ref (scm_c_lookup ("<primitive-generic>"));
  829. class_port = scm_variable_ref (scm_c_lookup ("<port>"));
  830. class_input_port = scm_variable_ref (scm_c_lookup ("<input-port>"));
  831. class_output_port = scm_variable_ref (scm_c_lookup ("<output-port>"));
  832. class_input_output_port = scm_variable_ref (scm_c_lookup ("<input-output-port>"));
  833. create_smob_classes ();
  834. create_struct_classes ();
  835. create_port_classes ();
  836. return SCM_UNSPECIFIED;
  837. }
  838. #undef FUNC_NAME
  839. SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0,
  840. (),
  841. "Announce that GOOPS is loaded and perform initialization\n"
  842. "on the C level which depends on the loaded GOOPS modules.")
  843. #define FUNC_NAME s_scm_sys_goops_loaded
  844. {
  845. goops_loaded_p = 1;
  846. var_class_name = scm_c_lookup ("class-name");
  847. var_class_direct_supers = scm_c_lookup ("class-direct-supers");
  848. var_class_direct_slots = scm_c_lookup ("class-direct-slots");
  849. var_class_direct_subclasses = scm_c_lookup ("class-direct-subclasses");
  850. var_class_direct_methods = scm_c_lookup ("class-direct-methods");
  851. var_class_slots = scm_c_lookup ("class-slots");
  852. var_generic_function_methods = scm_c_lookup ("generic-function-methods");
  853. var_method_generic_function = scm_c_lookup ("method-generic-function");
  854. var_method_specializers = scm_c_lookup ("method-specializers");
  855. var_method_procedure = scm_c_lookup ("method-procedure");
  856. var_change_class = scm_c_lookup ("change-class");
  857. #if (SCM_ENABLE_DEPRECATED == 1)
  858. scm_init_deprecated_goops ();
  859. #endif
  860. return SCM_UNSPECIFIED;
  861. }
  862. #undef FUNC_NAME
  863. static void
  864. scm_init_goops_builtins (void *unused)
  865. {
  866. scm_module_goops = scm_current_module ();
  867. hell = scm_calloc (hell_size * sizeof (*hell));
  868. hell_mutex = scm_make_mutex ();
  869. #include "libguile/goops.x"
  870. scm_c_define ("vtable-flag-vtable",
  871. scm_from_int (SCM_VTABLE_FLAG_VTABLE));
  872. scm_c_define ("vtable-flag-applicable-vtable",
  873. scm_from_int (SCM_VTABLE_FLAG_APPLICABLE_VTABLE));
  874. scm_c_define ("vtable-flag-setter-vtable",
  875. scm_from_int (SCM_VTABLE_FLAG_SETTER_VTABLE));
  876. scm_c_define ("vtable-flag-validated",
  877. scm_from_int (SCM_VTABLE_FLAG_VALIDATED));
  878. scm_c_define ("vtable-flag-goops-class",
  879. scm_from_int (SCM_VTABLE_FLAG_GOOPS_CLASS));
  880. scm_c_define ("vtable-flag-goops-valid",
  881. scm_from_int (SCM_VTABLE_FLAG_GOOPS_VALID));
  882. scm_c_define ("vtable-flag-goops-slot",
  883. scm_from_int (SCM_VTABLE_FLAG_GOOPS_SLOT));
  884. scm_c_define ("vtable-flag-goops-static",
  885. scm_from_int (SCM_VTABLE_FLAG_GOOPS_STATIC));
  886. }
  887. void
  888. scm_init_goops ()
  889. {
  890. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  891. "scm_init_goops_builtins", scm_init_goops_builtins,
  892. NULL);
  893. }
  894. /*
  895. Local Variables:
  896. c-file-style: "gnu"
  897. End:
  898. */