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