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