1.0.6.38: thread and interrupt safe ADD/REMOVE-METHOD
[sbcl/simd.git] / src / pcl / methods.lisp
blob36fb2c036d5cf81616ccca1a4749f4269f436b69
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
8 ;;;; information.
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
18 ;;;; control laws.
19 ;;;;
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
22 ;;;; specification.
24 (in-package "SB-PCL")
26 ;;; methods
27 ;;;
28 ;;; Methods themselves are simple inanimate objects. Most properties of
29 ;;; methods are immutable, methods cannot be reinitialized. The following
30 ;;; properties of methods can be changed:
31 ;;; METHOD-GENERIC-FUNCTION
33 ;;; initialization
34 ;;;
35 ;;; Error checking is done in before methods. Because of the simplicity of
36 ;;; standard method objects the standard primary method can fill the slots.
37 ;;;
38 ;;; Methods are not reinitializable.
40 (define-condition metaobject-initialization-violation
41 (reference-condition simple-error)
42 ())
44 (macrolet ((def (name args control)
45 `(defmethod ,name ,args
46 (declare (ignore initargs))
47 (error 'metaobject-initialization-violation
48 :format-control ,(format nil "~@<~A~@:>" control)
49 :format-arguments (list ',name)
50 :references (list '(:amop :initialization method))))))
51 (def reinitialize-instance ((method method) &rest initargs)
52 "Method objects cannot be redefined by ~S.")
53 (def change-class ((method method) new &rest initargs)
54 "Method objects cannot be redefined by ~S.")
55 ;; NEW being a subclass of method is dealt with in the general
56 ;; method of CHANGE-CLASS
57 (def update-instance-for-redefined-class ((method method) added discarded
58 plist &rest initargs)
59 "No behaviour specified for ~S on method objects.")
60 (def update-instance-for-different-class (old (new method) &rest initargs)
61 "No behaviour specified for ~S on method objects.")
62 (def update-instance-for-different-class ((old method) new &rest initargs)
63 "No behaviour specified for ~S on method objects."))
65 (define-condition invalid-method-initarg (simple-program-error)
66 ((method :initarg :method :reader invalid-method-initarg-method))
67 (:report
68 (lambda (c s)
69 (format s "~@<In initialization of ~S:~2I~_~?~@:>"
70 (invalid-method-initarg-method c)
71 (simple-condition-format-control c)
72 (simple-condition-format-arguments c)))))
74 (defun invalid-method-initarg (method format-control &rest args)
75 (error 'invalid-method-initarg :method method
76 :format-control format-control :format-arguments args))
78 (defun check-documentation (method doc)
79 (unless (or (null doc) (stringp doc))
80 (invalid-method-initarg method "~@<~S of ~S is neither ~S nor a ~S.~@:>"
81 :documentation doc 'null 'string)))
82 (defun check-lambda-list (method ll)
83 nil)
85 (defun check-method-function (method fun)
86 (unless (functionp fun)
87 (invalid-method-initarg method "~@<~S of ~S is not a ~S.~@:>"
88 :function fun 'function)))
90 (defun check-qualifiers (method qualifiers)
91 (flet ((improper-list ()
92 (invalid-method-initarg method
93 "~@<~S of ~S is an improper list.~@:>"
94 :qualifiers qualifiers)))
95 (dolist-carefully (q qualifiers improper-list)
96 (unless (and q (atom q))
97 (invalid-method-initarg method
98 "~@<~S, in ~S ~S, is not a non-~S atom.~@:>"
99 q :qualifiers qualifiers 'null)))))
101 (defun check-slot-name (method name)
102 (unless (symbolp name)
103 (invalid-method-initarg "~@<~S of ~S is not a ~S.~@:>"
104 :slot-name name 'symbol)))
106 (defun check-specializers (method specializers)
107 (flet ((improper-list ()
108 (invalid-method-initarg method
109 "~@<~S of ~S is an improper list.~@:>"
110 :specializers specializers)))
111 (dolist-carefully (s specializers improper-list)
112 (unless (specializerp s)
113 (invalid-method-initarg method
114 "~@<~S, in ~S ~S, is not a ~S.~@:>"
115 s :specializers specializers 'specializer)))
116 ;; KLUDGE: ANSI says that it's not valid to have methods
117 ;; specializing on classes which are "not defined", leaving
118 ;; unclear what the definedness of a class is; AMOP suggests that
119 ;; forward-referenced-classes, since they have proper names and
120 ;; all, are at least worthy of some level of definition. We allow
121 ;; methods specialized on forward-referenced-classes, but it's
122 ;; non-portable and potentially dubious, so
123 (let ((frcs (remove-if-not #'forward-referenced-class-p specializers)))
124 (unless (null frcs)
125 (style-warn "~@<Defining a method using ~
126 ~V[~;~1{~S~}~;~1{~S and ~S~}~:;~{~#[~;and ~]~S~^, ~}~] ~
127 as ~2:*~V[~;a specializer~:;specializers~].~@:>"
128 (length frcs) frcs)))))
130 (defmethod shared-initialize :before
131 ((method standard-method) slot-names &key
132 qualifiers lambda-list specializers function documentation)
133 (declare (ignore slot-names))
134 ;; FIXME: it's not clear to me (CSR, 2006-08-09) why methods get
135 ;; this extra paranoia and nothing else does; either everything
136 ;; should be aggressively checking initargs, or nothing much should.
137 ;; In either case, it would probably be better to have :type
138 ;; declarations in slots, which would then give a suitable type
139 ;; error (if we implement type-checking for slots...) rather than
140 ;; this hand-crafted thing.
141 (check-qualifiers method qualifiers)
142 (check-lambda-list method lambda-list)
143 (check-specializers method specializers)
144 (check-method-function method function)
145 (check-documentation method documentation))
147 (defmethod shared-initialize :before
148 ((method standard-accessor-method) slot-names &key
149 slot-name slot-definition)
150 (declare (ignore slot-names))
151 (unless slot-definition
152 (check-slot-name method slot-name)))
154 (defmethod shared-initialize :after ((method standard-method) slot-names
155 &rest initargs &key ((method-cell method-cell)))
156 (declare (ignore slot-names method-cell))
157 (initialize-method-function initargs method))
159 (defvar *the-class-generic-function*
160 (find-class 'generic-function))
161 (defvar *the-class-standard-generic-function*
162 (find-class 'standard-generic-function))
164 (defmethod shared-initialize :before
165 ((generic-function standard-generic-function)
166 slot-names
167 &key (name nil namep)
168 (lambda-list () lambda-list-p)
169 argument-precedence-order
170 declarations
171 documentation
172 (method-class nil method-class-supplied-p)
173 (method-combination nil method-combination-supplied-p))
174 (declare (ignore slot-names
175 declarations argument-precedence-order documentation
176 lambda-list lambda-list-p))
178 (when namep
179 (set-fun-name generic-function name))
181 (flet ((initarg-error (initarg value string)
182 (error "when initializing the generic function ~S:~%~
183 The ~S initialization argument was: ~A.~%~
184 It must be ~A."
185 generic-function initarg value string)))
186 (cond (method-class-supplied-p
187 (when (symbolp method-class)
188 (setq method-class (find-class method-class)))
189 (unless (and (classp method-class)
190 (*subtypep (class-eq-specializer method-class)
191 *the-class-method*))
192 (initarg-error :method-class
193 method-class
194 "a subclass of the class METHOD"))
195 (setf (slot-value generic-function 'method-class) method-class))
196 ((slot-boundp generic-function 'method-class))
198 (initarg-error :method-class
199 "not supplied"
200 "a subclass of the class METHOD")))
201 (cond (method-combination-supplied-p
202 (unless (method-combination-p method-combination)
203 (initarg-error :method-combination
204 method-combination
205 "a method combination object")))
206 ((slot-boundp generic-function '%method-combination))
208 (initarg-error :method-combination
209 "not supplied"
210 "a method combination object")))))
212 (defun find-generic-function (name &optional (errorp t))
213 (let ((fun (and (fboundp name) (fdefinition name))))
214 (cond
215 ((and fun (typep fun 'generic-function)) fun)
216 (errorp (error "No generic function named ~S." name))
217 (t nil))))
219 (defun real-add-named-method (generic-function-name qualifiers
220 specializers lambda-list &rest other-initargs)
221 (unless (and (fboundp generic-function-name)
222 (typep (fdefinition generic-function-name) 'generic-function))
223 (style-warn "implicitly creating new generic function ~S"
224 generic-function-name))
225 (let* ((existing-gf (find-generic-function generic-function-name nil))
226 (generic-function
227 (if existing-gf
228 (ensure-generic-function
229 generic-function-name
230 :generic-function-class (class-of existing-gf))
231 (ensure-generic-function generic-function-name)))
232 (proto (method-prototype-for-gf generic-function-name)))
233 ;; FIXME: Destructive modification of &REST list.
234 (setf (getf (getf other-initargs 'plist) :name)
235 (make-method-spec generic-function qualifiers specializers))
236 (let ((new (apply #'make-instance (class-of proto)
237 :qualifiers qualifiers :specializers specializers
238 :lambda-list lambda-list other-initargs)))
239 (add-method generic-function new)
240 new)))
242 (define-condition find-method-length-mismatch
243 (reference-condition simple-error)
245 (:default-initargs :references (list '(:ansi-cl :function find-method))))
247 (defun real-get-method (generic-function qualifiers specializers
248 &optional (errorp t)
249 always-check-specializers)
250 (let ((lspec (length specializers))
251 (methods (generic-function-methods generic-function)))
252 (when (or methods always-check-specializers)
253 (let ((nreq (length (arg-info-metatypes (gf-arg-info
254 generic-function)))))
255 ;; Since we internally bypass FIND-METHOD by using GET-METHOD
256 ;; instead we need to to this here or users may get hit by a
257 ;; failed AVER instead of a sensible error message.
258 (when (/= lspec nreq)
259 (error
260 'find-method-length-mismatch
261 :format-control
262 "~@<The generic function ~S takes ~D required argument~:P; ~
263 was asked to find a method with specializers ~S~@:>"
264 :format-arguments (list generic-function nreq specializers)))))
265 (let ((hit
266 (dolist (method methods)
267 (let ((mspecializers (method-specializers method)))
268 (aver (= lspec (length mspecializers)))
269 (when (and (equal qualifiers (method-qualifiers method))
270 (every #'same-specializer-p specializers
271 (method-specializers method)))
272 (return method))))))
273 (cond (hit hit)
274 ((null errorp) nil)
276 (error "~@<There is no method on ~S with ~
277 ~:[no qualifiers~;~:*qualifiers ~S~] ~
278 and specializers ~S.~@:>"
279 generic-function qualifiers specializers))))))
281 (defmethod find-method ((generic-function standard-generic-function)
282 qualifiers specializers &optional (errorp t))
283 ;; ANSI about FIND-METHOD: "The specializers argument contains the
284 ;; parameter specializers for the method. It must correspond in
285 ;; length to the number of required arguments of the generic
286 ;; function, or an error is signaled."
288 ;; This error checking is done by REAL-GET-METHOD.
289 (real-get-method
290 generic-function qualifiers
291 ;; ANSI for FIND-METHOD seems to imply that in fact specializers
292 ;; should always be passed in parsed form instead of being parsed
293 ;; at this point. Since there's no ANSI-blessed way of getting an
294 ;; EQL specializer, that seems unnecessarily painful, so we are
295 ;; nice to our users. -- CSR, 2007-06-01
296 (parse-specializers generic-function specializers) errorp t))
298 ;;; Compute various information about a generic-function's arglist by looking
299 ;;; at the argument lists of the methods. The hair for trying not to use
300 ;;; &REST arguments lives here.
301 ;;; The values returned are:
302 ;;; number-of-required-arguments
303 ;;; the number of required arguments to this generic-function's
304 ;;; discriminating function
305 ;;; &rest-argument-p
306 ;;; whether or not this generic-function's discriminating
307 ;;; function takes an &rest argument.
308 ;;; specialized-argument-positions
309 ;;; a list of the positions of the arguments this generic-function
310 ;;; specializes (e.g. for a classical generic-function this is the
311 ;;; list: (1)).
312 (defmethod compute-discriminating-function-arglist-info
313 ((generic-function standard-generic-function))
314 ;;(declare (values number-of-required-arguments &rest-argument-p
315 ;; specialized-argument-postions))
316 (let ((number-required nil)
317 (restp nil)
318 (specialized-positions ())
319 (methods (generic-function-methods generic-function)))
320 (dolist (method methods)
321 (multiple-value-setq (number-required restp specialized-positions)
322 (compute-discriminating-function-arglist-info-internal
323 generic-function method number-required restp specialized-positions)))
324 (values number-required restp (sort specialized-positions #'<))))
326 (defun compute-discriminating-function-arglist-info-internal
327 (generic-function method number-of-requireds restp
328 specialized-argument-positions)
329 (declare (ignore generic-function)
330 (type (or null fixnum) number-of-requireds))
331 (let ((requireds 0))
332 (declare (fixnum requireds))
333 ;; Go through this methods arguments seeing how many are required,
334 ;; and whether there is an &rest argument.
335 (dolist (arg (method-lambda-list method))
336 (cond ((eq arg '&aux) (return))
337 ((memq arg '(&optional &rest &key))
338 (return (setq restp t)))
339 ((memq arg lambda-list-keywords))
340 (t (incf requireds))))
341 ;; Now go through this method's type specifiers to see which
342 ;; argument positions are type specified. Treat T specially
343 ;; in the usual sort of way. For efficiency don't bother to
344 ;; keep specialized-argument-positions sorted, rather depend
345 ;; on our caller to do that.
346 (let ((pos 0))
347 (dolist (type-spec (method-specializers method))
348 (unless (eq type-spec *the-class-t*)
349 (pushnew pos specialized-argument-positions))
350 (incf pos)))
351 ;; Finally merge the values for this method into the values
352 ;; for the exisiting methods and return them. Note that if
353 ;; num-of-requireds is NIL it means this is the first method
354 ;; and we depend on that.
355 (values (min (or number-of-requireds requireds) requireds)
356 (or restp
357 (and number-of-requireds (/= number-of-requireds requireds)))
358 specialized-argument-positions)))
360 (defun make-discriminating-function-arglist (number-required-arguments restp)
361 (nconc (let ((args nil))
362 (dotimes (i number-required-arguments)
363 (push (format-symbol *package* ;; ! is this right?
364 "Discriminating Function Arg ~D"
366 args))
367 (nreverse args))
368 (when restp
369 `(&rest ,(format-symbol *package*
370 "Discriminating Function &rest Arg")))))
372 (defmethod generic-function-argument-precedence-order
373 ((gf standard-generic-function))
374 (aver (eq *boot-state* 'complete))
375 (loop with arg-info = (gf-arg-info gf)
376 with lambda-list = (arg-info-lambda-list arg-info)
377 for argument-position in (arg-info-precedence arg-info)
378 collect (nth argument-position lambda-list)))
380 (defmethod generic-function-lambda-list ((gf generic-function))
381 (gf-lambda-list gf))
383 (defmethod gf-fast-method-function-p ((gf standard-generic-function))
384 (gf-info-fast-mf-p (slot-value gf 'arg-info)))
386 (defmethod initialize-instance :after ((gf standard-generic-function)
387 &key (lambda-list nil lambda-list-p)
388 argument-precedence-order)
389 (with-slots (arg-info) gf
390 (if lambda-list-p
391 (set-arg-info gf
392 :lambda-list lambda-list
393 :argument-precedence-order argument-precedence-order)
394 (set-arg-info gf))
395 (when (arg-info-valid-p arg-info)
396 (update-dfun gf))))
398 (defmethod reinitialize-instance :around
399 ((gf standard-generic-function) &rest args &key
400 (lambda-list nil lambda-list-p) (argument-precedence-order nil apo-p))
401 (let ((old-mc (generic-function-method-combination gf)))
402 (prog1 (call-next-method)
403 ;; KLUDGE: EQ is too strong a test.
404 (unless (eq old-mc (generic-function-method-combination gf))
405 (flush-effective-method-cache gf))
406 (cond
407 ((and lambda-list-p apo-p)
408 (set-arg-info gf
409 :lambda-list lambda-list
410 :argument-precedence-order argument-precedence-order))
411 (lambda-list-p (set-arg-info gf :lambda-list lambda-list))
412 (t (set-arg-info gf)))
413 (when (arg-info-valid-p (gf-arg-info gf))
414 (update-dfun gf))
415 (map-dependents gf (lambda (dependent)
416 (apply #'update-dependent gf dependent args))))))
418 (declaim (special *lazy-dfun-compute-p*))
420 (defun set-methods (gf methods)
421 (setf (generic-function-methods gf) nil)
422 (loop (when (null methods) (return gf))
423 (real-add-method gf (pop methods) methods)))
425 (define-condition new-value-specialization (reference-condition error)
426 ((%method :initarg :method :reader new-value-specialization-method))
427 (:report
428 (lambda (c s)
429 (format s "~@<Cannot add method ~S to ~S, as it specializes the ~
430 new-value argument.~@:>"
431 (new-value-specialization-method c)
432 #'(setf slot-value-using-class))))
433 (:default-initargs :references
434 (list '(:sbcl :node "Metaobject Protocol")
435 '(:amop :generic-function (setf slot-value-using-class)))))
437 (defun real-add-method (generic-function method &optional skip-dfun-update-p)
438 (when (method-generic-function method)
439 (error "~@<The method ~S is already part of the generic ~
440 function ~S; it can't be added to another generic ~
441 function until it is removed from the first one.~@:>"
442 method (method-generic-function method)))
443 (flet ((similar-lambda-lists-p (method-a method-b)
444 (multiple-value-bind (a-nreq a-nopt a-keyp a-restp)
445 (analyze-lambda-list (method-lambda-list method-a))
446 (multiple-value-bind (b-nreq b-nopt b-keyp b-restp)
447 (analyze-lambda-list (method-lambda-list method-b))
448 (and (= a-nreq b-nreq)
449 (= a-nopt b-nopt)
450 (eq (or a-keyp a-restp)
451 (or b-keyp b-restp)))))))
452 (let ((lock (gf-lock generic-function)))
453 ;; HANDLER-CASE takes care of releasing the lock and enabling
454 ;; interrupts before going forth with the error.
455 (handler-case
456 ;; System lock because interrupts need to be disabled as
457 ;; well: it would be bad to unwind and leave the gf in an
458 ;; inconsistent state.
459 (sb-thread::with-recursive-system-spinlock (lock)
460 (let* ((qualifiers (method-qualifiers method))
461 (specializers (method-specializers method))
462 (existing (get-method generic-function
463 qualifiers
464 specializers
465 nil)))
467 ;; If there is already a method like this one then we must get
468 ;; rid of it before proceeding. Note that we call the generic
469 ;; function REMOVE-METHOD to remove it rather than doing it in
470 ;; some internal way.
471 (when (and existing (similar-lambda-lists-p existing method))
472 (remove-method generic-function existing))
474 ;; KLUDGE: We have a special case here, as we disallow
475 ;; specializations of the NEW-VALUE argument to (SETF
476 ;; SLOT-VALUE-USING-CLASS). GET-ACCESSOR-METHOD-FUNCTION is
477 ;; the optimizing function here: it precomputes the effective
478 ;; method, assuming that there is no dispatch to be done on
479 ;; the new-value argument.
480 (when (and (eq generic-function #'(setf slot-value-using-class))
481 (not (eq *the-class-t* (first specializers))))
482 (error 'new-value-specialization :method method))
484 (setf (method-generic-function method) generic-function)
485 (pushnew method (generic-function-methods generic-function))
486 (dolist (specializer specializers)
487 (add-direct-method specializer method))
489 ;; KLUDGE: SET-ARG-INFO contains the error-detecting logic for
490 ;; detecting attempts to add methods with incongruent lambda
491 ;; lists. However, according to Gerd Moellmann on cmucl-imp,
492 ;; it also depends on the new method already having been added
493 ;; to the generic function. Therefore, we need to remove it
494 ;; again on error:
495 (let ((remove-again-p t))
496 (unwind-protect
497 (progn
498 (set-arg-info generic-function :new-method method)
499 (setq remove-again-p nil))
500 (when remove-again-p
501 (remove-method generic-function method))))
503 ;; KLUDGE II: ANSI saith that it is not an error to add a
504 ;; method with invalid qualifiers to a generic function of the
505 ;; wrong kind; it's only an error at generic function
506 ;; invocation time; I dunno what the rationale was, and it
507 ;; sucks. Nevertheless, it's probably a programmer error, so
508 ;; let's warn anyway. -- CSR, 2003-08-20
509 (let ((mc (generic-function-method-combination generic-functioN)))
510 (cond
511 ((eq mc *standard-method-combination*)
512 (when (and qualifiers
513 (or (cdr qualifiers)
514 (not (memq (car qualifiers)
515 '(:around :before :after)))))
516 (warn "~@<Invalid qualifiers for standard method ~
517 combination in method ~S:~2I~_~S.~@:>"
518 method qualifiers)))
519 ((short-method-combination-p mc)
520 (let ((mc-name (method-combination-type-name mc)))
521 (when (or (null qualifiers)
522 (cdr qualifiers)
523 (and (neq (car qualifiers) :around)
524 (neq (car qualifiers) mc-name)))
525 (warn "~@<Invalid qualifiers for ~S method combination ~
526 in method ~S:~2I~_~S.~@:>"
527 mc-name method qualifiers))))))
529 (unless skip-dfun-update-p
530 (update-ctors 'add-method
531 :generic-function generic-function
532 :method method)
533 (update-dfun generic-function))
534 (map-dependents generic-function
535 (lambda (dep)
536 (update-dependent generic-function
537 dep 'add-method method)))))
538 (serious-condition (c)
539 (error c)))))
540 generic-function)
542 (defun real-remove-method (generic-function method)
543 (when (eq generic-function (method-generic-function method))
544 (let ((lock (gf-lock generic-function)))
545 ;; System lock because interrupts need to be disabled as well:
546 ;; it would be bad to unwind and leave the gf in an inconsistent
547 ;; state.
548 (sb-thread::with-recursive-system-spinlock (lock)
549 (let* ((specializers (method-specializers method))
550 (methods (generic-function-methods generic-function))
551 (new-methods (remove method methods)))
552 (setf (method-generic-function method) nil
553 (generic-function-methods generic-function) new-methods)
554 (dolist (specializer (method-specializers method))
555 (remove-direct-method specializer method))
556 (set-arg-info generic-function)
557 (update-ctors 'remove-method
558 :generic-function generic-function
559 :method method)
560 (update-dfun generic-function)
561 (map-dependents generic-function
562 (lambda (dep)
563 (update-dependent generic-function
564 dep 'remove-method method)))))))
565 generic-function)
567 (defun compute-applicable-methods-function (generic-function arguments)
568 (values (compute-applicable-methods-using-types
569 generic-function
570 (types-from-args generic-function arguments 'eql))))
572 (defmethod compute-applicable-methods
573 ((generic-function generic-function) arguments)
574 (values (compute-applicable-methods-using-types
575 generic-function
576 (types-from-args generic-function arguments 'eql))))
578 (defmethod compute-applicable-methods-using-classes
579 ((generic-function generic-function) classes)
580 (compute-applicable-methods-using-types
581 generic-function
582 (types-from-args generic-function classes 'class-eq)))
584 (defun proclaim-incompatible-superclasses (classes)
585 (setq classes (mapcar (lambda (class)
586 (if (symbolp class)
587 (find-class class)
588 class))
589 classes))
590 (dolist (class classes)
591 (dolist (other-class classes)
592 (unless (eq class other-class)
593 (pushnew other-class (class-incompatible-superclass-list class))))))
595 (defun superclasses-compatible-p (class1 class2)
596 (let ((cpl1 (cpl-or-nil class1))
597 (cpl2 (cpl-or-nil class2)))
598 (dolist (sc1 cpl1 t)
599 (dolist (ic (class-incompatible-superclass-list sc1))
600 (when (memq ic cpl2)
601 (return-from superclasses-compatible-p nil))))))
603 (mapc
604 #'proclaim-incompatible-superclasses
605 '(;; superclass class
606 (built-in-class std-class structure-class) ; direct subclasses of pcl-class
607 (standard-class funcallable-standard-class)
608 ;; superclass metaobject
609 (class eql-specializer class-eq-specializer method method-combination
610 generic-function slot-definition)
611 ;; metaclass built-in-class
612 (number sequence character ; direct subclasses of t, but not array
613 standard-object structure-object) ; or symbol
614 (number array character symbol ; direct subclasses of t, but not
615 standard-object structure-object) ; sequence
616 (complex float rational) ; direct subclasses of number
617 (integer ratio) ; direct subclasses of rational
618 (list vector) ; direct subclasses of sequence
619 (cons null) ; direct subclasses of list
620 (string bit-vector) ; direct subclasses of vector
623 (defmethod same-specializer-p ((specl1 specializer) (specl2 specializer))
624 (eql specl1 specl2))
626 (defmethod same-specializer-p ((specl1 class) (specl2 class))
627 (eq specl1 specl2))
629 (defmethod specializer-class ((specializer class))
630 specializer)
632 (defmethod same-specializer-p ((specl1 class-eq-specializer)
633 (specl2 class-eq-specializer))
634 (eq (specializer-class specl1) (specializer-class specl2)))
636 (defmethod same-specializer-p ((specl1 eql-specializer)
637 (specl2 eql-specializer))
638 (eq (specializer-object specl1) (specializer-object specl2)))
640 (defmethod specializer-class ((specializer eql-specializer))
641 (class-of (slot-value specializer 'object)))
643 ;;; KLUDGE: this is needed to allow for user-defined specializers in
644 ;;; RAISE-METATYPE; however, the list of methods is maintained by
645 ;;; hand, which is error-prone. We can't just add a method to
646 ;;; SPECIALIZER-CLASS, or at least not with confidence, as that
647 ;;; function is used elsewhere in PCL. `STANDARD' here is used in the
648 ;;; sense of `comes with PCL' rather than `blessed by the
649 ;;; authorities'. -- CSR, 2007-05-10
650 (defmethod standard-specializer-p ((specializer class)) t)
651 (defmethod standard-specializer-p ((specializer eql-specializer)) t)
652 (defmethod standard-specializer-p ((specializer class-eq-specializer)) t)
653 (defmethod standard-specializer-p ((specializer class-prototype-specializer))
655 (defmethod standard-specializer-p ((specializer specializer)) nil)
657 (defun specializer-class-or-nil (specializer)
658 (and (standard-specializer-p specializer)
659 (specializer-class specializer)))
661 (defun error-need-at-least-n-args (function n)
662 (error 'simple-program-error
663 :format-control "~@<The function ~2I~_~S ~I~_requires ~
664 at least ~W argument~:P.~:>"
665 :format-arguments (list function n)))
667 (defun types-from-args (generic-function arguments &optional type-modifier)
668 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
669 (get-generic-fun-info generic-function)
670 (declare (ignore applyp metatypes nkeys))
671 (let ((types-rev nil))
672 (dotimes-fixnum (i nreq)
674 (unless arguments
675 (error-need-at-least-n-args (generic-function-name generic-function)
676 nreq))
677 (let ((arg (pop arguments)))
678 (push (if type-modifier `(,type-modifier ,arg) arg) types-rev)))
679 (values (nreverse types-rev) arg-info))))
681 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes)
682 (let* ((w wrappers) (w-tail w) (mt-tail metatypes))
683 (dolist (class (if (listp classes) classes (list classes)))
684 (unless (eq t (car mt-tail))
685 (let ((c-w (class-wrapper class)))
686 (unless c-w (return-from get-wrappers-from-classes nil))
687 (if (eql nkeys 1)
688 (setq w c-w)
689 (setf (car w-tail) c-w
690 w-tail (cdr w-tail)))))
691 (setq mt-tail (cdr mt-tail)))
694 (defun sdfun-for-caching (gf classes)
695 (let ((types (mapcar #'class-eq-type classes)))
696 (multiple-value-bind (methods all-applicable-and-sorted-p)
697 (compute-applicable-methods-using-types gf types)
698 (let ((generator (get-secondary-dispatch-function1
699 gf methods types nil t all-applicable-and-sorted-p)))
700 (make-callable gf methods generator
701 nil (mapcar #'class-wrapper classes))))))
703 (defun value-for-caching (gf classes)
704 (let ((methods (compute-applicable-methods-using-types
705 gf (mapcar #'class-eq-type classes))))
706 (method-plist-value (car methods) :constant-value)))
708 (defun default-secondary-dispatch-function (generic-function)
709 (lambda (&rest args)
710 (let ((methods (compute-applicable-methods generic-function args)))
711 (if methods
712 (let ((emf (get-effective-method-function generic-function
713 methods)))
714 (invoke-emf emf args))
715 (apply #'no-applicable-method generic-function args)))))
717 (defun list-eq (x y)
718 (loop (when (atom x) (return (eq x y)))
719 (when (atom y) (return nil))
720 (unless (eq (car x) (car y)) (return nil))
721 (setq x (cdr x)
722 y (cdr y))))
724 (defvar *std-cam-methods* nil)
726 (defun compute-applicable-methods-emf (generic-function)
727 (if (eq *boot-state* 'complete)
728 (let* ((cam (gdefinition 'compute-applicable-methods))
729 (cam-methods (compute-applicable-methods-using-types
730 cam (list `(eql ,generic-function) t))))
731 (values (get-effective-method-function cam cam-methods)
732 (list-eq cam-methods
733 (or *std-cam-methods*
734 (setq *std-cam-methods*
735 (compute-applicable-methods-using-types
736 cam (list `(eql ,cam) t)))))))
737 (values #'compute-applicable-methods-function t)))
739 (defun compute-applicable-methods-emf-std-p (gf)
740 (gf-info-c-a-m-emf-std-p (gf-arg-info gf)))
742 (defvar *old-c-a-m-gf-methods* nil)
744 (defun update-all-c-a-m-gf-info (c-a-m-gf)
745 (let ((methods (generic-function-methods c-a-m-gf)))
746 (if (and *old-c-a-m-gf-methods*
747 (every (lambda (old-method)
748 (member old-method methods))
749 *old-c-a-m-gf-methods*))
750 (let ((gfs-to-do nil)
751 (gf-classes-to-do nil))
752 (dolist (method methods)
753 (unless (member method *old-c-a-m-gf-methods*)
754 (let ((specl (car (method-specializers method))))
755 (if (eql-specializer-p specl)
756 (pushnew (specializer-object specl) gfs-to-do)
757 (pushnew (specializer-class specl) gf-classes-to-do)))))
758 (map-all-generic-functions
759 (lambda (gf)
760 (when (or (member gf gfs-to-do)
761 (dolist (class gf-classes-to-do nil)
762 (member class
763 (class-precedence-list (class-of gf)))))
764 (update-c-a-m-gf-info gf)))))
765 (map-all-generic-functions #'update-c-a-m-gf-info))
766 (setq *old-c-a-m-gf-methods* methods)))
768 (defun update-gf-info (gf)
769 (update-c-a-m-gf-info gf)
770 (update-gf-simple-accessor-type gf))
772 (defun update-c-a-m-gf-info (gf)
773 (unless (early-gf-p gf)
774 (multiple-value-bind (c-a-m-emf std-p)
775 (compute-applicable-methods-emf gf)
776 (let ((arg-info (gf-arg-info gf)))
777 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
778 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)))))
780 (defun update-gf-simple-accessor-type (gf)
781 (let ((arg-info (gf-arg-info gf)))
782 (setf (gf-info-simple-accessor-type arg-info)
783 (let* ((methods (generic-function-methods gf))
784 (class (and methods (class-of (car methods))))
785 (type
786 (and class
787 (cond ((or (eq class *the-class-standard-reader-method*)
788 (eq class *the-class-global-reader-method*))
789 'reader)
790 ((or (eq class *the-class-standard-writer-method*)
791 (eq class *the-class-global-writer-method*))
792 'writer)
793 ((or (eq class *the-class-standard-boundp-method*)
794 (eq class *the-class-global-boundp-method*))
795 'boundp)))))
796 (when (and (gf-info-c-a-m-emf-std-p arg-info)
797 type
798 (dolist (method (cdr methods) t)
799 (unless (eq class (class-of method)) (return nil)))
800 (eq (generic-function-method-combination gf)
801 *standard-method-combination*))
802 type)))))
805 ;;; CMUCL (Gerd's PCL, 2002-04-25) comment:
807 ;;; Return two values. First value is a function to be stored in
808 ;;; effective slot definition SLOTD for reading it with
809 ;;; SLOT-VALUE-USING-CLASS, setting it with (SETF
810 ;;; SLOT-VALUE-USING-CLASS) or testing it with
811 ;;; SLOT-BOUNDP-USING-CLASS. GF is one of these generic functions,
812 ;;; TYPE is one of the symbols READER, WRITER, BOUNDP. CLASS is
813 ;;; SLOTD's class.
815 ;;; Second value is true if the function returned is one of the
816 ;;; optimized standard functions for the purpose, which are used
817 ;;; when only standard methods are applicable.
819 ;;; FIXME: Change all these wacky function names to something sane.
820 (defun get-accessor-method-function (gf type class slotd)
821 (let* ((std-method (standard-svuc-method type))
822 (str-method (structure-svuc-method type))
823 (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
824 (types (if (eq type 'writer) `(t ,@types1) types1))
825 (methods (compute-applicable-methods-using-types gf types))
826 (std-p (null (cdr methods))))
827 (values
828 (if std-p
829 (get-optimized-std-accessor-method-function class slotd type)
830 (let* ((optimized-std-fun
831 (get-optimized-std-slot-value-using-class-method-function
832 class slotd type))
833 (method-alist
834 `((,(car (or (member std-method methods)
835 (member str-method methods)
836 (bug "error in ~S"
837 'get-accessor-method-function)))
838 ,optimized-std-fun)))
839 (wrappers
840 (let ((wrappers (list (wrapper-of class)
841 (class-wrapper class)
842 (wrapper-of slotd))))
843 (if (eq type 'writer)
844 (cons (class-wrapper *the-class-t*) wrappers)
845 wrappers)))
846 (sdfun (get-secondary-dispatch-function
847 gf methods types method-alist wrappers)))
848 (get-accessor-from-svuc-method-function class slotd sdfun type)))
849 std-p)))
851 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
852 (defun update-slot-value-gf-info (gf type)
853 (unless *new-class*
854 (update-std-or-str-methods gf type))
855 (when (and (standard-svuc-method type) (structure-svuc-method type))
856 (flet ((update-accessor-info (class)
857 (when (class-finalized-p class)
858 (dolist (slotd (class-slots class))
859 (compute-slot-accessor-info slotd type gf)))))
860 (if *new-class*
861 (update-accessor-info *new-class*)
862 (map-all-classes #'update-accessor-info 'slot-object)))))
864 (defvar *standard-slot-value-using-class-method* nil)
865 (defvar *standard-setf-slot-value-using-class-method* nil)
866 (defvar *standard-slot-boundp-using-class-method* nil)
867 (defvar *condition-slot-value-using-class-method* nil)
868 (defvar *condition-setf-slot-value-using-class-method* nil)
869 (defvar *condition-slot-boundp-using-class-method* nil)
870 (defvar *structure-slot-value-using-class-method* nil)
871 (defvar *structure-setf-slot-value-using-class-method* nil)
872 (defvar *structure-slot-boundp-using-class-method* nil)
874 (defun standard-svuc-method (type)
875 (case type
876 (reader *standard-slot-value-using-class-method*)
877 (writer *standard-setf-slot-value-using-class-method*)
878 (boundp *standard-slot-boundp-using-class-method*)))
880 (defun set-standard-svuc-method (type method)
881 (case type
882 (reader (setq *standard-slot-value-using-class-method* method))
883 (writer (setq *standard-setf-slot-value-using-class-method* method))
884 (boundp (setq *standard-slot-boundp-using-class-method* method))))
886 (defun condition-svuc-method (type)
887 (case type
888 (reader *condition-slot-value-using-class-method*)
889 (writer *condition-setf-slot-value-using-class-method*)
890 (boundp *condition-slot-boundp-using-class-method*)))
892 (defun set-condition-svuc-method (type method)
893 (case type
894 (reader (setq *condition-slot-value-using-class-method* method))
895 (writer (setq *condition-setf-slot-value-using-class-method* method))
896 (boundp (setq *condition-slot-boundp-using-class-method* method))))
898 (defun structure-svuc-method (type)
899 (case type
900 (reader *structure-slot-value-using-class-method*)
901 (writer *structure-setf-slot-value-using-class-method*)
902 (boundp *structure-slot-boundp-using-class-method*)))
904 (defun set-structure-svuc-method (type method)
905 (case type
906 (reader (setq *structure-slot-value-using-class-method* method))
907 (writer (setq *structure-setf-slot-value-using-class-method* method))
908 (boundp (setq *structure-slot-boundp-using-class-method* method))))
910 (defun update-std-or-str-methods (gf type)
911 (dolist (method (generic-function-methods gf))
912 (let ((specls (method-specializers method)))
913 (when (and (or (not (eq type 'writer))
914 (eq (pop specls) *the-class-t*))
915 (every #'classp specls))
916 (cond ((and (eq (class-name (car specls)) 'std-class)
917 (eq (class-name (cadr specls)) 'standard-object)
918 (eq (class-name (caddr specls))
919 'standard-effective-slot-definition))
920 (set-standard-svuc-method type method))
921 ((and (eq (class-name (car specls)) 'condition-class)
922 (eq (class-name (cadr specls)) 'condition)
923 (eq (class-name (caddr specls))
924 'condition-effective-slot-definition))
925 (set-condition-svuc-method type method))
926 ((and (eq (class-name (car specls)) 'structure-class)
927 (eq (class-name (cadr specls)) 'structure-object)
928 (eq (class-name (caddr specls))
929 'structure-effective-slot-definition))
930 (set-structure-svuc-method type method)))))))
932 (defun mec-all-classes-internal (spec precompute-p)
933 (let ((wrapper (class-wrapper (specializer-class spec))))
934 (unless (or (not wrapper) (invalid-wrapper-p wrapper))
935 (cons (specializer-class spec)
936 (and (classp spec)
937 precompute-p
938 (not (or (eq spec *the-class-t*)
939 (eq spec *the-class-slot-object*)
940 (eq spec *the-class-standard-object*)
941 (eq spec *the-class-structure-object*)))
942 (let ((sc (class-direct-subclasses spec)))
943 (when sc
944 (mapcan (lambda (class)
945 (mec-all-classes-internal class precompute-p))
946 sc))))))))
948 (defun mec-all-classes (spec precompute-p)
949 (let ((classes (mec-all-classes-internal spec precompute-p)))
950 (if (null (cdr classes))
951 classes
952 (let* ((a-classes (cons nil classes))
953 (tail classes))
954 (loop (when (null (cdr tail))
955 (return (cdr a-classes)))
956 (let ((class (cadr tail))
957 (ttail (cddr tail)))
958 (if (dolist (c ttail nil)
959 (when (eq class c) (return t)))
960 (setf (cdr tail) (cddr tail))
961 (setf tail (cdr tail)))))))))
963 (defun mec-all-class-lists (spec-list precompute-p)
964 (if (null spec-list)
965 (list nil)
966 (let* ((car-all-classes (mec-all-classes (car spec-list)
967 precompute-p))
968 (all-class-lists (mec-all-class-lists (cdr spec-list)
969 precompute-p)))
970 (mapcan (lambda (list)
971 (mapcar (lambda (c) (cons c list)) car-all-classes))
972 all-class-lists))))
974 (defun make-emf-cache (generic-function valuep cache classes-list new-class)
975 (let* ((arg-info (gf-arg-info generic-function))
976 (nkeys (arg-info-nkeys arg-info))
977 (metatypes (arg-info-metatypes arg-info))
978 (wrappers (unless (eq nkeys 1) (make-list nkeys)))
979 (precompute-p (gf-precompute-dfun-and-emf-p arg-info)))
980 (flet ((add-class-list (classes)
981 (when (or (null new-class) (memq new-class classes))
982 (let ((%wrappers (get-wrappers-from-classes
983 nkeys wrappers classes metatypes)))
984 (when (and %wrappers (not (probe-cache cache %wrappers)))
985 (let ((value (cond ((eq valuep t)
986 (sdfun-for-caching generic-function
987 classes))
988 ((eq valuep :constant-value)
989 (value-for-caching generic-function
990 classes)))))
991 ;; need to get them again, as finalization might
992 ;; have happened in between, which would
993 ;; invalidate wrappers.
994 (let ((wrappers (get-wrappers-from-classes
995 nkeys wrappers classes metatypes)))
996 (when (if (atom wrappers)
997 (not (invalid-wrapper-p wrappers))
998 (every (complement #'invalid-wrapper-p)
999 wrappers))
1000 (setq cache (fill-cache cache wrappers value))))))))))
1001 (if classes-list
1002 (mapc #'add-class-list classes-list)
1003 (dolist (method (generic-function-methods generic-function))
1004 (mapc #'add-class-list
1005 (mec-all-class-lists (method-specializers method)
1006 precompute-p))))
1007 cache)))
1009 (defmacro class-test (arg class)
1010 (cond
1011 ((eq class *the-class-t*) t)
1012 ((eq class *the-class-slot-object*)
1013 `(not (typep (classoid-of ,arg) 'built-in-classoid)))
1014 ((eq class *the-class-standard-object*)
1015 `(or (std-instance-p ,arg) (fsc-instance-p ,arg)))
1016 ((eq class *the-class-funcallable-standard-object*)
1017 `(fsc-instance-p ,arg))
1019 `(typep ,arg ',(class-name class)))))
1021 (defmacro class-eq-test (arg class)
1022 `(eq (class-of ,arg) ',class))
1024 (defmacro eql-test (arg object)
1025 `(eql ,arg ',object))
1027 (defun dnet-methods-p (form)
1028 (and (consp form)
1029 (or (eq (car form) 'methods)
1030 (eq (car form) 'unordered-methods))))
1032 ;;; This is CASE, but without gensyms.
1033 (defmacro scase (arg &rest clauses)
1034 `(let ((.case-arg. ,arg))
1035 (cond ,@(mapcar (lambda (clause)
1036 (list* (cond ((null (car clause))
1037 nil)
1038 ((consp (car clause))
1039 (if (null (cdar clause))
1040 `(eql .case-arg.
1041 ',(caar clause))
1042 `(member .case-arg.
1043 ',(car clause))))
1044 ((member (car clause) '(t otherwise))
1047 `(eql .case-arg. ',(car clause))))
1049 (cdr clause)))
1050 clauses))))
1052 (defmacro mcase (arg &rest clauses) `(scase ,arg ,@clauses))
1054 (defun generate-discrimination-net (generic-function methods types sorted-p)
1055 (let* ((arg-info (gf-arg-info generic-function))
1056 (c-a-m-emf-std-p (gf-info-c-a-m-emf-std-p arg-info))
1057 (precedence (arg-info-precedence arg-info)))
1058 (generate-discrimination-net-internal
1059 generic-function methods types
1060 (lambda (methods known-types)
1061 (if (or sorted-p
1062 (and c-a-m-emf-std-p
1063 (block one-order-p
1064 (let ((sorted-methods nil))
1065 (map-all-orders
1066 (copy-list methods) precedence
1067 (lambda (methods)
1068 (when sorted-methods (return-from one-order-p nil))
1069 (setq sorted-methods methods)))
1070 (setq methods sorted-methods))
1071 t)))
1072 `(methods ,methods ,known-types)
1073 `(unordered-methods ,methods ,known-types)))
1074 (lambda (position type true-value false-value)
1075 (let ((arg (dfun-arg-symbol position)))
1076 (if (eq (car type) 'eql)
1077 (let* ((false-case-p (and (consp false-value)
1078 (or (eq (car false-value) 'scase)
1079 (eq (car false-value) 'mcase))
1080 (eq arg (cadr false-value))))
1081 (false-clauses (if false-case-p
1082 (cddr false-value)
1083 `((t ,false-value))))
1084 (case-sym (if (and (dnet-methods-p true-value)
1085 (if false-case-p
1086 (eq (car false-value) 'mcase)
1087 (dnet-methods-p false-value)))
1088 'mcase
1089 'scase))
1090 (type-sym `(,(cadr type))))
1091 `(,case-sym ,arg
1092 (,type-sym ,true-value)
1093 ,@false-clauses))
1094 `(if ,(let ((arg (dfun-arg-symbol position)))
1095 (case (car type)
1096 (class `(class-test ,arg ,(cadr type)))
1097 (class-eq `(class-eq-test ,arg ,(cadr type)))))
1098 ,true-value
1099 ,false-value))))
1100 #'identity)))
1102 (defun class-from-type (type)
1103 (if (or (atom type) (eq (car type) t))
1104 *the-class-t*
1105 (case (car type)
1106 (and (dolist (type (cdr type) *the-class-t*)
1107 (when (and (consp type) (not (eq (car type) 'not)))
1108 (return (class-from-type type)))))
1109 (not *the-class-t*)
1110 (eql (class-of (cadr type)))
1111 (class-eq (cadr type))
1112 (class (cadr type)))))
1114 (defun precompute-effective-methods (gf caching-p &optional classes-list-p)
1115 (let* ((arg-info (gf-arg-info gf))
1116 (methods (generic-function-methods gf))
1117 (precedence (arg-info-precedence arg-info))
1118 (*in-precompute-effective-methods-p* t)
1119 (classes-list nil))
1120 (generate-discrimination-net-internal
1121 gf methods nil
1122 (lambda (methods known-types)
1123 (when methods
1124 (when classes-list-p
1125 (push (mapcar #'class-from-type known-types) classes-list))
1126 (let ((no-eql-specls-p (not (methods-contain-eql-specializer-p
1127 methods))))
1128 (map-all-orders
1129 methods precedence
1130 (lambda (methods)
1131 (get-secondary-dispatch-function1
1132 gf methods known-types
1133 nil caching-p no-eql-specls-p))))))
1134 (lambda (position type true-value false-value)
1135 (declare (ignore position type true-value false-value))
1136 nil)
1137 (lambda (type)
1138 (if (and (consp type) (eq (car type) 'eql))
1139 `(class-eq ,(class-of (cadr type)))
1140 type)))
1141 classes-list))
1143 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1144 (defun augment-type (new-type known-type)
1145 (if (or (eq known-type t)
1146 (eq (car new-type) 'eql))
1147 new-type
1148 (let ((so-far (if (and (consp known-type) (eq (car known-type) 'and))
1149 (cdr known-type)
1150 (list known-type))))
1151 (unless (eq (car new-type) 'not)
1152 (setq so-far
1153 (mapcan (lambda (type)
1154 (unless (*subtypep new-type type)
1155 (list type)))
1156 so-far)))
1157 (if (null so-far)
1158 new-type
1159 `(and ,new-type ,@so-far)))))
1161 (defun generate-discrimination-net-internal
1162 (gf methods types methods-function test-fun type-function)
1163 (let* ((arg-info (gf-arg-info gf))
1164 (precedence (arg-info-precedence arg-info))
1165 (nreq (arg-info-number-required arg-info))
1166 (metatypes (arg-info-metatypes arg-info)))
1167 (labels ((do-column (p-tail contenders known-types)
1168 (if p-tail
1169 (let* ((position (car p-tail))
1170 (known-type (or (nth position types) t)))
1171 (if (eq (nth position metatypes) t)
1172 (do-column (cdr p-tail) contenders
1173 (cons (cons position known-type)
1174 known-types))
1175 (do-methods p-tail contenders
1176 known-type () known-types)))
1177 (funcall methods-function contenders
1178 (let ((k-t (make-list nreq)))
1179 (dolist (index+type known-types)
1180 (setf (nth (car index+type) k-t)
1181 (cdr index+type)))
1182 k-t))))
1183 (do-methods (p-tail contenders known-type winners known-types)
1184 ;; CONTENDERS
1185 ;; is a (sorted) list of methods that must be discriminated.
1186 ;; KNOWN-TYPE
1187 ;; is the type of this argument, constructed from tests
1188 ;; already made.
1189 ;; WINNERS
1190 ;; is a (sorted) list of methods that are potentially
1191 ;; applicable after the discrimination has been made.
1192 (if (null contenders)
1193 (do-column (cdr p-tail)
1194 winners
1195 (cons (cons (car p-tail) known-type)
1196 known-types))
1197 (let* ((position (car p-tail))
1198 (method (car contenders))
1199 (specl (nth position (method-specializers method)))
1200 (type (funcall type-function
1201 (type-from-specializer specl))))
1202 (multiple-value-bind (app-p maybe-app-p)
1203 (specializer-applicable-using-type-p type known-type)
1204 (flet ((determined-to-be (truth-value)
1205 (if truth-value app-p (not maybe-app-p)))
1206 (do-if (truth &optional implied)
1207 (let ((ntype (if truth type `(not ,type))))
1208 (do-methods p-tail
1209 (cdr contenders)
1210 (if implied
1211 known-type
1212 (augment-type ntype known-type))
1213 (if truth
1214 (append winners `(,method))
1215 winners)
1216 known-types))))
1217 (cond ((determined-to-be nil) (do-if nil t))
1218 ((determined-to-be t) (do-if t t))
1219 (t (funcall test-fun position type
1220 (do-if t) (do-if nil))))))))))
1221 (do-column precedence methods ()))))
1223 (defun compute-secondary-dispatch-function (generic-function net &optional
1224 method-alist wrappers)
1225 (function-funcall (compute-secondary-dispatch-function1 generic-function net)
1226 method-alist wrappers))
1228 (defvar *eq-case-table-limit* 15)
1229 (defvar *case-table-limit* 10)
1231 (defun compute-mcase-parameters (case-list)
1232 (unless (eq t (caar (last case-list)))
1233 (error "The key for the last case arg to mcase was not T"))
1234 (let* ((eq-p (dolist (case case-list t)
1235 (unless (or (eq (car case) t)
1236 (symbolp (caar case)))
1237 (return nil))))
1238 (len (1- (length case-list)))
1239 (type (cond ((= len 1)
1240 :simple)
1241 ((<= len
1242 (if eq-p
1243 *eq-case-table-limit*
1244 *case-table-limit*))
1245 :assoc)
1247 :hash-table))))
1248 (list eq-p type)))
1250 (defmacro mlookup (key info default &optional eq-p type)
1251 (unless (or (eq eq-p t) (null eq-p))
1252 (bug "Invalid eq-p argument: ~S" eq-p))
1253 (ecase type
1254 (:simple
1255 `(if (locally
1256 (declare (optimize (inhibit-warnings 3)))
1257 (,(if eq-p 'eq 'eql) ,key (car ,info)))
1258 (cdr ,info)
1259 ,default))
1260 (:assoc
1261 `(dolist (e ,info ,default)
1262 (when (locally
1263 (declare (optimize (inhibit-warnings 3)))
1264 (,(if eq-p 'eq 'eql) (car e) ,key))
1265 (return (cdr e)))))
1266 (:hash-table
1267 `(gethash ,key ,info ,default))))
1269 (defun net-test-converter (form)
1270 (if (atom form)
1271 (default-test-converter form)
1272 (case (car form)
1273 ((invoke-effective-method-function invoke-fast-method-call
1274 invoke-effective-narrow-method-function)
1275 '.call.)
1276 (methods
1277 '.methods.)
1278 (unordered-methods
1279 '.umethods.)
1280 (mcase
1281 `(mlookup ,(cadr form)
1284 ,@(compute-mcase-parameters (cddr form))))
1285 (t (default-test-converter form)))))
1287 (defun net-code-converter (form)
1288 (if (atom form)
1289 (default-code-converter form)
1290 (case (car form)
1291 ((methods unordered-methods)
1292 (let ((gensym (gensym)))
1293 (values gensym
1294 (list gensym))))
1295 (mcase
1296 (let ((mp (compute-mcase-parameters (cddr form)))
1297 (gensym (gensym)) (default (gensym)))
1298 (values `(mlookup ,(cadr form) ,gensym ,default ,@mp)
1299 (list gensym default))))
1301 (default-code-converter form)))))
1303 (defun net-constant-converter (form generic-function)
1304 (or (let ((c (methods-converter form generic-function)))
1305 (when c (list c)))
1306 (if (atom form)
1307 (default-constant-converter form)
1308 (case (car form)
1309 (mcase
1310 (let* ((mp (compute-mcase-parameters (cddr form)))
1311 (list (mapcar (lambda (clause)
1312 (let ((key (car clause))
1313 (meth (cadr clause)))
1314 (cons (if (consp key) (car key) key)
1315 (methods-converter
1316 meth generic-function))))
1317 (cddr form)))
1318 (default (car (last list))))
1319 (list (list* :mcase mp (nbutlast list))
1320 (cdr default))))
1322 (default-constant-converter form))))))
1324 (defun methods-converter (form generic-function)
1325 (cond ((and (consp form) (eq (car form) 'methods))
1326 (cons '.methods.
1327 (get-effective-method-function1 generic-function (cadr form))))
1328 ((and (consp form) (eq (car form) 'unordered-methods))
1329 (default-secondary-dispatch-function generic-function))))
1331 (defun convert-methods (constant method-alist wrappers)
1332 (if (and (consp constant)
1333 (eq (car constant) '.methods.))
1334 (funcall (cdr constant) method-alist wrappers)
1335 constant))
1337 (defun convert-table (constant method-alist wrappers)
1338 (cond ((and (consp constant)
1339 (eq (car constant) :mcase))
1340 (let ((alist (mapcar (lambda (k+m)
1341 (cons (car k+m)
1342 (convert-methods (cdr k+m)
1343 method-alist
1344 wrappers)))
1345 (cddr constant)))
1346 (mp (cadr constant)))
1347 (ecase (cadr mp)
1348 (:simple
1349 (car alist))
1350 (:assoc
1351 alist)
1352 (:hash-table
1353 (let ((table (make-hash-table :test (if (car mp) 'eq 'eql))))
1354 (dolist (k+m alist)
1355 (setf (gethash (car k+m) table) (cdr k+m)))
1356 table)))))))
1358 (defun compute-secondary-dispatch-function1 (generic-function net
1359 &optional function-p)
1360 (cond
1361 ((and (eq (car net) 'methods) (not function-p))
1362 (get-effective-method-function1 generic-function (cadr net)))
1364 (let* ((name (generic-function-name generic-function))
1365 (arg-info (gf-arg-info generic-function))
1366 (metatypes (arg-info-metatypes arg-info))
1367 (nargs (length metatypes))
1368 (applyp (arg-info-applyp arg-info))
1369 (fmc-arg-info (cons nargs applyp))
1370 (arglist (if function-p
1371 (make-dfun-lambda-list nargs applyp)
1372 (make-fast-method-call-lambda-list nargs applyp))))
1373 (multiple-value-bind (cfunction constants)
1374 (get-fun1 `(lambda
1375 ,arglist
1376 ,@(unless function-p
1377 `((declare (ignore .pv-cell. .next-method-call.))))
1378 (locally (declare #.*optimize-speed*)
1379 (let ((emf ,net))
1380 ,(make-emf-call nargs applyp 'emf))))
1381 #'net-test-converter
1382 #'net-code-converter
1383 (lambda (form)
1384 (net-constant-converter form generic-function)))
1385 (lambda (method-alist wrappers)
1386 (let* ((alist (list nil))
1387 (alist-tail alist))
1388 (dolist (constant constants)
1389 (let* ((a (or (dolist (a alist nil)
1390 (when (eq (car a) constant)
1391 (return a)))
1392 (cons constant
1393 (or (convert-table
1394 constant method-alist wrappers)
1395 (convert-methods
1396 constant method-alist wrappers)))))
1397 (new (list a)))
1398 (setf (cdr alist-tail) new)
1399 (setf alist-tail new)))
1400 (let ((function (apply cfunction (mapcar #'cdr (cdr alist)))))
1401 (if function-p
1402 function
1403 (make-fast-method-call
1404 :function (set-fun-name function `(sdfun-method ,name))
1405 :arg-info fmc-arg-info))))))))))
1407 (defvar *show-make-unordered-methods-emf-calls* nil)
1409 (defun make-unordered-methods-emf (generic-function methods)
1410 (when *show-make-unordered-methods-emf-calls*
1411 (format t "~&make-unordered-methods-emf ~S~%"
1412 (generic-function-name generic-function)))
1413 (lambda (&rest args)
1414 (let* ((types (types-from-args generic-function args 'eql))
1415 (smethods (sort-applicable-methods generic-function
1416 methods
1417 types))
1418 (emf (get-effective-method-function generic-function smethods)))
1419 (invoke-emf emf args))))
1421 ;;; The value returned by compute-discriminating-function is a function
1422 ;;; object. It is called a discriminating function because it is called
1423 ;;; when the generic function is called and its role is to discriminate
1424 ;;; on the arguments to the generic function and then call appropriate
1425 ;;; method functions.
1427 ;;; A discriminating function can only be called when it is installed as
1428 ;;; the funcallable instance function of the generic function for which
1429 ;;; it was computed.
1431 ;;; More precisely, if compute-discriminating-function is called with
1432 ;;; an argument <gf1>, and returns a result <df1>, that result must
1433 ;;; not be passed to apply or funcall directly. Rather, <df1> must be
1434 ;;; stored as the funcallable instance function of the same generic
1435 ;;; function <gf1> (using SET-FUNCALLABLE-INSTANCE-FUNCTION). Then the
1436 ;;; generic function can be passed to funcall or apply.
1438 ;;; An important exception is that methods on this generic function are
1439 ;;; permitted to return a function which itself ends up calling the value
1440 ;;; returned by a more specific method. This kind of `encapsulation' of
1441 ;;; discriminating function is critical to many uses of the MOP.
1443 ;;; As an example, the following canonical case is legal:
1445 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1446 ;;; (let ((std (call-next-method)))
1447 ;;; (lambda (arg)
1448 ;;; (print (list 'call-to-gf gf arg))
1449 ;;; (funcall std arg))))
1451 ;;; Because many discriminating functions would like to use a dynamic
1452 ;;; strategy in which the precise discriminating function changes with
1453 ;;; time it is important to specify how a discriminating function is
1454 ;;; permitted itself to change the funcallable instance function of the
1455 ;;; generic function.
1457 ;;; Discriminating functions may set the funcallable instance function
1458 ;;; of the generic function, but the new value must be generated by making
1459 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1460 ;;; more specific methods which may have encapsulated the discriminating
1461 ;;; function will get a chance to encapsulate the new, inner discriminating
1462 ;;; function.
1464 ;;; This implies that if a discriminating function wants to modify itself
1465 ;;; it should first store some information in the generic function proper,
1466 ;;; and then call compute-discriminating-function. The appropriate method
1467 ;;; on compute-discriminating-function will see the information stored in
1468 ;;; the generic function and generate a discriminating function accordingly.
1470 ;;; The following is an example of a discriminating function which modifies
1471 ;;; itself in accordance with this protocol:
1473 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1474 ;;; (lambda (arg)
1475 ;;; (cond (<some condition>
1476 ;;; <store some info in the generic function>
1477 ;;; (set-funcallable-instance-function
1478 ;;; gf
1479 ;;; (compute-discriminating-function gf))
1480 ;;; (funcall gf arg))
1481 ;;; (t
1482 ;;; <call-a-method-of-gf>))))
1484 ;;; Whereas this code would not be legal:
1486 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1487 ;;; (lambda (arg)
1488 ;;; (cond (<some condition>
1489 ;;; (set-funcallable-instance-function
1490 ;;; gf
1491 ;;; (lambda (a) ..))
1492 ;;; (funcall gf arg))
1493 ;;; (t
1494 ;;; <call-a-method-of-gf>))))
1496 ;;; NOTE: All the examples above assume that all instances of the class
1497 ;;; my-generic-function accept only one argument.
1499 (defun slot-value-using-class-dfun (class object slotd)
1500 (declare (ignore class))
1501 (function-funcall (slot-definition-reader-function slotd) object))
1503 (defun setf-slot-value-using-class-dfun (new-value class object slotd)
1504 (declare (ignore class))
1505 (function-funcall (slot-definition-writer-function slotd) new-value object))
1507 (defun slot-boundp-using-class-dfun (class object slotd)
1508 (declare (ignore class))
1509 (function-funcall (slot-definition-boundp-function slotd) object))
1511 (defun special-case-for-compute-discriminating-function-p (gf)
1512 (or (eq gf #'slot-value-using-class)
1513 (eq gf #'(setf slot-value-using-class))
1514 (eq gf #'slot-boundp-using-class)))
1516 (defmethod compute-discriminating-function ((gf standard-generic-function))
1517 (with-slots (dfun-state arg-info) gf
1518 (when (special-case-for-compute-discriminating-function-p gf)
1519 ;; if we have a special case for
1520 ;; COMPUTE-DISCRIMINATING-FUNCTION, then (at least for the
1521 ;; special cases implemented as of 2006-05-09) any information
1522 ;; in the cache is misplaced.
1523 (aver (null dfun-state)))
1524 (typecase dfun-state
1525 (null
1526 (when (eq gf #'compute-applicable-methods)
1527 (update-all-c-a-m-gf-info gf))
1528 (cond
1529 ((eq gf #'slot-value-using-class)
1530 (update-slot-value-gf-info gf 'reader)
1531 #'slot-value-using-class-dfun)
1532 ((eq gf #'(setf slot-value-using-class))
1533 (update-slot-value-gf-info gf 'writer)
1534 #'setf-slot-value-using-class-dfun)
1535 ((eq gf #'slot-boundp-using-class)
1536 (update-slot-value-gf-info gf 'boundp)
1537 #'slot-boundp-using-class-dfun)
1538 ((gf-precompute-dfun-and-emf-p arg-info)
1539 (make-final-dfun gf))
1541 (make-initial-dfun gf))))
1542 (function dfun-state)
1543 (cons (car dfun-state)))))
1545 (defmethod update-gf-dfun ((class std-class) gf)
1546 (let ((*new-class* class)
1547 (arg-info (gf-arg-info gf)))
1548 (cond
1549 ((special-case-for-compute-discriminating-function-p gf))
1550 ((gf-precompute-dfun-and-emf-p arg-info)
1551 (multiple-value-bind (dfun cache info)
1552 (make-final-dfun-internal gf)
1553 (update-dfun gf dfun cache info))))))
1555 (defmethod (setf class-name) (new-value class)
1556 (let ((classoid (wrapper-classoid (class-wrapper class))))
1557 (if (and new-value (symbolp new-value))
1558 (setf (classoid-name classoid) new-value)
1559 (setf (classoid-name classoid) nil)))
1560 (reinitialize-instance class :name new-value)
1561 new-value)
1563 (defmethod (setf generic-function-name) (new-value generic-function)
1564 (reinitialize-instance generic-function :name new-value)
1565 new-value)
1567 (defmethod function-keywords ((method standard-method))
1568 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1569 (analyze-lambda-list (if (consp method)
1570 (early-method-lambda-list method)
1571 (method-lambda-list method)))
1572 (declare (ignore nreq nopt keysp restp))
1573 (values keywords allow-other-keys-p)))
1575 (defun method-ll->generic-function-ll (ll)
1576 (multiple-value-bind
1577 (nreq nopt keysp restp allow-other-keys-p keywords keyword-parameters)
1578 (analyze-lambda-list ll)
1579 (declare (ignore nreq nopt keysp restp allow-other-keys-p keywords))
1580 (remove-if (lambda (s)
1581 (or (memq s keyword-parameters)
1582 (eq s '&allow-other-keys)))
1583 ll)))
1585 ;;; This is based on the rules of method lambda list congruency defined in
1586 ;;; the spec. The lambda list it constructs is the pretty union of the
1587 ;;; lambda lists of all the methods. It doesn't take method applicability
1588 ;;; into account at all yet.
1589 (defmethod generic-function-pretty-arglist
1590 ((generic-function standard-generic-function))
1591 (let ((methods (generic-function-methods generic-function)))
1592 (if methods
1593 (let ((arglist ()))
1594 ;; arglist is constructed from the GF's methods - maybe with
1595 ;; keys and rest stuff added
1596 (multiple-value-bind (required optional rest key allow-other-keys)
1597 (method-pretty-arglist (car methods))
1598 (dolist (m (cdr methods))
1599 (multiple-value-bind (method-key-keywords
1600 method-allow-other-keys
1601 method-key)
1602 (function-keywords m)
1603 ;; we've modified function-keywords to return what we want as
1604 ;; the third value, no other change here.
1605 (declare (ignore method-key-keywords))
1606 (setq key (union key method-key))
1607 (setq allow-other-keys (or allow-other-keys
1608 method-allow-other-keys))))
1609 (when allow-other-keys
1610 (setq arglist '(&allow-other-keys)))
1611 (when key
1612 (setq arglist (nconc (list '&key) key arglist)))
1613 (when rest
1614 (setq arglist (nconc (list '&rest rest) arglist)))
1615 (when optional
1616 (setq arglist (nconc (list '&optional) optional arglist)))
1617 (nconc required arglist)))
1618 ;; otherwise we take the lambda-list from the GF directly, with no
1619 ;; other 'keys' added ...
1620 (let ((lambda-list (generic-function-lambda-list generic-function)))
1621 lambda-list))))
1623 (defmethod method-pretty-arglist ((method standard-method))
1624 (let ((required ())
1625 (optional ())
1626 (rest nil)
1627 (key ())
1628 (allow-other-keys nil)
1629 (state 'required)
1630 (arglist (method-lambda-list method)))
1631 (dolist (arg arglist)
1632 (cond ((eq arg '&optional) (setq state 'optional))
1633 ((eq arg '&rest) (setq state 'rest))
1634 ((eq arg '&key) (setq state 'key))
1635 ((eq arg '&allow-other-keys) (setq allow-other-keys t))
1636 ((memq arg lambda-list-keywords))
1638 (ecase state
1639 (required (push arg required))
1640 (optional (push arg optional))
1641 (key (push arg key))
1642 (rest (setq rest arg))))))
1643 (values (nreverse required)
1644 (nreverse optional)
1645 rest
1646 (nreverse key)
1647 allow-other-keys)))