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