3 ;;;; Copyright (C) 1998, 1999, 2000, 2001 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
22 ;;;; This software is a derivative work of other copyrighted softwares; the
23 ;;;; copyright notices of these softwares are placed in the file COPYRIGHTS
25 ;;;; This file is based upon stklos.stk from the STk distribution by
26 ;;;; Erick Gallesio <eg@unice.fr>.
29 (define-module (oop goops)
30 :use-module (oop goops goopscore)
31 :use-module (oop goops util)
32 :use-module (oop goops dispatch)
33 :use-module (oop goops compile)
37 (export ; Define the exported symbols of this file
39 ensure-metaclass ensure-metaclass-with-supers
40 define-class class make-class
41 define-generic make-generic ensure-generic
42 define-accessor make-accessor ensure-accessor
43 define-method make-method method add-method!
44 object-eqv? object-equal?
45 class-slot-ref class-slot-set! slot-unbound slot-missing
46 slot-definition-name slot-definition-options slot-definition-allocation
47 slot-definition-getter slot-definition-setter slot-definition-accessor
48 slot-definition-init-value slot-definition-init-form
49 slot-definition-init-thunk slot-definition-init-keyword
50 slot-init-function class-slot-definition
52 compute-cpl compute-std-cpl compute-get-n-set compute-slots
53 compute-getter-method compute-setter-method
54 allocate-instance initialize make-instance make
55 no-next-method no-applicable-method no-method
56 change-class update-instance-for-different-class
57 shallow-clone deep-clone
59 apply-generic apply-method apply-methods
60 compute-applicable-methods %compute-applicable-methods
61 method-more-specific? sort-applicable-methods
62 class-subclasses class-methods
67 ;;; *fixme* Should go into goops.c
70 instance? slot-ref-using-class
71 slot-set-using-class! slot-bound-using-class?
72 slot-exists-using-class? slot-ref slot-set! slot-bound? class-of
73 class-name class-direct-supers class-direct-subclasses
74 class-direct-methods class-direct-slots class-precedence-list
75 class-slots class-environment
77 generic-function-methods method-generic-function method-specializers
78 primitive-generic-generic enable-primitive-generic!
79 method-procedure accessor-method-slot-definition
80 slot-exists? make find-method get-keyword)
83 (define min-fixnum (- (expt 2 29)))
85 (define max-fixnum (- (expt 2 29) 1))
90 (define (goops-error format-string . args)
92 (scm-error 'goops-error #f format-string args '()))
97 (define (is-a? obj class)
98 (and (memq class (class-precedence-list (class-of obj))) #t))
105 (define ensure-metaclass-with-supers
106 (let ((table-of-metas '()))
107 (lambda (meta-supers)
108 (let ((entry (assoc meta-supers table-of-metas)))
110 ;; Found a previously created metaclass
112 ;; Create a new meta-class which inherit from "meta-supers"
113 (let ((new (make <class> #:dsupers meta-supers
115 #:name (gensym "metaclass"))))
116 (set! table-of-metas (cons (cons meta-supers new) table-of-metas))
119 (define (ensure-metaclass supers env)
122 (let* ((all-metas (map (lambda (x) (class-of x)) supers))
123 (all-cpls (apply append
125 (cdr (class-precedence-list m)))
128 ;; Find the most specific metaclasses. The new metaclass will be
129 ;; a subclass of these.
132 (if (and (not (member meta all-cpls))
133 (not (member meta needed-metas)))
134 (set! needed-metas (append needed-metas (list meta)))))
136 ;; Now return a subclass of the metaclasses we found.
137 (if (null? (cdr needed-metas))
138 (car needed-metas) ; If there's only one, just use it.
139 (ensure-metaclass-with-supers needed-metas)))))
145 ;;; (define-class NAME (SUPER ...) SLOT-DEFINITION ... OPTION ...)
147 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
148 ;;; OPTION ::= KEYWORD VALUE
150 (define (define-class-pre-definition keyword exp env)
153 (if (defined? exp env)
154 `(define ,exp (ensure-generic ,exp ',exp))
155 `(define ,exp (make-generic ',exp))))
157 (if (defined? exp env)
158 `(define ,exp (ensure-accessor ,exp ',exp))
159 `(define ,exp (make-accessor ',exp))))
162 ;;; This code should be implemented in C.
165 (letrec (;; Some slot options require extra definitions to be made.
166 ;; In particular, we want to make sure that the generic
167 ;; function objects which represent accessors exist
168 ;; before `make-class' tries to add methods to them.
170 ;; Postpone error handling to class macro.
174 (do ((slots slots (cdr slots))
176 (if (pair? (car slots))
177 (do ((options (cdar slots) (cddr options))
178 (definitions definitions
179 (cond ((not (symbol? (cadr options)))
181 ((define-class-pre-definition
185 => (lambda (definition)
186 (cons definition definitions)))
187 (else definitions))))
188 ((not (and (pair? options)
189 (pair? (cdr options))))
192 ((or (not (pair? slots))
193 (keyword? (car slots)))
194 (reverse definitions)))))
202 (cond ((not (top-level-env? env))
203 (goops-error "define-class: Only allowed at top level"))
204 ((not (and (list? exp) (>= (length exp) 3)))
205 (goops-error "missing or extra expression"))
207 (let ((name (name exp)))
210 ,@(pre-definitions (slots exp) env)
212 ,(if (defined? name env)
214 ;; redefine an old class
217 (new (class ,@(cddr exp) #:name ',name)))
218 (if (and (is-a? old <class>)
219 ;; Prevent redefinition of non-objects
221 (class-precedence-list old)))
222 (class-redefinition old new)
225 ;; define a new class
227 (class ,@(cddr exp) #:name ',name)))))))))))
229 (define standard-define-class define-class)
231 ;;; (class (SUPER ...) SLOT-DEFINITION ... OPTION ...)
233 ;;; SLOT-DEFINITION ::= SLOT-NAME | (SLOT-NAME OPTION ...)
234 ;;; OPTION ::= KEYWORD VALUE
237 (letrec ((slot-option-keyword car)
238 (slot-option-value cadr)
239 (process-slot-options
241 (let loop ((options options)
243 (cond ((null? options)
245 ((null? (cdr options))
246 (goops-error "malformed slot option list"))
247 ((not (keyword? (slot-option-keyword options)))
248 (goops-error "malformed slot option list"))
250 (case (slot-option-keyword options)
253 (append (list `(lambda ()
254 ,(slot-option-value options))
257 (slot-option-value options))
266 (procedure->memoizing-macro
271 (cond ((not (and (list? exp) (>= (length exp) 2)))
272 (goops-error "missing or extra expression"))
273 ((not (list? (supers exp)))
274 (goops-error "malformed superclass list: ~S" (supers exp)))
276 (let ((slot-defs (cons #f '())))
277 (do ((slots (slots exp) (cdr slots))
278 (defs slot-defs (cdr defs)))
280 (keyword? (car slots)))
282 ;; evaluate super class variables
283 (list ,@(supers exp))
284 ;; evaluate slot definitions, except the slot name!
285 (list ,@(cdr slot-defs))
286 ;; evaluate class options
288 ;; place option last in case someone wants to
289 ;; pass a different value
290 #:environment ',env))
293 (list (if (pair? (car slots))
294 `(list ',(slot-definition-name (car slots))
295 ,@(process-slot-options
296 (slot-definition-options
298 `(list ',(car slots))))))))))))))
300 (define (make-class supers slots . options)
301 (let ((env (or (get-keyword #:environment options #f)
303 (let* ((name (get-keyword #:name options (make-unbound)))
304 (supers (if (not (or-map (lambda (class)
306 (class-precedence-list class)))
308 (append supers (list <object>))
310 (metaclass (or (get-keyword #:metaclass options #f)
311 (ensure-metaclass supers env))))
313 ;; Verify that all direct slots are different and that we don't inherit
314 ;; several time from the same class
315 (let ((tmp1 (find-duplicate supers))
316 (tmp2 (find-duplicate (map slot-definition-name slots))))
318 (goops-error "make-class: super class ~S is duplicate in class ~S"
321 (goops-error "make-class: slot ~S is duplicate in class ~S"
324 ;; Everything seems correct, build the class
325 (apply make metaclass
333 ;;; {Generic functions and accessors}
336 (define define-generic
339 (let ((name (cadr exp)))
340 (cond ((not (symbol? name))
341 (goops-error "bad generic function name: ~S" name))
344 (if (is-a? ,name <generic>)
345 (make <generic> #:name ',name)
346 (ensure-generic ,name ',name))))
348 `(define ,name (make <generic> #:name ',name))))))))
350 (define (make-generic . name)
351 (let ((name (and (pair? name) (car name))))
352 (make <generic> #:name name)))
354 (define (ensure-generic old-definition . name)
355 (let ((name (and (pair? name) (car name))))
356 (cond ((is-a? old-definition <generic>) old-definition)
357 ((procedure-with-setter? old-definition)
358 (make <generic-with-setter>
360 #:default (procedure old-definition)
361 #:setter (setter old-definition)))
362 ((procedure? old-definition)
363 (make <generic> #:name name #:default old-definition))
364 (else (make <generic> #:name name)))))
366 (define define-accessor
369 (let ((name (cadr exp)))
370 (cond ((not (symbol? name))
371 (goops-error "bad accessor name: ~S" name))
374 (if (and (is-a? ,name <generic-with-setter>)
375 (is-a? (setter ,name) <generic>))
376 (make-accessor ',name)
377 (ensure-accessor ,name ',name))))
379 `(define ,name (make-accessor ',name))))))))
381 (define (make-setter-name name)
382 (string->symbol (string-append "setter:" (symbol->string name))))
384 (define (make-accessor . name)
385 (let ((name (and (pair? name) (car name))))
386 (make <generic-with-setter>
388 #:setter (make <generic>
389 #:name (and name (make-setter-name name))))))
391 (define (ensure-accessor proc . name)
392 (let ((name (and (pair? name) (car name))))
393 (cond ((is-a? proc <generic-with-setter>)
394 (if (is-a? (setter proc) <generic>)
396 (upgrade-generic-with-setter proc (setter proc))))
397 ((is-a? proc <generic>)
398 (upgrade-generic-with-setter proc (make-generic name)))
399 ((procedure-with-setter? proc)
400 (make <generic-with-setter>
402 #:default (procedure proc)
403 #:setter (ensure-generic (setter proc) name)))
405 (ensure-accessor (ensure-generic proc name) name))
407 (make-accessor name)))))
409 (define (upgrade-generic-with-setter generic setter)
410 (let ((methods (generic-function-methods generic))
411 (gws (make <generic-with-setter>
412 #:name (generic-function-name generic)
415 (for-each (lambda (method)
416 (slot-set! method 'generic-function gws))
418 (slot-set! gws 'methods methods)
425 (define define-method
426 (procedure->memoizing-macro
428 (let ((name (cadr exp)))
429 (if (and (pair? name)
430 (eq? (car name) 'setter)
432 (symbol? (cadr name))
434 (let ((name (cadr name)))
435 (cond ((not (symbol? name))
436 (goops-error "bad method name: ~S" name))
439 ;; *fixme* Temporary hack for the current module system
441 (define-generic ,name))
442 (add-method! (setter ,name) (method ,@(cddr exp)))))
445 (define-accessor ,name)
446 (add-method! (setter ,name) (method ,@(cddr exp)))))))
448 ;; Convert new syntax to old
449 `(define-method ,(car name) ,(cdr name) ,@(cddr exp)))
450 ((not (symbol? name))
451 (goops-error "bad method name: ~S" name))
454 ;; *fixme* Temporary hack for the current module system
456 (define-generic ,name))
457 (add-method! ,name (method ,@(cddr exp)))))
460 (define-generic ,name)
461 (add-method! ,name (method ,@(cddr exp)))))))))))
463 (define (make-method specializers procedure)
465 #:specializers specializers
466 #:procedure procedure))
469 (letrec ((specializers
471 (cond ((null? ls) '('()))
472 ((pair? ls) (cons (if (pair? (car ls))
475 (specializers (cdr ls))))
480 (cons (if (pair? (car ls)) (caar ls) (car ls))
483 (procedure->memoizing-macro
485 (let ((args (cadr exp))
488 #:specializers (list* ,@(specializers args))
489 #:procedure (lambda ,(formals args)
498 (define (add-method-in-classes! m)
499 ;; Add method in all the classes which appears in its specializers list
500 (for-each* (lambda (x)
501 (let ((dm (class-direct-methods x)))
502 (if (not (memv m dm))
503 (slot-set! x 'direct-methods (cons m dm)))))
504 (method-specializers m)))
506 (define (remove-method-in-classes! m)
507 ;; Remove method in all the classes which appears in its specializers list
508 (for-each* (lambda (x)
511 (delv! m (class-direct-methods x))))
512 (method-specializers m)))
514 (define (compute-new-list-of-methods gf new)
515 (let ((new-spec (method-specializers new))
516 (methods (generic-function-methods gf)))
517 (let loop ((l methods))
520 (if (equal? (method-specializers (car l)) new-spec)
522 ;; This spec. list already exists. Remove old method from dependents
523 (remove-method-in-classes! (car l))
528 (define (internal-add-method! gf m)
529 (slot-set! m 'generic-function gf)
530 (slot-set! gf 'methods (compute-new-list-of-methods gf m))
531 (let ((specializers (slot-ref m 'specializers)))
532 (slot-set! gf 'n-specialized
533 (max (length* specializers)
534 (slot-ref gf 'n-specialized))))
535 (%invalidate-method-cache! gf)
536 (add-method-in-classes! m)
539 (define-generic add-method!)
541 (internal-add-method! add-method!
543 #:specializers (list <generic> <method>)
544 #:procedure internal-add-method!))
546 (define-method add-method! ((proc <procedure>) (m <method>))
547 (if (generic-capability? proc)
549 (enable-primitive-generic! proc)
550 (add-method! proc m))
553 (define-method add-method! ((pg <primitive-generic>) (m <method>))
554 (add-method! (primitive-generic-generic pg) m))
556 (define-method add-method! (obj (m <method>))
557 (goops-error "~S is not a valid generic function" obj))
560 ;;; {Access to meta objects}
566 (define-method method-source ((m <method>))
567 (let* ((spec (map* class-name (slot-ref m 'specializers)))
568 (proc (procedure-source (slot-ref m 'procedure)))
572 (cons (map* list args spec)
578 (define slot-definition-name car)
580 (define slot-definition-options cdr)
582 (define (slot-definition-allocation s)
583 (get-keyword #:allocation (cdr s) #:instance))
585 (define (slot-definition-getter s)
586 (get-keyword #:getter (cdr s) #f))
588 (define (slot-definition-setter s)
589 (get-keyword #:setter (cdr s) #f))
591 (define (slot-definition-accessor s)
592 (get-keyword #:accessor (cdr s) #f))
594 (define (slot-definition-init-value s)
595 ;; can be #f, so we can't use #f as non-value
596 (get-keyword #:init-value (cdr s) (make-unbound)))
598 (define (slot-definition-init-form s)
599 (get-keyword #:init-form (cdr s) (make-unbound)))
601 (define (slot-definition-init-thunk s)
602 (get-keyword #:init-thunk (cdr s) #f))
604 (define (slot-definition-init-keyword s)
605 (get-keyword #:init-keyword (cdr s) #f))
607 (define (class-slot-definition class slot-name)
608 (assq slot-name (class-slots class)))
610 (define (slot-init-function class slot-name)
611 (cadr (assq slot-name (slot-ref class 'getters-n-setters))))
615 ;;; {Standard methods used by the C runtime}
618 ;;; Methods to compare objects
621 (define-method object-eqv? (x y) #f)
622 (define-method object-equal? (x y) (eqv? x y))
625 ;;; methods to display/write an object
628 ; Code for writing objects must test that the slots they use are
629 ; bound. Otherwise a slot-unbound method will be called and will
630 ; conduct to an infinite loop.
633 (define (display-address o file)
634 (display (number->string (object-address o) 16) file))
636 (define-method write (o file)
637 (display "#<instance " file)
638 (display-address o file)
641 (define write-object (primitive-generic-generic write))
643 (define-method write ((o <object>) file)
644 (let ((class (class-of o)))
645 (if (slot-bound? class 'name)
648 (display (class-name class) file)
649 (display #\space file)
650 (display-address o file)
654 (define-method write ((o <foreign-object>) file)
655 (let ((class (class-of o)))
656 (if (slot-bound? class 'name)
658 (display "#<foreign-object " file)
659 (display (class-name class) file)
660 (display #\space file)
661 (display-address o file)
665 (define-method write ((class <class>) file)
666 (let ((meta (class-of class)))
667 (if (and (slot-bound? class 'name)
668 (slot-bound? meta 'name))
671 (display (class-name meta) file)
672 (display #\space file)
673 (display (class-name class) file)
674 (display #\space file)
675 (display-address class file)
679 (define-method write ((gf <generic>) file)
680 (let ((meta (class-of gf)))
681 (if (and (slot-bound? meta 'name)
682 (slot-bound? gf 'methods))
685 (display (class-name meta) file)
686 (let ((name (generic-function-name gf)))
689 (display #\space file)
690 (display name file))))
692 (display (length (generic-function-methods gf)) file)
696 (define-method write ((o <method>) file)
697 (let ((meta (class-of o)))
698 (if (and (slot-bound? meta 'name)
699 (slot-bound? o 'specializers))
702 (display (class-name meta) file)
703 (display #\space file)
704 (display (map* (lambda (spec)
705 (if (slot-bound? spec 'name)
706 (slot-ref spec 'name)
708 (method-specializers o))
710 (display #\space file)
711 (display-address o file)
715 ;; Display (do the same thing as write by default)
716 (define-method display (o file)
717 (write-object o file))
723 (define (class-slot-g-n-s class slot-name)
724 (let* ((this-slot (assq slot-name (slot-ref class 'slots)))
725 (g-n-s (cddr (or (assq slot-name (slot-ref class 'getters-n-setters))
726 (slot-missing class slot-name)))))
727 (if (not (memq (slot-definition-allocation this-slot)
728 '(#:class #:each-subclass)))
729 (slot-missing class slot-name))
732 (define (class-slot-ref class slot)
733 (let ((x ((car (class-slot-g-n-s class slot)) #f)))
735 (slot-unbound class slot)
738 (define (class-slot-set! class slot value)
739 ((cadr (class-slot-g-n-s class slot)) #f value))
741 (define-method slot-unbound ((c <class>) (o <object>) s)
742 (goops-error "Slot `~S' is unbound in object ~S" s o))
744 (define-method slot-unbound ((c <class>) s)
745 (goops-error "Slot `~S' is unbound in class ~S" s c))
747 (define-method slot-unbound ((o <object>))
748 (goops-error "Unbound slot in object ~S" o))
750 (define-method slot-missing ((c <class>) (o <object>) s)
751 (goops-error "No slot with name `~S' in object ~S" s o))
753 (define-method slot-missing ((c <class>) s)
754 (goops-error "No class slot with name `~S' in class ~S" s c))
757 (define-method slot-missing ((c <class>) (o <object>) s value)
758 (slot-missing c o s))
760 ;;; Methods for the possible error we can encounter when calling a gf
762 (define-method no-next-method ((gf <generic>) args)
763 (goops-error "No next method when calling ~S\nwith arguments ~S" gf args))
765 (define-method no-applicable-method ((gf <generic>) args)
766 (goops-error "No applicable method for ~S in call ~S"
767 gf (cons (generic-function-name gf) args)))
769 (define-method no-method ((gf <generic>) args)
770 (goops-error "No method defined for ~S" gf))
773 ;;; {Cloning functions (from rdeline@CS.CMU.EDU)}
776 (define-method shallow-clone ((self <object>))
777 (let ((clone (%allocate-instance (class-of self) '()))
778 (slots (map slot-definition-name
779 (class-slots (class-of self)))))
780 (for-each (lambda (slot)
781 (if (slot-bound? self slot)
782 (slot-set! clone slot (slot-ref self slot))))
786 (define-method deep-clone ((self <object>))
787 (let ((clone (%allocate-instance (class-of self) '()))
788 (slots (map slot-definition-name
789 (class-slots (class-of self)))))
790 (for-each (lambda (slot)
791 (if (slot-bound? self slot)
792 (slot-set! clone slot
793 (let ((value (slot-ref self slot)))
794 (if (instance? value)
801 ;;; {Class redefinition utilities}
804 ;;; (class-redefinition OLD NEW)
807 ;;; Has correct the following conditions:
811 ;;; 1. New accessor specializers refer to new header
815 ;;; 1. New class cpl refers to the new class header
816 ;;; 2. Old class header exists on old super classes direct-subclass lists
817 ;;; 3. New class header exists on new super classes direct-subclass lists
819 (define-method class-redefinition ((old <class>) (new <class>))
820 ;; Work on direct methods:
821 ;; 1. Remove accessor methods from the old class
822 ;; 2. Patch the occurences of new in the specializers by old
823 ;; 3. Displace the methods from old to new
824 (remove-class-accessors! old) ;; -1-
825 (let ((methods (class-direct-methods new)))
826 (for-each (lambda (m)
827 (update-direct-method! m new old)) ;; -2-
831 (append methods (class-direct-methods old))))
833 ;; Substitute old for new in new cpl
834 (set-car! (slot-ref new 'cpl) old)
836 ;; Remove the old class from the direct-subclasses list of its super classes
837 (for-each (lambda (c) (slot-set! c 'direct-subclasses
838 (delv! old (class-direct-subclasses c))))
839 (class-direct-supers old))
841 ;; Replace the new class with the old in the direct-subclasses of the supers
842 (for-each (lambda (c)
843 (slot-set! c 'direct-subclasses
844 (cons old (delv! new (class-direct-subclasses c)))))
845 (class-direct-supers new))
847 ;; Swap object headers
848 (%modify-class old new)
852 ;; Redefine all the subclasses of old to take into account modification
855 (update-direct-subclass! c new old))
856 (class-direct-subclasses new))
858 ;; Invalidate class so that subsequent instances slot accesses invoke
859 ;; change-object-class
860 (slot-set! new 'redefined old)
861 (%invalidate-class new) ;must come after slot-set!
866 ;;; remove-class-accessors!
869 (define-method remove-class-accessors! ((c <class>))
870 (for-each (lambda (m)
871 (if (is-a? m <accessor-method>)
872 (remove-method-in-classes! m)))
873 (class-direct-methods c)))
876 ;;; update-direct-method!
879 (define-method update-direct-method! ((m <method>)
882 (let loop ((l (method-specializers m)))
883 ;; Note: the <top> in dotted list is never used.
884 ;; So we can work as if we had only proper lists.
887 (if (eqv? (car l) old)
892 ;;; update-direct-subclass!
895 (define-method update-direct-subclass! ((c <class>)
898 (class-redefinition c
899 (make-class (class-direct-supers c)
900 (class-direct-slots c)
901 #:name (class-name c)
902 #:environment (slot-ref c 'environment)
903 #:metaclass (class-of c))))
906 ;;; {Utilities for INITIALIZE methods}
909 ;;; compute-slot-accessors
911 (define (compute-slot-accessors class slots env)
914 (let ((name (slot-definition-name s))
915 (getter-function (slot-definition-getter s))
916 (setter-function (slot-definition-setter s))
917 (accessor (slot-definition-accessor s)))
919 (add-method! getter-function
920 (compute-getter-method class g-n-s)))
922 (add-method! setter-function
923 (compute-setter-method class g-n-s)))
926 (add-method! accessor
927 (compute-getter-method class g-n-s))
928 (add-method! (setter accessor)
929 (compute-setter-method class g-n-s))))))
930 slots (slot-ref class 'getters-n-setters)))
932 (define-method compute-getter-method ((class <class>) slotdef)
933 (let ((init-thunk (cadr slotdef))
934 (g-n-s (cddr slotdef)))
935 (make <accessor-method>
936 #:specializers (list class)
937 #:procedure (cond ((pair? g-n-s)
940 (make-generic-bound-check-getter (car g-n-s))
943 (standard-get g-n-s))
945 (bound-check-get g-n-s)))
946 #:slot-definition slotdef)))
948 (define-method compute-setter-method ((class <class>) slotdef)
949 (let ((g-n-s (cddr slotdef)))
950 (make <accessor-method>
951 #:specializers (list class <top>)
952 #:procedure (if (pair? g-n-s)
954 (standard-set g-n-s))
955 #:slot-definition slotdef)))
957 (define (make-generic-bound-check-getter proc)
958 (let ((source (and (closure? proc) (procedure-source proc))))
959 (if (and source (null? (cdddr source)))
960 (let ((obj (caadr source)))
961 ;; smart closure compilation
963 `(lambda (,obj) (,assert-bound ,(caddr source) ,obj))
964 (procedure-environment proc)))
965 (lambda (o) (assert-bound (proc o) o)))))
967 (define n-standard-accessor-methods 10)
969 (define bound-check-get-methods (make-vector n-standard-accessor-methods #f))
970 (define standard-get-methods (make-vector n-standard-accessor-methods #f))
971 (define standard-set-methods (make-vector n-standard-accessor-methods #f))
973 (define (standard-accessor-method make methods)
975 (cond ((>= index n-standard-accessor-methods) (make index))
976 ((vector-ref methods index))
977 (else (let ((m (make index)))
978 (vector-set! methods index m)
981 (define (make-bound-check-get index)
982 (local-eval `(lambda (o) (@assert-bound-ref o ,index)) (the-environment)))
984 (define (make-get index)
985 (local-eval `(lambda (o) (@slot-ref o ,index)) (the-environment)))
987 (define (make-set index)
988 (local-eval `(lambda (o v) (@slot-set! o ,index v)) (the-environment)))
990 (define bound-check-get
991 (standard-accessor-method make-bound-check-get bound-check-get-methods))
992 (define standard-get (standard-accessor-method make-get standard-get-methods))
993 (define standard-set (standard-accessor-method make-set standard-set-methods))
995 ;;; compute-getters-n-setters
997 (define (compute-getters-n-setters class slots env)
999 (define (compute-slot-init-function s)
1000 (or (slot-definition-init-thunk s)
1001 (let ((init (slot-definition-init-value s)))
1002 (and (not (unbound? init))
1003 (lambda () init)))))
1005 (define (verify-accessors slot l)
1009 (if (not (and (closure? get)
1010 (= (car (procedure-property get 'arity)) 1)))
1011 (goops-error "Bad getter closure for slot `~S' in ~S: ~S"
1013 (if (not (and (closure? set)
1014 (= (car (procedure-property set 'arity)) 2)))
1015 (goops-error "Bad setter closure for slot `~S' in ~S: ~S"
1019 (let* ((g-n-s (compute-get-n-set class s))
1020 (name (slot-definition-name s)))
1021 ; For each slot we have '(name init-function getter setter)
1022 ; If slot, we have the simplest form '(name init-function . index)
1023 (verify-accessors name g-n-s)
1025 (cons (compute-slot-init-function s)
1031 ;;; Correct behaviour:
1033 ;;; (define-class food ())
1034 ;;; (define-class fruit (food))
1035 ;;; (define-class spice (food))
1036 ;;; (define-class apple (fruit))
1037 ;;; (define-class cinnamon (spice))
1038 ;;; (define-class pie (apple cinnamon))
1039 ;;; => cpl (pie) = pie apple fruit cinnamon spice food object top
1041 ;;; (define-class d ())
1042 ;;; (define-class e ())
1043 ;;; (define-class f ())
1044 ;;; (define-class b (d e))
1045 ;;; (define-class c (e f))
1046 ;;; (define-class a (b c))
1047 ;;; => cpl (a) = a b d c e f object top
1050 (define-method compute-cpl ((class <class>))
1051 (compute-std-cpl class class-direct-supers))
1055 (define (only-non-null lst)
1056 (filter (lambda (l) (not (null? l))) lst))
1058 (define (compute-std-cpl c get-direct-supers)
1059 (let ((c-direct-supers (get-direct-supers c)))
1060 (merge-lists (list c)
1061 (only-non-null (append (map class-precedence-list
1063 (list c-direct-supers))))))
1065 (define (merge-lists reversed-partial-result inputs)
1067 ((every null? inputs)
1068 (reverse! reversed-partial-result))
1070 (let* ((candidate (lambda (c)
1071 (and (not (any (lambda (l)
1075 (candidate-car (lambda (l)
1076 (and (not (null? l))
1077 (candidate (car l)))))
1078 (next (any candidate-car inputs)))
1080 (goops-error "merge-lists: Inconsistent precedence graph"))
1081 (let ((remove-next (lambda (l)
1082 (if (eq? (car l) next)
1085 (merge-lists (cons next reversed-partial-result)
1086 (only-non-null (map remove-next inputs))))))))
1088 ;; Modified from TinyClos:
1090 ;; A simple topological sort.
1092 ;; It's in this file so that both TinyClos and Objects can use it.
1094 ;; This is a fairly modified version of code I originally got from Anurag
1095 ;; Mendhekar <anurag@moose.cs.indiana.edu>.
1098 (define (compute-clos-cpl c get-direct-supers)
1099 (top-sort ((build-transitive-closure get-direct-supers) c)
1100 ((build-constraints get-direct-supers) c)
1101 (std-tie-breaker get-direct-supers)))
1104 (define (top-sort elements constraints tie-breaker)
1105 (let loop ((elements elements)
1106 (constraints constraints)
1108 (if (null? elements)
1110 (let ((can-go-in-now
1113 (every (lambda (constraint)
1114 (or (not (eq? (cadr constraint) x))
1115 (memq (car constraint) result)))
1118 (if (null? can-go-in-now)
1119 (goops-error "top-sort: Invalid constraints")
1120 (let ((choice (if (null? (cdr can-go-in-now))
1125 (filter (lambda (x) (not (eq? x choice)))
1128 (append result (list choice)))))))))
1130 (define (std-tie-breaker get-supers)
1131 (lambda (partial-cpl min-elts)
1132 (let loop ((pcpl (reverse partial-cpl)))
1133 (let ((current-elt (car pcpl)))
1134 (let ((ds-of-ce (get-supers current-elt)))
1135 (let ((common (filter (lambda (x)
1139 (if (null? (cdr pcpl))
1140 (goops-error "std-tie-breaker: Nothing valid")
1145 (define (build-transitive-closure get-follow-ons)
1147 (let track ((result '())
1151 (let ((next (car pending)))
1152 (if (memq next result)
1153 (track result (cdr pending))
1154 (track (cons next result)
1155 (append (get-follow-ons next)
1156 (cdr pending)))))))))
1158 (define (build-constraints get-follow-ons)
1160 (let loop ((elements ((build-transitive-closure get-follow-ons) x))
1163 (if (or (null? this-one) (null? (cdr this-one)))
1164 (if (null? elements)
1166 (loop (cdr elements)
1167 (cons (car elements)
1168 (get-follow-ons (car elements)))
1172 (cons (list (car this-one) (cadr this-one))
1175 ;;; compute-get-n-set
1177 (define-method compute-get-n-set ((class <class>) s)
1178 (case (slot-definition-allocation s)
1179 ((#:instance) ;; Instance slot
1180 ;; get-n-set is just its offset
1181 (let ((already-allocated (slot-ref class 'nfields)))
1182 (slot-set! class 'nfields (+ already-allocated 1))
1185 ((#:class) ;; Class slot
1186 ;; Class-slots accessors are implemented as 2 closures around
1187 ;; a Scheme variable. As instance slots, class slots must be
1188 ;; unbound at init time.
1189 (let ((name (slot-definition-name s)))
1190 (if (memq name (map slot-definition-name (class-direct-slots class)))
1191 ;; This slot is direct; create a new shared variable
1192 (make-closure-variable class)
1193 ;; Slot is inherited. Find its definition in superclass
1194 (let loop ((l (cdr (class-precedence-list class))))
1195 (let ((r (assoc name (slot-ref (car l) 'getters-n-setters))))
1198 (loop (cdr l))))))))
1200 ((#:each-subclass) ;; slot shared by instances of direct subclass.
1201 ;; (Thomas Buerger, April 1998)
1202 (make-closure-variable class))
1204 ((#:virtual) ;; No allocation
1205 ;; slot-ref and slot-set! function must be given by the user
1206 (let ((get (get-keyword #:slot-ref (slot-definition-options s) #f))
1207 (set (get-keyword #:slot-set! (slot-definition-options s) #f))
1208 (env (class-environment class)))
1209 (if (not (and get set))
1210 (goops-error "You must supply a :slot-ref and a :slot-set! in ~S"
1213 (else (next-method))))
1215 (define (make-closure-variable class)
1216 (let ((shared-variable (make-unbound)))
1217 (list (lambda (o) shared-variable)
1218 (lambda (o v) (set! shared-variable v)))))
1220 (define-method compute-get-n-set ((o <object>) s)
1221 (goops-error "Allocation \"~S\" is unknown" (slot-definition-allocation s)))
1223 (define-method compute-slots ((class <class>))
1224 (%compute-slots class))
1230 (define-method initialize ((object <object>) initargs)
1231 (%initialize-object object initargs))
1233 (define-method initialize ((class <class>) initargs)
1235 (let ((dslots (get-keyword #:slots initargs '()))
1236 (supers (get-keyword #:dsupers initargs '()))
1237 (env (get-keyword #:environment initargs (top-level-env))))
1239 (slot-set! class 'name (get-keyword #:name initargs '???))
1240 (slot-set! class 'direct-supers supers)
1241 (slot-set! class 'direct-slots dslots)
1242 (slot-set! class 'direct-subclasses '())
1243 (slot-set! class 'direct-methods '())
1244 (slot-set! class 'cpl (compute-cpl class))
1245 (slot-set! class 'redefined #f)
1246 (slot-set! class 'environment env)
1247 (let ((slots (compute-slots class)))
1248 (slot-set! class 'slots slots)
1249 (slot-set! class 'nfields 0)
1250 (slot-set! class 'getters-n-setters (compute-getters-n-setters class
1253 ;; Build getters - setters - accessors
1254 (compute-slot-accessors class slots env))
1256 ;; Update the "direct-subclasses" of each inherited classes
1257 (for-each (lambda (x)
1260 (cons class (slot-ref x 'direct-subclasses))))
1263 ;; Support for the underlying structs:
1265 ;; Inherit class flags (invisible on scheme level) from supers
1266 (%inherit-magic! class supers)
1268 ;; Set the layout slot
1269 (%prep-layout! class)))
1271 (define object-procedure-tags
1272 '(utag_closure utag_subr_1 utag_subr_2 utag_subr3 utag_lsubr_2))
1274 (define (initialize-object-procedure object initargs)
1275 (let ((proc (get-keyword #:procedure initargs #f)))
1278 (apply set-object-procedure! object proc))
1279 ((memq (tag proc) object-procedure-tags)
1280 (set-object-procedure! object proc))
1282 (set-object-procedure! object
1283 (lambda args (apply proc args)))))))
1285 (define-method initialize ((class <operator-class>) initargs)
1287 (initialize-object-procedure class initargs))
1289 (define-method initialize ((owsc <operator-with-setter-class>) initargs)
1291 (%set-object-setter! owsc (get-keyword #:setter initargs #f)))
1293 (define-method initialize ((entity <entity>) initargs)
1295 (initialize-object-procedure entity initargs))
1297 (define-method initialize ((ews <entity-with-setter>) initargs)
1299 (%set-object-setter! ews (get-keyword #:setter initargs #f)))
1301 (define-method initialize ((generic <generic>) initargs)
1302 (let ((previous-definition (get-keyword #:default initargs #f))
1303 (name (get-keyword #:name initargs #f)))
1305 (slot-set! generic 'methods (if (is-a? previous-definition <procedure>)
1306 (list (make <method>
1307 #:specializers <top>
1310 (apply previous-definition
1314 (set-procedure-property! generic 'name name))
1317 (define dummy-procedure (lambda args *unspecified*))
1319 (define-method initialize ((method <method>) initargs)
1321 (slot-set! method 'generic-function (get-keyword #:generic-function initargs #f))
1322 (slot-set! method 'specializers (get-keyword #:specializers initargs '()))
1323 (slot-set! method 'procedure
1324 (get-keyword #:procedure initargs dummy-procedure))
1325 (slot-set! method 'code-table '()))
1327 (define-method initialize ((obj <foreign-object>) initargs))
1333 (define (change-object-class old-instance old-class new-class)
1334 (let ((new-instance (allocate-instance new-class '())))
1335 ;; Initalize the slot of the new instance
1336 (for-each (lambda (slot)
1337 (if (and (slot-exists-using-class? old-class old-instance slot)
1338 (eq? (slot-definition-allocation
1339 (class-slot-definition old-class slot))
1341 (slot-bound-using-class? old-class old-instance slot))
1342 ;; Slot was present and allocated in old instance; copy it
1343 (slot-set-using-class!
1347 (slot-ref-using-class old-class old-instance slot))
1348 ;; slot was absent; initialize it with its default value
1349 (let ((init (slot-init-function new-class slot)))
1351 (slot-set-using-class!
1355 (apply init '()))))))
1356 (map slot-definition-name (class-slots new-class)))
1357 ;; Exchange old and new instance in place to keep pointers valid
1358 (%modify-instance old-instance new-instance)
1359 ;; Allow class specific updates of instances (which now are swapped)
1360 (update-instance-for-different-class new-instance old-instance)
1364 (define-method update-instance-for-different-class ((old-instance <object>)
1367 ;;not really important what we do, we just need a default method
1370 (define-method change-class ((old-instance <object>) (new-class <class>))
1371 (change-object-class old-instance (class-of old-instance) new-class))
1376 ;;; A new definition which overwrites the previous one which was built-in
1379 (define-method allocate-instance ((class <class>) initargs)
1380 (%allocate-instance class initargs))
1382 (define-method make-instance ((class <class>) . initargs)
1383 (let ((instance (allocate-instance class initargs)))
1384 (initialize instance initargs)
1387 (define make make-instance)
1392 ;;; Protocol for calling standard generic functions. This protocol is
1393 ;;; not used for real <generic> functions (in this case we use a
1394 ;;; completely C hard-coded protocol). Apply-generic is used by
1395 ;;; goops for calls to subclasses of <generic> and <generic-with-setter>.
1396 ;;; The code below is similar to the first MOP described in AMOP. In
1397 ;;; particular, it doesn't used the currified approach to gf
1398 ;;; call. There are 2 reasons for that:
1399 ;;; - the protocol below is exposed to mimic completely the one written in C
1400 ;;; - the currified protocol would be imho inefficient in C.
1403 (define-method apply-generic ((gf <generic>) args)
1404 (if (null? (slot-ref gf 'methods))
1405 (no-method gf args))
1406 (let ((methods (compute-applicable-methods gf args)))
1408 (apply-methods gf (sort-applicable-methods gf methods args) args)
1409 (no-applicable-method gf args))))
1411 ;; compute-applicable-methods is bound to %compute-applicable-methods.
1413 (define %%compute-applicable-methods
1414 (make <generic> #:name 'compute-applicable-methods))
1416 (define-method %%compute-applicable-methods ((gf <generic>) args)
1417 (%compute-applicable-methods gf args))
1419 (set! compute-applicable-methods %%compute-applicable-methods)
1421 (define-method sort-applicable-methods ((gf <generic>) methods args)
1422 (let ((targs (map class-of args)))
1423 (sort methods (lambda (m1 m2) (method-more-specific? m1 m2 targs)))))
1425 (define-method method-more-specific? ((m1 <method>) (m2 <method>) targs)
1426 (%method-more-specific? m1 m2 targs))
1428 (define-method apply-method ((gf <generic>) methods build-next args)
1429 (apply (method-procedure (car methods))
1430 (build-next (cdr methods) args)
1433 (define-method apply-methods ((gf <generic>) (l <list>) args)
1434 (letrec ((next (lambda (procs args)
1436 (let ((a (if (null? new-args) args new-args)))
1438 (no-next-method gf a)
1439 (apply-method gf procs next a)))))))
1440 (apply-method gf l next args)))
1442 ;; We don't want the following procedure to turn up in backtraces:
1443 (for-each (lambda (proc)
1444 (set-procedure-property! proc 'system-procedure #t))
1448 no-applicable-method
1453 ;;; {<composite-metaclass> and <active-metaclass>}
1456 ;(autoload "active-slot" <active-metaclass>)
1457 ;(autoload "composite-slot" <composite-metaclass>)
1458 ;(export <composite-metaclass> <active-metaclass>)
1466 ;; duplicate the standard list->set function but using eq instead of
1467 ;; eqv which really sucks a lot, uselessly here
1469 (define (list2set l)
1474 ((memq (car l) res) (loop (cdr l) res))
1475 (else (loop (cdr l) (cons (car l) res))))))
1477 (define (class-subclasses c)
1478 (letrec ((allsubs (lambda (c)
1479 (cons c (mapappend allsubs
1480 (class-direct-subclasses c))))))
1481 (list2set (cdr (allsubs c)))))
1483 (define (class-methods c)
1484 (list2set (mapappend class-direct-methods
1485 (cons c (class-subclasses c)))))
1488 ;;; {Final initialization}
1491 ;; Tell C code that the main bulk of Goops has been loaded