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