1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
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
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.
38 ;;; Methods are not reinitializable.
40 (define-condition metaobject-initialization-violation
41 (reference-condition simple-error
)
44 (defun change-class-to-metaobject-violation (to-name
45 &optional from-name references
)
46 (error 'metaobject-initialization-violation
47 :format-control
"~@<Cannot ~S~@[ ~S~] objects into ~S metaobjects.~@:>"
48 :format-arguments
(list 'change-class from-name to-name
)
49 :references references
))
51 (macrolet ((def (name args control
)
52 `(defmethod ,name
,args
53 (declare (ignore initargs
))
54 (error 'metaobject-initialization-violation
55 ;; FIXME: I'm pretty sure this wants to be "~~@<~A~~@:>"
56 :format-control
,(format nil
"~@<~A~@:>" control
)
57 :format-arguments
(list ',name
)
58 :references
(list '(:amop
:initialization method
))))))
59 (def reinitialize-instance
((method method
) &rest initargs
)
60 "Method objects cannot be redefined by ~S.")
61 (def change-class
((method method
) new
&rest initargs
)
62 "Method objects cannot be redefined by ~S.")
63 ;; NEW being a subclass of method is dealt with in the general
64 ;; method of CHANGE-CLASS
65 (def update-instance-for-redefined-class
((method method
) added discarded
67 "No behaviour specified for ~S on method objects.")
68 (def update-instance-for-different-class
(old (new method
) &rest initargs
)
69 "No behaviour specified for ~S on method objects.")
70 (def update-instance-for-different-class
((old method
) new
&rest initargs
)
71 "No behaviour specified for ~S on method objects."))
73 (define-condition invalid-method-initarg
(simple-program-error)
74 ((method :initarg
:method
:reader invalid-method-initarg-method
))
77 (format s
"~@<In initialization of ~S:~2I~_~?~@:>"
78 (invalid-method-initarg-method c
)
79 (simple-condition-format-control c
)
80 (simple-condition-format-arguments c
)))))
82 (defun invalid-method-initarg (method format-control
&rest args
)
83 (error 'invalid-method-initarg
:method method
84 :format-control format-control
:format-arguments args
))
86 (defun check-documentation (method doc
)
87 (unless (or (null doc
) (stringp doc
))
88 (invalid-method-initarg method
"~@<~S of ~S is neither ~S nor a ~S.~@:>"
89 :documentation doc
'null
'string
)))
90 (defun check-lambda-list (method ll
)
91 (declare (ignore method ll
))
94 (defun check-method-function (method fun
)
95 (unless (functionp fun
)
96 (invalid-method-initarg method
"~@<~S of ~S is not a ~S.~@:>"
97 :function fun
'function
)))
99 (defun check-qualifiers (method qualifiers
)
100 (flet ((improper-list ()
101 (invalid-method-initarg method
102 "~@<~S of ~S is an improper list.~@:>"
103 :qualifiers qualifiers
)))
104 (dolist-carefully (q qualifiers improper-list
)
105 (unless (and q
(atom q
))
106 (invalid-method-initarg method
107 "~@<~S, in ~S ~S, is not a non-~S atom.~@:>"
108 q
:qualifiers qualifiers
'null
)))))
110 (defun check-slot-name (method name
)
111 (declare (ignore method
))
112 (unless (symbolp name
)
113 (invalid-method-initarg "~@<~S of ~S is not a ~S.~@:>"
114 :slot-name name
'symbol
)))
116 (defun check-specializers (method specializers
)
117 (flet ((improper-list ()
118 (invalid-method-initarg method
119 "~@<~S of ~S is an improper list.~@:>"
120 :specializers specializers
)))
121 (dolist-carefully (s specializers improper-list
)
122 (unless (specializerp s
)
123 (invalid-method-initarg method
124 "~@<~S, in ~S ~S, is not a ~S.~@:>"
125 s
:specializers specializers
'specializer
)))
126 ;; KLUDGE: ANSI says that it's not valid to have methods
127 ;; specializing on classes which are "not defined", leaving
128 ;; unclear what the definedness of a class is; AMOP suggests that
129 ;; forward-referenced-classes, since they have proper names and
130 ;; all, are at least worthy of some level of definition. We allow
131 ;; methods specialized on forward-referenced-classes, but it's
132 ;; non-portable and potentially dubious, so
133 (let ((frcs (remove-if-not #'forward-referenced-class-p specializers
)))
135 (style-warn "~@<Defining a method using ~
136 ~V[~;~1{~S~}~;~1{~S and ~S~}~:;~{~#[~;and ~]~S~^, ~}~] ~
137 as ~2:*~V[~;a specializer~:;specializers~].~@:>"
138 (length frcs
) frcs
)))))
140 (defmethod shared-initialize :before
141 ((method standard-method
) slot-names
&key
142 qualifiers lambda-list specializers function documentation
)
143 (declare (ignore slot-names
))
144 ;; FIXME: it's not clear to me (CSR, 2006-08-09) why methods get
145 ;; this extra paranoia and nothing else does; either everything
146 ;; should be aggressively checking initargs, or nothing much should.
147 ;; In either case, it would probably be better to have :type
148 ;; declarations in slots, which would then give a suitable type
149 ;; error (if we implement type-checking for slots...) rather than
150 ;; this hand-crafted thing.
151 (check-qualifiers method qualifiers
)
152 (check-lambda-list method lambda-list
)
153 (check-specializers method specializers
)
154 (check-method-function method function
)
155 (check-documentation method documentation
))
157 (defmethod shared-initialize :before
158 ((method standard-accessor-method
) slot-names
&key
159 slot-name slot-definition
)
160 (declare (ignore slot-names
))
161 (unless slot-definition
162 (check-slot-name method slot-name
)))
164 (defmethod shared-initialize :after
((method standard-method
) slot-names
165 &rest initargs
&key
((method-cell method-cell
)))
166 (declare (ignore slot-names method-cell
))
167 (initialize-method-function initargs method
))
169 (defvar *the-class-generic-function
*
170 (find-class 'generic-function
))
171 (defvar *the-class-standard-generic-function
*
172 (find-class 'standard-generic-function
))
174 (defmethod shared-initialize :before
175 ((generic-function standard-generic-function
)
177 &key
(name nil namep
)
178 (lambda-list () lambda-list-p
)
179 argument-precedence-order
182 (method-class nil method-class-supplied-p
)
183 (method-combination nil method-combination-supplied-p
))
184 (declare (ignore slot-names
185 declarations argument-precedence-order documentation
186 lambda-list lambda-list-p
))
189 (set-fun-name generic-function name
))
191 (flet ((initarg-error (initarg value string
)
192 (error "when initializing the generic function ~S:~%~
193 The ~S initialization argument was: ~A.~%~
195 generic-function initarg value string
)))
196 (cond (method-class-supplied-p
197 (when (symbolp method-class
)
198 (setq method-class
(find-class method-class
)))
199 (unless (and (classp method-class
)
200 (*subtypep
(class-eq-specializer method-class
)
202 (initarg-error :method-class
204 "a subclass of the class METHOD"))
205 (setf (slot-value generic-function
'method-class
) method-class
))
206 ((slot-boundp generic-function
'method-class
))
208 (initarg-error :method-class
210 "a subclass of the class METHOD")))
211 (cond (method-combination-supplied-p
212 (unless (method-combination-p method-combination
)
213 (initarg-error :method-combination
215 "a method combination object")))
216 ((slot-boundp generic-function
'%method-combination
))
218 (initarg-error :method-combination
220 "a method combination object")))))
222 (defun find-generic-function (name &optional
(errorp t
))
223 (let ((fun (and (fboundp name
) (fdefinition name
))))
225 ((and fun
(typep fun
'generic-function
)) fun
)
226 (errorp (error "No generic function named ~S." name
))
229 (defun real-add-named-method (generic-function-name qualifiers
230 specializers lambda-list
&rest other-initargs
)
231 (unless (and (fboundp generic-function-name
)
232 (typep (fdefinition generic-function-name
) 'generic-function
))
233 (warn 'implicit-generic-function-warning
:name generic-function-name
))
234 (let* ((existing-gf (find-generic-function generic-function-name nil
))
237 (ensure-generic-function
238 generic-function-name
239 :generic-function-class
(class-of existing-gf
))
240 (ensure-generic-function generic-function-name
)))
241 (proto (method-prototype-for-gf generic-function-name
)))
242 ;; FIXME: Destructive modification of &REST list.
243 (setf (getf (getf other-initargs
'plist
) :name
)
244 (make-method-spec generic-function qualifiers specializers
))
245 (let ((new (apply #'make-instance
(class-of proto
)
246 :qualifiers qualifiers
:specializers specializers
247 :lambda-list lambda-list other-initargs
)))
248 (add-method generic-function new
)
251 (define-condition find-method-length-mismatch
252 (reference-condition simple-error
)
254 (:default-initargs
:references
(list '(:ansi-cl
:function find-method
))))
256 (defun real-get-method (generic-function qualifiers specializers
258 always-check-specializers
)
259 (let ((lspec (length specializers
))
260 (methods (generic-function-methods generic-function
)))
261 (when (or methods always-check-specializers
)
262 (let ((nreq (length (arg-info-metatypes (gf-arg-info
263 generic-function
)))))
264 ;; Since we internally bypass FIND-METHOD by using GET-METHOD
265 ;; instead we need to to this here or users may get hit by a
266 ;; failed AVER instead of a sensible error message.
267 (when (/= lspec nreq
)
269 'find-method-length-mismatch
271 "~@<The generic function ~S takes ~D required argument~:P; ~
272 was asked to find a method with specializers ~S~@:>"
273 :format-arguments
(list generic-function nreq specializers
)))))
275 (dolist (method methods
)
276 (let ((mspecializers (method-specializers method
)))
277 (aver (= lspec
(length mspecializers
)))
278 (when (and (equal qualifiers
(safe-method-qualifiers method
))
279 (every #'same-specializer-p specializers
280 (method-specializers method
)))
285 (error "~@<There is no method on ~S with ~
286 ~:[no qualifiers~;~:*qualifiers ~S~] ~
287 and specializers ~S.~@:>"
288 generic-function qualifiers specializers
))))))
290 (defmethod find-method ((generic-function standard-generic-function
)
291 qualifiers specializers
&optional
(errorp t
))
292 ;; ANSI about FIND-METHOD: "The specializers argument contains the
293 ;; parameter specializers for the method. It must correspond in
294 ;; length to the number of required arguments of the generic
295 ;; function, or an error is signaled."
297 ;; This error checking is done by REAL-GET-METHOD.
299 generic-function qualifiers
300 ;; ANSI for FIND-METHOD seems to imply that in fact specializers
301 ;; should always be passed in parsed form instead of being parsed
302 ;; at this point. Since there's no ANSI-blessed way of getting an
303 ;; EQL specializer, that seems unnecessarily painful, so we are
304 ;; nice to our users. -- CSR, 2007-06-01
305 (parse-specializers generic-function specializers
) errorp t
))
307 ;;; Compute various information about a generic-function's arglist by looking
308 ;;; at the argument lists of the methods. The hair for trying not to use
309 ;;; &REST arguments lives here.
310 ;;; The values returned are:
311 ;;; number-of-required-arguments
312 ;;; the number of required arguments to this generic-function's
313 ;;; discriminating function
315 ;;; whether or not this generic-function's discriminating
316 ;;; function takes an &rest argument.
317 ;;; specialized-argument-positions
318 ;;; a list of the positions of the arguments this generic-function
319 ;;; specializes (e.g. for a classical generic-function this is the
321 (defmethod compute-discriminating-function-arglist-info
322 ((generic-function standard-generic-function
))
323 ;;(declare (values number-of-required-arguments &rest-argument-p
324 ;; specialized-argument-postions))
325 (let ((number-required nil
)
327 (specialized-positions ())
328 (methods (generic-function-methods generic-function
)))
329 (dolist (method methods
)
330 (multiple-value-setq (number-required restp specialized-positions
)
331 (compute-discriminating-function-arglist-info-internal
332 generic-function method number-required restp specialized-positions
)))
333 (values number-required restp
(sort specialized-positions
#'<))))
335 (defun compute-discriminating-function-arglist-info-internal
336 (generic-function method number-of-requireds restp
337 specialized-argument-positions
)
338 (declare (ignore generic-function
)
339 (type (or null fixnum
) number-of-requireds
))
341 (declare (fixnum requireds
))
342 ;; Go through this methods arguments seeing how many are required,
343 ;; and whether there is an &rest argument.
344 (dolist (arg (method-lambda-list method
))
345 (cond ((eq arg
'&aux
) (return))
346 ((memq arg
'(&optional
&rest
&key
))
347 (return (setq restp t
)))
348 ((memq arg lambda-list-keywords
))
349 (t (incf requireds
))))
350 ;; Now go through this method's type specifiers to see which
351 ;; argument positions are type specified. Treat T specially
352 ;; in the usual sort of way. For efficiency don't bother to
353 ;; keep specialized-argument-positions sorted, rather depend
354 ;; on our caller to do that.
356 (dolist (type-spec (method-specializers method
))
357 (unless (eq type-spec
*the-class-t
*)
358 (pushnew pos specialized-argument-positions
:test
#'eq
))
360 ;; Finally merge the values for this method into the values
361 ;; for the exisiting methods and return them. Note that if
362 ;; num-of-requireds is NIL it means this is the first method
363 ;; and we depend on that.
364 (values (min (or number-of-requireds requireds
) requireds
)
366 (and number-of-requireds
(/= number-of-requireds requireds
)))
367 specialized-argument-positions
)))
369 (defun make-discriminating-function-arglist (number-required-arguments restp
)
370 (nconc (let ((args nil
))
371 (dotimes (i number-required-arguments
)
372 (push (format-symbol *package
* ;; ! is this right?
373 "Discriminating Function Arg ~D"
378 `(&rest
,(format-symbol *package
*
379 "Discriminating Function &rest Arg")))))
381 (defmethod generic-function-argument-precedence-order
382 ((gf standard-generic-function
))
383 (aver (eq **boot-state
** 'complete
))
384 (loop with arg-info
= (gf-arg-info gf
)
385 with lambda-list
= (arg-info-lambda-list arg-info
)
386 for argument-position in
(arg-info-precedence arg-info
)
387 collect
(nth argument-position lambda-list
)))
389 (defmethod generic-function-lambda-list ((gf generic-function
))
392 (defmethod gf-fast-method-function-p ((gf standard-generic-function
))
393 (gf-info-fast-mf-p (slot-value gf
'arg-info
)))
395 (defmethod initialize-instance :after
((gf standard-generic-function
)
396 &key
(lambda-list nil lambda-list-p
)
397 argument-precedence-order
)
398 (with-slots (arg-info) gf
401 :lambda-list lambda-list
402 :argument-precedence-order argument-precedence-order
)
404 (when (arg-info-valid-p arg-info
)
407 (defmethod reinitialize-instance :around
408 ((gf standard-generic-function
) &rest args
&key
409 (lambda-list nil lambda-list-p
) (argument-precedence-order nil apo-p
))
410 (let ((old-mc (generic-function-method-combination gf
)))
411 (prog1 (call-next-method)
412 ;; KLUDGE: EQ is too strong a test.
413 (unless (eq old-mc
(generic-function-method-combination gf
))
414 (flush-effective-method-cache gf
))
416 ((and lambda-list-p apo-p
)
418 :lambda-list lambda-list
419 :argument-precedence-order argument-precedence-order
))
420 (lambda-list-p (set-arg-info gf
:lambda-list lambda-list
))
421 (t (set-arg-info gf
)))
422 (when (arg-info-valid-p (gf-arg-info gf
))
424 (map-dependents gf
(lambda (dependent)
425 (apply #'update-dependent gf dependent args
))))))
427 (declaim (special *lazy-dfun-compute-p
*))
429 (defun set-methods (gf methods
)
430 (setf (generic-function-methods gf
) nil
)
431 (loop (when (null methods
) (return gf
))
432 (real-add-method gf
(pop methods
) methods
)))
434 (define-condition new-value-specialization
(reference-condition error
)
435 ((%method
:initarg
:method
:reader new-value-specialization-method
))
438 (format s
"~@<Cannot add method ~S to ~S, as it specializes the ~
439 new-value argument.~@:>"
440 (new-value-specialization-method c
)
441 #'(setf slot-value-using-class
))))
442 (:default-initargs
:references
443 (list '(:sbcl
:node
"Metaobject Protocol")
444 '(:amop
:generic-function
(setf slot-value-using-class
)))))
446 (defgeneric values-for-add-method
(gf method
)
447 (:method
((gf standard-generic-function
) (method standard-method
))
448 ;; KLUDGE: Just a single generic dispatch, and everything else
449 ;; comes from permutation vectors. Would be nicer to define
450 ;; REAL-ADD-METHOD with a proper method so that we could efficiently
451 ;; use SLOT-VALUE there.
453 ;; Optimization note: REAL-ADD-METHOD has a lot of O(N) stuff in it (as
454 ;; does PCL as a whole). It should not be too hard to internally store
455 ;; many of the things we now keep in lists as either purely functional
456 ;; O(log N) sets, or --if we don't mind the memory cost-- using
457 ;; specialized hash-tables: most things are used to answer questions about
458 ;; set-membership, not ordering.
459 (values (slot-value gf
'%lock
)
460 (slot-value method
'qualifiers
)
461 (slot-value method
'specializers
)
462 (slot-value method
'lambda-list
)
463 (slot-value method
'%generic-function
)
464 (slot-value gf
'name
))))
466 (define-condition print-object-stream-specializer
(reference-condition simple-warning
)
469 :references
(list '(:ansi-cl
:function print-object
))
470 :format-control
"~@<Specializing on the second argument to ~S has ~
471 unportable effects, and also interferes with ~
472 precomputation of print functions for exceptional ~
474 :format-arguments
(list 'print-object
)))
476 (defun real-add-method (generic-function method
&optional skip-dfun-update-p
)
477 (flet ((similar-lambda-lists-p (old-method new-lambda-list
)
478 (binding* (((a-llks a-nreq a-nopt
)
479 (analyze-lambda-list (method-lambda-list old-method
)))
480 ((b-llks b-nreq b-nopt
)
481 (analyze-lambda-list new-lambda-list
)))
482 (and (= a-nreq b-nreq
)
484 (eq (ll-keyp-or-restp a-llks
)
485 (ll-keyp-or-restp b-llks
))))))
486 (multiple-value-bind (lock qualifiers specializers new-lambda-list
488 (values-for-add-method generic-function method
)
490 (error "~@<The method ~S is already part of the generic ~
491 function ~S; it can't be added to another generic ~
492 function until it is removed from the first one.~@:>"
494 (when (and (eq name
'print-object
) (not (eq (second specializers
) *the-class-t
*)))
495 (warn 'print-object-stream-specializer
))
497 ;; System lock because interrupts need to be disabled as
498 ;; well: it would be bad to unwind and leave the gf in an
499 ;; inconsistent state.
500 (sb-thread::with-recursive-system-lock
(lock)
501 (let ((existing (get-method generic-function
506 ;; If there is already a method like this one then we must get
507 ;; rid of it before proceeding. Note that we call the generic
508 ;; function REMOVE-METHOD to remove it rather than doing it in
509 ;; some internal way.
510 (when (and existing
(similar-lambda-lists-p existing new-lambda-list
))
511 (remove-method generic-function existing
))
513 ;; KLUDGE: We have a special case here, as we disallow
514 ;; specializations of the NEW-VALUE argument to (SETF
515 ;; SLOT-VALUE-USING-CLASS). GET-ACCESSOR-METHOD-FUNCTION is
516 ;; the optimizing function here: it precomputes the effective
517 ;; method, assuming that there is no dispatch to be done on
518 ;; the new-value argument.
519 (when (and (eq generic-function
#'(setf slot-value-using-class
))
520 (not (eq *the-class-t
* (first specializers
))))
521 (error 'new-value-specialization
:method method
))
523 (setf (method-generic-function method
) generic-function
)
524 (pushnew method
(generic-function-methods generic-function
) :test
#'eq
)
525 (dolist (specializer specializers
)
526 (add-direct-method specializer method
))
528 ;; KLUDGE: SET-ARG-INFO contains the error-detecting logic for
529 ;; detecting attempts to add methods with incongruent lambda
530 ;; lists. However, according to Gerd Moellmann on cmucl-imp,
531 ;; it also depends on the new method already having been added
532 ;; to the generic function. Therefore, we need to remove it
534 (let ((remove-again-p t
))
537 (set-arg-info generic-function
:new-method method
)
538 (setq remove-again-p nil
))
540 (remove-method generic-function method
))))
542 ;; KLUDGE II: ANSI saith that it is not an error to add a
543 ;; method with invalid qualifiers to a generic function of the
544 ;; wrong kind; it's only an error at generic function
545 ;; invocation time; I dunno what the rationale was, and it
546 ;; sucks. Nevertheless, it's probably a programmer error, so
547 ;; let's warn anyway. -- CSR, 2003-08-20
548 (let ((mc (generic-function-method-combination generic-functioN
)))
550 ((eq mc
*standard-method-combination
*)
551 (when (and qualifiers
553 (not (memq (car qualifiers
)
554 '(:around
:before
:after
)))))
555 (warn "~@<Invalid qualifiers for standard method ~
556 combination in method ~S:~2I~_~S.~@:>"
558 ((short-method-combination-p mc
)
559 (let ((mc-name (method-combination-type-name mc
)))
560 (when (or (null qualifiers
)
562 (and (neq (car qualifiers
) :around
)
563 (neq (car qualifiers
) mc-name
)))
564 (warn "~@<Invalid qualifiers for ~S method combination ~
565 in method ~S:~2I~_~S.~@:>"
566 mc-name method qualifiers
))))))
567 (unless skip-dfun-update-p
568 (update-ctors 'add-method
569 :generic-function generic-function
571 (update-dfun generic-function
))
572 (setf (gf-info-needs-update generic-function
) t
)
573 (map-dependents generic-function
575 (update-dependent generic-function
576 dep
'add-method method
)))))
577 (serious-condition (c)
581 (defun real-remove-method (generic-function method
)
582 (when (eq generic-function
(method-generic-function method
))
583 (let ((lock (gf-lock generic-function
)))
584 ;; System lock because interrupts need to be disabled as well:
585 ;; it would be bad to unwind and leave the gf in an inconsistent
587 (sb-thread::with-recursive-system-lock
(lock)
588 (let* ((specializers (method-specializers method
)) ; flushable?
589 (methods (generic-function-methods generic-function
))
590 (new-methods (remove method methods
)))
591 (declare (ignore specializers
))
592 (setf (method-generic-function method
) nil
593 (generic-function-methods generic-function
) new-methods
)
594 (dolist (specializer (method-specializers method
))
595 (remove-direct-method specializer method
))
596 (set-arg-info generic-function
)
597 (update-ctors 'remove-method
598 :generic-function generic-function
600 (update-dfun generic-function
)
601 (setf (gf-info-needs-update generic-function
) t
)
602 (map-dependents generic-function
604 (update-dependent generic-function
605 dep
'remove-method method
)))))))
609 ;; Tell INFO about the generic function's methods' keys so that the
610 ;; compiler doesn't complain that the keys defined for some method are
612 (sb-ext:without-package-locks
613 (defun sb-c::maybe-update-info-for-gf
(name)
614 (let ((gf (if (fboundp name
) (fdefinition name
))))
615 (when (and gf
(generic-function-p gf
) (not (early-gf-p gf
))
616 (not (eq :declared
(info :function
:where-from name
)))
617 (gf-info-needs-update gf
))
618 (let* ((methods (generic-function-methods gf
))
619 (gf-lambda-list (generic-function-lambda-list gf
))
620 (tfun (constantly t
))
622 (multiple-value-bind (llks gf.required gf.optional gf.rest gf.keys
)
623 (parse-lambda-list gf-lambda-list
)
624 ;; 7.6.4 point 5 probably entails that if any method says
625 ;; &allow-other-keys then the gf should be construed to
627 (let* ((allowp (or (ll-kwds-allowp llks
)
628 (find '&allow-other-keys methods
630 :key
#'method-lambda-list
)))
633 ;; SERIOUSLY? Do we not already have like at least
634 ;; two other variations on this code?
636 (,@(mapcar tfun gf.required
)
638 `(&optional
,@(mapcar tfun gf.optional
)))
641 ,@(when (ll-kwds-keyp llks
)
648 (mapcan #'function-keywords methods
)
649 (mapcar #'parse-key-arg-spec gf.keys
))))))
652 `(&key
,@all-keys
))))
653 ,@(when (and (not keysp
) allowp
)
656 `(&allow-other-keys
)))
658 (setf (info :function
:type name
) ftype
659 (info :function
:where-from name
) :defined-method
660 (gf-info-needs-update gf
) nil
)
663 (defun compute-applicable-methods-function (generic-function arguments
)
664 (values (compute-applicable-methods-using-types
666 (types-from-args generic-function arguments
'eql
))))
668 (defmethod compute-applicable-methods
669 ((generic-function generic-function
) arguments
)
670 (values (compute-applicable-methods-using-types
672 (types-from-args generic-function arguments
'eql
))))
674 (defmethod compute-applicable-methods-using-classes
675 ((generic-function generic-function
) classes
)
676 (compute-applicable-methods-using-types
678 (types-from-args generic-function classes
'class-eq
)))
680 (defun !proclaim-incompatible-superclasses
(classes)
681 (setq classes
(mapcar (lambda (class)
686 (dolist (class classes
)
687 (dolist (other-class classes
)
688 (unless (eq class other-class
)
689 (pushnew other-class
(class-incompatible-superclass-list class
) :test
#'eq
)))))
691 (defun superclasses-compatible-p (class1 class2
)
692 (let ((cpl1 (cpl-or-nil class1
))
693 (cpl2 (cpl-or-nil class2
)))
695 (dolist (ic (class-incompatible-superclass-list sc1
))
697 (return-from superclasses-compatible-p nil
))))))
700 #'!proclaim-incompatible-superclasses
701 '(;; superclass class
702 (system-class std-class structure-class
) ; direct subclasses of pcl-class
703 (standard-class funcallable-standard-class
)
704 ;; superclass metaobject
705 (class eql-specializer class-eq-specializer method method-combination
706 generic-function slot-definition
)
707 ;; metaclass built-in-class
708 (number sequence character
; direct subclasses of t, but not array
709 standard-object structure-object
) ; or symbol
710 (number array character symbol
; direct subclasses of t, but not
711 standard-object structure-object
) ; sequence
712 (complex float rational
) ; direct subclasses of number
713 (integer ratio
) ; direct subclasses of rational
714 (list vector
) ; direct subclasses of sequence
715 (cons null
) ; direct subclasses of list
716 (string bit-vector
) ; direct subclasses of vector
719 (defmethod same-specializer-p ((specl1 specializer
) (specl2 specializer
))
722 (defmethod same-specializer-p ((specl1 class
) (specl2 class
))
725 (defmethod specializer-class ((specializer class
))
728 (defmethod same-specializer-p ((specl1 class-eq-specializer
)
729 (specl2 class-eq-specializer
))
730 (eq (specializer-class specl1
) (specializer-class specl2
)))
732 (defmethod same-specializer-p ((specl1 eql-specializer
)
733 (specl2 eql-specializer
))
734 (eq (specializer-object specl1
) (specializer-object specl2
)))
736 (defmethod specializer-class ((specializer eql-specializer
))
737 (class-of (slot-value specializer
'object
)))
739 (defun specializer-class-or-nil (specializer)
740 (and (standard-specializer-p specializer
)
741 (specializer-class specializer
)))
743 (defun error-need-at-least-n-args (function n
)
744 (error 'simple-program-error
745 :format-control
"~@<The function ~2I~_~S ~I~_requires ~
746 at least ~W argument~:P.~:>"
747 :format-arguments
(list function n
)))
749 (defun types-from-args (generic-function arguments
&optional type-modifier
)
750 (multiple-value-bind (nreq applyp metatypes nkeys arg-info
)
751 (get-generic-fun-info generic-function
)
752 (declare (ignore applyp metatypes nkeys
))
753 (let ((types-rev nil
))
754 (dotimes-fixnum (i nreq
)
756 (error-need-at-least-n-args (generic-function-name generic-function
)
758 (let ((arg (pop arguments
)))
759 (push (if type-modifier
`(,type-modifier
,arg
) arg
) types-rev
)))
760 (values (nreverse types-rev
) arg-info
))))
762 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes
)
763 (let* ((w wrappers
) (w-tail w
) (mt-tail metatypes
))
764 (dolist (class (ensure-list classes
))
765 (unless (eq t
(car mt-tail
))
766 (let ((c-w (class-wrapper class
)))
767 (unless c-w
(return-from get-wrappers-from-classes nil
))
770 (setf (car w-tail
) c-w
771 w-tail
(cdr w-tail
)))))
772 (setq mt-tail
(cdr mt-tail
)))
775 (defun sdfun-for-caching (gf classes
)
776 (let ((types (mapcar #'class-eq-type classes
)))
777 (multiple-value-bind (methods all-applicable-and-sorted-p
)
778 (compute-applicable-methods-using-types gf types
)
779 (let ((generator (get-secondary-dispatch-function1
780 gf methods types nil t all-applicable-and-sorted-p
)))
781 (make-callable gf methods generator
782 nil
(mapcar #'class-wrapper classes
))))))
784 (defun value-for-caching (gf classes
)
785 (let ((methods (compute-applicable-methods-using-types
786 gf
(mapcar #'class-eq-type classes
))))
787 (method-plist-value (car methods
) :constant-value
)))
789 (defun default-secondary-dispatch-function (generic-function)
791 (let ((methods (compute-applicable-methods generic-function args
)))
793 (let ((emf (get-effective-method-function generic-function
795 (invoke-emf emf args
))
796 (call-no-applicable-method generic-function args
)))))
799 (loop (when (atom x
) (return (eq x y
)))
800 (when (atom y
) (return nil
))
801 (unless (eq (car x
) (car y
)) (return nil
))
805 (defvar *std-cam-methods
* nil
)
807 (defun compute-applicable-methods-emf (generic-function)
808 (if (eq **boot-state
** 'complete
)
809 (let* ((cam (gdefinition 'compute-applicable-methods
))
810 (cam-methods (compute-applicable-methods-using-types
811 cam
(list `(eql ,generic-function
) t
))))
812 (values (get-effective-method-function cam cam-methods
)
814 (or *std-cam-methods
*
815 (setq *std-cam-methods
*
816 (compute-applicable-methods-using-types
817 cam
(list `(eql ,cam
) t
)))))))
818 (values #'compute-applicable-methods-function t
)))
820 (defun compute-applicable-methods-emf-std-p (gf)
821 (gf-info-c-a-m-emf-std-p (gf-arg-info gf
)))
823 (defvar *old-c-a-m-gf-methods
* nil
)
825 (defun update-all-c-a-m-gf-info (c-a-m-gf)
826 (let ((methods (generic-function-methods c-a-m-gf
)))
827 (if (and *old-c-a-m-gf-methods
*
828 (every (lambda (old-method)
829 (member old-method methods
:test
#'eq
))
830 *old-c-a-m-gf-methods
*))
831 (let ((gfs-to-do nil
)
832 (gf-classes-to-do nil
))
833 (dolist (method methods
)
834 (unless (member method
*old-c-a-m-gf-methods
* :test
#'eq
)
835 (let ((specl (car (method-specializers method
))))
836 (if (eql-specializer-p specl
)
837 (pushnew (specializer-object specl
) gfs-to-do
:test
#'eq
)
838 (pushnew (specializer-class specl
) gf-classes-to-do
:test
#'eq
)))))
839 (map-all-generic-functions
841 (when (or (member gf gfs-to-do
:test
#'eq
)
842 (dolist (class gf-classes-to-do nil
)
844 (class-precedence-list (class-of gf
))
846 (update-c-a-m-gf-info gf
)))))
847 (map-all-generic-functions #'update-c-a-m-gf-info
))
848 (setq *old-c-a-m-gf-methods
* methods
)))
850 (defun update-gf-info (gf)
851 (update-c-a-m-gf-info gf
)
852 (update-gf-simple-accessor-type gf
))
854 (defun update-c-a-m-gf-info (gf)
855 (unless (early-gf-p gf
)
856 (multiple-value-bind (c-a-m-emf std-p
)
857 (compute-applicable-methods-emf gf
)
858 (let ((arg-info (gf-arg-info gf
)))
859 (setf (gf-info-static-c-a-m-emf arg-info
) c-a-m-emf
)
860 (setf (gf-info-c-a-m-emf-std-p arg-info
) std-p
)))))
862 (defun update-gf-simple-accessor-type (gf)
863 (let ((arg-info (gf-arg-info gf
)))
864 (setf (gf-info-simple-accessor-type arg-info
)
865 (let* ((methods (generic-function-methods gf
))
866 (class (and methods
(class-of (car methods
))))
869 (cond ((or (eq class
*the-class-standard-reader-method
*)
870 (eq class
*the-class-global-reader-method
*))
872 ((or (eq class
*the-class-standard-writer-method
*)
873 (eq class
*the-class-global-writer-method
*))
875 ((or (eq class
*the-class-standard-boundp-method
*)
876 (eq class
*the-class-global-boundp-method
*))
878 (when (and (gf-info-c-a-m-emf-std-p arg-info
)
880 (dolist (method (cdr methods
) t
)
881 (unless (eq class
(class-of method
)) (return nil
)))
882 (eq (generic-function-method-combination gf
)
883 *standard-method-combination
*))
887 ;;; CMUCL (Gerd's PCL, 2002-04-25) comment:
889 ;;; Return two values. First value is a function to be stored in
890 ;;; effective slot definition SLOTD for reading it with
891 ;;; SLOT-VALUE-USING-CLASS, setting it with (SETF
892 ;;; SLOT-VALUE-USING-CLASS) or testing it with
893 ;;; SLOT-BOUNDP-USING-CLASS. GF is one of these generic functions,
894 ;;; TYPE is one of the symbols READER, WRITER, BOUNDP. CLASS is
897 ;;; Second value is true if the function returned is one of the
898 ;;; optimized standard functions for the purpose, which are used
899 ;;; when only standard methods are applicable.
901 ;;; FIXME: Change all these wacky function names to something sane.
902 (defun get-accessor-method-function (gf type class slotd
)
903 (let* ((std-method (standard-svuc-method type
))
904 (str-method (structure-svuc-method type
))
905 (types1 `((eql ,class
) (class-eq ,class
) (eql ,slotd
)))
906 (types (if (eq type
'writer
) `(t ,@types1
) types1
))
907 (methods (compute-applicable-methods-using-types gf types
))
908 (std-p (null (cdr methods
))))
911 (get-optimized-std-accessor-method-function class slotd type
)
912 (let* ((optimized-std-fun
913 (get-optimized-std-slot-value-using-class-method-function
916 `((,(car (or (member std-method methods
:test
#'eq
)
917 (member str-method methods
:test
#'eq
)
919 'get-accessor-method-function
)))
920 ,optimized-std-fun
)))
922 (let ((wrappers (list (layout-of class
)
923 (class-wrapper class
)
925 (if (eq type
'writer
)
926 (cons (class-wrapper *the-class-t
*) wrappers
)
928 (sdfun (get-secondary-dispatch-function
929 gf methods types method-alist wrappers
)))
930 (get-accessor-from-svuc-method-function class slotd sdfun type
)))
933 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
934 (defun update-slot-value-gf-info (gf type
)
936 (update-std-or-str-methods gf type
))
937 (when (and (standard-svuc-method type
) (structure-svuc-method type
))
938 (flet ((update-accessor-info (class)
939 (when (class-finalized-p class
)
940 (dolist (slotd (class-slots class
))
941 (compute-slot-accessor-info slotd type gf
)))))
943 (update-accessor-info *new-class
*)
944 (map-all-classes #'update-accessor-info
'slot-object
)))))
946 (defvar *standard-slot-value-using-class-method
* nil
)
947 (defvar *standard-setf-slot-value-using-class-method
* nil
)
948 (defvar *standard-slot-boundp-using-class-method
* nil
)
949 (defvar *condition-slot-value-using-class-method
* nil
)
950 (defvar *condition-setf-slot-value-using-class-method
* nil
)
951 (defvar *condition-slot-boundp-using-class-method
* nil
)
952 (defvar *structure-slot-value-using-class-method
* nil
)
953 (defvar *structure-setf-slot-value-using-class-method
* nil
)
954 (defvar *structure-slot-boundp-using-class-method
* nil
)
956 (defun standard-svuc-method (type)
958 (reader *standard-slot-value-using-class-method
*)
959 (writer *standard-setf-slot-value-using-class-method
*)
960 (boundp *standard-slot-boundp-using-class-method
*)))
962 (defun set-standard-svuc-method (type method
)
964 (reader (setq *standard-slot-value-using-class-method
* method
))
965 (writer (setq *standard-setf-slot-value-using-class-method
* method
))
966 (boundp (setq *standard-slot-boundp-using-class-method
* method
))))
968 (defun condition-svuc-method (type)
970 (reader *condition-slot-value-using-class-method
*)
971 (writer *condition-setf-slot-value-using-class-method
*)
972 (boundp *condition-slot-boundp-using-class-method
*)))
974 (defun set-condition-svuc-method (type method
)
976 (reader (setq *condition-slot-value-using-class-method
* method
))
977 (writer (setq *condition-setf-slot-value-using-class-method
* method
))
978 (boundp (setq *condition-slot-boundp-using-class-method
* method
))))
980 (defun structure-svuc-method (type)
982 (reader *structure-slot-value-using-class-method
*)
983 (writer *structure-setf-slot-value-using-class-method
*)
984 (boundp *structure-slot-boundp-using-class-method
*)))
986 (defun set-structure-svuc-method (type method
)
988 (reader (setq *structure-slot-value-using-class-method
* method
))
989 (writer (setq *structure-setf-slot-value-using-class-method
* method
))
990 (boundp (setq *structure-slot-boundp-using-class-method
* method
))))
992 (defun update-std-or-str-methods (gf type
)
993 (dolist (method (generic-function-methods gf
))
994 (let ((specls (method-specializers method
)))
995 (when (and (or (not (eq type
'writer
))
996 (eq (pop specls
) *the-class-t
*))
997 (every #'classp specls
))
998 (cond ((and (eq (class-name (car specls
)) 'std-class
)
999 (eq (class-name (cadr specls
)) 'standard-object
)
1000 (eq (class-name (caddr specls
))
1001 'standard-effective-slot-definition
))
1002 (set-standard-svuc-method type method
))
1003 ((and (eq (class-name (car specls
)) 'condition-class
)
1004 (eq (class-name (cadr specls
)) 'condition
)
1005 (eq (class-name (caddr specls
))
1006 'condition-effective-slot-definition
))
1007 (set-condition-svuc-method type method
))
1008 ((and (eq (class-name (car specls
)) 'structure-class
)
1009 (eq (class-name (cadr specls
)) 'structure-object
)
1010 (eq (class-name (caddr specls
))
1011 'structure-effective-slot-definition
))
1012 (set-structure-svuc-method type method
)))))))
1014 (defun mec-all-classes-internal (spec precompute-p
)
1015 (let ((wrapper (class-wrapper (specializer-class spec
))))
1016 (unless (or (not wrapper
) (invalid-wrapper-p wrapper
))
1017 (cons (specializer-class spec
)
1020 (not (or (eq spec
*the-class-t
*)
1021 (eq spec
*the-class-slot-object
*)
1022 (eq spec
*the-class-standard-object
*)
1023 (eq spec
*the-class-structure-object
*)))
1024 (let ((sc (class-direct-subclasses spec
)))
1026 (mapcan (lambda (class)
1027 (mec-all-classes-internal class precompute-p
))
1030 (defun mec-all-classes (spec precompute-p
)
1031 (let ((classes (mec-all-classes-internal spec precompute-p
)))
1032 (if (null (cdr classes
))
1034 (let* ((a-classes (cons nil classes
))
1036 (loop (when (null (cdr tail
))
1037 (return (cdr a-classes
)))
1038 (let ((class (cadr tail
))
1039 (ttail (cddr tail
)))
1040 (if (dolist (c ttail nil
)
1041 (when (eq class c
) (return t
)))
1042 (setf (cdr tail
) (cddr tail
))
1043 (setf tail
(cdr tail
)))))))))
1045 (defun mec-all-class-lists (spec-list precompute-p
)
1046 (if (null spec-list
)
1048 (let* ((car-all-classes (mec-all-classes (car spec-list
)
1050 (all-class-lists (mec-all-class-lists (cdr spec-list
)
1052 (mapcan (lambda (list)
1053 (mapcar (lambda (c) (cons c list
)) car-all-classes
))
1056 (defun make-emf-cache (generic-function valuep cache classes-list new-class
)
1057 (let* ((arg-info (gf-arg-info generic-function
))
1058 (nkeys (arg-info-nkeys arg-info
))
1059 (metatypes (arg-info-metatypes arg-info
))
1060 (wrappers (unless (eq nkeys
1) (make-list nkeys
)))
1061 (precompute-p (gf-precompute-dfun-and-emf-p arg-info
)))
1062 (flet ((add-class-list (classes)
1063 (when (or (null new-class
) (memq new-class classes
))
1064 (let ((%wrappers
(get-wrappers-from-classes
1065 nkeys wrappers classes metatypes
)))
1066 (when (and %wrappers
(not (probe-cache cache %wrappers
)))
1067 (let ((value (cond ((eq valuep t
)
1068 (sdfun-for-caching generic-function
1070 ((eq valuep
:constant-value
)
1071 (value-for-caching generic-function
1073 ;; need to get them again, as finalization might
1074 ;; have happened in between, which would
1075 ;; invalidate wrappers.
1076 (let ((wrappers (get-wrappers-from-classes
1077 nkeys wrappers classes metatypes
)))
1078 (when (if (atom wrappers
)
1079 (not (invalid-wrapper-p wrappers
))
1080 (every (complement #'invalid-wrapper-p
)
1082 (setq cache
(fill-cache cache wrappers value
))))))))))
1084 (mapc #'add-class-list classes-list
)
1085 (dolist (method (generic-function-methods generic-function
))
1086 (mapc #'add-class-list
1087 (mec-all-class-lists (method-specializers method
)
1091 (defmacro class-test
(arg class
)
1093 ((eq class
*the-class-t
*) t
)
1094 ((eq class
*the-class-slot-object
*)
1095 `(not (typep (classoid-of ,arg
) 'system-classoid
)))
1096 ((eq class
*the-class-standard-object
*)
1097 `(or (std-instance-p ,arg
) (fsc-instance-p ,arg
)))
1098 ((eq class
*the-class-funcallable-standard-object
*)
1099 `(fsc-instance-p ,arg
))
1101 `(typep ,arg
',(class-name class
)))))
1103 (defmacro class-eq-test
(arg class
)
1104 `(eq (class-of ,arg
) ',class
))
1106 (defmacro eql-test
(arg object
)
1107 `(eql ,arg
',object
))
1109 (defun dnet-methods-p (form)
1111 (or (eq (car form
) 'methods
)
1112 (eq (car form
) 'unordered-methods
))))
1114 ;;; This is CASE, but without gensyms.
1115 (defmacro scase
(arg &rest clauses
)
1116 `(let ((.case-arg.
,arg
))
1117 (cond ,@(mapcar (lambda (clause)
1118 (list* (cond ((null (car clause
))
1120 ((consp (car clause
))
1121 (if (null (cdar clause
))
1126 ((member (car clause
) '(t otherwise
))
1129 `(eql .case-arg.
',(car clause
))))
1134 (defmacro mcase
(arg &rest clauses
) `(scase ,arg
,@clauses
))
1136 (defun generate-discrimination-net (generic-function methods types sorted-p
)
1137 (let* ((arg-info (gf-arg-info generic-function
))
1138 (c-a-m-emf-std-p (gf-info-c-a-m-emf-std-p arg-info
))
1139 (precedence (arg-info-precedence arg-info
)))
1140 (generate-discrimination-net-internal
1141 generic-function methods types
1142 (lambda (methods known-types
)
1144 (and c-a-m-emf-std-p
1146 (let ((sorted-methods nil
))
1148 (copy-list methods
) precedence
1150 (when sorted-methods
(return-from one-order-p nil
))
1151 (setq sorted-methods methods
)))
1152 (setq methods sorted-methods
))
1154 `(methods ,methods
,known-types
)
1155 `(unordered-methods ,methods
,known-types
)))
1156 (lambda (position type true-value false-value
)
1157 (let ((arg (dfun-arg-symbol position
)))
1158 (if (eq (car type
) 'eql
)
1159 (let* ((false-case-p (and (consp false-value
)
1160 (or (eq (car false-value
) 'scase
)
1161 (eq (car false-value
) 'mcase
))
1162 (eq arg
(cadr false-value
))))
1163 (false-clauses (if false-case-p
1165 `((t ,false-value
))))
1166 (case-sym (if (and (dnet-methods-p true-value
)
1168 (eq (car false-value
) 'mcase
)
1169 (dnet-methods-p false-value
)))
1172 (type-sym `(,(cadr type
))))
1174 (,type-sym
,true-value
)
1176 `(if ,(let ((arg (dfun-arg-symbol position
)))
1178 (class `(class-test ,arg
,(cadr type
)))
1179 (class-eq `(class-eq-test ,arg
,(cadr type
)))))
1184 (defun class-from-type (type)
1185 (if (or (atom type
) (eq (car type
) t
))
1188 (and (dolist (type (cdr type
) *the-class-t
*)
1189 (when (and (consp type
) (not (eq (car type
) 'not
)))
1190 (return (class-from-type type
)))))
1192 (eql (class-of (cadr type
)))
1193 (class-eq (cadr type
))
1194 (class (cadr type
)))))
1196 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1197 (defun augment-type (new-type known-type
)
1198 (if (or (eq known-type t
)
1199 (eq (car new-type
) 'eql
))
1201 (let ((so-far (if (and (consp known-type
) (eq (car known-type
) 'and
))
1203 (list known-type
))))
1204 (unless (eq (car new-type
) 'not
)
1206 (mapcan (lambda (type)
1207 (unless (*subtypep new-type type
)
1212 `(and ,new-type
,@so-far
)))))
1214 (defun generate-discrimination-net-internal
1215 (gf methods types methods-function test-fun type-function
)
1216 (let* ((arg-info (gf-arg-info gf
))
1217 (precedence (arg-info-precedence arg-info
))
1218 (nreq (arg-info-number-required arg-info
))
1219 (metatypes (arg-info-metatypes arg-info
)))
1220 (labels ((do-column (p-tail contenders known-types
)
1222 (let* ((position (car p-tail
))
1223 (known-type (or (nth position types
) t
)))
1224 (if (eq (nth position metatypes
) t
)
1225 (do-column (cdr p-tail
) contenders
1226 (cons (cons position known-type
)
1228 (do-methods p-tail contenders
1229 known-type
() known-types
)))
1230 (funcall methods-function contenders
1231 (let ((k-t (make-list nreq
)))
1232 (dolist (index+type known-types
)
1233 (setf (nth (car index
+type
) k-t
)
1236 (do-methods (p-tail contenders known-type winners known-types
)
1238 ;; is a (sorted) list of methods that must be discriminated.
1240 ;; is the type of this argument, constructed from tests
1243 ;; is a (sorted) list of methods that are potentially
1244 ;; applicable after the discrimination has been made.
1245 (if (null contenders
)
1246 (do-column (cdr p-tail
)
1248 (cons (cons (car p-tail
) known-type
)
1250 (let* ((position (car p-tail
))
1251 (method (car contenders
))
1252 (specl (nth position
(method-specializers method
)))
1253 (type (funcall type-function
1254 (type-from-specializer specl
))))
1255 (multiple-value-bind (app-p maybe-app-p
)
1256 (specializer-applicable-using-type-p type known-type
)
1257 (flet ((determined-to-be (truth-value)
1258 (if truth-value app-p
(not maybe-app-p
)))
1259 (do-if (truth &optional implied
)
1260 (let ((ntype (if truth type
`(not ,type
))))
1265 (augment-type ntype known-type
))
1267 (append winners
`(,method
))
1270 (cond ((determined-to-be nil
) (do-if nil t
))
1271 ((determined-to-be t
) (do-if t t
))
1272 (t (funcall test-fun position type
1273 (do-if t
) (do-if nil
))))))))))
1274 (do-column precedence methods
()))))
1276 (defun compute-secondary-dispatch-function (generic-function net
&optional
1277 method-alist wrappers
)
1278 (function-funcall (compute-secondary-dispatch-function1 generic-function net
)
1279 method-alist wrappers
))
1281 (defvar *eq-case-table-limit
* 15)
1282 (defvar *case-table-limit
* 10)
1284 (defun compute-mcase-parameters (case-list)
1285 (unless (eq t
(caar (last case-list
)))
1286 (error "The key for the last case arg to mcase was not T"))
1287 (let* ((eq-p (dolist (case case-list t
)
1288 (unless (or (eq (car case
) t
)
1289 (symbolp (caar case
)))
1291 (len (1- (length case-list
)))
1292 (type (cond ((= len
1)
1296 *eq-case-table-limit
*
1297 *case-table-limit
*))
1303 (defmacro mlookup
(key info default
&optional eq-p type
)
1304 (unless (or (eq eq-p t
) (null eq-p
))
1305 (bug "Invalid eq-p argument: ~S" eq-p
))
1309 (declare (optimize (inhibit-warnings 3)))
1310 (,(if eq-p
'eq
'eql
) ,key
(car ,info
)))
1314 `(dolist (e ,info
,default
)
1316 (declare (optimize (inhibit-warnings 3)))
1317 (,(if eq-p
'eq
'eql
) (car e
) ,key
))
1320 `(gethash ,key
,info
,default
))))
1322 (defun net-test-converter (form)
1324 (default-test-converter form
)
1326 ((invoke-effective-method-function invoke-fast-method-call
1327 invoke-effective-narrow-method-function
)
1334 `(mlookup ,(cadr form
)
1337 ,@(compute-mcase-parameters (cddr form
))))
1338 (t (default-test-converter form
)))))
1340 (defun net-code-converter (form)
1342 (default-code-converter form
)
1344 ((methods unordered-methods
)
1345 (let ((gensym (gensym)))
1349 (let ((mp (compute-mcase-parameters (cddr form
)))
1350 (gensym (gensym)) (default (gensym)))
1351 (values `(mlookup ,(cadr form
) ,gensym
,default
,@mp
)
1352 (list gensym default
))))
1354 (default-code-converter form
)))))
1356 (defun net-constant-converter (form generic-function
)
1357 (or (let ((c (methods-converter form generic-function
)))
1360 (default-constant-converter form
)
1363 (let* ((mp (compute-mcase-parameters (cddr form
)))
1364 (list (mapcar (lambda (clause)
1365 (let ((key (car clause
))
1366 (meth (cadr clause
)))
1367 (cons (if (consp key
) (car key
) key
)
1369 meth generic-function
))))
1371 (default (car (last list
))))
1372 (list (list* :mcase mp
(nbutlast list
))
1375 (default-constant-converter form
))))))
1377 (defun methods-converter (form generic-function
)
1378 (cond ((and (consp form
) (eq (car form
) 'methods
))
1380 (get-effective-method-function1 generic-function
(cadr form
))))
1381 ((and (consp form
) (eq (car form
) 'unordered-methods
))
1382 (default-secondary-dispatch-function generic-function
))))
1384 (defun convert-methods (constant method-alist wrappers
)
1385 (if (and (consp constant
)
1386 (eq (car constant
) '.methods.
))
1387 (funcall (cdr constant
) method-alist wrappers
)
1390 (defun convert-table (constant method-alist wrappers
)
1391 (cond ((and (consp constant
)
1392 (eq (car constant
) :mcase
))
1393 (let ((alist (mapcar (lambda (k+m
)
1395 (convert-methods (cdr k
+m
)
1399 (mp (cadr constant
)))
1406 (let ((table (make-hash-table :test
(if (car mp
) 'eq
'eql
))))
1408 (setf (gethash (car k
+m
) table
) (cdr k
+m
)))
1411 (defun compute-secondary-dispatch-function1 (generic-function net
1412 &optional function-p
)
1414 ((and (eq (car net
) 'methods
) (not function-p
))
1415 (get-effective-method-function1 generic-function
(cadr net
)))
1417 (let* ((name (generic-function-name generic-function
))
1418 (arg-info (gf-arg-info generic-function
))
1419 (metatypes (arg-info-metatypes arg-info
))
1420 (nargs (length metatypes
))
1421 (applyp (arg-info-applyp arg-info
))
1422 (fmc-arg-info (cons nargs applyp
))
1423 (arglist (if function-p
1424 (make-dfun-lambda-list nargs applyp
)
1425 (make-fast-method-call-lambda-list nargs applyp
))))
1426 (multiple-value-bind (cfunction constants
)
1427 ;; We don't want NAMED-LAMBDA for any expressions handed to FNGEN,
1428 ;; because name mismatches will render the hashing ineffective.
1429 (get-fun1 `(lambda ,arglist
1430 (declare (optimize (sb-c::store-closure-debug-pointer
3)))
1431 ,@(unless function-p
1432 `((declare (ignore .pv. .next-method-call.
))))
1433 (locally (declare #.
*optimize-speed
*)
1435 ,(make-emf-call nargs applyp
'emf
))))
1436 #'net-test-converter
1437 #'net-code-converter
1439 (net-constant-converter form generic-function
)))
1440 (lambda (method-alist wrappers
)
1441 (let* ((alist (list nil
))
1443 (dolist (constant constants
)
1444 (let* ((a (or (dolist (a alist nil
)
1445 (when (eq (car a
) constant
)
1449 constant method-alist wrappers
)
1451 constant method-alist wrappers
)))))
1453 (setf (cdr alist-tail
) new
)
1454 (setf alist-tail new
)))
1455 (let ((function (apply cfunction
(mapcar #'cdr
(cdr alist
)))))
1457 (set-fun-name function
`(gf-dispatch ,name
))
1458 (make-fast-method-call
1459 :function
(set-fun-name function
`(sdfun-method ,name
))
1460 :arg-info fmc-arg-info
))))))))))
1462 (defvar *show-make-unordered-methods-emf-calls
* nil
)
1464 (defun make-unordered-methods-emf (generic-function methods
)
1465 (when *show-make-unordered-methods-emf-calls
*
1466 (format t
"~&make-unordered-methods-emf ~S~%"
1467 (generic-function-name generic-function
)))
1468 (lambda (&rest args
)
1469 (let* ((types (types-from-args generic-function args
'eql
))
1470 (smethods (sort-applicable-methods generic-function
1473 (emf (get-effective-method-function generic-function smethods
)))
1474 (invoke-emf emf args
))))
1476 ;;; The value returned by compute-discriminating-function is a function
1477 ;;; object. It is called a discriminating function because it is called
1478 ;;; when the generic function is called and its role is to discriminate
1479 ;;; on the arguments to the generic function and then call appropriate
1480 ;;; method functions.
1482 ;;; A discriminating function can only be called when it is installed as
1483 ;;; the funcallable instance function of the generic function for which
1484 ;;; it was computed.
1486 ;;; More precisely, if compute-discriminating-function is called with
1487 ;;; an argument <gf1>, and returns a result <df1>, that result must
1488 ;;; not be passed to apply or funcall directly. Rather, <df1> must be
1489 ;;; stored as the funcallable instance function of the same generic
1490 ;;; function <gf1> (using SET-FUNCALLABLE-INSTANCE-FUNCTION). Then the
1491 ;;; generic function can be passed to funcall or apply.
1493 ;;; An important exception is that methods on this generic function are
1494 ;;; permitted to return a function which itself ends up calling the value
1495 ;;; returned by a more specific method. This kind of `encapsulation' of
1496 ;;; discriminating function is critical to many uses of the MOP.
1498 ;;; As an example, the following canonical case is legal:
1500 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1501 ;;; (let ((std (call-next-method)))
1503 ;;; (print (list 'call-to-gf gf arg))
1504 ;;; (funcall std arg))))
1506 ;;; Because many discriminating functions would like to use a dynamic
1507 ;;; strategy in which the precise discriminating function changes with
1508 ;;; time it is important to specify how a discriminating function is
1509 ;;; permitted itself to change the funcallable instance function of the
1510 ;;; generic function.
1512 ;;; Discriminating functions may set the funcallable instance function
1513 ;;; of the generic function, but the new value must be generated by making
1514 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1515 ;;; more specific methods which may have encapsulated the discriminating
1516 ;;; function will get a chance to encapsulate the new, inner discriminating
1519 ;;; This implies that if a discriminating function wants to modify itself
1520 ;;; it should first store some information in the generic function proper,
1521 ;;; and then call compute-discriminating-function. The appropriate method
1522 ;;; on compute-discriminating-function will see the information stored in
1523 ;;; the generic function and generate a discriminating function accordingly.
1525 ;;; The following is an example of a discriminating function which modifies
1526 ;;; itself in accordance with this protocol:
1528 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1530 ;;; (cond (<some condition>
1531 ;;; <store some info in the generic function>
1532 ;;; (set-funcallable-instance-function
1534 ;;; (compute-discriminating-function gf))
1535 ;;; (funcall gf arg))
1537 ;;; <call-a-method-of-gf>))))
1539 ;;; Whereas this code would not be legal:
1541 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1543 ;;; (cond (<some condition>
1544 ;;; (set-funcallable-instance-function
1546 ;;; (lambda (a) ..))
1547 ;;; (funcall gf arg))
1549 ;;; <call-a-method-of-gf>))))
1551 ;;; NOTE: All the examples above assume that all instances of the class
1552 ;;; my-generic-function accept only one argument.
1554 (defun slot-value-using-class-dfun (class object slotd
)
1555 (declare (ignore class
))
1556 (funcall (slot-info-reader (slot-definition-info slotd
)) object
))
1558 (defun setf-slot-value-using-class-dfun (new-value class object slotd
)
1559 (declare (ignore class
))
1560 (funcall (slot-info-writer (slot-definition-info slotd
)) new-value object
))
1562 (defun slot-boundp-using-class-dfun (class object slotd
)
1563 (declare (ignore class
))
1564 (funcall (slot-info-boundp (slot-definition-info slotd
)) object
))
1566 (defun special-case-for-compute-discriminating-function-p (gf)
1567 (or (eq gf
#'slot-value-using-class
)
1568 (eq gf
#'(setf slot-value-using-class
))
1569 (eq gf
#'slot-boundp-using-class
)))
1571 ;;; this is the normal function for computing the discriminating
1572 ;;; function of a standard-generic-function
1573 (let (initial-print-object-cache)
1574 (defun standard-compute-discriminating-function (gf)
1575 (let ((dfun-state (slot-value gf
'dfun-state
)))
1576 (when (special-case-for-compute-discriminating-function-p gf
)
1577 ;; if we have a special case for
1578 ;; COMPUTE-DISCRIMINATING-FUNCTION, then (at least for the
1579 ;; special cases implemented as of 2006-05-09) any information
1580 ;; in the cache is misplaced.
1581 (aver (null dfun-state
)))
1582 (typecase dfun-state
1584 (when (eq gf
#'compute-applicable-methods
)
1585 (update-all-c-a-m-gf-info gf
))
1587 ((eq gf
#'slot-value-using-class
)
1588 (update-slot-value-gf-info gf
'reader
)
1589 #'slot-value-using-class-dfun
)
1590 ((eq gf
#'(setf slot-value-using-class
))
1591 (update-slot-value-gf-info gf
'writer
)
1592 #'setf-slot-value-using-class-dfun
)
1593 ((eq gf
#'slot-boundp-using-class
)
1594 (update-slot-value-gf-info gf
'boundp
)
1595 #'slot-boundp-using-class-dfun
)
1596 ;; KLUDGE: PRINT-OBJECT is not a special-case in the sense
1597 ;; of having a desperately special discriminating function.
1598 ;; However, it is important that the machinery for printing
1599 ;; conditions for stack and heap exhaustion, and the
1600 ;; restarts offered by the debugger, work without consuming
1601 ;; many extra resources. This way (testing by name of GF
1602 ;; rather than by identity) was the only way I found to get
1603 ;; this to bootstrap, given that the PRINT-OBJECT generic
1604 ;; function is only set up later, in
1605 ;; SRC;PCL;PRINT-OBJECT.LISP. -- CSR, 2008-06-09
1606 ((eq (slot-value gf
'name
) 'print-object
)
1607 (let ((nkeys (nth-value 3 (get-generic-fun-info gf
))))
1609 ;; KLUDGE: someone has defined a method
1610 ;; specialized on the second argument: punt.
1611 (setf initial-print-object-cache nil
)
1612 (make-initial-dfun gf
))
1613 (initial-print-object-cache
1614 (multiple-value-bind (dfun cache info
)
1615 (make-caching-dfun gf
(copy-cache initial-print-object-cache
))
1616 (set-dfun gf dfun cache info
)))
1617 ;; the relevant PRINT-OBJECT methods get defined
1618 ;; late, by delayed DEF!METHOD. We mustn't cache
1619 ;; the effective method for our classes earlier
1620 ;; than the relevant PRINT-OBJECT methods are
1622 ((boundp 'sb-impl
::*delayed-def
!method-args
*)
1623 (make-initial-dfun gf
))
1624 (t (multiple-value-bind (dfun cache info
)
1625 (make-final-dfun-internal
1627 (mapcar (lambda (x) (list (find-class x
)))
1628 '(sb-kernel::control-stack-exhausted
1629 sb-kernel
::binding-stack-exhausted
1630 sb-kernel
::alien-stack-exhausted
1631 sb-kernel
::heap-exhausted-error
1633 (setq initial-print-object-cache cache
)
1634 (set-dfun gf dfun
(copy-cache cache
) info
))))))
1635 ((gf-precompute-dfun-and-emf-p (slot-value gf
'arg-info
))
1636 (make-final-dfun gf
))
1638 (make-initial-dfun gf
))))
1639 (function dfun-state
)
1640 (cons (car dfun-state
))))))
1642 ;;; in general we need to support SBCL's encapsulation for generic
1643 ;;; functions: the default implementation of encapsulation changes the
1644 ;;; identity of the function bound to a name, which breaks anything
1645 ;;; class-based, so we implement the encapsulation ourselves in the
1646 ;;; discriminating function.
1647 (defun sb-impl::encapsulate-generic-function
(gf type function
)
1648 (push (cons type function
) (generic-function-encapsulations gf
))
1649 (reinitialize-instance gf
))
1651 (defun sb-impl::unencapsulate-generic-function
(gf type
)
1652 (setf (generic-function-encapsulations gf
)
1653 (remove type
(generic-function-encapsulations gf
)
1654 :key
#'car
:count
1))
1655 (reinitialize-instance gf
))
1656 (defun sb-impl::encapsulated-generic-function-p
(gf type
)
1657 (position type
(generic-function-encapsulations gf
) :key
#'car
))
1658 (defun maybe-encapsulate-discriminating-function (gf encs std
)
1661 (let ((inner (maybe-encapsulate-discriminating-function
1663 (function (cdar encs
)))
1664 (lambda (&rest args
)
1665 (apply function inner args
)))))
1666 (defmethod compute-discriminating-function ((gf standard-generic-function
))
1667 (standard-compute-discriminating-function gf
))
1668 (defmethod compute-discriminating-function :around
((gf standard-generic-function
))
1669 (maybe-encapsulate-discriminating-function
1670 gf
(generic-function-encapsulations gf
) (call-next-method)))
1672 (defmethod (setf class-name
) (new-value class
)
1673 (let ((classoid (layout-classoid (class-wrapper class
))))
1674 (if (and new-value
(symbolp new-value
))
1675 (setf (classoid-name classoid
) new-value
)
1676 (setf (classoid-name classoid
) nil
)))
1677 (reinitialize-instance class
:name new-value
)
1680 (defmethod (setf generic-function-name
) (new-value generic-function
)
1681 (reinitialize-instance generic-function
:name new-value
)
1684 (defmethod function-keywords ((method standard-method
))
1685 (multiple-value-bind (llks nreq nopt keywords
)
1686 (analyze-lambda-list (if (consp method
)
1687 (early-method-lambda-list method
)
1688 (method-lambda-list method
)))
1689 (declare (ignore nreq nopt
))
1690 (values keywords
(ll-kwds-allowp llks
))))
1692 (defmethod function-keyword-parameters ((method standard-method
))
1693 (multiple-value-bind (llks nreq nopt keywords keyword-parameters
)
1694 (analyze-lambda-list (if (consp method
)
1695 (early-method-lambda-list method
)
1696 (method-lambda-list method
)))
1697 (declare (ignore nreq nopt keywords
))
1698 (values keyword-parameters
(ll-kwds-allowp llks
))))
1700 ;; FIXME: this is just: "parse, set keys to nil, unparse" (I think).
1701 (defun method-ll->generic-function-ll
(ll)
1702 (multiple-value-bind (llks nreq nopt keywords keyword-parameters
)
1703 (analyze-lambda-list ll
)
1704 (declare (ignore llks nreq nopt keywords
))
1705 (remove-if (lambda (s)
1706 (or (memq s keyword-parameters
)
1707 (eq s
'&allow-other-keys
)))
1710 ;;; This is based on the rules of method lambda list congruency
1711 ;;; defined in the spec. The lambda list it constructs is the pretty
1712 ;;; union of the lambda lists of the generic function and of all its
1713 ;;; methods. It doesn't take method applicability into account at all
1716 ;;; (Notice that we ignore &AUX variables as they're not part of the
1717 ;;; "public interface" of a function.)
1719 (defmethod generic-function-pretty-arglist
1720 ((generic-function standard-generic-function
))
1721 (let ((gf-lambda-list (generic-function-lambda-list generic-function
))
1722 (methods (generic-function-methods generic-function
)))
1725 (multiple-value-bind (gf.llks gf.required gf.optional gf.rest gf.keys
)
1726 (parse-lambda-list gf-lambda-list
:silent t
)
1727 ;; Possibly extend the keyword parameters of the gf by
1728 ;; additional key parameters of its methods:
1729 (let ((methods.keys nil
) (methods.allowp nil
))
1731 (multiple-value-bind (m.keyparams m.allow-other-keys
)
1732 (function-keyword-parameters m
)
1733 (setq methods.keys
(union methods.keys m.keyparams
:key
#'maybe-car
))
1734 (setq methods.allowp
(or methods.allowp m.allow-other-keys
))))
1735 (let ((arglist '()))
1736 (when (or (ll-kwds-allowp gf.llks
) methods.allowp
)
1737 (push '&allow-other-keys arglist
))
1738 (when (or gf.keys methods.keys
)
1739 ;; We make sure that the keys of the gf appear before
1740 ;; those of its methods, since they're probably more
1741 ;; generally appliable.
1742 (setq arglist
(nconc (list '&key
) gf.keys
1743 (nset-difference methods.keys gf.keys
)
1746 (setq arglist
(nconc (cons '&rest gf.rest
) arglist
)))
1748 (setq arglist
(nconc (list '&optional
) gf.optional arglist
)))
1749 (nconc gf.required arglist
)))))))
1751 (defun maybe-car (thing)