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
,(coerce (format nil
"~@<~A~@:>" control
)
58 :format-arguments
(list ',name
)
59 :references
'((:amop
:initialization method
))))))
60 (def reinitialize-instance
((method method
) &rest initargs
)
61 "Method objects cannot be redefined by ~S.")
62 (def change-class
((method method
) new
&rest initargs
)
63 "Method objects cannot be redefined by ~S.")
64 ;; NEW being a subclass of method is dealt with in the general
65 ;; method of CHANGE-CLASS
66 (def update-instance-for-redefined-class
((method method
) added discarded
68 "No behaviour specified for ~S on method objects.")
69 (def update-instance-for-different-class
(old (new method
) &rest initargs
)
70 "No behaviour specified for ~S on method objects.")
71 (def update-instance-for-different-class
((old method
) new
&rest initargs
)
72 "No behaviour specified for ~S on method objects."))
74 (define-condition invalid-method-initarg
(simple-program-error)
75 ((method :initarg
:method
:reader invalid-method-initarg-method
))
78 (format s
"~@<In initialization of ~S:~2I~_~?~@:>"
79 (invalid-method-initarg-method c
)
80 (simple-condition-format-control c
)
81 (simple-condition-format-arguments c
)))))
83 (defun invalid-method-initarg (method format-control
&rest args
)
84 (error 'invalid-method-initarg
:method method
85 :format-control format-control
:format-arguments args
))
87 (defun check-documentation (method doc
)
88 (unless (or (null doc
) (stringp doc
))
89 (invalid-method-initarg method
"~@<~S of ~S is neither ~S nor a ~S.~@:>"
90 :documentation doc
'null
'string
)))
91 (defun check-lambda-list (method ll
)
92 (declare (ignore method ll
))
95 (defun check-method-function (method fun
)
96 (unless (functionp fun
)
97 (invalid-method-initarg method
"~@<~S of ~S is not a ~S.~@:>"
98 :function fun
'function
)))
100 (defun check-qualifiers (method qualifiers
)
101 (flet ((improper-list ()
102 (invalid-method-initarg method
103 "~@<~S of ~S is an improper list.~@:>"
104 :qualifiers qualifiers
)))
105 (dolist-carefully (q qualifiers improper-list
)
106 (unless (and q
(atom q
))
107 (invalid-method-initarg method
108 "~@<~S, in ~S ~S, is not a non-~S atom.~@:>"
109 q
:qualifiers qualifiers
'null
)))))
111 (defun check-slot-name (method name
)
112 (declare (ignore method
))
113 (unless (symbolp name
)
114 (invalid-method-initarg "~@<~S of ~S is not a ~S.~@:>"
115 :slot-name name
'symbol
)))
117 (defun check-specializers (method specializers
)
118 (flet ((improper-list ()
119 (invalid-method-initarg method
120 "~@<~S of ~S is an improper list.~@:>"
121 :specializers specializers
)))
122 (dolist-carefully (s specializers improper-list
)
123 (unless (specializerp s
)
124 (invalid-method-initarg method
125 "~@<~S, in ~S ~S, is not a ~S.~@:>"
126 s
:specializers specializers
'specializer
)))
127 ;; KLUDGE: ANSI says that it's not valid to have methods
128 ;; specializing on classes which are "not defined", leaving
129 ;; unclear what the definedness of a class is; AMOP suggests that
130 ;; forward-referenced-classes, since they have proper names and
131 ;; all, are at least worthy of some level of definition. We allow
132 ;; methods specialized on forward-referenced-classes, but it's
133 ;; non-portable and potentially dubious, so
134 (let ((frcs (remove-if-not #'forward-referenced-class-p specializers
)))
136 (style-warn "~@<Defining a method using ~
137 ~V[~;~1{~S~}~;~1{~S and ~S~}~:;~{~#[~;and ~]~S~^, ~}~] ~
138 as ~2:*~V[~;a specializer~:;specializers~].~@:>"
139 (length frcs
) frcs
)))))
141 (defmethod shared-initialize :before
142 ((method standard-method
) slot-names
&key
143 qualifiers lambda-list specializers function documentation
)
144 (declare (ignore slot-names
))
145 ;; FIXME: it's not clear to me (CSR, 2006-08-09) why methods get
146 ;; this extra paranoia and nothing else does; either everything
147 ;; should be aggressively checking initargs, or nothing much should.
148 ;; In either case, it would probably be better to have :type
149 ;; declarations in slots, which would then give a suitable type
150 ;; error (if we implement type-checking for slots...) rather than
151 ;; this hand-crafted thing.
152 (check-qualifiers method qualifiers
)
153 (check-lambda-list method lambda-list
)
154 (check-specializers method specializers
)
155 (check-method-function method function
)
156 (check-documentation method documentation
))
158 (defmethod shared-initialize :before
159 ((method standard-accessor-method
) slot-names
&key
160 slot-name slot-definition
)
161 (declare (ignore slot-names
))
162 (unless slot-definition
163 (check-slot-name method slot-name
)))
165 (defmethod shared-initialize :after
((method standard-method
) slot-names
166 &rest initargs
&key
((method-cell method-cell
)))
167 (declare (ignore slot-names method-cell
))
168 (initialize-method-function initargs method
))
170 (define-load-time-global *the-class-generic-function
*
171 (find-class 'generic-function
))
172 (define-load-time-global *the-class-standard-generic-function
*
173 (find-class 'standard-generic-function
))
175 (defmethod shared-initialize :before
176 ((generic-function standard-generic-function
)
178 &key
(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
))
188 (flet ((initarg-error (initarg value string
)
189 (error "when initializing the generic function ~S:~%~
190 The ~S initialization argument was: ~A.~%~
192 generic-function initarg value string
)))
193 (cond (method-class-supplied-p
194 (when (symbolp method-class
)
195 (setq method-class
(find-class method-class
)))
196 (unless (and (classp method-class
)
197 (*subtypep
(class-eq-specializer method-class
)
199 (initarg-error :method-class
201 "a subclass of the class METHOD"))
202 (setf (slot-value generic-function
'method-class
) method-class
))
203 ((slot-boundp generic-function
'method-class
))
205 (initarg-error :method-class
207 "a subclass of the class METHOD")))
208 (cond (method-combination-supplied-p
209 (unless (method-combination-p method-combination
)
210 (initarg-error :method-combination
212 "a method combination object")))
213 ((slot-boundp generic-function
'%method-combination
))
215 (initarg-error :method-combination
217 "a method combination object")))))
219 (defun find-generic-function (name &optional
(errorp t
))
220 (let ((fun (and (fboundp name
) (fdefinition name
))))
222 ((and fun
(typep fun
'generic-function
)) fun
)
223 (errorp (error "No generic function named ~S." name
))
226 (defun real-add-named-method (generic-function-name qualifiers
227 specializers lambda-list
&rest other-initargs
)
228 (let* ((existing-gf (find-generic-function generic-function-name nil
))
231 (ensure-generic-function
232 generic-function-name
233 :generic-function-class
(class-of existing-gf
))
234 (ensure-generic-function generic-function-name
)))
235 (proto (method-prototype-for-gf generic-function-name
)))
236 ;; FIXME: Destructive modification of &REST list.
237 (setf (getf (getf other-initargs
'plist
) :name
)
238 (make-method-spec generic-function qualifiers specializers
))
239 (let ((new (apply #'make-instance
(class-of proto
)
240 :qualifiers qualifiers
:specializers specializers
241 :lambda-list lambda-list other-initargs
)))
242 (add-method generic-function new
)
245 (define-condition find-method-length-mismatch
246 (reference-condition simple-error
)
248 (:default-initargs
:references
'((:ansi-cl
:function find-method
))))
250 (defun real-get-method (generic-function qualifiers specializers
252 always-check-specializers
)
253 (let ((specializer-count (length specializers
))
254 (methods (generic-function-methods generic-function
)))
255 (when (or methods always-check-specializers
)
256 (let ((required-parameter-count
257 (length (arg-info-metatypes (gf-arg-info generic-function
)))))
258 ;; Since we internally bypass FIND-METHOD by using GET-METHOD
259 ;; instead we need to do this here or users may get hit by a
260 ;; failed AVER instead of a sensible error message.
261 (unless (= specializer-count required-parameter-count
)
263 'find-method-length-mismatch
264 :format-control
"~@<The generic function ~S takes ~D ~
265 required argument~:P; was asked to ~
266 find a method with specializers ~:S~@:>"
267 :format-arguments
(list generic-function required-parameter-count
268 (unparse-specializers generic-function specializers
))))))
269 (flet ((congruentp (other-method)
270 (let ((other-specializers (method-specializers other-method
)))
271 (aver (= specializer-count
(length other-specializers
)))
272 (and (equal qualifiers
(safe-method-qualifiers other-method
))
273 (every #'same-specializer-p specializers other-specializers
)))))
274 (declare (dynamic-extent #'congruentp
))
275 (cond ((find-if #'congruentp methods
))
278 (error "~@<There is no method on ~S with ~:[no ~
279 qualifiers~;~:*qualifiers ~:S~] and specializers ~
281 generic-function qualifiers specializers
))))))
283 (defmethod find-method ((generic-function standard-generic-function
)
284 qualifiers specializers
&optional
(errorp t
))
285 ;; ANSI about FIND-METHOD: "The specializers argument contains the
286 ;; parameter specializers for the method. It must correspond in
287 ;; length to the number of required arguments of the generic
288 ;; function, or an error is signaled."
290 ;; This error checking is done by REAL-GET-METHOD.
292 generic-function qualifiers
293 ;; ANSI for FIND-METHOD seems to imply that in fact specializers
294 ;; should always be passed in parsed form instead of being parsed
295 ;; at this point. Since there's no ANSI-blessed way of getting an
296 ;; EQL specializer, that seems unnecessarily painful, so we are
297 ;; nice to our users. -- CSR, 2007-06-01
298 ;; Note that INTERN-EQL-SPECIALIZER is exported from SB-MOP, but MOP isn't
299 ;; part of the ANSI standard. Parsing introduces a tiny semantic problem in
300 ;; the edge case of an EQL specializer whose object is literally (EQL :X).
301 ;; That one must be supplied as a pre-parsed #<EQL-SPECIALIZER> because if
302 ;; not, we'd parse it into a specializer whose object is :X.
303 (parse-specializers generic-function specializers
) errorp t
))
305 ;;; Compute various information about a generic-function's arglist by looking
306 ;;; at the argument lists of the methods. The hair for trying not to use
307 ;;; &REST arguments lives here.
308 ;;; The values returned are:
309 ;;; number-of-required-arguments
310 ;;; the number of required arguments to this generic-function's
311 ;;; discriminating function
313 ;;; whether or not this generic-function's discriminating
314 ;;; function takes an &rest argument.
315 ;;; specialized-argument-positions
316 ;;; a list of the positions of the arguments this generic-function
317 ;;; specializes (e.g. for a classical generic-function this is the
319 (defmethod compute-discriminating-function-arglist-info
320 ((generic-function standard-generic-function
))
321 ;;(declare (values number-of-required-arguments &rest-argument-p
322 ;; specialized-argument-postions))
323 (let ((number-required nil
)
325 (specialized-positions ())
326 (methods (generic-function-methods generic-function
)))
327 (dolist (method methods
)
328 (multiple-value-setq (number-required restp specialized-positions
)
329 (compute-discriminating-function-arglist-info-internal
330 generic-function method number-required restp specialized-positions
)))
331 (values number-required restp
(sort specialized-positions
#'<))))
333 (defun compute-discriminating-function-arglist-info-internal
334 (generic-function method number-of-requireds restp
335 specialized-argument-positions
)
336 (declare (ignore generic-function
)
337 (type (or null fixnum
) number-of-requireds
))
339 (declare (fixnum requireds
))
340 ;; Go through this methods arguments seeing how many are required,
341 ;; and whether there is an &rest argument.
342 (dolist (arg (method-lambda-list method
))
343 (cond ((eq arg
'&aux
) (return))
344 ((memq arg
'(&optional
&rest
&key
))
345 (return (setq restp t
)))
346 ((memq arg lambda-list-keywords
))
347 (t (incf requireds
))))
348 ;; Now go through this method's type specifiers to see which
349 ;; argument positions are type specified. Treat T specially
350 ;; in the usual sort of way. For efficiency don't bother to
351 ;; keep specialized-argument-positions sorted, rather depend
352 ;; on our caller to do that.
354 (dolist (type-spec (method-specializers method
))
355 (unless (eq type-spec
*the-class-t
*)
356 (pushnew pos specialized-argument-positions
:test
#'eq
))
358 ;; Finally merge the values for this method into the values
359 ;; for the exisiting methods and return them. Note that if
360 ;; num-of-requireds is NIL it means this is the first method
361 ;; and we depend on that.
362 (values (min (or number-of-requireds requireds
) requireds
)
364 (and number-of-requireds
(/= number-of-requireds requireds
)))
365 specialized-argument-positions
)))
367 (defun make-discriminating-function-arglist (number-required-arguments restp
)
368 (nconc (let ((args nil
))
369 (dotimes (i number-required-arguments
)
370 (push (format-symbol *package
* ;; ! is this right?
371 "Discriminating Function Arg ~D"
376 `(&rest
,(format-symbol *package
*
377 "Discriminating Function &rest Arg")))))
379 (defmethod generic-function-argument-precedence-order
380 ((gf standard-generic-function
))
381 (aver (eq **boot-state
** 'complete
))
382 (loop with arg-info
= (gf-arg-info gf
)
383 with lambda-list
= (arg-info-lambda-list arg-info
)
384 for argument-position in
(arg-info-precedence arg-info
)
385 collect
(nth argument-position lambda-list
)))
387 (defmethod generic-function-lambda-list ((gf generic-function
))
390 (defmethod gf-fast-method-function-p ((gf standard-generic-function
))
391 (gf-info-fast-mf-p (slot-value gf
'arg-info
)))
393 (defmethod initialize-instance :after
((gf standard-generic-function
)
394 &key
(lambda-list nil lambda-list-p
)
395 argument-precedence-order
)
396 ;; FIXME: Because ARG-INFO is a STRUCTURE-OBJECT, it does not get
397 ;; a permutation vector, and therefore the code that SLOT-VALUE transforms
398 ;; to winds up punting to #'(SLOT-ACCESSOR :GLOBAL ARG-INFO READER).
399 ;; Using SLOT-VALUE the "slow" way sidesteps some bootstrap issues.
400 (declare (notinline slot-value
))
401 (progn ; WAS: with-slots (arg-info) gf
404 :lambda-list lambda-list
405 :argument-precedence-order argument-precedence-order
)
407 (when (arg-info-valid-p (slot-value gf
'arg-info
))
410 (defmethod reinitialize-instance :around
411 ((gf standard-generic-function
) &rest args
&key
412 (lambda-list nil lambda-list-p
) (argument-precedence-order nil apo-p
))
413 (let ((old-mc (generic-function-method-combination gf
)))
414 (prog1 (call-next-method)
415 ;; KLUDGE: EQ is too strong a test.
416 (unless (eq old-mc
(generic-function-method-combination gf
))
417 (flush-effective-method-cache gf
))
419 ((and lambda-list-p apo-p
)
421 :lambda-list lambda-list
422 :argument-precedence-order argument-precedence-order
))
423 (lambda-list-p (set-arg-info gf
:lambda-list lambda-list
))
424 (t (set-arg-info gf
)))
425 (when (arg-info-valid-p (gf-arg-info gf
))
427 (map-dependents gf
(lambda (dependent)
428 (apply #'update-dependent gf dependent args
))))))
430 (declaim (special *lazy-dfun-compute-p
*))
432 (defun set-methods (gf methods
)
433 (setf (generic-function-methods gf
) nil
)
434 (loop (when (null methods
) (return gf
))
435 (real-add-method gf
(pop methods
) methods
)))
437 (define-condition new-value-specialization
(reference-condition error
)
438 ((%method
:initarg
:method
:reader new-value-specialization-method
))
441 (format s
"~@<Cannot add method ~S to ~S, as it specializes the ~
442 new-value argument.~@:>"
443 (new-value-specialization-method c
)
444 #'(setf slot-value-using-class
))))
445 (:default-initargs
:references
446 (list '(:sbcl
:node
"Metaobject Protocol")
447 '(:amop
:generic-function
(setf slot-value-using-class
)))))
449 (defgeneric values-for-add-method
(gf method
)
450 (:method
((gf standard-generic-function
) (method standard-method
))
451 ;; KLUDGE: Just a single generic dispatch, and everything else
452 ;; comes from permutation vectors. Would be nicer to define
453 ;; REAL-ADD-METHOD with a proper method so that we could efficiently
454 ;; use SLOT-VALUE there.
456 ;; Optimization note: REAL-ADD-METHOD has a lot of O(N) stuff in it (as
457 ;; does PCL as a whole). It should not be too hard to internally store
458 ;; many of the things we now keep in lists as either purely functional
459 ;; O(log N) sets, or --if we don't mind the memory cost-- using
460 ;; specialized hash-tables: most things are used to answer questions about
461 ;; set-membership, not ordering.
462 (values (slot-value gf
'%lock
)
463 (slot-value method
'qualifiers
)
464 (slot-value method
'specializers
)
465 (slot-value method
'lambda-list
)
466 (slot-value method
'%generic-function
)
467 (slot-value gf
'name
))))
469 (define-condition print-object-stream-specializer
(reference-condition simple-warning
)
472 :references
'((:ansi-cl
:function print-object
))
473 :format-control
"~@<Specializing on the second argument to ~S has ~
474 unportable effects, and also interferes with ~
475 precomputation of print functions for exceptional ~
477 :format-arguments
(list 'print-object
)))
479 (defun defer-ftype-computation (gf)
480 ;; Is there any reason not to do this as soon as possible?
481 ;; While doing it with every ADD/REMOVE-METHOD call could result in
482 ;; wasted work, it seems like unnecessary complexity.
483 ;; I think it's just to get through bootstrap, probably,
484 ;; but if it's a semantics thing, it deserves some explanation.
485 (let ((name (generic-function-name gf
)))
486 (when (legal-fun-name-p name
) ; tautological ?
487 (unless (eq (info :function
:where-from name
) :declared
)
488 (when (and (fboundp name
) (eq (fdefinition name
) gf
))
489 (setf (info :function
:type name
) :generic-function
))))))
491 (defun compute-gf-ftype (name)
492 (let ((gf (and (fboundp name
) (fdefinition name
))))
493 (if (generic-function-p gf
)
494 (let* ((ll (generic-function-lambda-list gf
))
495 ;; If the GF has &REST without &KEY then we don't augment
496 ;; the FTYPE with keywords, so as not to complain about keywords
497 ;; which seem not to be accepted.
498 (type (sb-c::ftype-from-lambda-list
499 (if (and (member '&rest ll
) (not (member '&key ll
)))
501 (generic-function-pretty-arglist gf
)))))
502 ;; It would be nice if globaldb were transactional,
503 ;; so that either both updates or neither occur.
504 (setf (info :function
:type name
) type
505 (info :function
:where-from name
) :defined-method
)
507 ;; The defaulting expression for (:FUNCTION :TYPE) does not store
508 ;; the default. For :GENERIC-FUNCTION that is not FBOUNDP we also
509 ;; don't, however this branch should never be reached because the
510 ;; info only stores :GENERIC-FUNCTION when methods are loaded.
511 ;; Maybe AVER that it does not happen?
512 (sb-c::ftype-from-fdefn name
))))
514 (defun real-add-method (generic-function method
&optional skip-dfun-update-p
)
515 (flet ((similar-lambda-lists-p (old-method new-lambda-list
)
516 (binding* (((a-llks a-nreq a-nopt
)
517 (analyze-lambda-list (method-lambda-list old-method
)))
518 ((b-llks b-nreq b-nopt
)
519 (analyze-lambda-list new-lambda-list
)))
520 (and (= a-nreq b-nreq
)
522 (eq (ll-keyp-or-restp a-llks
)
523 (ll-keyp-or-restp b-llks
))))))
524 (multiple-value-bind (lock qualifiers specializers new-lambda-list
526 (values-for-add-method generic-function method
)
528 (error "~@<The method ~S is already part of the generic ~
529 function ~S; it can't be added to another generic ~
530 function until it is removed from the first one.~@:>"
532 (when (and (eq name
'print-object
) (not (eq (second specializers
) *the-class-t
*)))
533 (warn 'print-object-stream-specializer
))
535 ;; System lock because interrupts need to be disabled as
536 ;; well: it would be bad to unwind and leave the gf in an
537 ;; inconsistent state.
538 (sb-thread::with-recursive-system-lock
(lock)
539 (let ((existing (get-method generic-function
544 ;; If there is already a method like this one then we must get
545 ;; rid of it before proceeding. Note that we call the generic
546 ;; function REMOVE-METHOD to remove it rather than doing it in
547 ;; some internal way.
548 (when (and existing
(similar-lambda-lists-p existing new-lambda-list
))
549 (remove-method generic-function existing
))
551 ;; KLUDGE: We have a special case here, as we disallow
552 ;; specializations of the NEW-VALUE argument to (SETF
553 ;; SLOT-VALUE-USING-CLASS). GET-ACCESSOR-METHOD-FUNCTION is
554 ;; the optimizing function here: it precomputes the effective
555 ;; method, assuming that there is no dispatch to be done on
556 ;; the new-value argument.
557 (when (and (eq generic-function
#'(setf slot-value-using-class
))
558 (not (eq *the-class-t
* (first specializers
))))
559 (error 'new-value-specialization
:method method
))
561 (setf (method-generic-function method
) generic-function
)
562 (pushnew method
(generic-function-methods generic-function
) :test
#'eq
)
563 (dolist (specializer specializers
)
564 (add-direct-method specializer method
))
566 ;; KLUDGE: SET-ARG-INFO contains the error-detecting logic for
567 ;; detecting attempts to add methods with incongruent lambda
568 ;; lists. However, according to Gerd Moellmann on cmucl-imp,
569 ;; it also depends on the new method already having been added
570 ;; to the generic function. Therefore, we need to remove it
572 (let ((remove-again-p t
))
575 (set-arg-info generic-function
:new-method method
)
576 (setq remove-again-p nil
))
578 (remove-method generic-function method
))))
580 ;; KLUDGE II: ANSI saith that it is not an error to add a
581 ;; method with invalid qualifiers to a generic function of the
582 ;; wrong kind; it's only an error at generic function
583 ;; invocation time; I dunno what the rationale was, and it
584 ;; sucks. Nevertheless, it's probably a programmer error, so
585 ;; let's warn anyway. -- CSR, 2003-08-20
586 (let* ((mc (generic-function-method-combination generic-function
))
587 (type-name (method-combination-type-name mc
)))
589 (warn "~@<Invalid qualifiers for ~S method ~
590 combination in method ~S:~2I~_~S.~@:>"
591 type-name method qualifiers
)))
593 ((and (eq mc
*standard-method-combination
*)
596 (not (standard-method-combination-qualifier-p
599 ((and (short-method-combination-p mc
)
600 (or (null qualifiers
)
602 (not (short-method-combination-qualifier-p
603 type-name
(car qualifiers
)))))
605 (unless skip-dfun-update-p
606 (update-ctors 'add-method
607 :generic-function generic-function
609 (update-dfun generic-function
))
610 (defer-ftype-computation generic-function
)
611 (map-dependents generic-function
613 (update-dependent generic-function
614 dep
'add-method method
)))))
615 (serious-condition (c)
619 (defun real-remove-method (generic-function method
)
620 (when (eq generic-function
(method-generic-function method
))
621 (flush-effective-method-cache generic-function
)
622 (let ((lock (gf-lock generic-function
)))
623 ;; System lock because interrupts need to be disabled as well:
624 ;; it would be bad to unwind and leave the gf in an inconsistent
626 (sb-thread::with-recursive-system-lock
(lock)
627 (let* ((specializers (method-specializers method
))
628 (methods (generic-function-methods generic-function
))
629 (new-methods (remove method methods
)))
630 (setf (method-generic-function method
) nil
631 (generic-function-methods generic-function
) new-methods
)
632 (dolist (specializer specializers
)
633 (remove-direct-method specializer method
))
634 (set-arg-info generic-function
)
635 (update-ctors 'remove-method
636 :generic-function generic-function
638 (update-dfun generic-function
)
639 (defer-ftype-computation generic-function
)
640 (map-dependents generic-function
642 (update-dependent generic-function
643 dep
'remove-method method
)))))))
646 (defun compute-applicable-methods-function (generic-function arguments
)
647 (values (compute-applicable-methods-using-types
649 (types-from-args generic-function arguments
'eql
))))
651 (defmethod compute-applicable-methods
652 ((generic-function generic-function
) arguments
)
653 (values (compute-applicable-methods-using-types
655 (types-from-args generic-function arguments
'eql
))))
657 (defmethod compute-applicable-methods-using-classes
658 ((generic-function generic-function
) classes
)
659 (compute-applicable-methods-using-types
661 (types-from-args generic-function classes
'class-eq
)))
663 (defun !proclaim-incompatible-superclasses
(classes)
664 (setq classes
(mapcar (lambda (class)
669 (dolist (class classes
)
670 (dolist (other-class classes
)
671 (unless (eq class other-class
)
672 (pushnew other-class
(class-incompatible-superclass-list class
) :test
#'eq
)))))
674 (defun superclasses-compatible-p (class1 class2
)
675 (let ((cpl1 (cpl-or-nil class1
))
676 (cpl2 (cpl-or-nil class2
)))
678 (dolist (ic (class-incompatible-superclass-list sc1
))
680 (return-from superclasses-compatible-p nil
))))))
683 #'!proclaim-incompatible-superclasses
684 '(;; superclass class
685 (system-class std-class structure-class
) ; direct subclasses of pcl-class
686 (standard-class funcallable-standard-class
)
687 ;; superclass metaobject
688 (class eql-specializer class-eq-specializer method method-combination
689 generic-function slot-definition
)
690 ;; metaclass built-in-class
691 (number sequence character
; direct subclasses of t, but not array
692 standard-object structure-object
) ; or symbol
693 (number array character symbol
; direct subclasses of t, but not
694 standard-object structure-object
) ; sequence
695 (complex float rational
) ; direct subclasses of number
696 (integer ratio
) ; direct subclasses of rational
697 (list vector
) ; direct subclasses of sequence
698 (cons null
) ; direct subclasses of list
699 (string bit-vector
) ; direct subclasses of vector
702 (defmethod same-specializer-p ((specl1 specializer
) (specl2 specializer
))
705 (defmethod same-specializer-p ((specl1 class
) (specl2 class
))
708 (defmethod specializer-class ((specializer class
))
711 (defmethod same-specializer-p ((specl1 class-eq-specializer
)
712 (specl2 class-eq-specializer
))
713 (eq (specializer-class specl1
) (specializer-class specl2
)))
715 ;; FIXME: This method is wacky, and indicative of a coding style in which
716 ;; metaphorically the left hand does not know what the right is doing.
717 ;; If you want this to be the abstract comparator, and you "don't know"
718 ;; that EQL-specializers are interned, then the comparator should be EQL.
719 ;; But if you *do* know that they're interned, then why does this method
720 ;; exist at all? The method on SPECIALIZER works fine.
721 (defmethod same-specializer-p ((specl1 eql-specializer
)
722 (specl2 eql-specializer
))
723 ;; A bit of deception to confuse the enemy?
724 (eq (specializer-object specl1
) (specializer-object specl2
)))
726 (defmethod specializer-class ((specializer eql-specializer
))
727 (class-of (slot-value specializer
'object
)))
729 (defun specializer-class-or-nil (specializer)
730 (and (standard-specializer-p specializer
)
731 (specializer-class specializer
)))
733 (defun error-need-at-least-n-args (function n
)
734 (error 'simple-program-error
735 :format-control
"~@<The function ~2I~_~S ~I~_requires ~
736 at least ~W argument~:P.~:>"
737 :format-arguments
(list function n
)))
739 (defun types-from-args (generic-function arguments
&optional type-modifier
)
740 (multiple-value-bind (nreq applyp metatypes nkeys arg-info
)
741 (get-generic-fun-info generic-function
)
742 (declare (ignore applyp metatypes nkeys
))
743 (let ((types-rev nil
))
744 (dotimes-fixnum (i nreq
)
746 (error-need-at-least-n-args (generic-function-name generic-function
)
748 (let ((arg (pop arguments
)))
749 (push (if type-modifier
`(,type-modifier
,arg
) arg
) types-rev
)))
750 (values (nreverse types-rev
) arg-info
))))
752 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes
)
753 (let* ((w wrappers
) (w-tail w
) (mt-tail metatypes
))
754 (dolist (class (ensure-list classes
))
755 (unless (eq t
(car mt-tail
))
756 (let ((c-w (class-wrapper class
)))
757 (unless c-w
(return-from get-wrappers-from-classes nil
))
760 (setf (car w-tail
) c-w
761 w-tail
(cdr w-tail
)))))
762 (setq mt-tail
(cdr mt-tail
)))
765 (defun sdfun-for-caching (gf classes
)
766 (let ((types (mapcar #'class-eq-type classes
)))
767 (multiple-value-bind (methods all-applicable-and-sorted-p
)
768 (compute-applicable-methods-using-types gf types
)
769 (let ((generator (get-secondary-dispatch-function1
770 gf methods types nil t all-applicable-and-sorted-p
)))
771 (make-callable gf methods generator
772 nil
(mapcar #'class-wrapper classes
))))))
774 (defun value-for-caching (gf classes
)
775 (let ((methods (compute-applicable-methods-using-types
776 gf
(mapcar #'class-eq-type classes
))))
777 (method-plist-value (car methods
) :constant-value
)))
779 (defun default-secondary-dispatch-function (generic-function)
781 (let ((methods (compute-applicable-methods generic-function args
)))
783 (let ((emf (get-effective-method-function generic-function
785 (invoke-emf emf args
))
786 (call-no-applicable-method generic-function args
)))))
789 (loop (when (atom x
) (return (eq x y
)))
790 (when (atom y
) (return nil
))
791 (unless (eq (car x
) (car y
)) (return nil
))
795 (define-load-time-global *std-cam-methods
* nil
)
797 (defun compute-applicable-methods-emf (generic-function)
798 (if (eq **boot-state
** 'complete
)
799 (let* ((cam (gdefinition 'compute-applicable-methods
))
800 (cam-methods (compute-applicable-methods-using-types
801 cam
(list `(eql ,generic-function
) t
))))
802 (values (get-effective-method-function cam cam-methods
)
804 (or *std-cam-methods
*
805 (setq *std-cam-methods
*
806 (compute-applicable-methods-using-types
807 cam
(list `(eql ,cam
) t
)))))))
808 (values #'compute-applicable-methods-function t
)))
810 (defun compute-applicable-methods-emf-std-p (gf)
811 (gf-info-c-a-m-emf-std-p (gf-arg-info gf
)))
813 (defvar *old-c-a-m-gf-methods
* nil
)
815 (defun update-all-c-a-m-gf-info (c-a-m-gf)
816 (let ((methods (generic-function-methods c-a-m-gf
)))
817 (if (and *old-c-a-m-gf-methods
*
818 (every (lambda (old-method)
819 (member old-method methods
:test
#'eq
))
820 *old-c-a-m-gf-methods
*))
821 (let ((gfs-to-do nil
)
822 (gf-classes-to-do nil
))
823 (dolist (method methods
)
824 (unless (member method
*old-c-a-m-gf-methods
* :test
#'eq
)
825 (let ((specl (car (method-specializers method
))))
826 (if (eql-specializer-p specl
)
827 (pushnew (specializer-object specl
) gfs-to-do
:test
#'eq
)
828 (pushnew (specializer-class specl
) gf-classes-to-do
:test
#'eq
)))))
829 (map-all-generic-functions
831 (when (or (member gf gfs-to-do
:test
#'eq
)
832 (dolist (class gf-classes-to-do nil
)
834 (class-precedence-list (class-of gf
))
836 (update-c-a-m-gf-info gf
)))))
837 (map-all-generic-functions #'update-c-a-m-gf-info
))
838 (setq *old-c-a-m-gf-methods
* methods
)))
840 (defun update-gf-info (gf)
841 (update-c-a-m-gf-info gf
)
842 (update-gf-simple-accessor-type gf
))
844 (defun update-c-a-m-gf-info (gf)
845 (unless (early-gf-p gf
)
846 (multiple-value-bind (c-a-m-emf std-p
)
847 (compute-applicable-methods-emf gf
)
848 (let ((arg-info (gf-arg-info gf
)))
849 (setf (gf-info-static-c-a-m-emf arg-info
) c-a-m-emf
)
850 (setf (gf-info-c-a-m-emf-std-p arg-info
) std-p
)))))
852 (defun update-gf-simple-accessor-type (gf)
853 (let ((arg-info (gf-arg-info gf
)))
854 (setf (gf-info-simple-accessor-type arg-info
)
855 (let* ((methods (generic-function-methods gf
))
856 (class (and methods
(class-of (car methods
))))
859 (cond ((or (eq class
*the-class-standard-reader-method
*)
860 (eq class
*the-class-global-reader-method
*))
862 ((or (eq class
*the-class-standard-writer-method
*)
863 (eq class
*the-class-global-writer-method
*))
865 ((or (eq class
*the-class-standard-boundp-method
*)
866 (eq class
*the-class-global-boundp-method
*))
868 (when (and (gf-info-c-a-m-emf-std-p arg-info
)
870 (dolist (method (cdr methods
) t
)
871 (unless (eq class
(class-of method
)) (return nil
)))
872 (eq (generic-function-method-combination gf
)
873 *standard-method-combination
*))
877 ;;; CMUCL (Gerd's PCL, 2002-04-25) comment:
879 ;;; Return two values. First value is a function to be stored in
880 ;;; effective slot definition SLOTD for reading it with
881 ;;; SLOT-VALUE-USING-CLASS, setting it with (SETF
882 ;;; SLOT-VALUE-USING-CLASS) or testing it with
883 ;;; SLOT-BOUNDP-USING-CLASS. GF is one of these generic functions,
884 ;;; TYPE is one of the symbols READER, WRITER, BOUNDP. CLASS is
887 ;;; Second value is true if the function returned is one of the
888 ;;; optimized standard functions for the purpose, which are used
889 ;;; when only standard methods are applicable.
891 ;;; FIXME: Change all these wacky function names to something sane.
892 (defun get-accessor-method-function (gf type class slotd
)
893 (let* ((std-method (standard-svuc-method type
))
894 (str-method (structure-svuc-method type
))
895 (types1 `((eql ,class
) (class-eq ,class
) (eql ,slotd
)))
896 (types (if (eq type
'writer
) `(t ,@types1
) types1
))
897 (methods (compute-applicable-methods-using-types gf types
))
898 (std-p (null (cdr methods
))))
901 (get-optimized-std-accessor-method-function class slotd type
)
902 (let* ((optimized-std-fun
903 (get-optimized-std-slot-value-using-class-method-function
906 `((,(car (or (member std-method methods
:test
#'eq
)
907 (member str-method methods
:test
#'eq
)
909 'get-accessor-method-function
)))
910 ,optimized-std-fun
)))
912 (let ((wrappers (list (layout-of class
)
913 (class-wrapper class
)
915 (if (eq type
'writer
)
916 (cons (class-wrapper *the-class-t
*) wrappers
)
918 (sdfun (get-secondary-dispatch-function
919 gf methods types method-alist wrappers
)))
920 (get-accessor-from-svuc-method-function class slotd sdfun type
)))
923 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
924 (defun update-slot-value-gf-info (gf type
)
926 (update-std-or-str-methods gf type
))
927 (when (and (standard-svuc-method type
) (structure-svuc-method type
))
928 (flet ((update-accessor-info (class)
929 (when (class-finalized-p class
)
930 (dolist (slotd (class-slots class
))
931 (compute-slot-accessor-info slotd type gf
)))))
933 (update-accessor-info *new-class
*)
934 (map-all-classes #'update-accessor-info
'slot-object
)))))
936 (define-load-time-global *standard-slot-value-using-class-method
* nil
)
937 (define-load-time-global *standard-setf-slot-value-using-class-method
* nil
)
938 (define-load-time-global *standard-slot-boundp-using-class-method
* nil
)
939 (define-load-time-global *condition-slot-value-using-class-method
* nil
)
940 (define-load-time-global *condition-setf-slot-value-using-class-method
* nil
)
941 (define-load-time-global *condition-slot-boundp-using-class-method
* nil
)
942 (define-load-time-global *structure-slot-value-using-class-method
* nil
)
943 (define-load-time-global *structure-setf-slot-value-using-class-method
* nil
)
944 (define-load-time-global *structure-slot-boundp-using-class-method
* nil
)
946 (defun standard-svuc-method (type)
948 (reader *standard-slot-value-using-class-method
*)
949 (writer *standard-setf-slot-value-using-class-method
*)
950 (boundp *standard-slot-boundp-using-class-method
*)))
952 (defun set-standard-svuc-method (type method
)
954 (reader (setq *standard-slot-value-using-class-method
* method
))
955 (writer (setq *standard-setf-slot-value-using-class-method
* method
))
956 (boundp (setq *standard-slot-boundp-using-class-method
* method
))))
958 (defun condition-svuc-method (type)
960 (reader *condition-slot-value-using-class-method
*)
961 (writer *condition-setf-slot-value-using-class-method
*)
962 (boundp *condition-slot-boundp-using-class-method
*)))
964 (defun set-condition-svuc-method (type method
)
966 (reader (setq *condition-slot-value-using-class-method
* method
))
967 (writer (setq *condition-setf-slot-value-using-class-method
* method
))
968 (boundp (setq *condition-slot-boundp-using-class-method
* method
))))
970 (defun structure-svuc-method (type)
972 (reader *structure-slot-value-using-class-method
*)
973 (writer *structure-setf-slot-value-using-class-method
*)
974 (boundp *structure-slot-boundp-using-class-method
*)))
976 (defun set-structure-svuc-method (type method
)
978 (reader (setq *structure-slot-value-using-class-method
* method
))
979 (writer (setq *structure-setf-slot-value-using-class-method
* method
))
980 (boundp (setq *structure-slot-boundp-using-class-method
* method
))))
982 (defun update-std-or-str-methods (gf type
)
983 (dolist (method (generic-function-methods gf
))
984 (let ((specls (method-specializers method
)))
985 (when (and (or (not (eq type
'writer
))
986 (eq (pop specls
) *the-class-t
*))
987 (every #'classp specls
))
988 (cond ((and (eq (class-name (car specls
)) 'std-class
)
989 (eq (class-name (cadr specls
)) 'standard-object
)
990 (eq (class-name (caddr specls
))
991 'standard-effective-slot-definition
))
992 (set-standard-svuc-method type method
))
993 ((and (eq (class-name (car specls
)) 'condition-class
)
994 (eq (class-name (cadr specls
)) 'condition
)
995 (eq (class-name (caddr specls
))
996 'condition-effective-slot-definition
))
997 (set-condition-svuc-method type method
))
998 ((and (eq (class-name (car specls
)) 'structure-class
)
999 (eq (class-name (cadr specls
)) 'structure-object
)
1000 (eq (class-name (caddr specls
))
1001 'structure-effective-slot-definition
))
1002 (set-structure-svuc-method type method
)))))))
1004 (defun mec-all-classes-internal (spec precompute-p
)
1005 (let ((wrapper (class-wrapper (specializer-class spec
))))
1006 (unless (or (not wrapper
) (invalid-wrapper-p wrapper
))
1007 (cons (specializer-class spec
)
1010 (not (or (eq spec
*the-class-t
*)
1011 (eq spec
*the-class-slot-object
*)
1012 (eq spec
*the-class-standard-object
*)
1013 (eq spec
*the-class-structure-object
*)))
1014 (let ((sc (class-direct-subclasses spec
)))
1016 (mapcan (lambda (class)
1017 (mec-all-classes-internal class precompute-p
))
1020 (defun mec-all-classes (spec precompute-p
)
1021 (let ((classes (mec-all-classes-internal spec precompute-p
)))
1022 (if (null (cdr classes
))
1024 (let* ((a-classes (cons nil classes
))
1026 (loop (when (null (cdr tail
))
1027 (return (cdr a-classes
)))
1028 (let ((class (cadr tail
))
1029 (ttail (cddr tail
)))
1030 (if (dolist (c ttail nil
)
1031 (when (eq class c
) (return t
)))
1032 (setf (cdr tail
) (cddr tail
))
1033 (setf tail
(cdr tail
)))))))))
1035 (defun mec-all-class-lists (spec-list precompute-p
)
1036 (if (null spec-list
)
1038 (let* ((car-all-classes (mec-all-classes (car spec-list
)
1040 (all-class-lists (mec-all-class-lists (cdr spec-list
)
1042 (mapcan (lambda (list)
1043 (mapcar (lambda (c) (cons c list
)) car-all-classes
))
1046 (defun make-emf-cache (generic-function valuep cache classes-list new-class
)
1047 (let* ((arg-info (gf-arg-info generic-function
))
1048 (nkeys (arg-info-nkeys arg-info
))
1049 (metatypes (arg-info-metatypes arg-info
))
1050 (wrappers (unless (eq nkeys
1) (make-list nkeys
)))
1051 (precompute-p (gf-precompute-dfun-and-emf-p arg-info
)))
1052 (flet ((add-class-list (classes)
1053 (when (or (null new-class
) (memq new-class classes
))
1054 (let ((%wrappers
(get-wrappers-from-classes
1055 nkeys wrappers classes metatypes
)))
1056 (when (and %wrappers
(not (probe-cache cache %wrappers
)))
1057 (let ((value (cond ((eq valuep t
)
1058 (sdfun-for-caching generic-function
1060 ((eq valuep
:constant-value
)
1061 (value-for-caching generic-function
1063 ;; need to get them again, as finalization might
1064 ;; have happened in between, which would
1065 ;; invalidate wrappers.
1066 (let ((wrappers (get-wrappers-from-classes
1067 nkeys wrappers classes metatypes
)))
1068 (when (if (atom wrappers
)
1069 (not (invalid-wrapper-p wrappers
))
1070 (every (complement #'invalid-wrapper-p
)
1072 (setq cache
(fill-cache cache wrappers value
))))))))))
1074 (mapc #'add-class-list classes-list
)
1075 (dolist (method (generic-function-methods generic-function
))
1076 (mapc #'add-class-list
1077 (mec-all-class-lists (method-specializers method
)
1081 (defmacro class-test
(arg class
)
1083 ((eq class
*the-class-t
*) t
)
1084 ((eq class
*the-class-slot-object
*)
1085 `(not (typep (classoid-of ,arg
) 'system-classoid
)))
1086 ((eq class
*the-class-standard-object
*)
1087 `(or (std-instance-p ,arg
) (fsc-instance-p ,arg
)))
1088 ((eq class
*the-class-funcallable-standard-object
*)
1089 `(fsc-instance-p ,arg
))
1091 `(typep ,arg
',(class-name class
)))))
1093 (defmacro class-eq-test
(arg class
)
1094 `(eq (class-of ,arg
) ',class
))
1096 (defmacro eql-test
(arg object
)
1097 `(eql ,arg
',object
))
1099 (defun dnet-methods-p (form)
1101 (or (eq (car form
) 'methods
)
1102 (eq (car form
) 'unordered-methods
))))
1104 ;;; This is CASE, but without gensyms.
1105 (defmacro scase
(arg &rest clauses
)
1106 `(let ((.case-arg.
,arg
))
1107 (cond ,@(mapcar (lambda (clause)
1108 (list* (cond ((null (car clause
))
1110 ((consp (car clause
))
1111 (if (null (cdar clause
))
1116 ((member (car clause
) '(t otherwise
))
1119 `(eql .case-arg.
',(car clause
))))
1124 (defmacro mcase
(arg &rest clauses
) `(scase ,arg
,@clauses
))
1126 (defun generate-discrimination-net (generic-function methods types sorted-p
)
1127 (let* ((arg-info (gf-arg-info generic-function
))
1128 (c-a-m-emf-std-p (gf-info-c-a-m-emf-std-p arg-info
))
1129 (precedence (arg-info-precedence arg-info
)))
1130 (generate-discrimination-net-internal
1131 generic-function methods types
1132 (lambda (methods known-types
)
1134 (and c-a-m-emf-std-p
1136 (let ((sorted-methods nil
))
1138 (copy-list methods
) precedence
1140 (when sorted-methods
(return-from one-order-p nil
))
1141 (setq sorted-methods methods
)))
1142 (setq methods sorted-methods
))
1144 `(methods ,methods
,known-types
)
1145 `(unordered-methods ,methods
,known-types
)))
1146 (lambda (position type true-value false-value
)
1147 (let ((arg (dfun-arg-symbol position
)))
1148 (if (eq (car type
) 'eql
)
1149 (let* ((false-case-p (and (consp false-value
)
1150 (or (eq (car false-value
) 'scase
)
1151 (eq (car false-value
) 'mcase
))
1152 (eq arg
(cadr false-value
))))
1153 (false-clauses (if false-case-p
1155 `((t ,false-value
))))
1156 (case-sym (if (and (dnet-methods-p true-value
)
1158 (eq (car false-value
) 'mcase
)
1159 (dnet-methods-p false-value
)))
1162 (type-sym `(,(cadr type
))))
1164 (,type-sym
,true-value
)
1166 `(if ,(let ((arg (dfun-arg-symbol position
)))
1168 (class `(class-test ,arg
,(cadr type
)))
1169 (class-eq `(class-eq-test ,arg
,(cadr type
)))))
1174 (defun class-from-type (type)
1175 (if (or (atom type
) (eq (car type
) t
))
1178 (and (dolist (type (cdr type
) *the-class-t
*)
1179 (when (and (consp type
) (not (eq (car type
) 'not
)))
1180 (return (class-from-type type
)))))
1182 (eql (class-of (cadr type
)))
1183 (class-eq (cadr type
))
1184 (class (cadr type
)))))
1186 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1187 (defun augment-type (new-type known-type
)
1188 (if (or (eq known-type t
)
1189 (eq (car new-type
) 'eql
))
1191 (let ((so-far (if (and (consp known-type
) (eq (car known-type
) 'and
))
1193 (list known-type
))))
1194 (unless (eq (car new-type
) 'not
)
1196 (mapcan (lambda (type)
1197 (unless (*subtypep new-type type
)
1202 `(and ,new-type
,@so-far
)))))
1204 (defun generate-discrimination-net-internal
1205 (gf methods types methods-function test-fun type-function
)
1206 (let* ((arg-info (gf-arg-info gf
))
1207 (precedence (arg-info-precedence arg-info
))
1208 (nreq (arg-info-number-required arg-info
))
1209 (metatypes (arg-info-metatypes arg-info
)))
1210 (labels ((do-column (p-tail contenders known-types
)
1212 (let* ((position (car p-tail
))
1213 (known-type (or (nth position types
) t
)))
1214 (if (eq (nth position metatypes
) t
)
1215 (do-column (cdr p-tail
) contenders
1216 (cons (cons position known-type
)
1218 (do-methods p-tail contenders
1219 known-type
() known-types
)))
1220 (funcall methods-function contenders
1221 (let ((k-t (make-list nreq
)))
1222 (dolist (index+type known-types
)
1223 (setf (nth (car index
+type
) k-t
)
1226 (do-methods (p-tail contenders known-type winners known-types
)
1228 ;; is a (sorted) list of methods that must be discriminated.
1230 ;; is the type of this argument, constructed from tests
1233 ;; is a (sorted) list of methods that are potentially
1234 ;; applicable after the discrimination has been made.
1235 (if (null contenders
)
1236 (do-column (cdr p-tail
)
1238 (cons (cons (car p-tail
) known-type
)
1240 (let* ((position (car p-tail
))
1241 (method (car contenders
))
1242 (specl (nth position
(method-specializers method
)))
1243 (type (funcall type-function
1244 (type-from-specializer specl
))))
1245 (multiple-value-bind (app-p maybe-app-p
)
1246 (specializer-applicable-using-type-p type known-type
)
1247 (flet ((determined-to-be (truth-value)
1248 (if truth-value app-p
(not maybe-app-p
)))
1249 (do-if (truth &optional implied
)
1250 (let ((ntype (if truth type
`(not ,type
))))
1255 (augment-type ntype known-type
))
1257 (append winners
`(,method
))
1260 (cond ((determined-to-be nil
) (do-if nil t
))
1261 ((determined-to-be t
) (do-if t t
))
1262 (t (funcall test-fun position type
1263 (do-if t
) (do-if nil
))))))))))
1264 (do-column precedence methods
()))))
1266 (defun compute-secondary-dispatch-function (generic-function net
&optional
1267 method-alist wrappers
)
1268 (function-funcall (compute-secondary-dispatch-function1 generic-function net
)
1269 method-alist wrappers
))
1271 (defvar *eq-case-table-limit
* 15)
1272 (defvar *case-table-limit
* 10)
1274 (defun compute-mcase-parameters (case-list)
1275 (unless (eq t
(caar (last case-list
)))
1276 (error "The key for the last case arg to mcase was not T"))
1277 (let* ((eq-p (dolist (case case-list t
)
1278 (unless (or (eq (car case
) t
)
1279 (symbolp (caar case
)))
1281 (len (1- (length case-list
)))
1282 (type (cond ((= len
1)
1286 *eq-case-table-limit
*
1287 *case-table-limit
*))
1293 (defmacro mlookup
(key info default
&optional eq-p type
)
1294 (unless (or (eq eq-p t
) (null eq-p
))
1295 (bug "Invalid eq-p argument: ~S" eq-p
))
1299 (declare (optimize (inhibit-warnings 3)))
1300 (,(if eq-p
'eq
'eql
) ,key
(car ,info
)))
1304 `(dolist (e ,info
,default
)
1306 (declare (optimize (inhibit-warnings 3)))
1307 (,(if eq-p
'eq
'eql
) (car e
) ,key
))
1310 `(gethash ,key
,info
,default
))))
1312 (defun net-test-converter (form)
1314 (default-test-converter form
)
1316 ((invoke-effective-method-function invoke-fast-method-call
1317 invoke-effective-narrow-method-function
)
1324 `(mlookup ,(cadr form
)
1327 ,@(compute-mcase-parameters (cddr form
))))
1328 (t (default-test-converter form
)))))
1330 (defun net-code-converter (form)
1332 (default-code-converter form
)
1334 ((methods unordered-methods
)
1335 (let ((gensym (gensym)))
1339 (let ((mp (compute-mcase-parameters (cddr form
)))
1340 (gensym (gensym)) (default (gensym)))
1341 (values `(mlookup ,(cadr form
) ,gensym
,default
,@mp
)
1342 (list gensym default
))))
1344 (default-code-converter form
)))))
1346 (defun net-constant-converter (form generic-function
)
1347 (or (let ((c (methods-converter form generic-function
)))
1350 (default-constant-converter form
)
1353 (let* ((mp (compute-mcase-parameters (cddr form
)))
1354 (list (mapcar (lambda (clause)
1355 (let ((key (car clause
))
1356 (meth (cadr clause
)))
1357 (cons (if (consp key
) (car key
) key
)
1359 meth generic-function
))))
1361 (default (car (last list
))))
1362 (list (list* :mcase mp
(nbutlast list
))
1365 (default-constant-converter form
))))))
1367 (defun methods-converter (form generic-function
)
1368 (cond ((and (consp form
) (eq (car form
) 'methods
))
1370 (get-effective-method-function1 generic-function
(cadr form
))))
1371 ((and (consp form
) (eq (car form
) 'unordered-methods
))
1372 (default-secondary-dispatch-function generic-function
))))
1374 (defun convert-methods (constant method-alist wrappers
)
1375 (if (and (consp constant
)
1376 (eq (car constant
) '.methods.
))
1377 (funcall (cdr constant
) method-alist wrappers
)
1380 (defun convert-table (constant method-alist wrappers
)
1381 (cond ((and (consp constant
)
1382 (eq (car constant
) :mcase
))
1383 (let ((alist (mapcar (lambda (k+m
)
1385 (convert-methods (cdr k
+m
)
1389 (mp (cadr constant
)))
1396 (let ((table (make-hash-table :test
(if (car mp
) 'eq
'eql
))))
1398 (setf (gethash (car k
+m
) table
) (cdr k
+m
)))
1401 (defun compute-secondary-dispatch-function1 (generic-function net
1402 &optional function-p
)
1404 ((and (eq (car net
) 'methods
) (not function-p
))
1405 (get-effective-method-function1 generic-function
(cadr net
)))
1407 (let* ((name (generic-function-name generic-function
))
1408 (arg-info (gf-arg-info generic-function
))
1409 (metatypes (arg-info-metatypes arg-info
))
1410 (nargs (length metatypes
))
1411 (applyp (arg-info-applyp arg-info
))
1412 (fmc-arg-info (cons nargs applyp
))
1413 (arglist (if function-p
1414 (make-dfun-lambda-list nargs applyp
)
1415 (make-fast-method-call-lambda-list nargs applyp
))))
1416 (multiple-value-bind (cfunction constants
)
1417 ;; We don't want NAMED-LAMBDA for any expressions handed to FNGEN,
1418 ;; because name mismatches will render the hashing ineffective.
1419 (get-fun1 `(lambda ,arglist
1420 (declare (optimize (sb-c::store-closure-debug-pointer
3)))
1421 ,@(unless function-p
1422 `((declare (ignore .pv. .next-method-call.
))))
1423 (locally (declare #.
*optimize-speed
*)
1425 ,(make-emf-call nargs applyp
'emf
))))
1426 #'net-test-converter
1427 #'net-code-converter
1429 (net-constant-converter form generic-function
)))
1430 (lambda (method-alist wrappers
)
1431 (let* ((alist (list nil
))
1433 (dolist (constant constants
)
1434 (let* ((a (or (dolist (a alist nil
)
1435 (when (eq (car a
) constant
)
1439 constant method-alist wrappers
)
1441 constant method-alist wrappers
)))))
1443 (setf (cdr alist-tail
) new
)
1444 (setf alist-tail new
)))
1445 (let ((function (apply cfunction
(mapcar #'cdr
(cdr alist
)))))
1447 (set-fun-name function
`(gf-dispatch ,name
))
1448 (make-fast-method-call
1449 :function
(set-fun-name function
`(sdfun-method ,name
))
1450 :arg-info fmc-arg-info
))))))))))
1452 (defvar *show-make-unordered-methods-emf-calls
* nil
)
1454 (defun make-unordered-methods-emf (generic-function methods
)
1455 (when *show-make-unordered-methods-emf-calls
*
1456 (format t
"~&make-unordered-methods-emf ~S~%"
1457 (generic-function-name generic-function
)))
1458 (lambda (&rest args
)
1459 (let* ((types (types-from-args generic-function args
'eql
))
1460 (smethods (sort-applicable-methods generic-function
1463 (emf (get-effective-method-function generic-function smethods
)))
1464 (invoke-emf emf args
))))
1466 ;;; The value returned by compute-discriminating-function is a function
1467 ;;; object. It is called a discriminating function because it is called
1468 ;;; when the generic function is called and its role is to discriminate
1469 ;;; on the arguments to the generic function and then call appropriate
1470 ;;; method functions.
1472 ;;; A discriminating function can only be called when it is installed as
1473 ;;; the funcallable instance function of the generic function for which
1474 ;;; it was computed.
1476 ;;; More precisely, if compute-discriminating-function is called with
1477 ;;; an argument <gf1>, and returns a result <df1>, that result must
1478 ;;; not be passed to apply or funcall directly. Rather, <df1> must be
1479 ;;; stored as the funcallable instance function of the same generic
1480 ;;; function <gf1> (using SET-FUNCALLABLE-INSTANCE-FUNCTION). Then the
1481 ;;; generic function can be passed to funcall or apply.
1483 ;;; An important exception is that methods on this generic function are
1484 ;;; permitted to return a function which itself ends up calling the value
1485 ;;; returned by a more specific method. This kind of `encapsulation' of
1486 ;;; discriminating function is critical to many uses of the MOP.
1488 ;;; As an example, the following canonical case is legal:
1490 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1491 ;;; (let ((std (call-next-method)))
1493 ;;; (print (list 'call-to-gf gf arg))
1494 ;;; (funcall std arg))))
1496 ;;; Because many discriminating functions would like to use a dynamic
1497 ;;; strategy in which the precise discriminating function changes with
1498 ;;; time it is important to specify how a discriminating function is
1499 ;;; permitted itself to change the funcallable instance function of the
1500 ;;; generic function.
1502 ;;; Discriminating functions may set the funcallable instance function
1503 ;;; of the generic function, but the new value must be generated by making
1504 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1505 ;;; more specific methods which may have encapsulated the discriminating
1506 ;;; function will get a chance to encapsulate the new, inner discriminating
1509 ;;; This implies that if a discriminating function wants to modify itself
1510 ;;; it should first store some information in the generic function proper,
1511 ;;; and then call compute-discriminating-function. The appropriate method
1512 ;;; on compute-discriminating-function will see the information stored in
1513 ;;; the generic function and generate a discriminating function accordingly.
1515 ;;; The following is an example of a discriminating function which modifies
1516 ;;; itself in accordance with this protocol:
1518 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1520 ;;; (cond (<some condition>
1521 ;;; <store some info in the generic function>
1522 ;;; (set-funcallable-instance-function
1524 ;;; (compute-discriminating-function gf))
1525 ;;; (funcall gf arg))
1527 ;;; <call-a-method-of-gf>))))
1529 ;;; Whereas this code would not be legal:
1531 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1533 ;;; (cond (<some condition>
1534 ;;; (set-funcallable-instance-function
1536 ;;; (lambda (a) ..))
1537 ;;; (funcall gf arg))
1539 ;;; <call-a-method-of-gf>))))
1541 ;;; NOTE: All the examples above assume that all instances of the class
1542 ;;; my-generic-function accept only one argument.
1544 (defun slot-value-using-class-dfun (class object slotd
)
1545 (declare (ignore class
))
1546 (funcall (slot-info-reader (slot-definition-info slotd
)) object
))
1548 (defun setf-slot-value-using-class-dfun (new-value class object slotd
)
1549 (declare (ignore class
))
1550 (funcall (slot-info-writer (slot-definition-info slotd
)) new-value object
))
1552 (defun slot-boundp-using-class-dfun (class object slotd
)
1553 (declare (ignore class
))
1554 (funcall (slot-info-boundp (slot-definition-info slotd
)) object
))
1556 (defun special-case-for-compute-discriminating-function-p (gf)
1557 (or (eq gf
#'slot-value-using-class
)
1558 (eq gf
#'(setf slot-value-using-class
))
1559 (eq gf
#'slot-boundp-using-class
)))
1561 ;;; this is the normal function for computing the discriminating
1562 ;;; function of a standard-generic-function
1563 (let (initial-print-object-cache)
1564 (defun standard-compute-discriminating-function (gf)
1565 (declare (notinline slot-value
))
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
(load-time-value #'compute-applicable-methods t
))
1576 (update-all-c-a-m-gf-info gf
))
1578 ((eq gf
(load-time-value #'slot-value-using-class t
))
1579 (update-slot-value-gf-info gf
'reader
)
1580 #'slot-value-using-class-dfun
)
1581 ((eq gf
(load-time-value #'(setf slot-value-using-class
) t
))
1582 (update-slot-value-gf-info gf
'writer
)
1583 #'setf-slot-value-using-class-dfun
)
1584 ((eq gf
(load-time-value #'slot-boundp-using-class t
))
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. -- CSR, 2008-06-09
1593 ((eq gf
(locally (declare (optimize (safety 0))) #'print-object
))
1594 (let ((nkeys (nth-value 3 (get-generic-fun-info gf
))))
1596 ;; KLUDGE: someone has defined a method
1597 ;; specialized on the second argument: punt.
1598 (setf initial-print-object-cache nil
)
1599 (make-initial-dfun gf
))
1600 (initial-print-object-cache
1601 (multiple-value-bind (dfun cache info
)
1602 (make-caching-dfun gf
(copy-cache initial-print-object-cache
))
1603 (set-dfun gf dfun cache info
)))
1604 ;; the relevant PRINT-OBJECT methods get defined
1605 ;; late, by delayed DEFMETHOD. We mustn't cache
1606 ;; the effective method for our classes earlier
1607 ;; than the relevant PRINT-OBJECT methods are
1609 ((boundp '*!delayed-defmethod-args
*)
1610 (make-initial-dfun gf
))
1611 (t (multiple-value-bind (dfun cache info
)
1612 (make-final-dfun-internal
1614 (mapcar (lambda (x) (list (find-class x
)))
1615 '(sb-kernel::control-stack-exhausted
1616 sb-kernel
::binding-stack-exhausted
1617 sb-kernel
::alien-stack-exhausted
1618 sb-kernel
::heap-exhausted-error
1620 (setq initial-print-object-cache cache
)
1621 (set-dfun gf dfun
(copy-cache cache
) info
))))))
1622 ((gf-precompute-dfun-and-emf-p (slot-value gf
'arg-info
))
1623 (make-final-dfun gf
))
1625 (make-initial-dfun gf
))))
1626 (function dfun-state
)
1627 (cons (car dfun-state
))))))
1629 ;;; in general we need to support SBCL's encapsulation for generic
1630 ;;; functions: the default implementation of encapsulation changes the
1631 ;;; identity of the function bound to a name, which breaks anything
1632 ;;; class-based, so we implement the encapsulation ourselves in the
1633 ;;; discriminating function.
1634 (defun sb-impl::encapsulate-generic-function
(gf type function
)
1635 (push (cons type function
) (generic-function-encapsulations gf
))
1636 (reinitialize-instance gf
))
1638 (defun sb-impl::unencapsulate-generic-function
(gf type
)
1639 (setf (generic-function-encapsulations gf
)
1640 (remove type
(generic-function-encapsulations gf
)
1641 :key
#'car
:count
1))
1642 (reinitialize-instance gf
))
1643 (defun sb-impl::encapsulated-generic-function-p
(gf type
)
1644 (position type
(generic-function-encapsulations gf
) :key
#'car
))
1645 (defun maybe-encapsulate-discriminating-function (gf encs std
)
1648 (let ((inner (maybe-encapsulate-discriminating-function
1650 (function (cdar encs
)))
1651 (lambda (&rest args
)
1652 (apply function inner args
)))))
1653 (defmethod compute-discriminating-function ((gf standard-generic-function
))
1654 (standard-compute-discriminating-function gf
))
1655 (defmethod compute-discriminating-function :around
((gf standard-generic-function
))
1656 (maybe-encapsulate-discriminating-function
1657 gf
(generic-function-encapsulations gf
) (call-next-method)))
1659 (defmethod (setf class-name
) (new-value class
)
1660 (let ((classoid (layout-classoid (class-wrapper class
))))
1661 (if (and new-value
(symbolp new-value
))
1662 (setf (classoid-name classoid
) new-value
)
1663 (setf (classoid-name classoid
) nil
)))
1664 (reinitialize-instance class
:name new-value
)
1667 (defmethod (setf generic-function-name
) (new-value generic-function
)
1668 (reinitialize-instance generic-function
:name new-value
)
1671 (defmethod function-keywords ((method standard-method
))
1672 (multiple-value-bind (llks nreq nopt keywords
)
1673 (analyze-lambda-list (if (consp method
)
1674 (early-method-lambda-list method
)
1675 (method-lambda-list method
)))
1676 (declare (ignore nreq nopt
))
1677 (values keywords
(ll-kwds-allowp llks
))))
1679 ;;; This is based on the rules of method lambda list congruency
1680 ;;; defined in the spec. The lambda list it constructs is the pretty
1681 ;;; union of the lambda lists of the generic function and of all its
1682 ;;; methods. It doesn't take method applicability into account; we
1683 ;;; also ignore non-public parts of the interface (e.g. &AUX, default
1684 ;;; and supplied-p parameters)
1685 ;;; The compiler uses this for type-checking that callers pass acceptable
1686 ;;; keywords, so don't make this do anything fancy like looking at effective
1687 ;;; methods without also fixing the compiler.
1688 (defmethod generic-function-pretty-arglist ((gf standard-generic-function
))
1689 (let ((gf-lambda-list (generic-function-lambda-list gf
))
1690 (methods (generic-function-methods gf
)))
1691 (flet ((canonize (k)
1692 (multiple-value-bind (kw var
)
1693 (parse-key-arg-spec k
)
1694 (if (and (eql (symbol-package kw
) *keyword-package
*)
1697 (list (list kw var
))))))
1698 (multiple-value-bind (llks required optional rest keys
)
1699 (parse-lambda-list gf-lambda-list
:silent t
)
1700 (collect ((keys (mapcar #'canonize keys
)))
1701 ;; Possibly extend the keyword parameters of the gf by
1702 ;; additional key parameters of its methods:
1704 (make-lambda-list llks nil required optional rest
(keys)))
1705 (binding* (((m.llks nil nil nil m.keys
)
1706 (parse-lambda-list (method-lambda-list m
) :silent t
)))
1707 (setq llks
(logior llks m.llks
))
1709 (unless (member (parse-key-arg-spec k
) (keys)
1710 :key
#'parse-key-arg-spec
:test
#'eq
)
1711 (keys (canonize k
)))))))))))