goops.c 88 KB

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