1 ;;; written by David Lichteblau, based on code by Christophe Rhodes,
4 ;;; http://www.lichteblau.com/git/?p=specializable.git;a=blob_plain;f=specializable.lisp;hb=eb30d235951c3c1d128811278760f1db36cd336c
6 (defpackage "SPECIALIZABLE"
8 (:export "SPECIALIZABLE-GENERIC-FUNCTION" "SPECIALIZABLE-METHOD"
11 "SPECIALIZER-ACCEPTS-P" "SPECIALIZER-ACCEPTS-GENERALIZER-P"
14 "GENERALIZER-OF-USING-CLASS"
15 "COMPUTE-APPLICABLE-METHODS-USING-GENERALIZERS"
16 "GENERALIZER-EQUAL-HASH-KEY"
18 "DEFINE-EXTENDED-SPECIALIZER"))
20 (in-package "SPECIALIZABLE")
22 (defclass extended-specializer (sb-mop:specializer)
23 ;; FIXME: this doesn't actually do quite what I wanted.
24 ((direct-methods-table :allocation :class
25 :initform nil :accessor direct-methods-table)))
27 (defmethod sb-mop:add-direct-method ((specializer extended-specializer) method)
28 (let* ((table (direct-methods-table specializer))
29 (cell (assoc specializer table :test #'sb-pcl::same-specializer-p)))
31 (setf cell (cons specializer nil))
32 (push cell (direct-methods-table specializer)))
33 (push method (cdr cell))))
35 (defmethod sb-mop:remove-direct-method ((specializer extended-specializer) method)
36 (let* ((table (direct-methods-table specializer))
37 (cell (assoc specializer table :test #'sb-pcl::same-specializer-p)))
38 (setf (cdr cell) (remove method (cdr cell)))))
40 (defmethod sb-mop:specializer-direct-methods ((specializer extended-specializer))
41 (cdr (assoc specializer (direct-methods-table specializer)
42 :test #'sb-pcl::same-specializer-p)))
43 (defmethod sb-mop:specializer-direct-generic-functions ((specializer extended-specializer))
44 (remove-duplicates (mapcar #'sb-mop:method-generic-function (sb-mop:specializer-direct-methods specializer))))
46 (defclass specializable-generic-function (standard-generic-function)
47 ((extended-specializers :initform (make-hash-table :test 'equal)
48 :reader generic-function-extended-specializers)
49 (emf-table :initform (make-hash-table :test 'equal) :reader emf-table))
50 (:metaclass sb-mop:funcallable-standard-class)
51 (:default-initargs :method-class (find-class 'specializable-method)))
53 (defclass specializable-method (standard-method)
54 ((lambda-expression :initarg :lambda-expression
55 :accessor specializable-method-lambda-expression)))
57 (defmacro define-extended-specializer (name (gf-var &rest args) &body body)
59 `(setf (get ',name 'extended-specializer-parser)
60 (lambda (,gf-var ,@args)
63 ;; doesn't work, because we'd have to dump GF into the fasl for the macro
65 ;;; (defun intern-extended-specializer (gf sname)
66 ;;; (destructuring-bind (kind &rest args) sname
67 ;;; (setf (gethash sname (generic-function-extended-specializers gf))
68 ;;; (apply (or (get kind 'extended-specializer-parser)
69 ;;; (error "not declared as an extended specializer name: ~A"
74 (defun make-extended-specializer (sname)
75 (destructuring-bind (kind &rest args) sname
76 (apply (or (get kind 'extended-specializer-parser)
77 (error "not declared as an extended specializer name: ~A"
79 '|This is not a generic function| ;fixme, see comment above
84 (defmethod sb-pcl:parse-specializer-using-class
85 ((gf specializable-generic-function) name)
87 ((typep name 'sb-mop:specializer) name)
88 ((symbolp name) (find-class name))
91 (eql (sb-mop:intern-eql-specializer (cadr name)))
92 (t (make-extended-specializer name))))
93 (t (error "unexpected specializer name"))))
95 (defmethod sb-pcl:make-method-specializers-form
96 ((gf specializable-generic-function) method snames env)
97 (declare (ignore method env))
100 ((typep name 'sb-mop:specializer) name)
101 ((symbolp name) `(find-class ',name))
104 (eql `(sb-mop:intern-eql-specializer ,(cadr name)))
105 (t `(make-extended-specializer ',name))))
106 (t (error "unexpected specializer name")))))
107 `(list ,@(mapcar #'parse snames))))
109 ;;; from Closette, changed to use some SBCL functions:
111 ;;; FIXME: this is not actually sufficient argument checking
112 (defun required-portion (gf args)
113 (let ((number-required
114 (sb-pcl::arg-info-number-required (sb-pcl::gf-arg-info gf))))
115 (when (< (length args) number-required)
116 (error "Too few arguments to generic function ~S." gf))
117 (subseq args 0 number-required)))
119 (defgeneric generalizer-equal-hash-key (generic-function generalizer))
120 (defmethod generalizer-equal-hash-key
121 ((gf specializable-generic-function) (g class))
122 (sb-pcl::class-wrapper g))
124 ;;; FIXME: in some kind of order, the discriminating function needs to handle:
125 ;;; - argument count checking;
126 ;;; - DONE (in effective method) keyword argument validity;
127 ;;; - DONE flushing the emf cache on method addition/removal
128 ;;; - DONE (sort of, using wrappers/g-e-h-k) flushing the cache on class redefinition;
129 ;;; - cache thread-safety.
130 (defmethod sb-mop:compute-discriminating-function ((gf specializable-generic-function))
132 (let* ((generalizers (mapcar (lambda (x) (generalizer-of-using-class gf x))
133 (required-portion gf args)))
134 (keys (mapcar (lambda (x) (generalizer-equal-hash-key gf x)) generalizers))
135 (emfun (gethash keys (emf-table gf) nil)))
137 (sb-pcl::invoke-emf emfun args)
138 (slow-method-lookup gf args generalizers)))))
140 (defmethod reinitialize-instance :after ((gf specializable-generic-function) &key)
141 (clrhash (emf-table gf)))
143 (defun slow-method-lookup (gf args generalizers)
144 ;; differs from closette
145 (multiple-value-bind (applicable-methods definitivep)
146 (compute-applicable-methods-using-generalizers gf generalizers)
149 (compute-effective-method-function gf applicable-methods))
150 (keys (mapcar (lambda (x) (generalizer-equal-hash-key gf x)) generalizers)))
151 (setf (gethash keys (emf-table gf)) emfun)
152 (sb-pcl::invoke-emf emfun args))
153 (sb-pcl::invoke-emf (compute-effective-method-function
154 gf (sb-mop:compute-applicable-methods gf args))
157 (defun compute-effective-method-function (gf methods)
158 (let* ((mc (sb-mop:generic-function-method-combination gf))
159 (sb-pcl::*applicable-methods* methods)
160 (em (sb-mop:compute-effective-method gf mc methods)))
161 (sb-pcl::make-effective-method-function gf em)))
163 ;; new, not in closette
164 (defgeneric generalizer-of-using-class (generic-function object))
165 (defmethod generalizer-of-using-class ((generic-function specializable-generic-function) object)
168 (defgeneric specializer-accepts-generalizer-p (gf specializer generalizer))
169 (defmethod specializer-accepts-generalizer-p
170 ((gf specializable-generic-function) (specializer class) (generalizer class))
171 (if (subtypep generalizer specializer)
174 (defmethod specializer-accepts-generalizer-p
175 ((gf specializable-generic-function) (specializer sb-mop:eql-specializer) (generalizer class))
176 (if (eq generalizer (class-of (sb-mop:eql-specializer-object specializer)))
180 (defgeneric compute-applicable-methods-using-generalizers (gf generalizers))
181 (defmethod compute-applicable-methods-using-generalizers
182 ((gf specializable-generic-function) generalizers)
183 ;; differs from closette
184 (let ((result-definitive-p t))
185 (flet ((filter (method)
187 (multiple-value-bind (acceptsp definitivep)
188 (specializer-accepts-generalizer-p gf s g)
190 (setf result-definitive-p nil))
192 (sb-mop:method-specializers method) generalizers))
194 (method-more-specific-p gf m1 m2 generalizers)))
197 (copy-list (remove-if-not #'filter (sb-mop:generic-function-methods gf)))
199 result-definitive-p))))
201 ;; new, not in closette
202 (defgeneric specializer-accepts-p (specializer object))
203 (defmethod specializer-accepts-p ((specializer class) object)
204 (typep object specializer))
205 (defmethod specializer-accepts-p ((specializer sb-mop:eql-specializer) object)
206 (eq object (sb-mop:eql-specializer-object specializer)))
208 (defmethod compute-applicable-methods
209 ((gf specializable-generic-function) arguments)
210 ;; new, not in closette
213 (remove-if-not #'(lambda (method)
214 (every #'specializer-accepts-p
215 (sb-mop:method-specializers method)
217 (sb-mop:generic-function-methods gf)))
218 (let ((generalizers (mapcar (lambda (x) (generalizer-of-using-class gf x))
219 (required-portion gf arguments))))
221 (method-more-specific-p gf m1 m2 generalizers)))))
223 (defun method-more-specific-p (gf method1 method2 generalizers)
224 ;; FIXME: argument precedence order
226 (mapc #'(lambda (spec1 spec2 generalizer)
227 (ecase (specializer< gf spec1 spec2 generalizer)
230 ((nil > /=) (return nil))))
231 (sb-mop:method-specializers method1)
232 (sb-mop:method-specializers method2)
236 ;; new, not in closette
237 (defgeneric specializer< (gf s1 s2 generalizer))
238 (defmethod specializer<
239 ((gf specializable-generic-function) (s1 class) (s2 class) (generalizer class))
242 (let ((cpl (sb-mop:class-precedence-list generalizer)))
243 (if (find s2 (cdr (member s1 cpl)))
246 (defmethod specializer<
247 ((gf specializable-generic-function) (s1 sb-mop:eql-specializer) (s2 sb-mop:eql-specializer) generalizer)
248 (declare (ignore generalizer))
249 (if (eq (sb-mop:eql-specializer-object s1) (sb-mop:eql-specializer-object s2))
252 (defmethod specializer< ((gf specializable-generic-function) (s1 sb-mop:eql-specializer) (s2 class) generalizer)
253 (declare (ignore generalizer))
255 (defmethod specializer< ((gf specializable-generic-function) (c1 class) (c2 sb-mop:eql-specializer) generalizer)
256 (declare (ignore generalizer))