;;;; ;;;; object.stk -- -- A variation of the Gregor Kickzales Tiny CLOS for STklos ;;;; ;;;; Copyright © 1993-2010 Erick Gallesio - I3S-CNRS/ESSI ;;;; ;;;; ;;;; This program is free software; you can redistribute it and/or modify ;;;; it under the terms of the GNU General Public License as published by ;;;; the Free Software Foundation; either version 2 of the License, or ;;;; (at your option) any later version. ;;;; ;;;; This program is distributed in the hope that it will be useful, ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;;; GNU General Public License for more details. ;;;; ;;;; You should have received a copy of the GNU General Public License ;;;; along with this program; if not, write to the Free Software ;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, ;;;; USA. ;;;; ;;;; Author: Erick Gallesio [eg@unice.fr] ;;;; Creation date: 20-Feb-1994 21:09 ;;;; Last file update: 1-Jan-2010 14:52 (eg) #| //FIXMOI * decoupage du source * methods rapides * Vérification que tous les points du mop sont implémentés |# (define-module STKLOS-OBJECT (import SCHEME) (export ; Define the exported symbols of this file find-class is-a? ensure-metaclass ensure-metaclass-with-supers ensure-class ensure-generic-function ensure-method add-method! object-eqv? object-equal? write-object display-object slot-unbound slot-missing slot-definition-name slot-definition-options slot-definition-allocation slot-definition-getter slot-definition-setter slot-definition-accessor slot-definition-init-form slot-definition-init-keyword slot-init-function class-slot-definition compute-get-n-set allocate-instance initialize make-instance make no-next-method no-applicable-method no-method ;; next-method-exists? change-class change-object-class shallow-clone deep-clone apply-generic apply-method apply-methods compute-applicable-methods method-more-specific? sort-applicable-methods method-procedure method-specializers method-generic-function method-specializers-equal? class-subclasses class-methods class-name class-direct-supers class-direct-subclasses class-precedence-list class-direct-methods class-direct-slots class-slots generic-function-name generic-function-methods generic-function-documentation slot-value)) (define-module STKLOS-COMPILER (import STKLOS-OBJECT)) (select-module STKLOS-OBJECT) (define extended-lambda->lambda ;; Compiler function used here (in-module STKLOS-COMPILER extended-lambda->lambda)) (define class-redefinition (void)) ;; forward declaration ;============================================================================= ; ; U t i l i t i e s ; ;============================================================================= (define (%error-bad-class who obj) (error who "bad class ~S" obj)) (define (%error-bad-generic who obj) (error who "bad generic function ~S" obj)) (define (%error-bad-method who obj) (error who "bad method ~S" obj)) (define make-closure eval) (define (specializers l) (cond ((null? l) '()) ((pair? l) (cons (if (pair? (car l)) (cadar l) ') (specializers (cdr l)))) (else '))) (define (formals l) (if (pair? l) (cons (if (pair? (car l)) (caar l) (car l)) (formals (cdr l))) l)) (define (declare-slots slots) ;; This is a *HUGE* *HACK*. Since STklos compiler displays the ;; unknown symbols, slots accessors are not known at compilation ;; time and yield a message. So, we pre-declare them here. ;; In the abolute this code is false, since the keywords :accessor ;; :getter and :setter can have a different meaning than the usual ;; one for a peculiar meta-class. Anyway, the only risk is to ;; trust that a symbol is defined whereas it is not. (for-each (lambda (s) (let ((getter (slot-definition-getter s)) (setter (slot-definition-setter s)) (accessor (slot-definition-accessor s))) (when getter (register-new-global! getter)) (when setter (register-new-global! setter)) (when accessor (register-new-global! accessor)))) slots) slots) ;-------------------------------------------------- (define (make class . l) ;; A temporary version used for bootstrapping (cond ((eq? class ) (%make class 'generic (key-get l :name '???))) ((eq? class ) (%make class 'method (list (key-get l :generic-function #f) (key-get l :specializers '()) (key-get l :procedure '())))) (else (error 'basic-make "cannot make ~S with ~S" class l)))) ;============================================================================= ; ; Access to Meta objects ; ;============================================================================= ;// ;;; ;// ;;; Methods ;// ;;; ;// (define-method method-body ((m )) ;// (let* ((spec (map class-name (slot-ref m 'specializers))) ;// (proc (procedure-body (slot-ref m 'procedure))) ;// (args (cdadr proc)) ;// (body (cddr proc))) ;// (list* 'method (map list args spec) body))) ;// ;;; ;;; Classes ;;; (define (class-name C) (if (class? C) (slot-ref C 'name) (%error-bad-class 'class-name C))) (define (class-direct-supers C) (if (class? C) (slot-ref C 'direct-supers) (%error-bad-class 'class-direct-supers C))) (define (class-direct-slots C) (if (class? C) (slot-ref C 'direct-slots) (%error-bad-class 'class-direct-slots C))) (define (class-direct-subclasses C) (if (class? C) (slot-ref C 'direct-subclasses) (%error-bad-class 'class-direct-subclasses C))) (define (class-direct-methods C) (if (class? C) (slot-ref C 'direct-methods) (%error-bad-class 'class-direct-methods C))) (define (class-precedence-list C) (if (class? C) (slot-ref C 'cpl) (%error-bad-class 'class-precedence-list C))) (define (class-slots C) (if (class? C) (slot-ref C 'slots) (%error-bad-class 'class-slots C))) ;;; ;;; Slots ;;; (define (slot-definition-name s) (if (pair? s) (car s) s)) (define (slot-definition-options s) (and (pair? s) (cdr s))) (define (slot-definition-allocation s) (if (symbol? s) :instance (key-get (cdr s) :allocation :instance))) (define (slot-definition-getter s) (and (pair? s) (key-get (cdr s) :getter #f))) (define (slot-definition-setter s) (and (pair? s) (key-get (cdr s) :setter #f))) (define (slot-definition-accessor s) (and (pair? s) (key-get (cdr s) :accessor #f))) (define (slot-definition-init-form s) (if (pair? s) (key-get (cdr s) :init-form (void)) (void))) (define (slot-definition-init-keyword s) (and (pair? s) (key-get (cdr s) :init-keyword #f))) (define (slot-init-function c s) (let ((s (slot-definition-name s))) (cadr (assq s (slot-ref c 'getters-n-setters))))) (define (class-slot-definition class slot-name) (assq slot-name (class-slots class))) ;;; ;;; Generic functions ;;; (define (generic-function-name gf) (if (generic? gf) (slot-ref gf 'name) (%error-bad-generic 'generic-function-name gf))) (define (generic-function-methods gf) (if (generic? gf) (slot-ref gf 'methods) (%error-bad-generic 'generic-function-methods gf))) (define (generic-function-documentation gf) (if (generic? gf) (slot-ref gf 'documentation) (%error-bad-generic 'generic-function-documentation gf))) ;;; ;;; Methods ;;; (define (method-generic-function m) (if (method? m) (slot-ref m 'generic-function) (%error-bad-method 'method-generic-function m))) (define (method-specializers m) (if (method? m) (slot-ref m 'specializers) (%error-bad-method 'method-specializers m))) (define (method-procedure m) (if (method? m) (slot-ref m 'procedure) (%error-bad-method 'method-procedure m))) ;============================================================================= ;; ;; is-a? ;; (define (is-a? obj class) (and (memq class (class-precedence-list (class-of obj))) #t)) ;; ;; Find-class ;; (define (find-class name :optional (default #f default?)) (let ((cls (if (symbol? name) (symbol-value* name (current-module) #f) name))) (if (is-a? cls ) cls (if default? default (error 'find-class "bad class ~S" name))))) ;; ;; %compute-slots ;; (define (%compute-slots C) (define (remove-duplicate-slots l res) (if (null? l) res (let ((s (slot-definition-name (car l)))) (unless (symbol? s) (error 'compute-slots "bad slot name ~S" s)) (remove-duplicate-slots (cdr l) (if (assq s res) res (cons (car l) res)))))) (define (build-slots-list cpl res) (if (null? cpl) (remove-duplicate-slots (reverse res) '()) (build-slots-list (cdr cpl) (append (class-direct-slots (car cpl)) res)))) (define (sort-slots l) ;; Slots are sorted such that instance slots are always before other slots. ;; So, virtual slots are placed fist and, for instance, virtual slots can have ;; an initial value which depends of real slots. ;; Note that the following "sort" just displace non-instance slots at the ;; end keeping initial order (i.e. we don't use the STklos sort procedure ;; since it is not stable. (let Loop ((l l) (instance '()) (other '())) (cond ((null? l) (append (reverse! instance) (reverse! other))) ((eq? (slot-definition-allocation (car l)) :instance) (Loop (cdr l) (cons (car l) instance) other)) (else (Loop (cdr l) instance (cons (car l) other)))))) (sort-slots (build-slots-list (class-precedence-list C) '()))) ;============================================================================= ; ; M e t a c l a s s e s s t u f f ; ;============================================================================= (define ensure-metaclass-with-supers (let ((table-of-metas '())) (lambda (meta-supers) (let ((entry (assoc meta-supers table-of-metas))) (if entry ;; Found a previously created metaclass (cdr entry) ;; Create a new meta-class which inherit from "meta-supers" (let ((new (make :dsupers meta-supers :slots '() :name (gensym "metaclass")))) (set! table-of-metas (cons (cons meta-supers new) table-of-metas)) new)))))) (define (ensure-metaclass supers) (if (null? supers) (find-class ) (let* ((all-metas (map (lambda (x) (if (is-a? x ) x (class-of (find-class x)))) supers)) (all-cpls (apply append (map (lambda (m) (cdr (class-precedence-list m))) all-metas))) (needed-metas '())) ;; Find the most specific metaclasses. The new metaclass will be ;; a subclass of these. (for-each (lambda (meta) (when (and (not (member meta all-cpls)) (not (member meta needed-metas))) (set! needed-metas (append needed-metas (list meta))))) all-metas) ;; Now return a subclass of the metaclasses we found. (if (null? (cdr needed-metas)) (car needed-metas) ; If there's only one, just use it. (ensure-metaclass-with-supers needed-metas))))) ;============================================================================= ; ; D e f i n e - c l a s s ; ;============================================================================= ;==== Define-class (define-macro (define-class name supers slots . options) `(define ,name (ensure-class ',name ; name ',supers ; supers ',(declare-slots slots) ; slots ,(or (key-get options :metaclass #f) ; metaclass `(ensure-metaclass ',supers)) ,@options))) ;==== Ensure-class (define (ensure-class name supers slots metaclass . options) (define (find-duplicate l) ; find a duplicate in a list; #f otherwise (cond ((null? l) #f) ((memq (car l) (cdr l)) (car l)) (else (find-duplicate (cdr l))))) (let ((supers (if (null? supers) (list ) (map find-class supers)))) ;; Verify that all direct slots are different and that we don't inherit ;; several time from the same class (let ((tmp (find-duplicate supers))) (when tmp (error 'define-class "super class ~S is duplicated in class ~S" tmp name))) (let ((tmp (find-duplicate (map slot-definition-name slots)))) (when tmp (error 'define-class "slot ~S is duplicated in class ~S" tmp name))) ;; Everything seems correct, build the class (let ((old (find-class name #f)) (cls (apply make metaclass :dsupers supers :slots slots :name name options))) (when old (class-redefinition old cls)) cls))) ;============================================================================= ; ; D e f i n e - g e n e r i c ; ;============================================================================= ; ==== Define-generic (define-macro (define-generic gf :optional (meta ') :key (documentation #f)) `(define ,gf (ensure-generic-function ',gf ,meta ,documentation))) ;==== Ensure-generic-function (define (ensure-generic-function name :optional (metaclass ) (documentation #f)) (let ((old (symbol-value* name (current-module) #f))) (if (generic? old) old (%symbol-define name (make metaclass :name name :default (and (procedure? old) old) :documentation documentation) (current-module))))) ;============================================================================= ; ; D e f i n e - m e t h o d ; ;============================================================================= ;==== Add-method! (define (%method-specializers-equal? gf m1 m2) (equal? (method-specializers m1) (method-specializers m2))) (define method-specializers-equal? ; will be redefined later as a method. %method-specializers-equal?) (define (add-method-in-classes! m) ;; Add method in all the classes which appears in its specializers list (for-each* (lambda (x) (let ((dm (class-direct-methods x))) (unless (memq m dm) (slot-set! x 'direct-methods (cons m dm))))) (method-specializers m))) (define (remove-method-in-classes! m) ;; Remove method in all the classes which appears in its specializers list (for-each* (lambda (x) (slot-set! x 'direct-methods (delete! m (class-direct-methods x) eq?))) (method-specializers m))) (define (compute-new-list-of-methods gf new) (let ((methods (generic-function-methods gf))) (let Loop ((l methods)) (if (null? l) (cons new methods) (if (method-specializers-equal? gf (car l) new) (begin ;; This spec. list already exists. Remove old method from dependents (remove-method-in-classes! (car l)) (set-car! l new) methods) (Loop (cdr l))))))) ;; ;; Add-method! ;; (define (add-method! gf m) (slot-set! gf 'methods (compute-new-list-of-methods gf m)) (add-method-in-classes! m) m) (define (ensure-method gf args body :optional kind-of-method) (let* ((new (extended-lambda->lambda `(method ,args ,@body))) (args (cadr new)) (body (cddr new))) `(make ,(or kind-of-method ') :generic-function ,gf :specializers (map* find-class ',(specializers args)) :procedure (let ((next-method (void))) (lambda ,(formals args) ,@body))))) ;==== Method (define-macro (method args . body) (ensure-method #f args body)) ; ==== Define-method (define-macro (define-method name args . body) (let ((gf (gensym "gf"))) `(let ((,gf (ensure-generic-function ',name))) (add-method! ,gf ,(ensure-method gf args body)) (values (void) ',name)))) ;============================================================================= ; ; Standard methods ; used by the C runtime ; ;============================================================================= ;==== Methods to compare objects (define-method object-eqv? (x y) #f) (define-method object-equal? (x y) (eqv? x y)) ;==== Methods to display/write an object ; Code for writing objects must test that the slots they use are ; bound. Otherwise a slot-unbound method will be called and will ; conduct to an infinite loop. ;; Write (define-method write-object (o file) (format file "#[instance ~A]" (number->string (address-of o) 16))) (define-method write-object ((o ) file) (let ((class (class-of o))) (if (slot-bound? class 'name) (format file "#[~A ~A]" (class-name class) (number->string (address-of o) 16)) (next-method)))) (define-method write-object((class ) file) (let ((meta (class-of class))) (if (and (slot-bound? class 'name) (slot-bound? meta 'name)) (format file "#[~A ~A ~A]" (class-name meta) (class-name class) (number->string (address-of class) 16)) (next-method)))) (define-method write-object((gf ) file) (let ((meta (class-of gf))) (if (and (slot-bound? gf 'name) (slot-bound? meta 'name) (slot-bound? gf 'methods)) (format file "#[~A ~A (~A)]" (class-name meta) (generic-function-name gf) (length (generic-function-methods gf))) (next-method)))) ;; Display (do the same thing as write by default) (define-method display-object (o file) (write-object o file)) ;==== Slot access ;; Avoid printing object itself in the error message, because it might ;; cause an infinite loop (via write-object method). Problem signaled ;; by Shiro Kawai . (define-method slot-unbound ((c ) (o ) s) (error "slot ~S is unbound in #p~A (an object of class ~S)" s (number->string (address-of o) 16) c)) (define-method slot-missing ((c ) (o ) name :optional value) (error "no slot with name `~S' in #p~A (an object of class ~S)" name (number->string (address-of o) 16) c)) ; ==== Methods for the possible error we can encounter when calling a gf (define-method no-next-method ((gf ) method args) (error "no next method for ~S in call ~S" method (cons (generic-function-name gf) args))) (define-method no-applicable-method ((gf ) args) (error "no applicable method for ~S\nin call ~S" gf (cons (generic-function-name gf) args))) (define-method no-method ((gf ) args) (error "no method defined for ~S" gf)) ;// (define-macro (next-method-exists?) ;// `((with-module STklos %next-method-exists?) next-method)) ;============================================================================= ; ; Cloning functions (from Rob Deline ) ; ;============================================================================= (define-method shallow-clone ((self )) (let ((clone (%allocate-instance (class-of self))) (slots (map slot-definition-name (class-slots (class-of self))))) (for-each (lambda (slot) (if (slot-bound? self slot) (slot-set! clone slot (slot-ref self slot)))) slots) clone)) (define-method deep-clone ((self )) (let ((clone (%allocate-instance (class-of self))) (slots (map slot-definition-name (class-slots (class-of self))))) (for-each (lambda (slot) (if (slot-bound? self slot) (slot-set! clone slot (let ((value (slot-ref self slot))) (if (instance? value) (deep-clone value) value))))) slots) clone)) ;============================================================================= ; ; Class redefinition utilities ;; ;============================================================================= ;==== Remove-class-accessors (define-method remove-class-accessors ((c )) (for-each (lambda (m) (if (is-a? m ) (remove-method-in-classes! m))) (class-direct-methods c))) ;==== Update-direct-method (define-method update-direct-method ((m ) (old ) (new )) (let Loop ((l (method-specializers m))) (when (pair? l) ; Note: the in dotted list is never used. (if (eq? (car l) old) ; So we can work if we had only proper lists. (set-car! l new)) (Loop (cdr l))))) ;==== Update-direct-subclass (define-method update-direct-subclass ((c ) (old ) (new )) (let ((new-supers (map (lambda (cls) (if (eq? cls old) new cls)) (class-direct-supers c)))) ;; Create a new class with same name as c. This will automagically call ;; class-redefinition on this subclass and redefine all its descent (ensure-class (class-name c) new-supers (class-direct-slots c) (class-of c)))) ;==== Class-redefinition (define-method class-redefinition ((old ) (new )) ;; Work on direct methods: ;; 1. Remove accessor methods from the old class ;; 2. Patch the occurences of old in the specializers by new ;; 3. Displace the methods from old to new (remove-class-accessors old) ;; -1- (let ((methods (class-direct-methods old))) (for-each (lambda (m) (update-direct-method m old new)) ;; -2- methods) (slot-set! new 'direct-methods methods)) ;; -3- ;; Remove the old class from the direct-subclasses list of its super classes (for-each (lambda (c) (slot-set! c 'direct-subclasses (delete! old (class-direct-subclasses c) eq?))) (class-direct-supers old)) ;; Redefine all the subclasses of old to take into account modification (for-each (lambda (c) (update-direct-subclass c old new)) (class-direct-subclasses old)) ;; Invalidate class so that subsequent instances slot accesses invoke ;; change-object-class (slot-set! old 'redefined new)) ;============================================================================= ; ; Utilities for INITIALIZE methods ; ;============================================================================= (define (%find-inherited-get-n-set slot-name class) (let Loop ((l (cdr (class-precedence-list class)))) (let ((r (assq slot-name (slot-ref (car l) 'getters-n-setters)))) (if r (cddr r) (Loop (cdr l)))))) (define (%direct-slot? slot-name class) (memq slot-name (map slot-definition-name (class-direct-slots class)))) (define (%make-active-getter-n-setter index keys) (let ((before-ref (make-closure (key-get keys :before-slot-ref #f))) (after-ref (make-closure (key-get keys :after-slot-ref #f))) (before-set! (make-closure (key-get keys :before-slot-set! #f))) (after-set! (make-closure (key-get keys :after-slot-set! #f)))) ;; Compute the getter and setter of an active slot at given index ;; The result is of the form '(getter setter (:before-ref ... :after-ref ...)) (list (lambda (o) (if before-ref (if (before-ref o) (let ((res (%fast-slot-ref o index))) (and after-ref (not (eqv? res (void))) (after-ref o)) res) (void)) (let ((res (%fast-slot-ref o index))) (and after-ref (not (eqv? res (void))) (after-ref o)) res))) (lambda (o v) (if before-set! (when (before-set! o v) (%fast-slot-set! o index v) (and after-set! (after-set! o v))) (begin (%fast-slot-set! o index v) (and after-set! (after-set! o v))))) (list :before-slot-ref before-ref :after-slot-ref after-ref :before-slot-set! before-set! :after-slot-set! after-set!)))) ;;; ;;; Compute-get-n-set ;;; (define-method compute-get-n-set ((class ) s) (let ((name (slot-definition-name s))) (case (slot-definition-allocation s) ((:instance) ;; Instance slot ;; get-n-set is just its offset (let ((already-allocated (slot-ref class 'nfields))) (slot-set! class 'nfields (+ already-allocated 1)) already-allocated)) ((:class) ;; Class slot ;; Class-slots accessors are implemented as 2 closures around ;; a Scheme variable. (if (%direct-slot? name class) ;; This slot is direct; create a new shared cell (let ((shared-cell (void))) (list (lambda (o) shared-cell) (lambda (o v) (set! shared-cell v)))) ;; Slot is inherited. Find its definition in superclass (%find-inherited-get-n-set name class))) ((:each-subclass) ;; slot shared by instances of direct subclass only. ;; (Thomas Buerger, April 1998) (let ((shared-cell (void))) (list (lambda (o) shared-cell) (lambda (o v) (set! shared-cell v))))) ((:virtual) ;; No allocation ;; slot-ref and slot-set! function must be given by the user (if (%direct-slot? name class) (let* ((s (slot-definition-options s)) (get (key-get s :slot-ref #f)) (set (key-get s :slot-set! #f))) (unless (and get set) (error "a :slot-ref and a :slot-set! must be supplied in ~S" s)) (list (make-closure get) (make-closure set))) (%find-inherited-get-n-set name class))) ((:active) ;; Active slot ;; active slots admit a daemon before or after slot access (let ((index (slot-ref class 'nfields))) (slot-set! class 'nfields (+ index 1)) (if (%direct-slot? name class) (%make-active-getter-n-setter index (slot-definition-options s)) (let ((old (%find-inherited-get-n-set name class))) ;; Old is of the form ;; (getter setter XXXX) ;; where XXX is (:before-slot-ref closure ...) ;; We must re-create the getter and setter since the offset ;; of the slot can be different. but we MUST NOT re-evaluate ;; the 4 daemons (they can be not exported closures). (%make-active-getter-n-setter index (list-ref old 2)))))) (else (next-method))))) (define-method compute-get-n-set ((o ) s) (error "allocation type \"~S\" is unknown" (slot-definition-allocation s))) (define (compute-slot-accessors class slots) ;; accessors are made here in-line using a light version of ensure-method ;; in particular, the class of accessors is and there ;; is no next method defined for them, since they are terminal. (define (make-reader name getter) (let ((gf (ensure-generic-function getter))) (add-method! gf (make :generic-function gf :specializers (list class) :procedure (lambda (o) (slot-ref o name)))))) (define (make-writer name setter) (let ((gf (ensure-generic-function setter))) (add-method! gf (make :generic-function gf :specializers (list class ) :procedure (lambda (o v) (slot-set! o name v)))))) ;; ;; (for-each (lambda (s) (let ((name (slot-definition-name s)) (getter (slot-definition-getter s)) (setter (slot-definition-setter s)) (accessor (slot-definition-accessor s))) (when getter (make-reader name getter)) (when setter (make-writer name setter)) (when accessor (make-reader name accessor) (make-writer name accessor)))) slots)) ;;; ;;; compute-getters-n-setters ;;; (define (compute-getters-n-setters class slots) (define (compute-slot-init-function s) (let ((init (slot-definition-init-form s))) (cond ((eq? init (void)) #f) ((eq? (slot-definition-allocation s) :class) (let ((sn (slot-definition-name s)) (o (gensym)) (val (gensym))) (make-closure `(lambda (,o) (let ((,val (%slot-ref ,o ',sn))) (if (eq? ,val (void)) ,init ,val)))))) (else (make-closure `(lambda (,(gensym)) ,init)))))) (define (verify-accessors slot l) (if (pair? l) (let ((get (car l)) (set (cadr l))) (unless (and (closure? get) (= (%procedure-arity get) 1)) (error "bad getter closure for slot `~S' in ~S: ~S" slot class get)) (unless (and (closure? set) (= (%procedure-arity set) 2)) (error "bad setter closure for slot `~S' in ~S: ~S" slot class set))))) (map (lambda (s) (let* ((s (if (pair? s) s (list s))) (g-n-s (compute-get-n-set class s)) (name (slot-definition-name s))) ;; For each slot we have ;; '(name init-function getter setter ) ;; If slot is an instance slot, we have the simplest ;; form '(name init-function . index) (verify-accessors name g-n-s) (list* name (compute-slot-init-function s) g-n-s))) slots)) ;;; ;;; compute-cpl ;;; ;;; The current implementation was provided by Anthony Beurivé ;;; . This implementation is monotonic and ;;; works "by level". For more information on monotonic superclass ;;; linearization algorithm look at the paper ;;; A Monotonic Superclass Linearization for Dylan, ;;; K. Barrett, B. Cassels, P. Haahr, D. A. Moon, K. Playford, ;;; P. Tucker Withington, ;;; OOPSLA 96. ;;; also available at ;;; http://www.webcom.com/~haahr/dylan/linearization-oopsla96.html ;;; (define (compute-cpl class) (define (reduce sequences cpl) (define (aux sequence) (if (and (pair? sequence) (memq (car sequence) cpl)) (aux (cdr sequence)) sequence)) (map aux sequences)) (define (delete-null-lists sequences) (if (pair? sequences) (let ((first (car sequences))) (if (pair? first) (cons first (delete-null-lists (cdr sequences))) (delete-null-lists (cdr sequences)))) '())) (define (select-candidate candidate sequences) (if (pair? sequences) (and (not (memq candidate (cdr (car sequences)))) (select-candidate candidate (cdr sequences))) candidate)) (define (filter-candidates candidates sequences) (if (pair? candidates) (or (select-candidate (car candidates) sequences) (filter-candidates (cdr candidates) sequences)) #f)) ; No valid candidate (inconsistency). (define (next-class sequences) (let ((candidates (map car sequences))) (or (filter-candidates candidates sequences) (car candidates)))) ; Arbitrarily brake the inconsistency. (define (step cpl sequences) (if (pair? sequences) (let ((new-cpl (cons (next-class sequences) cpl))) (step new-cpl (delete-null-lists (reduce sequences new-cpl)))) (reverse cpl))) (step '() (let ((supers (slot-ref class 'direct-supers))) (cons (cons class supers) (map (lambda (super) (slot-ref super 'cpl)) supers))))) ;============================================================================= ; ; I n i t i a l i z e ; ;============================================================================= (define-method initialize ((object ) initargs) (%initialize-object object initargs)) (define-method initialize ((class ) initargs) (next-method) (let ((dslots (key-get initargs :slots '())) (supers (key-get initargs :dsupers '()))) (slot-set! class 'name (key-get initargs :name '???)) (slot-set! class 'direct-supers supers) (slot-set! class 'direct-slots dslots) (slot-set! class 'direct-subclasses '()) (slot-set! class 'direct-methods '()) (slot-set! class 'cpl (compute-cpl class)) (slot-set! class 'redefined #f) (let ((slots (%compute-slots class))) (slot-set! class 'slots slots) (slot-set! class 'nfields 0) (slot-set! class 'getters-n-setters (compute-getters-n-setters class slots))) ;; Update the "direct-subclasses" of each inherited classes (for-each (lambda (x) (slot-set! x 'direct-subclasses (cons class (slot-ref x 'direct-subclasses)))) supers) ;; Build getters - setters - accessors (compute-slot-accessors class dslots))) (define-method initialize ((generic ) initargs) (let ((previous-definition (key-get initargs :default #f)) (doc (key-get initargs :documentation #f))) (next-method) (slot-set! generic 'name (key-get initargs :name '???)) (slot-set! generic 'methods (if (procedure? previous-definition) (list (make ; /// FAST-METHOD :specializers :generic-function generic :procedure (let ((next-methhod #f)) (lambda l (apply previous-definition l))))) '())) (slot-set! generic 'documentation doc))) (define-method initialize ((method ) initargs) (next-method) (slot-set! method 'generic-function (key-get initargs :generic-function #f)) (slot-set! method 'specializers (key-get initargs :specializers '())) (slot-set! method 'procedure (key-get initargs :procedure (lambda l '())))) ;============================================================================= ; ; M a k e ; ; A new definition which overwrite the previous one which was built-in ; ;============================================================================= (define-method allocate-instance ((class ) initargs) (%allocate-instance class)) (define-method make-instance ((class ) . initargs) (let ((instance (allocate-instance class initargs))) (initialize instance initargs) instance)) (define make make-instance) ;============================================================================= ; ; C h a n g e - c l a s s ; ;============================================================================= (define (change-object-class old-instance old-class new-class) (let ((new-instance (allocate-instance new-class ()))) ;; Initalize the slot of the new instance (for-each (lambda (slot) (if (slot-exists-using-class? old-class old-instance slot) ;; Slot was present in old instance; set it (when (slot-bound-using-class? old-class old-instance slot) (slot-set-using-class! new-class new-instance slot (slot-ref-using-class old-class old-instance slot))) ;; slot was absent; initialize it with its default value (let ((init (slot-init-function new-class slot))) (when init (slot-set-using-class! new-class new-instance slot (init new-instance)))))) (map slot-definition-name (class-slots new-class))) ;; Exchange old an new instance in place to keep pointers valids (%modify-instance old-instance new-instance) old-instance)) (define-method change-class ((old-instance ) (new-class )) (change-object-class old-instance (class-of old-instance) new-class)) ;============================================================================= ; ; a p p l y - g e n e r i c ; ; Protocol for calling standard generic functions. ; This protocol is not used for real functions (in this case we use ; a completely C hard-coded protocol). ; Apply-generic is used by STklos for calls to subclasses of . ; ; The code below is similar to the first MOP described in AMOP. In particular, ; it doesn't used the currified approach to gf call. There are 3 reasons for ; that: ; - the protocol below is exposed to mimic completely the one written in C ; - the currified protocol would be imho inefficient in C. ; - nobody (except persons really interested with playing with a MOP) will ; probably use the following code ;============================================================================= (define-method compute-applicable-methods ((gf ) args) (apply find-method gf args)) (define-method method-more-specific? ((m1 ) (m2 ) targs) (%method-more-specific? m1 m2 targs)) (define-method sort-applicable-methods ((gf ) methods args) (let ((targs (map class-of args))) (sort methods (lambda (m1 m2) (method-more-specific? m1 m2 targs))))) (define-method apply-method ((gf ) methods build-next args) (let ((proc (method-procedure (car methods)))) (%set-next-method! proc (build-next methods args)) (apply proc args))) (define-method apply-methods ((gf ) (l ) args) (letrec ((next (lambda (procs args) (lambda new-args (let ((a (if (null? new-args) args new-args))) (if (null? (cdr procs)) (no-next-method gf (car procs) a) (apply-method gf (cdr procs) next a))))))) (apply-method gf l next args))) ;; ==== apply-generic (define-method apply-generic ((gf ) args) (if (null? (generic-function-methods gf)) (no-method gf args)) (let ((methods (compute-applicable-methods gf args))) (if methods (apply-methods gf (sort-applicable-methods gf methods args) args) (no-applicable-method gf args)))) ; ====================================================================== ; Functions redefined as methods ... ; ====================================================================== (define-method method-specializers-equal? ((gf ) m1 m2) (%method-specializers-equal? gf m1 m2)) ;============================================================================= ; ; T o o l s ; ;============================================================================= (define class-subclasses #f) (define class-methods #f) (let () (define (list2set l) (let Loop ((l l) (res '())) (cond ((null? l) res) ((memq (car l) res) (Loop (cdr l) res)) (else (Loop (cdr l) (cons (car l) res)))))) (define (mapappend func . args) (if (memq '() args) '() (append (apply func (map car args)) (apply mapappend func (map cdr args))))) ;; ==== class-subclasses (set! class-subclasses (lambda (c) (letrec ((allsubs (lambda (c) (cons c (mapappend allsubs (class-direct-subclasses c)))))) (list2set (cdr (allsubs c)))))) ;; ==== class-methods (set! class-methods (lambda (c) (list2set (mapappend class-direct-methods (cons c (class-subclasses c))))))) ;==== CLOS like slot-value (but as a gf instead of proc as slot-ref or slot-set!) (define-method slot-value ((o ) s) ;; The getter (slot-ref o s)) (define-method slot-value ((o ) s v) ;; The setter (slot-set! o s v)) ;; Just tell C that the object system is now fully initialized (%object-system-initialized) ;; ====================================================================== (select-module STklos) (%redefine-module-exports (find-module 'STKLOS-OBJECT)) ;; LocalWords: supers metaclass OOPSLA linearization beurive labri fr Cassels ;; LocalWords: Beuriv Playford Withington Haahr Gregor Kickzales init