3 ;;;; Copyright (C) 1998,1999,2000,2001,2002, 2003, 2006 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 2.1 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
69 class-slots class-environment
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)
76 :replace (<class> <operator-class> <entity-class> <entity>)
79 (define *goops-module* (current-module))
81 ;; First initialize the builtin part of GOOPS
82 (eval-when (eval load compile)
83 (%init-goops-builtins))
85 ;; Then load the rest of GOOPS
86 (use-modules (oop goops util)
91 (eval-when (eval load compile)
92 (define min-fixnum (- (expt 2 29)))
93 (define max-fixnum (- (expt 2 29) 1)))
98 (define (goops-error format-string . args)
100 (scm-error 'goops-error #f format-string args '()))
105 (define (is-a? obj class)
106 (and (memq class (class-precedence-list (class-of obj))) #t))
113 (define ensure-metaclass-with-supers
114 (let ((table-of-metas '()))
115 (lambda (meta-supers)
116 (let ((entry (assoc meta-supers table-of-metas)))
118 ;; Found a previously created metaclass
120 ;; Create a new meta-class which inherit from "meta-supers"
121 (let ((new (make <class> #:dsupers meta-supers
123 #:name (gensym "metaclass"))))
124 (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
127 (define (ensure-metaclass supers env)
130 (let* ((all-metas (map (lambda (x) (class-of x)) supers))
131 (all-cpls (append-map (lambda (m)
132 (cdr (class-precedence-list m)))
135 ;; Find the most specific metaclasses. The new metaclass will be
136 ;; a subclass of these.
139 (if (and (not (member meta all-cpls))
140 (not (member meta needed-metas)))
141 (set! needed-metas (append needed-metas (list meta)))))
143 ;; Now return a subclass of the metaclasses we found.
144 (if (null? (cdr needed-metas))
145 (car needed-metas) ; If there's only one, just use it.
146 (ensure-metaclass-with-supers needed-metas)))))
152 ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
154 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
155 ;;; OPTION ::= KEYWORD VALUE
158 (define (kw-do-map mapper f kwargs)
162 ((or (null? (cdr l)) (not (keyword? (car l))))
163 (goops-error "malformed keyword arguments: ~a" kwargs))
164 (else (cons (car l) (keywords (cddr l))))))
166 (if (null? l) '() (cons (cadr l) (args (cddr l)))))
167 ;; let* to check keywords first
168 (let* ((k (keywords kwargs))
172 (define (make-class supers slots . options)
173 (let ((env (or (get-keyword #:environment options #f)
175 (let* ((name (get-keyword #:name options (make-unbound)))
176 (supers (if (not (or-map (lambda (class)
178 (class-precedence-list class)))
180 (append supers (list <object>))
182 (metaclass (or (get-keyword #:metaclass options #f)
183 (ensure-metaclass supers env))))
185 ;; Verify that all direct slots are different and that we don't inherit
186 ;; several time from the same class
187 (let ((tmp1 (find-duplicate supers))
188 (tmp2 (find-duplicate (map slot-definition-name slots))))
190 (goops-error "make-class: super class ~S is duplicate in class ~S"
193 (goops-error "make-class: slot ~S is duplicate in class ~S"
196 ;; Everything seems correct, build the class
197 (apply make metaclass
204 ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
206 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
207 ;;; OPTION ::= KEYWORD VALUE
209 (define-macro (class supers . slots)
210 (define (make-slot-definition-forms slots)
216 ,@(kw-do-map append-map
221 #:init-thunk (lambda () ,arg)))
222 (else (list kw arg))))
227 (if (not (list? supers))
228 (goops-error "malformed superclass list: ~S" supers))
229 (let ((slot-defs (cons #f '()))
230 (slots (take-while (lambda (x) (not (keyword? x))) slots))
231 (options (or (find-tail keyword? slots) '())))
233 ;; evaluate super class variables
235 ;; evaluate slot definitions, except the slot name!
236 (list ,@(make-slot-definition-forms slots))
237 ;; evaluate class options
240 (define-syntax define-class-pre-definition
243 ((_ (k arg rest ...) out ...)
244 (keyword? (syntax->datum (syntax k)))
245 (case (syntax->datum (syntax k))
248 (define-class-pre-definition (rest ...)
250 (if (or (not (defined? 'arg))
251 (not (is-a? arg <generic>)))
254 (ensure-generic (if (defined? 'arg) arg #f) 'arg))))))
257 (define-class-pre-definition (rest ...)
259 (if (or (not (defined? 'arg))
260 (not (is-a? arg <accessor>)))
263 (ensure-accessor (if (defined? 'arg) arg #f) 'arg))))))
266 (define-class-pre-definition (rest ...) out ...)))))
268 (syntax (begin out ...))))))
270 ;; Some slot options require extra definitions to be made. In
271 ;; particular, we want to make sure that the generic function objects
272 ;; which represent accessors exist before `make-class' tries to add
274 (define-syntax define-class-pre-definitions
278 (syntax (begin out ...)))
279 ((_ (slot rest ...) out ...)
280 (keyword? (syntax->datum (syntax slot)))
281 (syntax (begin out ...)))
282 ((_ (slot rest ...) out ...)
283 (identifier? (syntax slot))
284 (syntax (define-class-pre-definitions (rest ...)
286 ((_ ((slotname slotopt ...) rest ...) out ...)
287 (syntax (define-class-pre-definitions (rest ...)
288 out ... (define-class-pre-definition (slotopt ...))))))))
290 (define-syntax define-class
292 ((_ name supers slot ...)
294 (define-class-pre-definitions (slot ...))
295 (if (and (defined? 'name)
297 (memq <object> (class-precedence-list name)))
298 (class-redefinition name
299 (class supers slot ... #:name 'name))
300 (toplevel-define! 'name (class supers slot ... #:name 'name)))))))
302 (define-syntax standard-define-class
304 ((_ arg ...) (define-class arg ...))))
307 ;;; {Generic functions and accessors}
310 ;; Apparently the desired semantics are that we extend previous
311 ;; procedural definitions, but that if `name' was already a generic, we
312 ;; overwrite its definition.
313 (define-macro (define-generic name)
314 (if (not (symbol? name))
315 (goops-error "bad generic function name: ~S" name))
317 (if (and (defined? ',name) (is-a? ,name <generic>))
318 (make <generic> #:name ',name)
319 (ensure-generic (if (defined? ',name) ,name #f) ',name))))
321 (define-macro (define-extended-generic name val)
322 (if (not (symbol? name))
323 (goops-error "bad generic function name: ~S" name))
324 `(define ,name (make-extended-generic ,val ',name)))
326 (define-macro (define-extended-generics names . args)
327 (let ((prefixes (get-keyword #:prefix args #f)))
330 ,@(map (lambda (name)
331 `(define-extended-generic ,name
332 (list ,@(map (lambda (prefix)
333 (symbol-append prefix name))
336 (goops-error "no prefixes supplied"))))
338 (define (make-generic . name)
339 (let ((name (and (pair? name) (car name))))
340 (make <generic> #:name name)))
342 (define (make-extended-generic gfs . name)
343 (let* ((name (and (pair? name) (car name)))
344 (gfs (if (pair? gfs) gfs (list gfs)))
345 (gws? (any (lambda (gf) (is-a? gf <generic-with-setter>)) gfs)))
347 (let* ((sname (and name (make-setter-name name)))
349 (append-map (lambda (gf)
350 (if (is-a? gf <generic-with-setter>)
351 (list (ensure-generic (setter gf)
355 (es (make <extended-generic-with-setter>
358 #:setter (make <extended-generic>
360 #:extends setters))))
361 (extended-by! setters (setter es))
363 (make <extended-generic>
366 (extended-by! gfs ans)
369 (define (extended-by! gfs eg)
370 (for-each (lambda (gf)
371 (slot-set! gf 'extended-by
372 (cons eg (slot-ref gf 'extended-by))))
375 (define (not-extended-by! gfs eg)
376 (for-each (lambda (gf)
377 (slot-set! gf 'extended-by
378 (delq! eg (slot-ref gf 'extended-by))))
381 (define (ensure-generic old-definition . name)
382 (let ((name (and (pair? name) (car name))))
383 (cond ((is-a? old-definition <generic>) old-definition)
384 ((procedure-with-setter? old-definition)
385 (make <generic-with-setter>
387 #:default (procedure old-definition)
388 #:setter (setter old-definition)))
389 ((procedure? old-definition)
390 (make <generic> #:name name #:default old-definition))
391 (else (make <generic> #:name name)))))
393 ;; same semantics as <generic>
394 (define-syntax define-accessor
398 (cond ((not (defined? 'name)) (ensure-accessor #f 'name))
399 ((is-a? name <accessor>) (make <accessor> #:name 'name))
400 (else (ensure-accessor name 'name)))))))
402 (define (make-setter-name name)
403 (string->symbol (string-append "setter:" (symbol->string name))))
405 (define (make-accessor . name)
406 (let ((name (and (pair? name) (car name))))
409 #:setter (make <generic>
410 #:name (and name (make-setter-name name))))))
412 (define (ensure-accessor proc . name)
413 (let ((name (and (pair? name) (car name))))
414 (cond ((and (is-a? proc <accessor>)
415 (is-a? (setter proc) <generic>))
417 ((is-a? proc <generic-with-setter>)
418 (upgrade-accessor proc (setter proc)))
419 ((is-a? proc <generic>)
420 (upgrade-accessor proc (make-generic name)))
421 ((procedure-with-setter? proc)
424 #:default (procedure proc)
425 #:setter (ensure-generic (setter proc) name)))
427 (ensure-accessor (ensure-generic proc name) name))
429 (make-accessor name)))))
431 (define (upgrade-accessor generic setter)
432 (let ((methods (slot-ref generic 'methods))
433 (gws (make (if (is-a? generic <extended-generic>)
434 <extended-generic-with-setter>
436 #:name (generic-function-name generic)
437 #:extended-by (slot-ref generic 'extended-by)
439 (if (is-a? generic <extended-generic>)
440 (let ((gfs (slot-ref generic 'extends)))
441 (not-extended-by! gfs generic)
442 (slot-set! gws 'extends gfs)
443 (extended-by! gfs gws)))
445 (for-each (lambda (method)
446 (slot-set! method 'generic-function gws))
448 (slot-set! gws 'methods methods)
455 (define (toplevel-define! name val)
456 (module-define! (current-module) name val))
458 (define-syntax define-method
459 (syntax-rules (setter)
460 ((_ ((setter name) . args) body ...)
462 (if (or (not (defined? 'name))
463 (not (is-a? name <accessor>)))
464 (toplevel-define! 'name
466 (if (defined? 'name) name #f) 'name)))
467 (add-method! (setter name) (method args body ...))))
468 ((_ (name . args) body ...)
470 ;; FIXME: this code is how it always was, but it's quite cracky:
471 ;; it will only define the generic function if it was undefined
472 ;; before (ok), or *was defined to #f*. The latter is crack. But
473 ;; there are bootstrap issues about fixing this -- change it to
474 ;; (is-a? name <generic>) and see.
475 (if (or (not (defined? 'name))
477 (toplevel-define! 'name (make <generic> #:name 'name)))
478 (add-method! name (method args body ...))))))
480 (define-syntax method
482 (define (parse-args args)
483 (let lp ((ls args) (formals '()) (specializers '()))
486 (and (identifier? (syntax f)) (identifier? (syntax s)))
488 (cons (syntax f) formals)
489 (cons (syntax s) specializers)))
491 (identifier? (syntax f))
493 (cons (syntax f) formals)
494 (cons (syntax <top>) specializers)))
496 (list (reverse formals)
497 (reverse (cons (syntax '()) specializers))))
499 (identifier? (syntax tail))
500 (list (append (reverse formals) (syntax tail))
501 (reverse (cons (syntax <top>) specializers)))))))
503 (define (find-free-id exp referent)
506 (or (find-free-id (syntax x) referent)
507 (find-free-id (syntax y) referent)))
509 (identifier? (syntax x))
510 (let ((id (datum->syntax (syntax x) referent)))
511 (and (free-identifier=? (syntax x) id) id)))
514 (define (compute-procedure formals body)
517 (with-syntax ((formals formals))
518 (syntax (lambda formals body0 ...))))))
520 (define (->proper args)
521 (let lp ((ls args) (out '()))
523 ((x . xs) (lp (syntax xs) (cons (syntax x) out)))
525 (tail (reverse (cons (syntax tail) out))))))
527 (define (compute-make-procedure formals body next-method)
530 (with-syntax ((next-method next-method))
531 (syntax-case formals ()
534 (lambda (real-next-method)
536 (let ((next-method (lambda args
538 (real-next-method formal ...)
539 (apply real-next-method args)))))
542 (with-syntax (((formal ...) (->proper (syntax formals))))
544 (lambda (real-next-method)
546 (let ((next-method (lambda args
548 (apply real-next-method formal ...)
549 (apply real-next-method args)))))
552 (define (compute-procedures formals body)
553 ;; So, our use of this is broken, because it operates on the
554 ;; pre-expansion source code. It's equivalent to just searching
555 ;; for referent in the datums. Ah well.
556 (let ((id (find-free-id body 'next-method)))
558 ;; return a make-procedure
560 (compute-make-procedure formals body id))
561 (values (compute-procedure formals body)
565 ((_ args) (syntax (method args (if #f #f))))
566 ((_ args body0 body1 ...)
567 (with-syntax (((formals (specializer ...)) (parse-args (syntax args))))
570 (compute-procedures (syntax formals) (syntax (body0 body1 ...))))
571 (lambda (procedure make-procedure)
572 (with-syntax ((procedure procedure)
573 (make-procedure make-procedure))
576 #:specializers (cons* specializer ...)
578 #:body '(body0 body1 ...)
579 #:make-procedure make-procedure
580 #:procedure procedure))))))))))
586 (define (add-method-in-classes! m)
587 ;; Add method in all the classes which appears in its specializers list
588 (for-each* (lambda (x)
589 (let ((dm (class-direct-methods x)))
590 (if (not (memq m dm))
591 (slot-set! x 'direct-methods (cons m dm)))))
592 (method-specializers m)))
594 (define (remove-method-in-classes! m)
595 ;; Remove method in all the classes which appears in its specializers list
596 (for-each* (lambda (x)
599 (delv! m (class-direct-methods x))))
600 (method-specializers m)))
602 (define (compute-new-list-of-methods gf new)
603 (let ((new-spec (method-specializers new))
604 (methods (slot-ref gf 'methods)))
605 (let loop ((l methods))
608 (if (equal? (method-specializers (car l)) new-spec)
610 ;; This spec. list already exists. Remove old method from dependents
611 (remove-method-in-classes! (car l))
616 (define internal-add-method!
617 (method ((gf <generic>) (m <method>))
618 (slot-set! m 'generic-function gf)
619 (slot-set! gf 'methods (compute-new-list-of-methods gf m))
620 (let ((specializers (slot-ref m 'specializers)))
621 (slot-set! gf 'n-specialized
622 (max (length* specializers)
623 (slot-ref gf 'n-specialized))))
624 (%invalidate-method-cache! gf)
625 (add-method-in-classes! m)
628 (define-generic add-method!)
630 ((method-procedure internal-add-method!) add-method! internal-add-method!)
632 (define-method (add-method! (proc <procedure>) (m <method>))
633 (if (generic-capability? proc)
635 (enable-primitive-generic! proc)
636 (add-method! proc m))
639 (define-method (add-method! (pg <primitive-generic>) (m <method>))
640 (add-method! (primitive-generic-generic pg) m))
642 (define-method (add-method! obj (m <method>))
643 (goops-error "~S is not a valid generic function" obj))
646 ;;; {Access to meta objects}
652 (define-method (method-source (m <method>))
653 (let* ((spec (map* class-name (slot-ref m 'specializers)))
654 (src (procedure-source (slot-ref m 'procedure))))
656 (let ((args (cadr src))
659 (cons (map* list args spec)
662 (define-method (method-formals (m <method>))
663 (slot-ref m 'formals))
668 (define slot-definition-name car)
670 (define slot-definition-options cdr)
672 (define (slot-definition-allocation s)
673 (get-keyword #:allocation (cdr s) #:instance))
675 (define (slot-definition-getter s)
676 (get-keyword #:getter (cdr s) #f))
678 (define (slot-definition-setter s)
679 (get-keyword #:setter (cdr s) #f))
681 (define (slot-definition-accessor s)
682 (get-keyword #:accessor (cdr s) #f))
684 (define (slot-definition-init-value s)
685 ;; can be #f, so we can't use #f as non-value
686 (get-keyword #:init-value (cdr s) (make-unbound)))
688 (define (slot-definition-init-form s)
689 (get-keyword #:init-form (cdr s) (make-unbound)))
691 (define (slot-definition-init-thunk s)
692 (get-keyword #:init-thunk (cdr s) #f))
694 (define (slot-definition-init-keyword s)
695 (get-keyword #:init-keyword (cdr s) #f))
697 (define (class-slot-definition class slot-name)
698 (assq slot-name (class-slots class)))
700 (define (slot-init-function class slot-name)
701 (cadr (assq slot-name (slot-ref class 'getters-n-setters))))
705 ;;; {Standard methods used by the C runtime}
708 ;;; Methods to compare objects
711 (define-method (eqv? x y) #f)
712 (define-method (equal? x y) (eqv? x y))
715 ;;; methods to display/write an object
718 ; Code for writing objects must test that the slots they use are
719 ; bound. Otherwise a slot-unbound method will be called and will
720 ; conduct to an infinite loop.
723 (define (display-address o file)
724 (display (number->string (object-address o) 16) file))
726 (define-method (write o file)
727 (display "#<instance " file)
728 (display-address o file)
731 (define write-object (primitive-generic-generic write))
733 (define-method (write (o <object>) file)
734 (let ((class (class-of o)))
735 (if (slot-bound? class 'name)
738 (display (class-name class) file)
739 (display #\space file)
740 (display-address o file)
744 (define-method (write (o <foreign-object>) file)
745 (let ((class (class-of o)))
746 (if (slot-bound? class 'name)
748 (display "#<foreign-object " file)
749 (display (class-name class) file)
750 (display #\space file)
751 (display-address o file)
755 (define-method (write (class <class>) file)
756 (let ((meta (class-of class)))
757 (if (and (slot-bound? class 'name)
758 (slot-bound? meta 'name))
761 (display (class-name meta) file)
762 (display #\space file)
763 (display (class-name class) file)
764 (display #\space file)
765 (display-address class file)
769 (define-method (write (gf <generic>) file)
770 (let ((meta (class-of gf)))
771 (if (and (slot-bound? meta 'name)
772 (slot-bound? gf 'methods))
775 (display (class-name meta) file)
776 (let ((name (generic-function-name gf)))
779 (display #\space file)
780 (display name file))))
782 (display (length (generic-function-methods gf)) file)
786 (define-method (write (o <method>) file)
787 (let ((meta (class-of o)))
788 (if (and (slot-bound? meta 'name)
789 (slot-bound? o 'specializers))
792 (display (class-name meta) file)
793 (display #\space file)
794 (display (map* (lambda (spec)
795 (if (slot-bound? spec 'name)
796 (slot-ref spec 'name)
798 (method-specializers o))
800 (display #\space file)
801 (display-address o file)
805 ;; Display (do the same thing as write by default)
806 (define-method (display o file)
807 (write-object o file))
810 ;;; Handling of duplicate bindings in the module system
813 (define-method (merge-generics (module <module>)
823 (define-method (merge-generics (module <module>)
831 (and (not (eq? val1 val2))
832 (make-variable (make-extended-generic (list val2 val1) name))))
834 (define-method (merge-generics (module <module>)
841 (gf <extended-generic>))
842 (and (not (memq val2 (slot-ref gf 'extends)))
846 (cons val2 (delq! val2 (slot-ref gf 'extends))))
849 (cons gf (delq! gf (slot-ref val2 'extended-by))))
852 (module-define! duplicate-handlers 'merge-generics merge-generics)
854 (define-method (merge-accessors (module <module>)
864 (define-method (merge-accessors (module <module>)
872 (merge-generics module name int1 val1 int2 val2 var val))
874 (module-define! duplicate-handlers 'merge-accessors merge-accessors)
880 (define (class-slot-g-n-s class slot-name)
881 (let* ((this-slot (assq slot-name (slot-ref class 'slots)))
882 (g-n-s (cddr (or (assq slot-name (slot-ref class 'getters-n-setters))
883 (slot-missing class slot-name)))))
884 (if (not (memq (slot-definition-allocation this-slot)
885 '(#:class #:each-subclass)))
886 (slot-missing class slot-name))
889 (define (class-slot-ref class slot)
890 (let ((x ((car (class-slot-g-n-s class slot)) #f)))
892 (slot-unbound class slot)
895 (define (class-slot-set! class slot value)
896 ((cadr (class-slot-g-n-s class slot)) #f value))
898 (define-method (slot-unbound (c <class>) (o <object>) s)
899 (goops-error "Slot `~S' is unbound in object ~S" s o))
901 (define-method (slot-unbound (c <class>) s)
902 (goops-error "Slot `~S' is unbound in class ~S" s c))
904 (define-method (slot-unbound (o <object>))
905 (goops-error "Unbound slot in object ~S" o))
907 (define-method (slot-missing (c <class>) (o <object>) s)
908 (goops-error "No slot with name `~S' in object ~S" s o))
910 (define-method (slot-missing (c <class>) s)
911 (goops-error "No class slot with name `~S' in class ~S" s c))
914 (define-method (slot-missing (c <class>) (o <object>) s value)
915 (slot-missing c o s))
917 ;;; Methods for the possible error we can encounter when calling a gf
919 (define-method (no-next-method (gf <generic>) args)
920 (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
922 (define-method (no-applicable-method (gf <generic>) args)
923 (goops-error "No applicable method for ~S in call ~S"
924 gf (cons (generic-function-name gf) args)))
926 (define-method (no-method (gf <generic>) args)
927 (goops-error "No method defined for ~S" gf))
930 ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
933 (define-method (shallow-clone (self <object>))
934 (let ((clone (%allocate-instance (class-of self) '()))
935 (slots (map slot-definition-name
936 (class-slots (class-of self)))))
937 (for-each (lambda (slot)
938 (if (slot-bound? self slot)
939 (slot-set! clone slot (slot-ref self slot))))
943 (define-method (deep-clone (self <object>))
944 (let ((clone (%allocate-instance (class-of self) '()))
945 (slots (map slot-definition-name
946 (class-slots (class-of self)))))
947 (for-each (lambda (slot)
948 (if (slot-bound? self slot)
949 (slot-set! clone slot
950 (let ((value (slot-ref self slot)))
951 (if (instance? value)
958 ;;; {Class redefinition utilities}
961 ;;; (class-redefinition OLD NEW)
964 ;;; Has correct the following conditions:
968 ;;; 1. New accessor specializers refer to new header
972 ;;; 1. New class cpl refers to the new class header
973 ;;; 2. Old class header exists on old super classes direct-subclass lists
974 ;;; 3. New class header exists on new super classes direct-subclass lists
976 (define-method (class-redefinition (old <class>) (new <class>))
977 ;; Work on direct methods:
978 ;; 1. Remove accessor methods from the old class
979 ;; 2. Patch the occurences of new in the specializers by old
980 ;; 3. Displace the methods from old to new
981 (remove-class-accessors! old) ;; -1-
982 (let ((methods (class-direct-methods new)))
983 (for-each (lambda (m)
984 (update-direct-method! m new old)) ;; -2-
988 (append methods (class-direct-methods old))))
990 ;; Substitute old for new in new cpl
991 (set-car! (slot-ref new 'cpl) old)
993 ;; Remove the old class from the direct-subclasses list of its super classes
994 (for-each (lambda (c) (slot-set! c 'direct-subclasses
995 (delv! old (class-direct-subclasses c))))
996 (class-direct-supers old))
998 ;; Replace the new class with the old in the direct-subclasses of the supers
999 (for-each (lambda (c)
1000 (slot-set! c 'direct-subclasses
1001 (cons old (delv! new (class-direct-subclasses c)))))
1002 (class-direct-supers new))
1004 ;; Swap object headers
1005 (%modify-class old new)
1009 ;; Redefine all the subclasses of old to take into account modification
1012 (update-direct-subclass! c new old))
1013 (class-direct-subclasses new))
1015 ;; Invalidate class so that subsequent instances slot accesses invoke
1016 ;; change-object-class
1017 (slot-set! new 'redefined old)
1018 (%invalidate-class new) ;must come after slot-set!
1023 ;;; remove-class-accessors!
1026 (define-method (remove-class-accessors! (c <class>))
1027 (for-each (lambda (m)
1028 (if (is-a? m <accessor-method>)
1029 (let ((gf (slot-ref m 'generic-function)))
1030 ;; remove the method from its GF
1031 (slot-set! gf 'methods
1032 (delq1! m (slot-ref gf 'methods)))
1033 (%invalidate-method-cache! gf)
1034 ;; remove the method from its specializers
1035 (remove-method-in-classes! m))))
1036 (class-direct-methods c)))
1039 ;;; update-direct-method!
1042 (define-method (update-direct-method! (m <method>)
1045 (let loop ((l (method-specializers m)))
1046 ;; Note: the <top> in dotted list is never used.
1047 ;; So we can work as if we had only proper lists.
1050 (if (eqv? (car l) old)
1055 ;;; update-direct-subclass!
1058 (define-method (update-direct-subclass! (c <class>)
1061 (class-redefinition c
1062 (make-class (class-direct-supers c)
1063 (class-direct-slots c)
1064 #:name (class-name c)
1065 #:environment (slot-ref c 'environment)
1066 #:metaclass (class-of c))))
1069 ;;; {Utilities for INITIALIZE methods}
1072 ;;; compute-slot-accessors
1074 (define (compute-slot-accessors class slots env)
1077 (let ((name (slot-definition-name s))
1078 (getter-function (slot-definition-getter s))
1079 (setter-function (slot-definition-setter s))
1080 (accessor (slot-definition-accessor s)))
1082 (add-method! getter-function
1083 (compute-getter-method class g-n-s)))
1085 (add-method! setter-function
1086 (compute-setter-method class g-n-s)))
1089 (add-method! accessor
1090 (compute-getter-method class g-n-s))
1091 (add-method! (setter accessor)
1092 (compute-setter-method class g-n-s))))))
1093 slots (slot-ref class 'getters-n-setters)))
1095 (define-method (compute-getter-method (class <class>) slotdef)
1096 (let ((init-thunk (cadr slotdef))
1097 (g-n-s (cddr slotdef)))
1098 (make <accessor-method>
1099 #:specializers (list class)
1100 #:procedure (cond ((pair? g-n-s)
1101 (make-generic-bound-check-getter (car g-n-s)))
1103 (standard-get g-n-s))
1105 (bound-check-get g-n-s)))
1106 #:slot-definition slotdef)))
1108 (define-method (compute-setter-method (class <class>) slotdef)
1109 (let ((g-n-s (cddr slotdef)))
1110 (make <accessor-method>
1111 #:specializers (list class <top>)
1112 #:procedure (if (pair? g-n-s)
1114 (standard-set g-n-s))
1115 #:slot-definition slotdef)))
1117 (define (make-generic-bound-check-getter proc)
1118 (let ((source (and (closure? proc) (procedure-source proc))))
1119 (if (and source (null? (cdddr source)))
1120 (let ((obj (caadr source)))
1121 ;; smart closure compilation
1123 `(lambda (,obj) (,assert-bound ,(caddr source) ,obj))
1124 (procedure-environment proc)))
1125 (lambda (o) (assert-bound (proc o) o)))))
1127 ;; the idea is to compile the index into the procedure, for fastest
1128 ;; lookup. Also, @slot-ref and @slot-set! have their own bytecodes.
1130 (eval-when (compile)
1131 (use-modules ((language tree-il primitives) :select (add-interesting-primitive!)))
1132 (add-interesting-primitive! '@slot-ref)
1133 (add-interesting-primitive! '@slot-set!))
1135 (eval-when (eval load compile)
1136 (define num-standard-pre-cache 20))
1138 (define-macro (define-standard-accessor-method form . body)
1139 (let ((name (caar form))
1140 (n-var (cadar form))
1142 (define (make-one x)
1143 (define (body-trans form)
1144 (cond ((not (pair? form)) form)
1145 ((eq? (car form) '@slot-ref)
1146 `(,(car form) ,(cadr form) ,x))
1147 ((eq? (car form) '@slot-set!)
1148 `(,(car form) ,(cadr form) ,x ,(cadddr form)))
1150 (map body-trans form))))
1151 `(lambda ,args ,@(map body-trans body)))
1153 (let ((cache (vector ,@(map make-one (iota num-standard-pre-cache)))))
1155 (if (< n ,num-standard-pre-cache)
1156 (vector-ref cache n)
1157 ((lambda (,n-var) (lambda ,args ,@body)) n)))))))
1159 (define-standard-accessor-method ((bound-check-get n) o)
1160 (let ((x (@slot-ref o n)))
1165 (define-standard-accessor-method ((standard-get n) o)
1168 (define-standard-accessor-method ((standard-set n) o v)
1171 ;;; compute-getters-n-setters
1173 (define (make-thunk thunk)
1174 (lambda () (thunk)))
1176 (define (compute-getters-n-setters class slots env)
1178 (define (compute-slot-init-function name s)
1179 (or (let ((thunk (slot-definition-init-thunk s)))
1181 (cond ((not (thunk? thunk))
1182 (goops-error "Bad init-thunk for slot `~S' in ~S: ~S"
1184 ((closure? thunk) thunk)
1185 (else (make-thunk thunk)))))
1186 (let ((init (slot-definition-init-value s)))
1187 (and (not (unbound? init))
1188 (lambda () init)))))
1190 (define (verify-accessors slot l)
1191 (cond ((integer? l))
1192 ((not (and (list? l) (= (length l) 2)))
1193 (goops-error "Bad getter and setter for slot `~S' in ~S: ~S"
1198 ;; note that we allow non-closures; we only check arity on
1199 ;; the closures, though, because we inline their dispatch
1200 ;; in %get-slot-value / %set-slot-value.
1201 (if (or (not (procedure? get))
1203 (not (= (car (procedure-property get 'arity)) 1))))
1204 (goops-error "Bad getter closure for slot `~S' in ~S: ~S"
1206 (if (or (not (procedure? set))
1208 (not (= (car (procedure-property set 'arity)) 2))))
1209 (goops-error "Bad setter closure for slot `~S' in ~S: ~S"
1210 slot class set))))))
1213 ;; The strange treatment of nfields is due to backward compatibility.
1214 (let* ((index (slot-ref class 'nfields))
1215 (g-n-s (compute-get-n-set class s))
1216 (size (- (slot-ref class 'nfields) index))
1217 (name (slot-definition-name s)))
1218 ;; NOTE: The following is interdependent with C macros
1219 ;; defined above goops.c:scm_sys_prep_layout_x.
1221 ;; For simple instance slots, we have the simplest form
1222 ;; '(name init-function . index)
1223 ;; For other slots we have
1224 ;; '(name init-function getter setter . alloc)
1226 ;; '(index size) for instance allocated slots
1227 ;; '() for other slots
1228 (verify-accessors name g-n-s)
1230 (cons (compute-slot-init-function name s)
1231 (if (or (integer? g-n-s)
1234 (append g-n-s (list index size)))))))
1239 ;;; Correct behaviour:
1241 ;;; (define-class food ())
1242 ;;; (define-class fruit (food))
1243 ;;; (define-class spice (food))
1244 ;;; (define-class apple (fruit))
1245 ;;; (define-class cinnamon (spice))
1246 ;;; (define-class pie (apple cinnamon))
1247 ;;; => cpl (pie) = pie apple fruit cinnamon spice food object top
1249 ;;; (define-class d ())
1250 ;;; (define-class e ())
1251 ;;; (define-class f ())
1252 ;;; (define-class b (d e))
1253 ;;; (define-class c (e f))
1254 ;;; (define-class a (b c))
1255 ;;; => cpl (a) = a b d c e f object top
1258 (define-method (compute-cpl (class <class>))
1259 (compute-std-cpl class class-direct-supers))
1263 (define (only-non-null lst)
1264 (filter (lambda (l) (not (null? l))) lst))
1266 (define (compute-std-cpl c get-direct-supers)
1267 (let ((c-direct-supers (get-direct-supers c)))
1268 (merge-lists (list c)
1269 (only-non-null (append (map class-precedence-list
1271 (list c-direct-supers))))))
1273 (define (merge-lists reversed-partial-result inputs)
1275 ((every null? inputs)
1276 (reverse! reversed-partial-result))
1278 (let* ((candidate (lambda (c)
1279 (and (not (any (lambda (l)
1283 (candidate-car (lambda (l)
1284 (and (not (null? l))
1285 (candidate (car l)))))
1286 (next (any candidate-car inputs)))
1288 (goops-error "merge-lists: Inconsistent precedence graph"))
1289 (let ((remove-next (lambda (l)
1290 (if (eq? (car l) next)
1293 (merge-lists (cons next reversed-partial-result)
1294 (only-non-null (map remove-next inputs))))))))
1296 ;; Modified from TinyClos:
1298 ;; A simple topological sort.
1300 ;; It's in this file so that both TinyClos and Objects can use it.
1302 ;; This is a fairly modified version of code I originally got from Anurag
1303 ;; Mendhekar <anurag@moose.cs.indiana.edu>.
1306 (define (compute-clos-cpl c get-direct-supers)
1307 (top-sort ((build-transitive-closure get-direct-supers) c)
1308 ((build-constraints get-direct-supers) c)
1309 (std-tie-breaker get-direct-supers)))
1312 (define (top-sort elements constraints tie-breaker)
1313 (let loop ((elements elements)
1314 (constraints constraints)
1316 (if (null? elements)
1318 (let ((can-go-in-now
1321 (every (lambda (constraint)
1322 (or (not (eq? (cadr constraint) x))
1323 (memq (car constraint) result)))
1326 (if (null? can-go-in-now)
1327 (goops-error "top-sort: Invalid constraints")
1328 (let ((choice (if (null? (cdr can-go-in-now))
1333 (filter (lambda (x) (not (eq? x choice)))
1336 (append result (list choice)))))))))
1338 (define (std-tie-breaker get-supers)
1339 (lambda (partial-cpl min-elts)
1340 (let loop ((pcpl (reverse partial-cpl)))
1341 (let ((current-elt (car pcpl)))
1342 (let ((ds-of-ce (get-supers current-elt)))
1343 (let ((common (filter (lambda (x)
1347 (if (null? (cdr pcpl))
1348 (goops-error "std-tie-breaker: Nothing valid")
1353 (define (build-transitive-closure get-follow-ons)
1355 (let track ((result '())
1359 (let ((next (car pending)))
1360 (if (memq next result)
1361 (track result (cdr pending))
1362 (track (cons next result)
1363 (append (get-follow-ons next)
1364 (cdr pending)))))))))
1366 (define (build-constraints get-follow-ons)
1368 (let loop ((elements ((build-transitive-closure get-follow-ons) x))
1371 (if (or (null? this-one) (null? (cdr this-one)))
1372 (if (null? elements)
1374 (loop (cdr elements)
1375 (cons (car elements)
1376 (get-follow-ons (car elements)))
1380 (cons (list (car this-one) (cadr this-one))
1383 ;;; compute-get-n-set
1385 (define-method (compute-get-n-set (class <class>) s)
1386 (case (slot-definition-allocation s)
1387 ((#:instance) ;; Instance slot
1388 ;; get-n-set is just its offset
1389 (let ((already-allocated (slot-ref class 'nfields)))
1390 (slot-set! class 'nfields (+ already-allocated 1))
1393 ((#:class) ;; Class slot
1394 ;; Class-slots accessors are implemented as 2 closures around
1395 ;; a Scheme variable. As instance slots, class slots must be
1396 ;; unbound at init time.
1397 (let ((name (slot-definition-name s)))
1398 (if (memq name (map slot-definition-name (class-direct-slots class)))
1399 ;; This slot is direct; create a new shared variable
1400 (make-closure-variable class)
1401 ;; Slot is inherited. Find its definition in superclass
1402 (let loop ((l (cdr (class-precedence-list class))))
1403 (let ((r (assoc name (slot-ref (car l) 'getters-n-setters))))
1406 (loop (cdr l))))))))
1408 ((#:each-subclass) ;; slot shared by instances of direct subclass.
1409 ;; (Thomas Buerger, April 1998)
1410 (make-closure-variable class))
1412 ((#:virtual) ;; No allocation
1413 ;; slot-ref and slot-set! function must be given by the user
1414 (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
1415 (set (get-keyword #:slot-set! (slot-definition-options s) #f))
1416 (env (class-environment class)))
1417 (if (not (and get set))
1418 (goops-error "You must supply a #:slot-ref and a #:slot-set! in ~S"
1421 (else (next-method))))
1423 (define (make-closure-variable class)
1424 (let ((shared-variable (make-unbound)))
1425 (list (lambda (o) shared-variable)
1426 (lambda (o v) (set! shared-variable v)))))
1428 (define-method (compute-get-n-set (o <object>) s)
1429 (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
1431 (define-method (compute-slots (class <class>))
1432 (%compute-slots class))
1438 (define-method (initialize (object <object>) initargs)
1439 (%initialize-object object initargs))
1441 (define-method (initialize (class <class>) initargs)
1443 (let ((dslots (get-keyword #:slots initargs '()))
1444 (supers (get-keyword #:dsupers initargs '()))
1445 (env (get-keyword #:environment initargs (top-level-env))))
1447 (slot-set! class 'name (get-keyword #:name initargs '???))
1448 (slot-set! class 'direct-supers supers)
1449 (slot-set! class 'direct-slots dslots)
1450 (slot-set! class 'direct-subclasses '())
1451 (slot-set! class 'direct-methods '())
1452 (slot-set! class 'cpl (compute-cpl class))
1453 (slot-set! class 'redefined #f)
1454 (slot-set! class 'environment env)
1455 (let ((slots (compute-slots class)))
1456 (slot-set! class 'slots slots)
1457 (slot-set! class 'nfields 0)
1458 (slot-set! class 'getters-n-setters (compute-getters-n-setters class
1461 ;; Build getters - setters - accessors
1462 (compute-slot-accessors class slots env))
1464 ;; Update the "direct-subclasses" of each inherited classes
1465 (for-each (lambda (x)
1468 (cons class (slot-ref x 'direct-subclasses))))
1471 ;; Support for the underlying structs:
1473 ;; Inherit class flags (invisible on scheme level) from supers
1474 (%inherit-magic! class supers)
1476 ;; Set the layout slot
1477 (%prep-layout! class)))
1479 (define (initialize-object-procedure object initargs)
1480 (let ((proc (get-keyword #:procedure initargs #f)))
1483 (apply set-object-procedure! object proc))
1484 ((valid-object-procedure? proc)
1485 (set-object-procedure! object proc))
1487 (set-object-procedure! object
1488 (lambda args (apply proc args)))))))
1490 (define-method (initialize (class <operator-class>) initargs)
1492 (initialize-object-procedure class initargs))
1494 (define-method (initialize (owsc <operator-with-setter-class>) initargs)
1496 (%set-object-setter! owsc (get-keyword #:setter initargs #f)))
1498 (define-method (initialize (entity <entity>) initargs)
1500 (initialize-object-procedure entity initargs))
1502 (define-method (initialize (ews <entity-with-setter>) initargs)
1504 (%set-object-setter! ews (get-keyword #:setter initargs #f)))
1506 (define-method (initialize (generic <generic>) initargs)
1507 (let ((previous-definition (get-keyword #:default initargs #f))
1508 (name (get-keyword #:name initargs #f)))
1510 (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
1512 (apply previous-definition args)))
1515 (set-procedure-property! generic 'name name))
1518 (define-method (initialize (eg <extended-generic>) initargs)
1520 (slot-set! eg 'extends (get-keyword #:extends initargs '())))
1522 (define dummy-procedure (lambda args *unspecified*))
1524 (define-method (initialize (method <method>) initargs)
1526 (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
1527 (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
1528 (slot-set! method 'procedure
1529 (get-keyword #:procedure initargs #f))
1530 (slot-set! method 'code-table '())
1531 (slot-set! method 'formals (get-keyword #:formals initargs '()))
1532 (slot-set! method 'body (get-keyword #:body initargs '()))
1533 (slot-set! method 'make-procedure (get-keyword #:make-procedure initargs #f)))
1536 (define-method (initialize (obj <foreign-object>) initargs))
1542 (define (change-object-class old-instance old-class new-class)
1543 (let ((new-instance (allocate-instance new-class '())))
1544 ;; Initialize the slots of the new instance
1545 (for-each (lambda (slot)
1546 (if (and (slot-exists-using-class? old-class old-instance slot)
1547 (eq? (slot-definition-allocation
1548 (class-slot-definition old-class slot))
1550 (slot-bound-using-class? old-class old-instance slot))
1551 ;; Slot was present and allocated in old instance; copy it
1552 (slot-set-using-class!
1556 (slot-ref-using-class old-class old-instance slot))
1557 ;; slot was absent; initialize it with its default value
1558 (let ((init (slot-init-function new-class slot)))
1560 (slot-set-using-class!
1564 (apply init '()))))))
1565 (map slot-definition-name (class-slots new-class)))
1566 ;; Exchange old and new instance in place to keep pointers valid
1567 (%modify-instance old-instance new-instance)
1568 ;; Allow class specific updates of instances (which now are swapped)
1569 (update-instance-for-different-class new-instance old-instance)
1573 (define-method (update-instance-for-different-class (old-instance <object>)
1576 ;;not really important what we do, we just need a default method
1579 (define-method (change-class (old-instance <object>) (new-class <class>))
1580 (change-object-class old-instance (class-of old-instance) new-class))
1585 ;;; A new definition which overwrites the previous one which was built-in
1588 (define-method (allocate-instance (class <class>) initargs)
1589 (%allocate-instance class initargs))
1591 (define-method (make-instance (class <class>) . initargs)
1592 (let ((instance (allocate-instance class initargs)))
1593 (initialize instance initargs)
1596 (define make make-instance)
1601 ;;; Protocol for calling standard generic functions. This protocol is
1602 ;;; not used for real <generic> functions (in this case we use a
1603 ;;; completely C hard-coded protocol). Apply-generic is used by
1604 ;;; goops for calls to subclasses of <generic> and <generic-with-setter>.
1605 ;;; The code below is similar to the first MOP described in AMOP. In
1606 ;;; particular, it doesn't used the currified approach to gf
1607 ;;; call. There are 2 reasons for that:
1608 ;;; - the protocol below is exposed to mimic completely the one written in C
1609 ;;; - the currified protocol would be imho inefficient in C.
1612 (define-method (apply-generic (gf <generic>) args)
1613 (if (null? (slot-ref gf 'methods))
1614 (no-method gf args))
1615 (let ((methods (compute-applicable-methods gf args)))
1617 (apply-methods gf (sort-applicable-methods gf methods args) args)
1618 (no-applicable-method gf args))))
1620 ;; compute-applicable-methods is bound to %compute-applicable-methods.
1622 (define %%compute-applicable-methods
1623 (make <generic> #:name 'compute-applicable-methods))
1625 (define-method (%%compute-applicable-methods (gf <generic>) args)
1626 (%compute-applicable-methods gf args))
1628 (set! compute-applicable-methods %%compute-applicable-methods)
1630 (define-method (sort-applicable-methods (gf <generic>) methods args)
1631 (let ((targs (map class-of args)))
1632 (sort methods (lambda (m1 m2) (method-more-specific? m1 m2 targs)))))
1634 (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
1635 (%method-more-specific? m1 m2 targs))
1637 (define-method (apply-method (gf <generic>) methods build-next args)
1638 (apply (method-procedure (car methods))
1639 (build-next (cdr methods) args)
1642 (define-method (apply-methods (gf <generic>) (l <list>) args)
1643 (letrec ((next (lambda (procs args)
1645 (let ((a (if (null? new-args) args new-args)))
1647 (no-next-method gf a)
1648 (apply-method gf procs next a)))))))
1649 (apply-method gf l next args)))
1651 ;; We don't want the following procedure to turn up in backtraces:
1652 (for-each (lambda (proc)
1653 (set-procedure-property! proc 'system-procedure #t))
1657 no-applicable-method
1662 ;;; {<composite-metaclass> and <active-metaclass>}
1665 ;(autoload "active-slot" <active-metaclass>)
1666 ;(autoload "composite-slot" <composite-metaclass>)
1667 ;(export <composite-metaclass> <active-metaclass>)
1675 ;; duplicate the standard list->set function but using eq instead of
1676 ;; eqv which really sucks a lot, uselessly here
1678 (define (list2set l)
1683 ((memq (car l) res) (loop (cdr l) res))
1684 (else (loop (cdr l) (cons (car l) res))))))
1686 (define (class-subclasses c)
1687 (letrec ((allsubs (lambda (c)
1688 (cons c (mapappend allsubs
1689 (class-direct-subclasses c))))))
1690 (list2set (cdr (allsubs c)))))
1692 (define (class-methods c)
1693 (list2set (mapappend class-direct-methods
1694 (cons c (class-subclasses c)))))
1697 ;;; {Final initialization}
1700 ;; Tell C code that the main bulk of Goops has been loaded