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 (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
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
))
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 (declare (ignore method ll
))
86 (defun check-method-function (method fun
)
87 (unless (functionp fun
)
88 (invalid-method-initarg method
"~@<~S of ~S is not a ~S.~@:>"
89 :function fun
'function
)))
91 (defun check-qualifiers (method qualifiers
)
92 (flet ((improper-list ()
93 (invalid-method-initarg method
94 "~@<~S of ~S is an improper list.~@:>"
95 :qualifiers qualifiers
)))
96 (dolist-carefully (q qualifiers improper-list
)
97 (unless (and q
(atom q
))
98 (invalid-method-initarg method
99 "~@<~S, in ~S ~S, is not a non-~S atom.~@:>"
100 q
:qualifiers qualifiers
'null
)))))
102 (defun check-slot-name (method name
)
103 (declare (ignore method
))
104 (unless (symbolp name
)
105 (invalid-method-initarg "~@<~S of ~S is not a ~S.~@:>"
106 :slot-name name
'symbol
)))
108 (defun check-specializers (method specializers
)
109 (flet ((improper-list ()
110 (invalid-method-initarg method
111 "~@<~S of ~S is an improper list.~@:>"
112 :specializers specializers
)))
113 (dolist-carefully (s specializers improper-list
)
114 (unless (specializerp s
)
115 (invalid-method-initarg method
116 "~@<~S, in ~S ~S, is not a ~S.~@:>"
117 s
:specializers specializers
'specializer
)))
118 ;; KLUDGE: ANSI says that it's not valid to have methods
119 ;; specializing on classes which are "not defined", leaving
120 ;; unclear what the definedness of a class is; AMOP suggests that
121 ;; forward-referenced-classes, since they have proper names and
122 ;; all, are at least worthy of some level of definition. We allow
123 ;; methods specialized on forward-referenced-classes, but it's
124 ;; non-portable and potentially dubious, so
125 (let ((frcs (remove-if-not #'forward-referenced-class-p specializers
)))
127 (style-warn "~@<Defining a method using ~
128 ~V[~;~1{~S~}~;~1{~S and ~S~}~:;~{~#[~;and ~]~S~^, ~}~] ~
129 as ~2:*~V[~;a specializer~:;specializers~].~@:>"
130 (length frcs
) frcs
)))))
132 (defmethod shared-initialize :before
133 ((method standard-method
) slot-names
&key
134 qualifiers lambda-list specializers function documentation
)
135 (declare (ignore slot-names
))
136 ;; FIXME: it's not clear to me (CSR, 2006-08-09) why methods get
137 ;; this extra paranoia and nothing else does; either everything
138 ;; should be aggressively checking initargs, or nothing much should.
139 ;; In either case, it would probably be better to have :type
140 ;; declarations in slots, which would then give a suitable type
141 ;; error (if we implement type-checking for slots...) rather than
142 ;; this hand-crafted thing.
143 (check-qualifiers method qualifiers
)
144 (check-lambda-list method lambda-list
)
145 (check-specializers method specializers
)
146 (check-method-function method function
)
147 (check-documentation method documentation
))
149 (defmethod shared-initialize :before
150 ((method standard-accessor-method
) slot-names
&key
151 slot-name slot-definition
)
152 (declare (ignore slot-names
))
153 (unless slot-definition
154 (check-slot-name method slot-name
)))
156 (defmethod shared-initialize :after
((method standard-method
) slot-names
157 &rest initargs
&key
((method-cell method-cell
)))
158 (declare (ignore slot-names method-cell
))
159 (initialize-method-function initargs method
))
161 (defvar *the-class-generic-function
*
162 (find-class 'generic-function
))
163 (defvar *the-class-standard-generic-function
*
164 (find-class 'standard-generic-function
))
166 (defmethod shared-initialize :before
167 ((generic-function standard-generic-function
)
169 &key
(name nil namep
)
170 (lambda-list () lambda-list-p
)
171 argument-precedence-order
174 (method-class nil method-class-supplied-p
)
175 (method-combination nil method-combination-supplied-p
))
176 (declare (ignore slot-names
177 declarations argument-precedence-order documentation
178 lambda-list lambda-list-p
))
181 (set-fun-name generic-function name
))
183 (flet ((initarg-error (initarg value string
)
184 (error "when initializing the generic function ~S:~%~
185 The ~S initialization argument was: ~A.~%~
187 generic-function initarg value string
)))
188 (cond (method-class-supplied-p
189 (when (symbolp method-class
)
190 (setq method-class
(find-class method-class
)))
191 (unless (and (classp method-class
)
192 (*subtypep
(class-eq-specializer method-class
)
194 (initarg-error :method-class
196 "a subclass of the class METHOD"))
197 (setf (slot-value generic-function
'method-class
) method-class
))
198 ((slot-boundp generic-function
'method-class
))
200 (initarg-error :method-class
202 "a subclass of the class METHOD")))
203 (cond (method-combination-supplied-p
204 (unless (method-combination-p method-combination
)
205 (initarg-error :method-combination
207 "a method combination object")))
208 ((slot-boundp generic-function
'%method-combination
))
210 (initarg-error :method-combination
212 "a method combination object")))))
214 (defun find-generic-function (name &optional
(errorp t
))
215 (let ((fun (and (fboundp name
) (fdefinition name
))))
217 ((and fun
(typep fun
'generic-function
)) fun
)
218 (errorp (error "No generic function named ~S." name
))
221 (defun real-add-named-method (generic-function-name qualifiers
222 specializers lambda-list
&rest other-initargs
)
223 (unless (and (fboundp generic-function-name
)
224 (typep (fdefinition generic-function-name
) 'generic-function
))
225 (warn 'implicit-generic-function-warning
:name generic-function-name
))
226 (let* ((existing-gf (find-generic-function generic-function-name nil
))
229 (ensure-generic-function
230 generic-function-name
231 :generic-function-class
(class-of existing-gf
))
232 (ensure-generic-function generic-function-name
)))
233 (proto (method-prototype-for-gf generic-function-name
)))
234 ;; FIXME: Destructive modification of &REST list.
235 (setf (getf (getf other-initargs
'plist
) :name
)
236 (make-method-spec generic-function qualifiers specializers
))
237 (let ((new (apply #'make-instance
(class-of proto
)
238 :qualifiers qualifiers
:specializers specializers
239 :lambda-list lambda-list other-initargs
)))
240 (add-method generic-function new
)
243 (define-condition find-method-length-mismatch
244 (reference-condition simple-error
)
246 (:default-initargs
:references
(list '(:ansi-cl
:function find-method
))))
248 (defun real-get-method (generic-function qualifiers specializers
250 always-check-specializers
)
251 (let ((lspec (length specializers
))
252 (methods (generic-function-methods generic-function
)))
253 (when (or methods always-check-specializers
)
254 (let ((nreq (length (arg-info-metatypes (gf-arg-info
255 generic-function
)))))
256 ;; Since we internally bypass FIND-METHOD by using GET-METHOD
257 ;; instead we need to to this here or users may get hit by a
258 ;; failed AVER instead of a sensible error message.
259 (when (/= lspec nreq
)
261 'find-method-length-mismatch
263 "~@<The generic function ~S takes ~D required argument~:P; ~
264 was asked to find a method with specializers ~S~@:>"
265 :format-arguments
(list generic-function nreq specializers
)))))
267 (dolist (method methods
)
268 (let ((mspecializers (method-specializers method
)))
269 (aver (= lspec
(length mspecializers
)))
270 (when (and (equal qualifiers
(safe-method-qualifiers method
))
271 (every #'same-specializer-p specializers
272 (method-specializers method
)))
277 (error "~@<There is no method on ~S with ~
278 ~:[no qualifiers~;~:*qualifiers ~S~] ~
279 and specializers ~S.~@:>"
280 generic-function qualifiers specializers
))))))
282 (defmethod find-method ((generic-function standard-generic-function
)
283 qualifiers specializers
&optional
(errorp t
))
284 ;; ANSI about FIND-METHOD: "The specializers argument contains the
285 ;; parameter specializers for the method. It must correspond in
286 ;; length to the number of required arguments of the generic
287 ;; function, or an error is signaled."
289 ;; This error checking is done by REAL-GET-METHOD.
291 generic-function qualifiers
292 ;; ANSI for FIND-METHOD seems to imply that in fact specializers
293 ;; should always be passed in parsed form instead of being parsed
294 ;; at this point. Since there's no ANSI-blessed way of getting an
295 ;; EQL specializer, that seems unnecessarily painful, so we are
296 ;; nice to our users. -- CSR, 2007-06-01
297 (parse-specializers generic-function specializers
) errorp t
))
299 ;;; Compute various information about a generic-function's arglist by looking
300 ;;; at the argument lists of the methods. The hair for trying not to use
301 ;;; &REST arguments lives here.
302 ;;; The values returned are:
303 ;;; number-of-required-arguments
304 ;;; the number of required arguments to this generic-function's
305 ;;; discriminating function
307 ;;; whether or not this generic-function's discriminating
308 ;;; function takes an &rest argument.
309 ;;; specialized-argument-positions
310 ;;; a list of the positions of the arguments this generic-function
311 ;;; specializes (e.g. for a classical generic-function this is the
313 (defmethod compute-discriminating-function-arglist-info
314 ((generic-function standard-generic-function
))
315 ;;(declare (values number-of-required-arguments &rest-argument-p
316 ;; specialized-argument-postions))
317 (let ((number-required nil
)
319 (specialized-positions ())
320 (methods (generic-function-methods generic-function
)))
321 (dolist (method methods
)
322 (multiple-value-setq (number-required restp specialized-positions
)
323 (compute-discriminating-function-arglist-info-internal
324 generic-function method number-required restp specialized-positions
)))
325 (values number-required restp
(sort specialized-positions
#'<))))
327 (defun compute-discriminating-function-arglist-info-internal
328 (generic-function method number-of-requireds restp
329 specialized-argument-positions
)
330 (declare (ignore generic-function
)
331 (type (or null fixnum
) number-of-requireds
))
333 (declare (fixnum requireds
))
334 ;; Go through this methods arguments seeing how many are required,
335 ;; and whether there is an &rest argument.
336 (dolist (arg (method-lambda-list method
))
337 (cond ((eq arg
'&aux
) (return))
338 ((memq arg
'(&optional
&rest
&key
))
339 (return (setq restp t
)))
340 ((memq arg lambda-list-keywords
))
341 (t (incf requireds
))))
342 ;; Now go through this method's type specifiers to see which
343 ;; argument positions are type specified. Treat T specially
344 ;; in the usual sort of way. For efficiency don't bother to
345 ;; keep specialized-argument-positions sorted, rather depend
346 ;; on our caller to do that.
348 (dolist (type-spec (method-specializers method
))
349 (unless (eq type-spec
*the-class-t
*)
350 (pushnew pos specialized-argument-positions
:test
#'eq
))
352 ;; Finally merge the values for this method into the values
353 ;; for the exisiting methods and return them. Note that if
354 ;; num-of-requireds is NIL it means this is the first method
355 ;; and we depend on that.
356 (values (min (or number-of-requireds requireds
) requireds
)
358 (and number-of-requireds
(/= number-of-requireds requireds
)))
359 specialized-argument-positions
)))
361 (defun make-discriminating-function-arglist (number-required-arguments restp
)
362 (nconc (let ((args nil
))
363 (dotimes (i number-required-arguments
)
364 (push (format-symbol *package
* ;; ! is this right?
365 "Discriminating Function Arg ~D"
370 `(&rest
,(format-symbol *package
*
371 "Discriminating Function &rest Arg")))))
373 (defmethod generic-function-argument-precedence-order
374 ((gf standard-generic-function
))
375 (aver (eq **boot-state
** 'complete
))
376 (loop with arg-info
= (gf-arg-info gf
)
377 with lambda-list
= (arg-info-lambda-list arg-info
)
378 for argument-position in
(arg-info-precedence arg-info
)
379 collect
(nth argument-position lambda-list
)))
381 (defmethod generic-function-lambda-list ((gf generic-function
))
384 (defmethod gf-fast-method-function-p ((gf standard-generic-function
))
385 (gf-info-fast-mf-p (slot-value gf
'arg-info
)))
387 (defmethod initialize-instance :after
((gf standard-generic-function
)
388 &key
(lambda-list nil lambda-list-p
)
389 argument-precedence-order
)
390 (with-slots (arg-info) gf
393 :lambda-list lambda-list
394 :argument-precedence-order argument-precedence-order
)
396 (when (arg-info-valid-p arg-info
)
399 (defmethod reinitialize-instance :around
400 ((gf standard-generic-function
) &rest args
&key
401 (lambda-list nil lambda-list-p
) (argument-precedence-order nil apo-p
))
402 (let ((old-mc (generic-function-method-combination gf
)))
403 (prog1 (call-next-method)
404 ;; KLUDGE: EQ is too strong a test.
405 (unless (eq old-mc
(generic-function-method-combination gf
))
406 (flush-effective-method-cache gf
))
408 ((and lambda-list-p apo-p
)
410 :lambda-list lambda-list
411 :argument-precedence-order argument-precedence-order
))
412 (lambda-list-p (set-arg-info gf
:lambda-list lambda-list
))
413 (t (set-arg-info gf
)))
414 (when (arg-info-valid-p (gf-arg-info gf
))
416 (map-dependents gf
(lambda (dependent)
417 (apply #'update-dependent gf dependent args
))))))
419 (declaim (special *lazy-dfun-compute-p
*))
421 (defun set-methods (gf methods
)
422 (setf (generic-function-methods gf
) nil
)
423 (loop (when (null methods
) (return gf
))
424 (real-add-method gf
(pop methods
) methods
)))
426 (define-condition new-value-specialization
(reference-condition error
)
427 ((%method
:initarg
:method
:reader new-value-specialization-method
))
430 (format s
"~@<Cannot add method ~S to ~S, as it specializes the ~
431 new-value argument.~@:>"
432 (new-value-specialization-method c
)
433 #'(setf slot-value-using-class
))))
434 (:default-initargs
:references
435 (list '(:sbcl
:node
"Metaobject Protocol")
436 '(:amop
:generic-function
(setf slot-value-using-class
)))))
438 (defgeneric values-for-add-method
(gf method
)
439 (:method
((gf standard-generic-function
) (method standard-method
))
440 ;; KLUDGE: Just a single generic dispatch, and everything else
441 ;; comes from permutation vectors. Would be nicer to define
442 ;; REAL-ADD-METHOD with a proper method so that we could efficiently
443 ;; use SLOT-VALUE there.
445 ;; Optimization note: REAL-ADD-METHOD has a lot of O(N) stuff in it (as
446 ;; does PCL as a whole). It should not be too hard to internally store
447 ;; many of the things we now keep in lists as either purely functional
448 ;; O(log N) sets, or --if we don't mind the memory cost-- using
449 ;; specialized hash-tables: most things are used to answer questions about
450 ;; set-membership, not ordering.
451 (values (slot-value gf
'%lock
)
452 (slot-value method
'qualifiers
)
453 (slot-value method
'specializers
)
454 (slot-value method
'lambda-list
)
455 (slot-value method
'%generic-function
)
456 (slot-value gf
'name
))))
458 (define-condition print-object-stream-specializer
(reference-condition simple-warning
)
461 :references
(list '(:ansi-cl
:function print-object
))
462 :format-control
"~@<Specializing on the second argument to ~S has ~
463 unportable effects, and also interferes with ~
464 precomputation of print functions for exceptional ~
466 :format-arguments
(list 'print-object
)))
468 (defun real-add-method (generic-function method
&optional skip-dfun-update-p
)
469 (flet ((similar-lambda-lists-p (old-method new-lambda-list
)
470 (multiple-value-bind (a-nreq a-nopt a-keyp a-restp
)
471 (analyze-lambda-list (method-lambda-list old-method
))
472 (multiple-value-bind (b-nreq b-nopt b-keyp b-restp
)
473 (analyze-lambda-list new-lambda-list
)
474 (and (= a-nreq b-nreq
)
476 (eq (or a-keyp a-restp
)
477 (or b-keyp b-restp
)))))))
478 (multiple-value-bind (lock qualifiers specializers new-lambda-list
480 (values-for-add-method generic-function method
)
482 (error "~@<The method ~S is already part of the generic ~
483 function ~S; it can't be added to another generic ~
484 function until it is removed from the first one.~@:>"
486 (when (and (eq name
'print-object
) (not (eq (second specializers
) *the-class-t
*)))
487 (warn 'print-object-stream-specializer
))
489 ;; System lock because interrupts need to be disabled as
490 ;; well: it would be bad to unwind and leave the gf in an
491 ;; inconsistent state.
492 (sb-thread::with-recursive-system-lock
(lock)
493 (let ((existing (get-method generic-function
498 ;; If there is already a method like this one then we must get
499 ;; rid of it before proceeding. Note that we call the generic
500 ;; function REMOVE-METHOD to remove it rather than doing it in
501 ;; some internal way.
502 (when (and existing
(similar-lambda-lists-p existing new-lambda-list
))
503 (remove-method generic-function existing
))
505 ;; KLUDGE: We have a special case here, as we disallow
506 ;; specializations of the NEW-VALUE argument to (SETF
507 ;; SLOT-VALUE-USING-CLASS). GET-ACCESSOR-METHOD-FUNCTION is
508 ;; the optimizing function here: it precomputes the effective
509 ;; method, assuming that there is no dispatch to be done on
510 ;; the new-value argument.
511 (when (and (eq generic-function
#'(setf slot-value-using-class
))
512 (not (eq *the-class-t
* (first specializers
))))
513 (error 'new-value-specialization
:method method
))
515 (setf (method-generic-function method
) generic-function
)
516 (pushnew method
(generic-function-methods generic-function
) :test
#'eq
)
517 (dolist (specializer specializers
)
518 (add-direct-method specializer method
))
520 ;; KLUDGE: SET-ARG-INFO contains the error-detecting logic for
521 ;; detecting attempts to add methods with incongruent lambda
522 ;; lists. However, according to Gerd Moellmann on cmucl-imp,
523 ;; it also depends on the new method already having been added
524 ;; to the generic function. Therefore, we need to remove it
526 (let ((remove-again-p t
))
529 (set-arg-info generic-function
:new-method method
)
530 (setq remove-again-p nil
))
532 (remove-method generic-function method
))))
534 ;; KLUDGE II: ANSI saith that it is not an error to add a
535 ;; method with invalid qualifiers to a generic function of the
536 ;; wrong kind; it's only an error at generic function
537 ;; invocation time; I dunno what the rationale was, and it
538 ;; sucks. Nevertheless, it's probably a programmer error, so
539 ;; let's warn anyway. -- CSR, 2003-08-20
540 (let ((mc (generic-function-method-combination generic-functioN
)))
542 ((eq mc
*standard-method-combination
*)
543 (when (and qualifiers
545 (not (memq (car qualifiers
)
546 '(:around
:before
:after
)))))
547 (warn "~@<Invalid qualifiers for standard method ~
548 combination in method ~S:~2I~_~S.~@:>"
550 ((short-method-combination-p mc
)
551 (let ((mc-name (method-combination-type-name mc
)))
552 (when (or (null qualifiers
)
554 (and (neq (car qualifiers
) :around
)
555 (neq (car qualifiers
) mc-name
)))
556 (warn "~@<Invalid qualifiers for ~S method combination ~
557 in method ~S:~2I~_~S.~@:>"
558 mc-name method qualifiers
))))))
559 (unless skip-dfun-update-p
560 (update-ctors 'add-method
561 :generic-function generic-function
563 (update-dfun generic-function
))
564 (setf (gf-info-needs-update generic-function
) t
)
565 (map-dependents generic-function
567 (update-dependent generic-function
568 dep
'add-method method
)))))
569 (serious-condition (c)
573 (defun real-remove-method (generic-function method
)
574 (when (eq generic-function
(method-generic-function method
))
575 (let ((lock (gf-lock generic-function
)))
576 ;; System lock because interrupts need to be disabled as well:
577 ;; it would be bad to unwind and leave the gf in an inconsistent
579 (sb-thread::with-recursive-system-lock
(lock)
580 (let* ((specializers (method-specializers method
)) ; flushable?
581 (methods (generic-function-methods generic-function
))
582 (new-methods (remove method methods
)))
583 (declare (ignore specializers
))
584 (setf (method-generic-function method
) nil
585 (generic-function-methods generic-function
) new-methods
)
586 (dolist (specializer (method-specializers method
))
587 (remove-direct-method specializer method
))
588 (set-arg-info generic-function
)
589 (update-ctors 'remove-method
590 :generic-function generic-function
592 (update-dfun generic-function
)
593 (setf (gf-info-needs-update generic-function
) t
)
594 (map-dependents generic-function
596 (update-dependent generic-function
597 dep
'remove-method method
)))))))
601 ;; Tell INFO about the generic function's methods' keys so that the
602 ;; compiler doesn't complain that the keys defined for some method are
604 (sb-ext:without-package-locks
605 (defun sb-c::maybe-update-info-for-gf
(name)
606 (let ((gf (if (fboundp name
) (fdefinition name
))))
607 (when (and gf
(generic-function-p gf
) (not (early-gf-p gf
))
608 (not (eq :declared
(info :function
:where-from name
)))
609 (gf-info-needs-update gf
))
610 (let* ((methods (generic-function-methods gf
))
611 (gf-lambda-list (generic-function-lambda-list gf
))
612 (tfun (constantly t
))
614 (multiple-value-bind (gf.required gf.optional gf.restp gf.rest
615 gf.keyp gf.keys gf.allowp
)
616 (parse-lambda-list gf-lambda-list
)
617 (declare (ignore gf.rest
))
618 ;; 7.6.4 point 5 probably entails that if any method says
619 ;; &allow-other-keys then the gf should be construed to
621 (let* ((allowp (or gf.allowp
622 (find '&allow-other-keys methods
624 :key
#'method-lambda-list
)))
628 (,@(mapcar tfun gf.required
)
630 `(&optional
,@(mapcar tfun gf.optional
)))
640 (mapcan #'function-keywords methods
)
641 (mapcar #'keyword-spec-name gf.keys
))))))
644 `(&key
,@all-keys
))))
645 ,@(when (and (not keysp
) allowp
)
648 `(&allow-other-keys
)))
650 (setf (info :function
:type name
) ftype
651 (info :function
:where-from name
) :defined-method
652 (gf-info-needs-update gf
) nil
)
655 (defun compute-applicable-methods-function (generic-function arguments
)
656 (values (compute-applicable-methods-using-types
658 (types-from-args generic-function arguments
'eql
))))
660 (defmethod compute-applicable-methods
661 ((generic-function generic-function
) arguments
)
662 (values (compute-applicable-methods-using-types
664 (types-from-args generic-function arguments
'eql
))))
666 (defmethod compute-applicable-methods-using-classes
667 ((generic-function generic-function
) classes
)
668 (compute-applicable-methods-using-types
670 (types-from-args generic-function classes
'class-eq
)))
672 (defun proclaim-incompatible-superclasses (classes)
673 (setq classes
(mapcar (lambda (class)
678 (dolist (class classes
)
679 (dolist (other-class classes
)
680 (unless (eq class other-class
)
681 (pushnew other-class
(class-incompatible-superclass-list class
) :test
#'eq
)))))
683 (defun superclasses-compatible-p (class1 class2
)
684 (let ((cpl1 (cpl-or-nil class1
))
685 (cpl2 (cpl-or-nil class2
)))
687 (dolist (ic (class-incompatible-superclass-list sc1
))
689 (return-from superclasses-compatible-p nil
))))))
692 #'proclaim-incompatible-superclasses
693 '(;; superclass class
694 (system-class std-class structure-class
) ; direct subclasses of pcl-class
695 (standard-class funcallable-standard-class
)
696 ;; superclass metaobject
697 (class eql-specializer class-eq-specializer method method-combination
698 generic-function slot-definition
)
699 ;; metaclass built-in-class
700 (number sequence character
; direct subclasses of t, but not array
701 standard-object structure-object
) ; or symbol
702 (number array character symbol
; direct subclasses of t, but not
703 standard-object structure-object
) ; sequence
704 (complex float rational
) ; direct subclasses of number
705 (integer ratio
) ; direct subclasses of rational
706 (list vector
) ; direct subclasses of sequence
707 (cons null
) ; direct subclasses of list
708 (string bit-vector
) ; direct subclasses of vector
711 (defmethod same-specializer-p ((specl1 specializer
) (specl2 specializer
))
714 (defmethod same-specializer-p ((specl1 class
) (specl2 class
))
717 (defmethod specializer-class ((specializer class
))
720 (defmethod same-specializer-p ((specl1 class-eq-specializer
)
721 (specl2 class-eq-specializer
))
722 (eq (specializer-class specl1
) (specializer-class specl2
)))
724 (defmethod same-specializer-p ((specl1 eql-specializer
)
725 (specl2 eql-specializer
))
726 (eq (specializer-object specl1
) (specializer-object specl2
)))
728 (defmethod specializer-class ((specializer eql-specializer
))
729 (class-of (slot-value specializer
'object
)))
731 (defun specializer-class-or-nil (specializer)
732 (and (standard-specializer-p specializer
)
733 (specializer-class specializer
)))
735 (defun error-need-at-least-n-args (function n
)
736 (error 'simple-program-error
737 :format-control
"~@<The function ~2I~_~S ~I~_requires ~
738 at least ~W argument~:P.~:>"
739 :format-arguments
(list function n
)))
741 (defun types-from-args (generic-function arguments
&optional type-modifier
)
742 (multiple-value-bind (nreq applyp metatypes nkeys arg-info
)
743 (get-generic-fun-info generic-function
)
744 (declare (ignore applyp metatypes nkeys
))
745 (let ((types-rev nil
))
746 (dotimes-fixnum (i nreq
)
749 (error-need-at-least-n-args (generic-function-name generic-function
)
751 (let ((arg (pop arguments
)))
752 (push (if type-modifier
`(,type-modifier
,arg
) arg
) types-rev
)))
753 (values (nreverse types-rev
) arg-info
))))
755 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes
)
756 (let* ((w wrappers
) (w-tail w
) (mt-tail metatypes
))
757 (dolist (class (if (listp classes
) classes
(list classes
)))
758 (unless (eq t
(car mt-tail
))
759 (let ((c-w (class-wrapper class
)))
760 (unless c-w
(return-from get-wrappers-from-classes nil
))
763 (setf (car w-tail
) c-w
764 w-tail
(cdr w-tail
)))))
765 (setq mt-tail
(cdr mt-tail
)))
768 (defun sdfun-for-caching (gf classes
)
769 (let ((types (mapcar #'class-eq-type classes
)))
770 (multiple-value-bind (methods all-applicable-and-sorted-p
)
771 (compute-applicable-methods-using-types gf types
)
772 (let ((generator (get-secondary-dispatch-function1
773 gf methods types nil t all-applicable-and-sorted-p
)))
774 (make-callable gf methods generator
775 nil
(mapcar #'class-wrapper classes
))))))
777 (defun value-for-caching (gf classes
)
778 (let ((methods (compute-applicable-methods-using-types
779 gf
(mapcar #'class-eq-type classes
))))
780 (method-plist-value (car methods
) :constant-value
)))
782 (defun default-secondary-dispatch-function (generic-function)
784 (let ((methods (compute-applicable-methods generic-function args
)))
786 (let ((emf (get-effective-method-function generic-function
788 (invoke-emf emf args
))
789 (call-no-applicable-method generic-function args
)))))
792 (loop (when (atom x
) (return (eq x y
)))
793 (when (atom y
) (return nil
))
794 (unless (eq (car x
) (car y
)) (return nil
))
798 (defvar *std-cam-methods
* nil
)
800 (defun compute-applicable-methods-emf (generic-function)
801 (if (eq **boot-state
** 'complete
)
802 (let* ((cam (gdefinition 'compute-applicable-methods
))
803 (cam-methods (compute-applicable-methods-using-types
804 cam
(list `(eql ,generic-function
) t
))))
805 (values (get-effective-method-function cam cam-methods
)
807 (or *std-cam-methods
*
808 (setq *std-cam-methods
*
809 (compute-applicable-methods-using-types
810 cam
(list `(eql ,cam
) t
)))))))
811 (values #'compute-applicable-methods-function t
)))
813 (defun compute-applicable-methods-emf-std-p (gf)
814 (gf-info-c-a-m-emf-std-p (gf-arg-info gf
)))
816 (defvar *old-c-a-m-gf-methods
* nil
)
818 (defun update-all-c-a-m-gf-info (c-a-m-gf)
819 (let ((methods (generic-function-methods c-a-m-gf
)))
820 (if (and *old-c-a-m-gf-methods
*
821 (every (lambda (old-method)
822 (member old-method methods
:test
#'eq
))
823 *old-c-a-m-gf-methods
*))
824 (let ((gfs-to-do nil
)
825 (gf-classes-to-do nil
))
826 (dolist (method methods
)
827 (unless (member method
*old-c-a-m-gf-methods
* :test
#'eq
)
828 (let ((specl (car (method-specializers method
))))
829 (if (eql-specializer-p specl
)
830 (pushnew (specializer-object specl
) gfs-to-do
:test
#'eq
)
831 (pushnew (specializer-class specl
) gf-classes-to-do
:test
#'eq
)))))
832 (map-all-generic-functions
834 (when (or (member gf gfs-to-do
:test
#'eq
)
835 (dolist (class gf-classes-to-do nil
)
837 (class-precedence-list (class-of gf
))
839 (update-c-a-m-gf-info gf
)))))
840 (map-all-generic-functions #'update-c-a-m-gf-info
))
841 (setq *old-c-a-m-gf-methods
* methods
)))
843 (defun update-gf-info (gf)
844 (update-c-a-m-gf-info gf
)
845 (update-gf-simple-accessor-type gf
))
847 (defun update-c-a-m-gf-info (gf)
848 (unless (early-gf-p gf
)
849 (multiple-value-bind (c-a-m-emf std-p
)
850 (compute-applicable-methods-emf gf
)
851 (let ((arg-info (gf-arg-info gf
)))
852 (setf (gf-info-static-c-a-m-emf arg-info
) c-a-m-emf
)
853 (setf (gf-info-c-a-m-emf-std-p arg-info
) std-p
)))))
855 (defun update-gf-simple-accessor-type (gf)
856 (let ((arg-info (gf-arg-info gf
)))
857 (setf (gf-info-simple-accessor-type arg-info
)
858 (let* ((methods (generic-function-methods gf
))
859 (class (and methods
(class-of (car methods
))))
862 (cond ((or (eq class
*the-class-standard-reader-method
*)
863 (eq class
*the-class-global-reader-method
*))
865 ((or (eq class
*the-class-standard-writer-method
*)
866 (eq class
*the-class-global-writer-method
*))
868 ((or (eq class
*the-class-standard-boundp-method
*)
869 (eq class
*the-class-global-boundp-method
*))
871 (when (and (gf-info-c-a-m-emf-std-p arg-info
)
873 (dolist (method (cdr methods
) t
)
874 (unless (eq class
(class-of method
)) (return nil
)))
875 (eq (generic-function-method-combination gf
)
876 *standard-method-combination
*))
880 ;;; CMUCL (Gerd's PCL, 2002-04-25) comment:
882 ;;; Return two values. First value is a function to be stored in
883 ;;; effective slot definition SLOTD for reading it with
884 ;;; SLOT-VALUE-USING-CLASS, setting it with (SETF
885 ;;; SLOT-VALUE-USING-CLASS) or testing it with
886 ;;; SLOT-BOUNDP-USING-CLASS. GF is one of these generic functions,
887 ;;; TYPE is one of the symbols READER, WRITER, BOUNDP. CLASS is
890 ;;; Second value is true if the function returned is one of the
891 ;;; optimized standard functions for the purpose, which are used
892 ;;; when only standard methods are applicable.
894 ;;; FIXME: Change all these wacky function names to something sane.
895 (defun get-accessor-method-function (gf type class slotd
)
896 (let* ((std-method (standard-svuc-method type
))
897 (str-method (structure-svuc-method type
))
898 (types1 `((eql ,class
) (class-eq ,class
) (eql ,slotd
)))
899 (types (if (eq type
'writer
) `(t ,@types1
) types1
))
900 (methods (compute-applicable-methods-using-types gf types
))
901 (std-p (null (cdr methods
))))
904 (get-optimized-std-accessor-method-function class slotd type
)
905 (let* ((optimized-std-fun
906 (get-optimized-std-slot-value-using-class-method-function
909 `((,(car (or (member std-method methods
:test
#'eq
)
910 (member str-method methods
:test
#'eq
)
912 'get-accessor-method-function
)))
913 ,optimized-std-fun
)))
915 (let ((wrappers (list (layout-of class
)
916 (class-wrapper class
)
918 (if (eq type
'writer
)
919 (cons (class-wrapper *the-class-t
*) wrappers
)
921 (sdfun (get-secondary-dispatch-function
922 gf methods types method-alist wrappers
)))
923 (get-accessor-from-svuc-method-function class slotd sdfun type
)))
926 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
927 (defun update-slot-value-gf-info (gf type
)
929 (update-std-or-str-methods gf type
))
930 (when (and (standard-svuc-method type
) (structure-svuc-method type
))
931 (flet ((update-accessor-info (class)
932 (when (class-finalized-p class
)
933 (dolist (slotd (class-slots class
))
934 (compute-slot-accessor-info slotd type gf
)))))
936 (update-accessor-info *new-class
*)
937 (map-all-classes #'update-accessor-info
'slot-object
)))))
939 (defvar *standard-slot-value-using-class-method
* nil
)
940 (defvar *standard-setf-slot-value-using-class-method
* nil
)
941 (defvar *standard-slot-boundp-using-class-method
* nil
)
942 (defvar *condition-slot-value-using-class-method
* nil
)
943 (defvar *condition-setf-slot-value-using-class-method
* nil
)
944 (defvar *condition-slot-boundp-using-class-method
* nil
)
945 (defvar *structure-slot-value-using-class-method
* nil
)
946 (defvar *structure-setf-slot-value-using-class-method
* nil
)
947 (defvar *structure-slot-boundp-using-class-method
* nil
)
949 (defun standard-svuc-method (type)
951 (reader *standard-slot-value-using-class-method
*)
952 (writer *standard-setf-slot-value-using-class-method
*)
953 (boundp *standard-slot-boundp-using-class-method
*)))
955 (defun set-standard-svuc-method (type method
)
957 (reader (setq *standard-slot-value-using-class-method
* method
))
958 (writer (setq *standard-setf-slot-value-using-class-method
* method
))
959 (boundp (setq *standard-slot-boundp-using-class-method
* method
))))
961 (defun condition-svuc-method (type)
963 (reader *condition-slot-value-using-class-method
*)
964 (writer *condition-setf-slot-value-using-class-method
*)
965 (boundp *condition-slot-boundp-using-class-method
*)))
967 (defun set-condition-svuc-method (type method
)
969 (reader (setq *condition-slot-value-using-class-method
* method
))
970 (writer (setq *condition-setf-slot-value-using-class-method
* method
))
971 (boundp (setq *condition-slot-boundp-using-class-method
* method
))))
973 (defun structure-svuc-method (type)
975 (reader *structure-slot-value-using-class-method
*)
976 (writer *structure-setf-slot-value-using-class-method
*)
977 (boundp *structure-slot-boundp-using-class-method
*)))
979 (defun set-structure-svuc-method (type method
)
981 (reader (setq *structure-slot-value-using-class-method
* method
))
982 (writer (setq *structure-setf-slot-value-using-class-method
* method
))
983 (boundp (setq *structure-slot-boundp-using-class-method
* method
))))
985 (defun update-std-or-str-methods (gf type
)
986 (dolist (method (generic-function-methods gf
))
987 (let ((specls (method-specializers method
)))
988 (when (and (or (not (eq type
'writer
))
989 (eq (pop specls
) *the-class-t
*))
990 (every #'classp specls
))
991 (cond ((and (eq (class-name (car specls
)) 'std-class
)
992 (eq (class-name (cadr specls
)) 'standard-object
)
993 (eq (class-name (caddr specls
))
994 'standard-effective-slot-definition
))
995 (set-standard-svuc-method type method
))
996 ((and (eq (class-name (car specls
)) 'condition-class
)
997 (eq (class-name (cadr specls
)) 'condition
)
998 (eq (class-name (caddr specls
))
999 'condition-effective-slot-definition
))
1000 (set-condition-svuc-method type method
))
1001 ((and (eq (class-name (car specls
)) 'structure-class
)
1002 (eq (class-name (cadr specls
)) 'structure-object
)
1003 (eq (class-name (caddr specls
))
1004 'structure-effective-slot-definition
))
1005 (set-structure-svuc-method type method
)))))))
1007 (defun mec-all-classes-internal (spec precompute-p
)
1008 (let ((wrapper (class-wrapper (specializer-class spec
))))
1009 (unless (or (not wrapper
) (invalid-wrapper-p wrapper
))
1010 (cons (specializer-class spec
)
1013 (not (or (eq spec
*the-class-t
*)
1014 (eq spec
*the-class-slot-object
*)
1015 (eq spec
*the-class-standard-object
*)
1016 (eq spec
*the-class-structure-object
*)))
1017 (let ((sc (class-direct-subclasses spec
)))
1019 (mapcan (lambda (class)
1020 (mec-all-classes-internal class precompute-p
))
1023 (defun mec-all-classes (spec precompute-p
)
1024 (let ((classes (mec-all-classes-internal spec precompute-p
)))
1025 (if (null (cdr classes
))
1027 (let* ((a-classes (cons nil classes
))
1029 (loop (when (null (cdr tail
))
1030 (return (cdr a-classes
)))
1031 (let ((class (cadr tail
))
1032 (ttail (cddr tail
)))
1033 (if (dolist (c ttail nil
)
1034 (when (eq class c
) (return t
)))
1035 (setf (cdr tail
) (cddr tail
))
1036 (setf tail
(cdr tail
)))))))))
1038 (defun mec-all-class-lists (spec-list precompute-p
)
1039 (if (null spec-list
)
1041 (let* ((car-all-classes (mec-all-classes (car spec-list
)
1043 (all-class-lists (mec-all-class-lists (cdr spec-list
)
1045 (mapcan (lambda (list)
1046 (mapcar (lambda (c) (cons c list
)) car-all-classes
))
1049 (defun make-emf-cache (generic-function valuep cache classes-list new-class
)
1050 (let* ((arg-info (gf-arg-info generic-function
))
1051 (nkeys (arg-info-nkeys arg-info
))
1052 (metatypes (arg-info-metatypes arg-info
))
1053 (wrappers (unless (eq nkeys
1) (make-list nkeys
)))
1054 (precompute-p (gf-precompute-dfun-and-emf-p arg-info
)))
1055 (flet ((add-class-list (classes)
1056 (when (or (null new-class
) (memq new-class classes
))
1057 (let ((%wrappers
(get-wrappers-from-classes
1058 nkeys wrappers classes metatypes
)))
1059 (when (and %wrappers
(not (probe-cache cache %wrappers
)))
1060 (let ((value (cond ((eq valuep t
)
1061 (sdfun-for-caching generic-function
1063 ((eq valuep
:constant-value
)
1064 (value-for-caching generic-function
1066 ;; need to get them again, as finalization might
1067 ;; have happened in between, which would
1068 ;; invalidate wrappers.
1069 (let ((wrappers (get-wrappers-from-classes
1070 nkeys wrappers classes metatypes
)))
1071 (when (if (atom wrappers
)
1072 (not (invalid-wrapper-p wrappers
))
1073 (every (complement #'invalid-wrapper-p
)
1075 (setq cache
(fill-cache cache wrappers value
))))))))))
1077 (mapc #'add-class-list classes-list
)
1078 (dolist (method (generic-function-methods generic-function
))
1079 (mapc #'add-class-list
1080 (mec-all-class-lists (method-specializers method
)
1084 (defmacro class-test
(arg class
)
1086 ((eq class
*the-class-t
*) t
)
1087 ((eq class
*the-class-slot-object
*)
1088 `(not (typep (classoid-of ,arg
) 'system-classoid
)))
1089 ((eq class
*the-class-standard-object
*)
1090 `(or (std-instance-p ,arg
) (fsc-instance-p ,arg
)))
1091 ((eq class
*the-class-funcallable-standard-object
*)
1092 `(fsc-instance-p ,arg
))
1094 `(typep ,arg
',(class-name class
)))))
1096 (defmacro class-eq-test
(arg class
)
1097 `(eq (class-of ,arg
) ',class
))
1099 (defmacro eql-test
(arg object
)
1100 `(eql ,arg
',object
))
1102 (defun dnet-methods-p (form)
1104 (or (eq (car form
) 'methods
)
1105 (eq (car form
) 'unordered-methods
))))
1107 ;;; This is CASE, but without gensyms.
1108 (defmacro scase
(arg &rest clauses
)
1109 `(let ((.case-arg.
,arg
))
1110 (cond ,@(mapcar (lambda (clause)
1111 (list* (cond ((null (car clause
))
1113 ((consp (car clause
))
1114 (if (null (cdar clause
))
1119 ((member (car clause
) '(t otherwise
))
1122 `(eql .case-arg.
',(car clause
))))
1127 (defmacro mcase
(arg &rest clauses
) `(scase ,arg
,@clauses
))
1129 (defun generate-discrimination-net (generic-function methods types sorted-p
)
1130 (let* ((arg-info (gf-arg-info generic-function
))
1131 (c-a-m-emf-std-p (gf-info-c-a-m-emf-std-p arg-info
))
1132 (precedence (arg-info-precedence arg-info
)))
1133 (generate-discrimination-net-internal
1134 generic-function methods types
1135 (lambda (methods known-types
)
1137 (and c-a-m-emf-std-p
1139 (let ((sorted-methods nil
))
1141 (copy-list methods
) precedence
1143 (when sorted-methods
(return-from one-order-p nil
))
1144 (setq sorted-methods methods
)))
1145 (setq methods sorted-methods
))
1147 `(methods ,methods
,known-types
)
1148 `(unordered-methods ,methods
,known-types
)))
1149 (lambda (position type true-value false-value
)
1150 (let ((arg (dfun-arg-symbol position
)))
1151 (if (eq (car type
) 'eql
)
1152 (let* ((false-case-p (and (consp false-value
)
1153 (or (eq (car false-value
) 'scase
)
1154 (eq (car false-value
) 'mcase
))
1155 (eq arg
(cadr false-value
))))
1156 (false-clauses (if false-case-p
1158 `((t ,false-value
))))
1159 (case-sym (if (and (dnet-methods-p true-value
)
1161 (eq (car false-value
) 'mcase
)
1162 (dnet-methods-p false-value
)))
1165 (type-sym `(,(cadr type
))))
1167 (,type-sym
,true-value
)
1169 `(if ,(let ((arg (dfun-arg-symbol position
)))
1171 (class `(class-test ,arg
,(cadr type
)))
1172 (class-eq `(class-eq-test ,arg
,(cadr type
)))))
1177 (defun class-from-type (type)
1178 (if (or (atom type
) (eq (car type
) t
))
1181 (and (dolist (type (cdr type
) *the-class-t
*)
1182 (when (and (consp type
) (not (eq (car type
) 'not
)))
1183 (return (class-from-type type
)))))
1185 (eql (class-of (cadr type
)))
1186 (class-eq (cadr type
))
1187 (class (cadr type
)))))
1189 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1190 (defun augment-type (new-type known-type
)
1191 (if (or (eq known-type t
)
1192 (eq (car new-type
) 'eql
))
1194 (let ((so-far (if (and (consp known-type
) (eq (car known-type
) 'and
))
1196 (list known-type
))))
1197 (unless (eq (car new-type
) 'not
)
1199 (mapcan (lambda (type)
1200 (unless (*subtypep new-type type
)
1205 `(and ,new-type
,@so-far
)))))
1207 (defun generate-discrimination-net-internal
1208 (gf methods types methods-function test-fun type-function
)
1209 (let* ((arg-info (gf-arg-info gf
))
1210 (precedence (arg-info-precedence arg-info
))
1211 (nreq (arg-info-number-required arg-info
))
1212 (metatypes (arg-info-metatypes arg-info
)))
1213 (labels ((do-column (p-tail contenders known-types
)
1215 (let* ((position (car p-tail
))
1216 (known-type (or (nth position types
) t
)))
1217 (if (eq (nth position metatypes
) t
)
1218 (do-column (cdr p-tail
) contenders
1219 (cons (cons position known-type
)
1221 (do-methods p-tail contenders
1222 known-type
() known-types
)))
1223 (funcall methods-function contenders
1224 (let ((k-t (make-list nreq
)))
1225 (dolist (index+type known-types
)
1226 (setf (nth (car index
+type
) k-t
)
1229 (do-methods (p-tail contenders known-type winners known-types
)
1231 ;; is a (sorted) list of methods that must be discriminated.
1233 ;; is the type of this argument, constructed from tests
1236 ;; is a (sorted) list of methods that are potentially
1237 ;; applicable after the discrimination has been made.
1238 (if (null contenders
)
1239 (do-column (cdr p-tail
)
1241 (cons (cons (car p-tail
) known-type
)
1243 (let* ((position (car p-tail
))
1244 (method (car contenders
))
1245 (specl (nth position
(method-specializers method
)))
1246 (type (funcall type-function
1247 (type-from-specializer specl
))))
1248 (multiple-value-bind (app-p maybe-app-p
)
1249 (specializer-applicable-using-type-p type known-type
)
1250 (flet ((determined-to-be (truth-value)
1251 (if truth-value app-p
(not maybe-app-p
)))
1252 (do-if (truth &optional implied
)
1253 (let ((ntype (if truth type
`(not ,type
))))
1258 (augment-type ntype known-type
))
1260 (append winners
`(,method
))
1263 (cond ((determined-to-be nil
) (do-if nil t
))
1264 ((determined-to-be t
) (do-if t t
))
1265 (t (funcall test-fun position type
1266 (do-if t
) (do-if nil
))))))))))
1267 (do-column precedence methods
()))))
1269 (defun compute-secondary-dispatch-function (generic-function net
&optional
1270 method-alist wrappers
)
1271 (function-funcall (compute-secondary-dispatch-function1 generic-function net
)
1272 method-alist wrappers
))
1274 (defvar *eq-case-table-limit
* 15)
1275 (defvar *case-table-limit
* 10)
1277 (defun compute-mcase-parameters (case-list)
1278 (unless (eq t
(caar (last case-list
)))
1279 (error "The key for the last case arg to mcase was not T"))
1280 (let* ((eq-p (dolist (case case-list t
)
1281 (unless (or (eq (car case
) t
)
1282 (symbolp (caar case
)))
1284 (len (1- (length case-list
)))
1285 (type (cond ((= len
1)
1289 *eq-case-table-limit
*
1290 *case-table-limit
*))
1296 (defmacro mlookup
(key info default
&optional eq-p type
)
1297 (unless (or (eq eq-p t
) (null eq-p
))
1298 (bug "Invalid eq-p argument: ~S" eq-p
))
1302 (declare (optimize (inhibit-warnings 3)))
1303 (,(if eq-p
'eq
'eql
) ,key
(car ,info
)))
1307 `(dolist (e ,info
,default
)
1309 (declare (optimize (inhibit-warnings 3)))
1310 (,(if eq-p
'eq
'eql
) (car e
) ,key
))
1313 `(gethash ,key
,info
,default
))))
1315 (defun net-test-converter (form)
1317 (default-test-converter form
)
1319 ((invoke-effective-method-function invoke-fast-method-call
1320 invoke-effective-narrow-method-function
)
1327 `(mlookup ,(cadr form
)
1330 ,@(compute-mcase-parameters (cddr form
))))
1331 (t (default-test-converter form
)))))
1333 (defun net-code-converter (form)
1335 (default-code-converter form
)
1337 ((methods unordered-methods
)
1338 (let ((gensym (gensym)))
1342 (let ((mp (compute-mcase-parameters (cddr form
)))
1343 (gensym (gensym)) (default (gensym)))
1344 (values `(mlookup ,(cadr form
) ,gensym
,default
,@mp
)
1345 (list gensym default
))))
1347 (default-code-converter form
)))))
1349 (defun net-constant-converter (form generic-function
)
1350 (or (let ((c (methods-converter form generic-function
)))
1353 (default-constant-converter form
)
1356 (let* ((mp (compute-mcase-parameters (cddr form
)))
1357 (list (mapcar (lambda (clause)
1358 (let ((key (car clause
))
1359 (meth (cadr clause
)))
1360 (cons (if (consp key
) (car key
) key
)
1362 meth generic-function
))))
1364 (default (car (last list
))))
1365 (list (list* :mcase mp
(nbutlast list
))
1368 (default-constant-converter form
))))))
1370 (defun methods-converter (form generic-function
)
1371 (cond ((and (consp form
) (eq (car form
) 'methods
))
1373 (get-effective-method-function1 generic-function
(cadr form
))))
1374 ((and (consp form
) (eq (car form
) 'unordered-methods
))
1375 (default-secondary-dispatch-function generic-function
))))
1377 (defun convert-methods (constant method-alist wrappers
)
1378 (if (and (consp constant
)
1379 (eq (car constant
) '.methods.
))
1380 (funcall (cdr constant
) method-alist wrappers
)
1383 (defun convert-table (constant method-alist wrappers
)
1384 (cond ((and (consp constant
)
1385 (eq (car constant
) :mcase
))
1386 (let ((alist (mapcar (lambda (k+m
)
1388 (convert-methods (cdr k
+m
)
1392 (mp (cadr constant
)))
1399 (let ((table (make-hash-table :test
(if (car mp
) 'eq
'eql
))))
1401 (setf (gethash (car k
+m
) table
) (cdr k
+m
)))
1404 (defun compute-secondary-dispatch-function1 (generic-function net
1405 &optional function-p
)
1407 ((and (eq (car net
) 'methods
) (not function-p
))
1408 (get-effective-method-function1 generic-function
(cadr net
)))
1410 (let* ((name (generic-function-name generic-function
))
1411 (arg-info (gf-arg-info generic-function
))
1412 (metatypes (arg-info-metatypes arg-info
))
1413 (nargs (length metatypes
))
1414 (applyp (arg-info-applyp arg-info
))
1415 (fmc-arg-info (cons nargs applyp
))
1416 (arglist (if function-p
1417 (make-dfun-lambda-list nargs applyp
)
1418 (make-fast-method-call-lambda-list nargs applyp
))))
1419 (multiple-value-bind (cfunction constants
)
1420 (get-fun1 `(named-lambda (gf-dispatch ,name
)
1422 ,@(unless function-p
1423 `((declare (ignore .pv. .next-method-call.
))))
1424 (locally (declare #.
*optimize-speed
*)
1426 ,(make-emf-call nargs applyp
'emf
))))
1427 #'net-test-converter
1428 #'net-code-converter
1430 (net-constant-converter form generic-function
)))
1431 (lambda (method-alist wrappers
)
1432 (let* ((alist (list nil
))
1434 (dolist (constant constants
)
1435 (let* ((a (or (dolist (a alist nil
)
1436 (when (eq (car a
) constant
)
1440 constant method-alist wrappers
)
1442 constant method-alist wrappers
)))))
1444 (setf (cdr alist-tail
) new
)
1445 (setf alist-tail new
)))
1446 (let ((function (apply cfunction
(mapcar #'cdr
(cdr alist
)))))
1449 (make-fast-method-call
1450 :function
(set-fun-name function
`(sdfun-method ,name
))
1451 :arg-info fmc-arg-info
))))))))))
1453 (defvar *show-make-unordered-methods-emf-calls
* nil
)
1455 (defun make-unordered-methods-emf (generic-function methods
)
1456 (when *show-make-unordered-methods-emf-calls
*
1457 (format t
"~&make-unordered-methods-emf ~S~%"
1458 (generic-function-name generic-function
)))
1459 (lambda (&rest args
)
1460 (let* ((types (types-from-args generic-function args
'eql
))
1461 (smethods (sort-applicable-methods generic-function
1464 (emf (get-effective-method-function generic-function smethods
)))
1465 (invoke-emf emf args
))))
1467 ;;; The value returned by compute-discriminating-function is a function
1468 ;;; object. It is called a discriminating function because it is called
1469 ;;; when the generic function is called and its role is to discriminate
1470 ;;; on the arguments to the generic function and then call appropriate
1471 ;;; method functions.
1473 ;;; A discriminating function can only be called when it is installed as
1474 ;;; the funcallable instance function of the generic function for which
1475 ;;; it was computed.
1477 ;;; More precisely, if compute-discriminating-function is called with
1478 ;;; an argument <gf1>, and returns a result <df1>, that result must
1479 ;;; not be passed to apply or funcall directly. Rather, <df1> must be
1480 ;;; stored as the funcallable instance function of the same generic
1481 ;;; function <gf1> (using SET-FUNCALLABLE-INSTANCE-FUNCTION). Then the
1482 ;;; generic function can be passed to funcall or apply.
1484 ;;; An important exception is that methods on this generic function are
1485 ;;; permitted to return a function which itself ends up calling the value
1486 ;;; returned by a more specific method. This kind of `encapsulation' of
1487 ;;; discriminating function is critical to many uses of the MOP.
1489 ;;; As an example, the following canonical case is legal:
1491 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1492 ;;; (let ((std (call-next-method)))
1494 ;;; (print (list 'call-to-gf gf arg))
1495 ;;; (funcall std arg))))
1497 ;;; Because many discriminating functions would like to use a dynamic
1498 ;;; strategy in which the precise discriminating function changes with
1499 ;;; time it is important to specify how a discriminating function is
1500 ;;; permitted itself to change the funcallable instance function of the
1501 ;;; generic function.
1503 ;;; Discriminating functions may set the funcallable instance function
1504 ;;; of the generic function, but the new value must be generated by making
1505 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1506 ;;; more specific methods which may have encapsulated the discriminating
1507 ;;; function will get a chance to encapsulate the new, inner discriminating
1510 ;;; This implies that if a discriminating function wants to modify itself
1511 ;;; it should first store some information in the generic function proper,
1512 ;;; and then call compute-discriminating-function. The appropriate method
1513 ;;; on compute-discriminating-function will see the information stored in
1514 ;;; the generic function and generate a discriminating function accordingly.
1516 ;;; The following is an example of a discriminating function which modifies
1517 ;;; itself in accordance with this protocol:
1519 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1521 ;;; (cond (<some condition>
1522 ;;; <store some info in the generic function>
1523 ;;; (set-funcallable-instance-function
1525 ;;; (compute-discriminating-function gf))
1526 ;;; (funcall gf arg))
1528 ;;; <call-a-method-of-gf>))))
1530 ;;; Whereas this code would not be legal:
1532 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1534 ;;; (cond (<some condition>
1535 ;;; (set-funcallable-instance-function
1537 ;;; (lambda (a) ..))
1538 ;;; (funcall gf arg))
1540 ;;; <call-a-method-of-gf>))))
1542 ;;; NOTE: All the examples above assume that all instances of the class
1543 ;;; my-generic-function accept only one argument.
1545 (defun slot-value-using-class-dfun (class object slotd
)
1546 (declare (ignore class
))
1547 (funcall (slot-info-reader (slot-definition-info slotd
)) object
))
1549 (defun setf-slot-value-using-class-dfun (new-value class object slotd
)
1550 (declare (ignore class
))
1551 (funcall (slot-info-writer (slot-definition-info slotd
)) new-value object
))
1553 (defun slot-boundp-using-class-dfun (class object slotd
)
1554 (declare (ignore class
))
1555 (funcall (slot-info-boundp (slot-definition-info slotd
)) object
))
1557 (defun special-case-for-compute-discriminating-function-p (gf)
1558 (or (eq gf
#'slot-value-using-class
)
1559 (eq gf
#'(setf slot-value-using-class
))
1560 (eq gf
#'slot-boundp-using-class
)))
1562 ;;; this is the normal function for computing the discriminating
1563 ;;; function of a standard-generic-function
1564 (let (initial-print-object-cache)
1565 (defun standard-compute-discriminating-function (gf)
1566 (let ((dfun-state (slot-value gf
'dfun-state
)))
1567 (when (special-case-for-compute-discriminating-function-p gf
)
1568 ;; if we have a special case for
1569 ;; COMPUTE-DISCRIMINATING-FUNCTION, then (at least for the
1570 ;; special cases implemented as of 2006-05-09) any information
1571 ;; in the cache is misplaced.
1572 (aver (null dfun-state
)))
1573 (typecase dfun-state
1575 (when (eq gf
#'compute-applicable-methods
)
1576 (update-all-c-a-m-gf-info gf
))
1578 ((eq gf
#'slot-value-using-class
)
1579 (update-slot-value-gf-info gf
'reader
)
1580 #'slot-value-using-class-dfun
)
1581 ((eq gf
#'(setf slot-value-using-class
))
1582 (update-slot-value-gf-info gf
'writer
)
1583 #'setf-slot-value-using-class-dfun
)
1584 ((eq gf
#'slot-boundp-using-class
)
1585 (update-slot-value-gf-info gf
'boundp
)
1586 #'slot-boundp-using-class-dfun
)
1587 ;; KLUDGE: PRINT-OBJECT is not a special-case in the sense
1588 ;; of having a desperately special discriminating function.
1589 ;; However, it is important that the machinery for printing
1590 ;; conditions for stack and heap exhaustion, and the
1591 ;; restarts offered by the debugger, work without consuming
1592 ;; many extra resources. This way (testing by name of GF
1593 ;; rather than by identity) was the only way I found to get
1594 ;; this to bootstrap, given that the PRINT-OBJECT generic
1595 ;; function is only set up later, in
1596 ;; SRC;PCL;PRINT-OBJECT.LISP. -- CSR, 2008-06-09
1597 ((eq (slot-value gf
'name
) 'print-object
)
1598 (let ((nkeys (nth-value 3 (get-generic-fun-info gf
))))
1600 ;; KLUDGE: someone has defined a method
1601 ;; specialized on the second argument: punt.
1602 (setf initial-print-object-cache nil
)
1603 (make-initial-dfun gf
))
1604 (initial-print-object-cache
1605 (multiple-value-bind (dfun cache info
)
1606 (make-caching-dfun gf
(copy-cache initial-print-object-cache
))
1607 (set-dfun gf dfun cache info
)))
1608 ;; the relevant PRINT-OBJECT methods get defined
1609 ;; late, by delayed DEF!METHOD. We mustn't cache
1610 ;; the effective method for our classes earlier
1611 ;; than the relevant PRINT-OBJECT methods are
1613 ((boundp 'sb-impl
::*delayed-def
!method-args
*)
1614 (make-initial-dfun gf
))
1615 (t (multiple-value-bind (dfun cache info
)
1616 (make-final-dfun-internal
1618 (mapcar (lambda (x) (list (find-class x
)))
1619 '(sb-kernel::control-stack-exhausted
1620 sb-kernel
::binding-stack-exhausted
1621 sb-kernel
::alien-stack-exhausted
1622 sb-kernel
::heap-exhausted-error
1624 (setq initial-print-object-cache cache
)
1625 (set-dfun gf dfun
(copy-cache cache
) info
))))))
1626 ((gf-precompute-dfun-and-emf-p (slot-value gf
'arg-info
))
1627 (make-final-dfun gf
))
1629 (make-initial-dfun gf
))))
1630 (function dfun-state
)
1631 (cons (car dfun-state
))))))
1633 ;;; in general we need to support SBCL's encapsulation for generic
1634 ;;; functions: the default implementation of encapsulation changes the
1635 ;;; identity of the function bound to a name, which breaks anything
1636 ;;; class-based, so we implement the encapsulation ourselves in the
1637 ;;; discriminating function.
1638 (defun sb-impl::encapsulate-generic-function
(gf type function
)
1639 (push (cons type function
) (generic-function-encapsulations gf
))
1640 (reinitialize-instance gf
))
1642 (defun sb-impl::unencapsulate-generic-function
(gf type
)
1643 (setf (generic-function-encapsulations gf
)
1644 (remove type
(generic-function-encapsulations gf
)
1645 :key
#'car
:count
1))
1646 (reinitialize-instance gf
))
1647 (defun sb-impl::encapsulated-generic-function-p
(gf type
)
1648 (position type
(generic-function-encapsulations gf
) :key
#'car
))
1649 (defun maybe-encapsulate-discriminating-function (gf encs std
)
1652 (let ((inner (maybe-encapsulate-discriminating-function
1654 (function (cdar encs
)))
1655 (lambda (&rest args
)
1656 (apply function inner args
)))))
1657 (defmethod compute-discriminating-function ((gf standard-generic-function
))
1658 (standard-compute-discriminating-function gf
))
1659 (defmethod compute-discriminating-function :around
((gf standard-generic-function
))
1660 (maybe-encapsulate-discriminating-function
1661 gf
(generic-function-encapsulations gf
) (call-next-method)))
1663 (defmethod (setf class-name
) (new-value class
)
1664 (let ((classoid (layout-classoid (class-wrapper class
))))
1665 (if (and new-value
(symbolp new-value
))
1666 (setf (classoid-name classoid
) new-value
)
1667 (setf (classoid-name classoid
) nil
)))
1668 (reinitialize-instance class
:name new-value
)
1671 (defmethod (setf generic-function-name
) (new-value generic-function
)
1672 (reinitialize-instance generic-function
:name new-value
)
1675 (defmethod function-keywords ((method standard-method
))
1676 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p
1678 (analyze-lambda-list (if (consp method
)
1679 (early-method-lambda-list method
)
1680 (method-lambda-list method
)))
1681 (declare (ignore nreq nopt keysp restp
))
1682 (values keywords allow-other-keys-p
)))
1684 (defmethod function-keyword-parameters ((method standard-method
))
1685 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p
1686 keywords keyword-parameters
)
1687 (analyze-lambda-list (if (consp method
)
1688 (early-method-lambda-list method
)
1689 (method-lambda-list method
)))
1690 (declare (ignore nreq nopt keysp restp keywords
))
1691 (values keyword-parameters allow-other-keys-p
)))
1693 (defun method-ll->generic-function-ll
(ll)
1694 (multiple-value-bind
1695 (nreq nopt keysp restp allow-other-keys-p keywords keyword-parameters
)
1696 (analyze-lambda-list ll
)
1697 (declare (ignore nreq nopt keysp restp allow-other-keys-p keywords
))
1698 (remove-if (lambda (s)
1699 (or (memq s keyword-parameters
)
1700 (eq s
'&allow-other-keys
)))
1703 ;;; This is based on the rules of method lambda list congruency
1704 ;;; defined in the spec. The lambda list it constructs is the pretty
1705 ;;; union of the lambda lists of the generic function and of all its
1706 ;;; methods. It doesn't take method applicability into account at all
1709 ;;; (Notice that we ignore &AUX variables as they're not part of the
1710 ;;; "public interface" of a function.)
1712 (defmethod generic-function-pretty-arglist
1713 ((generic-function standard-generic-function
))
1714 (let ((gf-lambda-list (generic-function-lambda-list generic-function
))
1715 (methods (generic-function-methods generic-function
)))
1718 (multiple-value-bind (gf.required gf.optional gf.rest gf.keys gf.allowp
)
1719 (%split-arglist gf-lambda-list
)
1720 ;; Possibly extend the keyword parameters of the gf by
1721 ;; additional key parameters of its methods:
1722 (let ((methods.keys nil
) (methods.allowp nil
))
1724 (multiple-value-bind (m.keyparams m.allow-other-keys
)
1725 (function-keyword-parameters m
)
1726 (setq methods.keys
(union methods.keys m.keyparams
:key
#'maybe-car
))
1727 (setq methods.allowp
(or methods.allowp m.allow-other-keys
))))
1728 (let ((arglist '()))
1729 (when (or gf.allowp methods.allowp
)
1730 (push '&allow-other-keys arglist
))
1731 (when (or gf.keys methods.keys
)
1732 ;; We make sure that the keys of the gf appear before
1733 ;; those of its methods, since they're probably more
1734 ;; generally appliable.
1735 (setq arglist
(nconc (list '&key
) gf.keys
1736 (nset-difference methods.keys gf.keys
)
1739 (setq arglist
(nconc (list '&rest gf.rest
) arglist
)))
1741 (setq arglist
(nconc (list '&optional
) gf.optional arglist
)))
1742 (nconc gf.required arglist
)))))))
1744 (defun maybe-car (thing)
1750 (defun %split-arglist
(lambda-list)
1751 ;; This function serves to shrink the number of returned values of
1752 ;; PARSE-LAMBDA-LIST to something handier.
1753 (multiple-value-bind (required optional restp rest keyp keys allowp
1754 auxp aux morep more-context more-count
)
1755 (parse-lambda-list lambda-list
:silent t
)
1756 (declare (ignore restp keyp auxp aux morep
))
1757 (declare (ignore more-context more-count
))
1758 (values required optional rest keys allowp
)))