3 ;;;; Copyright (C) 1998,1999,2000,2001,2002, 2003, 2006, 2009 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 ;;;; This software is a derivative work of other copyrighted softwares; the
22 ;;;; copyright notices of these softwares are placed in the file COPYRIGHTS
24 ;;;; This file is based upon stklos.stk from the STk distribution by
25 ;;;; Erick Gallesio <eg@unice.fr>.
28 (define-module (oop goops)
29 :use-module (srfi srfi-1)
30 :export-syntax (define-class class standard-define-class
31 define-generic define-accessor define-method
32 define-extended-generic define-extended-generics
34 :export (goops-version is-a? class-of
35 ensure-metaclass ensure-metaclass-with-supers
37 make-generic ensure-generic
39 make-accessor ensure-accessor
41 class-slot-ref class-slot-set! slot-unbound slot-missing
42 slot-definition-name slot-definition-options
43 slot-definition-allocation
44 slot-definition-getter slot-definition-setter
45 slot-definition-accessor
46 slot-definition-init-value slot-definition-init-form
47 slot-definition-init-thunk slot-definition-init-keyword
48 slot-init-function class-slot-definition
50 compute-cpl compute-std-cpl compute-get-n-set compute-slots
51 compute-getter-method compute-setter-method
52 allocate-instance initialize make-instance make
53 no-next-method no-applicable-method no-method
54 change-class update-instance-for-different-class
55 shallow-clone deep-clone
57 apply-generic apply-method apply-methods
58 compute-applicable-methods %compute-applicable-methods
59 method-more-specific? sort-applicable-methods
60 class-subclasses class-methods
63 ;;; *fixme* Should go into goops.c
64 instance? slot-ref-using-class
65 slot-set-using-class! slot-bound-using-class?
66 slot-exists-using-class? slot-ref slot-set! slot-bound?
67 class-name class-direct-supers class-direct-subclasses
68 class-direct-methods class-direct-slots class-precedence-list
71 generic-function-methods method-generic-function
72 method-specializers method-formals
73 primitive-generic-generic enable-primitive-generic!
74 method-procedure accessor-method-slot-definition
75 slot-exists? make find-method get-keyword)
78 (define *goops-module* (current-module))
80 ;; First initialize the builtin part of GOOPS
81 (eval-when (eval load compile)
82 (%init-goops-builtins))
84 (eval-when (eval load compile)
85 (use-modules ((language tree-il primitives) :select (add-interesting-primitive!)))
86 (add-interesting-primitive! 'class-of)
87 (define (@slot-ref o n)
89 (define (@slot-set! o n v)
91 (add-interesting-primitive! '@slot-ref)
92 (add-interesting-primitive! '@slot-set!))
94 ;; Then load the rest of GOOPS
95 (use-modules (oop goops util)
100 (eval-when (eval load compile)
101 (define min-fixnum (- (expt 2 29)))
102 (define max-fixnum (- (expt 2 29) 1)))
107 (define (goops-error format-string . args)
109 (scm-error 'goops-error #f format-string args '()))
114 (define (is-a? obj class)
115 (and (memq class (class-precedence-list (class-of obj))) #t))
122 (define ensure-metaclass-with-supers
123 (let ((table-of-metas '()))
124 (lambda (meta-supers)
125 (let ((entry (assoc meta-supers table-of-metas)))
127 ;; Found a previously created metaclass
129 ;; Create a new meta-class which inherit from "meta-supers"
130 (let ((new (make <class> #:dsupers meta-supers
132 #:name (gensym "metaclass"))))
133 (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
136 (define (ensure-metaclass supers)
139 (let* ((all-metas (map (lambda (x) (class-of x)) supers))
140 (all-cpls (append-map (lambda (m)
141 (cdr (class-precedence-list m)))
144 ;; Find the most specific metaclasses. The new metaclass will be
145 ;; a subclass of these.
148 (if (and (not (member meta all-cpls))
149 (not (member meta needed-metas)))
150 (set! needed-metas (append needed-metas (list meta)))))
152 ;; Now return a subclass of the metaclasses we found.
153 (if (null? (cdr needed-metas))
154 (car needed-metas) ; If there's only one, just use it.
155 (ensure-metaclass-with-supers needed-metas)))))
161 ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
163 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
164 ;;; OPTION ::= KEYWORD VALUE
167 (define (kw-do-map mapper f kwargs)
171 ((or (null? (cdr l)) (not (keyword? (car l))))
172 (goops-error "malformed keyword arguments: ~a" kwargs))
173 (else (cons (car l) (keywords (cddr l))))))
175 (if (null? l) '() (cons (cadr l) (args (cddr l)))))
176 ;; let* to check keywords first
177 (let* ((k (keywords kwargs))
181 (define (make-class supers slots . options)
182 (let* ((name (get-keyword #:name options (make-unbound)))
183 (supers (if (not (or-map (lambda (class)
185 (class-precedence-list class)))
187 (append supers (list <object>))
189 (metaclass (or (get-keyword #:metaclass options #f)
190 (ensure-metaclass supers))))
192 ;; Verify that all direct slots are different and that we don't inherit
193 ;; several time from the same class
194 (let ((tmp1 (find-duplicate supers))
195 (tmp2 (find-duplicate (map slot-definition-name slots))))
197 (goops-error "make-class: super class ~S is duplicate in class ~S"
200 (goops-error "make-class: slot ~S is duplicate in class ~S"
203 ;; Everything seems correct, build the class
204 (apply make metaclass
210 ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
212 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
213 ;;; OPTION ::= KEYWORD VALUE
215 (define-macro (class supers . slots)
216 (define (make-slot-definition-forms slots)
222 ,@(kw-do-map append-map
227 #:init-thunk (lambda () ,arg)))
228 (else (list kw arg))))
233 (if (not (list? supers))
234 (goops-error "malformed superclass list: ~S" supers))
235 (let ((slots (take-while (lambda (x) (not (keyword? x))) slots))
236 (options (or (find-tail keyword? slots) '())))
238 ;; evaluate super class variables
240 ;; evaluate slot definitions, except the slot name!
241 (list ,@(make-slot-definition-forms slots))
242 ;; evaluate class options
245 (define-syntax define-class-pre-definition
248 ((_ (k arg rest ...) out ...)
249 (keyword? (syntax->datum (syntax k)))
250 (case (syntax->datum (syntax k))
253 (define-class-pre-definition (rest ...)
255 (if (or (not (defined? 'arg))
256 (not (is-a? arg <generic>)))
259 (ensure-generic (if (defined? 'arg) arg #f) 'arg))))))
262 (define-class-pre-definition (rest ...)
264 (if (or (not (defined? 'arg))
265 (not (is-a? arg <accessor>)))
268 (ensure-accessor (if (defined? 'arg) arg #f) 'arg))))))
271 (define-class-pre-definition (rest ...) out ...)))))
273 (syntax (begin out ...))))))
275 ;; Some slot options require extra definitions to be made. In
276 ;; particular, we want to make sure that the generic function objects
277 ;; which represent accessors exist before `make-class' tries to add
279 (define-syntax define-class-pre-definitions
283 (syntax (begin out ...)))
284 ((_ (slot rest ...) out ...)
285 (keyword? (syntax->datum (syntax slot)))
286 (syntax (begin out ...)))
287 ((_ (slot rest ...) out ...)
288 (identifier? (syntax slot))
289 (syntax (define-class-pre-definitions (rest ...)
291 ((_ ((slotname slotopt ...) rest ...) out ...)
292 (syntax (define-class-pre-definitions (rest ...)
293 out ... (define-class-pre-definition (slotopt ...))))))))
295 (define-syntax define-class
297 ((_ name supers slot ...)
299 (define-class-pre-definitions (slot ...))
300 (if (and (defined? 'name)
302 (memq <object> (class-precedence-list name)))
303 (class-redefinition name
304 (class supers slot ... #:name 'name))
305 (toplevel-define! 'name (class supers slot ... #:name 'name)))))))
307 (define-syntax standard-define-class
309 ((_ arg ...) (define-class arg ...))))
312 ;;; {Generic functions and accessors}
315 ;; Apparently the desired semantics are that we extend previous
316 ;; procedural definitions, but that if `name' was already a generic, we
317 ;; overwrite its definition.
318 (define-macro (define-generic name)
319 (if (not (symbol? name))
320 (goops-error "bad generic function name: ~S" name))
322 (if (and (defined? ',name) (is-a? ,name <generic>))
323 (make <generic> #:name ',name)
324 (ensure-generic (if (defined? ',name) ,name #f) ',name))))
326 (define-macro (define-extended-generic name val)
327 (if (not (symbol? name))
328 (goops-error "bad generic function name: ~S" name))
329 `(define ,name (make-extended-generic ,val ',name)))
331 (define-macro (define-extended-generics names . args)
332 (let ((prefixes (get-keyword #:prefix args #f)))
335 ,@(map (lambda (name)
336 `(define-extended-generic ,name
337 (list ,@(map (lambda (prefix)
338 (symbol-append prefix name))
341 (goops-error "no prefixes supplied"))))
343 (define (make-generic . name)
344 (let ((name (and (pair? name) (car name))))
345 (make <generic> #:name name)))
347 (define (make-extended-generic gfs . name)
348 (let* ((name (and (pair? name) (car name)))
349 (gfs (if (pair? gfs) gfs (list gfs)))
350 (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs)))
352 (let* ((sname (and name (make-setter-name name)))
354 (append-map (lambda (gf)
355 (if (is-a? gf <generic-with-setter>)
356 (list (ensure-generic (setter gf)
360 (es (make <extended-generic-with-setter>
363 #:setter (make <extended-generic>
365 #:extends setters))))
366 (extended-by! setters (setter es))
368 (make <extended-generic>
371 (extended-by! gfs ans)
374 (define (extended-by! gfs eg)
375 (for-each (lambda (gf)
376 (slot-set! gf 'extended-by
377 (cons eg (slot-ref gf 'extended-by))))
380 (define (not-extended-by! gfs eg)
381 (for-each (lambda (gf)
382 (slot-set! gf 'extended-by
383 (delq! eg (slot-ref gf 'extended-by))))
386 (define (ensure-generic old-definition . name)
387 (let ((name (and (pair? name) (car name))))
388 (cond ((is-a? old-definition <generic>) old-definition)
389 ((procedure-with-setter? old-definition)
390 (make <generic-with-setter>
392 #:default (procedure old-definition)
393 #:setter (setter old-definition)))
394 ((procedure? old-definition)
395 (make <generic> #:name name #:default old-definition))
396 (else (make <generic> #:name name)))))
398 ;; same semantics as <generic>
399 (define-syntax define-accessor
403 (cond ((not (defined? 'name)) (ensure-accessor #f 'name))
404 ((is-a? name <accessor>) (make <accessor> #:name 'name))
405 (else (ensure-accessor name 'name)))))))
407 (define (make-setter-name name)
408 (string->symbol (string-append "setter:" (symbol->string name))))
410 (define (make-accessor . name)
411 (let ((name (and (pair? name) (car name))))
414 #:setter (make <generic>
415 #:name (and name (make-setter-name name))))))
417 (define (ensure-accessor proc . name)
418 (let ((name (and (pair? name) (car name))))
419 (cond ((and (is-a? proc <accessor>)
420 (is-a? (setter proc) <generic>))
422 ((is-a? proc <generic-with-setter>)
423 (upgrade-accessor proc (setter proc)))
424 ((is-a? proc <generic>)
425 (upgrade-accessor proc (make-generic name)))
426 ((procedure-with-setter? proc)
429 #:default (procedure proc)
430 #:setter (ensure-generic (setter proc) name)))
432 (ensure-accessor (ensure-generic proc name) name))
434 (make-accessor name)))))
436 (define (upgrade-accessor generic setter)
437 (let ((methods (slot-ref generic 'methods))
438 (gws (make (if (is-a? generic <extended-generic>)
439 <extended-generic-with-setter>
441 #:name (generic-function-name generic)
442 #:extended-by (slot-ref generic 'extended-by)
444 (if (is-a? generic <extended-generic>)
445 (let ((gfs (slot-ref generic 'extends)))
446 (not-extended-by! gfs generic)
447 (slot-set! gws 'extends gfs)
448 (extended-by! gfs gws)))
450 (for-each (lambda (method)
451 (slot-set! method 'generic-function gws))
453 (slot-set! gws 'methods methods)
460 (define (toplevel-define! name val)
461 (module-define! (current-module) name val))
463 (define-syntax define-method
464 (syntax-rules (setter)
465 ((_ ((setter name) . args) body ...)
467 (if (or (not (defined? 'name))
468 (not (is-a? name <accessor>)))
469 (toplevel-define! 'name
471 (if (defined? 'name) name #f) 'name)))
472 (add-method! (setter name) (method args body ...))))
473 ((_ (name . args) body ...)
475 ;; FIXME: this code is how it always was, but it's quite cracky:
476 ;; it will only define the generic function if it was undefined
477 ;; before (ok), or *was defined to #f*. The latter is crack. But
478 ;; there are bootstrap issues about fixing this -- change it to
479 ;; (is-a? name <generic>) and see.
480 (if (or (not (defined? 'name))
482 (toplevel-define! 'name (make <generic> #:name 'name)))
483 (add-method! name (method args body ...))))))
485 (define-syntax method
487 (define (parse-args args)
488 (let lp ((ls args) (formals '()) (specializers '()))
491 (and (identifier? (syntax f)) (identifier? (syntax s)))
493 (cons (syntax f) formals)
494 (cons (syntax s) specializers)))
496 (identifier? (syntax f))
498 (cons (syntax f) formals)
499 (cons (syntax <top>) specializers)))
501 (list (reverse formals)
502 (reverse (cons (syntax '()) specializers))))
504 (identifier? (syntax tail))
505 (list (append (reverse formals) (syntax tail))
506 (reverse (cons (syntax <top>) specializers)))))))
508 (define (find-free-id exp referent)
511 (or (find-free-id (syntax x) referent)
512 (find-free-id (syntax y) referent)))
514 (identifier? (syntax x))
515 (let ((id (datum->syntax (syntax x) referent)))
516 (and (free-identifier=? (syntax x) id) id)))
519 (define (compute-procedure formals body)
522 (with-syntax ((formals formals))
523 (syntax (lambda formals body0 ...))))))
525 (define (->proper args)
526 (let lp ((ls args) (out '()))
528 ((x . xs) (lp (syntax xs) (cons (syntax x) out)))
530 (tail (reverse (cons (syntax tail) out))))))
532 (define (compute-make-procedure formals body next-method)
535 (with-syntax ((next-method next-method))
536 (syntax-case formals ()
539 (lambda (real-next-method)
541 (let ((next-method (lambda args
543 (real-next-method formal ...)
544 (apply real-next-method args)))))
547 (with-syntax (((formal ...) (->proper (syntax formals))))
549 (lambda (real-next-method)
551 (let ((next-method (lambda args
553 (apply real-next-method formal ...)
554 (apply real-next-method args)))))
557 (define (compute-procedures formals body)
558 ;; So, our use of this is broken, because it operates on the
559 ;; pre-expansion source code. It's equivalent to just searching
560 ;; for referent in the datums. Ah well.
561 (let ((id (find-free-id body 'next-method)))
563 ;; return a make-procedure
565 (compute-make-procedure formals body id))
566 (values (compute-procedure formals body)
570 ((_ args) (syntax (method args (if #f #f))))
571 ((_ args body0 body1 ...)
572 (with-syntax (((formals (specializer ...)) (parse-args (syntax args))))
575 (compute-procedures (syntax formals) (syntax (body0 body1 ...))))
576 (lambda (procedure make-procedure)
577 (with-syntax ((procedure procedure)
578 (make-procedure make-procedure))
581 #:specializers (cons* specializer ...)
583 #:body '(body0 body1 ...)
584 #:make-procedure make-procedure
585 #:procedure procedure))))))))))
591 (define (add-method-in-classes! m)
592 ;; Add method in all the classes which appears in its specializers list
593 (for-each* (lambda (x)
594 (let ((dm (class-direct-methods x)))
595 (if (not (memq m dm))
596 (slot-set! x 'direct-methods (cons m dm)))))
597 (method-specializers m)))
599 (define (remove-method-in-classes! m)
600 ;; Remove method in all the classes which appears in its specializers list
601 (for-each* (lambda (x)
604 (delv! m (class-direct-methods x))))
605 (method-specializers m)))
607 (define (compute-new-list-of-methods gf new)
608 (let ((new-spec (method-specializers new))
609 (methods (slot-ref gf 'methods)))
610 (let loop ((l methods))
613 (if (equal? (method-specializers (car l)) new-spec)
615 ;; This spec. list already exists. Remove old method from dependents
616 (remove-method-in-classes! (car l))
621 (define internal-add-method!
622 (method ((gf <generic>) (m <method>))
623 (slot-set! m 'generic-function gf)
624 (slot-set! gf 'methods (compute-new-list-of-methods gf m))
625 (let ((specializers (slot-ref m 'specializers)))
626 (slot-set! gf 'n-specialized
627 (max (length* specializers)
628 (slot-ref gf 'n-specialized))))
629 (%invalidate-method-cache! gf)
630 (add-method-in-classes! m)
633 (define-generic add-method!)
635 ((method-procedure internal-add-method!) add-method! internal-add-method!)
637 (define-method (add-method! (proc <procedure>) (m <method>))
638 (if (generic-capability? proc)
640 (enable-primitive-generic! proc)
641 (add-method! proc m))
644 (define-method (add-method! (pg <primitive-generic>) (m <method>))
645 (add-method! (primitive-generic-generic pg) m))
647 (define-method (add-method! obj (m <method>))
648 (goops-error "~S is not a valid generic function" obj))
651 ;;; {Access to meta objects}
657 (define-method (method-source (m <method>))
658 (let* ((spec (map* class-name (slot-ref m 'specializers)))
659 (src (procedure-source (slot-ref m 'procedure))))
661 (let ((args (cadr src))
664 (cons (map* list args spec)
667 (define-method (method-formals (m <method>))
668 (slot-ref m 'formals))
673 (define slot-definition-name car)
675 (define slot-definition-options cdr)
677 (define (slot-definition-allocation s)
678 (get-keyword #:allocation (cdr s) #:instance))
680 (define (slot-definition-getter s)
681 (get-keyword #:getter (cdr s) #f))
683 (define (slot-definition-setter s)
684 (get-keyword #:setter (cdr s) #f))
686 (define (slot-definition-accessor s)
687 (get-keyword #:accessor (cdr s) #f))
689 (define (slot-definition-init-value s)
690 ;; can be #f, so we can't use #f as non-value
691 (get-keyword #:init-value (cdr s) (make-unbound)))
693 (define (slot-definition-init-form s)
694 (get-keyword #:init-form (cdr s) (make-unbound)))
696 (define (slot-definition-init-thunk s)
697 (get-keyword #:init-thunk (cdr s) #f))
699 (define (slot-definition-init-keyword s)
700 (get-keyword #:init-keyword (cdr s) #f))
702 (define (class-slot-definition class slot-name)
703 (assq slot-name (class-slots class)))
705 (define (slot-init-function class slot-name)
706 (cadr (assq slot-name (slot-ref class 'getters-n-setters))))
708 (define (accessor-method-slot-definition obj)
709 "Return the slot definition of the accessor @var{obj}."
710 (slot-ref obj 'slot-definition))
714 ;;; {Standard methods used by the C runtime}
717 ;;; Methods to compare objects
720 ;; Have to do this in a strange order because equal? is used in the
721 ;; add-method! implementation; we need to make sure that when the
722 ;; primitive is extended, that the generic has a method. =
723 (define g-equal? (make-generic 'equal?))
724 ;; When this generic gets called, we will have already checked eq? and
725 ;; eqv? -- the purpose of this generic is to extend equality. So by
726 ;; default, there is no extension, thus the #f return.
727 (add-method! g-equal? (method (x y) #f))
728 (set-primitive-generic! equal? g-equal?)
731 ;;; methods to display/write an object
734 ; Code for writing objects must test that the slots they use are
735 ; bound. Otherwise a slot-unbound method will be called and will
736 ; conduct to an infinite loop.
739 (define (display-address o file)
740 (display (number->string (object-address o) 16) file))
742 (define-method (write o file)
743 (display "#<instance " file)
744 (display-address o file)
747 (define write-object (primitive-generic-generic write))
749 (define-method (write (o <object>) file)
750 (let ((class (class-of o)))
751 (if (slot-bound? class 'name)
754 (display (class-name class) file)
755 (display #\space file)
756 (display-address o file)
760 (define-method (write (class <class>) file)
761 (let ((meta (class-of class)))
762 (if (and (slot-bound? class 'name)
763 (slot-bound? meta 'name))
766 (display (class-name meta) file)
767 (display #\space file)
768 (display (class-name class) file)
769 (display #\space file)
770 (display-address class file)
774 (define-method (write (gf <generic>) file)
775 (let ((meta (class-of gf)))
776 (if (and (slot-bound? meta 'name)
777 (slot-bound? gf 'methods))
780 (display (class-name meta) file)
781 (let ((name (generic-function-name gf)))
784 (display #\space file)
785 (display name file))))
787 (display (length (generic-function-methods gf)) file)
791 (define-method (write (o <method>) file)
792 (let ((meta (class-of o)))
793 (if (and (slot-bound? meta 'name)
794 (slot-bound? o 'specializers))
797 (display (class-name meta) file)
798 (display #\space file)
799 (display (map* (lambda (spec)
800 (if (slot-bound? spec 'name)
801 (slot-ref spec 'name)
803 (method-specializers o))
805 (display #\space file)
806 (display-address o file)
810 ;; Display (do the same thing as write by default)
811 (define-method (display o file)
812 (write-object o file))
815 ;;; Handling of duplicate bindings in the module system
818 (define-method (merge-generics (module <module>)
828 (define-method (merge-generics (module <module>)
836 (and (not (eq? val1 val2))
837 (make-variable (make-extended-generic (list val2 val1) name))))
839 (define-method (merge-generics (module <module>)
846 (gf <extended-generic>))
847 (and (not (memq val2 (slot-ref gf 'extends)))
851 (cons val2 (delq! val2 (slot-ref gf 'extends))))
854 (cons gf (delq! gf (slot-ref val2 'extended-by))))
857 (module-define! duplicate-handlers 'merge-generics merge-generics)
859 (define-method (merge-accessors (module <module>)
869 (define-method (merge-accessors (module <module>)
877 (merge-generics module name int1 val1 int2 val2 var val))
879 (module-define! duplicate-handlers 'merge-accessors merge-accessors)
885 (define (class-slot-g-n-s class slot-name)
886 (let* ((this-slot (assq slot-name (slot-ref class 'slots)))
887 (g-n-s (cddr (or (assq slot-name (slot-ref class 'getters-n-setters))
888 (slot-missing class slot-name)))))
889 (if (not (memq (slot-definition-allocation this-slot)
890 '(#:class #:each-subclass)))
891 (slot-missing class slot-name))
894 (define (class-slot-ref class slot)
895 (let ((x ((car (class-slot-g-n-s class slot)) #f)))
897 (slot-unbound class slot)
900 (define (class-slot-set! class slot value)
901 ((cadr (class-slot-g-n-s class slot)) #f value))
903 (define-method (slot-unbound (c <class>) (o <object>) s)
904 (goops-error "Slot `~S' is unbound in object ~S" s o))
906 (define-method (slot-unbound (c <class>) s)
907 (goops-error "Slot `~S' is unbound in class ~S" s c))
909 (define-method (slot-unbound (o <object>))
910 (goops-error "Unbound slot in object ~S" o))
912 (define-method (slot-missing (c <class>) (o <object>) s)
913 (goops-error "No slot with name `~S' in object ~S" s o))
915 (define-method (slot-missing (c <class>) s)
916 (goops-error "No class slot with name `~S' in class ~S" s c))
919 (define-method (slot-missing (c <class>) (o <object>) s value)
920 (slot-missing c o s))
922 ;;; Methods for the possible error we can encounter when calling a gf
924 (define-method (no-next-method (gf <generic>) args)
925 (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
927 (define-method (no-applicable-method (gf <generic>) args)
928 (goops-error "No applicable method for ~S in call ~S"
929 gf (cons (generic-function-name gf) args)))
931 (define-method (no-method (gf <generic>) args)
932 (goops-error "No method defined for ~S" gf))
935 ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
938 (define-method (shallow-clone (self <object>))
939 (let ((clone (%allocate-instance (class-of self) '()))
940 (slots (map slot-definition-name
941 (class-slots (class-of self)))))
942 (for-each (lambda (slot)
943 (if (slot-bound? self slot)
944 (slot-set! clone slot (slot-ref self slot))))
948 (define-method (deep-clone (self <object>))
949 (let ((clone (%allocate-instance (class-of self) '()))
950 (slots (map slot-definition-name
951 (class-slots (class-of self)))))
952 (for-each (lambda (slot)
953 (if (slot-bound? self slot)
954 (slot-set! clone slot
955 (let ((value (slot-ref self slot)))
956 (if (instance? value)
963 ;;; {Class redefinition utilities}
966 ;;; (class-redefinition OLD NEW)
969 ;;; Has correct the following conditions:
973 ;;; 1. New accessor specializers refer to new header
977 ;;; 1. New class cpl refers to the new class header
978 ;;; 2. Old class header exists on old super classes direct-subclass lists
979 ;;; 3. New class header exists on new super classes direct-subclass lists
981 (define-method (class-redefinition (old <class>) (new <class>))
982 ;; Work on direct methods:
983 ;; 1. Remove accessor methods from the old class
984 ;; 2. Patch the occurences of new in the specializers by old
985 ;; 3. Displace the methods from old to new
986 (remove-class-accessors! old) ;; -1-
987 (let ((methods (class-direct-methods new)))
988 (for-each (lambda (m)
989 (update-direct-method! m new old)) ;; -2-
993 (append methods (class-direct-methods old))))
995 ;; Substitute old for new in new cpl
996 (set-car! (slot-ref new 'cpl) old)
998 ;; Remove the old class from the direct-subclasses list of its super classes
999 (for-each (lambda (c) (slot-set! c 'direct-subclasses
1000 (delv! old (class-direct-subclasses c))))
1001 (class-direct-supers old))
1003 ;; Replace the new class with the old in the direct-subclasses of the supers
1004 (for-each (lambda (c)
1005 (slot-set! c 'direct-subclasses
1006 (cons old (delv! new (class-direct-subclasses c)))))
1007 (class-direct-supers new))
1009 ;; Swap object headers
1010 (%modify-class old new)
1014 ;; Redefine all the subclasses of old to take into account modification
1017 (update-direct-subclass! c new old))
1018 (class-direct-subclasses new))
1020 ;; Invalidate class so that subsequent instances slot accesses invoke
1021 ;; change-object-class
1022 (slot-set! new 'redefined old)
1023 (%invalidate-class new) ;must come after slot-set!
1028 ;;; remove-class-accessors!
1031 (define-method (remove-class-accessors! (c <class>))
1032 (for-each (lambda (m)
1033 (if (is-a? m <accessor-method>)
1034 (let ((gf (slot-ref m 'generic-function)))
1035 ;; remove the method from its GF
1036 (slot-set! gf 'methods
1037 (delq1! m (slot-ref gf 'methods)))
1038 (%invalidate-method-cache! gf)
1039 ;; remove the method from its specializers
1040 (remove-method-in-classes! m))))
1041 (class-direct-methods c)))
1044 ;;; update-direct-method!
1047 (define-method (update-direct-method! (m <method>)
1050 (let loop ((l (method-specializers m)))
1051 ;; Note: the <top> in dotted list is never used.
1052 ;; So we can work as if we had only proper lists.
1055 (if (eqv? (car l) old)
1060 ;;; update-direct-subclass!
1063 (define-method (update-direct-subclass! (c <class>)
1066 (class-redefinition c
1067 (make-class (class-direct-supers c)
1068 (class-direct-slots c)
1069 #:name (class-name c)
1070 #:metaclass (class-of c))))
1073 ;;; {Utilities for INITIALIZE methods}
1076 ;;; compute-slot-accessors
1078 (define (compute-slot-accessors class slots)
1081 (let ((getter-function (slot-definition-getter s))
1082 (setter-function (slot-definition-setter s))
1083 (accessor (slot-definition-accessor s)))
1085 (add-method! getter-function
1086 (compute-getter-method class g-n-s)))
1088 (add-method! setter-function
1089 (compute-setter-method class g-n-s)))
1092 (add-method! accessor
1093 (compute-getter-method class g-n-s))
1094 (add-method! (setter accessor)
1095 (compute-setter-method class g-n-s))))))
1096 slots (slot-ref class 'getters-n-setters)))
1098 (define-method (compute-getter-method (class <class>) slotdef)
1099 (let ((init-thunk (cadr slotdef))
1100 (g-n-s (cddr slotdef)))
1101 (make <accessor-method>
1102 #:specializers (list class)
1103 #:procedure (cond ((pair? g-n-s)
1104 (make-generic-bound-check-getter (car g-n-s)))
1106 (standard-get g-n-s))
1108 (bound-check-get g-n-s)))
1109 #:slot-definition slotdef)))
1111 (define-method (compute-setter-method (class <class>) slotdef)
1112 (let ((g-n-s (cddr slotdef)))
1113 (make <accessor-method>
1114 #:specializers (list class <top>)
1115 #:procedure (if (pair? g-n-s)
1117 (standard-set g-n-s))
1118 #:slot-definition slotdef)))
1120 (define (make-generic-bound-check-getter proc)
1121 (lambda (o) (assert-bound (proc o) o)))
1123 ;; the idea is to compile the index into the procedure, for fastest
1124 ;; lookup. Also, @slot-ref and @slot-set! have their own bytecodes.
1126 (eval-when (eval load compile)
1127 (define num-standard-pre-cache 20))
1129 (define-macro (define-standard-accessor-method form . body)
1130 (let ((name (caar form))
1131 (n-var (cadar form))
1133 (define (make-one x)
1134 (define (body-trans form)
1135 (cond ((not (pair? form)) form)
1136 ((eq? (car form) '@slot-ref)
1137 `(,(car form) ,(cadr form) ,x))
1138 ((eq? (car form) '@slot-set!)
1139 `(,(car form) ,(cadr form) ,x ,(cadddr form)))
1141 (map body-trans form))))
1142 `(lambda ,args ,@(map body-trans body)))
1144 (let ((cache (vector ,@(map make-one (iota num-standard-pre-cache)))))
1146 (if (< n ,num-standard-pre-cache)
1147 (vector-ref cache n)
1148 ((lambda (,n-var) (lambda ,args ,@body)) n)))))))
1150 (define-standard-accessor-method ((bound-check-get n) o)
1151 (let ((x (@slot-ref o n)))
1156 (define-standard-accessor-method ((standard-get n) o)
1159 (define-standard-accessor-method ((standard-set n) o v)
1162 ;;; compute-getters-n-setters
1164 (define (compute-getters-n-setters class slots)
1166 (define (compute-slot-init-function name s)
1167 (or (let ((thunk (slot-definition-init-thunk s)))
1171 (goops-error "Bad init-thunk for slot `~S' in ~S: ~S"
1172 name class thunk))))
1173 (let ((init (slot-definition-init-value s)))
1174 (and (not (unbound? init))
1175 (lambda () init)))))
1177 (define (verify-accessors slot l)
1178 (cond ((integer? l))
1179 ((not (and (list? l) (= (length l) 2)))
1180 (goops-error "Bad getter and setter for slot `~S' in ~S: ~S"
1185 (if (not (procedure? get))
1186 (goops-error "Bad getter closure for slot `~S' in ~S: ~S"
1188 (if (not (procedure? set))
1189 (goops-error "Bad setter closure for slot `~S' in ~S: ~S"
1190 slot class set))))))
1193 ;; The strange treatment of nfields is due to backward compatibility.
1194 (let* ((index (slot-ref class 'nfields))
1195 (g-n-s (compute-get-n-set class s))
1196 (size (- (slot-ref class 'nfields) index))
1197 (name (slot-definition-name s)))
1198 ;; NOTE: The following is interdependent with C macros
1199 ;; defined above goops.c:scm_sys_prep_layout_x.
1201 ;; For simple instance slots, we have the simplest form
1202 ;; '(name init-function . index)
1203 ;; For other slots we have
1204 ;; '(name init-function getter setter . alloc)
1206 ;; '(index size) for instance allocated slots
1207 ;; '() for other slots
1208 (verify-accessors name g-n-s)
1210 (cons (compute-slot-init-function name s)
1211 (if (or (integer? g-n-s)
1214 (append g-n-s (list index size)))))))
1219 ;;; Correct behaviour:
1221 ;;; (define-class food ())
1222 ;;; (define-class fruit (food))
1223 ;;; (define-class spice (food))
1224 ;;; (define-class apple (fruit))
1225 ;;; (define-class cinnamon (spice))
1226 ;;; (define-class pie (apple cinnamon))
1227 ;;; => cpl (pie) = pie apple fruit cinnamon spice food object top
1229 ;;; (define-class d ())
1230 ;;; (define-class e ())
1231 ;;; (define-class f ())
1232 ;;; (define-class b (d e))
1233 ;;; (define-class c (e f))
1234 ;;; (define-class a (b c))
1235 ;;; => cpl (a) = a b d c e f object top
1238 (define-method (compute-cpl (class <class>))
1239 (compute-std-cpl class class-direct-supers))
1243 (define (only-non-null lst)
1244 (filter (lambda (l) (not (null? l))) lst))
1246 (define (compute-std-cpl c get-direct-supers)
1247 (let ((c-direct-supers (get-direct-supers c)))
1248 (merge-lists (list c)
1249 (only-non-null (append (map class-precedence-list
1251 (list c-direct-supers))))))
1253 (define (merge-lists reversed-partial-result inputs)
1255 ((every null? inputs)
1256 (reverse! reversed-partial-result))
1258 (let* ((candidate (lambda (c)
1259 (and (not (any (lambda (l)
1263 (candidate-car (lambda (l)
1264 (and (not (null? l))
1265 (candidate (car l)))))
1266 (next (any candidate-car inputs)))
1268 (goops-error "merge-lists: Inconsistent precedence graph"))
1269 (let ((remove-next (lambda (l)
1270 (if (eq? (car l) next)
1273 (merge-lists (cons next reversed-partial-result)
1274 (only-non-null (map remove-next inputs))))))))
1276 ;; Modified from TinyClos:
1278 ;; A simple topological sort.
1280 ;; It's in this file so that both TinyClos and Objects can use it.
1282 ;; This is a fairly modified version of code I originally got from Anurag
1283 ;; Mendhekar <anurag@moose.cs.indiana.edu>.
1286 (define (compute-clos-cpl c get-direct-supers)
1287 (top-sort ((build-transitive-closure get-direct-supers) c)
1288 ((build-constraints get-direct-supers) c)
1289 (std-tie-breaker get-direct-supers)))
1292 (define (top-sort elements constraints tie-breaker)
1293 (let loop ((elements elements)
1294 (constraints constraints)
1296 (if (null? elements)
1298 (let ((can-go-in-now
1301 (every (lambda (constraint)
1302 (or (not (eq? (cadr constraint) x))
1303 (memq (car constraint) result)))
1306 (if (null? can-go-in-now)
1307 (goops-error "top-sort: Invalid constraints")
1308 (let ((choice (if (null? (cdr can-go-in-now))
1313 (filter (lambda (x) (not (eq? x choice)))
1316 (append result (list choice)))))))))
1318 (define (std-tie-breaker get-supers)
1319 (lambda (partial-cpl min-elts)
1320 (let loop ((pcpl (reverse partial-cpl)))
1321 (let ((current-elt (car pcpl)))
1322 (let ((ds-of-ce (get-supers current-elt)))
1323 (let ((common (filter (lambda (x)
1327 (if (null? (cdr pcpl))
1328 (goops-error "std-tie-breaker: Nothing valid")
1333 (define (build-transitive-closure get-follow-ons)
1335 (let track ((result '())
1339 (let ((next (car pending)))
1340 (if (memq next result)
1341 (track result (cdr pending))
1342 (track (cons next result)
1343 (append (get-follow-ons next)
1344 (cdr pending)))))))))
1346 (define (build-constraints get-follow-ons)
1348 (let loop ((elements ((build-transitive-closure get-follow-ons) x))
1351 (if (or (null? this-one) (null? (cdr this-one)))
1352 (if (null? elements)
1354 (loop (cdr elements)
1355 (cons (car elements)
1356 (get-follow-ons (car elements)))
1360 (cons (list (car this-one) (cadr this-one))
1363 ;;; compute-get-n-set
1365 (define-method (compute-get-n-set (class <class>) s)
1366 (case (slot-definition-allocation s)
1367 ((#:instance) ;; Instance slot
1368 ;; get-n-set is just its offset
1369 (let ((already-allocated (slot-ref class 'nfields)))
1370 (slot-set! class 'nfields (+ already-allocated 1))
1373 ((#:class) ;; Class slot
1374 ;; Class-slots accessors are implemented as 2 closures around
1375 ;; a Scheme variable. As instance slots, class slots must be
1376 ;; unbound at init time.
1377 (let ((name (slot-definition-name s)))
1378 (if (memq name (map slot-definition-name (class-direct-slots class)))
1379 ;; This slot is direct; create a new shared variable
1380 (make-closure-variable class)
1381 ;; Slot is inherited. Find its definition in superclass
1382 (let loop ((l (cdr (class-precedence-list class))))
1383 (let ((r (assoc name (slot-ref (car l) 'getters-n-setters))))
1386 (loop (cdr l))))))))
1388 ((#:each-subclass) ;; slot shared by instances of direct subclass.
1389 ;; (Thomas Buerger, April 1998)
1390 (make-closure-variable class))
1392 ((#:virtual) ;; No allocation
1393 ;; slot-ref and slot-set! function must be given by the user
1394 (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
1395 (set (get-keyword #:slot-set! (slot-definition-options s) #f)))
1396 (if (not (and get set))
1397 (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S"
1400 (else (next-method))))
1402 (define (make-closure-variable class)
1403 (let ((shared-variable (make-unbound)))
1404 (list (lambda (o) shared-variable)
1405 (lambda (o v) (set! shared-variable v)))))
1407 (define-method (compute-get-n-set (o <object>) s)
1408 (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
1410 (define-method (compute-slots (class <class>))
1411 (%compute-slots class))
1417 (define-method (initialize (object <object>) initargs)
1418 (%initialize-object object initargs))
1420 (define-method (initialize (class <class>) initargs)
1422 (let ((dslots (get-keyword #:slots initargs '()))
1423 (supers (get-keyword #:dsupers initargs '())))
1424 (slot-set! class 'name (get-keyword #:name initargs '???))
1425 (slot-set! class 'direct-supers supers)
1426 (slot-set! class 'direct-slots dslots)
1427 (slot-set! class 'direct-subclasses '())
1428 (slot-set! class 'direct-methods '())
1429 (slot-set! class 'cpl (compute-cpl class))
1430 (slot-set! class 'redefined #f)
1431 (let ((slots (compute-slots class)))
1432 (slot-set! class 'slots slots)
1433 (slot-set! class 'nfields 0)
1434 (slot-set! class 'getters-n-setters (compute-getters-n-setters class
1436 ;; Build getters - setters - accessors
1437 (compute-slot-accessors class slots))
1439 ;; Update the "direct-subclasses" of each inherited classes
1440 (for-each (lambda (x)
1443 (cons class (slot-ref x 'direct-subclasses))))
1446 ;; Support for the underlying structs:
1448 ;; Set the layout slot
1449 (%prep-layout! class)
1450 ;; Inherit class flags (invisible on scheme level) from supers
1451 (%inherit-magic! class supers)))
1453 (define (initialize-object-procedure object initargs)
1454 (let ((proc (get-keyword #:procedure initargs #f)))
1457 (apply set-object-procedure! object proc))
1459 (set-object-procedure! object proc)))))
1461 (define-method (initialize (applicable-struct <applicable-struct>) initargs)
1463 (initialize-object-procedure applicable-struct initargs))
1465 (define-method (initialize (generic <generic>) initargs)
1466 (let ((previous-definition (get-keyword #:default initargs #f))
1467 (name (get-keyword #:name initargs #f)))
1469 (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
1471 (apply previous-definition args)))
1474 (set-procedure-property! generic 'name name))
1477 (define-method (initialize (gws <generic-with-setter>) initargs)
1479 (%set-object-setter! gws (get-keyword #:setter initargs #f)))
1481 (define-method (initialize (eg <extended-generic>) initargs)
1483 (slot-set! eg 'extends (get-keyword #:extends initargs '())))
1485 (define dummy-procedure (lambda args *unspecified*))
1487 (define-method (initialize (method <method>) initargs)
1489 (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
1490 (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
1491 (slot-set! method 'procedure
1492 (get-keyword #:procedure initargs #f))
1493 (slot-set! method 'formals (get-keyword #:formals initargs '()))
1494 (slot-set! method 'body (get-keyword #:body initargs '()))
1495 (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f)))
1502 (define (change-object-class old-instance old-class new-class)
1503 (let ((new-instance (allocate-instance new-class '())))
1504 ;; Initialize the slots of the new instance
1505 (for-each (lambda (slot)
1506 (if (and (slot-exists-using-class? old-class old-instance slot)
1507 (eq? (slot-definition-allocation
1508 (class-slot-definition old-class slot))
1510 (slot-bound-using-class? old-class old-instance slot))
1511 ;; Slot was present and allocated in old instance; copy it
1512 (slot-set-using-class!
1516 (slot-ref-using-class old-class old-instance slot))
1517 ;; slot was absent; initialize it with its default value
1518 (let ((init (slot-init-function new-class slot)))
1520 (slot-set-using-class!
1524 (apply init '()))))))
1525 (map slot-definition-name (class-slots new-class)))
1526 ;; Exchange old and new instance in place to keep pointers valid
1527 (%modify-instance old-instance new-instance)
1528 ;; Allow class specific updates of instances (which now are swapped)
1529 (update-instance-for-different-class new-instance old-instance)
1533 (define-method (update-instance-for-different-class (old-instance <object>)
1536 ;;not really important what we do, we just need a default method
1539 (define-method (change-class (old-instance <object>) (new-class <class>))
1540 (change-object-class old-instance (class-of old-instance) new-class))
1545 ;;; A new definition which overwrites the previous one which was built-in
1548 (define-method (allocate-instance (class <class>) initargs)
1549 (%allocate-instance class initargs))
1551 (define-method (make-instance (class <class>) . initargs)
1552 (let ((instance (allocate-instance class initargs)))
1553 (initialize instance initargs)
1556 (define make make-instance)
1561 ;;; Protocol for calling standard generic functions. This protocol is
1562 ;;; not used for real <generic> functions (in this case we use a
1563 ;;; completely C hard-coded protocol). Apply-generic is used by
1564 ;;; goops for calls to subclasses of <generic> and <generic-with-setter>.
1565 ;;; The code below is similar to the first MOP described in AMOP. In
1566 ;;; particular, it doesn't used the currified approach to gf
1567 ;;; call. There are 2 reasons for that:
1568 ;;; - the protocol below is exposed to mimic completely the one written in C
1569 ;;; - the currified protocol would be imho inefficient in C.
1572 (define-method (apply-generic (gf <generic>) args)
1573 (if (null? (slot-ref gf 'methods))
1574 (no-method gf args))
1575 (let ((methods (compute-applicable-methods gf args)))
1577 (apply-methods gf (sort-applicable-methods gf methods args) args)
1578 (no-applicable-method gf args))))
1580 ;; compute-applicable-methods is bound to %compute-applicable-methods.
1582 (define %%compute-applicable-methods
1583 (make <generic> #:name 'compute-applicable-methods))
1585 (define-method (%%compute-applicable-methods (gf <generic>) args)
1586 (%compute-applicable-methods gf args))
1588 (set! compute-applicable-methods %%compute-applicable-methods)
1590 (define-method (sort-applicable-methods (gf <generic>) methods args)
1591 (let ((targs (map class-of args)))
1592 (sort methods (lambda (m1 m2) (method-more-specific? m1 m2 targs)))))
1594 (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
1595 (%method-more-specific? m1 m2 targs))
1597 (define-method (apply-method (gf <generic>) methods build-next args)
1598 (apply (method-procedure (car methods))
1599 (build-next (cdr methods) args)
1602 (define-method (apply-methods (gf <generic>) (l <list>) args)
1603 (letrec ((next (lambda (procs args)
1605 (let ((a (if (null? new-args) args new-args)))
1607 (no-next-method gf a)
1608 (apply-method gf procs next a)))))))
1609 (apply-method gf l next args)))
1611 ;; We don't want the following procedure to turn up in backtraces:
1612 (for-each (lambda (proc)
1613 (set-procedure-property! proc 'system-procedure #t))
1617 no-applicable-method
1622 ;;; {<composite-metaclass> and <active-metaclass>}
1625 ;(autoload "active-slot" <active-metaclass>)
1626 ;(autoload "composite-slot" <composite-metaclass>)
1627 ;(export <composite-metaclass> <active-metaclass>)
1635 ;; duplicate the standard list->set function but using eq instead of
1636 ;; eqv which really sucks a lot, uselessly here
1638 (define (list2set l)
1643 ((memq (car l) res) (loop (cdr l) res))
1644 (else (loop (cdr l) (cons (car l) res))))))
1646 (define (class-subclasses c)
1647 (letrec ((allsubs (lambda (c)
1648 (cons c (mapappend allsubs
1649 (class-direct-subclasses c))))))
1650 (list2set (cdr (allsubs c)))))
1652 (define (class-methods c)
1653 (list2set (mapappend class-direct-methods
1654 (cons c (class-subclasses c)))))
1657 ;;; {Final initialization}
1660 ;; Tell C code that the main bulk of Goops has been loaded