goops.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /* Copyright 1998-2004,2008-2015,2017-2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* This software is a derivative work of other copyrighted softwares; the
  16. * copyright notices of these softwares are placed in the file COPYRIGHTS
  17. *
  18. * This file is based upon stklos.c from the STk distribution by
  19. * Erick Gallesio <eg@unice.fr>.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. # include <config.h>
  23. #endif
  24. #include "async.h"
  25. #include "boolean.h"
  26. #include "chars.h"
  27. #include "dynwind.h"
  28. #include "eval.h"
  29. #include "extensions.h"
  30. #include "foreign.h"
  31. #include "gsubr.h"
  32. #include "hashtab.h"
  33. #include "keywords.h"
  34. #include "macros.h"
  35. #include "modules.h"
  36. #include "numbers.h"
  37. #include "pairs.h"
  38. #include "ports-internal.h"
  39. #include "ports.h"
  40. #include "procprop.h"
  41. #include "programs.h"
  42. #include "smob.h"
  43. #include "strings.h"
  44. #include "strports.h"
  45. #include "symbols.h"
  46. #include "variable.h"
  47. #include "vectors.h"
  48. #include "version.h"
  49. #include "weak-table.h"
  50. #include "goops.h"
  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_class_of_obsolete_indirect_instance = 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_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_syntax;
  104. static SCM class_atomic_box;
  105. static SCM class_port, class_input_output_port;
  106. static SCM class_input_port, class_output_port;
  107. static SCM class_foreign;
  108. static SCM class_hashtable;
  109. static SCM class_fluid;
  110. static SCM class_dynamic_state;
  111. static SCM class_frame;
  112. static SCM class_vm_cont;
  113. static SCM class_bytevector;
  114. static SCM class_uvec;
  115. static SCM class_array;
  116. static SCM class_bitvector;
  117. static SCM vtable_class_map = SCM_BOOL_F;
  118. /* SMOB classes. */
  119. SCM scm_i_smob_class[SCM_I_MAX_SMOB_TYPE_COUNT];
  120. SCM scm_module_goops;
  121. static SCM scm_sys_make_vtable_vtable (SCM layout);
  122. static SCM scm_sys_init_layout_x (SCM class, SCM layout);
  123. static SCM scm_sys_clear_fields_x (SCM obj, SCM unbound);
  124. static SCM scm_sys_goops_early_init (void);
  125. static SCM scm_sys_goops_loaded (void);
  126. SCM_DEFINE (scm_sys_make_vtable_vtable, "%make-vtable-vtable", 1, 0, 0,
  127. (SCM layout),
  128. "")
  129. #define FUNC_NAME s_scm_sys_make_vtable_vtable
  130. {
  131. return scm_i_make_vtable_vtable (layout);
  132. }
  133. #undef FUNC_NAME
  134. SCM
  135. scm_make_standard_class (SCM meta, SCM name, SCM dsupers, SCM dslots)
  136. {
  137. return scm_call_4 (scm_variable_ref (var_make_standard_class),
  138. meta, name, dsupers, dslots);
  139. }
  140. SCM_DEFINE (scm_sys_init_layout_x, "%init-layout!", 2, 0, 0,
  141. (SCM class, SCM layout),
  142. "")
  143. #define FUNC_NAME s_scm_sys_init_layout_x
  144. {
  145. SCM_VALIDATE_INSTANCE (1, class);
  146. SCM_ASSERT (!scm_is_symbol (SCM_VTABLE_LAYOUT (class)), class, 1, FUNC_NAME);
  147. SCM_VALIDATE_STRING (2, layout);
  148. SCM_SET_VTABLE_LAYOUT (class, scm_make_struct_layout (layout));
  149. scm_i_struct_inherit_vtable_magic (scm_class_of (class), class);
  150. SCM_SET_CLASS_FLAGS (class, SCM_VTABLE_FLAG_GOOPS_CLASS);
  151. return SCM_UNSPECIFIED;
  152. }
  153. #undef FUNC_NAME
  154. static SCM
  155. get_indirect_slots (SCM x)
  156. {
  157. /* Precondition: X is an indirect instance. The indirect slots are in
  158. the last field. */
  159. scm_t_bits nfields =
  160. SCM_STRUCT_DATA_REF (SCM_STRUCT_VTABLE (x), scm_vtable_index_size);
  161. return SCM_STRUCT_SLOT_REF (x, nfields - 1);
  162. }
  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_syntax:
  206. return class_syntax;
  207. case scm_tc7_atomic_box:
  208. return class_atomic_box;
  209. case scm_tc7_vm_cont:
  210. return class_vm_cont;
  211. case scm_tc7_bytevector:
  212. if (SCM_BYTEVECTOR_ELEMENT_TYPE (x) == SCM_ARRAY_ELEMENT_TYPE_VU8)
  213. return class_bytevector;
  214. else
  215. return class_uvec;
  216. case scm_tc7_array:
  217. return class_array;
  218. case scm_tc7_bitvector:
  219. return class_bitvector;
  220. case scm_tc7_string:
  221. return class_string;
  222. case scm_tc7_number:
  223. switch SCM_TYP16 (x) {
  224. case scm_tc16_big:
  225. return class_integer;
  226. case scm_tc16_real:
  227. return class_real;
  228. case scm_tc16_complex:
  229. return class_complex;
  230. case scm_tc16_fraction:
  231. return class_fraction;
  232. }
  233. case scm_tc7_program:
  234. if (SCM_PROGRAM_IS_PRIMITIVE_GENERIC (x)
  235. && SCM_UNPACK (*SCM_SUBR_GENERIC (x)))
  236. return class_primitive_generic;
  237. else
  238. return class_procedure;
  239. case scm_tc7_smob:
  240. {
  241. scm_t_bits type = SCM_TYP16 (x);
  242. if (type != scm_tc16_port_with_ps)
  243. return scm_i_smob_class[SCM_TC2SMOBNUM (type)];
  244. x = SCM_PORT_WITH_PS_PORT (x);
  245. /* fall through to ports */
  246. }
  247. case scm_tc7_port:
  248. {
  249. scm_t_port_type *ptob = SCM_PORT_TYPE (x);
  250. if (SCM_INPUT_PORT_P (x))
  251. {
  252. if (SCM_OUTPUT_PORT_P (x))
  253. return ptob->input_output_class;
  254. return ptob->input_class;
  255. }
  256. return ptob->output_class;
  257. }
  258. case scm_tcs_struct:
  259. {
  260. SCM vtable = SCM_STRUCT_VTABLE (x);
  261. scm_t_bits flags = SCM_VTABLE_FLAGS (vtable);
  262. scm_t_bits direct = SCM_VTABLE_FLAG_GOOPS_CLASS;
  263. scm_t_bits indirect = direct | SCM_VTABLE_FLAG_GOOPS_INDIRECT;
  264. scm_t_bits mask = indirect;
  265. if ((flags & mask) == direct)
  266. /* A direct GOOPS object. */
  267. return vtable;
  268. else if ((flags & mask) == indirect)
  269. /* An indirect GOOPS object. If the vtable of the slots
  270. object is flagged to indicate that there's a new class
  271. definition available, migrate the instance before
  272. returning the class. */
  273. {
  274. SCM slots = get_indirect_slots (x);
  275. scm_t_bits slot_flags = SCM_OBJ_CLASS_FLAGS (slots);
  276. if (slot_flags & SCM_VTABLE_FLAG_GOOPS_NEEDS_MIGRATION)
  277. return scm_call_1
  278. (scm_variable_ref (var_class_of_obsolete_indirect_instance),
  279. x);
  280. else
  281. return vtable;
  282. }
  283. else
  284. /* A non-GOOPS struct. */
  285. return scm_i_define_class_for_vtable (vtable);
  286. }
  287. default:
  288. if (scm_is_pair (x))
  289. return class_pair;
  290. else
  291. return class_unknown;
  292. }
  293. case scm_tc3_struct:
  294. case scm_tc3_tc7_1:
  295. case scm_tc3_tc7_2:
  296. /* case scm_tc3_unused: */
  297. /* Never reached */
  298. break;
  299. }
  300. return class_unknown;
  301. }
  302. #undef FUNC_NAME
  303. SCM_DEFINE (scm_instance_p, "instance?", 1, 0, 0,
  304. (SCM obj),
  305. "Return @code{#t} if @var{obj} is an instance.")
  306. #define FUNC_NAME s_scm_instance_p
  307. {
  308. return scm_from_bool (SCM_INSTANCEP (obj));
  309. }
  310. #undef FUNC_NAME
  311. int
  312. scm_is_generic (SCM x)
  313. {
  314. return SCM_INSTANCEP (x) && SCM_SUBCLASSP (scm_class_of (x), class_generic);
  315. }
  316. int
  317. scm_is_method (SCM x)
  318. {
  319. return SCM_INSTANCEP (x) && SCM_SUBCLASSP (scm_class_of (x), class_method);
  320. }
  321. SCM
  322. scm_class_name (SCM obj)
  323. {
  324. return scm_call_1 (scm_variable_ref (var_class_name), obj);
  325. }
  326. SCM
  327. scm_class_direct_supers (SCM obj)
  328. {
  329. return scm_call_1 (scm_variable_ref (var_class_direct_supers), obj);
  330. }
  331. SCM
  332. scm_class_direct_slots (SCM obj)
  333. {
  334. return scm_call_1 (scm_variable_ref (var_class_direct_slots), obj);
  335. }
  336. SCM
  337. scm_class_direct_subclasses (SCM obj)
  338. {
  339. return scm_call_1 (scm_variable_ref (var_class_direct_subclasses), obj);
  340. }
  341. SCM
  342. scm_class_direct_methods (SCM obj)
  343. {
  344. return scm_call_1 (scm_variable_ref (var_class_direct_methods), obj);
  345. }
  346. SCM
  347. scm_class_precedence_list (SCM obj)
  348. {
  349. return scm_call_1 (scm_variable_ref (var_class_precedence_list), obj);
  350. }
  351. SCM
  352. scm_class_slots (SCM obj)
  353. {
  354. return scm_call_1 (scm_variable_ref (var_class_slots), obj);
  355. }
  356. SCM_DEFINE (scm_generic_function_name, "generic-function-name", 1, 0, 0,
  357. (SCM obj),
  358. "Return the name of the generic function @var{obj}.")
  359. #define FUNC_NAME s_scm_generic_function_name
  360. {
  361. SCM_VALIDATE_GENERIC (1, obj);
  362. return scm_procedure_property (obj, scm_sym_name);
  363. }
  364. #undef FUNC_NAME
  365. SCM
  366. scm_generic_function_methods (SCM obj)
  367. {
  368. return scm_call_1 (scm_variable_ref (var_generic_function_methods), obj);
  369. }
  370. SCM
  371. scm_method_generic_function (SCM obj)
  372. {
  373. return scm_call_1 (scm_variable_ref (var_method_generic_function), obj);
  374. }
  375. SCM
  376. scm_method_specializers (SCM obj)
  377. {
  378. return scm_call_1 (scm_variable_ref (var_method_specializers), obj);
  379. }
  380. SCM
  381. scm_method_procedure (SCM obj)
  382. {
  383. return scm_call_1 (scm_variable_ref (var_method_procedure), obj);
  384. }
  385. SCM
  386. scm_slot_ref (SCM obj, SCM slot_name)
  387. {
  388. return scm_call_2 (scm_variable_ref (var_slot_ref), obj, slot_name);
  389. }
  390. SCM
  391. scm_slot_set_x (SCM obj, SCM slot_name, SCM value)
  392. {
  393. return scm_call_3 (scm_variable_ref (var_slot_set_x), obj, slot_name, value);
  394. }
  395. SCM
  396. scm_slot_bound_p (SCM obj, SCM slot_name)
  397. {
  398. return scm_call_2 (scm_variable_ref (var_slot_bound_p), obj, slot_name);
  399. }
  400. SCM
  401. scm_slot_exists_p (SCM obj, SCM slot_name)
  402. {
  403. return scm_call_2 (scm_variable_ref (var_slot_exists_p), obj, slot_name);
  404. }
  405. SCM_DEFINE (scm_sys_clear_fields_x, "%clear-fields!", 2, 0, 0,
  406. (SCM obj, SCM unbound),
  407. "")
  408. #define FUNC_NAME s_scm_sys_clear_fields_x
  409. {
  410. scm_t_signed_bits n, i;
  411. SCM_VALIDATE_STRUCT (1, obj);
  412. n = SCM_STRUCT_SIZE (obj);
  413. /* Set all SCM-holding slots to the GOOPS unbound value. */
  414. for (i = 0; i < n; i++)
  415. if (!SCM_STRUCT_FIELD_IS_UNBOXED (obj, i))
  416. SCM_STRUCT_SLOT_SET (obj, i, unbound);
  417. return SCM_UNSPECIFIED;
  418. }
  419. #undef FUNC_NAME
  420. static scm_i_pthread_mutex_t goops_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  421. SCM_DEFINE (scm_sys_modify_instance, "%modify-instance", 2, 0, 0,
  422. (SCM old, SCM new),
  423. "Used by change-class to modify objects in place.")
  424. #define FUNC_NAME s_scm_sys_modify_instance
  425. {
  426. scm_t_bits i, old_nfields, new_nfields;
  427. SCM_VALIDATE_INSTANCE (1, old);
  428. SCM_VALIDATE_INSTANCE (2, new);
  429. old_nfields = SCM_STRUCT_DATA_REF (SCM_STRUCT_VTABLE (old),
  430. scm_vtable_index_size);
  431. new_nfields = SCM_STRUCT_DATA_REF (SCM_STRUCT_VTABLE (new),
  432. scm_vtable_index_size);
  433. SCM_ASSERT (old_nfields == new_nfields, new, SCM_ARG2, FUNC_NAME);
  434. /* Exchange the data contained in old and new. We exchange rather than
  435. scratch the old value with new to be correct with GC. See "Class
  436. redefinition protocol" in goops.scm. */
  437. scm_i_pthread_mutex_lock (&goops_lock);
  438. /* Swap vtables. */
  439. {
  440. scm_t_bits tmp = SCM_CELL_WORD_0 (old);
  441. SCM_SET_CELL_WORD_0 (old, SCM_CELL_WORD_0 (new));
  442. SCM_SET_CELL_WORD_0 (new, tmp);
  443. }
  444. /* Swap data. */
  445. for (i = 0; i < old_nfields; i++)
  446. {
  447. scm_t_bits tmp = SCM_STRUCT_DATA_REF (old, i);
  448. SCM_STRUCT_DATA_SET (old, i, SCM_STRUCT_DATA_REF (new, i));
  449. SCM_STRUCT_DATA_SET (new, i, tmp);
  450. }
  451. scm_i_pthread_mutex_unlock (&goops_lock);
  452. return SCM_UNSPECIFIED;
  453. }
  454. #undef FUNC_NAME
  455. /* Primitive generics: primitives that can dispatch to generics if their
  456. arguments fail to apply. */
  457. SCM_DEFINE (scm_generic_capability_p, "generic-capability?", 1, 0, 0,
  458. (SCM proc),
  459. "")
  460. #define FUNC_NAME s_scm_generic_capability_p
  461. {
  462. SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
  463. proc, SCM_ARG1, FUNC_NAME);
  464. return (SCM_PRIMITIVE_GENERIC_P (proc) ? SCM_BOOL_T : SCM_BOOL_F);
  465. }
  466. #undef FUNC_NAME
  467. SCM_DEFINE (scm_enable_primitive_generic_x, "enable-primitive-generic!", 0, 0, 1,
  468. (SCM subrs),
  469. "")
  470. #define FUNC_NAME s_scm_enable_primitive_generic_x
  471. {
  472. SCM_VALIDATE_REST_ARGUMENT (subrs);
  473. while (!scm_is_null (subrs))
  474. {
  475. SCM subr = SCM_CAR (subrs);
  476. SCM_ASSERT (SCM_PRIMITIVE_GENERIC_P (subr), subr, SCM_ARGn, FUNC_NAME);
  477. SCM_SET_SUBR_GENERIC (subr,
  478. scm_make (scm_list_3 (class_generic,
  479. k_name,
  480. SCM_SUBR_NAME (subr))));
  481. subrs = SCM_CDR (subrs);
  482. }
  483. return SCM_UNSPECIFIED;
  484. }
  485. #undef FUNC_NAME
  486. SCM_DEFINE (scm_set_primitive_generic_x, "set-primitive-generic!", 2, 0, 0,
  487. (SCM subr, SCM generic),
  488. "")
  489. #define FUNC_NAME s_scm_set_primitive_generic_x
  490. {
  491. SCM_ASSERT (SCM_PRIMITIVE_GENERIC_P (subr), subr, SCM_ARG1, FUNC_NAME);
  492. SCM_ASSERT (SCM_GENERICP (generic), generic, SCM_ARG2, FUNC_NAME);
  493. SCM_SET_SUBR_GENERIC (subr, generic);
  494. return SCM_UNSPECIFIED;
  495. }
  496. #undef FUNC_NAME
  497. SCM_DEFINE (scm_primitive_generic_generic, "primitive-generic-generic", 1, 0, 0,
  498. (SCM subr),
  499. "")
  500. #define FUNC_NAME s_scm_primitive_generic_generic
  501. {
  502. if (SCM_PRIMITIVE_GENERIC_P (subr))
  503. {
  504. if (!SCM_UNPACK (*SCM_SUBR_GENERIC (subr)))
  505. scm_enable_primitive_generic_x (scm_list_1 (subr));
  506. return *SCM_SUBR_GENERIC (subr);
  507. }
  508. SCM_WRONG_TYPE_ARG (SCM_ARG1, subr);
  509. }
  510. #undef FUNC_NAME
  511. SCM
  512. scm_wta_dispatch_0 (SCM gf, const char *subr)
  513. {
  514. if (!SCM_UNPACK (gf))
  515. scm_error_num_args_subr (subr);
  516. return scm_call_0 (gf);
  517. }
  518. SCM
  519. scm_wta_dispatch_1 (SCM gf, SCM a1, int pos, const char *subr)
  520. {
  521. if (!SCM_UNPACK (gf))
  522. scm_wrong_type_arg (subr, pos, a1);
  523. return scm_call_1 (gf, a1);
  524. }
  525. SCM
  526. scm_wta_dispatch_2 (SCM gf, SCM a1, SCM a2, int pos, const char *subr)
  527. {
  528. if (!SCM_UNPACK (gf))
  529. scm_wrong_type_arg (subr, pos, (pos == SCM_ARG1) ? a1 : a2);
  530. return scm_call_2 (gf, a1, a2);
  531. }
  532. SCM
  533. scm_wta_dispatch_n (SCM gf, SCM args, int pos, const char *subr)
  534. {
  535. if (!SCM_UNPACK (gf))
  536. scm_wrong_type_arg (subr, pos, scm_list_ref (args, scm_from_int (pos)));
  537. return scm_apply_0 (gf, args);
  538. }
  539. SCM
  540. scm_make (SCM args)
  541. {
  542. return scm_apply_0 (scm_variable_ref (var_make), args);
  543. }
  544. /* SMOB, struct, and port classes. */
  545. static SCM
  546. make_class_name (const char *prefix, const char *type_name, const char *suffix)
  547. {
  548. if (!type_name)
  549. type_name = "";
  550. return scm_string_to_symbol (scm_string_append
  551. (scm_list_3 (scm_from_utf8_string (prefix),
  552. scm_from_utf8_string (type_name),
  553. scm_from_utf8_string (suffix))));
  554. }
  555. SCM
  556. scm_make_extended_class (char const *type_name, int applicablep)
  557. {
  558. SCM name, meta, supers;
  559. name = make_class_name ("<", type_name, ">");
  560. meta = class_class;
  561. if (applicablep)
  562. supers = scm_list_1 (class_applicable);
  563. else
  564. supers = scm_list_1 (class_top);
  565. return scm_make_standard_class (meta, name, supers, SCM_EOL);
  566. }
  567. void
  568. scm_i_inherit_applicable (SCM c)
  569. {
  570. scm_call_1 (scm_variable_ref (var_inherit_applicable), c);
  571. }
  572. static void
  573. create_smob_classes (void)
  574. {
  575. long i;
  576. for (i = 0; i < SCM_I_MAX_SMOB_TYPE_COUNT; ++i)
  577. scm_i_smob_class[i] = SCM_BOOL_F;
  578. for (i = 0; i < scm_numsmob; ++i)
  579. if (scm_is_false (scm_i_smob_class[i]))
  580. scm_i_smob_class[i] = scm_make_extended_class (SCM_SMOBNAME (i),
  581. scm_smobs[i].apply != 0);
  582. }
  583. struct pre_goops_port_type
  584. {
  585. scm_t_port_type *ptob;
  586. struct pre_goops_port_type *prev;
  587. };
  588. struct pre_goops_port_type *pre_goops_port_types;
  589. static void
  590. make_port_classes (scm_t_port_type *ptob)
  591. {
  592. SCM name, meta, super, supers;
  593. meta = class_class;
  594. name = make_class_name ("<", ptob->name, "-port>");
  595. supers = scm_list_1 (class_port);
  596. super = scm_make_standard_class (meta, name, supers, SCM_EOL);
  597. name = make_class_name ("<", ptob->name, "-input-port>");
  598. supers = scm_list_2 (super, class_input_port);
  599. ptob->input_class = scm_make_standard_class (meta, name, supers, SCM_EOL);
  600. name = make_class_name ("<", ptob->name, "-output-port>");
  601. supers = scm_list_2 (super, class_output_port);
  602. ptob->output_class = scm_make_standard_class (meta, name, supers, SCM_EOL);
  603. name = make_class_name ("<", ptob->name, "-input-output-port>");
  604. supers = scm_list_2 (super, class_input_output_port);
  605. ptob->input_output_class =
  606. scm_make_standard_class (meta, name, supers, SCM_EOL);
  607. }
  608. void
  609. scm_make_port_classes (scm_t_port_type *ptob)
  610. {
  611. ptob->input_class = SCM_BOOL_F;
  612. ptob->output_class = SCM_BOOL_F;
  613. ptob->input_output_class = SCM_BOOL_F;
  614. if (!goops_loaded_p)
  615. {
  616. /* Not really a pair. */
  617. struct pre_goops_port_type *link;
  618. link = scm_gc_typed_calloc (struct pre_goops_port_type);
  619. link->ptob = ptob;
  620. link->prev = pre_goops_port_types;
  621. pre_goops_port_types = link;
  622. return;
  623. }
  624. make_port_classes (ptob);
  625. }
  626. static void
  627. create_port_classes (void)
  628. {
  629. while (pre_goops_port_types)
  630. {
  631. make_port_classes (pre_goops_port_types->ptob);
  632. pre_goops_port_types = pre_goops_port_types->prev;
  633. }
  634. }
  635. SCM
  636. scm_i_define_class_for_vtable (SCM vtable)
  637. {
  638. SCM class;
  639. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  640. if (scm_is_false (vtable_class_map))
  641. vtable_class_map = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  642. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  643. if (scm_is_false (scm_struct_vtable_p (vtable)))
  644. abort ();
  645. class = scm_weak_table_refq (vtable_class_map, vtable, SCM_BOOL_F);
  646. if (scm_is_false (class))
  647. {
  648. if (SCM_UNPACK (class_class))
  649. {
  650. SCM name, meta, supers;
  651. name = SCM_VTABLE_NAME (vtable);
  652. if (scm_is_symbol (name))
  653. name = scm_string_to_symbol
  654. (scm_string_append
  655. (scm_list_3 (scm_from_latin1_string ("<"),
  656. scm_symbol_to_string (name),
  657. scm_from_latin1_string (">"))));
  658. else
  659. name = scm_from_latin1_symbol ("<>");
  660. if (SCM_STRUCT_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_SETTER))
  661. {
  662. meta = class_applicable_struct_with_setter_class;
  663. supers = scm_list_1 (class_applicable_struct_with_setter);
  664. }
  665. else if (SCM_STRUCT_VTABLE_FLAG_IS_SET (vtable,
  666. SCM_VTABLE_FLAG_APPLICABLE))
  667. {
  668. meta = class_applicable_struct_class;
  669. supers = scm_list_1 (class_applicable_struct);
  670. }
  671. else
  672. {
  673. meta = class_class;
  674. supers = scm_list_1 (class_top);
  675. }
  676. class = scm_make_standard_class (meta, name, supers, SCM_EOL);
  677. }
  678. else
  679. /* `create_struct_classes' will fill this in later. */
  680. class = SCM_BOOL_F;
  681. /* Don't worry about races. This only happens when creating a
  682. vtable, which happens by definition in one thread. */
  683. scm_weak_table_putq_x (vtable_class_map, vtable, class);
  684. }
  685. return class;
  686. }
  687. static SCM
  688. make_struct_class (void *closure SCM_UNUSED,
  689. SCM vtable, SCM data, SCM prev SCM_UNUSED)
  690. {
  691. if (scm_is_false (data))
  692. scm_i_define_class_for_vtable (vtable);
  693. return SCM_UNSPECIFIED;
  694. }
  695. static void
  696. create_struct_classes (void)
  697. {
  698. /* FIXME: take the vtable_class_map while initializing goops? */
  699. scm_internal_hash_fold (make_struct_class, 0, SCM_BOOL_F,
  700. vtable_class_map);
  701. }
  702. void
  703. scm_load_goops ()
  704. {
  705. if (!goops_loaded_p)
  706. scm_c_resolve_module ("oop goops");
  707. }
  708. SCM
  709. scm_ensure_accessor (SCM name)
  710. {
  711. SCM var, gf;
  712. var = scm_module_variable (scm_current_module (), name);
  713. if (SCM_VARIABLEP (var) && !SCM_UNBNDP (SCM_VARIABLE_REF (var)))
  714. gf = SCM_VARIABLE_REF (var);
  715. else
  716. gf = SCM_BOOL_F;
  717. if (!SCM_IS_A_P (gf, class_accessor))
  718. {
  719. gf = scm_make (scm_list_3 (class_generic, k_name, name));
  720. gf = scm_make (scm_list_5 (class_accessor,
  721. k_name, name, k_setter, gf));
  722. }
  723. return gf;
  724. }
  725. SCM_DEFINE (scm_sys_goops_early_init, "%goops-early-init", 0, 0, 0,
  726. (),
  727. "")
  728. #define FUNC_NAME s_scm_sys_goops_early_init
  729. {
  730. var_make_standard_class = scm_c_lookup ("make-standard-class");
  731. var_make = scm_c_lookup ("make");
  732. var_inherit_applicable = scm_c_lookup ("inherit-applicable!");
  733. /* For SCM_SUBCLASSP. */
  734. var_class_precedence_list = scm_c_lookup ("class-precedence-list");
  735. var_slot_ref = scm_c_lookup ("slot-ref");
  736. var_slot_set_x = scm_c_lookup ("slot-set!");
  737. var_slot_bound_p = scm_c_lookup ("slot-bound?");
  738. var_slot_exists_p = scm_c_lookup ("slot-exists?");
  739. class_class = scm_variable_ref (scm_c_lookup ("<class>"));
  740. class_top = scm_variable_ref (scm_c_lookup ("<top>"));
  741. /* Applicables */
  742. class_procedure_class = scm_variable_ref (scm_c_lookup ("<procedure-class>"));
  743. class_applicable_struct_class = scm_variable_ref (scm_c_lookup ("<applicable-struct-class>"));
  744. class_applicable_struct_with_setter_class =
  745. scm_variable_ref (scm_c_lookup ("<applicable-struct-with-setter-class>"));
  746. class_method = scm_variable_ref (scm_c_lookup ("<method>"));
  747. class_accessor_method = scm_variable_ref (scm_c_lookup ("<accessor-method>"));
  748. class_applicable = scm_variable_ref (scm_c_lookup ("<applicable>"));
  749. class_applicable_struct = scm_variable_ref (scm_c_lookup ("<applicable-struct>"));
  750. class_applicable_struct_with_setter = scm_variable_ref (scm_c_lookup ("<applicable-struct-with-setter>"));
  751. class_generic = scm_variable_ref (scm_c_lookup ("<generic>"));
  752. class_extended_generic = scm_variable_ref (scm_c_lookup ("<extended-generic>"));
  753. class_generic_with_setter = scm_variable_ref (scm_c_lookup ("<generic-with-setter>"));
  754. class_accessor = scm_variable_ref (scm_c_lookup ("<accessor>"));
  755. class_extended_generic_with_setter = scm_variable_ref (scm_c_lookup ("<extended-generic-with-setter>"));
  756. class_extended_accessor = scm_variable_ref (scm_c_lookup ("<extended-accessor>"));
  757. /* Primitive types classes */
  758. class_boolean = scm_variable_ref (scm_c_lookup ("<boolean>"));
  759. class_char = scm_variable_ref (scm_c_lookup ("<char>"));
  760. class_list = scm_variable_ref (scm_c_lookup ("<list>"));
  761. class_pair = scm_variable_ref (scm_c_lookup ("<pair>"));
  762. class_null = scm_variable_ref (scm_c_lookup ("<null>"));
  763. class_string = scm_variable_ref (scm_c_lookup ("<string>"));
  764. class_symbol = scm_variable_ref (scm_c_lookup ("<symbol>"));
  765. class_vector = scm_variable_ref (scm_c_lookup ("<vector>"));
  766. class_foreign = scm_variable_ref (scm_c_lookup ("<foreign>"));
  767. class_hashtable = scm_variable_ref (scm_c_lookup ("<hashtable>"));
  768. class_fluid = scm_variable_ref (scm_c_lookup ("<fluid>"));
  769. class_dynamic_state = scm_variable_ref (scm_c_lookup ("<dynamic-state>"));
  770. class_frame = scm_variable_ref (scm_c_lookup ("<frame>"));
  771. class_keyword = scm_variable_ref (scm_c_lookup ("<keyword>"));
  772. class_syntax = scm_variable_ref (scm_c_lookup ("<syntax>"));
  773. class_atomic_box = scm_variable_ref (scm_c_lookup ("<atomic-box>"));
  774. class_vm_cont = scm_variable_ref (scm_c_lookup ("<vm-continuation>"));
  775. class_bytevector = scm_variable_ref (scm_c_lookup ("<bytevector>"));
  776. class_uvec = scm_variable_ref (scm_c_lookup ("<uvec>"));
  777. class_array = scm_variable_ref (scm_c_lookup ("<array>"));
  778. class_bitvector = scm_variable_ref (scm_c_lookup ("<bitvector>"));
  779. class_number = scm_variable_ref (scm_c_lookup ("<number>"));
  780. class_complex = scm_variable_ref (scm_c_lookup ("<complex>"));
  781. class_real = scm_variable_ref (scm_c_lookup ("<real>"));
  782. class_integer = scm_variable_ref (scm_c_lookup ("<integer>"));
  783. class_fraction = scm_variable_ref (scm_c_lookup ("<fraction>"));
  784. class_unknown = scm_variable_ref (scm_c_lookup ("<unknown>"));
  785. class_procedure = scm_variable_ref (scm_c_lookup ("<procedure>"));
  786. class_primitive_generic = scm_variable_ref (scm_c_lookup ("<primitive-generic>"));
  787. class_port = scm_variable_ref (scm_c_lookup ("<port>"));
  788. class_input_port = scm_variable_ref (scm_c_lookup ("<input-port>"));
  789. class_output_port = scm_variable_ref (scm_c_lookup ("<output-port>"));
  790. class_input_output_port = scm_variable_ref (scm_c_lookup ("<input-output-port>"));
  791. create_smob_classes ();
  792. create_struct_classes ();
  793. create_port_classes ();
  794. return SCM_UNSPECIFIED;
  795. }
  796. #undef FUNC_NAME
  797. SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0,
  798. (),
  799. "Announce that GOOPS is loaded and perform initialization\n"
  800. "on the C level which depends on the loaded GOOPS modules.")
  801. #define FUNC_NAME s_scm_sys_goops_loaded
  802. {
  803. goops_loaded_p = 1;
  804. var_class_name = scm_c_lookup ("class-name");
  805. var_class_direct_supers = scm_c_lookup ("class-direct-supers");
  806. var_class_direct_slots = scm_c_lookup ("class-direct-slots");
  807. var_class_direct_subclasses = scm_c_lookup ("class-direct-subclasses");
  808. var_class_direct_methods = scm_c_lookup ("class-direct-methods");
  809. var_class_slots = scm_c_lookup ("class-slots");
  810. var_generic_function_methods = scm_c_lookup ("generic-function-methods");
  811. var_method_generic_function = scm_c_lookup ("method-generic-function");
  812. var_method_specializers = scm_c_lookup ("method-specializers");
  813. var_method_procedure = scm_c_lookup ("method-procedure");
  814. var_class_of_obsolete_indirect_instance =
  815. scm_c_lookup ("class-of-obsolete-indirect-instance");
  816. return SCM_UNSPECIFIED;
  817. }
  818. #undef FUNC_NAME
  819. static void
  820. scm_init_goops_builtins (void *unused)
  821. {
  822. scm_module_goops = scm_current_module ();
  823. #include "goops.x"
  824. scm_c_define ("vtable-flag-vtable",
  825. scm_from_int (SCM_VTABLE_FLAG_VTABLE));
  826. scm_c_define ("vtable-flag-applicable-vtable",
  827. scm_from_int (SCM_VTABLE_FLAG_APPLICABLE_VTABLE));
  828. scm_c_define ("vtable-flag-setter-vtable",
  829. scm_from_int (SCM_VTABLE_FLAG_SETTER_VTABLE));
  830. scm_c_define ("vtable-flag-validated",
  831. scm_from_int (SCM_VTABLE_FLAG_VALIDATED));
  832. scm_c_define ("vtable-flag-goops-class",
  833. scm_from_int (SCM_VTABLE_FLAG_GOOPS_CLASS));
  834. scm_c_define ("vtable-flag-goops-slot",
  835. scm_from_int (SCM_VTABLE_FLAG_GOOPS_SLOT));
  836. scm_c_define ("vtable-flag-goops-static-slot-allocation",
  837. scm_from_int (SCM_VTABLE_FLAG_GOOPS_STATIC_SLOT_ALLOCATION));
  838. scm_c_define ("vtable-flag-goops-indirect",
  839. scm_from_int (SCM_VTABLE_FLAG_GOOPS_INDIRECT));
  840. scm_c_define ("vtable-flag-goops-needs-migration",
  841. scm_from_int (SCM_VTABLE_FLAG_GOOPS_NEEDS_MIGRATION));
  842. }
  843. void
  844. scm_init_goops ()
  845. {
  846. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  847. "scm_init_goops_builtins", scm_init_goops_builtins,
  848. NULL);
  849. }