goops.scm 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326
  1. ;;;; goops.scm -- The Guile Object-Oriented Programming System
  2. ;;;;
  3. ;;;; Copyright (C) 1998-2003,2006,2009-2011,2013-2015,2017-2018,2021
  4. ;;;; Free Software Foundation, Inc.
  5. ;;;; Copyright (C) 1993-1998 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
  6. ;;;;
  7. ;;;; This library is free software; you can redistribute it and/or
  8. ;;;; modify it under the terms of the GNU Lesser General Public
  9. ;;;; License as published by the Free Software Foundation; either
  10. ;;;; version 3 of the License, or (at your option) any later version.
  11. ;;;;
  12. ;;;; This library is distributed in the hope that it will be useful,
  13. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;;;; Lesser General Public License for more details.
  16. ;;;;
  17. ;;;; You should have received a copy of the GNU Lesser General Public
  18. ;;;; License along with this library; if not, write to the Free Software
  19. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. ;;;;
  21. ;;;;
  22. ;;;; This file was based upon stklos.stk from the STk distribution
  23. ;;;; version 4.0.1 by Erick Gallesio <eg@unice.fr>.
  24. ;;;;
  25. (define-module (oop goops)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (ice-9 match)
  28. #:use-module ((ice-9 control) #:select (let/ec))
  29. #:use-module (ice-9 threads)
  30. #:use-module ((language tree-il primitives)
  31. :select (add-interesting-primitive!))
  32. #:export-syntax (define-class class standard-define-class
  33. define-generic define-accessor define-method
  34. define-extended-generic define-extended-generics
  35. method)
  36. #:export ( ;; The root of everything.
  37. <top>
  38. <class> <object>
  39. ;; Slot types.
  40. <slot>
  41. <foreign-slot> <protected-slot> <hidden-slot> <opaque-slot>
  42. <read-only-slot> <protected-opaque-slot>
  43. <protected-hidden-slot> <protected-read-only-slot>
  44. <scm-slot>
  45. ;; Redefinable classes.
  46. <redefinable-class>
  47. ;; Methods are implementations of generic functions.
  48. <method> <accessor-method>
  49. ;; Applicable objects, either procedures or applicable structs.
  50. <procedure-class> <applicable>
  51. <procedure> <primitive-generic>
  52. ;; Applicable structs.
  53. <applicable-struct-class> <applicable-struct-with-setter-class>
  54. <applicable-struct> <applicable-struct-with-setter>
  55. <generic> <extended-generic>
  56. <generic-with-setter> <extended-generic-with-setter>
  57. <accessor> <extended-accessor>
  58. ;; Types with their own allocated typecodes.
  59. <boolean> <char> <list> <pair> <null> <string> <symbol>
  60. <vector> <bytevector> <uvec> <foreign> <hashtable>
  61. <fluid> <dynamic-state> <frame> <vm> <vm-continuation>
  62. <keyword> <syntax> <atomic-box>
  63. ;; Numbers.
  64. <number> <complex> <real> <integer> <fraction>
  65. ;; Unknown.
  66. <unknown>
  67. ;; Particular SMOB data types. All SMOB types have
  68. ;; corresponding classes, which may be obtained via class-of,
  69. ;; once you have an instance. Perhaps FIXME to provide a
  70. ;; smob-type-name->class procedure.
  71. <promise> <thread> <mutex> <condition-variable>
  72. <regexp> <hook> <bitvector> <random-state>
  73. <directory> <array> <character-set>
  74. <dynamic-object> <guardian> <macro>
  75. ;; Modules.
  76. <module>
  77. ;; Ports.
  78. <port> <input-port> <output-port> <input-output-port>
  79. ;; Like SMOB types, all port types have their own classes,
  80. ;; which can be accessed via `class-of' once you have an
  81. ;; instance. Here we export bindings just for file ports.
  82. <file-port>
  83. <file-input-port> <file-output-port> <file-input-output-port>
  84. is-a? class-of
  85. ensure-metaclass ensure-metaclass-with-supers
  86. make-class
  87. make-generic ensure-generic
  88. make-extended-generic
  89. make-accessor ensure-accessor
  90. add-method!
  91. class-slot-ref class-slot-set! slot-unbound slot-missing
  92. slot-definition-name slot-definition-options
  93. slot-definition-allocation
  94. slot-definition-getter slot-definition-setter
  95. slot-definition-accessor
  96. slot-definition-init-value slot-definition-init-form
  97. slot-definition-init-thunk slot-definition-init-keyword
  98. slot-init-function class-slot-definition
  99. method-source
  100. compute-cpl compute-std-cpl compute-get-n-set compute-slots
  101. compute-getter-method compute-setter-method
  102. allocate-instance initialize make-instance make
  103. no-next-method no-applicable-method no-method
  104. change-class update-instance-for-different-class
  105. shallow-clone deep-clone
  106. class-redefinition
  107. apply-generic apply-method apply-methods
  108. compute-applicable-methods %compute-applicable-methods
  109. method-more-specific? sort-applicable-methods
  110. class-subclasses class-methods
  111. goops-error
  112. min-fixnum max-fixnum
  113. instance?
  114. slot-ref slot-set! slot-bound? slot-exists?
  115. class-name class-direct-supers class-direct-subclasses
  116. class-direct-methods class-direct-slots class-precedence-list
  117. class-slots
  118. generic-function-name
  119. generic-function-methods method-generic-function
  120. method-specializers method-formals
  121. primitive-generic-generic enable-primitive-generic!
  122. method-procedure accessor-method-slot-definition
  123. make find-method get-keyword))
  124. ;;;
  125. ;;; Booting GOOPS is a tortuous process. We begin by loading a small
  126. ;;; set of primitives from C.
  127. ;;;
  128. (eval-when (expand load eval)
  129. (load-extension (string-append "libguile-" (effective-version))
  130. "scm_init_goops_builtins")
  131. (add-interesting-primitive! 'class-of))
  132. ;;;
  133. ;;; We then define the slots that must appear in all classes (<class>
  134. ;;; objects) and slot definitions (<slot> objects). These slots must
  135. ;;; appear in order. We'll use this list to statically compute offsets
  136. ;;; for the various fields, to compute the struct layout for <class>
  137. ;;; instances, and to compute the slot definition lists for <class>.
  138. ;;; Because the list is needed at expansion-time, we define it as a
  139. ;;; macro.
  140. ;;;
  141. (define-syntax macro-fold-left
  142. (syntax-rules ()
  143. ((_ folder seed ()) seed)
  144. ((_ folder seed (head . tail))
  145. (macro-fold-left folder (folder head seed) tail))))
  146. (define-syntax macro-fold-right
  147. (syntax-rules ()
  148. ((_ folder seed ()) seed)
  149. ((_ folder seed (head . tail))
  150. (folder head (macro-fold-right folder seed tail)))))
  151. (define-syntax-rule (define-macro-folder macro-folder value ...)
  152. (define-syntax macro-folder
  153. (lambda (x)
  154. (syntax-case x ()
  155. ((_ fold visit seed)
  156. ;; The datum->syntax makes it as if each `value' were present
  157. ;; in the initial form, which allows them to be used as
  158. ;; (components of) introduced identifiers.
  159. #`(fold visit seed #,(datum->syntax #'visit '(value ...))))))))
  160. (define-macro-folder fold-class-slots
  161. (layout #:class <protected-read-only-slot>)
  162. (flags #:class <hidden-slot>)
  163. (instance-finalizer #:class <hidden-slot>)
  164. (print)
  165. (name #:class <protected-hidden-slot>)
  166. (nfields #:class <hidden-slot>)
  167. (%reserved-6 #:class <hidden-slot>)
  168. (%reserved-7 #:class <hidden-slot>)
  169. (direct-supers)
  170. (direct-slots)
  171. (direct-subclasses)
  172. (direct-methods)
  173. (cpl)
  174. (slots))
  175. (define-macro-folder fold-slot-slots
  176. (name #:init-keyword #:name)
  177. (allocation #:init-keyword #:allocation #:init-value #:instance)
  178. (init-keyword #:init-keyword #:init-keyword #:init-value #f)
  179. (init-form #:init-keyword #:init-form)
  180. (init-value #:init-keyword #:init-value)
  181. (init-thunk #:init-keyword #:init-thunk #:init-value #f)
  182. (options)
  183. (getter #:init-keyword #:getter #:init-value #f)
  184. (setter #:init-keyword #:setter #:init-value #f)
  185. (accessor #:init-keyword #:accessor #:init-value #f)
  186. ;; These last don't have #:init-keyword because they are meant to be
  187. ;; set by `allocate-slots', not in compute-effective-slot-definition.
  188. (slot-ref/raw #:init-value #f)
  189. (slot-ref #:init-value #f)
  190. (slot-set! #:init-value #f)
  191. (index #:init-value #f)
  192. (size #:init-value #f))
  193. ;;;
  194. ;;; Statically define variables for slot offsets: `class-index-layout'
  195. ;;; will be 0, `class-index-flags' will be 1, and so on, and the same
  196. ;;; for `slot-index-name' and such for <slot>.
  197. ;;;
  198. (let-syntax ((define-slot-indexer
  199. (syntax-rules ()
  200. ((_ define-index prefix)
  201. (define-syntax define-index
  202. (lambda (x)
  203. (define (id-append ctx a b)
  204. (datum->syntax ctx (symbol-append (syntax->datum a)
  205. (syntax->datum b))))
  206. (define (tail-length tail)
  207. (syntax-case tail ()
  208. ((begin) 0)
  209. ((visit head tail) (1+ (tail-length #'tail)))))
  210. (syntax-case x ()
  211. ((_ (name . _) tail)
  212. #`(begin
  213. (define-syntax #,(id-append #'name #'prefix #'name)
  214. (identifier-syntax #,(tail-length #'tail)))
  215. tail)))))))))
  216. (define-slot-indexer define-class-index class-index-)
  217. (define-slot-indexer define-slot-index slot-index-)
  218. (fold-class-slots macro-fold-left define-class-index (begin))
  219. (fold-slot-slots macro-fold-left define-slot-index (begin)))
  220. ;;;
  221. ;;; Structs that are vtables have a "flags" slot, which corresponds to
  222. ;;; class-index-flags. `vtable-flag-vtable' indicates that instances of
  223. ;;; a vtable are themselves vtables, and `vtable-flag-validated'
  224. ;;; indicates that the struct's layout has been validated. goops.c
  225. ;;; defines a few additional flags: one to indicate that a vtable is
  226. ;;; actually a class, one to indicate that instances of a class are slot
  227. ;;; definition objects (<slot> instances), one to indicate that this
  228. ;;; class has "static slot allocation" (meaning that its slots must
  229. ;;; always be allocated to the same indices in all subclasses), and two
  230. ;;; more flags used for redefinable classes (more below).
  231. ;;;
  232. (define vtable-flag-goops-metaclass
  233. (logior vtable-flag-vtable vtable-flag-goops-class))
  234. (define-inlinable (class-add-flags! class flags)
  235. (struct-set!/unboxed
  236. class
  237. class-index-flags
  238. (logior flags (struct-ref/unboxed class class-index-flags))))
  239. (define-inlinable (class-clear-flags! class flags)
  240. (struct-set!/unboxed
  241. class
  242. class-index-flags
  243. (logand (lognot flags) (struct-ref/unboxed class class-index-flags))))
  244. (define-inlinable (class-has-flags? class flags)
  245. (eqv? flags
  246. (logand (struct-ref/unboxed class class-index-flags) flags)))
  247. (define-inlinable (class? obj)
  248. (class-has-flags? (struct-vtable obj) vtable-flag-goops-metaclass))
  249. (define-inlinable (slot? obj)
  250. (and (struct? obj)
  251. (class-has-flags? (struct-vtable obj) vtable-flag-goops-slot)))
  252. (define-inlinable (instance? obj)
  253. (and (struct? obj)
  254. (class-has-flags? (struct-vtable obj) vtable-flag-goops-class)))
  255. (define (class-has-statically-allocated-slots? class)
  256. (class-has-flags? class vtable-flag-goops-static-slot-allocation))
  257. (define (class-has-indirect-instances? class)
  258. (class-has-flags? class vtable-flag-goops-indirect))
  259. (define (indirect-slots-need-migration? slots)
  260. (class-has-flags? (struct-vtable slots) vtable-flag-goops-needs-migration))
  261. ;;;
  262. ;;; Now that we know the slots that must be present in classes, and
  263. ;;; their offsets, we can create the root of the class hierarchy.
  264. ;;;
  265. ;;; Note that the `direct-supers', `direct-slots', `cpl', and `slots'
  266. ;;; fields will be updated later, once we can create slot definition
  267. ;;; objects and once we have definitions for <top> and <object>.
  268. ;;;
  269. (define <class>
  270. (let-syntax ((cons-layout
  271. ;; A simple way to compute class layout for the concrete
  272. ;; types used in <class>.
  273. (syntax-rules (<protected-read-only-slot>
  274. <hidden-slot>
  275. <protected-hidden-slot>)
  276. ((_ (name) tail)
  277. (string-append "pw" tail))
  278. ((_ (name #:class <protected-read-only-slot>) tail)
  279. (string-append "pw" tail))
  280. ((_ (name #:class <hidden-slot>) tail)
  281. (string-append "uh" tail))
  282. ((_ (name #:class <protected-hidden-slot>) tail)
  283. (string-append "ph" tail)))))
  284. (let* ((layout (fold-class-slots macro-fold-right cons-layout ""))
  285. (nfields (/ (string-length layout) 2))
  286. (<class> (%make-vtable-vtable layout)))
  287. (class-add-flags! <class> vtable-flag-goops-class)
  288. (struct-set! <class> class-index-name '<class>)
  289. (struct-set!/unboxed <class> class-index-nfields nfields)
  290. (struct-set! <class> class-index-direct-supers '())
  291. (struct-set! <class> class-index-direct-slots '())
  292. (struct-set! <class> class-index-direct-subclasses '())
  293. (struct-set! <class> class-index-direct-methods '())
  294. (struct-set! <class> class-index-cpl '())
  295. (struct-set! <class> class-index-slots '())
  296. <class>)))
  297. ;;;
  298. ;;; Accessors to fields of <class>.
  299. ;;;
  300. (define-syntax-rule (define-class-accessor name docstring field)
  301. (define (name obj)
  302. docstring
  303. (let ((val obj))
  304. (unless (class? val)
  305. (scm-error 'wrong-type-arg #f "Not a class: ~S"
  306. (list val) #f))
  307. (struct-ref val field))))
  308. (define-class-accessor class-name
  309. "Return the class name of @var{obj}."
  310. class-index-name)
  311. (define-class-accessor class-direct-supers
  312. "Return the direct superclasses of the class @var{obj}."
  313. class-index-direct-supers)
  314. (define-class-accessor class-direct-slots
  315. "Return the direct slots of the class @var{obj}."
  316. class-index-direct-slots)
  317. (define-class-accessor class-direct-subclasses
  318. "Return the direct subclasses of the class @var{obj}."
  319. class-index-direct-subclasses)
  320. (define-class-accessor class-direct-methods
  321. "Return the direct methods of the class @var{obj}."
  322. class-index-direct-methods)
  323. (define-class-accessor class-precedence-list
  324. "Return the class precedence list of the class @var{obj}."
  325. class-index-cpl)
  326. (define-class-accessor class-slots
  327. "Return the slot list of the class @var{obj}."
  328. class-index-slots)
  329. (define (class-subclasses c)
  330. "Compute a list of all subclasses of @var{c}, direct and indirect."
  331. (define (all-subclasses c)
  332. (cons c (append-map all-subclasses
  333. (class-direct-subclasses c))))
  334. (delete-duplicates (cdr (all-subclasses c)) eq?))
  335. (define (class-methods c)
  336. "Compute a list of all methods that specialize on @var{c} or
  337. subclasses of @var{c}."
  338. (delete-duplicates (append-map class-direct-methods
  339. (cons c (class-subclasses c)))
  340. eq?))
  341. (define (is-a? obj class)
  342. "Return @code{#t} if @var{obj} is an instance of @var{class}, or
  343. @code{#f} otherwise."
  344. (and (memq class (class-precedence-list (class-of obj))) #t))
  345. ;;;
  346. ;;; At this point, <class> is missing slot definitions, but we can't
  347. ;;; create slot definitions until we have a slot definition class.
  348. ;;; Continue with manual object creation until we're able to bootstrap
  349. ;;; more of the protocol. Again, the CPL and class hierarchy slots
  350. ;;; remain uninitialized.
  351. ;;;
  352. (define* (get-keyword key l #:optional default)
  353. "Determine an associated value for the keyword @var{key} from the list
  354. @var{l}. The list @var{l} has to consist of an even number of elements,
  355. where, starting with the first, every second element is a keyword,
  356. followed by its associated value. If @var{l} does not hold a value for
  357. @var{key}, the value @var{default} is returned."
  358. (unless (keyword? key)
  359. (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list key) #f))
  360. (let lp ((l l))
  361. (match l
  362. (() default)
  363. ((kw arg . l)
  364. (unless (keyword? kw)
  365. (scm-error 'wrong-type-arg #f "Not a keyword: ~S" (list kw) #f))
  366. (if (eq? kw key) arg (lp l))))))
  367. (define *unbound* (list 'unbound))
  368. (define-inlinable (unbound? x)
  369. (eq? x *unbound*))
  370. (define (%allocate-instance class)
  371. (let ((obj (allocate-struct class
  372. (struct-ref/unboxed class class-index-nfields))))
  373. (%clear-fields! obj *unbound*)
  374. obj))
  375. (define <slot>
  376. (let-syntax ((cons-layout
  377. ;; All slots are "pw" in <slot>.
  378. (syntax-rules ()
  379. ((_ _ tail) (string-append "pw" tail)))))
  380. (let* ((layout (fold-slot-slots macro-fold-right cons-layout ""))
  381. (nfields (/ (string-length layout) 2))
  382. (<slot> (make-struct/no-tail <class> (make-struct-layout layout))))
  383. (class-add-flags! <slot> (logior vtable-flag-goops-class
  384. vtable-flag-goops-slot))
  385. (struct-set! <slot> class-index-name '<slot>)
  386. (struct-set!/unboxed <slot> class-index-nfields nfields)
  387. (struct-set! <slot> class-index-direct-supers '())
  388. (struct-set! <slot> class-index-direct-slots '())
  389. (struct-set! <slot> class-index-direct-subclasses '())
  390. (struct-set! <slot> class-index-direct-methods '())
  391. (struct-set! <slot> class-index-cpl (list <slot>))
  392. (struct-set! <slot> class-index-slots '())
  393. <slot>)))
  394. ;;; Access to slot objects is performance-sensitive for slot-ref, so in
  395. ;;; addition to the type-checking accessors that we export, we also
  396. ;;; define some internal inlined helpers that just do an unchecked
  397. ;;; struct-ref in cases where we know the object must be a slot, as
  398. ;;; when accessing class-slots.
  399. ;;;
  400. (define-syntax-rule (define-slot-accessor name docstring %name field)
  401. (begin
  402. (define-syntax-rule (%name obj)
  403. (struct-ref obj field))
  404. (define (name obj)
  405. docstring
  406. (unless (slot? obj)
  407. (scm-error 'wrong-type-arg #f "Not a slot: ~S"
  408. (list obj) #f))
  409. (%name obj))))
  410. (define-slot-accessor slot-definition-name
  411. "Return the name of @var{obj}."
  412. %slot-definition-name slot-index-name)
  413. (define-slot-accessor slot-definition-allocation
  414. "Return the allocation of the slot @var{obj}."
  415. %slot-definition-allocation slot-index-allocation)
  416. (define-slot-accessor slot-definition-init-keyword
  417. "Return the init keyword of the slot @var{obj}, or @code{#f}."
  418. %slot-definition-init-keyword slot-index-init-keyword)
  419. (define-slot-accessor slot-definition-init-form
  420. "Return the init form of the slot @var{obj}, or the unbound value"
  421. %slot-definition-init-form slot-index-init-form)
  422. (define-slot-accessor slot-definition-init-value
  423. "Return the init value of the slot @var{obj}, or the unbound value."
  424. %slot-definition-init-value slot-index-init-value)
  425. (define-slot-accessor slot-definition-init-thunk
  426. "Return the init thunk of the slot @var{obj}, or @code{#f}."
  427. %slot-definition-init-thunk slot-index-init-thunk)
  428. (define-slot-accessor slot-definition-options
  429. "Return the initargs given when creating the slot @var{obj}."
  430. %slot-definition-options slot-index-options)
  431. (define-slot-accessor slot-definition-getter
  432. "Return the getter of the slot @var{obj}, or @code{#f}."
  433. %slot-definition-getter slot-index-getter)
  434. (define-slot-accessor slot-definition-setter
  435. "Return the setter of the slot @var{obj}, or @code{#f}."
  436. %slot-definition-setter slot-index-setter)
  437. (define-slot-accessor slot-definition-accessor
  438. "Return the accessor of the slot @var{obj}, or @code{#f}."
  439. %slot-definition-accessor slot-index-accessor)
  440. (define-slot-accessor slot-definition-slot-ref/raw
  441. "Return the raw slot-ref procedure of the slot @var{obj}."
  442. %slot-definition-slot-ref/raw slot-index-slot-ref/raw)
  443. (define-slot-accessor slot-definition-slot-ref
  444. "Return the slot-ref procedure of the slot @var{obj}."
  445. %slot-definition-slot-ref slot-index-slot-ref)
  446. (define-slot-accessor slot-definition-slot-set!
  447. "Return the slot-set! procedure of the slot @var{obj}."
  448. %slot-definition-slot-set! slot-index-slot-set!)
  449. (define-slot-accessor slot-definition-index
  450. "Return the allocated struct offset of the slot @var{obj}, or @code{#f}."
  451. %slot-definition-index slot-index-index)
  452. (define-slot-accessor slot-definition-size
  453. "Return the number fields used by the slot @var{obj}, or @code{#f}."
  454. %slot-definition-size slot-index-size)
  455. ;; Boot definition.
  456. (define (direct-slot-definition-class class initargs)
  457. (get-keyword #:class initargs <slot>))
  458. ;; Boot definition.
  459. (define (make-slot class initargs)
  460. (let ((slot (make-struct/no-tail class)))
  461. (define-syntax-rule (init-slot offset kw default)
  462. (struct-set! slot offset (get-keyword kw initargs default)))
  463. (init-slot slot-index-name #:name #f)
  464. (init-slot slot-index-allocation #:allocation #:instance)
  465. (init-slot slot-index-init-keyword #:init-keyword #f)
  466. (init-slot slot-index-init-form #:init-form *unbound*)
  467. (init-slot slot-index-init-value #:init-value *unbound*)
  468. (struct-set! slot slot-index-init-thunk
  469. (or (get-keyword #:init-thunk initargs #f)
  470. (let ((val (%slot-definition-init-value slot)))
  471. (if (unbound? val)
  472. #f
  473. (lambda () val)))))
  474. (struct-set! slot slot-index-options initargs)
  475. (init-slot slot-index-getter #:getter #f)
  476. (init-slot slot-index-setter #:setter #f)
  477. (init-slot slot-index-accessor #:accessor #f)
  478. (struct-set! slot slot-index-slot-ref/raw #f)
  479. (struct-set! slot slot-index-slot-ref #f)
  480. (struct-set! slot slot-index-slot-set! #f)
  481. (struct-set! slot slot-index-index #f)
  482. (struct-set! slot slot-index-size #f)
  483. slot))
  484. ;; Boot definition.
  485. (define (make class . args)
  486. (unless (memq <slot> (class-precedence-list class))
  487. (error "Unsupported class: ~S" class))
  488. (make-slot class args))
  489. ;; Boot definition.
  490. (define (compute-direct-slot-definition class initargs)
  491. (apply make (direct-slot-definition-class class initargs) initargs))
  492. (define (compute-direct-slot-definition-initargs class slot-spec)
  493. (match slot-spec
  494. ((? symbol? name) (list #:name name))
  495. (((? symbol? name) . initargs)
  496. (cons* #:name name
  497. ;; If there is an #:init-form, the `class' macro will have
  498. ;; already added an #:init-thunk. Still, if there isn't an
  499. ;; #:init-thunk already but we do have an #:init-value,
  500. ;; synthesize an #:init-thunk initarg. This will ensure
  501. ;; that the #:init-thunk gets passed on to the effective
  502. ;; slot definition too.
  503. (if (get-keyword #:init-thunk initargs)
  504. initargs
  505. (let ((value (get-keyword #:init-value initargs *unbound*)))
  506. (if (unbound? value)
  507. initargs
  508. (cons* #:init-thunk (lambda () value) initargs))))))))
  509. (let ()
  510. (define-syntax cons-slot
  511. (syntax-rules ()
  512. ((_ (name #:class class) tail)
  513. ;; Special case to avoid referencing specialized <slot> kinds,
  514. ;; which are not defined yet.
  515. (cons (list 'name) tail))
  516. ((_ (name . initargs) tail)
  517. (cons (list 'name . initargs) tail))))
  518. (define-syntax-rule (initialize-direct-slots! class fold-slots)
  519. (let ((specs (fold-slots macro-fold-right cons-slot '())))
  520. (define (make-direct-slot-definition spec)
  521. (let ((initargs (compute-direct-slot-definition-initargs class spec)))
  522. (compute-direct-slot-definition class initargs)))
  523. (struct-set! class class-index-direct-slots
  524. (map make-direct-slot-definition specs))))
  525. (initialize-direct-slots! <class> fold-class-slots)
  526. (initialize-direct-slots! <slot> fold-slot-slots))
  527. ;;;
  528. ;;; OK, at this point we have initialized `direct-slots' on both <class>
  529. ;;; and <slot>. We need to define a standard way to make subclasses:
  530. ;;; how to compute the precedence list of subclasses, how to compute the
  531. ;;; list of slots in a subclass, and what layout to use for instances of
  532. ;;; those classes.
  533. ;;;
  534. (define (compute-std-cpl c get-direct-supers)
  535. "The standard class precedence list computation algorithm."
  536. (define (only-non-null lst)
  537. (filter (lambda (l) (not (null? l))) lst))
  538. (define (merge-lists reversed-partial-result inputs)
  539. (cond
  540. ((every null? inputs)
  541. (reverse! reversed-partial-result))
  542. (else
  543. (let* ((candidate (lambda (c)
  544. (and (not (any (lambda (l)
  545. (memq c (cdr l)))
  546. inputs))
  547. c)))
  548. (candidate-car (lambda (l)
  549. (and (not (null? l))
  550. (candidate (car l)))))
  551. (next (any candidate-car inputs)))
  552. (unless next
  553. (goops-error "merge-lists: Inconsistent precedence graph"))
  554. (let ((remove-next (lambda (l)
  555. (if (eq? (car l) next)
  556. (cdr l)
  557. l))))
  558. (merge-lists (cons next reversed-partial-result)
  559. (only-non-null (map remove-next inputs))))))))
  560. (let ((c-direct-supers (get-direct-supers c)))
  561. (merge-lists (list c)
  562. (only-non-null (append (map class-precedence-list
  563. c-direct-supers)
  564. (list c-direct-supers))))))
  565. ;; This version of compute-cpl is replaced with a generic function once
  566. ;; GOOPS has booted.
  567. (define (compute-cpl class)
  568. (compute-std-cpl class class-direct-supers))
  569. (define (effective-slot-definition-class class slot)
  570. (class-of slot))
  571. (define (compute-effective-slot-definition class slot)
  572. ;; FIXME: Support slot being a list of slots, as in CLOS.
  573. (apply make
  574. (effective-slot-definition-class class slot)
  575. (slot-definition-options slot)))
  576. (define (build-slots-list dslots cpl)
  577. (define (slot-memq slot slots)
  578. (let ((name (%slot-definition-name slot)))
  579. (let lp ((slots slots))
  580. (match slots
  581. (() #f)
  582. ((slot . slots)
  583. (or (eq? (%slot-definition-name slot) name) (lp slots)))))))
  584. (define (check-cpl slots static-slots)
  585. (match static-slots
  586. (() #t)
  587. ((static-slot . static-slots)
  588. (when (slot-memq static-slot slots)
  589. (scm-error 'misc-error #f
  590. "statically allocated inherited field cannot be redefined: ~a"
  591. (list (%slot-definition-name static-slot)) '()))
  592. (check-cpl slots static-slots))))
  593. (define (remove-duplicate-slots slots)
  594. (let lp ((slots (reverse slots)) (res '()) (seen '()))
  595. (match slots
  596. (() res)
  597. ((slot . slots)
  598. (let ((name (%slot-definition-name slot)))
  599. (if (memq name seen)
  600. (lp slots res seen)
  601. (lp slots (cons slot res) (cons name seen))))))))
  602. ;; For subclases of <class> and <slot>, we need to ensure that the
  603. ;; <class> or <slot> slots come first.
  604. (let ((static-slots
  605. (match (filter class-has-statically-allocated-slots? (cdr cpl))
  606. (() #f)
  607. ((class) (struct-ref class class-index-direct-slots))
  608. (classes
  609. (error "can't subtype multiple classes with static slot allocation"
  610. classes)))))
  611. (when static-slots
  612. (check-cpl dslots static-slots))
  613. (let lp ((cpl (cdr cpl)) (res dslots) (static-slots '()))
  614. (match cpl
  615. (() (remove-duplicate-slots (append static-slots res)))
  616. ((head . cpl)
  617. (let ((new-slots (struct-ref head class-index-direct-slots)))
  618. (cond
  619. ((not static-slots)
  620. (lp cpl (append new-slots res) static-slots))
  621. ((class-has-statically-allocated-slots? head)
  622. ;; Move static slots to the head of the list.
  623. (lp cpl res new-slots))
  624. (else
  625. (check-cpl new-slots static-slots)
  626. (lp cpl (append new-slots res) static-slots)))))))))
  627. ;; Boot definition.
  628. (define (compute-get-n-set class slot)
  629. (let ((index (struct-ref/unboxed class class-index-nfields)))
  630. (struct-set!/unboxed class class-index-nfields (1+ index))
  631. index))
  632. ;;; Pre-generate getters and setters for the first 20 slots.
  633. (define-syntax define-standard-accessor-method
  634. (lambda (stx)
  635. (define num-standard-pre-cache 20)
  636. (syntax-case stx ()
  637. ((_ ((proc n) arg ...) body)
  638. #`(define proc
  639. (let ((cache (vector #,@(map (lambda (n*)
  640. #`(lambda (arg ...)
  641. (let ((n #,n*))
  642. body)))
  643. (iota num-standard-pre-cache)))))
  644. (lambda (n)
  645. (if (< n #,num-standard-pre-cache)
  646. (vector-ref cache n)
  647. (lambda (arg ...) body)))))))))
  648. (define-standard-accessor-method ((bound-check-get n) o)
  649. (let ((x (struct-ref o n)))
  650. (if (unbound? x)
  651. (slot-unbound o)
  652. x)))
  653. (define-standard-accessor-method ((standard-get n) o)
  654. (struct-ref o n))
  655. (define-standard-accessor-method ((standard-set n) o v)
  656. (struct-set! o n v))
  657. (define-standard-accessor-method ((unboxed-get n) o)
  658. (struct-ref/unboxed o n))
  659. (define-standard-accessor-method ((unboxed-set n) o v)
  660. (struct-set!/unboxed o n v))
  661. ;; Boot definitions.
  662. (define (opaque-slot? slot) #f)
  663. (define (read-only-slot? slot) #f)
  664. (define (unboxed-slot? slot)
  665. (memq (%slot-definition-name slot)
  666. '(flags instance-finalizer nfields %reserved-6 %reserved-7)))
  667. (define (allocate-slots class slots)
  668. "Transform the computed list of direct slot definitions @var{slots}
  669. into a corresponding list of effective slot definitions, allocating
  670. slots as we go."
  671. (define (make-effective-slot-definition slot)
  672. ;; `compute-get-n-set' is expected to mutate `nfields' if it
  673. ;; allocates a field to the object. Pretty strange, but we preserve
  674. ;; the behavior for backward compatibility.
  675. (let* ((slot (compute-effective-slot-definition class slot))
  676. (name (%slot-definition-name slot))
  677. (index (struct-ref/unboxed class class-index-nfields))
  678. (g-n-s (compute-get-n-set class slot))
  679. (size (- (struct-ref/unboxed class class-index-nfields) index)))
  680. (call-with-values
  681. (lambda ()
  682. (match g-n-s
  683. ((? integer?)
  684. (unless (= size 1)
  685. (error "unexpected return from compute-get-n-set"))
  686. (cond
  687. ((unboxed-slot? slot)
  688. (let ((get (unboxed-get g-n-s)))
  689. (values get get (unboxed-set g-n-s))))
  690. (else
  691. (values (standard-get g-n-s)
  692. (if (slot-definition-init-thunk slot)
  693. (standard-get g-n-s)
  694. (bound-check-get g-n-s))
  695. (standard-set g-n-s)))))
  696. (((? procedure? get) (? procedure? set))
  697. (values get
  698. (lambda (o)
  699. (let ((value (get o)))
  700. (if (unbound? value)
  701. (slot-unbound class o name)
  702. value)))
  703. set))))
  704. (lambda (get/raw get set)
  705. (let ((get (if (opaque-slot? slot)
  706. (lambda (o)
  707. (error "Slot is opaque" name))
  708. get))
  709. (set (cond
  710. ((opaque-slot? slot)
  711. (lambda (o v)
  712. (error "Slot is opaque" name)))
  713. ((read-only-slot? slot)
  714. (if (unboxed-slot? slot)
  715. (lambda (o v)
  716. (let ((v* (get/raw o)))
  717. (if (zero? v*)
  718. ;; Allow initialization.
  719. (set o v)
  720. (error "Slot is read-only" name))))
  721. (lambda (o v)
  722. (let ((v* (get/raw o)))
  723. (if (unbound? v*)
  724. ;; Allow initialization.
  725. (set o v)
  726. (error "Slot is read-only" name))))))
  727. (else set))))
  728. (struct-set! slot slot-index-slot-ref/raw get/raw)
  729. (struct-set! slot slot-index-slot-ref get)
  730. (struct-set! slot slot-index-slot-set! set)
  731. (struct-set! slot slot-index-index index)
  732. (struct-set! slot slot-index-size size))))
  733. slot))
  734. (struct-set!/unboxed class class-index-nfields 0)
  735. (map-in-order make-effective-slot-definition slots))
  736. (define (%compute-layout slots nfields is-class?)
  737. (define (slot-protection-and-kind slot)
  738. (define (subclass? class parent)
  739. (memq parent (class-precedence-list class)))
  740. (let ((type (get-keyword #:class (%slot-definition-options slot))))
  741. (if (and type (subclass? type <foreign-slot>))
  742. (values (cond
  743. ((subclass? type <protected-slot>) #\p)
  744. (else #\u))
  745. (cond
  746. ((subclass? type <hidden-slot>) #\h)
  747. (else #\w)))
  748. (values #\p #\w))))
  749. (let ((layout (make-string (* nfields 2))))
  750. (let lp ((n 0) (slots slots))
  751. (match slots
  752. (()
  753. (unless (= n nfields) (error "bad nfields"))
  754. (when is-class?
  755. (let ((class-layout (struct-ref <class> class-index-layout)))
  756. (unless (string-prefix? (symbol->string class-layout) layout)
  757. (error "bad layout for class"))))
  758. layout)
  759. ((slot . slots)
  760. (unless (= n (%slot-definition-index slot)) (error "bad allocation"))
  761. (call-with-values (lambda () (slot-protection-and-kind slot))
  762. (lambda (protection kind)
  763. (let init ((n n) (size (%slot-definition-size slot)))
  764. (cond
  765. ((zero? size) (lp n slots))
  766. (else
  767. (unless (< n nfields) (error "bad nfields"))
  768. (string-set! layout (* n 2) protection)
  769. (string-set! layout (1+ (* n 2)) kind)
  770. (init (1+ n) (1- size))))))))))))
  771. ;;;
  772. ;;; With all of this, we are now able to define subclasses of <class>.
  773. ;;;
  774. (define (%prep-layout! class)
  775. (let* ((is-class? (and (memq <class> (struct-ref class class-index-cpl)) #t))
  776. (layout (%compute-layout (struct-ref class class-index-slots)
  777. (struct-ref/unboxed class class-index-nfields)
  778. is-class?)))
  779. (%init-layout! class layout)))
  780. (define (make-standard-class class name dsupers dslots)
  781. (let ((z (make-struct/no-tail class)))
  782. (define (make-direct-slot-definition dslot)
  783. (let ((initargs (compute-direct-slot-definition-initargs z dslot)))
  784. (compute-direct-slot-definition z initargs)))
  785. (struct-set! z class-index-name name)
  786. (struct-set!/unboxed z class-index-nfields 0)
  787. (struct-set! z class-index-direct-supers dsupers)
  788. (struct-set! z class-index-direct-subclasses '())
  789. (struct-set! z class-index-direct-methods '())
  790. (let ((cpl (compute-cpl z)))
  791. (struct-set! z class-index-cpl cpl)
  792. (when (memq <slot> cpl)
  793. (class-add-flags! z vtable-flag-goops-slot))
  794. (let* ((dslots (map make-direct-slot-definition dslots))
  795. (slots (allocate-slots z (build-slots-list dslots cpl))))
  796. (struct-set! z class-index-direct-slots dslots)
  797. (struct-set! z class-index-slots slots)))
  798. (for-each
  799. (lambda (super)
  800. (let ((subclasses (struct-ref super class-index-direct-subclasses)))
  801. (struct-set! super class-index-direct-subclasses
  802. (cons z subclasses))))
  803. dsupers)
  804. (%prep-layout! z)
  805. z))
  806. (define-syntax define-standard-class
  807. (syntax-rules ()
  808. ((define-standard-class name (super ...) #:metaclass meta slot ...)
  809. (define name
  810. (make-standard-class meta 'name (list super ...) '(slot ...))))
  811. ((define-standard-class name (super ...) slot ...)
  812. (define-standard-class name (super ...) #:metaclass <class> slot ...))))
  813. ;;;
  814. ;;; Sweet! Now we can define <top> and <object>, and finish
  815. ;;; initializing the `direct-subclasses', `direct-supers', and `cpl'
  816. ;;; slots of <class>.
  817. ;;;
  818. (define-standard-class <top> ())
  819. (define-standard-class <object> (<top>))
  820. ;; The inheritance links for <top>, <object>, <class>, and <slot> were
  821. ;; partially initialized. Correct them here.
  822. (struct-set! <object> class-index-direct-subclasses (list <slot> <class>))
  823. (struct-set! <class> class-index-direct-supers (list <object>))
  824. (struct-set! <slot> class-index-direct-supers (list <object>))
  825. (struct-set! <class> class-index-cpl (list <class> <object> <top>))
  826. (struct-set! <slot> class-index-cpl (list <slot> <object> <top>))
  827. ;;;
  828. ;;; We can also define the various slot types, and finish initializing
  829. ;;; `direct-slots' and `slots' on <class> and <slot>.
  830. ;;;
  831. (define-standard-class <foreign-slot> (<slot>))
  832. (define-standard-class <protected-slot> (<foreign-slot>))
  833. (define-standard-class <hidden-slot> (<foreign-slot>))
  834. (define-standard-class <opaque-slot> (<foreign-slot>))
  835. (define-standard-class <read-only-slot> (<foreign-slot>))
  836. (define-standard-class <protected-opaque-slot> (<protected-slot>
  837. <opaque-slot>))
  838. (define-standard-class <protected-hidden-slot> (<protected-slot>
  839. <hidden-slot>))
  840. (define-standard-class <protected-read-only-slot> (<protected-slot>
  841. <read-only-slot>))
  842. (define-standard-class <scm-slot> (<protected-slot>))
  843. (define (opaque-slot? slot) (is-a? slot <opaque-slot>))
  844. (define (read-only-slot? slot) (is-a? slot <read-only-slot>))
  845. (define (unboxed-slot? slot)
  846. (and (is-a? slot <foreign-slot>)
  847. (not (is-a? slot <protected-slot>))))
  848. ;;;
  849. ;;; Finally! Initialize `direct-slots' and `slots' on <class>, and
  850. ;;; `slots' on <slot>.
  851. ;;;
  852. (let ()
  853. (define-syntax-rule (cons-slot (name . initargs) tail)
  854. (cons (list 'name . initargs) tail))
  855. (define-syntax-rule (initialize-direct-slots! class fold-slots)
  856. (let ((specs (fold-slots macro-fold-right cons-slot '())))
  857. (define (make-direct-slot-definition spec)
  858. (let ((initargs (compute-direct-slot-definition-initargs class spec)))
  859. (compute-direct-slot-definition class initargs)))
  860. (struct-set! class class-index-direct-slots
  861. (map make-direct-slot-definition specs))))
  862. ;; Boot definition that avoids munging nfields.
  863. (define (allocate-slots class slots)
  864. (define (make-effective-slot-definition slot index)
  865. (let* ((slot (compute-effective-slot-definition class slot))
  866. (get/raw (standard-get index))
  867. (set/raw (standard-set index)))
  868. (struct-set! slot slot-index-slot-ref/raw (standard-get index))
  869. (struct-set! slot slot-index-slot-ref
  870. (if (slot-definition-init-thunk slot)
  871. get/raw
  872. (bound-check-get index)))
  873. (struct-set! slot slot-index-slot-set!
  874. (if (read-only-slot? slot)
  875. (lambda (o v)
  876. (let ((v* (get/raw o)))
  877. (if (unbound? v*)
  878. ;; Allow initialization.
  879. (set/raw o v)
  880. (error "Slot is read-only" slot))))
  881. set/raw))
  882. (struct-set! slot slot-index-index index)
  883. (struct-set! slot slot-index-size 1)
  884. slot))
  885. (map make-effective-slot-definition slots (iota (length slots))))
  886. (define (initialize-slots! class)
  887. (let ((slots (build-slots-list (class-direct-slots class)
  888. (class-precedence-list class))))
  889. (struct-set! class class-index-slots (allocate-slots class slots))))
  890. ;; Finish initializing <class> with the specialized slot kinds.
  891. (initialize-direct-slots! <class> fold-class-slots)
  892. (initialize-slots! <class>)
  893. (initialize-slots! <slot>)
  894. ;; Now that we're all done with that, mark <class> and <slot> as
  895. ;; static.
  896. (class-add-flags! <class> vtable-flag-goops-static-slot-allocation)
  897. (class-add-flags! <slot> vtable-flag-goops-static-slot-allocation))
  898. ;;;
  899. ;;; Now, to build out the class hierarchy.
  900. ;;;
  901. (define-standard-class <procedure-class> (<class>))
  902. (define-standard-class <applicable-struct-class>
  903. (<procedure-class>))
  904. (class-add-flags! <applicable-struct-class>
  905. vtable-flag-applicable-vtable)
  906. (define-standard-class <applicable-struct-with-setter-class>
  907. (<applicable-struct-class>))
  908. (class-add-flags! <applicable-struct-with-setter-class>
  909. vtable-flag-setter-vtable)
  910. (define-standard-class <applicable> (<top>))
  911. (define-standard-class <applicable-struct> (<object> <applicable>)
  912. #:metaclass <applicable-struct-class>
  913. procedure)
  914. (define-standard-class <applicable-struct-with-setter> (<applicable-struct>)
  915. #:metaclass <applicable-struct-with-setter-class>
  916. setter)
  917. (define-standard-class <generic> (<applicable-struct>)
  918. #:metaclass <applicable-struct-class>
  919. methods
  920. (n-specialized #:init-value 0)
  921. (extended-by #:init-value ())
  922. effective-methods)
  923. (define-standard-class <extended-generic> (<generic>)
  924. #:metaclass <applicable-struct-class>
  925. (extends #:init-value ()))
  926. (define-standard-class <generic-with-setter> (<generic>
  927. <applicable-struct-with-setter>)
  928. #:metaclass <applicable-struct-with-setter-class>)
  929. (define-standard-class <accessor> (<generic-with-setter>)
  930. #:metaclass <applicable-struct-with-setter-class>)
  931. (define-standard-class <extended-generic-with-setter> (<extended-generic>
  932. <generic-with-setter>)
  933. #:metaclass <applicable-struct-with-setter-class>)
  934. (define-standard-class <extended-accessor> (<accessor>
  935. <extended-generic-with-setter>)
  936. #:metaclass <applicable-struct-with-setter-class>)
  937. (define-standard-class <method> (<object>)
  938. generic-function
  939. specializers
  940. procedure
  941. formals
  942. body
  943. make-procedure)
  944. (define-standard-class <accessor-method> (<method>)
  945. (slot-definition #:init-keyword #:slot-definition))
  946. (define-standard-class <boolean> (<top>))
  947. (define-standard-class <char> (<top>))
  948. (define-standard-class <list> (<top>))
  949. ;; Not all pairs are lists, but there is code out there that relies on
  950. ;; (is-a? '(1 2 3) <list>) to work. Terrible. How to fix?
  951. (define-standard-class <pair> (<list>))
  952. (define-standard-class <null> (<list>))
  953. (define-standard-class <string> (<top>))
  954. (define-standard-class <symbol> (<top>))
  955. (define-standard-class <vector> (<top>))
  956. (define-standard-class <foreign> (<top>))
  957. (define-standard-class <hashtable> (<top>))
  958. (define-standard-class <fluid> (<top>))
  959. (define-standard-class <dynamic-state> (<top>))
  960. (define-standard-class <frame> (<top>))
  961. (define-standard-class <vm-continuation> (<top>))
  962. (define-standard-class <bytevector> (<top>))
  963. (define-standard-class <uvec> (<bytevector>))
  964. (define-standard-class <array> (<top>))
  965. (define-standard-class <bitvector> (<top>))
  966. (define-standard-class <number> (<top>))
  967. (define-standard-class <complex> (<number>))
  968. (define-standard-class <real> (<complex>))
  969. (define-standard-class <integer> (<real>))
  970. (define-standard-class <fraction> (<real>))
  971. (define-standard-class <keyword> (<top>))
  972. (define-standard-class <syntax> (<top>))
  973. (define-standard-class <atomic-box> (<top>))
  974. (define-standard-class <unknown> (<top>))
  975. (define-standard-class <procedure> (<applicable>)
  976. #:metaclass <procedure-class>)
  977. (define-standard-class <primitive-generic> (<procedure>)
  978. #:metaclass <procedure-class>)
  979. (define-standard-class <port> (<top>))
  980. (define-standard-class <input-port> (<port>))
  981. (define-standard-class <output-port> (<port>))
  982. (define-standard-class <input-output-port> (<input-port> <output-port>))
  983. (define (inherit-applicable! class)
  984. "An internal routine to redefine a SMOB class that was added after
  985. GOOPS was loaded, and on which scm_set_smob_apply installed an apply
  986. function."
  987. (unless (memq <applicable> (class-precedence-list class))
  988. (unless (null? (class-slots class))
  989. (error "SMOB object has slots?"))
  990. (for-each
  991. (lambda (super)
  992. (let ((subclasses (struct-ref super class-index-direct-subclasses)))
  993. (struct-set! super class-index-direct-subclasses
  994. (delq class subclasses))))
  995. (struct-ref class class-index-direct-supers))
  996. (struct-set! class class-index-direct-supers (list <applicable>))
  997. (struct-set! class class-index-cpl (compute-cpl class))
  998. (let ((subclasses (struct-ref <applicable> class-index-direct-subclasses)))
  999. (struct-set! <applicable> class-index-direct-subclasses
  1000. (cons class subclasses)))))
  1001. ;;;
  1002. ;;; At this point we have defined the class hierarchy, and it's time to
  1003. ;;; move on to instance allocation and generics. Once we have generics,
  1004. ;;; we'll fill out the metaobject protocol.
  1005. ;;;
  1006. ;;; Here we define a limited version of `make', so that we can allocate
  1007. ;;; instances of specific classes. This definition will be replaced
  1008. ;;; later.
  1009. ;;;
  1010. (define (%invalidate-method-cache! gf)
  1011. (slot-set! gf 'effective-methods '())
  1012. (recompute-generic-function-dispatch-procedure! gf))
  1013. ;; Boot definition.
  1014. (define (invalidate-method-cache! gf)
  1015. (%invalidate-method-cache! gf))
  1016. (define (make class . args)
  1017. (cond
  1018. ((or (eq? class <generic>) (eq? class <accessor>))
  1019. (let ((z (make-struct/no-tail class #f '() 0 '())))
  1020. (set-procedure-property! z 'name (get-keyword #:name args #f))
  1021. (invalidate-method-cache! z)
  1022. (when (eq? class <accessor>)
  1023. (let ((setter (get-keyword #:setter args #f)))
  1024. (when setter
  1025. (slot-set! z 'setter setter))))
  1026. z))
  1027. (else
  1028. (let ((z (%allocate-instance class)))
  1029. (cond
  1030. ((or (eq? class <method>) (eq? class <accessor-method>))
  1031. (for-each (match-lambda
  1032. ((kw slot default)
  1033. (slot-set! z slot (get-keyword kw args default))))
  1034. '((#:generic-function generic-function #f)
  1035. (#:specializers specializers ())
  1036. (#:procedure procedure #f)
  1037. (#:formals formals ())
  1038. (#:body body ())
  1039. (#:make-procedure make-procedure #f))))
  1040. ((memq <class> (class-precedence-list class))
  1041. (class-add-flags! z vtable-flag-goops-class)
  1042. (for-each (match-lambda
  1043. ((kw slot default)
  1044. (slot-set! z slot (get-keyword kw args default))))
  1045. '((#:name name ???)
  1046. (#:dsupers direct-supers ())
  1047. (#:slots direct-slots ()))))
  1048. (else
  1049. (error "boot `make' does not support this class" class)))
  1050. z))))
  1051. ;;;
  1052. ;;; Slot access.
  1053. ;;;
  1054. (define-inlinable (%class-slot-definition class slot-name kt kf)
  1055. (let lp ((slots (struct-ref class class-index-slots)))
  1056. (match slots
  1057. ((slot . slots)
  1058. (if (eq? (%slot-definition-name slot) slot-name)
  1059. (kt slot)
  1060. (lp slots)))
  1061. (_ (kf)))))
  1062. (define (class-slot-definition class slot-name)
  1063. (unless (class? class)
  1064. (scm-error 'wrong-type-arg #f "Not a class: ~S" (list class) #f))
  1065. (%class-slot-definition class slot-name
  1066. (lambda (slot) slot)
  1067. (lambda () #f)))
  1068. (define (slot-ref obj slot-name)
  1069. "Return the value from @var{obj}'s slot with the nam var{slot_name}."
  1070. (let ((class (class-of obj)))
  1071. (define (have-slot slot)
  1072. ((%slot-definition-slot-ref slot) obj))
  1073. (define (no-slot)
  1074. (unless (symbol? slot-name)
  1075. (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
  1076. (list slot-name) #f))
  1077. (let ((val (slot-missing class obj slot-name)))
  1078. (if (unbound? val)
  1079. (slot-unbound class obj slot-name)
  1080. val)))
  1081. (%class-slot-definition class slot-name have-slot no-slot)))
  1082. (define (slot-set! obj slot-name value)
  1083. "Set the slot named @var{slot_name} of @var{obj} to @var{value}."
  1084. (let ((class (class-of obj)))
  1085. (define (have-slot slot)
  1086. ((%slot-definition-slot-set! slot) obj value))
  1087. (define (no-slot)
  1088. (unless (symbol? slot-name)
  1089. (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
  1090. (list slot-name) #f))
  1091. (slot-missing class obj slot-name value))
  1092. (%class-slot-definition class slot-name have-slot no-slot)))
  1093. (define (slot-bound? obj slot-name)
  1094. "Return the value from @var{obj}'s slot with the nam var{slot_name}."
  1095. (let ((class (class-of obj)))
  1096. (define (have-slot slot)
  1097. (not (unbound? ((%slot-definition-slot-ref/raw slot) obj))))
  1098. (define (no-slot)
  1099. (unless (symbol? slot-name)
  1100. (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
  1101. (list slot-name) #f))
  1102. (not (unbound? (slot-missing class obj slot-name))))
  1103. (%class-slot-definition class slot-name have-slot no-slot)))
  1104. (define (slot-exists? obj slot-name)
  1105. "Return @code{#t} if @var{obj} has a slot named @var{slot_name}."
  1106. (define (have-slot slot) #t)
  1107. (define (no-slot)
  1108. (unless (symbol? slot-name)
  1109. (scm-error 'wrong-type-arg #f "Not a symbol: ~S"
  1110. (list slot-name) #f))
  1111. #f)
  1112. (%class-slot-definition (class-of obj) slot-name have-slot no-slot))
  1113. ;;;
  1114. ;;; Method accessors.
  1115. ;;;
  1116. (define (method-generic-function obj)
  1117. "Return the generic function for the method @var{obj}."
  1118. (unless (is-a? obj <method>)
  1119. (scm-error 'wrong-type-arg #f "Not a method: ~S"
  1120. (list obj) #f))
  1121. (slot-ref obj 'generic-function))
  1122. (define (method-specializers obj)
  1123. "Return specializers of the method @var{obj}."
  1124. (unless (is-a? obj <method>)
  1125. (scm-error 'wrong-type-arg #f "Not a method: ~S"
  1126. (list obj) #f))
  1127. (slot-ref obj 'specializers))
  1128. (define (method-procedure obj)
  1129. "Return the procedure of the method @var{obj}."
  1130. (unless (is-a? obj <method>)
  1131. (scm-error 'wrong-type-arg #f "Not a method: ~S"
  1132. (list obj) #f))
  1133. (slot-ref obj 'procedure))
  1134. ;;;
  1135. ;;; Generic functions!
  1136. ;;;
  1137. ;;; Generic functions have an applicable-methods cache associated with
  1138. ;;; them. Every distinct set of types that is dispatched through a
  1139. ;;; generic adds an entry to the cache. A composite dispatch procedure
  1140. ;;; is recomputed every time an entry gets added to the cache, or when
  1141. ;;; the cache is invalidated.
  1142. ;;;
  1143. ;;; In steady-state, this dispatch procedure is never regenerated; but
  1144. ;;; during warm-up there is some churn.
  1145. ;;;
  1146. ;;; So what is the deal if warm-up happens in a multithreaded context?
  1147. ;;; There is indeed a window between missing the cache for a certain set
  1148. ;;; of arguments, and then updating the cache with the newly computed
  1149. ;;; applicable methods. One of the updaters is liable to lose their new
  1150. ;;; entry.
  1151. ;;;
  1152. ;;; This is actually OK though, because a subsequent cache miss for the
  1153. ;;; race loser will just cause memoization to try again. The cache will
  1154. ;;; eventually be consistent. We're not mutating the old part of the
  1155. ;;; cache, just consing on the new entry.
  1156. ;;;
  1157. ;;; It doesn't even matter if the dispatch procedure and the cache are
  1158. ;;; inconsistent -- most likely the type-set that lost the dispatch
  1159. ;;; procedure race will simply re-trigger a memoization, but since the
  1160. ;;; winner isn't in the effective-methods cache, it will likely also
  1161. ;;; re-trigger a memoization, and the cache will finally be consistent.
  1162. ;;; As you can see there is a possibility for ping-pong effects, but
  1163. ;;; it's unlikely given the shortness of the window between slot-set!
  1164. ;;; invocations.
  1165. ;;;
  1166. ;;; We probably do need to use atomic access primitives to correctly
  1167. ;;; handle concurrency, but that's a more general Guile concern.
  1168. ;;;
  1169. (define-syntax arity-case
  1170. (lambda (x)
  1171. (syntax-case x ()
  1172. ;; (arity-case n 2 foo bar)
  1173. ;; => (case n
  1174. ;; ((0) (foo))
  1175. ;; ((1) (foo a))
  1176. ;; ((2) (foo a b))
  1177. ;; (else bar))
  1178. ((arity-case n max form alternate)
  1179. (let ((max (syntax->datum #'max)))
  1180. #`(case n
  1181. #,@(let lp ((n 0))
  1182. (let ((ids (map (lambda (n)
  1183. (let* ((n (+ (char->integer #\a) n))
  1184. (c (integer->char n)))
  1185. (datum->syntax #'here (symbol c))))
  1186. (iota n))))
  1187. #`(((#,n) (form #,@ids))
  1188. . #,(if (< n max)
  1189. (lp (1+ n))
  1190. #'()))))
  1191. (else alternate)))))))
  1192. ;;;
  1193. ;;; These dispatchers are set as the "procedure" field of <generic>
  1194. ;;; instances. Unlike CLOS, in GOOPS a generic function can have
  1195. ;;; multiple arities.
  1196. ;;;
  1197. ;;; We pre-generate fast dispatchers for applications of up to 20
  1198. ;;; arguments. More arguments than that will go through slower generic
  1199. ;;; routines that cons arguments into a rest list.
  1200. ;;;
  1201. (define (multiple-arity-dispatcher fv miss)
  1202. (define-syntax dispatch
  1203. (lambda (x)
  1204. (define (build-clauses args)
  1205. (let ((len (length (syntax->datum args))))
  1206. #`((#,args ((vector-ref fv #,len) . #,args))
  1207. . #,(syntax-case args ()
  1208. (() #'())
  1209. ((arg ... _) (build-clauses #'(arg ...)))))))
  1210. (syntax-case x ()
  1211. ((dispatch arg ...)
  1212. #`(case-lambda
  1213. #,@(build-clauses #'(arg ...))
  1214. (args (apply miss args)))))))
  1215. (arity-case (1- (vector-length fv)) 20 dispatch
  1216. (lambda args
  1217. (let ((nargs (length args)))
  1218. (if (< nargs (vector-length fv))
  1219. (apply (vector-ref fv nargs) args)
  1220. (apply miss args))))))
  1221. ;;;
  1222. ;;; The above multiple-arity-dispatcher is entirely sufficient, and
  1223. ;;; should be fast enough. Still, for no good reason we also have an
  1224. ;;; arity dispatcher for generics that are only called with one arity.
  1225. ;;;
  1226. (define (single-arity-dispatcher f nargs miss)
  1227. (define-syntax-rule (dispatch arg ...)
  1228. (case-lambda
  1229. ((arg ...) (f arg ...))
  1230. (args (apply miss args))))
  1231. (arity-case nargs 20 dispatch
  1232. (lambda args
  1233. (if (eqv? (length args) nargs)
  1234. (apply f args)
  1235. (apply miss args)))))
  1236. ;;;
  1237. ;;; The guts of generic function dispatch are here. Once we've selected
  1238. ;;; an arity, we need to map from arguments to effective method. Until
  1239. ;;; we have `eqv?' specializers, this map is entirely a function of the
  1240. ;;; types (classes) of the arguments. So, we look in the cache to see
  1241. ;;; if we have seen this set of concrete types, and if so we apply the
  1242. ;;; previously computed effective method. Otherwise we miss the cache,
  1243. ;;; so we'll have to compute the right answer for this set of types, add
  1244. ;;; the mapping to the cache, and apply the newly computed method.
  1245. ;;;
  1246. ;;; The cached mapping is invalidated whenever a new method is defined
  1247. ;;; on this generic, or whenever the class hierarchy of any method
  1248. ;;; specializer changes.
  1249. ;;;
  1250. (define (single-arity-cache-dispatch cache nargs cache-miss)
  1251. (match cache
  1252. (() cache-miss)
  1253. (((typev . cmethod) . cache)
  1254. (cond
  1255. ((eqv? nargs (vector-length typev))
  1256. (let ((cache-miss (single-arity-cache-dispatch cache nargs cache-miss)))
  1257. (define (type-ref n)
  1258. (and (< n nargs) (vector-ref typev n)))
  1259. (define-syntax args-match?
  1260. (syntax-rules ()
  1261. ((args-match?) #t)
  1262. ((args-match? (arg type) (arg* type*) ...)
  1263. ;; Check that the arg has the exact type that we saw. It
  1264. ;; could be that `type' is #f, which indicates the end of
  1265. ;; the specializers list. Once all specializers have been
  1266. ;; examined, we don't need to look at any more arguments
  1267. ;; to know that this is a cache hit.
  1268. (or (not type)
  1269. (and (eq? (class-of arg) type)
  1270. (args-match? (arg* type*) ...))))))
  1271. (define-syntax dispatch
  1272. (lambda (x)
  1273. (define (bind-types types k)
  1274. (let lp ((types types) (n 0))
  1275. (syntax-case types ()
  1276. (() (k))
  1277. ((type . types)
  1278. #`(let ((type (type-ref #,n)))
  1279. #,(lp #'types (1+ n)))))))
  1280. (syntax-case x ()
  1281. ((dispatch arg ...)
  1282. (with-syntax (((type ...) (generate-temporaries #'(arg ...))))
  1283. (bind-types
  1284. #'(type ...)
  1285. (lambda ()
  1286. #'(lambda (arg ...)
  1287. (if (args-match? (arg type) ...)
  1288. (cmethod arg ...)
  1289. (cache-miss arg ...))))))))))
  1290. (arity-case nargs 20 dispatch
  1291. (lambda args
  1292. (define (args-match? args)
  1293. (let lp ((args args) (n 0))
  1294. (match args
  1295. ((arg . args)
  1296. (or (not (vector-ref typev n))
  1297. (and (eq? (vector-ref typev n) (class-of arg))
  1298. (lp args (1+ n)))))
  1299. (_ #t))))
  1300. (if (args-match? args)
  1301. (apply cmethod args)
  1302. (apply cache-miss args))))))
  1303. (else
  1304. (single-arity-cache-dispatch cache nargs cache-miss))))))
  1305. (define (compute-generic-function-dispatch-procedure gf)
  1306. (define (seen-arities cache)
  1307. (let lp ((arities 0) (cache cache))
  1308. (match cache
  1309. (() arities)
  1310. (((typev . cmethod) . cache)
  1311. (lp (logior arities (ash 1 (vector-length typev)))
  1312. cache)))))
  1313. (define (cache-miss . args)
  1314. (memoize-generic-function-application! gf args)
  1315. (apply gf args))
  1316. (let* ((cache (slot-ref gf 'effective-methods))
  1317. (arities (seen-arities cache))
  1318. (max-arity (let lp ((max -1))
  1319. (if (< arities (ash 1 (1+ max)))
  1320. max
  1321. (lp (1+ max))))))
  1322. (cond
  1323. ((= max-arity -1)
  1324. ;; Nothing in the cache.
  1325. cache-miss)
  1326. ((= arities (ash 1 max-arity))
  1327. ;; Only one arity in the cache.
  1328. (let* ((nargs max-arity)
  1329. (f (single-arity-cache-dispatch cache nargs cache-miss)))
  1330. (single-arity-dispatcher f nargs cache-miss)))
  1331. (else
  1332. ;; Multiple arities.
  1333. (let ((fv (make-vector (1+ max-arity) #f)))
  1334. (let lp ((n 0))
  1335. (when (<= n max-arity)
  1336. (let ((f (single-arity-cache-dispatch cache n cache-miss)))
  1337. (vector-set! fv n f)
  1338. (lp (1+ n)))))
  1339. (multiple-arity-dispatcher fv cache-miss))))))
  1340. (define (recompute-generic-function-dispatch-procedure! gf)
  1341. (slot-set! gf 'procedure
  1342. (compute-generic-function-dispatch-procedure gf)))
  1343. (define (memoize-effective-method! gf args applicable)
  1344. (define (record-types args)
  1345. (let ((typev (make-vector (length args) #f)))
  1346. (let lp ((n 0) (args args))
  1347. (when (and (< n (slot-ref gf 'n-specialized))
  1348. (pair? args))
  1349. (match args
  1350. ((arg . args)
  1351. (vector-set! typev n (class-of arg))
  1352. (lp (1+ n) args)))))
  1353. typev))
  1354. (let* ((typev (record-types args))
  1355. (compute-effective-method (if (eq? (class-of gf) <generic>)
  1356. %compute-effective-method
  1357. compute-effective-method))
  1358. (cmethod (compute-effective-method gf applicable typev))
  1359. (cache (acons typev cmethod (slot-ref gf 'effective-methods))))
  1360. (slot-set! gf 'effective-methods cache)
  1361. (recompute-generic-function-dispatch-procedure! gf)
  1362. cmethod))
  1363. ;;;
  1364. ;;; If a method refers to `next-method' in its body, that method will be
  1365. ;;; able to dispatch to the next most specific method. The exact
  1366. ;;; `next-method' implementation is only known at runtime, as it is a
  1367. ;;; function of which precise argument types are being dispatched, which
  1368. ;;; might be subclasses of the method's declared specializers.
  1369. ;;;
  1370. ;;; Guile implements `next-method' by binding it as a closure variable.
  1371. ;;; An effective method is bound to a specific `next-method' by the
  1372. ;;; `make-procedure' slot of a <method>, which returns the new closure.
  1373. ;;;
  1374. (define (%compute-specialized-effective-method gf method types next-method)
  1375. (match (slot-ref method 'make-procedure)
  1376. (#f (method-procedure method))
  1377. (make-procedure (make-procedure next-method))))
  1378. (define (compute-specialized-effective-method gf method types next-method)
  1379. (%compute-specialized-effective-method gf method types next-method))
  1380. (define (%compute-effective-method gf methods types)
  1381. (match methods
  1382. ((method . methods)
  1383. (let ((compute-specialized-effective-method
  1384. (if (and (eq? (class-of gf) <generic>)
  1385. (eq? (class-of method) <method>))
  1386. %compute-specialized-effective-method
  1387. compute-specialized-effective-method)))
  1388. (compute-specialized-effective-method
  1389. gf method types
  1390. (match methods
  1391. (()
  1392. (lambda args
  1393. (no-next-method gf args)))
  1394. (methods
  1395. (let ((compute-effective-method (if (eq? (class-of gf) <generic>)
  1396. %compute-effective-method
  1397. compute-effective-method)))
  1398. (compute-effective-method gf methods types)))))))))
  1399. ;; Boot definition; overrided with a generic later.
  1400. (define (compute-effective-method gf methods types)
  1401. (%compute-effective-method gf methods types))
  1402. ;;;
  1403. ;;; Memoization
  1404. ;;;
  1405. (define (memoize-generic-function-application! gf args)
  1406. (let ((applicable ((if (eq? (class-of gf) <generic>)
  1407. %compute-applicable-methods
  1408. compute-applicable-methods)
  1409. gf args)))
  1410. (cond (applicable
  1411. (memoize-effective-method! gf args applicable))
  1412. (else
  1413. (no-applicable-method gf args)))))
  1414. (define no-applicable-method
  1415. (make <generic> #:name 'no-applicable-method))
  1416. (%goops-early-init)
  1417. ;; Then load the rest of GOOPS
  1418. ;; FIXME: deprecate.
  1419. (define min-fixnum (- (expt 2 29)))
  1420. (define max-fixnum (- (expt 2 29) 1))
  1421. ;;
  1422. ;; goops-error
  1423. ;;
  1424. (define (goops-error format-string . args)
  1425. (scm-error 'goops-error #f format-string args '()))
  1426. ;;;
  1427. ;;; {Meta classes}
  1428. ;;;
  1429. (define ensure-metaclass-with-supers
  1430. (let ((table-of-metas '()))
  1431. (lambda (meta-supers)
  1432. (let ((entry (assoc meta-supers table-of-metas)))
  1433. (if entry
  1434. ;; Found a previously created metaclass
  1435. (cdr entry)
  1436. ;; Create a new meta-class which inherit from "meta-supers"
  1437. (let ((new (make <class> #:dsupers meta-supers
  1438. #:slots '()
  1439. #:name (gensym "metaclass"))))
  1440. (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
  1441. new))))))
  1442. (define (ensure-metaclass supers)
  1443. (if (null? supers)
  1444. <class>
  1445. (let* ((all-metas (map (lambda (x) (class-of x)) supers))
  1446. (all-cpls (append-map (lambda (m)
  1447. (cdr (class-precedence-list m)))
  1448. all-metas))
  1449. (needed-metas '()))
  1450. ;; Find the most specific metaclasses. The new metaclass will be
  1451. ;; a subclass of these.
  1452. (for-each
  1453. (lambda (meta)
  1454. (when (and (not (member meta all-cpls))
  1455. (not (member meta needed-metas)))
  1456. (set! needed-metas (append needed-metas (list meta)))))
  1457. all-metas)
  1458. ;; Now return a subclass of the metaclasses we found.
  1459. (if (null? (cdr needed-metas))
  1460. (car needed-metas) ; If there's only one, just use it.
  1461. (ensure-metaclass-with-supers needed-metas)))))
  1462. ;;;
  1463. ;;; {Classes}
  1464. ;;;
  1465. ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
  1466. ;;;
  1467. ;;; SLOT-DEFINITION ::= INSTANCE-OF-<SLOT> | (SLOT-NAME OPTION ...)
  1468. ;;; OPTION ::= KEYWORD VALUE
  1469. ;;;
  1470. (define (make-class supers slots . options)
  1471. (define (find-duplicate l)
  1472. (match l
  1473. (() #f)
  1474. ((head . tail)
  1475. (if (memq head tail)
  1476. head
  1477. (find-duplicate tail)))))
  1478. (define (slot-spec->name slot-spec)
  1479. (match slot-spec
  1480. (((? symbol? name) . args) name)
  1481. ;; We can get here when redefining classes.
  1482. ((? slot? slot) (%slot-definition-name slot))))
  1483. (let* ((name (get-keyword #:name options *unbound*))
  1484. (supers (if (not (or-map (lambda (class)
  1485. (memq <object>
  1486. (class-precedence-list class)))
  1487. supers))
  1488. (append supers (list <object>))
  1489. supers))
  1490. (metaclass (or (get-keyword #:metaclass options #f)
  1491. (ensure-metaclass supers))))
  1492. ;; Verify that all direct slots are different and that we don't inherit
  1493. ;; several time from the same class
  1494. (let ((tmp1 (find-duplicate supers))
  1495. (tmp2 (find-duplicate (map slot-spec->name slots))))
  1496. (if tmp1
  1497. (goops-error "make-class: super class ~S is duplicate in class ~S"
  1498. tmp1 name))
  1499. (if tmp2
  1500. (goops-error "make-class: slot ~S is duplicate in class ~S"
  1501. tmp2 name)))
  1502. ;; Everything seems correct, build the class
  1503. (apply make metaclass
  1504. #:dsupers supers
  1505. #:slots slots
  1506. #:name name
  1507. options)))
  1508. ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
  1509. ;;;
  1510. ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
  1511. ;;; OPTION ::= KEYWORD VALUE
  1512. ;;;
  1513. (define-syntax class
  1514. (lambda (x)
  1515. (define (parse-options options)
  1516. (syntax-case options ()
  1517. (() #'())
  1518. ((kw arg . options) (keyword? (syntax->datum #'kw))
  1519. (with-syntax ((options (parse-options #'options)))
  1520. (syntax-case #'kw ()
  1521. (#:init-form
  1522. #'(kw 'arg #:init-thunk (lambda () arg) . options))
  1523. (_
  1524. #'(kw arg . options)))))))
  1525. (define (check-valid-kwargs args)
  1526. (syntax-case args ()
  1527. (() #'())
  1528. ((kw arg . args) (keyword? (syntax->datum #'kw))
  1529. #`(kw arg . #,(check-valid-kwargs #'args)))))
  1530. (define (parse-slots-and-kwargs args)
  1531. (syntax-case args ()
  1532. (()
  1533. #'(() ()))
  1534. ((kw . _) (keyword? (syntax->datum #'kw))
  1535. #`(() #,(check-valid-kwargs args)))
  1536. (((name option ...) args ...)
  1537. (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...)))
  1538. ((option ...) (parse-options #'(option ...))))
  1539. #'(((list 'name option ...) . slots) kwargs)))
  1540. ((name args ...) (symbol? (syntax->datum #'name))
  1541. (with-syntax (((slots kwargs) (parse-slots-and-kwargs #'(args ...))))
  1542. #'(('(name) . slots) kwargs)))))
  1543. (syntax-case x ()
  1544. ((class (super ...) arg ...)
  1545. (with-syntax ((((slot-def ...) (option ...))
  1546. (parse-slots-and-kwargs #'(arg ...))))
  1547. #'(make-class (list super ...)
  1548. (list slot-def ...)
  1549. option ...))))))
  1550. (define-syntax define-class-pre-definition
  1551. (lambda (x)
  1552. (syntax-case x ()
  1553. ((_ (k arg rest ...) out ...)
  1554. (keyword? (syntax->datum #'k))
  1555. (case (syntax->datum #'k)
  1556. ((#:getter #:setter)
  1557. #'(define-class-pre-definition (rest ...)
  1558. out ...
  1559. (when (or (not (defined? 'arg))
  1560. (not (is-a? arg <generic>)))
  1561. (toplevel-define!
  1562. 'arg
  1563. (ensure-generic (if (defined? 'arg) arg #f) 'arg)))))
  1564. ((#:accessor)
  1565. #'(define-class-pre-definition (rest ...)
  1566. out ...
  1567. (when (or (not (defined? 'arg))
  1568. (not (is-a? arg <accessor>)))
  1569. (toplevel-define!
  1570. 'arg
  1571. (ensure-accessor (if (defined? 'arg) arg #f) 'arg)))))
  1572. (else
  1573. #'(define-class-pre-definition (rest ...) out ...))))
  1574. ((_ () out ...)
  1575. #'(begin out ...)))))
  1576. ;; Some slot options require extra definitions to be made. In
  1577. ;; particular, we want to make sure that the generic function objects
  1578. ;; which represent accessors exist before `make-class' tries to add
  1579. ;; methods to them.
  1580. (define-syntax define-class-pre-definitions
  1581. (lambda (x)
  1582. (syntax-case x ()
  1583. ((_ () out ...)
  1584. #'(begin out ...))
  1585. ((_ (slot rest ...) out ...)
  1586. (keyword? (syntax->datum #'slot))
  1587. #'(begin out ...))
  1588. ((_ (slot rest ...) out ...)
  1589. (identifier? #'slot)
  1590. #'(define-class-pre-definitions (rest ...)
  1591. out ...))
  1592. ((_ ((slotname slotopt ...) rest ...) out ...)
  1593. #'(define-class-pre-definitions (rest ...)
  1594. out ... (define-class-pre-definition (slotopt ...)))))))
  1595. (define-syntax-rule (define-class name supers slot ...)
  1596. (begin
  1597. (define-class-pre-definitions (slot ...))
  1598. (let ((cls (class supers slot ... #:name 'name)))
  1599. (toplevel-define!
  1600. 'name
  1601. (if (defined? 'name)
  1602. (class-redefinition name cls)
  1603. cls)))))
  1604. (define-syntax-rule (standard-define-class arg ...)
  1605. (define-class arg ...))
  1606. ;;;
  1607. ;;; {Generic functions and accessors}
  1608. ;;;
  1609. ;; Apparently the desired semantics are that we extend previous
  1610. ;; procedural definitions, but that if `name' was already a generic, we
  1611. ;; overwrite its definition.
  1612. (define-syntax define-generic
  1613. (lambda (x)
  1614. (syntax-case x ()
  1615. ((define-generic name) (symbol? (syntax->datum #'name))
  1616. #'(define name
  1617. (if (and (defined? 'name) (is-a? name <generic>))
  1618. (make <generic> #:name 'name)
  1619. (ensure-generic (if (defined? 'name) name #f) 'name)))))))
  1620. (define-syntax define-extended-generic
  1621. (lambda (x)
  1622. (syntax-case x ()
  1623. ((define-extended-generic name val) (symbol? (syntax->datum #'name))
  1624. #'(define name (make-extended-generic val 'name))))))
  1625. (define-syntax define-extended-generics
  1626. (lambda (x)
  1627. (define (id-append ctx a b)
  1628. (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
  1629. (syntax-case x ()
  1630. ((define-extended-generic (name ...) #:prefix (prefix ...))
  1631. (and (and-map symbol? (syntax->datum #'(name ...)))
  1632. (and-map symbol? (syntax->datum #'(prefix ...))))
  1633. (with-syntax ((((val ...)) (map (lambda (name)
  1634. (map (lambda (prefix)
  1635. (id-append name prefix name))
  1636. #'(prefix ...)))
  1637. #'(name ...))))
  1638. #'(begin
  1639. (define-extended-generic name (list val ...))
  1640. ...))))))
  1641. (define* (make-generic #:optional name)
  1642. (make <generic> #:name name))
  1643. (define* (make-extended-generic gfs #:optional name)
  1644. (let* ((gfs (if (list? gfs) gfs (list gfs)))
  1645. (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs)))
  1646. (let ((ans (if gws?
  1647. (let* ((sname (and name (make-setter-name name)))
  1648. (setters
  1649. (append-map (lambda (gf)
  1650. (if (is-a? gf <generic-with-setter>)
  1651. (list (ensure-generic (setter gf)
  1652. sname))
  1653. '()))
  1654. gfs))
  1655. (es (make <extended-generic-with-setter>
  1656. #:name name
  1657. #:extends gfs
  1658. #:setter (make <extended-generic>
  1659. #:name sname
  1660. #:extends setters))))
  1661. (extended-by! setters (setter es))
  1662. es)
  1663. (make <extended-generic>
  1664. #:name name
  1665. #:extends gfs))))
  1666. (extended-by! gfs ans)
  1667. ans)))
  1668. (define (extended-by! gfs eg)
  1669. (for-each (lambda (gf)
  1670. (slot-set! gf 'extended-by
  1671. (cons eg (slot-ref gf 'extended-by))))
  1672. gfs)
  1673. (invalidate-method-cache! eg))
  1674. (define (not-extended-by! gfs eg)
  1675. (for-each (lambda (gf)
  1676. (slot-set! gf 'extended-by
  1677. (delq! eg (slot-ref gf 'extended-by))))
  1678. gfs)
  1679. (invalidate-method-cache! eg))
  1680. (define* (ensure-generic old-definition #:optional name)
  1681. (cond ((is-a? old-definition <generic>) old-definition)
  1682. ((procedure-with-setter? old-definition)
  1683. (make <generic-with-setter>
  1684. #:name name
  1685. #:default (procedure old-definition)
  1686. #:setter (setter old-definition)))
  1687. ((procedure? old-definition)
  1688. (if (generic-capability? old-definition) old-definition
  1689. (make <generic> #:name name #:default old-definition)))
  1690. (else (make <generic> #:name name))))
  1691. ;; same semantics as <generic>
  1692. (define-syntax-rule (define-accessor name)
  1693. (define name
  1694. (cond ((not (defined? 'name)) (ensure-accessor #f 'name))
  1695. ((is-a? name <accessor>) (make <accessor> #:name 'name))
  1696. (else (ensure-accessor name 'name)))))
  1697. (define (make-setter-name name)
  1698. (string->symbol (string-append "setter:" (symbol->string name))))
  1699. (define* (make-accessor #:optional name)
  1700. (make <accessor>
  1701. #:name name
  1702. #:setter (make <generic>
  1703. #:name (and name (make-setter-name name)))))
  1704. (define* (ensure-accessor proc #:optional name)
  1705. (cond ((and (is-a? proc <accessor>)
  1706. (is-a? (setter proc) <generic>))
  1707. proc)
  1708. ((is-a? proc <generic-with-setter>)
  1709. (upgrade-accessor proc (setter proc)))
  1710. ((is-a? proc <generic>)
  1711. (upgrade-accessor proc (make-generic name)))
  1712. ((procedure-with-setter? proc)
  1713. (make <accessor>
  1714. #:name name
  1715. #:default (procedure proc)
  1716. #:setter (ensure-generic (setter proc) name)))
  1717. ((procedure? proc)
  1718. (ensure-accessor (if (generic-capability? proc)
  1719. (make <generic> #:name name #:default proc)
  1720. (ensure-generic proc name))
  1721. name))
  1722. (else
  1723. (make-accessor name))))
  1724. (define (upgrade-accessor generic setter)
  1725. (let ((methods (slot-ref generic 'methods))
  1726. (gws (make (if (is-a? generic <extended-generic>)
  1727. <extended-generic-with-setter>
  1728. <accessor>)
  1729. #:name (generic-function-name generic)
  1730. #:extended-by (slot-ref generic 'extended-by)
  1731. #:setter setter)))
  1732. (when (is-a? generic <extended-generic>)
  1733. (let ((gfs (slot-ref generic 'extends)))
  1734. (not-extended-by! gfs generic)
  1735. (slot-set! gws 'extends gfs)
  1736. (extended-by! gfs gws)))
  1737. ;; Steal old methods
  1738. (for-each (lambda (method)
  1739. (slot-set! method 'generic-function gws))
  1740. methods)
  1741. (slot-set! gws 'methods methods)
  1742. (invalidate-method-cache! gws)
  1743. gws))
  1744. ;;;
  1745. ;;; {Methods}
  1746. ;;;
  1747. ;; Note: `a' and `b' can have unequal lengths (i.e. one can be one
  1748. ;; element longer than the other when we have a dotted parameter
  1749. ;; list). For instance, with the call
  1750. ;;
  1751. ;; (M 1)
  1752. ;;
  1753. ;; with
  1754. ;;
  1755. ;; (define-method M (a . l) ....)
  1756. ;; (define-method M (a) ....)
  1757. ;;
  1758. ;; we consider that the second method is more specific.
  1759. ;;
  1760. ;; Precondition: `a' and `b' are methods and are applicable to `types'.
  1761. (define (%method-more-specific? a b types)
  1762. (let lp ((a-specializers (method-specializers a))
  1763. (b-specializers (method-specializers b))
  1764. (types types))
  1765. (cond
  1766. ;; (a) less specific than (a b ...) or (a . b)
  1767. ((null? a-specializers) #t)
  1768. ;; (a b ...) or (a . b) less specific than (a)
  1769. ((null? b-specializers) #f)
  1770. ;; (a . b) less specific than (a b ...)
  1771. ((not (pair? a-specializers)) #f)
  1772. ;; (a b ...) more specific than (a . b)
  1773. ((not (pair? b-specializers)) #t)
  1774. (else
  1775. (let ((a-specializer (car a-specializers))
  1776. (b-specializer (car b-specializers))
  1777. (a-specializers (cdr a-specializers))
  1778. (b-specializers (cdr b-specializers))
  1779. (type (car types))
  1780. (types (cdr types)))
  1781. (if (eq? a-specializer b-specializer)
  1782. (lp a-specializers b-specializers types)
  1783. (let lp ((cpl (class-precedence-list type)))
  1784. (let ((elt (car cpl)))
  1785. (cond
  1786. ((eq? a-specializer elt) #t)
  1787. ((eq? b-specializer elt) #f)
  1788. (else (lp (cdr cpl))))))))))))
  1789. (define (%sort-applicable-methods methods types)
  1790. (sort methods (lambda (a b) (%method-more-specific? a b types))))
  1791. (define (generic-function-methods obj)
  1792. "Return the methods of the generic function @var{obj}."
  1793. (define (fold-upward method-lists gf)
  1794. (cond
  1795. ((is-a? gf <extended-generic>)
  1796. (let lp ((method-lists method-lists) (gfs (slot-ref gf 'extends)))
  1797. (match gfs
  1798. (() method-lists)
  1799. ((gf . gfs)
  1800. (lp (fold-upward (cons (slot-ref gf 'methods) method-lists) gf)
  1801. gfs)))))
  1802. (else method-lists)))
  1803. (define (fold-downward method-lists gf)
  1804. (let lp ((method-lists (cons (slot-ref gf 'methods) method-lists))
  1805. (gfs (slot-ref gf 'extended-by)))
  1806. (match gfs
  1807. (() method-lists)
  1808. ((gf . gfs)
  1809. (lp (fold-downward method-lists gf) gfs)))))
  1810. (unless (is-a? obj <generic>)
  1811. (scm-error 'wrong-type-arg #f "Not a generic: ~S"
  1812. (list obj) #f))
  1813. (concatenate (fold-downward (fold-upward '() obj) obj)))
  1814. (define (%compute-applicable-methods gf args)
  1815. (define (method-applicable? m types)
  1816. (let ((specs (method-specializers m)))
  1817. (cond
  1818. ((and (is-a? m <accessor-method>)
  1819. (or (null? specs) (null? types)
  1820. (not (eq? (car specs) (car types)))))
  1821. ;; Slot accessor methods are added to each subclass with the
  1822. ;; slot. They only apply to that specific concrete class, which
  1823. ;; appears as the first argument.
  1824. #f)
  1825. (else
  1826. (let lp ((specs specs) (types types))
  1827. (cond
  1828. ((null? specs) (null? types))
  1829. ((not (pair? specs)) #t)
  1830. ((null? types) #f)
  1831. (else
  1832. (and (memq (car specs) (class-precedence-list (car types)))
  1833. (lp (cdr specs) (cdr types))))))))))
  1834. (let ((n (length args))
  1835. (types (map class-of args)))
  1836. (let lp ((methods (generic-function-methods gf))
  1837. (applicable '()))
  1838. (if (null? methods)
  1839. (and (not (null? applicable))
  1840. (%sort-applicable-methods applicable types))
  1841. (let ((m (car methods)))
  1842. (lp (cdr methods)
  1843. (if (method-applicable? m types)
  1844. (cons m applicable)
  1845. applicable)))))))
  1846. (define compute-applicable-methods %compute-applicable-methods)
  1847. (define (toplevel-define! name val)
  1848. (module-define! (current-module) name val))
  1849. (define-syntax define-method
  1850. (syntax-rules (setter)
  1851. ((_ ((setter name) . args) body ...)
  1852. (begin
  1853. (when (or (not (defined? 'name))
  1854. (not (is-a? name <accessor>)))
  1855. (toplevel-define! 'name
  1856. (ensure-accessor
  1857. (if (defined? 'name) name #f) 'name)))
  1858. (add-method! (setter name) (method args body ...))))
  1859. ((_ (name . args) body ...)
  1860. (begin
  1861. ;; FIXME: this code is how it always was, but it's quite cracky:
  1862. ;; it will only define the generic function if it was undefined
  1863. ;; before (ok), or *was defined to #f*. The latter is crack. But
  1864. ;; there are bootstrap issues about fixing this -- change it to
  1865. ;; (is-a? name <generic>) and see.
  1866. (when (or (not (defined? 'name))
  1867. (not name))
  1868. (toplevel-define! 'name (make <generic> #:name 'name)))
  1869. (add-method! name (method args body ...))))))
  1870. (define-syntax method
  1871. (lambda (x)
  1872. (define (parse-args args)
  1873. (let lp ((ls args) (formals '()) (specializers '()))
  1874. (syntax-case ls ()
  1875. (((f s) . rest)
  1876. (and (identifier? #'f) (identifier? #'s))
  1877. (lp #'rest
  1878. (cons #'f formals)
  1879. (cons #'s specializers)))
  1880. ((f . rest)
  1881. (identifier? #'f)
  1882. (lp #'rest
  1883. (cons #'f formals)
  1884. (cons #'<top> specializers)))
  1885. (()
  1886. (list (reverse formals)
  1887. (reverse (cons #''() specializers))))
  1888. (tail
  1889. (identifier? #'tail)
  1890. (list (append (reverse formals) #'tail)
  1891. (reverse (cons #'<top> specializers)))))))
  1892. (define (find-free-id exp referent)
  1893. (syntax-case exp ()
  1894. ((x . y)
  1895. (or (find-free-id #'x referent)
  1896. (find-free-id #'y referent)))
  1897. (x
  1898. (identifier? #'x)
  1899. (let ((id (datum->syntax #'x referent)))
  1900. (and (free-identifier=? #'x id) id)))
  1901. (_ #f)))
  1902. (define (compute-procedure formals body)
  1903. (syntax-case body ()
  1904. ((body0 ...)
  1905. (with-syntax ((formals formals))
  1906. #'(lambda formals body0 ...)))))
  1907. (define (->proper args)
  1908. (let lp ((ls args) (out '()))
  1909. (syntax-case ls ()
  1910. ((x . xs) (lp #'xs (cons #'x out)))
  1911. (() (reverse out))
  1912. (tail (reverse (cons #'tail out))))))
  1913. (define (compute-make-procedure formals body next-method)
  1914. (syntax-case body ()
  1915. ((body ...)
  1916. (with-syntax ((next-method next-method))
  1917. (syntax-case formals ()
  1918. ((formal ...)
  1919. #'(lambda (real-next-method)
  1920. (lambda (formal ...)
  1921. (let ((next-method (lambda args
  1922. (if (null? args)
  1923. (real-next-method formal ...)
  1924. (apply real-next-method args)))))
  1925. body ...))))
  1926. (formals
  1927. (with-syntax (((formal ...) (->proper #'formals)))
  1928. #'(lambda (real-next-method)
  1929. (lambda formals
  1930. (let ((next-method (lambda args
  1931. (if (null? args)
  1932. (apply real-next-method formal ...)
  1933. (apply real-next-method args)))))
  1934. body ...))))))))))
  1935. (define (compute-procedures formals body)
  1936. ;; So, our use of this is broken, because it operates on the
  1937. ;; pre-expansion source code. It's equivalent to just searching
  1938. ;; for referent in the datums. Ah well.
  1939. (let ((id (find-free-id body 'next-method)))
  1940. (if id
  1941. ;; return a make-procedure
  1942. (values #'#f
  1943. (compute-make-procedure formals body id))
  1944. (values (compute-procedure formals body)
  1945. #'#f))))
  1946. (syntax-case x ()
  1947. ((_ args) #'(method args (if #f #f)))
  1948. ((_ args body0 body1 ...)
  1949. (with-syntax (((formals (specializer ...)) (parse-args #'args)))
  1950. (call-with-values
  1951. (lambda ()
  1952. (compute-procedures #'formals #'(body0 body1 ...)))
  1953. (lambda (procedure make-procedure)
  1954. (with-syntax ((procedure procedure)
  1955. (make-procedure make-procedure))
  1956. #'(make <method>
  1957. #:specializers (cons* specializer ...)
  1958. #:formals 'formals
  1959. #:body '(body0 body1 ...)
  1960. #:make-procedure make-procedure
  1961. #:procedure procedure)))))))))
  1962. ;;;
  1963. ;;; {Utilities}
  1964. ;;;
  1965. ;;; These are useful when dealing with method specializers, which might
  1966. ;;; have a rest argument.
  1967. ;;;
  1968. (define (map* fn . l) ; A map which accepts dotted lists (arg lists
  1969. (cond ; must be "isomorph"
  1970. ((null? (car l)) '())
  1971. ((pair? (car l)) (cons (apply fn (map car l))
  1972. (apply map* fn (map cdr l))))
  1973. (else (apply fn l))))
  1974. (define (for-each* fn . l) ; A for-each which accepts dotted lists (arg lists
  1975. (cond ; must be "isomorph"
  1976. ((null? (car l)) '())
  1977. ((pair? (car l)) (apply fn (map car l)) (apply for-each* fn (map cdr l)))
  1978. (else (apply fn l))))
  1979. (define (length* ls)
  1980. (do ((n 0 (+ 1 n))
  1981. (ls ls (cdr ls)))
  1982. ((not (pair? ls)) n)))
  1983. ;;;
  1984. ;;; {add-method!}
  1985. ;;;
  1986. (define (add-method-in-classes! m)
  1987. ;; Add method in all the classes which appears in its specializers list
  1988. (for-each* (lambda (x)
  1989. (let ((dm (class-direct-methods x)))
  1990. (unless (memq m dm)
  1991. (struct-set! x class-index-direct-methods (cons m dm)))))
  1992. (method-specializers m)))
  1993. (define (remove-method-in-classes! m)
  1994. ;; Remove method in all the classes which appears in its specializers list
  1995. (for-each* (lambda (x)
  1996. (struct-set! x
  1997. class-index-direct-methods
  1998. (delv! m (class-direct-methods x))))
  1999. (method-specializers m)))
  2000. (define (compute-new-list-of-methods gf new)
  2001. (let ((new-spec (method-specializers new))
  2002. (methods (slot-ref gf 'methods)))
  2003. (let loop ((l methods))
  2004. (if (null? l)
  2005. (cons new methods)
  2006. (if (equal? (method-specializers (car l)) new-spec)
  2007. (begin
  2008. ;; This spec. list already exists. Remove old method from dependents
  2009. (remove-method-in-classes! (car l))
  2010. (set-car! l new)
  2011. methods)
  2012. (loop (cdr l)))))))
  2013. (define (method-n-specializers m)
  2014. (length* (slot-ref m 'specializers)))
  2015. (define (calculate-n-specialized gf)
  2016. (fold (lambda (m n) (max n (method-n-specializers m)))
  2017. 0
  2018. (generic-function-methods gf)))
  2019. (define (invalidate-method-cache! gf)
  2020. (slot-set! gf 'n-specialized (calculate-n-specialized gf))
  2021. (%invalidate-method-cache! gf)
  2022. (for-each (lambda (gf) (invalidate-method-cache! gf))
  2023. (slot-ref gf 'extended-by)))
  2024. (define internal-add-method!
  2025. (method ((gf <generic>) (m <method>))
  2026. (slot-set! m 'generic-function gf)
  2027. (slot-set! gf 'methods (compute-new-list-of-methods gf m))
  2028. (invalidate-method-cache! gf)
  2029. (add-method-in-classes! m)
  2030. *unspecified*))
  2031. (define-generic add-method!)
  2032. ((method-procedure internal-add-method!) add-method! internal-add-method!)
  2033. (define-method (add-method! (proc <procedure>) (m <method>))
  2034. (if (generic-capability? proc)
  2035. (begin
  2036. (enable-primitive-generic! proc)
  2037. (add-method! proc m))
  2038. (next-method)))
  2039. (define-method (add-method! (pg <primitive-generic>) (m <method>))
  2040. (add-method! (primitive-generic-generic pg) m))
  2041. (define-method (add-method! obj (m <method>))
  2042. (goops-error "~S is not a valid generic function" obj))
  2043. ;;;
  2044. ;;; {Access to meta objects}
  2045. ;;;
  2046. ;;;
  2047. ;;; Methods
  2048. ;;;
  2049. (define-method (method-source (m <method>))
  2050. (let* ((spec (map* class-name (slot-ref m 'specializers)))
  2051. (src (procedure-source (slot-ref m 'procedure))))
  2052. (and src
  2053. (let ((args (cadr src))
  2054. (body (cddr src)))
  2055. (cons 'method
  2056. (cons (map* list args spec)
  2057. body))))))
  2058. (define-method (method-formals (m <method>))
  2059. (slot-ref m 'formals))
  2060. ;;;
  2061. ;;; Slots
  2062. ;;;
  2063. (define (slot-init-function class slot-name)
  2064. (%slot-definition-init-thunk (or (class-slot-definition class slot-name)
  2065. (error "slot not found" slot-name))))
  2066. (define (accessor-method-slot-definition obj)
  2067. "Return the slot definition of the accessor @var{obj}."
  2068. (slot-ref obj 'slot-definition))
  2069. ;;;
  2070. ;;; {Standard methods used by the C runtime}
  2071. ;;;
  2072. ;;; Methods to compare objects
  2073. ;;;
  2074. ;; Have to do this in a strange order because equal? is used in the
  2075. ;; add-method! implementation; we need to make sure that when the
  2076. ;; primitive is extended, that the generic has a method. =
  2077. (define g-equal? (make-generic 'equal?))
  2078. ;; When this generic gets called, we will have already checked eq? and
  2079. ;; eqv? -- the purpose of this generic is to extend equality. So by
  2080. ;; default, there is no extension, thus the #f return.
  2081. (add-method! g-equal? (method (x y) #f))
  2082. (set-primitive-generic! equal? g-equal?)
  2083. ;;;
  2084. ;;; methods to display/write an object
  2085. ;;;
  2086. ; Code for writing objects must test that the slots they use are
  2087. ; bound. Otherwise a slot-unbound method will be called and will
  2088. ; conduct to an infinite loop.
  2089. ;; Write
  2090. (define (display-address o file)
  2091. (display (number->string (object-address o) 16) file))
  2092. (define-method (write o file)
  2093. (display "#<instance " file)
  2094. (display-address o file)
  2095. (display #\> file))
  2096. (define write-object (primitive-generic-generic write))
  2097. (define-method (write (o <object>) file)
  2098. (let ((class (class-of o)))
  2099. (if (slot-bound? class 'name)
  2100. (begin
  2101. (display "#<" file)
  2102. (display (class-name class) file)
  2103. (display #\space file)
  2104. (display-address o file)
  2105. (display #\> file))
  2106. (next-method))))
  2107. (define-method (write (slot <slot>) file)
  2108. (let ((class (class-of slot)))
  2109. (if (and (slot-bound? class 'name)
  2110. (slot-bound? slot 'name))
  2111. (begin
  2112. (display "#<" file)
  2113. (display (class-name class) file)
  2114. (display #\space file)
  2115. (display (%slot-definition-name slot) file)
  2116. (display #\space file)
  2117. (display-address slot file)
  2118. (display #\> file))
  2119. (next-method))))
  2120. (define-method (write (class <class>) file)
  2121. (let ((meta (class-of class)))
  2122. (if (and (slot-bound? class 'name)
  2123. (slot-bound? meta 'name))
  2124. (begin
  2125. (display "#<" file)
  2126. (display (class-name meta) file)
  2127. (display #\space file)
  2128. (display (class-name class) file)
  2129. (display #\space file)
  2130. (display-address class file)
  2131. (display #\> file))
  2132. (next-method))))
  2133. (define-method (write (gf <generic>) file)
  2134. (let ((meta (class-of gf)))
  2135. (if (and (slot-bound? meta 'name)
  2136. (slot-bound? gf 'methods))
  2137. (begin
  2138. (display "#<" file)
  2139. (display (class-name meta) file)
  2140. (let ((name (generic-function-name gf)))
  2141. (if name
  2142. (begin
  2143. (display #\space file)
  2144. (display name file))))
  2145. (display " (" file)
  2146. (display (length (generic-function-methods gf)) file)
  2147. (display ")>" file))
  2148. (next-method))))
  2149. (define-method (write (o <method>) file)
  2150. (let ((meta (class-of o)))
  2151. (if (and (slot-bound? meta 'name)
  2152. (slot-bound? o 'specializers))
  2153. (begin
  2154. (display "#<" file)
  2155. (display (class-name meta) file)
  2156. (display #\space file)
  2157. (display (map* (lambda (spec)
  2158. (if (slot-bound? spec 'name)
  2159. (slot-ref spec 'name)
  2160. spec))
  2161. (method-specializers o))
  2162. file)
  2163. (display #\space file)
  2164. (display-address o file)
  2165. (display #\> file))
  2166. (next-method))))
  2167. ;; Display (do the same thing as write by default)
  2168. (define-method (display o file)
  2169. (write-object o file))
  2170. ;;;
  2171. ;;; Handling of duplicate bindings in the module system
  2172. ;;;
  2173. (define (find-subclass super name)
  2174. (let lp ((classes (class-direct-subclasses super)))
  2175. (cond
  2176. ((null? classes)
  2177. (error "class not found" name))
  2178. ((and (slot-bound? (car classes) 'name)
  2179. (eq? (class-name (car classes)) name))
  2180. (car classes))
  2181. (else
  2182. (lp (cdr classes))))))
  2183. ;; A record type.
  2184. (define <module> (find-subclass <top> '<module>))
  2185. (define-method (merge-generics (module <module>)
  2186. (name <symbol>)
  2187. (int1 <module>)
  2188. (val1 <top>)
  2189. (int2 <module>)
  2190. (val2 <top>)
  2191. (var <top>)
  2192. (val <top>))
  2193. #f)
  2194. (define-method (merge-generics (module <module>)
  2195. (name <symbol>)
  2196. (int1 <module>)
  2197. (val1 <generic>)
  2198. (int2 <module>)
  2199. (val2 <generic>)
  2200. (var <top>)
  2201. (val <boolean>))
  2202. (and (not (eq? val1 val2))
  2203. (make-variable (make-extended-generic (list val2 val1) name))))
  2204. (define-method (merge-generics (module <module>)
  2205. (name <symbol>)
  2206. (int1 <module>)
  2207. (val1 <generic>)
  2208. (int2 <module>)
  2209. (val2 <generic>)
  2210. (var <top>)
  2211. (gf <extended-generic>))
  2212. (and (not (memq val2 (slot-ref gf 'extends)))
  2213. (begin
  2214. (slot-set! gf
  2215. 'extends
  2216. (cons val2 (delq! val2 (slot-ref gf 'extends))))
  2217. (slot-set! val2
  2218. 'extended-by
  2219. (cons gf (delq! gf (slot-ref val2 'extended-by))))
  2220. (invalidate-method-cache! gf)
  2221. var)))
  2222. (module-define! duplicate-handlers 'merge-generics merge-generics)
  2223. (define-method (merge-accessors (module <module>)
  2224. (name <symbol>)
  2225. (int1 <module>)
  2226. (val1 <top>)
  2227. (int2 <module>)
  2228. (val2 <top>)
  2229. (var <top>)
  2230. (val <top>))
  2231. #f)
  2232. (define-method (merge-accessors (module <module>)
  2233. (name <symbol>)
  2234. (int1 <module>)
  2235. (val1 <accessor>)
  2236. (int2 <module>)
  2237. (val2 <accessor>)
  2238. (var <top>)
  2239. (val <top>))
  2240. (merge-generics module name int1 val1 int2 val2 var val))
  2241. (module-define! duplicate-handlers 'merge-accessors merge-accessors)
  2242. ;;;
  2243. ;;; slot access
  2244. ;;;
  2245. (define (class-slot-ref class slot-name)
  2246. (let ((slot (class-slot-definition class slot-name)))
  2247. (unless (memq (%slot-definition-allocation slot) '(#:class #:each-subclass))
  2248. (slot-missing class slot-name))
  2249. (let ((x ((%slot-definition-slot-ref/raw slot) #f)))
  2250. (if (unbound? x)
  2251. (slot-unbound class slot-name)
  2252. x))))
  2253. (define (class-slot-set! class slot-name value)
  2254. (let ((slot (class-slot-definition class slot-name)))
  2255. (unless (memq (%slot-definition-allocation slot) '(#:class #:each-subclass))
  2256. (slot-missing class slot-name))
  2257. ((%slot-definition-slot-set! slot) #f value)))
  2258. (define-method (slot-unbound (c <class>) (o <object>) s)
  2259. (goops-error "Slot `~S' is unbound in object ~S" s o))
  2260. (define-method (slot-unbound (c <class>) s)
  2261. (goops-error "Slot `~S' is unbound in class ~S" s c))
  2262. (define-method (slot-unbound (o <object>))
  2263. (goops-error "Unbound slot in object ~S" o))
  2264. (define-method (slot-missing (c <class>) (o <object>) s)
  2265. (goops-error "No slot with name `~S' in object ~S" s o))
  2266. (define-method (slot-missing (c <class>) s)
  2267. (goops-error "No class slot with name `~S' in class ~S" s c))
  2268. (define-method (slot-missing (c <class>) (o <object>) s value)
  2269. (slot-missing c o s))
  2270. ;;; Methods for the possible error we can encounter when calling a gf
  2271. (define-method (no-next-method (gf <generic>) args)
  2272. (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
  2273. (define-method (no-applicable-method (gf <generic>) args)
  2274. (goops-error "No applicable method for ~S in call ~S"
  2275. gf (cons (generic-function-name gf) args)))
  2276. (define-method (no-method (gf <generic>) args)
  2277. (goops-error "No method defined for ~S" gf))
  2278. ;;;
  2279. ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
  2280. ;;;
  2281. (define-method (shallow-clone (self <object>))
  2282. (let* ((class (class-of self))
  2283. (clone (%allocate-instance class))
  2284. (slots (map slot-definition-name (class-slots class))))
  2285. (for-each (lambda (slot)
  2286. (when (slot-bound? self slot)
  2287. (slot-set! clone slot (slot-ref self slot))))
  2288. slots)
  2289. clone))
  2290. (define-method (deep-clone (self <object>))
  2291. (let* ((class (class-of self))
  2292. (clone (%allocate-instance class))
  2293. (slots (map slot-definition-name (class-slots class))))
  2294. (for-each (lambda (slot)
  2295. (when (slot-bound? self slot)
  2296. (slot-set! clone slot
  2297. (let ((value (slot-ref self slot)))
  2298. (if (instance? value)
  2299. (deep-clone value)
  2300. value)))))
  2301. slots)
  2302. clone))
  2303. ;;;
  2304. ;;; {Utilities for INITIALIZE methods}
  2305. ;;;
  2306. ;;; compute-slot-accessors
  2307. ;;;
  2308. (define (compute-slot-accessors class slots)
  2309. (for-each
  2310. (lambda (slot)
  2311. (let ((getter (%slot-definition-getter slot))
  2312. (setter (%slot-definition-setter slot))
  2313. (accessor-setter setter)
  2314. (accessor (%slot-definition-accessor slot)))
  2315. (when getter
  2316. (add-method! getter (compute-getter-method class slot)))
  2317. (when setter
  2318. (add-method! setter (compute-setter-method class slot)))
  2319. (when accessor
  2320. (add-method! accessor (compute-getter-method class slot))
  2321. (add-method! (accessor-setter accessor)
  2322. (compute-setter-method class slot)))))
  2323. slots))
  2324. (define-method (compute-getter-method (class <class>) slot)
  2325. (make <accessor-method>
  2326. #:specializers (list class)
  2327. #:procedure (slot-definition-slot-ref slot)
  2328. #:slot-definition slot))
  2329. (define-method (compute-setter-method (class <class>) slot)
  2330. (make <accessor-method>
  2331. #:specializers (list class <top>)
  2332. #:procedure (slot-definition-slot-set! slot)
  2333. #:slot-definition slot))
  2334. (define (make-generic-bound-check-getter proc)
  2335. (lambda (o)
  2336. (let ((val (proc o)))
  2337. (if (unbound? val)
  2338. (slot-unbound o)
  2339. val))))
  2340. ;;; compute-cpl
  2341. ;;;
  2342. ;; Replace the bootstrap compute-cpl with this definition.
  2343. (define compute-cpl
  2344. (make <generic> #:name 'compute-cpl))
  2345. (define-method (compute-cpl (class <class>))
  2346. (compute-std-cpl class class-direct-supers))
  2347. ;;; compute-get-n-set
  2348. ;;;
  2349. (define compute-get-n-set
  2350. (make <generic> #:name 'compute-get-n-set))
  2351. (define-method (compute-get-n-set (class <class>) s)
  2352. (define (class-slot-init-value)
  2353. (let ((thunk (slot-definition-init-thunk s)))
  2354. (if thunk
  2355. (thunk)
  2356. (slot-definition-init-value s))))
  2357. (define (make-closure-variable class value)
  2358. (list (lambda (o) value)
  2359. (lambda (o v) (set! value v))))
  2360. (case (slot-definition-allocation s)
  2361. ((#:instance) ;; Instance slot
  2362. ;; get-n-set is just its offset
  2363. (let ((already-allocated (struct-ref/unboxed class class-index-nfields)))
  2364. (struct-set!/unboxed class class-index-nfields (+ already-allocated 1))
  2365. already-allocated))
  2366. ((#:class) ;; Class slot
  2367. ;; Class-slots accessors are implemented as 2 closures around
  2368. ;; a Scheme variable. As instance slots, class slots must be
  2369. ;; unbound at init time.
  2370. (let ((name (slot-definition-name s)))
  2371. (if (memq name (map slot-definition-name (class-direct-slots class)))
  2372. ;; This slot is direct; create a new shared variable
  2373. (make-closure-variable class (class-slot-init-value))
  2374. ;; Slot is inherited. Find its definition in superclass
  2375. (let lp ((cpl (cdr (class-precedence-list class))))
  2376. (match cpl
  2377. ((super . cpl)
  2378. (let ((s (class-slot-definition super name)))
  2379. (if s
  2380. (list (slot-definition-slot-ref s)
  2381. (slot-definition-slot-set! s))
  2382. ;; Multiple inheritance means that we might have
  2383. ;; to look deeper in the CPL.
  2384. (lp cpl)))))))))
  2385. ((#:each-subclass) ;; slot shared by instances of direct subclass.
  2386. ;; (Thomas Buerger, April 1998)
  2387. (make-closure-variable class (class-slot-init-value)))
  2388. ((#:virtual) ;; No allocation
  2389. ;; slot-ref and slot-set! function must be given by the user
  2390. (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
  2391. (set (get-keyword #:slot-set! (slot-definition-options s) #f)))
  2392. (unless (and get set)
  2393. (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S" s))
  2394. (list get set)))
  2395. (else (next-method))))
  2396. (define-method (compute-get-n-set (o <object>) s)
  2397. (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
  2398. (define-method (compute-slots (class <class>))
  2399. (build-slots-list (class-direct-slots class)
  2400. (class-precedence-list class)))
  2401. ;;;
  2402. ;;; {Initialize}
  2403. ;;;
  2404. ;; FIXME: This could be much more efficient.
  2405. (define (%initialize-object obj initargs)
  2406. "Initialize the object @var{obj} with the given arguments
  2407. var{initargs}."
  2408. (define (valid-initargs? initargs)
  2409. (match initargs
  2410. (() #t)
  2411. (((? keyword?) _ . initargs) (valid-initargs? initargs))
  2412. (_ #f)))
  2413. (unless (instance? obj)
  2414. (scm-error 'wrong-type-arg #f "Not an object: ~S"
  2415. (list obj) #f))
  2416. (unless (valid-initargs? initargs)
  2417. (scm-error 'wrong-type-arg #f "Invalid initargs: ~S"
  2418. (list initargs) #f))
  2419. (let ((class (class-of obj)))
  2420. (define (get-initarg kw)
  2421. (if kw
  2422. ;; Inlined get-keyword to avoid checking initargs for validity
  2423. ;; each time.
  2424. (let lp ((initargs initargs))
  2425. (match initargs
  2426. ((kw* val . initargs)
  2427. (if (eq? kw* kw)
  2428. val
  2429. (lp initargs)))
  2430. (_ *unbound*)))
  2431. *unbound*))
  2432. (let lp ((slots (struct-ref class class-index-slots)))
  2433. (match slots
  2434. (() obj)
  2435. ((slot . slots)
  2436. (define (initialize-slot! value)
  2437. ((%slot-definition-slot-set! slot) obj value))
  2438. (let ((initarg (get-initarg (%slot-definition-init-keyword slot))))
  2439. (cond
  2440. ((not (unbound? initarg))
  2441. (initialize-slot! initarg))
  2442. ((%slot-definition-init-thunk slot)
  2443. => (lambda (init-thunk)
  2444. (unless (memq (slot-definition-allocation slot)
  2445. '(#:class #:each-subclass))
  2446. (initialize-slot! (init-thunk)))))))
  2447. (lp slots))))))
  2448. (define-method (initialize (object <object>) initargs)
  2449. (%initialize-object object initargs))
  2450. (define-method (initialize (slot <slot>) initargs)
  2451. (next-method)
  2452. (struct-set! slot slot-index-options initargs)
  2453. (let ((init-thunk (%slot-definition-init-thunk slot)))
  2454. (when init-thunk
  2455. (unless (thunk? init-thunk)
  2456. (goops-error "Bad init-thunk for slot `~S': ~S"
  2457. (%slot-definition-name slot) init-thunk)))))
  2458. (define-method (initialize (class <class>) initargs)
  2459. (define (make-direct-slot-definition dslot)
  2460. (let ((initargs (compute-direct-slot-definition-initargs class dslot)))
  2461. (compute-direct-slot-definition class initargs)))
  2462. (next-method)
  2463. (class-add-flags! class vtable-flag-goops-class)
  2464. (struct-set! class class-index-name (get-keyword #:name initargs '???))
  2465. (struct-set!/unboxed class class-index-nfields 0)
  2466. (struct-set! class class-index-direct-supers
  2467. (get-keyword #:dsupers initargs '()))
  2468. (struct-set! class class-index-direct-subclasses '())
  2469. (struct-set! class class-index-direct-methods '())
  2470. (struct-set! class class-index-cpl (compute-cpl class))
  2471. (when (get-keyword #:static-slot-allocation? initargs #f)
  2472. (match (filter class-has-statically-allocated-slots?
  2473. (class-precedence-list class))
  2474. (()
  2475. (class-add-flags! class vtable-flag-goops-static-slot-allocation))
  2476. (classes
  2477. (error "Class has superclasses with static slot allocation" classes))))
  2478. (struct-set! class class-index-direct-slots
  2479. (map (lambda (slot)
  2480. (if (slot? slot)
  2481. slot
  2482. (make-direct-slot-definition slot)))
  2483. (get-keyword #:slots initargs '())))
  2484. (struct-set! class class-index-slots
  2485. (allocate-slots class (compute-slots class)))
  2486. ;; This is a hack.
  2487. (when (memq <slot> (struct-ref class class-index-cpl))
  2488. (class-add-flags! class vtable-flag-goops-slot))
  2489. ;; Build getters - setters - accessors
  2490. (compute-slot-accessors class (struct-ref class class-index-slots))
  2491. ;; Update the "direct-subclasses" of each inherited classes
  2492. (for-each (lambda (x)
  2493. (let ((dsubs (struct-ref x class-index-direct-subclasses)))
  2494. (struct-set! x class-index-direct-subclasses
  2495. (cons class dsubs))))
  2496. (struct-ref class class-index-direct-supers))
  2497. ;; Compute struct layout of instances, set the `layout' slot, and
  2498. ;; update class flags.
  2499. (%prep-layout! class))
  2500. (define (initialize-object-procedure object initargs)
  2501. (let ((proc (get-keyword #:procedure initargs #f)))
  2502. (cond ((not proc))
  2503. ((pair? proc)
  2504. (apply slot-set! object 'procedure proc))
  2505. (else
  2506. (slot-set! object 'procedure proc)))))
  2507. (define-method (initialize (applicable-struct <applicable-struct>) initargs)
  2508. (next-method)
  2509. (initialize-object-procedure applicable-struct initargs))
  2510. (define-method (initialize (applicable-struct <applicable-struct-with-setter>)
  2511. initargs)
  2512. (next-method)
  2513. (slot-set! applicable-struct 'setter (get-keyword #:setter initargs #f)))
  2514. (define-method (initialize (generic <generic>) initargs)
  2515. (let ((previous-definition (get-keyword #:default initargs #f))
  2516. (name (get-keyword #:name initargs #f)))
  2517. (next-method)
  2518. (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
  2519. (list (method args
  2520. (apply previous-definition args)))
  2521. '()))
  2522. (if name
  2523. (set-procedure-property! generic 'name name))
  2524. (invalidate-method-cache! generic)))
  2525. (define-method (initialize (eg <extended-generic>) initargs)
  2526. (next-method)
  2527. (slot-set! eg 'extends (get-keyword #:extends initargs '())))
  2528. (define dummy-procedure (lambda args *unspecified*))
  2529. (define-method (initialize (method <method>) initargs)
  2530. (next-method)
  2531. (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
  2532. (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
  2533. (slot-set! method 'procedure
  2534. (get-keyword #:procedure initargs #f))
  2535. (slot-set! method 'formals (get-keyword #:formals initargs '()))
  2536. (slot-set! method 'body (get-keyword #:body initargs '()))
  2537. (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f)))
  2538. ;;;
  2539. ;;; {make}
  2540. ;;;
  2541. ;;; A new definition which overwrites the previous one which was built-in
  2542. ;;;
  2543. (define-method (allocate-instance (class <class>) initargs)
  2544. (%allocate-instance class))
  2545. (define-method (make-instance (class <class>) . initargs)
  2546. (let ((instance (allocate-instance class initargs)))
  2547. (initialize instance initargs)
  2548. instance))
  2549. (define make make-instance)
  2550. ;;;
  2551. ;;; {apply-generic}
  2552. ;;;
  2553. ;;; Protocol for calling generic functions, intended to be used when
  2554. ;;; applying subclasses of <generic> and <generic-with-setter>. The
  2555. ;;; code below is similar to the first MOP described in AMOP.
  2556. ;;;
  2557. ;;; Note that standard generic functions dispatch only on the classes of
  2558. ;;; the arguments, and the result of such dispatch can be memoized. The
  2559. ;;; `dispatch-generic-function-application-from-cache' routine
  2560. ;;; implements this. `apply-generic' isn't called currently; the
  2561. ;;; generic function MOP was never fully implemented in GOOPS. However
  2562. ;;; now that GOOPS is implemented entirely in Scheme (2015) it's much
  2563. ;;; easier to complete this work. Contributions gladly accepted!
  2564. ;;; Please read the AMOP first though :)
  2565. ;;;
  2566. ;;; The protocol is:
  2567. ;;;
  2568. ;;; + apply-generic (gf args)
  2569. ;;; + compute-applicable-methods (gf args ...)
  2570. ;;; + sort-applicable-methods (gf methods args)
  2571. ;;; + apply-methods (gf methods args)
  2572. ;;;
  2573. ;;; apply-methods calls make-next-method to build the "continuation" of
  2574. ;;; a method. Applying a next-method will call apply-next-method which
  2575. ;;; in turn will call apply again to call effectively the following
  2576. ;;; method. (This paragraph is out of date but is kept so that maybe it
  2577. ;;; illuminates some future hack.)
  2578. ;;;
  2579. (define-method (apply-generic (gf <generic>) args)
  2580. (when (null? (slot-ref gf 'methods))
  2581. (no-method gf args))
  2582. (let ((methods (compute-applicable-methods gf args)))
  2583. (if methods
  2584. (apply-methods gf (sort-applicable-methods gf methods args) args)
  2585. (no-applicable-method gf args))))
  2586. ;; compute-applicable-methods is bound to %compute-applicable-methods.
  2587. (define compute-applicable-methods
  2588. (let ((gf (make <generic> #:name 'compute-applicable-methods)))
  2589. (add-method! gf (method ((gf <generic>) args)
  2590. (%compute-applicable-methods gf args)))
  2591. gf))
  2592. (define-method (sort-applicable-methods (gf <generic>) methods args)
  2593. (%sort-applicable-methods methods (map class-of args)))
  2594. (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
  2595. (%method-more-specific? m1 m2 targs))
  2596. (define compute-effective-method
  2597. (let ((gf (make <generic> #:name 'compute-effective-method)))
  2598. (add-method! gf (method ((gf <generic>) methods typev)
  2599. (%compute-effective-method gf methods typev)))
  2600. gf))
  2601. (define compute-specialized-effective-method
  2602. (let ((gf (make <generic> #:name 'compute-specialized-effective-method)))
  2603. (add-method!
  2604. gf
  2605. (method ((gf <generic>) (method <method>) typev next)
  2606. (%compute-specialized-effective-method gf method typev next)))
  2607. gf))
  2608. (define-method (compute-specialized-effective-method (gf <generic>)
  2609. (m <accessor-method>)
  2610. typev
  2611. next)
  2612. (let ((name (slot-definition-name (accessor-method-slot-definition m))))
  2613. (match typev
  2614. (#(class)
  2615. (slot-definition-slot-ref (class-slot-definition class name)))
  2616. (#(class _)
  2617. (slot-definition-slot-set! (class-slot-definition class name)))
  2618. (_
  2619. (next-method)))))
  2620. (define-method (apply-method (gf <generic>) methods build-next args)
  2621. (apply (method-procedure (car methods))
  2622. (build-next (cdr methods) args)
  2623. args))
  2624. (define-method (apply-methods (gf <generic>) (l <list>) args)
  2625. (letrec ((next (lambda (procs args)
  2626. (lambda new-args
  2627. (let ((a (if (null? new-args) args new-args)))
  2628. (if (null? procs)
  2629. (no-next-method gf a)
  2630. (apply-method gf procs next a)))))))
  2631. (apply-method gf l next args)))
  2632. ;; We don't want the following procedure to turn up in backtraces:
  2633. (for-each (lambda (proc)
  2634. (set-procedure-property! proc 'system-procedure #t))
  2635. (list slot-unbound
  2636. slot-missing
  2637. no-next-method
  2638. no-applicable-method
  2639. no-method
  2640. ))
  2641. ;;;
  2642. ;;; Class redefinition
  2643. ;;;
  2644. ;;; GOOPS has a facility to allow a user to change the definition of
  2645. ;;; class. This will cause instances of that class to lazily migrate
  2646. ;;; over to the new definition. Implementing this is tricky because
  2647. ;;; identity is a fundamental part of object-oriented programming; you
  2648. ;;; can't just make a new class and start using it, just like that. In
  2649. ;;; GOOPS, classes are objects too and need to be addressable by
  2650. ;;; identity (by `eq?'). Classes need the ability to change their
  2651. ;;; definition "in place". The same goes for instances; redefining a
  2652. ;;; class might change the amount of storage associated with each
  2653. ;;; instance, and yet we need to update the instances in place, and
  2654. ;;; without having classes maintain a list of all of their instances.
  2655. ;;;
  2656. ;;; The way that we implement this is by adding an indirection. An
  2657. ;;; instance of a redefinable class becomes a small object containing
  2658. ;;; only a single field, a reference to an external "slots" objects that
  2659. ;;; holds the actual slots. There is an exception however for objects
  2660. ;;; that have statically allocated slots, most importantly classes -- in
  2661. ;;; that case the indirected slots are allocated "directly" in the
  2662. ;;; object.
  2663. ;;;
  2664. ;;; Instances update by checking the class of their their indirected
  2665. ;;; slots object. In addition to describing the slots of the indirected
  2666. ;;; slots object, that slots class (which is a direct class) has a
  2667. ;;; "redefined" slot. If the indirect slots object is current, this
  2668. ;;; value is #f. Otherwise it points to the old class definition
  2669. ;;; corresponding to its instances.
  2670. ;;;
  2671. ;;; To try to clarify things, here is a diagram of the "normal" state of
  2672. ;;; affairs. The redefinable class has an associated slots class. When
  2673. ;;; it makes instances, the instances have a pointer to the indirect
  2674. ;;; "slots" object. The class of the indirect slots object is the slots
  2675. ;;; class associated with the instance's class. The "V" arrows indicate
  2676. ;;; a vtable (class-of) relationship. Dashed arrows indicate a reference
  2677. ;;; from a struct slot to an object.
  2678. ;;;
  2679. ;;; Initial state.
  2680. ;;; +-------------+ +------------------------------+
  2681. ;;; | class ----> slots class, redefined: #f |
  2682. ;;; +-V-----------+ +-V----------------------------+
  2683. ;;; V V
  2684. ;;; +-V-----------+ +-V----------------------------+
  2685. ;;; | instance ----> slots ... |
  2686. ;;; +-------------+ +------------------------------+
  2687. ;;;
  2688. ;;; When a class is redefined, it is updated in place. However existing
  2689. ;;; instances are only migrated lazily. So after a class has been
  2690. ;;; redefined but before the instance has been updated, the state looks
  2691. ;;; like this:
  2692. ;;;
  2693. ;;; Redefined state.
  2694. ;;; ,-------------------------------------------.
  2695. ;;; | |
  2696. ;;; +-v-----------+ +----------------------------|-+
  2697. ;;; | old class ----> old slots class, redefined:' VVV
  2698. ;;; +-------------+ +------------------------------+ V
  2699. ;;; V
  2700. ;;; +-------------+ +------------------------------+ V
  2701. ;;; | new class ----> new slots class, redefined:#f| V
  2702. ;;; +-V-----------+ +------------------------------+ V
  2703. ;;; V V
  2704. ;;; +-V-----------+ +------------------------------+ V
  2705. ;;; | old inst ----> slots ... VVV
  2706. ;;; +-------------+ +------------------------------+
  2707. ;;;
  2708. ;;; That is to say, because the class was updated in place, the old
  2709. ;;; instance's vtable is the new class, even though the old instance's
  2710. ;;; slots still correspond to the old class. The vtable of the old slots
  2711. ;;; has the "redefined" field, which has been set to point to a fresh
  2712. ;;; object containing the direct slots of the old class, and a pointer to
  2713. ;;; the old slots class -- as if it were the old class, but with a new
  2714. ;;; temporary identity. This allows us to then call
  2715. ;;;
  2716. ;;; (change-object-class obj old-class new-class)
  2717. ;;;
  2718. ;;; which will allocate a fresh slots object for the old instance
  2719. ;;; corresponding to the new class, completing the migration for that
  2720. ;;; instance.
  2721. ;;;
  2722. ;;; Lazy instance migration is triggered by "class-of". Calling
  2723. ;;; "class-of" on an indirect instance will check the indirect slots to
  2724. ;;; see if they need redefinition. If so, we construct a fresh instance
  2725. ;;; of the new class and swap fields with the old instance (including
  2726. ;;; the indirect-slots field). Unfortunately there is some
  2727. ;;; thread-unsafety here, as retrieving the class is unsynchronized with
  2728. ;;; retrieving the indirect slots.
  2729. ;;;
  2730. (define-class <indirect-slots-class> (<class>)
  2731. (%redefined #:init-value #f))
  2732. (define-class <redefinable-class> (<class>)
  2733. (indirect-slots-class))
  2734. (define-method (compute-slots (class <redefinable-class>))
  2735. (let* ((slots (next-method))
  2736. ;; The base method ensured that at most one superclass has
  2737. ;; statically allocated slots.
  2738. (static-slots
  2739. (match (filter class-has-statically-allocated-slots?
  2740. (cdr (class-precedence-list class)))
  2741. (() '())
  2742. ((class) (struct-ref class class-index-direct-slots)))))
  2743. (define (simplify-slot-definition s)
  2744. ;; Here we take a slot definition and strip it to just be a plain
  2745. ;; old name, suitable for use as a slot for the plain-old-data
  2746. ;; indirect-slots class.
  2747. (and (eq? (slot-definition-allocation s) #:instance)
  2748. (make (class-of s) #:name (slot-definition-name s))))
  2749. (define (maybe-make-indirect-slot-definition s)
  2750. ;; Here we copy over all the frippery of a slot definition
  2751. ;; (accessors, init-keywords, and so on), but we change the slot
  2752. ;; to have virtual allocation and we provide explicit
  2753. ;; slot-ref/slot-set! functions that access the slot value through
  2754. ;; the indirect slots object. For slot definitions without
  2755. ;; instance allocation though, we just pass them through.
  2756. (cond
  2757. ((eq? (slot-definition-allocation s) #:instance)
  2758. (let* ((s* (class-slot-definition (slot-ref class 'indirect-slots-class)
  2759. (slot-definition-name s)))
  2760. (ref (slot-definition-slot-ref/raw s*))
  2761. (set! (slot-definition-slot-set! s*)))
  2762. (apply make (class-of s)
  2763. #:allocation #:virtual
  2764. ;; TODO: Make faster.
  2765. #:slot-ref (lambda (o)
  2766. (ref (slot-ref o 'indirect-slots)))
  2767. #:slot-set! (lambda (o v)
  2768. (set! (slot-ref o 'indirect-slots) v))
  2769. (let loop ((options (slot-definition-options s)))
  2770. (match options
  2771. (() '())
  2772. (((or #:allocation #:slot-ref #:slot-set!) _ . rest)
  2773. (loop rest))
  2774. ((kw arg . rest)
  2775. (cons* kw arg (loop rest))))))))
  2776. (else s)))
  2777. (unless (equal? (list-head slots (length static-slots))
  2778. static-slots)
  2779. (error "unexpected slots"))
  2780. (let* ((indirect-slots (list-tail slots (length static-slots)))
  2781. (indirect-slots-class
  2782. (make-class '()
  2783. (filter-map simplify-slot-definition
  2784. indirect-slots)
  2785. #:name 'indirect-slots
  2786. #:metaclass <indirect-slots-class>)))
  2787. (slot-set! class 'indirect-slots-class indirect-slots-class)
  2788. (append static-slots
  2789. (cons (make <slot> #:name 'indirect-slots)
  2790. (map maybe-make-indirect-slot-definition
  2791. indirect-slots))))))
  2792. (define-method (initialize (class <redefinable-class>) initargs)
  2793. (next-method)
  2794. (class-add-flags! class vtable-flag-goops-indirect))
  2795. (define-method (allocate-instance (class <redefinable-class>) initargs)
  2796. (let ((instance (next-method))
  2797. (nfields (struct-ref/unboxed class class-index-nfields))
  2798. (indirect-slots-class (slot-ref class 'indirect-slots-class)))
  2799. ;; Indirect slots will be last struct field.
  2800. (struct-set! instance (1- nfields) (make indirect-slots-class))
  2801. instance))
  2802. ;; Called when redefining an existing binding, and the new binding is a
  2803. ;; class. Two arguments: the old value, and the new.
  2804. (define-generic class-redefinition)
  2805. (define-method (class-redefinition (old <top>) (new <class>))
  2806. ;; Default class-redefinition method is to just replace old binding
  2807. ;; with the class.
  2808. new)
  2809. (define-method (class-redefinition (old <redefinable-class>)
  2810. (new <redefinable-class>))
  2811. ;; When redefining a redefinable class with a redefinable class, we
  2812. ;; migrate the old definition and its instances to become the new
  2813. ;; definition.
  2814. ;;
  2815. ;; Work on direct methods:
  2816. ;; 1. Remove accessor methods from the old class
  2817. ;; 2. Patch the occurences of new in the specializers by old
  2818. ;; 3. Displace the methods from old to new
  2819. (remove-class-accessors! old) ;; -1-
  2820. (let ((methods (class-direct-methods new)))
  2821. (for-each (lambda (m)
  2822. (update-direct-method! m new old)) ;; -2-
  2823. methods)
  2824. (struct-set! new
  2825. class-index-direct-methods
  2826. (append methods (class-direct-methods old))))
  2827. ;; Substitute old for new in new cpl
  2828. (set-car! (struct-ref new class-index-cpl) old)
  2829. ;; Remove the old class from the direct-subclasses list of its super classes
  2830. (for-each (lambda (c) (struct-set! c class-index-direct-subclasses
  2831. (delv! old (class-direct-subclasses c))))
  2832. (class-direct-supers old))
  2833. ;; Replace the new class with the old in the direct-subclasses of the supers
  2834. (for-each (lambda (c)
  2835. (struct-set! c class-index-direct-subclasses
  2836. (cons old (delv! new (class-direct-subclasses c)))))
  2837. (class-direct-supers new))
  2838. ;; Swap object headers
  2839. (%modify-instance old new)
  2840. ;; Now old is NEW!
  2841. ;; Redefine all the subclasses of old to take into account modification
  2842. (for-each
  2843. (lambda (c)
  2844. (update-direct-subclass! c new old))
  2845. (class-direct-subclasses new))
  2846. ;; Invalidate class so that subsequent instance slot accesses invoke
  2847. ;; change-object-class
  2848. (let ((slots-class (slot-ref new 'indirect-slots-class)))
  2849. (slot-set! slots-class '%redefined new)
  2850. (class-add-flags! slots-class vtable-flag-goops-needs-migration))
  2851. old)
  2852. (define-method (remove-class-accessors! (c <class>))
  2853. (for-each (lambda (m)
  2854. (when (is-a? m <accessor-method>)
  2855. (let ((gf (slot-ref m 'generic-function)))
  2856. ;; remove the method from its GF
  2857. (slot-set! gf 'methods
  2858. (delq1! m (slot-ref gf 'methods)))
  2859. (invalidate-method-cache! gf)
  2860. ;; remove the method from its specializers
  2861. (remove-method-in-classes! m))))
  2862. (class-direct-methods c)))
  2863. (define-method (update-direct-method! (m <method>)
  2864. (old <class>)
  2865. (new <class>))
  2866. (let loop ((l (method-specializers m)))
  2867. ;; Note: the <top> in dotted list is never used.
  2868. ;; So we can work as if we had only proper lists.
  2869. (when (pair? l)
  2870. (when (eqv? (car l) old)
  2871. (set-car! l new))
  2872. (loop (cdr l)))))
  2873. (define-method (update-direct-subclass! (c <class>)
  2874. (old <class>)
  2875. (new <class>))
  2876. (class-redefinition c
  2877. (make-class (class-direct-supers c)
  2878. (class-direct-slots c)
  2879. #:name (class-name c)
  2880. #:metaclass (class-of c))))
  2881. (define (change-object-class old-instance old-class new-class)
  2882. (let ((new-instance (allocate-instance new-class '())))
  2883. ;; Initialize the slots of the new instance
  2884. (for-each
  2885. (lambda (slot)
  2886. (unless (eq? slot 'indirect-slots)
  2887. (if (and (slot-exists? old-instance slot)
  2888. (memq (%slot-definition-allocation
  2889. (class-slot-definition old-class slot))
  2890. '(#:instance #:virtual))
  2891. (slot-bound? old-instance slot))
  2892. ;; Slot was present and allocated in old instance; copy it
  2893. (slot-set! new-instance slot (slot-ref old-instance slot))
  2894. ;; slot was absent; initialize it with its default value
  2895. (let ((init (slot-init-function new-class slot)))
  2896. (when init
  2897. (slot-set! new-instance slot (init)))))))
  2898. (map slot-definition-name (class-slots new-class)))
  2899. ;; Exchange old and new instance in place to keep pointers valid
  2900. (%modify-instance old-instance new-instance)
  2901. ;; Allow class specific updates of instances (which now are swapped)
  2902. (update-instance-for-different-class new-instance old-instance)
  2903. old-instance))
  2904. (define-method (update-instance-for-different-class (old-instance <object>)
  2905. (new-instance
  2906. <object>))
  2907. ;;not really important what we do, we just need a default method
  2908. new-instance)
  2909. (define-method (change-class (old-instance <object>)
  2910. (new-class <redefinable-class>))
  2911. (unless (is-a? (class-of old-instance) <redefinable-class>)
  2912. (error (string-append
  2913. "Default change-class implementation only works on"
  2914. " instances of redefinable classes")))
  2915. (change-object-class old-instance (class-of old-instance) new-class))
  2916. (define class-of-obsolete-indirect-instance
  2917. (let ((lock (make-mutex))
  2918. (stack '()))
  2919. (lambda (instance)
  2920. (let* ((new-class (struct-vtable instance))
  2921. (nfields (struct-ref/unboxed new-class class-index-nfields))
  2922. ;; Indirect slots are in last instance slot. For normal
  2923. ;; instances last slot is 0 of course.
  2924. (slots (struct-ref instance (1- nfields)))
  2925. (old-class (slot-ref (class-of slots) '%redefined)))
  2926. (let/ec return
  2927. (dynamic-wind
  2928. (lambda ()
  2929. (with-mutex lock
  2930. (if (memv slots stack)
  2931. (return (or old-class new-class))
  2932. (set! stack (cons slots stack)))))
  2933. (lambda ()
  2934. (when old-class
  2935. (change-class instance new-class))
  2936. new-class)
  2937. (lambda ()
  2938. (with-mutex lock
  2939. (set! stack (delq! slots stack))))))))))
  2940. ;;;
  2941. ;;; {Final initialization}
  2942. ;;;
  2943. ;; Tell C code that the main bulk of Goops has been loaded
  2944. (%goops-loaded)
  2945. ;;;
  2946. ;;; {SMOB and port classes}
  2947. ;;;
  2948. (define <promise> (find-subclass <top> '<promise>))
  2949. (define <thread> (find-subclass <top> '<thread>))
  2950. (define <mutex> (find-subclass <top> '<mutex>))
  2951. (define <condition-variable> (find-subclass <top> '<condition-variable>))
  2952. (define <regexp> (find-subclass <top> '<regexp>))
  2953. (define <hook> (find-subclass <top> '<hook>))
  2954. (define <bitvector> (find-subclass <top> '<bitvector>))
  2955. (define <random-state> (find-subclass <top> '<random-state>))
  2956. (define <directory> (find-subclass <top> '<directory>))
  2957. (define <array> (find-subclass <top> '<array>))
  2958. (define <character-set> (find-subclass <top> '<character-set>))
  2959. (define <guardian> (find-subclass <applicable> '<guardian>))
  2960. (define <macro> (find-subclass <top> '<macro>))
  2961. ;; <dynamic-object> used to be a SMOB type, albeit not exported even to
  2962. ;; C. However now it's a record type, though still private. Cross our
  2963. ;; fingers that nobody is using it in anger!
  2964. (define <dynamic-object>
  2965. (module-ref (resolve-module '(system foreign-library)) '<foreign-library>))
  2966. (define (define-class-subtree class)
  2967. (define! (class-name class) class)
  2968. (for-each define-class-subtree (class-direct-subclasses class)))
  2969. (define-class-subtree (find-subclass <port> '<file-port>))