1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
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
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
71 ;;; FIXME: As of sbcl-0.6.9.10, PCL still uses this nonstandard type
72 ;;; of declaration internally. It would be good to figure out how to
73 ;;; get rid of it, or failing that, (1) document why it's needed and
74 ;;; (2) use a private symbol with a forbidding name which suggests
75 ;;; it's not to be messed with by the user (e.g. SB-PCL:%CLASS)
76 ;;; instead of the too-inviting CLASS. (I tried just deleting the
77 ;;; declarations in MAKE-METHOD-LAMBDA-INTERNAL ca. sbcl-0.6.9.10, but
78 ;;; then things break.)
79 (declaim (declaration class))
81 ;;; FIXME: SB-KERNEL::PCL-CHECK-WRAPPER-VALIDITY-HOOK shouldn't be a
82 ;;; separate function. Instead, we should define a simple placeholder
83 ;;; version of SB-PCL:CHECK-WRAPPER-VALIDITY where
84 ;;; SB-KERNEL::PCL-CHECK-WRAPPER-VALIDITY is defined now, then just
85 ;;; let the later real PCL DEFUN of SB-PCL:CHECK-WRAPPER-VALIDITY
87 (setf (symbol-function 'sb-kernel::pcl-check-wrapper-validity-hook)
88 #'check-wrapper-validity)
90 (declaim (notinline make-a-method
92 ensure-generic-function-using-class
96 (defvar *!early-functions*
97 '((make-a-method early-make-a-method
99 (add-named-method early-add-named-method
100 real-add-named-method)
103 ;;; For each of the early functions, arrange to have it point to its
104 ;;; early definition. Do this in a way that makes sure that if we
105 ;;; redefine one of the early definitions the redefinition will take
106 ;;; effect. This makes development easier.
107 (dolist (fns *!early-functions*)
108 (let ((name (car fns))
109 (early-name (cadr fns)))
110 (setf (gdefinition name)
113 (apply (fdefinition early-name) args))
116 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
117 ;;; to convert the few functions in the bootstrap which are supposed
118 ;;; to be generic functions but can't be early on.
119 (defvar *!generic-function-fixups*
121 ((generic-function method) ;lambda-list
122 (standard-generic-function method) ;specializers
123 real-add-method)) ;method-function
125 ((generic-function method)
126 (standard-generic-function method)
129 ((generic-function qualifiers specializers &optional (errorp t))
130 (standard-generic-function t t)
132 (ensure-generic-function-using-class
133 ((generic-function fun-name
134 &key generic-function-class environment
137 real-ensure-gf-using-class--generic-function)
138 ((generic-function fun-name
139 &key generic-function-class environment
142 real-ensure-gf-using-class--null))
144 ((proto-generic-function proto-method lambda-expression environment)
145 (standard-generic-function standard-method t t)
146 real-make-method-lambda))
147 (make-method-initargs-form
148 ((proto-generic-function proto-method
150 lambda-list environment)
151 (standard-generic-function standard-method t t t)
152 real-make-method-initargs-form))
153 (compute-effective-method
154 ((generic-function combin applicable-methods)
155 (generic-function standard-method-combination t)
156 standard-compute-effective-method))))
158 (defmacro defgeneric (fun-name lambda-list &body options)
159 (declare (type list lambda-list))
160 (unless (legal-fun-name-p fun-name)
161 (error 'simple-program-error
162 :format-control "illegal generic function name ~S"
163 :format-arguments (list fun-name)))
166 (flet ((duplicate-option (name)
167 (error 'simple-program-error
168 :format-control "The option ~S appears more than once."
169 :format-arguments (list name)))
170 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
171 (let* ((arglist-pos (position-if #'listp qab))
172 (arglist (elt qab arglist-pos))
173 (qualifiers (subseq qab 0 arglist-pos))
174 (body (nthcdr (1+ arglist-pos) qab)))
175 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
176 (generic-function-initial-methods #',fun-name)))))
177 (macrolet ((initarg (key) `(getf initargs ,key)))
178 (dolist (option options)
179 (let ((car-option (car option)))
182 (push (cdr option) (initarg :declarations)))
183 ((:argument-precedence-order :method-combination)
184 (if (initarg car-option)
185 (duplicate-option car-option)
186 (setf (initarg car-option)
188 ((:documentation :generic-function-class :method-class)
189 (unless (proper-list-of-length-p option 2)
190 (error "bad list length for ~S" option))
191 (if (initarg car-option)
192 (duplicate-option car-option)
193 (setf (initarg car-option) `',(cadr option))))
195 (push (cdr option) methods))
197 ;; ANSI requires that unsupported things must get a
199 (error 'simple-program-error
200 :format-control "unsupported option ~S"
201 :format-arguments (list option))))))
203 (when (initarg :declarations)
204 (setf (initarg :declarations)
205 `',(initarg :declarations))))
207 (eval-when (:compile-toplevel :load-toplevel :execute)
208 (compile-or-load-defgeneric ',fun-name))
209 (load-defgeneric ',fun-name ',lambda-list ,@initargs)
210 ,@(mapcar #'expand-method-definition methods)
213 (defun compile-or-load-defgeneric (fun-name)
214 (sb-kernel:proclaim-as-fun-name fun-name)
215 (sb-kernel:note-name-defined fun-name :function)
216 (unless (eq (info :function :where-from fun-name) :declared)
217 (setf (info :function :where-from fun-name) :defined)
218 (setf (info :function :type fun-name)
219 (sb-kernel:specifier-type 'function))))
221 (defun load-defgeneric (fun-name lambda-list &rest initargs)
222 (when (fboundp fun-name)
223 (sb-kernel::style-warn "redefining ~S in DEFGENERIC" fun-name)
224 (let ((fun (fdefinition fun-name)))
225 (when (generic-function-p fun)
226 (loop for method in (generic-function-initial-methods fun)
227 do (remove-method fun method))
228 (setf (generic-function-initial-methods fun) '()))))
229 (apply #'ensure-generic-function
231 :lambda-list lambda-list
232 :definition-source `((defgeneric ,fun-name) ,*load-truename*)
235 (defmacro defmethod (&rest args &environment env)
236 (multiple-value-bind (name qualifiers lambda-list body)
237 (parse-defmethod args)
238 (multiple-value-bind (proto-gf proto-method)
239 (prototypes-for-make-method-lambda name)
240 (expand-defmethod name
248 (defun prototypes-for-make-method-lambda (name)
249 (if (not (eq *boot-state* 'complete))
251 (let ((gf? (and (gboundp name)
252 (gdefinition name))))
254 (not (generic-function-p gf?)))
255 (values (class-prototype (find-class 'standard-generic-function))
256 (class-prototype (find-class 'standard-method)))
258 (class-prototype (or (generic-function-method-class gf?)
259 (find-class 'standard-method))))))))
261 ;;; Take a name which is either a generic function name or a list specifying
262 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
263 ;;; the prototype instance of the method-class for that generic function.
265 ;;; If there is no generic function by that name, this returns the
266 ;;; default value, the prototype instance of the class
267 ;;; STANDARD-METHOD. This default value is also returned if the spec
268 ;;; names an ordinary function or even a macro. In effect, this leaves
269 ;;; the signalling of the appropriate error until load time.
271 ;;; Note: During bootstrapping, this function is allowed to return NIL.
272 (defun method-prototype-for-gf (name)
273 (let ((gf? (and (gboundp name)
274 (gdefinition name))))
275 (cond ((neq *boot-state* 'complete) nil)
277 (not (generic-function-p gf?))) ; Someone else MIGHT
278 ; error at load time.
279 (class-prototype (find-class 'standard-method)))
281 (class-prototype (or (generic-function-method-class gf?)
282 (find-class 'standard-method)))))))
284 (defvar *optimize-asv-funcall-p* nil)
285 (defvar *asv-readers*)
286 (defvar *asv-writers*)
287 (defvar *asv-boundps*)
289 (defun expand-defmethod (name
296 (let ((*make-instance-function-keys* nil)
297 (*optimize-asv-funcall-p* t)
298 (*asv-readers* nil) (*asv-writers* nil) (*asv-boundps* nil))
299 (declare (special *make-instance-function-keys*))
300 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
301 (add-method-declarations name qualifiers lambda-list body env)
302 (multiple-value-bind (method-function-lambda initargs)
303 (make-method-lambda proto-gf proto-method method-lambda env)
304 (let ((initargs-form (make-method-initargs-form proto-gf
306 method-function-lambda
310 ;; Note: We could DECLAIM the ftype of the generic
311 ;; function here, since ANSI specifies that we create it
312 ;; if it does not exist. However, I chose not to, because
313 ;; I think it's more useful to support a style of
314 ;; programming where every generic function has an
315 ;; explicit DEFGENERIC and any typos in DEFMETHODs are
316 ;; warned about. Otherwise
317 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
318 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
319 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
320 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
321 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
322 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
323 ;; compiles without raising an error and runs without
324 ;; raising an error (since SIMPLE-VECTOR cases fall
325 ;; through to VECTOR) but still doesn't do what was
326 ;; intended. I hate that kind of bug (code which silently
327 ;; gives the wrong answer), so we don't do a DECLAIM
328 ;; here. -- WHN 20000229
329 ,@(when *make-instance-function-keys*
330 `((get-make-instance-functions
331 ',*make-instance-function-keys*)))
332 ,@(when (or *asv-readers* *asv-writers* *asv-boundps*)
333 `((initialize-internal-slot-gfs*
334 ',*asv-readers* ',*asv-writers* ',*asv-boundps*)))
335 ,(make-defmethod-form name qualifiers specializers
336 unspecialized-lambda-list
338 (class-name (class-of proto-method))
341 (getf (getf initargs :plist)
342 :pv-table-symbol))))))))
344 (defun interned-symbol-p (x)
345 (and (symbolp x) (symbol-package x)))
347 (defun make-defmethod-form (name qualifiers specializers
348 unspecialized-lambda-list method-class-name
349 initargs-form &optional pv-table-symbol)
352 (if (and (interned-symbol-p (fun-name-block-name name))
353 (every #'interned-symbol-p qualifiers)
356 (and (eq (car s) 'eql)
358 (let ((sv (eval (cadr s))))
359 (or (interned-symbol-p sv)
362 (standard-char-p sv)))))
363 (interned-symbol-p s)))
365 (consp initargs-form)
366 (eq (car initargs-form) 'list*)
367 (memq (cadr initargs-form) '(:function :fast-function))
368 (consp (setq fn (caddr initargs-form)))
369 (eq (car fn) 'function)
370 (consp (setq fn-lambda (cadr fn)))
371 (eq (car fn-lambda) 'lambda))
372 (let* ((specls (mapcar (lambda (specl)
374 `(,(car specl) ,(eval (cadr specl)))
377 (mname `(,(if (eq (cadr initargs-form) :function)
378 'method 'fast-method)
379 ,name ,@qualifiers ,specls))
380 (mname-sym (intern (let ((*print-pretty* nil)
381 ;; (We bind *PACKAGE* to
382 ;; KEYWORD here as a way to
383 ;; force symbols to be printed
384 ;; with explicit package
386 (*package* *keyword-package*))
387 (format nil "~S" mname)))))
389 (defun ,mname-sym ,(cadr fn-lambda)
391 ,(make-defmethod-form-internal
392 name qualifiers `',specls
393 unspecialized-lambda-list method-class-name
394 `(list* ,(cadr initargs-form)
396 ,@(cdddr initargs-form))
398 (make-defmethod-form-internal
400 `(list ,@(mapcar (lambda (specializer)
401 (if (consp specializer)
402 ``(,',(car specializer)
403 ,,(cadr specializer))
406 unspecialized-lambda-list
411 (defun make-defmethod-form-internal
412 (name qualifiers specializers-form unspecialized-lambda-list
413 method-class-name initargs-form &optional pv-table-symbol)
419 ',unspecialized-lambda-list
421 ;; Paper over a bug in KCL by passing the cache-symbol here in
422 ;; addition to in the list. FIXME: We should no longer need to do
423 ;; this, since the CLOS code is now SBCL-specific, and doesn't
424 ;; need to be ported to every buggy compiler in existence.
427 (defmacro make-method-function (method-lambda &environment env)
428 (make-method-function-internal method-lambda env))
430 (defun make-method-function-internal (method-lambda &optional env)
431 (multiple-value-bind (proto-gf proto-method)
432 (prototypes-for-make-method-lambda nil)
433 (multiple-value-bind (method-function-lambda initargs)
434 (make-method-lambda proto-gf proto-method method-lambda env)
435 (make-method-initargs-form proto-gf
437 method-function-lambda
441 (defun add-method-declarations (name qualifiers lambda-list body env)
442 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
443 (parse-specialized-lambda-list lambda-list)
444 (declare (ignore parameters))
445 (multiple-value-bind (documentation declarations real-body)
446 (extract-declarations body env)
447 (values `(lambda ,unspecialized-lambda-list
448 ,@(when documentation `(,documentation))
449 ;; (Old PCL code used a somewhat different style of
450 ;; list for %METHOD-NAME values. Our names use
451 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
452 ;; method names look more like what you see in a
455 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
456 ;; least the code to set up named BLOCKs around the
457 ;; bodies of methods, depends on the function's base
458 ;; name being the first element of the %METHOD-NAME
459 ;; list. It would be good to remove this dependency,
460 ;; perhaps by building the BLOCK here, or by using
461 ;; another declaration (e.g. %BLOCK-NAME), so that
462 ;; our method debug names are free to have any format,
463 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
464 (declare (%method-name (,name
467 (declare (%method-lambda-list ,@lambda-list))
470 unspecialized-lambda-list specializers))))
472 (defun real-make-method-initargs-form (proto-gf proto-method
473 method-lambda initargs env)
474 (declare (ignore proto-gf proto-method))
475 (unless (and (consp method-lambda)
476 (eq (car method-lambda) 'lambda))
477 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
478 is not a lambda form."
480 (make-method-initargs-form-internal method-lambda initargs env))
482 (unless (fboundp 'make-method-initargs-form)
483 (setf (gdefinition 'make-method-initargs-form)
484 (symbol-function 'real-make-method-initargs-form)))
486 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
487 (declare (ignore proto-gf proto-method))
488 (make-method-lambda-internal method-lambda env))
490 ;;; a helper function for creating Python-friendly type declarations
491 ;;; in DEFMETHOD forms
492 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
493 (cond ((and (consp specializer)
494 (eq (car specializer) 'eql))
495 ;; KLUDGE: ANSI, in its wisdom, says that
496 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
497 ;; DEFMETHOD expansion time. Thus, although one might think
499 ;; (DEFMETHOD FOO ((X PACKAGE)
502 ;; the PACKAGE and (EQL 12) forms are both parallel type
503 ;; names, they're not, as is made clear when you do
504 ;; (DEFMETHOD FOO ((X PACKAGE)
507 ;; where Y needs to be a symbol named "BAR", not some cons
508 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
509 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
510 ;; to be of type (EQL X). It'd be easy to transform one to
511 ;; the other, but it'd be somewhat messier to do so while
512 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
513 ;; once. (The new code wouldn't be messy, but it'd require a
514 ;; big transformation of the old code.) So instead we punt.
518 ;; KLUDGE: For some low-level implementation
519 ;; classes, perhaps because of some problems related
520 ;; to the incomplete integration of PCL into SBCL's
521 ;; type system, some specializer classes can't be
522 ;; declared as argument types. E.g.
523 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
524 ;; (DECLARE (TYPE SLOT-OBJECT X))
527 ;; (DEFSTRUCT BAR A B)
529 ;; perhaps because of the way that STRUCTURE-OBJECT
530 ;; inherits both from SLOT-OBJECT and from
531 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
532 ;; problems under the rug, we exclude these problem
533 ;; cases by blacklisting them here. -- WHN 2001-01-19
536 ((not (eq *boot-state* 'complete))
537 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
538 ;; types which don't match their specializers. (Specifically,
539 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
540 ;; second argument.) Hopefully it only does this kind of
541 ;; weirdness when bootstrapping.. -- WHN 20000610
544 ;; Otherwise, we can make Python very happy.
545 `(type ,specializer ,parameter))))
547 (defun make-method-lambda-internal (method-lambda &optional env)
548 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
549 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
550 is not a lambda form."
552 (multiple-value-bind (documentation declarations real-body)
553 (extract-declarations (cddr method-lambda) env)
554 (let* ((name-decl (get-declaration '%method-name declarations))
555 (sll-decl (get-declaration '%method-lambda-list declarations))
556 (method-name (when (consp name-decl) (car name-decl)))
557 (generic-function-name (when method-name (car method-name)))
558 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
559 (multiple-value-bind (parameters lambda-list specializers)
560 (parse-specialized-lambda-list specialized-lambda-list)
561 (let* ((required-parameters
562 (mapcar (lambda (r s) (declare (ignore s)) r)
565 (slots (mapcar #'list required-parameters))
569 ;; These declarations seem to be used by PCL to pass
570 ;; information to itself; when I tried to delete 'em
571 ;; ca. 0.6.10 it didn't work. I'm not sure how
572 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
573 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
575 (mapcar (lambda (a s) (and (symbolp s)
580 ;; These TYPE declarations weren't in the original
581 ;; PCL code, but the Python compiler likes them a
582 ;; lot. (We're telling the compiler about our
583 ;; knowledge of specialized argument types so that
584 ;; it can avoid run-time type dispatch overhead,
585 ;; which can be a huge win for Python.)
587 ;; FIXME: Perhaps these belong in
588 ;; ADD-METHOD-DECLARATIONS instead of here?
589 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
593 ;; Remove the documentation string and insert the
594 ;; appropriate class declarations. The documentation
595 ;; string is removed to make it easy for us to insert
596 ;; new declarations later, they will just go after the
597 ;; CADR of the method lambda. The class declarations
598 ;; are inserted to communicate the class of the method's
599 ;; arguments to the code walk.
600 `(lambda ,lambda-list
601 ;; The default ignorability of method parameters
602 ;; doesn't seem to be specified by ANSI. PCL had
603 ;; them basically ignorable but was a little
604 ;; inconsistent. E.g. even though the two
605 ;; method definitions
606 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
607 ;; (DEFMETHOD FOO ((X T) Y) "Z")
608 ;; are otherwise equivalent, PCL treated Y as
609 ;; ignorable in the first definition but not in the
610 ;; second definition. We make all required
611 ;; parameters ignorable as a way of systematizing
612 ;; the old PCL behavior. -- WHN 2000-11-24
613 (declare (ignorable ,@required-parameters))
616 (block ,(fun-name-block-name generic-function-name)
618 (constant-value-p (and (null (cdr real-body))
619 (constantp (car real-body))))
620 (constant-value (and constant-value-p
621 (eval (car real-body))))
622 (plist (and constant-value-p
623 (or (typep constant-value
624 '(or number character))
625 (and (symbolp constant-value)
626 (symbol-package constant-value)))
627 (list :constant-value constant-value)))
628 (applyp (dolist (p lambda-list nil)
629 (cond ((memq p '(&optional &rest &key))
634 (walked-lambda call-next-method-p closurep next-method-p-p)
635 (walk-method-lambda method-lambda
641 (ignore walked-declarations walked-lambda-body)
642 (extract-declarations (cddr walked-lambda))
643 (declare (ignore ignore))
644 (when (or next-method-p-p call-next-method-p)
645 (setq plist (list* :needs-next-methods-p t plist)))
646 (when (some #'cdr slots)
647 (multiple-value-bind (slot-name-lists call-list)
648 (slot-name-lists-from-slots slots calls)
649 (let ((pv-table-symbol (make-symbol "pv-table")))
651 `(,@(when slot-name-lists
652 `(:slot-name-lists ,slot-name-lists))
654 `(:call-list ,call-list))
655 :pv-table-symbol ,pv-table-symbol
657 (setq walked-lambda-body
658 `((pv-binding (,required-parameters
661 ,@walked-lambda-body))))))
662 (when (and (memq '&key lambda-list)
663 (not (memq '&allow-other-keys lambda-list)))
664 (let ((aux (memq '&aux lambda-list)))
665 (setq lambda-list (nconc (ldiff lambda-list aux)
666 (list '&allow-other-keys)
668 (values `(lambda (.method-args. .next-methods.)
669 (simple-lexical-method-functions
670 (,lambda-list .method-args. .next-methods.
673 :next-method-p-p ,next-method-p-p
676 ,@walked-declarations
677 ,@walked-lambda-body))
680 ,@(when documentation
681 `(:documentation ,documentation)))))))))))
683 (unless (fboundp 'make-method-lambda)
684 (setf (gdefinition 'make-method-lambda)
685 (symbol-function 'real-make-method-lambda)))
687 (defmacro simple-lexical-method-functions ((lambda-list
693 ,method-args ,next-methods
694 (bind-simple-lexical-method-macros (,method-args ,next-methods)
695 (bind-lexical-method-functions (,@lmf-options)
696 (bind-args (,lambda-list ,method-args)
699 (defmacro fast-lexical-method-functions ((lambda-list
705 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
706 (bind-lexical-method-functions (,@lmf-options)
707 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
710 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
712 `(macrolet ((call-next-method-bind (&body body)
713 `(let ((.next-method. (car ,',next-methods))
714 (,',next-methods (cdr ,',next-methods)))
715 .next-method. ,',next-methods
717 (call-next-method-body (cnm-args)
719 (funcall (if (std-instance-p .next-method.)
720 (method-function .next-method.)
721 .next-method.) ; for early methods
722 (or ,cnm-args ,',method-args)
724 (error "no next method")))
725 (next-method-p-body ()
726 `(not (null .next-method.))))
729 (defstruct (method-call (:copier nil))
730 (function #'identity :type function)
733 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
735 (defmacro invoke-method-call1 (function args cm-args)
736 `(let ((.function. ,function)
738 (.cm-args. ,cm-args))
739 (if (and .cm-args. (null (cdr .cm-args.)))
740 (funcall .function. .args. (car .cm-args.))
741 (apply .function. .args. .cm-args.))))
743 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
744 `(invoke-method-call1 (method-call-function ,method-call)
746 `(list* ,@required-args+rest-arg)
747 `(list ,@required-args+rest-arg))
748 (method-call-call-method-args ,method-call)))
750 (defstruct (fast-method-call (:copier nil))
751 (function #'identity :type function)
756 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
758 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
759 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
761 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
762 `(fmc-funcall (fast-method-call-function ,method-call)
763 (fast-method-call-pv-cell ,method-call)
764 (fast-method-call-next-method-call ,method-call)
765 ,@required-args+rest-arg))
767 (defstruct (fast-instance-boundp (:copier nil))
768 (index 0 :type fixnum))
770 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
772 (eval-when (:compile-toplevel :load-toplevel :execute)
773 (defvar *allow-emf-call-tracing-p* nil)
774 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
776 ;;;; effective method functions
778 (defvar *emf-call-trace-size* 200)
779 (defvar *emf-call-trace* nil)
780 (defvar *emf-call-trace-index* 0)
782 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
783 ;;; without explanation. It appears to be intended for debugging, so
784 ;;; it might be useful someday, so I haven't deleted it.
785 ;;; But it isn't documented and isn't used for anything now, so
786 ;;; I've conditionalized it out of the base system. -- WHN 19991213
788 (defun show-emf-call-trace ()
789 (when *emf-call-trace*
790 (let ((j *emf-call-trace-index*)
791 (*enable-emf-call-tracing-p* nil))
792 (format t "~&(The oldest entries are printed first)~%")
793 (dotimes-fixnum (i *emf-call-trace-size*)
794 (let ((ct (aref *emf-call-trace* j)))
795 (when ct (print ct)))
797 (when (= j *emf-call-trace-size*)
800 (defun trace-emf-call-internal (emf format args)
801 (unless *emf-call-trace*
802 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
803 (setf (aref *emf-call-trace* *emf-call-trace-index*)
804 (list* emf format args))
805 (incf *emf-call-trace-index*)
806 (when (= *emf-call-trace-index* *emf-call-trace-size*)
807 (setq *emf-call-trace-index* 0)))
809 (defmacro trace-emf-call (emf format args)
810 (when *allow-emf-call-tracing-p*
811 `(when *enable-emf-call-tracing-p*
812 (trace-emf-call-internal ,emf ,format ,args))))
814 (defmacro invoke-effective-method-function-fast
815 (emf restp &rest required-args+rest-arg)
817 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
818 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
820 (defmacro invoke-effective-method-function (emf restp
821 &rest required-args+rest-arg)
822 (unless (constantp restp)
823 (error "The RESTP argument is not constant."))
824 ;; FIXME: The RESTP handling here is confusing and maybe slightly
825 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
826 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
827 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
828 (setq restp (eval restp))
830 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
831 (cond ((typep ,emf 'fast-method-call)
832 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
833 ;; "What," you may wonder, "do these next two clauses do?"
834 ;; In that case, you are not a PCL implementor, for they
835 ;; considered this to be self-documenting.:-| Or CSR, for
836 ;; that matter, since he can also figure it out by looking
837 ;; at it without breaking stride. For the rest of us,
838 ;; though: From what the code is doing with .SLOTS. and
839 ;; whatnot, evidently it's implementing SLOT-VALUEish and
840 ;; GET-SLOT-VALUEish things. Then we can reason backwards
841 ;; and conclude that setting EMF to a FIXNUM is an
842 ;; optimized way to represent these slot access operations.
843 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
844 `(((typep ,emf 'fixnum)
845 (let* ((.slots. (get-slots-or-nil
846 ,(car required-args+rest-arg)))
847 (value (when .slots. (clos-slots-ref .slots. ,emf))))
848 (if (eq value +slot-unbound+)
849 (slot-unbound-internal ,(car required-args+rest-arg)
852 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
853 `(((typep ,emf 'fixnum)
854 (let ((.new-value. ,(car required-args+rest-arg))
855 (.slots. (get-slots-or-nil
856 ,(car required-args+rest-arg))))
858 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
859 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
860 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
861 ;; there was no explanation and presumably the code is 10+
862 ;; years stale, I simply deleted it. -- WHN)
866 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
869 `(apply (the function ,emf) ,@required-args+rest-arg)
870 `(funcall (the function ,emf)
871 ,@required-args+rest-arg))))))))
873 (defun invoke-emf (emf args)
874 (trace-emf-call emf t args)
877 (let* ((arg-info (fast-method-call-arg-info emf))
878 (restp (cdr arg-info))
879 (nreq (car arg-info)))
881 (let* ((rest-args (nthcdr nreq args))
882 (req-args (ldiff args rest-args)))
883 (apply (fast-method-call-function emf)
884 (fast-method-call-pv-cell emf)
885 (fast-method-call-next-method-call emf)
886 (nconc req-args (list rest-args))))
889 (invoke-fast-method-call emf)
890 (error "wrong number of args")))
893 (invoke-fast-method-call emf (car args))
894 (error "wrong number of args")))
897 (invoke-fast-method-call emf (car args) (cadr args))
898 (error "wrong number of args")))
900 (apply (fast-method-call-function emf)
901 (fast-method-call-pv-cell emf)
902 (fast-method-call-next-method-call emf)
905 (apply (method-call-function emf)
907 (method-call-call-method-args emf)))
909 (cond ((null args) (error "1 or 2 args were expected."))
911 (let* ((slots (get-slots (car args)))
912 (value (clos-slots-ref slots emf)))
913 (if (eq value +slot-unbound+)
914 (slot-unbound-internal (car args) emf)
917 (setf (clos-slots-ref (get-slots (cadr args)) emf)
919 (t (error "1 or 2 args were expected."))))
920 (fast-instance-boundp
921 (if (or (null args) (cdr args))
922 (error "1 arg was expected.")
923 (let ((slots (get-slots (car args))))
924 (not (eq (clos-slots-ref slots
925 (fast-instance-boundp-index emf))
930 ;; KLUDGE: A comment from the original PCL said "This can be improved alot."
931 (defun gf-make-function-from-emf (gf emf)
933 (fast-method-call (let* ((arg-info (gf-arg-info gf))
934 (nreq (arg-info-number-required arg-info))
935 (restp (arg-info-applyp arg-info)))
937 (trace-emf-call emf t args)
938 (apply (fast-method-call-function emf)
939 (fast-method-call-pv-cell emf)
940 (fast-method-call-next-method-call emf)
942 (let* ((rest-args (nthcdr nreq args))
943 (req-args (ldiff args
945 (nconc req-args rest-args))
947 (method-call (lambda (&rest args)
948 (trace-emf-call emf t args)
949 (apply (method-call-function emf)
951 (method-call-call-method-args emf))))
954 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
956 `(macrolet ((narrowed-emf (emf)
957 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
958 ;; dispatch on the possibility that EMF might be of
959 ;; type FIXNUM (as an optimized representation of a
960 ;; slot accessor). But as far as I (WHN 2002-06-11)
961 ;; can tell, it's impossible for such a representation
962 ;; to end up as .NEXT-METHOD-CALL. By reassuring
963 ;; INVOKE-E-M-F that when called from this context
964 ;; it needn't worry about the FIXNUM case, we can
965 ;; keep those cases from being compiled, which is
966 ;; good both because it saves bytes and because it
967 ;; avoids annoying type mismatch compiler warnings.
969 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
970 ;; system isn't smart enough about NOT and intersection
971 ;; types to benefit from a (NOT FIXNUM) declaration
972 ;; here. -- WHN 2002-06-12
974 ;; FIXME: Might the FUNCTION type be omittable here,
975 ;; leaving only METHOD-CALLs? Failing that, could this
976 ;; be documented somehow? (It'd be nice if the types
977 ;; involved could be understood without solving the
979 `(the (or function method-call fast-method-call)
981 (call-next-method-bind (&body body)
983 (call-next-method-body (cnm-args)
984 `(if ,',next-method-call
986 ;; This declaration suppresses a "deleting
987 ;; unreachable code" note for the following IF when
988 ;; REST-ARG is NIL. It is not nice for debugging
989 ;; SBCL itself, but at least it keeps us from
991 (declare (optimize (inhibit-warnings 3)))
992 (if (and (null ',rest-arg)
994 (eq (car cnm-args) 'list))
995 `(invoke-effective-method-function
996 (narrowed-emf ,',next-method-call)
999 (let ((call `(invoke-effective-method-function
1000 (narrowed-emf ,',next-method-call)
1001 ,',(not (null rest-arg))
1003 ,@',(when rest-arg `(,rest-arg)))))
1005 (bind-args ((,@',args
1007 `(&rest ,rest-arg)))
1011 (error "no next method")))
1012 (next-method-p-body ()
1013 `(not (null ,',next-method-call))))
1016 (defmacro bind-lexical-method-functions
1017 ((&key call-next-method-p next-method-p-p closurep applyp)
1019 (cond ((and (null call-next-method-p) (null next-method-p-p)
1023 ((and (null closurep)
1025 ;; OK to use MACROLET, and all args are mandatory
1026 ;; (else APPLYP would be true).
1027 `(call-next-method-bind
1028 (macrolet ((call-next-method (&rest cnm-args)
1029 `(call-next-method-body ,(when cnm-args
1030 `(list ,@cnm-args))))
1032 `(next-method-p-body)))
1035 `(call-next-method-bind
1036 (flet (,@(and call-next-method-p
1037 '((call-next-method (&rest cnm-args)
1038 (call-next-method-body cnm-args))))
1039 ,@(and next-method-p-p
1041 (next-method-p-body)))))
1044 (defmacro bind-args ((lambda-list args) &body body)
1045 (let ((args-tail '.args-tail.)
1048 (flet ((process-var (var)
1049 (if (memq var lambda-list-keywords)
1052 (&optional (setq state 'optional))
1053 (&key (setq state 'key))
1055 (&rest (setq state 'rest))
1056 (&aux (setq state 'aux))
1059 "encountered the non-standard lambda list keyword ~S"
1063 (required `((,var (pop ,args-tail))))
1064 (optional (cond ((not (consp var))
1065 `((,var (when ,args-tail
1066 (pop ,args-tail)))))
1068 `((,(car var) (if ,args-tail
1072 `((,(caddr var) ,args-tail)
1073 (,(car var) (if ,args-tail
1076 (rest `((,var ,args-tail)))
1077 (key (cond ((not (consp var))
1078 `((,var (get-key-arg ,(keywordicate var)
1081 (multiple-value-bind (keyword variable)
1082 (if (consp (car var))
1085 (values (keywordicate (car var))
1087 `((,key (get-key-arg1 ',keyword ,args-tail))
1088 (,variable (if (consp ,key)
1092 (multiple-value-bind (keyword variable)
1093 (if (consp (car var))
1096 (values (keywordicate (car var))
1098 `((,key (get-key-arg1 ',keyword ,args-tail))
1100 (,variable (if (consp ,key)
1104 (let ((bindings (mapcan #'process-var lambda-list)))
1105 `(let* ((,args-tail ,args)
1107 (declare (ignorable ,args-tail))
1110 (defun get-key-arg (keyword list)
1111 (loop (when (atom list) (return nil))
1112 (when (eq (car list) keyword) (return (cadr list)))
1113 (setq list (cddr list))))
1115 (defun get-key-arg1 (keyword list)
1116 (loop (when (atom list) (return nil))
1117 (when (eq (car list) keyword) (return (cdr list)))
1118 (setq list (cddr list))))
1120 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1121 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1122 ; should be in the method definition
1123 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1124 ; was seen in the body of a method
1125 (next-method-p-p nil)) ; flag indicating that NEXT-METHOD-P
1126 ; should be in the method definition
1127 (flet ((walk-function (form context env)
1128 (cond ((not (eq context :eval)) form)
1129 ;; FIXME: Jumping to a conclusion from the way it's used
1130 ;; above, perhaps CONTEXT should be called SITUATION
1131 ;; (after the term used in the ANSI specification of
1132 ;; EVAL-WHEN) and given modern ANSI keyword values
1133 ;; like :LOAD-TOPLEVEL.
1134 ((not (listp form)) form)
1135 ((eq (car form) 'call-next-method)
1136 (setq call-next-method-p t)
1138 ((eq (car form) 'next-method-p)
1139 (setq next-method-p-p t)
1141 ((and (eq (car form) 'function)
1142 (cond ((eq (cadr form) 'call-next-method)
1143 (setq call-next-method-p t)
1146 ((eq (cadr form) 'next-method-p)
1147 (setq next-method-p-p t)
1151 ((and (memq (car form)
1152 '(slot-value set-slot-value slot-boundp))
1153 (constantp (caddr form)))
1154 (let ((parameter (can-optimize-access form
1157 (let ((fun (ecase (car form)
1158 (slot-value #'optimize-slot-value)
1159 (set-slot-value #'optimize-set-slot-value)
1160 (slot-boundp #'optimize-slot-boundp))))
1161 (funcall fun slots parameter form))))
1162 ((and (eq (car form) 'apply)
1164 (eq (car (cadr form)) 'function)
1165 (generic-function-name-p (cadr (cadr form))))
1166 (optimize-generic-function-call
1167 form required-parameters env slots calls))
1168 ((generic-function-name-p (car form))
1169 (optimize-generic-function-call
1170 form required-parameters env slots calls))
1171 ((and (eq (car form) 'asv-funcall)
1172 *optimize-asv-funcall-p*)
1174 (reader (push (third form) *asv-readers*))
1175 (writer (push (third form) *asv-writers*))
1176 (boundp (push (third form) *asv-boundps*)))
1177 `(,(second form) ,@(cddddr form)))
1180 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1181 (values walked-lambda
1184 next-method-p-p)))))
1186 (defun generic-function-name-p (name)
1187 (and (legal-fun-name-p name)
1189 (if (eq *boot-state* 'complete)
1190 (standard-generic-function-p (gdefinition name))
1191 (funcallable-instance-p (gdefinition name)))))
1193 (defvar *method-function-plist* (make-hash-table :test 'eq))
1196 (defvar *mf1cp* nil)
1199 (defvar *mf2cp* nil)
1201 (defun method-function-plist (method-function)
1202 (unless (eq method-function *mf1*)
1203 (rotatef *mf1* *mf2*)
1204 (rotatef *mf1p* *mf2p*)
1205 (rotatef *mf1cp* *mf2cp*))
1206 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1207 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1208 (unless (eq method-function *mf1*)
1209 (setf *mf1* method-function
1211 *mf1p* (gethash method-function *method-function-plist*)))
1214 (defun (setf method-function-plist)
1215 (val method-function)
1216 (unless (eq method-function *mf1*)
1217 (rotatef *mf1* *mf2*)
1218 (rotatef *mf1cp* *mf2cp*)
1219 (rotatef *mf1p* *mf2p*))
1220 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1221 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1222 (setf *mf1* method-function
1226 (defun method-function-get (method-function key &optional default)
1227 (getf (method-function-plist method-function) key default))
1229 (defun (setf method-function-get)
1230 (val method-function key)
1231 (setf (getf (method-function-plist method-function) key) val))
1233 (defun method-function-pv-table (method-function)
1234 (method-function-get method-function :pv-table))
1236 (defun method-function-method (method-function)
1237 (method-function-get method-function :method))
1239 (defun method-function-needs-next-methods-p (method-function)
1240 (method-function-get method-function :needs-next-methods-p t))
1242 (defmacro method-function-closure-generator (method-function)
1243 `(method-function-get ,method-function 'closure-generator))
1245 (defun load-defmethod
1246 (class name quals specls ll initargs &optional pv-table-symbol)
1247 (setq initargs (copy-tree initargs))
1248 (let ((method-spec (or (getf initargs :method-spec)
1249 (make-method-spec name quals specls))))
1250 (setf (getf initargs :method-spec) method-spec)
1251 (load-defmethod-internal class name quals specls
1252 ll initargs pv-table-symbol)))
1254 (defun load-defmethod-internal
1255 (method-class gf-spec qualifiers specializers lambda-list
1256 initargs pv-table-symbol)
1257 (when pv-table-symbol
1258 (setf (getf (getf initargs :plist) :pv-table-symbol)
1260 (when (and (eq *boot-state* 'complete)
1262 (let* ((gf (fdefinition gf-spec))
1263 (method (and (generic-function-p gf)
1266 (parse-specializers specializers)
1269 (sb-kernel::style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1270 gf-spec qualifiers specializers))))
1271 (let ((method (apply #'add-named-method
1272 gf-spec qualifiers specializers lambda-list
1273 :definition-source `((defmethod ,gf-spec
1278 (unless (or (eq method-class 'standard-method)
1279 (eq (find-class method-class nil) (class-of method)))
1280 ;; FIXME: should be STYLE-WARNING?
1281 (format *error-output*
1282 "~&At the time the method with qualifiers ~:S and~%~
1283 specializers ~:S on the generic function ~S~%~
1284 was compiled, the method-class for that generic function was~%~
1285 ~S. But, the method class is now ~S, this~%~
1286 may mean that this method was compiled improperly.~%"
1287 qualifiers specializers gf-spec
1288 method-class (class-name (class-of method))))
1291 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1292 `(method ,gf-spec ,@qualifiers ,unparsed-specializers))
1294 (defun initialize-method-function (initargs &optional return-function-p method)
1295 (let* ((mf (getf initargs :function))
1296 (method-spec (getf initargs :method-spec))
1297 (plist (getf initargs :plist))
1298 (pv-table-symbol (getf plist :pv-table-symbol))
1300 (mff (getf initargs :fast-function)))
1301 (flet ((set-mf-property (p v)
1303 (setf (method-function-get mf p) v))
1305 (setf (method-function-get mff p) v))))
1308 (setq mf (set-fun-name mf method-spec)))
1310 (let ((name `(,(or (get (car method-spec) 'fast-sym)
1311 (setf (get (car method-spec) 'fast-sym)
1312 ;; KLUDGE: If we're going to be
1313 ;; interning private symbols in our
1314 ;; a this way, it would be cleanest
1315 ;; to use a separate package
1316 ;; %PCL-PRIVATE or something, and
1317 ;; failing that, to use a special
1318 ;; symbol prefix denoting privateness.
1320 (intern (format nil "FAST-~A"
1323 ,@(cdr method-spec))))
1324 (set-fun-name mff name)
1326 (set-mf-property :name name)))))
1328 (let ((snl (getf plist :slot-name-lists))
1329 (cl (getf plist :call-list)))
1331 (setq pv-table (intern-pv-table :slot-name-lists snl
1333 (when pv-table (set pv-table-symbol pv-table))
1334 (set-mf-property :pv-table pv-table)))
1335 (loop (when (null plist) (return nil))
1336 (set-mf-property (pop plist) (pop plist)))
1338 (set-mf-property :method method))
1339 (when return-function-p
1340 (or mf (method-function-from-fast-function mff)))))))
1342 (defun analyze-lambda-list (lambda-list)
1343 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1344 (parse-key-arg (arg)
1346 (if (listp (car arg))
1348 (keywordicate (car arg)))
1349 (keywordicate arg))))
1355 (allow-other-keys-p nil)
1357 (keyword-parameters ())
1359 (dolist (x lambda-list)
1360 (if (memq x lambda-list-keywords)
1362 (&optional (setq state 'optional))
1365 (&allow-other-keys (setq allow-other-keys-p t))
1366 (&rest (setq restp t
1370 (error "encountered the non-standard lambda list keyword ~S"
1373 (required (incf nrequired))
1374 (optional (incf noptional))
1375 (key (push (parse-key-arg x) keywords)
1376 (push x keyword-parameters))
1377 (rest (incf nrest)))))
1378 (when (and restp (zerop nrest))
1379 (error "Error in lambda-list:~%~
1380 After &REST, a DEFGENERIC lambda-list ~
1381 must be followed by at least one variable."))
1382 (values nrequired noptional keysp restp allow-other-keys-p
1384 (reverse keyword-parameters)))))
1386 (defun keyword-spec-name (x)
1387 (let ((key (if (atom x) x (car x))))
1392 (defun ftype-declaration-from-lambda-list (lambda-list name)
1393 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1394 keywords keyword-parameters)
1395 (analyze-lambda-list lambda-list)
1396 (declare (ignore keyword-parameters))
1397 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1398 (old-ftype (if (sb-kernel:fun-type-p old) old nil))
1399 (old-restp (and old-ftype (sb-kernel:fun-type-rest old-ftype)))
1400 (old-keys (and old-ftype
1401 (mapcar #'sb-kernel:key-info-name
1402 (sb-kernel:fun-type-keywords
1404 (old-keysp (and old-ftype (sb-kernel:fun-type-keyp old-ftype)))
1405 (old-allowp (and old-ftype
1406 (sb-kernel:fun-type-allowp old-ftype)))
1407 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1408 `(function ,(append (make-list nrequired :initial-element t)
1409 (when (plusp noptional)
1410 (append '(&optional)
1411 (make-list noptional :initial-element t)))
1412 (when (or restp old-restp)
1414 (when (or keysp old-keysp)
1416 (mapcar (lambda (key)
1419 (when (or allow-other-keys-p old-allowp)
1420 '(&allow-other-keys)))))
1423 (defun defgeneric-declaration (spec lambda-list)
1425 (setq spec (get-setf-fun-name (cadr spec))))
1426 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1428 ;;;; early generic function support
1430 (defvar *!early-generic-functions* ())
1432 (defun ensure-generic-function (fun-name
1436 (declare (ignore environment))
1437 (let ((existing (and (gboundp fun-name)
1438 (gdefinition fun-name))))
1440 (eq *boot-state* 'complete)
1441 (null (generic-function-p existing)))
1442 (generic-clobbers-function fun-name)
1443 (apply #'ensure-generic-function-using-class
1444 existing fun-name all-keys))))
1446 (defun generic-clobbers-function (fun-name)
1447 (error 'simple-program-error
1448 :format-control "~S already names an ordinary function or a macro."
1449 :format-arguments (list fun-name)))
1451 (defvar *sgf-wrapper*
1452 (boot-make-wrapper (early-class-size 'standard-generic-function)
1453 'standard-generic-function))
1455 (defvar *sgf-slots-init*
1456 (mapcar (lambda (canonical-slot)
1457 (if (memq (getf canonical-slot :name) '(arg-info source))
1459 (let ((initfunction (getf canonical-slot :initfunction)))
1461 (funcall initfunction)
1463 (early-collect-inheritance 'standard-generic-function)))
1465 (defvar *sgf-method-class-index*
1466 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1468 (defun early-gf-p (x)
1469 (and (fsc-instance-p x)
1470 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1473 (defvar *sgf-methods-index*
1474 (!bootstrap-slot-index 'standard-generic-function 'methods))
1476 (defmacro early-gf-methods (gf)
1477 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1479 (defvar *sgf-arg-info-index*
1480 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1482 (defmacro early-gf-arg-info (gf)
1483 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1485 (defvar *sgf-dfun-state-index*
1486 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1488 (defstruct (arg-info
1490 (:constructor make-arg-info ())
1492 (arg-info-lambda-list :no-lambda-list)
1495 arg-info-number-optional
1497 arg-info-keys ;nil no &KEY or &REST allowed
1498 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1499 ;T must have &KEY or &REST
1501 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1502 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1504 gf-info-static-c-a-m-emf
1505 (gf-info-c-a-m-emf-std-p t)
1508 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1510 (defun arg-info-valid-p (arg-info)
1511 (not (null (arg-info-number-optional arg-info))))
1513 (defun arg-info-applyp (arg-info)
1514 (or (plusp (arg-info-number-optional arg-info))
1515 (arg-info-key/rest-p arg-info)))
1517 (defun arg-info-number-required (arg-info)
1518 (length (arg-info-metatypes arg-info)))
1520 (defun arg-info-nkeys (arg-info)
1521 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1523 ;;; Keep pages clean by not setting if the value is already the same.
1524 (defmacro esetf (pos val)
1525 (let ((valsym (gensym "value")))
1526 `(let ((,valsym ,val))
1527 (unless (equal ,pos ,valsym)
1528 (setf ,pos ,valsym)))))
1530 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1531 argument-precedence-order)
1532 (let* ((arg-info (if (eq *boot-state* 'complete)
1534 (early-gf-arg-info gf)))
1535 (methods (if (eq *boot-state* 'complete)
1536 (generic-function-methods gf)
1537 (early-gf-methods gf)))
1538 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1539 (first-p (and new-method (null (cdr methods)))))
1540 (when (and (not lambda-list-p) methods)
1541 (setq lambda-list (gf-lambda-list gf)))
1542 (when (or lambda-list-p
1544 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1545 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1546 (analyze-lambda-list lambda-list)
1547 (when (and methods (not first-p))
1548 (let ((gf-nreq (arg-info-number-required arg-info))
1549 (gf-nopt (arg-info-number-optional arg-info))
1550 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1551 (unless (and (= nreq gf-nreq)
1553 (eq (or keysp restp) gf-key/rest-p))
1554 (error "The lambda-list ~S is incompatible with ~
1555 existing methods of ~S."
1558 (esetf (arg-info-lambda-list arg-info) lambda-list))
1559 (when (or lambda-list-p argument-precedence-order
1560 (null (arg-info-precedence arg-info)))
1561 (esetf (arg-info-precedence arg-info)
1562 (compute-precedence lambda-list nreq
1563 argument-precedence-order)))
1564 (esetf (arg-info-metatypes arg-info) (make-list nreq))
1565 (esetf (arg-info-number-optional arg-info) nopt)
1566 (esetf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1567 (esetf (arg-info-keys arg-info)
1569 (if allow-other-keys-p t keywords)
1570 (arg-info-key/rest-p arg-info)))))
1572 (check-method-arg-info gf arg-info new-method))
1573 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1576 (defun check-method-arg-info (gf arg-info method)
1577 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1578 (analyze-lambda-list (if (consp method)
1579 (early-method-lambda-list method)
1580 (method-lambda-list method)))
1581 (flet ((lose (string &rest args)
1583 "attempt to add the method ~S to the generic function ~S.~%~
1587 (apply #'format nil string args)))
1588 (comparison-description (x y)
1589 (if (> x y) "more" "fewer")))
1590 (let ((gf-nreq (arg-info-number-required arg-info))
1591 (gf-nopt (arg-info-number-optional arg-info))
1592 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1593 (gf-keywords (arg-info-keys arg-info)))
1594 (unless (= nreq gf-nreq)
1596 "the method has ~A required arguments than the generic function."
1597 (comparison-description nreq gf-nreq)))
1598 (unless (= nopt gf-nopt)
1600 "the method has ~A optional arguments than the generic function."
1601 (comparison-description nopt gf-nopt)))
1602 (unless (eq (or keysp restp) gf-key/rest-p)
1604 "The method and generic function differ in whether they accept~%~
1605 &REST or &KEY arguments."))
1606 (when (consp gf-keywords)
1607 (unless (or (and restp (not keysp))
1609 (every (lambda (k) (memq k keywords)) gf-keywords))
1610 (lose "the method does not accept each of the &KEY arguments~%~
1614 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1615 (let* ((existing-p (and methods (cdr methods) new-method))
1616 (nreq (length (arg-info-metatypes arg-info)))
1617 (metatypes (if existing-p
1618 (arg-info-metatypes arg-info)
1620 (type (if existing-p
1621 (gf-info-simple-accessor-type arg-info)
1623 (when (arg-info-valid-p arg-info)
1624 (dolist (method (if new-method (list new-method) methods))
1625 (let* ((specializers (if (or (eq *boot-state* 'complete)
1626 (not (consp method)))
1627 (method-specializers method)
1628 (early-method-specializers method t)))
1629 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1631 (early-method-class method)))
1632 (new-type (when (and class
1633 (or (not (eq *boot-state* 'complete))
1634 (eq (generic-function-method-combination gf)
1635 *standard-method-combination*)))
1636 (cond ((eq class *the-class-standard-reader-method*)
1638 ((eq class *the-class-standard-writer-method*)
1640 ((eq class *the-class-standard-boundp-method*)
1642 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1643 (setq type (cond ((null type) new-type)
1644 ((eq type new-type) type)
1646 (esetf (arg-info-metatypes arg-info) metatypes)
1647 (esetf (gf-info-simple-accessor-type arg-info) type)))
1648 (when (or (not was-valid-p) first-p)
1649 (multiple-value-bind (c-a-m-emf std-p)
1652 (compute-applicable-methods-emf gf))
1653 (esetf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1654 (esetf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1655 (unless (gf-info-c-a-m-emf-std-p arg-info)
1656 (esetf (gf-info-simple-accessor-type arg-info) t))))
1658 (let ((name (if (eq *boot-state* 'complete)
1659 (generic-function-name gf)
1660 (!early-gf-name gf))))
1661 (esetf (gf-precompute-dfun-and-emf-p arg-info)
1662 (let* ((sym (if (atom name) name (cadr name)))
1663 (pkg-list (cons *pcl-package*
1664 (package-use-list *pcl-package*))))
1665 (and sym (symbolp sym)
1666 (not (null (memq (symbol-package sym) pkg-list)))
1667 (not (find #\space (symbol-name sym))))))))
1668 (esetf (gf-info-fast-mf-p arg-info)
1669 (or (not (eq *boot-state* 'complete))
1670 (let* ((method-class (generic-function-method-class gf))
1671 (methods (compute-applicable-methods
1672 #'make-method-lambda
1673 (list gf (class-prototype method-class)
1675 (and methods (null (cdr methods))
1676 (let ((specls (method-specializers (car methods))))
1677 (and (classp (car specls))
1678 (eq 'standard-generic-function
1679 (class-name (car specls)))
1680 (classp (cadr specls))
1681 (eq 'standard-method
1682 (class-name (cadr specls)))))))))
1685 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1687 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1688 ;;; generic functions is used to store the early methods and early
1689 ;;; discriminator code for the early generic function. The static
1690 ;;; slots field of the fins contains a list whose:
1691 ;;; CAR - a list of the early methods on this early gf
1692 ;;; CADR - the early discriminator code for this method
1693 (defun ensure-generic-function-using-class (existing spec &rest keys
1694 &key (lambda-list nil
1697 (declare (ignore keys))
1698 (cond ((and existing (early-gf-p existing))
1700 ((assoc spec *!generic-function-fixups* :test #'equal)
1702 (make-early-gf spec lambda-list lambda-list-p existing)
1703 (error "The function ~S is not already defined." spec)))
1705 (error "~S should be on the list ~S."
1707 '*!generic-function-fixups*))
1709 (pushnew spec *!early-generic-functions* :test #'equal)
1710 (make-early-gf spec lambda-list lambda-list-p))))
1712 (defun make-early-gf (spec &optional lambda-list lambda-list-p function)
1713 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1714 (set-funcallable-instance-fun
1717 (if (eq spec 'print-object)
1718 #'(sb-kernel:instance-lambda (instance stream)
1719 (print-unreadable-object (instance stream :identity t)
1720 (format stream "std-instance")))
1721 #'(sb-kernel:instance-lambda (&rest args)
1722 (declare (ignore args))
1723 (error "The function of the funcallable-instance ~S~
1724 has not been set." fin)))))
1725 (setf (gdefinition spec) fin)
1726 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1727 (!bootstrap-set-slot 'standard-generic-function
1731 (set-fun-name fin spec)
1732 (let ((arg-info (make-arg-info)))
1733 (setf (early-gf-arg-info fin) arg-info)
1735 (proclaim (defgeneric-declaration spec lambda-list))
1736 (set-arg-info fin :lambda-list lambda-list)))
1739 (defun set-dfun (gf &optional dfun cache info)
1741 (setf (cache-owner cache) gf))
1742 (let ((new-state (if (and dfun (or cache info))
1743 (list* dfun cache info)
1745 (if (eq *boot-state* 'complete)
1746 (setf (gf-dfun-state gf) new-state)
1747 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1751 (defun gf-dfun-cache (gf)
1752 (let ((state (if (eq *boot-state* 'complete)
1754 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1757 (cons (cadr state)))))
1759 (defun gf-dfun-info (gf)
1760 (let ((state (if (eq *boot-state* 'complete)
1762 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1765 (cons (cddr state)))))
1767 (defvar *sgf-name-index*
1768 (!bootstrap-slot-index 'standard-generic-function 'name))
1770 (defun !early-gf-name (gf)
1771 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1773 (defun gf-lambda-list (gf)
1774 (let ((arg-info (if (eq *boot-state* 'complete)
1776 (early-gf-arg-info gf))))
1777 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1778 (let ((methods (if (eq *boot-state* 'complete)
1779 (generic-function-methods gf)
1780 (early-gf-methods gf))))
1783 (warn "no way to determine the lambda list for ~S" gf)
1785 (let* ((method (car (last methods)))
1786 (ll (if (consp method)
1787 (early-method-lambda-list method)
1788 (method-lambda-list method)))
1789 (k (member '&key ll)))
1791 (append (ldiff ll (cdr k)) '(&allow-other-keys))
1793 (arg-info-lambda-list arg-info))))
1795 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1797 (cond ((symbolp ,gf-class)
1798 (setq ,gf-class (find-class ,gf-class t ,env)))
1799 ((classp ,gf-class))
1801 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1802 class nor a symbol that names a class."
1804 (remf ,all-keys :generic-function-class)
1805 (remf ,all-keys :environment)
1806 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1807 (unless (eq combin '.shes-not-there.)
1808 (setf (getf ,all-keys :method-combination)
1809 (find-method-combination (class-prototype ,gf-class)
1812 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1813 (unless (eq method-class '.shes-not-there.)
1814 (setf (getf ,all-keys :method-class)
1815 (find-class method-class t ,env))))))
1817 (defun real-ensure-gf-using-class--generic-function
1821 &key environment (lambda-list nil lambda-list-p)
1822 (generic-function-class 'standard-generic-function gf-class-p)
1824 (real-ensure-gf-internal generic-function-class all-keys environment)
1825 (unless (or (null gf-class-p)
1826 (eq (class-of existing) generic-function-class))
1827 (change-class existing generic-function-class))
1829 (apply #'reinitialize-instance existing all-keys)
1831 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1833 (defun real-ensure-gf-using-class--null
1837 &key environment (lambda-list nil lambda-list-p)
1838 (generic-function-class 'standard-generic-function)
1840 (declare (ignore existing))
1841 (real-ensure-gf-internal generic-function-class all-keys environment)
1843 (setf (gdefinition fun-name)
1844 (apply #'make-instance generic-function-class
1845 :name fun-name all-keys))
1847 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1849 (defun get-generic-fun-info (gf)
1850 ;; values nreq applyp metatypes nkeys arg-info
1851 (multiple-value-bind (applyp metatypes arg-info)
1852 (let* ((arg-info (if (early-gf-p gf)
1853 (early-gf-arg-info gf)
1855 (metatypes (arg-info-metatypes arg-info)))
1856 (values (arg-info-applyp arg-info)
1859 (values (length metatypes) applyp metatypes
1860 (count-if (lambda (x) (neq x t)) metatypes)
1863 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
1864 &optional slot-name)
1865 (initialize-method-function initargs)
1868 ;; Figure out whether we got class objects or class names as the
1869 ;; specializers and set parsed and unparsed appropriately. If we
1870 ;; got class objects, then we can compute unparsed, but if we got
1871 ;; class names we don't try to compute parsed.
1873 ;; Note that the use of not symbolp in this call to every should be
1874 ;; read as 'classp' we can't use classp itself because it doesn't
1876 (if (every (lambda (s) (not (symbolp s))) specializers)
1877 (setq parsed specializers
1878 unparsed (mapcar (lambda (s)
1879 (if (eq s t) t (class-name s)))
1881 (setq unparsed specializers
1883 (list :early-method ;This is an early method dammit!
1885 (getf initargs :function)
1886 (getf initargs :fast-function)
1888 parsed ;The parsed specializers. This is used
1889 ;by early-method-specializers to cache
1890 ;the parse. Note that this only comes
1891 ;into play when there is more than one
1892 ;early method on an early gf.
1894 (list class ;A list to which real-make-a-method
1895 qualifiers ;can be applied to make a real method
1896 arglist ;corresponding to this early one.
1902 (defun real-make-a-method
1903 (class qualifiers lambda-list specializers initargs doc
1904 &optional slot-name)
1905 (setq specializers (parse-specializers specializers))
1906 (apply #'make-instance class
1907 :qualifiers qualifiers
1908 :lambda-list lambda-list
1909 :specializers specializers
1911 :slot-name slot-name
1915 (defun early-method-function (early-method)
1916 (values (cadr early-method) (caddr early-method)))
1918 (defun early-method-class (early-method)
1919 (find-class (car (fifth early-method))))
1921 (defun early-method-standard-accessor-p (early-method)
1922 (let ((class (first (fifth early-method))))
1923 (or (eq class 'standard-reader-method)
1924 (eq class 'standard-writer-method)
1925 (eq class 'standard-boundp-method))))
1927 (defun early-method-standard-accessor-slot-name (early-method)
1928 (seventh (fifth early-method)))
1930 ;;; Fetch the specializers of an early method. This is basically just
1931 ;;; a simple accessor except that when the second argument is t, this
1932 ;;; converts the specializers from symbols into class objects. The
1933 ;;; class objects are cached in the early method, this makes
1934 ;;; bootstrapping faster because the class objects only have to be
1938 ;;; The second argument should only be passed as T by
1939 ;;; early-lookup-method. This is to implement the rule that only when
1940 ;;; there is more than one early method on a generic function is the
1941 ;;; conversion from class names to class objects done. This
1942 ;;; corresponds to the fact that we are only allowed to have one
1943 ;;; method on any generic function up until the time classes exist.
1944 (defun early-method-specializers (early-method &optional objectsp)
1945 (if (and (listp early-method)
1946 (eq (car early-method) :early-method))
1947 (cond ((eq objectsp t)
1948 (or (fourth early-method)
1949 (setf (fourth early-method)
1950 (mapcar #'find-class (cadddr (fifth early-method))))))
1952 (cadddr (fifth early-method))))
1953 (error "~S is not an early-method." early-method)))
1955 (defun early-method-qualifiers (early-method)
1956 (cadr (fifth early-method)))
1958 (defun early-method-lambda-list (early-method)
1959 (caddr (fifth early-method)))
1961 (defun early-add-named-method (generic-function-name
1966 (let* ((gf (ensure-generic-function generic-function-name))
1968 (dolist (m (early-gf-methods gf))
1969 (when (and (equal (early-method-specializers m) specializers)
1970 (equal (early-method-qualifiers m) qualifiers))
1972 (new (make-a-method 'standard-method
1978 (when existing (remove-method gf existing))
1979 (add-method gf new)))
1981 ;;; This is the early version of ADD-METHOD. Later this will become a
1982 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
1983 ;;; special knowledge about ADD-METHOD.
1984 (defun add-method (generic-function method)
1985 (when (not (fsc-instance-p generic-function))
1986 (error "Early ADD-METHOD didn't get a funcallable instance."))
1987 (when (not (and (listp method) (eq (car method) :early-method)))
1988 (error "Early ADD-METHOD didn't get an early method."))
1989 (push method (early-gf-methods generic-function))
1990 (set-arg-info generic-function :new-method method)
1991 (unless (assoc (!early-gf-name generic-function)
1992 *!generic-function-fixups*
1994 (update-dfun generic-function)))
1996 ;;; This is the early version of REMOVE-METHOD. See comments on
1997 ;;; the early version of ADD-METHOD.
1998 (defun remove-method (generic-function method)
1999 (when (not (fsc-instance-p generic-function))
2000 (error "An early remove-method didn't get a funcallable instance."))
2001 (when (not (and (listp method) (eq (car method) :early-method)))
2002 (error "An early remove-method didn't get an early method."))
2003 (setf (early-gf-methods generic-function)
2004 (remove method (early-gf-methods generic-function)))
2005 (set-arg-info generic-function)
2006 (unless (assoc (!early-gf-name generic-function)
2007 *!generic-function-fixups*
2009 (update-dfun generic-function)))
2011 ;;; This is the early version of GET-METHOD. See comments on the early
2012 ;;; version of ADD-METHOD.
2013 (defun get-method (generic-function qualifiers specializers
2014 &optional (errorp t))
2015 (if (early-gf-p generic-function)
2016 (or (dolist (m (early-gf-methods generic-function))
2017 (when (and (or (equal (early-method-specializers m nil)
2019 (equal (early-method-specializers m t)
2021 (equal (early-method-qualifiers m) qualifiers))
2024 (error "can't get early method")
2026 (real-get-method generic-function qualifiers specializers errorp)))
2028 (defun !fix-early-generic-functions ()
2029 (let ((accessors nil))
2030 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2031 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2032 (dolist (early-gf-spec *!early-generic-functions*)
2033 (when (every #'early-method-standard-accessor-p
2034 (early-gf-methods (gdefinition early-gf-spec)))
2035 (push early-gf-spec accessors)))
2036 (dolist (spec (nconc accessors
2037 '(accessor-method-slot-name
2038 generic-function-methods
2043 slot-definition-location
2044 slot-definition-name
2047 class-precedence-list
2048 slot-boundp-using-class
2049 (setf slot-value-using-class)
2050 slot-value-using-class
2053 funcallable-standard-class-p
2056 (setq *!early-generic-functions*
2058 (delete spec *!early-generic-functions* :test #'equal))))
2060 (dolist (early-gf-spec *!early-generic-functions*)
2061 (/show early-gf-spec)
2062 (let* ((gf (gdefinition early-gf-spec))
2063 (methods (mapcar (lambda (early-method)
2064 (let ((args (copy-list (fifth
2067 (early-method-specializers
2069 (apply #'real-make-a-method args)))
2070 (early-gf-methods gf))))
2071 (setf (generic-function-method-class gf) *the-class-standard-method*)
2072 (setf (generic-function-method-combination gf)
2073 *standard-method-combination*)
2074 (set-methods gf methods)))
2076 (dolist (fn *!early-functions*)
2078 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2080 (dolist (fixup *!generic-function-fixups*)
2082 (let* ((fspec (car fixup))
2083 (gf (gdefinition fspec))
2084 (methods (mapcar (lambda (method)
2085 (let* ((lambda-list (first method))
2086 (specializers (second method))
2087 (method-fn-name (third method))
2088 (fn-name (or method-fn-name fspec))
2089 (fn (fdefinition fn-name))
2093 (lambda (args next-methods)
2097 `(call ,fn-name)))))
2098 (declare (type function fn))
2099 (make-a-method 'standard-method
2106 (setf (generic-function-method-class gf) *the-class-standard-method*)
2107 (setf (generic-function-method-combination gf)
2108 *standard-method-combination*)
2109 (set-methods gf methods))))
2110 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2112 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2113 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2114 ;;; is really implemented.
2115 (defun parse-defmethod (cdr-of-form)
2116 (declare (list cdr-of-form))
2117 (let ((name (pop cdr-of-form))
2120 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2121 (push (pop cdr-of-form) qualifiers)
2122 (return (setq qualifiers (nreverse qualifiers)))))
2123 (setq spec-ll (pop cdr-of-form))
2124 (values name qualifiers spec-ll cdr-of-form)))
2126 (defun parse-specializers (specializers)
2127 (declare (list specializers))
2128 (flet ((parse (spec)
2129 (let ((result (specializer-from-type spec)))
2130 (if (specializerp result)
2133 (error "~S was used as a specializer,~%~
2134 but is not the name of a class."
2136 (error "~S is not a legal specializer." spec))))))
2137 (mapcar #'parse specializers)))
2139 (defun unparse-specializers (specializers-or-method)
2140 (if (listp specializers-or-method)
2141 (flet ((unparse (spec)
2142 (if (specializerp spec)
2143 (let ((type (specializer-type spec)))
2144 (if (and (consp type)
2145 (eq (car type) 'class))
2146 (let* ((class (cadr type))
2147 (class-name (class-name class)))
2148 (if (eq class (find-class class-name nil))
2152 (error "~S is not a legal specializer." spec))))
2153 (mapcar #'unparse specializers-or-method))
2154 (unparse-specializers (method-specializers specializers-or-method))))
2156 (defun parse-method-or-spec (spec &optional (errorp t))
2157 (let (gf method name temp)
2160 gf (method-generic-function method)
2161 temp (and gf (generic-function-name gf))
2164 (make-method-spec temp
2165 (method-qualifiers method)
2166 (unparse-specializers
2167 (method-specializers method))))
2168 (make-symbol (format nil "~S" method))))
2169 (multiple-value-bind (gf-spec quals specls)
2170 (parse-defmethod spec)
2171 (and (setq gf (and (or errorp (gboundp gf-spec))
2172 (gdefinition gf-spec)))
2173 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2174 (setq specls (append (parse-specializers specls)
2175 (make-list (- nreq (length specls))
2179 (setq method (get-method gf quals specls errorp))
2181 (intern-fun-name (make-method-spec gf-spec
2184 (values gf method name)))
2186 (defun extract-parameters (specialized-lambda-list)
2187 (multiple-value-bind (parameters ignore1 ignore2)
2188 (parse-specialized-lambda-list specialized-lambda-list)
2189 (declare (ignore ignore1 ignore2))
2192 (defun extract-lambda-list (specialized-lambda-list)
2193 (multiple-value-bind (ignore1 lambda-list ignore2)
2194 (parse-specialized-lambda-list specialized-lambda-list)
2195 (declare (ignore ignore1 ignore2))
2198 (defun extract-specializer-names (specialized-lambda-list)
2199 (multiple-value-bind (ignore1 ignore2 specializers)
2200 (parse-specialized-lambda-list specialized-lambda-list)
2201 (declare (ignore ignore1 ignore2))
2204 (defun extract-required-parameters (specialized-lambda-list)
2205 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2206 (parse-specialized-lambda-list specialized-lambda-list)
2207 (declare (ignore ignore1 ignore2 ignore3))
2208 required-parameters))
2210 (defun parse-specialized-lambda-list (arglist &optional post-keyword)
2211 ;;(declare (values parameters lambda-list specializers required-parameters))
2212 (let ((arg (car arglist)))
2213 (cond ((null arglist) (values nil nil nil nil))
2215 (values nil arglist nil))
2216 ((memq arg lambda-list-keywords)
2217 (unless (memq arg '(&optional &rest &key &allow-other-keys &aux))
2218 ;; Now, since we try to conform to ANSI, non-standard
2219 ;; lambda-list-keywords should be treated as errors.
2220 (error 'simple-program-error
2221 :format-control "unrecognized lambda-list keyword ~S ~
2223 :format-arguments (list arg)))
2224 ;; When we are at a lambda-list keyword, the parameters
2225 ;; don't include the lambda-list keyword; the lambda-list
2226 ;; does include the lambda-list keyword; and no
2227 ;; specializers are allowed to follow the lambda-list
2228 ;; keywords (at least for now).
2229 (multiple-value-bind (parameters lambda-list)
2230 (parse-specialized-lambda-list (cdr arglist) t)
2231 (when (eq arg '&rest)
2232 ;; check, if &rest is followed by a var ...
2233 (when (or (null lambda-list)
2234 (memq (car lambda-list) lambda-list-keywords))
2235 (error "Error in lambda-list:~%~
2236 After &REST, a DEFMETHOD lambda-list ~
2237 must be followed by at least one variable.")))
2239 (cons arg lambda-list)
2243 ;; After a lambda-list keyword there can be no specializers.
2244 (multiple-value-bind (parameters lambda-list)
2245 (parse-specialized-lambda-list (cdr arglist) t)
2246 (values (cons (if (listp arg) (car arg) arg) parameters)
2247 (cons arg lambda-list)
2251 (multiple-value-bind (parameters lambda-list specializers required)
2252 (parse-specialized-lambda-list (cdr arglist))
2253 (values (cons (if (listp arg) (car arg) arg) parameters)
2254 (cons (if (listp arg) (car arg) arg) lambda-list)
2255 (cons (if (listp arg) (cadr arg) t) specializers)
2256 (cons (if (listp arg) (car arg) arg) required)))))))
2258 (setq *boot-state* 'early)
2260 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2261 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2262 ;;; walker stuff was only used for implementing stuff like that; maybe
2263 ;;; it's not needed any more? Hunt down what it was used for and see.
2265 (defmacro with-slots (slots instance &body body)
2266 (let ((in (gensym)))
2267 `(let ((,in ,instance))
2268 (declare (ignorable ,in))
2269 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2272 (and (symbolp instance)
2273 `((declare (%variable-rebinding ,in ,instance)))))
2275 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2277 (if (symbolp slot-entry)
2281 (if (symbolp slot-entry)
2283 (cadr slot-entry))))
2285 (slot-value ,in ',slot-name))))
2289 (defmacro with-accessors (slots instance &body body)
2290 (let ((in (gensym)))
2291 `(let ((,in ,instance))
2292 (declare (ignorable ,in))
2293 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2296 (and (symbolp instance)
2297 `((declare (%variable-rebinding ,in ,instance)))))
2299 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2300 (let ((var-name (car slot-entry))
2301 (accessor-name (cadr slot-entry)))
2302 `(,var-name (,accessor-name ,in))))