A few small improvements to PARSE-LAMBDA-LIST.
[sbcl.git] / src / pcl / boot.lisp
blob424c65b13d27f1f70e33c800030c512b46bf37c5
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 (let ((method-lambda `(lambda ,unspecialized-lambda-list ,@body))
410 (*method-name* `(,name ,@qualifiers ,specializers))
411 (*method-lambda-list* lambda-list))
412 (multiple-value-bind (method-function-lambda initargs)
413 (make-method-lambda proto-gf proto-method method-lambda env)
414 (let ((initargs-form (make-method-initargs-form
415 proto-gf proto-method method-function-lambda
416 initargs env))
417 (specializers-form (make-method-specializers-form
418 proto-gf proto-method specializers env)))
419 `(progn
420 ;; Note: We could DECLAIM the ftype of the generic function
421 ;; here, since ANSI specifies that we create it if it does
422 ;; not exist. However, I chose not to, because I think it's
423 ;; more useful to support a style of programming where every
424 ;; generic function has an explicit DEFGENERIC and any typos
425 ;; in DEFMETHODs are warned about. Otherwise
427 ;; (DEFGENERIC FOO-BAR-BLETCH (X))
428 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
429 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
430 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
431 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
432 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
434 ;; compiles without raising an error and runs without
435 ;; raising an error (since SIMPLE-VECTOR cases fall through
436 ;; to VECTOR) but still doesn't do what was intended. I hate
437 ;; that kind of bug (code which silently gives the wrong
438 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
439 ,(make-defmethod-form name qualifiers specializers-form
440 unspecialized-lambda-list
441 (if proto-method
442 (class-name (class-of proto-method))
443 'standard-method)
444 initargs-form)))))))
446 (defun interned-symbol-p (x)
447 (and (symbolp x) (symbol-package x)))
449 (defun make-defmethod-form
450 (name qualifiers specializers unspecialized-lambda-list
451 method-class-name initargs-form)
452 (declare (sb-ext:muffle-conditions sb-ext:code-deletion-note))
453 (let (fn
454 fn-lambda)
455 (if (and (interned-symbol-p (fun-name-block-name name))
456 (every #'interned-symbol-p qualifiers)
457 (every (lambda (s)
458 (if (consp s)
459 (and (eq (car s) 'eql)
460 (constantp (cadr s))
461 (let ((sv (constant-form-value (cadr s))))
462 (or (interned-symbol-p sv)
463 (integerp sv)
464 (and (characterp sv)
465 (standard-char-p sv)))))
466 (interned-symbol-p s)))
467 specializers)
468 (consp initargs-form)
469 (eq (car initargs-form) 'list*)
470 (memq (cadr initargs-form) '(:function))
471 (consp (setq fn (caddr initargs-form)))
472 (eq (car fn) 'function)
473 (consp (setq fn-lambda (cadr fn)))
474 (eq (car fn-lambda) 'lambda)
475 (bug "Really got here"))
476 (let* ((specls (mapcar (lambda (specl)
477 (if (consp specl)
478 ;; CONSTANT-FORM-VALUE? What I
479 ;; kind of want to know, though,
480 ;; is what happens if we don't do
481 ;; this for some slow-method
482 ;; function because of a hairy
483 ;; lexenv -- is the only bad
484 ;; effect that the method
485 ;; function ends up unnamed? If
486 ;; so, couldn't we arrange to
487 ;; name it later?
488 `(,(car specl) ,(eval (cadr specl)))
489 specl))
490 specializers))
491 (mname `(,(if (eq (cadr initargs-form) :function)
492 'slow-method 'fast-method)
493 ,name ,@qualifiers ,specls)))
494 `(progn
495 (defun ,mname ,(cadr fn-lambda)
496 ,@(cddr fn-lambda))
497 ,(make-defmethod-form-internal
498 name qualifiers `',specls
499 unspecialized-lambda-list method-class-name
500 `(list* ,(cadr initargs-form)
501 #',mname
502 ,@(cdddr initargs-form)))))
503 (make-defmethod-form-internal
504 name qualifiers
505 specializers
506 #+nil
507 `(list ,@(mapcar (lambda (specializer)
508 (if (consp specializer)
509 ``(,',(car specializer)
510 ,,(cadr specializer))
511 `',specializer))
512 specializers))
513 unspecialized-lambda-list
514 method-class-name
515 initargs-form))))
517 (defun make-defmethod-form-internal
518 (name qualifiers specializers-form unspecialized-lambda-list
519 method-class-name initargs-form)
520 `(load-defmethod
521 ',method-class-name
522 ',name
523 ',qualifiers
524 ,specializers-form
525 ',unspecialized-lambda-list
526 ,initargs-form
527 (sb-c:source-location)))
529 (defmacro make-method-function (method-lambda &environment env)
530 (multiple-value-bind (proto-gf proto-method)
531 (prototypes-for-make-method-lambda nil)
532 (multiple-value-bind (method-function-lambda initargs)
533 (make-method-lambda proto-gf proto-method method-lambda env)
534 (make-method-initargs-form proto-gf
535 proto-method
536 method-function-lambda
537 initargs
538 env))))
540 (defun real-make-method-initargs-form (proto-gf proto-method
541 method-lambda initargs env)
542 (declare (ignore proto-gf proto-method))
543 (unless (and (consp method-lambda)
544 (eq (car method-lambda) 'lambda))
545 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
546 is not a lambda form."
547 method-lambda))
548 (make-method-initargs-form-internal method-lambda initargs env))
550 (unless (fboundp 'make-method-initargs-form)
551 (setf (gdefinition 'make-method-initargs-form)
552 (symbol-function 'real-make-method-initargs-form)))
554 ;;; When bootstrapping PCL MAKE-METHOD-LAMBDA starts out as a regular
555 ;;; functions: REAL-MAKE-METHOD-LAMBDA set to the fdefinition of
556 ;;; MAKE-METHOD-LAMBDA. Once generic functions are born, the
557 ;;; REAL-MAKE-METHOD lambda is used as the body of the default method.
558 ;;; MAKE-METHOD-LAMBDA-INTERNAL is split out into a separate function
559 ;;; so that changing it in a live image is easy, and changes actually
560 ;;; take effect.
561 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
562 (make-method-lambda-internal proto-gf proto-method method-lambda env))
564 (unless (fboundp 'make-method-lambda)
565 (setf (gdefinition 'make-method-lambda)
566 (symbol-function 'real-make-method-lambda)))
568 (defun declared-specials (declarations)
569 (loop for (declare . specifiers) in declarations
570 append (loop for specifier in specifiers
571 when (eq 'special (car specifier))
572 append (cdr specifier))))
574 (defun make-method-lambda-internal (proto-gf proto-method method-lambda env)
575 (declare (ignore proto-gf proto-method))
576 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
577 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
578 is not a lambda form."
579 method-lambda))
580 (multiple-value-bind (real-body declarations documentation)
581 (parse-body (cddr method-lambda))
582 ;; We have the %METHOD-NAME declaration in the place where we expect it only
583 ;; if there is are no non-standard prior MAKE-METHOD-LAMBDA methods -- or
584 ;; unless they're fantastically unintrusive.
585 (let* ((method-name *method-name*)
586 (method-lambda-list *method-lambda-list*)
587 ;; Macroexpansion caused by code-walking may call make-method-lambda and
588 ;; end up with wrong values
589 (*method-name* nil)
590 (*method-lambda-list* nil)
591 (generic-function-name (when method-name (car method-name)))
592 (specialized-lambda-list (or method-lambda-list
593 (ecase (car method-lambda)
594 (lambda (second method-lambda))
595 (named-lambda (third method-lambda)))))
596 ;; the method-cell is a way of communicating what method a
597 ;; method-function implements, for the purpose of
598 ;; NO-NEXT-METHOD. We need something that can be shared
599 ;; between function and initargs, but not something that
600 ;; will be coalesced as a constant (because we are naughty,
601 ;; oh yes) with the expansion of any other methods in the
602 ;; same file. -- CSR, 2007-05-30
603 (method-cell (list (make-symbol "METHOD-CELL"))))
604 (multiple-value-bind (parameters lambda-list specializers)
605 (parse-specialized-lambda-list specialized-lambda-list)
606 (let* ((required-parameters
607 (mapcar (lambda (r s) (declare (ignore s)) r)
608 parameters
609 specializers))
610 (slots (mapcar #'list required-parameters))
611 (class-declarations
612 `(declare
613 ;; These declarations seem to be used by PCL to pass
614 ;; information to itself; when I tried to delete 'em
615 ;; ca. 0.6.10 it didn't work. I'm not sure how
616 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
617 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
618 ,@(remove nil
619 (mapcar (lambda (a s) (and (symbolp s)
620 (neq s t)
621 `(%class ,a ,s)))
622 parameters
623 specializers))
624 ;; These TYPE declarations weren't in the original
625 ;; PCL code, but the Python compiler likes them a
626 ;; lot. (We're telling the compiler about our
627 ;; knowledge of specialized argument types so that
628 ;; it can avoid run-time type dispatch overhead,
629 ;; which can be a huge win for Python.)
631 ;; KLUDGE: when I tried moving these to
632 ;; ADD-METHOD-DECLARATIONS, things broke. No idea
633 ;; why. -- CSR, 2004-06-16
634 ,@(let ((specials (declared-specials declarations)))
635 (mapcar (lambda (par spec)
636 (parameter-specializer-declaration-in-defmethod
637 par spec specials env))
638 parameters
639 specializers))))
640 (method-lambda
641 ;; Remove the documentation string and insert the
642 ;; appropriate class declarations. The documentation
643 ;; string is removed to make it easy for us to insert
644 ;; new declarations later, they will just go after the
645 ;; CADR of the method lambda. The class declarations
646 ;; are inserted to communicate the class of the method's
647 ;; arguments to the code walk.
648 `(lambda ,lambda-list
649 ;; The default ignorability of method parameters
650 ;; doesn't seem to be specified by ANSI. PCL had
651 ;; them basically ignorable but was a little
652 ;; inconsistent. E.g. even though the two
653 ;; method definitions
654 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
655 ;; (DEFMETHOD FOO ((X T) Y) "Z")
656 ;; are otherwise equivalent, PCL treated Y as
657 ;; ignorable in the first definition but not in the
658 ;; second definition. We make all required
659 ;; parameters ignorable as a way of systematizing
660 ;; the old PCL behavior. -- WHN 2000-11-24
661 (declare (ignorable ,@required-parameters))
662 ,class-declarations
663 ,@declarations
664 (block ,(fun-name-block-name generic-function-name)
665 ,@real-body)))
666 (constant-value-p (and (null (cdr real-body))
667 (constantp (car real-body))))
668 (constant-value (and constant-value-p
669 (constant-form-value (car real-body))))
670 (plist (and constant-value-p
671 (or (typep constant-value
672 '(or number character))
673 (and (symbolp constant-value)
674 (symbol-package constant-value)))
675 (list :constant-value constant-value)))
676 (applyp (dolist (p lambda-list nil)
677 (cond ((memq p '(&optional &rest &key))
678 (return t))
679 ((eq p '&aux)
680 (return nil))))))
681 (multiple-value-bind (walked-lambda call-next-method-p setq-p
682 parameters-setqd)
683 (walk-method-lambda method-lambda
684 required-parameters
686 slots)
687 (multiple-value-bind (walked-lambda-body
688 walked-declarations
689 walked-documentation)
690 (parse-body (cddr walked-lambda))
691 (declare (ignore walked-documentation))
692 (when (some #'cdr slots)
693 (let ((slot-name-lists (slot-name-lists-from-slots slots)))
694 (setq plist
695 `(,@(when slot-name-lists
696 `(:slot-name-lists ,slot-name-lists))
697 ,@plist))
698 (setq walked-lambda-body
699 `((pv-binding (,required-parameters
700 ,slot-name-lists
701 (load-time-value
702 (intern-pv-table
703 :slot-name-lists ',slot-name-lists)))
704 ,@walked-lambda-body)))))
705 (when (and (memq '&key lambda-list)
706 (not (memq '&allow-other-keys lambda-list)))
707 (let ((aux (memq '&aux lambda-list)))
708 (setq lambda-list (nconc (ldiff lambda-list aux)
709 (list '&allow-other-keys)
710 aux))))
711 (values `(lambda (.method-args. .next-methods.)
712 (simple-lexical-method-functions
713 (,lambda-list .method-args. .next-methods.
714 :call-next-method-p
715 ,(when call-next-method-p t)
716 :setq-p ,setq-p
717 :parameters-setqd ,parameters-setqd
718 :method-cell ,method-cell
719 :applyp ,applyp)
720 ,@walked-declarations
721 (locally
722 (declare (disable-package-locks
723 %parameter-binding-modified))
724 (symbol-macrolet ((%parameter-binding-modified
725 ',@parameters-setqd))
726 (declare (enable-package-locks
727 %parameter-binding-modified))
728 ,@walked-lambda-body))))
729 `(,@(when call-next-method-p `(method-cell ,method-cell))
730 ,@(when (member call-next-method-p '(:simple nil))
731 '(simple-next-method-call t))
732 ,@(when plist `(plist ,plist))
733 ,@(when documentation `(:documentation ,documentation)))))))))))
735 (defun real-make-method-specializers-form
736 (proto-gf proto-method specializer-names env)
737 (declare (ignore env proto-gf proto-method))
738 (flet ((parse (name)
739 (cond
740 ((and (eq **boot-state** 'complete)
741 (specializerp name))
742 name)
743 ((symbolp name) `(find-class ',name))
744 ((consp name) (ecase (car name)
745 ((eql) `(intern-eql-specializer ,(cadr name)))
746 ((class-eq) `(class-eq-specializer (find-class ',(cadr name))))))
748 ;; FIXME: Document CLASS-EQ specializers.
749 (error 'simple-reference-error
750 :format-control
751 "~@<~S is not a valid parameter specializer name.~@:>"
752 :format-arguments (list name)
753 :references (list '(:ansi-cl :macro defmethod)
754 '(:ansi-cl :glossary "parameter specializer name")))))))
755 `(list ,@(mapcar #'parse specializer-names))))
757 (unless (fboundp 'make-method-specializers-form)
758 (setf (gdefinition 'make-method-specializers-form)
759 (symbol-function 'real-make-method-specializers-form)))
761 (defun real-parse-specializer-using-class (generic-function specializer)
762 (let ((result (specializer-from-type specializer)))
763 (if (specializerp result)
764 result
765 (error "~@<~S cannot be parsed as a specializer for ~S.~@:>"
766 specializer generic-function))))
768 (unless (fboundp 'parse-specializer-using-class)
769 (setf (gdefinition 'parse-specializer-using-class)
770 (symbol-function 'real-parse-specializer-using-class)))
772 (defun real-unparse-specializer-using-class (generic-function specializer)
773 (if (specializerp specializer)
774 ;; FIXME: this HANDLER-CASE is a bit of a hammer to crack a nut:
775 ;; the idea is that we want to unparse permissively, so that the
776 ;; lazy (or rather the "portable") specializer extender (who
777 ;; does not define methods on these new SBCL-specific MOP
778 ;; functions) can still subclass specializer and define methods
779 ;; without everything going wrong. Making it cleaner and
780 ;; clearer that that is what we are defending against would be
781 ;; nice. -- CSR, 2007-06-01
782 (handler-case
783 (let ((type (specializer-type specializer)))
784 (if (and (consp type) (eq (car type) 'class))
785 (let* ((class (cadr type))
786 (class-name (class-name class)))
787 (if (eq class (find-class class-name nil))
788 class-name
789 type))
790 type))
791 (error () specializer))
792 (error "~@<~S is not a legal specializer for ~S.~@:>"
793 specializer generic-function)))
795 (unless (fboundp 'unparse-specializer-using-class)
796 (setf (gdefinition 'unparse-specializer-using-class)
797 (symbol-function 'real-unparse-specializer-using-class)))
799 ;;; a helper function for creating Python-friendly type declarations
800 ;;; in DEFMETHOD forms.
802 ;;; We're too lazy to cons up a new environment for this, so we just pass in
803 ;;; the list of locally declared specials in addition to the old environment.
804 (defun parameter-specializer-declaration-in-defmethod
805 (parameter specializer specials env)
806 (cond ((and (consp specializer)
807 (eq (car specializer) 'eql))
808 ;; KLUDGE: ANSI, in its wisdom, says that
809 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
810 ;; DEFMETHOD expansion time. Thus, although one might think
811 ;; that in
812 ;; (DEFMETHOD FOO ((X PACKAGE)
813 ;; (Y (EQL 12))
814 ;; ..))
815 ;; the PACKAGE and (EQL 12) forms are both parallel type
816 ;; names, they're not, as is made clear when you do
817 ;; (DEFMETHOD FOO ((X PACKAGE)
818 ;; (Y (EQL 'BAR)))
819 ;; ..)
820 ;; where Y needs to be a symbol named "BAR", not some cons
821 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
822 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
823 ;; to be of type (EQL X). It'd be easy to transform one to
824 ;; the other, but it'd be somewhat messier to do so while
825 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
826 ;; once. (The new code wouldn't be messy, but it'd require a
827 ;; big transformation of the old code.) So instead we punt.
828 ;; -- WHN 20000610
829 '(ignorable))
830 ((member specializer
831 ;; KLUDGE: For some low-level implementation
832 ;; classes, perhaps because of some problems related
833 ;; to the incomplete integration of PCL into SBCL's
834 ;; type system, some specializer classes can't be
835 ;; declared as argument types. E.g.
836 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
837 ;; (DECLARE (TYPE SLOT-OBJECT X))
838 ;; ..)
839 ;; loses when
840 ;; (DEFSTRUCT BAR A B)
841 ;; (FOO (MAKE-BAR))
842 ;; perhaps because of the way that STRUCTURE-OBJECT
843 ;; inherits both from SLOT-OBJECT and from
844 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
845 ;; problems under the rug, we exclude these problem
846 ;; cases by blacklisting them here. -- WHN 2001-01-19
847 (list 'slot-object #+nil (find-class 'slot-object)))
848 '(ignorable))
849 ((not (eq **boot-state** 'complete))
850 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
851 ;; types which don't match their specializers. (Specifically,
852 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
853 ;; second argument.) Hopefully it only does this kind of
854 ;; weirdness when bootstrapping.. -- WHN 20000610
855 '(ignorable))
856 ((typep specializer 'eql-specializer)
857 `(type (eql ,(eql-specializer-object specializer)) ,parameter))
858 ((or (var-special-p parameter env) (member parameter specials))
859 ;; Don't declare types for special variables -- our rebinding magic
860 ;; for SETQ cases don't work right there as SET, (SETF SYMBOL-VALUE),
861 ;; etc. make things undecidable.
862 '(ignorable))
864 ;; Otherwise, we can usually make Python very happy.
866 ;; KLUDGE: Since INFO doesn't work right for class objects here,
867 ;; and they are valid specializers, see if the specializer is
868 ;; a named class, and use the name in that case -- otherwise
869 ;; the class instance is ok, since info will just return NIL, NIL.
871 ;; We still need to deal with the class case too, but at
872 ;; least #.(find-class 'integer) and integer as equivalent
873 ;; specializers with this.
874 (let* ((specializer-nameoid
875 (if (and (typep specializer 'class)
876 (let ((name (class-name specializer)))
877 (and name (symbolp name)
878 (eq specializer (find-class name nil)))))
879 (class-name specializer)
880 specializer))
881 (kind (info :type :kind specializer-nameoid)))
883 (flet ((specializer-nameoid-class ()
884 (typecase specializer-nameoid
885 (symbol (find-class specializer-nameoid nil))
886 (class specializer-nameoid)
887 (class-eq-specializer
888 (specializer-class specializer-nameoid))
889 (t nil))))
890 (ecase kind
891 ((:primitive) `(type ,specializer-nameoid ,parameter))
892 ((:defined)
893 (let ((class (specializer-nameoid-class)))
894 ;; CLASS can be null here if the user has
895 ;; erroneously tried to use a defined type as a
896 ;; specializer; it can be a non-SYSTEM-CLASS if
897 ;; the user defines a type and calls (SETF
898 ;; FIND-CLASS) in a consistent way.
899 (when (and class (typep class 'system-class))
900 `(type ,(class-name class) ,parameter))))
901 ((:instance nil)
902 (let ((class (specializer-nameoid-class)))
903 (cond
904 (class
905 (if (typep class '(or system-class structure-class))
906 `(type ,class ,parameter)
907 ;; don't declare CLOS classes as parameters;
908 ;; it's too expensive.
909 '(ignorable)))
911 ;; we can get here, and still not have a failure
912 ;; case, by doing MOP programming like (PROGN
913 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
914 ;; ...)). Best to let the user know we haven't
915 ;; been able to extract enough information:
916 (style-warn
917 "~@<can't find type for specializer ~S in ~S.~@:>"
918 specializer-nameoid
919 'parameter-specializer-declaration-in-defmethod)
920 '(ignorable)))))
921 ((:forthcoming-defclass-type)
922 '(ignorable))))))))
924 ;;; For passing a list (groveled by the walker) of the required
925 ;;; parameters whose bindings are modified in the method body to the
926 ;;; optimized-slot-value* macros.
927 (define-symbol-macro %parameter-binding-modified ())
929 (defmacro simple-lexical-method-functions ((lambda-list
930 method-args
931 next-methods
932 &rest lmf-options)
933 &body body)
934 `(progn
935 ,method-args ,next-methods
936 (bind-simple-lexical-method-functions (,method-args ,next-methods
937 ,lmf-options)
938 (bind-args (,lambda-list ,method-args)
939 ,@body))))
941 (defmacro fast-lexical-method-functions ((lambda-list
942 next-method-call
943 args
944 rest-arg
945 &rest lmf-options)
946 &body body)
947 `(bind-fast-lexical-method-functions (,args ,rest-arg ,next-method-call ,lmf-options)
948 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
949 ,@body)))
951 (defmacro bind-simple-lexical-method-functions
952 ((method-args next-methods (&key call-next-method-p setq-p
953 parameters-setqd applyp method-cell))
954 &body body
955 &environment env)
956 (declare (ignore parameters-setqd))
957 (if (not (or call-next-method-p setq-p applyp))
958 ;; always provide the lexical function NEXT-METHOD-P.
959 ;; I would think this to be a good candidate for declaring INLINE
960 ;; but that's not the way it was done before.
961 `(flet ((next-method-p () (not (null (car ,next-methods)))))
962 (declare (ignorable #'next-method-p))
963 ,@body)
964 `(let ((.next-method. (car ,next-methods))
965 (,next-methods (cdr ,next-methods)))
966 (declare (ignorable .next-method. ,next-methods))
967 (flet (,@(when call-next-method-p
968 `((call-next-method (&rest cnm-args)
969 (declare (dynamic-extent cnm-args))
970 ,@(if (safe-code-p env)
971 `((%check-cnm-args cnm-args
972 ,method-args
973 ',method-cell))
974 nil)
975 (if .next-method.
976 (funcall (if (std-instance-p .next-method.)
977 (method-function .next-method.)
978 .next-method.) ; for early methods
979 (or cnm-args ,method-args)
980 ,next-methods)
981 (apply #'call-no-next-method
982 ',method-cell
983 (or cnm-args ,method-args))))))
984 (next-method-p () (not (null .next-method.))))
985 (declare (ignorable #'next-method-p))
986 ,@body))))
988 (defun call-no-next-method (method-cell &rest args)
989 (let ((method (car method-cell)))
990 (aver method)
991 ;; Can't easily provide a RETRY restart here, as the return value here is
992 ;; for the method, not the generic function.
993 (apply #'no-next-method (method-generic-function method)
994 method args)))
996 (defun call-no-applicable-method (gf args)
997 (restart-case
998 (apply #'no-applicable-method gf args)
999 (retry ()
1000 :report "Retry calling the generic function."
1001 (apply gf args))))
1003 (defun call-no-primary-method (gf args)
1004 (restart-case
1005 (apply #'no-primary-method gf args)
1006 (retry ()
1007 :report "Retry calling the generic function."
1008 (apply gf args))))
1010 (defstruct (method-call (:copier nil))
1011 (function #'identity :type function)
1012 call-method-args)
1013 (defstruct (constant-method-call (:copier nil) (:include method-call))
1014 value)
1016 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
1018 (defmacro invoke-method-call1 (function args cm-args)
1019 `(let ((.function. ,function)
1020 (.args. ,args)
1021 (.cm-args. ,cm-args))
1022 (if (and .cm-args. (null (cdr .cm-args.)))
1023 (funcall .function. .args. (car .cm-args.))
1024 (apply .function. .args. .cm-args.))))
1026 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
1027 `(invoke-method-call1 (method-call-function ,method-call)
1028 ,(if restp
1029 `(list* ,@required-args+rest-arg)
1030 `(list ,@required-args+rest-arg))
1031 (method-call-call-method-args ,method-call)))
1033 (defstruct (fast-method-call (:copier nil))
1034 (function #'identity :type function)
1036 next-method-call
1037 arg-info)
1038 (defstruct (constant-fast-method-call
1039 (:copier nil) (:include fast-method-call))
1040 value)
1042 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
1044 ;; The two variants of INVOKE-FAST-METHOD-CALL differ in how REST-ARGs
1045 ;; are handled. The first one will get REST-ARG as a single list (as
1046 ;; the last argument), and will thus need to use APPLY. The second one
1047 ;; will get them as a &MORE argument, so we can pass the arguments
1048 ;; directly with MULTIPLE-VALUE-CALL and %MORE-ARG-VALUES.
1050 (defmacro invoke-fast-method-call (method-call restp &rest required-args+rest-arg)
1051 `(,(if restp 'apply 'funcall) (fast-method-call-function ,method-call)
1052 (fast-method-call-pv ,method-call)
1053 (fast-method-call-next-method-call ,method-call)
1054 ,@required-args+rest-arg))
1056 (defmacro invoke-fast-method-call/more (method-call
1057 more-context
1058 more-count
1059 &rest required-args)
1060 (macrolet ((generate-call (n)
1061 ``(funcall (fast-method-call-function ,method-call)
1062 (fast-method-call-pv ,method-call)
1063 (fast-method-call-next-method-call ,method-call)
1064 ,@required-args
1065 ,@(loop for x below ,n
1066 collect `(sb-c::%more-arg ,more-context ,x)))))
1067 ;; The cases with only small amounts of required arguments passed
1068 ;; are probably very common, and special-casing speeds them up by
1069 ;; a factor of 2 with very little effect on the other
1070 ;; cases. Though it'd be nice to have the generic case be equally
1071 ;; fast.
1072 `(case ,more-count
1073 (0 ,(generate-call 0))
1074 (1 ,(generate-call 1))
1075 (t (multiple-value-call (fast-method-call-function ,method-call)
1076 (values (fast-method-call-pv ,method-call))
1077 (values (fast-method-call-next-method-call ,method-call))
1078 ,@required-args
1079 (sb-c::%more-arg-values ,more-context 0 ,more-count))))))
1081 (defstruct (fast-instance-boundp (:copier nil))
1082 (index 0 :type fixnum))
1084 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
1086 (eval-when (:compile-toplevel :load-toplevel :execute)
1087 (defvar *allow-emf-call-tracing-p* nil)
1088 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
1090 ;;;; effective method functions
1092 (defvar *emf-call-trace-size* 200)
1093 (defvar *emf-call-trace* nil)
1094 (defvar *emf-call-trace-index* 0)
1096 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
1097 ;;; without explanation. It appears to be intended for debugging, so
1098 ;;; it might be useful someday, so I haven't deleted it.
1099 ;;; But it isn't documented and isn't used for anything now, so
1100 ;;; I've conditionalized it out of the base system. -- WHN 19991213
1101 #+sb-show
1102 (defun show-emf-call-trace ()
1103 (when *emf-call-trace*
1104 (let ((j *emf-call-trace-index*)
1105 (*enable-emf-call-tracing-p* nil))
1106 (format t "~&(The oldest entries are printed first)~%")
1107 (dotimes-fixnum (i *emf-call-trace-size*)
1108 (let ((ct (aref *emf-call-trace* j)))
1109 (when ct (print ct)))
1110 (incf j)
1111 (when (= j *emf-call-trace-size*)
1112 (setq j 0))))))
1114 (defun trace-emf-call-internal (emf format args)
1115 (unless *emf-call-trace*
1116 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
1117 (setf (aref *emf-call-trace* *emf-call-trace-index*)
1118 (list* emf format args))
1119 (incf *emf-call-trace-index*)
1120 (when (= *emf-call-trace-index* *emf-call-trace-size*)
1121 (setq *emf-call-trace-index* 0)))
1123 (defmacro trace-emf-call (emf format args)
1124 (when *allow-emf-call-tracing-p*
1125 `(when *enable-emf-call-tracing-p*
1126 (trace-emf-call-internal ,emf ,format ,args))))
1128 (defmacro invoke-effective-method-function-fast
1129 (emf restp &key required-args rest-arg more-arg)
1130 `(progn
1131 (trace-emf-call ,emf ,restp (list ,@required-args rest-arg))
1132 ,(if more-arg
1133 `(invoke-fast-method-call/more ,emf
1134 ,@more-arg
1135 ,@required-args)
1136 `(invoke-fast-method-call ,emf
1137 ,restp
1138 ,@required-args
1139 ,@rest-arg))))
1141 (defun effective-method-optimized-slot-access-clause
1142 (emf restp required-args)
1143 ;; "What," you may wonder, "do these next two clauses do?" In that
1144 ;; case, you are not a PCL implementor, for they considered this to
1145 ;; be self-documenting.:-| Or CSR, for that matter, since he can
1146 ;; also figure it out by looking at it without breaking stride. For
1147 ;; the rest of us, though: From what the code is doing with .SLOTS.
1148 ;; and whatnot, evidently it's implementing SLOT-VALUEish and
1149 ;; GET-SLOT-VALUEish things. Then we can reason backwards and
1150 ;; conclude that setting EMF to a FIXNUM is an optimized way to
1151 ;; represent these slot access operations.
1152 (when (not restp)
1153 (let ((length (length required-args)))
1154 (cond ((= 1 length)
1155 `((fixnum
1156 (let* ((.slots. (get-slots-or-nil
1157 ,(car required-args)))
1158 (value (when .slots. (clos-slots-ref .slots. ,emf))))
1159 (if (eq value +slot-unbound+)
1160 (slot-unbound-internal ,(car required-args)
1161 ,emf)
1162 value)))))
1163 ((= 2 length)
1164 `((fixnum
1165 (let ((.new-value. ,(car required-args))
1166 (.slots. (get-slots-or-nil
1167 ,(cadr required-args))))
1168 (when .slots.
1169 (setf (clos-slots-ref .slots. ,emf) .new-value.)))))))
1170 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
1171 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
1172 ;; there was no explanation and presumably the code is 10+
1173 ;; years stale, I simply deleted it. -- WHN)
1176 ;;; Before SBCL 0.9.16.7 instead of
1177 ;;; INVOKE-NARROW-EFFECTIVE-METHOD-FUNCTION we passed a (THE (OR
1178 ;;; FUNCTION METHOD-CALL FAST-METHOD-CALL) EMF) form as the EMF. Now,
1179 ;;; to make less work for the compiler we take a path that doesn't
1180 ;;; involve the slot-accessor clause (where EMF is a FIXNUM) at all.
1181 (macrolet ((def (name &optional narrow)
1182 `(defmacro ,name (emf restp &key required-args rest-arg more-arg)
1183 (unless (constantp restp)
1184 (error "The RESTP argument is not constant."))
1185 (setq restp (constant-form-value restp))
1186 (with-unique-names (emf-n)
1187 `(locally
1188 (declare (optimize (sb-c:insert-step-conditions 0)))
1189 (let ((,emf-n ,emf))
1190 (trace-emf-call ,emf-n ,restp (list ,@required-args ,@rest-arg))
1191 (etypecase ,emf-n
1192 (fast-method-call
1193 ,(if more-arg
1194 `(invoke-fast-method-call/more ,emf-n
1195 ,@more-arg
1196 ,@required-args)
1197 `(invoke-fast-method-call ,emf-n
1198 ,restp
1199 ,@required-args
1200 ,@rest-arg)))
1201 ,@,(unless narrow
1202 `(effective-method-optimized-slot-access-clause
1203 emf-n restp required-args))
1204 (method-call
1205 (invoke-method-call ,emf-n ,restp ,@required-args
1206 ,@rest-arg))
1207 (function
1208 ,(if restp
1209 `(apply ,emf-n ,@required-args ,@rest-arg)
1210 `(funcall ,emf-n ,@required-args
1211 ,@rest-arg))))))))))
1212 (def invoke-effective-method-function nil)
1213 (def invoke-narrow-effective-method-function t))
1215 (defun invoke-emf (emf args)
1216 (trace-emf-call emf t args)
1217 (etypecase emf
1218 (fast-method-call
1219 (let* ((arg-info (fast-method-call-arg-info emf))
1220 (restp (cdr arg-info))
1221 (nreq (car arg-info)))
1222 (if restp
1223 (apply (fast-method-call-function emf)
1224 (fast-method-call-pv emf)
1225 (fast-method-call-next-method-call emf)
1226 args)
1227 (cond ((null args)
1228 (if (eql nreq 0)
1229 (invoke-fast-method-call emf nil)
1230 (error 'simple-program-error
1231 :format-control "invalid number of arguments: 0"
1232 :format-arguments nil)))
1233 ((null (cdr args))
1234 (if (eql nreq 1)
1235 (invoke-fast-method-call emf nil (car args))
1236 (error 'simple-program-error
1237 :format-control "invalid number of arguments: 1"
1238 :format-arguments nil)))
1239 ((null (cddr args))
1240 (if (eql nreq 2)
1241 (invoke-fast-method-call emf nil (car args) (cadr args))
1242 (error 'simple-program-error
1243 :format-control "invalid number of arguments: 2"
1244 :format-arguments nil)))
1246 (apply (fast-method-call-function emf)
1247 (fast-method-call-pv emf)
1248 (fast-method-call-next-method-call emf)
1249 args))))))
1250 (method-call
1251 (apply (method-call-function emf)
1252 args
1253 (method-call-call-method-args emf)))
1254 (fixnum
1255 (cond ((null args)
1256 (error 'simple-program-error
1257 :format-control "invalid number of arguments: 0"
1258 :format-arguments nil))
1259 ((null (cdr args))
1260 (let* ((slots (get-slots (car args)))
1261 (value (clos-slots-ref slots emf)))
1262 (if (eq value +slot-unbound+)
1263 (slot-unbound-internal (car args) emf)
1264 value)))
1265 ((null (cddr args))
1266 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1267 (car args)))
1268 (t (error 'simple-program-error
1269 :format-control "invalid number of arguments"
1270 :format-arguments nil))))
1271 (fast-instance-boundp
1272 (if (or (null args) (cdr args))
1273 (error 'simple-program-error
1274 :format-control "invalid number of arguments"
1275 :format-arguments nil)
1276 (let ((slots (get-slots (car args))))
1277 (not (eq (clos-slots-ref slots (fast-instance-boundp-index emf))
1278 +slot-unbound+)))))
1279 (function
1280 (apply emf args))))
1283 (defmacro fast-call-next-method-body ((args next-method-call rest-arg)
1284 method-cell
1285 cnm-args)
1286 `(if ,next-method-call
1287 ,(let ((call `(invoke-narrow-effective-method-function
1288 ,next-method-call
1289 ,(not (null rest-arg))
1290 :required-args ,args
1291 :rest-arg ,(when rest-arg (list rest-arg)))))
1292 `(if ,cnm-args
1293 (bind-args ((,@args
1294 ,@(when rest-arg
1295 `(&rest ,rest-arg)))
1296 ,cnm-args)
1297 ,call)
1298 ,call))
1299 (call-no-next-method ',method-cell
1300 ,@args
1301 ,@(when rest-arg
1302 `(,rest-arg)))))
1304 (defmacro bind-fast-lexical-method-functions
1305 ((args rest-arg next-method-call (&key
1306 call-next-method-p
1307 setq-p
1308 parameters-setqd
1309 method-cell
1310 applyp))
1311 &body body
1312 &environment env)
1313 (let* ((next-method-p-def
1314 `((next-method-p ()
1315 (declare (optimize (sb-c:insert-step-conditions 0)))
1316 (not (null ,next-method-call)))))
1317 (rebindings (when (or setq-p call-next-method-p)
1318 (mapcar (lambda (x) (list x x)) parameters-setqd))))
1319 (if (not (or call-next-method-p setq-p applyp))
1320 `(flet ,next-method-p-def
1321 (declare (ignorable #'next-method-p))
1322 ,@body)
1323 `(flet (,@(when call-next-method-p
1324 `((call-next-method (&rest cnm-args)
1325 (declare (dynamic-extent cnm-args)
1326 (muffle-conditions code-deletion-note)
1327 (optimize (sb-c:insert-step-conditions 0)))
1328 ,@(if (safe-code-p env)
1329 `((%check-cnm-args cnm-args (list ,@args)
1330 ',method-cell))
1331 nil)
1332 (fast-call-next-method-body (,args
1333 ,next-method-call
1334 ,rest-arg)
1335 ,method-cell
1336 cnm-args))))
1337 ,@next-method-p-def)
1338 (declare (ignorable #'next-method-p))
1339 (let ,rebindings
1340 ,@body)))))
1342 ;;; CMUCL comment (Gerd Moellmann):
1344 ;;; The standard says it's an error if CALL-NEXT-METHOD is called with
1345 ;;; arguments, and the set of methods applicable to those arguments is
1346 ;;; different from the set of methods applicable to the original
1347 ;;; method arguments. (According to Barry Margolin, this rule was
1348 ;;; probably added to ensure that before and around methods are always
1349 ;;; run before primary methods.)
1351 ;;; This could be optimized for the case that the generic function
1352 ;;; doesn't have hairy methods, does have standard method combination,
1353 ;;; is a standard generic function, there are no methods defined on it
1354 ;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such
1355 ;;; preconditions. That looks hairy and is probably not worth it,
1356 ;;; because this check will never be fast.
1357 (defun %check-cnm-args (cnm-args orig-args method-cell)
1358 ;; 1. Check for no arguments.
1359 (when cnm-args
1360 (let* ((gf (method-generic-function (car method-cell)))
1361 (nreq (generic-function-nreq gf)))
1362 (declare (fixnum nreq))
1363 ;; 2. Requirement arguments pairwise: if all are EQL, the applicable
1364 ;; methods must be the same. This takes care of the relatively common
1365 ;; case of twiddling with &KEY arguments without being horribly
1366 ;; expensive.
1367 (unless (do ((orig orig-args (cdr orig))
1368 (args cnm-args (cdr args))
1369 (n nreq (1- nreq)))
1370 ((zerop n) t)
1371 (unless (and orig args (eql (car orig) (car args)))
1372 (return nil)))
1373 ;; 3. Only then do the full check.
1374 (let ((omethods (compute-applicable-methods gf orig-args))
1375 (nmethods (compute-applicable-methods gf cnm-args)))
1376 (unless (equal omethods nmethods)
1377 (error "~@<The set of methods ~S applicable to argument~P ~
1378 ~{~S~^, ~} to call-next-method is different from ~
1379 the set of methods ~S applicable to the original ~
1380 method argument~P ~{~S~^, ~}.~@:>"
1381 nmethods (length cnm-args) cnm-args omethods
1382 (length orig-args) orig-args)))))))
1384 ;; FIXME: replacing this entire mess with DESTRUCTURING-BIND would correct
1385 ;; problems similar to those already solved by a correct implementation
1386 ;; of DESTRUCTURING-BIND, such as incorrect binding order:
1387 ;; e.g. (macroexpand-1 '(bind-args ((&optional (x nil xsp)) args) (form)))
1388 ;; -> (LET* ((.ARGS-TAIL. ARGS) (XSP (NOT (NULL .ARGS-TAIL.))) (X ...)))
1389 ;; It's mostly irrelevant unless a method uses CALL-NEXT-METHOD though.
1390 (defmacro bind-args ((lambda-list args) &body body)
1391 (let ((args-tail '.args-tail.)
1392 (key '.key.)
1393 (state 'required))
1394 (flet ((process-var (var)
1395 (if (memq var lambda-list-keywords)
1396 (progn
1397 (case var
1398 (&optional (setq state 'optional))
1399 (&key (setq state 'key))
1400 (&allow-other-keys)
1401 (&rest (setq state 'rest))
1402 (&aux (setq state 'aux))
1403 (otherwise
1404 (error
1405 "encountered the non-standard lambda list keyword ~S"
1406 var)))
1407 nil)
1408 (case state
1409 (required `((,var (pop ,args-tail))))
1410 (optional (cond ((not (consp var))
1411 `((,var (when ,args-tail
1412 (pop ,args-tail)))))
1413 ((null (cddr var))
1414 `((,(car var) (if ,args-tail
1415 (pop ,args-tail)
1416 ,(cadr var)))))
1418 `((,(caddr var) (not (null ,args-tail)))
1419 (,(car var) (if ,args-tail
1420 (pop ,args-tail)
1421 ,(cadr var)))))))
1422 (rest `((,var ,args-tail)))
1423 (key (cond ((not (consp var))
1424 `((,var (car
1425 (get-key-arg-tail ,(keywordicate var)
1426 ,args-tail)))))
1427 ((null (cddr var))
1428 (multiple-value-bind (keyword variable)
1429 (if (consp (car var))
1430 (values (caar var)
1431 (cadar var))
1432 (values (keywordicate (car var))
1433 (car var)))
1434 `((,key (get-key-arg-tail ',keyword
1435 ,args-tail))
1436 (,variable (if ,key
1437 (car ,key)
1438 ,(cadr var))))))
1440 (multiple-value-bind (keyword variable)
1441 (if (consp (car var))
1442 (values (caar var)
1443 (cadar var))
1444 (values (keywordicate (car var))
1445 (car var)))
1446 `((,key (get-key-arg-tail ',keyword
1447 ,args-tail))
1448 (,(caddr var) (not (null,key)))
1449 (,variable (if ,key
1450 (car ,key)
1451 ,(cadr var))))))))
1452 (aux `(,var))))))
1453 (let ((bindings (mapcan #'process-var lambda-list)))
1454 `(let* ((,args-tail ,args)
1455 ,@bindings
1456 (.dummy0.
1457 ,@(when (eq state 'optional)
1458 `((unless (null ,args-tail)
1459 (error 'simple-program-error
1460 :format-control "surplus arguments: ~S"
1461 :format-arguments (list ,args-tail)))))))
1462 (declare (ignorable ,args-tail .dummy0.))
1463 ,@body)))))
1465 (defun get-key-arg-tail (keyword list)
1466 (loop for (key . tail) on list by #'cddr
1467 when (null tail) do
1468 ;; FIXME: Do we want to export this symbol? Or maybe use an
1469 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1470 (sb-c::%odd-key-args-error)
1471 when (eq key keyword)
1472 return tail))
1474 (defun walk-method-lambda (method-lambda required-parameters env slots)
1475 (let (;; flag indicating that CALL-NEXT-METHOD should be in the
1476 ;; method definition
1477 (call-next-method-p nil)
1478 ;; a list of all required parameters whose bindings might be
1479 ;; modified in the method body.
1480 (parameters-setqd nil))
1481 (flet ((walk-function (form context env)
1482 (unless (and (eq context :eval) (consp form))
1483 (return-from walk-function form))
1484 (case (car form)
1485 (call-next-method
1486 ;; hierarchy: nil -> :simple -> T.
1487 (unless (eq call-next-method-p t)
1488 (setq call-next-method-p (if (cdr form) t :simple)))
1489 form)
1490 ((setq multiple-value-setq)
1491 ;; The walker will split (SETQ A 1 B 2) to
1492 ;; separate (SETQ A 1) and (SETQ B 2) forms, so we
1493 ;; only need to handle the simple case of SETQ
1494 ;; here.
1495 (let ((vars (if (eq (car form) 'setq)
1496 (list (second form))
1497 (second form))))
1498 (dolist (var vars)
1499 ;; Note that we don't need to check for
1500 ;; %VARIABLE-REBINDING declarations like is
1501 ;; done in CAN-OPTIMIZE-ACCESS1, since the
1502 ;; bindings that will have that declation will
1503 ;; never be SETQd.
1504 (when (var-declaration '%class var env)
1505 ;; If a parameter binding is shadowed by
1506 ;; another binding it won't have a %CLASS
1507 ;; declaration anymore, and this won't get
1508 ;; executed.
1509 (pushnew var parameters-setqd :test #'eq))))
1510 form)
1511 (function
1512 (when (equal (cdr form) '(call-next-method))
1513 (setq call-next-method-p t))
1514 form)
1515 ((slot-value set-slot-value slot-boundp)
1516 (if (constantp (third form) env)
1517 (let ((fun (ecase (car form)
1518 (slot-value #'optimize-slot-value)
1519 (set-slot-value #'optimize-set-slot-value)
1520 (slot-boundp #'optimize-slot-boundp))))
1521 (funcall fun form slots required-parameters env))
1522 form))
1523 (t form))))
1525 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1526 ;;; FIXME: the walker's rewriting of the source code causes
1527 ;;; trouble when doing code coverage. The rewrites should be
1528 ;;; removed, and the same operations done using
1529 ;;; compiler-macros or tranforms.
1530 (values (if (sb-c:policy env (= sb-c:store-coverage-data 0))
1531 walked-lambda
1532 method-lambda)
1533 call-next-method-p
1534 (not (null parameters-setqd))
1535 parameters-setqd)))))
1537 (defun generic-function-name-p (name)
1538 (and (legal-fun-name-p name)
1539 (fboundp name)
1540 (if (eq **boot-state** 'complete)
1541 (standard-generic-function-p (gdefinition name))
1542 (funcallable-instance-p (gdefinition name)))))
1544 (defun method-plist-value (method key &optional default)
1545 (let ((plist (if (consp method)
1546 (getf (early-method-initargs method) 'plist)
1547 (object-plist method))))
1548 (getf plist key default)))
1550 (defun (setf method-plist-value) (new-value method key &optional default)
1551 (if (consp method)
1552 (setf (getf (getf (early-method-initargs method) 'plist) key default)
1553 new-value)
1554 (setf (getf (object-plist method) key default) new-value)))
1556 (defun load-defmethod (class name quals specls ll initargs source-location)
1557 (let ((method-cell (getf initargs 'method-cell)))
1558 (setq initargs (copy-tree initargs))
1559 (when method-cell
1560 (setf (getf initargs 'method-cell) method-cell))
1561 #+nil
1562 (setf (getf (getf initargs 'plist) :name)
1563 (make-method-spec name quals specls))
1564 (load-defmethod-internal class name quals specls
1565 ll initargs source-location)))
1567 (defun load-defmethod-internal
1568 (method-class gf-spec qualifiers specializers lambda-list
1569 initargs source-location)
1570 (when (and (eq **boot-state** 'complete)
1571 (fboundp gf-spec))
1572 (let* ((gf (fdefinition gf-spec))
1573 (method (and (generic-function-p gf)
1574 (generic-function-methods gf)
1575 (find-method gf qualifiers specializers nil))))
1576 (when method
1577 (warn 'sb-kernel:redefinition-with-defmethod
1578 :name gf-spec
1579 :new-location source-location
1580 :old-method method
1581 :qualifiers qualifiers :specializers specializers))))
1582 (let ((method (apply #'add-named-method
1583 gf-spec qualifiers specializers lambda-list
1584 :definition-source source-location
1585 initargs)))
1586 (unless (or (eq method-class 'standard-method)
1587 (eq (find-class method-class nil) (class-of method)))
1588 ;; FIXME: should be STYLE-WARNING?
1589 (format *error-output*
1590 "~&At the time the method with qualifiers ~:S and~%~
1591 specializers ~:S on the generic function ~S~%~
1592 was compiled, the method-class for that generic function was~%~
1593 ~S. But, the method class is now ~S, this~%~
1594 may mean that this method was compiled improperly.~%"
1595 qualifiers specializers gf-spec
1596 method-class (class-name (class-of method))))
1597 method))
1599 (defun make-method-spec (gf qualifiers specializers)
1600 (let ((name (generic-function-name gf))
1601 (unparsed-specializers (unparse-specializers gf specializers)))
1602 `(slow-method ,name ,@qualifiers ,unparsed-specializers)))
1604 (defun initialize-method-function (initargs method)
1605 (let* ((mf (getf initargs :function))
1606 (mff (and (typep mf '%method-function)
1607 (%method-function-fast-function mf)))
1608 (plist (getf initargs 'plist))
1609 (name (getf plist :name))
1610 (method-cell (getf initargs 'method-cell)))
1611 (when method-cell
1612 (setf (car method-cell) method))
1613 (when name
1614 (when mf
1615 (setq mf (set-fun-name mf name)))
1616 (when (and mff (consp name) (eq (car name) 'slow-method))
1617 (let ((fast-name `(fast-method ,@(cdr name))))
1618 (set-fun-name mff fast-name))))
1619 (when plist
1620 (let ((plist plist))
1621 (let ((snl (getf plist :slot-name-lists)))
1622 (when snl
1623 (setf (method-plist-value method :pv-table)
1624 (intern-pv-table :slot-name-lists snl))))))))
1626 (defun analyze-lambda-list (lambda-list)
1627 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1628 (parse-key-arg (arg)
1629 (if (listp arg)
1630 (if (listp (car arg))
1631 (caar arg)
1632 (keywordicate (car arg)))
1633 (keywordicate arg))))
1634 (let ((nrequired 0)
1635 (noptional 0)
1636 (keysp nil)
1637 (restp nil)
1638 (nrest 0)
1639 (allow-other-keys-p nil)
1640 (keywords ())
1641 (keyword-parameters ())
1642 (state 'required))
1643 (dolist (x lambda-list)
1644 (if (memq x lambda-list-keywords)
1645 (case x
1646 (&optional (setq state 'optional))
1647 (&key (setq keysp t
1648 state 'key))
1649 (&allow-other-keys (setq allow-other-keys-p t))
1650 (&rest (setq restp t
1651 state 'rest))
1652 (&aux (return t))
1653 (otherwise
1654 (error "encountered the non-standard lambda list keyword ~S"
1655 x)))
1656 (ecase state
1657 (required (incf nrequired))
1658 (optional (incf noptional))
1659 (key (push (parse-key-arg x) keywords)
1660 (push x keyword-parameters))
1661 (rest (incf nrest)))))
1662 (when (and restp (zerop nrest))
1663 (error "Error in lambda-list:~%~
1664 After &REST, a DEFGENERIC lambda-list ~
1665 must be followed by at least one variable."))
1666 (values nrequired noptional keysp restp allow-other-keys-p
1667 (reverse keywords)
1668 (reverse keyword-parameters)))))
1670 (defun keyword-spec-name (x)
1671 (let ((key (if (atom x) x (car x))))
1672 (if (atom key)
1673 (keywordicate key)
1674 (car key))))
1676 (defun ftype-declaration-from-lambda-list (lambda-list name)
1677 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1678 keywords keyword-parameters)
1679 (analyze-lambda-list lambda-list)
1680 (declare (ignore keyword-parameters))
1681 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1682 (old-ftype (if (fun-type-p old) old nil))
1683 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1684 (old-keys (and old-ftype
1685 (mapcar #'key-info-name
1686 (fun-type-keywords
1687 old-ftype))))
1688 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1689 (old-allowp (and old-ftype
1690 (fun-type-allowp old-ftype)))
1691 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1692 `(function ,(append (make-list nrequired :initial-element t)
1693 (when (plusp noptional)
1694 (append '(&optional)
1695 (make-list noptional :initial-element t)))
1696 (when (or restp old-restp)
1697 '(&rest t))
1698 (when (or keysp old-keysp)
1699 (append '(&key)
1700 (mapcar (lambda (key)
1701 `(,key t))
1702 keywords)
1703 (when (or allow-other-keys-p old-allowp)
1704 '(&allow-other-keys)))))
1705 *))))
1707 ;;;; early generic function support
1709 (defvar *!early-generic-functions* ())
1711 ;; CLHS doesn't specify &allow-other-keys here but I guess the supposition
1712 ;; is that they'll be checked by ENSURE-GENERIC-FUNCTION-USING-CLASS.
1713 ;; Except we don't do that either, so I think the blame, if any, lies there
1714 ;; for not catching errant keywords.
1715 (defun ensure-generic-function (fun-name &rest all-keys)
1716 (let ((existing (and (fboundp fun-name)
1717 (gdefinition fun-name))))
1718 (cond ((and existing
1719 (eq **boot-state** 'complete)
1720 (null (generic-function-p existing)))
1721 (generic-clobbers-function fun-name)
1722 (fmakunbound fun-name)
1723 (apply #'ensure-generic-function fun-name all-keys))
1725 (apply #'ensure-generic-function-using-class
1726 existing fun-name all-keys)))))
1728 (defun generic-clobbers-function (fun-name)
1729 (cerror "Replace the function binding"
1730 'simple-program-error
1731 :format-control "~S already names an ordinary function or a macro."
1732 :format-arguments (list fun-name)))
1734 (defvar *sgf-wrapper*
1735 (!boot-make-wrapper (early-class-size 'standard-generic-function)
1736 'standard-generic-function))
1738 (defvar *sgf-slots-init*
1739 (mapcar (lambda (canonical-slot)
1740 (if (memq (getf canonical-slot :name) '(arg-info source))
1741 +slot-unbound+
1742 (let ((initfunction (getf canonical-slot :initfunction)))
1743 (if initfunction
1744 (funcall initfunction)
1745 +slot-unbound+))))
1746 (early-collect-inheritance 'standard-generic-function)))
1748 (defconstant +sgf-method-class-index+
1749 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1751 (defun early-gf-p (x)
1752 (and (fsc-instance-p x)
1753 (eq (clos-slots-ref (get-slots x) +sgf-method-class-index+)
1754 +slot-unbound+)))
1756 (defconstant +sgf-methods-index+
1757 (!bootstrap-slot-index 'standard-generic-function 'methods))
1759 (defmacro early-gf-methods (gf)
1760 `(clos-slots-ref (get-slots ,gf) +sgf-methods-index+))
1762 (defun safe-generic-function-methods (generic-function)
1763 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1764 (clos-slots-ref (get-slots generic-function) +sgf-methods-index+)
1765 (generic-function-methods generic-function)))
1767 (defconstant +sgf-arg-info-index+
1768 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1770 (defmacro early-gf-arg-info (gf)
1771 `(clos-slots-ref (get-slots ,gf) +sgf-arg-info-index+))
1773 (defconstant +sgf-dfun-state-index+
1774 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1776 (defstruct (arg-info
1777 (:conc-name nil)
1778 (:constructor make-arg-info ())
1779 (:copier nil))
1780 (arg-info-lambda-list :no-lambda-list)
1781 arg-info-precedence
1782 arg-info-metatypes
1783 arg-info-number-optional
1784 arg-info-key/rest-p
1785 arg-info-keys ;nil no &KEY or &REST allowed
1786 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1787 ;T must have &KEY or &REST
1789 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1790 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1792 gf-info-static-c-a-m-emf
1793 (gf-info-c-a-m-emf-std-p t)
1794 gf-info-fast-mf-p)
1796 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1798 (defun arg-info-valid-p (arg-info)
1799 (not (null (arg-info-number-optional arg-info))))
1801 (defun arg-info-applyp (arg-info)
1802 (or (plusp (arg-info-number-optional arg-info))
1803 (arg-info-key/rest-p arg-info)))
1805 (defun arg-info-number-required (arg-info)
1806 (length (arg-info-metatypes arg-info)))
1808 (defun arg-info-nkeys (arg-info)
1809 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1811 (defun create-gf-lambda-list (lambda-list)
1812 ;;; Create a gf lambda list from a method lambda list
1813 (loop for x in lambda-list
1814 collect (if (consp x) (list (car x)) x)
1815 if (eq x '&key) do (loop-finish)))
1817 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1818 argument-precedence-order)
1819 (let* ((arg-info (if (eq **boot-state** 'complete)
1820 (gf-arg-info gf)
1821 (early-gf-arg-info gf)))
1822 (methods (if (eq **boot-state** 'complete)
1823 (generic-function-methods gf)
1824 (early-gf-methods gf)))
1825 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1826 (first-p (and new-method (null (cdr methods)))))
1827 (when (and (not lambda-list-p) methods)
1828 (setq lambda-list (gf-lambda-list gf)))
1829 (when (or lambda-list-p
1830 (and first-p
1831 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1832 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1833 (analyze-lambda-list lambda-list)
1834 (when (and methods (not first-p))
1835 (let ((gf-nreq (arg-info-number-required arg-info))
1836 (gf-nopt (arg-info-number-optional arg-info))
1837 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1838 (unless (and (= nreq gf-nreq)
1839 (= nopt gf-nopt)
1840 (eq (or keysp restp) gf-key/rest-p))
1841 (error "The lambda-list ~S is incompatible with ~
1842 existing methods of ~S."
1843 lambda-list gf))))
1844 (setf (arg-info-lambda-list arg-info)
1845 (if lambda-list-p
1846 lambda-list
1847 (create-gf-lambda-list lambda-list)))
1848 (when (or lambda-list-p argument-precedence-order
1849 (null (arg-info-precedence arg-info)))
1850 (setf (arg-info-precedence arg-info)
1851 (compute-precedence lambda-list nreq argument-precedence-order)))
1852 (setf (arg-info-metatypes arg-info) (make-list nreq))
1853 (setf (arg-info-number-optional arg-info) nopt)
1854 (setf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1855 (setf (arg-info-keys arg-info)
1856 (if lambda-list-p
1857 (if allow-other-keys-p t keywords)
1858 (arg-info-key/rest-p arg-info)))))
1859 (when new-method
1860 (check-method-arg-info gf arg-info new-method))
1861 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1862 arg-info))
1864 (defun check-method-arg-info (gf arg-info method)
1865 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1866 (analyze-lambda-list (if (consp method)
1867 (early-method-lambda-list method)
1868 (method-lambda-list method)))
1869 (flet ((lose (string &rest args)
1870 (error 'simple-program-error
1871 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1872 to the generic function~2I~_~S;~I~_~
1873 but ~?~:>"
1874 :format-arguments (list method gf string args)))
1875 (comparison-description (x y)
1876 (if (> x y) "more" "fewer")))
1877 (let ((gf-nreq (arg-info-number-required arg-info))
1878 (gf-nopt (arg-info-number-optional arg-info))
1879 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1880 (gf-keywords (arg-info-keys arg-info)))
1881 (unless (= nreq gf-nreq)
1882 (lose
1883 "the method has ~A required arguments than the generic function."
1884 (comparison-description nreq gf-nreq)))
1885 (unless (= nopt gf-nopt)
1886 (lose
1887 "the method has ~A optional arguments than the generic function."
1888 (comparison-description nopt gf-nopt)))
1889 (unless (eq (or keysp restp) gf-key/rest-p)
1890 (lose
1891 "the method and generic function differ in whether they accept~_~
1892 &REST or &KEY arguments."))
1893 (when (consp gf-keywords)
1894 (unless (or (and restp (not keysp))
1895 allow-other-keys-p
1896 (every (lambda (k) (memq k keywords)) gf-keywords))
1897 (lose "the method does not accept each of the &KEY arguments~2I~_~
1898 ~S."
1899 gf-keywords)))))))
1901 (defconstant +sm-specializers-index+
1902 (!bootstrap-slot-index 'standard-method 'specializers))
1903 (defconstant +sm-%function-index+
1904 (!bootstrap-slot-index 'standard-method '%function))
1905 (defconstant +sm-qualifiers-index+
1906 (!bootstrap-slot-index 'standard-method 'qualifiers))
1908 ;;; FIXME: we don't actually need this; we could test for the exact
1909 ;;; class and deal with it as appropriate. In fact we probably don't
1910 ;;; need it anyway because we only use this for METHOD-SPECIALIZERS on
1911 ;;; the standard reader method for METHOD-SPECIALIZERS. Probably.
1912 (dolist (s '(specializers %function))
1913 (aver (= (symbol-value (intern (format nil "+SM-~A-INDEX+" s)))
1914 (!bootstrap-slot-index 'standard-reader-method s)
1915 (!bootstrap-slot-index 'standard-writer-method s)
1916 (!bootstrap-slot-index 'standard-boundp-method s)
1917 (!bootstrap-slot-index 'global-reader-method s)
1918 (!bootstrap-slot-index 'global-writer-method s)
1919 (!bootstrap-slot-index 'global-boundp-method s))))
1921 (defvar *standard-method-class-names*
1922 '(standard-method standard-reader-method
1923 standard-writer-method standard-boundp-method
1924 global-reader-method global-writer-method
1925 global-boundp-method))
1927 (declaim (list **standard-method-classes**))
1928 (defglobal **standard-method-classes** nil)
1930 (defun safe-method-specializers (method)
1931 (if (member (class-of method) **standard-method-classes** :test #'eq)
1932 (clos-slots-ref (std-instance-slots method) +sm-specializers-index+)
1933 (method-specializers method)))
1934 (defun safe-method-fast-function (method)
1935 (let ((mf (safe-method-function method)))
1936 (and (typep mf '%method-function)
1937 (%method-function-fast-function mf))))
1938 (defun safe-method-function (method)
1939 (if (member (class-of method) **standard-method-classes** :test #'eq)
1940 (clos-slots-ref (std-instance-slots method) +sm-%function-index+)
1941 (method-function method)))
1942 (defun safe-method-qualifiers (method)
1943 (if (member (class-of method) **standard-method-classes** :test #'eq)
1944 (clos-slots-ref (std-instance-slots method) +sm-qualifiers-index+)
1945 (method-qualifiers method)))
1947 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1948 (let* ((existing-p (and methods (cdr methods) new-method))
1949 (nreq (length (arg-info-metatypes arg-info)))
1950 (metatypes (if existing-p
1951 (arg-info-metatypes arg-info)
1952 (make-list nreq)))
1953 (type (if existing-p
1954 (gf-info-simple-accessor-type arg-info)
1955 nil)))
1956 (when (arg-info-valid-p arg-info)
1957 (dolist (method (if new-method (list new-method) methods))
1958 (let* ((specializers (if (or (eq **boot-state** 'complete)
1959 (not (consp method)))
1960 (safe-method-specializers method)
1961 (early-method-specializers method t)))
1962 (class (if (or (eq **boot-state** 'complete) (not (consp method)))
1963 (class-of method)
1964 (early-method-class method)))
1965 (new-type
1966 (when (and class
1967 (or (not (eq **boot-state** 'complete))
1968 (eq (generic-function-method-combination gf)
1969 *standard-method-combination*)))
1970 (cond ((or (eq class *the-class-standard-reader-method*)
1971 (eq class *the-class-global-reader-method*))
1972 'reader)
1973 ((or (eq class *the-class-standard-writer-method*)
1974 (eq class *the-class-global-writer-method*))
1975 'writer)
1976 ((or (eq class *the-class-standard-boundp-method*)
1977 (eq class *the-class-global-boundp-method*))
1978 'boundp)))))
1979 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1980 (setq type (cond ((null type) new-type)
1981 ((eq type new-type) type)
1982 (t nil)))))
1983 (setf (arg-info-metatypes arg-info) metatypes)
1984 (setf (gf-info-simple-accessor-type arg-info) type)))
1985 (when (or (not was-valid-p) first-p)
1986 (multiple-value-bind (c-a-m-emf std-p)
1987 (if (early-gf-p gf)
1988 (values t t)
1989 (compute-applicable-methods-emf gf))
1990 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1991 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1992 (unless (gf-info-c-a-m-emf-std-p arg-info)
1993 (setf (gf-info-simple-accessor-type arg-info) t))))
1994 (unless was-valid-p
1995 (let ((name (if (eq **boot-state** 'complete)
1996 (generic-function-name gf)
1997 (!early-gf-name gf))))
1998 (setf (gf-precompute-dfun-and-emf-p arg-info)
1999 (cond
2000 ((and (consp name)
2001 (member (car name)
2002 *internal-pcl-generalized-fun-name-symbols*))
2003 nil)
2004 (t (let* ((symbol (fun-name-block-name name))
2005 (package (symbol-package symbol)))
2006 (and (or (eq package *pcl-package*)
2007 (memq package (package-use-list *pcl-package*)))
2008 (not (eq package *cl-package*))
2009 ;; FIXME: this test will eventually be
2010 ;; superseded by the *internal-pcl...* test,
2011 ;; above. While we are in a process of
2012 ;; transition, however, it should probably
2013 ;; remain.
2014 (not (find #\Space (symbol-name symbol))))))))))
2015 (setf (gf-info-fast-mf-p arg-info)
2016 (or (not (eq **boot-state** 'complete))
2017 (let* ((method-class (generic-function-method-class gf))
2018 (methods (compute-applicable-methods
2019 #'make-method-lambda
2020 (list gf (class-prototype method-class)
2021 '(lambda) nil))))
2022 (and methods (null (cdr methods))
2023 (let ((specls (method-specializers (car methods))))
2024 (and (classp (car specls))
2025 (eq 'standard-generic-function
2026 (class-name (car specls)))
2027 (classp (cadr specls))
2028 (eq 'standard-method
2029 (class-name (cadr specls)))))))))
2030 arg-info)
2032 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
2034 ;;; The STATIC-SLOTS field of the funcallable instances used as early
2035 ;;; generic functions is used to store the early methods and early
2036 ;;; discriminator code for the early generic function. The static
2037 ;;; slots field of the fins contains a list whose:
2038 ;;; CAR - a list of the early methods on this early gf
2039 ;;; CADR - the early discriminator code for this method
2040 (defun ensure-generic-function-using-class (existing spec &rest keys
2041 &key (lambda-list nil
2042 lambda-list-p)
2043 argument-precedence-order
2044 definition-source
2045 documentation
2046 &allow-other-keys)
2047 (declare (ignore keys))
2048 (cond ((and existing (early-gf-p existing))
2049 (when lambda-list-p
2050 (set-arg-info existing :lambda-list lambda-list))
2051 existing)
2052 ((assoc spec *!generic-function-fixups* :test #'equal)
2053 (if existing
2054 (make-early-gf spec lambda-list lambda-list-p existing
2055 argument-precedence-order definition-source
2056 documentation)
2057 (bug "The function ~S is not already defined." spec)))
2058 (existing
2059 (bug "~S should be on the list ~S."
2060 spec '*!generic-function-fixups*))
2062 (pushnew spec *!early-generic-functions* :test #'equal)
2063 (make-early-gf spec lambda-list lambda-list-p nil
2064 argument-precedence-order definition-source
2065 documentation))))
2067 (defun make-early-gf (spec &optional lambda-list lambda-list-p
2068 function argument-precedence-order source-location
2069 documentation)
2070 (let ((fin (allocate-standard-funcallable-instance
2071 *sgf-wrapper* *sgf-slots-init*)))
2072 (set-funcallable-instance-function
2074 (or function
2075 (if (eq spec 'print-object)
2076 #'(lambda (instance stream)
2077 (print-unreadable-object (instance stream :identity t)
2078 (format stream "std-instance")))
2079 #'(lambda (&rest args)
2080 (declare (ignore args))
2081 (error "The function of the funcallable-instance ~S~
2082 has not been set." fin)))))
2083 (setf (gdefinition spec) fin)
2084 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
2085 (!bootstrap-set-slot 'standard-generic-function fin
2086 'source source-location)
2087 (!bootstrap-set-slot 'standard-generic-function fin
2088 '%documentation documentation)
2089 (set-fun-name fin spec)
2090 (let ((arg-info (make-arg-info)))
2091 (setf (early-gf-arg-info fin) arg-info)
2092 (when lambda-list-p
2093 (setf (info :function :type spec)
2094 (specifier-type
2095 (ftype-declaration-from-lambda-list lambda-list spec))
2096 (info :function :where-from spec) :defined-method)
2097 (if argument-precedence-order
2098 (set-arg-info fin
2099 :lambda-list lambda-list
2100 :argument-precedence-order argument-precedence-order)
2101 (set-arg-info fin :lambda-list lambda-list))))
2102 fin))
2104 (defun safe-gf-dfun-state (generic-function)
2105 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2106 (clos-slots-ref (fsc-instance-slots generic-function) +sgf-dfun-state-index+)
2107 (gf-dfun-state generic-function)))
2108 (defun (setf safe-gf-dfun-state) (new-value generic-function)
2109 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2110 (setf (clos-slots-ref (fsc-instance-slots generic-function)
2111 +sgf-dfun-state-index+)
2112 new-value)
2113 (setf (gf-dfun-state generic-function) new-value)))
2115 (defun set-dfun (gf &optional dfun cache info)
2116 (let ((new-state (if (and dfun (or cache info))
2117 (list* dfun cache info)
2118 dfun)))
2119 (cond
2120 ((eq **boot-state** 'complete)
2121 ;; Check that we are under the lock.
2122 #+sb-thread
2123 (aver (eq sb-thread:*current-thread* (sb-thread:mutex-owner (gf-lock gf))))
2124 (setf (safe-gf-dfun-state gf) new-state))
2126 (setf (clos-slots-ref (get-slots gf) +sgf-dfun-state-index+)
2127 new-state))))
2128 dfun)
2130 (defun gf-dfun-cache (gf)
2131 (let ((state (if (eq **boot-state** 'complete)
2132 (safe-gf-dfun-state gf)
2133 (clos-slots-ref (get-slots gf) +sgf-dfun-state-index+))))
2134 (typecase state
2135 (function nil)
2136 (cons (cadr state)))))
2138 (defun gf-dfun-info (gf)
2139 (let ((state (if (eq **boot-state** 'complete)
2140 (safe-gf-dfun-state gf)
2141 (clos-slots-ref (get-slots gf) +sgf-dfun-state-index+))))
2142 (typecase state
2143 (function nil)
2144 (cons (cddr state)))))
2146 (defconstant +sgf-name-index+
2147 (!bootstrap-slot-index 'standard-generic-function 'name))
2149 (defun !early-gf-name (gf)
2150 (clos-slots-ref (get-slots gf) +sgf-name-index+))
2152 (defun gf-lambda-list (gf)
2153 (let ((arg-info (if (eq **boot-state** 'complete)
2154 (gf-arg-info gf)
2155 (early-gf-arg-info gf))))
2156 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
2157 (let ((methods (if (eq **boot-state** 'complete)
2158 (generic-function-methods gf)
2159 (early-gf-methods gf))))
2160 (if (null methods)
2161 (progn
2162 (warn "no way to determine the lambda list for ~S" gf)
2163 nil)
2164 (let* ((method (car (last methods)))
2165 (ll (if (consp method)
2166 (early-method-lambda-list method)
2167 (method-lambda-list method))))
2168 (create-gf-lambda-list ll))))
2169 (arg-info-lambda-list arg-info))))
2171 (defmacro real-ensure-gf-internal (gf-class all-keys env)
2172 `(progn
2173 (cond ((symbolp ,gf-class)
2174 (setq ,gf-class (find-class ,gf-class t ,env)))
2175 ((classp ,gf-class))
2177 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
2178 class nor a symbol that names a class."
2179 ,gf-class)))
2180 (unless (class-finalized-p ,gf-class)
2181 (if (class-has-a-forward-referenced-superclass-p ,gf-class)
2182 ;; FIXME: reference MOP documentation -- this is an
2183 ;; additional requirement on our users
2184 (error "The generic function class ~S is not finalizeable" ,gf-class)
2185 (finalize-inheritance ,gf-class)))
2186 (remf ,all-keys :generic-function-class)
2187 (remf ,all-keys :environment)
2188 (let ((combin (getf ,all-keys :method-combination)))
2189 (etypecase combin
2190 (cons
2191 (setf (getf ,all-keys :method-combination)
2192 (find-method-combination (class-prototype ,gf-class)
2193 (car combin)
2194 (cdr combin))))
2195 ((or null method-combination))))
2196 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
2197 (unless (eq method-class '.shes-not-there.)
2198 (setf (getf ,all-keys :method-class)
2199 (cond ((classp method-class)
2200 method-class)
2201 (t (find-class method-class t ,env))))))))
2203 (defun note-gf-signature (fun-name lambda-list-p lambda-list)
2204 (unless lambda-list-p
2205 ;; Use the existing lambda-list, if any. It is reasonable to do eg.
2207 ;; (if (fboundp name)
2208 ;; (ensure-generic-function name)
2209 ;; (ensure-generic-function name :lambda-list '(foo)))
2211 ;; in which case we end up here with no lambda-list in the first leg.
2212 (setf (values lambda-list lambda-list-p)
2213 (handler-case
2214 (values (generic-function-lambda-list (fdefinition fun-name))
2216 ((or warning error) ()
2217 (values nil nil)))))
2218 (let ((gf-type
2219 (specifier-type
2220 (if lambda-list-p
2221 (ftype-declaration-from-lambda-list lambda-list fun-name)
2222 'function)))
2223 (old-type nil))
2224 ;; FIXME: Ideally we would like to not clobber it, but because generic
2225 ;; functions assert their FTYPEs callers believing the FTYPE are left with
2226 ;; unsafe assumptions. Hence the clobbering. Be quiet when the new type
2227 ;; is a subtype of the old one, though -- even though the type is not
2228 ;; trusted anymore, the warning is still not quite as interesting.
2229 (when (and (eq :declared (info :function :where-from fun-name))
2230 (not (csubtypep gf-type (setf old-type (info :function :type fun-name)))))
2231 (style-warn "~@<Generic function ~S clobbers an earlier ~S proclamation ~S ~
2232 for the same name with ~S.~:@>"
2233 fun-name 'ftype
2234 (type-specifier old-type)
2235 (type-specifier gf-type)))
2236 (setf (info :function :type fun-name) gf-type
2237 (info :function :where-from fun-name) :defined-method)
2238 fun-name))
2240 (defun real-ensure-gf-using-class--generic-function
2241 (existing
2242 fun-name
2243 &rest all-keys
2244 &key environment (lambda-list nil lambda-list-p)
2245 (generic-function-class 'standard-generic-function)
2246 &allow-other-keys)
2247 (real-ensure-gf-internal generic-function-class all-keys environment)
2248 ;; KLUDGE: the above macro does SETQ on GENERIC-FUNCTION-CLASS,
2249 ;; which is what makes the next line work
2250 (unless (eq (class-of existing) generic-function-class)
2251 (change-class existing generic-function-class))
2252 (prog1
2253 (apply #'reinitialize-instance existing all-keys)
2254 (note-gf-signature fun-name lambda-list-p lambda-list)))
2256 (defun real-ensure-gf-using-class--null
2257 (existing
2258 fun-name
2259 &rest all-keys
2260 &key environment (lambda-list nil lambda-list-p)
2261 (generic-function-class 'standard-generic-function)
2262 &allow-other-keys)
2263 (declare (ignore existing))
2264 (real-ensure-gf-internal generic-function-class all-keys environment)
2265 (prog1
2266 (setf (gdefinition fun-name)
2267 (apply #'make-instance generic-function-class
2268 :name fun-name all-keys))
2269 (note-gf-signature fun-name lambda-list-p lambda-list)))
2271 (defun safe-gf-arg-info (generic-function)
2272 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2273 (clos-slots-ref (fsc-instance-slots generic-function)
2274 +sgf-arg-info-index+)
2275 (gf-arg-info generic-function)))
2277 ;;; FIXME: this function took on a slightly greater role than it
2278 ;;; previously had around 2005-11-02, when CSR fixed the bug whereby
2279 ;;; having more than one subclass of standard-generic-function caused
2280 ;;; the whole system to die horribly through a metacircle in
2281 ;;; GF-ARG-INFO. The fix is to be slightly more disciplined about
2282 ;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when
2283 ;;; computing discriminating functions, so we need to be careful about
2284 ;;; having a base case for the recursion, and we provide that with the
2285 ;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet)
2286 ;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to
2287 ;;; that stage, where all potentially dangerous cases are enumerated
2288 ;;; and stopped. -- CSR, 2005-11-02.
2289 (defun get-generic-fun-info (gf)
2290 ;; values nreq applyp metatypes nkeys arg-info
2291 (multiple-value-bind (applyp metatypes arg-info)
2292 (let* ((arg-info (if (early-gf-p gf)
2293 (early-gf-arg-info gf)
2294 (safe-gf-arg-info gf)))
2295 (metatypes (arg-info-metatypes arg-info)))
2296 (values (arg-info-applyp arg-info)
2297 metatypes
2298 arg-info))
2299 (let ((nreq 0)
2300 (nkeys 0))
2301 (declare (fixnum nreq nkeys))
2302 (dolist (x metatypes)
2303 (incf nreq)
2304 (unless (eq x t)
2305 (incf nkeys)))
2306 (values nreq applyp metatypes
2307 nkeys
2308 arg-info))))
2310 (defun generic-function-nreq (gf)
2311 (let* ((arg-info (if (early-gf-p gf)
2312 (early-gf-arg-info gf)
2313 (safe-gf-arg-info gf)))
2314 (metatypes (arg-info-metatypes arg-info)))
2315 (declare (list metatypes))
2316 (length metatypes)))
2318 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
2319 &key slot-name object-class method-class-function
2320 definition-source)
2321 (let ((parsed ())
2322 (unparsed ()))
2323 ;; Figure out whether we got class objects or class names as the
2324 ;; specializers and set parsed and unparsed appropriately. If we
2325 ;; got class objects, then we can compute unparsed, but if we got
2326 ;; class names we don't try to compute parsed.
2328 ;; Note that the use of not symbolp in this call to every should be
2329 ;; read as 'classp' we can't use classp itself because it doesn't
2330 ;; exist yet.
2331 (if (every (lambda (s) (not (symbolp s))) specializers)
2332 (setq parsed specializers
2333 unparsed (mapcar (lambda (s)
2334 (if (eq s t) t (class-name s)))
2335 specializers))
2336 (setq unparsed specializers
2337 parsed ()))
2338 (let ((result
2339 (list :early-method
2341 (getf initargs :function)
2342 (let ((mf (getf initargs :function)))
2343 (aver mf)
2344 (and (typep mf '%method-function)
2345 (%method-function-fast-function mf)))
2347 ;; the parsed specializers. This is used by
2348 ;; EARLY-METHOD-SPECIALIZERS to cache the parse.
2349 ;; Note that this only comes into play when there is
2350 ;; more than one early method on an early gf.
2351 parsed
2353 ;; A list to which REAL-MAKE-A-METHOD can be applied
2354 ;; to make a real method corresponding to this early
2355 ;; one.
2356 (append
2357 (list class qualifiers arglist unparsed
2358 initargs doc)
2359 (when slot-name
2360 (list :slot-name slot-name :object-class object-class
2361 :method-class-function method-class-function))
2362 (list :definition-source definition-source)))))
2363 (initialize-method-function initargs result)
2364 result)))
2366 (defun real-make-a-method
2367 (class qualifiers lambda-list specializers initargs doc
2368 &rest args &key slot-name object-class method-class-function
2369 definition-source)
2370 (if method-class-function
2371 (let* ((object-class (if (classp object-class) object-class
2372 (find-class object-class)))
2373 (slots (class-direct-slots object-class))
2374 (slot-definition (find slot-name slots
2375 :key #'slot-definition-name)))
2376 (aver slot-name)
2377 (aver slot-definition)
2378 (let ((initargs (list* :qualifiers qualifiers :lambda-list lambda-list
2379 :specializers specializers :documentation doc
2380 :slot-definition slot-definition
2381 :slot-name slot-name initargs)))
2382 (apply #'make-instance
2383 (apply method-class-function object-class slot-definition
2384 initargs)
2385 :definition-source definition-source
2386 initargs)))
2387 (apply #'make-instance class :qualifiers qualifiers
2388 :lambda-list lambda-list :specializers specializers
2389 :documentation doc (append args initargs))))
2391 (defun early-method-function (early-method)
2392 (values (cadr early-method) (caddr early-method)))
2394 (defun early-method-class (early-method)
2395 (find-class (car (fifth early-method))))
2397 (defun early-method-standard-accessor-p (early-method)
2398 (let ((class (first (fifth early-method))))
2399 (or (eq class 'standard-reader-method)
2400 (eq class 'standard-writer-method)
2401 (eq class 'standard-boundp-method))))
2403 (defun early-method-standard-accessor-slot-name (early-method)
2404 (eighth (fifth early-method)))
2406 ;;; Fetch the specializers of an early method. This is basically just
2407 ;;; a simple accessor except that when the second argument is t, this
2408 ;;; converts the specializers from symbols into class objects. The
2409 ;;; class objects are cached in the early method, this makes
2410 ;;; bootstrapping faster because the class objects only have to be
2411 ;;; computed once.
2413 ;;; NOTE:
2414 ;;; The second argument should only be passed as T by
2415 ;;; early-lookup-method. This is to implement the rule that only when
2416 ;;; there is more than one early method on a generic function is the
2417 ;;; conversion from class names to class objects done. This
2418 ;;; corresponds to the fact that we are only allowed to have one
2419 ;;; method on any generic function up until the time classes exist.
2420 (defun early-method-specializers (early-method &optional objectsp)
2421 (if (and (listp early-method)
2422 (eq (car early-method) :early-method))
2423 (cond ((eq objectsp t)
2424 (or (fourth early-method)
2425 (setf (fourth early-method)
2426 (mapcar #'find-class (cadddr (fifth early-method))))))
2428 (fourth (fifth early-method))))
2429 (error "~S is not an early-method." early-method)))
2431 (defun early-method-qualifiers (early-method)
2432 (second (fifth early-method)))
2434 (defun early-method-lambda-list (early-method)
2435 (third (fifth early-method)))
2437 (defun early-method-initargs (early-method)
2438 (fifth (fifth early-method)))
2440 (defun (setf early-method-initargs) (new-value early-method)
2441 (setf (fifth (fifth early-method)) new-value))
2443 (defun early-add-named-method (generic-function-name qualifiers
2444 specializers arglist &rest initargs
2445 &key documentation definition-source
2446 &allow-other-keys)
2447 (let* (;; we don't need to deal with the :generic-function-class
2448 ;; argument here because the default,
2449 ;; STANDARD-GENERIC-FUNCTION, is right for all early generic
2450 ;; functions. (See REAL-ADD-NAMED-METHOD)
2451 (gf (ensure-generic-function generic-function-name))
2452 (existing
2453 (dolist (m (early-gf-methods gf))
2454 (when (and (equal (early-method-specializers m) specializers)
2455 (equal (early-method-qualifiers m) qualifiers))
2456 (return m)))))
2457 (setf (getf (getf initargs 'plist) :name)
2458 (make-method-spec gf qualifiers specializers))
2459 (let ((new (make-a-method 'standard-method qualifiers arglist
2460 specializers initargs documentation
2461 :definition-source definition-source)))
2462 (when existing (remove-method gf existing))
2463 (add-method gf new))))
2465 ;;; This is the early version of ADD-METHOD. Later this will become a
2466 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2467 ;;; special knowledge about ADD-METHOD.
2468 (defun add-method (generic-function method)
2469 (when (not (fsc-instance-p generic-function))
2470 (error "Early ADD-METHOD didn't get a funcallable instance."))
2471 (when (not (and (listp method) (eq (car method) :early-method)))
2472 (error "Early ADD-METHOD didn't get an early method."))
2473 (push method (early-gf-methods generic-function))
2474 (set-arg-info generic-function :new-method method)
2475 (unless (assoc (!early-gf-name generic-function)
2476 *!generic-function-fixups*
2477 :test #'equal)
2478 (update-dfun generic-function)))
2480 ;;; This is the early version of REMOVE-METHOD. See comments on
2481 ;;; the early version of ADD-METHOD.
2482 (defun remove-method (generic-function method)
2483 (when (not (fsc-instance-p generic-function))
2484 (error "An early remove-method didn't get a funcallable instance."))
2485 (when (not (and (listp method) (eq (car method) :early-method)))
2486 (error "An early remove-method didn't get an early method."))
2487 (setf (early-gf-methods generic-function)
2488 (remove method (early-gf-methods generic-function)))
2489 (set-arg-info generic-function)
2490 (unless (assoc (!early-gf-name generic-function)
2491 *!generic-function-fixups*
2492 :test #'equal)
2493 (update-dfun generic-function)))
2495 ;;; This is the early version of GET-METHOD. See comments on the early
2496 ;;; version of ADD-METHOD.
2497 (defun get-method (generic-function qualifiers specializers
2498 &optional (errorp t))
2499 (if (early-gf-p generic-function)
2500 (or (dolist (m (early-gf-methods generic-function))
2501 (when (and (or (equal (early-method-specializers m nil)
2502 specializers)
2503 (equal (early-method-specializers m t)
2504 specializers))
2505 (equal (early-method-qualifiers m) qualifiers))
2506 (return m)))
2507 (if errorp
2508 (error "can't get early method")
2509 nil))
2510 (real-get-method generic-function qualifiers specializers errorp)))
2512 ;; minor KLUDGE: a separate code component for this function allows GCing
2513 ;; a few symbols and their associated code that would otherwise be retained:
2514 ;; *!EARLY-{GENERIC-}FUNCTIONS*, *!GENERIC-FUNCTION-FIXUPS*
2515 (defun early-gf-primary-slow-method-fn (fn)
2516 (lambda (args next-methods)
2517 (declare (ignore next-methods))
2518 (apply fn args)))
2520 (defun !fix-early-generic-functions ()
2521 (let ((accessors nil))
2522 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2523 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2524 (dolist (early-gf-spec *!early-generic-functions*)
2525 (when (every #'early-method-standard-accessor-p
2526 (early-gf-methods (gdefinition early-gf-spec)))
2527 (push early-gf-spec accessors)))
2528 (dolist (spec (nconc accessors
2529 '(accessor-method-slot-name
2530 generic-function-methods
2531 method-specializers
2532 specializerp
2533 specializer-type
2534 specializer-class
2535 slot-definition-location
2536 slot-definition-name
2537 class-slots
2538 gf-arg-info
2539 class-precedence-list
2540 slot-boundp-using-class
2541 (setf slot-value-using-class)
2542 slot-value-using-class
2543 structure-class-p
2544 standard-class-p
2545 funcallable-standard-class-p
2546 specializerp)))
2547 (/show spec)
2548 (setq *!early-generic-functions*
2549 (cons spec
2550 (delete spec *!early-generic-functions* :test #'equal))))
2552 (dolist (early-gf-spec *!early-generic-functions*)
2553 (/show early-gf-spec)
2554 (let* ((gf (gdefinition early-gf-spec))
2555 (methods (mapcar (lambda (early-method)
2556 (let ((args (copy-list (fifth
2557 early-method))))
2558 (setf (fourth args)
2559 (early-method-specializers
2560 early-method t))
2561 (apply #'real-make-a-method args)))
2562 (early-gf-methods gf))))
2563 (setf (generic-function-method-class gf) *the-class-standard-method*)
2564 (setf (generic-function-method-combination gf)
2565 *standard-method-combination*)
2566 (set-methods gf methods)))
2568 (dolist (fn *!early-functions*)
2569 (/show fn)
2570 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2572 (dolist (fixup *!generic-function-fixups*)
2573 (/show fixup)
2574 (let* ((fspec (car fixup))
2575 (gf (gdefinition fspec))
2576 (methods (mapcar (lambda (method)
2577 (let* ((lambda-list (first method))
2578 (specializers (mapcar #'find-class (second method)))
2579 (method-fn-name (third method))
2580 (fn-name (or method-fn-name fspec))
2581 (fn (fdefinition fn-name))
2582 (initargs
2583 (list :function
2584 (set-fun-name
2585 (early-gf-primary-slow-method-fn fn)
2586 `(call ,fn-name)))))
2587 (declare (type function fn))
2588 (make-a-method 'standard-method
2590 lambda-list
2591 specializers
2592 initargs
2593 nil)))
2594 (cdr fixup))))
2595 (setf (generic-function-method-class gf) *the-class-standard-method*)
2596 (setf (generic-function-method-combination gf)
2597 *standard-method-combination*)
2598 (set-methods gf methods))))
2599 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2601 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2602 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2603 ;;; is really implemented.
2604 (defun parse-defmethod (cdr-of-form)
2605 (declare (list cdr-of-form))
2606 (let ((qualifiers ())
2607 (spec-ll ()))
2608 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2609 (push (pop cdr-of-form) qualifiers)
2610 (return (setq qualifiers (nreverse qualifiers)))))
2611 (setq spec-ll (pop cdr-of-form))
2612 (values qualifiers spec-ll cdr-of-form)))
2614 (defun parse-specializers (generic-function specializers)
2615 (declare (list specializers))
2616 (flet ((parse (spec)
2617 (parse-specializer-using-class generic-function spec)))
2618 (mapcar #'parse specializers)))
2620 (defun unparse-specializers (generic-function specializers)
2621 (declare (list specializers))
2622 (flet ((unparse (spec)
2623 (unparse-specializer-using-class generic-function spec)))
2624 (mapcar #'unparse specializers)))
2626 (defun extract-parameters (specialized-lambda-list)
2627 (multiple-value-bind (parameters ignore1 ignore2)
2628 (parse-specialized-lambda-list specialized-lambda-list)
2629 (declare (ignore ignore1 ignore2))
2630 parameters))
2632 (defun extract-lambda-list (specialized-lambda-list)
2633 (multiple-value-bind (ignore1 lambda-list ignore2)
2634 (parse-specialized-lambda-list specialized-lambda-list)
2635 (declare (ignore ignore1 ignore2))
2636 lambda-list))
2638 (defun extract-specializer-names (specialized-lambda-list)
2639 (multiple-value-bind (ignore1 ignore2 specializers)
2640 (parse-specialized-lambda-list specialized-lambda-list)
2641 (declare (ignore ignore1 ignore2))
2642 specializers))
2644 (defun extract-required-parameters (specialized-lambda-list)
2645 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2646 (parse-specialized-lambda-list specialized-lambda-list)
2647 (declare (ignore ignore1 ignore2 ignore3))
2648 required-parameters))
2650 (define-condition specialized-lambda-list-error
2651 (reference-condition simple-program-error)
2653 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2655 (defun parse-specialized-lambda-list
2656 (arglist
2657 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2658 &aux (specialized-lambda-list-keywords
2659 '(&optional &rest &key &allow-other-keys &aux)))
2660 (let ((arg (car arglist)))
2661 (cond ((null arglist) (values nil nil nil nil))
2662 ((eq arg '&aux)
2663 (values nil arglist nil nil))
2664 ((memq arg lambda-list-keywords)
2665 ;; non-standard lambda-list-keywords are errors.
2666 (unless (memq arg specialized-lambda-list-keywords)
2667 (error 'specialized-lambda-list-error
2668 :format-control "unknown specialized-lambda-list ~
2669 keyword ~S~%"
2670 :format-arguments (list arg)))
2671 ;; no multiple &rest x &rest bla specifying
2672 (when (memq arg supplied-keywords)
2673 (error 'specialized-lambda-list-error
2674 :format-control "multiple occurrence of ~
2675 specialized-lambda-list keyword ~S~%"
2676 :format-arguments (list arg)))
2677 ;; And no placing &key in front of &optional, either.
2678 (unless (memq arg allowed-keywords)
2679 (error 'specialized-lambda-list-error
2680 :format-control "misplaced specialized-lambda-list ~
2681 keyword ~S~%"
2682 :format-arguments (list arg)))
2683 ;; When we are at a lambda-list keyword, the parameters
2684 ;; don't include the lambda-list keyword; the lambda-list
2685 ;; does include the lambda-list keyword; and no
2686 ;; specializers are allowed to follow the lambda-list
2687 ;; keywords (at least for now).
2688 (multiple-value-bind (parameters lambda-list)
2689 (parse-specialized-lambda-list (cdr arglist)
2690 (cons arg supplied-keywords)
2691 (if (eq arg '&key)
2692 (cons '&allow-other-keys
2693 (cdr (member arg allowed-keywords)))
2694 (cdr (member arg allowed-keywords))))
2695 (when (and (eq arg '&rest)
2696 (or (null lambda-list)
2697 (memq (car lambda-list)
2698 specialized-lambda-list-keywords)
2699 (not (or (null (cadr lambda-list))
2700 (memq (cadr lambda-list)
2701 specialized-lambda-list-keywords)))))
2702 (error 'specialized-lambda-list-error
2703 :format-control
2704 "in a specialized-lambda-list, excactly one ~
2705 variable must follow &REST.~%"
2706 :format-arguments nil))
2707 (values parameters
2708 (cons arg lambda-list)
2710 ())))
2711 (supplied-keywords
2712 ;; After a lambda-list keyword there can be no specializers.
2713 (multiple-value-bind (parameters lambda-list)
2714 (parse-specialized-lambda-list (cdr arglist)
2715 supplied-keywords
2716 allowed-keywords)
2717 (values (cons (if (listp arg) (car arg) arg) parameters)
2718 (cons arg lambda-list)
2720 ())))
2722 (multiple-value-bind (parameters lambda-list specializers required)
2723 (parse-specialized-lambda-list (cdr arglist))
2724 ;; Check for valid arguments.
2725 (unless (or (and (symbolp arg) (not (null arg)))
2726 (and (consp arg)
2727 (consp (cdr arg))
2728 (null (cddr arg))))
2729 (error 'specialized-lambda-list-error
2730 :format-control "arg is not a non-NIL symbol or a list of two elements: ~A"
2731 :format-arguments (list arg)))
2732 (values (cons (if (listp arg) (car arg) arg) parameters)
2733 (cons (if (listp arg) (car arg) arg) lambda-list)
2734 (cons (if (listp arg) (cadr arg) t) specializers)
2735 (cons (if (listp arg) (car arg) arg) required)))))))
2737 (setq **boot-state** 'early)
2739 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2740 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2741 ;;; walker stuff was only used for implementing stuff like that; maybe
2742 ;;; it's not needed any more? Hunt down what it was used for and see.
2744 (defun extract-the (form)
2745 (cond ((and (consp form) (eq (car form) 'the))
2746 (aver (proper-list-of-length-p form 3))
2747 (third form))
2749 form)))
2751 (defmacro with-slots (slots instance &body body)
2752 (let ((in (gensym)))
2753 `(let ((,in ,instance))
2754 (declare (ignorable ,in))
2755 ,@(let ((instance (extract-the instance)))
2756 (and (symbolp instance)
2757 `((declare (%variable-rebinding ,in ,instance)))))
2759 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2760 (let ((var-name
2761 (if (symbolp slot-entry)
2762 slot-entry
2763 (car slot-entry)))
2764 (slot-name
2765 (if (symbolp slot-entry)
2766 slot-entry
2767 (cadr slot-entry))))
2768 `(,var-name
2769 (slot-value ,in ',slot-name))))
2770 slots)
2771 ,@body))))
2773 (defmacro with-accessors (slots instance &body body)
2774 (let ((in (gensym)))
2775 `(let ((,in ,instance))
2776 (declare (ignorable ,in))
2777 ,@(let ((instance (extract-the instance)))
2778 (and (symbolp instance)
2779 `((declare (%variable-rebinding ,in ,instance)))))
2781 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2782 (let ((var-name (car slot-entry))
2783 (accessor-name (cadr slot-entry)))
2784 `(,var-name (,accessor-name ,in))))
2785 slots)
2786 ,@body))))