Declare COERCE and two helpers as EXPLICIT-CHECK.
[sbcl.git] / src / pcl / boot.lisp
blob97cc2880d9c40817262363777dd77052980cfbdd
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")
28 The CommonLoops evaluator is meta-circular.
30 Most of the code in PCL is methods on generic functions, including
31 most of the code that actually implements generic functions and method
32 lookup.
34 So, we have a classic bootstrapping problem. The solution to this is
35 to first get a cheap implementation of generic functions running,
36 these are called early generic functions. These early generic
37 functions and the corresponding early methods and early method lookup
38 are used to get enough of the system running that it is possible to
39 create real generic functions and methods and implement real method
40 lookup. At that point (done in the file FIXUP) the function
41 !FIX-EARLY-GENERIC-FUNCTIONS is called to convert all the early generic
42 functions to real generic functions.
44 The cheap generic functions are built using the same
45 FUNCALLABLE-INSTANCE objects that real generic functions are made out of.
46 This means that as PCL is being bootstrapped, the cheap generic
47 function objects which are being created are the same objects which
48 will later be real generic functions. This is good because:
49 - we don't cons garbage structure, and
50 - we can keep pointers to the cheap generic function objects
51 during booting because those pointers will still point to
52 the right object after the generic functions are all fixed up.
54 This file defines the DEFMETHOD macro and the mechanism used to expand
55 it. This includes the mechanism for processing the body of a method.
56 DEFMETHOD basically expands into a call to LOAD-DEFMETHOD, which
57 basically calls ADD-METHOD to add the method to the generic function.
58 These expansions can be loaded either during bootstrapping or when PCL
59 is fully up and running.
61 An important effect of this arrangement is it means we can compile
62 files with DEFMETHOD forms in them in a completely running PCL, but
63 then load those files back in during bootstrapping. This makes
64 development easier. It also means there is only one set of code for
65 processing DEFMETHOD. Bootstrapping works by being sure to have
66 LOAD-METHOD be careful to call only primitives which work during
67 bootstrapping.
71 (declaim (notinline make-a-method add-named-method
72 ensure-generic-function-using-class
73 add-method remove-method))
75 (defvar *!early-functions*
76 '((make-a-method early-make-a-method real-make-a-method)
77 (add-named-method early-add-named-method real-add-named-method)))
79 ;;; For each of the early functions, arrange to have it point to its
80 ;;; early definition. Do this in a way that makes sure that if we
81 ;;; redefine one of the early definitions the redefinition will take
82 ;;; effect. This makes development easier.
83 (dolist (fns *!early-functions*)
84 (let ((name (car fns))
85 (early-name (cadr fns)))
86 (setf (gdefinition name)
87 (set-fun-name
88 (lambda (&rest args)
89 (apply (fdefinition early-name) args))
90 name))))
92 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
93 ;;; to convert the few functions in the bootstrap which are supposed
94 ;;; to be generic functions but can't be early on.
95 ;;;
96 ;;; each entry is a list of name and lambda-list, class names as
97 ;;; specializers, and method body function name.
98 (defvar *!generic-function-fixups*
99 '((add-method
100 ((generic-function method)
101 (standard-generic-function method)
102 real-add-method))
103 (remove-method
104 ((generic-function method)
105 (standard-generic-function method)
106 real-remove-method))
107 (get-method
108 ((generic-function qualifiers specializers &optional (errorp t))
109 (standard-generic-function t t)
110 real-get-method))
111 (ensure-generic-function-using-class
112 ((generic-function fun-name
113 &key generic-function-class environment
114 &allow-other-keys)
115 (generic-function t)
116 real-ensure-gf-using-class--generic-function)
117 ((generic-function fun-name
118 &key generic-function-class environment
119 &allow-other-keys)
120 (null t)
121 real-ensure-gf-using-class--null))
122 (make-method-lambda
123 ((proto-generic-function proto-method lambda-expression environment)
124 (standard-generic-function standard-method t t)
125 real-make-method-lambda))
126 (make-method-specializers-form
127 ((proto-generic-function proto-method specializer-names environment)
128 (standard-generic-function standard-method t t)
129 real-make-method-specializers-form))
130 (parse-specializer-using-class
131 ((generic-function specializer)
132 (standard-generic-function t)
133 real-parse-specializer-using-class))
134 (unparse-specializer-using-class
135 ((generic-function specializer)
136 (standard-generic-function t)
137 real-unparse-specializer-using-class))
138 (make-method-initargs-form
139 ((proto-generic-function proto-method
140 lambda-expression
141 lambda-list environment)
142 (standard-generic-function standard-method t t t)
143 real-make-method-initargs-form))
144 (compute-effective-method
145 ((generic-function combin applicable-methods)
146 (generic-function standard-method-combination t)
147 standard-compute-effective-method))))
149 (defmacro defgeneric (fun-name lambda-list &body options)
150 (declare (type list lambda-list))
151 (unless (legal-fun-name-p fun-name)
152 (error 'simple-program-error
153 :format-control "illegal generic function name ~S"
154 :format-arguments (list fun-name)))
155 (check-gf-lambda-list lambda-list)
156 (let ((initargs ())
157 (methods ()))
158 (flet ((duplicate-option (name)
159 (error 'simple-program-error
160 :format-control "The option ~S appears more than once."
161 :format-arguments (list name)))
162 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
163 (let* ((arglist-pos (position-if #'listp qab))
164 (arglist (elt qab arglist-pos))
165 (qualifiers (subseq qab 0 arglist-pos))
166 (body (nthcdr (1+ arglist-pos) qab)))
167 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
168 (generic-function-initial-methods (fdefinition ',fun-name))))))
169 (macrolet ((initarg (key) `(getf initargs ,key)))
170 (dolist (option options)
171 (let ((car-option (car option)))
172 (case car-option
173 (declare
174 (dolist (spec (cdr option))
175 (unless (consp spec)
176 (error 'simple-program-error
177 :format-control "~@<Invalid declaration specifier in ~
178 DEFGENERIC: ~S~:@>"
179 :format-arguments (list spec)))
180 (when (member (first spec)
181 ;; FIXME: this list is slightly weird.
182 ;; ANSI (on the DEFGENERIC page) in one
183 ;; place allows only OPTIMIZE; in
184 ;; another place gives this list of
185 ;; disallowed declaration specifiers.
186 ;; This seems to be the only place where
187 ;; the FUNCTION declaration is
188 ;; mentioned; TYPE seems to be missing.
189 ;; Very strange. -- CSR, 2002-10-21
190 '(declaration ftype function
191 inline notinline special))
192 (error 'simple-program-error
193 :format-control "The declaration specifier ~S ~
194 is not allowed inside DEFGENERIC."
195 :format-arguments (list spec)))
196 (if (or (eq 'optimize (first spec))
197 (info :declaration :recognized (first spec)))
198 (push spec (initarg :declarations))
199 (warn "Ignoring unrecognized declaration in DEFGENERIC: ~S"
200 spec))))
201 (:method-combination
202 (when (initarg car-option)
203 (duplicate-option car-option))
204 (unless (symbolp (cadr option))
205 (error 'simple-program-error
206 :format-control "METHOD-COMBINATION name not a ~
207 symbol: ~S"
208 :format-arguments (list (cadr option))))
209 (setf (initarg car-option)
210 `',(cdr option)))
211 (:argument-precedence-order
212 (let* ((required (nth-value 1 (parse-lambda-list lambda-list)))
213 (supplied (cdr option)))
214 (unless (= (length required) (length supplied))
215 (error 'simple-program-error
216 :format-control "argument count discrepancy in ~
217 :ARGUMENT-PRECEDENCE-ORDER clause."
218 :format-arguments nil))
219 (when (set-difference required supplied)
220 (error 'simple-program-error
221 :format-control "unequal sets for ~
222 :ARGUMENT-PRECEDENCE-ORDER clause: ~
223 ~S and ~S"
224 :format-arguments (list required supplied)))
225 (setf (initarg car-option)
226 `',(cdr option))))
227 ((:documentation :generic-function-class :method-class)
228 (unless (proper-list-of-length-p option 2)
229 (error "bad list length for ~S" option))
230 (if (initarg car-option)
231 (duplicate-option car-option)
232 (setf (initarg car-option) `',(cadr option))))
233 (:method
234 (push (cdr option) methods))
236 ;; ANSI requires that unsupported things must get a
237 ;; PROGRAM-ERROR.
238 (error 'simple-program-error
239 :format-control "unsupported option ~S"
240 :format-arguments (list option))))))
242 (when (initarg :declarations)
243 (setf (initarg :declarations)
244 `',(initarg :declarations))))
245 `(progn
246 (eval-when (:compile-toplevel :load-toplevel :execute)
247 (compile-or-load-defgeneric ',fun-name))
248 (load-defgeneric ',fun-name ',lambda-list
249 (sb-c:source-location) ,@initargs)
250 ,@(mapcar #'expand-method-definition methods)
251 (fdefinition ',fun-name)))))
253 (defun compile-or-load-defgeneric (fun-name)
254 (proclaim-as-fun-name fun-name)
255 (when (typep fun-name '(cons (eql setf)))
256 (sb-c::warn-if-setf-macro fun-name))
257 (note-name-defined fun-name :function)
258 (unless (eq (info :function :where-from fun-name) :declared)
259 ;; Hmm. This is similar to BECOME-DEFINED-FUN-NAME
260 ;; except that it doesn't clear an :ASSUMED-TYPE. Should it?
261 (setf (info :function :where-from fun-name) :defined)
262 (setf (info :function :type fun-name)
263 (specifier-type 'function))))
265 (defun load-defgeneric (fun-name lambda-list source-location &rest initargs)
266 (when (fboundp fun-name)
267 (warn 'sb-kernel:redefinition-with-defgeneric
268 :name fun-name
269 :new-location source-location)
270 (let ((fun (fdefinition fun-name)))
271 (when (generic-function-p fun)
272 (loop for method in (generic-function-initial-methods fun)
273 do (remove-method fun method))
274 (setf (generic-function-initial-methods fun) '()))))
275 (apply #'ensure-generic-function
276 fun-name
277 :lambda-list lambda-list
278 :definition-source source-location
279 initargs))
281 (define-condition generic-function-lambda-list-error
282 (reference-condition simple-program-error)
284 (:default-initargs :references (list '(:ansi-cl :section (3 4 2)))))
286 (defun check-gf-lambda-list (lambda-list)
287 (flet ((symbol-or-singleton-p (arg)
288 (typep arg '(or symbol (cons symbol null))))
289 (complain (arg)
290 (error 'generic-function-lambda-list-error
291 :format-control
292 "~@<invalid ~S ~_in the generic function lambda list ~S~:>"
293 :format-arguments (list arg lambda-list))))
294 (multiple-value-bind (llks required optional rest keys)
295 (parse-lambda-list
296 lambda-list
297 :accept (lambda-list-keyword-mask
298 '(&optional &rest &key &allow-other-keys))
299 :condition-class 'generic-function-lambda-list-error
300 :context "a generic function lambda list")
301 (declare (ignore llks required rest))
302 ;; no defaults allowed for &OPTIONAL arguments
303 (dolist (arg optional)
304 (or (symbol-or-singleton-p arg) (complain arg)))
305 ;; no defaults allowed for &KEY arguments
306 (dolist (arg keys)
307 (or (symbol-or-singleton-p arg)
308 (typep arg '(cons (cons symbol (cons symbol null)) null))
309 (complain arg))))))
311 (defmacro defmethod (name &rest args)
312 (multiple-value-bind (qualifiers lambda-list body)
313 (parse-defmethod args)
314 `(progn
315 (eval-when (:compile-toplevel :execute)
316 ;; :compile-toplevel is needed for subsequent forms
317 ;; :execute is needed for references to itself inside the body
318 (compile-or-load-defgeneric ',name))
319 ;; KLUDGE: this double expansion is quite a monumental
320 ;; workaround: it comes about because of a fantastic interaction
321 ;; between the processing rules of CLHS 3.2.3.1 and the
322 ;; bizarreness of MAKE-METHOD-LAMBDA.
324 ;; MAKE-METHOD-LAMBDA can be called by the user, and if the
325 ;; lambda itself doesn't refer to outside bindings the return
326 ;; value must be compileable in the null lexical environment.
327 ;; However, the function must also refer somehow to the
328 ;; associated method object, so that it can call NO-NEXT-METHOD
329 ;; with the appropriate arguments if there is no next method --
330 ;; but when the function is generated, the method object doesn't
331 ;; exist yet.
333 ;; In order to resolve this issue, we insert a literal cons cell
334 ;; into the body of the method lambda, return the same cons cell
335 ;; as part of the second (initargs) return value of
336 ;; MAKE-METHOD-LAMBDA, and a method on INITIALIZE-INSTANCE fills
337 ;; in the cell when the method is created. However, this
338 ;; strategy depends on having a fresh cons cell for every method
339 ;; lambda, which (without the workaround below) is skewered by
340 ;; the processing in CLHS 3.2.3.1, which permits implementations
341 ;; to macroexpand the bodies of EVAL-WHEN forms with both
342 ;; :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL only once. The
343 ;; expansion below forces the double expansion in those cases,
344 ;; while expanding only once in the common case.
345 (eval-when (:load-toplevel)
346 (%defmethod-expander ,name ,qualifiers ,lambda-list ,body))
347 (eval-when (:execute)
348 (%defmethod-expander ,name ,qualifiers ,lambda-list ,body)))))
350 (defmacro %defmethod-expander
351 (name qualifiers lambda-list body &environment env)
352 (multiple-value-bind (proto-gf proto-method)
353 (prototypes-for-make-method-lambda name)
354 (expand-defmethod name proto-gf proto-method qualifiers
355 lambda-list body env)))
358 (defun prototypes-for-make-method-lambda (name)
359 (if (not (eq **boot-state** 'complete))
360 (values nil nil)
361 (let ((gf? (and (fboundp name)
362 (gdefinition name))))
363 (if (or (null gf?)
364 (not (generic-function-p gf?)))
365 (values (class-prototype (find-class 'standard-generic-function))
366 (class-prototype (find-class 'standard-method)))
367 (values gf?
368 (class-prototype (or (generic-function-method-class gf?)
369 (find-class 'standard-method))))))))
371 ;;; Take a name which is either a generic function name or a list specifying
372 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
373 ;;; the prototype instance of the method-class for that generic function.
375 ;;; If there is no generic function by that name, this returns the
376 ;;; default value, the prototype instance of the class
377 ;;; STANDARD-METHOD. This default value is also returned if the spec
378 ;;; names an ordinary function or even a macro. In effect, this leaves
379 ;;; the signalling of the appropriate error until load time.
381 ;;; Note: During bootstrapping, this function is allowed to return NIL.
382 (defun method-prototype-for-gf (name)
383 (let ((gf? (and (fboundp name)
384 (gdefinition name))))
385 (cond ((neq **boot-state** 'complete) nil)
386 ((or (null gf?)
387 (not (generic-function-p gf?))) ; Someone else MIGHT
388 ; error at load time.
389 (class-prototype (find-class 'standard-method)))
391 (class-prototype (or (generic-function-method-class gf?)
392 (find-class 'standard-method)))))))
394 ;;; These are used to communicate the method name and lambda-list to
395 ;;; MAKE-METHOD-LAMBDA-INTERNAL.
396 (defvar *method-name* nil)
397 (defvar *method-lambda-list* nil)
399 (defun expand-defmethod (name
400 proto-gf
401 proto-method
402 qualifiers
403 lambda-list
404 body
405 env)
406 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
407 (parse-specialized-lambda-list lambda-list)
408 (declare (ignore parameters))
409 (mapc (lambda (specializer)
410 (when (typep specializer 'type-specifier)
411 (check-deprecated-type specializer)))
412 specializers)
413 (let ((method-lambda `(lambda ,unspecialized-lambda-list ,@body))
414 (*method-name* `(,name ,@qualifiers ,specializers))
415 (*method-lambda-list* lambda-list))
416 (multiple-value-bind (method-function-lambda initargs)
417 (make-method-lambda proto-gf proto-method method-lambda env)
418 (let ((initargs-form (make-method-initargs-form
419 proto-gf proto-method method-function-lambda
420 initargs env))
421 (specializers-form (make-method-specializers-form
422 proto-gf proto-method specializers env)))
423 `(progn
424 ;; Note: We could DECLAIM the ftype of the generic function
425 ;; here, since ANSI specifies that we create it if it does
426 ;; not exist. However, I chose not to, because I think it's
427 ;; more useful to support a style of programming where every
428 ;; generic function has an explicit DEFGENERIC and any typos
429 ;; in DEFMETHODs are warned about. Otherwise
431 ;; (DEFGENERIC FOO-BAR-BLETCH (X))
432 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
433 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
434 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
435 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
436 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
438 ;; compiles without raising an error and runs without
439 ;; raising an error (since SIMPLE-VECTOR cases fall through
440 ;; to VECTOR) but still doesn't do what was intended. I hate
441 ;; that kind of bug (code which silently gives the wrong
442 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
443 ,(make-defmethod-form name qualifiers specializers-form
444 unspecialized-lambda-list
445 (if proto-method
446 (class-name (class-of proto-method))
447 'standard-method)
448 initargs-form)))))))
450 (defun interned-symbol-p (x)
451 (and (symbolp x) (symbol-package x)))
453 (defun make-defmethod-form
454 (name qualifiers specializers unspecialized-lambda-list
455 method-class-name initargs-form)
456 (declare (sb-ext:muffle-conditions sb-ext:code-deletion-note))
457 (let (fn
458 fn-lambda)
459 (if (and (interned-symbol-p (fun-name-block-name name))
460 (every #'interned-symbol-p qualifiers)
461 (every (lambda (s)
462 (if (consp s)
463 (and (eq (car s) 'eql)
464 (constantp (cadr s))
465 (let ((sv (constant-form-value (cadr s))))
466 (or (interned-symbol-p sv)
467 (integerp sv)
468 (and (characterp sv)
469 (standard-char-p sv)))))
470 (interned-symbol-p s)))
471 specializers)
472 (consp initargs-form)
473 (eq (car initargs-form) 'list*)
474 (memq (cadr initargs-form) '(:function))
475 (consp (setq fn (caddr initargs-form)))
476 (eq (car fn) 'function)
477 (consp (setq fn-lambda (cadr fn)))
478 (eq (car fn-lambda) 'lambda)
479 (bug "Really got here"))
480 (let* ((specls (mapcar (lambda (specl)
481 (if (consp specl)
482 ;; CONSTANT-FORM-VALUE? What I
483 ;; kind of want to know, though,
484 ;; is what happens if we don't do
485 ;; this for some slow-method
486 ;; function because of a hairy
487 ;; lexenv -- is the only bad
488 ;; effect that the method
489 ;; function ends up unnamed? If
490 ;; so, couldn't we arrange to
491 ;; name it later?
492 `(,(car specl) ,(eval (cadr specl)))
493 specl))
494 specializers))
495 (mname `(,(if (eq (cadr initargs-form) :function)
496 'slow-method 'fast-method)
497 ,name ,@qualifiers ,specls)))
498 `(progn
499 (defun ,mname ,(cadr fn-lambda)
500 ,@(cddr fn-lambda))
501 ,(make-defmethod-form-internal
502 name qualifiers `',specls
503 unspecialized-lambda-list method-class-name
504 `(list* ,(cadr initargs-form)
505 #',mname
506 ,@(cdddr initargs-form)))))
507 (make-defmethod-form-internal
508 name qualifiers
509 specializers
510 #+nil
511 `(list ,@(mapcar (lambda (specializer)
512 (if (consp specializer)
513 ``(,',(car specializer)
514 ,,(cadr specializer))
515 `',specializer))
516 specializers))
517 unspecialized-lambda-list
518 method-class-name
519 initargs-form))))
521 (defun make-defmethod-form-internal
522 (name qualifiers specializers-form unspecialized-lambda-list
523 method-class-name initargs-form)
524 `(load-defmethod
525 ',method-class-name
526 ',name
527 ',qualifiers
528 ,specializers-form
529 ',unspecialized-lambda-list
530 ,initargs-form
531 (sb-c:source-location)))
533 (defmacro make-method-function (method-lambda &environment env)
534 (multiple-value-bind (proto-gf proto-method)
535 (prototypes-for-make-method-lambda nil)
536 (multiple-value-bind (method-function-lambda initargs)
537 (make-method-lambda proto-gf proto-method method-lambda env)
538 (make-method-initargs-form proto-gf
539 proto-method
540 method-function-lambda
541 initargs
542 env))))
544 (defun real-make-method-initargs-form (proto-gf proto-method
545 method-lambda initargs env)
546 (declare (ignore proto-gf proto-method))
547 (unless (and (consp method-lambda)
548 (eq (car method-lambda) 'lambda))
549 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
550 is not a lambda form."
551 method-lambda))
552 (make-method-initargs-form-internal method-lambda initargs env))
554 (unless (fboundp 'make-method-initargs-form)
555 (setf (gdefinition 'make-method-initargs-form)
556 (symbol-function 'real-make-method-initargs-form)))
558 ;;; When bootstrapping PCL MAKE-METHOD-LAMBDA starts out as a regular
559 ;;; functions: REAL-MAKE-METHOD-LAMBDA set to the fdefinition of
560 ;;; MAKE-METHOD-LAMBDA. Once generic functions are born, the
561 ;;; REAL-MAKE-METHOD lambda is used as the body of the default method.
562 ;;; MAKE-METHOD-LAMBDA-INTERNAL is split out into a separate function
563 ;;; so that changing it in a live image is easy, and changes actually
564 ;;; take effect.
565 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
566 (make-method-lambda-internal proto-gf proto-method method-lambda env))
568 (unless (fboundp 'make-method-lambda)
569 (setf (gdefinition 'make-method-lambda)
570 (symbol-function 'real-make-method-lambda)))
572 (defun declared-specials (declarations)
573 (loop for (declare . specifiers) in declarations
574 append (loop for specifier in specifiers
575 when (eq 'special (car specifier))
576 append (cdr specifier))))
578 (defun make-method-lambda-internal (proto-gf proto-method method-lambda env)
579 (declare (ignore proto-gf proto-method))
580 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
581 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
582 is not a lambda form."
583 method-lambda))
584 (multiple-value-bind (real-body declarations documentation)
585 (parse-body (cddr method-lambda))
586 ;; We have the %METHOD-NAME declaration in the place where we expect it only
587 ;; if there is are no non-standard prior MAKE-METHOD-LAMBDA methods -- or
588 ;; unless they're fantastically unintrusive.
589 (let* ((method-name *method-name*)
590 (method-lambda-list *method-lambda-list*)
591 ;; Macroexpansion caused by code-walking may call make-method-lambda and
592 ;; end up with wrong values
593 (*method-name* nil)
594 (*method-lambda-list* nil)
595 (generic-function-name (when method-name (car method-name)))
596 (specialized-lambda-list (or method-lambda-list
597 (ecase (car method-lambda)
598 (lambda (second method-lambda))
599 (named-lambda (third method-lambda)))))
600 ;; the method-cell is a way of communicating what method a
601 ;; method-function implements, for the purpose of
602 ;; NO-NEXT-METHOD. We need something that can be shared
603 ;; between function and initargs, but not something that
604 ;; will be coalesced as a constant (because we are naughty,
605 ;; oh yes) with the expansion of any other methods in the
606 ;; same file. -- CSR, 2007-05-30
607 (method-cell (list (make-symbol "METHOD-CELL"))))
608 (multiple-value-bind (parameters lambda-list specializers)
609 (parse-specialized-lambda-list specialized-lambda-list)
610 (let* ((required-parameters
611 (mapcar (lambda (r s) (declare (ignore s)) r)
612 parameters
613 specializers))
614 (slots (mapcar #'list required-parameters))
615 (class-declarations
616 `(declare
617 ;; These declarations seem to be used by PCL to pass
618 ;; information to itself; when I tried to delete 'em
619 ;; ca. 0.6.10 it didn't work. I'm not sure how
620 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
621 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
622 ,@(remove nil
623 (mapcar (lambda (a s) (and (symbolp s)
624 (neq s t)
625 `(%class ,a ,s)))
626 parameters
627 specializers))
628 ;; These TYPE declarations weren't in the original
629 ;; PCL code, but the Python compiler likes them a
630 ;; lot. (We're telling the compiler about our
631 ;; knowledge of specialized argument types so that
632 ;; it can avoid run-time type dispatch overhead,
633 ;; which can be a huge win for Python.)
635 ;; KLUDGE: when I tried moving these to
636 ;; ADD-METHOD-DECLARATIONS, things broke. No idea
637 ;; why. -- CSR, 2004-06-16
638 ,@(let ((specials (declared-specials declarations)))
639 (mapcar (lambda (par spec)
640 (parameter-specializer-declaration-in-defmethod
641 par spec specials env))
642 parameters
643 specializers))))
644 (method-lambda
645 ;; Remove the documentation string and insert the
646 ;; appropriate class declarations. The documentation
647 ;; string is removed to make it easy for us to insert
648 ;; new declarations later, they will just go after the
649 ;; CADR of the method lambda. The class declarations
650 ;; are inserted to communicate the class of the method's
651 ;; arguments to the code walk.
652 `(lambda ,lambda-list
653 ;; The default ignorability of method parameters
654 ;; doesn't seem to be specified by ANSI. PCL had
655 ;; them basically ignorable but was a little
656 ;; inconsistent. E.g. even though the two
657 ;; method definitions
658 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
659 ;; (DEFMETHOD FOO ((X T) Y) "Z")
660 ;; are otherwise equivalent, PCL treated Y as
661 ;; ignorable in the first definition but not in the
662 ;; second definition. We make all required
663 ;; parameters ignorable as a way of systematizing
664 ;; the old PCL behavior. -- WHN 2000-11-24
665 (declare (ignorable ,@required-parameters))
666 ,class-declarations
667 ,@declarations
668 (block ,(fun-name-block-name generic-function-name)
669 ,@real-body)))
670 (constant-value-p (and (null (cdr real-body))
671 (constantp (car real-body))))
672 (constant-value (and constant-value-p
673 (constant-form-value (car real-body))))
674 (plist (and constant-value-p
675 (or (typep constant-value
676 '(or number character))
677 (and (symbolp constant-value)
678 (symbol-package constant-value)))
679 (list :constant-value constant-value)))
680 (applyp (dolist (p lambda-list nil)
681 (cond ((memq p '(&optional &rest &key))
682 (return t))
683 ((eq p '&aux)
684 (return nil))))))
685 (multiple-value-bind (walked-lambda call-next-method-p setq-p
686 parameters-setqd)
687 (walk-method-lambda method-lambda
688 required-parameters
690 slots)
691 (multiple-value-bind (walked-lambda-body
692 walked-declarations
693 walked-documentation)
694 (parse-body (cddr walked-lambda))
695 (declare (ignore walked-documentation))
696 (when (some #'cdr slots)
697 (let ((slot-name-lists (slot-name-lists-from-slots slots)))
698 (setq plist
699 `(,@(when slot-name-lists
700 `(:slot-name-lists ,slot-name-lists))
701 ,@plist))
702 (setq walked-lambda-body
703 `((pv-binding (,required-parameters
704 ,slot-name-lists
705 (load-time-value
706 (intern-pv-table
707 :slot-name-lists ',slot-name-lists)))
708 ,@walked-lambda-body)))))
709 (when (and (memq '&key lambda-list)
710 (not (memq '&allow-other-keys lambda-list)))
711 (let ((aux (memq '&aux lambda-list)))
712 (setq lambda-list (nconc (ldiff lambda-list aux)
713 (list '&allow-other-keys)
714 aux))))
715 (values `(lambda (.method-args. .next-methods.)
716 (simple-lexical-method-functions
717 (,lambda-list .method-args. .next-methods.
718 :call-next-method-p
719 ,(when call-next-method-p t)
720 :setq-p ,setq-p
721 :parameters-setqd ,parameters-setqd
722 :method-cell ,method-cell
723 :applyp ,applyp)
724 ,@walked-declarations
725 (locally
726 (declare (disable-package-locks
727 %parameter-binding-modified))
728 (symbol-macrolet ((%parameter-binding-modified
729 ',@parameters-setqd))
730 (declare (enable-package-locks
731 %parameter-binding-modified))
732 ,@walked-lambda-body))))
733 `(,@(when call-next-method-p `(method-cell ,method-cell))
734 ,@(when (member call-next-method-p '(:simple nil))
735 '(simple-next-method-call t))
736 ,@(when plist `(plist ,plist))
737 ,@(when documentation `(:documentation ,documentation)))))))))))
739 (defun real-make-method-specializers-form
740 (proto-gf proto-method specializer-names env)
741 (declare (ignore env proto-gf proto-method))
742 (flet ((parse (name)
743 (cond
744 ((and (eq **boot-state** 'complete)
745 (specializerp name))
746 name)
747 ((symbolp name) `(find-class ',name))
748 ((consp name) (ecase (car name)
749 ((eql) `(intern-eql-specializer ,(cadr name)))
750 ((class-eq) `(class-eq-specializer (find-class ',(cadr name))))))
752 ;; FIXME: Document CLASS-EQ specializers.
753 (error 'simple-reference-error
754 :format-control
755 "~@<~S is not a valid parameter specializer name.~@:>"
756 :format-arguments (list name)
757 :references (list '(:ansi-cl :macro defmethod)
758 '(:ansi-cl :glossary "parameter specializer name")))))))
759 `(list ,@(mapcar #'parse specializer-names))))
761 (unless (fboundp 'make-method-specializers-form)
762 (setf (gdefinition 'make-method-specializers-form)
763 (symbol-function 'real-make-method-specializers-form)))
765 (defun real-parse-specializer-using-class (generic-function specializer)
766 (let ((result (specializer-from-type specializer)))
767 (if (specializerp result)
768 result
769 (error "~@<~S cannot be parsed as a specializer for ~S.~@:>"
770 specializer generic-function))))
772 (unless (fboundp 'parse-specializer-using-class)
773 (setf (gdefinition 'parse-specializer-using-class)
774 (symbol-function 'real-parse-specializer-using-class)))
776 (defun real-unparse-specializer-using-class (generic-function specializer)
777 (if (specializerp specializer)
778 ;; FIXME: this HANDLER-CASE is a bit of a hammer to crack a nut:
779 ;; the idea is that we want to unparse permissively, so that the
780 ;; lazy (or rather the "portable") specializer extender (who
781 ;; does not define methods on these new SBCL-specific MOP
782 ;; functions) can still subclass specializer and define methods
783 ;; without everything going wrong. Making it cleaner and
784 ;; clearer that that is what we are defending against would be
785 ;; nice. -- CSR, 2007-06-01
786 (handler-case
787 (let ((type (specializer-type specializer)))
788 (if (and (consp type) (eq (car type) 'class))
789 (let* ((class (cadr type))
790 (class-name (class-name class)))
791 (if (eq class (find-class class-name nil))
792 class-name
793 type))
794 type))
795 (error () specializer))
796 (error "~@<~S is not a legal specializer for ~S.~@:>"
797 specializer generic-function)))
799 (unless (fboundp 'unparse-specializer-using-class)
800 (setf (gdefinition 'unparse-specializer-using-class)
801 (symbol-function 'real-unparse-specializer-using-class)))
803 ;;; a helper function for creating Python-friendly type declarations
804 ;;; in DEFMETHOD forms.
806 ;;; We're too lazy to cons up a new environment for this, so we just pass in
807 ;;; the list of locally declared specials in addition to the old environment.
808 (defun parameter-specializer-declaration-in-defmethod
809 (parameter specializer specials env)
810 (cond ((and (consp specializer)
811 (eq (car specializer) 'eql))
812 ;; KLUDGE: ANSI, in its wisdom, says that
813 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
814 ;; DEFMETHOD expansion time. Thus, although one might think
815 ;; that in
816 ;; (DEFMETHOD FOO ((X PACKAGE)
817 ;; (Y (EQL 12))
818 ;; ..))
819 ;; the PACKAGE and (EQL 12) forms are both parallel type
820 ;; names, they're not, as is made clear when you do
821 ;; (DEFMETHOD FOO ((X PACKAGE)
822 ;; (Y (EQL 'BAR)))
823 ;; ..)
824 ;; where Y needs to be a symbol named "BAR", not some cons
825 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
826 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
827 ;; to be of type (EQL X). It'd be easy to transform one to
828 ;; the other, but it'd be somewhat messier to do so while
829 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
830 ;; once. (The new code wouldn't be messy, but it'd require a
831 ;; big transformation of the old code.) So instead we punt.
832 ;; -- WHN 20000610
833 '(ignorable))
834 ((member specializer
835 ;; KLUDGE: For some low-level implementation
836 ;; classes, perhaps because of some problems related
837 ;; to the incomplete integration of PCL into SBCL's
838 ;; type system, some specializer classes can't be
839 ;; declared as argument types. E.g.
840 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
841 ;; (DECLARE (TYPE SLOT-OBJECT X))
842 ;; ..)
843 ;; loses when
844 ;; (DEFSTRUCT BAR A B)
845 ;; (FOO (MAKE-BAR))
846 ;; perhaps because of the way that STRUCTURE-OBJECT
847 ;; inherits both from SLOT-OBJECT and from
848 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
849 ;; problems under the rug, we exclude these problem
850 ;; cases by blacklisting them here. -- WHN 2001-01-19
851 (list 'slot-object #+nil (find-class 'slot-object)))
852 '(ignorable))
853 ((not (eq **boot-state** 'complete))
854 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
855 ;; types which don't match their specializers. (Specifically,
856 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
857 ;; second argument.) Hopefully it only does this kind of
858 ;; weirdness when bootstrapping.. -- WHN 20000610
859 '(ignorable))
860 ((typep specializer 'eql-specializer)
861 `(type (eql ,(eql-specializer-object specializer)) ,parameter))
862 ((or (var-special-p parameter env) (member parameter specials))
863 ;; Don't declare types for special variables -- our rebinding magic
864 ;; for SETQ cases don't work right there as SET, (SETF SYMBOL-VALUE),
865 ;; etc. make things undecidable.
866 '(ignorable))
868 ;; Otherwise, we can usually make Python very happy.
870 ;; KLUDGE: Since INFO doesn't work right for class objects here,
871 ;; and they are valid specializers, see if the specializer is
872 ;; a named class, and use the name in that case -- otherwise
873 ;; the class instance is ok, since info will just return NIL, NIL.
875 ;; We still need to deal with the class case too, but at
876 ;; least #.(find-class 'integer) and integer as equivalent
877 ;; specializers with this.
878 (let* ((specializer-nameoid
879 (if (and (typep specializer 'class)
880 (let ((name (class-name specializer)))
881 (and name (symbolp name)
882 (eq specializer (find-class name nil)))))
883 (class-name specializer)
884 specializer))
885 (kind (info :type :kind specializer-nameoid)))
887 (flet ((specializer-nameoid-class ()
888 (typecase specializer-nameoid
889 (symbol (find-class specializer-nameoid nil))
890 (class specializer-nameoid)
891 (class-eq-specializer
892 (specializer-class specializer-nameoid))
893 (t nil))))
894 (ecase kind
895 ((:primitive) `(type ,specializer-nameoid ,parameter))
896 ((:defined)
897 (let ((class (specializer-nameoid-class)))
898 ;; CLASS can be null here if the user has
899 ;; erroneously tried to use a defined type as a
900 ;; specializer; it can be a non-SYSTEM-CLASS if
901 ;; the user defines a type and calls (SETF
902 ;; FIND-CLASS) in a consistent way.
903 (when (and class (typep class 'system-class))
904 `(type ,(class-name class) ,parameter))))
905 ((:instance nil)
906 (let ((class (specializer-nameoid-class)))
907 (cond
908 (class
909 (if (typep class '(or system-class structure-class))
910 `(type ,class ,parameter)
911 ;; don't declare CLOS classes as parameters;
912 ;; it's too expensive.
913 '(ignorable)))
915 ;; we can get here, and still not have a failure
916 ;; case, by doing MOP programming like (PROGN
917 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
918 ;; ...)). Best to let the user know we haven't
919 ;; been able to extract enough information:
920 (style-warn
921 "~@<can't find type for specializer ~S in ~S.~@:>"
922 specializer-nameoid
923 'parameter-specializer-declaration-in-defmethod)
924 '(ignorable)))))
925 ((:forthcoming-defclass-type)
926 '(ignorable))))))))
928 ;;; For passing a list (groveled by the walker) of the required
929 ;;; parameters whose bindings are modified in the method body to the
930 ;;; optimized-slot-value* macros.
931 (define-symbol-macro %parameter-binding-modified ())
933 (defmacro simple-lexical-method-functions ((lambda-list
934 method-args
935 next-methods
936 &rest lmf-options)
937 &body body)
938 `(progn
939 ,method-args ,next-methods
940 (bind-simple-lexical-method-functions (,method-args ,next-methods
941 ,lmf-options)
942 (bind-args (,lambda-list ,method-args)
943 ,@body))))
945 (defmacro fast-lexical-method-functions ((lambda-list
946 next-method-call
947 args
948 rest-arg
949 &rest lmf-options)
950 &body body)
951 `(bind-fast-lexical-method-functions (,args ,rest-arg ,next-method-call ,lmf-options)
952 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
953 ,@body)))
955 (defmacro bind-simple-lexical-method-functions
956 ((method-args next-methods (&key call-next-method-p setq-p
957 parameters-setqd applyp method-cell))
958 &body body
959 &environment env)
960 (declare (ignore parameters-setqd))
961 (if (not (or call-next-method-p setq-p applyp))
962 ;; always provide the lexical function NEXT-METHOD-P.
963 ;; I would think this to be a good candidate for declaring INLINE
964 ;; but that's not the way it was done before.
965 `(flet ((next-method-p () (not (null (car ,next-methods)))))
966 (declare (ignorable #'next-method-p))
967 ,@body)
968 `(let ((.next-method. (car ,next-methods))
969 (,next-methods (cdr ,next-methods)))
970 (declare (ignorable .next-method. ,next-methods))
971 (flet (,@(when call-next-method-p
972 `((call-next-method (&rest cnm-args)
973 (declare (dynamic-extent cnm-args))
974 ,@(if (safe-code-p env)
975 `((%check-cnm-args cnm-args
976 ,method-args
977 ',method-cell))
978 nil)
979 (if .next-method.
980 (funcall (if (std-instance-p .next-method.)
981 (method-function .next-method.)
982 .next-method.) ; for early methods
983 (or cnm-args ,method-args)
984 ,next-methods)
985 (apply #'call-no-next-method
986 ',method-cell
987 (or cnm-args ,method-args))))))
988 (next-method-p () (not (null .next-method.))))
989 (declare (ignorable #'next-method-p))
990 ,@body))))
992 (defun call-no-next-method (method-cell &rest args)
993 (let ((method (car method-cell)))
994 (aver method)
995 ;; Can't easily provide a RETRY restart here, as the return value here is
996 ;; for the method, not the generic function.
997 (apply #'no-next-method (method-generic-function method)
998 method args)))
1000 (defun call-no-applicable-method (gf args)
1001 (restart-case
1002 (apply #'no-applicable-method gf args)
1003 (retry ()
1004 :report "Retry calling the generic function."
1005 (apply gf args))))
1007 (defun call-no-primary-method (gf args)
1008 (restart-case
1009 (apply #'no-primary-method gf args)
1010 (retry ()
1011 :report "Retry calling the generic function."
1012 (apply gf args))))
1014 (defstruct (method-call (:copier nil))
1015 (function #'identity :type function)
1016 call-method-args)
1017 (defstruct (constant-method-call (:copier nil) (:include method-call))
1018 value)
1020 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
1022 (defmacro invoke-method-call1 (function args cm-args)
1023 `(let ((.function. ,function)
1024 (.args. ,args)
1025 (.cm-args. ,cm-args))
1026 (if (and .cm-args. (null (cdr .cm-args.)))
1027 (funcall .function. .args. (car .cm-args.))
1028 (apply .function. .args. .cm-args.))))
1030 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
1031 `(invoke-method-call1 (method-call-function ,method-call)
1032 ,(if restp
1033 `(list* ,@required-args+rest-arg)
1034 `(list ,@required-args+rest-arg))
1035 (method-call-call-method-args ,method-call)))
1037 (defstruct (fast-method-call (:copier nil))
1038 (function #'identity :type function)
1040 next-method-call
1041 arg-info)
1042 (defstruct (constant-fast-method-call
1043 (:copier nil) (:include fast-method-call))
1044 value)
1046 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
1048 ;; The two variants of INVOKE-FAST-METHOD-CALL differ in how REST-ARGs
1049 ;; are handled. The first one will get REST-ARG as a single list (as
1050 ;; the last argument), and will thus need to use APPLY. The second one
1051 ;; will get them as a &MORE argument, so we can pass the arguments
1052 ;; directly with MULTIPLE-VALUE-CALL and %MORE-ARG-VALUES.
1054 (defmacro invoke-fast-method-call (method-call restp &rest required-args+rest-arg)
1055 `(,(if restp 'apply 'funcall) (fast-method-call-function ,method-call)
1056 (fast-method-call-pv ,method-call)
1057 (fast-method-call-next-method-call ,method-call)
1058 ,@required-args+rest-arg))
1060 (defmacro invoke-fast-method-call/more (method-call
1061 more-context
1062 more-count
1063 &rest required-args)
1064 (macrolet ((generate-call (n)
1065 ``(funcall (fast-method-call-function ,method-call)
1066 (fast-method-call-pv ,method-call)
1067 (fast-method-call-next-method-call ,method-call)
1068 ,@required-args
1069 ,@(loop for x below ,n
1070 collect `(sb-c::%more-arg ,more-context ,x)))))
1071 ;; The cases with only small amounts of required arguments passed
1072 ;; are probably very common, and special-casing speeds them up by
1073 ;; a factor of 2 with very little effect on the other
1074 ;; cases. Though it'd be nice to have the generic case be equally
1075 ;; fast.
1076 `(case ,more-count
1077 (0 ,(generate-call 0))
1078 (1 ,(generate-call 1))
1079 (t (multiple-value-call (fast-method-call-function ,method-call)
1080 (values (fast-method-call-pv ,method-call))
1081 (values (fast-method-call-next-method-call ,method-call))
1082 ,@required-args
1083 (sb-c::%more-arg-values ,more-context 0 ,more-count))))))
1085 (defstruct (fast-instance-boundp (:copier nil))
1086 (index 0 :type fixnum))
1088 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
1090 (eval-when (:compile-toplevel :load-toplevel :execute)
1091 (defvar *allow-emf-call-tracing-p* nil)
1092 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
1094 ;;;; effective method functions
1096 (defvar *emf-call-trace-size* 200)
1097 (defvar *emf-call-trace* nil)
1098 (defvar *emf-call-trace-index* 0)
1100 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
1101 ;;; without explanation. It appears to be intended for debugging, so
1102 ;;; it might be useful someday, so I haven't deleted it.
1103 ;;; But it isn't documented and isn't used for anything now, so
1104 ;;; I've conditionalized it out of the base system. -- WHN 19991213
1105 #+sb-show
1106 (defun show-emf-call-trace ()
1107 (when *emf-call-trace*
1108 (let ((j *emf-call-trace-index*)
1109 (*enable-emf-call-tracing-p* nil))
1110 (format t "~&(The oldest entries are printed first)~%")
1111 (dotimes-fixnum (i *emf-call-trace-size*)
1112 (let ((ct (aref *emf-call-trace* j)))
1113 (when ct (print ct)))
1114 (incf j)
1115 (when (= j *emf-call-trace-size*)
1116 (setq j 0))))))
1118 (defun trace-emf-call-internal (emf format args)
1119 (unless *emf-call-trace*
1120 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
1121 (setf (aref *emf-call-trace* *emf-call-trace-index*)
1122 (list* emf format args))
1123 (incf *emf-call-trace-index*)
1124 (when (= *emf-call-trace-index* *emf-call-trace-size*)
1125 (setq *emf-call-trace-index* 0)))
1127 (defmacro trace-emf-call (emf format args)
1128 (when *allow-emf-call-tracing-p*
1129 `(when *enable-emf-call-tracing-p*
1130 (trace-emf-call-internal ,emf ,format ,args))))
1132 (defmacro invoke-effective-method-function-fast
1133 (emf restp &key required-args rest-arg more-arg)
1134 `(progn
1135 (trace-emf-call ,emf ,restp (list ,@required-args rest-arg))
1136 ,(if more-arg
1137 `(invoke-fast-method-call/more ,emf
1138 ,@more-arg
1139 ,@required-args)
1140 `(invoke-fast-method-call ,emf
1141 ,restp
1142 ,@required-args
1143 ,@rest-arg))))
1145 (defun effective-method-optimized-slot-access-clause
1146 (emf restp required-args)
1147 ;; "What," you may wonder, "do these next two clauses do?" In that
1148 ;; case, you are not a PCL implementor, for they considered this to
1149 ;; be self-documenting.:-| Or CSR, for that matter, since he can
1150 ;; also figure it out by looking at it without breaking stride. For
1151 ;; the rest of us, though: From what the code is doing with .SLOTS.
1152 ;; and whatnot, evidently it's implementing SLOT-VALUEish and
1153 ;; GET-SLOT-VALUEish things. Then we can reason backwards and
1154 ;; conclude that setting EMF to a FIXNUM is an optimized way to
1155 ;; represent these slot access operations.
1156 (when (not restp)
1157 (let ((length (length required-args)))
1158 (cond ((= 1 length)
1159 `((fixnum
1160 (let* ((.slots. (get-slots-or-nil
1161 ,(car required-args)))
1162 (value (when .slots. (clos-slots-ref .slots. ,emf))))
1163 (if (eq value +slot-unbound+)
1164 (slot-unbound-internal ,(car required-args)
1165 ,emf)
1166 value)))))
1167 ((= 2 length)
1168 `((fixnum
1169 (let ((.new-value. ,(car required-args))
1170 (.slots. (get-slots-or-nil
1171 ,(cadr required-args))))
1172 (when .slots.
1173 (setf (clos-slots-ref .slots. ,emf) .new-value.)))))))
1174 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
1175 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
1176 ;; there was no explanation and presumably the code is 10+
1177 ;; years stale, I simply deleted it. -- WHN)
1180 ;;; Before SBCL 0.9.16.7 instead of
1181 ;;; INVOKE-NARROW-EFFECTIVE-METHOD-FUNCTION we passed a (THE (OR
1182 ;;; FUNCTION METHOD-CALL FAST-METHOD-CALL) EMF) form as the EMF. Now,
1183 ;;; to make less work for the compiler we take a path that doesn't
1184 ;;; involve the slot-accessor clause (where EMF is a FIXNUM) at all.
1185 (macrolet ((def (name &optional narrow)
1186 `(defmacro ,name (emf restp &key required-args rest-arg more-arg)
1187 (unless (constantp restp)
1188 (error "The RESTP argument is not constant."))
1189 (setq restp (constant-form-value restp))
1190 (with-unique-names (emf-n)
1191 `(locally
1192 (declare (optimize (sb-c:insert-step-conditions 0)))
1193 (let ((,emf-n ,emf))
1194 (trace-emf-call ,emf-n ,restp (list ,@required-args ,@rest-arg))
1195 (etypecase ,emf-n
1196 (fast-method-call
1197 ,(if more-arg
1198 `(invoke-fast-method-call/more ,emf-n
1199 ,@more-arg
1200 ,@required-args)
1201 `(invoke-fast-method-call ,emf-n
1202 ,restp
1203 ,@required-args
1204 ,@rest-arg)))
1205 ,@,(unless narrow
1206 `(effective-method-optimized-slot-access-clause
1207 emf-n restp required-args))
1208 (method-call
1209 (invoke-method-call ,emf-n ,restp ,@required-args
1210 ,@rest-arg))
1211 (function
1212 ,(if restp
1213 `(apply ,emf-n ,@required-args ,@rest-arg)
1214 `(funcall ,emf-n ,@required-args
1215 ,@rest-arg))))))))))
1216 (def invoke-effective-method-function nil)
1217 (def invoke-narrow-effective-method-function t))
1219 (defun invoke-emf (emf args)
1220 (trace-emf-call emf t args)
1221 (etypecase emf
1222 (fast-method-call
1223 (let* ((arg-info (fast-method-call-arg-info emf))
1224 (restp (cdr arg-info))
1225 (nreq (car arg-info)))
1226 (if restp
1227 (apply (fast-method-call-function emf)
1228 (fast-method-call-pv emf)
1229 (fast-method-call-next-method-call emf)
1230 args)
1231 (cond ((null args)
1232 (if (eql nreq 0)
1233 (invoke-fast-method-call emf nil)
1234 (error 'simple-program-error
1235 :format-control "invalid number of arguments: 0"
1236 :format-arguments nil)))
1237 ((null (cdr args))
1238 (if (eql nreq 1)
1239 (invoke-fast-method-call emf nil (car args))
1240 (error 'simple-program-error
1241 :format-control "invalid number of arguments: 1"
1242 :format-arguments nil)))
1243 ((null (cddr args))
1244 (if (eql nreq 2)
1245 (invoke-fast-method-call emf nil (car args) (cadr args))
1246 (error 'simple-program-error
1247 :format-control "invalid number of arguments: 2"
1248 :format-arguments nil)))
1250 (apply (fast-method-call-function emf)
1251 (fast-method-call-pv emf)
1252 (fast-method-call-next-method-call emf)
1253 args))))))
1254 (method-call
1255 (apply (method-call-function emf)
1256 args
1257 (method-call-call-method-args emf)))
1258 (fixnum
1259 (cond ((null args)
1260 (error 'simple-program-error
1261 :format-control "invalid number of arguments: 0"
1262 :format-arguments nil))
1263 ((null (cdr args))
1264 (let* ((slots (get-slots (car args)))
1265 (value (clos-slots-ref slots emf)))
1266 (if (eq value +slot-unbound+)
1267 (slot-unbound-internal (car args) emf)
1268 value)))
1269 ((null (cddr args))
1270 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1271 (car args)))
1272 (t (error 'simple-program-error
1273 :format-control "invalid number of arguments"
1274 :format-arguments nil))))
1275 (fast-instance-boundp
1276 (if (or (null args) (cdr args))
1277 (error 'simple-program-error
1278 :format-control "invalid number of arguments"
1279 :format-arguments nil)
1280 (let ((slots (get-slots (car args))))
1281 (not (eq (clos-slots-ref slots (fast-instance-boundp-index emf))
1282 +slot-unbound+)))))
1283 (function
1284 (apply emf args))))
1287 (defmacro fast-call-next-method-body ((args next-method-call rest-arg)
1288 method-cell
1289 cnm-args)
1290 `(if ,next-method-call
1291 ,(let ((call `(invoke-narrow-effective-method-function
1292 ,next-method-call
1293 ,(not (null rest-arg))
1294 :required-args ,args
1295 :rest-arg ,(when rest-arg (list rest-arg)))))
1296 `(if ,cnm-args
1297 (bind-args ((,@args
1298 ,@(when rest-arg
1299 `(&rest ,rest-arg)))
1300 ,cnm-args)
1301 ,call)
1302 ,call))
1303 (call-no-next-method ',method-cell
1304 ,@args
1305 ,@(when rest-arg
1306 `(,rest-arg)))))
1308 (defmacro bind-fast-lexical-method-functions
1309 ((args rest-arg next-method-call (&key
1310 call-next-method-p
1311 setq-p
1312 parameters-setqd
1313 method-cell
1314 applyp))
1315 &body body
1316 &environment env)
1317 (let* ((next-method-p-def
1318 `((next-method-p ()
1319 (declare (optimize (sb-c:insert-step-conditions 0)))
1320 (not (null ,next-method-call)))))
1321 (rebindings (when (or setq-p call-next-method-p)
1322 (mapcar (lambda (x) (list x x)) parameters-setqd))))
1323 (if (not (or call-next-method-p setq-p applyp))
1324 `(flet ,next-method-p-def
1325 (declare (ignorable #'next-method-p))
1326 ,@body)
1327 `(flet (,@(when call-next-method-p
1328 `((call-next-method (&rest cnm-args)
1329 (declare (dynamic-extent cnm-args)
1330 (muffle-conditions code-deletion-note)
1331 (optimize (sb-c:insert-step-conditions 0)))
1332 ,@(if (safe-code-p env)
1333 `((%check-cnm-args cnm-args (list ,@args)
1334 ',method-cell))
1335 nil)
1336 (fast-call-next-method-body (,args
1337 ,next-method-call
1338 ,rest-arg)
1339 ,method-cell
1340 cnm-args))))
1341 ,@next-method-p-def)
1342 (declare (ignorable #'next-method-p))
1343 (let ,rebindings
1344 ,@body)))))
1346 ;;; CMUCL comment (Gerd Moellmann):
1348 ;;; The standard says it's an error if CALL-NEXT-METHOD is called with
1349 ;;; arguments, and the set of methods applicable to those arguments is
1350 ;;; different from the set of methods applicable to the original
1351 ;;; method arguments. (According to Barry Margolin, this rule was
1352 ;;; probably added to ensure that before and around methods are always
1353 ;;; run before primary methods.)
1355 ;;; This could be optimized for the case that the generic function
1356 ;;; doesn't have hairy methods, does have standard method combination,
1357 ;;; is a standard generic function, there are no methods defined on it
1358 ;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such
1359 ;;; preconditions. That looks hairy and is probably not worth it,
1360 ;;; because this check will never be fast.
1361 (defun %check-cnm-args (cnm-args orig-args method-cell)
1362 ;; 1. Check for no arguments.
1363 (when cnm-args
1364 (let* ((gf (method-generic-function (car method-cell)))
1365 (nreq (generic-function-nreq gf)))
1366 (declare (fixnum nreq))
1367 ;; 2. Requirement arguments pairwise: if all are EQL, the applicable
1368 ;; methods must be the same. This takes care of the relatively common
1369 ;; case of twiddling with &KEY arguments without being horribly
1370 ;; expensive.
1371 (unless (do ((orig orig-args (cdr orig))
1372 (args cnm-args (cdr args))
1373 (n nreq (1- nreq)))
1374 ((zerop n) t)
1375 (unless (and orig args (eql (car orig) (car args)))
1376 (return nil)))
1377 ;; 3. Only then do the full check.
1378 (let ((omethods (compute-applicable-methods gf orig-args))
1379 (nmethods (compute-applicable-methods gf cnm-args)))
1380 (unless (equal omethods nmethods)
1381 (error "~@<The set of methods ~S applicable to argument~P ~
1382 ~{~S~^, ~} to call-next-method is different from ~
1383 the set of methods ~S applicable to the original ~
1384 method argument~P ~{~S~^, ~}.~@:>"
1385 nmethods (length cnm-args) cnm-args omethods
1386 (length orig-args) orig-args)))))))
1388 ;; FIXME: replacing this entire mess with DESTRUCTURING-BIND would correct
1389 ;; problems similar to those already solved by a correct implementation
1390 ;; of DESTRUCTURING-BIND, such as incorrect binding order:
1391 ;; e.g. (macroexpand-1 '(bind-args ((&optional (x nil xsp)) args) (form)))
1392 ;; -> (LET* ((.ARGS-TAIL. ARGS) (XSP (NOT (NULL .ARGS-TAIL.))) (X ...)))
1393 ;; It's mostly irrelevant unless a method uses CALL-NEXT-METHOD though.
1394 (defmacro bind-args ((lambda-list args) &body body)
1395 (let ((args-tail '.args-tail.)
1396 (key '.key.)
1397 (state 'required))
1398 (flet ((process-var (var)
1399 (if (memq var lambda-list-keywords)
1400 (progn
1401 (case var
1402 (&optional (setq state 'optional))
1403 (&key (setq state 'key))
1404 (&allow-other-keys)
1405 (&rest (setq state 'rest))
1406 (&aux (setq state 'aux))
1407 (otherwise
1408 (error
1409 "encountered the non-standard lambda list keyword ~S"
1410 var)))
1411 nil)
1412 (case state
1413 (required `((,var (pop ,args-tail))))
1414 (optional (cond ((not (consp var))
1415 `((,var (when ,args-tail
1416 (pop ,args-tail)))))
1417 ((null (cddr var))
1418 `((,(car var) (if ,args-tail
1419 (pop ,args-tail)
1420 ,(cadr var)))))
1422 `((,(caddr var) (not (null ,args-tail)))
1423 (,(car var) (if ,args-tail
1424 (pop ,args-tail)
1425 ,(cadr var)))))))
1426 (rest `((,var ,args-tail)))
1427 (key (cond ((not (consp var))
1428 `((,var (car
1429 (get-key-arg-tail ,(keywordicate var)
1430 ,args-tail)))))
1431 ((null (cddr var))
1432 (multiple-value-bind (keyword variable)
1433 (if (consp (car var))
1434 (values (caar var)
1435 (cadar var))
1436 (values (keywordicate (car var))
1437 (car var)))
1438 `((,key (get-key-arg-tail ',keyword
1439 ,args-tail))
1440 (,variable (if ,key
1441 (car ,key)
1442 ,(cadr var))))))
1444 (multiple-value-bind (keyword variable)
1445 (if (consp (car var))
1446 (values (caar var)
1447 (cadar var))
1448 (values (keywordicate (car var))
1449 (car var)))
1450 `((,key (get-key-arg-tail ',keyword
1451 ,args-tail))
1452 (,(caddr var) (not (null,key)))
1453 (,variable (if ,key
1454 (car ,key)
1455 ,(cadr var))))))))
1456 (aux `(,var))))))
1457 (let ((bindings (mapcan #'process-var lambda-list)))
1458 `(let* ((,args-tail ,args)
1459 ,@bindings
1460 (.dummy0.
1461 ,@(when (eq state 'optional)
1462 `((unless (null ,args-tail)
1463 (error 'simple-program-error
1464 :format-control "surplus arguments: ~S"
1465 :format-arguments (list ,args-tail)))))))
1466 (declare (ignorable ,args-tail .dummy0.))
1467 ,@body)))))
1469 (defun get-key-arg-tail (keyword list)
1470 (loop for (key . tail) on list by #'cddr
1471 when (null tail) do
1472 ;; FIXME: Do we want to export this symbol? Or maybe use an
1473 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1474 (sb-c::%odd-key-args-error)
1475 when (eq key keyword)
1476 return tail))
1478 (defun walk-method-lambda (method-lambda required-parameters env slots)
1479 (let (;; flag indicating that CALL-NEXT-METHOD should be in the
1480 ;; method definition
1481 (call-next-method-p nil)
1482 ;; a list of all required parameters whose bindings might be
1483 ;; modified in the method body.
1484 (parameters-setqd nil))
1485 (flet ((walk-function (form context env)
1486 (unless (and (eq context :eval) (consp form))
1487 (return-from walk-function form))
1488 (case (car form)
1489 (call-next-method
1490 ;; hierarchy: nil -> :simple -> T.
1491 (unless (eq call-next-method-p t)
1492 (setq call-next-method-p (if (cdr form) t :simple)))
1493 form)
1494 ((setq multiple-value-setq)
1495 ;; The walker will split (SETQ A 1 B 2) to
1496 ;; separate (SETQ A 1) and (SETQ B 2) forms, so we
1497 ;; only need to handle the simple case of SETQ
1498 ;; here.
1499 (let ((vars (if (eq (car form) 'setq)
1500 (list (second form))
1501 (second form))))
1502 (dolist (var vars)
1503 ;; Note that we don't need to check for
1504 ;; %VARIABLE-REBINDING declarations like is
1505 ;; done in CAN-OPTIMIZE-ACCESS1, since the
1506 ;; bindings that will have that declation will
1507 ;; never be SETQd.
1508 (when (var-declaration '%class var env)
1509 ;; If a parameter binding is shadowed by
1510 ;; another binding it won't have a %CLASS
1511 ;; declaration anymore, and this won't get
1512 ;; executed.
1513 (pushnew var parameters-setqd :test #'eq))))
1514 form)
1515 (function
1516 (when (equal (cdr form) '(call-next-method))
1517 (setq call-next-method-p t))
1518 form)
1519 ((slot-value set-slot-value slot-boundp)
1520 (if (constantp (third form) env)
1521 (let ((fun (ecase (car form)
1522 (slot-value #'optimize-slot-value)
1523 (set-slot-value #'optimize-set-slot-value)
1524 (slot-boundp #'optimize-slot-boundp))))
1525 (funcall fun form slots required-parameters env))
1526 form))
1527 (t form))))
1529 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1530 ;;; FIXME: the walker's rewriting of the source code causes
1531 ;;; trouble when doing code coverage. The rewrites should be
1532 ;;; removed, and the same operations done using
1533 ;;; compiler-macros or tranforms.
1534 (values (if (sb-c:policy env (= sb-c:store-coverage-data 0))
1535 walked-lambda
1536 method-lambda)
1537 call-next-method-p
1538 (not (null parameters-setqd))
1539 parameters-setqd)))))
1541 (defun generic-function-name-p (name)
1542 (and (legal-fun-name-p name)
1543 (fboundp name)
1544 (if (eq **boot-state** 'complete)
1545 (standard-generic-function-p (gdefinition name))
1546 (funcallable-instance-p (gdefinition name)))))
1548 (defun method-plist-value (method key &optional default)
1549 (let ((plist (if (consp method)
1550 (getf (early-method-initargs method) 'plist)
1551 (object-plist method))))
1552 (getf plist key default)))
1554 (defun (setf method-plist-value) (new-value method key &optional default)
1555 (if (consp method)
1556 (setf (getf (getf (early-method-initargs method) 'plist) key default)
1557 new-value)
1558 (setf (getf (object-plist method) key default) new-value)))
1560 (defun load-defmethod (class name quals specls ll initargs source-location)
1561 (let ((method-cell (getf initargs 'method-cell)))
1562 (setq initargs (copy-tree initargs))
1563 (when method-cell
1564 (setf (getf initargs 'method-cell) method-cell))
1565 #+nil
1566 (setf (getf (getf initargs 'plist) :name)
1567 (make-method-spec name quals specls))
1568 (load-defmethod-internal class name quals specls
1569 ll initargs source-location)))
1571 (defun load-defmethod-internal
1572 (method-class gf-spec qualifiers specializers lambda-list
1573 initargs source-location)
1574 (when (and (eq **boot-state** 'complete)
1575 (fboundp gf-spec))
1576 (let* ((gf (fdefinition gf-spec))
1577 (method (and (generic-function-p gf)
1578 (generic-function-methods gf)
1579 (find-method gf qualifiers specializers nil))))
1580 (when method
1581 (warn 'sb-kernel:redefinition-with-defmethod
1582 :name gf-spec
1583 :new-location source-location
1584 :old-method method
1585 :qualifiers qualifiers :specializers specializers))))
1586 (let ((method (apply #'add-named-method
1587 gf-spec qualifiers specializers lambda-list
1588 :definition-source source-location
1589 initargs)))
1590 (unless (or (eq method-class 'standard-method)
1591 (eq (find-class method-class nil) (class-of method)))
1592 ;; FIXME: should be STYLE-WARNING?
1593 (format *error-output*
1594 "~&At the time the method with qualifiers ~:S and~%~
1595 specializers ~:S on the generic function ~S~%~
1596 was compiled, the method-class for that generic function was~%~
1597 ~S. But, the method class is now ~S, this~%~
1598 may mean that this method was compiled improperly.~%"
1599 qualifiers specializers gf-spec
1600 method-class (class-name (class-of method))))
1601 method))
1603 (defun make-method-spec (gf qualifiers specializers)
1604 (let ((name (generic-function-name gf))
1605 (unparsed-specializers (unparse-specializers gf specializers)))
1606 `(slow-method ,name ,@qualifiers ,unparsed-specializers)))
1608 (defun initialize-method-function (initargs method)
1609 (let* ((mf (getf initargs :function))
1610 (mff (and (typep mf '%method-function)
1611 (%method-function-fast-function mf)))
1612 (plist (getf initargs 'plist))
1613 (name (getf plist :name))
1614 (method-cell (getf initargs 'method-cell)))
1615 (when method-cell
1616 (setf (car method-cell) method))
1617 (when name
1618 (when mf
1619 (setq mf (set-fun-name mf name)))
1620 (when (and mff (consp name) (eq (car name) 'slow-method))
1621 (let ((fast-name `(fast-method ,@(cdr name))))
1622 (set-fun-name mff fast-name))))
1623 (when plist
1624 (let ((plist plist))
1625 (let ((snl (getf plist :slot-name-lists)))
1626 (when snl
1627 (setf (method-plist-value method :pv-table)
1628 (intern-pv-table :slot-name-lists snl))))))))
1630 (defun analyze-lambda-list (lambda-list)
1631 (multiple-value-bind (llks required optional rest keywords)
1632 ;; We say "&MUMBLE is not allowed in a generic function lambda list"
1633 ;; whether this is called by DEFMETHOD or DEFGENERIC.
1634 ;; [It is used for either. Why else recognize and silently ignore &AUX?]
1635 (parse-lambda-list lambda-list
1636 :accept (lambda-list-keyword-mask
1637 '(&optional &rest &key &allow-other-keys &aux))
1638 :silent t
1639 :context "a generic function lambda list")
1640 (declare (ignore rest))
1641 (values llks (length required) (length optional)
1642 (mapcar #'parse-key-arg-spec keywords) keywords)))
1644 ;; FIXME: this does more than return an FTYPE from a lambda list -
1645 ;; it unions the type with an existing ctype object. It needs a better name,
1646 ;; and to be reimplemented as "union and call sb-c::ftype-from-lambda-list".
1647 (defun ftype-declaration-from-lambda-list (lambda-list name)
1648 (multiple-value-bind (llks nrequired noptional keywords keyword-parameters)
1649 (analyze-lambda-list lambda-list)
1650 (declare (ignore keyword-parameters))
1651 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1652 (old-ftype (if (fun-type-p old) old nil))
1653 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1654 (old-keys (and old-ftype
1655 (mapcar #'key-info-name
1656 (fun-type-keywords
1657 old-ftype))))
1658 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1659 (old-allowp (and old-ftype
1660 (fun-type-allowp old-ftype)))
1661 (keywords (union old-keys (mapcar #'parse-key-arg-spec keywords))))
1662 `(function ,(append (make-list nrequired :initial-element t)
1663 (when (plusp noptional)
1664 (append '(&optional)
1665 (make-list noptional :initial-element t)))
1666 (when (or (ll-kwds-restp llks) old-restp)
1667 '(&rest t))
1668 (when (or (ll-kwds-keyp llks) old-keysp)
1669 (append '(&key)
1670 (mapcar (lambda (key)
1671 `(,key t))
1672 keywords)
1673 (when (or (ll-kwds-allowp llks) old-allowp)
1674 '(&allow-other-keys)))))
1675 *))))
1677 ;;;; early generic function support
1679 (defvar *!early-generic-functions* ())
1681 ;; CLHS doesn't specify &allow-other-keys here but I guess the supposition
1682 ;; is that they'll be checked by ENSURE-GENERIC-FUNCTION-USING-CLASS.
1683 ;; Except we don't do that either, so I think the blame, if any, lies there
1684 ;; for not catching errant keywords.
1685 (defun ensure-generic-function (fun-name &rest all-keys)
1686 (let ((existing (and (fboundp fun-name)
1687 (gdefinition fun-name))))
1688 (cond ((and existing
1689 (eq **boot-state** 'complete)
1690 (null (generic-function-p existing)))
1691 (generic-clobbers-function fun-name)
1692 (fmakunbound fun-name)
1693 (apply #'ensure-generic-function fun-name all-keys))
1695 (apply #'ensure-generic-function-using-class
1696 existing fun-name all-keys)))))
1698 (defun generic-clobbers-function (fun-name)
1699 (cerror "Replace the function binding"
1700 'simple-program-error
1701 :format-control "~S already names an ordinary function or a macro."
1702 :format-arguments (list fun-name)))
1704 (defvar *sgf-wrapper*
1705 (!boot-make-wrapper (early-class-size 'standard-generic-function)
1706 'standard-generic-function))
1708 (defvar *sgf-slots-init*
1709 (mapcar (lambda (canonical-slot)
1710 (if (memq (getf canonical-slot :name) '(arg-info source))
1711 +slot-unbound+
1712 (let ((initfunction (getf canonical-slot :initfunction)))
1713 (if initfunction
1714 (funcall initfunction)
1715 +slot-unbound+))))
1716 (early-collect-inheritance 'standard-generic-function)))
1718 (defconstant +sgf-method-class-index+
1719 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1721 (defun early-gf-p (x)
1722 (and (fsc-instance-p x)
1723 (eq (clos-slots-ref (get-slots x) +sgf-method-class-index+)
1724 +slot-unbound+)))
1726 (defconstant +sgf-methods-index+
1727 (!bootstrap-slot-index 'standard-generic-function 'methods))
1729 (defmacro early-gf-methods (gf)
1730 `(clos-slots-ref (get-slots ,gf) +sgf-methods-index+))
1732 (defun safe-generic-function-methods (generic-function)
1733 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1734 (clos-slots-ref (get-slots generic-function) +sgf-methods-index+)
1735 (generic-function-methods generic-function)))
1737 (defconstant +sgf-arg-info-index+
1738 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1740 (defmacro early-gf-arg-info (gf)
1741 `(clos-slots-ref (get-slots ,gf) +sgf-arg-info-index+))
1743 (defconstant +sgf-dfun-state-index+
1744 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1746 (defstruct (arg-info
1747 (:conc-name nil)
1748 (:constructor make-arg-info ())
1749 (:copier nil))
1750 (arg-info-lambda-list :no-lambda-list)
1751 arg-info-precedence
1752 arg-info-metatypes
1753 arg-info-number-optional
1754 arg-info-key/rest-p
1755 arg-info-keys ;nil no &KEY or &REST allowed
1756 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1757 ;T must have &KEY or &REST
1759 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1760 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1762 gf-info-static-c-a-m-emf
1763 (gf-info-c-a-m-emf-std-p t)
1764 gf-info-fast-mf-p)
1766 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1768 (defun arg-info-valid-p (arg-info)
1769 (not (null (arg-info-number-optional arg-info))))
1771 (defun arg-info-applyp (arg-info)
1772 (or (plusp (arg-info-number-optional arg-info))
1773 (arg-info-key/rest-p arg-info)))
1775 (defun arg-info-number-required (arg-info)
1776 (length (arg-info-metatypes arg-info)))
1778 (defun arg-info-nkeys (arg-info)
1779 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1781 (defun create-gf-lambda-list (lambda-list)
1782 ;;; Create a gf lambda list from a method lambda list
1783 (loop for x in lambda-list
1784 collect (if (consp x) (list (car x)) x)
1785 if (eq x '&key) do (loop-finish)))
1787 (defun ll-keyp-or-restp (bits)
1788 (logtest (lambda-list-keyword-mask '(&key &rest)) bits))
1790 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1791 argument-precedence-order)
1792 (let* ((arg-info (if (eq **boot-state** 'complete)
1793 (gf-arg-info gf)
1794 (early-gf-arg-info gf)))
1795 (methods (if (eq **boot-state** 'complete)
1796 (generic-function-methods gf)
1797 (early-gf-methods gf)))
1798 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1799 (first-p (and new-method (null (cdr methods)))))
1800 (when (and (not lambda-list-p) methods)
1801 (setq lambda-list (gf-lambda-list gf)))
1802 (when (or lambda-list-p
1803 (and first-p
1804 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1805 (multiple-value-bind (llks nreq nopt keywords)
1806 (analyze-lambda-list lambda-list)
1807 (when (and methods (not first-p))
1808 (let ((gf-nreq (arg-info-number-required arg-info))
1809 (gf-nopt (arg-info-number-optional arg-info))
1810 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1811 (unless (and (= nreq gf-nreq)
1812 (= nopt gf-nopt)
1813 (eq (ll-keyp-or-restp llks) gf-key/rest-p))
1814 (error "The lambda-list ~S is incompatible with ~
1815 existing methods of ~S."
1816 lambda-list gf))))
1817 (setf (arg-info-lambda-list arg-info)
1818 (if lambda-list-p
1819 lambda-list
1820 (create-gf-lambda-list lambda-list)))
1821 (when (or lambda-list-p argument-precedence-order
1822 (null (arg-info-precedence arg-info)))
1823 (setf (arg-info-precedence arg-info)
1824 (compute-precedence lambda-list nreq argument-precedence-order)))
1825 (setf (arg-info-metatypes arg-info) (make-list nreq))
1826 (setf (arg-info-number-optional arg-info) nopt)
1827 (setf (arg-info-key/rest-p arg-info) (ll-keyp-or-restp llks))
1828 (setf (arg-info-keys arg-info)
1829 (if lambda-list-p
1830 (if (ll-kwds-allowp llks) t keywords)
1831 (arg-info-key/rest-p arg-info)))))
1832 (when new-method
1833 (check-method-arg-info gf arg-info new-method))
1834 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1835 arg-info))
1837 (defun check-method-arg-info (gf arg-info method)
1838 (multiple-value-bind (llks nreq nopt keywords)
1839 (analyze-lambda-list (if (consp method)
1840 (early-method-lambda-list method)
1841 (method-lambda-list method)))
1842 (flet ((lose (string &rest args)
1843 (error 'simple-program-error
1844 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1845 to the generic function~2I~_~S;~I~_~
1846 but ~?~:>"
1847 :format-arguments (list method gf string args)))
1848 (comparison-description (x y)
1849 (if (> x y) "more" "fewer")))
1850 (let ((gf-nreq (arg-info-number-required arg-info))
1851 (gf-nopt (arg-info-number-optional arg-info))
1852 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1853 (gf-keywords (arg-info-keys arg-info)))
1854 (unless (= nreq gf-nreq)
1855 (lose
1856 "the method has ~A required arguments than the generic function."
1857 (comparison-description nreq gf-nreq)))
1858 (unless (= nopt gf-nopt)
1859 (lose
1860 "the method has ~A optional arguments than the generic function."
1861 (comparison-description nopt gf-nopt)))
1862 (unless (eq (ll-keyp-or-restp llks) gf-key/rest-p)
1863 (lose
1864 "the method and generic function differ in whether they accept~_~
1865 &REST or &KEY arguments."))
1866 (when (consp gf-keywords)
1867 (unless (or (and (ll-kwds-restp llks) (not (ll-kwds-keyp llks)))
1868 (ll-kwds-allowp llks)
1869 (every (lambda (k) (memq k keywords)) gf-keywords))
1870 (lose "the method does not accept each of the &KEY arguments~2I~_~
1871 ~S."
1872 gf-keywords)))))))
1874 (defconstant +sm-specializers-index+
1875 (!bootstrap-slot-index 'standard-method 'specializers))
1876 (defconstant +sm-%function-index+
1877 (!bootstrap-slot-index 'standard-method '%function))
1878 (defconstant +sm-qualifiers-index+
1879 (!bootstrap-slot-index 'standard-method 'qualifiers))
1881 ;;; FIXME: we don't actually need this; we could test for the exact
1882 ;;; class and deal with it as appropriate. In fact we probably don't
1883 ;;; need it anyway because we only use this for METHOD-SPECIALIZERS on
1884 ;;; the standard reader method for METHOD-SPECIALIZERS. Probably.
1885 (dolist (s '(specializers %function))
1886 (aver (= (symbol-value (intern (format nil "+SM-~A-INDEX+" s)))
1887 (!bootstrap-slot-index 'standard-reader-method s)
1888 (!bootstrap-slot-index 'standard-writer-method s)
1889 (!bootstrap-slot-index 'standard-boundp-method s)
1890 (!bootstrap-slot-index 'global-reader-method s)
1891 (!bootstrap-slot-index 'global-writer-method s)
1892 (!bootstrap-slot-index 'global-boundp-method s))))
1894 (defvar *standard-method-class-names*
1895 '(standard-method standard-reader-method
1896 standard-writer-method standard-boundp-method
1897 global-reader-method global-writer-method
1898 global-boundp-method))
1900 (declaim (list **standard-method-classes**))
1901 (defglobal **standard-method-classes** nil)
1903 (defun safe-method-specializers (method)
1904 (if (member (class-of method) **standard-method-classes** :test #'eq)
1905 (clos-slots-ref (std-instance-slots method) +sm-specializers-index+)
1906 (method-specializers method)))
1907 (defun safe-method-fast-function (method)
1908 (let ((mf (safe-method-function method)))
1909 (and (typep mf '%method-function)
1910 (%method-function-fast-function mf))))
1911 (defun safe-method-function (method)
1912 (if (member (class-of method) **standard-method-classes** :test #'eq)
1913 (clos-slots-ref (std-instance-slots method) +sm-%function-index+)
1914 (method-function method)))
1915 (defun safe-method-qualifiers (method)
1916 (if (member (class-of method) **standard-method-classes** :test #'eq)
1917 (clos-slots-ref (std-instance-slots method) +sm-qualifiers-index+)
1918 (method-qualifiers method)))
1920 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1921 (let* ((existing-p (and methods (cdr methods) new-method))
1922 (nreq (length (arg-info-metatypes arg-info)))
1923 (metatypes (if existing-p
1924 (arg-info-metatypes arg-info)
1925 (make-list nreq)))
1926 (type (if existing-p
1927 (gf-info-simple-accessor-type arg-info)
1928 nil)))
1929 (when (arg-info-valid-p arg-info)
1930 (dolist (method (if new-method (list new-method) methods))
1931 (let* ((specializers (if (or (eq **boot-state** 'complete)
1932 (not (consp method)))
1933 (safe-method-specializers method)
1934 (early-method-specializers method t)))
1935 (class (if (or (eq **boot-state** 'complete) (not (consp method)))
1936 (class-of method)
1937 (early-method-class method)))
1938 (new-type
1939 (when (and class
1940 (or (not (eq **boot-state** 'complete))
1941 (eq (generic-function-method-combination gf)
1942 *standard-method-combination*)))
1943 (cond ((or (eq class *the-class-standard-reader-method*)
1944 (eq class *the-class-global-reader-method*))
1945 'reader)
1946 ((or (eq class *the-class-standard-writer-method*)
1947 (eq class *the-class-global-writer-method*))
1948 'writer)
1949 ((or (eq class *the-class-standard-boundp-method*)
1950 (eq class *the-class-global-boundp-method*))
1951 'boundp)))))
1952 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1953 (setq type (cond ((null type) new-type)
1954 ((eq type new-type) type)
1955 (t nil)))))
1956 (setf (arg-info-metatypes arg-info) metatypes)
1957 (setf (gf-info-simple-accessor-type arg-info) type)))
1958 (when (or (not was-valid-p) first-p)
1959 (multiple-value-bind (c-a-m-emf std-p)
1960 (if (early-gf-p gf)
1961 (values t t)
1962 (compute-applicable-methods-emf gf))
1963 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1964 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1965 (unless (gf-info-c-a-m-emf-std-p arg-info)
1966 (setf (gf-info-simple-accessor-type arg-info) t))))
1967 (unless was-valid-p
1968 (let ((name (if (eq **boot-state** 'complete)
1969 (generic-function-name gf)
1970 (!early-gf-name gf))))
1971 (setf (gf-precompute-dfun-and-emf-p arg-info)
1972 (cond
1973 ((and (consp name)
1974 (member (car name)
1975 *internal-pcl-generalized-fun-name-symbols*))
1976 nil)
1977 (t (let* ((symbol (fun-name-block-name name))
1978 (package (symbol-package symbol)))
1979 (and (or (eq package *pcl-package*)
1980 (memq package (package-use-list *pcl-package*)))
1981 (not (eq package *cl-package*))
1982 ;; FIXME: this test will eventually be
1983 ;; superseded by the *internal-pcl...* test,
1984 ;; above. While we are in a process of
1985 ;; transition, however, it should probably
1986 ;; remain.
1987 (not (find #\Space (symbol-name symbol))))))))))
1988 (setf (gf-info-fast-mf-p arg-info)
1989 (or (not (eq **boot-state** 'complete))
1990 (let* ((method-class (generic-function-method-class gf))
1991 (methods (compute-applicable-methods
1992 #'make-method-lambda
1993 (list gf (class-prototype method-class)
1994 '(lambda) nil))))
1995 (and methods (null (cdr methods))
1996 (let ((specls (method-specializers (car methods))))
1997 (and (classp (car specls))
1998 (eq 'standard-generic-function
1999 (class-name (car specls)))
2000 (classp (cadr specls))
2001 (eq 'standard-method
2002 (class-name (cadr specls)))))))))
2003 arg-info)
2005 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
2007 ;;; The STATIC-SLOTS field of the funcallable instances used as early
2008 ;;; generic functions is used to store the early methods and early
2009 ;;; discriminator code for the early generic function. The static
2010 ;;; slots field of the fins contains a list whose:
2011 ;;; CAR - a list of the early methods on this early gf
2012 ;;; CADR - the early discriminator code for this method
2013 (defun ensure-generic-function-using-class (existing spec &rest keys
2014 &key (lambda-list nil
2015 lambda-list-p)
2016 argument-precedence-order
2017 definition-source
2018 documentation
2019 &allow-other-keys)
2020 (declare (ignore keys))
2021 (cond ((and existing (early-gf-p existing))
2022 (when lambda-list-p
2023 (set-arg-info existing :lambda-list lambda-list))
2024 existing)
2025 ((assoc spec *!generic-function-fixups* :test #'equal)
2026 (if existing
2027 (make-early-gf spec lambda-list lambda-list-p existing
2028 argument-precedence-order definition-source
2029 documentation)
2030 (bug "The function ~S is not already defined." spec)))
2031 (existing
2032 (bug "~S should be on the list ~S."
2033 spec '*!generic-function-fixups*))
2035 (pushnew spec *!early-generic-functions* :test #'equal)
2036 (make-early-gf spec lambda-list lambda-list-p nil
2037 argument-precedence-order definition-source
2038 documentation))))
2040 (defun make-early-gf (spec &optional lambda-list lambda-list-p
2041 function argument-precedence-order source-location
2042 documentation)
2043 (let ((fin (allocate-standard-funcallable-instance
2044 *sgf-wrapper* *sgf-slots-init*)))
2045 (set-funcallable-instance-function
2047 (or function
2048 (if (eq spec 'print-object)
2049 #'(lambda (instance stream)
2050 (print-unreadable-object (instance stream :identity t)
2051 (format stream "std-instance")))
2052 #'(lambda (&rest args)
2053 (declare (ignore args))
2054 (error "The function of the funcallable-instance ~S~
2055 has not been set." fin)))))
2056 (setf (gdefinition spec) fin)
2057 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
2058 (!bootstrap-set-slot 'standard-generic-function fin
2059 'source source-location)
2060 (!bootstrap-set-slot 'standard-generic-function fin
2061 '%documentation documentation)
2062 (set-fun-name fin spec)
2063 (let ((arg-info (make-arg-info)))
2064 (setf (early-gf-arg-info fin) arg-info)
2065 (when lambda-list-p
2066 (setf (info :function :type spec)
2067 (specifier-type
2068 (ftype-declaration-from-lambda-list lambda-list spec))
2069 (info :function :where-from spec) :defined-method)
2070 (if argument-precedence-order
2071 (set-arg-info fin
2072 :lambda-list lambda-list
2073 :argument-precedence-order argument-precedence-order)
2074 (set-arg-info fin :lambda-list lambda-list))))
2075 fin))
2077 (defun safe-gf-dfun-state (generic-function)
2078 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2079 (clos-slots-ref (fsc-instance-slots generic-function) +sgf-dfun-state-index+)
2080 (gf-dfun-state generic-function)))
2081 (defun (setf safe-gf-dfun-state) (new-value generic-function)
2082 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2083 (setf (clos-slots-ref (fsc-instance-slots generic-function)
2084 +sgf-dfun-state-index+)
2085 new-value)
2086 (setf (gf-dfun-state generic-function) new-value)))
2088 (defun set-dfun (gf &optional dfun cache info)
2089 (let ((new-state (if (and dfun (or cache info))
2090 (list* dfun cache info)
2091 dfun)))
2092 (cond
2093 ((eq **boot-state** 'complete)
2094 ;; Check that we are under the lock.
2095 #+sb-thread
2096 (aver (eq sb-thread:*current-thread* (sb-thread:mutex-owner (gf-lock gf))))
2097 (setf (safe-gf-dfun-state gf) new-state))
2099 (setf (clos-slots-ref (get-slots gf) +sgf-dfun-state-index+)
2100 new-state))))
2101 dfun)
2103 (defun gf-dfun-cache (gf)
2104 (let ((state (if (eq **boot-state** 'complete)
2105 (safe-gf-dfun-state gf)
2106 (clos-slots-ref (get-slots gf) +sgf-dfun-state-index+))))
2107 (typecase state
2108 (function nil)
2109 (cons (cadr state)))))
2111 (defun gf-dfun-info (gf)
2112 (let ((state (if (eq **boot-state** 'complete)
2113 (safe-gf-dfun-state gf)
2114 (clos-slots-ref (get-slots gf) +sgf-dfun-state-index+))))
2115 (typecase state
2116 (function nil)
2117 (cons (cddr state)))))
2119 (defconstant +sgf-name-index+
2120 (!bootstrap-slot-index 'standard-generic-function 'name))
2122 (defun !early-gf-name (gf)
2123 (clos-slots-ref (get-slots gf) +sgf-name-index+))
2125 (defun gf-lambda-list (gf)
2126 (let ((arg-info (if (eq **boot-state** 'complete)
2127 (gf-arg-info gf)
2128 (early-gf-arg-info gf))))
2129 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
2130 (let ((methods (if (eq **boot-state** 'complete)
2131 (generic-function-methods gf)
2132 (early-gf-methods gf))))
2133 (if (null methods)
2134 (progn
2135 (warn "no way to determine the lambda list for ~S" gf)
2136 nil)
2137 (let* ((method (car (last methods)))
2138 (ll (if (consp method)
2139 (early-method-lambda-list method)
2140 (method-lambda-list method))))
2141 (create-gf-lambda-list ll))))
2142 (arg-info-lambda-list arg-info))))
2144 (defmacro real-ensure-gf-internal (gf-class all-keys env)
2145 `(progn
2146 (cond ((symbolp ,gf-class)
2147 (setq ,gf-class (find-class ,gf-class t ,env)))
2148 ((classp ,gf-class))
2150 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
2151 class nor a symbol that names a class."
2152 ,gf-class)))
2153 (unless (class-finalized-p ,gf-class)
2154 (if (class-has-a-forward-referenced-superclass-p ,gf-class)
2155 ;; FIXME: reference MOP documentation -- this is an
2156 ;; additional requirement on our users
2157 (error "The generic function class ~S is not finalizeable" ,gf-class)
2158 (finalize-inheritance ,gf-class)))
2159 (remf ,all-keys :generic-function-class)
2160 (remf ,all-keys :environment)
2161 (let ((combin (getf ,all-keys :method-combination)))
2162 (etypecase combin
2163 (cons
2164 (setf (getf ,all-keys :method-combination)
2165 (find-method-combination (class-prototype ,gf-class)
2166 (car combin)
2167 (cdr combin))))
2168 ((or null method-combination))))
2169 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
2170 (unless (eq method-class '.shes-not-there.)
2171 (setf (getf ,all-keys :method-class)
2172 (cond ((classp method-class)
2173 method-class)
2174 (t (find-class method-class t ,env))))))))
2176 (defun note-gf-signature (fun-name lambda-list-p lambda-list)
2177 (unless lambda-list-p
2178 ;; Use the existing lambda-list, if any. It is reasonable to do eg.
2180 ;; (if (fboundp name)
2181 ;; (ensure-generic-function name)
2182 ;; (ensure-generic-function name :lambda-list '(foo)))
2184 ;; in which case we end up here with no lambda-list in the first leg.
2185 (setf (values lambda-list lambda-list-p)
2186 (handler-case
2187 (values (generic-function-lambda-list (fdefinition fun-name))
2189 ((or warning error) ()
2190 (values nil nil)))))
2191 (let ((gf-type
2192 (specifier-type
2193 (if lambda-list-p
2194 (ftype-declaration-from-lambda-list lambda-list fun-name)
2195 'function)))
2196 (old-type nil))
2197 ;; FIXME: Ideally we would like to not clobber it, but because generic
2198 ;; functions assert their FTYPEs callers believing the FTYPE are left with
2199 ;; unsafe assumptions. Hence the clobbering. Be quiet when the new type
2200 ;; is a subtype of the old one, though -- even though the type is not
2201 ;; trusted anymore, the warning is still not quite as interesting.
2202 (when (and (eq :declared (info :function :where-from fun-name))
2203 (not (csubtypep gf-type (setf old-type (info :function :type fun-name)))))
2204 (style-warn "~@<Generic function ~S clobbers an earlier ~S proclamation ~S ~
2205 for the same name with ~S.~:@>"
2206 fun-name 'ftype
2207 (type-specifier old-type)
2208 (type-specifier gf-type)))
2209 (setf (info :function :type fun-name) gf-type
2210 (info :function :where-from fun-name) :defined-method)
2211 fun-name))
2213 (defun real-ensure-gf-using-class--generic-function
2214 (existing
2215 fun-name
2216 &rest all-keys
2217 &key environment (lambda-list nil lambda-list-p)
2218 (generic-function-class 'standard-generic-function)
2219 &allow-other-keys)
2220 (real-ensure-gf-internal generic-function-class all-keys environment)
2221 ;; KLUDGE: the above macro does SETQ on GENERIC-FUNCTION-CLASS,
2222 ;; which is what makes the next line work
2223 (unless (eq (class-of existing) generic-function-class)
2224 (change-class existing generic-function-class))
2225 (prog1
2226 (apply #'reinitialize-instance existing all-keys)
2227 (note-gf-signature fun-name lambda-list-p lambda-list)))
2229 (defun real-ensure-gf-using-class--null
2230 (existing
2231 fun-name
2232 &rest all-keys
2233 &key environment (lambda-list nil lambda-list-p)
2234 (generic-function-class 'standard-generic-function)
2235 &allow-other-keys)
2236 (declare (ignore existing))
2237 (real-ensure-gf-internal generic-function-class all-keys environment)
2238 (prog1
2239 (setf (gdefinition fun-name)
2240 (apply #'make-instance generic-function-class
2241 :name fun-name all-keys))
2242 (note-gf-signature fun-name lambda-list-p lambda-list)))
2244 (defun safe-gf-arg-info (generic-function)
2245 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2246 (clos-slots-ref (fsc-instance-slots generic-function)
2247 +sgf-arg-info-index+)
2248 (gf-arg-info generic-function)))
2250 ;;; FIXME: this function took on a slightly greater role than it
2251 ;;; previously had around 2005-11-02, when CSR fixed the bug whereby
2252 ;;; having more than one subclass of standard-generic-function caused
2253 ;;; the whole system to die horribly through a metacircle in
2254 ;;; GF-ARG-INFO. The fix is to be slightly more disciplined about
2255 ;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when
2256 ;;; computing discriminating functions, so we need to be careful about
2257 ;;; having a base case for the recursion, and we provide that with the
2258 ;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet)
2259 ;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to
2260 ;;; that stage, where all potentially dangerous cases are enumerated
2261 ;;; and stopped. -- CSR, 2005-11-02.
2262 (defun get-generic-fun-info (gf)
2263 ;; values nreq applyp metatypes nkeys arg-info
2264 (multiple-value-bind (applyp metatypes arg-info)
2265 (let* ((arg-info (if (early-gf-p gf)
2266 (early-gf-arg-info gf)
2267 (safe-gf-arg-info gf)))
2268 (metatypes (arg-info-metatypes arg-info)))
2269 (values (arg-info-applyp arg-info)
2270 metatypes
2271 arg-info))
2272 (let ((nreq 0)
2273 (nkeys 0))
2274 (declare (fixnum nreq nkeys))
2275 (dolist (x metatypes)
2276 (incf nreq)
2277 (unless (eq x t)
2278 (incf nkeys)))
2279 (values nreq applyp metatypes
2280 nkeys
2281 arg-info))))
2283 (defun generic-function-nreq (gf)
2284 (let* ((arg-info (if (early-gf-p gf)
2285 (early-gf-arg-info gf)
2286 (safe-gf-arg-info gf)))
2287 (metatypes (arg-info-metatypes arg-info)))
2288 (declare (list metatypes))
2289 (length metatypes)))
2291 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
2292 &key slot-name object-class method-class-function
2293 definition-source)
2294 (let ((parsed ())
2295 (unparsed ()))
2296 ;; Figure out whether we got class objects or class names as the
2297 ;; specializers and set parsed and unparsed appropriately. If we
2298 ;; got class objects, then we can compute unparsed, but if we got
2299 ;; class names we don't try to compute parsed.
2301 ;; Note that the use of not symbolp in this call to every should be
2302 ;; read as 'classp' we can't use classp itself because it doesn't
2303 ;; exist yet.
2304 (if (every (lambda (s) (not (symbolp s))) specializers)
2305 (setq parsed specializers
2306 unparsed (mapcar (lambda (s)
2307 (if (eq s t) t (class-name s)))
2308 specializers))
2309 (setq unparsed specializers
2310 parsed ()))
2311 (let ((result
2312 (list :early-method
2314 (getf initargs :function)
2315 (let ((mf (getf initargs :function)))
2316 (aver mf)
2317 (and (typep mf '%method-function)
2318 (%method-function-fast-function mf)))
2320 ;; the parsed specializers. This is used by
2321 ;; EARLY-METHOD-SPECIALIZERS to cache the parse.
2322 ;; Note that this only comes into play when there is
2323 ;; more than one early method on an early gf.
2324 parsed
2326 ;; A list to which REAL-MAKE-A-METHOD can be applied
2327 ;; to make a real method corresponding to this early
2328 ;; one.
2329 (append
2330 (list class qualifiers arglist unparsed
2331 initargs doc)
2332 (when slot-name
2333 (list :slot-name slot-name :object-class object-class
2334 :method-class-function method-class-function))
2335 (list :definition-source definition-source)))))
2336 (initialize-method-function initargs result)
2337 result)))
2339 (defun real-make-a-method
2340 (class qualifiers lambda-list specializers initargs doc
2341 &rest args &key slot-name object-class method-class-function
2342 definition-source)
2343 (if method-class-function
2344 (let* ((object-class (if (classp object-class) object-class
2345 (find-class object-class)))
2346 (slots (class-direct-slots object-class))
2347 (slot-definition (find slot-name slots
2348 :key #'slot-definition-name)))
2349 (aver slot-name)
2350 (aver slot-definition)
2351 (let ((initargs (list* :qualifiers qualifiers :lambda-list lambda-list
2352 :specializers specializers :documentation doc
2353 :slot-definition slot-definition
2354 :slot-name slot-name initargs)))
2355 (apply #'make-instance
2356 (apply method-class-function object-class slot-definition
2357 initargs)
2358 :definition-source definition-source
2359 initargs)))
2360 (apply #'make-instance class :qualifiers qualifiers
2361 :lambda-list lambda-list :specializers specializers
2362 :documentation doc (append args initargs))))
2364 (defun early-method-function (early-method)
2365 (values (cadr early-method) (caddr early-method)))
2367 (defun early-method-class (early-method)
2368 (find-class (car (fifth early-method))))
2370 (defun early-method-standard-accessor-p (early-method)
2371 (let ((class (first (fifth early-method))))
2372 (or (eq class 'standard-reader-method)
2373 (eq class 'standard-writer-method)
2374 (eq class 'standard-boundp-method))))
2376 (defun early-method-standard-accessor-slot-name (early-method)
2377 (eighth (fifth early-method)))
2379 ;;; Fetch the specializers of an early method. This is basically just
2380 ;;; a simple accessor except that when the second argument is t, this
2381 ;;; converts the specializers from symbols into class objects. The
2382 ;;; class objects are cached in the early method, this makes
2383 ;;; bootstrapping faster because the class objects only have to be
2384 ;;; computed once.
2386 ;;; NOTE:
2387 ;;; The second argument should only be passed as T by
2388 ;;; early-lookup-method. This is to implement the rule that only when
2389 ;;; there is more than one early method on a generic function is the
2390 ;;; conversion from class names to class objects done. This
2391 ;;; corresponds to the fact that we are only allowed to have one
2392 ;;; method on any generic function up until the time classes exist.
2393 (defun early-method-specializers (early-method &optional objectsp)
2394 (if (and (listp early-method)
2395 (eq (car early-method) :early-method))
2396 (cond ((eq objectsp t)
2397 (or (fourth early-method)
2398 (setf (fourth early-method)
2399 (mapcar #'find-class (cadddr (fifth early-method))))))
2401 (fourth (fifth early-method))))
2402 (error "~S is not an early-method." early-method)))
2404 (defun early-method-qualifiers (early-method)
2405 (second (fifth early-method)))
2407 (defun early-method-lambda-list (early-method)
2408 (third (fifth early-method)))
2410 (defun early-method-initargs (early-method)
2411 (fifth (fifth early-method)))
2413 (defun (setf early-method-initargs) (new-value early-method)
2414 (setf (fifth (fifth early-method)) new-value))
2416 (defun early-add-named-method (generic-function-name qualifiers
2417 specializers arglist &rest initargs
2418 &key documentation definition-source
2419 &allow-other-keys)
2420 (let* (;; we don't need to deal with the :generic-function-class
2421 ;; argument here because the default,
2422 ;; STANDARD-GENERIC-FUNCTION, is right for all early generic
2423 ;; functions. (See REAL-ADD-NAMED-METHOD)
2424 (gf (ensure-generic-function generic-function-name))
2425 (existing
2426 (dolist (m (early-gf-methods gf))
2427 (when (and (equal (early-method-specializers m) specializers)
2428 (equal (early-method-qualifiers m) qualifiers))
2429 (return m)))))
2430 (setf (getf (getf initargs 'plist) :name)
2431 (make-method-spec gf qualifiers specializers))
2432 (let ((new (make-a-method 'standard-method qualifiers arglist
2433 specializers initargs documentation
2434 :definition-source definition-source)))
2435 (when existing (remove-method gf existing))
2436 (add-method gf new))))
2438 ;;; This is the early version of ADD-METHOD. Later this will become a
2439 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2440 ;;; special knowledge about ADD-METHOD.
2441 (defun add-method (generic-function method)
2442 (when (not (fsc-instance-p generic-function))
2443 (error "Early ADD-METHOD didn't get a funcallable instance."))
2444 (when (not (and (listp method) (eq (car method) :early-method)))
2445 (error "Early ADD-METHOD didn't get an early method."))
2446 (push method (early-gf-methods generic-function))
2447 (set-arg-info generic-function :new-method method)
2448 (unless (assoc (!early-gf-name generic-function)
2449 *!generic-function-fixups*
2450 :test #'equal)
2451 (update-dfun generic-function)))
2453 ;;; This is the early version of REMOVE-METHOD. See comments on
2454 ;;; the early version of ADD-METHOD.
2455 (defun remove-method (generic-function method)
2456 (when (not (fsc-instance-p generic-function))
2457 (error "An early remove-method didn't get a funcallable instance."))
2458 (when (not (and (listp method) (eq (car method) :early-method)))
2459 (error "An early remove-method didn't get an early method."))
2460 (setf (early-gf-methods generic-function)
2461 (remove method (early-gf-methods generic-function)))
2462 (set-arg-info generic-function)
2463 (unless (assoc (!early-gf-name generic-function)
2464 *!generic-function-fixups*
2465 :test #'equal)
2466 (update-dfun generic-function)))
2468 ;;; This is the early version of GET-METHOD. See comments on the early
2469 ;;; version of ADD-METHOD.
2470 (defun get-method (generic-function qualifiers specializers
2471 &optional (errorp t))
2472 (if (early-gf-p generic-function)
2473 (or (dolist (m (early-gf-methods generic-function))
2474 (when (and (or (equal (early-method-specializers m nil)
2475 specializers)
2476 (equal (early-method-specializers m t)
2477 specializers))
2478 (equal (early-method-qualifiers m) qualifiers))
2479 (return m)))
2480 (if errorp
2481 (error "can't get early method")
2482 nil))
2483 (real-get-method generic-function qualifiers specializers errorp)))
2485 ;; minor KLUDGE: a separate code component for this function allows GCing
2486 ;; a few symbols and their associated code that would otherwise be retained:
2487 ;; *!EARLY-{GENERIC-}FUNCTIONS*, *!GENERIC-FUNCTION-FIXUPS*
2488 (defun early-gf-primary-slow-method-fn (fn)
2489 (lambda (args next-methods)
2490 (declare (ignore next-methods))
2491 (apply fn args)))
2493 (defun !fix-early-generic-functions ()
2494 (let ((accessors nil))
2495 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2496 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2497 (dolist (early-gf-spec *!early-generic-functions*)
2498 (when (every #'early-method-standard-accessor-p
2499 (early-gf-methods (gdefinition early-gf-spec)))
2500 (push early-gf-spec accessors)))
2501 (dolist (spec (nconc accessors
2502 '(accessor-method-slot-name
2503 generic-function-methods
2504 method-specializers
2505 specializerp
2506 specializer-type
2507 specializer-class
2508 slot-definition-location
2509 slot-definition-name
2510 class-slots
2511 gf-arg-info
2512 class-precedence-list
2513 slot-boundp-using-class
2514 (setf slot-value-using-class)
2515 slot-value-using-class
2516 structure-class-p
2517 standard-class-p
2518 funcallable-standard-class-p
2519 specializerp)))
2520 (/show spec)
2521 (setq *!early-generic-functions*
2522 (cons spec
2523 (delete spec *!early-generic-functions* :test #'equal))))
2525 (dolist (early-gf-spec *!early-generic-functions*)
2526 (/show early-gf-spec)
2527 (let* ((gf (gdefinition early-gf-spec))
2528 (methods (mapcar (lambda (early-method)
2529 (let ((args (copy-list (fifth
2530 early-method))))
2531 (setf (fourth args)
2532 (early-method-specializers
2533 early-method t))
2534 (apply #'real-make-a-method args)))
2535 (early-gf-methods gf))))
2536 (setf (generic-function-method-class gf) *the-class-standard-method*)
2537 (setf (generic-function-method-combination gf)
2538 *standard-method-combination*)
2539 (set-methods gf methods)))
2541 (dolist (fn *!early-functions*)
2542 (/show fn)
2543 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2545 (dolist (fixup *!generic-function-fixups*)
2546 (/show fixup)
2547 (let* ((fspec (car fixup))
2548 (gf (gdefinition fspec))
2549 (methods (mapcar (lambda (method)
2550 (let* ((lambda-list (first method))
2551 (specializers (mapcar #'find-class (second method)))
2552 (method-fn-name (third method))
2553 (fn-name (or method-fn-name fspec))
2554 (fn (fdefinition fn-name))
2555 (initargs
2556 (list :function
2557 (set-fun-name
2558 (early-gf-primary-slow-method-fn fn)
2559 `(call ,fn-name)))))
2560 (declare (type function fn))
2561 (make-a-method 'standard-method
2563 lambda-list
2564 specializers
2565 initargs
2566 nil)))
2567 (cdr fixup))))
2568 (setf (generic-function-method-class gf) *the-class-standard-method*)
2569 (setf (generic-function-method-combination gf)
2570 *standard-method-combination*)
2571 (set-methods gf methods))))
2572 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2574 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2575 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2576 ;;; is really implemented.
2577 (defun parse-defmethod (cdr-of-form)
2578 (declare (list cdr-of-form))
2579 (let ((qualifiers ())
2580 (spec-ll ()))
2581 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2582 (push (pop cdr-of-form) qualifiers)
2583 (return (setq qualifiers (nreverse qualifiers)))))
2584 (setq spec-ll (pop cdr-of-form))
2585 (values qualifiers spec-ll cdr-of-form)))
2587 (defun parse-specializers (generic-function specializers)
2588 (declare (list specializers))
2589 (flet ((parse (spec)
2590 (parse-specializer-using-class generic-function spec)))
2591 (mapcar #'parse specializers)))
2593 (defun unparse-specializers (generic-function specializers)
2594 (declare (list specializers))
2595 (flet ((unparse (spec)
2596 (unparse-specializer-using-class generic-function spec)))
2597 (mapcar #'unparse specializers)))
2599 (defun extract-parameters (specialized-lambda-list)
2600 (multiple-value-bind (parameters ignore1 ignore2)
2601 (parse-specialized-lambda-list specialized-lambda-list)
2602 (declare (ignore ignore1 ignore2))
2603 parameters))
2605 (defun extract-lambda-list (specialized-lambda-list)
2606 (multiple-value-bind (ignore1 lambda-list ignore2)
2607 (parse-specialized-lambda-list specialized-lambda-list)
2608 (declare (ignore ignore1 ignore2))
2609 lambda-list))
2611 (defun extract-specializer-names (specialized-lambda-list)
2612 (multiple-value-bind (ignore1 ignore2 specializers)
2613 (parse-specialized-lambda-list specialized-lambda-list)
2614 (declare (ignore ignore1 ignore2))
2615 specializers))
2617 (defun extract-required-parameters (specialized-lambda-list)
2618 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2619 (parse-specialized-lambda-list specialized-lambda-list)
2620 (declare (ignore ignore1 ignore2 ignore3))
2621 required-parameters))
2623 (define-condition specialized-lambda-list-error
2624 (reference-condition simple-program-error)
2626 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2628 (defun parse-specialized-lambda-list
2629 (arglist
2630 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2631 &aux (specialized-lambda-list-keywords
2632 '(&optional &rest &key &allow-other-keys &aux)))
2633 (let ((arg (car arglist)))
2634 (cond ((null arglist) (values nil nil nil nil))
2635 ((eq arg '&aux)
2636 (values nil arglist nil nil))
2637 ((memq arg lambda-list-keywords)
2638 ;; non-standard lambda-list-keywords are errors.
2639 (unless (memq arg specialized-lambda-list-keywords)
2640 (error 'specialized-lambda-list-error
2641 :format-control "unknown specialized-lambda-list ~
2642 keyword ~S~%"
2643 :format-arguments (list arg)))
2644 ;; no multiple &rest x &rest bla specifying
2645 (when (memq arg supplied-keywords)
2646 (error 'specialized-lambda-list-error
2647 :format-control "multiple occurrence of ~
2648 specialized-lambda-list keyword ~S~%"
2649 :format-arguments (list arg)))
2650 ;; And no placing &key in front of &optional, either.
2651 (unless (memq arg allowed-keywords)
2652 (error 'specialized-lambda-list-error
2653 :format-control "misplaced specialized-lambda-list ~
2654 keyword ~S~%"
2655 :format-arguments (list arg)))
2656 ;; When we are at a lambda-list keyword, the parameters
2657 ;; don't include the lambda-list keyword; the lambda-list
2658 ;; does include the lambda-list keyword; and no
2659 ;; specializers are allowed to follow the lambda-list
2660 ;; keywords (at least for now).
2661 (multiple-value-bind (parameters lambda-list)
2662 (parse-specialized-lambda-list (cdr arglist)
2663 (cons arg supplied-keywords)
2664 (if (eq arg '&key)
2665 (cons '&allow-other-keys
2666 (cdr (member arg allowed-keywords)))
2667 (cdr (member arg allowed-keywords))))
2668 (when (and (eq arg '&rest)
2669 (or (null lambda-list)
2670 (memq (car lambda-list)
2671 specialized-lambda-list-keywords)
2672 (not (or (null (cadr lambda-list))
2673 (memq (cadr lambda-list)
2674 specialized-lambda-list-keywords)))))
2675 (error 'specialized-lambda-list-error
2676 :format-control
2677 "in a specialized-lambda-list, excactly one ~
2678 variable must follow &REST.~%"
2679 :format-arguments nil))
2680 (values parameters
2681 (cons arg lambda-list)
2683 ())))
2684 (supplied-keywords
2685 ;; After a lambda-list keyword there can be no specializers.
2686 (multiple-value-bind (parameters lambda-list)
2687 (parse-specialized-lambda-list (cdr arglist)
2688 supplied-keywords
2689 allowed-keywords)
2690 (values (cons (if (listp arg) (car arg) arg) parameters)
2691 (cons arg lambda-list)
2693 ())))
2695 (multiple-value-bind (parameters lambda-list specializers required)
2696 (parse-specialized-lambda-list (cdr arglist))
2697 ;; Check for valid arguments.
2698 (unless (or (and (symbolp arg) (not (null arg)))
2699 (and (consp arg)
2700 (consp (cdr arg))
2701 (null (cddr arg))))
2702 (error 'specialized-lambda-list-error
2703 :format-control "arg is not a non-NIL symbol or a list of two elements: ~A"
2704 :format-arguments (list arg)))
2705 (values (cons (if (listp arg) (car arg) arg) parameters)
2706 (cons (if (listp arg) (car arg) arg) lambda-list)
2707 (cons (if (listp arg) (cadr arg) t) specializers)
2708 (cons (if (listp arg) (car arg) arg) required)))))))
2710 (setq **boot-state** 'early)
2712 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2713 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2714 ;;; walker stuff was only used for implementing stuff like that; maybe
2715 ;;; it's not needed any more? Hunt down what it was used for and see.
2717 (defun extract-the (form)
2718 (cond ((and (consp form) (eq (car form) 'the))
2719 (aver (proper-list-of-length-p form 3))
2720 (third form))
2722 form)))
2724 (defmacro with-slots (slots instance &body body)
2725 (let ((in (gensym)))
2726 `(let ((,in ,instance))
2727 (declare (ignorable ,in))
2728 ,@(let ((instance (extract-the instance)))
2729 (and (symbolp instance)
2730 `((declare (%variable-rebinding ,in ,instance)))))
2732 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2733 (let ((var-name
2734 (if (symbolp slot-entry)
2735 slot-entry
2736 (car slot-entry)))
2737 (slot-name
2738 (if (symbolp slot-entry)
2739 slot-entry
2740 (cadr slot-entry))))
2741 `(,var-name
2742 (slot-value ,in ',slot-name))))
2743 slots)
2744 ,@body))))
2746 (defmacro with-accessors (slots instance &body body)
2747 (let ((in (gensym)))
2748 `(let ((,in ,instance))
2749 (declare (ignorable ,in))
2750 ,@(let ((instance (extract-the instance)))
2751 (and (symbolp instance)
2752 `((declare (%variable-rebinding ,in ,instance)))))
2754 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2755 (let ((var-name (car slot-entry))
2756 (accessor-name (cadr slot-entry)))
2757 `(,var-name (,accessor-name ,in))))
2758 slots)
2759 ,@body))))