goops.scm 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1998,1999,2000,2001,2002, 2003, 2006, 2009, 2010 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. ;;;; This software is a derivative work of other copyrighted softwares; the
  19. ;;;; copyright notices of these softwares are placed in the file COPYRIGHTS
  20. ;;;;
  21. ;;;; This file is based upon stklos.stk from the STk distribution by
  22. ;;;; Erick Gallesio <eg@unice.fr>.
  23. ;;;;
  24. (define-module (oop goops)
  25. :use-module (srfi srfi-1)
  26. :export-syntax (define-class class standard-define-class
  27. define-generic define-accessor define-method
  28. define-extended-generic define-extended-generics
  29. method)
  30. :export (is-a? class-of
  31. ensure-metaclass ensure-metaclass-with-supers
  32. make-class
  33. make-generic ensure-generic
  34. make-extended-generic
  35. make-accessor ensure-accessor
  36. add-method!
  37. class-slot-ref class-slot-set! slot-unbound slot-missing
  38. slot-definition-name slot-definition-options
  39. slot-definition-allocation
  40. slot-definition-getter slot-definition-setter
  41. slot-definition-accessor
  42. slot-definition-init-value slot-definition-init-form
  43. slot-definition-init-thunk slot-definition-init-keyword
  44. slot-init-function class-slot-definition
  45. method-source
  46. compute-cpl compute-std-cpl compute-get-n-set compute-slots
  47. compute-getter-method compute-setter-method
  48. allocate-instance initialize make-instance make
  49. no-next-method no-applicable-method no-method
  50. change-class update-instance-for-different-class
  51. shallow-clone deep-clone
  52. class-redefinition
  53. apply-generic apply-method apply-methods
  54. compute-applicable-methods %compute-applicable-methods
  55. method-more-specific? sort-applicable-methods
  56. class-subclasses class-methods
  57. goops-error
  58. min-fixnum max-fixnum
  59. ;;; *fixme* Should go into goops.c
  60. instance? slot-ref-using-class
  61. slot-set-using-class! slot-bound-using-class?
  62. slot-exists-using-class? slot-ref slot-set! slot-bound?
  63. class-name class-direct-supers class-direct-subclasses
  64. class-direct-methods class-direct-slots class-precedence-list
  65. class-slots
  66. generic-function-name
  67. generic-function-methods method-generic-function
  68. method-specializers method-formals
  69. primitive-generic-generic enable-primitive-generic!
  70. method-procedure accessor-method-slot-definition
  71. slot-exists? make find-method get-keyword)
  72. :no-backtrace)
  73. (define *goops-module* (current-module))
  74. ;; First initialize the builtin part of GOOPS
  75. (eval-when (eval load compile)
  76. (%init-goops-builtins))
  77. (eval-when (eval load compile)
  78. (use-modules ((language tree-il primitives) :select (add-interesting-primitive!)))
  79. (add-interesting-primitive! 'class-of)
  80. (define (@slot-ref o n)
  81. (struct-ref o n))
  82. (define (@slot-set! o n v)
  83. (struct-set! o n v))
  84. (add-interesting-primitive! '@slot-ref)
  85. (add-interesting-primitive! '@slot-set!))
  86. ;; Then load the rest of GOOPS
  87. (use-modules (oop goops util)
  88. (oop goops dispatch)
  89. (oop goops compile))
  90. (eval-when (eval load compile)
  91. (define min-fixnum (- (expt 2 29)))
  92. (define max-fixnum (- (expt 2 29) 1)))
  93. ;;
  94. ;; goops-error
  95. ;;
  96. (define (goops-error format-string . args)
  97. (scm-error 'goops-error #f format-string args '()))
  98. ;;
  99. ;; is-a?
  100. ;;
  101. (define (is-a? obj class)
  102. (and (memq class (class-precedence-list (class-of obj))) #t))
  103. ;;;
  104. ;;; {Meta classes}
  105. ;;;
  106. (define ensure-metaclass-with-supers
  107. (let ((table-of-metas '()))
  108. (lambda (meta-supers)
  109. (let ((entry (assoc meta-supers table-of-metas)))
  110. (if entry
  111. ;; Found a previously created metaclass
  112. (cdr entry)
  113. ;; Create a new meta-class which inherit from "meta-supers"
  114. (let ((new (make <class> #:dsupers meta-supers
  115. #:slots '()
  116. #:name (gensym "metaclass"))))
  117. (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
  118. new))))))
  119. (define (ensure-metaclass supers)
  120. (if (null? supers)
  121. <class>
  122. (let* ((all-metas (map (lambda (x) (class-of x)) supers))
  123. (all-cpls (append-map (lambda (m)
  124. (cdr (class-precedence-list m)))
  125. all-metas))
  126. (needed-metas '()))
  127. ;; Find the most specific metaclasses. The new metaclass will be
  128. ;; a subclass of these.
  129. (for-each
  130. (lambda (meta)
  131. (if (and (not (member meta all-cpls))
  132. (not (member meta needed-metas)))
  133. (set! needed-metas (append needed-metas (list meta)))))
  134. all-metas)
  135. ;; Now return a subclass of the metaclasses we found.
  136. (if (null? (cdr needed-metas))
  137. (car needed-metas) ; If there's only one, just use it.
  138. (ensure-metaclass-with-supers needed-metas)))))
  139. ;;;
  140. ;;; {Classes}
  141. ;;;
  142. ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
  143. ;;;
  144. ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
  145. ;;; OPTION ::= KEYWORD VALUE
  146. ;;;
  147. (define (kw-do-map mapper f kwargs)
  148. (define (keywords l)
  149. (cond
  150. ((null? l) '())
  151. ((or (null? (cdr l)) (not (keyword? (car l))))
  152. (goops-error "malformed keyword arguments: ~a" kwargs))
  153. (else (cons (car l) (keywords (cddr l))))))
  154. (define (args l)
  155. (if (null? l) '() (cons (cadr l) (args (cddr l)))))
  156. ;; let* to check keywords first
  157. (let* ((k (keywords kwargs))
  158. (a (args kwargs)))
  159. (mapper f k a)))
  160. (define (make-class supers slots . options)
  161. (let* ((name (get-keyword #:name options (make-unbound)))
  162. (supers (if (not (or-map (lambda (class)
  163. (memq <object>
  164. (class-precedence-list class)))
  165. supers))
  166. (append supers (list <object>))
  167. supers))
  168. (metaclass (or (get-keyword #:metaclass options #f)
  169. (ensure-metaclass supers))))
  170. ;; Verify that all direct slots are different and that we don't inherit
  171. ;; several time from the same class
  172. (let ((tmp1 (find-duplicate supers))
  173. (tmp2 (find-duplicate (map slot-definition-name slots))))
  174. (if tmp1
  175. (goops-error "make-class: super class ~S is duplicate in class ~S"
  176. tmp1 name))
  177. (if tmp2
  178. (goops-error "make-class: slot ~S is duplicate in class ~S"
  179. tmp2 name)))
  180. ;; Everything seems correct, build the class
  181. (apply make metaclass
  182. #:dsupers supers
  183. #:slots slots
  184. #:name name
  185. options)))
  186. ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
  187. ;;;
  188. ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
  189. ;;; OPTION ::= KEYWORD VALUE
  190. ;;;
  191. (define-macro (class supers . slots)
  192. (define (make-slot-definition-forms slots)
  193. (map
  194. (lambda (def)
  195. (cond
  196. ((pair? def)
  197. `(list ',(car def)
  198. ,@(kw-do-map append-map
  199. (lambda (kw arg)
  200. (case kw
  201. ((#:init-form)
  202. `(#:init-form ',arg
  203. #:init-thunk (lambda () ,arg)))
  204. (else (list kw arg))))
  205. (cdr def))))
  206. (else
  207. `(list ',def))))
  208. slots))
  209. (if (not (list? supers))
  210. (goops-error "malformed superclass list: ~S" supers))
  211. (let ((slots (take-while (lambda (x) (not (keyword? x))) slots))
  212. (options (or (find-tail keyword? slots) '())))
  213. `(make-class
  214. ;; evaluate super class variables
  215. (list ,@supers)
  216. ;; evaluate slot definitions, except the slot name!
  217. (list ,@(make-slot-definition-forms slots))
  218. ;; evaluate class options
  219. ,@options)))
  220. (define-syntax define-class-pre-definition
  221. (lambda (x)
  222. (syntax-case x ()
  223. ((_ (k arg rest ...) out ...)
  224. (keyword? (syntax->datum (syntax k)))
  225. (case (syntax->datum (syntax k))
  226. ((#:getter #:setter)
  227. (syntax
  228. (define-class-pre-definition (rest ...)
  229. out ...
  230. (if (or (not (defined? 'arg))
  231. (not (is-a? arg <generic>)))
  232. (toplevel-define!
  233. 'arg
  234. (ensure-generic (if (defined? 'arg) arg #f) 'arg))))))
  235. ((#:accessor)
  236. (syntax
  237. (define-class-pre-definition (rest ...)
  238. out ...
  239. (if (or (not (defined? 'arg))
  240. (not (is-a? arg <accessor>)))
  241. (toplevel-define!
  242. 'arg
  243. (ensure-accessor (if (defined? 'arg) arg #f) 'arg))))))
  244. (else
  245. (syntax
  246. (define-class-pre-definition (rest ...) out ...)))))
  247. ((_ () out ...)
  248. (syntax (begin out ...))))))
  249. ;; Some slot options require extra definitions to be made. In
  250. ;; particular, we want to make sure that the generic function objects
  251. ;; which represent accessors exist before `make-class' tries to add
  252. ;; methods to them.
  253. (define-syntax define-class-pre-definitions
  254. (lambda (x)
  255. (syntax-case x ()
  256. ((_ () out ...)
  257. (syntax (begin out ...)))
  258. ((_ (slot rest ...) out ...)
  259. (keyword? (syntax->datum (syntax slot)))
  260. (syntax (begin out ...)))
  261. ((_ (slot rest ...) out ...)
  262. (identifier? (syntax slot))
  263. (syntax (define-class-pre-definitions (rest ...)
  264. out ...)))
  265. ((_ ((slotname slotopt ...) rest ...) out ...)
  266. (syntax (define-class-pre-definitions (rest ...)
  267. out ... (define-class-pre-definition (slotopt ...))))))))
  268. (define-syntax define-class
  269. (syntax-rules ()
  270. ((_ name supers slot ...)
  271. (begin
  272. (define-class-pre-definitions (slot ...))
  273. (if (and (defined? 'name)
  274. (is-a? name <class>)
  275. (memq <object> (class-precedence-list name)))
  276. (class-redefinition name
  277. (class supers slot ... #:name 'name))
  278. (toplevel-define! 'name (class supers slot ... #:name 'name)))))))
  279. (define-syntax standard-define-class
  280. (syntax-rules ()
  281. ((_ arg ...) (define-class arg ...))))
  282. ;;;
  283. ;;; {Generic functions and accessors}
  284. ;;;
  285. ;; Apparently the desired semantics are that we extend previous
  286. ;; procedural definitions, but that if `name' was already a generic, we
  287. ;; overwrite its definition.
  288. (define-macro (define-generic name)
  289. (if (not (symbol? name))
  290. (goops-error "bad generic function name: ~S" name))
  291. `(define ,name
  292. (if (and (defined? ',name) (is-a? ,name <generic>))
  293. (make <generic> #:name ',name)
  294. (ensure-generic (if (defined? ',name) ,name #f) ',name))))
  295. (define-macro (define-extended-generic name val)
  296. (if (not (symbol? name))
  297. (goops-error "bad generic function name: ~S" name))
  298. `(define ,name (make-extended-generic ,val ',name)))
  299. (define-macro (define-extended-generics names . args)
  300. (let ((prefixes (get-keyword #:prefix args #f)))
  301. (if prefixes
  302. `(begin
  303. ,@(map (lambda (name)
  304. `(define-extended-generic ,name
  305. (list ,@(map (lambda (prefix)
  306. (symbol-append prefix name))
  307. prefixes))))
  308. names))
  309. (goops-error "no prefixes supplied"))))
  310. (define (make-generic . name)
  311. (let ((name (and (pair? name) (car name))))
  312. (make <generic> #:name name)))
  313. (define (make-extended-generic gfs . name)
  314. (let* ((name (and (pair? name) (car name)))
  315. (gfs (if (pair? gfs) gfs (list gfs)))
  316. (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs)))
  317. (let ((ans (if gws?
  318. (let* ((sname (and name (make-setter-name name)))
  319. (setters
  320. (append-map (lambda (gf)
  321. (if (is-a? gf <generic-with-setter>)
  322. (list (ensure-generic (setter gf)
  323. sname))
  324. '()))
  325. gfs))
  326. (es (make <extended-generic-with-setter>
  327. #:name name
  328. #:extends gfs
  329. #:setter (make <extended-generic>
  330. #:name sname
  331. #:extends setters))))
  332. (extended-by! setters (setter es))
  333. es)
  334. (make <extended-generic>
  335. #:name name
  336. #:extends gfs))))
  337. (extended-by! gfs ans)
  338. ans)))
  339. (define (extended-by! gfs eg)
  340. (for-each (lambda (gf)
  341. (slot-set! gf 'extended-by
  342. (cons eg (slot-ref gf 'extended-by))))
  343. gfs))
  344. (define (not-extended-by! gfs eg)
  345. (for-each (lambda (gf)
  346. (slot-set! gf 'extended-by
  347. (delq! eg (slot-ref gf 'extended-by))))
  348. gfs))
  349. (define (ensure-generic old-definition . name)
  350. (let ((name (and (pair? name) (car name))))
  351. (cond ((is-a? old-definition <generic>) old-definition)
  352. ((procedure-with-setter? old-definition)
  353. (make <generic-with-setter>
  354. #:name name
  355. #:default (procedure old-definition)
  356. #:setter (setter old-definition)))
  357. ((procedure? old-definition)
  358. (if (generic-capability? old-definition) old-definition
  359. (make <generic> #:name name #:default old-definition)))
  360. (else (make <generic> #:name name)))))
  361. ;; same semantics as <generic>
  362. (define-syntax define-accessor
  363. (syntax-rules ()
  364. ((_ name)
  365. (define name
  366. (cond ((not (defined? 'name)) (ensure-accessor #f 'name))
  367. ((is-a? name <accessor>) (make <accessor> #:name 'name))
  368. (else (ensure-accessor name 'name)))))))
  369. (define (make-setter-name name)
  370. (string->symbol (string-append "setter:" (symbol->string name))))
  371. (define (make-accessor . name)
  372. (let ((name (and (pair? name) (car name))))
  373. (make <accessor>
  374. #:name name
  375. #:setter (make <generic>
  376. #:name (and name (make-setter-name name))))))
  377. (define (ensure-accessor proc . name)
  378. (let ((name (and (pair? name) (car name))))
  379. (cond ((and (is-a? proc <accessor>)
  380. (is-a? (setter proc) <generic>))
  381. proc)
  382. ((is-a? proc <generic-with-setter>)
  383. (upgrade-accessor proc (setter proc)))
  384. ((is-a? proc <generic>)
  385. (upgrade-accessor proc (make-generic name)))
  386. ((procedure-with-setter? proc)
  387. (make <accessor>
  388. #:name name
  389. #:default (procedure proc)
  390. #:setter (ensure-generic (setter proc) name)))
  391. ((procedure? proc)
  392. (ensure-accessor (if (generic-capability? proc)
  393. (make <generic> #:name name #:default proc)
  394. (ensure-generic proc name))
  395. name))
  396. (else
  397. (make-accessor name)))))
  398. (define (upgrade-accessor generic setter)
  399. (let ((methods (slot-ref generic 'methods))
  400. (gws (make (if (is-a? generic <extended-generic>)
  401. <extended-generic-with-setter>
  402. <accessor>)
  403. #:name (generic-function-name generic)
  404. #:extended-by (slot-ref generic 'extended-by)
  405. #:setter setter)))
  406. (if (is-a? generic <extended-generic>)
  407. (let ((gfs (slot-ref generic 'extends)))
  408. (not-extended-by! gfs generic)
  409. (slot-set! gws 'extends gfs)
  410. (extended-by! gfs gws)))
  411. ;; Steal old methods
  412. (for-each (lambda (method)
  413. (slot-set! method 'generic-function gws))
  414. methods)
  415. (slot-set! gws 'methods methods)
  416. gws))
  417. ;;;
  418. ;;; {Methods}
  419. ;;;
  420. (define (toplevel-define! name val)
  421. (module-define! (current-module) name val))
  422. (define-syntax define-method
  423. (syntax-rules (setter)
  424. ((_ ((setter name) . args) body ...)
  425. (begin
  426. (if (or (not (defined? 'name))
  427. (not (is-a? name <accessor>)))
  428. (toplevel-define! 'name
  429. (ensure-accessor
  430. (if (defined? 'name) name #f) 'name)))
  431. (add-method! (setter name) (method args body ...))))
  432. ((_ (name . args) body ...)
  433. (begin
  434. ;; FIXME: this code is how it always was, but it's quite cracky:
  435. ;; it will only define the generic function if it was undefined
  436. ;; before (ok), or *was defined to #f*. The latter is crack. But
  437. ;; there are bootstrap issues about fixing this -- change it to
  438. ;; (is-a? name <generic>) and see.
  439. (if (or (not (defined? 'name))
  440. (not name))
  441. (toplevel-define! 'name (make <generic> #:name 'name)))
  442. (add-method! name (method args body ...))))))
  443. (define-syntax method
  444. (lambda (x)
  445. (define (parse-args args)
  446. (let lp ((ls args) (formals '()) (specializers '()))
  447. (syntax-case ls ()
  448. (((f s) . rest)
  449. (and (identifier? (syntax f)) (identifier? (syntax s)))
  450. (lp (syntax rest)
  451. (cons (syntax f) formals)
  452. (cons (syntax s) specializers)))
  453. ((f . rest)
  454. (identifier? (syntax f))
  455. (lp (syntax rest)
  456. (cons (syntax f) formals)
  457. (cons (syntax <top>) specializers)))
  458. (()
  459. (list (reverse formals)
  460. (reverse (cons (syntax '()) specializers))))
  461. (tail
  462. (identifier? (syntax tail))
  463. (list (append (reverse formals) (syntax tail))
  464. (reverse (cons (syntax <top>) specializers)))))))
  465. (define (find-free-id exp referent)
  466. (syntax-case exp ()
  467. ((x . y)
  468. (or (find-free-id (syntax x) referent)
  469. (find-free-id (syntax y) referent)))
  470. (x
  471. (identifier? (syntax x))
  472. (let ((id (datum->syntax (syntax x) referent)))
  473. (and (free-identifier=? (syntax x) id) id)))
  474. (_ #f)))
  475. (define (compute-procedure formals body)
  476. (syntax-case body ()
  477. ((body0 ...)
  478. (with-syntax ((formals formals))
  479. (syntax (lambda formals body0 ...))))))
  480. (define (->proper args)
  481. (let lp ((ls args) (out '()))
  482. (syntax-case ls ()
  483. ((x . xs) (lp (syntax xs) (cons (syntax x) out)))
  484. (() (reverse out))
  485. (tail (reverse (cons (syntax tail) out))))))
  486. (define (compute-make-procedure formals body next-method)
  487. (syntax-case body ()
  488. ((body ...)
  489. (with-syntax ((next-method next-method))
  490. (syntax-case formals ()
  491. ((formal ...)
  492. (syntax
  493. (lambda (real-next-method)
  494. (lambda (formal ...)
  495. (let ((next-method (lambda args
  496. (if (null? args)
  497. (real-next-method formal ...)
  498. (apply real-next-method args)))))
  499. body ...)))))
  500. (formals
  501. (with-syntax (((formal ...) (->proper (syntax formals))))
  502. (syntax
  503. (lambda (real-next-method)
  504. (lambda formals
  505. (let ((next-method (lambda args
  506. (if (null? args)
  507. (apply real-next-method formal ...)
  508. (apply real-next-method args)))))
  509. body ...)))))))))))
  510. (define (compute-procedures formals body)
  511. ;; So, our use of this is broken, because it operates on the
  512. ;; pre-expansion source code. It's equivalent to just searching
  513. ;; for referent in the datums. Ah well.
  514. (let ((id (find-free-id body 'next-method)))
  515. (if id
  516. ;; return a make-procedure
  517. (values (syntax #f)
  518. (compute-make-procedure formals body id))
  519. (values (compute-procedure formals body)
  520. (syntax #f)))))
  521. (syntax-case x ()
  522. ((_ args) (syntax (method args (if #f #f))))
  523. ((_ args body0 body1 ...)
  524. (with-syntax (((formals (specializer ...)) (parse-args (syntax args))))
  525. (call-with-values
  526. (lambda ()
  527. (compute-procedures (syntax formals) (syntax (body0 body1 ...))))
  528. (lambda (procedure make-procedure)
  529. (with-syntax ((procedure procedure)
  530. (make-procedure make-procedure))
  531. (syntax
  532. (make <method>
  533. #:specializers (cons* specializer ...)
  534. #:formals 'formals
  535. #:body '(body0 body1 ...)
  536. #:make-procedure make-procedure
  537. #:procedure procedure))))))))))
  538. ;;;
  539. ;;; {add-method!}
  540. ;;;
  541. (define (add-method-in-classes! m)
  542. ;; Add method in all the classes which appears in its specializers list
  543. (for-each* (lambda (x)
  544. (let ((dm (class-direct-methods x)))
  545. (if (not (memq m dm))
  546. (slot-set! x 'direct-methods (cons m dm)))))
  547. (method-specializers m)))
  548. (define (remove-method-in-classes! m)
  549. ;; Remove method in all the classes which appears in its specializers list
  550. (for-each* (lambda (x)
  551. (slot-set! x
  552. 'direct-methods
  553. (delv! m (class-direct-methods x))))
  554. (method-specializers m)))
  555. (define (compute-new-list-of-methods gf new)
  556. (let ((new-spec (method-specializers new))
  557. (methods (slot-ref gf 'methods)))
  558. (let loop ((l methods))
  559. (if (null? l)
  560. (cons new methods)
  561. (if (equal? (method-specializers (car l)) new-spec)
  562. (begin
  563. ;; This spec. list already exists. Remove old method from dependents
  564. (remove-method-in-classes! (car l))
  565. (set-car! l new)
  566. methods)
  567. (loop (cdr l)))))))
  568. (define internal-add-method!
  569. (method ((gf <generic>) (m <method>))
  570. (slot-set! m 'generic-function gf)
  571. (slot-set! gf 'methods (compute-new-list-of-methods gf m))
  572. (let ((specializers (slot-ref m 'specializers)))
  573. (slot-set! gf 'n-specialized
  574. (max (length* specializers)
  575. (slot-ref gf 'n-specialized))))
  576. (%invalidate-method-cache! gf)
  577. (add-method-in-classes! m)
  578. *unspecified*))
  579. (define-generic add-method!)
  580. ((method-procedure internal-add-method!) add-method! internal-add-method!)
  581. (define-method (add-method! (proc <procedure>) (m <method>))
  582. (if (generic-capability? proc)
  583. (begin
  584. (enable-primitive-generic! proc)
  585. (add-method! proc m))
  586. (next-method)))
  587. (define-method (add-method! (pg <primitive-generic>) (m <method>))
  588. (add-method! (primitive-generic-generic pg) m))
  589. (define-method (add-method! obj (m <method>))
  590. (goops-error "~S is not a valid generic function" obj))
  591. ;;;
  592. ;;; {Access to meta objects}
  593. ;;;
  594. ;;;
  595. ;;; Methods
  596. ;;;
  597. (define-method (method-source (m <method>))
  598. (let* ((spec (map* class-name (slot-ref m 'specializers)))
  599. (src (procedure-source (slot-ref m 'procedure))))
  600. (and src
  601. (let ((args (cadr src))
  602. (body (cddr src)))
  603. (cons 'method
  604. (cons (map* list args spec)
  605. body))))))
  606. (define-method (method-formals (m <method>))
  607. (slot-ref m 'formals))
  608. ;;;
  609. ;;; Slots
  610. ;;;
  611. (define slot-definition-name car)
  612. (define slot-definition-options cdr)
  613. (define (slot-definition-allocation s)
  614. (get-keyword #:allocation (cdr s) #:instance))
  615. (define (slot-definition-getter s)
  616. (get-keyword #:getter (cdr s) #f))
  617. (define (slot-definition-setter s)
  618. (get-keyword #:setter (cdr s) #f))
  619. (define (slot-definition-accessor s)
  620. (get-keyword #:accessor (cdr s) #f))
  621. (define (slot-definition-init-value s)
  622. ;; can be #f, so we can't use #f as non-value
  623. (get-keyword #:init-value (cdr s) (make-unbound)))
  624. (define (slot-definition-init-form s)
  625. (get-keyword #:init-form (cdr s) (make-unbound)))
  626. (define (slot-definition-init-thunk s)
  627. (get-keyword #:init-thunk (cdr s) #f))
  628. (define (slot-definition-init-keyword s)
  629. (get-keyword #:init-keyword (cdr s) #f))
  630. (define (class-slot-definition class slot-name)
  631. (assq slot-name (class-slots class)))
  632. (define (slot-init-function class slot-name)
  633. (cadr (assq slot-name (slot-ref class 'getters-n-setters))))
  634. (define (accessor-method-slot-definition obj)
  635. "Return the slot definition of the accessor @var{obj}."
  636. (slot-ref obj 'slot-definition))
  637. ;;;
  638. ;;; {Standard methods used by the C runtime}
  639. ;;;
  640. ;;; Methods to compare objects
  641. ;;;
  642. ;; Have to do this in a strange order because equal? is used in the
  643. ;; add-method! implementation; we need to make sure that when the
  644. ;; primitive is extended, that the generic has a method. =
  645. (define g-equal? (make-generic 'equal?))
  646. ;; When this generic gets called, we will have already checked eq? and
  647. ;; eqv? -- the purpose of this generic is to extend equality. So by
  648. ;; default, there is no extension, thus the #f return.
  649. (add-method! g-equal? (method (x y) #f))
  650. (set-primitive-generic! equal? g-equal?)
  651. ;;;
  652. ;;; methods to display/write an object
  653. ;;;
  654. ; Code for writing objects must test that the slots they use are
  655. ; bound. Otherwise a slot-unbound method will be called and will
  656. ; conduct to an infinite loop.
  657. ;; Write
  658. (define (display-address o file)
  659. (display (number->string (object-address o) 16) file))
  660. (define-method (write o file)
  661. (display "#<instance " file)
  662. (display-address o file)
  663. (display #\> file))
  664. (define write-object (primitive-generic-generic write))
  665. (define-method (write (o <object>) file)
  666. (let ((class (class-of o)))
  667. (if (slot-bound? class 'name)
  668. (begin
  669. (display "#<" file)
  670. (display (class-name class) file)
  671. (display #\space file)
  672. (display-address o file)
  673. (display #\> file))
  674. (next-method))))
  675. (define-method (write (class <class>) file)
  676. (let ((meta (class-of class)))
  677. (if (and (slot-bound? class 'name)
  678. (slot-bound? meta 'name))
  679. (begin
  680. (display "#<" file)
  681. (display (class-name meta) file)
  682. (display #\space file)
  683. (display (class-name class) file)
  684. (display #\space file)
  685. (display-address class file)
  686. (display #\> file))
  687. (next-method))))
  688. (define-method (write (gf <generic>) file)
  689. (let ((meta (class-of gf)))
  690. (if (and (slot-bound? meta 'name)
  691. (slot-bound? gf 'methods))
  692. (begin
  693. (display "#<" file)
  694. (display (class-name meta) file)
  695. (let ((name (generic-function-name gf)))
  696. (if name
  697. (begin
  698. (display #\space file)
  699. (display name file))))
  700. (display " (" file)
  701. (display (length (generic-function-methods gf)) file)
  702. (display ")>" file))
  703. (next-method))))
  704. (define-method (write (o <method>) file)
  705. (let ((meta (class-of o)))
  706. (if (and (slot-bound? meta 'name)
  707. (slot-bound? o 'specializers))
  708. (begin
  709. (display "#<" file)
  710. (display (class-name meta) file)
  711. (display #\space file)
  712. (display (map* (lambda (spec)
  713. (if (slot-bound? spec 'name)
  714. (slot-ref spec 'name)
  715. spec))
  716. (method-specializers o))
  717. file)
  718. (display #\space file)
  719. (display-address o file)
  720. (display #\> file))
  721. (next-method))))
  722. ;; Display (do the same thing as write by default)
  723. (define-method (display o file)
  724. (write-object o file))
  725. ;;;
  726. ;;; Handling of duplicate bindings in the module system
  727. ;;;
  728. (define-method (merge-generics (module <module>)
  729. (name <symbol>)
  730. (int1 <module>)
  731. (val1 <top>)
  732. (int2 <module>)
  733. (val2 <top>)
  734. (var <top>)
  735. (val <top>))
  736. #f)
  737. (define-method (merge-generics (module <module>)
  738. (name <symbol>)
  739. (int1 <module>)
  740. (val1 <generic>)
  741. (int2 <module>)
  742. (val2 <generic>)
  743. (var <top>)
  744. (val <boolean>))
  745. (and (not (eq? val1 val2))
  746. (make-variable (make-extended-generic (list val2 val1) name))))
  747. (define-method (merge-generics (module <module>)
  748. (name <symbol>)
  749. (int1 <module>)
  750. (val1 <generic>)
  751. (int2 <module>)
  752. (val2 <generic>)
  753. (var <top>)
  754. (gf <extended-generic>))
  755. (and (not (memq val2 (slot-ref gf 'extends)))
  756. (begin
  757. (slot-set! gf
  758. 'extends
  759. (cons val2 (delq! val2 (slot-ref gf 'extends))))
  760. (slot-set! val2
  761. 'extended-by
  762. (cons gf (delq! gf (slot-ref val2 'extended-by))))
  763. var)))
  764. (module-define! duplicate-handlers 'merge-generics merge-generics)
  765. (define-method (merge-accessors (module <module>)
  766. (name <symbol>)
  767. (int1 <module>)
  768. (val1 <top>)
  769. (int2 <module>)
  770. (val2 <top>)
  771. (var <top>)
  772. (val <top>))
  773. #f)
  774. (define-method (merge-accessors (module <module>)
  775. (name <symbol>)
  776. (int1 <module>)
  777. (val1 <accessor>)
  778. (int2 <module>)
  779. (val2 <accessor>)
  780. (var <top>)
  781. (val <top>))
  782. (merge-generics module name int1 val1 int2 val2 var val))
  783. (module-define! duplicate-handlers 'merge-accessors merge-accessors)
  784. ;;;
  785. ;;; slot access
  786. ;;;
  787. (define (class-slot-g-n-s class slot-name)
  788. (let* ((this-slot (assq slot-name (slot-ref class 'slots)))
  789. (g-n-s (cddr (or (assq slot-name (slot-ref class 'getters-n-setters))
  790. (slot-missing class slot-name)))))
  791. (if (not (memq (slot-definition-allocation this-slot)
  792. '(#:class #:each-subclass)))
  793. (slot-missing class slot-name))
  794. g-n-s))
  795. (define (class-slot-ref class slot)
  796. (let ((x ((car (class-slot-g-n-s class slot)) #f)))
  797. (if (unbound? x)
  798. (slot-unbound class slot)
  799. x)))
  800. (define (class-slot-set! class slot value)
  801. ((cadr (class-slot-g-n-s class slot)) #f value))
  802. (define-method (slot-unbound (c <class>) (o <object>) s)
  803. (goops-error "Slot `~S' is unbound in object ~S" s o))
  804. (define-method (slot-unbound (c <class>) s)
  805. (goops-error "Slot `~S' is unbound in class ~S" s c))
  806. (define-method (slot-unbound (o <object>))
  807. (goops-error "Unbound slot in object ~S" o))
  808. (define-method (slot-missing (c <class>) (o <object>) s)
  809. (goops-error "No slot with name `~S' in object ~S" s o))
  810. (define-method (slot-missing (c <class>) s)
  811. (goops-error "No class slot with name `~S' in class ~S" s c))
  812. (define-method (slot-missing (c <class>) (o <object>) s value)
  813. (slot-missing c o s))
  814. ;;; Methods for the possible error we can encounter when calling a gf
  815. (define-method (no-next-method (gf <generic>) args)
  816. (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
  817. (define-method (no-applicable-method (gf <generic>) args)
  818. (goops-error "No applicable method for ~S in call ~S"
  819. gf (cons (generic-function-name gf) args)))
  820. (define-method (no-method (gf <generic>) args)
  821. (goops-error "No method defined for ~S" gf))
  822. ;;;
  823. ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
  824. ;;;
  825. (define-method (shallow-clone (self <object>))
  826. (let ((clone (%allocate-instance (class-of self) '()))
  827. (slots (map slot-definition-name
  828. (class-slots (class-of self)))))
  829. (for-each (lambda (slot)
  830. (if (slot-bound? self slot)
  831. (slot-set! clone slot (slot-ref self slot))))
  832. slots)
  833. clone))
  834. (define-method (deep-clone (self <object>))
  835. (let ((clone (%allocate-instance (class-of self) '()))
  836. (slots (map slot-definition-name
  837. (class-slots (class-of self)))))
  838. (for-each (lambda (slot)
  839. (if (slot-bound? self slot)
  840. (slot-set! clone slot
  841. (let ((value (slot-ref self slot)))
  842. (if (instance? value)
  843. (deep-clone value)
  844. value)))))
  845. slots)
  846. clone))
  847. ;;;
  848. ;;; {Class redefinition utilities}
  849. ;;;
  850. ;;; (class-redefinition OLD NEW)
  851. ;;;
  852. ;;; Has correct the following conditions:
  853. ;;; Methods
  854. ;;;
  855. ;;; 1. New accessor specializers refer to new header
  856. ;;;
  857. ;;; Classes
  858. ;;;
  859. ;;; 1. New class cpl refers to the new class header
  860. ;;; 2. Old class header exists on old super classes direct-subclass lists
  861. ;;; 3. New class header exists on new super classes direct-subclass lists
  862. (define-method (class-redefinition (old <class>) (new <class>))
  863. ;; Work on direct methods:
  864. ;; 1. Remove accessor methods from the old class
  865. ;; 2. Patch the occurences of new in the specializers by old
  866. ;; 3. Displace the methods from old to new
  867. (remove-class-accessors! old) ;; -1-
  868. (let ((methods (class-direct-methods new)))
  869. (for-each (lambda (m)
  870. (update-direct-method! m new old)) ;; -2-
  871. methods)
  872. (slot-set! new
  873. 'direct-methods
  874. (append methods (class-direct-methods old))))
  875. ;; Substitute old for new in new cpl
  876. (set-car! (slot-ref new 'cpl) old)
  877. ;; Remove the old class from the direct-subclasses list of its super classes
  878. (for-each (lambda (c) (slot-set! c 'direct-subclasses
  879. (delv! old (class-direct-subclasses c))))
  880. (class-direct-supers old))
  881. ;; Replace the new class with the old in the direct-subclasses of the supers
  882. (for-each (lambda (c)
  883. (slot-set! c 'direct-subclasses
  884. (cons old (delv! new (class-direct-subclasses c)))))
  885. (class-direct-supers new))
  886. ;; Swap object headers
  887. (%modify-class old new)
  888. ;; Now old is NEW!
  889. ;; Redefine all the subclasses of old to take into account modification
  890. (for-each
  891. (lambda (c)
  892. (update-direct-subclass! c new old))
  893. (class-direct-subclasses new))
  894. ;; Invalidate class so that subsequent instances slot accesses invoke
  895. ;; change-object-class
  896. (slot-set! new 'redefined old)
  897. (%invalidate-class new) ;must come after slot-set!
  898. old)
  899. ;;;
  900. ;;; remove-class-accessors!
  901. ;;;
  902. (define-method (remove-class-accessors! (c <class>))
  903. (for-each (lambda (m)
  904. (if (is-a? m <accessor-method>)
  905. (let ((gf (slot-ref m 'generic-function)))
  906. ;; remove the method from its GF
  907. (slot-set! gf 'methods
  908. (delq1! m (slot-ref gf 'methods)))
  909. (%invalidate-method-cache! gf)
  910. ;; remove the method from its specializers
  911. (remove-method-in-classes! m))))
  912. (class-direct-methods c)))
  913. ;;;
  914. ;;; update-direct-method!
  915. ;;;
  916. (define-method (update-direct-method! (m <method>)
  917. (old <class>)
  918. (new <class>))
  919. (let loop ((l (method-specializers m)))
  920. ;; Note: the <top> in dotted list is never used.
  921. ;; So we can work as if we had only proper lists.
  922. (if (pair? l)
  923. (begin
  924. (if (eqv? (car l) old)
  925. (set-car! l new))
  926. (loop (cdr l))))))
  927. ;;;
  928. ;;; update-direct-subclass!
  929. ;;;
  930. (define-method (update-direct-subclass! (c <class>)
  931. (old <class>)
  932. (new <class>))
  933. (class-redefinition c
  934. (make-class (class-direct-supers c)
  935. (class-direct-slots c)
  936. #:name (class-name c)
  937. #:metaclass (class-of c))))
  938. ;;;
  939. ;;; {Utilities for INITIALIZE methods}
  940. ;;;
  941. ;;; compute-slot-accessors
  942. ;;;
  943. (define (compute-slot-accessors class slots)
  944. (for-each
  945. (lambda (s g-n-s)
  946. (let ((getter-function (slot-definition-getter s))
  947. (setter-function (slot-definition-setter s))
  948. (accessor (slot-definition-accessor s)))
  949. (if getter-function
  950. (add-method! getter-function
  951. (compute-getter-method class g-n-s)))
  952. (if setter-function
  953. (add-method! setter-function
  954. (compute-setter-method class g-n-s)))
  955. (if accessor
  956. (begin
  957. (add-method! accessor
  958. (compute-getter-method class g-n-s))
  959. (add-method! (setter accessor)
  960. (compute-setter-method class g-n-s))))))
  961. slots (slot-ref class 'getters-n-setters)))
  962. (define-method (compute-getter-method (class <class>) slotdef)
  963. (let ((init-thunk (cadr slotdef))
  964. (g-n-s (cddr slotdef)))
  965. (make <accessor-method>
  966. #:specializers (list class)
  967. #:procedure (cond ((pair? g-n-s)
  968. (make-generic-bound-check-getter (car g-n-s)))
  969. (init-thunk
  970. (standard-get g-n-s))
  971. (else
  972. (bound-check-get g-n-s)))
  973. #:slot-definition slotdef)))
  974. (define-method (compute-setter-method (class <class>) slotdef)
  975. (let ((g-n-s (cddr slotdef)))
  976. (make <accessor-method>
  977. #:specializers (list class <top>)
  978. #:procedure (if (pair? g-n-s)
  979. (cadr g-n-s)
  980. (standard-set g-n-s))
  981. #:slot-definition slotdef)))
  982. (define (make-generic-bound-check-getter proc)
  983. (lambda (o) (assert-bound (proc o) o)))
  984. ;; the idea is to compile the index into the procedure, for fastest
  985. ;; lookup. Also, @slot-ref and @slot-set! have their own bytecodes.
  986. (eval-when (eval load compile)
  987. (define num-standard-pre-cache 20))
  988. (define-macro (define-standard-accessor-method form . body)
  989. (let ((name (caar form))
  990. (n-var (cadar form))
  991. (args (cdr form)))
  992. (define (make-one x)
  993. (define (body-trans form)
  994. (cond ((not (pair? form)) form)
  995. ((eq? (car form) '@slot-ref)
  996. `(,(car form) ,(cadr form) ,x))
  997. ((eq? (car form) '@slot-set!)
  998. `(,(car form) ,(cadr form) ,x ,(cadddr form)))
  999. (else
  1000. (map body-trans form))))
  1001. `(lambda ,args ,@(map body-trans body)))
  1002. `(define ,name
  1003. (let ((cache (vector ,@(map make-one (iota num-standard-pre-cache)))))
  1004. (lambda (n)
  1005. (if (< n ,num-standard-pre-cache)
  1006. (vector-ref cache n)
  1007. ((lambda (,n-var) (lambda ,args ,@body)) n)))))))
  1008. (define-standard-accessor-method ((bound-check-get n) o)
  1009. (let ((x (@slot-ref o n)))
  1010. (if (unbound? x)
  1011. (slot-unbound o)
  1012. x)))
  1013. (define-standard-accessor-method ((standard-get n) o)
  1014. (@slot-ref o n))
  1015. (define-standard-accessor-method ((standard-set n) o v)
  1016. (@slot-set! o n v))
  1017. ;;; compute-getters-n-setters
  1018. ;;;
  1019. (define (compute-getters-n-setters class slots)
  1020. (define (compute-slot-init-function name s)
  1021. (or (let ((thunk (slot-definition-init-thunk s)))
  1022. (and thunk
  1023. (if (thunk? thunk)
  1024. thunk
  1025. (goops-error "Bad init-thunk for slot `~S' in ~S: ~S"
  1026. name class thunk))))
  1027. (let ((init (slot-definition-init-value s)))
  1028. (and (not (unbound? init))
  1029. (lambda () init)))))
  1030. (define (verify-accessors slot l)
  1031. (cond ((integer? l))
  1032. ((not (and (list? l) (= (length l) 2)))
  1033. (goops-error "Bad getter and setter for slot `~S' in ~S: ~S"
  1034. slot class l))
  1035. (else
  1036. (let ((get (car l))
  1037. (set (cadr l)))
  1038. (if (not (procedure? get))
  1039. (goops-error "Bad getter closure for slot `~S' in ~S: ~S"
  1040. slot class get))
  1041. (if (not (procedure? set))
  1042. (goops-error "Bad setter closure for slot `~S' in ~S: ~S"
  1043. slot class set))))))
  1044. (map (lambda (s)
  1045. ;; The strange treatment of nfields is due to backward compatibility.
  1046. (let* ((index (slot-ref class 'nfields))
  1047. (g-n-s (compute-get-n-set class s))
  1048. (size (- (slot-ref class 'nfields) index))
  1049. (name (slot-definition-name s)))
  1050. ;; NOTE: The following is interdependent with C macros
  1051. ;; defined above goops.c:scm_sys_prep_layout_x.
  1052. ;;
  1053. ;; For simple instance slots, we have the simplest form
  1054. ;; '(name init-function . index)
  1055. ;; For other slots we have
  1056. ;; '(name init-function getter setter . alloc)
  1057. ;; where alloc is:
  1058. ;; '(index size) for instance allocated slots
  1059. ;; '() for other slots
  1060. (verify-accessors name g-n-s)
  1061. (cons name
  1062. (cons (compute-slot-init-function name s)
  1063. (if (or (integer? g-n-s)
  1064. (zero? size))
  1065. g-n-s
  1066. (append g-n-s (list index size)))))))
  1067. slots))
  1068. ;;; compute-cpl
  1069. ;;;
  1070. ;;; Correct behaviour:
  1071. ;;;
  1072. ;;; (define-class food ())
  1073. ;;; (define-class fruit (food))
  1074. ;;; (define-class spice (food))
  1075. ;;; (define-class apple (fruit))
  1076. ;;; (define-class cinnamon (spice))
  1077. ;;; (define-class pie (apple cinnamon))
  1078. ;;; => cpl (pie) = pie apple fruit cinnamon spice food object top
  1079. ;;;
  1080. ;;; (define-class d ())
  1081. ;;; (define-class e ())
  1082. ;;; (define-class f ())
  1083. ;;; (define-class b (d e))
  1084. ;;; (define-class c (e f))
  1085. ;;; (define-class a (b c))
  1086. ;;; => cpl (a) = a b d c e f object top
  1087. ;;;
  1088. (define-method (compute-cpl (class <class>))
  1089. (compute-std-cpl class class-direct-supers))
  1090. ;; Support
  1091. (define (only-non-null lst)
  1092. (filter (lambda (l) (not (null? l))) lst))
  1093. (define (compute-std-cpl c get-direct-supers)
  1094. (let ((c-direct-supers (get-direct-supers c)))
  1095. (merge-lists (list c)
  1096. (only-non-null (append (map class-precedence-list
  1097. c-direct-supers)
  1098. (list c-direct-supers))))))
  1099. (define (merge-lists reversed-partial-result inputs)
  1100. (cond
  1101. ((every null? inputs)
  1102. (reverse! reversed-partial-result))
  1103. (else
  1104. (let* ((candidate (lambda (c)
  1105. (and (not (any (lambda (l)
  1106. (memq c (cdr l)))
  1107. inputs))
  1108. c)))
  1109. (candidate-car (lambda (l)
  1110. (and (not (null? l))
  1111. (candidate (car l)))))
  1112. (next (any candidate-car inputs)))
  1113. (if (not next)
  1114. (goops-error "merge-lists: Inconsistent precedence graph"))
  1115. (let ((remove-next (lambda (l)
  1116. (if (eq? (car l) next)
  1117. (cdr l)
  1118. l))))
  1119. (merge-lists (cons next reversed-partial-result)
  1120. (only-non-null (map remove-next inputs))))))))
  1121. ;; Modified from TinyClos:
  1122. ;;
  1123. ;; A simple topological sort.
  1124. ;;
  1125. ;; It's in this file so that both TinyClos and Objects can use it.
  1126. ;;
  1127. ;; This is a fairly modified version of code I originally got from Anurag
  1128. ;; Mendhekar <anurag@moose.cs.indiana.edu>.
  1129. ;;
  1130. (define (compute-clos-cpl c get-direct-supers)
  1131. (top-sort ((build-transitive-closure get-direct-supers) c)
  1132. ((build-constraints get-direct-supers) c)
  1133. (std-tie-breaker get-direct-supers)))
  1134. (define (top-sort elements constraints tie-breaker)
  1135. (let loop ((elements elements)
  1136. (constraints constraints)
  1137. (result '()))
  1138. (if (null? elements)
  1139. result
  1140. (let ((can-go-in-now
  1141. (filter
  1142. (lambda (x)
  1143. (every (lambda (constraint)
  1144. (or (not (eq? (cadr constraint) x))
  1145. (memq (car constraint) result)))
  1146. constraints))
  1147. elements)))
  1148. (if (null? can-go-in-now)
  1149. (goops-error "top-sort: Invalid constraints")
  1150. (let ((choice (if (null? (cdr can-go-in-now))
  1151. (car can-go-in-now)
  1152. (tie-breaker result
  1153. can-go-in-now))))
  1154. (loop
  1155. (filter (lambda (x) (not (eq? x choice)))
  1156. elements)
  1157. constraints
  1158. (append result (list choice)))))))))
  1159. (define (std-tie-breaker get-supers)
  1160. (lambda (partial-cpl min-elts)
  1161. (let loop ((pcpl (reverse partial-cpl)))
  1162. (let ((current-elt (car pcpl)))
  1163. (let ((ds-of-ce (get-supers current-elt)))
  1164. (let ((common (filter (lambda (x)
  1165. (memq x ds-of-ce))
  1166. min-elts)))
  1167. (if (null? common)
  1168. (if (null? (cdr pcpl))
  1169. (goops-error "std-tie-breaker: Nothing valid")
  1170. (loop (cdr pcpl)))
  1171. (car common))))))))
  1172. (define (build-transitive-closure get-follow-ons)
  1173. (lambda (x)
  1174. (let track ((result '())
  1175. (pending (list x)))
  1176. (if (null? pending)
  1177. result
  1178. (let ((next (car pending)))
  1179. (if (memq next result)
  1180. (track result (cdr pending))
  1181. (track (cons next result)
  1182. (append (get-follow-ons next)
  1183. (cdr pending)))))))))
  1184. (define (build-constraints get-follow-ons)
  1185. (lambda (x)
  1186. (let loop ((elements ((build-transitive-closure get-follow-ons) x))
  1187. (this-one '())
  1188. (result '()))
  1189. (if (or (null? this-one) (null? (cdr this-one)))
  1190. (if (null? elements)
  1191. result
  1192. (loop (cdr elements)
  1193. (cons (car elements)
  1194. (get-follow-ons (car elements)))
  1195. result))
  1196. (loop elements
  1197. (cdr this-one)
  1198. (cons (list (car this-one) (cadr this-one))
  1199. result))))))
  1200. ;;; compute-get-n-set
  1201. ;;;
  1202. (define-method (compute-get-n-set (class <class>) s)
  1203. (case (slot-definition-allocation s)
  1204. ((#:instance) ;; Instance slot
  1205. ;; get-n-set is just its offset
  1206. (let ((already-allocated (slot-ref class 'nfields)))
  1207. (slot-set! class 'nfields (+ already-allocated 1))
  1208. already-allocated))
  1209. ((#:class) ;; Class slot
  1210. ;; Class-slots accessors are implemented as 2 closures around
  1211. ;; a Scheme variable. As instance slots, class slots must be
  1212. ;; unbound at init time.
  1213. (let ((name (slot-definition-name s)))
  1214. (if (memq name (map slot-definition-name (class-direct-slots class)))
  1215. ;; This slot is direct; create a new shared variable
  1216. (make-closure-variable class)
  1217. ;; Slot is inherited. Find its definition in superclass
  1218. (let loop ((l (cdr (class-precedence-list class))))
  1219. (let ((r (assoc name (slot-ref (car l) 'getters-n-setters))))
  1220. (if r
  1221. (cddr r)
  1222. (loop (cdr l))))))))
  1223. ((#:each-subclass) ;; slot shared by instances of direct subclass.
  1224. ;; (Thomas Buerger, April 1998)
  1225. (make-closure-variable class))
  1226. ((#:virtual) ;; No allocation
  1227. ;; slot-ref and slot-set! function must be given by the user
  1228. (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
  1229. (set (get-keyword #:slot-set! (slot-definition-options s) #f)))
  1230. (if (not (and get set))
  1231. (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S"
  1232. s))
  1233. (list get set)))
  1234. (else (next-method))))
  1235. (define (make-closure-variable class)
  1236. (let ((shared-variable (make-unbound)))
  1237. (list (lambda (o) shared-variable)
  1238. (lambda (o v) (set! shared-variable v)))))
  1239. (define-method (compute-get-n-set (o <object>) s)
  1240. (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
  1241. (define-method (compute-slots (class <class>))
  1242. (%compute-slots class))
  1243. ;;;
  1244. ;;; {Initialize}
  1245. ;;;
  1246. (define-method (initialize (object <object>) initargs)
  1247. (%initialize-object object initargs))
  1248. (define-method (initialize (class <class>) initargs)
  1249. (next-method)
  1250. (let ((dslots (get-keyword #:slots initargs '()))
  1251. (supers (get-keyword #:dsupers initargs '())))
  1252. (slot-set! class 'name (get-keyword #:name initargs '???))
  1253. (slot-set! class 'direct-supers supers)
  1254. (slot-set! class 'direct-slots dslots)
  1255. (slot-set! class 'direct-subclasses '())
  1256. (slot-set! class 'direct-methods '())
  1257. (slot-set! class 'cpl (compute-cpl class))
  1258. (slot-set! class 'redefined #f)
  1259. (let ((slots (compute-slots class)))
  1260. (slot-set! class 'slots slots)
  1261. (slot-set! class 'nfields 0)
  1262. (slot-set! class 'getters-n-setters (compute-getters-n-setters class
  1263. slots))
  1264. ;; Build getters - setters - accessors
  1265. (compute-slot-accessors class slots))
  1266. ;; Update the "direct-subclasses" of each inherited classes
  1267. (for-each (lambda (x)
  1268. (slot-set! x
  1269. 'direct-subclasses
  1270. (cons class (slot-ref x 'direct-subclasses))))
  1271. supers)
  1272. ;; Support for the underlying structs:
  1273. ;; Set the layout slot
  1274. (%prep-layout! class)
  1275. ;; Inherit class flags (invisible on scheme level) from supers
  1276. (%inherit-magic! class supers)))
  1277. (define (initialize-object-procedure object initargs)
  1278. (let ((proc (get-keyword #:procedure initargs #f)))
  1279. (cond ((not proc))
  1280. ((pair? proc)
  1281. (apply slot-set! object 'procedure proc))
  1282. (else
  1283. (slot-set! object 'procedure proc)))))
  1284. (define-method (initialize (applicable-struct <applicable-struct>) initargs)
  1285. (next-method)
  1286. (initialize-object-procedure applicable-struct initargs))
  1287. (define-method (initialize (generic <generic>) initargs)
  1288. (let ((previous-definition (get-keyword #:default initargs #f))
  1289. (name (get-keyword #:name initargs #f)))
  1290. (next-method)
  1291. (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
  1292. (list (method args
  1293. (apply previous-definition args)))
  1294. '()))
  1295. (if name
  1296. (set-procedure-property! generic 'name name))
  1297. ))
  1298. (define-method (initialize (gws <generic-with-setter>) initargs)
  1299. (next-method)
  1300. (%set-object-setter! gws (get-keyword #:setter initargs #f)))
  1301. (define-method (initialize (eg <extended-generic>) initargs)
  1302. (next-method)
  1303. (slot-set! eg 'extends (get-keyword #:extends initargs '())))
  1304. (define dummy-procedure (lambda args *unspecified*))
  1305. (define-method (initialize (method <method>) initargs)
  1306. (next-method)
  1307. (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
  1308. (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
  1309. (slot-set! method 'procedure
  1310. (get-keyword #:procedure initargs #f))
  1311. (slot-set! method 'formals (get-keyword #:formals initargs '()))
  1312. (slot-set! method 'body (get-keyword #:body initargs '()))
  1313. (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f)))
  1314. ;;;
  1315. ;;; {Change-class}
  1316. ;;;
  1317. (define (change-object-class old-instance old-class new-class)
  1318. (let ((new-instance (allocate-instance new-class '())))
  1319. ;; Initialize the slots of the new instance
  1320. (for-each (lambda (slot)
  1321. (if (and (slot-exists-using-class? old-class old-instance slot)
  1322. (eq? (slot-definition-allocation
  1323. (class-slot-definition old-class slot))
  1324. #:instance)
  1325. (slot-bound-using-class? old-class old-instance slot))
  1326. ;; Slot was present and allocated in old instance; copy it
  1327. (slot-set-using-class!
  1328. new-class
  1329. new-instance
  1330. slot
  1331. (slot-ref-using-class old-class old-instance slot))
  1332. ;; slot was absent; initialize it with its default value
  1333. (let ((init (slot-init-function new-class slot)))
  1334. (if init
  1335. (slot-set-using-class!
  1336. new-class
  1337. new-instance
  1338. slot
  1339. (apply init '()))))))
  1340. (map slot-definition-name (class-slots new-class)))
  1341. ;; Exchange old and new instance in place to keep pointers valid
  1342. (%modify-instance old-instance new-instance)
  1343. ;; Allow class specific updates of instances (which now are swapped)
  1344. (update-instance-for-different-class new-instance old-instance)
  1345. old-instance))
  1346. (define-method (update-instance-for-different-class (old-instance <object>)
  1347. (new-instance
  1348. <object>))
  1349. ;;not really important what we do, we just need a default method
  1350. new-instance)
  1351. (define-method (change-class (old-instance <object>) (new-class <class>))
  1352. (change-object-class old-instance (class-of old-instance) new-class))
  1353. ;;;
  1354. ;;; {make}
  1355. ;;;
  1356. ;;; A new definition which overwrites the previous one which was built-in
  1357. ;;;
  1358. (define-method (allocate-instance (class <class>) initargs)
  1359. (%allocate-instance class initargs))
  1360. (define-method (make-instance (class <class>) . initargs)
  1361. (let ((instance (allocate-instance class initargs)))
  1362. (initialize instance initargs)
  1363. instance))
  1364. (define make make-instance)
  1365. ;;;
  1366. ;;; {apply-generic}
  1367. ;;;
  1368. ;;; Protocol for calling standard generic functions. This protocol is
  1369. ;;; not used for real <generic> functions (in this case we use a
  1370. ;;; completely C hard-coded protocol). Apply-generic is used by
  1371. ;;; goops for calls to subclasses of <generic> and <generic-with-setter>.
  1372. ;;; The code below is similar to the first MOP described in AMOP. In
  1373. ;;; particular, it doesn't used the currified approach to gf
  1374. ;;; call. There are 2 reasons for that:
  1375. ;;; - the protocol below is exposed to mimic completely the one written in C
  1376. ;;; - the currified protocol would be imho inefficient in C.
  1377. ;;;
  1378. (define-method (apply-generic (gf <generic>) args)
  1379. (if (null? (slot-ref gf 'methods))
  1380. (no-method gf args))
  1381. (let ((methods (compute-applicable-methods gf args)))
  1382. (if methods
  1383. (apply-methods gf (sort-applicable-methods gf methods args) args)
  1384. (no-applicable-method gf args))))
  1385. ;; compute-applicable-methods is bound to %compute-applicable-methods.
  1386. ;; *fixme* use let
  1387. (define %%compute-applicable-methods
  1388. (make <generic> #:name 'compute-applicable-methods))
  1389. (define-method (%%compute-applicable-methods (gf <generic>) args)
  1390. (%compute-applicable-methods gf args))
  1391. (set! compute-applicable-methods %%compute-applicable-methods)
  1392. (define-method (sort-applicable-methods (gf <generic>) methods args)
  1393. (let ((targs (map class-of args)))
  1394. (sort methods (lambda (m1 m2) (method-more-specific? m1 m2 targs)))))
  1395. (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
  1396. (%method-more-specific? m1 m2 targs))
  1397. (define-method (apply-method (gf <generic>) methods build-next args)
  1398. (apply (method-procedure (car methods))
  1399. (build-next (cdr methods) args)
  1400. args))
  1401. (define-method (apply-methods (gf <generic>) (l <list>) args)
  1402. (letrec ((next (lambda (procs args)
  1403. (lambda new-args
  1404. (let ((a (if (null? new-args) args new-args)))
  1405. (if (null? procs)
  1406. (no-next-method gf a)
  1407. (apply-method gf procs next a)))))))
  1408. (apply-method gf l next args)))
  1409. ;; We don't want the following procedure to turn up in backtraces:
  1410. (for-each (lambda (proc)
  1411. (set-procedure-property! proc 'system-procedure #t))
  1412. (list slot-unbound
  1413. slot-missing
  1414. no-next-method
  1415. no-applicable-method
  1416. no-method
  1417. ))
  1418. ;;;
  1419. ;;; {<composite-metaclass> and <active-metaclass>}
  1420. ;;;
  1421. ;(autoload "active-slot" <active-metaclass>)
  1422. ;(autoload "composite-slot" <composite-metaclass>)
  1423. ;(export <composite-metaclass> <active-metaclass>)
  1424. ;;;
  1425. ;;; {Tools}
  1426. ;;;
  1427. ;; list2set
  1428. ;;
  1429. ;; duplicate the standard list->set function but using eq instead of
  1430. ;; eqv which really sucks a lot, uselessly here
  1431. ;;
  1432. (define (list2set l)
  1433. (let loop ((l l)
  1434. (res '()))
  1435. (cond
  1436. ((null? l) res)
  1437. ((memq (car l) res) (loop (cdr l) res))
  1438. (else (loop (cdr l) (cons (car l) res))))))
  1439. (define (class-subclasses c)
  1440. (letrec ((allsubs (lambda (c)
  1441. (cons c (mapappend allsubs
  1442. (class-direct-subclasses c))))))
  1443. (list2set (cdr (allsubs c)))))
  1444. (define (class-methods c)
  1445. (list2set (mapappend class-direct-methods
  1446. (cons c (class-subclasses c)))))
  1447. ;;;
  1448. ;;; {Final initialization}
  1449. ;;;
  1450. ;; Tell C code that the main bulk of Goops has been loaded
  1451. (%goops-loaded)