goops.c 83 KB

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