goops.c 89 KB

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