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 (declaim (notinline make-a-method
83 ensure-generic-function-using-class
87 (defvar *!early-functions*
88 '((make-a-method early-make-a-method
90 (add-named-method early-add-named-method
91 real-add-named-method)
94 ;;; For each of the early functions, arrange to have it point to its
95 ;;; early definition. Do this in a way that makes sure that if we
96 ;;; redefine one of the early definitions the redefinition will take
97 ;;; effect. This makes development easier.
98 (dolist (fns *!early-functions*)
99 (let ((name (car fns))
100 (early-name (cadr fns)))
101 (setf (gdefinition name)
104 (apply (fdefinition early-name) args))
107 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
108 ;;; to convert the few functions in the bootstrap which are supposed
109 ;;; to be generic functions but can't be early on.
110 (defvar *!generic-function-fixups*
112 ((generic-function method) ;lambda-list
113 (standard-generic-function method) ;specializers
114 real-add-method)) ;method-function
116 ((generic-function method)
117 (standard-generic-function method)
120 ((generic-function qualifiers specializers &optional (errorp t))
121 (standard-generic-function t t)
123 (ensure-generic-function-using-class
124 ((generic-function fun-name
125 &key generic-function-class environment
128 real-ensure-gf-using-class--generic-function)
129 ((generic-function fun-name
130 &key generic-function-class environment
133 real-ensure-gf-using-class--null))
135 ((proto-generic-function proto-method lambda-expression environment)
136 (standard-generic-function standard-method t t)
137 real-make-method-lambda))
138 (make-method-initargs-form
139 ((proto-generic-function proto-method
141 lambda-list environment)
142 (standard-generic-function standard-method t t t)
143 real-make-method-initargs-form))
144 (compute-effective-method
145 ((generic-function combin applicable-methods)
146 (generic-function standard-method-combination t)
147 standard-compute-effective-method))))
149 (defmacro defgeneric (fun-name lambda-list &body options)
150 (declare (type list lambda-list))
151 (unless (legal-fun-name-p fun-name)
152 (error 'simple-program-error
153 :format-control "illegal generic function name ~S"
154 :format-arguments (list fun-name)))
155 (check-gf-lambda-list lambda-list)
158 (flet ((duplicate-option (name)
159 (error 'simple-program-error
160 :format-control "The option ~S appears more than once."
161 :format-arguments (list name)))
162 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
163 (let* ((arglist-pos (position-if #'listp qab))
164 (arglist (elt qab arglist-pos))
165 (qualifiers (subseq qab 0 arglist-pos))
166 (body (nthcdr (1+ arglist-pos) qab)))
167 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
168 (generic-function-initial-methods (fdefinition ',fun-name))))))
169 (macrolet ((initarg (key) `(getf initargs ,key)))
170 (dolist (option options)
171 (let ((car-option (car option)))
175 (consp (cadr option))
176 (member (first (cadr option))
177 ;; FIXME: this list is slightly weird.
178 ;; ANSI (on the DEFGENERIC page) in one
179 ;; place allows only OPTIMIZE; in
180 ;; another place gives this list of
181 ;; disallowed declaration specifiers.
182 ;; This seems to be the only place where
183 ;; the FUNCTION declaration is
184 ;; mentioned; TYPE seems to be missing.
185 ;; Very strange. -- CSR, 2002-10-21
186 '(declaration ftype function
187 inline notinline special)))
188 (error 'simple-program-error
189 :format-control "The declaration specifier ~S ~
190 is not allowed inside DEFGENERIC."
191 :format-arguments (list (cadr option))))
192 (push (cadr option) (initarg :declarations)))
194 (when (initarg car-option)
195 (duplicate-option car-option))
196 (unless (symbolp (cadr option))
197 (error 'simple-program-error
198 :format-control "METHOD-COMBINATION name not a ~
200 :format-arguments (list (cadr option))))
201 (setf (initarg car-option)
203 (:argument-precedence-order
204 (let* ((required (parse-lambda-list lambda-list))
205 (supplied (cdr option)))
206 (unless (= (length required) (length supplied))
207 (error 'simple-program-error
208 :format-control "argument count discrepancy in ~
209 :ARGUMENT-PRECEDENCE-ORDER clause."
210 :format-arguments nil))
211 (when (set-difference required supplied)
212 (error 'simple-program-error
213 :format-control "unequal sets for ~
214 :ARGUMENT-PRECEDENCE-ORDER clause: ~
216 :format-arguments (list required supplied)))
217 (setf (initarg car-option)
219 ((:documentation :generic-function-class :method-class)
220 (unless (proper-list-of-length-p option 2)
221 (error "bad list length for ~S" option))
222 (if (initarg car-option)
223 (duplicate-option car-option)
224 (setf (initarg car-option) `',(cadr option))))
226 (push (cdr option) methods))
228 ;; ANSI requires that unsupported things must get a
230 (error 'simple-program-error
231 :format-control "unsupported option ~S"
232 :format-arguments (list option))))))
234 (when (initarg :declarations)
235 (setf (initarg :declarations)
236 `',(initarg :declarations))))
238 (eval-when (:compile-toplevel :load-toplevel :execute)
239 (compile-or-load-defgeneric ',fun-name))
240 (load-defgeneric ',fun-name ',lambda-list ,@initargs)
241 ,@(mapcar #'expand-method-definition methods)
242 (fdefinition ',fun-name)))))
244 (defun compile-or-load-defgeneric (fun-name)
245 (proclaim-as-fun-name fun-name)
246 (note-name-defined fun-name :function)
247 (unless (eq (info :function :where-from fun-name) :declared)
248 (setf (info :function :where-from fun-name) :defined)
249 (setf (info :function :type fun-name)
250 (specifier-type 'function))))
252 (defun load-defgeneric (fun-name lambda-list &rest initargs)
253 (when (fboundp fun-name)
254 (style-warn "redefining ~S in DEFGENERIC" fun-name)
255 (let ((fun (fdefinition fun-name)))
256 (when (generic-function-p fun)
257 (loop for method in (generic-function-initial-methods fun)
258 do (remove-method fun method))
259 (setf (generic-function-initial-methods fun) '()))))
260 (apply #'ensure-generic-function
262 :lambda-list lambda-list
263 :definition-source `((defgeneric ,fun-name) ,*load-pathname*)
266 (define-condition generic-function-lambda-list-error
267 (reference-condition simple-program-error)
269 (:default-initargs :references (list '(:ansi-cl :section (3 4 2)))))
271 (defun check-gf-lambda-list (lambda-list)
272 (flet ((ensure (arg ok)
274 (error 'generic-function-lambda-list-error
276 "~@<invalid ~S ~_in the generic function lambda list ~S~:>"
277 :format-arguments (list arg lambda-list)))))
278 (multiple-value-bind (required optional restp rest keyp keys allowp
279 auxp aux morep more-context more-count)
280 (parse-lambda-list lambda-list)
281 (declare (ignore required)) ; since they're no different in a gf ll
282 (declare (ignore restp rest)) ; since they're no different in a gf ll
283 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
284 (declare (ignore aux)) ; since we require AUXP=NIL
285 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
286 ;; no defaults allowed for &OPTIONAL arguments
288 (ensure i (or (symbolp i)
289 (and (consp i) (symbolp (car i)) (null (cdr i))))))
290 ;; no defaults allowed for &KEY arguments
293 (ensure i (or (symbolp i)
295 (or (symbolp (car i))
303 (error "&AUX is not allowed in a generic function lambda list: ~S"
305 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
306 ;; the ANSI spec, but the CMU CL &MORE extension does not
308 (aver (not morep)))))
310 (defmacro defmethod (&rest args &environment env)
311 (multiple-value-bind (name qualifiers lambda-list body)
312 (parse-defmethod args)
313 (multiple-value-bind (proto-gf proto-method)
314 (prototypes-for-make-method-lambda name)
315 (expand-defmethod name
323 (defun prototypes-for-make-method-lambda (name)
324 (if (not (eq *boot-state* 'complete))
326 (let ((gf? (and (gboundp name)
327 (gdefinition name))))
329 (not (generic-function-p gf?)))
330 (values (class-prototype (find-class 'standard-generic-function))
331 (class-prototype (find-class 'standard-method)))
333 (class-prototype (or (generic-function-method-class gf?)
334 (find-class 'standard-method))))))))
336 ;;; Take a name which is either a generic function name or a list specifying
337 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
338 ;;; the prototype instance of the method-class for that generic function.
340 ;;; If there is no generic function by that name, this returns the
341 ;;; default value, the prototype instance of the class
342 ;;; STANDARD-METHOD. This default value is also returned if the spec
343 ;;; names an ordinary function or even a macro. In effect, this leaves
344 ;;; the signalling of the appropriate error until load time.
346 ;;; Note: During bootstrapping, this function is allowed to return NIL.
347 (defun method-prototype-for-gf (name)
348 (let ((gf? (and (gboundp name)
349 (gdefinition name))))
350 (cond ((neq *boot-state* 'complete) nil)
352 (not (generic-function-p gf?))) ; Someone else MIGHT
353 ; error at load time.
354 (class-prototype (find-class 'standard-method)))
356 (class-prototype (or (generic-function-method-class gf?)
357 (find-class 'standard-method)))))))
359 (defun expand-defmethod (name
366 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
367 (add-method-declarations name qualifiers lambda-list body env)
368 (multiple-value-bind (method-function-lambda initargs)
369 (make-method-lambda proto-gf proto-method method-lambda env)
370 (let ((initargs-form (make-method-initargs-form proto-gf
372 method-function-lambda
376 ;; Note: We could DECLAIM the ftype of the generic function
377 ;; here, since ANSI specifies that we create it if it does
378 ;; not exist. However, I chose not to, because I think it's
379 ;; more useful to support a style of programming where every
380 ;; generic function has an explicit DEFGENERIC and any typos
381 ;; in DEFMETHODs are warned about. Otherwise
383 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
384 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
385 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
386 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
387 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
388 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
390 ;; compiles without raising an error and runs without
391 ;; raising an error (since SIMPLE-VECTOR cases fall through
392 ;; to VECTOR) but still doesn't do what was intended. I hate
393 ;; that kind of bug (code which silently gives the wrong
394 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
395 ,(make-defmethod-form name qualifiers specializers
396 unspecialized-lambda-list
398 (class-name (class-of proto-method))
401 (getf (getf initargs :plist)
402 :pv-table-symbol)))))))
404 (defun interned-symbol-p (x)
405 (and (symbolp x) (symbol-package x)))
407 (defun make-defmethod-form (name qualifiers specializers
408 unspecialized-lambda-list method-class-name
409 initargs-form &optional pv-table-symbol)
412 (if (and (interned-symbol-p (fun-name-block-name name))
413 (every #'interned-symbol-p qualifiers)
416 (and (eq (car s) 'eql)
418 (let ((sv (eval (cadr s))))
419 (or (interned-symbol-p sv)
422 (standard-char-p sv)))))
423 (interned-symbol-p s)))
425 (consp initargs-form)
426 (eq (car initargs-form) 'list*)
427 (memq (cadr initargs-form) '(:function :fast-function))
428 (consp (setq fn (caddr initargs-form)))
429 (eq (car fn) 'function)
430 (consp (setq fn-lambda (cadr fn)))
431 (eq (car fn-lambda) 'lambda))
432 (let* ((specls (mapcar (lambda (specl)
434 `(,(car specl) ,(eval (cadr specl)))
437 (mname `(,(if (eq (cadr initargs-form) :function)
438 'slow-method 'fast-method)
439 ,name ,@qualifiers ,specls)))
441 (defun ,mname ,(cadr fn-lambda)
443 ,(make-defmethod-form-internal
444 name qualifiers `',specls
445 unspecialized-lambda-list method-class-name
446 `(list* ,(cadr initargs-form)
448 ,@(cdddr initargs-form))
450 (make-defmethod-form-internal
452 `(list ,@(mapcar (lambda (specializer)
453 (if (consp specializer)
454 ``(,',(car specializer)
455 ,,(cadr specializer))
458 unspecialized-lambda-list
463 (defun make-defmethod-form-internal
464 (name qualifiers specializers-form unspecialized-lambda-list
465 method-class-name initargs-form &optional pv-table-symbol)
471 ',unspecialized-lambda-list
473 ;; Paper over a bug in KCL by passing the cache-symbol here in
474 ;; addition to in the list. FIXME: We should no longer need to do
475 ;; this, since the CLOS code is now SBCL-specific, and doesn't
476 ;; need to be ported to every buggy compiler in existence.
479 (defmacro make-method-function (method-lambda &environment env)
480 (make-method-function-internal method-lambda env))
482 (defun make-method-function-internal (method-lambda &optional env)
483 (multiple-value-bind (proto-gf proto-method)
484 (prototypes-for-make-method-lambda nil)
485 (multiple-value-bind (method-function-lambda initargs)
486 (make-method-lambda proto-gf proto-method method-lambda env)
487 (make-method-initargs-form proto-gf
489 method-function-lambda
493 (defun add-method-declarations (name qualifiers lambda-list body env)
494 (declare (ignore env))
495 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
496 (parse-specialized-lambda-list lambda-list)
497 (multiple-value-bind (real-body declarations documentation)
499 (values `(lambda ,unspecialized-lambda-list
500 ,@(when documentation `(,documentation))
501 ;; (Old PCL code used a somewhat different style of
502 ;; list for %METHOD-NAME values. Our names use
503 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
504 ;; method names look more like what you see in a
507 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
508 ;; least the code to set up named BLOCKs around the
509 ;; bodies of methods, depends on the function's base
510 ;; name being the first element of the %METHOD-NAME
511 ;; list. It would be good to remove this dependency,
512 ;; perhaps by building the BLOCK here, or by using
513 ;; another declaration (e.g. %BLOCK-NAME), so that
514 ;; our method debug names are free to have any format,
515 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
517 ;; Further, as of sbcl-0.7.9.10, the code to
518 ;; implement NO-NEXT-METHOD is coupled to the form of
519 ;; this declaration; see the definition of
520 ;; CALL-NO-NEXT-METHOD (and the passing of
521 ;; METHOD-NAME-DECLARATION arguments around the
522 ;; various CALL-NEXT-METHOD logic).
523 (declare (%method-name (,name
526 (declare (%method-lambda-list ,@lambda-list))
529 unspecialized-lambda-list specializers))))
531 (defun real-make-method-initargs-form (proto-gf proto-method
532 method-lambda initargs env)
533 (declare (ignore proto-gf proto-method))
534 (unless (and (consp method-lambda)
535 (eq (car method-lambda) 'lambda))
536 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
537 is not a lambda form."
539 (make-method-initargs-form-internal method-lambda initargs env))
541 (unless (fboundp 'make-method-initargs-form)
542 (setf (gdefinition 'make-method-initargs-form)
543 (symbol-function 'real-make-method-initargs-form)))
545 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
546 (declare (ignore proto-gf proto-method))
547 (make-method-lambda-internal method-lambda env))
549 ;;; a helper function for creating Python-friendly type declarations
550 ;;; in DEFMETHOD forms
551 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
552 (cond ((and (consp specializer)
553 (eq (car specializer) 'eql))
554 ;; KLUDGE: ANSI, in its wisdom, says that
555 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
556 ;; DEFMETHOD expansion time. Thus, although one might think
558 ;; (DEFMETHOD FOO ((X PACKAGE)
561 ;; the PACKAGE and (EQL 12) forms are both parallel type
562 ;; names, they're not, as is made clear when you do
563 ;; (DEFMETHOD FOO ((X PACKAGE)
566 ;; where Y needs to be a symbol named "BAR", not some cons
567 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
568 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
569 ;; to be of type (EQL X). It'd be easy to transform one to
570 ;; the other, but it'd be somewhat messier to do so while
571 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
572 ;; once. (The new code wouldn't be messy, but it'd require a
573 ;; big transformation of the old code.) So instead we punt.
577 ;; KLUDGE: For some low-level implementation
578 ;; classes, perhaps because of some problems related
579 ;; to the incomplete integration of PCL into SBCL's
580 ;; type system, some specializer classes can't be
581 ;; declared as argument types. E.g.
582 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
583 ;; (DECLARE (TYPE SLOT-OBJECT X))
586 ;; (DEFSTRUCT BAR A B)
588 ;; perhaps because of the way that STRUCTURE-OBJECT
589 ;; inherits both from SLOT-OBJECT and from
590 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
591 ;; problems under the rug, we exclude these problem
592 ;; cases by blacklisting them here. -- WHN 2001-01-19
595 ((not (eq *boot-state* 'complete))
596 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
597 ;; types which don't match their specializers. (Specifically,
598 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
599 ;; second argument.) Hopefully it only does this kind of
600 ;; weirdness when bootstrapping.. -- WHN 20000610
602 ((var-globally-special-p parameter)
603 ;; KLUDGE: Don't declare types for global special variables
604 ;; -- our rebinding magic for SETQ cases don't work right
607 ;; FIXME: It would be better to detect the SETQ earlier and
608 ;; skip declarations for specials only when needed, not
614 ;; Otherwise, we can usually make Python very happy.
615 (let ((kind (info :type :kind specializer)))
617 ((:primitive) `(type ,specializer ,parameter))
619 (let ((class (find-class specializer nil)))
620 ;; CLASS can be null here if the user has erroneously
621 ;; tried to use a defined type as a specializer; it
622 ;; can be a non-BUILT-IN-CLASS if the user defines a
623 ;; type and calls (SETF FIND-CLASS) in a consistent
625 (when (and class (typep class 'built-in-class))
626 `(type ,specializer ,parameter))))
628 (let ((class (find-class specializer nil)))
631 (if (typep class '(or built-in-class structure-class))
632 `(type ,specializer ,parameter)
633 ;; don't declare CLOS classes as parameters;
634 ;; it's too expensive.
637 ;; we can get here, and still not have a failure
638 ;; case, by doing MOP programming like (PROGN
639 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
640 ;; ...)). Best to let the user know we haven't
641 ;; been able to extract enough information:
643 "~@<can't find type for presumed class ~S in ~S.~@:>"
645 'parameter-specializer-declaration-in-defmethod)
647 ((:forthcoming-defclass-type) '(ignorable)))))))
649 (defun make-method-lambda-internal (method-lambda &optional env)
650 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
651 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
652 is not a lambda form."
654 (multiple-value-bind (real-body declarations documentation)
655 (parse-body (cddr method-lambda))
656 (let* ((name-decl (get-declaration '%method-name declarations))
657 (sll-decl (get-declaration '%method-lambda-list declarations))
658 (method-name (when (consp name-decl) (car name-decl)))
659 (generic-function-name (when method-name (car method-name)))
660 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
661 (multiple-value-bind (parameters lambda-list specializers)
662 (parse-specialized-lambda-list specialized-lambda-list)
663 (let* ((required-parameters
664 (mapcar (lambda (r s) (declare (ignore s)) r)
667 (slots (mapcar #'list required-parameters))
671 ;; These declarations seem to be used by PCL to pass
672 ;; information to itself; when I tried to delete 'em
673 ;; ca. 0.6.10 it didn't work. I'm not sure how
674 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
675 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
677 (mapcar (lambda (a s) (and (symbolp s)
682 ;; These TYPE declarations weren't in the original
683 ;; PCL code, but the Python compiler likes them a
684 ;; lot. (We're telling the compiler about our
685 ;; knowledge of specialized argument types so that
686 ;; it can avoid run-time type dispatch overhead,
687 ;; which can be a huge win for Python.)
689 ;; KLUDGE: when I tried moving these to
690 ;; ADD-METHOD-DECLARATIONS, things broke. No idea
691 ;; why. -- CSR, 2004-06-16
692 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
696 ;; Remove the documentation string and insert the
697 ;; appropriate class declarations. The documentation
698 ;; string is removed to make it easy for us to insert
699 ;; new declarations later, they will just go after the
700 ;; CADR of the method lambda. The class declarations
701 ;; are inserted to communicate the class of the method's
702 ;; arguments to the code walk.
703 `(lambda ,lambda-list
704 ;; The default ignorability of method parameters
705 ;; doesn't seem to be specified by ANSI. PCL had
706 ;; them basically ignorable but was a little
707 ;; inconsistent. E.g. even though the two
708 ;; method definitions
709 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
710 ;; (DEFMETHOD FOO ((X T) Y) "Z")
711 ;; are otherwise equivalent, PCL treated Y as
712 ;; ignorable in the first definition but not in the
713 ;; second definition. We make all required
714 ;; parameters ignorable as a way of systematizing
715 ;; the old PCL behavior. -- WHN 2000-11-24
716 (declare (ignorable ,@required-parameters))
719 (block ,(fun-name-block-name generic-function-name)
721 (constant-value-p (and (null (cdr real-body))
722 (constantp (car real-body))))
723 (constant-value (and constant-value-p
724 (eval (car real-body))))
725 (plist (and constant-value-p
726 (or (typep constant-value
727 '(or number character))
728 (and (symbolp constant-value)
729 (symbol-package constant-value)))
730 (list :constant-value constant-value)))
731 (applyp (dolist (p lambda-list nil)
732 (cond ((memq p '(&optional &rest &key))
737 (walked-lambda call-next-method-p closurep
738 next-method-p-p setq-p)
739 (walk-method-lambda method-lambda
744 (multiple-value-bind (walked-lambda-body
746 walked-documentation)
747 (parse-body (cddr walked-lambda))
748 (declare (ignore walked-documentation))
749 (when (or next-method-p-p call-next-method-p)
750 (setq plist (list* :needs-next-methods-p t plist)))
751 (when (some #'cdr slots)
752 (multiple-value-bind (slot-name-lists call-list)
753 (slot-name-lists-from-slots slots calls)
754 (let ((pv-table-symbol (make-symbol "pv-table")))
756 `(,@(when slot-name-lists
757 `(:slot-name-lists ,slot-name-lists))
759 `(:call-list ,call-list))
760 :pv-table-symbol ,pv-table-symbol
762 (setq walked-lambda-body
763 `((pv-binding (,required-parameters
766 ,@walked-lambda-body))))))
767 (when (and (memq '&key lambda-list)
768 (not (memq '&allow-other-keys lambda-list)))
769 (let ((aux (memq '&aux lambda-list)))
770 (setq lambda-list (nconc (ldiff lambda-list aux)
771 (list '&allow-other-keys)
773 (values `(lambda (.method-args. .next-methods.)
774 (simple-lexical-method-functions
775 (,lambda-list .method-args. .next-methods.
778 :next-method-p-p ,next-method-p-p
780 ;; we need to pass this along
781 ;; so that NO-NEXT-METHOD can
782 ;; be given a suitable METHOD
783 ;; argument; we need the
784 ;; QUALIFIERS and SPECIALIZERS
785 ;; inside the declaration to
786 ;; give to FIND-METHOD.
787 :method-name-declaration ,name-decl
790 ,@walked-declarations
791 ,@walked-lambda-body))
794 ,@(when documentation
795 `(:documentation ,documentation)))))))))))
797 (unless (fboundp 'make-method-lambda)
798 (setf (gdefinition 'make-method-lambda)
799 (symbol-function 'real-make-method-lambda)))
801 (defmacro simple-lexical-method-functions ((lambda-list
807 ,method-args ,next-methods
808 (bind-simple-lexical-method-macros (,method-args ,next-methods)
809 (bind-lexical-method-functions (,@lmf-options)
810 (bind-args (,lambda-list ,method-args)
813 (defmacro fast-lexical-method-functions ((lambda-list
819 `(bind-fast-lexical-method-macros (,args ,rest-arg ,next-method-call)
820 (bind-lexical-method-functions (,@lmf-options)
821 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
824 (defmacro bind-simple-lexical-method-macros ((method-args next-methods)
826 `(macrolet ((call-next-method-bind (&body body)
827 `(let ((.next-method. (car ,',next-methods))
828 (,',next-methods (cdr ,',next-methods)))
829 .next-method. ,',next-methods
831 (call-next-method-body (method-name-declaration cnm-args)
833 (funcall (if (std-instance-p .next-method.)
834 (method-function .next-method.)
835 .next-method.) ; for early methods
836 (or ,cnm-args ,',method-args)
838 (apply #'call-no-next-method ',method-name-declaration
839 (or ,cnm-args ,',method-args))))
840 (next-method-p-body ()
841 `(not (null .next-method.)))
842 (with-rebound-original-args ((call-next-method-p setq-p)
844 (declare (ignore call-next-method-p setq-p))
848 (defun call-no-next-method (method-name-declaration &rest args)
849 (destructuring-bind (name) method-name-declaration
850 (destructuring-bind (name &rest qualifiers-and-specializers) name
851 ;; KLUDGE: inefficient traversal, but hey. This should only
852 ;; happen on the slow error path anyway.
853 (let* ((qualifiers (butlast qualifiers-and-specializers))
854 (specializers (car (last qualifiers-and-specializers)))
855 (method (find-method (gdefinition name) qualifiers specializers)))
856 (apply #'no-next-method
857 (method-generic-function method)
861 (defstruct (method-call (:copier nil))
862 (function #'identity :type function)
865 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
867 (defmacro invoke-method-call1 (function args cm-args)
868 `(let ((.function. ,function)
870 (.cm-args. ,cm-args))
871 (if (and .cm-args. (null (cdr .cm-args.)))
872 (funcall .function. .args. (car .cm-args.))
873 (apply .function. .args. .cm-args.))))
875 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
876 `(invoke-method-call1 (method-call-function ,method-call)
878 `(list* ,@required-args+rest-arg)
879 `(list ,@required-args+rest-arg))
880 (method-call-call-method-args ,method-call)))
882 (defstruct (fast-method-call (:copier nil))
883 (function #'identity :type function)
888 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
890 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
891 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
893 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
894 `(fmc-funcall (fast-method-call-function ,method-call)
895 (fast-method-call-pv-cell ,method-call)
896 (fast-method-call-next-method-call ,method-call)
897 ,@required-args+rest-arg))
899 (defstruct (fast-instance-boundp (:copier nil))
900 (index 0 :type fixnum))
902 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
904 (eval-when (:compile-toplevel :load-toplevel :execute)
905 (defvar *allow-emf-call-tracing-p* nil)
906 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
908 ;;;; effective method functions
910 (defvar *emf-call-trace-size* 200)
911 (defvar *emf-call-trace* nil)
912 (defvar *emf-call-trace-index* 0)
914 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
915 ;;; without explanation. It appears to be intended for debugging, so
916 ;;; it might be useful someday, so I haven't deleted it.
917 ;;; But it isn't documented and isn't used for anything now, so
918 ;;; I've conditionalized it out of the base system. -- WHN 19991213
920 (defun show-emf-call-trace ()
921 (when *emf-call-trace*
922 (let ((j *emf-call-trace-index*)
923 (*enable-emf-call-tracing-p* nil))
924 (format t "~&(The oldest entries are printed first)~%")
925 (dotimes-fixnum (i *emf-call-trace-size*)
926 (let ((ct (aref *emf-call-trace* j)))
927 (when ct (print ct)))
929 (when (= j *emf-call-trace-size*)
932 (defun trace-emf-call-internal (emf format args)
933 (unless *emf-call-trace*
934 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
935 (setf (aref *emf-call-trace* *emf-call-trace-index*)
936 (list* emf format args))
937 (incf *emf-call-trace-index*)
938 (when (= *emf-call-trace-index* *emf-call-trace-size*)
939 (setq *emf-call-trace-index* 0)))
941 (defmacro trace-emf-call (emf format args)
942 (when *allow-emf-call-tracing-p*
943 `(when *enable-emf-call-tracing-p*
944 (trace-emf-call-internal ,emf ,format ,args))))
946 (defmacro invoke-effective-method-function-fast
947 (emf restp &rest required-args+rest-arg)
949 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
950 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
952 (defmacro invoke-effective-method-function (emf restp
953 &rest required-args+rest-arg)
954 (unless (constantp restp)
955 (error "The RESTP argument is not constant."))
956 ;; FIXME: The RESTP handling here is confusing and maybe slightly
957 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
958 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
959 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
960 (setq restp (eval restp))
962 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
963 (cond ((typep ,emf 'fast-method-call)
964 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
965 ;; "What," you may wonder, "do these next two clauses do?"
966 ;; In that case, you are not a PCL implementor, for they
967 ;; considered this to be self-documenting.:-| Or CSR, for
968 ;; that matter, since he can also figure it out by looking
969 ;; at it without breaking stride. For the rest of us,
970 ;; though: From what the code is doing with .SLOTS. and
971 ;; whatnot, evidently it's implementing SLOT-VALUEish and
972 ;; GET-SLOT-VALUEish things. Then we can reason backwards
973 ;; and conclude that setting EMF to a FIXNUM is an
974 ;; optimized way to represent these slot access operations.
975 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
976 `(((typep ,emf 'fixnum)
977 (let* ((.slots. (get-slots-or-nil
978 ,(car required-args+rest-arg)))
979 (value (when .slots. (clos-slots-ref .slots. ,emf))))
980 (if (eq value +slot-unbound+)
981 (slot-unbound-internal ,(car required-args+rest-arg)
984 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
985 `(((typep ,emf 'fixnum)
986 (let ((.new-value. ,(car required-args+rest-arg))
987 (.slots. (get-slots-or-nil
988 ,(cadr required-args+rest-arg))))
990 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
991 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
992 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
993 ;; there was no explanation and presumably the code is 10+
994 ;; years stale, I simply deleted it. -- WHN)
998 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
1001 `(apply (the function ,emf) ,@required-args+rest-arg)
1002 `(funcall (the function ,emf)
1003 ,@required-args+rest-arg))))))))
1005 (defun invoke-emf (emf args)
1006 (trace-emf-call emf t args)
1009 (let* ((arg-info (fast-method-call-arg-info emf))
1010 (restp (cdr arg-info))
1011 (nreq (car arg-info)))
1013 (let* ((rest-args (nthcdr nreq args))
1014 (req-args (ldiff args rest-args)))
1015 (apply (fast-method-call-function emf)
1016 (fast-method-call-pv-cell emf)
1017 (fast-method-call-next-method-call emf)
1018 (nconc req-args (list rest-args))))
1021 (invoke-fast-method-call emf)
1022 (error "wrong number of args")))
1025 (invoke-fast-method-call emf (car args))
1026 (error "wrong number of args")))
1029 (invoke-fast-method-call emf (car args) (cadr args))
1030 (error "wrong number of args")))
1032 (apply (fast-method-call-function emf)
1033 (fast-method-call-pv-cell emf)
1034 (fast-method-call-next-method-call emf)
1037 (apply (method-call-function emf)
1039 (method-call-call-method-args emf)))
1041 (cond ((null args) (error "1 or 2 args were expected."))
1043 (let* ((slots (get-slots (car args)))
1044 (value (clos-slots-ref slots emf)))
1045 (if (eq value +slot-unbound+)
1046 (slot-unbound-internal (car args) emf)
1049 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1051 (t (error "1 or 2 args were expected."))))
1052 (fast-instance-boundp
1053 (if (or (null args) (cdr args))
1054 (error "1 arg was expected.")
1055 (let ((slots (get-slots (car args))))
1056 (not (eq (clos-slots-ref slots
1057 (fast-instance-boundp-index emf))
1062 (defmacro bind-fast-lexical-method-macros ((args rest-arg next-method-call)
1064 (let* ((all-params (append args (when rest-arg (list rest-arg))))
1065 (rebindings (mapcar (lambda (x) (list x x)) all-params)))
1066 `(macrolet ((narrowed-emf (emf)
1067 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to
1068 ;; dispatch on the possibility that EMF might be of
1069 ;; type FIXNUM (as an optimized representation of a
1070 ;; slot accessor). But as far as I (WHN 2002-06-11)
1071 ;; can tell, it's impossible for such a representation
1072 ;; to end up as .NEXT-METHOD-CALL. By reassuring
1073 ;; INVOKE-E-M-F that when called from this context
1074 ;; it needn't worry about the FIXNUM case, we can
1075 ;; keep those cases from being compiled, which is
1076 ;; good both because it saves bytes and because it
1077 ;; avoids annoying type mismatch compiler warnings.
1079 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type
1080 ;; system isn't smart enough about NOT and
1081 ;; intersection types to benefit from a (NOT FIXNUM)
1082 ;; declaration here. -- WHN 2002-06-12 (FIXME: maybe
1083 ;; it is now... -- CSR, 2003-06-07)
1085 ;; FIXME: Might the FUNCTION type be omittable here,
1086 ;; leaving only METHOD-CALLs? Failing that, could this
1087 ;; be documented somehow? (It'd be nice if the types
1088 ;; involved could be understood without solving the
1089 ;; halting problem.)
1090 `(the (or function method-call fast-method-call)
1092 (call-next-method-bind (&body body)
1094 (call-next-method-body (method-name-declaration cnm-args)
1095 `(if ,',next-method-call
1097 ;; This declaration suppresses a "deleting
1098 ;; unreachable code" note for the following IF
1099 ;; when REST-ARG is NIL. It is not nice for
1100 ;; debugging SBCL itself, but at least it
1101 ;; keeps us from annoying users.
1102 (declare (optimize (inhibit-warnings 3)))
1103 (if (and (null ',rest-arg)
1105 (eq (car cnm-args) 'list))
1106 `(invoke-effective-method-function
1107 (narrowed-emf ,',next-method-call)
1110 (let ((call `(invoke-effective-method-function
1111 (narrowed-emf ,',next-method-call)
1112 ,',(not (null rest-arg))
1114 ,@',(when rest-arg `(,rest-arg)))))
1116 (bind-args ((,@',args
1118 `(&rest ,rest-arg)))
1123 ;; As above, this declaration suppresses code
1125 (declare (optimize (inhibit-warnings 3)))
1126 (if (and (null ',rest-arg)
1128 (eq (car cnm-args) 'list))
1129 `(call-no-next-method ',method-name-declaration
1131 `(call-no-next-method ',method-name-declaration
1135 (next-method-p-body ()
1136 `(not (null ,',next-method-call)))
1137 (with-rebound-original-args ((cnm-p setq-p) &body body)
1138 (if (or cnm-p setq-p)
1140 (declare (ignorable ,@',all-params))
1145 (defmacro bind-lexical-method-functions
1146 ((&key call-next-method-p next-method-p-p setq-p
1147 closurep applyp method-name-declaration)
1149 (cond ((and (null call-next-method-p) (null next-method-p-p)
1150 (null closurep) (null applyp) (null setq-p))
1153 `(call-next-method-bind
1154 (flet (,@(and call-next-method-p
1155 `((call-next-method (&rest cnm-args)
1156 (call-next-method-body
1157 ,method-name-declaration
1159 ,@(and next-method-p-p
1161 (next-method-p-body)))))
1162 (with-rebound-original-args (,call-next-method-p ,setq-p)
1165 (defmacro bind-args ((lambda-list args) &body body)
1166 (let ((args-tail '.args-tail.)
1169 (flet ((process-var (var)
1170 (if (memq var lambda-list-keywords)
1173 (&optional (setq state 'optional))
1174 (&key (setq state 'key))
1176 (&rest (setq state 'rest))
1177 (&aux (setq state 'aux))
1180 "encountered the non-standard lambda list keyword ~S"
1184 (required `((,var (pop ,args-tail))))
1185 (optional (cond ((not (consp var))
1186 `((,var (when ,args-tail
1187 (pop ,args-tail)))))
1189 `((,(car var) (if ,args-tail
1193 `((,(caddr var) ,args-tail)
1194 (,(car var) (if ,args-tail
1197 (rest `((,var ,args-tail)))
1198 (key (cond ((not (consp var))
1200 (get-key-arg-tail ,(keywordicate var)
1203 (multiple-value-bind (keyword variable)
1204 (if (consp (car var))
1207 (values (keywordicate (car var))
1209 `((,key (get-key-arg-tail ',keyword
1215 (multiple-value-bind (keyword variable)
1216 (if (consp (car var))
1219 (values (keywordicate (car var))
1221 `((,key (get-key-arg-tail ',keyword
1228 (let ((bindings (mapcan #'process-var lambda-list)))
1229 `(let* ((,args-tail ,args)
1232 ,@(when (eq state 'optional)
1233 `((unless (null ,args-tail)
1234 (error 'simple-program-error
1235 :format-control "surplus arguments: ~S"
1236 :format-arguments (list ,args-tail)))))))
1237 (declare (ignorable ,args-tail .dummy0.))
1240 (defun get-key-arg-tail (keyword list)
1241 (loop for (key . tail) on list by #'cddr
1243 ;; FIXME: Do we want to export this symbol? Or maybe use an
1244 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1245 (sb-c::%odd-key-args-error)
1246 when (eq key keyword)
1249 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1250 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1251 ; should be in the method definition
1252 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1253 ; was seen in the body of a method
1254 (next-method-p-p nil) ; flag indicating that NEXT-METHOD-P
1255 ; should be in the method definition
1257 (flet ((walk-function (form context env)
1258 (cond ((not (eq context :eval)) form)
1259 ;; FIXME: Jumping to a conclusion from the way it's used
1260 ;; above, perhaps CONTEXT should be called SITUATION
1261 ;; (after the term used in the ANSI specification of
1262 ;; EVAL-WHEN) and given modern ANSI keyword values
1263 ;; like :LOAD-TOPLEVEL.
1264 ((not (listp form)) form)
1265 ((eq (car form) 'call-next-method)
1266 (setq call-next-method-p t)
1268 ((eq (car form) 'next-method-p)
1269 (setq next-method-p-p t)
1271 ((memq (car form) '(setq multiple-value-setq))
1272 ;; FIXME: this is possibly a little strong as
1273 ;; conditions go. Ideally we would want to detect
1274 ;; which, if any, of the method parameters are
1275 ;; being set, and communicate that information to
1276 ;; e.g. SPLIT-DECLARATIONS. However, the brute
1277 ;; force method doesn't really cost much; a little
1278 ;; loss of discrimination over IGNORED variables
1279 ;; should be all. -- CSR, 2004-07-01
1282 ((and (eq (car form) 'function)
1283 (cond ((eq (cadr form) 'call-next-method)
1284 (setq call-next-method-p t)
1287 ((eq (cadr form) 'next-method-p)
1288 (setq next-method-p-p t)
1292 ((and (memq (car form)
1293 '(slot-value set-slot-value slot-boundp))
1294 (constantp (caddr form)))
1295 (let ((parameter (can-optimize-access form
1298 (let ((fun (ecase (car form)
1299 (slot-value #'optimize-slot-value)
1300 (set-slot-value #'optimize-set-slot-value)
1301 (slot-boundp #'optimize-slot-boundp))))
1302 (funcall fun slots parameter form))))
1303 ((and (eq (car form) 'apply)
1305 (eq (car (cadr form)) 'function)
1306 (generic-function-name-p (cadr (cadr form))))
1307 (optimize-generic-function-call
1308 form required-parameters env slots calls))
1309 ((generic-function-name-p (car form))
1310 (optimize-generic-function-call
1311 form required-parameters env slots calls))
1314 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1315 (values walked-lambda
1321 (defun generic-function-name-p (name)
1322 (and (legal-fun-name-p name)
1324 (if (eq *boot-state* 'complete)
1325 (standard-generic-function-p (gdefinition name))
1326 (funcallable-instance-p (gdefinition name)))))
1328 (defvar *method-function-plist* (make-hash-table :test 'eq))
1331 (defvar *mf1cp* nil)
1334 (defvar *mf2cp* nil)
1336 (defun method-function-plist (method-function)
1337 (unless (eq method-function *mf1*)
1338 (rotatef *mf1* *mf2*)
1339 (rotatef *mf1p* *mf2p*)
1340 (rotatef *mf1cp* *mf2cp*))
1341 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1342 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1343 (unless (eq method-function *mf1*)
1344 (setf *mf1* method-function
1346 *mf1p* (gethash method-function *method-function-plist*)))
1349 (defun (setf method-function-plist)
1350 (val method-function)
1351 (unless (eq method-function *mf1*)
1352 (rotatef *mf1* *mf2*)
1353 (rotatef *mf1cp* *mf2cp*)
1354 (rotatef *mf1p* *mf2p*))
1355 (unless (or (eq method-function *mf1*) (null *mf1cp*))
1356 (setf (gethash *mf1* *method-function-plist*) *mf1p*))
1357 (setf *mf1* method-function
1361 (defun method-function-get (method-function key &optional default)
1362 (getf (method-function-plist method-function) key default))
1364 (defun (setf method-function-get)
1365 (val method-function key)
1366 (setf (getf (method-function-plist method-function) key) val))
1368 (defun method-function-pv-table (method-function)
1369 (method-function-get method-function :pv-table))
1371 (defun method-function-method (method-function)
1372 (method-function-get method-function :method))
1374 (defun method-function-needs-next-methods-p (method-function)
1375 (method-function-get method-function :needs-next-methods-p t))
1377 (defmacro method-function-closure-generator (method-function)
1378 `(method-function-get ,method-function 'closure-generator))
1380 (defun load-defmethod
1381 (class name quals specls ll initargs &optional pv-table-symbol)
1382 (setq initargs (copy-tree initargs))
1383 (let ((method-spec (or (getf initargs :method-spec)
1384 (make-method-spec name quals specls))))
1385 (setf (getf initargs :method-spec) method-spec)
1386 (load-defmethod-internal class name quals specls
1387 ll initargs pv-table-symbol)))
1389 (defun load-defmethod-internal
1390 (method-class gf-spec qualifiers specializers lambda-list
1391 initargs pv-table-symbol)
1392 (when pv-table-symbol
1393 (setf (getf (getf initargs :plist) :pv-table-symbol)
1395 (when (and (eq *boot-state* 'complete)
1397 (let* ((gf (fdefinition gf-spec))
1398 (method (and (generic-function-p gf)
1399 (generic-function-methods gf)
1402 (parse-specializers specializers)
1405 (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1406 gf-spec qualifiers specializers))))
1407 (let ((method (apply #'add-named-method
1408 gf-spec qualifiers specializers lambda-list
1409 :definition-source `((defmethod ,gf-spec
1414 (unless (or (eq method-class 'standard-method)
1415 (eq (find-class method-class nil) (class-of method)))
1416 ;; FIXME: should be STYLE-WARNING?
1417 (format *error-output*
1418 "~&At the time the method with qualifiers ~:S and~%~
1419 specializers ~:S on the generic function ~S~%~
1420 was compiled, the method-class for that generic function was~%~
1421 ~S. But, the method class is now ~S, this~%~
1422 may mean that this method was compiled improperly.~%"
1423 qualifiers specializers gf-spec
1424 method-class (class-name (class-of method))))
1427 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1428 `(slow-method ,gf-spec ,@qualifiers ,unparsed-specializers))
1430 (defun initialize-method-function (initargs &optional return-function-p method)
1431 (let* ((mf (getf initargs :function))
1432 (method-spec (getf initargs :method-spec))
1433 (plist (getf initargs :plist))
1434 (pv-table-symbol (getf plist :pv-table-symbol))
1436 (mff (getf initargs :fast-function)))
1437 (flet ((set-mf-property (p v)
1439 (setf (method-function-get mf p) v))
1441 (setf (method-function-get mff p) v))))
1444 (setq mf (set-fun-name mf method-spec)))
1446 (let ((name `(fast-method ,@(cdr method-spec))))
1447 (set-fun-name mff name)
1449 (set-mf-property :name name)))))
1451 (let ((snl (getf plist :slot-name-lists))
1452 (cl (getf plist :call-list)))
1454 (setq pv-table (intern-pv-table :slot-name-lists snl
1456 (when pv-table (set pv-table-symbol pv-table))
1457 (set-mf-property :pv-table pv-table)))
1458 (loop (when (null plist) (return nil))
1459 (set-mf-property (pop plist) (pop plist)))
1461 (set-mf-property :method method))
1462 (when return-function-p
1463 (or mf (method-function-from-fast-function mff)))))))
1465 (defun analyze-lambda-list (lambda-list)
1466 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1467 (parse-key-arg (arg)
1469 (if (listp (car arg))
1471 (keywordicate (car arg)))
1472 (keywordicate arg))))
1478 (allow-other-keys-p nil)
1480 (keyword-parameters ())
1482 (dolist (x lambda-list)
1483 (if (memq x lambda-list-keywords)
1485 (&optional (setq state 'optional))
1488 (&allow-other-keys (setq allow-other-keys-p t))
1489 (&rest (setq restp t
1493 (error "encountered the non-standard lambda list keyword ~S"
1496 (required (incf nrequired))
1497 (optional (incf noptional))
1498 (key (push (parse-key-arg x) keywords)
1499 (push x keyword-parameters))
1500 (rest (incf nrest)))))
1501 (when (and restp (zerop nrest))
1502 (error "Error in lambda-list:~%~
1503 After &REST, a DEFGENERIC lambda-list ~
1504 must be followed by at least one variable."))
1505 (values nrequired noptional keysp restp allow-other-keys-p
1507 (reverse keyword-parameters)))))
1509 (defun keyword-spec-name (x)
1510 (let ((key (if (atom x) x (car x))))
1515 (defun ftype-declaration-from-lambda-list (lambda-list name)
1516 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1517 keywords keyword-parameters)
1518 (analyze-lambda-list lambda-list)
1519 (declare (ignore keyword-parameters))
1520 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1521 (old-ftype (if (fun-type-p old) old nil))
1522 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1523 (old-keys (and old-ftype
1524 (mapcar #'key-info-name
1527 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1528 (old-allowp (and old-ftype
1529 (fun-type-allowp old-ftype)))
1530 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1531 `(function ,(append (make-list nrequired :initial-element t)
1532 (when (plusp noptional)
1533 (append '(&optional)
1534 (make-list noptional :initial-element t)))
1535 (when (or restp old-restp)
1537 (when (or keysp old-keysp)
1539 (mapcar (lambda (key)
1542 (when (or allow-other-keys-p old-allowp)
1543 '(&allow-other-keys)))))
1546 (defun defgeneric-declaration (spec lambda-list)
1547 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1549 ;;;; early generic function support
1551 (defvar *!early-generic-functions* ())
1553 (defun ensure-generic-function (fun-name
1557 (declare (ignore environment))
1558 (let ((existing (and (gboundp fun-name)
1559 (gdefinition fun-name))))
1561 (eq *boot-state* 'complete)
1562 (null (generic-function-p existing)))
1563 (generic-clobbers-function fun-name)
1564 (apply #'ensure-generic-function-using-class
1565 existing fun-name all-keys))))
1567 (defun generic-clobbers-function (fun-name)
1568 (error 'simple-program-error
1569 :format-control "~S already names an ordinary function or a macro."
1570 :format-arguments (list fun-name)))
1572 (defvar *sgf-wrapper*
1573 (boot-make-wrapper (early-class-size 'standard-generic-function)
1574 'standard-generic-function))
1576 (defvar *sgf-slots-init*
1577 (mapcar (lambda (canonical-slot)
1578 (if (memq (getf canonical-slot :name) '(arg-info source))
1580 (let ((initfunction (getf canonical-slot :initfunction)))
1582 (funcall initfunction)
1584 (early-collect-inheritance 'standard-generic-function)))
1586 (defvar *sgf-method-class-index*
1587 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1589 (defun early-gf-p (x)
1590 (and (fsc-instance-p x)
1591 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1594 (defvar *sgf-methods-index*
1595 (!bootstrap-slot-index 'standard-generic-function 'methods))
1597 (defmacro early-gf-methods (gf)
1598 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1600 (defvar *sgf-arg-info-index*
1601 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1603 (defmacro early-gf-arg-info (gf)
1604 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1606 (defvar *sgf-dfun-state-index*
1607 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1609 (defstruct (arg-info
1611 (:constructor make-arg-info ())
1613 (arg-info-lambda-list :no-lambda-list)
1616 arg-info-number-optional
1618 arg-info-keys ;nil no &KEY or &REST allowed
1619 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1620 ;T must have &KEY or &REST
1622 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1623 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1625 gf-info-static-c-a-m-emf
1626 (gf-info-c-a-m-emf-std-p t)
1629 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1631 (defun arg-info-valid-p (arg-info)
1632 (not (null (arg-info-number-optional arg-info))))
1634 (defun arg-info-applyp (arg-info)
1635 (or (plusp (arg-info-number-optional arg-info))
1636 (arg-info-key/rest-p arg-info)))
1638 (defun arg-info-number-required (arg-info)
1639 (length (arg-info-metatypes arg-info)))
1641 (defun arg-info-nkeys (arg-info)
1642 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1644 (defun create-gf-lambda-list (lambda-list)
1645 ;;; Create a gf lambda list from a method lambda list
1646 (loop for x in lambda-list
1647 collect (if (consp x) (list (car x)) x)
1648 if (eq x '&key) do (loop-finish)))
1650 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1651 argument-precedence-order)
1652 (let* ((arg-info (if (eq *boot-state* 'complete)
1654 (early-gf-arg-info gf)))
1655 (methods (if (eq *boot-state* 'complete)
1656 (generic-function-methods gf)
1657 (early-gf-methods gf)))
1658 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1659 (first-p (and new-method (null (cdr methods)))))
1660 (when (and (not lambda-list-p) methods)
1661 (setq lambda-list (gf-lambda-list gf)))
1662 (when (or lambda-list-p
1664 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1665 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1666 (analyze-lambda-list lambda-list)
1667 (when (and methods (not first-p))
1668 (let ((gf-nreq (arg-info-number-required arg-info))
1669 (gf-nopt (arg-info-number-optional arg-info))
1670 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1671 (unless (and (= nreq gf-nreq)
1673 (eq (or keysp restp) gf-key/rest-p))
1674 (error "The lambda-list ~S is incompatible with ~
1675 existing methods of ~S."
1677 (setf (arg-info-lambda-list arg-info)
1680 (create-gf-lambda-list lambda-list)))
1681 (when (or lambda-list-p argument-precedence-order
1682 (null (arg-info-precedence arg-info)))
1683 (setf (arg-info-precedence arg-info)
1684 (compute-precedence lambda-list nreq argument-precedence-order)))
1685 (setf (arg-info-metatypes arg-info) (make-list nreq))
1686 (setf (arg-info-number-optional arg-info) nopt)
1687 (setf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1688 (setf (arg-info-keys arg-info)
1690 (if allow-other-keys-p t keywords)
1691 (arg-info-key/rest-p arg-info)))))
1693 (check-method-arg-info gf arg-info new-method))
1694 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1697 (defun check-method-arg-info (gf arg-info method)
1698 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1699 (analyze-lambda-list (if (consp method)
1700 (early-method-lambda-list method)
1701 (method-lambda-list method)))
1702 (flet ((lose (string &rest args)
1703 (error 'simple-program-error
1704 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1705 to the generic function~2I~_~S;~I~_~
1707 :format-arguments (list method gf string args)))
1708 (comparison-description (x y)
1709 (if (> x y) "more" "fewer")))
1710 (let ((gf-nreq (arg-info-number-required arg-info))
1711 (gf-nopt (arg-info-number-optional arg-info))
1712 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1713 (gf-keywords (arg-info-keys arg-info)))
1714 (unless (= nreq gf-nreq)
1716 "the method has ~A required arguments than the generic function."
1717 (comparison-description nreq gf-nreq)))
1718 (unless (= nopt gf-nopt)
1720 "the method has ~A optional arguments than the generic function."
1721 (comparison-description nopt gf-nopt)))
1722 (unless (eq (or keysp restp) gf-key/rest-p)
1724 "the method and generic function differ in whether they accept~_~
1725 &REST or &KEY arguments."))
1726 (when (consp gf-keywords)
1727 (unless (or (and restp (not keysp))
1729 (every (lambda (k) (memq k keywords)) gf-keywords))
1730 (lose "the method does not accept each of the &KEY arguments~2I~_~
1734 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1735 (let* ((existing-p (and methods (cdr methods) new-method))
1736 (nreq (length (arg-info-metatypes arg-info)))
1737 (metatypes (if existing-p
1738 (arg-info-metatypes arg-info)
1740 (type (if existing-p
1741 (gf-info-simple-accessor-type arg-info)
1743 (when (arg-info-valid-p arg-info)
1744 (dolist (method (if new-method (list new-method) methods))
1745 (let* ((specializers (if (or (eq *boot-state* 'complete)
1746 (not (consp method)))
1747 (method-specializers method)
1748 (early-method-specializers method t)))
1749 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1751 (early-method-class method)))
1752 (new-type (when (and class
1753 (or (not (eq *boot-state* 'complete))
1754 (eq (generic-function-method-combination gf)
1755 *standard-method-combination*)))
1756 (cond ((eq class *the-class-standard-reader-method*)
1758 ((eq class *the-class-standard-writer-method*)
1760 ((eq class *the-class-standard-boundp-method*)
1762 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1763 (setq type (cond ((null type) new-type)
1764 ((eq type new-type) type)
1766 (setf (arg-info-metatypes arg-info) metatypes)
1767 (setf (gf-info-simple-accessor-type arg-info) type)))
1768 (when (or (not was-valid-p) first-p)
1769 (multiple-value-bind (c-a-m-emf std-p)
1772 (compute-applicable-methods-emf gf))
1773 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1774 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1775 (unless (gf-info-c-a-m-emf-std-p arg-info)
1776 (setf (gf-info-simple-accessor-type arg-info) t))))
1778 (let ((name (if (eq *boot-state* 'complete)
1779 (generic-function-name gf)
1780 (!early-gf-name gf))))
1781 (setf (gf-precompute-dfun-and-emf-p arg-info)
1785 *internal-pcl-generalized-fun-name-symbols*))
1787 (t (let* ((symbol (fun-name-block-name name))
1788 (package (symbol-package symbol)))
1789 (and (or (eq package *pcl-package*)
1790 (memq package (package-use-list *pcl-package*)))
1791 ;; FIXME: this test will eventually be
1792 ;; superseded by the *internal-pcl...* test,
1793 ;; above. While we are in a process of
1794 ;; transition, however, it should probably
1796 (not (find #\Space (symbol-name symbol))))))))))
1797 (setf (gf-info-fast-mf-p arg-info)
1798 (or (not (eq *boot-state* 'complete))
1799 (let* ((method-class (generic-function-method-class gf))
1800 (methods (compute-applicable-methods
1801 #'make-method-lambda
1802 (list gf (class-prototype method-class)
1804 (and methods (null (cdr methods))
1805 (let ((specls (method-specializers (car methods))))
1806 (and (classp (car specls))
1807 (eq 'standard-generic-function
1808 (class-name (car specls)))
1809 (classp (cadr specls))
1810 (eq 'standard-method
1811 (class-name (cadr specls)))))))))
1814 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1816 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1817 ;;; generic functions is used to store the early methods and early
1818 ;;; discriminator code for the early generic function. The static
1819 ;;; slots field of the fins contains a list whose:
1820 ;;; CAR - a list of the early methods on this early gf
1821 ;;; CADR - the early discriminator code for this method
1822 (defun ensure-generic-function-using-class (existing spec &rest keys
1823 &key (lambda-list nil
1825 argument-precedence-order
1827 (declare (ignore keys))
1828 (cond ((and existing (early-gf-p existing))
1830 (set-arg-info existing :lambda-list lambda-list))
1832 ((assoc spec *!generic-function-fixups* :test #'equal)
1834 (make-early-gf spec lambda-list lambda-list-p existing
1835 argument-precedence-order)
1836 (error "The function ~S is not already defined." spec)))
1838 (error "~S should be on the list ~S."
1840 '*!generic-function-fixups*))
1842 (pushnew spec *!early-generic-functions* :test #'equal)
1843 (make-early-gf spec lambda-list lambda-list-p nil
1844 argument-precedence-order))))
1846 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1847 function argument-precedence-order)
1848 (let ((fin (allocate-funcallable-instance *sgf-wrapper* *sgf-slots-init*)))
1849 (set-funcallable-instance-function
1852 (if (eq spec 'print-object)
1853 #'(instance-lambda (instance stream)
1854 (print-unreadable-object (instance stream :identity t)
1855 (format stream "std-instance")))
1856 #'(instance-lambda (&rest args)
1857 (declare (ignore args))
1858 (error "The function of the funcallable-instance ~S~
1859 has not been set." fin)))))
1860 (setf (gdefinition spec) fin)
1861 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1862 (!bootstrap-set-slot 'standard-generic-function
1866 (set-fun-name fin spec)
1867 (let ((arg-info (make-arg-info)))
1868 (setf (early-gf-arg-info fin) arg-info)
1870 (proclaim (defgeneric-declaration spec lambda-list))
1871 (if argument-precedence-order
1873 :lambda-list lambda-list
1874 :argument-precedence-order argument-precedence-order)
1875 (set-arg-info fin :lambda-list lambda-list))))
1878 (defun set-dfun (gf &optional dfun cache info)
1880 (setf (cache-owner cache) gf))
1881 (let ((new-state (if (and dfun (or cache info))
1882 (list* dfun cache info)
1884 (if (eq *boot-state* 'complete)
1885 (setf (gf-dfun-state gf) new-state)
1886 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1890 (defun gf-dfun-cache (gf)
1891 (let ((state (if (eq *boot-state* 'complete)
1893 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1896 (cons (cadr state)))))
1898 (defun gf-dfun-info (gf)
1899 (let ((state (if (eq *boot-state* 'complete)
1901 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1904 (cons (cddr state)))))
1906 (defvar *sgf-name-index*
1907 (!bootstrap-slot-index 'standard-generic-function 'name))
1909 (defun !early-gf-name (gf)
1910 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1912 (defun gf-lambda-list (gf)
1913 (let ((arg-info (if (eq *boot-state* 'complete)
1915 (early-gf-arg-info gf))))
1916 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
1917 (let ((methods (if (eq *boot-state* 'complete)
1918 (generic-function-methods gf)
1919 (early-gf-methods gf))))
1922 (warn "no way to determine the lambda list for ~S" gf)
1924 (let* ((method (car (last methods)))
1925 (ll (if (consp method)
1926 (early-method-lambda-list method)
1927 (method-lambda-list method))))
1928 (create-gf-lambda-list ll))))
1929 (arg-info-lambda-list arg-info))))
1931 (defmacro real-ensure-gf-internal (gf-class all-keys env)
1933 (cond ((symbolp ,gf-class)
1934 (setq ,gf-class (find-class ,gf-class t ,env)))
1935 ((classp ,gf-class))
1937 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
1938 class nor a symbol that names a class."
1940 (remf ,all-keys :generic-function-class)
1941 (remf ,all-keys :environment)
1942 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
1943 (unless (eq combin '.shes-not-there.)
1944 (setf (getf ,all-keys :method-combination)
1945 (find-method-combination (class-prototype ,gf-class)
1948 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
1949 (unless (eq method-class '.shes-not-there.)
1950 (setf (getf ,all-keys :method-class)
1951 (find-class method-class t ,env))))))
1953 (defun real-ensure-gf-using-class--generic-function
1957 &key environment (lambda-list nil lambda-list-p)
1958 (generic-function-class 'standard-generic-function gf-class-p)
1960 (real-ensure-gf-internal generic-function-class all-keys environment)
1961 (unless (or (null gf-class-p)
1962 (eq (class-of existing) generic-function-class))
1963 (change-class existing generic-function-class))
1965 (apply #'reinitialize-instance existing all-keys)
1967 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1969 (defun real-ensure-gf-using-class--null
1973 &key environment (lambda-list nil lambda-list-p)
1974 (generic-function-class 'standard-generic-function)
1976 (declare (ignore existing))
1977 (real-ensure-gf-internal generic-function-class all-keys environment)
1979 (setf (gdefinition fun-name)
1980 (apply #'make-instance generic-function-class
1981 :name fun-name all-keys))
1983 (proclaim (defgeneric-declaration fun-name lambda-list)))))
1985 (defun get-generic-fun-info (gf)
1986 ;; values nreq applyp metatypes nkeys arg-info
1987 (multiple-value-bind (applyp metatypes arg-info)
1988 (let* ((arg-info (if (early-gf-p gf)
1989 (early-gf-arg-info gf)
1991 (metatypes (arg-info-metatypes arg-info)))
1992 (values (arg-info-applyp arg-info)
1995 (values (length metatypes) applyp metatypes
1996 (count-if (lambda (x) (neq x t)) metatypes)
1999 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
2000 &optional slot-name)
2001 (initialize-method-function initargs)
2004 ;; Figure out whether we got class objects or class names as the
2005 ;; specializers and set parsed and unparsed appropriately. If we
2006 ;; got class objects, then we can compute unparsed, but if we got
2007 ;; class names we don't try to compute parsed.
2009 ;; Note that the use of not symbolp in this call to every should be
2010 ;; read as 'classp' we can't use classp itself because it doesn't
2012 (if (every (lambda (s) (not (symbolp s))) specializers)
2013 (setq parsed specializers
2014 unparsed (mapcar (lambda (s)
2015 (if (eq s t) t (class-name s)))
2017 (setq unparsed specializers
2019 (list :early-method ;This is an early method dammit!
2021 (getf initargs :function)
2022 (getf initargs :fast-function)
2024 parsed ;The parsed specializers. This is used
2025 ;by early-method-specializers to cache
2026 ;the parse. Note that this only comes
2027 ;into play when there is more than one
2028 ;early method on an early gf.
2030 (list class ;A list to which real-make-a-method
2031 qualifiers ;can be applied to make a real method
2032 arglist ;corresponding to this early one.
2038 (defun real-make-a-method
2039 (class qualifiers lambda-list specializers initargs doc
2040 &optional slot-name)
2041 (setq specializers (parse-specializers specializers))
2042 (apply #'make-instance class
2043 :qualifiers qualifiers
2044 :lambda-list lambda-list
2045 :specializers specializers
2047 :slot-name slot-name
2051 (defun early-method-function (early-method)
2052 (values (cadr early-method) (caddr early-method)))
2054 (defun early-method-class (early-method)
2055 (find-class (car (fifth early-method))))
2057 (defun early-method-standard-accessor-p (early-method)
2058 (let ((class (first (fifth early-method))))
2059 (or (eq class 'standard-reader-method)
2060 (eq class 'standard-writer-method)
2061 (eq class 'standard-boundp-method))))
2063 (defun early-method-standard-accessor-slot-name (early-method)
2064 (seventh (fifth early-method)))
2066 ;;; Fetch the specializers of an early method. This is basically just
2067 ;;; a simple accessor except that when the second argument is t, this
2068 ;;; converts the specializers from symbols into class objects. The
2069 ;;; class objects are cached in the early method, this makes
2070 ;;; bootstrapping faster because the class objects only have to be
2074 ;;; The second argument should only be passed as T by
2075 ;;; early-lookup-method. This is to implement the rule that only when
2076 ;;; there is more than one early method on a generic function is the
2077 ;;; conversion from class names to class objects done. This
2078 ;;; corresponds to the fact that we are only allowed to have one
2079 ;;; method on any generic function up until the time classes exist.
2080 (defun early-method-specializers (early-method &optional objectsp)
2081 (if (and (listp early-method)
2082 (eq (car early-method) :early-method))
2083 (cond ((eq objectsp t)
2084 (or (fourth early-method)
2085 (setf (fourth early-method)
2086 (mapcar #'find-class (cadddr (fifth early-method))))))
2088 (cadddr (fifth early-method))))
2089 (error "~S is not an early-method." early-method)))
2091 (defun early-method-qualifiers (early-method)
2092 (cadr (fifth early-method)))
2094 (defun early-method-lambda-list (early-method)
2095 (caddr (fifth early-method)))
2097 (defun early-add-named-method (generic-function-name
2102 (let* ((gf (ensure-generic-function generic-function-name))
2104 (dolist (m (early-gf-methods gf))
2105 (when (and (equal (early-method-specializers m) specializers)
2106 (equal (early-method-qualifiers m) qualifiers))
2108 (new (make-a-method 'standard-method
2114 (when existing (remove-method gf existing))
2115 (add-method gf new)))
2117 ;;; This is the early version of ADD-METHOD. Later this will become a
2118 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2119 ;;; special knowledge about ADD-METHOD.
2120 (defun add-method (generic-function method)
2121 (when (not (fsc-instance-p generic-function))
2122 (error "Early ADD-METHOD didn't get a funcallable instance."))
2123 (when (not (and (listp method) (eq (car method) :early-method)))
2124 (error "Early ADD-METHOD didn't get an early method."))
2125 (push method (early-gf-methods generic-function))
2126 (set-arg-info generic-function :new-method method)
2127 (unless (assoc (!early-gf-name generic-function)
2128 *!generic-function-fixups*
2130 (update-dfun generic-function)))
2132 ;;; This is the early version of REMOVE-METHOD. See comments on
2133 ;;; the early version of ADD-METHOD.
2134 (defun remove-method (generic-function method)
2135 (when (not (fsc-instance-p generic-function))
2136 (error "An early remove-method didn't get a funcallable instance."))
2137 (when (not (and (listp method) (eq (car method) :early-method)))
2138 (error "An early remove-method didn't get an early method."))
2139 (setf (early-gf-methods generic-function)
2140 (remove method (early-gf-methods generic-function)))
2141 (set-arg-info generic-function)
2142 (unless (assoc (!early-gf-name generic-function)
2143 *!generic-function-fixups*
2145 (update-dfun generic-function)))
2147 ;;; This is the early version of GET-METHOD. See comments on the early
2148 ;;; version of ADD-METHOD.
2149 (defun get-method (generic-function qualifiers specializers
2150 &optional (errorp t))
2151 (if (early-gf-p generic-function)
2152 (or (dolist (m (early-gf-methods generic-function))
2153 (when (and (or (equal (early-method-specializers m nil)
2155 (equal (early-method-specializers m t)
2157 (equal (early-method-qualifiers m) qualifiers))
2160 (error "can't get early method")
2162 (real-get-method generic-function qualifiers specializers errorp)))
2164 (defun !fix-early-generic-functions ()
2165 (let ((accessors nil))
2166 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2167 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2168 (dolist (early-gf-spec *!early-generic-functions*)
2169 (when (every #'early-method-standard-accessor-p
2170 (early-gf-methods (gdefinition early-gf-spec)))
2171 (push early-gf-spec accessors)))
2172 (dolist (spec (nconc accessors
2173 '(accessor-method-slot-name
2174 generic-function-methods
2179 slot-definition-location
2180 slot-definition-name
2183 class-precedence-list
2184 slot-boundp-using-class
2185 (setf slot-value-using-class)
2186 slot-value-using-class
2189 funcallable-standard-class-p
2192 (setq *!early-generic-functions*
2194 (delete spec *!early-generic-functions* :test #'equal))))
2196 (dolist (early-gf-spec *!early-generic-functions*)
2197 (/show early-gf-spec)
2198 (let* ((gf (gdefinition early-gf-spec))
2199 (methods (mapcar (lambda (early-method)
2200 (let ((args (copy-list (fifth
2203 (early-method-specializers
2205 (apply #'real-make-a-method args)))
2206 (early-gf-methods gf))))
2207 (setf (generic-function-method-class gf) *the-class-standard-method*)
2208 (setf (generic-function-method-combination gf)
2209 *standard-method-combination*)
2210 (set-methods gf methods)))
2212 (dolist (fn *!early-functions*)
2214 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2216 (dolist (fixup *!generic-function-fixups*)
2218 (let* ((fspec (car fixup))
2219 (gf (gdefinition fspec))
2220 (methods (mapcar (lambda (method)
2221 (let* ((lambda-list (first method))
2222 (specializers (second method))
2223 (method-fn-name (third method))
2224 (fn-name (or method-fn-name fspec))
2225 (fn (fdefinition fn-name))
2229 (lambda (args next-methods)
2233 `(call ,fn-name)))))
2234 (declare (type function fn))
2235 (make-a-method 'standard-method
2242 (setf (generic-function-method-class gf) *the-class-standard-method*)
2243 (setf (generic-function-method-combination gf)
2244 *standard-method-combination*)
2245 (set-methods gf methods))))
2246 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2248 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2249 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2250 ;;; is really implemented.
2251 (defun parse-defmethod (cdr-of-form)
2252 (declare (list cdr-of-form))
2253 (let ((name (pop cdr-of-form))
2256 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2257 (push (pop cdr-of-form) qualifiers)
2258 (return (setq qualifiers (nreverse qualifiers)))))
2259 (setq spec-ll (pop cdr-of-form))
2260 (values name qualifiers spec-ll cdr-of-form)))
2262 (defun parse-specializers (specializers)
2263 (declare (list specializers))
2264 (flet ((parse (spec)
2265 (let ((result (specializer-from-type spec)))
2266 (if (specializerp result)
2269 (error "~S was used as a specializer,~%~
2270 but is not the name of a class."
2272 (error "~S is not a legal specializer." spec))))))
2273 (mapcar #'parse specializers)))
2275 (defun unparse-specializers (specializers-or-method)
2276 (if (listp specializers-or-method)
2277 (flet ((unparse (spec)
2278 (if (specializerp spec)
2279 (let ((type (specializer-type spec)))
2280 (if (and (consp type)
2281 (eq (car type) 'class))
2282 (let* ((class (cadr type))
2283 (class-name (class-name class)))
2284 (if (eq class (find-class class-name nil))
2288 (error "~S is not a legal specializer." spec))))
2289 (mapcar #'unparse specializers-or-method))
2290 (unparse-specializers (method-specializers specializers-or-method))))
2292 (defun parse-method-or-spec (spec &optional (errorp t))
2293 (let (gf method name temp)
2296 gf (method-generic-function method)
2297 temp (and gf (generic-function-name gf))
2299 (make-method-spec temp
2300 (method-qualifiers method)
2301 (unparse-specializers
2302 (method-specializers method)))
2303 (make-symbol (format nil "~S" method))))
2304 (multiple-value-bind (gf-spec quals specls)
2305 (parse-defmethod spec)
2306 (and (setq gf (and (or errorp (gboundp gf-spec))
2307 (gdefinition gf-spec)))
2308 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2309 (setq specls (append (parse-specializers specls)
2310 (make-list (- nreq (length specls))
2314 (setq method (get-method gf quals specls errorp))
2317 gf-spec quals (unparse-specializers specls))))))))
2318 (values gf method name)))
2320 (defun extract-parameters (specialized-lambda-list)
2321 (multiple-value-bind (parameters ignore1 ignore2)
2322 (parse-specialized-lambda-list specialized-lambda-list)
2323 (declare (ignore ignore1 ignore2))
2326 (defun extract-lambda-list (specialized-lambda-list)
2327 (multiple-value-bind (ignore1 lambda-list ignore2)
2328 (parse-specialized-lambda-list specialized-lambda-list)
2329 (declare (ignore ignore1 ignore2))
2332 (defun extract-specializer-names (specialized-lambda-list)
2333 (multiple-value-bind (ignore1 ignore2 specializers)
2334 (parse-specialized-lambda-list specialized-lambda-list)
2335 (declare (ignore ignore1 ignore2))
2338 (defun extract-required-parameters (specialized-lambda-list)
2339 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2340 (parse-specialized-lambda-list specialized-lambda-list)
2341 (declare (ignore ignore1 ignore2 ignore3))
2342 required-parameters))
2344 (define-condition specialized-lambda-list-error
2345 (reference-condition simple-program-error)
2347 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2349 (defun parse-specialized-lambda-list
2351 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2352 &aux (specialized-lambda-list-keywords
2353 '(&optional &rest &key &allow-other-keys &aux)))
2354 (let ((arg (car arglist)))
2355 (cond ((null arglist) (values nil nil nil nil))
2357 (values nil arglist nil nil))
2358 ((memq arg lambda-list-keywords)
2359 ;; non-standard lambda-list-keywords are errors.
2360 (unless (memq arg specialized-lambda-list-keywords)
2361 (error 'specialized-lambda-list-error
2362 :format-control "unknown specialized-lambda-list ~
2364 :format-arguments (list arg)))
2365 ;; no multiple &rest x &rest bla specifying
2366 (when (memq arg supplied-keywords)
2367 (error 'specialized-lambda-list-error
2368 :format-control "multiple occurrence of ~
2369 specialized-lambda-list keyword ~S~%"
2370 :format-arguments (list arg)))
2371 ;; And no placing &key in front of &optional, either.
2372 (unless (memq arg allowed-keywords)
2373 (error 'specialized-lambda-list-error
2374 :format-control "misplaced specialized-lambda-list ~
2376 :format-arguments (list arg)))
2377 ;; When we are at a lambda-list keyword, the parameters
2378 ;; don't include the lambda-list keyword; the lambda-list
2379 ;; does include the lambda-list keyword; and no
2380 ;; specializers are allowed to follow the lambda-list
2381 ;; keywords (at least for now).
2382 (multiple-value-bind (parameters lambda-list)
2383 (parse-specialized-lambda-list (cdr arglist)
2384 (cons arg supplied-keywords)
2386 (cons '&allow-other-keys
2387 (cdr (member arg allowed-keywords)))
2388 (cdr (member arg allowed-keywords))))
2389 (when (and (eq arg '&rest)
2390 (or (null lambda-list)
2391 (memq (car lambda-list)
2392 specialized-lambda-list-keywords)
2393 (not (or (null (cadr lambda-list))
2394 (memq (cadr lambda-list)
2395 specialized-lambda-list-keywords)))))
2396 (error 'specialized-lambda-list-error
2398 "in a specialized-lambda-list, excactly one ~
2399 variable must follow &REST.~%"
2400 :format-arguments nil))
2402 (cons arg lambda-list)
2406 ;; After a lambda-list keyword there can be no specializers.
2407 (multiple-value-bind (parameters lambda-list)
2408 (parse-specialized-lambda-list (cdr arglist)
2411 (values (cons (if (listp arg) (car arg) arg) parameters)
2412 (cons arg lambda-list)
2416 (multiple-value-bind (parameters lambda-list specializers required)
2417 (parse-specialized-lambda-list (cdr arglist))
2418 (values (cons (if (listp arg) (car arg) arg) parameters)
2419 (cons (if (listp arg) (car arg) arg) lambda-list)
2420 (cons (if (listp arg) (cadr arg) t) specializers)
2421 (cons (if (listp arg) (car arg) arg) required)))))))
2423 (setq *boot-state* 'early)
2425 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2426 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2427 ;;; walker stuff was only used for implementing stuff like that; maybe
2428 ;;; it's not needed any more? Hunt down what it was used for and see.
2430 (defmacro with-slots (slots instance &body body)
2431 (let ((in (gensym)))
2432 `(let ((,in ,instance))
2433 (declare (ignorable ,in))
2434 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2437 (and (symbolp instance)
2438 `((declare (%variable-rebinding ,in ,instance)))))
2440 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2442 (if (symbolp slot-entry)
2446 (if (symbolp slot-entry)
2448 (cadr slot-entry))))
2450 (slot-value ,in ',slot-name))))
2454 (defmacro with-accessors (slots instance &body body)
2455 (let ((in (gensym)))
2456 `(let ((,in ,instance))
2457 (declare (ignorable ,in))
2458 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2461 (and (symbolp instance)
2462 `((declare (%variable-rebinding ,in ,instance)))))
2464 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2465 (let ((var-name (car slot-entry))
2466 (accessor-name (cadr slot-entry)))
2467 `(,var-name (,accessor-name ,in))))