3 ;;;; Copyright (C) 1998,1999,2000,2001,2002 Free Software Foundation, Inc.
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
10 ;;;; This program 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
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
20 ;;;; As a special exception, the Free Software Foundation gives permission
21 ;;;; for additional uses of the text contained in its release of GUILE.
23 ;;;; The exception is that, if you link the GUILE library with other files
24 ;;;; to produce an executable, this does not by itself cause the
25 ;;;; resulting executable to be covered by the GNU General Public License.
26 ;;;; Your use of that executable is in no way restricted on account of
27 ;;;; linking the GUILE library code into it.
29 ;;;; This exception does not however invalidate any other reasons why
30 ;;;; the executable file might be covered by the GNU General Public License.
32 ;;;; This exception applies only to the code released by the
33 ;;;; Free Software Foundation under the name GUILE. If you copy
34 ;;;; code from other Free Software Foundation releases into a copy of
35 ;;;; GUILE, as the General Public License permits, the exception does
36 ;;;; not apply to the code that you add in this way. To avoid misleading
37 ;;;; anyone as to the status of such modified files, you must delete
38 ;;;; this exception notice from them.
40 ;;;; If you write modifications of your own for GUILE, it is your choice
41 ;;;; whether to permit this exception to apply to your modifications.
42 ;;;; If you do not wish that, delete this exception notice.
46 ;;;; This software is a derivative work of other copyrighted softwares; the
47 ;;;; copyright notices of these softwares are placed in the file COPYRIGHTS
49 ;;;; This file is based upon stklos.stk from the STk distribution by
50 ;;;; Erick Gallesio <eg@unice.fr>.
53 (define-module (oop goops)
54 :export-syntax (define-class class
55 define-generic define-accessor define-method
57 :export (goops-version is-a?
58 ensure-metaclass ensure-metaclass-with-supers
60 make-generic ensure-generic
61 make-accessor ensure-accessor
62 make-method add-method!
63 object-eqv? object-equal?
64 class-slot-ref class-slot-set! slot-unbound slot-missing
65 slot-definition-name slot-definition-options
66 slot-definition-allocation
67 slot-definition-getter slot-definition-setter
68 slot-definition-accessor
69 slot-definition-init-value slot-definition-init-form
70 slot-definition-init-thunk slot-definition-init-keyword
71 slot-init-function class-slot-definition
73 compute-cpl compute-std-cpl compute-get-n-set compute-slots
74 compute-getter-method compute-setter-method
75 allocate-instance initialize make-instance make
76 no-next-method no-applicable-method no-method
77 change-class update-instance-for-different-class
78 shallow-clone deep-clone
80 apply-generic apply-method apply-methods
81 compute-applicable-methods %compute-applicable-methods
82 method-more-specific? sort-applicable-methods
83 class-subclasses class-methods
86 ;;; *fixme* Should go into goops.c
87 instance? slot-ref-using-class
88 slot-set-using-class! slot-bound-using-class?
89 slot-exists-using-class? slot-ref slot-set! slot-bound?
90 class-name class-direct-supers class-direct-subclasses
91 class-direct-methods class-direct-slots class-precedence-list
92 class-slots class-environment
94 generic-function-methods method-generic-function method-specializers
95 primitive-generic-generic enable-primitive-generic!
96 method-procedure accessor-method-slot-definition
97 slot-exists? make find-method get-keyword)
98 :re-export (class-of) ;; from (guile)
101 ;; First initialize the builtin part of GOOPS
102 (%init-goops-builtins)
104 ;; Then load the rest of GOOPS
105 (use-modules (oop goops util)
110 (define min-fixnum (- (expt 2 29)))
112 (define max-fixnum (- (expt 2 29) 1))
117 (define (goops-error format-string . args)
119 (scm-error 'goops-error #f format-string args '()))
124 (define (is-a? obj class)
125 (and (memq class (class-precedence-list (class-of obj))) #t))
132 (define ensure-metaclass-with-supers
133 (let ((table-of-metas '()))
134 (lambda (meta-supers)
135 (let ((entry (assoc meta-supers table-of-metas)))
137 ;; Found a previously created metaclass
139 ;; Create a new meta-class which inherit from "meta-supers"
140 (let ((new (make <class> #:dsupers meta-supers
142 #:name (gensym "metaclass"))))
143 (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
146 (define (ensure-metaclass supers env)
149 (let* ((all-metas (map (lambda (x) (class-of x)) supers))
150 (all-cpls (apply append
152 (cdr (class-precedence-list m)))
155 ;; Find the most specific metaclasses. The new metaclass will be
156 ;; a subclass of these.
159 (if (and (not (member meta all-cpls))
160 (not (member meta needed-metas)))
161 (set! needed-metas (append needed-metas (list meta)))))
163 ;; Now return a subclass of the metaclasses we found.
164 (if (null? (cdr needed-metas))
165 (car needed-metas) ; If there's only one, just use it.
166 (ensure-metaclass-with-supers needed-metas)))))
172 ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
174 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
175 ;;; OPTION ::= KEYWORD VALUE
177 (define (define-class-pre-definition keyword exp env)
180 (if (defined? exp env)
181 `(define ,exp (ensure-generic ,exp ',exp))
182 `(define ,exp (make-generic ',exp))))
184 (if (defined? exp env)
185 `(define ,exp (ensure-accessor ,exp ',exp))
186 `(define ,exp (make-accessor ',exp))))
189 ;;; This code should be implemented in C.
192 (letrec (;; Some slot options require extra definitions to be made.
193 ;; In particular, we want to make sure that the generic
194 ;; function objects which represent accessors exist
195 ;; before `make-class' tries to add methods to them.
197 ;; Postpone error handling to class macro.
201 (do ((slots slots (cdr slots))
203 (if (pair? (car slots))
204 (do ((options (cdar slots) (cddr options))
205 (definitions definitions
206 (cond ((not (symbol? (cadr options)))
208 ((define-class-pre-definition
212 => (lambda (definition)
213 (cons definition definitions)))
214 (else definitions))))
215 ((not (and (pair? options)
216 (pair? (cdr options))))
219 ((or (not (pair? slots))
220 (keyword? (car slots)))
221 (reverse definitions)))))
227 (procedure->memoizing-macro
229 (cond ((not (top-level-env? env))
230 (goops-error "define-class: Only allowed at top level"))
231 ((not (and (list? exp) (>= (length exp) 3)))
232 (goops-error "missing or extra expression"))
234 (let ((name (name exp)))
237 ,@(pre-definitions (slots exp) env)
238 ;; update the current-module
239 (let* ((class (class ,@(cddr exp) #:name ',name))
240 (var (module-ensure-local-variable!
241 (current-module) ',name))
242 (old (and (variable-bound? var)
243 (variable-ref var))))
246 (memq <object> (class-precedence-list old)))
247 (variable-set! var (class-redefinition old class))
248 (variable-set! var class)))))))))))
250 (define standard-define-class define-class)
252 ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
254 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
255 ;;; OPTION ::= KEYWORD VALUE
258 (letrec ((slot-option-keyword car)
259 (slot-option-value cadr)
260 (process-slot-options
262 (let loop ((options options)
264 (cond ((null? options)
266 ((null? (cdr options))
267 (goops-error "malformed slot option list"))
268 ((not (keyword? (slot-option-keyword options)))
269 (goops-error "malformed slot option list"))
271 (case (slot-option-keyword options)
274 (append (list `(lambda ()
275 ,(slot-option-value options))
278 (slot-option-value options))
287 (procedure->memoizing-macro
292 (cond ((not (and (list? exp) (>= (length exp) 2)))
293 (goops-error "missing or extra expression"))
294 ((not (list? (supers exp)))
295 (goops-error "malformed superclass list: ~S" (supers exp)))
297 (let ((slot-defs (cons #f '())))
298 (do ((slots (slots exp) (cdr slots))
299 (defs slot-defs (cdr defs)))
301 (keyword? (car slots)))
303 ;; evaluate super class variables
304 (list ,@(supers exp))
305 ;; evaluate slot definitions, except the slot name!
306 (list ,@(cdr slot-defs))
307 ;; evaluate class options
309 ;; place option last in case someone wants to
310 ;; pass a different value
311 #:environment ',env))
314 (list (if (pair? (car slots))
315 `(list ',(slot-definition-name (car slots))
316 ,@(process-slot-options
317 (slot-definition-options
319 `(list ',(car slots))))))))))))))
321 (define (make-class supers slots . options)
322 (let ((env (or (get-keyword #:environment options #f)
324 (let* ((name (get-keyword #:name options (make-unbound)))
325 (supers (if (not (or-map (lambda (class)
327 (class-precedence-list class)))
329 (append supers (list <object>))
331 (metaclass (or (get-keyword #:metaclass options #f)
332 (ensure-metaclass supers env))))
334 ;; Verify that all direct slots are different and that we don't inherit
335 ;; several time from the same class
336 (let ((tmp1 (find-duplicate supers))
337 (tmp2 (find-duplicate (map slot-definition-name slots))))
339 (goops-error "make-class: super class ~S is duplicate in class ~S"
342 (goops-error "make-class: slot ~S is duplicate in class ~S"
345 ;; Everything seems correct, build the class
346 (apply make metaclass
354 ;;; {Generic functions and accessors}
357 (define define-generic
358 (procedure->memoizing-macro
360 (let ((name (cadr exp)))
361 (cond ((not (symbol? name))
362 (goops-error "bad generic function name: ~S" name))
363 ((top-level-env? env)
364 `(let* ((var (module-ensure-local-variable!
365 (current-module) ',name))
366 (old (and (variable-bound? var) (variable-ref var))))
367 (if (or (not old) (is-a? old <generic>))
368 (variable-set! var (make <generic> #:name ',name))
369 (variable-set! var (ensure-generic old ',name)))))
371 `(define ,name (make <generic> #:name ',name))))))))
373 (define (make-generic . name)
374 (let ((name (and (pair? name) (car name))))
375 (make <generic> #:name name)))
377 (define (ensure-generic old-definition . name)
378 (let ((name (and (pair? name) (car name))))
379 (cond ((is-a? old-definition <generic>) old-definition)
380 ((procedure-with-setter? old-definition)
381 (make <generic-with-setter>
383 #:default (procedure old-definition)
384 #:setter (setter old-definition)))
385 ((procedure? old-definition)
386 (make <generic> #:name name #:default old-definition))
387 (else (make <generic> #:name name)))))
389 (define define-accessor
390 (procedure->memoizing-macro
392 (let ((name (cadr exp)))
393 (cond ((not (symbol? name))
394 (goops-error "bad accessor name: ~S" name))
395 ((top-level-env? env)
396 `(let* ((var (module-ensure-local-variable!
397 (current-module) ',name))
398 (old (and (variable-bound? var) (variable-ref var))))
400 (and (is-a? old <generic-with-setter>)
401 (is-a? (setter old) <generic>)))
402 (variable-set! var (make-accessor ',name))
403 (variable-set! var (ensure-accessor old ',name)))))
405 `(define ,name (make-accessor ',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))))
412 (make <generic-with-setter>
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 ((is-a? proc <generic-with-setter>)
420 (if (is-a? (setter proc) <generic>)
422 (upgrade-generic-with-setter proc (setter proc))))
423 ((is-a? proc <generic>)
424 (upgrade-generic-with-setter proc (make-generic name)))
425 ((procedure-with-setter? proc)
426 (make <generic-with-setter>
428 #:default (procedure proc)
429 #:setter (ensure-generic (setter proc) name)))
431 (ensure-accessor (ensure-generic proc name) name))
433 (make-accessor name)))))
435 (define (upgrade-generic-with-setter generic setter)
436 (let ((methods (generic-function-methods generic))
437 (gws (make <generic-with-setter>
438 #:name (generic-function-name generic)
441 (for-each (lambda (method)
442 (slot-set! method 'generic-function gws))
444 (slot-set! gws 'methods methods)
451 (define define-method
452 (procedure->memoizing-macro
454 (let ((head (cadr exp)))
455 (if (not (pair? head))
456 (goops-error "bad method head: ~S" head)
457 (let ((gf (car head)))
458 (cond ((and (pair? gf)
459 (eq? (car gf) 'setter)
463 ;; named setter method
464 (let ((name (cadr gf)))
465 (cond ((not (symbol? name))
466 `(add-method! (setter ,name)
471 ;; *fixme* Temporary hack for the current
474 (define-accessor ,name))
475 (add-method! (setter ,name)
480 (define-accessor ,name)
481 (add-method! (setter ,name)
485 `(add-method! ,gf (method ,(cdadr exp) ,@(cddr exp))))
488 ;; *fixme* Temporary hack for the current
491 (define-generic ,gf))
500 ,@(cddr exp))))))))))))
502 (define (make-method specializers procedure)
504 #:specializers specializers
505 #:procedure procedure))
508 (letrec ((specializers
510 (cond ((null? ls) '('()))
511 ((pair? ls) (cons (if (pair? (car ls))
514 (specializers (cdr ls))))
519 (cons (if (pair? (car ls)) (caar ls) (car ls))
522 (procedure->memoizing-macro
524 (let ((args (cadr exp))
527 #:specializers (cons* ,@(specializers args))
528 #:procedure (lambda ,(formals args)
537 (define (add-method-in-classes! m)
538 ;; Add method in all the classes which appears in its specializers list
539 (for-each* (lambda (x)
540 (let ((dm (class-direct-methods x)))
541 (if (not (memv m dm))
542 (slot-set! x 'direct-methods (cons m dm)))))
543 (method-specializers m)))
545 (define (remove-method-in-classes! m)
546 ;; Remove method in all the classes which appears in its specializers list
547 (for-each* (lambda (x)
550 (delv! m (class-direct-methods x))))
551 (method-specializers m)))
553 (define (compute-new-list-of-methods gf new)
554 (let ((new-spec (method-specializers new))
555 (methods (generic-function-methods gf)))
556 (let loop ((l methods))
559 (if (equal? (method-specializers (car l)) new-spec)
561 ;; This spec. list already exists. Remove old method from dependents
562 (remove-method-in-classes! (car l))
567 (define (internal-add-method! gf m)
568 (slot-set! m 'generic-function gf)
569 (slot-set! gf 'methods (compute-new-list-of-methods gf m))
570 (let ((specializers (slot-ref m 'specializers)))
571 (slot-set! gf 'n-specialized
572 (max (length* specializers)
573 (slot-ref gf 'n-specialized))))
574 (%invalidate-method-cache! gf)
575 (add-method-in-classes! m)
578 (define-generic add-method!)
580 (internal-add-method! add-method!
582 #:specializers (list <generic> <method>)
583 #:procedure internal-add-method!))
585 (define-method (add-method! (proc <procedure>) (m <method>))
586 (if (generic-capability? proc)
588 (enable-primitive-generic! proc)
589 (add-method! proc m))
592 (define-method (add-method! (pg <primitive-generic>) (m <method>))
593 (add-method! (primitive-generic-generic pg) m))
595 (define-method (add-method! obj (m <method>))
596 (goops-error "~S is not a valid generic function" obj))
599 ;;; {Access to meta objects}
605 (define-method (method-source (m <method>))
606 (let* ((spec (map* class-name (slot-ref m 'specializers)))
607 (proc (procedure-source (slot-ref m 'procedure)))
611 (cons (map* list args spec)
617 (define slot-definition-name car)
619 (define slot-definition-options cdr)
621 (define (slot-definition-allocation s)
622 (get-keyword #:allocation (cdr s) #:instance))
624 (define (slot-definition-getter s)
625 (get-keyword #:getter (cdr s) #f))
627 (define (slot-definition-setter s)
628 (get-keyword #:setter (cdr s) #f))
630 (define (slot-definition-accessor s)
631 (get-keyword #:accessor (cdr s) #f))
633 (define (slot-definition-init-value s)
634 ;; can be #f, so we can't use #f as non-value
635 (get-keyword #:init-value (cdr s) (make-unbound)))
637 (define (slot-definition-init-form s)
638 (get-keyword #:init-form (cdr s) (make-unbound)))
640 (define (slot-definition-init-thunk s)
641 (get-keyword #:init-thunk (cdr s) #f))
643 (define (slot-definition-init-keyword s)
644 (get-keyword #:init-keyword (cdr s) #f))
646 (define (class-slot-definition class slot-name)
647 (assq slot-name (class-slots class)))
649 (define (slot-init-function class slot-name)
650 (cadr (assq slot-name (slot-ref class 'getters-n-setters))))
654 ;;; {Standard methods used by the C runtime}
657 ;;; Methods to compare objects
660 (define-method (object-eqv? x y) #f)
661 (define-method (object-equal? x y) (eqv? x y))
664 ;;; methods to display/write an object
667 ; Code for writing objects must test that the slots they use are
668 ; bound. Otherwise a slot-unbound method will be called and will
669 ; conduct to an infinite loop.
672 (define (display-address o file)
673 (display (number->string (object-address o) 16) file))
675 (define-method (write o file)
676 (display "#<instance " file)
677 (display-address o file)
680 (define write-object (primitive-generic-generic write))
682 (define-method (write (o <object>) file)
683 (let ((class (class-of o)))
684 (if (slot-bound? class 'name)
687 (display (class-name class) file)
688 (display #\space file)
689 (display-address o file)
693 (define-method (write (o <foreign-object>) file)
694 (let ((class (class-of o)))
695 (if (slot-bound? class 'name)
697 (display "#<foreign-object " file)
698 (display (class-name class) file)
699 (display #\space file)
700 (display-address o file)
704 (define-method (write (class <class>) file)
705 (let ((meta (class-of class)))
706 (if (and (slot-bound? class 'name)
707 (slot-bound? meta 'name))
710 (display (class-name meta) file)
711 (display #\space file)
712 (display (class-name class) file)
713 (display #\space file)
714 (display-address class file)
718 (define-method (write (gf <generic>) file)
719 (let ((meta (class-of gf)))
720 (if (and (slot-bound? meta 'name)
721 (slot-bound? gf 'methods))
724 (display (class-name meta) file)
725 (let ((name (generic-function-name gf)))
728 (display #\space file)
729 (display name file))))
731 (display (length (generic-function-methods gf)) file)
735 (define-method (write (o <method>) file)
736 (let ((meta (class-of o)))
737 (if (and (slot-bound? meta 'name)
738 (slot-bound? o 'specializers))
741 (display (class-name meta) file)
742 (display #\space file)
743 (display (map* (lambda (spec)
744 (if (slot-bound? spec 'name)
745 (slot-ref spec 'name)
747 (method-specializers o))
749 (display #\space file)
750 (display-address o file)
754 ;; Display (do the same thing as write by default)
755 (define-method (display o file)
756 (write-object o file))
762 (define (class-slot-g-n-s class slot-name)
763 (let* ((this-slot (assq slot-name (slot-ref class 'slots)))
764 (g-n-s (cddr (or (assq slot-name (slot-ref class 'getters-n-setters))
765 (slot-missing class slot-name)))))
766 (if (not (memq (slot-definition-allocation this-slot)
767 '(#:class #:each-subclass)))
768 (slot-missing class slot-name))
771 (define (class-slot-ref class slot)
772 (let ((x ((car (class-slot-g-n-s class slot)) #f)))
774 (slot-unbound class slot)
777 (define (class-slot-set! class slot value)
778 ((cadr (class-slot-g-n-s class slot)) #f value))
780 (define-method (slot-unbound (c <class>) (o <object>) s)
781 (goops-error "Slot `~S' is unbound in object ~S" s o))
783 (define-method (slot-unbound (c <class>) s)
784 (goops-error "Slot `~S' is unbound in class ~S" s c))
786 (define-method (slot-unbound (o <object>))
787 (goops-error "Unbound slot in object ~S" o))
789 (define-method (slot-missing (c <class>) (o <object>) s)
790 (goops-error "No slot with name `~S' in object ~S" s o))
792 (define-method (slot-missing (c <class>) s)
793 (goops-error "No class slot with name `~S' in class ~S" s c))
796 (define-method (slot-missing (c <class>) (o <object>) s value)
797 (slot-missing c o s))
799 ;;; Methods for the possible error we can encounter when calling a gf
801 (define-method (no-next-method (gf <generic>) args)
802 (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
804 (define-method (no-applicable-method (gf <generic>) args)
805 (goops-error "No applicable method for ~S in call ~S"
806 gf (cons (generic-function-name gf) args)))
808 (define-method (no-method (gf <generic>) args)
809 (goops-error "No method defined for ~S" gf))
812 ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
815 (define-method (shallow-clone (self <object>))
816 (let ((clone (%allocate-instance (class-of self) '()))
817 (slots (map slot-definition-name
818 (class-slots (class-of self)))))
819 (for-each (lambda (slot)
820 (if (slot-bound? self slot)
821 (slot-set! clone slot (slot-ref self slot))))
825 (define-method (deep-clone (self <object>))
826 (let ((clone (%allocate-instance (class-of self) '()))
827 (slots (map slot-definition-name
828 (class-slots (class-of self)))))
829 (for-each (lambda (slot)
830 (if (slot-bound? self slot)
831 (slot-set! clone slot
832 (let ((value (slot-ref self slot)))
833 (if (instance? value)
840 ;;; {Class redefinition utilities}
843 ;;; (class-redefinition OLD NEW)
846 ;;; Has correct the following conditions:
850 ;;; 1. New accessor specializers refer to new header
854 ;;; 1. New class cpl refers to the new class header
855 ;;; 2. Old class header exists on old super classes direct-subclass lists
856 ;;; 3. New class header exists on new super classes direct-subclass lists
858 (define-method (class-redefinition (old <class>) (new <class>))
859 ;; Work on direct methods:
860 ;; 1. Remove accessor methods from the old class
861 ;; 2. Patch the occurences of new in the specializers by old
862 ;; 3. Displace the methods from old to new
863 (remove-class-accessors! old) ;; -1-
864 (let ((methods (class-direct-methods new)))
865 (for-each (lambda (m)
866 (update-direct-method! m new old)) ;; -2-
870 (append methods (class-direct-methods old))))
872 ;; Substitute old for new in new cpl
873 (set-car! (slot-ref new 'cpl) old)
875 ;; Remove the old class from the direct-subclasses list of its super classes
876 (for-each (lambda (c) (slot-set! c 'direct-subclasses
877 (delv! old (class-direct-subclasses c))))
878 (class-direct-supers old))
880 ;; Replace the new class with the old in the direct-subclasses of the supers
881 (for-each (lambda (c)
882 (slot-set! c 'direct-subclasses
883 (cons old (delv! new (class-direct-subclasses c)))))
884 (class-direct-supers new))
886 ;; Swap object headers
887 (%modify-class old new)
891 ;; Redefine all the subclasses of old to take into account modification
894 (update-direct-subclass! c new old))
895 (class-direct-subclasses new))
897 ;; Invalidate class so that subsequent instances slot accesses invoke
898 ;; change-object-class
899 (slot-set! new 'redefined old)
900 (%invalidate-class new) ;must come after slot-set!
905 ;;; remove-class-accessors!
908 (define-method (remove-class-accessors! (c <class>))
909 (for-each (lambda (m)
910 (if (is-a? m <accessor-method>)
911 (remove-method-in-classes! m)))
912 (class-direct-methods c)))
915 ;;; update-direct-method!
918 (define-method (update-direct-method! (m <method>)
921 (let loop ((l (method-specializers m)))
922 ;; Note: the <top> in dotted list is never used.
923 ;; So we can work as if we had only proper lists.
926 (if (eqv? (car l) old)
931 ;;; update-direct-subclass!
934 (define-method (update-direct-subclass! (c <class>)
937 (class-redefinition c
938 (make-class (class-direct-supers c)
939 (class-direct-slots c)
940 #:name (class-name c)
941 #:environment (slot-ref c 'environment)
942 #:metaclass (class-of c))))
945 ;;; {Utilities for INITIALIZE methods}
948 ;;; compute-slot-accessors
950 (define (compute-slot-accessors class slots env)
953 (let ((name (slot-definition-name s))
954 (getter-function (slot-definition-getter s))
955 (setter-function (slot-definition-setter s))
956 (accessor (slot-definition-accessor s)))
958 (add-method! getter-function
959 (compute-getter-method class g-n-s)))
961 (add-method! setter-function
962 (compute-setter-method class g-n-s)))
965 (add-method! accessor
966 (compute-getter-method class g-n-s))
967 (add-method! (setter accessor)
968 (compute-setter-method class g-n-s))))))
969 slots (slot-ref class 'getters-n-setters)))
971 (define-method (compute-getter-method (class <class>) slotdef)
972 (let ((init-thunk (cadr slotdef))
973 (g-n-s (cddr slotdef)))
974 (make <accessor-method>
975 #:specializers (list class)
976 #:procedure (cond ((pair? g-n-s)
979 (make-generic-bound-check-getter (car g-n-s))
982 (standard-get g-n-s))
984 (bound-check-get g-n-s)))
985 #:slot-definition slotdef)))
987 (define-method (compute-setter-method (class <class>) slotdef)
988 (let ((g-n-s (cddr slotdef)))
989 (make <accessor-method>
990 #:specializers (list class <top>)
991 #:procedure (if (pair? g-n-s)
993 (standard-set g-n-s))
994 #:slot-definition slotdef)))
996 (define (make-generic-bound-check-getter proc)
997 (let ((source (and (closure? proc) (procedure-source proc))))
998 (if (and source (null? (cdddr source)))
999 (let ((obj (caadr source)))
1000 ;; smart closure compilation
1002 `(lambda (,obj) (,assert-bound ,(caddr source) ,obj))
1003 (procedure-environment proc)))
1004 (lambda (o) (assert-bound (proc o) o)))))
1006 (define n-standard-accessor-methods 10)
1008 (define bound-check-get-methods (make-vector n-standard-accessor-methods #f))
1009 (define standard-get-methods (make-vector n-standard-accessor-methods #f))
1010 (define standard-set-methods (make-vector n-standard-accessor-methods #f))
1012 (define (standard-accessor-method make methods)
1014 (cond ((>= index n-standard-accessor-methods) (make index))
1015 ((vector-ref methods index))
1016 (else (let ((m (make index)))
1017 (vector-set! methods index m)
1020 (define (make-bound-check-get index)
1021 (local-eval `(lambda (o) (@assert-bound-ref o ,index)) (the-environment)))
1023 (define (make-get index)
1024 (local-eval `(lambda (o) (@slot-ref o ,index)) (the-environment)))
1026 (define (make-set index)
1027 (local-eval `(lambda (o v) (@slot-set! o ,index v)) (the-environment)))
1029 (define bound-check-get
1030 (standard-accessor-method make-bound-check-get bound-check-get-methods))
1031 (define standard-get (standard-accessor-method make-get standard-get-methods))
1032 (define standard-set (standard-accessor-method make-set standard-set-methods))
1034 ;;; compute-getters-n-setters
1036 (define (compute-getters-n-setters class slots env)
1038 (define (compute-slot-init-function s)
1039 (or (slot-definition-init-thunk s)
1040 (let ((init (slot-definition-init-value s)))
1041 (and (not (unbound? init))
1042 (lambda () init)))))
1044 (define (verify-accessors slot l)
1048 (if (not (and (closure? get)
1049 (= (car (procedure-property get 'arity)) 1)))
1050 (goops-error "Bad getter closure for slot `~S' in ~S: ~S"
1052 (if (not (and (closure? set)
1053 (= (car (procedure-property set 'arity)) 2)))
1054 (goops-error "Bad setter closure for slot `~S' in ~S: ~S"
1058 (let* ((g-n-s (compute-get-n-set class s))
1059 (name (slot-definition-name s)))
1060 ; For each slot we have '(name init-function getter setter)
1061 ; If slot, we have the simplest form '(name init-function . index)
1062 (verify-accessors name g-n-s)
1064 (cons (compute-slot-init-function s)
1070 ;;; Correct behaviour:
1072 ;;; (define-class food ())
1073 ;;; (define-class fruit (food))
1074 ;;; (define-class spice (food))
1075 ;;; (define-class apple (fruit))
1076 ;;; (define-class cinnamon (spice))
1077 ;;; (define-class pie (apple cinnamon))
1078 ;;; => cpl (pie) = pie apple fruit cinnamon spice food object top
1080 ;;; (define-class d ())
1081 ;;; (define-class e ())
1082 ;;; (define-class f ())
1083 ;;; (define-class b (d e))
1084 ;;; (define-class c (e f))
1085 ;;; (define-class a (b c))
1086 ;;; => cpl (a) = a b d c e f object top
1089 (define-method (compute-cpl (class <class>))
1090 (compute-std-cpl class class-direct-supers))
1094 (define (only-non-null lst)
1095 (filter (lambda (l) (not (null? l))) lst))
1097 (define (compute-std-cpl c get-direct-supers)
1098 (let ((c-direct-supers (get-direct-supers c)))
1099 (merge-lists (list c)
1100 (only-non-null (append (map class-precedence-list
1102 (list c-direct-supers))))))
1104 (define (merge-lists reversed-partial-result inputs)
1106 ((every null? inputs)
1107 (reverse! reversed-partial-result))
1109 (let* ((candidate (lambda (c)
1110 (and (not (any (lambda (l)
1114 (candidate-car (lambda (l)
1115 (and (not (null? l))
1116 (candidate (car l)))))
1117 (next (any candidate-car inputs)))
1119 (goops-error "merge-lists: Inconsistent precedence graph"))
1120 (let ((remove-next (lambda (l)
1121 (if (eq? (car l) next)
1124 (merge-lists (cons next reversed-partial-result)
1125 (only-non-null (map remove-next inputs))))))))
1127 ;; Modified from TinyClos:
1129 ;; A simple topological sort.
1131 ;; It's in this file so that both TinyClos and Objects can use it.
1133 ;; This is a fairly modified version of code I originally got from Anurag
1134 ;; Mendhekar <anurag@moose.cs.indiana.edu>.
1137 (define (compute-clos-cpl c get-direct-supers)
1138 (top-sort ((build-transitive-closure get-direct-supers) c)
1139 ((build-constraints get-direct-supers) c)
1140 (std-tie-breaker get-direct-supers)))
1143 (define (top-sort elements constraints tie-breaker)
1144 (let loop ((elements elements)
1145 (constraints constraints)
1147 (if (null? elements)
1149 (let ((can-go-in-now
1152 (every (lambda (constraint)
1153 (or (not (eq? (cadr constraint) x))
1154 (memq (car constraint) result)))
1157 (if (null? can-go-in-now)
1158 (goops-error "top-sort: Invalid constraints")
1159 (let ((choice (if (null? (cdr can-go-in-now))
1164 (filter (lambda (x) (not (eq? x choice)))
1167 (append result (list choice)))))))))
1169 (define (std-tie-breaker get-supers)
1170 (lambda (partial-cpl min-elts)
1171 (let loop ((pcpl (reverse partial-cpl)))
1172 (let ((current-elt (car pcpl)))
1173 (let ((ds-of-ce (get-supers current-elt)))
1174 (let ((common (filter (lambda (x)
1178 (if (null? (cdr pcpl))
1179 (goops-error "std-tie-breaker: Nothing valid")
1184 (define (build-transitive-closure get-follow-ons)
1186 (let track ((result '())
1190 (let ((next (car pending)))
1191 (if (memq next result)
1192 (track result (cdr pending))
1193 (track (cons next result)
1194 (append (get-follow-ons next)
1195 (cdr pending)))))))))
1197 (define (build-constraints get-follow-ons)
1199 (let loop ((elements ((build-transitive-closure get-follow-ons) x))
1202 (if (or (null? this-one) (null? (cdr this-one)))
1203 (if (null? elements)
1205 (loop (cdr elements)
1206 (cons (car elements)
1207 (get-follow-ons (car elements)))
1211 (cons (list (car this-one) (cadr this-one))
1214 ;;; compute-get-n-set
1216 (define-method (compute-get-n-set (class <class>) s)
1217 (case (slot-definition-allocation s)
1218 ((#:instance) ;; Instance slot
1219 ;; get-n-set is just its offset
1220 (let ((already-allocated (slot-ref class 'nfields)))
1221 (slot-set! class 'nfields (+ already-allocated 1))
1224 ((#:class) ;; Class slot
1225 ;; Class-slots accessors are implemented as 2 closures around
1226 ;; a Scheme variable. As instance slots, class slots must be
1227 ;; unbound at init time.
1228 (let ((name (slot-definition-name s)))
1229 (if (memq name (map slot-definition-name (class-direct-slots class)))
1230 ;; This slot is direct; create a new shared variable
1231 (make-closure-variable class)
1232 ;; Slot is inherited. Find its definition in superclass
1233 (let loop ((l (cdr (class-precedence-list class))))
1234 (let ((r (assoc name (slot-ref (car l) 'getters-n-setters))))
1237 (loop (cdr l))))))))
1239 ((#:each-subclass) ;; slot shared by instances of direct subclass.
1240 ;; (Thomas Buerger, April 1998)
1241 (make-closure-variable class))
1243 ((#:virtual) ;; No allocation
1244 ;; slot-ref and slot-set! function must be given by the user
1245 (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
1246 (set (get-keyword #:slot-set! (slot-definition-options s) #f))
1247 (env (class-environment class)))
1248 (if (not (and get set))
1249 (goops-error "You must supply a :slot-ref and a :slot-set! in ~S"
1252 (else (next-method))))
1254 (define (make-closure-variable class)
1255 (let ((shared-variable (make-unbound)))
1256 (list (lambda (o) shared-variable)
1257 (lambda (o v) (set! shared-variable v)))))
1259 (define-method (compute-get-n-set (o <object>) s)
1260 (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
1262 (define-method (compute-slots (class <class>))
1263 (%compute-slots class))
1269 (define-method (initialize (object <object>) initargs)
1270 (%initialize-object object initargs))
1272 (define-method (initialize (class <class>) initargs)
1274 (let ((dslots (get-keyword #:slots initargs '()))
1275 (supers (get-keyword #:dsupers initargs '()))
1276 (env (get-keyword #:environment initargs (top-level-env))))
1278 (slot-set! class 'name (get-keyword #:name initargs '???))
1279 (slot-set! class 'direct-supers supers)
1280 (slot-set! class 'direct-slots dslots)
1281 (slot-set! class 'direct-subclasses '())
1282 (slot-set! class 'direct-methods '())
1283 (slot-set! class 'cpl (compute-cpl class))
1284 (slot-set! class 'redefined #f)
1285 (slot-set! class 'environment env)
1286 (let ((slots (compute-slots class)))
1287 (slot-set! class 'slots slots)
1288 (slot-set! class 'nfields 0)
1289 (slot-set! class 'getters-n-setters (compute-getters-n-setters class
1292 ;; Build getters - setters - accessors
1293 (compute-slot-accessors class slots env))
1295 ;; Update the "direct-subclasses" of each inherited classes
1296 (for-each (lambda (x)
1299 (cons class (slot-ref x 'direct-subclasses))))
1302 ;; Support for the underlying structs:
1304 ;; Inherit class flags (invisible on scheme level) from supers
1305 (%inherit-magic! class supers)
1307 ;; Set the layout slot
1308 (%prep-layout! class)))
1310 (define (initialize-object-procedure object initargs)
1311 (let ((proc (get-keyword #:procedure initargs #f)))
1314 (apply set-object-procedure! object proc))
1315 ((valid-object-procedure? proc)
1316 (set-object-procedure! object proc))
1318 (set-object-procedure! object
1319 (lambda args (apply proc args)))))))
1321 (define-method (initialize (class <operator-class>) initargs)
1323 (initialize-object-procedure class initargs))
1325 (define-method (initialize (owsc <operator-with-setter-class>) initargs)
1327 (%set-object-setter! owsc (get-keyword #:setter initargs #f)))
1329 (define-method (initialize (entity <entity>) initargs)
1331 (initialize-object-procedure entity initargs))
1333 (define-method (initialize (ews <entity-with-setter>) initargs)
1335 (%set-object-setter! ews (get-keyword #:setter initargs #f)))
1337 (define-method (initialize (generic <generic>) initargs)
1338 (let ((previous-definition (get-keyword #:default initargs #f))
1339 (name (get-keyword #:name initargs #f)))
1341 (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
1342 (list (make <method>
1343 #:specializers <top>
1346 (apply previous-definition
1350 (set-procedure-property! generic 'name name))
1353 (define dummy-procedure (lambda args *unspecified*))
1355 (define-method (initialize (method <method>) initargs)
1357 (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
1358 (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
1359 (slot-set! method 'procedure
1360 (get-keyword #:procedure initargs dummy-procedure))
1361 (slot-set! method 'code-table '()))
1363 (define-method (initialize (obj <foreign-object>) initargs))
1369 (define (change-object-class old-instance old-class new-class)
1370 (let ((new-instance (allocate-instance new-class '())))
1371 ;; Initalize the slot of the new instance
1372 (for-each (lambda (slot)
1373 (if (and (slot-exists-using-class? old-class old-instance slot)
1374 (eq? (slot-definition-allocation
1375 (class-slot-definition old-class slot))
1377 (slot-bound-using-class? old-class old-instance slot))
1378 ;; Slot was present and allocated in old instance; copy it
1379 (slot-set-using-class!
1383 (slot-ref-using-class old-class old-instance slot))
1384 ;; slot was absent; initialize it with its default value
1385 (let ((init (slot-init-function new-class slot)))
1387 (slot-set-using-class!
1391 (apply init '()))))))
1392 (map slot-definition-name (class-slots new-class)))
1393 ;; Exchange old and new instance in place to keep pointers valid
1394 (%modify-instance old-instance new-instance)
1395 ;; Allow class specific updates of instances (which now are swapped)
1396 (update-instance-for-different-class new-instance old-instance)
1400 (define-method (update-instance-for-different-class (old-instance <object>)
1403 ;;not really important what we do, we just need a default method
1406 (define-method (change-class (old-instance <object>) (new-class <class>))
1407 (change-object-class old-instance (class-of old-instance) new-class))
1412 ;;; A new definition which overwrites the previous one which was built-in
1415 (define-method (allocate-instance (class <class>) initargs)
1416 (%allocate-instance class initargs))
1418 (define-method (make-instance (class <class>) . initargs)
1419 (let ((instance (allocate-instance class initargs)))
1420 (initialize instance initargs)
1423 (define make make-instance)
1428 ;;; Protocol for calling standard generic functions. This protocol is
1429 ;;; not used for real <generic> functions (in this case we use a
1430 ;;; completely C hard-coded protocol). Apply-generic is used by
1431 ;;; goops for calls to subclasses of <generic> and <generic-with-setter>.
1432 ;;; The code below is similar to the first MOP described in AMOP. In
1433 ;;; particular, it doesn't used the currified approach to gf
1434 ;;; call. There are 2 reasons for that:
1435 ;;; - the protocol below is exposed to mimic completely the one written in C
1436 ;;; - the currified protocol would be imho inefficient in C.
1439 (define-method (apply-generic (gf <generic>) args)
1440 (if (null? (slot-ref gf 'methods))
1441 (no-method gf args))
1442 (let ((methods (compute-applicable-methods gf args)))
1444 (apply-methods gf (sort-applicable-methods gf methods args) args)
1445 (no-applicable-method gf args))))
1447 ;; compute-applicable-methods is bound to %compute-applicable-methods.
1449 (define %%compute-applicable-methods
1450 (make <generic> #:name 'compute-applicable-methods))
1452 (define-method (%%compute-applicable-methods (gf <generic>) args)
1453 (%compute-applicable-methods gf args))
1455 (set! compute-applicable-methods %%compute-applicable-methods)
1457 (define-method (sort-applicable-methods (gf <generic>) methods args)
1458 (let ((targs (map class-of args)))
1459 (sort methods (lambda (m1 m2) (method-more-specific? m1 m2 targs)))))
1461 (define-method (method-more-specific? (m1 <method>) (m2 <method>) targs)
1462 (%method-more-specific? m1 m2 targs))
1464 (define-method (apply-method (gf <generic>) methods build-next args)
1465 (apply (method-procedure (car methods))
1466 (build-next (cdr methods) args)
1469 (define-method (apply-methods (gf <generic>) (l <list>) args)
1470 (letrec ((next (lambda (procs args)
1472 (let ((a (if (null? new-args) args new-args)))
1474 (no-next-method gf a)
1475 (apply-method gf procs next a)))))))
1476 (apply-method gf l next args)))
1478 ;; We don't want the following procedure to turn up in backtraces:
1479 (for-each (lambda (proc)
1480 (set-procedure-property! proc 'system-procedure #t))
1484 no-applicable-method
1489 ;;; {<composite-metaclass> and <active-metaclass>}
1492 ;(autoload "active-slot" <active-metaclass>)
1493 ;(autoload "composite-slot" <composite-metaclass>)
1494 ;(export <composite-metaclass> <active-metaclass>)
1502 ;; duplicate the standard list->set function but using eq instead of
1503 ;; eqv which really sucks a lot, uselessly here
1505 (define (list2set l)
1510 ((memq (car l) res) (loop (cdr l) res))
1511 (else (loop (cdr l) (cons (car l) res))))))
1513 (define (class-subclasses c)
1514 (letrec ((allsubs (lambda (c)
1515 (cons c (mapappend allsubs
1516 (class-direct-subclasses c))))))
1517 (list2set (cdr (allsubs c)))))
1519 (define (class-methods c)
1520 (list2set (mapappend class-direct-methods
1521 (cons c (class-subclasses c)))))
1524 ;;; {Final initialization}
1527 ;; Tell C code that the main bulk of Goops has been loaded