goops.c 32 KB

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