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