3 ;;;; Copyright (C) 1998,1999,2000,2001,2002, 2003, 2006, 2009, 2010, 2011, 2014, 2015 Free Software Foundation, Inc.
4 ;;;; Copyright (C) 1993-1998 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
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.
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.
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
23 ;;;; This file was based upon stklos.stk from the STk distribution
24 ;;;; version 4.0.1 by Erick Gallesio <eg@unice.fr>.
27 (define-module (oop goops)
28 #:use-module (srfi srfi-1)
29 #:use-module (ice-9 match)
30 #:use-module (oop goops util)
31 #:export-syntax (define-class class standard-define-class
32 define-generic define-accessor define-method
33 define-extended-generic define-extended-generics
35 #:export (is-a? class-of
36 ensure-metaclass ensure-metaclass-with-supers
38 make-generic ensure-generic
40 make-accessor ensure-accessor
42 class-slot-ref class-slot-set! slot-unbound slot-missing
43 slot-definition-name slot-definition-options
44 slot-definition-allocation
45 slot-definition-getter slot-definition-setter
46 slot-definition-accessor
47 slot-definition-init-value slot-definition-init-form
48 slot-definition-init-thunk slot-definition-init-keyword
49 slot-init-function class-slot-definition
51 compute-cpl compute-std-cpl compute-get-n-set compute-slots
52 compute-getter-method compute-setter-method
53 allocate-instance initialize make-instance make
54 no-next-method no-applicable-method no-method
55 change-class update-instance-for-different-class
56 shallow-clone deep-clone
58 apply-generic apply-method apply-methods
59 compute-applicable-methods %compute-applicable-methods
60 method-more-specific? sort-applicable-methods
61 class-subclasses class-methods
64 ;;; *fixme* Should go into goops.c
65 instance? slot-ref-using-class
66 slot-set-using-class! slot-bound-using-class?
67 slot-exists-using-class? slot-ref slot-set! slot-bound?
68 class-name class-direct-supers class-direct-subclasses
69 class-direct-methods class-direct-slots class-precedence-list
72 generic-function-methods method-generic-function
73 method-specializers method-formals
74 primitive-generic-generic enable-primitive-generic!
75 method-procedure accessor-method-slot-definition
76 slot-exists? make find-method get-keyword))
78 (define *goops-module* (current-module))
80 ;; First initialize the builtin part of GOOPS
81 (eval-when (expand load eval)
82 (%init-goops-builtins))
84 (eval-when (expand load eval)
85 (use-modules ((language tree-il primitives) :select (add-interesting-primitive!)))
86 (add-interesting-primitive! 'class-of))
88 ;; Then load the rest of GOOPS
89 (use-modules (oop goops dispatch))
92 ;;; Compiling next methods into method bodies
95 ;;; So, for the reader: there basic idea is that, given that the
96 ;;; semantics of `next-method' depend on the concrete types being
97 ;;; dispatched, why not compile a specific procedure to handle each type
98 ;;; combination that we see at runtime.
100 ;;; In theory we can do much better than a bytecode compilation, because
101 ;;; we know the *exact* types of the arguments. It's ideal for native
102 ;;; compilation. A task for the future.
104 ;;; I think this whole generic application mess would benefit from a
107 (define (compute-cmethod methods types)
110 (let ((make-procedure (slot-ref method 'make-procedure)))
115 (no-next-method (method-generic-function method) args))
116 (compute-cmethod methods types)))
117 (method-procedure method))))))
120 (eval-when (expand load eval)
121 (define min-fixnum (- (expt 2 29)))
122 (define max-fixnum (- (expt 2 29) 1)))
127 (define (goops-error format-string . args)
128 (scm-error 'goops-error #f format-string args '()))
133 (define (is-a? obj class)
134 (and (memq class (class-precedence-list (class-of obj))) #t))
141 (define ensure-metaclass-with-supers
142 (let ((table-of-metas '()))
143 (lambda (meta-supers)
144 (let ((entry (assoc meta-supers table-of-metas)))
146 ;; Found a previously created metaclass
148 ;; Create a new meta-class which inherit from "meta-supers"
149 (let ((new (make <class> #:dsupers meta-supers
151 #:name (gensym "metaclass"))))
152 (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
155 (define (ensure-metaclass supers)
158 (let* ((all-metas (map (lambda (x) (class-of x)) supers))
159 (all-cpls (append-map (lambda (m)
160 (cdr (class-precedence-list m)))
163 ;; Find the most specific metaclasses. The new metaclass will be
164 ;; a subclass of these.
167 (if (and (not (member meta all-cpls))
168 (not (member meta needed-metas)))
169 (set! needed-metas (append needed-metas (list meta)))))
171 ;; Now return a subclass of the metaclasses we found.
172 (if (null? (cdr needed-metas))
173 (car needed-metas) ; If there's only one, just use it.
174 (ensure-metaclass-with-supers needed-metas)))))
180 ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
182 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
183 ;;; OPTION ::= KEYWORD VALUE
186 (define (kw-do-map mapper f kwargs)
190 ((or (null? (cdr l)) (not (keyword? (car l))))
191 (goops-error "malformed keyword arguments: ~a" kwargs))
192 (else (cons (car l) (keywords (cddr l))))))
194 (if (null? l) '() (cons (cadr l) (args (cddr l)))))
195 ;; let* to check keywords first
196 (let* ((k (keywords kwargs))
200 (define (make-class supers slots . options)
201 (let* ((name (get-keyword #:name options (make-unbound)))
202 (supers (if (not (or-map (lambda (class)
204 (class-precedence-list class)))
206 (append supers (list <object>))
208 (metaclass (or (get-keyword #:metaclass options #f)
209 (ensure-metaclass supers))))
211 ;; Verify that all direct slots are different and that we don't inherit
212 ;; several time from the same class
213 (let ((tmp1 (find-duplicate supers))
214 (tmp2 (find-duplicate (map slot-definition-name slots))))
216 (goops-error "make-class: super class ~S is duplicate in class ~S"
219 (goops-error "make-class: slot ~S is duplicate in class ~S"
222 ;; Everything seems correct, build the class
223 (apply make metaclass
229 ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
231 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
232 ;;; OPTION ::= KEYWORD VALUE
234 (define-macro (class supers . slots)
235 (define (make-slot-definition-forms slots)
241 ,@(kw-do-map append-map
246 #:init-thunk (lambda () ,arg)))
247 (else (list kw arg))))
252 (if (not (list? supers))
253 (goops-error "malformed superclass list: ~S" supers))
254 (let ((slots (take-while (lambda (x) (not (keyword? x))) slots))
255 (options (or (find-tail keyword? slots) '())))
257 ;; evaluate super class variables
259 ;; evaluate slot definitions, except the slot name!
260 (list ,@(make-slot-definition-forms slots))
261 ;; evaluate class options
264 (define-syntax define-class-pre-definition
267 ((_ (k arg rest ...) out ...)
268 (keyword? (syntax->datum #'k))
269 (case (syntax->datum #'k)
271 #'(define-class-pre-definition (rest ...)
273 (if (or (not (defined? 'arg))
274 (not (is-a? arg <generic>)))
277 (ensure-generic (if (defined? 'arg) arg #f) 'arg)))))
279 #'(define-class-pre-definition (rest ...)
281 (if (or (not (defined? 'arg))
282 (not (is-a? arg <accessor>)))
285 (ensure-accessor (if (defined? 'arg) arg #f) 'arg)))))
287 #'(define-class-pre-definition (rest ...) out ...))))
289 #'(begin out ...)))))
291 ;; Some slot options require extra definitions to be made. In
292 ;; particular, we want to make sure that the generic function objects
293 ;; which represent accessors exist before `make-class' tries to add
295 (define-syntax define-class-pre-definitions
300 ((_ (slot rest ...) out ...)
301 (keyword? (syntax->datum #'slot))
303 ((_ (slot rest ...) out ...)
305 #'(define-class-pre-definitions (rest ...)
307 ((_ ((slotname slotopt ...) rest ...) out ...)
308 #'(define-class-pre-definitions (rest ...)
309 out ... (define-class-pre-definition (slotopt ...)))))))
311 (define-syntax-rule (define-class name supers slot ...)
313 (define-class-pre-definitions (slot ...))
314 (if (and (defined? 'name)
316 (memq <object> (class-precedence-list name)))
317 (class-redefinition name
318 (class supers slot ... #:name 'name))
319 (toplevel-define! 'name (class supers slot ... #:name 'name)))))
321 (define-syntax-rule (standard-define-class arg ...)
322 (define-class arg ...))
325 ;;; {Generic functions and accessors}
328 ;; Apparently the desired semantics are that we extend previous
329 ;; procedural definitions, but that if `name' was already a generic, we
330 ;; overwrite its definition.
331 (define-macro (define-generic name)
332 (if (not (symbol? name))
333 (goops-error "bad generic function name: ~S" name))
335 (if (and (defined? ',name) (is-a? ,name <generic>))
336 (make <generic> #:name ',name)
337 (ensure-generic (if (defined? ',name) ,name #f) ',name))))
339 (define-macro (define-extended-generic name val)
340 (if (not (symbol? name))
341 (goops-error "bad generic function name: ~S" name))
342 `(define ,name (make-extended-generic ,val ',name)))
344 (define-macro (define-extended-generics names . args)
345 (let ((prefixes (get-keyword #:prefix args #f)))
348 ,@(map (lambda (name)
349 `(define-extended-generic ,name
350 (list ,@(map (lambda (prefix)
351 (symbol-append prefix name))
354 (goops-error "no prefixes supplied"))))
356 (define* (make-generic #:optional name)
357 (make <generic> #:name name))
359 (define* (make-extended-generic gfs #:optional name)
360 (let* ((gfs (if (list? gfs) gfs (list gfs)))
361 (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs)))
363 (let* ((sname (and name (make-setter-name name)))
365 (append-map (lambda (gf)
366 (if (is-a? gf <generic-with-setter>)
367 (list (ensure-generic (setter gf)
371 (es (make <extended-generic-with-setter>
374 #:setter (make <extended-generic>
376 #:extends setters))))
377 (extended-by! setters (setter es))
379 (make <extended-generic>
382 (extended-by! gfs ans)
385 (define (extended-by! gfs eg)
386 (for-each (lambda (gf)
387 (slot-set! gf 'extended-by
388 (cons eg (slot-ref gf 'extended-by))))
390 (invalidate-method-cache! eg))
392 (define (not-extended-by! gfs eg)
393 (for-each (lambda (gf)
394 (slot-set! gf 'extended-by
395 (delq! eg (slot-ref gf 'extended-by))))
397 (invalidate-method-cache! eg))
399 (define* (ensure-generic old-definition #:optional name)
400 (cond ((is-a? old-definition <generic>) old-definition)
401 ((procedure-with-setter? old-definition)
402 (make <generic-with-setter>
404 #:default (procedure old-definition)
405 #:setter (setter old-definition)))
406 ((procedure? old-definition)
407 (if (generic-capability? old-definition) old-definition
408 (make <generic> #:name name #:default old-definition)))
409 (else (make <generic> #:name name))))
411 ;; same semantics as <generic>
412 (define-syntax-rule (define-accessor name)
414 (cond ((not (defined? 'name)) (ensure-accessor #f 'name))
415 ((is-a? name <accessor>) (make <accessor> #:name 'name))
416 (else (ensure-accessor name 'name)))))
418 (define (make-setter-name name)
419 (string->symbol (string-append "setter:" (symbol->string name))))
421 (define* (make-accessor #:optional name)
424 #:setter (make <generic>
425 #:name (and name (make-setter-name name)))))
427 (define* (ensure-accessor proc #:optional name)
428 (cond ((and (is-a? proc <accessor>)
429 (is-a? (setter proc) <generic>))
431 ((is-a? proc <generic-with-setter>)
432 (upgrade-accessor proc (setter proc)))
433 ((is-a? proc <generic>)
434 (upgrade-accessor proc (make-generic name)))
435 ((procedure-with-setter? proc)
438 #:default (procedure proc)
439 #:setter (ensure-generic (setter proc) name)))
441 (ensure-accessor (if (generic-capability? proc)
442 (make <generic> #:name name #:default proc)
443 (ensure-generic proc name))
446 (make-accessor name))))
448 (define (upgrade-accessor generic setter)
449 (let ((methods (slot-ref generic 'methods))
450 (gws (make (if (is-a? generic <extended-generic>)
451 <extended-generic-with-setter>
453 #:name (generic-function-name generic)
454 #:extended-by (slot-ref generic 'extended-by)
456 (if (is-a? generic <extended-generic>)
457 (let ((gfs (slot-ref generic 'extends)))
458 (not-extended-by! gfs generic)
459 (slot-set! gws 'extends gfs)
460 (extended-by! gfs gws)))
462 (for-each (lambda (method)
463 (slot-set! method 'generic-function gws))
465 (slot-set! gws 'methods methods)
466 (invalidate-method-cache! gws)
473 (define (toplevel-define! name val)
474 (module-define! (current-module) name val))
476 (define-syntax define-method
477 (syntax-rules (setter)
478 ((_ ((setter name) . args) body ...)
480 (if (or (not (defined? 'name))
481 (not (is-a? name <accessor>)))
482 (toplevel-define! 'name
484 (if (defined? 'name) name #f) 'name)))
485 (add-method! (setter name) (method args body ...))))
486 ((_ (name . args) body ...)
488 ;; FIXME: this code is how it always was, but it's quite cracky:
489 ;; it will only define the generic function if it was undefined
490 ;; before (ok), or *was defined to #f*. The latter is crack. But
491 ;; there are bootstrap issues about fixing this -- change it to
492 ;; (is-a? name <generic>) and see.
493 (if (or (not (defined? 'name))
495 (toplevel-define! 'name (make <generic> #:name 'name)))
496 (add-method! name (method args body ...))))))
498 (define-syntax method
500 (define (parse-args args)
501 (let lp ((ls args) (formals '()) (specializers '()))
504 (and (identifier? #'f) (identifier? #'s))
507 (cons #'s specializers)))
512 (cons #'<top> specializers)))
514 (list (reverse formals)
515 (reverse (cons #''() specializers))))
518 (list (append (reverse formals) #'tail)
519 (reverse (cons #'<top> specializers)))))))
521 (define (find-free-id exp referent)
524 (or (find-free-id #'x referent)
525 (find-free-id #'y referent)))
528 (let ((id (datum->syntax #'x referent)))
529 (and (free-identifier=? #'x id) id)))
532 (define (compute-procedure formals body)
535 (with-syntax ((formals formals))
536 #'(lambda formals body0 ...)))))
538 (define (->proper args)
539 (let lp ((ls args) (out '()))
541 ((x . xs) (lp #'xs (cons #'x out)))
543 (tail (reverse (cons #'tail out))))))
545 (define (compute-make-procedure formals body next-method)
548 (with-syntax ((next-method next-method))
549 (syntax-case formals ()
551 #'(lambda (real-next-method)
553 (let ((next-method (lambda args
555 (real-next-method formal ...)
556 (apply real-next-method args)))))
559 (with-syntax (((formal ...) (->proper #'formals)))
560 #'(lambda (real-next-method)
562 (let ((next-method (lambda args
564 (apply real-next-method formal ...)
565 (apply real-next-method args)))))
568 (define (compute-procedures formals body)
569 ;; So, our use of this is broken, because it operates on the
570 ;; pre-expansion source code. It's equivalent to just searching
571 ;; for referent in the datums. Ah well.
572 (let ((id (find-free-id body 'next-method)))
574 ;; return a make-procedure
576 (compute-make-procedure formals body id))
577 (values (compute-procedure formals body)
581 ((_ args) #'(method args (if #f #f)))
582 ((_ args body0 body1 ...)
583 (with-syntax (((formals (specializer ...)) (parse-args #'args)))
586 (compute-procedures #'formals #'(body0 body1 ...)))
587 (lambda (procedure make-procedure)
588 (with-syntax ((procedure procedure)
589 (make-procedure make-procedure))
591 #:specializers (cons* specializer ...)
593 #:body '(body0 body1 ...)
594 #:make-procedure make-procedure
595 #:procedure procedure)))))))))
601 (define (add-method-in-classes! m)
602 ;; Add method in all the classes which appears in its specializers list
603 (for-each* (lambda (x)
604 (let ((dm (class-direct-methods x)))
605 (if (not (memq m dm))
606 (slot-set! x 'direct-methods (cons m dm)))))
607 (method-specializers m)))
609 (define (remove-method-in-classes! m)
610 ;; Remove method in all the classes which appears in its specializers list
611 (for-each* (lambda (x)
614 (delv! m (class-direct-methods x))))
615 (method-specializers m)))
617 (define (compute-new-list-of-methods gf new)
618 (let ((new-spec (method-specializers new))
619 (methods (slot-ref gf 'methods)))
620 (let loop ((l methods))
623 (if (equal? (method-specializers (car l)) new-spec)
625 ;; This spec. list already exists. Remove old method from dependents
626 (remove-method-in-classes! (car l))
631 (define (method-n-specializers m)
632 (length* (slot-ref m 'specializers)))
634 (define (calculate-n-specialized gf)
635 (fold (lambda (m n) (max n (method-n-specializers m)))
637 (generic-function-methods gf)))
639 (define (invalidate-method-cache! gf)
640 (%invalidate-method-cache! gf)
641 (slot-set! gf 'n-specialized (calculate-n-specialized gf))
642 (for-each (lambda (gf) (invalidate-method-cache! gf))
643 (slot-ref gf 'extended-by)))
645 (define internal-add-method!
646 (method ((gf <generic>) (m <method>))
647 (slot-set! m 'generic-function gf)
648 (slot-set! gf 'methods (compute-new-list-of-methods gf m))
649 (invalidate-method-cache! gf)
650 (add-method-in-classes! m)
653 (define-generic add-method!)
655 ((method-procedure internal-add-method!) add-method! internal-add-method!)
657 (define-method (add-method! (proc <procedure>) (m <method>))
658 (if (generic-capability? proc)
660 (enable-primitive-generic! proc)
661 (add-method! proc m))
664 (define-method (add-method! (pg <primitive-generic>) (m <method>))
665 (add-method! (primitive-generic-generic pg) m))
667 (define-method (add-method! obj (m <method>))
668 (goops-error "~S is not a valid generic function" obj))
671 ;;; {Access to meta objects}
677 (define-method (method-source (m <method>))
678 (let* ((spec (map* class-name (slot-ref m 'specializers)))
679 (src (procedure-source (slot-ref m 'procedure))))
681 (let ((args (cadr src))
684 (cons (map* list args spec)
687 (define-method (method-formals (m <method>))
688 (slot-ref m 'formals))
693 (define slot-definition-name car)
695 (define slot-definition-options cdr)
697 (define (slot-definition-allocation s)
698 (get-keyword #:allocation (cdr s) #:instance))
700 (define (slot-definition-getter s)
701 (get-keyword #:getter (cdr s) #f))
703 (define (slot-definition-setter s)
704 (get-keyword #:setter (cdr s) #f))
706 (define (slot-definition-accessor s)
707 (get-keyword #:accessor (cdr s) #f))
709 (define (slot-definition-init-value s)
710 ;; can be #f, so we can't use #f as non-value
711 (get-keyword #:init-value (cdr s) (make-unbound)))
713 (define (slot-definition-init-form s)
714 (get-keyword #:init-form (cdr s) (make-unbound)))
716 (define (slot-definition-init-thunk s)
717 (get-keyword #:init-thunk (cdr s) #f))
719 (define (slot-definition-init-keyword s)
720 (get-keyword #:init-keyword (cdr s) #f))
722 (define (class-slot-definition class slot-name)
723 (assq slot-name (class-slots class)))
725 (define (slot-init-function class slot-name)
726 (cadr (assq slot-name (slot-ref class 'getters-n-setters))))
728 (define (accessor-method-slot-definition obj)
729 "Return the slot definition of the accessor @var{obj}."
730 (slot-ref obj 'slot-definition))
734 ;;; {Standard methods used by the C runtime}
737 ;;; Methods to compare objects
740 ;; Have to do this in a strange order because equal? is used in the
741 ;; add-method! implementation; we need to make sure that when the
742 ;; primitive is extended, that the generic has a method. =
743 (define g-equal? (make-generic 'equal?))
744 ;; When this generic gets called, we will have already checked eq? and
745 ;; eqv? -- the purpose of this generic is to extend equality. So by
746 ;; default, there is no extension, thus the #f return.
747 (add-method! g-equal? (method (x y) #f))
748 (set-primitive-generic! equal? g-equal?)
751 ;;; methods to display/write an object
754 ; Code for writing objects must test that the slots they use are
755 ; bound. Otherwise a slot-unbound method will be called and will
756 ; conduct to an infinite loop.
759 (define (display-address o file)
760 (display (number->string (object-address o) 16) file))
762 (define-method (write o file)
763 (display "#<instance " file)
764 (display-address o file)
767 (define write-object (primitive-generic-generic write))
769 (define-method (write (o <object>) file)
770 (let ((class (class-of o)))
771 (if (slot-bound? class 'name)
774 (display (class-name class) file)
775 (display #\space file)
776 (display-address o file)
780 (define-method (write (class <class>) file)
781 (let ((meta (class-of class)))
782 (if (and (slot-bound? class 'name)
783 (slot-bound? meta 'name))
786 (display (class-name meta) file)
787 (display #\space file)
788 (display (class-name class) file)
789 (display #\space file)
790 (display-address class file)
794 (define-method (write (gf <generic>) file)
795 (let ((meta (class-of gf)))
796 (if (and (slot-bound? meta 'name)
797 (slot-bound? gf 'methods))
800 (display (class-name meta) file)
801 (let ((name (generic-function-name gf)))
804 (display #\space file)
805 (display name file))))
807 (display (length (generic-function-methods gf)) file)
811 (define-method (write (o <method>) file)
812 (let ((meta (class-of o)))
813 (if (and (slot-bound? meta 'name)
814 (slot-bound? o 'specializers))
817 (display (class-name meta) file)
818 (display #\space file)
819 (display (map* (lambda (spec)
820 (if (slot-bound? spec 'name)
821 (slot-ref spec 'name)
823 (method-specializers o))
825 (display #\space file)
826 (display-address o file)
830 ;; Display (do the same thing as write by default)
831 (define-method (display o file)
832 (write-object o file))
835 ;;; Handling of duplicate bindings in the module system
838 (define-method (merge-generics (module <module>)
848 (define-method (merge-generics (module <module>)
856 (and (not (eq? val1 val2))
857 (make-variable (make-extended-generic (list val2 val1) name))))
859 (define-method (merge-generics (module <module>)
866 (gf <extended-generic>))
867 (and (not (memq val2 (slot-ref gf 'extends)))
871 (cons val2 (delq! val2 (slot-ref gf 'extends))))
874 (cons gf (delq! gf (slot-ref val2 'extended-by))))
875 (invalidate-method-cache! gf)
878 (module-define! duplicate-handlers 'merge-generics merge-generics)
880 (define-method (merge-accessors (module <module>)
890 (define-method (merge-accessors (module <module>)
898 (merge-generics module name int1 val1 int2 val2 var val))
900 (module-define! duplicate-handlers 'merge-accessors merge-accessors)
906 (define (class-slot-g-n-s class slot-name)
907 (let* ((this-slot (assq slot-name (slot-ref class 'slots)))
908 (g-n-s (cddr (or (assq slot-name (slot-ref class 'getters-n-setters))
909 (slot-missing class slot-name)))))
910 (if (not (memq (slot-definition-allocation this-slot)
911 '(#:class #:each-subclass)))
912 (slot-missing class slot-name))
915 (define (class-slot-ref class slot)
916 (let ((x ((car (class-slot-g-n-s class slot)) #f)))
918 (slot-unbound class slot)
921 (define (class-slot-set! class slot value)
922 ((cadr (class-slot-g-n-s class slot)) #f value))
924 (define-method (slot-unbound (c <class>) (o <object>) s)
925 (goops-error "Slot `~S' is unbound in object ~S" s o))
927 (define-method (slot-unbound (c <class>) s)
928 (goops-error "Slot `~S' is unbound in class ~S" s c))
930 (define-method (slot-unbound (o <object>))
931 (goops-error "Unbound slot in object ~S" o))
933 (define-method (slot-missing (c <class>) (o <object>) s)
934 (goops-error "No slot with name `~S' in object ~S" s o))
936 (define-method (slot-missing (c <class>) s)
937 (goops-error "No class slot with name `~S' in class ~S" s c))
940 (define-method (slot-missing (c <class>) (o <object>) s value)
941 (slot-missing c o s))
943 ;;; Methods for the possible error we can encounter when calling a gf
945 (define-method (no-next-method (gf <generic>) args)
946 (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
948 (define-method (no-applicable-method (gf <generic>) args)
949 (goops-error "No applicable method for ~S in call ~S"
950 gf (cons (generic-function-name gf) args)))
952 (define-method (no-method (gf <generic>) args)
953 (goops-error "No method defined for ~S" gf))
956 ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
959 (define-method (shallow-clone (self <object>))
960 (let ((clone (%allocate-instance (class-of self) '()))
961 (slots (map slot-definition-name
962 (class-slots (class-of self)))))
963 (for-each (lambda (slot)
964 (if (slot-bound? self slot)
965 (slot-set! clone slot (slot-ref self slot))))
969 (define-method (deep-clone (self <object>))
970 (let ((clone (%allocate-instance (class-of self) '()))
971 (slots (map slot-definition-name
972 (class-slots (class-of self)))))
973 (for-each (lambda (slot)
974 (if (slot-bound? self slot)
975 (slot-set! clone slot
976 (let ((value (slot-ref self slot)))
977 (if (instance? value)
984 ;;; {Class redefinition utilities}
987 ;;; (class-redefinition OLD NEW)
990 ;;; Has correct the following conditions:
994 ;;; 1. New accessor specializers refer to new header
998 ;;; 1. New class cpl refers to the new class header
999 ;;; 2. Old class header exists on old super classes direct-subclass lists
1000 ;;; 3. New class header exists on new super classes direct-subclass lists
1002 (define-method (class-redefinition (old <class>) (new <class>))
1003 ;; Work on direct methods:
1004 ;; 1. Remove accessor methods from the old class
1005 ;; 2. Patch the occurences of new in the specializers by old
1006 ;; 3. Displace the methods from old to new
1007 (remove-class-accessors! old) ;; -1-
1008 (let ((methods (class-direct-methods new)))
1009 (for-each (lambda (m)
1010 (update-direct-method! m new old)) ;; -2-
1014 (append methods (class-direct-methods old))))
1016 ;; Substitute old for new in new cpl
1017 (set-car! (slot-ref new 'cpl) old)
1019 ;; Remove the old class from the direct-subclasses list of its super classes
1020 (for-each (lambda (c) (slot-set! c 'direct-subclasses
1021 (delv! old (class-direct-subclasses c))))
1022 (class-direct-supers old))
1024 ;; Replace the new class with the old in the direct-subclasses of the supers
1025 (for-each (lambda (c)
1026 (slot-set! c 'direct-subclasses
1027 (cons old (delv! new (class-direct-subclasses c)))))
1028 (class-direct-supers new))
1030 ;; Swap object headers
1031 (%modify-class old new)
1035 ;; Redefine all the subclasses of old to take into account modification
1038 (update-direct-subclass! c new old))
1039 (class-direct-subclasses new))
1041 ;; Invalidate class so that subsequent instances slot accesses invoke
1042 ;; change-object-class
1043 (slot-set! new 'redefined old)
1044 (%invalidate-class new) ;must come after slot-set!
1049 ;;; remove-class-accessors!
1052 (define-method (remove-class-accessors! (c <class>))
1053 (for-each (lambda (m)
1054 (if (is-a? m <accessor-method>)
1055 (let ((gf (slot-ref m 'generic-function)))
1056 ;; remove the method from its GF
1057 (slot-set! gf 'methods
1058 (delq1! m (slot-ref gf 'methods)))
1059 (invalidate-method-cache! gf)
1060 ;; remove the method from its specializers
1061 (remove-method-in-classes! m))))
1062 (class-direct-methods c)))
1065 ;;; update-direct-method!
1068 (define-method (update-direct-method! (m <method>)
1071 (let loop ((l (method-specializers m)))
1072 ;; Note: the <top> in dotted list is never used.
1073 ;; So we can work as if we had only proper lists.
1076 (if (eqv? (car l) old)
1081 ;;; update-direct-subclass!
1084 (define-method (update-direct-subclass! (c <class>)
1087 (class-redefinition c
1088 (make-class (class-direct-supers c)
1089 (class-direct-slots c)
1090 #:name (class-name c)
1091 #:metaclass (class-of c))))
1094 ;;; {Utilities for INITIALIZE methods}
1097 ;;; compute-slot-accessors
1099 (define (compute-slot-accessors class slots)
1102 (let ((getter-function (slot-definition-getter s))
1103 (setter-function (slot-definition-setter s))
1104 (accessor (slot-definition-accessor s)))
1106 (add-method! getter-function
1107 (compute-getter-method class g-n-s)))
1109 (add-method! setter-function
1110 (compute-setter-method class g-n-s)))
1113 (add-method! accessor
1114 (compute-getter-method class g-n-s))
1115 (add-method! (setter accessor)
1116 (compute-setter-method class g-n-s))))))
1117 slots (slot-ref class 'getters-n-setters)))
1119 (define-method (compute-getter-method (class <class>) g-n-s)
1120 (let ((init-thunk (cadr g-n-s))
1121 (g-n-s (cddr g-n-s)))
1122 (make <accessor-method>
1123 #:specializers (list class)
1124 #:procedure (cond ((pair? g-n-s)
1125 (make-generic-bound-check-getter (car g-n-s)))
1127 (standard-get g-n-s))
1129 (bound-check-get g-n-s)))
1130 #:slot-definition g-n-s)))
1132 (define-method (compute-setter-method (class <class>) g-n-s)
1133 (let ((init-thunk (cadr g-n-s))
1134 (g-n-s (cddr g-n-s)))
1135 (make <accessor-method>
1136 #:specializers (list class <top>)
1137 #:procedure (if (pair? g-n-s)
1139 (standard-set g-n-s))
1140 #:slot-definition g-n-s)))
1142 (define (make-generic-bound-check-getter proc)
1143 (lambda (o) (assert-bound (proc o) o)))
1145 ;; the idea is to compile the index into the procedure, for fastest
1148 (eval-when (expand load eval)
1149 (define num-standard-pre-cache 20))
1151 (define-macro (define-standard-accessor-method form . body)
1152 (let ((name (caar form))
1153 (n-var (cadar form))
1155 (define (make-one x)
1156 (define (body-trans form)
1157 (cond ((not (pair? form)) form)
1158 ((eq? (car form) 'struct-ref)
1159 `(,(car form) ,(cadr form) ,x))
1160 ((eq? (car form) 'struct-set!)
1161 `(,(car form) ,(cadr form) ,x ,(cadddr form)))
1163 (map body-trans form))))
1164 `(lambda ,args ,@(map body-trans body)))
1166 (let ((cache (vector ,@(map make-one (iota num-standard-pre-cache)))))
1168 (if (< n ,num-standard-pre-cache)
1169 (vector-ref cache n)
1170 ((lambda (,n-var) (lambda ,args ,@body)) n)))))))
1172 (define-standard-accessor-method ((bound-check-get n) o)
1173 (let ((x (struct-ref o n)))
1178 (define-standard-accessor-method ((standard-get n) o)
1181 (define-standard-accessor-method ((standard-set n) o v)
1182 (struct-set! o n v))
1184 ;;; compute-getters-n-setters
1186 (define (compute-getters-n-setters class slots)
1188 (define (compute-slot-init-function name s)
1189 (or (let ((thunk (slot-definition-init-thunk s)))
1193 (goops-error "Bad init-thunk for slot `~S' in ~S: ~S"
1194 name class thunk))))
1195 (let ((init (slot-definition-init-value s)))
1196 (and (not (unbound? init))
1197 (lambda () init)))))
1199 (define (verify-accessors slot l)
1200 (cond ((integer? l))
1201 ((not (and (list? l) (= (length l) 2)))
1202 (goops-error "Bad getter and setter for slot `~S' in ~S: ~S"
1207 (if (not (procedure? get))
1208 (goops-error "Bad getter closure for slot `~S' in ~S: ~S"
1210 (if (not (procedure? set))
1211 (goops-error "Bad setter closure for slot `~S' in ~S: ~S"
1212 slot class set))))))
1215 ;; The strange treatment of nfields is due to backward compatibility.
1216 (let* ((index (slot-ref class 'nfields))
1217 (g-n-s (compute-get-n-set class s))
1218 (size (- (slot-ref class 'nfields) index))
1219 (name (slot-definition-name s)))
1220 ;; NOTE: The following is interdependent with C macros
1221 ;; defined above goops.c:scm_sys_prep_layout_x.
1223 ;; For simple instance slots, we have the simplest form
1224 ;; '(name init-function . index)
1225 ;; For other slots we have
1226 ;; '(name init-function getter setter . alloc)
1228 ;; '(index size) for instance allocated slots
1229 ;; '() for other slots
1230 (verify-accessors name g-n-s)
1231 (case (slot-definition-allocation s)
1232 ((#:each-subclass #:class)
1233 (unless (and (zero? size) (pair? g-n-s))
1234 (error "Class-allocated slots should not reserve fields"))
1235 ;; Don't initialize the slot; that's handled when the slot
1236 ;; is allocated, in compute-get-n-set.
1237 (cons name (cons #f g-n-s)))
1240 (cons (compute-slot-init-function name s)
1241 (if (or (integer? g-n-s)
1244 (append g-n-s (list index size)))))))))
1249 ;;; Correct behaviour:
1251 ;;; (define-class food ())
1252 ;;; (define-class fruit (food))
1253 ;;; (define-class spice (food))
1254 ;;; (define-class apple (fruit))
1255 ;;; (define-class cinnamon (spice))
1256 ;;; (define-class pie (apple cinnamon))
1257 ;;; => cpl (pie) = pie apple fruit cinnamon spice food object top
1259 ;;; (define-class d ())
1260 ;;; (define-class e ())
1261 ;;; (define-class f ())
1262 ;;; (define-class b (d e))
1263 ;;; (define-class c (e f))
1264 ;;; (define-class a (b c))
1265 ;;; => cpl (a) = a b d c e f object top
1268 (define-method (compute-cpl (class <class>))
1269 (compute-std-cpl class class-direct-supers))
1273 (define (only-non-null lst)
1274 (filter (lambda (l) (not (null? l))) lst))
1276 (define (compute-std-cpl c get-direct-supers)
1277 (let ((c-direct-supers (get-direct-supers c)))
1278 (merge-lists (list c)
1279 (only-non-null (append (map class-precedence-list
1281 (list c-direct-supers))))))
1283 (define (merge-lists reversed-partial-result inputs)
1285 ((every null? inputs)
1286 (reverse! reversed-partial-result))
1288 (let* ((candidate (lambda (c)
1289 (and (not (any (lambda (l)
1293 (candidate-car (lambda (l)
1294 (and (not (null? l))
1295 (candidate (car l)))))
1296 (next (any candidate-car inputs)))
1298 (goops-error "merge-lists: Inconsistent precedence graph"))
1299 (let ((remove-next (lambda (l)
1300 (if (eq? (car l) next)
1303 (merge-lists (cons next reversed-partial-result)
1304 (only-non-null (map remove-next inputs))))))))
1306 ;; Modified from TinyClos:
1308 ;; A simple topological sort.
1310 ;; It's in this file so that both TinyClos and Objects can use it.
1312 ;; This is a fairly modified version of code I originally got from Anurag
1313 ;; Mendhekar <anurag@moose.cs.indiana.edu>.
1316 (define (compute-clos-cpl c get-direct-supers)
1317 (top-sort ((build-transitive-closure get-direct-supers) c)
1318 ((build-constraints get-direct-supers) c)
1319 (std-tie-breaker get-direct-supers)))
1322 (define (top-sort elements constraints tie-breaker)
1323 (let loop ((elements elements)
1324 (constraints constraints)
1326 (if (null? elements)
1328 (let ((can-go-in-now
1331 (every (lambda (constraint)
1332 (or (not (eq? (cadr constraint) x))
1333 (memq (car constraint) result)))
1336 (if (null? can-go-in-now)
1337 (goops-error "top-sort: Invalid constraints")
1338 (let ((choice (if (null? (cdr can-go-in-now))
1343 (filter (lambda (x) (not (eq? x choice)))
1346 (append result (list choice)))))))))
1348 (define (std-tie-breaker get-supers)
1349 (lambda (partial-cpl min-elts)
1350 (let loop ((pcpl (reverse partial-cpl)))
1351 (let ((current-elt (car pcpl)))
1352 (let ((ds-of-ce (get-supers current-elt)))
1353 (let ((common (filter (lambda (x)
1357 (if (null? (cdr pcpl))
1358 (goops-error "std-tie-breaker: Nothing valid")
1363 (define (build-transitive-closure get-follow-ons)
1365 (let track ((result '())
1369 (let ((next (car pending)))
1370 (if (memq next result)
1371 (track result (cdr pending))
1372 (track (cons next result)
1373 (append (get-follow-ons next)
1374 (cdr pending)))))))))
1376 (define (build-constraints get-follow-ons)
1378 (let loop ((elements ((build-transitive-closure get-follow-ons) x))
1381 (if (or (null? this-one) (null? (cdr this-one)))
1382 (if (null? elements)
1384 (loop (cdr elements)
1385 (cons (car elements)
1386 (get-follow-ons (car elements)))
1390 (cons (list (car this-one) (cadr this-one))
1393 ;;; compute-get-n-set
1395 (define-method (compute-get-n-set (class <class>) s)
1396 (define (class-slot-init-value)
1397 (let ((thunk (slot-definition-init-thunk s)))
1400 (slot-definition-init-value s))))
1402 (case (slot-definition-allocation s)
1403 ((#:instance) ;; Instance slot
1404 ;; get-n-set is just its offset
1405 (let ((already-allocated (slot-ref class 'nfields)))
1406 (slot-set! class 'nfields (+ already-allocated 1))
1409 ((#:class) ;; Class slot
1410 ;; Class-slots accessors are implemented as 2 closures around
1411 ;; a Scheme variable. As instance slots, class slots must be
1412 ;; unbound at init time.
1413 (let ((name (slot-definition-name s)))
1414 (if (memq name (map slot-definition-name (class-direct-slots class)))
1415 ;; This slot is direct; create a new shared variable
1416 (make-closure-variable class (class-slot-init-value))
1417 ;; Slot is inherited. Find its definition in superclass
1418 (let loop ((l (cdr (class-precedence-list class))))
1419 (let ((r (assoc name (slot-ref (car l) 'getters-n-setters))))
1422 (loop (cdr l))))))))
1424 ((#:each-subclass) ;; slot shared by instances of direct subclass.
1425 ;; (Thomas Buerger, April 1998)
1426 (make-closure-variable class (class-slot-init-value)))
1428 ((#:virtual) ;; No allocation
1429 ;; slot-ref and slot-set! function must be given by the user
1430 (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
1431 (set (get-keyword #:slot-set! (slot-definition-options s) #f)))
1432 (if (not (and get set))
1433 (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S"
1436 (else (next-method))))
1438 (define (make-closure-variable class value)
1439 (list (lambda (o) value)
1440 (lambda (o v) (set! value v))))
1442 (define-method (compute-get-n-set (o <object>) s)
1443 (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
1445 (define-method (compute-slots (class <class>))
1446 (%compute-slots class))
1452 (define-method (initialize (object <object>) initargs)
1453 (%initialize-object object initargs))
1455 (define-method (initialize (class <class>) initargs)
1457 (let ((dslots (get-keyword #:slots initargs '()))
1458 (supers (get-keyword #:dsupers initargs '())))
1459 (slot-set! class 'name (get-keyword #:name initargs '???))
1460 (slot-set! class 'direct-supers supers)
1461 (slot-set! class 'direct-slots dslots)
1462 (slot-set! class 'direct-subclasses '())
1463 (slot-set! class 'direct-methods '())
1464 (slot-set! class 'cpl (compute-cpl class))
1465 (slot-set! class 'redefined #f)
1466 (let ((slots (compute-slots class)))
1467 (slot-set! class 'slots slots)
1468 (slot-set! class 'nfields 0)
1469 (slot-set! class 'getters-n-setters (compute-getters-n-setters class
1471 ;; Build getters - setters - accessors
1472 (compute-slot-accessors class slots))
1474 ;; Update the "direct-subclasses" of each inherited classes
1475 (for-each (lambda (x)
1478 (cons class (slot-ref x 'direct-subclasses))))
1481 ;; Support for the underlying structs:
1483 ;; Set the layout slot
1484 (%prep-layout! class)
1485 ;; Inherit class flags (invisible on scheme level) from supers
1486 (%inherit-magic! class supers)))
1488 (define (initialize-object-procedure object initargs)
1489 (let ((proc (get-keyword #:procedure initargs #f)))
1492 (apply slot-set! object 'procedure proc))
1494 (slot-set! object 'procedure proc)))))
1496 (define-method (initialize (applicable-struct <applicable-struct>) initargs)
1498 (initialize-object-procedure applicable-struct initargs))
1500 (define-method (initialize (generic <generic>) initargs)
1501 (let ((previous-definition (get-keyword #:default initargs #f))
1502 (name (get-keyword #:name initargs #f)))
1504 (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
1506 (apply previous-definition args)))
1509 (set-procedure-property! generic 'name name))
1512 (define-method (initialize (gws <generic-with-setter>) initargs)
1514 (%set-object-setter! gws (get-keyword #:setter initargs #f)))
1516 (define-method (initialize (eg <extended-generic>) initargs)
1518 (slot-set! eg 'extends (get-keyword #:extends initargs '())))
1520 (define dummy-procedure (lambda args *unspecified*))
1522 (define-method (initialize (method <method>) initargs)
1524 (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
1525 (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
1526 (slot-set! method 'procedure
1527 (get-keyword #:procedure initargs #f))
1528 (slot-set! method 'formals (get-keyword #:formals initargs '()))
1529 (slot-set! method 'body (get-keyword #:body initargs '()))
1530 (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f)))
1537 (define (change-object-class old-instance old-class new-class)
1538 (let ((new-instance (allocate-instance new-class '())))
1539 ;; Initialize the slots of the new instance
1540 (for-each (lambda (slot)
1541 (if (and (slot-exists-using-class? old-class old-instance slot)
1542 (eq? (slot-definition-allocation
1543 (class-slot-definition old-class slot))
1545 (slot-bound-using-class? old-class old-instance slot))
1546 ;; Slot was present and allocated in old instance; copy it
1547 (slot-set-using-class!
1551 (slot-ref-using-class old-class old-instance slot))
1552 ;; slot was absent; initialize it with its default value
1553 (let ((init (slot-init-function new-class slot)))
1555 (slot-set-using-class!
1559 (apply init '()))))))
1560 (map slot-definition-name (class-slots new-class)))
1561 ;; Exchange old and new instance in place to keep pointers valid
1562 (%modify-instance old-instance new-instance)
1563 ;; Allow class specific updates of instances (which now are swapped)
1564 (update-instance-for-different-class new-instance old-instance)
1568 (define-method (update-instance-for-different-class (old-instance <object>)
1571 ;;not really important what we do, we just need a default method
1574 (define-method (change-class (old-instance <object>) (new-class <class>))
1575 (change-object-class old-instance (class-of old-instance) new-class))
1580 ;;; A new definition which overwrites the previous one which was built-in
1583 (define-method (allocate-instance (class <class>) initargs)
1584 (%allocate-instance class initargs))
1586 (define-method (make-instance (class <class>) . initargs)
1587 (let ((instance (allocate-instance class initargs)))
1588 (initialize instance initargs)
1591 (define make make-instance)
1596 ;;; Protocol for calling standard generic functions. This protocol is
1597 ;;; not used for real <generic> functions (in this case we use a
1598 ;;; completely C hard-coded protocol). Apply-generic is used by
1599 ;;; goops for calls to subclasses of <generic> and <generic-with-setter>.
1600 ;;; The code below is similar to the first MOP described in AMOP. In
1601 ;;; particular, it doesn't used the currified approach to gf
1602 ;;; call. There are 2 reasons for that:
1603 ;;; - the protocol below is exposed to mimic completely the one written in C
1604 ;;; - the currified protocol would be imho inefficient in C.
1607 (define-method (apply-generic (gf <generic>) args)
1608 (if (null? (slot-ref gf 'methods))
1609 (no-method gf args))
1610 (let ((methods (compute-applicable-methods gf args)))
1612 (apply-methods gf (sort-applicable-methods gf methods args) args)
1613 (no-applicable-method gf args))))
1615 ;; compute-applicable-methods is bound to %compute-applicable-methods.
1617 (define %%compute-applicable-methods
1618 (make <generic> #:name 'compute-applicable-methods))
1620 (define-method (%%compute-applicable-methods (gf <generic>) args)
1621 (%compute-applicable-methods gf args))
1623 (set! compute-applicable-methods %%compute-applicable-methods)
1625 (define-method (sort-applicable-methods (gf <generic>) methods args)
1626 (let ((targs (map class-of args)))
1627 (sort methods (lambda (m1 m2) (method-more-specific? m1 m2 targs)))))
1629 (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
1630 (%method-more-specific? m1 m2 targs))
1632 (define-method (apply-method (gf <generic>) methods build-next args)
1633 (apply (method-procedure (car methods))
1634 (build-next (cdr methods) args)
1637 (define-method (apply-methods (gf <generic>) (l <list>) args)
1638 (letrec ((next (lambda (procs args)
1640 (let ((a (if (null? new-args) args new-args)))
1642 (no-next-method gf a)
1643 (apply-method gf procs next a)))))))
1644 (apply-method gf l next args)))
1646 ;; We don't want the following procedure to turn up in backtraces:
1647 (for-each (lambda (proc)
1648 (set-procedure-property! proc 'system-procedure #t))
1652 no-applicable-method
1657 ;;; {<composite-metaclass> and <active-metaclass>}
1660 ;(autoload "active-slot" <active-metaclass>)
1661 ;(autoload "composite-slot" <composite-metaclass>)
1662 ;(export <composite-metaclass> <active-metaclass>)
1670 ;; duplicate the standard list->set function but using eq instead of
1671 ;; eqv which really sucks a lot, uselessly here
1673 (define (list2set l)
1678 ((memq (car l) res) (loop (cdr l) res))
1679 (else (loop (cdr l) (cons (car l) res))))))
1681 (define (class-subclasses c)
1682 (letrec ((allsubs (lambda (c)
1683 (cons c (mapappend allsubs
1684 (class-direct-subclasses c))))))
1685 (list2set (cdr (allsubs c)))))
1687 (define (class-methods c)
1688 (list2set (mapappend class-direct-methods
1689 (cons c (class-subclasses c)))))
1692 ;;; {Final initialization}
1695 ;; Tell C code that the main bulk of Goops has been loaded