Fix grammar in lossage message
[sbcl.git] / src / pcl / methods.lisp
blob7868ebb5760124efce38290cea95831719d8e5be
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
8 ;;;; information.
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
18 ;;;; control laws.
19 ;;;;
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
22 ;;;; specification.
24 (in-package "SB-PCL")
26 ;;; methods
27 ;;;
28 ;;; Methods themselves are simple inanimate objects. Most properties of
29 ;;; methods are immutable, methods cannot be reinitialized. The following
30 ;;; properties of methods can be changed:
31 ;;; METHOD-GENERIC-FUNCTION
33 ;;; initialization
34 ;;;
35 ;;; Error checking is done in before methods. Because of the simplicity of
36 ;;; standard method objects the standard primary method can fill the slots.
37 ;;;
38 ;;; Methods are not reinitializable.
40 (define-condition metaobject-initialization-violation
41 (reference-condition simple-error)
42 ())
44 (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)
57 'base-string)
58 :format-arguments (list ',name)
59 :references (list '(: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
67 plist &rest initargs)
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))
76 (:report
77 (lambda (c s)
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))
93 nil)
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)))
135 (unless (null frcs)
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 (defvar *the-class-generic-function*
171 (find-class 'generic-function))
172 (defvar *the-class-standard-generic-function*
173 (find-class 'standard-generic-function))
175 (defmethod shared-initialize :before
176 ((generic-function standard-generic-function)
177 slot-names
178 &key (lambda-list () lambda-list-p)
179 argument-precedence-order
180 declarations
181 documentation
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.~%~
191 It must be ~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)
198 *the-class-method*))
199 (initarg-error :method-class
200 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
206 "not supplied"
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
211 method-combination
212 "a method combination object")))
213 ((slot-boundp generic-function '%method-combination))
215 (initarg-error :method-combination
216 "not supplied"
217 "a method combination object")))))
219 (defun find-generic-function (name &optional (errorp t))
220 (let ((fun (and (fboundp name) (fdefinition name))))
221 (cond
222 ((and fun (typep fun 'generic-function)) fun)
223 (errorp (error "No generic function named ~S." name))
224 (t nil))))
226 (defun real-add-named-method (generic-function-name qualifiers
227 specializers lambda-list &rest other-initargs)
228 (unless (and (fboundp generic-function-name)
229 (typep (fdefinition generic-function-name) 'generic-function))
230 (warn 'implicit-generic-function-warning :name generic-function-name))
231 (let* ((existing-gf (find-generic-function generic-function-name nil))
232 (generic-function
233 (if existing-gf
234 (ensure-generic-function
235 generic-function-name
236 :generic-function-class (class-of existing-gf))
237 (ensure-generic-function generic-function-name)))
238 (proto (method-prototype-for-gf generic-function-name)))
239 ;; FIXME: Destructive modification of &REST list.
240 (setf (getf (getf other-initargs 'plist) :name)
241 (make-method-spec generic-function qualifiers specializers))
242 (let ((new (apply #'make-instance (class-of proto)
243 :qualifiers qualifiers :specializers specializers
244 :lambda-list lambda-list other-initargs)))
245 (add-method generic-function new)
246 new)))
248 (define-condition find-method-length-mismatch
249 (reference-condition simple-error)
251 (:default-initargs :references (list '(:ansi-cl :function find-method))))
253 (defun real-get-method (generic-function qualifiers specializers
254 &optional (errorp t)
255 always-check-specializers)
256 (let ((lspec (length specializers))
257 (methods (generic-function-methods generic-function)))
258 (when (or methods always-check-specializers)
259 (let ((nreq (length (arg-info-metatypes (gf-arg-info
260 generic-function)))))
261 ;; Since we internally bypass FIND-METHOD by using GET-METHOD
262 ;; instead we need to to this here or users may get hit by a
263 ;; failed AVER instead of a sensible error message.
264 (when (/= lspec nreq)
265 (error
266 'find-method-length-mismatch
267 :format-control
268 "~@<The generic function ~S takes ~D required argument~:P; ~
269 was asked to find a method with specializers ~S~@:>"
270 :format-arguments (list generic-function nreq specializers)))))
271 (let ((hit
272 (dolist (method methods)
273 (let ((mspecializers (method-specializers method)))
274 (aver (= lspec (length mspecializers)))
275 (when (and (equal qualifiers (safe-method-qualifiers method))
276 (every #'same-specializer-p specializers
277 (method-specializers method)))
278 (return method))))))
279 (cond (hit hit)
280 ((null errorp) nil)
282 (error "~@<There is no method on ~S with ~
283 ~:[no qualifiers~;~:*qualifiers ~S~] ~
284 and specializers ~S.~@:>"
285 generic-function qualifiers specializers))))))
287 (defmethod find-method ((generic-function standard-generic-function)
288 qualifiers specializers &optional (errorp t))
289 ;; ANSI about FIND-METHOD: "The specializers argument contains the
290 ;; parameter specializers for the method. It must correspond in
291 ;; length to the number of required arguments of the generic
292 ;; function, or an error is signaled."
294 ;; This error checking is done by REAL-GET-METHOD.
295 (real-get-method
296 generic-function qualifiers
297 ;; ANSI for FIND-METHOD seems to imply that in fact specializers
298 ;; should always be passed in parsed form instead of being parsed
299 ;; at this point. Since there's no ANSI-blessed way of getting an
300 ;; EQL specializer, that seems unnecessarily painful, so we are
301 ;; nice to our users. -- CSR, 2007-06-01
302 ;; Note that INTERN-EQL-SPECIALIZER is exported from SB-MOP, but MOP isn't
303 ;; part of the ANSI standard. Parsing introduces a tiny semantic problem in
304 ;; the edge case of an EQL specializer whose object is literally (EQL :X).
305 ;; That one must be supplied as a pre-parsed #<EQL-SPECIALIZER> because if
306 ;; not, we'd parse it into a specializer whose object is :X.
307 (parse-specializers generic-function specializers) errorp t))
309 ;;; Compute various information about a generic-function's arglist by looking
310 ;;; at the argument lists of the methods. The hair for trying not to use
311 ;;; &REST arguments lives here.
312 ;;; The values returned are:
313 ;;; number-of-required-arguments
314 ;;; the number of required arguments to this generic-function's
315 ;;; discriminating function
316 ;;; &rest-argument-p
317 ;;; whether or not this generic-function's discriminating
318 ;;; function takes an &rest argument.
319 ;;; specialized-argument-positions
320 ;;; a list of the positions of the arguments this generic-function
321 ;;; specializes (e.g. for a classical generic-function this is the
322 ;;; list: (1)).
323 (defmethod compute-discriminating-function-arglist-info
324 ((generic-function standard-generic-function))
325 ;;(declare (values number-of-required-arguments &rest-argument-p
326 ;; specialized-argument-postions))
327 (let ((number-required nil)
328 (restp nil)
329 (specialized-positions ())
330 (methods (generic-function-methods generic-function)))
331 (dolist (method methods)
332 (multiple-value-setq (number-required restp specialized-positions)
333 (compute-discriminating-function-arglist-info-internal
334 generic-function method number-required restp specialized-positions)))
335 (values number-required restp (sort specialized-positions #'<))))
337 (defun compute-discriminating-function-arglist-info-internal
338 (generic-function method number-of-requireds restp
339 specialized-argument-positions)
340 (declare (ignore generic-function)
341 (type (or null fixnum) number-of-requireds))
342 (let ((requireds 0))
343 (declare (fixnum requireds))
344 ;; Go through this methods arguments seeing how many are required,
345 ;; and whether there is an &rest argument.
346 (dolist (arg (method-lambda-list method))
347 (cond ((eq arg '&aux) (return))
348 ((memq arg '(&optional &rest &key))
349 (return (setq restp t)))
350 ((memq arg lambda-list-keywords))
351 (t (incf requireds))))
352 ;; Now go through this method's type specifiers to see which
353 ;; argument positions are type specified. Treat T specially
354 ;; in the usual sort of way. For efficiency don't bother to
355 ;; keep specialized-argument-positions sorted, rather depend
356 ;; on our caller to do that.
357 (let ((pos 0))
358 (dolist (type-spec (method-specializers method))
359 (unless (eq type-spec *the-class-t*)
360 (pushnew pos specialized-argument-positions :test #'eq))
361 (incf pos)))
362 ;; Finally merge the values for this method into the values
363 ;; for the exisiting methods and return them. Note that if
364 ;; num-of-requireds is NIL it means this is the first method
365 ;; and we depend on that.
366 (values (min (or number-of-requireds requireds) requireds)
367 (or restp
368 (and number-of-requireds (/= number-of-requireds requireds)))
369 specialized-argument-positions)))
371 (defun make-discriminating-function-arglist (number-required-arguments restp)
372 (nconc (let ((args nil))
373 (dotimes (i number-required-arguments)
374 (push (format-symbol *package* ;; ! is this right?
375 "Discriminating Function Arg ~D"
377 args))
378 (nreverse args))
379 (when restp
380 `(&rest ,(format-symbol *package*
381 "Discriminating Function &rest Arg")))))
383 (defmethod generic-function-argument-precedence-order
384 ((gf standard-generic-function))
385 (aver (eq **boot-state** 'complete))
386 (loop with arg-info = (gf-arg-info gf)
387 with lambda-list = (arg-info-lambda-list arg-info)
388 for argument-position in (arg-info-precedence arg-info)
389 collect (nth argument-position lambda-list)))
391 (defmethod generic-function-lambda-list ((gf generic-function))
392 (gf-lambda-list gf))
394 (defmethod gf-fast-method-function-p ((gf standard-generic-function))
395 (gf-info-fast-mf-p (slot-value gf 'arg-info)))
397 (defmethod initialize-instance :after ((gf standard-generic-function)
398 &key (lambda-list nil lambda-list-p)
399 argument-precedence-order)
400 (with-slots (arg-info) gf
401 (if lambda-list-p
402 (set-arg-info gf
403 :lambda-list lambda-list
404 :argument-precedence-order argument-precedence-order)
405 (set-arg-info gf))
406 (when (arg-info-valid-p arg-info)
407 (update-dfun gf))))
409 (defmethod reinitialize-instance :around
410 ((gf standard-generic-function) &rest args &key
411 (lambda-list nil lambda-list-p) (argument-precedence-order nil apo-p))
412 (let ((old-mc (generic-function-method-combination gf)))
413 (prog1 (call-next-method)
414 ;; KLUDGE: EQ is too strong a test.
415 (unless (eq old-mc (generic-function-method-combination gf))
416 (flush-effective-method-cache gf))
417 (cond
418 ((and lambda-list-p apo-p)
419 (set-arg-info gf
420 :lambda-list lambda-list
421 :argument-precedence-order argument-precedence-order))
422 (lambda-list-p (set-arg-info gf :lambda-list lambda-list))
423 (t (set-arg-info gf)))
424 (when (arg-info-valid-p (gf-arg-info gf))
425 (update-dfun gf))
426 (map-dependents gf (lambda (dependent)
427 (apply #'update-dependent gf dependent args))))))
429 (declaim (special *lazy-dfun-compute-p*))
431 (defun set-methods (gf methods)
432 (setf (generic-function-methods gf) nil)
433 (loop (when (null methods) (return gf))
434 (real-add-method gf (pop methods) methods)))
436 (define-condition new-value-specialization (reference-condition error)
437 ((%method :initarg :method :reader new-value-specialization-method))
438 (:report
439 (lambda (c s)
440 (format s "~@<Cannot add method ~S to ~S, as it specializes the ~
441 new-value argument.~@:>"
442 (new-value-specialization-method c)
443 #'(setf slot-value-using-class))))
444 (:default-initargs :references
445 (list '(:sbcl :node "Metaobject Protocol")
446 '(:amop :generic-function (setf slot-value-using-class)))))
448 (defgeneric values-for-add-method (gf method)
449 (:method ((gf standard-generic-function) (method standard-method))
450 ;; KLUDGE: Just a single generic dispatch, and everything else
451 ;; comes from permutation vectors. Would be nicer to define
452 ;; REAL-ADD-METHOD with a proper method so that we could efficiently
453 ;; use SLOT-VALUE there.
455 ;; Optimization note: REAL-ADD-METHOD has a lot of O(N) stuff in it (as
456 ;; does PCL as a whole). It should not be too hard to internally store
457 ;; many of the things we now keep in lists as either purely functional
458 ;; O(log N) sets, or --if we don't mind the memory cost-- using
459 ;; specialized hash-tables: most things are used to answer questions about
460 ;; set-membership, not ordering.
461 (values (slot-value gf '%lock)
462 (slot-value method 'qualifiers)
463 (slot-value method 'specializers)
464 (slot-value method 'lambda-list)
465 (slot-value method '%generic-function)
466 (slot-value gf 'name))))
468 (define-condition print-object-stream-specializer (reference-condition simple-warning)
470 (:default-initargs
471 :references (list '(:ansi-cl :function print-object))
472 :format-control "~@<Specializing on the second argument to ~S has ~
473 unportable effects, and also interferes with ~
474 precomputation of print functions for exceptional ~
475 situations.~@:>"
476 :format-arguments (list 'print-object)))
478 (defun defer-ftype-computation (gf)
479 ;; Is there any reason not to do this as soon as possible?
480 ;; While doing it with every ADD/REMOVE-METHOD call could result in
481 ;; wasted work, it seems like unnecessary complexity.
482 ;; I think it's just to get through bootstrap, probably,
483 ;; but if it's a semantics thing, it deserves some explanation.
484 (let ((name (generic-function-name gf)))
485 (when (legal-fun-name-p name) ; tautological ?
486 (unless (eq (info :function :where-from name) :declared)
487 (when (and (fboundp name) (eq (fdefinition name) gf))
488 (setf (info :function :type name) :generic-function))))))
490 (defun compute-gf-ftype (name)
491 (let ((gf (and (fboundp name) (fdefinition name))))
492 (if (generic-function-p gf)
493 (let* ((ll (generic-function-lambda-list gf))
494 ;; If the GF has &REST without &KEY then we don't augment
495 ;; the FTYPE with keywords, so as not to complain about keywords
496 ;; which seem not to be accepted.
497 (type (sb-c::ftype-from-lambda-list
498 (if (and (member '&rest ll) (not (member '&key ll)))
500 (generic-function-pretty-arglist gf)))))
501 ;; It would be nice if globaldb were transactional,
502 ;; so that either both updates or neither occur.
503 (setf (info :function :type name) type
504 (info :function :where-from name) :defined-method)
505 type)
506 ;; The defaulting expression for (:FUNCTION :TYPE) does not store
507 ;; the default. For :GENERIC-FUNCTION that is not FBOUNDP we also
508 ;; don't, however this branch should never be reached because the
509 ;; info only stores :GENERIC-FUNCTION when methods are loaded.
510 ;; Maybe AVER that it does not happen?
511 (sb-c::ftype-from-fdefn name))))
513 (defun real-add-method (generic-function method &optional skip-dfun-update-p)
514 (flet ((similar-lambda-lists-p (old-method new-lambda-list)
515 (binding* (((a-llks a-nreq a-nopt)
516 (analyze-lambda-list (method-lambda-list old-method)))
517 ((b-llks b-nreq b-nopt)
518 (analyze-lambda-list new-lambda-list)))
519 (and (= a-nreq b-nreq)
520 (= a-nopt b-nopt)
521 (eq (ll-keyp-or-restp a-llks)
522 (ll-keyp-or-restp b-llks))))))
523 (multiple-value-bind (lock qualifiers specializers new-lambda-list
524 method-gf name)
525 (values-for-add-method generic-function method)
526 (when method-gf
527 (error "~@<The method ~S is already part of the generic ~
528 function ~S; it can't be added to another generic ~
529 function until it is removed from the first one.~@:>"
530 method method-gf))
531 (when (and (eq name 'print-object) (not (eq (second specializers) *the-class-t*)))
532 (warn 'print-object-stream-specializer))
533 (handler-case
534 ;; System lock because interrupts need to be disabled as
535 ;; well: it would be bad to unwind and leave the gf in an
536 ;; inconsistent state.
537 (sb-thread::with-recursive-system-lock (lock)
538 (let ((existing (get-method generic-function
539 qualifiers
540 specializers
541 nil)))
543 ;; If there is already a method like this one then we must get
544 ;; rid of it before proceeding. Note that we call the generic
545 ;; function REMOVE-METHOD to remove it rather than doing it in
546 ;; some internal way.
547 (when (and existing (similar-lambda-lists-p existing new-lambda-list))
548 (remove-method generic-function existing))
550 ;; KLUDGE: We have a special case here, as we disallow
551 ;; specializations of the NEW-VALUE argument to (SETF
552 ;; SLOT-VALUE-USING-CLASS). GET-ACCESSOR-METHOD-FUNCTION is
553 ;; the optimizing function here: it precomputes the effective
554 ;; method, assuming that there is no dispatch to be done on
555 ;; the new-value argument.
556 (when (and (eq generic-function #'(setf slot-value-using-class))
557 (not (eq *the-class-t* (first specializers))))
558 (error 'new-value-specialization :method method))
560 (setf (method-generic-function method) generic-function)
561 (pushnew method (generic-function-methods generic-function) :test #'eq)
562 (dolist (specializer specializers)
563 (add-direct-method specializer method))
565 ;; KLUDGE: SET-ARG-INFO contains the error-detecting logic for
566 ;; detecting attempts to add methods with incongruent lambda
567 ;; lists. However, according to Gerd Moellmann on cmucl-imp,
568 ;; it also depends on the new method already having been added
569 ;; to the generic function. Therefore, we need to remove it
570 ;; again on error:
571 (let ((remove-again-p t))
572 (unwind-protect
573 (progn
574 (set-arg-info generic-function :new-method method)
575 (setq remove-again-p nil))
576 (when remove-again-p
577 (remove-method generic-function method))))
579 ;; KLUDGE II: ANSI saith that it is not an error to add a
580 ;; method with invalid qualifiers to a generic function of the
581 ;; wrong kind; it's only an error at generic function
582 ;; invocation time; I dunno what the rationale was, and it
583 ;; sucks. Nevertheless, it's probably a programmer error, so
584 ;; let's warn anyway. -- CSR, 2003-08-20
585 (let ((mc (generic-function-method-combination generic-functioN)))
586 (cond
587 ((eq mc *standard-method-combination*)
588 (when (and qualifiers
589 (or (cdr qualifiers)
590 (not (memq (car qualifiers)
591 '(:around :before :after)))))
592 (warn "~@<Invalid qualifiers for standard method ~
593 combination in method ~S:~2I~_~S.~@:>"
594 method qualifiers)))
595 ((short-method-combination-p mc)
596 (let ((mc-name (method-combination-type-name mc)))
597 (when (or (null qualifiers)
598 (cdr qualifiers)
599 (and (neq (car qualifiers) :around)
600 (neq (car qualifiers) mc-name)))
601 (warn "~@<Invalid qualifiers for ~S method combination ~
602 in method ~S:~2I~_~S.~@:>"
603 mc-name method qualifiers))))))
604 (unless skip-dfun-update-p
605 (update-ctors 'add-method
606 :generic-function generic-function
607 :method method)
608 (update-dfun generic-function))
609 (defer-ftype-computation generic-function)
610 (map-dependents generic-function
611 (lambda (dep)
612 (update-dependent generic-function
613 dep 'add-method method)))))
614 (serious-condition (c)
615 (error c)))))
616 generic-function)
618 (defun real-remove-method (generic-function method)
619 (when (eq generic-function (method-generic-function method))
620 (flush-effective-method-cache generic-function)
621 (let ((lock (gf-lock generic-function)))
622 ;; System lock because interrupts need to be disabled as well:
623 ;; it would be bad to unwind and leave the gf in an inconsistent
624 ;; state.
625 (sb-thread::with-recursive-system-lock (lock)
626 (let* ((specializers (method-specializers method))
627 (methods (generic-function-methods generic-function))
628 (new-methods (remove method methods)))
629 (setf (method-generic-function method) nil
630 (generic-function-methods generic-function) new-methods)
631 (dolist (specializer specializers)
632 (remove-direct-method specializer method))
633 (set-arg-info generic-function)
634 (update-ctors 'remove-method
635 :generic-function generic-function
636 :method method)
637 (update-dfun generic-function)
638 (defer-ftype-computation generic-function)
639 (map-dependents generic-function
640 (lambda (dep)
641 (update-dependent generic-function
642 dep 'remove-method method)))))))
643 generic-function)
645 (defun compute-applicable-methods-function (generic-function arguments)
646 (values (compute-applicable-methods-using-types
647 generic-function
648 (types-from-args generic-function arguments 'eql))))
650 (defmethod compute-applicable-methods
651 ((generic-function generic-function) arguments)
652 (values (compute-applicable-methods-using-types
653 generic-function
654 (types-from-args generic-function arguments 'eql))))
656 (defmethod compute-applicable-methods-using-classes
657 ((generic-function generic-function) classes)
658 (compute-applicable-methods-using-types
659 generic-function
660 (types-from-args generic-function classes 'class-eq)))
662 (defun !proclaim-incompatible-superclasses (classes)
663 (setq classes (mapcar (lambda (class)
664 (if (symbolp class)
665 (find-class class)
666 class))
667 classes))
668 (dolist (class classes)
669 (dolist (other-class classes)
670 (unless (eq class other-class)
671 (pushnew other-class (class-incompatible-superclass-list class) :test #'eq)))))
673 (defun superclasses-compatible-p (class1 class2)
674 (let ((cpl1 (cpl-or-nil class1))
675 (cpl2 (cpl-or-nil class2)))
676 (dolist (sc1 cpl1 t)
677 (dolist (ic (class-incompatible-superclass-list sc1))
678 (when (memq ic cpl2)
679 (return-from superclasses-compatible-p nil))))))
681 (mapc
682 #'!proclaim-incompatible-superclasses
683 '(;; superclass class
684 (system-class std-class structure-class) ; direct subclasses of pcl-class
685 (standard-class funcallable-standard-class)
686 ;; superclass metaobject
687 (class eql-specializer class-eq-specializer method method-combination
688 generic-function slot-definition)
689 ;; metaclass built-in-class
690 (number sequence character ; direct subclasses of t, but not array
691 standard-object structure-object) ; or symbol
692 (number array character symbol ; direct subclasses of t, but not
693 standard-object structure-object) ; sequence
694 (complex float rational) ; direct subclasses of number
695 (integer ratio) ; direct subclasses of rational
696 (list vector) ; direct subclasses of sequence
697 (cons null) ; direct subclasses of list
698 (string bit-vector) ; direct subclasses of vector
701 (defmethod same-specializer-p ((specl1 specializer) (specl2 specializer))
702 (eql specl1 specl2))
704 (defmethod same-specializer-p ((specl1 class) (specl2 class))
705 (eq specl1 specl2))
707 (defmethod specializer-class ((specializer class))
708 specializer)
710 (defmethod same-specializer-p ((specl1 class-eq-specializer)
711 (specl2 class-eq-specializer))
712 (eq (specializer-class specl1) (specializer-class specl2)))
714 ;; FIXME: This method is wacky, and indicative of a coding style in which
715 ;; metaphorically the left hand does not know what the right is doing.
716 ;; If you want this to be the abstract comparator, and you "don't know"
717 ;; that EQL-specializers are interned, then the comparator should be EQL.
718 ;; But if you *do* know that they're interned, then why does this method
719 ;; exist at all? The method on SPECIALIZER works fine.
720 (defmethod same-specializer-p ((specl1 eql-specializer)
721 (specl2 eql-specializer))
722 ;; A bit of deception to confuse the enemy?
723 (eq (specializer-object specl1) (specializer-object specl2)))
725 (defmethod specializer-class ((specializer eql-specializer))
726 (class-of (slot-value specializer 'object)))
728 (defun specializer-class-or-nil (specializer)
729 (and (standard-specializer-p specializer)
730 (specializer-class specializer)))
732 (defun error-need-at-least-n-args (function n)
733 (error 'simple-program-error
734 :format-control "~@<The function ~2I~_~S ~I~_requires ~
735 at least ~W argument~:P.~:>"
736 :format-arguments (list function n)))
738 (defun types-from-args (generic-function arguments &optional type-modifier)
739 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
740 (get-generic-fun-info generic-function)
741 (declare (ignore applyp metatypes nkeys))
742 (let ((types-rev nil))
743 (dotimes-fixnum (i nreq)
744 (unless arguments
745 (error-need-at-least-n-args (generic-function-name generic-function)
746 nreq))
747 (let ((arg (pop arguments)))
748 (push (if type-modifier `(,type-modifier ,arg) arg) types-rev)))
749 (values (nreverse types-rev) arg-info))))
751 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes)
752 (let* ((w wrappers) (w-tail w) (mt-tail metatypes))
753 (dolist (class (ensure-list classes))
754 (unless (eq t (car mt-tail))
755 (let ((c-w (class-wrapper class)))
756 (unless c-w (return-from get-wrappers-from-classes nil))
757 (if (eql nkeys 1)
758 (setq w c-w)
759 (setf (car w-tail) c-w
760 w-tail (cdr w-tail)))))
761 (setq mt-tail (cdr mt-tail)))
764 (defun sdfun-for-caching (gf classes)
765 (let ((types (mapcar #'class-eq-type classes)))
766 (multiple-value-bind (methods all-applicable-and-sorted-p)
767 (compute-applicable-methods-using-types gf types)
768 (let ((generator (get-secondary-dispatch-function1
769 gf methods types nil t all-applicable-and-sorted-p)))
770 (make-callable gf methods generator
771 nil (mapcar #'class-wrapper classes))))))
773 (defun value-for-caching (gf classes)
774 (let ((methods (compute-applicable-methods-using-types
775 gf (mapcar #'class-eq-type classes))))
776 (method-plist-value (car methods) :constant-value)))
778 (defun default-secondary-dispatch-function (generic-function)
779 (lambda (&rest args)
780 (let ((methods (compute-applicable-methods generic-function args)))
781 (if methods
782 (let ((emf (get-effective-method-function generic-function
783 methods)))
784 (invoke-emf emf args))
785 (call-no-applicable-method generic-function args)))))
787 (defun list-eq (x y)
788 (loop (when (atom x) (return (eq x y)))
789 (when (atom y) (return nil))
790 (unless (eq (car x) (car y)) (return nil))
791 (setq x (cdr x)
792 y (cdr y))))
794 (defvar *std-cam-methods* nil)
796 (defun compute-applicable-methods-emf (generic-function)
797 (if (eq **boot-state** 'complete)
798 (let* ((cam (gdefinition 'compute-applicable-methods))
799 (cam-methods (compute-applicable-methods-using-types
800 cam (list `(eql ,generic-function) t))))
801 (values (get-effective-method-function cam cam-methods)
802 (list-eq cam-methods
803 (or *std-cam-methods*
804 (setq *std-cam-methods*
805 (compute-applicable-methods-using-types
806 cam (list `(eql ,cam) t)))))))
807 (values #'compute-applicable-methods-function t)))
809 (defun compute-applicable-methods-emf-std-p (gf)
810 (gf-info-c-a-m-emf-std-p (gf-arg-info gf)))
812 (defvar *old-c-a-m-gf-methods* nil)
814 (defun update-all-c-a-m-gf-info (c-a-m-gf)
815 (let ((methods (generic-function-methods c-a-m-gf)))
816 (if (and *old-c-a-m-gf-methods*
817 (every (lambda (old-method)
818 (member old-method methods :test #'eq))
819 *old-c-a-m-gf-methods*))
820 (let ((gfs-to-do nil)
821 (gf-classes-to-do nil))
822 (dolist (method methods)
823 (unless (member method *old-c-a-m-gf-methods* :test #'eq)
824 (let ((specl (car (method-specializers method))))
825 (if (eql-specializer-p specl)
826 (pushnew (specializer-object specl) gfs-to-do :test #'eq)
827 (pushnew (specializer-class specl) gf-classes-to-do :test #'eq)))))
828 (map-all-generic-functions
829 (lambda (gf)
830 (when (or (member gf gfs-to-do :test #'eq)
831 (dolist (class gf-classes-to-do nil)
832 (member class
833 (class-precedence-list (class-of gf))
834 :test #'eq)))
835 (update-c-a-m-gf-info gf)))))
836 (map-all-generic-functions #'update-c-a-m-gf-info))
837 (setq *old-c-a-m-gf-methods* methods)))
839 (defun update-gf-info (gf)
840 (update-c-a-m-gf-info gf)
841 (update-gf-simple-accessor-type gf))
843 (defun update-c-a-m-gf-info (gf)
844 (unless (early-gf-p gf)
845 (multiple-value-bind (c-a-m-emf std-p)
846 (compute-applicable-methods-emf gf)
847 (let ((arg-info (gf-arg-info gf)))
848 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
849 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)))))
851 (defun update-gf-simple-accessor-type (gf)
852 (let ((arg-info (gf-arg-info gf)))
853 (setf (gf-info-simple-accessor-type arg-info)
854 (let* ((methods (generic-function-methods gf))
855 (class (and methods (class-of (car methods))))
856 (type
857 (and class
858 (cond ((or (eq class *the-class-standard-reader-method*)
859 (eq class *the-class-global-reader-method*))
860 'reader)
861 ((or (eq class *the-class-standard-writer-method*)
862 (eq class *the-class-global-writer-method*))
863 'writer)
864 ((or (eq class *the-class-standard-boundp-method*)
865 (eq class *the-class-global-boundp-method*))
866 'boundp)))))
867 (when (and (gf-info-c-a-m-emf-std-p arg-info)
868 type
869 (dolist (method (cdr methods) t)
870 (unless (eq class (class-of method)) (return nil)))
871 (eq (generic-function-method-combination gf)
872 *standard-method-combination*))
873 type)))))
876 ;;; CMUCL (Gerd's PCL, 2002-04-25) comment:
878 ;;; Return two values. First value is a function to be stored in
879 ;;; effective slot definition SLOTD for reading it with
880 ;;; SLOT-VALUE-USING-CLASS, setting it with (SETF
881 ;;; SLOT-VALUE-USING-CLASS) or testing it with
882 ;;; SLOT-BOUNDP-USING-CLASS. GF is one of these generic functions,
883 ;;; TYPE is one of the symbols READER, WRITER, BOUNDP. CLASS is
884 ;;; SLOTD's class.
886 ;;; Second value is true if the function returned is one of the
887 ;;; optimized standard functions for the purpose, which are used
888 ;;; when only standard methods are applicable.
890 ;;; FIXME: Change all these wacky function names to something sane.
891 (defun get-accessor-method-function (gf type class slotd)
892 (let* ((std-method (standard-svuc-method type))
893 (str-method (structure-svuc-method type))
894 (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
895 (types (if (eq type 'writer) `(t ,@types1) types1))
896 (methods (compute-applicable-methods-using-types gf types))
897 (std-p (null (cdr methods))))
898 (values
899 (if std-p
900 (get-optimized-std-accessor-method-function class slotd type)
901 (let* ((optimized-std-fun
902 (get-optimized-std-slot-value-using-class-method-function
903 class slotd type))
904 (method-alist
905 `((,(car (or (member std-method methods :test #'eq)
906 (member str-method methods :test #'eq)
907 (bug "error in ~S"
908 'get-accessor-method-function)))
909 ,optimized-std-fun)))
910 (wrappers
911 (let ((wrappers (list (layout-of class)
912 (class-wrapper class)
913 (layout-of slotd))))
914 (if (eq type 'writer)
915 (cons (class-wrapper *the-class-t*) wrappers)
916 wrappers)))
917 (sdfun (get-secondary-dispatch-function
918 gf methods types method-alist wrappers)))
919 (get-accessor-from-svuc-method-function class slotd sdfun type)))
920 std-p)))
922 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
923 (defun update-slot-value-gf-info (gf type)
924 (unless *new-class*
925 (update-std-or-str-methods gf type))
926 (when (and (standard-svuc-method type) (structure-svuc-method type))
927 (flet ((update-accessor-info (class)
928 (when (class-finalized-p class)
929 (dolist (slotd (class-slots class))
930 (compute-slot-accessor-info slotd type gf)))))
931 (if *new-class*
932 (update-accessor-info *new-class*)
933 (map-all-classes #'update-accessor-info 'slot-object)))))
935 (defvar *standard-slot-value-using-class-method* nil)
936 (defvar *standard-setf-slot-value-using-class-method* nil)
937 (defvar *standard-slot-boundp-using-class-method* nil)
938 (defvar *condition-slot-value-using-class-method* nil)
939 (defvar *condition-setf-slot-value-using-class-method* nil)
940 (defvar *condition-slot-boundp-using-class-method* nil)
941 (defvar *structure-slot-value-using-class-method* nil)
942 (defvar *structure-setf-slot-value-using-class-method* nil)
943 (defvar *structure-slot-boundp-using-class-method* nil)
945 (defun standard-svuc-method (type)
946 (case type
947 (reader *standard-slot-value-using-class-method*)
948 (writer *standard-setf-slot-value-using-class-method*)
949 (boundp *standard-slot-boundp-using-class-method*)))
951 (defun set-standard-svuc-method (type method)
952 (case type
953 (reader (setq *standard-slot-value-using-class-method* method))
954 (writer (setq *standard-setf-slot-value-using-class-method* method))
955 (boundp (setq *standard-slot-boundp-using-class-method* method))))
957 (defun condition-svuc-method (type)
958 (case type
959 (reader *condition-slot-value-using-class-method*)
960 (writer *condition-setf-slot-value-using-class-method*)
961 (boundp *condition-slot-boundp-using-class-method*)))
963 (defun set-condition-svuc-method (type method)
964 (case type
965 (reader (setq *condition-slot-value-using-class-method* method))
966 (writer (setq *condition-setf-slot-value-using-class-method* method))
967 (boundp (setq *condition-slot-boundp-using-class-method* method))))
969 (defun structure-svuc-method (type)
970 (case type
971 (reader *structure-slot-value-using-class-method*)
972 (writer *structure-setf-slot-value-using-class-method*)
973 (boundp *structure-slot-boundp-using-class-method*)))
975 (defun set-structure-svuc-method (type method)
976 (case type
977 (reader (setq *structure-slot-value-using-class-method* method))
978 (writer (setq *structure-setf-slot-value-using-class-method* method))
979 (boundp (setq *structure-slot-boundp-using-class-method* method))))
981 (defun update-std-or-str-methods (gf type)
982 (dolist (method (generic-function-methods gf))
983 (let ((specls (method-specializers method)))
984 (when (and (or (not (eq type 'writer))
985 (eq (pop specls) *the-class-t*))
986 (every #'classp specls))
987 (cond ((and (eq (class-name (car specls)) 'std-class)
988 (eq (class-name (cadr specls)) 'standard-object)
989 (eq (class-name (caddr specls))
990 'standard-effective-slot-definition))
991 (set-standard-svuc-method type method))
992 ((and (eq (class-name (car specls)) 'condition-class)
993 (eq (class-name (cadr specls)) 'condition)
994 (eq (class-name (caddr specls))
995 'condition-effective-slot-definition))
996 (set-condition-svuc-method type method))
997 ((and (eq (class-name (car specls)) 'structure-class)
998 (eq (class-name (cadr specls)) 'structure-object)
999 (eq (class-name (caddr specls))
1000 'structure-effective-slot-definition))
1001 (set-structure-svuc-method type method)))))))
1003 (defun mec-all-classes-internal (spec precompute-p)
1004 (let ((wrapper (class-wrapper (specializer-class spec))))
1005 (unless (or (not wrapper) (invalid-wrapper-p wrapper))
1006 (cons (specializer-class spec)
1007 (and (classp spec)
1008 precompute-p
1009 (not (or (eq spec *the-class-t*)
1010 (eq spec *the-class-slot-object*)
1011 (eq spec *the-class-standard-object*)
1012 (eq spec *the-class-structure-object*)))
1013 (let ((sc (class-direct-subclasses spec)))
1014 (when sc
1015 (mapcan (lambda (class)
1016 (mec-all-classes-internal class precompute-p))
1017 sc))))))))
1019 (defun mec-all-classes (spec precompute-p)
1020 (let ((classes (mec-all-classes-internal spec precompute-p)))
1021 (if (null (cdr classes))
1022 classes
1023 (let* ((a-classes (cons nil classes))
1024 (tail classes))
1025 (loop (when (null (cdr tail))
1026 (return (cdr a-classes)))
1027 (let ((class (cadr tail))
1028 (ttail (cddr tail)))
1029 (if (dolist (c ttail nil)
1030 (when (eq class c) (return t)))
1031 (setf (cdr tail) (cddr tail))
1032 (setf tail (cdr tail)))))))))
1034 (defun mec-all-class-lists (spec-list precompute-p)
1035 (if (null spec-list)
1036 (list nil)
1037 (let* ((car-all-classes (mec-all-classes (car spec-list)
1038 precompute-p))
1039 (all-class-lists (mec-all-class-lists (cdr spec-list)
1040 precompute-p)))
1041 (mapcan (lambda (list)
1042 (mapcar (lambda (c) (cons c list)) car-all-classes))
1043 all-class-lists))))
1045 (defun make-emf-cache (generic-function valuep cache classes-list new-class)
1046 (let* ((arg-info (gf-arg-info generic-function))
1047 (nkeys (arg-info-nkeys arg-info))
1048 (metatypes (arg-info-metatypes arg-info))
1049 (wrappers (unless (eq nkeys 1) (make-list nkeys)))
1050 (precompute-p (gf-precompute-dfun-and-emf-p arg-info)))
1051 (flet ((add-class-list (classes)
1052 (when (or (null new-class) (memq new-class classes))
1053 (let ((%wrappers (get-wrappers-from-classes
1054 nkeys wrappers classes metatypes)))
1055 (when (and %wrappers (not (probe-cache cache %wrappers)))
1056 (let ((value (cond ((eq valuep t)
1057 (sdfun-for-caching generic-function
1058 classes))
1059 ((eq valuep :constant-value)
1060 (value-for-caching generic-function
1061 classes)))))
1062 ;; need to get them again, as finalization might
1063 ;; have happened in between, which would
1064 ;; invalidate wrappers.
1065 (let ((wrappers (get-wrappers-from-classes
1066 nkeys wrappers classes metatypes)))
1067 (when (if (atom wrappers)
1068 (not (invalid-wrapper-p wrappers))
1069 (every (complement #'invalid-wrapper-p)
1070 wrappers))
1071 (setq cache (fill-cache cache wrappers value))))))))))
1072 (if classes-list
1073 (mapc #'add-class-list classes-list)
1074 (dolist (method (generic-function-methods generic-function))
1075 (mapc #'add-class-list
1076 (mec-all-class-lists (method-specializers method)
1077 precompute-p))))
1078 cache)))
1080 (defmacro class-test (arg class)
1081 (cond
1082 ((eq class *the-class-t*) t)
1083 ((eq class *the-class-slot-object*)
1084 `(not (typep (classoid-of ,arg) 'system-classoid)))
1085 ((eq class *the-class-standard-object*)
1086 `(or (std-instance-p ,arg) (fsc-instance-p ,arg)))
1087 ((eq class *the-class-funcallable-standard-object*)
1088 `(fsc-instance-p ,arg))
1090 `(typep ,arg ',(class-name class)))))
1092 (defmacro class-eq-test (arg class)
1093 `(eq (class-of ,arg) ',class))
1095 (defmacro eql-test (arg object)
1096 `(eql ,arg ',object))
1098 (defun dnet-methods-p (form)
1099 (and (consp form)
1100 (or (eq (car form) 'methods)
1101 (eq (car form) 'unordered-methods))))
1103 ;;; This is CASE, but without gensyms.
1104 (defmacro scase (arg &rest clauses)
1105 `(let ((.case-arg. ,arg))
1106 (cond ,@(mapcar (lambda (clause)
1107 (list* (cond ((null (car clause))
1108 nil)
1109 ((consp (car clause))
1110 (if (null (cdar clause))
1111 `(eql .case-arg.
1112 ',(caar clause))
1113 `(member .case-arg.
1114 ',(car clause))))
1115 ((member (car clause) '(t otherwise))
1118 `(eql .case-arg. ',(car clause))))
1120 (cdr clause)))
1121 clauses))))
1123 (defmacro mcase (arg &rest clauses) `(scase ,arg ,@clauses))
1125 (defun generate-discrimination-net (generic-function methods types sorted-p)
1126 (let* ((arg-info (gf-arg-info generic-function))
1127 (c-a-m-emf-std-p (gf-info-c-a-m-emf-std-p arg-info))
1128 (precedence (arg-info-precedence arg-info)))
1129 (generate-discrimination-net-internal
1130 generic-function methods types
1131 (lambda (methods known-types)
1132 (if (or sorted-p
1133 (and c-a-m-emf-std-p
1134 (block one-order-p
1135 (let ((sorted-methods nil))
1136 (map-all-orders
1137 (copy-list methods) precedence
1138 (lambda (methods)
1139 (when sorted-methods (return-from one-order-p nil))
1140 (setq sorted-methods methods)))
1141 (setq methods sorted-methods))
1142 t)))
1143 `(methods ,methods ,known-types)
1144 `(unordered-methods ,methods ,known-types)))
1145 (lambda (position type true-value false-value)
1146 (let ((arg (dfun-arg-symbol position)))
1147 (if (eq (car type) 'eql)
1148 (let* ((false-case-p (and (consp false-value)
1149 (or (eq (car false-value) 'scase)
1150 (eq (car false-value) 'mcase))
1151 (eq arg (cadr false-value))))
1152 (false-clauses (if false-case-p
1153 (cddr false-value)
1154 `((t ,false-value))))
1155 (case-sym (if (and (dnet-methods-p true-value)
1156 (if false-case-p
1157 (eq (car false-value) 'mcase)
1158 (dnet-methods-p false-value)))
1159 'mcase
1160 'scase))
1161 (type-sym `(,(cadr type))))
1162 `(,case-sym ,arg
1163 (,type-sym ,true-value)
1164 ,@false-clauses))
1165 `(if ,(let ((arg (dfun-arg-symbol position)))
1166 (case (car type)
1167 (class `(class-test ,arg ,(cadr type)))
1168 (class-eq `(class-eq-test ,arg ,(cadr type)))))
1169 ,true-value
1170 ,false-value))))
1171 #'identity)))
1173 (defun class-from-type (type)
1174 (if (or (atom type) (eq (car type) t))
1175 *the-class-t*
1176 (case (car type)
1177 (and (dolist (type (cdr type) *the-class-t*)
1178 (when (and (consp type) (not (eq (car type) 'not)))
1179 (return (class-from-type type)))))
1180 (not *the-class-t*)
1181 (eql (class-of (cadr type)))
1182 (class-eq (cadr type))
1183 (class (cadr type)))))
1185 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1186 (defun augment-type (new-type known-type)
1187 (if (or (eq known-type t)
1188 (eq (car new-type) 'eql))
1189 new-type
1190 (let ((so-far (if (and (consp known-type) (eq (car known-type) 'and))
1191 (cdr known-type)
1192 (list known-type))))
1193 (unless (eq (car new-type) 'not)
1194 (setq so-far
1195 (mapcan (lambda (type)
1196 (unless (*subtypep new-type type)
1197 (list type)))
1198 so-far)))
1199 (if (null so-far)
1200 new-type
1201 `(and ,new-type ,@so-far)))))
1203 (defun generate-discrimination-net-internal
1204 (gf methods types methods-function test-fun type-function)
1205 (let* ((arg-info (gf-arg-info gf))
1206 (precedence (arg-info-precedence arg-info))
1207 (nreq (arg-info-number-required arg-info))
1208 (metatypes (arg-info-metatypes arg-info)))
1209 (labels ((do-column (p-tail contenders known-types)
1210 (if p-tail
1211 (let* ((position (car p-tail))
1212 (known-type (or (nth position types) t)))
1213 (if (eq (nth position metatypes) t)
1214 (do-column (cdr p-tail) contenders
1215 (cons (cons position known-type)
1216 known-types))
1217 (do-methods p-tail contenders
1218 known-type () known-types)))
1219 (funcall methods-function contenders
1220 (let ((k-t (make-list nreq)))
1221 (dolist (index+type known-types)
1222 (setf (nth (car index+type) k-t)
1223 (cdr index+type)))
1224 k-t))))
1225 (do-methods (p-tail contenders known-type winners known-types)
1226 ;; CONTENDERS
1227 ;; is a (sorted) list of methods that must be discriminated.
1228 ;; KNOWN-TYPE
1229 ;; is the type of this argument, constructed from tests
1230 ;; already made.
1231 ;; WINNERS
1232 ;; is a (sorted) list of methods that are potentially
1233 ;; applicable after the discrimination has been made.
1234 (if (null contenders)
1235 (do-column (cdr p-tail)
1236 winners
1237 (cons (cons (car p-tail) known-type)
1238 known-types))
1239 (let* ((position (car p-tail))
1240 (method (car contenders))
1241 (specl (nth position (method-specializers method)))
1242 (type (funcall type-function
1243 (type-from-specializer specl))))
1244 (multiple-value-bind (app-p maybe-app-p)
1245 (specializer-applicable-using-type-p type known-type)
1246 (flet ((determined-to-be (truth-value)
1247 (if truth-value app-p (not maybe-app-p)))
1248 (do-if (truth &optional implied)
1249 (let ((ntype (if truth type `(not ,type))))
1250 (do-methods p-tail
1251 (cdr contenders)
1252 (if implied
1253 known-type
1254 (augment-type ntype known-type))
1255 (if truth
1256 (append winners `(,method))
1257 winners)
1258 known-types))))
1259 (cond ((determined-to-be nil) (do-if nil t))
1260 ((determined-to-be t) (do-if t t))
1261 (t (funcall test-fun position type
1262 (do-if t) (do-if nil))))))))))
1263 (do-column precedence methods ()))))
1265 (defun compute-secondary-dispatch-function (generic-function net &optional
1266 method-alist wrappers)
1267 (function-funcall (compute-secondary-dispatch-function1 generic-function net)
1268 method-alist wrappers))
1270 (defvar *eq-case-table-limit* 15)
1271 (defvar *case-table-limit* 10)
1273 (defun compute-mcase-parameters (case-list)
1274 (unless (eq t (caar (last case-list)))
1275 (error "The key for the last case arg to mcase was not T"))
1276 (let* ((eq-p (dolist (case case-list t)
1277 (unless (or (eq (car case) t)
1278 (symbolp (caar case)))
1279 (return nil))))
1280 (len (1- (length case-list)))
1281 (type (cond ((= len 1)
1282 :simple)
1283 ((<= len
1284 (if eq-p
1285 *eq-case-table-limit*
1286 *case-table-limit*))
1287 :assoc)
1289 :hash-table))))
1290 (list eq-p type)))
1292 (defmacro mlookup (key info default &optional eq-p type)
1293 (unless (or (eq eq-p t) (null eq-p))
1294 (bug "Invalid eq-p argument: ~S" eq-p))
1295 (ecase type
1296 (:simple
1297 `(if (locally
1298 (declare (optimize (inhibit-warnings 3)))
1299 (,(if eq-p 'eq 'eql) ,key (car ,info)))
1300 (cdr ,info)
1301 ,default))
1302 (:assoc
1303 `(dolist (e ,info ,default)
1304 (when (locally
1305 (declare (optimize (inhibit-warnings 3)))
1306 (,(if eq-p 'eq 'eql) (car e) ,key))
1307 (return (cdr e)))))
1308 (:hash-table
1309 `(gethash ,key ,info ,default))))
1311 (defun net-test-converter (form)
1312 (if (atom form)
1313 (default-test-converter form)
1314 (case (car form)
1315 ((invoke-effective-method-function invoke-fast-method-call
1316 invoke-effective-narrow-method-function)
1317 '.call.)
1318 (methods
1319 '.methods.)
1320 (unordered-methods
1321 '.umethods.)
1322 (mcase
1323 `(mlookup ,(cadr form)
1326 ,@(compute-mcase-parameters (cddr form))))
1327 (t (default-test-converter form)))))
1329 (defun net-code-converter (form)
1330 (if (atom form)
1331 (default-code-converter form)
1332 (case (car form)
1333 ((methods unordered-methods)
1334 (let ((gensym (gensym)))
1335 (values gensym
1336 (list gensym))))
1337 (mcase
1338 (let ((mp (compute-mcase-parameters (cddr form)))
1339 (gensym (gensym)) (default (gensym)))
1340 (values `(mlookup ,(cadr form) ,gensym ,default ,@mp)
1341 (list gensym default))))
1343 (default-code-converter form)))))
1345 (defun net-constant-converter (form generic-function)
1346 (or (let ((c (methods-converter form generic-function)))
1347 (when c (list c)))
1348 (if (atom form)
1349 (default-constant-converter form)
1350 (case (car form)
1351 (mcase
1352 (let* ((mp (compute-mcase-parameters (cddr form)))
1353 (list (mapcar (lambda (clause)
1354 (let ((key (car clause))
1355 (meth (cadr clause)))
1356 (cons (if (consp key) (car key) key)
1357 (methods-converter
1358 meth generic-function))))
1359 (cddr form)))
1360 (default (car (last list))))
1361 (list (list* :mcase mp (nbutlast list))
1362 (cdr default))))
1364 (default-constant-converter form))))))
1366 (defun methods-converter (form generic-function)
1367 (cond ((and (consp form) (eq (car form) 'methods))
1368 (cons '.methods.
1369 (get-effective-method-function1 generic-function (cadr form))))
1370 ((and (consp form) (eq (car form) 'unordered-methods))
1371 (default-secondary-dispatch-function generic-function))))
1373 (defun convert-methods (constant method-alist wrappers)
1374 (if (and (consp constant)
1375 (eq (car constant) '.methods.))
1376 (funcall (cdr constant) method-alist wrappers)
1377 constant))
1379 (defun convert-table (constant method-alist wrappers)
1380 (cond ((and (consp constant)
1381 (eq (car constant) :mcase))
1382 (let ((alist (mapcar (lambda (k+m)
1383 (cons (car k+m)
1384 (convert-methods (cdr k+m)
1385 method-alist
1386 wrappers)))
1387 (cddr constant)))
1388 (mp (cadr constant)))
1389 (ecase (cadr mp)
1390 (:simple
1391 (car alist))
1392 (:assoc
1393 alist)
1394 (:hash-table
1395 (let ((table (make-hash-table :test (if (car mp) 'eq 'eql))))
1396 (dolist (k+m alist)
1397 (setf (gethash (car k+m) table) (cdr k+m)))
1398 table)))))))
1400 (defun compute-secondary-dispatch-function1 (generic-function net
1401 &optional function-p)
1402 (cond
1403 ((and (eq (car net) 'methods) (not function-p))
1404 (get-effective-method-function1 generic-function (cadr net)))
1406 (let* ((name (generic-function-name generic-function))
1407 (arg-info (gf-arg-info generic-function))
1408 (metatypes (arg-info-metatypes arg-info))
1409 (nargs (length metatypes))
1410 (applyp (arg-info-applyp arg-info))
1411 (fmc-arg-info (cons nargs applyp))
1412 (arglist (if function-p
1413 (make-dfun-lambda-list nargs applyp)
1414 (make-fast-method-call-lambda-list nargs applyp))))
1415 (multiple-value-bind (cfunction constants)
1416 ;; We don't want NAMED-LAMBDA for any expressions handed to FNGEN,
1417 ;; because name mismatches will render the hashing ineffective.
1418 (get-fun1 `(lambda ,arglist
1419 (declare (optimize (sb-c::store-closure-debug-pointer 3)))
1420 ,@(unless function-p
1421 `((declare (ignore .pv. .next-method-call.))))
1422 (locally (declare #.*optimize-speed*)
1423 (let ((emf ,net))
1424 ,(make-emf-call nargs applyp 'emf))))
1425 #'net-test-converter
1426 #'net-code-converter
1427 (lambda (form)
1428 (net-constant-converter form generic-function)))
1429 (lambda (method-alist wrappers)
1430 (let* ((alist (list nil))
1431 (alist-tail alist))
1432 (dolist (constant constants)
1433 (let* ((a (or (dolist (a alist nil)
1434 (when (eq (car a) constant)
1435 (return a)))
1436 (cons constant
1437 (or (convert-table
1438 constant method-alist wrappers)
1439 (convert-methods
1440 constant method-alist wrappers)))))
1441 (new (list a)))
1442 (setf (cdr alist-tail) new)
1443 (setf alist-tail new)))
1444 (let ((function (apply cfunction (mapcar #'cdr (cdr alist)))))
1445 (if function-p
1446 (set-fun-name function `(gf-dispatch ,name))
1447 (make-fast-method-call
1448 :function (set-fun-name function `(sdfun-method ,name))
1449 :arg-info fmc-arg-info))))))))))
1451 (defvar *show-make-unordered-methods-emf-calls* nil)
1453 (defun make-unordered-methods-emf (generic-function methods)
1454 (when *show-make-unordered-methods-emf-calls*
1455 (format t "~&make-unordered-methods-emf ~S~%"
1456 (generic-function-name generic-function)))
1457 (lambda (&rest args)
1458 (let* ((types (types-from-args generic-function args 'eql))
1459 (smethods (sort-applicable-methods generic-function
1460 methods
1461 types))
1462 (emf (get-effective-method-function generic-function smethods)))
1463 (invoke-emf emf args))))
1465 ;;; The value returned by compute-discriminating-function is a function
1466 ;;; object. It is called a discriminating function because it is called
1467 ;;; when the generic function is called and its role is to discriminate
1468 ;;; on the arguments to the generic function and then call appropriate
1469 ;;; method functions.
1471 ;;; A discriminating function can only be called when it is installed as
1472 ;;; the funcallable instance function of the generic function for which
1473 ;;; it was computed.
1475 ;;; More precisely, if compute-discriminating-function is called with
1476 ;;; an argument <gf1>, and returns a result <df1>, that result must
1477 ;;; not be passed to apply or funcall directly. Rather, <df1> must be
1478 ;;; stored as the funcallable instance function of the same generic
1479 ;;; function <gf1> (using SET-FUNCALLABLE-INSTANCE-FUNCTION). Then the
1480 ;;; generic function can be passed to funcall or apply.
1482 ;;; An important exception is that methods on this generic function are
1483 ;;; permitted to return a function which itself ends up calling the value
1484 ;;; returned by a more specific method. This kind of `encapsulation' of
1485 ;;; discriminating function is critical to many uses of the MOP.
1487 ;;; As an example, the following canonical case is legal:
1489 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1490 ;;; (let ((std (call-next-method)))
1491 ;;; (lambda (arg)
1492 ;;; (print (list 'call-to-gf gf arg))
1493 ;;; (funcall std arg))))
1495 ;;; Because many discriminating functions would like to use a dynamic
1496 ;;; strategy in which the precise discriminating function changes with
1497 ;;; time it is important to specify how a discriminating function is
1498 ;;; permitted itself to change the funcallable instance function of the
1499 ;;; generic function.
1501 ;;; Discriminating functions may set the funcallable instance function
1502 ;;; of the generic function, but the new value must be generated by making
1503 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1504 ;;; more specific methods which may have encapsulated the discriminating
1505 ;;; function will get a chance to encapsulate the new, inner discriminating
1506 ;;; function.
1508 ;;; This implies that if a discriminating function wants to modify itself
1509 ;;; it should first store some information in the generic function proper,
1510 ;;; and then call compute-discriminating-function. The appropriate method
1511 ;;; on compute-discriminating-function will see the information stored in
1512 ;;; the generic function and generate a discriminating function accordingly.
1514 ;;; The following is an example of a discriminating function which modifies
1515 ;;; itself in accordance with this protocol:
1517 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1518 ;;; (lambda (arg)
1519 ;;; (cond (<some condition>
1520 ;;; <store some info in the generic function>
1521 ;;; (set-funcallable-instance-function
1522 ;;; gf
1523 ;;; (compute-discriminating-function gf))
1524 ;;; (funcall gf arg))
1525 ;;; (t
1526 ;;; <call-a-method-of-gf>))))
1528 ;;; Whereas this code would not be legal:
1530 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1531 ;;; (lambda (arg)
1532 ;;; (cond (<some condition>
1533 ;;; (set-funcallable-instance-function
1534 ;;; gf
1535 ;;; (lambda (a) ..))
1536 ;;; (funcall gf arg))
1537 ;;; (t
1538 ;;; <call-a-method-of-gf>))))
1540 ;;; NOTE: All the examples above assume that all instances of the class
1541 ;;; my-generic-function accept only one argument.
1543 (defun slot-value-using-class-dfun (class object slotd)
1544 (declare (ignore class))
1545 (funcall (slot-info-reader (slot-definition-info slotd)) object))
1547 (defun setf-slot-value-using-class-dfun (new-value class object slotd)
1548 (declare (ignore class))
1549 (funcall (slot-info-writer (slot-definition-info slotd)) new-value object))
1551 (defun slot-boundp-using-class-dfun (class object slotd)
1552 (declare (ignore class))
1553 (funcall (slot-info-boundp (slot-definition-info slotd)) object))
1555 (defun special-case-for-compute-discriminating-function-p (gf)
1556 (or (eq gf #'slot-value-using-class)
1557 (eq gf #'(setf slot-value-using-class))
1558 (eq gf #'slot-boundp-using-class)))
1560 ;;; this is the normal function for computing the discriminating
1561 ;;; function of a standard-generic-function
1562 (let (initial-print-object-cache)
1563 (defun standard-compute-discriminating-function (gf)
1564 (let ((dfun-state (slot-value gf 'dfun-state)))
1565 (when (special-case-for-compute-discriminating-function-p gf)
1566 ;; if we have a special case for
1567 ;; COMPUTE-DISCRIMINATING-FUNCTION, then (at least for the
1568 ;; special cases implemented as of 2006-05-09) any information
1569 ;; in the cache is misplaced.
1570 (aver (null dfun-state)))
1571 (typecase dfun-state
1572 (null
1573 (when (eq gf #'compute-applicable-methods)
1574 (update-all-c-a-m-gf-info gf))
1575 (cond
1576 ((eq gf #'slot-value-using-class)
1577 (update-slot-value-gf-info gf 'reader)
1578 #'slot-value-using-class-dfun)
1579 ((eq gf #'(setf slot-value-using-class))
1580 (update-slot-value-gf-info gf 'writer)
1581 #'setf-slot-value-using-class-dfun)
1582 ((eq gf #'slot-boundp-using-class)
1583 (update-slot-value-gf-info gf 'boundp)
1584 #'slot-boundp-using-class-dfun)
1585 ;; KLUDGE: PRINT-OBJECT is not a special-case in the sense
1586 ;; of having a desperately special discriminating function.
1587 ;; However, it is important that the machinery for printing
1588 ;; conditions for stack and heap exhaustion, and the
1589 ;; restarts offered by the debugger, work without consuming
1590 ;; many extra resources. This way (testing by name of GF
1591 ;; rather than by identity) was the only way I found to get
1592 ;; this to bootstrap, given that the PRINT-OBJECT generic
1593 ;; function is only set up later, in
1594 ;; SRC;PCL;PRINT-OBJECT.LISP. -- CSR, 2008-06-09
1595 ((eq (slot-value gf 'name) 'print-object)
1596 (let ((nkeys (nth-value 3 (get-generic-fun-info gf))))
1597 (cond ((/= nkeys 1)
1598 ;; KLUDGE: someone has defined a method
1599 ;; specialized on the second argument: punt.
1600 (setf initial-print-object-cache nil)
1601 (make-initial-dfun gf))
1602 (initial-print-object-cache
1603 (multiple-value-bind (dfun cache info)
1604 (make-caching-dfun gf (copy-cache initial-print-object-cache))
1605 (set-dfun gf dfun cache info)))
1606 ;; the relevant PRINT-OBJECT methods get defined
1607 ;; late, by delayed DEFMETHOD. We mustn't cache
1608 ;; the effective method for our classes earlier
1609 ;; than the relevant PRINT-OBJECT methods are
1610 ;; defined...
1611 ((boundp '*!delayed-defmethod-args*)
1612 (make-initial-dfun gf))
1613 (t (multiple-value-bind (dfun cache info)
1614 (make-final-dfun-internal
1616 (mapcar (lambda (x) (list (find-class x)))
1617 '(sb-kernel::control-stack-exhausted
1618 sb-kernel::binding-stack-exhausted
1619 sb-kernel::alien-stack-exhausted
1620 sb-kernel::heap-exhausted-error
1621 restart)))
1622 (setq initial-print-object-cache cache)
1623 (set-dfun gf dfun (copy-cache cache) info))))))
1624 ((gf-precompute-dfun-and-emf-p (slot-value gf 'arg-info))
1625 (make-final-dfun gf))
1627 (make-initial-dfun gf))))
1628 (function dfun-state)
1629 (cons (car dfun-state))))))
1631 ;;; in general we need to support SBCL's encapsulation for generic
1632 ;;; functions: the default implementation of encapsulation changes the
1633 ;;; identity of the function bound to a name, which breaks anything
1634 ;;; class-based, so we implement the encapsulation ourselves in the
1635 ;;; discriminating function.
1636 (defun sb-impl::encapsulate-generic-function (gf type function)
1637 (push (cons type function) (generic-function-encapsulations gf))
1638 (reinitialize-instance gf))
1640 (defun sb-impl::unencapsulate-generic-function (gf type)
1641 (setf (generic-function-encapsulations gf)
1642 (remove type (generic-function-encapsulations gf)
1643 :key #'car :count 1))
1644 (reinitialize-instance gf))
1645 (defun sb-impl::encapsulated-generic-function-p (gf type)
1646 (position type (generic-function-encapsulations gf) :key #'car))
1647 (defun maybe-encapsulate-discriminating-function (gf encs std)
1648 (if (null encs)
1650 (let ((inner (maybe-encapsulate-discriminating-function
1651 gf (cdr encs) std))
1652 (function (cdar encs)))
1653 (lambda (&rest args)
1654 (apply function inner args)))))
1655 (defmethod compute-discriminating-function ((gf standard-generic-function))
1656 (standard-compute-discriminating-function gf))
1657 (defmethod compute-discriminating-function :around ((gf standard-generic-function))
1658 (maybe-encapsulate-discriminating-function
1659 gf (generic-function-encapsulations gf) (call-next-method)))
1661 (defmethod (setf class-name) (new-value class)
1662 (let ((classoid (layout-classoid (class-wrapper class))))
1663 (if (and new-value (symbolp new-value))
1664 (setf (classoid-name classoid) new-value)
1665 (setf (classoid-name classoid) nil)))
1666 (reinitialize-instance class :name new-value)
1667 new-value)
1669 (defmethod (setf generic-function-name) (new-value generic-function)
1670 (reinitialize-instance generic-function :name new-value)
1671 new-value)
1673 (defmethod function-keywords ((method standard-method))
1674 (multiple-value-bind (llks nreq nopt keywords)
1675 (analyze-lambda-list (if (consp method)
1676 (early-method-lambda-list method)
1677 (method-lambda-list method)))
1678 (declare (ignore nreq nopt))
1679 (values keywords (ll-kwds-allowp llks))))
1681 ;;; This is based on the rules of method lambda list congruency
1682 ;;; defined in the spec. The lambda list it constructs is the pretty
1683 ;;; union of the lambda lists of the generic function and of all its
1684 ;;; methods. It doesn't take method applicability into account; we
1685 ;;; also ignore non-public parts of the interface (e.g. &AUX, default
1686 ;;; and supplied-p parameters)
1687 ;;; The compiler uses this for type-checking that callers pass acceptable
1688 ;;; keywords, so don't make this do anything fancy like looking at effective
1689 ;;; methods without also fixing the compiler.
1690 (defmethod generic-function-pretty-arglist ((gf standard-generic-function))
1691 (let ((gf-lambda-list (generic-function-lambda-list gf))
1692 (methods (generic-function-methods gf)))
1693 (flet ((canonize (k)
1694 (multiple-value-bind (kw var)
1695 (parse-key-arg-spec k)
1696 (if (and (eql (symbol-package kw) *keyword-package*)
1697 (string= kw var))
1699 (list (list kw var))))))
1700 (multiple-value-bind (llks required optional rest keys)
1701 (parse-lambda-list gf-lambda-list :silent t)
1702 (collect ((keys (mapcar #'canonize keys)))
1703 ;; Possibly extend the keyword parameters of the gf by
1704 ;; additional key parameters of its methods:
1705 (dolist (m methods
1706 (make-lambda-list llks nil required optional rest (keys)))
1707 (binding* (((m.llks nil nil nil m.keys)
1708 (parse-lambda-list (method-lambda-list m) :silent t)))
1709 (setq llks (logior llks m.llks))
1710 (dolist (k m.keys)
1711 (unless (member (parse-key-arg-spec k) (keys)
1712 :key #'parse-key-arg-spec :test #'eq)
1713 (keys (canonize k)))))))))))