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