Use DEFINE-LOAD-TIME-GLOBAL for a bunch of PCL global state
[sbcl.git] / src / pcl / methods.lisp
blob005168667bfc08507e722995b937ea0bb7f67e99
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 (define-load-time-global *the-class-generic-function*
171 (find-class 'generic-function))
172 (define-load-time-global *the-class-standard-generic-function*
173 (find-class 'standard-generic-function))
175 (defmethod shared-initialize :before
176 ((generic-function standard-generic-function)
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 ((specializer-count (length specializers))
257 (methods (generic-function-methods generic-function)))
258 (when (or methods always-check-specializers)
259 (let ((required-parameter-count
260 (length (arg-info-metatypes (gf-arg-info generic-function)))))
261 ;; Since we internally bypass FIND-METHOD by using GET-METHOD
262 ;; instead we need to do this here or users may get hit by a
263 ;; failed AVER instead of a sensible error message.
264 (unless (= specializer-count required-parameter-count)
265 (error
266 'find-method-length-mismatch
267 :format-control "~@<The generic function ~S takes ~D ~
268 required argument~:P; was asked to ~
269 find a method with specializers ~:S~@:>"
270 :format-arguments (list generic-function required-parameter-count
271 (unparse-specializers generic-function specializers))))))
272 (flet ((congruentp (other-method)
273 (let ((other-specializers (method-specializers other-method)))
274 (aver (= specializer-count (length other-specializers)))
275 (and (equal qualifiers (safe-method-qualifiers other-method))
276 (every #'same-specializer-p specializers other-specializers)))))
277 (declare (dynamic-extent #'congruentp))
278 (cond ((find-if #'congruentp methods))
279 ((null errorp) nil)
281 (error "~@<There is no method on ~S with ~:[no ~
282 qualifiers~;~:*qualifiers ~:S~] and specializers ~
283 ~: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 ;; FIXME: Because ARG-INFO is a STRUCTURE-OBJECT, it does not get
400 ;; a permutation vector, and therefore the code that SLOT-VALUE transforms
401 ;; to winds up punting to #'(SLOT-ACCESSOR :GLOBAL ARG-INFO READER).
402 ;; Using SLOT-VALUE the "slow" way sidesteps some bootstrap issues.
403 (declare (notinline slot-value))
404 (progn ; WAS: with-slots (arg-info) gf
405 (if lambda-list-p
406 (set-arg-info gf
407 :lambda-list lambda-list
408 :argument-precedence-order argument-precedence-order)
409 (set-arg-info gf))
410 (when (arg-info-valid-p (slot-value gf 'arg-info))
411 (update-dfun gf))))
413 (defmethod reinitialize-instance :around
414 ((gf standard-generic-function) &rest args &key
415 (lambda-list nil lambda-list-p) (argument-precedence-order nil apo-p))
416 (let ((old-mc (generic-function-method-combination gf)))
417 (prog1 (call-next-method)
418 ;; KLUDGE: EQ is too strong a test.
419 (unless (eq old-mc (generic-function-method-combination gf))
420 (flush-effective-method-cache gf))
421 (cond
422 ((and lambda-list-p apo-p)
423 (set-arg-info gf
424 :lambda-list lambda-list
425 :argument-precedence-order argument-precedence-order))
426 (lambda-list-p (set-arg-info gf :lambda-list lambda-list))
427 (t (set-arg-info gf)))
428 (when (arg-info-valid-p (gf-arg-info gf))
429 (update-dfun gf))
430 (map-dependents gf (lambda (dependent)
431 (apply #'update-dependent gf dependent args))))))
433 (declaim (special *lazy-dfun-compute-p*))
435 (defun set-methods (gf methods)
436 (setf (generic-function-methods gf) nil)
437 (loop (when (null methods) (return gf))
438 (real-add-method gf (pop methods) methods)))
440 (define-condition new-value-specialization (reference-condition error)
441 ((%method :initarg :method :reader new-value-specialization-method))
442 (:report
443 (lambda (c s)
444 (format s "~@<Cannot add method ~S to ~S, as it specializes the ~
445 new-value argument.~@:>"
446 (new-value-specialization-method c)
447 #'(setf slot-value-using-class))))
448 (:default-initargs :references
449 (list '(:sbcl :node "Metaobject Protocol")
450 '(:amop :generic-function (setf slot-value-using-class)))))
452 (defgeneric values-for-add-method (gf method)
453 (:method ((gf standard-generic-function) (method standard-method))
454 ;; KLUDGE: Just a single generic dispatch, and everything else
455 ;; comes from permutation vectors. Would be nicer to define
456 ;; REAL-ADD-METHOD with a proper method so that we could efficiently
457 ;; use SLOT-VALUE there.
459 ;; Optimization note: REAL-ADD-METHOD has a lot of O(N) stuff in it (as
460 ;; does PCL as a whole). It should not be too hard to internally store
461 ;; many of the things we now keep in lists as either purely functional
462 ;; O(log N) sets, or --if we don't mind the memory cost-- using
463 ;; specialized hash-tables: most things are used to answer questions about
464 ;; set-membership, not ordering.
465 (values (slot-value gf '%lock)
466 (slot-value method 'qualifiers)
467 (slot-value method 'specializers)
468 (slot-value method 'lambda-list)
469 (slot-value method '%generic-function)
470 (slot-value gf 'name))))
472 (define-condition print-object-stream-specializer (reference-condition simple-warning)
474 (:default-initargs
475 :references (list '(:ansi-cl :function print-object))
476 :format-control "~@<Specializing on the second argument to ~S has ~
477 unportable effects, and also interferes with ~
478 precomputation of print functions for exceptional ~
479 situations.~@:>"
480 :format-arguments (list 'print-object)))
482 (defun defer-ftype-computation (gf)
483 ;; Is there any reason not to do this as soon as possible?
484 ;; While doing it with every ADD/REMOVE-METHOD call could result in
485 ;; wasted work, it seems like unnecessary complexity.
486 ;; I think it's just to get through bootstrap, probably,
487 ;; but if it's a semantics thing, it deserves some explanation.
488 (let ((name (generic-function-name gf)))
489 (when (legal-fun-name-p name) ; tautological ?
490 (unless (eq (info :function :where-from name) :declared)
491 (when (and (fboundp name) (eq (fdefinition name) gf))
492 (setf (info :function :type name) :generic-function))))))
494 (defun compute-gf-ftype (name)
495 (let ((gf (and (fboundp name) (fdefinition name))))
496 (if (generic-function-p gf)
497 (let* ((ll (generic-function-lambda-list gf))
498 ;; If the GF has &REST without &KEY then we don't augment
499 ;; the FTYPE with keywords, so as not to complain about keywords
500 ;; which seem not to be accepted.
501 (type (sb-c::ftype-from-lambda-list
502 (if (and (member '&rest ll) (not (member '&key ll)))
504 (generic-function-pretty-arglist gf)))))
505 ;; It would be nice if globaldb were transactional,
506 ;; so that either both updates or neither occur.
507 (setf (info :function :type name) type
508 (info :function :where-from name) :defined-method)
509 type)
510 ;; The defaulting expression for (:FUNCTION :TYPE) does not store
511 ;; the default. For :GENERIC-FUNCTION that is not FBOUNDP we also
512 ;; don't, however this branch should never be reached because the
513 ;; info only stores :GENERIC-FUNCTION when methods are loaded.
514 ;; Maybe AVER that it does not happen?
515 (sb-c::ftype-from-fdefn name))))
517 (defun real-add-method (generic-function method &optional skip-dfun-update-p)
518 (flet ((similar-lambda-lists-p (old-method new-lambda-list)
519 (binding* (((a-llks a-nreq a-nopt)
520 (analyze-lambda-list (method-lambda-list old-method)))
521 ((b-llks b-nreq b-nopt)
522 (analyze-lambda-list new-lambda-list)))
523 (and (= a-nreq b-nreq)
524 (= a-nopt b-nopt)
525 (eq (ll-keyp-or-restp a-llks)
526 (ll-keyp-or-restp b-llks))))))
527 (multiple-value-bind (lock qualifiers specializers new-lambda-list
528 method-gf name)
529 (values-for-add-method generic-function method)
530 (when method-gf
531 (error "~@<The method ~S is already part of the generic ~
532 function ~S; it can't be added to another generic ~
533 function until it is removed from the first one.~@:>"
534 method method-gf))
535 (when (and (eq name 'print-object) (not (eq (second specializers) *the-class-t*)))
536 (warn 'print-object-stream-specializer))
537 (handler-case
538 ;; System lock because interrupts need to be disabled as
539 ;; well: it would be bad to unwind and leave the gf in an
540 ;; inconsistent state.
541 (sb-thread::with-recursive-system-lock (lock)
542 (let ((existing (get-method generic-function
543 qualifiers
544 specializers
545 nil)))
547 ;; If there is already a method like this one then we must get
548 ;; rid of it before proceeding. Note that we call the generic
549 ;; function REMOVE-METHOD to remove it rather than doing it in
550 ;; some internal way.
551 (when (and existing (similar-lambda-lists-p existing new-lambda-list))
552 (remove-method generic-function existing))
554 ;; KLUDGE: We have a special case here, as we disallow
555 ;; specializations of the NEW-VALUE argument to (SETF
556 ;; SLOT-VALUE-USING-CLASS). GET-ACCESSOR-METHOD-FUNCTION is
557 ;; the optimizing function here: it precomputes the effective
558 ;; method, assuming that there is no dispatch to be done on
559 ;; the new-value argument.
560 (when (and (eq generic-function #'(setf slot-value-using-class))
561 (not (eq *the-class-t* (first specializers))))
562 (error 'new-value-specialization :method method))
564 (setf (method-generic-function method) generic-function)
565 (pushnew method (generic-function-methods generic-function) :test #'eq)
566 (dolist (specializer specializers)
567 (add-direct-method specializer method))
569 ;; KLUDGE: SET-ARG-INFO contains the error-detecting logic for
570 ;; detecting attempts to add methods with incongruent lambda
571 ;; lists. However, according to Gerd Moellmann on cmucl-imp,
572 ;; it also depends on the new method already having been added
573 ;; to the generic function. Therefore, we need to remove it
574 ;; again on error:
575 (let ((remove-again-p t))
576 (unwind-protect
577 (progn
578 (set-arg-info generic-function :new-method method)
579 (setq remove-again-p nil))
580 (when remove-again-p
581 (remove-method generic-function method))))
583 ;; KLUDGE II: ANSI saith that it is not an error to add a
584 ;; method with invalid qualifiers to a generic function of the
585 ;; wrong kind; it's only an error at generic function
586 ;; invocation time; I dunno what the rationale was, and it
587 ;; sucks. Nevertheless, it's probably a programmer error, so
588 ;; let's warn anyway. -- CSR, 2003-08-20
589 (let* ((mc (generic-function-method-combination generic-function))
590 (type-name (method-combination-type-name mc)))
591 (flet ((invalid ()
592 (warn "~@<Invalid qualifiers for ~S method ~
593 combination in method ~S:~2I~_~S.~@:>"
594 type-name method qualifiers)))
595 (cond
596 ((and (eq mc *standard-method-combination*)
597 qualifiers
598 (or (cdr qualifiers)
599 (not (standard-method-combination-qualifier-p
600 (car qualifiers)))))
601 (invalid))
602 ((and (short-method-combination-p mc)
603 (or (null qualifiers)
604 (cdr qualifiers)
605 (not (short-method-combination-qualifier-p
606 type-name (car qualifiers)))))
607 (invalid)))))
608 (unless skip-dfun-update-p
609 (update-ctors 'add-method
610 :generic-function generic-function
611 :method method)
612 (update-dfun generic-function))
613 (defer-ftype-computation generic-function)
614 (map-dependents generic-function
615 (lambda (dep)
616 (update-dependent generic-function
617 dep 'add-method method)))))
618 (serious-condition (c)
619 (error c)))))
620 generic-function)
622 (defun real-remove-method (generic-function method)
623 (when (eq generic-function (method-generic-function method))
624 (flush-effective-method-cache generic-function)
625 (let ((lock (gf-lock generic-function)))
626 ;; System lock because interrupts need to be disabled as well:
627 ;; it would be bad to unwind and leave the gf in an inconsistent
628 ;; state.
629 (sb-thread::with-recursive-system-lock (lock)
630 (let* ((specializers (method-specializers method))
631 (methods (generic-function-methods generic-function))
632 (new-methods (remove method methods)))
633 (setf (method-generic-function method) nil
634 (generic-function-methods generic-function) new-methods)
635 (dolist (specializer specializers)
636 (remove-direct-method specializer method))
637 (set-arg-info generic-function)
638 (update-ctors 'remove-method
639 :generic-function generic-function
640 :method method)
641 (update-dfun generic-function)
642 (defer-ftype-computation generic-function)
643 (map-dependents generic-function
644 (lambda (dep)
645 (update-dependent generic-function
646 dep 'remove-method method)))))))
647 generic-function)
649 (defun compute-applicable-methods-function (generic-function arguments)
650 (values (compute-applicable-methods-using-types
651 generic-function
652 (types-from-args generic-function arguments 'eql))))
654 (defmethod compute-applicable-methods
655 ((generic-function generic-function) arguments)
656 (values (compute-applicable-methods-using-types
657 generic-function
658 (types-from-args generic-function arguments 'eql))))
660 (defmethod compute-applicable-methods-using-classes
661 ((generic-function generic-function) classes)
662 (compute-applicable-methods-using-types
663 generic-function
664 (types-from-args generic-function classes 'class-eq)))
666 (defun !proclaim-incompatible-superclasses (classes)
667 (setq classes (mapcar (lambda (class)
668 (if (symbolp class)
669 (find-class class)
670 class))
671 classes))
672 (dolist (class classes)
673 (dolist (other-class classes)
674 (unless (eq class other-class)
675 (pushnew other-class (class-incompatible-superclass-list class) :test #'eq)))))
677 (defun superclasses-compatible-p (class1 class2)
678 (let ((cpl1 (cpl-or-nil class1))
679 (cpl2 (cpl-or-nil class2)))
680 (dolist (sc1 cpl1 t)
681 (dolist (ic (class-incompatible-superclass-list sc1))
682 (when (memq ic cpl2)
683 (return-from superclasses-compatible-p nil))))))
685 (mapc
686 #'!proclaim-incompatible-superclasses
687 '(;; superclass class
688 (system-class std-class structure-class) ; direct subclasses of pcl-class
689 (standard-class funcallable-standard-class)
690 ;; superclass metaobject
691 (class eql-specializer class-eq-specializer method method-combination
692 generic-function slot-definition)
693 ;; metaclass built-in-class
694 (number sequence character ; direct subclasses of t, but not array
695 standard-object structure-object) ; or symbol
696 (number array character symbol ; direct subclasses of t, but not
697 standard-object structure-object) ; sequence
698 (complex float rational) ; direct subclasses of number
699 (integer ratio) ; direct subclasses of rational
700 (list vector) ; direct subclasses of sequence
701 (cons null) ; direct subclasses of list
702 (string bit-vector) ; direct subclasses of vector
705 (defmethod same-specializer-p ((specl1 specializer) (specl2 specializer))
706 (eql specl1 specl2))
708 (defmethod same-specializer-p ((specl1 class) (specl2 class))
709 (eq specl1 specl2))
711 (defmethod specializer-class ((specializer class))
712 specializer)
714 (defmethod same-specializer-p ((specl1 class-eq-specializer)
715 (specl2 class-eq-specializer))
716 (eq (specializer-class specl1) (specializer-class specl2)))
718 ;; FIXME: This method is wacky, and indicative of a coding style in which
719 ;; metaphorically the left hand does not know what the right is doing.
720 ;; If you want this to be the abstract comparator, and you "don't know"
721 ;; that EQL-specializers are interned, then the comparator should be EQL.
722 ;; But if you *do* know that they're interned, then why does this method
723 ;; exist at all? The method on SPECIALIZER works fine.
724 (defmethod same-specializer-p ((specl1 eql-specializer)
725 (specl2 eql-specializer))
726 ;; A bit of deception to confuse the enemy?
727 (eq (specializer-object specl1) (specializer-object specl2)))
729 (defmethod specializer-class ((specializer eql-specializer))
730 (class-of (slot-value specializer 'object)))
732 (defun specializer-class-or-nil (specializer)
733 (and (standard-specializer-p specializer)
734 (specializer-class specializer)))
736 (defun error-need-at-least-n-args (function n)
737 (error 'simple-program-error
738 :format-control "~@<The function ~2I~_~S ~I~_requires ~
739 at least ~W argument~:P.~:>"
740 :format-arguments (list function n)))
742 (defun types-from-args (generic-function arguments &optional type-modifier)
743 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
744 (get-generic-fun-info generic-function)
745 (declare (ignore applyp metatypes nkeys))
746 (let ((types-rev nil))
747 (dotimes-fixnum (i nreq)
748 (unless arguments
749 (error-need-at-least-n-args (generic-function-name generic-function)
750 nreq))
751 (let ((arg (pop arguments)))
752 (push (if type-modifier `(,type-modifier ,arg) arg) types-rev)))
753 (values (nreverse types-rev) arg-info))))
755 (defun get-wrappers-from-classes (nkeys wrappers classes metatypes)
756 (let* ((w wrappers) (w-tail w) (mt-tail metatypes))
757 (dolist (class (ensure-list classes))
758 (unless (eq t (car mt-tail))
759 (let ((c-w (class-wrapper class)))
760 (unless c-w (return-from get-wrappers-from-classes nil))
761 (if (eql nkeys 1)
762 (setq w c-w)
763 (setf (car w-tail) c-w
764 w-tail (cdr w-tail)))))
765 (setq mt-tail (cdr mt-tail)))
768 (defun sdfun-for-caching (gf classes)
769 (let ((types (mapcar #'class-eq-type classes)))
770 (multiple-value-bind (methods all-applicable-and-sorted-p)
771 (compute-applicable-methods-using-types gf types)
772 (let ((generator (get-secondary-dispatch-function1
773 gf methods types nil t all-applicable-and-sorted-p)))
774 (make-callable gf methods generator
775 nil (mapcar #'class-wrapper classes))))))
777 (defun value-for-caching (gf classes)
778 (let ((methods (compute-applicable-methods-using-types
779 gf (mapcar #'class-eq-type classes))))
780 (method-plist-value (car methods) :constant-value)))
782 (defun default-secondary-dispatch-function (generic-function)
783 (lambda (&rest args)
784 (let ((methods (compute-applicable-methods generic-function args)))
785 (if methods
786 (let ((emf (get-effective-method-function generic-function
787 methods)))
788 (invoke-emf emf args))
789 (call-no-applicable-method generic-function args)))))
791 (defun list-eq (x y)
792 (loop (when (atom x) (return (eq x y)))
793 (when (atom y) (return nil))
794 (unless (eq (car x) (car y)) (return nil))
795 (setq x (cdr x)
796 y (cdr y))))
798 (define-load-time-global *std-cam-methods* nil)
800 (defun compute-applicable-methods-emf (generic-function)
801 (if (eq **boot-state** 'complete)
802 (let* ((cam (gdefinition 'compute-applicable-methods))
803 (cam-methods (compute-applicable-methods-using-types
804 cam (list `(eql ,generic-function) t))))
805 (values (get-effective-method-function cam cam-methods)
806 (list-eq cam-methods
807 (or *std-cam-methods*
808 (setq *std-cam-methods*
809 (compute-applicable-methods-using-types
810 cam (list `(eql ,cam) t)))))))
811 (values #'compute-applicable-methods-function t)))
813 (defun compute-applicable-methods-emf-std-p (gf)
814 (gf-info-c-a-m-emf-std-p (gf-arg-info gf)))
816 (defvar *old-c-a-m-gf-methods* nil)
818 (defun update-all-c-a-m-gf-info (c-a-m-gf)
819 (let ((methods (generic-function-methods c-a-m-gf)))
820 (if (and *old-c-a-m-gf-methods*
821 (every (lambda (old-method)
822 (member old-method methods :test #'eq))
823 *old-c-a-m-gf-methods*))
824 (let ((gfs-to-do nil)
825 (gf-classes-to-do nil))
826 (dolist (method methods)
827 (unless (member method *old-c-a-m-gf-methods* :test #'eq)
828 (let ((specl (car (method-specializers method))))
829 (if (eql-specializer-p specl)
830 (pushnew (specializer-object specl) gfs-to-do :test #'eq)
831 (pushnew (specializer-class specl) gf-classes-to-do :test #'eq)))))
832 (map-all-generic-functions
833 (lambda (gf)
834 (when (or (member gf gfs-to-do :test #'eq)
835 (dolist (class gf-classes-to-do nil)
836 (member class
837 (class-precedence-list (class-of gf))
838 :test #'eq)))
839 (update-c-a-m-gf-info gf)))))
840 (map-all-generic-functions #'update-c-a-m-gf-info))
841 (setq *old-c-a-m-gf-methods* methods)))
843 (defun update-gf-info (gf)
844 (update-c-a-m-gf-info gf)
845 (update-gf-simple-accessor-type gf))
847 (defun update-c-a-m-gf-info (gf)
848 (unless (early-gf-p gf)
849 (multiple-value-bind (c-a-m-emf std-p)
850 (compute-applicable-methods-emf gf)
851 (let ((arg-info (gf-arg-info gf)))
852 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
853 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)))))
855 (defun update-gf-simple-accessor-type (gf)
856 (let ((arg-info (gf-arg-info gf)))
857 (setf (gf-info-simple-accessor-type arg-info)
858 (let* ((methods (generic-function-methods gf))
859 (class (and methods (class-of (car methods))))
860 (type
861 (and class
862 (cond ((or (eq class *the-class-standard-reader-method*)
863 (eq class *the-class-global-reader-method*))
864 'reader)
865 ((or (eq class *the-class-standard-writer-method*)
866 (eq class *the-class-global-writer-method*))
867 'writer)
868 ((or (eq class *the-class-standard-boundp-method*)
869 (eq class *the-class-global-boundp-method*))
870 'boundp)))))
871 (when (and (gf-info-c-a-m-emf-std-p arg-info)
872 type
873 (dolist (method (cdr methods) t)
874 (unless (eq class (class-of method)) (return nil)))
875 (eq (generic-function-method-combination gf)
876 *standard-method-combination*))
877 type)))))
880 ;;; CMUCL (Gerd's PCL, 2002-04-25) comment:
882 ;;; Return two values. First value is a function to be stored in
883 ;;; effective slot definition SLOTD for reading it with
884 ;;; SLOT-VALUE-USING-CLASS, setting it with (SETF
885 ;;; SLOT-VALUE-USING-CLASS) or testing it with
886 ;;; SLOT-BOUNDP-USING-CLASS. GF is one of these generic functions,
887 ;;; TYPE is one of the symbols READER, WRITER, BOUNDP. CLASS is
888 ;;; SLOTD's class.
890 ;;; Second value is true if the function returned is one of the
891 ;;; optimized standard functions for the purpose, which are used
892 ;;; when only standard methods are applicable.
894 ;;; FIXME: Change all these wacky function names to something sane.
895 (defun get-accessor-method-function (gf type class slotd)
896 (let* ((std-method (standard-svuc-method type))
897 (str-method (structure-svuc-method type))
898 (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
899 (types (if (eq type 'writer) `(t ,@types1) types1))
900 (methods (compute-applicable-methods-using-types gf types))
901 (std-p (null (cdr methods))))
902 (values
903 (if std-p
904 (get-optimized-std-accessor-method-function class slotd type)
905 (let* ((optimized-std-fun
906 (get-optimized-std-slot-value-using-class-method-function
907 class slotd type))
908 (method-alist
909 `((,(car (or (member std-method methods :test #'eq)
910 (member str-method methods :test #'eq)
911 (bug "error in ~S"
912 'get-accessor-method-function)))
913 ,optimized-std-fun)))
914 (wrappers
915 (let ((wrappers (list (layout-of class)
916 (class-wrapper class)
917 (layout-of slotd))))
918 (if (eq type 'writer)
919 (cons (class-wrapper *the-class-t*) wrappers)
920 wrappers)))
921 (sdfun (get-secondary-dispatch-function
922 gf methods types method-alist wrappers)))
923 (get-accessor-from-svuc-method-function class slotd sdfun type)))
924 std-p)))
926 ;;; used by OPTIMIZE-SLOT-VALUE-BY-CLASS-P (vector.lisp)
927 (defun update-slot-value-gf-info (gf type)
928 (unless *new-class*
929 (update-std-or-str-methods gf type))
930 (when (and (standard-svuc-method type) (structure-svuc-method type))
931 (flet ((update-accessor-info (class)
932 (when (class-finalized-p class)
933 (dolist (slotd (class-slots class))
934 (compute-slot-accessor-info slotd type gf)))))
935 (if *new-class*
936 (update-accessor-info *new-class*)
937 (map-all-classes #'update-accessor-info 'slot-object)))))
939 (define-load-time-global *standard-slot-value-using-class-method* nil)
940 (define-load-time-global *standard-setf-slot-value-using-class-method* nil)
941 (define-load-time-global *standard-slot-boundp-using-class-method* nil)
942 (define-load-time-global *condition-slot-value-using-class-method* nil)
943 (define-load-time-global *condition-setf-slot-value-using-class-method* nil)
944 (define-load-time-global *condition-slot-boundp-using-class-method* nil)
945 (define-load-time-global *structure-slot-value-using-class-method* nil)
946 (define-load-time-global *structure-setf-slot-value-using-class-method* nil)
947 (define-load-time-global *structure-slot-boundp-using-class-method* nil)
949 (defun standard-svuc-method (type)
950 (case type
951 (reader *standard-slot-value-using-class-method*)
952 (writer *standard-setf-slot-value-using-class-method*)
953 (boundp *standard-slot-boundp-using-class-method*)))
955 (defun set-standard-svuc-method (type method)
956 (case type
957 (reader (setq *standard-slot-value-using-class-method* method))
958 (writer (setq *standard-setf-slot-value-using-class-method* method))
959 (boundp (setq *standard-slot-boundp-using-class-method* method))))
961 (defun condition-svuc-method (type)
962 (case type
963 (reader *condition-slot-value-using-class-method*)
964 (writer *condition-setf-slot-value-using-class-method*)
965 (boundp *condition-slot-boundp-using-class-method*)))
967 (defun set-condition-svuc-method (type method)
968 (case type
969 (reader (setq *condition-slot-value-using-class-method* method))
970 (writer (setq *condition-setf-slot-value-using-class-method* method))
971 (boundp (setq *condition-slot-boundp-using-class-method* method))))
973 (defun structure-svuc-method (type)
974 (case type
975 (reader *structure-slot-value-using-class-method*)
976 (writer *structure-setf-slot-value-using-class-method*)
977 (boundp *structure-slot-boundp-using-class-method*)))
979 (defun set-structure-svuc-method (type method)
980 (case type
981 (reader (setq *structure-slot-value-using-class-method* method))
982 (writer (setq *structure-setf-slot-value-using-class-method* method))
983 (boundp (setq *structure-slot-boundp-using-class-method* method))))
985 (defun update-std-or-str-methods (gf type)
986 (dolist (method (generic-function-methods gf))
987 (let ((specls (method-specializers method)))
988 (when (and (or (not (eq type 'writer))
989 (eq (pop specls) *the-class-t*))
990 (every #'classp specls))
991 (cond ((and (eq (class-name (car specls)) 'std-class)
992 (eq (class-name (cadr specls)) 'standard-object)
993 (eq (class-name (caddr specls))
994 'standard-effective-slot-definition))
995 (set-standard-svuc-method type method))
996 ((and (eq (class-name (car specls)) 'condition-class)
997 (eq (class-name (cadr specls)) 'condition)
998 (eq (class-name (caddr specls))
999 'condition-effective-slot-definition))
1000 (set-condition-svuc-method type method))
1001 ((and (eq (class-name (car specls)) 'structure-class)
1002 (eq (class-name (cadr specls)) 'structure-object)
1003 (eq (class-name (caddr specls))
1004 'structure-effective-slot-definition))
1005 (set-structure-svuc-method type method)))))))
1007 (defun mec-all-classes-internal (spec precompute-p)
1008 (let ((wrapper (class-wrapper (specializer-class spec))))
1009 (unless (or (not wrapper) (invalid-wrapper-p wrapper))
1010 (cons (specializer-class spec)
1011 (and (classp spec)
1012 precompute-p
1013 (not (or (eq spec *the-class-t*)
1014 (eq spec *the-class-slot-object*)
1015 (eq spec *the-class-standard-object*)
1016 (eq spec *the-class-structure-object*)))
1017 (let ((sc (class-direct-subclasses spec)))
1018 (when sc
1019 (mapcan (lambda (class)
1020 (mec-all-classes-internal class precompute-p))
1021 sc))))))))
1023 (defun mec-all-classes (spec precompute-p)
1024 (let ((classes (mec-all-classes-internal spec precompute-p)))
1025 (if (null (cdr classes))
1026 classes
1027 (let* ((a-classes (cons nil classes))
1028 (tail classes))
1029 (loop (when (null (cdr tail))
1030 (return (cdr a-classes)))
1031 (let ((class (cadr tail))
1032 (ttail (cddr tail)))
1033 (if (dolist (c ttail nil)
1034 (when (eq class c) (return t)))
1035 (setf (cdr tail) (cddr tail))
1036 (setf tail (cdr tail)))))))))
1038 (defun mec-all-class-lists (spec-list precompute-p)
1039 (if (null spec-list)
1040 (list nil)
1041 (let* ((car-all-classes (mec-all-classes (car spec-list)
1042 precompute-p))
1043 (all-class-lists (mec-all-class-lists (cdr spec-list)
1044 precompute-p)))
1045 (mapcan (lambda (list)
1046 (mapcar (lambda (c) (cons c list)) car-all-classes))
1047 all-class-lists))))
1049 (defun make-emf-cache (generic-function valuep cache classes-list new-class)
1050 (let* ((arg-info (gf-arg-info generic-function))
1051 (nkeys (arg-info-nkeys arg-info))
1052 (metatypes (arg-info-metatypes arg-info))
1053 (wrappers (unless (eq nkeys 1) (make-list nkeys)))
1054 (precompute-p (gf-precompute-dfun-and-emf-p arg-info)))
1055 (flet ((add-class-list (classes)
1056 (when (or (null new-class) (memq new-class classes))
1057 (let ((%wrappers (get-wrappers-from-classes
1058 nkeys wrappers classes metatypes)))
1059 (when (and %wrappers (not (probe-cache cache %wrappers)))
1060 (let ((value (cond ((eq valuep t)
1061 (sdfun-for-caching generic-function
1062 classes))
1063 ((eq valuep :constant-value)
1064 (value-for-caching generic-function
1065 classes)))))
1066 ;; need to get them again, as finalization might
1067 ;; have happened in between, which would
1068 ;; invalidate wrappers.
1069 (let ((wrappers (get-wrappers-from-classes
1070 nkeys wrappers classes metatypes)))
1071 (when (if (atom wrappers)
1072 (not (invalid-wrapper-p wrappers))
1073 (every (complement #'invalid-wrapper-p)
1074 wrappers))
1075 (setq cache (fill-cache cache wrappers value))))))))))
1076 (if classes-list
1077 (mapc #'add-class-list classes-list)
1078 (dolist (method (generic-function-methods generic-function))
1079 (mapc #'add-class-list
1080 (mec-all-class-lists (method-specializers method)
1081 precompute-p))))
1082 cache)))
1084 (defmacro class-test (arg class)
1085 (cond
1086 ((eq class *the-class-t*) t)
1087 ((eq class *the-class-slot-object*)
1088 `(not (typep (classoid-of ,arg) 'system-classoid)))
1089 ((eq class *the-class-standard-object*)
1090 `(or (std-instance-p ,arg) (fsc-instance-p ,arg)))
1091 ((eq class *the-class-funcallable-standard-object*)
1092 `(fsc-instance-p ,arg))
1094 `(typep ,arg ',(class-name class)))))
1096 (defmacro class-eq-test (arg class)
1097 `(eq (class-of ,arg) ',class))
1099 (defmacro eql-test (arg object)
1100 `(eql ,arg ',object))
1102 (defun dnet-methods-p (form)
1103 (and (consp form)
1104 (or (eq (car form) 'methods)
1105 (eq (car form) 'unordered-methods))))
1107 ;;; This is CASE, but without gensyms.
1108 (defmacro scase (arg &rest clauses)
1109 `(let ((.case-arg. ,arg))
1110 (cond ,@(mapcar (lambda (clause)
1111 (list* (cond ((null (car clause))
1112 nil)
1113 ((consp (car clause))
1114 (if (null (cdar clause))
1115 `(eql .case-arg.
1116 ',(caar clause))
1117 `(member .case-arg.
1118 ',(car clause))))
1119 ((member (car clause) '(t otherwise))
1122 `(eql .case-arg. ',(car clause))))
1124 (cdr clause)))
1125 clauses))))
1127 (defmacro mcase (arg &rest clauses) `(scase ,arg ,@clauses))
1129 (defun generate-discrimination-net (generic-function methods types sorted-p)
1130 (let* ((arg-info (gf-arg-info generic-function))
1131 (c-a-m-emf-std-p (gf-info-c-a-m-emf-std-p arg-info))
1132 (precedence (arg-info-precedence arg-info)))
1133 (generate-discrimination-net-internal
1134 generic-function methods types
1135 (lambda (methods known-types)
1136 (if (or sorted-p
1137 (and c-a-m-emf-std-p
1138 (block one-order-p
1139 (let ((sorted-methods nil))
1140 (map-all-orders
1141 (copy-list methods) precedence
1142 (lambda (methods)
1143 (when sorted-methods (return-from one-order-p nil))
1144 (setq sorted-methods methods)))
1145 (setq methods sorted-methods))
1146 t)))
1147 `(methods ,methods ,known-types)
1148 `(unordered-methods ,methods ,known-types)))
1149 (lambda (position type true-value false-value)
1150 (let ((arg (dfun-arg-symbol position)))
1151 (if (eq (car type) 'eql)
1152 (let* ((false-case-p (and (consp false-value)
1153 (or (eq (car false-value) 'scase)
1154 (eq (car false-value) 'mcase))
1155 (eq arg (cadr false-value))))
1156 (false-clauses (if false-case-p
1157 (cddr false-value)
1158 `((t ,false-value))))
1159 (case-sym (if (and (dnet-methods-p true-value)
1160 (if false-case-p
1161 (eq (car false-value) 'mcase)
1162 (dnet-methods-p false-value)))
1163 'mcase
1164 'scase))
1165 (type-sym `(,(cadr type))))
1166 `(,case-sym ,arg
1167 (,type-sym ,true-value)
1168 ,@false-clauses))
1169 `(if ,(let ((arg (dfun-arg-symbol position)))
1170 (case (car type)
1171 (class `(class-test ,arg ,(cadr type)))
1172 (class-eq `(class-eq-test ,arg ,(cadr type)))))
1173 ,true-value
1174 ,false-value))))
1175 #'identity)))
1177 (defun class-from-type (type)
1178 (if (or (atom type) (eq (car type) t))
1179 *the-class-t*
1180 (case (car type)
1181 (and (dolist (type (cdr type) *the-class-t*)
1182 (when (and (consp type) (not (eq (car type) 'not)))
1183 (return (class-from-type type)))))
1184 (not *the-class-t*)
1185 (eql (class-of (cadr type)))
1186 (class-eq (cadr type))
1187 (class (cadr type)))))
1189 ;;; We know that known-type implies neither new-type nor `(not ,new-type).
1190 (defun augment-type (new-type known-type)
1191 (if (or (eq known-type t)
1192 (eq (car new-type) 'eql))
1193 new-type
1194 (let ((so-far (if (and (consp known-type) (eq (car known-type) 'and))
1195 (cdr known-type)
1196 (list known-type))))
1197 (unless (eq (car new-type) 'not)
1198 (setq so-far
1199 (mapcan (lambda (type)
1200 (unless (*subtypep new-type type)
1201 (list type)))
1202 so-far)))
1203 (if (null so-far)
1204 new-type
1205 `(and ,new-type ,@so-far)))))
1207 (defun generate-discrimination-net-internal
1208 (gf methods types methods-function test-fun type-function)
1209 (let* ((arg-info (gf-arg-info gf))
1210 (precedence (arg-info-precedence arg-info))
1211 (nreq (arg-info-number-required arg-info))
1212 (metatypes (arg-info-metatypes arg-info)))
1213 (labels ((do-column (p-tail contenders known-types)
1214 (if p-tail
1215 (let* ((position (car p-tail))
1216 (known-type (or (nth position types) t)))
1217 (if (eq (nth position metatypes) t)
1218 (do-column (cdr p-tail) contenders
1219 (cons (cons position known-type)
1220 known-types))
1221 (do-methods p-tail contenders
1222 known-type () known-types)))
1223 (funcall methods-function contenders
1224 (let ((k-t (make-list nreq)))
1225 (dolist (index+type known-types)
1226 (setf (nth (car index+type) k-t)
1227 (cdr index+type)))
1228 k-t))))
1229 (do-methods (p-tail contenders known-type winners known-types)
1230 ;; CONTENDERS
1231 ;; is a (sorted) list of methods that must be discriminated.
1232 ;; KNOWN-TYPE
1233 ;; is the type of this argument, constructed from tests
1234 ;; already made.
1235 ;; WINNERS
1236 ;; is a (sorted) list of methods that are potentially
1237 ;; applicable after the discrimination has been made.
1238 (if (null contenders)
1239 (do-column (cdr p-tail)
1240 winners
1241 (cons (cons (car p-tail) known-type)
1242 known-types))
1243 (let* ((position (car p-tail))
1244 (method (car contenders))
1245 (specl (nth position (method-specializers method)))
1246 (type (funcall type-function
1247 (type-from-specializer specl))))
1248 (multiple-value-bind (app-p maybe-app-p)
1249 (specializer-applicable-using-type-p type known-type)
1250 (flet ((determined-to-be (truth-value)
1251 (if truth-value app-p (not maybe-app-p)))
1252 (do-if (truth &optional implied)
1253 (let ((ntype (if truth type `(not ,type))))
1254 (do-methods p-tail
1255 (cdr contenders)
1256 (if implied
1257 known-type
1258 (augment-type ntype known-type))
1259 (if truth
1260 (append winners `(,method))
1261 winners)
1262 known-types))))
1263 (cond ((determined-to-be nil) (do-if nil t))
1264 ((determined-to-be t) (do-if t t))
1265 (t (funcall test-fun position type
1266 (do-if t) (do-if nil))))))))))
1267 (do-column precedence methods ()))))
1269 (defun compute-secondary-dispatch-function (generic-function net &optional
1270 method-alist wrappers)
1271 (function-funcall (compute-secondary-dispatch-function1 generic-function net)
1272 method-alist wrappers))
1274 (defvar *eq-case-table-limit* 15)
1275 (defvar *case-table-limit* 10)
1277 (defun compute-mcase-parameters (case-list)
1278 (unless (eq t (caar (last case-list)))
1279 (error "The key for the last case arg to mcase was not T"))
1280 (let* ((eq-p (dolist (case case-list t)
1281 (unless (or (eq (car case) t)
1282 (symbolp (caar case)))
1283 (return nil))))
1284 (len (1- (length case-list)))
1285 (type (cond ((= len 1)
1286 :simple)
1287 ((<= len
1288 (if eq-p
1289 *eq-case-table-limit*
1290 *case-table-limit*))
1291 :assoc)
1293 :hash-table))))
1294 (list eq-p type)))
1296 (defmacro mlookup (key info default &optional eq-p type)
1297 (unless (or (eq eq-p t) (null eq-p))
1298 (bug "Invalid eq-p argument: ~S" eq-p))
1299 (ecase type
1300 (:simple
1301 `(if (locally
1302 (declare (optimize (inhibit-warnings 3)))
1303 (,(if eq-p 'eq 'eql) ,key (car ,info)))
1304 (cdr ,info)
1305 ,default))
1306 (:assoc
1307 `(dolist (e ,info ,default)
1308 (when (locally
1309 (declare (optimize (inhibit-warnings 3)))
1310 (,(if eq-p 'eq 'eql) (car e) ,key))
1311 (return (cdr e)))))
1312 (:hash-table
1313 `(gethash ,key ,info ,default))))
1315 (defun net-test-converter (form)
1316 (if (atom form)
1317 (default-test-converter form)
1318 (case (car form)
1319 ((invoke-effective-method-function invoke-fast-method-call
1320 invoke-effective-narrow-method-function)
1321 '.call.)
1322 (methods
1323 '.methods.)
1324 (unordered-methods
1325 '.umethods.)
1326 (mcase
1327 `(mlookup ,(cadr form)
1330 ,@(compute-mcase-parameters (cddr form))))
1331 (t (default-test-converter form)))))
1333 (defun net-code-converter (form)
1334 (if (atom form)
1335 (default-code-converter form)
1336 (case (car form)
1337 ((methods unordered-methods)
1338 (let ((gensym (gensym)))
1339 (values gensym
1340 (list gensym))))
1341 (mcase
1342 (let ((mp (compute-mcase-parameters (cddr form)))
1343 (gensym (gensym)) (default (gensym)))
1344 (values `(mlookup ,(cadr form) ,gensym ,default ,@mp)
1345 (list gensym default))))
1347 (default-code-converter form)))))
1349 (defun net-constant-converter (form generic-function)
1350 (or (let ((c (methods-converter form generic-function)))
1351 (when c (list c)))
1352 (if (atom form)
1353 (default-constant-converter form)
1354 (case (car form)
1355 (mcase
1356 (let* ((mp (compute-mcase-parameters (cddr form)))
1357 (list (mapcar (lambda (clause)
1358 (let ((key (car clause))
1359 (meth (cadr clause)))
1360 (cons (if (consp key) (car key) key)
1361 (methods-converter
1362 meth generic-function))))
1363 (cddr form)))
1364 (default (car (last list))))
1365 (list (list* :mcase mp (nbutlast list))
1366 (cdr default))))
1368 (default-constant-converter form))))))
1370 (defun methods-converter (form generic-function)
1371 (cond ((and (consp form) (eq (car form) 'methods))
1372 (cons '.methods.
1373 (get-effective-method-function1 generic-function (cadr form))))
1374 ((and (consp form) (eq (car form) 'unordered-methods))
1375 (default-secondary-dispatch-function generic-function))))
1377 (defun convert-methods (constant method-alist wrappers)
1378 (if (and (consp constant)
1379 (eq (car constant) '.methods.))
1380 (funcall (cdr constant) method-alist wrappers)
1381 constant))
1383 (defun convert-table (constant method-alist wrappers)
1384 (cond ((and (consp constant)
1385 (eq (car constant) :mcase))
1386 (let ((alist (mapcar (lambda (k+m)
1387 (cons (car k+m)
1388 (convert-methods (cdr k+m)
1389 method-alist
1390 wrappers)))
1391 (cddr constant)))
1392 (mp (cadr constant)))
1393 (ecase (cadr mp)
1394 (:simple
1395 (car alist))
1396 (:assoc
1397 alist)
1398 (:hash-table
1399 (let ((table (make-hash-table :test (if (car mp) 'eq 'eql))))
1400 (dolist (k+m alist)
1401 (setf (gethash (car k+m) table) (cdr k+m)))
1402 table)))))))
1404 (defun compute-secondary-dispatch-function1 (generic-function net
1405 &optional function-p)
1406 (cond
1407 ((and (eq (car net) 'methods) (not function-p))
1408 (get-effective-method-function1 generic-function (cadr net)))
1410 (let* ((name (generic-function-name generic-function))
1411 (arg-info (gf-arg-info generic-function))
1412 (metatypes (arg-info-metatypes arg-info))
1413 (nargs (length metatypes))
1414 (applyp (arg-info-applyp arg-info))
1415 (fmc-arg-info (cons nargs applyp))
1416 (arglist (if function-p
1417 (make-dfun-lambda-list nargs applyp)
1418 (make-fast-method-call-lambda-list nargs applyp))))
1419 (multiple-value-bind (cfunction constants)
1420 ;; We don't want NAMED-LAMBDA for any expressions handed to FNGEN,
1421 ;; because name mismatches will render the hashing ineffective.
1422 (get-fun1 `(lambda ,arglist
1423 (declare (optimize (sb-c::store-closure-debug-pointer 3)))
1424 ,@(unless function-p
1425 `((declare (ignore .pv. .next-method-call.))))
1426 (locally (declare #.*optimize-speed*)
1427 (let ((emf ,net))
1428 ,(make-emf-call nargs applyp 'emf))))
1429 #'net-test-converter
1430 #'net-code-converter
1431 (lambda (form)
1432 (net-constant-converter form generic-function)))
1433 (lambda (method-alist wrappers)
1434 (let* ((alist (list nil))
1435 (alist-tail alist))
1436 (dolist (constant constants)
1437 (let* ((a (or (dolist (a alist nil)
1438 (when (eq (car a) constant)
1439 (return a)))
1440 (cons constant
1441 (or (convert-table
1442 constant method-alist wrappers)
1443 (convert-methods
1444 constant method-alist wrappers)))))
1445 (new (list a)))
1446 (setf (cdr alist-tail) new)
1447 (setf alist-tail new)))
1448 (let ((function (apply cfunction (mapcar #'cdr (cdr alist)))))
1449 (if function-p
1450 (set-fun-name function `(gf-dispatch ,name))
1451 (make-fast-method-call
1452 :function (set-fun-name function `(sdfun-method ,name))
1453 :arg-info fmc-arg-info))))))))))
1455 (defvar *show-make-unordered-methods-emf-calls* nil)
1457 (defun make-unordered-methods-emf (generic-function methods)
1458 (when *show-make-unordered-methods-emf-calls*
1459 (format t "~&make-unordered-methods-emf ~S~%"
1460 (generic-function-name generic-function)))
1461 (lambda (&rest args)
1462 (let* ((types (types-from-args generic-function args 'eql))
1463 (smethods (sort-applicable-methods generic-function
1464 methods
1465 types))
1466 (emf (get-effective-method-function generic-function smethods)))
1467 (invoke-emf emf args))))
1469 ;;; The value returned by compute-discriminating-function is a function
1470 ;;; object. It is called a discriminating function because it is called
1471 ;;; when the generic function is called and its role is to discriminate
1472 ;;; on the arguments to the generic function and then call appropriate
1473 ;;; method functions.
1475 ;;; A discriminating function can only be called when it is installed as
1476 ;;; the funcallable instance function of the generic function for which
1477 ;;; it was computed.
1479 ;;; More precisely, if compute-discriminating-function is called with
1480 ;;; an argument <gf1>, and returns a result <df1>, that result must
1481 ;;; not be passed to apply or funcall directly. Rather, <df1> must be
1482 ;;; stored as the funcallable instance function of the same generic
1483 ;;; function <gf1> (using SET-FUNCALLABLE-INSTANCE-FUNCTION). Then the
1484 ;;; generic function can be passed to funcall or apply.
1486 ;;; An important exception is that methods on this generic function are
1487 ;;; permitted to return a function which itself ends up calling the value
1488 ;;; returned by a more specific method. This kind of `encapsulation' of
1489 ;;; discriminating function is critical to many uses of the MOP.
1491 ;;; As an example, the following canonical case is legal:
1493 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1494 ;;; (let ((std (call-next-method)))
1495 ;;; (lambda (arg)
1496 ;;; (print (list 'call-to-gf gf arg))
1497 ;;; (funcall std arg))))
1499 ;;; Because many discriminating functions would like to use a dynamic
1500 ;;; strategy in which the precise discriminating function changes with
1501 ;;; time it is important to specify how a discriminating function is
1502 ;;; permitted itself to change the funcallable instance function of the
1503 ;;; generic function.
1505 ;;; Discriminating functions may set the funcallable instance function
1506 ;;; of the generic function, but the new value must be generated by making
1507 ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION. This is to ensure that any
1508 ;;; more specific methods which may have encapsulated the discriminating
1509 ;;; function will get a chance to encapsulate the new, inner discriminating
1510 ;;; function.
1512 ;;; This implies that if a discriminating function wants to modify itself
1513 ;;; it should first store some information in the generic function proper,
1514 ;;; and then call compute-discriminating-function. The appropriate method
1515 ;;; on compute-discriminating-function will see the information stored in
1516 ;;; the generic function and generate a discriminating function accordingly.
1518 ;;; The following is an example of a discriminating function which modifies
1519 ;;; itself in accordance with this protocol:
1521 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1522 ;;; (lambda (arg)
1523 ;;; (cond (<some condition>
1524 ;;; <store some info in the generic function>
1525 ;;; (set-funcallable-instance-function
1526 ;;; gf
1527 ;;; (compute-discriminating-function gf))
1528 ;;; (funcall gf arg))
1529 ;;; (t
1530 ;;; <call-a-method-of-gf>))))
1532 ;;; Whereas this code would not be legal:
1534 ;;; (defmethod compute-discriminating-function ((gf my-generic-function))
1535 ;;; (lambda (arg)
1536 ;;; (cond (<some condition>
1537 ;;; (set-funcallable-instance-function
1538 ;;; gf
1539 ;;; (lambda (a) ..))
1540 ;;; (funcall gf arg))
1541 ;;; (t
1542 ;;; <call-a-method-of-gf>))))
1544 ;;; NOTE: All the examples above assume that all instances of the class
1545 ;;; my-generic-function accept only one argument.
1547 (defun slot-value-using-class-dfun (class object slotd)
1548 (declare (ignore class))
1549 (funcall (slot-info-reader (slot-definition-info slotd)) object))
1551 (defun setf-slot-value-using-class-dfun (new-value class object slotd)
1552 (declare (ignore class))
1553 (funcall (slot-info-writer (slot-definition-info slotd)) new-value object))
1555 (defun slot-boundp-using-class-dfun (class object slotd)
1556 (declare (ignore class))
1557 (funcall (slot-info-boundp (slot-definition-info slotd)) object))
1559 (defun special-case-for-compute-discriminating-function-p (gf)
1560 (or (eq gf #'slot-value-using-class)
1561 (eq gf #'(setf slot-value-using-class))
1562 (eq gf #'slot-boundp-using-class)))
1564 ;;; this is the normal function for computing the discriminating
1565 ;;; function of a standard-generic-function
1566 (let (initial-print-object-cache)
1567 (defun standard-compute-discriminating-function (gf)
1568 (declare (notinline slot-value))
1569 (let ((dfun-state (slot-value gf 'dfun-state)))
1570 (when (special-case-for-compute-discriminating-function-p gf)
1571 ;; if we have a special case for
1572 ;; COMPUTE-DISCRIMINATING-FUNCTION, then (at least for the
1573 ;; special cases implemented as of 2006-05-09) any information
1574 ;; in the cache is misplaced.
1575 (aver (null dfun-state)))
1576 (typecase dfun-state
1577 (null
1578 (when (eq gf (load-time-value #'compute-applicable-methods t))
1579 (update-all-c-a-m-gf-info gf))
1580 (cond
1581 ((eq gf (load-time-value #'slot-value-using-class t))
1582 (update-slot-value-gf-info gf 'reader)
1583 #'slot-value-using-class-dfun)
1584 ((eq gf (load-time-value #'(setf slot-value-using-class) t))
1585 (update-slot-value-gf-info gf 'writer)
1586 #'setf-slot-value-using-class-dfun)
1587 ((eq gf (load-time-value #'slot-boundp-using-class t))
1588 (update-slot-value-gf-info gf 'boundp)
1589 #'slot-boundp-using-class-dfun)
1590 ;; KLUDGE: PRINT-OBJECT is not a special-case in the sense
1591 ;; of having a desperately special discriminating function.
1592 ;; However, it is important that the machinery for printing
1593 ;; conditions for stack and heap exhaustion, and the
1594 ;; restarts offered by the debugger, work without consuming
1595 ;; many extra resources. -- CSR, 2008-06-09
1596 ((eq gf (locally (declare (optimize (safety 0))) #'print-object))
1597 (let ((nkeys (nth-value 3 (get-generic-fun-info gf))))
1598 (cond ((/= nkeys 1)
1599 ;; KLUDGE: someone has defined a method
1600 ;; specialized on the second argument: punt.
1601 (setf initial-print-object-cache nil)
1602 (make-initial-dfun gf))
1603 (initial-print-object-cache
1604 (multiple-value-bind (dfun cache info)
1605 (make-caching-dfun gf (copy-cache initial-print-object-cache))
1606 (set-dfun gf dfun cache info)))
1607 ;; the relevant PRINT-OBJECT methods get defined
1608 ;; late, by delayed DEFMETHOD. We mustn't cache
1609 ;; the effective method for our classes earlier
1610 ;; than the relevant PRINT-OBJECT methods are
1611 ;; defined...
1612 ((boundp '*!delayed-defmethod-args*)
1613 (make-initial-dfun gf))
1614 (t (multiple-value-bind (dfun cache info)
1615 (make-final-dfun-internal
1617 (mapcar (lambda (x) (list (find-class x)))
1618 '(sb-kernel::control-stack-exhausted
1619 sb-kernel::binding-stack-exhausted
1620 sb-kernel::alien-stack-exhausted
1621 sb-kernel::heap-exhausted-error
1622 restart)))
1623 (setq initial-print-object-cache cache)
1624 (set-dfun gf dfun (copy-cache cache) info))))))
1625 ((gf-precompute-dfun-and-emf-p (slot-value gf 'arg-info))
1626 (make-final-dfun gf))
1628 (make-initial-dfun gf))))
1629 (function dfun-state)
1630 (cons (car dfun-state))))))
1632 ;;; in general we need to support SBCL's encapsulation for generic
1633 ;;; functions: the default implementation of encapsulation changes the
1634 ;;; identity of the function bound to a name, which breaks anything
1635 ;;; class-based, so we implement the encapsulation ourselves in the
1636 ;;; discriminating function.
1637 (defun sb-impl::encapsulate-generic-function (gf type function)
1638 (push (cons type function) (generic-function-encapsulations gf))
1639 (reinitialize-instance gf))
1641 (defun sb-impl::unencapsulate-generic-function (gf type)
1642 (setf (generic-function-encapsulations gf)
1643 (remove type (generic-function-encapsulations gf)
1644 :key #'car :count 1))
1645 (reinitialize-instance gf))
1646 (defun sb-impl::encapsulated-generic-function-p (gf type)
1647 (position type (generic-function-encapsulations gf) :key #'car))
1648 (defun maybe-encapsulate-discriminating-function (gf encs std)
1649 (if (null encs)
1651 (let ((inner (maybe-encapsulate-discriminating-function
1652 gf (cdr encs) std))
1653 (function (cdar encs)))
1654 (lambda (&rest args)
1655 (apply function inner args)))))
1656 (defmethod compute-discriminating-function ((gf standard-generic-function))
1657 (standard-compute-discriminating-function gf))
1658 (defmethod compute-discriminating-function :around ((gf standard-generic-function))
1659 (maybe-encapsulate-discriminating-function
1660 gf (generic-function-encapsulations gf) (call-next-method)))
1662 (defmethod (setf class-name) (new-value class)
1663 (let ((classoid (layout-classoid (class-wrapper class))))
1664 (if (and new-value (symbolp new-value))
1665 (setf (classoid-name classoid) new-value)
1666 (setf (classoid-name classoid) nil)))
1667 (reinitialize-instance class :name new-value)
1668 new-value)
1670 (defmethod (setf generic-function-name) (new-value generic-function)
1671 (reinitialize-instance generic-function :name new-value)
1672 new-value)
1674 (defmethod function-keywords ((method standard-method))
1675 (multiple-value-bind (llks nreq nopt keywords)
1676 (analyze-lambda-list (if (consp method)
1677 (early-method-lambda-list method)
1678 (method-lambda-list method)))
1679 (declare (ignore nreq nopt))
1680 (values keywords (ll-kwds-allowp llks))))
1682 ;;; This is based on the rules of method lambda list congruency
1683 ;;; defined in the spec. The lambda list it constructs is the pretty
1684 ;;; union of the lambda lists of the generic function and of all its
1685 ;;; methods. It doesn't take method applicability into account; we
1686 ;;; also ignore non-public parts of the interface (e.g. &AUX, default
1687 ;;; and supplied-p parameters)
1688 ;;; The compiler uses this for type-checking that callers pass acceptable
1689 ;;; keywords, so don't make this do anything fancy like looking at effective
1690 ;;; methods without also fixing the compiler.
1691 (defmethod generic-function-pretty-arglist ((gf standard-generic-function))
1692 (let ((gf-lambda-list (generic-function-lambda-list gf))
1693 (methods (generic-function-methods gf)))
1694 (flet ((canonize (k)
1695 (multiple-value-bind (kw var)
1696 (parse-key-arg-spec k)
1697 (if (and (eql (symbol-package kw) *keyword-package*)
1698 (string= kw var))
1700 (list (list kw var))))))
1701 (multiple-value-bind (llks required optional rest keys)
1702 (parse-lambda-list gf-lambda-list :silent t)
1703 (collect ((keys (mapcar #'canonize keys)))
1704 ;; Possibly extend the keyword parameters of the gf by
1705 ;; additional key parameters of its methods:
1706 (dolist (m methods
1707 (make-lambda-list llks nil required optional rest (keys)))
1708 (binding* (((m.llks nil nil nil m.keys)
1709 (parse-lambda-list (method-lambda-list m) :silent t)))
1710 (setq llks (logior llks m.llks))
1711 (dolist (k m.keys)
1712 (unless (member (parse-key-arg-spec k) (keys)
1713 :key #'parse-key-arg-spec :test #'eq)
1714 (keys (canonize k)))))))))))