goops.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860
  1. /* Copyright (C) 1998,1999,2000,2001,2002,2003,2004,2008,2009,2010,2011,2012
  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 <stdio.h>
  29. #include "libguile/_scm.h"
  30. #include "libguile/alist.h"
  31. #include "libguile/async.h"
  32. #include "libguile/chars.h"
  33. #include "libguile/debug.h"
  34. #include "libguile/dynl.h"
  35. #include "libguile/dynwind.h"
  36. #include "libguile/eval.h"
  37. #include "libguile/gsubr.h"
  38. #include "libguile/hashtab.h"
  39. #include "libguile/keywords.h"
  40. #include "libguile/macros.h"
  41. #include "libguile/modules.h"
  42. #include "libguile/ports.h"
  43. #include "libguile/procprop.h"
  44. #include "libguile/programs.h"
  45. #include "libguile/random.h"
  46. #include "libguile/root.h"
  47. #include "libguile/smob.h"
  48. #include "libguile/strings.h"
  49. #include "libguile/strports.h"
  50. #include "libguile/vectors.h"
  51. #include "libguile/vm.h"
  52. #include "libguile/validate.h"
  53. #include "libguile/goops.h"
  54. #define SPEC_OF(x) SCM_SLOT (x, scm_si_specializers)
  55. /* Port classes */
  56. #define SCM_IN_PCLASS_INDEX 0
  57. #define SCM_OUT_PCLASS_INDEX SCM_I_MAX_PORT_TYPE_COUNT
  58. #define SCM_INOUT_PCLASS_INDEX (2 * SCM_I_MAX_PORT_TYPE_COUNT)
  59. /* this file is a mess. in theory, though, we shouldn't have many SCM references
  60. -- most of the references should be to vars. */
  61. static SCM var_slot_unbound = SCM_BOOL_F;
  62. static SCM var_slot_missing = SCM_BOOL_F;
  63. static SCM var_compute_cpl = SCM_BOOL_F;
  64. static SCM var_no_applicable_method = SCM_BOOL_F;
  65. static SCM var_change_class = SCM_BOOL_F;
  66. SCM_SYMBOL (sym_slot_unbound, "slot-unbound");
  67. SCM_SYMBOL (sym_slot_missing, "slot-missing");
  68. SCM_SYMBOL (sym_compute_cpl, "compute-cpl");
  69. SCM_SYMBOL (sym_no_applicable_method, "no-applicable-method");
  70. SCM_SYMBOL (sym_memoize_method_x, "memoize-method!");
  71. SCM_SYMBOL (sym_change_class, "change-class");
  72. SCM_VARIABLE (scm_var_make_extended_generic, "make-extended-generic");
  73. /* Class redefinition protocol:
  74. A class is represented by a heap header h1 which points to a
  75. malloc:ed memory block m1.
  76. When a new version of a class is created, a new header h2 and
  77. memory block m2 are allocated. The headers h1 and h2 then switch
  78. pointers so that h1 refers to m2 and h2 to m1. In this way, names
  79. bound to h1 will point to the new class at the same time as h2 will
  80. be a handle which the GC will use to free m1.
  81. The `redefined' slot of m1 will be set to point to h1. An old
  82. instance will have its class pointer (the CAR of the heap header)
  83. pointing to m1. The non-immediate `redefined'-slot in m1 indicates
  84. the class modification and the new class pointer can be found via
  85. h1.
  86. */
  87. #define TEST_CHANGE_CLASS(obj, class) \
  88. { \
  89. class = SCM_CLASS_OF (obj); \
  90. if (scm_is_true (SCM_OBJ_CLASS_REDEF (obj))) \
  91. { \
  92. scm_change_object_class (obj, class, SCM_OBJ_CLASS_REDEF (obj));\
  93. class = SCM_CLASS_OF (obj); \
  94. } \
  95. }
  96. #define NXT_MTHD_METHODS(m) (SCM_VELTS (m)[1])
  97. #define NXT_MTHD_ARGS(m) (SCM_VELTS (m)[2])
  98. #define SCM_GOOPS_UNBOUND SCM_UNBOUND
  99. #define SCM_GOOPS_UNBOUNDP(x) (scm_is_eq (x, SCM_GOOPS_UNBOUND))
  100. static int goops_loaded_p = 0;
  101. static scm_t_rstate *goops_rstate;
  102. /* These variables are filled in by the object system when loaded. */
  103. SCM scm_class_boolean, scm_class_char, scm_class_pair;
  104. SCM scm_class_procedure, scm_class_string, scm_class_symbol;
  105. SCM scm_class_primitive_generic;
  106. SCM scm_class_vector, scm_class_null;
  107. SCM scm_class_integer, scm_class_real, scm_class_complex, scm_class_fraction;
  108. SCM scm_class_unknown;
  109. SCM scm_class_top, scm_class_object, scm_class_class;
  110. SCM scm_class_applicable;
  111. SCM scm_class_applicable_struct, scm_class_applicable_struct_with_setter;
  112. SCM scm_class_generic, scm_class_generic_with_setter;
  113. SCM scm_class_accessor;
  114. SCM scm_class_extended_generic, scm_class_extended_generic_with_setter;
  115. SCM scm_class_extended_accessor;
  116. SCM scm_class_method;
  117. SCM scm_class_accessor_method;
  118. SCM scm_class_procedure_class;
  119. SCM scm_class_applicable_struct_class;
  120. SCM scm_class_number, scm_class_list;
  121. SCM scm_class_keyword;
  122. SCM scm_class_port, scm_class_input_output_port;
  123. SCM scm_class_input_port, scm_class_output_port;
  124. SCM scm_class_foreign_slot;
  125. SCM scm_class_self, scm_class_protected;
  126. SCM scm_class_hidden, scm_class_opaque, scm_class_read_only;
  127. SCM scm_class_protected_hidden, scm_class_protected_opaque, scm_class_protected_read_only;
  128. SCM scm_class_scm;
  129. SCM scm_class_int, scm_class_float, scm_class_double;
  130. static SCM class_foreign;
  131. static SCM class_hashtable;
  132. static SCM class_fluid;
  133. static SCM class_dynamic_state;
  134. static SCM class_frame;
  135. static SCM class_objcode;
  136. static SCM class_vm;
  137. static SCM class_vm_cont;
  138. static SCM class_bytevector;
  139. static SCM class_uvec;
  140. static SCM class_array;
  141. static SCM class_bitvector;
  142. static SCM vtable_class_map = SCM_BOOL_F;
  143. /* Port classes. Allocate 3 times the maximum number of port types so that
  144. input ports, output ports, and in/out ports can be stored at different
  145. offsets. See `SCM_IN_PCLASS_INDEX' et al. */
  146. SCM scm_port_class[3 * SCM_I_MAX_PORT_TYPE_COUNT];
  147. /* SMOB classes. */
  148. SCM scm_smob_class[SCM_I_MAX_SMOB_TYPE_COUNT];
  149. SCM scm_no_applicable_method;
  150. SCM_SYMBOL (scm_sym_define_public, "define-public");
  151. static SCM scm_make_unbound (void);
  152. static SCM scm_unbound_p (SCM obj);
  153. static SCM scm_assert_bound (SCM value, SCM obj);
  154. static SCM scm_at_assert_bound_ref (SCM obj, SCM index);
  155. static SCM scm_sys_goops_loaded (void);
  156. static SCM scm_make_extended_class_from_symbol (SCM type_name_sym,
  157. int applicablep);
  158. SCM
  159. scm_i_define_class_for_vtable (SCM vtable)
  160. {
  161. SCM class;
  162. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  163. if (scm_is_false (vtable_class_map))
  164. vtable_class_map = scm_c_make_weak_table (0, SCM_WEAK_TABLE_KIND_KEY);
  165. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  166. if (scm_is_false (scm_struct_vtable_p (vtable)))
  167. abort ();
  168. class = scm_weak_table_refq (vtable_class_map, vtable, SCM_BOOL_F);
  169. if (scm_is_false (class))
  170. {
  171. if (SCM_UNPACK (scm_class_class))
  172. {
  173. SCM name = SCM_VTABLE_NAME (vtable);
  174. if (!scm_is_symbol (name))
  175. name = scm_string_to_symbol (scm_nullstr);
  176. class = scm_make_extended_class_from_symbol
  177. (name, SCM_VTABLE_FLAG_IS_SET (vtable, SCM_VTABLE_FLAG_APPLICABLE));
  178. }
  179. else
  180. /* `create_struct_classes' will fill this in later. */
  181. class = SCM_BOOL_F;
  182. /* Don't worry about races. This only happens when creating a
  183. vtable, which happens by definition in one thread. */
  184. scm_weak_table_putq_x (vtable_class_map, vtable, class);
  185. }
  186. return class;
  187. }
  188. /* This function is used for efficient type dispatch. */
  189. SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0,
  190. (SCM x),
  191. "Return the class of @var{x}.")
  192. #define FUNC_NAME s_scm_class_of
  193. {
  194. switch (SCM_ITAG3 (x))
  195. {
  196. case scm_tc3_int_1:
  197. case scm_tc3_int_2:
  198. return scm_class_integer;
  199. case scm_tc3_imm24:
  200. if (SCM_CHARP (x))
  201. return scm_class_char;
  202. else if (scm_is_bool (x))
  203. return scm_class_boolean;
  204. else if (scm_is_null (x))
  205. return scm_class_null;
  206. else
  207. return scm_class_unknown;
  208. case scm_tc3_cons:
  209. switch (SCM_TYP7 (x))
  210. {
  211. case scm_tcs_cons_nimcar:
  212. return scm_class_pair;
  213. case scm_tc7_symbol:
  214. return scm_class_symbol;
  215. case scm_tc7_vector:
  216. case scm_tc7_wvect:
  217. return scm_class_vector;
  218. case scm_tc7_pointer:
  219. return class_foreign;
  220. case scm_tc7_hashtable:
  221. return class_hashtable;
  222. case scm_tc7_fluid:
  223. return class_fluid;
  224. case scm_tc7_dynamic_state:
  225. return class_dynamic_state;
  226. case scm_tc7_frame:
  227. return class_frame;
  228. case scm_tc7_objcode:
  229. return class_objcode;
  230. case scm_tc7_vm:
  231. return class_vm;
  232. case scm_tc7_vm_cont:
  233. return class_vm_cont;
  234. case scm_tc7_bytevector:
  235. if (SCM_BYTEVECTOR_ELEMENT_TYPE (x) == SCM_ARRAY_ELEMENT_TYPE_VU8)
  236. return class_bytevector;
  237. else
  238. return class_uvec;
  239. case scm_tc7_array:
  240. return class_array;
  241. case scm_tc7_bitvector:
  242. return class_bitvector;
  243. case scm_tc7_string:
  244. return scm_class_string;
  245. case scm_tc7_number:
  246. switch SCM_TYP16 (x) {
  247. case scm_tc16_big:
  248. return scm_class_integer;
  249. case scm_tc16_real:
  250. return scm_class_real;
  251. case scm_tc16_complex:
  252. return scm_class_complex;
  253. case scm_tc16_fraction:
  254. return scm_class_fraction;
  255. }
  256. case scm_tc7_program:
  257. if (SCM_PROGRAM_IS_PRIMITIVE_GENERIC (x)
  258. && SCM_UNPACK (*SCM_SUBR_GENERIC (x)))
  259. return scm_class_primitive_generic;
  260. else
  261. return scm_class_procedure;
  262. case scm_tc7_rtl_program:
  263. return scm_class_procedure;
  264. case scm_tc7_smob:
  265. {
  266. scm_t_bits type = SCM_TYP16 (x);
  267. if (type != scm_tc16_port_with_ps)
  268. return scm_smob_class[SCM_TC2SMOBNUM (type)];
  269. x = SCM_PORT_WITH_PS_PORT (x);
  270. /* fall through to ports */
  271. }
  272. case scm_tc7_port:
  273. return scm_port_class[(SCM_WRTNG & SCM_CELL_WORD_0 (x)
  274. ? (SCM_RDNG & SCM_CELL_WORD_0 (x)
  275. ? SCM_INOUT_PCLASS_INDEX | SCM_PTOBNUM (x)
  276. : SCM_OUT_PCLASS_INDEX | SCM_PTOBNUM (x))
  277. : SCM_IN_PCLASS_INDEX | SCM_PTOBNUM (x))];
  278. case scm_tcs_struct:
  279. if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS_VALID)
  280. return SCM_CLASS_OF (x);
  281. else if (SCM_OBJ_CLASS_FLAGS (x) & SCM_CLASSF_GOOPS)
  282. {
  283. /* Goops object */
  284. if (! scm_is_false (SCM_OBJ_CLASS_REDEF (x)))
  285. scm_change_object_class (x,
  286. SCM_CLASS_OF (x), /* old */
  287. SCM_OBJ_CLASS_REDEF (x)); /* new */
  288. return SCM_CLASS_OF (x);
  289. }
  290. else
  291. return scm_i_define_class_for_vtable (SCM_CLASS_OF (x));
  292. default:
  293. if (scm_is_pair (x))
  294. return scm_class_pair;
  295. else
  296. return scm_class_unknown;
  297. }
  298. case scm_tc3_struct:
  299. case scm_tc3_tc7_1:
  300. case scm_tc3_tc7_2:
  301. /* case scm_tc3_unused: */
  302. /* Never reached */
  303. break;
  304. }
  305. return scm_class_unknown;
  306. }
  307. #undef FUNC_NAME
  308. /******************************************************************************
  309. *
  310. * Compute-cpl
  311. *
  312. * This version doesn't fully handle multiple-inheritance. It serves
  313. * only for booting classes and will be overloaded in Scheme
  314. *
  315. ******************************************************************************/
  316. static SCM
  317. map (SCM (*proc) (SCM), SCM ls)
  318. {
  319. if (scm_is_null (ls))
  320. return ls;
  321. else
  322. {
  323. SCM res = scm_cons (proc (SCM_CAR (ls)), SCM_EOL);
  324. SCM h = res;
  325. ls = SCM_CDR (ls);
  326. while (scm_is_pair (ls))
  327. {
  328. SCM_SETCDR (h, scm_cons (proc (SCM_CAR (ls)), SCM_EOL));
  329. h = SCM_CDR (h);
  330. ls = SCM_CDR (ls);
  331. }
  332. return res;
  333. }
  334. }
  335. static SCM
  336. filter_cpl (SCM ls)
  337. {
  338. SCM res = SCM_EOL;
  339. while (scm_is_pair (ls))
  340. {
  341. SCM el = SCM_CAR (ls);
  342. if (scm_is_false (scm_c_memq (el, res)))
  343. res = scm_cons (el, res);
  344. ls = SCM_CDR (ls);
  345. }
  346. return res;
  347. }
  348. static SCM
  349. compute_cpl (SCM class)
  350. {
  351. if (goops_loaded_p)
  352. return scm_call_1 (SCM_VARIABLE_REF (var_compute_cpl), class);
  353. else
  354. {
  355. SCM supers = SCM_SLOT (class, scm_si_direct_supers);
  356. SCM ls = scm_append (scm_acons (class, supers,
  357. map (compute_cpl, supers)));
  358. return scm_reverse_x (filter_cpl (ls), SCM_EOL);
  359. }
  360. }
  361. /******************************************************************************
  362. *
  363. * compute-slots
  364. *
  365. ******************************************************************************/
  366. static SCM
  367. remove_duplicate_slots (SCM l, SCM res, SCM slots_already_seen)
  368. {
  369. SCM tmp;
  370. if (!scm_is_pair (l))
  371. return res;
  372. tmp = SCM_CAAR (l);
  373. if (!scm_is_symbol (tmp))
  374. scm_misc_error ("%compute-slots", "bad slot name ~S", scm_list_1 (tmp));
  375. if (scm_is_false (scm_c_memq (tmp, slots_already_seen))) {
  376. res = scm_cons (SCM_CAR (l), res);
  377. slots_already_seen = scm_cons (tmp, slots_already_seen);
  378. }
  379. return remove_duplicate_slots (SCM_CDR (l), res, slots_already_seen);
  380. }
  381. static void
  382. check_cpl (SCM slots, SCM bslots)
  383. {
  384. for (; scm_is_pair (bslots); bslots = SCM_CDR (bslots))
  385. if (scm_is_true (scm_assq (SCM_CAAR (bslots), slots)))
  386. scm_misc_error ("init-object", "a predefined <class> inherited "
  387. "field cannot be redefined", SCM_EOL);
  388. }
  389. static SCM
  390. build_class_class_slots (void);
  391. static SCM
  392. build_slots_list (SCM dslots, SCM cpl)
  393. {
  394. SCM bslots, class_slots;
  395. int classp;
  396. SCM res = dslots;
  397. class_slots = SCM_EOL;
  398. classp = scm_is_true (scm_memq (scm_class_class, cpl));
  399. if (classp)
  400. {
  401. bslots = build_class_class_slots ();
  402. check_cpl (res, bslots);
  403. }
  404. else
  405. bslots = SCM_EOL;
  406. if (scm_is_pair (cpl))
  407. {
  408. for (cpl = SCM_CDR (cpl); scm_is_pair (cpl); cpl = SCM_CDR (cpl))
  409. {
  410. SCM new_slots = SCM_SLOT (SCM_CAR (cpl),
  411. scm_si_direct_slots);
  412. if (classp)
  413. {
  414. if (!scm_is_eq (SCM_CAR (cpl), scm_class_class))
  415. check_cpl (new_slots, bslots);
  416. else
  417. {
  418. /* Move class slots to the head of the list. */
  419. class_slots = new_slots;
  420. continue;
  421. }
  422. }
  423. res = scm_append (scm_list_2 (new_slots, res));
  424. }
  425. }
  426. else
  427. scm_misc_error ("%compute-slots", "malformed cpl argument in "
  428. "build_slots_list", SCM_EOL);
  429. /* make sure to add the <class> slots to the head of the list */
  430. if (classp)
  431. res = scm_append (scm_list_2 (class_slots, res));
  432. /* res contains a list of slots. Remove slots which appears more than once */
  433. return remove_duplicate_slots (scm_reverse (res), SCM_EOL, SCM_EOL);
  434. }
  435. static SCM
  436. maplist (SCM ls)
  437. {
  438. SCM orig = ls;
  439. while (!scm_is_null (ls))
  440. {
  441. if (!scm_is_pair (ls))
  442. scm_misc_error ("%compute-slots", "malformed ls argument in "
  443. "maplist", SCM_EOL);
  444. if (!scm_is_pair (SCM_CAR (ls)))
  445. SCM_SETCAR (ls, scm_cons (SCM_CAR (ls), SCM_EOL));
  446. ls = SCM_CDR (ls);
  447. }
  448. return orig;
  449. }
  450. SCM_DEFINE (scm_sys_compute_slots, "%compute-slots", 1, 0, 0,
  451. (SCM class),
  452. "Return a list consisting of the names of all slots belonging to\n"
  453. "class @var{class}, i. e. the slots of @var{class} and of all of\n"
  454. "its superclasses.")
  455. #define FUNC_NAME s_scm_sys_compute_slots
  456. {
  457. SCM_VALIDATE_CLASS (1, class);
  458. return build_slots_list (SCM_SLOT (class, scm_si_direct_slots),
  459. SCM_SLOT (class, scm_si_cpl));
  460. }
  461. #undef FUNC_NAME
  462. /******************************************************************************
  463. *
  464. * compute-getters-n-setters
  465. *
  466. * This version doesn't handle slot options. It serves only for booting
  467. * classes and will be overloaded in Scheme.
  468. *
  469. ******************************************************************************/
  470. SCM_KEYWORD (k_init_value, "init-value");
  471. SCM_KEYWORD (k_init_thunk, "init-thunk");
  472. static SCM
  473. compute_getters_n_setters (SCM slots)
  474. {
  475. SCM res = SCM_EOL;
  476. SCM *cdrloc = &res;
  477. long i = 0;
  478. for ( ; !scm_is_null (slots); slots = SCM_CDR (slots))
  479. {
  480. SCM init = SCM_BOOL_F;
  481. SCM options = SCM_CDAR (slots);
  482. if (!scm_is_null (options))
  483. {
  484. init = scm_get_keyword (k_init_value, options, SCM_PACK (0));
  485. if (SCM_UNPACK (init))
  486. {
  487. init = scm_primitive_eval (scm_list_3 (scm_sym_lambda,
  488. SCM_EOL,
  489. scm_list_2 (scm_sym_quote,
  490. init)));
  491. }
  492. else
  493. init = scm_get_keyword (k_init_thunk, options, SCM_BOOL_F);
  494. }
  495. *cdrloc = scm_cons (scm_cons (SCM_CAAR (slots),
  496. scm_cons (init,
  497. scm_from_int (i++))),
  498. SCM_EOL);
  499. cdrloc = SCM_CDRLOC (*cdrloc);
  500. }
  501. return res;
  502. }
  503. /******************************************************************************
  504. *
  505. * initialize-object
  506. *
  507. ******************************************************************************/
  508. /*fixme* Manufacture keywords in advance */
  509. SCM
  510. scm_i_get_keyword (SCM key, SCM l, long len, SCM default_value, const char *subr)
  511. {
  512. long i;
  513. for (i = 0; i != len; i += 2)
  514. {
  515. SCM obj = SCM_CAR (l);
  516. if (!scm_is_keyword (obj))
  517. scm_misc_error (subr, "bad keyword: ~S", scm_list_1 (obj));
  518. else if (scm_is_eq (obj, key))
  519. return SCM_CADR (l);
  520. else
  521. l = SCM_CDDR (l);
  522. }
  523. return default_value;
  524. }
  525. SCM_DEFINE (scm_get_keyword, "get-keyword", 3, 0, 0,
  526. (SCM key, SCM l, SCM default_value),
  527. "Determine an associated value for the keyword @var{key} from\n"
  528. "the list @var{l}. The list @var{l} has to consist of an even\n"
  529. "number of elements, where, starting with the first, every\n"
  530. "second element is a keyword, followed by its associated value.\n"
  531. "If @var{l} does not hold a value for @var{key}, the value\n"
  532. "@var{default_value} is returned.")
  533. #define FUNC_NAME s_scm_get_keyword
  534. {
  535. long len;
  536. SCM_ASSERT (scm_is_keyword (key), key, SCM_ARG1, FUNC_NAME);
  537. len = scm_ilength (l);
  538. if (len < 0 || len % 2 == 1)
  539. scm_misc_error (FUNC_NAME, "Bad keyword-value list: ~S", scm_list_1 (l));
  540. return scm_i_get_keyword (key, l, len, default_value, FUNC_NAME);
  541. }
  542. #undef FUNC_NAME
  543. SCM_KEYWORD (k_init_keyword, "init-keyword");
  544. static SCM get_slot_value (SCM class, SCM obj, SCM slotdef);
  545. static SCM set_slot_value (SCM class, SCM obj, SCM slotdef, SCM value);
  546. SCM_DEFINE (scm_sys_initialize_object, "%initialize-object", 2, 0, 0,
  547. (SCM obj, SCM initargs),
  548. "Initialize the object @var{obj} with the given arguments\n"
  549. "@var{initargs}.")
  550. #define FUNC_NAME s_scm_sys_initialize_object
  551. {
  552. SCM tmp, get_n_set, slots;
  553. SCM class = SCM_CLASS_OF (obj);
  554. long n_initargs;
  555. SCM_VALIDATE_INSTANCE (1, obj);
  556. n_initargs = scm_ilength (initargs);
  557. SCM_ASSERT ((n_initargs & 1) == 0, initargs, SCM_ARG2, FUNC_NAME);
  558. get_n_set = SCM_SLOT (class, scm_si_getters_n_setters);
  559. slots = SCM_SLOT (class, scm_si_slots);
  560. /* See for each slot how it must be initialized */
  561. for (;
  562. !scm_is_null (slots);
  563. get_n_set = SCM_CDR (get_n_set), slots = SCM_CDR (slots))
  564. {
  565. SCM slot_name = SCM_CAR (slots);
  566. SCM slot_value = SCM_PACK (0);
  567. if (!scm_is_null (SCM_CDR (slot_name)))
  568. {
  569. /* This slot admits (perhaps) to be initialized at creation time */
  570. long n = scm_ilength (SCM_CDR (slot_name));
  571. if (n & 1) /* odd or -1 */
  572. SCM_MISC_ERROR ("class contains bogus slot definition: ~S",
  573. scm_list_1 (slot_name));
  574. tmp = scm_i_get_keyword (k_init_keyword,
  575. SCM_CDR (slot_name),
  576. n,
  577. SCM_PACK (0),
  578. FUNC_NAME);
  579. slot_name = SCM_CAR (slot_name);
  580. if (SCM_UNPACK (tmp))
  581. {
  582. /* an initarg was provided for this slot */
  583. if (!scm_is_keyword (tmp))
  584. SCM_MISC_ERROR ("initarg must be a keyword. It was ~S",
  585. scm_list_1 (tmp));
  586. slot_value = scm_i_get_keyword (tmp,
  587. initargs,
  588. n_initargs,
  589. SCM_PACK (0),
  590. FUNC_NAME);
  591. }
  592. }
  593. if (SCM_UNPACK (slot_value))
  594. /* set slot to provided value */
  595. set_slot_value (class, obj, SCM_CAR (get_n_set), slot_value);
  596. else
  597. {
  598. /* set slot to its :init-form if it exists */
  599. tmp = SCM_CADAR (get_n_set);
  600. if (scm_is_true (tmp))
  601. {
  602. slot_value = get_slot_value (class, obj, SCM_CAR (get_n_set));
  603. if (SCM_GOOPS_UNBOUNDP (slot_value))
  604. set_slot_value (class,
  605. obj,
  606. SCM_CAR (get_n_set),
  607. scm_call_0 (tmp));
  608. }
  609. }
  610. }
  611. return obj;
  612. }
  613. #undef FUNC_NAME
  614. /* NOTE: The following macros are interdependent with code
  615. * in goops.scm:compute-getters-n-setters
  616. */
  617. #define SCM_GNS_INSTANCE_ALLOCATED_P(gns) \
  618. (SCM_I_INUMP (SCM_CDDR (gns)) \
  619. || (scm_is_pair (SCM_CDDR (gns)) \
  620. && scm_is_pair (SCM_CDDDR (gns)) \
  621. && scm_is_pair (SCM_CDDDDR (gns))))
  622. #define SCM_GNS_INDEX(gns) \
  623. (SCM_I_INUMP (SCM_CDDR (gns)) \
  624. ? SCM_I_INUM (SCM_CDDR (gns)) \
  625. : scm_to_long (SCM_CAR (SCM_CDDDDR (gns))))
  626. #define SCM_GNS_SIZE(gns) \
  627. (SCM_I_INUMP (SCM_CDDR (gns)) \
  628. ? 1 \
  629. : scm_to_long (SCM_CADR (SCM_CDDDDR (gns))))
  630. SCM_KEYWORD (k_class, "class");
  631. SCM_KEYWORD (k_allocation, "allocation");
  632. SCM_KEYWORD (k_instance, "instance");
  633. SCM_DEFINE (scm_sys_prep_layout_x, "%prep-layout!", 1, 0, 0,
  634. (SCM class),
  635. "")
  636. #define FUNC_NAME s_scm_sys_prep_layout_x
  637. {
  638. SCM slots, getters_n_setters, nfields;
  639. unsigned long int n, i;
  640. char *s;
  641. SCM layout;
  642. SCM_VALIDATE_INSTANCE (1, class);
  643. slots = SCM_SLOT (class, scm_si_slots);
  644. getters_n_setters = SCM_SLOT (class, scm_si_getters_n_setters);
  645. nfields = SCM_SLOT (class, scm_si_nfields);
  646. if (!SCM_I_INUMP (nfields) || SCM_I_INUM (nfields) < 0)
  647. SCM_MISC_ERROR ("bad value in nfields slot: ~S",
  648. scm_list_1 (nfields));
  649. n = 2 * SCM_I_INUM (nfields);
  650. if (n < sizeof (SCM_CLASS_CLASS_LAYOUT) - 1
  651. && SCM_SUBCLASSP (class, scm_class_class))
  652. SCM_MISC_ERROR ("class object doesn't have enough fields: ~S",
  653. scm_list_1 (nfields));
  654. layout = scm_i_make_string (n, &s, 0);
  655. i = 0;
  656. while (scm_is_pair (getters_n_setters))
  657. {
  658. if (SCM_GNS_INSTANCE_ALLOCATED_P (SCM_CAR (getters_n_setters)))
  659. {
  660. SCM type;
  661. int len, index, size;
  662. char p, a;
  663. if (i >= n || !scm_is_pair (slots))
  664. goto inconsistent;
  665. /* extract slot type */
  666. len = scm_ilength (SCM_CDAR (slots));
  667. type = scm_i_get_keyword (k_class, SCM_CDAR (slots),
  668. len, SCM_BOOL_F, FUNC_NAME);
  669. /* determine slot GC protection and access mode */
  670. if (scm_is_false (type))
  671. {
  672. p = 'p';
  673. a = 'w';
  674. }
  675. else
  676. {
  677. if (!SCM_CLASSP (type))
  678. SCM_MISC_ERROR ("bad slot class", SCM_EOL);
  679. else if (SCM_SUBCLASSP (type, scm_class_foreign_slot))
  680. {
  681. if (SCM_SUBCLASSP (type, scm_class_self))
  682. p = 's';
  683. else if (SCM_SUBCLASSP (type, scm_class_protected))
  684. p = 'p';
  685. else
  686. p = 'u';
  687. if (SCM_SUBCLASSP (type, scm_class_opaque))
  688. a = 'o';
  689. else if (SCM_SUBCLASSP (type, scm_class_read_only))
  690. a = 'r';
  691. else if (SCM_SUBCLASSP (type, scm_class_hidden))
  692. a = 'h';
  693. else
  694. a = 'w';
  695. }
  696. else
  697. {
  698. p = 'p';
  699. a = 'w';
  700. }
  701. }
  702. index = SCM_GNS_INDEX (SCM_CAR (getters_n_setters));
  703. if (index != (i >> 1))
  704. goto inconsistent;
  705. size = SCM_GNS_SIZE (SCM_CAR (getters_n_setters));
  706. while (size)
  707. {
  708. s[i++] = p;
  709. s[i++] = a;
  710. --size;
  711. }
  712. }
  713. slots = SCM_CDR (slots);
  714. getters_n_setters = SCM_CDR (getters_n_setters);
  715. }
  716. if (!scm_is_null (slots))
  717. {
  718. inconsistent:
  719. SCM_MISC_ERROR ("inconsistent getters-n-setters", SCM_EOL);
  720. }
  721. SCM_SET_VTABLE_LAYOUT (class, scm_string_to_symbol (layout));
  722. return SCM_UNSPECIFIED;
  723. }
  724. #undef FUNC_NAME
  725. static void prep_hashsets (SCM);
  726. SCM_DEFINE (scm_sys_inherit_magic_x, "%inherit-magic!", 2, 0, 0,
  727. (SCM class, SCM dsupers),
  728. "")
  729. #define FUNC_NAME s_scm_sys_inherit_magic_x
  730. {
  731. SCM_VALIDATE_INSTANCE (1, class);
  732. scm_i_struct_inherit_vtable_magic (SCM_CLASS_OF (class), class);
  733. SCM_SET_CLASS_FLAGS (class, SCM_CLASSF_GOOPS_OR_VALID);
  734. prep_hashsets (class);
  735. return SCM_UNSPECIFIED;
  736. }
  737. #undef FUNC_NAME
  738. static void
  739. prep_hashsets (SCM class)
  740. {
  741. unsigned int i;
  742. for (i = 0; i < 8; ++i)
  743. SCM_SET_HASHSET (class, i, scm_c_uniform32 (goops_rstate));
  744. }
  745. /******************************************************************************/
  746. SCM
  747. scm_basic_basic_make_class (SCM class, SCM name, SCM dsupers, SCM dslots)
  748. {
  749. SCM z, cpl, slots, nfields, g_n_s;
  750. /* Allocate one instance */
  751. z = scm_make_struct (class, SCM_INUM0, SCM_EOL);
  752. /* Initialize its slots */
  753. SCM_SET_SLOT (z, scm_si_direct_supers, dsupers);
  754. cpl = compute_cpl (z);
  755. slots = build_slots_list (maplist (dslots), cpl);
  756. nfields = scm_from_int (scm_ilength (slots));
  757. g_n_s = compute_getters_n_setters (slots);
  758. SCM_SET_SLOT (z, scm_vtable_index_name, name);
  759. SCM_SET_SLOT (z, scm_si_direct_slots, dslots);
  760. SCM_SET_SLOT (z, scm_si_direct_subclasses, SCM_EOL);
  761. SCM_SET_SLOT (z, scm_si_direct_methods, SCM_EOL);
  762. SCM_SET_SLOT (z, scm_si_cpl, cpl);
  763. SCM_SET_SLOT (z, scm_si_slots, slots);
  764. SCM_SET_SLOT (z, scm_si_nfields, nfields);
  765. SCM_SET_SLOT (z, scm_si_getters_n_setters, g_n_s);
  766. SCM_SET_SLOT (z, scm_si_redefined, SCM_BOOL_F);
  767. /* Add this class in the direct-subclasses slot of dsupers */
  768. {
  769. SCM tmp;
  770. for (tmp = dsupers; !scm_is_null (tmp); tmp = SCM_CDR (tmp))
  771. SCM_SET_SLOT (SCM_CAR (tmp), scm_si_direct_subclasses,
  772. scm_cons (z, SCM_SLOT (SCM_CAR (tmp),
  773. scm_si_direct_subclasses)));
  774. }
  775. return z;
  776. }
  777. SCM
  778. scm_basic_make_class (SCM class, SCM name, SCM dsupers, SCM dslots)
  779. {
  780. SCM z = scm_basic_basic_make_class (class, name, dsupers, dslots);
  781. scm_sys_prep_layout_x (z);
  782. scm_sys_inherit_magic_x (z, dsupers);
  783. return z;
  784. }
  785. /******************************************************************************/
  786. SCM_SYMBOL (sym_layout, "layout");
  787. SCM_SYMBOL (sym_flags, "flags");
  788. SCM_SYMBOL (sym_self, "%self");
  789. SCM_SYMBOL (sym_instance_finalizer, "instance-finalizer");
  790. SCM_SYMBOL (sym_reserved_0, "%reserved-0");
  791. SCM_SYMBOL (sym_reserved_1, "%reserved-1");
  792. SCM_SYMBOL (sym_print, "print");
  793. SCM_SYMBOL (sym_procedure, "procedure");
  794. SCM_SYMBOL (sym_setter, "setter");
  795. SCM_SYMBOL (sym_redefined, "redefined");
  796. SCM_SYMBOL (sym_h0, "h0");
  797. SCM_SYMBOL (sym_h1, "h1");
  798. SCM_SYMBOL (sym_h2, "h2");
  799. SCM_SYMBOL (sym_h3, "h3");
  800. SCM_SYMBOL (sym_h4, "h4");
  801. SCM_SYMBOL (sym_h5, "h5");
  802. SCM_SYMBOL (sym_h6, "h6");
  803. SCM_SYMBOL (sym_h7, "h7");
  804. SCM_SYMBOL (sym_name, "name");
  805. SCM_SYMBOL (sym_direct_supers, "direct-supers");
  806. SCM_SYMBOL (sym_direct_slots, "direct-slots");
  807. SCM_SYMBOL (sym_direct_subclasses, "direct-subclasses");
  808. SCM_SYMBOL (sym_direct_methods, "direct-methods");
  809. SCM_SYMBOL (sym_cpl, "cpl");
  810. SCM_SYMBOL (sym_default_slot_definition_class, "default-slot-definition-class");
  811. SCM_SYMBOL (sym_slots, "slots");
  812. SCM_SYMBOL (sym_getters_n_setters, "getters-n-setters");
  813. SCM_SYMBOL (sym_nfields, "nfields");
  814. static SCM
  815. build_class_class_slots (void)
  816. {
  817. /* has to be kept in sync with SCM_VTABLE_BASE_LAYOUT and
  818. SCM_CLASS_CLASS_LAYOUT */
  819. return scm_list_n (
  820. scm_list_3 (sym_layout, k_class, scm_class_protected_read_only),
  821. scm_list_3 (sym_flags, k_class, scm_class_hidden),
  822. scm_list_3 (sym_self, k_class, scm_class_self),
  823. scm_list_3 (sym_instance_finalizer, k_class, scm_class_hidden),
  824. scm_list_1 (sym_print),
  825. scm_list_3 (sym_name, k_class, scm_class_protected_hidden),
  826. scm_list_3 (sym_reserved_0, k_class, scm_class_hidden),
  827. scm_list_3 (sym_reserved_1, k_class, scm_class_hidden),
  828. scm_list_1 (sym_redefined),
  829. scm_list_3 (sym_h0, k_class, scm_class_int),
  830. scm_list_3 (sym_h1, k_class, scm_class_int),
  831. scm_list_3 (sym_h2, k_class, scm_class_int),
  832. scm_list_3 (sym_h3, k_class, scm_class_int),
  833. scm_list_3 (sym_h4, k_class, scm_class_int),
  834. scm_list_3 (sym_h5, k_class, scm_class_int),
  835. scm_list_3 (sym_h6, k_class, scm_class_int),
  836. scm_list_3 (sym_h7, k_class, scm_class_int),
  837. scm_list_1 (sym_direct_supers),
  838. scm_list_1 (sym_direct_slots),
  839. scm_list_1 (sym_direct_subclasses),
  840. scm_list_1 (sym_direct_methods),
  841. scm_list_1 (sym_cpl),
  842. scm_list_1 (sym_default_slot_definition_class),
  843. scm_list_1 (sym_slots),
  844. scm_list_1 (sym_getters_n_setters),
  845. scm_list_1 (sym_nfields),
  846. SCM_UNDEFINED);
  847. }
  848. static void
  849. create_basic_classes (void)
  850. {
  851. /* SCM slots_of_class = build_class_class_slots (); */
  852. /**** <class> ****/
  853. SCM cs = scm_from_locale_string (SCM_CLASS_CLASS_LAYOUT);
  854. SCM name = scm_from_latin1_symbol ("<class>");
  855. scm_class_class = scm_i_make_vtable_vtable (cs);
  856. SCM_SET_CLASS_FLAGS (scm_class_class, (SCM_CLASSF_GOOPS_OR_VALID
  857. | SCM_CLASSF_METACLASS));
  858. SCM_SET_SLOT (scm_class_class, scm_vtable_index_name, name);
  859. SCM_SET_SLOT (scm_class_class, scm_si_direct_supers, SCM_EOL); /* will be changed */
  860. /* SCM_SET_SLOT (scm_class_class, scm_si_direct_slots, slots_of_class); */
  861. SCM_SET_SLOT (scm_class_class, scm_si_direct_subclasses, SCM_EOL);
  862. SCM_SET_SLOT (scm_class_class, scm_si_direct_methods, SCM_EOL);
  863. SCM_SET_SLOT (scm_class_class, scm_si_cpl, SCM_EOL); /* will be changed */
  864. /* SCM_SET_SLOT (scm_class_class, scm_si_slots, slots_of_class); */
  865. SCM_SET_SLOT (scm_class_class, scm_si_nfields, scm_from_int (SCM_N_CLASS_SLOTS));
  866. /* SCM_SET_SLOT (scm_class_class, scm_si_getters_n_setters,
  867. compute_getters_n_setters (slots_of_class)); */
  868. SCM_SET_SLOT (scm_class_class, scm_si_redefined, SCM_BOOL_F);
  869. prep_hashsets (scm_class_class);
  870. scm_module_define (scm_module_goops, name, scm_class_class);
  871. /**** <top> ****/
  872. name = scm_from_latin1_symbol ("<top>");
  873. scm_class_top = scm_basic_make_class (scm_class_class, name,
  874. SCM_EOL, SCM_EOL);
  875. scm_module_define (scm_module_goops, name, scm_class_top);
  876. /**** <object> ****/
  877. name = scm_from_latin1_symbol ("<object>");
  878. scm_class_object = scm_basic_make_class (scm_class_class, name,
  879. scm_list_1 (scm_class_top), SCM_EOL);
  880. scm_module_define (scm_module_goops, name, scm_class_object);
  881. /* <top> <object> and <class> were partially initialized. Correct them here */
  882. SCM_SET_SLOT (scm_class_object, scm_si_direct_subclasses, scm_list_1 (scm_class_class));
  883. SCM_SET_SLOT (scm_class_class, scm_si_direct_supers, scm_list_1 (scm_class_object));
  884. SCM_SET_SLOT (scm_class_class, scm_si_cpl, scm_list_3 (scm_class_class, scm_class_object, scm_class_top));
  885. }
  886. /******************************************************************************/
  887. SCM_DEFINE (scm_instance_p, "instance?", 1, 0, 0,
  888. (SCM obj),
  889. "Return @code{#t} if @var{obj} is an instance.")
  890. #define FUNC_NAME s_scm_instance_p
  891. {
  892. return scm_from_bool (SCM_INSTANCEP (obj));
  893. }
  894. #undef FUNC_NAME
  895. /******************************************************************************
  896. *
  897. * Meta object accessors
  898. *
  899. ******************************************************************************/
  900. SCM_DEFINE (scm_class_name, "class-name", 1, 0, 0,
  901. (SCM obj),
  902. "Return the class name of @var{obj}.")
  903. #define FUNC_NAME s_scm_class_name
  904. {
  905. SCM_VALIDATE_CLASS (1, obj);
  906. return scm_slot_ref (obj, sym_name);
  907. }
  908. #undef FUNC_NAME
  909. SCM_DEFINE (scm_class_direct_supers, "class-direct-supers", 1, 0, 0,
  910. (SCM obj),
  911. "Return the direct superclasses of the class @var{obj}.")
  912. #define FUNC_NAME s_scm_class_direct_supers
  913. {
  914. SCM_VALIDATE_CLASS (1, obj);
  915. return scm_slot_ref (obj, sym_direct_supers);
  916. }
  917. #undef FUNC_NAME
  918. SCM_DEFINE (scm_class_direct_slots, "class-direct-slots", 1, 0, 0,
  919. (SCM obj),
  920. "Return the direct slots of the class @var{obj}.")
  921. #define FUNC_NAME s_scm_class_direct_slots
  922. {
  923. SCM_VALIDATE_CLASS (1, obj);
  924. return scm_slot_ref (obj, sym_direct_slots);
  925. }
  926. #undef FUNC_NAME
  927. SCM_DEFINE (scm_class_direct_subclasses, "class-direct-subclasses", 1, 0, 0,
  928. (SCM obj),
  929. "Return the direct subclasses of the class @var{obj}.")
  930. #define FUNC_NAME s_scm_class_direct_subclasses
  931. {
  932. SCM_VALIDATE_CLASS (1, obj);
  933. return scm_slot_ref(obj, sym_direct_subclasses);
  934. }
  935. #undef FUNC_NAME
  936. SCM_DEFINE (scm_class_direct_methods, "class-direct-methods", 1, 0, 0,
  937. (SCM obj),
  938. "Return the direct methods of the class @var{obj}")
  939. #define FUNC_NAME s_scm_class_direct_methods
  940. {
  941. SCM_VALIDATE_CLASS (1, obj);
  942. return scm_slot_ref (obj, sym_direct_methods);
  943. }
  944. #undef FUNC_NAME
  945. SCM_DEFINE (scm_class_precedence_list, "class-precedence-list", 1, 0, 0,
  946. (SCM obj),
  947. "Return the class precedence list of the class @var{obj}.")
  948. #define FUNC_NAME s_scm_class_precedence_list
  949. {
  950. SCM_VALIDATE_CLASS (1, obj);
  951. return scm_slot_ref (obj, sym_cpl);
  952. }
  953. #undef FUNC_NAME
  954. SCM_DEFINE (scm_class_slots, "class-slots", 1, 0, 0,
  955. (SCM obj),
  956. "Return the slot list of the class @var{obj}.")
  957. #define FUNC_NAME s_scm_class_slots
  958. {
  959. SCM_VALIDATE_CLASS (1, obj);
  960. return scm_slot_ref (obj, sym_slots);
  961. }
  962. #undef FUNC_NAME
  963. SCM_DEFINE (scm_generic_function_name, "generic-function-name", 1, 0, 0,
  964. (SCM obj),
  965. "Return the name of the generic function @var{obj}.")
  966. #define FUNC_NAME s_scm_generic_function_name
  967. {
  968. SCM_VALIDATE_GENERIC (1, obj);
  969. return scm_procedure_property (obj, scm_sym_name);
  970. }
  971. #undef FUNC_NAME
  972. SCM_SYMBOL (sym_methods, "methods");
  973. SCM_SYMBOL (sym_extended_by, "extended-by");
  974. SCM_SYMBOL (sym_extends, "extends");
  975. static
  976. SCM fold_downward_gf_methods (SCM method_lists, SCM gf)
  977. {
  978. SCM gfs = scm_slot_ref (gf, sym_extended_by);
  979. method_lists = scm_cons (scm_slot_ref (gf, sym_methods), method_lists);
  980. while (!scm_is_null (gfs))
  981. {
  982. method_lists = fold_downward_gf_methods (method_lists, SCM_CAR (gfs));
  983. gfs = SCM_CDR (gfs);
  984. }
  985. return method_lists;
  986. }
  987. static
  988. SCM fold_upward_gf_methods (SCM method_lists, SCM gf)
  989. {
  990. if (SCM_IS_A_P (gf, scm_class_extended_generic))
  991. {
  992. SCM gfs = scm_slot_ref (gf, sym_extends);
  993. while (!scm_is_null (gfs))
  994. {
  995. SCM methods = scm_slot_ref (SCM_CAR (gfs), sym_methods);
  996. method_lists = fold_upward_gf_methods (scm_cons (methods,
  997. method_lists),
  998. SCM_CAR (gfs));
  999. gfs = SCM_CDR (gfs);
  1000. }
  1001. }
  1002. return method_lists;
  1003. }
  1004. SCM_DEFINE (scm_generic_function_methods, "generic-function-methods", 1, 0, 0,
  1005. (SCM obj),
  1006. "Return the methods of the generic function @var{obj}.")
  1007. #define FUNC_NAME s_scm_generic_function_methods
  1008. {
  1009. SCM methods;
  1010. SCM_VALIDATE_GENERIC (1, obj);
  1011. methods = fold_upward_gf_methods (SCM_EOL, obj);
  1012. methods = fold_downward_gf_methods (methods, obj);
  1013. return scm_append (methods);
  1014. }
  1015. #undef FUNC_NAME
  1016. SCM_DEFINE (scm_method_generic_function, "method-generic-function", 1, 0, 0,
  1017. (SCM obj),
  1018. "Return the generic function for the method @var{obj}.")
  1019. #define FUNC_NAME s_scm_method_generic_function
  1020. {
  1021. SCM_VALIDATE_METHOD (1, obj);
  1022. return scm_slot_ref (obj, scm_from_latin1_symbol ("generic-function"));
  1023. }
  1024. #undef FUNC_NAME
  1025. SCM_DEFINE (scm_method_specializers, "method-specializers", 1, 0, 0,
  1026. (SCM obj),
  1027. "Return specializers of the method @var{obj}.")
  1028. #define FUNC_NAME s_scm_method_specializers
  1029. {
  1030. SCM_VALIDATE_METHOD (1, obj);
  1031. return scm_slot_ref (obj, scm_from_latin1_symbol ("specializers"));
  1032. }
  1033. #undef FUNC_NAME
  1034. SCM_DEFINE (scm_method_procedure, "method-procedure", 1, 0, 0,
  1035. (SCM obj),
  1036. "Return the procedure of the method @var{obj}.")
  1037. #define FUNC_NAME s_scm_method_procedure
  1038. {
  1039. SCM_VALIDATE_METHOD (1, obj);
  1040. return scm_slot_ref (obj, sym_procedure);
  1041. }
  1042. #undef FUNC_NAME
  1043. /******************************************************************************
  1044. *
  1045. * S l o t a c c e s s
  1046. *
  1047. ******************************************************************************/
  1048. SCM_DEFINE (scm_make_unbound, "make-unbound", 0, 0, 0,
  1049. (),
  1050. "Return the unbound value.")
  1051. #define FUNC_NAME s_scm_make_unbound
  1052. {
  1053. return SCM_GOOPS_UNBOUND;
  1054. }
  1055. #undef FUNC_NAME
  1056. SCM_DEFINE (scm_unbound_p, "unbound?", 1, 0, 0,
  1057. (SCM obj),
  1058. "Return @code{#t} if @var{obj} is unbound.")
  1059. #define FUNC_NAME s_scm_unbound_p
  1060. {
  1061. return SCM_GOOPS_UNBOUNDP (obj) ? SCM_BOOL_T : SCM_BOOL_F;
  1062. }
  1063. #undef FUNC_NAME
  1064. SCM_DEFINE (scm_assert_bound, "assert-bound", 2, 0, 0,
  1065. (SCM value, SCM obj),
  1066. "Return @var{value} if it is bound, and invoke the\n"
  1067. "@var{slot-unbound} method of @var{obj} if it is not.")
  1068. #define FUNC_NAME s_scm_assert_bound
  1069. {
  1070. if (SCM_GOOPS_UNBOUNDP (value))
  1071. return scm_call_1 (SCM_VARIABLE_REF (var_slot_unbound), obj);
  1072. return value;
  1073. }
  1074. #undef FUNC_NAME
  1075. SCM_DEFINE (scm_at_assert_bound_ref, "@assert-bound-ref", 2, 0, 0,
  1076. (SCM obj, SCM index),
  1077. "Like @code{assert-bound}, but use @var{index} for accessing\n"
  1078. "the value from @var{obj}.")
  1079. #define FUNC_NAME s_scm_at_assert_bound_ref
  1080. {
  1081. SCM value = SCM_SLOT (obj, scm_to_int (index));
  1082. if (SCM_GOOPS_UNBOUNDP (value))
  1083. return scm_call_1 (SCM_VARIABLE_REF (var_slot_unbound), obj);
  1084. return value;
  1085. }
  1086. #undef FUNC_NAME
  1087. SCM_DEFINE (scm_sys_fast_slot_ref, "%fast-slot-ref", 2, 0, 0,
  1088. (SCM obj, SCM index),
  1089. "Return the slot value with index @var{index} from @var{obj}.")
  1090. #define FUNC_NAME s_scm_sys_fast_slot_ref
  1091. {
  1092. scm_t_bits i;
  1093. SCM_VALIDATE_INSTANCE (1, obj);
  1094. i = scm_to_unsigned_integer (index, 0,
  1095. SCM_I_INUM (SCM_SLOT (SCM_CLASS_OF (obj),
  1096. scm_si_nfields))
  1097. - 1);
  1098. return SCM_SLOT (obj, i);
  1099. }
  1100. #undef FUNC_NAME
  1101. SCM_DEFINE (scm_sys_fast_slot_set_x, "%fast-slot-set!", 3, 0, 0,
  1102. (SCM obj, SCM index, SCM value),
  1103. "Set the slot with index @var{index} in @var{obj} to\n"
  1104. "@var{value}.")
  1105. #define FUNC_NAME s_scm_sys_fast_slot_set_x
  1106. {
  1107. scm_t_bits i;
  1108. SCM_VALIDATE_INSTANCE (1, obj);
  1109. i = scm_to_unsigned_integer (index, 0,
  1110. SCM_I_INUM (SCM_SLOT (SCM_CLASS_OF (obj),
  1111. scm_si_nfields))
  1112. - 1);
  1113. SCM_SET_SLOT (obj, i, value);
  1114. return SCM_UNSPECIFIED;
  1115. }
  1116. #undef FUNC_NAME
  1117. /** Utilities **/
  1118. /* In the future, this function will return the effective slot
  1119. * definition associated with SLOT_NAME. Now it just returns some of
  1120. * the information which will be stored in the effective slot
  1121. * definition.
  1122. */
  1123. static SCM
  1124. slot_definition_using_name (SCM class, SCM slot_name)
  1125. {
  1126. register SCM slots = SCM_SLOT (class, scm_si_getters_n_setters);
  1127. for (; !scm_is_null (slots); slots = SCM_CDR (slots))
  1128. if (scm_is_eq (SCM_CAAR (slots), slot_name))
  1129. return SCM_CAR (slots);
  1130. return SCM_BOOL_F;
  1131. }
  1132. static SCM
  1133. get_slot_value (SCM class SCM_UNUSED, SCM obj, SCM slotdef)
  1134. #define FUNC_NAME "%get-slot-value"
  1135. {
  1136. SCM access = SCM_CDDR (slotdef);
  1137. /* Two cases here:
  1138. * - access is an integer (the offset of this slot in the slots vector)
  1139. * - otherwise (car access) is the getter function to apply
  1140. *
  1141. * Instances have never more than SCM_MOST_POSITIVE_FIXNUM slots, so
  1142. * we can just assume fixnums here.
  1143. */
  1144. if (SCM_I_INUMP (access))
  1145. /* Don't poke at the slots directly, because scm_struct_ref handles the
  1146. access bits for us. */
  1147. return scm_struct_ref (obj, access);
  1148. else
  1149. return scm_call_1 (SCM_CAR (access), obj);
  1150. }
  1151. #undef FUNC_NAME
  1152. static SCM
  1153. get_slot_value_using_name (SCM class, SCM obj, SCM slot_name)
  1154. {
  1155. SCM slotdef = slot_definition_using_name (class, slot_name);
  1156. if (scm_is_true (slotdef))
  1157. return get_slot_value (class, obj, slotdef);
  1158. else
  1159. return scm_call_3 (SCM_VARIABLE_REF (var_slot_missing), class, obj, slot_name);
  1160. }
  1161. static SCM
  1162. set_slot_value (SCM class SCM_UNUSED, SCM obj, SCM slotdef, SCM value)
  1163. #define FUNC_NAME "%set-slot-value"
  1164. {
  1165. SCM access = SCM_CDDR (slotdef);
  1166. /* Two cases here:
  1167. * - access is an integer (the offset of this slot in the slots vector)
  1168. * - otherwise (cadr access) is the setter function to apply
  1169. *
  1170. * Instances have never more than SCM_MOST_POSITIVE_FIXNUM slots, so
  1171. * we can just assume fixnums here.
  1172. */
  1173. if (SCM_I_INUMP (access))
  1174. /* obey permissions bits via going through struct-set! */
  1175. scm_struct_set_x (obj, access, value);
  1176. else
  1177. /* ((cadr l) obj value) */
  1178. scm_call_2 (SCM_CADR (access), obj, value);
  1179. return SCM_UNSPECIFIED;
  1180. }
  1181. #undef FUNC_NAME
  1182. static SCM
  1183. set_slot_value_using_name (SCM class, SCM obj, SCM slot_name, SCM value)
  1184. {
  1185. SCM slotdef = slot_definition_using_name (class, slot_name);
  1186. if (scm_is_true (slotdef))
  1187. return set_slot_value (class, obj, slotdef, value);
  1188. else
  1189. return scm_call_4 (SCM_VARIABLE_REF (var_slot_missing), class, obj, slot_name, value);
  1190. }
  1191. static SCM
  1192. test_slot_existence (SCM class SCM_UNUSED, SCM obj, SCM slot_name)
  1193. {
  1194. register SCM l;
  1195. for (l = SCM_ACCESSORS_OF (obj); !scm_is_null (l); l = SCM_CDR (l))
  1196. if (scm_is_eq (SCM_CAAR (l), slot_name))
  1197. return SCM_BOOL_T;
  1198. return SCM_BOOL_F;
  1199. }
  1200. /* ======================================== */
  1201. SCM_DEFINE (scm_slot_ref_using_class, "slot-ref-using-class", 3, 0, 0,
  1202. (SCM class, SCM obj, SCM slot_name),
  1203. "")
  1204. #define FUNC_NAME s_scm_slot_ref_using_class
  1205. {
  1206. SCM res;
  1207. SCM_VALIDATE_CLASS (1, class);
  1208. SCM_VALIDATE_INSTANCE (2, obj);
  1209. SCM_VALIDATE_SYMBOL (3, slot_name);
  1210. res = get_slot_value_using_name (class, obj, slot_name);
  1211. if (SCM_GOOPS_UNBOUNDP (res))
  1212. return scm_call_3 (SCM_VARIABLE_REF (var_slot_unbound), class, obj, slot_name);
  1213. return res;
  1214. }
  1215. #undef FUNC_NAME
  1216. SCM_DEFINE (scm_slot_set_using_class_x, "slot-set-using-class!", 4, 0, 0,
  1217. (SCM class, SCM obj, SCM slot_name, SCM value),
  1218. "")
  1219. #define FUNC_NAME s_scm_slot_set_using_class_x
  1220. {
  1221. SCM_VALIDATE_CLASS (1, class);
  1222. SCM_VALIDATE_INSTANCE (2, obj);
  1223. SCM_VALIDATE_SYMBOL (3, slot_name);
  1224. return set_slot_value_using_name (class, obj, slot_name, value);
  1225. }
  1226. #undef FUNC_NAME
  1227. SCM_DEFINE (scm_slot_bound_using_class_p, "slot-bound-using-class?", 3, 0, 0,
  1228. (SCM class, SCM obj, SCM slot_name),
  1229. "")
  1230. #define FUNC_NAME s_scm_slot_bound_using_class_p
  1231. {
  1232. SCM_VALIDATE_CLASS (1, class);
  1233. SCM_VALIDATE_INSTANCE (2, obj);
  1234. SCM_VALIDATE_SYMBOL (3, slot_name);
  1235. return (SCM_GOOPS_UNBOUNDP (get_slot_value_using_name (class, obj, slot_name))
  1236. ? SCM_BOOL_F
  1237. : SCM_BOOL_T);
  1238. }
  1239. #undef FUNC_NAME
  1240. SCM_DEFINE (scm_slot_exists_using_class_p, "slot-exists-using-class?", 3, 0, 0,
  1241. (SCM class, SCM obj, SCM slot_name),
  1242. "")
  1243. #define FUNC_NAME s_scm_slot_exists_using_class_p
  1244. {
  1245. SCM_VALIDATE_CLASS (1, class);
  1246. SCM_VALIDATE_INSTANCE (2, obj);
  1247. SCM_VALIDATE_SYMBOL (3, slot_name);
  1248. return test_slot_existence (class, obj, slot_name);
  1249. }
  1250. #undef FUNC_NAME
  1251. /* ======================================== */
  1252. SCM_DEFINE (scm_slot_ref, "slot-ref", 2, 0, 0,
  1253. (SCM obj, SCM slot_name),
  1254. "Return the value from @var{obj}'s slot with the name\n"
  1255. "@var{slot_name}.")
  1256. #define FUNC_NAME s_scm_slot_ref
  1257. {
  1258. SCM res, class;
  1259. SCM_VALIDATE_INSTANCE (1, obj);
  1260. TEST_CHANGE_CLASS (obj, class);
  1261. res = get_slot_value_using_name (class, obj, slot_name);
  1262. if (SCM_GOOPS_UNBOUNDP (res))
  1263. return scm_call_3 (SCM_VARIABLE_REF (var_slot_unbound), class, obj, slot_name);
  1264. return res;
  1265. }
  1266. #undef FUNC_NAME
  1267. SCM_DEFINE (scm_slot_set_x, "slot-set!", 3, 0, 0,
  1268. (SCM obj, SCM slot_name, SCM value),
  1269. "Set the slot named @var{slot_name} of @var{obj} to @var{value}.")
  1270. #define FUNC_NAME s_scm_slot_set_x
  1271. {
  1272. SCM class;
  1273. SCM_VALIDATE_INSTANCE (1, obj);
  1274. TEST_CHANGE_CLASS(obj, class);
  1275. return set_slot_value_using_name (class, obj, slot_name, value);
  1276. }
  1277. #undef FUNC_NAME
  1278. const char *scm_s_slot_set_x = s_scm_slot_set_x;
  1279. SCM_DEFINE (scm_slot_bound_p, "slot-bound?", 2, 0, 0,
  1280. (SCM obj, SCM slot_name),
  1281. "Return @code{#t} if the slot named @var{slot_name} of @var{obj}\n"
  1282. "is bound.")
  1283. #define FUNC_NAME s_scm_slot_bound_p
  1284. {
  1285. SCM class;
  1286. SCM_VALIDATE_INSTANCE (1, obj);
  1287. TEST_CHANGE_CLASS(obj, class);
  1288. return (SCM_GOOPS_UNBOUNDP (get_slot_value_using_name (class,
  1289. obj,
  1290. slot_name))
  1291. ? SCM_BOOL_F
  1292. : SCM_BOOL_T);
  1293. }
  1294. #undef FUNC_NAME
  1295. SCM_DEFINE (scm_slot_exists_p, "slot-exists?", 2, 0, 0,
  1296. (SCM obj, SCM slot_name),
  1297. "Return @code{#t} if @var{obj} has a slot named @var{slot_name}.")
  1298. #define FUNC_NAME s_scm_slot_exists_p
  1299. {
  1300. SCM class;
  1301. SCM_VALIDATE_INSTANCE (1, obj);
  1302. SCM_VALIDATE_SYMBOL (2, slot_name);
  1303. TEST_CHANGE_CLASS (obj, class);
  1304. return test_slot_existence (class, obj, slot_name);
  1305. }
  1306. #undef FUNC_NAME
  1307. /******************************************************************************
  1308. *
  1309. * %allocate-instance (the low level instance allocation primitive)
  1310. *
  1311. ******************************************************************************/
  1312. static void clear_method_cache (SCM);
  1313. SCM_DEFINE (scm_sys_allocate_instance, "%allocate-instance", 2, 0, 0,
  1314. (SCM class, SCM initargs),
  1315. "Create a new instance of class @var{class} and initialize it\n"
  1316. "from the arguments @var{initargs}.")
  1317. #define FUNC_NAME s_scm_sys_allocate_instance
  1318. {
  1319. SCM obj;
  1320. scm_t_signed_bits n, i;
  1321. SCM layout;
  1322. SCM_VALIDATE_CLASS (1, class);
  1323. /* FIXME: duplicates some of scm_make_struct. */
  1324. n = SCM_I_INUM (SCM_SLOT (class, scm_si_nfields));
  1325. obj = scm_i_alloc_struct (SCM_STRUCT_DATA (class), n);
  1326. layout = SCM_VTABLE_LAYOUT (class);
  1327. /* Set all SCM-holding slots to unbound */
  1328. for (i = 0; i < n; i++)
  1329. {
  1330. scm_t_wchar c = scm_i_symbol_ref (layout, i*2);
  1331. if (c == 'p')
  1332. SCM_STRUCT_DATA (obj)[i] = SCM_UNPACK (SCM_GOOPS_UNBOUND);
  1333. else if (c == 's')
  1334. SCM_STRUCT_DATA (obj)[i] = SCM_UNPACK (obj);
  1335. else
  1336. SCM_STRUCT_DATA (obj)[i] = 0;
  1337. }
  1338. if (SCM_CLASS_FLAGS (class) & SCM_CLASSF_PURE_GENERIC)
  1339. clear_method_cache (obj);
  1340. return obj;
  1341. }
  1342. #undef FUNC_NAME
  1343. SCM_DEFINE (scm_sys_set_object_setter_x, "%set-object-setter!", 2, 0, 0,
  1344. (SCM obj, SCM setter),
  1345. "")
  1346. #define FUNC_NAME s_scm_sys_set_object_setter_x
  1347. {
  1348. SCM_ASSERT (SCM_STRUCTP (obj)
  1349. && (SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_PURE_GENERIC),
  1350. obj,
  1351. SCM_ARG1,
  1352. FUNC_NAME);
  1353. SCM_SET_GENERIC_SETTER (obj, setter);
  1354. return SCM_UNSPECIFIED;
  1355. }
  1356. #undef FUNC_NAME
  1357. /******************************************************************************
  1358. *
  1359. * %modify-instance (used by change-class to modify in place)
  1360. *
  1361. ******************************************************************************/
  1362. SCM_DEFINE (scm_sys_modify_instance, "%modify-instance", 2, 0, 0,
  1363. (SCM old, SCM new),
  1364. "")
  1365. #define FUNC_NAME s_scm_sys_modify_instance
  1366. {
  1367. SCM_VALIDATE_INSTANCE (1, old);
  1368. SCM_VALIDATE_INSTANCE (2, new);
  1369. /* Exchange the data contained in old and new. We exchange rather than
  1370. * scratch the old value with new to be correct with GC.
  1371. * See "Class redefinition protocol above".
  1372. */
  1373. SCM_CRITICAL_SECTION_START;
  1374. {
  1375. scm_t_bits word0, word1;
  1376. word0 = SCM_CELL_WORD_0 (old);
  1377. word1 = SCM_CELL_WORD_1 (old);
  1378. SCM_SET_CELL_WORD_0 (old, SCM_CELL_WORD_0 (new));
  1379. SCM_SET_CELL_WORD_1 (old, SCM_CELL_WORD_1 (new));
  1380. SCM_SET_CELL_WORD_0 (new, word0);
  1381. SCM_SET_CELL_WORD_1 (new, word1);
  1382. }
  1383. SCM_CRITICAL_SECTION_END;
  1384. return SCM_UNSPECIFIED;
  1385. }
  1386. #undef FUNC_NAME
  1387. SCM_DEFINE (scm_sys_modify_class, "%modify-class", 2, 0, 0,
  1388. (SCM old, SCM new),
  1389. "")
  1390. #define FUNC_NAME s_scm_sys_modify_class
  1391. {
  1392. SCM_VALIDATE_CLASS (1, old);
  1393. SCM_VALIDATE_CLASS (2, new);
  1394. SCM_CRITICAL_SECTION_START;
  1395. {
  1396. scm_t_bits word0, word1;
  1397. word0 = SCM_CELL_WORD_0 (old);
  1398. word1 = SCM_CELL_WORD_1 (old);
  1399. SCM_SET_CELL_WORD_0 (old, SCM_CELL_WORD_0 (new));
  1400. SCM_SET_CELL_WORD_1 (old, SCM_CELL_WORD_1 (new));
  1401. SCM_STRUCT_DATA (old)[scm_vtable_index_self] = SCM_UNPACK (old);
  1402. SCM_SET_CELL_WORD_0 (new, word0);
  1403. SCM_SET_CELL_WORD_1 (new, word1);
  1404. SCM_STRUCT_DATA (new)[scm_vtable_index_self] = SCM_UNPACK (new);
  1405. }
  1406. SCM_CRITICAL_SECTION_END;
  1407. return SCM_UNSPECIFIED;
  1408. }
  1409. #undef FUNC_NAME
  1410. SCM_DEFINE (scm_sys_invalidate_class, "%invalidate-class", 1, 0, 0,
  1411. (SCM class),
  1412. "")
  1413. #define FUNC_NAME s_scm_sys_invalidate_class
  1414. {
  1415. SCM_VALIDATE_CLASS (1, class);
  1416. SCM_CLEAR_CLASS_FLAGS (class, SCM_CLASSF_GOOPS_VALID);
  1417. return SCM_UNSPECIFIED;
  1418. }
  1419. #undef FUNC_NAME
  1420. /* When instances change class, they finally get a new body, but
  1421. * before that, they go through purgatory in hell. Odd as it may
  1422. * seem, this data structure saves us from eternal suffering in
  1423. * infinite recursions.
  1424. */
  1425. static scm_t_bits **hell;
  1426. static long n_hell = 1; /* one place for the evil one himself */
  1427. static long hell_size = 4;
  1428. static SCM hell_mutex;
  1429. static long
  1430. burnin (SCM o)
  1431. {
  1432. long i;
  1433. for (i = 1; i < n_hell; ++i)
  1434. if (SCM_STRUCT_DATA (o) == hell[i])
  1435. return i;
  1436. return 0;
  1437. }
  1438. static void
  1439. go_to_hell (void *o)
  1440. {
  1441. SCM obj = *(SCM*)o;
  1442. scm_lock_mutex (hell_mutex);
  1443. if (n_hell >= hell_size)
  1444. {
  1445. hell_size *= 2;
  1446. hell = scm_realloc (hell, hell_size * sizeof(*hell));
  1447. }
  1448. hell[n_hell++] = SCM_STRUCT_DATA (obj);
  1449. scm_unlock_mutex (hell_mutex);
  1450. }
  1451. static void
  1452. go_to_heaven (void *o)
  1453. {
  1454. SCM obj = *(SCM*)o;
  1455. scm_lock_mutex (hell_mutex);
  1456. hell[burnin (obj)] = hell[--n_hell];
  1457. scm_unlock_mutex (hell_mutex);
  1458. }
  1459. SCM_SYMBOL (scm_sym_change_class, "change-class");
  1460. static SCM
  1461. purgatory (SCM obj, SCM new_class)
  1462. {
  1463. return scm_call_2 (SCM_VARIABLE_REF (var_change_class), obj, new_class);
  1464. }
  1465. /* This function calls the generic function change-class for all
  1466. * instances which aren't currently undergoing class change.
  1467. */
  1468. void
  1469. scm_change_object_class (SCM obj, SCM old_class SCM_UNUSED, SCM new_class)
  1470. {
  1471. if (!burnin (obj))
  1472. {
  1473. scm_dynwind_begin (SCM_F_DYNWIND_REWINDABLE);
  1474. scm_dynwind_rewind_handler (go_to_hell, &obj, SCM_F_WIND_EXPLICITLY);
  1475. scm_dynwind_unwind_handler (go_to_heaven, &obj, SCM_F_WIND_EXPLICITLY);
  1476. purgatory (obj, new_class);
  1477. scm_dynwind_end ();
  1478. }
  1479. }
  1480. /******************************************************************************
  1481. *
  1482. * GGGG FFFFF
  1483. * G F
  1484. * G GG FFF
  1485. * G G F
  1486. * GGG E N E R I C F U N C T I O N S
  1487. *
  1488. * This implementation provides
  1489. * - generic functions (with class specializers)
  1490. * - multi-methods
  1491. * - next-method
  1492. * - a hard-coded MOP for standard gf, which can be overloaded for non-std gf
  1493. *
  1494. ******************************************************************************/
  1495. SCM_KEYWORD (k_name, "name");
  1496. SCM_GLOBAL_SYMBOL (scm_sym_args, "args");
  1497. SCM_SYMBOL (sym_delayed_compile, "delayed-compile");
  1498. static SCM
  1499. make_dispatch_procedure (SCM gf)
  1500. {
  1501. static SCM var = SCM_BOOL_F;
  1502. if (scm_is_false (var))
  1503. var = scm_module_variable (scm_c_resolve_module ("oop goops dispatch"),
  1504. sym_delayed_compile);
  1505. return scm_call_1 (SCM_VARIABLE_REF (var), gf);
  1506. }
  1507. static void
  1508. clear_method_cache (SCM gf)
  1509. {
  1510. SCM_SET_GENERIC_DISPATCH_PROCEDURE (gf, make_dispatch_procedure (gf));
  1511. SCM_CLEAR_GENERIC_EFFECTIVE_METHODS (gf);
  1512. }
  1513. SCM_DEFINE (scm_sys_invalidate_method_cache_x, "%invalidate-method-cache!", 1, 0, 0,
  1514. (SCM gf),
  1515. "")
  1516. #define FUNC_NAME s_scm_sys_invalidate_method_cache_x
  1517. {
  1518. SCM_ASSERT (SCM_PUREGENERICP (gf), gf, SCM_ARG1, FUNC_NAME);
  1519. clear_method_cache (gf);
  1520. return SCM_UNSPECIFIED;
  1521. }
  1522. #undef FUNC_NAME
  1523. SCM_DEFINE (scm_generic_capability_p, "generic-capability?", 1, 0, 0,
  1524. (SCM proc),
  1525. "")
  1526. #define FUNC_NAME s_scm_generic_capability_p
  1527. {
  1528. SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
  1529. proc, SCM_ARG1, FUNC_NAME);
  1530. return (SCM_PRIMITIVE_GENERIC_P (proc) ? SCM_BOOL_T : SCM_BOOL_F);
  1531. }
  1532. #undef FUNC_NAME
  1533. SCM_DEFINE (scm_enable_primitive_generic_x, "enable-primitive-generic!", 0, 0, 1,
  1534. (SCM subrs),
  1535. "")
  1536. #define FUNC_NAME s_scm_enable_primitive_generic_x
  1537. {
  1538. SCM_VALIDATE_REST_ARGUMENT (subrs);
  1539. while (!scm_is_null (subrs))
  1540. {
  1541. SCM subr = SCM_CAR (subrs);
  1542. SCM_ASSERT (SCM_PRIMITIVE_GENERIC_P (subr), subr, SCM_ARGn, FUNC_NAME);
  1543. SCM_SET_SUBR_GENERIC (subr,
  1544. scm_make (scm_list_3 (scm_class_generic,
  1545. k_name,
  1546. SCM_SUBR_NAME (subr))));
  1547. subrs = SCM_CDR (subrs);
  1548. }
  1549. return SCM_UNSPECIFIED;
  1550. }
  1551. #undef FUNC_NAME
  1552. SCM_DEFINE (scm_set_primitive_generic_x, "set-primitive-generic!", 2, 0, 0,
  1553. (SCM subr, SCM generic),
  1554. "")
  1555. #define FUNC_NAME s_scm_set_primitive_generic_x
  1556. {
  1557. SCM_ASSERT (SCM_PRIMITIVE_GENERIC_P (subr), subr, SCM_ARG1, FUNC_NAME);
  1558. SCM_ASSERT (SCM_PUREGENERICP (generic), generic, SCM_ARG2, FUNC_NAME);
  1559. SCM_SET_SUBR_GENERIC (subr, generic);
  1560. return SCM_UNSPECIFIED;
  1561. }
  1562. #undef FUNC_NAME
  1563. SCM_DEFINE (scm_primitive_generic_generic, "primitive-generic-generic", 1, 0, 0,
  1564. (SCM subr),
  1565. "")
  1566. #define FUNC_NAME s_scm_primitive_generic_generic
  1567. {
  1568. if (SCM_PRIMITIVE_GENERIC_P (subr))
  1569. {
  1570. if (!SCM_UNPACK (*SCM_SUBR_GENERIC (subr)))
  1571. scm_enable_primitive_generic_x (scm_list_1 (subr));
  1572. return *SCM_SUBR_GENERIC (subr);
  1573. }
  1574. SCM_WRONG_TYPE_ARG (SCM_ARG1, subr);
  1575. }
  1576. #undef FUNC_NAME
  1577. typedef struct t_extension {
  1578. struct t_extension *next;
  1579. SCM extended;
  1580. SCM extension;
  1581. } t_extension;
  1582. /* Hint for `scm_gc_malloc ()' et al. when allocating `t_extension'
  1583. objects. */
  1584. static const char extension_gc_hint[] = "GOOPS extension";
  1585. static t_extension *extensions = 0;
  1586. void
  1587. scm_c_extend_primitive_generic (SCM extended, SCM extension)
  1588. {
  1589. if (goops_loaded_p)
  1590. {
  1591. SCM gf, gext;
  1592. if (!SCM_UNPACK (*SCM_SUBR_GENERIC (extended)))
  1593. scm_enable_primitive_generic_x (scm_list_1 (extended));
  1594. gf = *SCM_SUBR_GENERIC (extended);
  1595. gext = scm_call_2 (SCM_VARIABLE_REF (scm_var_make_extended_generic),
  1596. gf,
  1597. SCM_SUBR_NAME (extension));
  1598. SCM_SET_SUBR_GENERIC (extension, gext);
  1599. }
  1600. else
  1601. {
  1602. t_extension *e = scm_gc_malloc (sizeof (t_extension),
  1603. extension_gc_hint);
  1604. t_extension **loc = &extensions;
  1605. /* Make sure that extensions are placed before their own
  1606. * extensions in the extensions list. O(N^2) algorithm, but
  1607. * extensions of primitive generics are rare.
  1608. */
  1609. while (*loc && !scm_is_eq (extension, (*loc)->extended))
  1610. loc = &(*loc)->next;
  1611. e->next = *loc;
  1612. e->extended = extended;
  1613. e->extension = extension;
  1614. *loc = e;
  1615. }
  1616. }
  1617. static void
  1618. setup_extended_primitive_generics ()
  1619. {
  1620. while (extensions)
  1621. {
  1622. t_extension *e = extensions;
  1623. scm_c_extend_primitive_generic (e->extended, e->extension);
  1624. extensions = e->next;
  1625. }
  1626. }
  1627. /* Dirk:FIXME:: In all of these scm_wta_dispatch_* routines it is
  1628. * assumed that 'gf' is zero if uninitialized. It would be cleaner if
  1629. * some valid SCM value like SCM_BOOL_F or SCM_UNDEFINED were chosen.
  1630. */
  1631. SCM
  1632. scm_wta_dispatch_0 (SCM gf, const char *subr)
  1633. {
  1634. if (!SCM_UNPACK (gf))
  1635. scm_error_num_args_subr (subr);
  1636. return scm_call_0 (gf);
  1637. }
  1638. SCM
  1639. scm_wta_dispatch_1 (SCM gf, SCM a1, int pos, const char *subr)
  1640. {
  1641. if (!SCM_UNPACK (gf))
  1642. scm_wrong_type_arg (subr, pos, a1);
  1643. return scm_call_1 (gf, a1);
  1644. }
  1645. SCM
  1646. scm_wta_dispatch_2 (SCM gf, SCM a1, SCM a2, int pos, const char *subr)
  1647. {
  1648. if (!SCM_UNPACK (gf))
  1649. scm_wrong_type_arg (subr, pos, (pos == SCM_ARG1) ? a1 : a2);
  1650. return scm_call_2 (gf, a1, a2);
  1651. }
  1652. SCM
  1653. scm_wta_dispatch_n (SCM gf, SCM args, int pos, const char *subr)
  1654. {
  1655. if (!SCM_UNPACK (gf))
  1656. scm_wrong_type_arg (subr, pos, scm_list_ref (args, scm_from_int (pos)));
  1657. return scm_apply_0 (gf, args);
  1658. }
  1659. /******************************************************************************
  1660. *
  1661. * Protocol for calling a generic fumction
  1662. * This protocol is roughly equivalent to (parameter are a little bit different
  1663. * for efficiency reasons):
  1664. *
  1665. * + apply-generic (gf args)
  1666. * + compute-applicable-methods (gf args ...)
  1667. * + sort-applicable-methods (methods args)
  1668. * + apply-methods (gf methods args)
  1669. *
  1670. * apply-methods calls make-next-method to build the "continuation" of a a
  1671. * method. Applying a next-method will call apply-next-method which in
  1672. * turn will call apply again to call effectively the following method.
  1673. *
  1674. ******************************************************************************/
  1675. static int
  1676. applicablep (SCM actual, SCM formal)
  1677. {
  1678. /* We already know that the cpl is well formed. */
  1679. return scm_is_true (scm_c_memq (formal, SCM_SLOT (actual, scm_si_cpl)));
  1680. }
  1681. static int
  1682. more_specificp (SCM m1, SCM m2, SCM const *targs)
  1683. {
  1684. register SCM s1, s2;
  1685. register long i;
  1686. /*
  1687. * Note:
  1688. * m1 and m2 can have != length (i.e. one can be one element longer than the
  1689. * other when we have a dotted parameter list). For instance, with the call
  1690. * (M 1)
  1691. * with
  1692. * (define-method M (a . l) ....)
  1693. * (define-method M (a) ....)
  1694. *
  1695. * we consider that the second method is more specific.
  1696. *
  1697. * BTW, targs is an array of types. We don't need it's size since
  1698. * we already know that m1 and m2 are applicable (no risk to go past
  1699. * the end of this array).
  1700. *
  1701. */
  1702. for (i=0, s1=SPEC_OF(m1), s2=SPEC_OF(m2); ; i++, s1=SCM_CDR(s1), s2=SCM_CDR(s2)) {
  1703. if (scm_is_null(s1)) return 1;
  1704. if (scm_is_null(s2)) return 0;
  1705. if (!scm_is_eq (SCM_CAR(s1), SCM_CAR(s2))) {
  1706. register SCM l, cs1 = SCM_CAR(s1), cs2 = SCM_CAR(s2);
  1707. for (l = SCM_SLOT (targs[i], scm_si_cpl); ; l = SCM_CDR(l)) {
  1708. if (scm_is_eq (cs1, SCM_CAR (l)))
  1709. return 1;
  1710. if (scm_is_eq (cs2, SCM_CAR (l)))
  1711. return 0;
  1712. }
  1713. return 0;/* should not occur! */
  1714. }
  1715. }
  1716. return 0; /* should not occur! */
  1717. }
  1718. #define BUFFSIZE 32 /* big enough for most uses */
  1719. static SCM
  1720. scm_i_vector2list (SCM l, long len)
  1721. {
  1722. long j;
  1723. SCM z = scm_c_make_vector (len, SCM_UNDEFINED);
  1724. for (j = 0; j < len; j++, l = SCM_CDR (l)) {
  1725. SCM_SIMPLE_VECTOR_SET (z, j, SCM_CAR (l));
  1726. }
  1727. return z;
  1728. }
  1729. static SCM
  1730. sort_applicable_methods (SCM method_list, long size, SCM const *targs)
  1731. {
  1732. long i, j, incr;
  1733. SCM *v, vector = SCM_EOL;
  1734. SCM buffer[BUFFSIZE];
  1735. SCM save = method_list;
  1736. scm_t_array_handle handle;
  1737. /* For reasonably sized method_lists we can try to avoid all the
  1738. * consing and reorder the list in place...
  1739. * This idea is due to David McClain <Dave_McClain@msn.com>
  1740. */
  1741. if (size <= BUFFSIZE)
  1742. {
  1743. for (i = 0; i < size; i++)
  1744. {
  1745. buffer[i] = SCM_CAR (method_list);
  1746. method_list = SCM_CDR (method_list);
  1747. }
  1748. v = buffer;
  1749. }
  1750. else
  1751. {
  1752. /* Too many elements in method_list to keep everything locally */
  1753. vector = scm_i_vector2list (save, size);
  1754. v = scm_vector_writable_elements (vector, &handle, NULL, NULL);
  1755. }
  1756. /* Use a simple shell sort since it is generally faster than qsort on
  1757. * small vectors (which is probably mostly the case when we have to
  1758. * sort a list of applicable methods).
  1759. */
  1760. for (incr = size / 2; incr; incr /= 2)
  1761. {
  1762. for (i = incr; i < size; i++)
  1763. {
  1764. for (j = i - incr; j >= 0; j -= incr)
  1765. {
  1766. if (more_specificp (v[j], v[j+incr], targs))
  1767. break;
  1768. else
  1769. {
  1770. SCM tmp = v[j + incr];
  1771. v[j + incr] = v[j];
  1772. v[j] = tmp;
  1773. }
  1774. }
  1775. }
  1776. }
  1777. if (size <= BUFFSIZE)
  1778. {
  1779. /* We did it in locally, so restore the original list (reordered) in-place */
  1780. for (i = 0, method_list = save; i < size; i++, v++)
  1781. {
  1782. SCM_SETCAR (method_list, *v);
  1783. method_list = SCM_CDR (method_list);
  1784. }
  1785. return save;
  1786. }
  1787. /* If we are here, that's that we did it the hard way... */
  1788. scm_array_handle_release (&handle);
  1789. return scm_vector_to_list (vector);
  1790. }
  1791. SCM
  1792. scm_compute_applicable_methods (SCM gf, SCM args, long len, int find_method_p)
  1793. {
  1794. register long i;
  1795. long count = 0;
  1796. SCM l, fl, applicable = SCM_EOL;
  1797. SCM save = args;
  1798. SCM buffer[BUFFSIZE];
  1799. SCM const *types;
  1800. SCM *p;
  1801. SCM tmp = SCM_EOL;
  1802. scm_t_array_handle handle;
  1803. /* Build the list of arguments types */
  1804. if (len >= BUFFSIZE)
  1805. {
  1806. tmp = scm_c_make_vector (len, SCM_UNDEFINED);
  1807. types = p = scm_vector_writable_elements (tmp, &handle, NULL, NULL);
  1808. /*
  1809. note that we don't have to work to reset the generation
  1810. count. TMP is a new vector anyway, and it is found
  1811. conservatively.
  1812. */
  1813. }
  1814. else
  1815. types = p = buffer;
  1816. for ( ; !scm_is_null (args); args = SCM_CDR (args))
  1817. *p++ = scm_class_of (SCM_CAR (args));
  1818. /* Build a list of all applicable methods */
  1819. for (l = scm_generic_function_methods (gf); !scm_is_null (l); l = SCM_CDR (l))
  1820. {
  1821. fl = SPEC_OF (SCM_CAR (l));
  1822. for (i = 0; ; i++, fl = SCM_CDR (fl))
  1823. {
  1824. if (SCM_INSTANCEP (fl)
  1825. /* We have a dotted argument list */
  1826. || (i >= len && scm_is_null (fl)))
  1827. { /* both list exhausted */
  1828. applicable = scm_cons (SCM_CAR (l), applicable);
  1829. count += 1;
  1830. break;
  1831. }
  1832. if (i >= len
  1833. || scm_is_null (fl)
  1834. || !applicablep (types[i], SCM_CAR (fl)))
  1835. break;
  1836. }
  1837. }
  1838. if (len >= BUFFSIZE)
  1839. scm_array_handle_release (&handle);
  1840. if (count == 0)
  1841. {
  1842. if (find_method_p)
  1843. return SCM_BOOL_F;
  1844. scm_call_2 (SCM_VARIABLE_REF (var_no_applicable_method), gf, save);
  1845. /* if we are here, it's because no-applicable-method hasn't signaled an error */
  1846. return SCM_BOOL_F;
  1847. }
  1848. return (count == 1
  1849. ? applicable
  1850. : sort_applicable_methods (applicable, count, types));
  1851. }
  1852. #if 0
  1853. SCM_PROC (s_sys_compute_applicable_methods, "%compute-applicable-methods", 2, 0, 0, scm_sys_compute_applicable_methods);
  1854. #endif
  1855. static const char s_sys_compute_applicable_methods[] = "%compute-applicable-methods";
  1856. SCM
  1857. scm_sys_compute_applicable_methods (SCM gf, SCM args)
  1858. #define FUNC_NAME s_sys_compute_applicable_methods
  1859. {
  1860. long n;
  1861. SCM_VALIDATE_GENERIC (1, gf);
  1862. n = scm_ilength (args);
  1863. SCM_ASSERT (n >= 0, args, SCM_ARG2, FUNC_NAME);
  1864. return scm_compute_applicable_methods (gf, args, n, 1);
  1865. }
  1866. #undef FUNC_NAME
  1867. SCM_SYMBOL (sym_compute_applicable_methods, "compute-applicable-methods");
  1868. SCM_VARIABLE_INIT (var_compute_applicable_methods, "compute-applicable-methods",
  1869. scm_c_define_gsubr (s_sys_compute_applicable_methods, 2, 0, 0,
  1870. scm_sys_compute_applicable_methods));
  1871. /******************************************************************************
  1872. *
  1873. * A simple make (which will be redefined later in Scheme)
  1874. * This version handles only creation of gf, methods and classes (no instances)
  1875. *
  1876. * Since this code will disappear when Goops will be fully booted,
  1877. * no precaution is taken to be efficient.
  1878. *
  1879. ******************************************************************************/
  1880. SCM_KEYWORD (k_setter, "setter");
  1881. SCM_KEYWORD (k_specializers, "specializers");
  1882. SCM_KEYWORD (k_procedure, "procedure");
  1883. SCM_KEYWORD (k_formals, "formals");
  1884. SCM_KEYWORD (k_body, "body");
  1885. SCM_KEYWORD (k_make_procedure, "make-procedure");
  1886. SCM_KEYWORD (k_dsupers, "dsupers");
  1887. SCM_KEYWORD (k_slots, "slots");
  1888. SCM_KEYWORD (k_gf, "generic-function");
  1889. SCM_DEFINE (scm_make, "make", 0, 0, 1,
  1890. (SCM args),
  1891. "Make a new object. @var{args} must contain the class and\n"
  1892. "all necessary initialization information.")
  1893. #define FUNC_NAME s_scm_make
  1894. {
  1895. SCM class, z;
  1896. long len = scm_ilength (args);
  1897. if (len <= 0 || (len & 1) == 0)
  1898. SCM_WRONG_NUM_ARGS ();
  1899. class = SCM_CAR(args);
  1900. args = SCM_CDR(args);
  1901. if (scm_is_eq (class, scm_class_generic)
  1902. || scm_is_eq (class, scm_class_accessor))
  1903. {
  1904. z = scm_make_struct (class, SCM_INUM0,
  1905. scm_list_4 (SCM_BOOL_F,
  1906. SCM_EOL,
  1907. SCM_INUM0,
  1908. SCM_EOL));
  1909. scm_set_procedure_property_x (z, scm_sym_name,
  1910. scm_get_keyword (k_name,
  1911. args,
  1912. SCM_BOOL_F));
  1913. clear_method_cache (z);
  1914. if (scm_is_eq (class, scm_class_accessor))
  1915. {
  1916. SCM setter = scm_get_keyword (k_setter, args, SCM_BOOL_F);
  1917. if (scm_is_true (setter))
  1918. scm_sys_set_object_setter_x (z, setter);
  1919. }
  1920. }
  1921. else
  1922. {
  1923. z = scm_sys_allocate_instance (class, args);
  1924. if (scm_is_eq (class, scm_class_method)
  1925. || scm_is_eq (class, scm_class_accessor_method))
  1926. {
  1927. SCM_SET_SLOT (z, scm_si_generic_function,
  1928. scm_i_get_keyword (k_gf,
  1929. args,
  1930. len - 1,
  1931. SCM_BOOL_F,
  1932. FUNC_NAME));
  1933. SCM_SET_SLOT (z, scm_si_specializers,
  1934. scm_i_get_keyword (k_specializers,
  1935. args,
  1936. len - 1,
  1937. SCM_EOL,
  1938. FUNC_NAME));
  1939. SCM_SET_SLOT (z, scm_si_procedure,
  1940. scm_i_get_keyword (k_procedure,
  1941. args,
  1942. len - 1,
  1943. SCM_BOOL_F,
  1944. FUNC_NAME));
  1945. SCM_SET_SLOT (z, scm_si_formals,
  1946. scm_i_get_keyword (k_formals,
  1947. args,
  1948. len - 1,
  1949. SCM_EOL,
  1950. FUNC_NAME));
  1951. SCM_SET_SLOT (z, scm_si_body,
  1952. scm_i_get_keyword (k_body,
  1953. args,
  1954. len - 1,
  1955. SCM_EOL,
  1956. FUNC_NAME));
  1957. SCM_SET_SLOT (z, scm_si_make_procedure,
  1958. scm_i_get_keyword (k_make_procedure,
  1959. args,
  1960. len - 1,
  1961. SCM_BOOL_F,
  1962. FUNC_NAME));
  1963. }
  1964. else
  1965. {
  1966. /* In all the others case, make a new class .... No instance here */
  1967. SCM_SET_SLOT (z, scm_vtable_index_name,
  1968. scm_i_get_keyword (k_name,
  1969. args,
  1970. len - 1,
  1971. scm_from_latin1_symbol ("???"),
  1972. FUNC_NAME));
  1973. SCM_SET_SLOT (z, scm_si_direct_supers,
  1974. scm_i_get_keyword (k_dsupers,
  1975. args,
  1976. len - 1,
  1977. SCM_EOL,
  1978. FUNC_NAME));
  1979. SCM_SET_SLOT (z, scm_si_direct_slots,
  1980. scm_i_get_keyword (k_slots,
  1981. args,
  1982. len - 1,
  1983. SCM_EOL,
  1984. FUNC_NAME));
  1985. }
  1986. }
  1987. return z;
  1988. }
  1989. #undef FUNC_NAME
  1990. SCM_DEFINE (scm_find_method, "find-method", 0, 0, 1,
  1991. (SCM l),
  1992. "")
  1993. #define FUNC_NAME s_scm_find_method
  1994. {
  1995. SCM gf;
  1996. long len = scm_ilength (l);
  1997. if (len == 0)
  1998. SCM_WRONG_NUM_ARGS ();
  1999. gf = SCM_CAR(l); l = SCM_CDR(l);
  2000. SCM_VALIDATE_GENERIC (1, gf);
  2001. if (scm_is_null (SCM_SLOT (gf, scm_si_methods)))
  2002. SCM_MISC_ERROR ("no methods for generic ~S", scm_list_1 (gf));
  2003. return scm_compute_applicable_methods (gf, l, len - 1, 1);
  2004. }
  2005. #undef FUNC_NAME
  2006. SCM_DEFINE (scm_sys_method_more_specific_p, "%method-more-specific?", 3, 0, 0,
  2007. (SCM m1, SCM m2, SCM targs),
  2008. "Return true if method @var{m1} is more specific than @var{m2} "
  2009. "given the argument types (classes) listed in @var{targs}.")
  2010. #define FUNC_NAME s_scm_sys_method_more_specific_p
  2011. {
  2012. SCM l, v, result;
  2013. SCM *v_elts;
  2014. long i, len, m1_specs, m2_specs;
  2015. scm_t_array_handle handle;
  2016. SCM_VALIDATE_METHOD (1, m1);
  2017. SCM_VALIDATE_METHOD (2, m2);
  2018. len = scm_ilength (targs);
  2019. m1_specs = scm_ilength (SPEC_OF (m1));
  2020. m2_specs = scm_ilength (SPEC_OF (m2));
  2021. SCM_ASSERT ((len >= m1_specs) || (len >= m2_specs),
  2022. targs, SCM_ARG3, FUNC_NAME);
  2023. /* Verify that all the arguments of TARGS are classes and place them
  2024. in a vector. */
  2025. v = scm_c_make_vector (len, SCM_EOL);
  2026. v_elts = scm_vector_writable_elements (v, &handle, NULL, NULL);
  2027. for (i = 0, l = targs;
  2028. i < len && scm_is_pair (l);
  2029. i++, l = SCM_CDR (l))
  2030. {
  2031. SCM_ASSERT (SCM_CLASSP (SCM_CAR (l)), targs, SCM_ARG3, FUNC_NAME);
  2032. v_elts[i] = SCM_CAR (l);
  2033. }
  2034. result = more_specificp (m1, m2, v_elts) ? SCM_BOOL_T: SCM_BOOL_F;
  2035. scm_array_handle_release (&handle);
  2036. return result;
  2037. }
  2038. #undef FUNC_NAME
  2039. /******************************************************************************
  2040. *
  2041. * Initializations
  2042. *
  2043. ******************************************************************************/
  2044. /* Munge the CPL of C in place such that BEFORE appears before AFTER,
  2045. assuming that currently the reverse is true. Recalculate slots and
  2046. associated getters-n-setters. */
  2047. static void
  2048. fix_cpl (SCM c, SCM before, SCM after)
  2049. {
  2050. SCM cpl = SCM_SLOT (c, scm_si_cpl);
  2051. SCM ls = scm_c_memq (after, cpl);
  2052. SCM tail;
  2053. if (scm_is_false (ls))
  2054. /* if this condition occurs, fix_cpl should not be applied this way */
  2055. abort ();
  2056. tail = scm_delq1_x (before, SCM_CDR (ls));
  2057. SCM_SETCAR (ls, before);
  2058. SCM_SETCDR (ls, scm_cons (after, tail));
  2059. {
  2060. SCM dslots = SCM_SLOT (c, scm_si_direct_slots);
  2061. SCM slots = build_slots_list (maplist (dslots), cpl);
  2062. SCM g_n_s = compute_getters_n_setters (slots);
  2063. SCM_SET_SLOT (c, scm_si_slots, slots);
  2064. SCM_SET_SLOT (c, scm_si_getters_n_setters, g_n_s);
  2065. }
  2066. }
  2067. static void
  2068. make_stdcls (SCM *var, char *name, SCM meta, SCM super, SCM slots)
  2069. {
  2070. SCM tmp = scm_from_utf8_symbol (name);
  2071. *var = scm_basic_make_class (meta, tmp,
  2072. scm_is_pair (super) ? super : scm_list_1 (super),
  2073. slots);
  2074. scm_module_define (scm_module_goops, tmp, *var);
  2075. }
  2076. SCM_KEYWORD (k_slot_definition, "slot-definition");
  2077. static void
  2078. create_standard_classes (void)
  2079. {
  2080. SCM slots;
  2081. SCM method_slots = scm_list_n (scm_from_latin1_symbol ("generic-function"),
  2082. scm_from_latin1_symbol ("specializers"),
  2083. sym_procedure,
  2084. scm_from_latin1_symbol ("formals"),
  2085. scm_from_latin1_symbol ("body"),
  2086. scm_from_latin1_symbol ("make-procedure"),
  2087. SCM_UNDEFINED);
  2088. SCM amethod_slots = scm_list_1 (scm_list_3 (scm_from_latin1_symbol ("slot-definition"),
  2089. k_init_keyword,
  2090. k_slot_definition));
  2091. SCM gf_slots = scm_list_4 (scm_from_latin1_symbol ("methods"),
  2092. scm_list_3 (scm_from_latin1_symbol ("n-specialized"),
  2093. k_init_value,
  2094. SCM_INUM0),
  2095. scm_list_3 (scm_from_latin1_symbol ("extended-by"),
  2096. k_init_value,
  2097. SCM_EOL),
  2098. scm_from_latin1_symbol ("effective-methods"));
  2099. SCM setter_slots = scm_list_1 (sym_setter);
  2100. SCM egf_slots = scm_list_1 (scm_list_3 (scm_from_latin1_symbol ("extends"),
  2101. k_init_value,
  2102. SCM_EOL));
  2103. /* Foreign class slot classes */
  2104. make_stdcls (&scm_class_foreign_slot, "<foreign-slot>",
  2105. scm_class_class, scm_class_top, SCM_EOL);
  2106. make_stdcls (&scm_class_protected, "<protected-slot>",
  2107. scm_class_class, scm_class_foreign_slot, SCM_EOL);
  2108. make_stdcls (&scm_class_hidden, "<hidden-slot>",
  2109. scm_class_class, scm_class_foreign_slot, SCM_EOL);
  2110. make_stdcls (&scm_class_opaque, "<opaque-slot>",
  2111. scm_class_class, scm_class_foreign_slot, SCM_EOL);
  2112. make_stdcls (&scm_class_read_only, "<read-only-slot>",
  2113. scm_class_class, scm_class_foreign_slot, SCM_EOL);
  2114. make_stdcls (&scm_class_self, "<self-slot>",
  2115. scm_class_class, scm_class_read_only, SCM_EOL);
  2116. make_stdcls (&scm_class_protected_opaque, "<protected-opaque-slot>",
  2117. scm_class_class,
  2118. scm_list_2 (scm_class_protected, scm_class_opaque),
  2119. SCM_EOL);
  2120. make_stdcls (&scm_class_protected_hidden, "<protected-hidden-slot>",
  2121. scm_class_class,
  2122. scm_list_2 (scm_class_protected, scm_class_hidden),
  2123. SCM_EOL);
  2124. make_stdcls (&scm_class_protected_read_only, "<protected-read-only-slot>",
  2125. scm_class_class,
  2126. scm_list_2 (scm_class_protected, scm_class_read_only),
  2127. SCM_EOL);
  2128. make_stdcls (&scm_class_scm, "<scm-slot>",
  2129. scm_class_class, scm_class_protected, SCM_EOL);
  2130. make_stdcls (&scm_class_int, "<int-slot>",
  2131. scm_class_class, scm_class_foreign_slot, SCM_EOL);
  2132. make_stdcls (&scm_class_float, "<float-slot>",
  2133. scm_class_class, scm_class_foreign_slot, SCM_EOL);
  2134. make_stdcls (&scm_class_double, "<double-slot>",
  2135. scm_class_class, scm_class_foreign_slot, SCM_EOL);
  2136. /* Continue initialization of class <class> */
  2137. slots = build_class_class_slots ();
  2138. SCM_SET_SLOT (scm_class_class, scm_si_direct_slots, slots);
  2139. SCM_SET_SLOT (scm_class_class, scm_si_slots, slots);
  2140. SCM_SET_SLOT (scm_class_class, scm_si_getters_n_setters,
  2141. compute_getters_n_setters (slots));
  2142. /* scm_class_generic functions classes */
  2143. make_stdcls (&scm_class_procedure_class, "<procedure-class>",
  2144. scm_class_class, scm_class_class, SCM_EOL);
  2145. make_stdcls (&scm_class_applicable_struct_class, "<applicable-struct-class>",
  2146. scm_class_class, scm_class_procedure_class, SCM_EOL);
  2147. SCM_SET_VTABLE_FLAGS (scm_class_applicable_struct_class, SCM_VTABLE_FLAG_APPLICABLE_VTABLE);
  2148. make_stdcls (&scm_class_method, "<method>",
  2149. scm_class_class, scm_class_object, method_slots);
  2150. make_stdcls (&scm_class_accessor_method, "<accessor-method>",
  2151. scm_class_class, scm_class_method, amethod_slots);
  2152. make_stdcls (&scm_class_applicable, "<applicable>",
  2153. scm_class_class, scm_class_top, SCM_EOL);
  2154. make_stdcls (&scm_class_applicable_struct, "<applicable-struct>",
  2155. scm_class_applicable_struct_class,
  2156. scm_list_2 (scm_class_object, scm_class_applicable),
  2157. scm_list_1 (sym_procedure));
  2158. make_stdcls (&scm_class_generic, "<generic>",
  2159. scm_class_applicable_struct_class, scm_class_applicable_struct, gf_slots);
  2160. SCM_SET_CLASS_FLAGS (scm_class_generic, SCM_CLASSF_PURE_GENERIC);
  2161. make_stdcls (&scm_class_extended_generic, "<extended-generic>",
  2162. scm_class_applicable_struct_class, scm_class_generic, egf_slots);
  2163. SCM_SET_CLASS_FLAGS (scm_class_extended_generic, SCM_CLASSF_PURE_GENERIC);
  2164. make_stdcls (&scm_class_generic_with_setter, "<generic-with-setter>",
  2165. scm_class_applicable_struct_class, scm_class_generic, setter_slots);
  2166. SCM_SET_CLASS_FLAGS (scm_class_generic_with_setter, SCM_CLASSF_PURE_GENERIC);
  2167. make_stdcls (&scm_class_accessor, "<accessor>",
  2168. scm_class_applicable_struct_class, scm_class_generic_with_setter, SCM_EOL);
  2169. SCM_SET_CLASS_FLAGS (scm_class_accessor, SCM_CLASSF_PURE_GENERIC);
  2170. make_stdcls (&scm_class_extended_generic_with_setter,
  2171. "<extended-generic-with-setter>",
  2172. scm_class_applicable_struct_class,
  2173. scm_list_2 (scm_class_extended_generic,
  2174. scm_class_generic_with_setter),
  2175. SCM_EOL);
  2176. SCM_SET_CLASS_FLAGS (scm_class_extended_generic_with_setter,
  2177. SCM_CLASSF_PURE_GENERIC);
  2178. make_stdcls (&scm_class_extended_accessor, "<extended-accessor>",
  2179. scm_class_applicable_struct_class,
  2180. scm_list_2 (scm_class_accessor,
  2181. scm_class_extended_generic_with_setter),
  2182. SCM_EOL);
  2183. /* <extended-generic> is misplaced. */
  2184. fix_cpl (scm_class_extended_accessor,
  2185. scm_class_extended_generic, scm_class_generic_with_setter);
  2186. SCM_SET_CLASS_FLAGS (scm_class_extended_accessor, SCM_CLASSF_PURE_GENERIC);
  2187. /* Primitive types classes */
  2188. make_stdcls (&scm_class_boolean, "<boolean>",
  2189. scm_class_class, scm_class_top, SCM_EOL);
  2190. make_stdcls (&scm_class_char, "<char>",
  2191. scm_class_class, scm_class_top, SCM_EOL);
  2192. make_stdcls (&scm_class_list, "<list>",
  2193. scm_class_class, scm_class_top, SCM_EOL);
  2194. make_stdcls (&scm_class_pair, "<pair>",
  2195. scm_class_class, scm_class_list, SCM_EOL);
  2196. make_stdcls (&scm_class_null, "<null>",
  2197. scm_class_class, scm_class_list, SCM_EOL);
  2198. make_stdcls (&scm_class_string, "<string>",
  2199. scm_class_class, scm_class_top, SCM_EOL);
  2200. make_stdcls (&scm_class_symbol, "<symbol>",
  2201. scm_class_class, scm_class_top, SCM_EOL);
  2202. make_stdcls (&scm_class_vector, "<vector>",
  2203. scm_class_class, scm_class_top, SCM_EOL);
  2204. make_stdcls (&class_foreign, "<foreign>",
  2205. scm_class_class, scm_class_top, SCM_EOL);
  2206. make_stdcls (&class_hashtable, "<hashtable>",
  2207. scm_class_class, scm_class_top, SCM_EOL);
  2208. make_stdcls (&class_fluid, "<fluid>",
  2209. scm_class_class, scm_class_top, SCM_EOL);
  2210. make_stdcls (&class_dynamic_state, "<dynamic-state>",
  2211. scm_class_class, scm_class_top, SCM_EOL);
  2212. make_stdcls (&class_frame, "<frame>",
  2213. scm_class_class, scm_class_top, SCM_EOL);
  2214. make_stdcls (&class_objcode, "<objcode>",
  2215. scm_class_class, scm_class_top, SCM_EOL);
  2216. make_stdcls (&class_vm, "<vm>",
  2217. scm_class_class, scm_class_top, SCM_EOL);
  2218. make_stdcls (&class_vm_cont, "<vm-continuation>",
  2219. scm_class_class, scm_class_top, SCM_EOL);
  2220. make_stdcls (&class_bytevector, "<bytevector>",
  2221. scm_class_class, scm_class_top, SCM_EOL);
  2222. make_stdcls (&class_uvec, "<uvec>",
  2223. scm_class_class, class_bytevector, SCM_EOL);
  2224. make_stdcls (&class_array, "<array>",
  2225. scm_class_class, scm_class_top, SCM_EOL);
  2226. make_stdcls (&class_bitvector, "<bitvector>",
  2227. scm_class_class, scm_class_top, SCM_EOL);
  2228. make_stdcls (&scm_class_number, "<number>",
  2229. scm_class_class, scm_class_top, SCM_EOL);
  2230. make_stdcls (&scm_class_complex, "<complex>",
  2231. scm_class_class, scm_class_number, SCM_EOL);
  2232. make_stdcls (&scm_class_real, "<real>",
  2233. scm_class_class, scm_class_complex, SCM_EOL);
  2234. make_stdcls (&scm_class_integer, "<integer>",
  2235. scm_class_class, scm_class_real, SCM_EOL);
  2236. make_stdcls (&scm_class_fraction, "<fraction>",
  2237. scm_class_class, scm_class_real, SCM_EOL);
  2238. make_stdcls (&scm_class_keyword, "<keyword>",
  2239. scm_class_class, scm_class_top, SCM_EOL);
  2240. make_stdcls (&scm_class_unknown, "<unknown>",
  2241. scm_class_class, scm_class_top, SCM_EOL);
  2242. make_stdcls (&scm_class_procedure, "<procedure>",
  2243. scm_class_procedure_class, scm_class_applicable, SCM_EOL);
  2244. make_stdcls (&scm_class_primitive_generic, "<primitive-generic>",
  2245. scm_class_procedure_class, scm_class_procedure, SCM_EOL);
  2246. make_stdcls (&scm_class_port, "<port>",
  2247. scm_class_class, scm_class_top, SCM_EOL);
  2248. make_stdcls (&scm_class_input_port, "<input-port>",
  2249. scm_class_class, scm_class_port, SCM_EOL);
  2250. make_stdcls (&scm_class_output_port, "<output-port>",
  2251. scm_class_class, scm_class_port, SCM_EOL);
  2252. make_stdcls (&scm_class_input_output_port, "<input-output-port>",
  2253. scm_class_class,
  2254. scm_list_2 (scm_class_input_port, scm_class_output_port),
  2255. SCM_EOL);
  2256. }
  2257. /**********************************************************************
  2258. *
  2259. * Smob classes
  2260. *
  2261. **********************************************************************/
  2262. static SCM
  2263. make_class_from_template (char const *template, char const *type_name, SCM supers, int applicablep)
  2264. {
  2265. SCM name;
  2266. if (type_name)
  2267. {
  2268. char buffer[100];
  2269. sprintf (buffer, template, type_name);
  2270. name = scm_from_utf8_symbol (buffer);
  2271. }
  2272. else
  2273. name = SCM_GOOPS_UNBOUND;
  2274. return scm_basic_make_class (applicablep ? scm_class_procedure_class : scm_class_class,
  2275. name, supers, SCM_EOL);
  2276. }
  2277. static SCM
  2278. make_class_from_symbol (SCM type_name_sym, SCM supers, int applicablep)
  2279. {
  2280. SCM name;
  2281. if (scm_is_true (type_name_sym))
  2282. {
  2283. name = scm_string_append (scm_list_3 (scm_from_locale_string ("<"),
  2284. scm_symbol_to_string (type_name_sym),
  2285. scm_from_locale_string (">")));
  2286. name = scm_string_to_symbol (name);
  2287. }
  2288. else
  2289. name = SCM_GOOPS_UNBOUND;
  2290. return scm_basic_make_class (applicablep ? scm_class_procedure_class : scm_class_class,
  2291. name, supers, SCM_EOL);
  2292. }
  2293. SCM
  2294. scm_make_extended_class (char const *type_name, int applicablep)
  2295. {
  2296. return make_class_from_template ("<%s>",
  2297. type_name,
  2298. scm_list_1 (applicablep
  2299. ? scm_class_applicable
  2300. : scm_class_top),
  2301. applicablep);
  2302. }
  2303. static SCM
  2304. scm_make_extended_class_from_symbol (SCM type_name_sym, int applicablep)
  2305. {
  2306. return make_class_from_symbol (type_name_sym,
  2307. scm_list_1 (applicablep
  2308. ? scm_class_applicable
  2309. : scm_class_top),
  2310. applicablep);
  2311. }
  2312. void
  2313. scm_i_inherit_applicable (SCM c)
  2314. {
  2315. if (!SCM_SUBCLASSP (c, scm_class_applicable))
  2316. {
  2317. SCM dsupers = SCM_SLOT (c, scm_si_direct_supers);
  2318. SCM cpl = SCM_SLOT (c, scm_si_cpl);
  2319. /* patch scm_class_applicable into direct-supers */
  2320. SCM top = scm_c_memq (scm_class_top, dsupers);
  2321. if (scm_is_false (top))
  2322. dsupers = scm_append (scm_list_2 (dsupers,
  2323. scm_list_1 (scm_class_applicable)));
  2324. else
  2325. {
  2326. SCM_SETCAR (top, scm_class_applicable);
  2327. SCM_SETCDR (top, scm_cons (scm_class_top, SCM_CDR (top)));
  2328. }
  2329. SCM_SET_SLOT (c, scm_si_direct_supers, dsupers);
  2330. /* patch scm_class_applicable into cpl */
  2331. top = scm_c_memq (scm_class_top, cpl);
  2332. if (scm_is_false (top))
  2333. abort ();
  2334. else
  2335. {
  2336. SCM_SETCAR (top, scm_class_applicable);
  2337. SCM_SETCDR (top, scm_cons (scm_class_top, SCM_CDR (top)));
  2338. }
  2339. /* add class to direct-subclasses of scm_class_applicable */
  2340. SCM_SET_SLOT (scm_class_applicable,
  2341. scm_si_direct_subclasses,
  2342. scm_cons (c, SCM_SLOT (scm_class_applicable,
  2343. scm_si_direct_subclasses)));
  2344. }
  2345. }
  2346. static void
  2347. create_smob_classes (void)
  2348. {
  2349. long i;
  2350. for (i = 0; i < SCM_I_MAX_SMOB_TYPE_COUNT; ++i)
  2351. scm_smob_class[i] = SCM_BOOL_F;
  2352. scm_smob_class[SCM_TC2SMOBNUM (scm_tc16_keyword)] = scm_class_keyword;
  2353. for (i = 0; i < scm_numsmob; ++i)
  2354. if (scm_is_false (scm_smob_class[i]))
  2355. scm_smob_class[i] = scm_make_extended_class (SCM_SMOBNAME (i),
  2356. scm_smobs[i].apply != 0);
  2357. }
  2358. void
  2359. scm_make_port_classes (long ptobnum, char *type_name)
  2360. {
  2361. SCM c, class = make_class_from_template ("<%s-port>",
  2362. type_name,
  2363. scm_list_1 (scm_class_port),
  2364. 0);
  2365. scm_port_class[SCM_IN_PCLASS_INDEX + ptobnum]
  2366. = make_class_from_template ("<%s-input-port>",
  2367. type_name,
  2368. scm_list_2 (class, scm_class_input_port),
  2369. 0);
  2370. scm_port_class[SCM_OUT_PCLASS_INDEX + ptobnum]
  2371. = make_class_from_template ("<%s-output-port>",
  2372. type_name,
  2373. scm_list_2 (class, scm_class_output_port),
  2374. 0);
  2375. scm_port_class[SCM_INOUT_PCLASS_INDEX + ptobnum]
  2376. = c
  2377. = make_class_from_template ("<%s-input-output-port>",
  2378. type_name,
  2379. scm_list_2 (class, scm_class_input_output_port),
  2380. 0);
  2381. /* Patch cpl (since this tree is too complex for the C level compute-cpl) */
  2382. SCM_SET_SLOT (c, scm_si_cpl,
  2383. scm_cons2 (c, class, SCM_SLOT (scm_class_input_output_port, scm_si_cpl)));
  2384. }
  2385. static void
  2386. create_port_classes (void)
  2387. {
  2388. long i;
  2389. for (i = scm_c_num_port_types () - 1; i >= 0; i--)
  2390. scm_make_port_classes (i, SCM_PTOBNAME (i));
  2391. }
  2392. static SCM
  2393. make_struct_class (void *closure SCM_UNUSED,
  2394. SCM vtable, SCM data, SCM prev SCM_UNUSED)
  2395. {
  2396. if (scm_is_false (data))
  2397. scm_i_define_class_for_vtable (vtable);
  2398. return SCM_UNSPECIFIED;
  2399. }
  2400. static void
  2401. create_struct_classes (void)
  2402. {
  2403. /* FIXME: take the vtable_class_map while initializing goops? */
  2404. scm_internal_hash_fold (make_struct_class, 0, SCM_BOOL_F,
  2405. vtable_class_map);
  2406. }
  2407. /**********************************************************************
  2408. *
  2409. * C interface
  2410. *
  2411. **********************************************************************/
  2412. void
  2413. scm_load_goops ()
  2414. {
  2415. if (!goops_loaded_p)
  2416. scm_c_resolve_module ("oop goops");
  2417. }
  2418. SCM_SYMBOL (sym_o, "o");
  2419. SCM_SYMBOL (sym_x, "x");
  2420. SCM_KEYWORD (k_accessor, "accessor");
  2421. SCM_KEYWORD (k_getter, "getter");
  2422. SCM
  2423. scm_ensure_accessor (SCM name)
  2424. {
  2425. SCM var, gf;
  2426. var = scm_module_variable (scm_current_module (), name);
  2427. if (SCM_VARIABLEP (var) && !SCM_UNBNDP (SCM_VARIABLE_REF (var)))
  2428. gf = SCM_VARIABLE_REF (var);
  2429. else
  2430. gf = SCM_BOOL_F;
  2431. if (!SCM_IS_A_P (gf, scm_class_accessor))
  2432. {
  2433. gf = scm_make (scm_list_3 (scm_class_generic, k_name, name));
  2434. gf = scm_make (scm_list_5 (scm_class_accessor,
  2435. k_name, name, k_setter, gf));
  2436. }
  2437. return gf;
  2438. }
  2439. #ifdef GUILE_DEBUG
  2440. /*
  2441. * Debugging utilities
  2442. */
  2443. SCM_DEFINE (scm_pure_generic_p, "pure-generic?", 1, 0, 0,
  2444. (SCM obj),
  2445. "Return @code{#t} if @var{obj} is a pure generic.")
  2446. #define FUNC_NAME s_scm_pure_generic_p
  2447. {
  2448. return scm_from_bool (SCM_PUREGENERICP (obj));
  2449. }
  2450. #undef FUNC_NAME
  2451. #endif /* GUILE_DEBUG */
  2452. /*
  2453. * Initialization
  2454. */
  2455. SCM_DEFINE (scm_sys_goops_loaded, "%goops-loaded", 0, 0, 0,
  2456. (),
  2457. "Announce that GOOPS is loaded and perform initialization\n"
  2458. "on the C level which depends on the loaded GOOPS modules.")
  2459. #define FUNC_NAME s_scm_sys_goops_loaded
  2460. {
  2461. goops_loaded_p = 1;
  2462. var_compute_applicable_methods =
  2463. scm_module_variable (scm_module_goops, sym_compute_applicable_methods);
  2464. var_slot_unbound =
  2465. scm_module_variable (scm_module_goops, sym_slot_unbound);
  2466. var_slot_missing =
  2467. scm_module_variable (scm_module_goops, sym_slot_missing);
  2468. var_compute_cpl =
  2469. scm_module_variable (scm_module_goops, sym_compute_cpl);
  2470. var_no_applicable_method =
  2471. scm_module_variable (scm_module_goops, sym_no_applicable_method);
  2472. var_change_class =
  2473. scm_module_variable (scm_module_goops, sym_change_class);
  2474. setup_extended_primitive_generics ();
  2475. return SCM_UNSPECIFIED;
  2476. }
  2477. #undef FUNC_NAME
  2478. SCM scm_module_goops;
  2479. SCM
  2480. scm_init_goops_builtins (void)
  2481. {
  2482. scm_module_goops = scm_current_module ();
  2483. goops_rstate = scm_c_make_rstate ("GOOPS", 5);
  2484. #include "libguile/goops.x"
  2485. hell = scm_calloc (hell_size * sizeof (*hell));
  2486. hell_mutex = scm_make_mutex ();
  2487. create_basic_classes ();
  2488. create_standard_classes ();
  2489. create_smob_classes ();
  2490. create_struct_classes ();
  2491. create_port_classes ();
  2492. {
  2493. SCM name = scm_from_latin1_symbol ("no-applicable-method");
  2494. scm_no_applicable_method =
  2495. scm_make (scm_list_3 (scm_class_generic, k_name, name));
  2496. scm_module_define (scm_module_goops, name, scm_no_applicable_method);
  2497. }
  2498. return SCM_UNSPECIFIED;
  2499. }
  2500. void
  2501. scm_init_goops ()
  2502. {
  2503. scm_c_define_gsubr ("%init-goops-builtins", 0, 0, 0,
  2504. scm_init_goops_builtins);
  2505. }
  2506. /*
  2507. Local Variables:
  2508. c-file-style: "gnu"
  2509. End:
  2510. */