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 (declaim (notinline make-a-method add-named-method
72 ensure-generic-function-using-class
73 add-method remove-method
))
75 (defvar *!early-functions
*
76 '((make-a-method !early-make-a-method real-make-a-method
)
77 (add-named-method !early-add-named-method real-add-named-method
)))
79 ;;; For each of the early functions, arrange to have it point to its
80 ;;; early definition. Do this in a way that makes sure that if we
81 ;;; redefine one of the early definitions the redefinition will take
82 ;;; effect. This makes development easier.
83 (dolist (fns *!early-functions
*)
84 (let ((name (car fns
))
85 (early-name (cadr fns
)))
86 (setf (gdefinition name
)
89 (apply (fdefinition early-name
) args
))
92 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
93 ;;; to convert the few functions in the bootstrap which are supposed
94 ;;; to be generic functions but can't be early on.
96 ;;; each entry is a list of name and lambda-list, class names as
97 ;;; specializers, and method body function name.
98 (defvar *!generic-function-fixups
*
100 ((generic-function method
)
101 (standard-generic-function method
)
104 ((generic-function method
)
105 (standard-generic-function method
)
108 ((generic-function qualifiers specializers
&optional
(errorp t
))
109 (standard-generic-function t t
)
111 (ensure-generic-function-using-class
112 ((generic-function fun-name
113 &key generic-function-class environment
116 real-ensure-gf-using-class--generic-function
)
117 ((generic-function fun-name
118 &key generic-function-class environment
121 real-ensure-gf-using-class--null
))
123 ((proto-generic-function proto-method lambda-expression environment
)
124 (standard-generic-function standard-method t t
)
125 real-make-method-lambda
))
126 (make-method-specializers-form
127 ((proto-generic-function proto-method specializer-names environment
)
128 (standard-generic-function standard-method t t
)
129 real-make-method-specializers-form
))
130 (parse-specializer-using-class
131 ((generic-function specializer
)
132 (standard-generic-function t
)
133 real-parse-specializer-using-class
))
134 (unparse-specializer-using-class
135 ((generic-function specializer
)
136 (standard-generic-function t
)
137 real-unparse-specializer-using-class
))
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
)))
174 (dolist (spec (cdr option
))
176 (error 'simple-program-error
177 :format-control
"~@<Invalid declaration specifier in ~
179 :format-arguments
(list spec
)))
180 (when (member (first spec
)
181 ;; FIXME: this list is slightly weird.
182 ;; ANSI (on the DEFGENERIC page) in one
183 ;; place allows only OPTIMIZE; in
184 ;; another place gives this list of
185 ;; disallowed declaration specifiers.
186 ;; This seems to be the only place where
187 ;; the FUNCTION declaration is
188 ;; mentioned; TYPE seems to be missing.
189 ;; Very strange. -- CSR, 2002-10-21
190 '(declaration ftype function
191 inline notinline special
))
192 (error 'simple-program-error
193 :format-control
"The declaration specifier ~S ~
194 is not allowed inside DEFGENERIC."
195 :format-arguments
(list spec
)))
196 (if (or (eq 'optimize
(first spec
))
197 (info :declaration
:recognized
(first spec
)))
198 (push spec
(initarg :declarations
))
199 (warn "Ignoring unrecognized declaration in DEFGENERIC: ~S"
202 (when (initarg car-option
)
203 (duplicate-option car-option
))
204 (unless (symbolp (cadr option
))
205 (error 'simple-program-error
206 :format-control
"METHOD-COMBINATION name not a ~
208 :format-arguments
(list (cadr option
))))
209 (setf (initarg car-option
)
211 (:argument-precedence-order
212 (let* ((required (nth-value 1 (parse-lambda-list lambda-list
)))
213 (supplied (cdr option
)))
214 (unless (= (length required
) (length supplied
))
215 (error 'simple-program-error
216 :format-control
"argument count discrepancy in ~
217 :ARGUMENT-PRECEDENCE-ORDER clause."
218 :format-arguments nil
))
219 (when (set-difference required supplied
)
220 (error 'simple-program-error
221 :format-control
"unequal sets for ~
222 :ARGUMENT-PRECEDENCE-ORDER clause: ~
224 :format-arguments
(list required supplied
)))
225 (setf (initarg car-option
)
227 ((:documentation
:generic-function-class
:method-class
)
228 (unless (proper-list-of-length-p option
2)
229 (error "bad list length for ~S" option
))
230 (if (initarg car-option
)
231 (duplicate-option car-option
)
232 (setf (initarg car-option
) `',(cadr option
))))
234 (push (cdr option
) methods
))
236 ;; ANSI requires that unsupported things must get a
238 (error 'simple-program-error
239 :format-control
"unsupported option ~S"
240 :format-arguments
(list option
))))))
242 (when (initarg :declarations
)
243 (setf (initarg :declarations
)
244 `',(initarg :declarations
))))
246 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
247 (compile-or-load-defgeneric ',fun-name
))
248 (load-defgeneric ',fun-name
',lambda-list
249 (sb-c:source-location
) ,@initargs
)
250 ,@(mapcar #'expand-method-definition methods
)
251 (fdefinition ',fun-name
)))))
253 (defun compile-or-load-defgeneric (fun-name)
254 (proclaim-as-fun-name fun-name
)
255 (when (typep fun-name
'(cons (eql setf
)))
256 (sb-c::warn-if-setf-macro fun-name
))
257 (note-name-defined fun-name
:function
)
258 (unless (eq (info :function
:where-from fun-name
) :declared
)
259 ;; Hmm. This is similar to BECOME-DEFINED-FUN-NAME
260 ;; except that it doesn't clear an :ASSUMED-TYPE. Should it?
261 (setf (info :function
:where-from fun-name
) :defined
)
262 (setf (info :function
:type fun-name
)
263 (specifier-type 'function
))))
265 (defun load-defgeneric (fun-name lambda-list source-location
&rest initargs
)
266 (when (fboundp fun-name
)
267 (warn 'sb-kernel
:redefinition-with-defgeneric
269 :new-location source-location
)
270 (let ((fun (fdefinition fun-name
)))
271 (when (generic-function-p fun
)
272 (loop for method in
(generic-function-initial-methods fun
)
273 do
(remove-method fun method
))
274 (setf (generic-function-initial-methods fun
) '()))))
275 (apply #'ensure-generic-function
277 :lambda-list lambda-list
278 :definition-source source-location
281 (define-condition generic-function-lambda-list-error
282 (reference-condition simple-program-error
)
284 (:default-initargs
:references
(list '(:ansi-cl
:section
(3 4 2)))))
286 (defun check-gf-lambda-list (lambda-list)
287 (flet ((verify-each-atom-or-singleton (kind args
)
288 ;; PARSE-LAMBDA-LIST validates the skeleton,
289 ;; so just check for incorrect use of defaults.
290 ;; This works for both &OPTIONAL and &KEY.
292 (or (not (listp arg
))
294 (error 'generic-function-lambda-list-error
296 "~@<invalid ~A argument specifier ~S ~_in the ~
297 generic function lambda list ~S~:>"
298 :format-arguments
(list kind arg lambda-list
))))))
299 (multiple-value-bind (llks required optional rest keys
)
302 :accept
(lambda-list-keyword-mask
303 '(&optional
&rest
&key
&allow-other-keys
))
304 :condition-class
'generic-function-lambda-list-error
305 :context
"a generic function lambda list")
306 (declare (ignore llks required rest
))
307 ;; no defaults or supplied-p vars allowed for &OPTIONAL or &KEY
308 (verify-each-atom-or-singleton '&optional optional
)
309 (verify-each-atom-or-singleton '&key keys
))))
311 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
312 ;; Kill the existing definition of DEFMETHOD which expands to DEF!METHOD.
313 ;; It's there mainly so that DEFSTRUCT's printer options can expand
314 ;; to DEFMETHOD instead of a DEF!METHOD.
315 (fmakunbound 'defmethod
))
316 (defmacro defmethod
(name &rest args
)
317 (multiple-value-bind (qualifiers lambda-list body
)
318 (parse-defmethod args)
320 (eval-when (:compile-toplevel
:execute
)
321 ;; :compile-toplevel is needed for subsequent forms
322 ;; :execute is needed for references to itself inside the body
323 (compile-or-load-defgeneric ',name
))
324 ;; KLUDGE: this double expansion is quite a monumental
325 ;; workaround: it comes about because of a fantastic interaction
326 ;; between the processing rules of CLHS 3.2.3.1 and the
327 ;; bizarreness of MAKE-METHOD-LAMBDA.
329 ;; MAKE-METHOD-LAMBDA can be called by the user, and if the
330 ;; lambda itself doesn't refer to outside bindings the return
331 ;; value must be compileable in the null lexical environment.
332 ;; However, the function must also refer somehow to the
333 ;; associated method object, so that it can call NO-NEXT-METHOD
334 ;; with the appropriate arguments if there is no next method --
335 ;; but when the function is generated, the method object doesn't
338 ;; In order to resolve this issue, we insert a literal cons cell
339 ;; into the body of the method lambda, return the same cons cell
340 ;; as part of the second (initargs) return value of
341 ;; MAKE-METHOD-LAMBDA, and a method on INITIALIZE-INSTANCE fills
342 ;; in the cell when the method is created. However, this
343 ;; strategy depends on having a fresh cons cell for every method
344 ;; lambda, which (without the workaround below) is skewered by
345 ;; the processing in CLHS 3.2.3.1, which permits implementations
346 ;; to macroexpand the bodies of EVAL-WHEN forms with both
347 ;; :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL only once. The
348 ;; expansion below forces the double expansion in those cases,
349 ;; while expanding only once in the common case.
350 (eval-when (:load-toplevel
)
351 (%defmethod-expander
,name
,qualifiers
,lambda-list
,body
))
352 (eval-when (:execute
)
353 (%defmethod-expander
,name
,qualifiers
,lambda-list
,body
)))))
355 (defmacro %defmethod-expander
356 (name qualifiers lambda-list body
&environment env
)
357 (multiple-value-bind (proto-gf proto-method
)
358 (prototypes-for-make-method-lambda name
)
359 (expand-defmethod name proto-gf proto-method qualifiers
360 lambda-list body env
)))
363 (defun prototypes-for-make-method-lambda (name)
364 (if (not (eq **boot-state
** 'complete
))
366 (let ((gf?
(and (fboundp name
)
367 (gdefinition name
))))
369 (not (generic-function-p gf?
)))
370 (values (class-prototype (find-class 'standard-generic-function
))
371 (class-prototype (find-class 'standard-method
)))
373 (class-prototype (or (generic-function-method-class gf?
)
374 (find-class 'standard-method
))))))))
376 ;;; Take a name which is either a generic function name or a list specifying
377 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
378 ;;; the prototype instance of the method-class for that generic function.
380 ;;; If there is no generic function by that name, this returns the
381 ;;; default value, the prototype instance of the class
382 ;;; STANDARD-METHOD. This default value is also returned if the spec
383 ;;; names an ordinary function or even a macro. In effect, this leaves
384 ;;; the signalling of the appropriate error until load time.
386 ;;; Note: During bootstrapping, this function is allowed to return NIL.
387 (defun method-prototype-for-gf (name)
388 (let ((gf?
(and (fboundp name
)
389 (gdefinition name
))))
390 (cond ((neq **boot-state
** 'complete
) nil
)
392 (not (generic-function-p gf?
))) ; Someone else MIGHT
393 ; error at load time.
394 (class-prototype (find-class 'standard-method
)))
396 (class-prototype (or (generic-function-method-class gf?
)
397 (find-class 'standard-method
)))))))
399 ;;; These are used to communicate the method name and lambda-list to
400 ;;; MAKE-METHOD-LAMBDA-INTERNAL.
401 (defvar *method-name
* nil
)
402 (defvar *method-lambda-list
* nil
)
404 (defun expand-defmethod (name
411 ;; ENV could be of type SB!INTERPRETER:BASIC-ENV
412 ;; but I don't care to figure out what parts of PCL
413 ;; would have to change to accept that, so coerce.
414 &aux
(env (sb-kernel:coerce-to-lexenv env
)))
415 (multiple-value-bind (parameters unspecialized-lambda-list specializers
)
416 (parse-specialized-lambda-list lambda-list
)
417 (declare (ignore parameters
))
418 (mapc (lambda (specializer)
419 (when (typep specializer
'type-specifier
)
420 (check-deprecated-type specializer
)))
422 (let ((method-lambda `(lambda ,unspecialized-lambda-list
,@body
))
423 (*method-name
* `(,name
,@qualifiers
,specializers
))
424 (*method-lambda-list
* lambda-list
))
425 (multiple-value-bind (method-function-lambda initargs
)
426 (make-method-lambda proto-gf proto-method method-lambda env
)
427 (let ((initargs-form (make-method-initargs-form
428 proto-gf proto-method method-function-lambda
430 (specializers-form (make-method-specializers-form
431 proto-gf proto-method specializers env
)))
433 ;; Note: We could DECLAIM the ftype of the generic function
434 ;; here, since ANSI specifies that we create it if it does
435 ;; not exist. However, I chose not to, because I think it's
436 ;; more useful to support a style of programming where every
437 ;; generic function has an explicit DEFGENERIC and any typos
438 ;; in DEFMETHODs are warned about. Otherwise
440 ;; (DEFGENERIC FOO-BAR-BLETCH (X))
441 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
442 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
443 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
444 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
445 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
447 ;; compiles without raising an error and runs without
448 ;; raising an error (since SIMPLE-VECTOR cases fall through
449 ;; to VECTOR) but still doesn't do what was intended. I hate
450 ;; that kind of bug (code which silently gives the wrong
451 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
452 ,(make-defmethod-form name qualifiers specializers-form
453 unspecialized-lambda-list
455 (class-name (class-of proto-method
))
459 (defun interned-symbol-p (x)
460 (and (symbolp x
) (symbol-package x
)))
462 (defun make-defmethod-form
463 (name qualifiers specializers unspecialized-lambda-list
464 method-class-name initargs-form
)
465 (declare (sb-ext:muffle-conditions sb-ext
:code-deletion-note
))
468 (if (and (interned-symbol-p (fun-name-block-name name
))
469 (every #'interned-symbol-p qualifiers
)
472 (and (eq (car s
) 'eql
)
474 (let ((sv (constant-form-value (cadr s
))))
475 (or (interned-symbol-p sv
)
478 (standard-char-p sv
)))))
479 (interned-symbol-p s
)))
481 (consp initargs-form
)
482 (eq (car initargs-form
) 'list
*)
483 (memq (cadr initargs-form
) '(:function
))
484 (consp (setq fn
(caddr initargs-form
)))
485 (eq (car fn
) 'function
)
486 (consp (setq fn-lambda
(cadr fn
)))
487 (eq (car fn-lambda
) 'lambda
)
488 (bug "Really got here"))
489 (let* ((specls (mapcar (lambda (specl)
491 ;; CONSTANT-FORM-VALUE? What I
492 ;; kind of want to know, though,
493 ;; is what happens if we don't do
494 ;; this for some slow-method
495 ;; function because of a hairy
496 ;; lexenv -- is the only bad
497 ;; effect that the method
498 ;; function ends up unnamed? If
499 ;; so, couldn't we arrange to
501 `(,(car specl
) ,(eval (cadr specl
)))
504 (mname `(,(if (eq (cadr initargs-form
) :function
)
505 'slow-method
'fast-method
)
506 ,name
,@qualifiers
,specls
)))
508 (defun ,mname
,(cadr fn-lambda
)
510 ,(make-defmethod-form-internal
511 name qualifiers
`',specls
512 unspecialized-lambda-list method-class-name
513 `(list* ,(cadr initargs-form
)
515 ,@(cdddr initargs-form
)))))
516 (make-defmethod-form-internal
520 `(list ,@(mapcar (lambda (specializer)
521 (if (consp specializer
)
522 ``(,',(car specializer
)
523 ,,(cadr specializer
))
526 unspecialized-lambda-list
530 (defun make-defmethod-form-internal
531 (name qualifiers specializers-form unspecialized-lambda-list
532 method-class-name initargs-form
)
538 ',unspecialized-lambda-list
540 (sb-c:source-location
)))
542 (defmacro make-method-function
(method-lambda &environment env
)
543 (multiple-value-bind (proto-gf proto-method
)
544 (prototypes-for-make-method-lambda nil
)
545 (multiple-value-bind (method-function-lambda initargs
)
546 (make-method-lambda proto-gf proto-method method-lambda env
)
547 (make-method-initargs-form proto-gf
549 method-function-lambda
551 ;; FIXME: coerce-to-lexenv?
554 (defun real-make-method-initargs-form (proto-gf proto-method
555 method-lambda initargs env
)
556 (declare (ignore proto-gf proto-method
))
557 (unless (and (consp method-lambda
)
558 (eq (car method-lambda
) 'lambda
))
559 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
560 is not a lambda form."
562 (make-method-initargs-form-internal method-lambda initargs env
))
564 (unless (fboundp 'make-method-initargs-form
)
565 (setf (gdefinition 'make-method-initargs-form
)
566 (symbol-function 'real-make-method-initargs-form
)))
568 ;;; When bootstrapping PCL MAKE-METHOD-LAMBDA starts out as a regular
569 ;;; functions: REAL-MAKE-METHOD-LAMBDA set to the fdefinition of
570 ;;; MAKE-METHOD-LAMBDA. Once generic functions are born, the
571 ;;; REAL-MAKE-METHOD lambda is used as the body of the default method.
572 ;;; MAKE-METHOD-LAMBDA-INTERNAL is split out into a separate function
573 ;;; so that changing it in a live image is easy, and changes actually
575 (defun real-make-method-lambda (proto-gf proto-method method-lambda env
)
576 (make-method-lambda-internal proto-gf proto-method method-lambda env
))
578 (unless (fboundp 'make-method-lambda
)
579 (setf (gdefinition 'make-method-lambda
)
580 (symbol-function 'real-make-method-lambda
)))
582 (defun declared-specials (declarations)
583 (loop for
(declare . specifiers
) in declarations
584 append
(loop for specifier in specifiers
585 when
(eq 'special
(car specifier
))
586 append
(cdr specifier
))))
588 (defun make-method-lambda-internal (proto-gf proto-method method-lambda env
)
589 (declare (ignore proto-gf proto-method
))
590 (unless (and (consp method-lambda
) (eq (car method-lambda
) 'lambda
))
591 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
592 is not a lambda form."
594 (multiple-value-bind (real-body declarations documentation
)
595 (parse-body (cddr method-lambda
) t
)
596 ;; We have the %METHOD-NAME declaration in the place where we expect it only
597 ;; if there is are no non-standard prior MAKE-METHOD-LAMBDA methods -- or
598 ;; unless they're fantastically unintrusive.
599 (let* ((method-name *method-name
*)
600 (method-lambda-list *method-lambda-list
*)
601 ;; Macroexpansion caused by code-walking may call make-method-lambda and
602 ;; end up with wrong values
604 (*method-lambda-list
* nil
)
605 (generic-function-name (when method-name
(car method-name
)))
606 (specialized-lambda-list (or method-lambda-list
607 (ecase (car method-lambda
)
608 (lambda (second method-lambda
))
609 (named-lambda (third method-lambda
)))))
610 ;; the method-cell is a way of communicating what method a
611 ;; method-function implements, for the purpose of
612 ;; NO-NEXT-METHOD. We need something that can be shared
613 ;; between function and initargs, but not something that
614 ;; will be coalesced as a constant (because we are naughty,
615 ;; oh yes) with the expansion of any other methods in the
616 ;; same file. -- CSR, 2007-05-30
617 (method-cell (list (make-symbol "METHOD-CELL"))))
618 (multiple-value-bind (parameters lambda-list specializers
)
619 (parse-specialized-lambda-list specialized-lambda-list
)
620 (let* ((required-parameters
621 (mapcar (lambda (r s
) (declare (ignore s
)) r
)
624 (slots (mapcar #'list required-parameters
))
627 ;; These declarations seem to be used by PCL to pass
628 ;; information to itself; when I tried to delete 'em
629 ;; ca. 0.6.10 it didn't work. I'm not sure how
630 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
631 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
633 (mapcar (lambda (a s
) (and (symbolp s
)
638 ;; These TYPE declarations weren't in the original
639 ;; PCL code, but the Python compiler likes them a
640 ;; lot. (We're telling the compiler about our
641 ;; knowledge of specialized argument types so that
642 ;; it can avoid run-time type dispatch overhead,
643 ;; which can be a huge win for Python.)
645 ;; KLUDGE: when I tried moving these to
646 ;; ADD-METHOD-DECLARATIONS, things broke. No idea
647 ;; why. -- CSR, 2004-06-16
648 ,@(let ((specials (declared-specials declarations
)))
649 (mapcar (lambda (par spec
)
650 (parameter-specializer-declaration-in-defmethod
651 par spec specials env
))
655 ;; Remove the documentation string and insert the
656 ;; appropriate class declarations. The documentation
657 ;; string is removed to make it easy for us to insert
658 ;; new declarations later, they will just go after the
659 ;; CADR of the method lambda. The class declarations
660 ;; are inserted to communicate the class of the method's
661 ;; arguments to the code walk.
662 `(lambda ,lambda-list
663 ;; The default ignorability of method parameters
664 ;; doesn't seem to be specified by ANSI. PCL had
665 ;; them basically ignorable but was a little
666 ;; inconsistent. E.g. even though the two
667 ;; method definitions
668 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
669 ;; (DEFMETHOD FOO ((X T) Y) "Z")
670 ;; are otherwise equivalent, PCL treated Y as
671 ;; ignorable in the first definition but not in the
672 ;; second definition. We make all required
673 ;; parameters ignorable as a way of systematizing
674 ;; the old PCL behavior. -- WHN 2000-11-24
675 (declare (ignorable ,@required-parameters
))
678 (block ,(fun-name-block-name generic-function-name
)
680 (constant-value-p (and (null (cdr real-body
))
681 (constantp (car real-body
))))
682 (constant-value (and constant-value-p
683 (constant-form-value (car real-body
))))
684 (plist (and constant-value-p
685 (or (typep constant-value
686 '(or number character
))
687 (and (symbolp constant-value
)
688 (symbol-package constant-value
)))
689 (list :constant-value constant-value
)))
690 (applyp (dolist (p lambda-list nil
)
691 (cond ((memq p
'(&optional
&rest
&key
))
695 (multiple-value-bind (walked-lambda call-next-method-p setq-p
697 (walk-method-lambda method-lambda
701 (multiple-value-bind (walked-lambda-body
703 walked-documentation
)
704 (parse-body (cddr walked-lambda
) t
)
705 (declare (ignore walked-documentation
))
706 (when (some #'cdr slots
)
707 (let ((slot-name-lists (slot-name-lists-from-slots slots
)))
709 `(,@(when slot-name-lists
710 `(:slot-name-lists
,slot-name-lists
))
712 (setq walked-lambda-body
713 `((pv-binding (,required-parameters
717 :slot-name-lists
',slot-name-lists
)))
718 ,@walked-lambda-body
)))))
719 (when (and (memq '&key lambda-list
)
720 (not (memq '&allow-other-keys lambda-list
)))
721 (let ((aux (memq '&aux lambda-list
)))
722 (setq lambda-list
(nconc (ldiff lambda-list aux
)
723 (list '&allow-other-keys
)
725 (values `(lambda (.method-args. .next-methods.
)
726 (simple-lexical-method-functions
727 (,lambda-list .method-args. .next-methods.
729 ,(when call-next-method-p t
)
731 :parameters-setqd
,parameters-setqd
732 :method-cell
,method-cell
734 ,@walked-declarations
736 (declare (disable-package-locks
737 %parameter-binding-modified
))
738 (symbol-macrolet ((%parameter-binding-modified
739 ',@parameters-setqd
))
740 (declare (enable-package-locks
741 %parameter-binding-modified
))
742 ,@walked-lambda-body
))))
743 `(,@(when call-next-method-p
`(method-cell ,method-cell
))
744 ,@(when (member call-next-method-p
'(:simple nil
))
745 '(simple-next-method-call t
))
746 ,@(when plist
`(plist ,plist
))
747 ,@(when documentation
`(:documentation
,documentation
)))))))))))
749 (defun real-make-method-specializers-form
750 (proto-gf proto-method specializer-names env
)
751 (declare (ignore env proto-gf proto-method
))
754 ((and (eq **boot-state
** 'complete
)
757 ((symbolp name
) `(find-class ',name
))
758 ((consp name
) (ecase (car name
)
759 ((eql) `(intern-eql-specializer ,(cadr name
)))
760 ((class-eq) `(class-eq-specializer (find-class ',(cadr name
))))))
762 ;; FIXME: Document CLASS-EQ specializers.
763 (error 'simple-reference-error
765 "~@<~S is not a valid parameter specializer name.~@:>"
766 :format-arguments
(list name
)
767 :references
(list '(:ansi-cl
:macro defmethod
)
768 '(:ansi-cl
:glossary
"parameter specializer name")))))))
769 `(list ,@(mapcar #'parse specializer-names
))))
771 (unless (fboundp 'make-method-specializers-form
)
772 (setf (gdefinition 'make-method-specializers-form
)
773 (symbol-function 'real-make-method-specializers-form
)))
775 (defun real-parse-specializer-using-class (generic-function specializer
)
776 (let ((result (specializer-from-type specializer
)))
777 (if (specializerp result
)
779 (error "~@<~S cannot be parsed as a specializer for ~S.~@:>"
780 specializer generic-function
))))
782 (unless (fboundp 'parse-specializer-using-class
)
783 (setf (gdefinition 'parse-specializer-using-class
)
784 (symbol-function 'real-parse-specializer-using-class
)))
786 (defun real-unparse-specializer-using-class (generic-function specializer
)
787 (if (specializerp specializer
)
788 ;; FIXME: this HANDLER-CASE is a bit of a hammer to crack a nut:
789 ;; the idea is that we want to unparse permissively, so that the
790 ;; lazy (or rather the "portable") specializer extender (who
791 ;; does not define methods on these new SBCL-specific MOP
792 ;; functions) can still subclass specializer and define methods
793 ;; without everything going wrong. Making it cleaner and
794 ;; clearer that that is what we are defending against would be
795 ;; nice. -- CSR, 2007-06-01
797 (let ((type (specializer-type specializer
)))
798 (if (and (consp type
) (eq (car type
) 'class
))
799 (let* ((class (cadr type
))
800 (class-name (class-name class
)))
801 (if (eq class
(find-class class-name nil
))
805 (error () specializer
))
806 (error "~@<~S is not a legal specializer for ~S.~@:>"
807 specializer generic-function
)))
809 (unless (fboundp 'unparse-specializer-using-class
)
810 (setf (gdefinition 'unparse-specializer-using-class
)
811 (symbol-function 'real-unparse-specializer-using-class
)))
813 ;;; a helper function for creating Python-friendly type declarations
814 ;;; in DEFMETHOD forms.
816 ;;; We're too lazy to cons up a new environment for this, so we just pass in
817 ;;; the list of locally declared specials in addition to the old environment.
818 (defun parameter-specializer-declaration-in-defmethod
819 (parameter specializer specials env
)
820 (cond ((and (consp specializer
)
821 (eq (car specializer
) 'eql
))
822 ;; KLUDGE: ANSI, in its wisdom, says that
823 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
824 ;; DEFMETHOD expansion time. Thus, although one might think
826 ;; (DEFMETHOD FOO ((X PACKAGE)
829 ;; the PACKAGE and (EQL 12) forms are both parallel type
830 ;; names, they're not, as is made clear when you do
831 ;; (DEFMETHOD FOO ((X PACKAGE)
834 ;; where Y needs to be a symbol named "BAR", not some cons
835 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
836 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
837 ;; to be of type (EQL X). It'd be easy to transform one to
838 ;; the other, but it'd be somewhat messier to do so while
839 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
840 ;; once. (The new code wouldn't be messy, but it'd require a
841 ;; big transformation of the old code.) So instead we punt.
845 ;; KLUDGE: For some low-level implementation
846 ;; classes, perhaps because of some problems related
847 ;; to the incomplete integration of PCL into SBCL's
848 ;; type system, some specializer classes can't be
849 ;; declared as argument types. E.g.
850 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
851 ;; (DECLARE (TYPE SLOT-OBJECT X))
854 ;; (DEFSTRUCT BAR A B)
856 ;; perhaps because of the way that STRUCTURE-OBJECT
857 ;; inherits both from SLOT-OBJECT and from
858 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
859 ;; problems under the rug, we exclude these problem
860 ;; cases by blacklisting them here. -- WHN 2001-01-19
861 (list 'slot-object
#+nil
(find-class 'slot-object
)))
863 ((not (eq **boot-state
** 'complete
))
864 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
865 ;; types which don't match their specializers. (Specifically,
866 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
867 ;; second argument.) Hopefully it only does this kind of
868 ;; weirdness when bootstrapping.. -- WHN 20000610
870 ((typep specializer
'eql-specializer
)
871 `(type (eql ,(eql-specializer-object specializer
)) ,parameter
))
872 ((or (var-special-p parameter env
) (member parameter specials
))
873 ;; Don't declare types for special variables -- our rebinding magic
874 ;; for SETQ cases don't work right there as SET, (SETF SYMBOL-VALUE),
875 ;; etc. make things undecidable.
878 ;; Otherwise, we can usually make Python very happy.
880 ;; KLUDGE: Since INFO doesn't work right for class objects here,
881 ;; and they are valid specializers, see if the specializer is
882 ;; a named class, and use the name in that case -- otherwise
883 ;; the class instance is ok, since info will just return NIL, NIL.
885 ;; We still need to deal with the class case too, but at
886 ;; least #.(find-class 'integer) and integer as equivalent
887 ;; specializers with this.
888 (let* ((specializer-nameoid
889 (if (and (typep specializer
'class
)
890 (let ((name (class-name specializer
)))
891 (and name
(symbolp name
)
892 (eq specializer
(find-class name nil
)))))
893 (class-name specializer
)
895 (kind (info :type
:kind specializer-nameoid
)))
897 (flet ((specializer-nameoid-class ()
898 (typecase specializer-nameoid
899 (symbol (find-class specializer-nameoid nil
))
900 (class specializer-nameoid
)
901 (class-eq-specializer
902 (specializer-class specializer-nameoid
))
905 ((:primitive
) `(type ,specializer-nameoid
,parameter
))
907 (let ((class (specializer-nameoid-class)))
908 ;; CLASS can be null here if the user has
909 ;; erroneously tried to use a defined type as a
910 ;; specializer; it can be a non-SYSTEM-CLASS if
911 ;; the user defines a type and calls (SETF
912 ;; FIND-CLASS) in a consistent way.
913 (when (and class
(typep class
'system-class
))
914 `(type ,(class-name class
) ,parameter
))))
916 (let ((class (specializer-nameoid-class)))
919 (if (typep class
'(or system-class structure-class
))
920 `(type ,class
,parameter
)
921 ;; don't declare CLOS classes as parameters;
922 ;; it's too expensive.
925 ;; we can get here, and still not have a failure
926 ;; case, by doing MOP programming like (PROGN
927 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
928 ;; ...)). Best to let the user know we haven't
929 ;; been able to extract enough information:
931 "~@<can't find type for specializer ~S in ~S.~@:>"
933 'parameter-specializer-declaration-in-defmethod
)
935 ((:forthcoming-defclass-type
)
938 ;;; For passing a list (groveled by the walker) of the required
939 ;;; parameters whose bindings are modified in the method body to the
940 ;;; optimized-slot-value* macros.
941 (define-symbol-macro %parameter-binding-modified
())
943 (defmacro simple-lexical-method-functions
((lambda-list
949 ,method-args
,next-methods
950 (bind-simple-lexical-method-functions (,method-args
,next-methods
952 (bind-args (,lambda-list
,method-args
)
955 (defmacro fast-lexical-method-functions
((lambda-list
961 `(bind-fast-lexical-method-functions (,args
,rest-arg
,next-method-call
,lmf-options
)
962 (bind-args (,(nthcdr (length args
) lambda-list
) ,rest-arg
)
965 (defmacro bind-simple-lexical-method-functions
966 ((method-args next-methods
(&key call-next-method-p setq-p
967 parameters-setqd applyp method-cell
))
970 (declare (ignore parameters-setqd
))
971 (if (not (or call-next-method-p setq-p applyp
))
972 ;; always provide the lexical function NEXT-METHOD-P.
973 ;; I would think this to be a good candidate for declaring INLINE
974 ;; but that's not the way it was done before.
975 `(flet ((next-method-p () (not (null (car ,next-methods
)))))
976 (declare (ignorable #'next-method-p
))
978 `(let ((.next-method.
(car ,next-methods
))
979 (,next-methods
(cdr ,next-methods
)))
980 (declare (ignorable .next-method.
,next-methods
))
981 (flet (,@(when call-next-method-p
982 `((call-next-method (&rest cnm-args
)
983 (declare (dynamic-extent cnm-args
))
984 ,@(if (safe-code-p env
)
985 `((%check-cnm-args cnm-args
990 (funcall (if (std-instance-p .next-method.
)
991 (method-function .next-method.
)
992 .next-method.
) ; for early methods
993 (or cnm-args
,method-args
)
995 (apply #'call-no-next-method
997 (or cnm-args
,method-args
))))))
998 (next-method-p () (not (null .next-method.
))))
999 (declare (ignorable #'next-method-p
))
1002 (defun call-no-next-method (method-cell &rest args
)
1003 (let ((method (car method-cell
)))
1005 ;; Can't easily provide a RETRY restart here, as the return value here is
1006 ;; for the method, not the generic function.
1007 (apply #'no-next-method
(method-generic-function method
)
1010 (defun call-no-applicable-method (gf args
)
1012 (apply #'no-applicable-method gf args
)
1014 :report
"Retry calling the generic function."
1017 (defun call-no-primary-method (gf args
)
1019 (apply #'no-primary-method gf args
)
1021 :report
"Retry calling the generic function."
1024 (defstruct (method-call (:copier nil
))
1025 (function #'identity
:type function
)
1027 (defstruct (constant-method-call (:copier nil
) (:include method-call
))
1030 #-sb-fluid
(declaim (sb-ext:freeze-type method-call
))
1032 (defmacro invoke-method-call1
(function args cm-args
)
1033 `(let ((.function.
,function
)
1035 (.cm-args.
,cm-args
))
1036 (if (and .cm-args.
(null (cdr .cm-args.
)))
1037 (funcall .function. .args.
(car .cm-args.
))
1038 (apply .function. .args. .cm-args.
))))
1040 (defmacro invoke-method-call
(method-call restp
&rest required-args
+rest-arg
)
1041 `(invoke-method-call1 (method-call-function ,method-call
)
1043 `(list* ,@required-args
+rest-arg
)
1044 `(list ,@required-args
+rest-arg
))
1045 (method-call-call-method-args ,method-call
)))
1047 (defstruct (fast-method-call (:copier nil
))
1048 (function #'identity
:type function
)
1052 (defstruct (constant-fast-method-call
1053 (:copier nil
) (:include fast-method-call
))
1056 #-sb-fluid
(declaim (sb-ext:freeze-type fast-method-call
))
1058 ;; The two variants of INVOKE-FAST-METHOD-CALL differ in how REST-ARGs
1059 ;; are handled. The first one will get REST-ARG as a single list (as
1060 ;; the last argument), and will thus need to use APPLY. The second one
1061 ;; will get them as a &MORE argument, so we can pass the arguments
1062 ;; directly with MULTIPLE-VALUE-CALL and %MORE-ARG-VALUES.
1064 (defmacro invoke-fast-method-call
(method-call restp
&rest required-args
+rest-arg
)
1065 `(,(if restp
'apply
'funcall
) (fast-method-call-function ,method-call
)
1066 (fast-method-call-pv ,method-call
)
1067 (fast-method-call-next-method-call ,method-call
)
1068 ,@required-args
+rest-arg
))
1070 (defmacro invoke-fast-method-call
/more
(method-call
1073 &rest required-args
)
1074 (macrolet ((generate-call (n)
1075 ``(funcall (fast-method-call-function ,method-call
)
1076 (fast-method-call-pv ,method-call
)
1077 (fast-method-call-next-method-call ,method-call
)
1079 ,@(loop for x below
,n
1080 collect
`(sb-c::%more-arg
,more-context
,x
)))))
1081 ;; The cases with only small amounts of required arguments passed
1082 ;; are probably very common, and special-casing speeds them up by
1083 ;; a factor of 2 with very little effect on the other
1084 ;; cases. Though it'd be nice to have the generic case be equally
1087 (0 ,(generate-call 0))
1088 (1 ,(generate-call 1))
1089 (t (multiple-value-call (fast-method-call-function ,method-call
)
1090 (values (fast-method-call-pv ,method-call
))
1091 (values (fast-method-call-next-method-call ,method-call
))
1093 (sb-c::%more-arg-values
,more-context
0 ,more-count
))))))
1095 (defstruct (fast-instance-boundp (:copier nil
))
1096 (index 0 :type fixnum
))
1098 #-sb-fluid
(declaim (sb-ext:freeze-type fast-instance-boundp
))
1100 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
1101 (defvar *allow-emf-call-tracing-p
* nil
)
1102 (defvar *enable-emf-call-tracing-p
* #-sb-show nil
#+sb-show t
))
1104 ;;;; effective method functions
1106 (defvar *emf-call-trace-size
* 200)
1107 (defvar *emf-call-trace
* nil
)
1108 (defvar *emf-call-trace-index
* 0)
1110 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
1111 ;;; without explanation. It appears to be intended for debugging, so
1112 ;;; it might be useful someday, so I haven't deleted it.
1113 ;;; But it isn't documented and isn't used for anything now, so
1114 ;;; I've conditionalized it out of the base system. -- WHN 19991213
1116 (defun show-emf-call-trace ()
1117 (when *emf-call-trace
*
1118 (let ((j *emf-call-trace-index
*)
1119 (*enable-emf-call-tracing-p
* nil
))
1120 (format t
"~&(The oldest entries are printed first)~%")
1121 (dotimes-fixnum (i *emf-call-trace-size
*)
1122 (let ((ct (aref *emf-call-trace
* j
)))
1123 (when ct
(print ct
)))
1125 (when (= j
*emf-call-trace-size
*)
1128 (defun trace-emf-call-internal (emf format args
)
1129 (unless *emf-call-trace
*
1130 (setq *emf-call-trace
* (make-array *emf-call-trace-size
*)))
1131 (setf (aref *emf-call-trace
* *emf-call-trace-index
*)
1132 (list* emf format args
))
1133 (incf *emf-call-trace-index
*)
1134 (when (= *emf-call-trace-index
* *emf-call-trace-size
*)
1135 (setq *emf-call-trace-index
* 0)))
1137 (defmacro trace-emf-call
(emf format args
)
1138 (when *allow-emf-call-tracing-p
*
1139 `(when *enable-emf-call-tracing-p
*
1140 (trace-emf-call-internal ,emf
,format
,args
))))
1142 (defmacro invoke-effective-method-function-fast
1143 (emf restp
&key required-args rest-arg more-arg
)
1145 (trace-emf-call ,emf
,restp
(list ,@required-args rest-arg
))
1147 `(invoke-fast-method-call/more
,emf
1150 `(invoke-fast-method-call ,emf
1155 (defun effective-method-optimized-slot-access-clause
1156 (emf restp required-args
)
1157 ;; "What," you may wonder, "do these next two clauses do?" In that
1158 ;; case, you are not a PCL implementor, for they considered this to
1159 ;; be self-documenting.:-| Or CSR, for that matter, since he can
1160 ;; also figure it out by looking at it without breaking stride. For
1161 ;; the rest of us, though: From what the code is doing with .SLOTS.
1162 ;; and whatnot, evidently it's implementing SLOT-VALUEish and
1163 ;; GET-SLOT-VALUEish things. Then we can reason backwards and
1164 ;; conclude that setting EMF to a FIXNUM is an optimized way to
1165 ;; represent these slot access operations.
1167 (let ((length (length required-args
)))
1170 (let* ((.slots.
(get-slots-or-nil
1171 ,(car required-args
)))
1172 (value (when .slots.
(clos-slots-ref .slots.
,emf
))))
1173 (if (eq value
+slot-unbound
+)
1174 (slot-unbound-internal ,(car required-args
)
1179 (let ((.new-value.
,(car required-args
))
1180 (.slots.
(get-slots-or-nil
1181 ,(cadr required-args
))))
1183 (setf (clos-slots-ref .slots.
,emf
) .new-value.
)))))))
1184 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
1185 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
1186 ;; there was no explanation and presumably the code is 10+
1187 ;; years stale, I simply deleted it. -- WHN)
1190 ;;; Before SBCL 0.9.16.7 instead of
1191 ;;; INVOKE-NARROW-EFFECTIVE-METHOD-FUNCTION we passed a (THE (OR
1192 ;;; FUNCTION METHOD-CALL FAST-METHOD-CALL) EMF) form as the EMF. Now,
1193 ;;; to make less work for the compiler we take a path that doesn't
1194 ;;; involve the slot-accessor clause (where EMF is a FIXNUM) at all.
1195 (macrolet ((def (name &optional narrow
)
1196 `(defmacro ,name
(emf restp
&key required-args rest-arg more-arg
)
1197 (unless (constantp restp
)
1198 (error "The RESTP argument is not constant."))
1199 (setq restp
(constant-form-value restp
))
1200 (with-unique-names (emf-n)
1202 (declare (optimize (sb-c:insert-step-conditions
0)))
1203 (let ((,emf-n
,emf
))
1204 (trace-emf-call ,emf-n
,restp
(list ,@required-args
,@rest-arg
))
1208 `(invoke-fast-method-call/more
,emf-n
1211 `(invoke-fast-method-call ,emf-n
1216 `(effective-method-optimized-slot-access-clause
1217 emf-n restp required-args
))
1219 (invoke-method-call ,emf-n
,restp
,@required-args
1223 `(apply ,emf-n
,@required-args
,@rest-arg
)
1224 `(funcall ,emf-n
,@required-args
1225 ,@rest-arg
))))))))))
1226 (def invoke-effective-method-function nil
)
1227 (def invoke-narrow-effective-method-function t
))
1229 (defun invoke-emf (emf args
)
1230 (trace-emf-call emf t args
)
1233 (let* ((arg-info (fast-method-call-arg-info emf
))
1234 (restp (cdr arg-info
))
1235 (nreq (car arg-info
)))
1237 (apply (fast-method-call-function emf
)
1238 (fast-method-call-pv emf
)
1239 (fast-method-call-next-method-call emf
)
1243 (invoke-fast-method-call emf nil
)
1244 (error 'simple-program-error
1245 :format-control
"invalid number of arguments: 0"
1246 :format-arguments nil
)))
1249 (invoke-fast-method-call emf nil
(car args
))
1250 (error 'simple-program-error
1251 :format-control
"invalid number of arguments: 1"
1252 :format-arguments nil
)))
1255 (invoke-fast-method-call emf nil
(car args
) (cadr args
))
1256 (error 'simple-program-error
1257 :format-control
"invalid number of arguments: 2"
1258 :format-arguments nil
)))
1260 (apply (fast-method-call-function emf
)
1261 (fast-method-call-pv emf
)
1262 (fast-method-call-next-method-call emf
)
1265 (apply (method-call-function emf
)
1267 (method-call-call-method-args emf
)))
1270 (error 'simple-program-error
1271 :format-control
"invalid number of arguments: 0"
1272 :format-arguments nil
))
1274 (let* ((slots (get-slots (car args
)))
1275 (value (clos-slots-ref slots emf
)))
1276 (if (eq value
+slot-unbound
+)
1277 (slot-unbound-internal (car args
) emf
)
1280 (setf (clos-slots-ref (get-slots (cadr args
)) emf
)
1282 (t (error 'simple-program-error
1283 :format-control
"invalid number of arguments"
1284 :format-arguments nil
))))
1285 (fast-instance-boundp
1286 (if (or (null args
) (cdr args
))
1287 (error 'simple-program-error
1288 :format-control
"invalid number of arguments"
1289 :format-arguments nil
)
1290 (let ((slots (get-slots (car args
))))
1291 (not (eq (clos-slots-ref slots
(fast-instance-boundp-index emf
))
1297 (defmacro fast-call-next-method-body
((args next-method-call rest-arg
)
1300 `(if ,next-method-call
1301 ,(let ((call `(invoke-narrow-effective-method-function
1303 ,(not (null rest-arg
))
1304 :required-args
,args
1305 :rest-arg
,(when rest-arg
(list rest-arg
)))))
1309 `(&rest
,rest-arg
)))
1313 (call-no-next-method ',method-cell
1318 (defmacro bind-fast-lexical-method-functions
1319 ((args rest-arg next-method-call
(&key
1327 (let* ((next-method-p-def
1329 (declare (optimize (sb-c:insert-step-conditions
0)))
1330 (not (null ,next-method-call
)))))
1331 (rebindings (when (or setq-p call-next-method-p
)
1332 (mapcar (lambda (x) (list x x
)) parameters-setqd
))))
1333 (if (not (or call-next-method-p setq-p applyp
))
1334 `(flet ,next-method-p-def
1335 (declare (ignorable #'next-method-p
))
1337 `(flet (,@(when call-next-method-p
1338 `((call-next-method (&rest cnm-args
)
1339 (declare (dynamic-extent cnm-args
)
1340 (muffle-conditions code-deletion-note
)
1341 (optimize (sb-c:insert-step-conditions
0)))
1342 ,@(if (safe-code-p env
)
1343 `((%check-cnm-args cnm-args
(list ,@args
)
1346 (fast-call-next-method-body (,args
1351 ,@next-method-p-def
)
1352 (declare (ignorable #'next-method-p
))
1356 ;;; CMUCL comment (Gerd Moellmann):
1358 ;;; The standard says it's an error if CALL-NEXT-METHOD is called with
1359 ;;; arguments, and the set of methods applicable to those arguments is
1360 ;;; different from the set of methods applicable to the original
1361 ;;; method arguments. (According to Barry Margolin, this rule was
1362 ;;; probably added to ensure that before and around methods are always
1363 ;;; run before primary methods.)
1365 ;;; This could be optimized for the case that the generic function
1366 ;;; doesn't have hairy methods, does have standard method combination,
1367 ;;; is a standard generic function, there are no methods defined on it
1368 ;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such
1369 ;;; preconditions. That looks hairy and is probably not worth it,
1370 ;;; because this check will never be fast.
1371 (defun %check-cnm-args
(cnm-args orig-args method-cell
)
1372 ;; 1. Check for no arguments.
1374 (let* ((gf (method-generic-function (car method-cell
)))
1375 (nreq (generic-function-nreq gf
)))
1376 (declare (fixnum nreq
))
1377 ;; 2. Requirement arguments pairwise: if all are EQL, the applicable
1378 ;; methods must be the same. This takes care of the relatively common
1379 ;; case of twiddling with &KEY arguments without being horribly
1381 (unless (do ((orig orig-args
(cdr orig
))
1382 (args cnm-args
(cdr args
))
1385 (unless (and orig args
(eql (car orig
) (car args
)))
1387 ;; 3. Only then do the full check.
1388 (let ((omethods (compute-applicable-methods gf orig-args
))
1389 (nmethods (compute-applicable-methods gf cnm-args
)))
1390 (unless (equal omethods nmethods
)
1391 (error "~@<The set of methods ~S applicable to argument~P ~
1392 ~{~S~^, ~} to call-next-method is different from ~
1393 the set of methods ~S applicable to the original ~
1394 method argument~P ~{~S~^, ~}.~@:>"
1395 nmethods
(length cnm-args
) cnm-args omethods
1396 (length orig-args
) orig-args
)))))))
1398 ;; FIXME: replacing this entire mess with DESTRUCTURING-BIND would correct
1399 ;; problems similar to those already solved by a correct implementation
1400 ;; of DESTRUCTURING-BIND, such as incorrect binding order:
1401 ;; e.g. (macroexpand-1 '(bind-args ((&optional (x nil xsp)) args) (form)))
1402 ;; -> (LET* ((.ARGS-TAIL. ARGS) (XSP (NOT (NULL .ARGS-TAIL.))) (X ...)))
1403 ;; It's mostly irrelevant unless a method uses CALL-NEXT-METHOD though.
1404 (defmacro bind-args
((lambda-list args
) &body body
)
1405 (let ((args-tail '.args-tail.
)
1408 (flet ((process-var (var)
1409 (if (memq var lambda-list-keywords
)
1412 (&optional
(setq state
'optional
))
1413 (&key
(setq state
'key
))
1415 (&rest
(setq state
'rest
))
1416 (&aux
(setq state
'aux
))
1419 "encountered the non-standard lambda list keyword ~S"
1423 (required `((,var
(pop ,args-tail
))))
1424 (optional (cond ((not (consp var
))
1425 `((,var
(when ,args-tail
1426 (pop ,args-tail
)))))
1428 `((,(car var
) (if ,args-tail
1432 `((,(caddr var
) (not (null ,args-tail
)))
1433 (,(car var
) (if ,args-tail
1436 (rest `((,var
,args-tail
)))
1437 (key (cond ((not (consp var
))
1439 (get-key-arg-tail ,(keywordicate var
)
1442 (multiple-value-bind (keyword variable
)
1443 (if (consp (car var
))
1446 (values (keywordicate (car var
))
1448 `((,key
(get-key-arg-tail ',keyword
1454 (multiple-value-bind (keyword variable
)
1455 (if (consp (car var
))
1458 (values (keywordicate (car var
))
1460 `((,key
(get-key-arg-tail ',keyword
1462 (,(caddr var
) (not (null,key
)))
1467 (let ((bindings (mapcan #'process-var lambda-list
)))
1468 `(let* ((,args-tail
,args
)
1471 ,@(when (eq state
'optional
)
1472 `((unless (null ,args-tail
)
1473 (error 'simple-program-error
1474 :format-control
"surplus arguments: ~S"
1475 :format-arguments
(list ,args-tail
)))))))
1476 (declare (ignorable ,args-tail .dummy0.
))
1479 (defun get-key-arg-tail (keyword list
)
1480 (loop for
(key . tail
) on list by
#'cddr
1482 ;; FIXME: Do we want to export this symbol? Or maybe use an
1483 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1484 (sb-c::%odd-key-args-error
)
1485 when
(eq key keyword
)
1488 (defun walk-method-lambda (method-lambda required-parameters env slots
)
1489 (let (;; flag indicating that CALL-NEXT-METHOD should be in the
1490 ;; method definition
1491 (call-next-method-p nil
)
1492 ;; a list of all required parameters whose bindings might be
1493 ;; modified in the method body.
1494 (parameters-setqd nil
))
1495 (flet ((walk-function (form context env
)
1496 (unless (and (eq context
:eval
) (consp form
))
1497 (return-from walk-function form
))
1500 ;; hierarchy: nil -> :simple -> T.
1501 (unless (eq call-next-method-p t
)
1502 (setq call-next-method-p
(if (cdr form
) t
:simple
)))
1504 ((setq multiple-value-setq
)
1505 ;; The walker will split (SETQ A 1 B 2) to
1506 ;; separate (SETQ A 1) and (SETQ B 2) forms, so we
1507 ;; only need to handle the simple case of SETQ
1509 (let ((vars (if (eq (car form
) 'setq
)
1510 (list (second form
))
1513 ;; Note that we don't need to check for
1514 ;; %VARIABLE-REBINDING declarations like is
1515 ;; done in CAN-OPTIMIZE-ACCESS1, since the
1516 ;; bindings that will have that declation will
1518 (when (var-declaration '%class var env
)
1519 ;; If a parameter binding is shadowed by
1520 ;; another binding it won't have a %CLASS
1521 ;; declaration anymore, and this won't get
1523 (pushnew var parameters-setqd
:test
#'eq
))))
1526 (when (equal (cdr form
) '(call-next-method))
1527 (setq call-next-method-p t
))
1529 ((slot-value set-slot-value slot-boundp
)
1530 (if (constantp (third form
) env
)
1531 (let ((fun (ecase (car form
)
1532 (slot-value #'optimize-slot-value
)
1533 (set-slot-value #'optimize-set-slot-value
)
1534 (slot-boundp #'optimize-slot-boundp
))))
1535 (funcall fun form slots required-parameters env
))
1539 (let ((walked-lambda (walk-form method-lambda env
#'walk-function
)))
1540 ;;; FIXME: the walker's rewriting of the source code causes
1541 ;;; trouble when doing code coverage. The rewrites should be
1542 ;;; removed, and the same operations done using
1543 ;;; compiler-macros or tranforms.
1544 (values (if (sb-c:policy env
(= sb-c
:store-coverage-data
0))
1548 (not (null parameters-setqd
))
1549 parameters-setqd
)))))
1551 (defun generic-function-name-p (name)
1552 (and (legal-fun-name-p name
)
1554 (if (eq **boot-state
** 'complete
)
1555 (standard-generic-function-p (gdefinition name
))
1556 (funcallable-instance-p (gdefinition name
)))))
1558 (defun method-plist-value (method key
&optional default
)
1559 (let ((plist (if (consp method
)
1560 (getf (early-method-initargs method
) 'plist
)
1561 (object-plist method
))))
1562 (getf plist key default
)))
1564 (defun (setf method-plist-value
) (new-value method key
&optional default
)
1566 (setf (getf (getf (early-method-initargs method
) 'plist
) key default
)
1568 (setf (getf (object-plist method
) key default
) new-value
)))
1570 (defun load-defmethod (class name quals specls ll initargs source-location
)
1571 (let ((method-cell (getf initargs
'method-cell
)))
1572 (setq initargs
(copy-tree initargs
))
1574 (setf (getf initargs
'method-cell
) method-cell
))
1576 (setf (getf (getf initargs
'plist
) :name
)
1577 (make-method-spec name quals specls
))
1578 (load-defmethod-internal class name quals specls
1579 ll initargs source-location
)))
1581 (defun load-defmethod-internal
1582 (method-class gf-spec qualifiers specializers lambda-list
1583 initargs source-location
)
1584 (when (and (eq **boot-state
** 'complete
)
1586 (let* ((gf (fdefinition gf-spec
))
1587 (method (and (generic-function-p gf
)
1588 (generic-function-methods gf
)
1589 (find-method gf qualifiers specializers nil
))))
1591 (warn 'sb-kernel
:redefinition-with-defmethod
1593 :new-location source-location
1595 :qualifiers qualifiers
:specializers specializers
))))
1596 (let ((method (apply #'add-named-method
1597 gf-spec qualifiers specializers lambda-list
1598 :definition-source source-location
1600 (unless (or (eq method-class
'standard-method
)
1601 (eq (find-class method-class nil
) (class-of method
)))
1602 ;; FIXME: should be STYLE-WARNING?
1603 (format *error-output
*
1604 "~&At the time the method with qualifiers ~:S and~%~
1605 specializers ~:S on the generic function ~S~%~
1606 was compiled, the method-class for that generic function was~%~
1607 ~S. But, the method class is now ~S, this~%~
1608 may mean that this method was compiled improperly.~%"
1609 qualifiers specializers gf-spec
1610 method-class
(class-name (class-of method
))))
1613 (defun make-method-spec (gf qualifiers specializers
)
1614 (let ((name (generic-function-name gf
))
1615 (unparsed-specializers (unparse-specializers gf specializers
)))
1616 `(slow-method ,name
,@qualifiers
,unparsed-specializers
)))
1618 (defun initialize-method-function (initargs method
)
1619 (let* ((mf (getf initargs
:function
))
1620 (mff (and (typep mf
'%method-function
)
1621 (%method-function-fast-function mf
)))
1622 (plist (getf initargs
'plist
))
1623 (name (getf plist
:name
))
1624 (method-cell (getf initargs
'method-cell
)))
1626 (setf (car method-cell
) method
))
1629 (setq mf
(set-fun-name mf name
)))
1630 (when (and mff
(consp name
) (eq (car name
) 'slow-method
))
1631 (let ((fast-name `(fast-method ,@(cdr name
))))
1632 (set-fun-name mff fast-name
))))
1634 (let ((plist plist
))
1635 (let ((snl (getf plist
:slot-name-lists
)))
1637 (setf (method-plist-value method
:pv-table
)
1638 (intern-pv-table :slot-name-lists snl
))))))))
1640 (defun analyze-lambda-list (lambda-list)
1641 (multiple-value-bind (llks required optional rest keywords
)
1642 ;; We say "&MUMBLE is not allowed in a generic function lambda list"
1643 ;; whether this is called by DEFMETHOD or DEFGENERIC.
1644 ;; [It is used for either. Why else recognize and silently ignore &AUX?]
1645 (parse-lambda-list lambda-list
1646 :accept
(lambda-list-keyword-mask
1647 '(&optional
&rest
&key
&allow-other-keys
&aux
))
1649 :context
"a generic function lambda list")
1650 (declare (ignore rest
))
1651 (values llks
(length required
) (length optional
)
1652 (mapcar #'parse-key-arg-spec keywords
) keywords
)))
1654 ;; FIXME: this does more than return an FTYPE from a lambda list -
1655 ;; it unions the type with an existing ctype object. It needs a better name,
1656 ;; and to be reimplemented as "union and call sb-c::ftype-from-lambda-list".
1657 (defun ftype-declaration-from-lambda-list (lambda-list name
)
1658 (multiple-value-bind (llks nrequired noptional keywords keyword-parameters
)
1659 (analyze-lambda-list lambda-list
)
1660 (declare (ignore keyword-parameters
))
1661 (let* ((old (proclaimed-ftype name
)) ;FIXME:FDOCUMENTATION instead?
1662 (old-ftype (if (fun-type-p old
) old nil
))
1663 (old-restp (and old-ftype
(fun-type-rest old-ftype
)))
1664 (old-keys (and old-ftype
1665 (mapcar #'key-info-name
1668 (old-keysp (and old-ftype
(fun-type-keyp old-ftype
)))
1669 (old-allowp (and old-ftype
1670 (fun-type-allowp old-ftype
)))
1671 (keywords (union old-keys
(mapcar #'parse-key-arg-spec keywords
))))
1672 `(function ,(append (make-list nrequired
:initial-element t
)
1673 (when (plusp noptional
)
1674 (append '(&optional
)
1675 (make-list noptional
:initial-element t
)))
1676 (when (or (ll-kwds-restp llks
) old-restp
)
1678 (when (or (ll-kwds-keyp llks
) old-keysp
)
1680 (mapcar (lambda (key)
1683 (when (or (ll-kwds-allowp llks
) old-allowp
)
1684 '(&allow-other-keys
)))))
1687 ;;;; early generic function support
1689 (defvar *!early-generic-functions
* ())
1691 ;; CLHS doesn't specify &allow-other-keys here but I guess the supposition
1692 ;; is that they'll be checked by ENSURE-GENERIC-FUNCTION-USING-CLASS.
1693 ;; Except we don't do that either, so I think the blame, if any, lies there
1694 ;; for not catching errant keywords.
1695 (defun ensure-generic-function (fun-name &rest all-keys
)
1696 (let ((existing (and (fboundp fun-name
)
1697 (gdefinition fun-name
))))
1698 (cond ((and existing
1699 (eq **boot-state
** 'complete
)
1700 (null (generic-function-p existing
)))
1701 (generic-clobbers-function fun-name
)
1702 (fmakunbound fun-name
)
1703 (apply #'ensure-generic-function fun-name all-keys
))
1705 (apply #'ensure-generic-function-using-class
1706 existing fun-name all-keys
)))))
1708 (defun generic-clobbers-function (fun-name)
1709 (cerror "Replace the function binding"
1710 'simple-program-error
1711 :format-control
"~S already names an ordinary function or a macro."
1712 :format-arguments
(list fun-name
)))
1714 (defvar *sgf-wrapper
*
1715 (!boot-make-wrapper
(early-class-size 'standard-generic-function
)
1716 'standard-generic-function
))
1718 (defvar *sgf-slots-init
*
1719 (mapcar (lambda (canonical-slot)
1720 (if (memq (getf canonical-slot
:name
) '(arg-info source
))
1722 (let ((initfunction (getf canonical-slot
:initfunction
)))
1724 (funcall initfunction
)
1726 (early-collect-inheritance 'standard-generic-function
)))
1728 (defconstant +sgf-method-class-index
+
1729 (!bootstrap-slot-index
'standard-generic-function
'method-class
))
1731 (defun early-gf-p (x)
1732 (and (fsc-instance-p x
)
1733 (eq (clos-slots-ref (get-slots x
) +sgf-method-class-index
+)
1736 (defconstant +sgf-methods-index
+
1737 (!bootstrap-slot-index
'standard-generic-function
'methods
))
1739 (defmacro early-gf-methods
(gf)
1740 `(clos-slots-ref (get-slots ,gf
) +sgf-methods-index
+))
1742 (defun safe-generic-function-methods (generic-function)
1743 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
1744 (clos-slots-ref (get-slots generic-function
) +sgf-methods-index
+)
1745 (generic-function-methods generic-function
)))
1747 (defconstant +sgf-arg-info-index
+
1748 (!bootstrap-slot-index
'standard-generic-function
'arg-info
))
1750 (defmacro early-gf-arg-info
(gf)
1751 `(clos-slots-ref (get-slots ,gf
) +sgf-arg-info-index
+))
1753 (defconstant +sgf-dfun-state-index
+
1754 (!bootstrap-slot-index
'standard-generic-function
'dfun-state
))
1756 (defstruct (arg-info
1758 (:constructor make-arg-info
())
1760 (arg-info-lambda-list :no-lambda-list
)
1763 arg-info-number-optional
1765 arg-info-keys
;nil no &KEY or &REST allowed
1766 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1767 ;T must have &KEY or &REST
1769 gf-info-simple-accessor-type
; nil, reader, writer, boundp
1770 (gf-precompute-dfun-and-emf-p nil
) ; set by set-arg-info
1772 gf-info-static-c-a-m-emf
1773 (gf-info-c-a-m-emf-std-p t
)
1776 #-sb-fluid
(declaim (sb-ext:freeze-type arg-info
))
1778 (defun arg-info-valid-p (arg-info)
1779 (not (null (arg-info-number-optional arg-info
))))
1781 (defun arg-info-applyp (arg-info)
1782 (or (plusp (arg-info-number-optional arg-info
))
1783 (arg-info-key/rest-p arg-info
)))
1785 (defun arg-info-number-required (arg-info)
1786 (length (arg-info-metatypes arg-info
)))
1788 (defun arg-info-nkeys (arg-info)
1789 (count-if (lambda (x) (neq x t
)) (arg-info-metatypes arg-info
)))
1791 (defun create-gf-lambda-list (lambda-list)
1792 ;;; Create a gf lambda list from a method lambda list
1793 (loop for x in lambda-list
1794 collect
(if (consp x
) (list (car x
)) x
)
1795 if
(eq x
'&key
) do
(loop-finish)))
1797 (defun ll-keyp-or-restp (bits)
1798 (logtest (lambda-list-keyword-mask '(&key
&rest
)) bits
))
1800 (defun set-arg-info (gf &key new-method
(lambda-list nil lambda-list-p
)
1801 argument-precedence-order
)
1802 (let* ((arg-info (if (eq **boot-state
** 'complete
)
1804 (early-gf-arg-info gf
)))
1805 (methods (if (eq **boot-state
** 'complete
)
1806 (generic-function-methods gf
)
1807 (early-gf-methods gf
)))
1808 (was-valid-p (integerp (arg-info-number-optional arg-info
)))
1809 (first-p (and new-method
(null (cdr methods
)))))
1810 (when (and (not lambda-list-p
) methods
)
1811 (setq lambda-list
(gf-lambda-list gf
)))
1812 (when (or lambda-list-p
1814 (eq (arg-info-lambda-list arg-info
) :no-lambda-list
)))
1815 (multiple-value-bind (llks nreq nopt keywords
)
1816 (analyze-lambda-list lambda-list
)
1817 (when (and methods
(not first-p
))
1818 (let ((gf-nreq (arg-info-number-required arg-info
))
1819 (gf-nopt (arg-info-number-optional arg-info
))
1820 (gf-key/rest-p
(arg-info-key/rest-p arg-info
)))
1821 (unless (and (= nreq gf-nreq
)
1823 (eq (ll-keyp-or-restp llks
) gf-key
/rest-p
))
1824 (error "The lambda-list ~S is incompatible with ~
1825 existing methods of ~S."
1827 (setf (arg-info-lambda-list arg-info
)
1830 (create-gf-lambda-list lambda-list
)))
1831 (when (or lambda-list-p argument-precedence-order
1832 (null (arg-info-precedence arg-info
)))
1833 (setf (arg-info-precedence arg-info
)
1834 (compute-precedence lambda-list nreq argument-precedence-order
)))
1835 (setf (arg-info-metatypes arg-info
) (make-list nreq
))
1836 (setf (arg-info-number-optional arg-info
) nopt
)
1837 (setf (arg-info-key/rest-p arg-info
) (ll-keyp-or-restp llks
))
1838 (setf (arg-info-keys arg-info
)
1840 (if (ll-kwds-allowp llks
) t keywords
)
1841 (arg-info-key/rest-p arg-info
)))))
1843 (check-method-arg-info gf arg-info new-method
))
1844 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p
)
1847 (defun check-method-arg-info (gf arg-info method
)
1848 (multiple-value-bind (llks nreq nopt keywords
)
1849 (analyze-lambda-list (if (consp method
)
1850 (early-method-lambda-list method
)
1851 (method-lambda-list method
)))
1852 (flet ((lose (string &rest args
)
1853 (error 'simple-program-error
1854 :format-control
"~@<attempt to add the method~2I~_~S~I~_~
1855 to the generic function~2I~_~S;~I~_~
1857 :format-arguments
(list method gf string args
)))
1858 (comparison-description (x y
)
1859 (if (> x y
) "more" "fewer")))
1860 (let ((gf-nreq (arg-info-number-required arg-info
))
1861 (gf-nopt (arg-info-number-optional arg-info
))
1862 (gf-key/rest-p
(arg-info-key/rest-p arg-info
))
1863 (gf-keywords (arg-info-keys arg-info
)))
1864 (unless (= nreq gf-nreq
)
1866 "the method has ~A required arguments than the generic function."
1867 (comparison-description nreq gf-nreq
)))
1868 (unless (= nopt gf-nopt
)
1870 "the method has ~A optional arguments than the generic function."
1871 (comparison-description nopt gf-nopt
)))
1872 (unless (eq (ll-keyp-or-restp llks
) gf-key
/rest-p
)
1874 "the method and generic function differ in whether they accept~_~
1875 &REST or &KEY arguments."))
1876 (when (consp gf-keywords
)
1877 (unless (or (and (ll-kwds-restp llks
) (not (ll-kwds-keyp llks
)))
1878 (ll-kwds-allowp llks
)
1879 (every (lambda (k) (memq k keywords
)) gf-keywords
))
1880 (lose "the method does not accept each of the &KEY arguments~2I~_~
1884 (defconstant +sm-specializers-index
+
1885 (!bootstrap-slot-index
'standard-method
'specializers
))
1886 (defconstant +sm-%function-index
+
1887 (!bootstrap-slot-index
'standard-method
'%function
))
1888 (defconstant +sm-qualifiers-index
+
1889 (!bootstrap-slot-index
'standard-method
'qualifiers
))
1891 ;;; FIXME: we don't actually need this; we could test for the exact
1892 ;;; class and deal with it as appropriate. In fact we probably don't
1893 ;;; need it anyway because we only use this for METHOD-SPECIALIZERS on
1894 ;;; the standard reader method for METHOD-SPECIALIZERS. Probably.
1895 (dolist (s '(specializers %function
))
1896 (aver (= (symbol-value (intern (format nil
"+SM-~A-INDEX+" s
)))
1897 (!bootstrap-slot-index
'standard-reader-method s
)
1898 (!bootstrap-slot-index
'standard-writer-method s
)
1899 (!bootstrap-slot-index
'standard-boundp-method s
)
1900 (!bootstrap-slot-index
'global-reader-method s
)
1901 (!bootstrap-slot-index
'global-writer-method s
)
1902 (!bootstrap-slot-index
'global-boundp-method s
))))
1904 (defvar *standard-method-class-names
*
1905 '(standard-method standard-reader-method
1906 standard-writer-method standard-boundp-method
1907 global-reader-method global-writer-method
1908 global-boundp-method
))
1910 (declaim (list **standard-method-classes
**))
1911 (defglobal **standard-method-classes
** nil
)
1913 (defun safe-method-specializers (method)
1914 (if (member (class-of method
) **standard-method-classes
** :test
#'eq
)
1915 (clos-slots-ref (std-instance-slots method
) +sm-specializers-index
+)
1916 (method-specializers method
)))
1917 (defun safe-method-fast-function (method)
1918 (let ((mf (safe-method-function method
)))
1919 (and (typep mf
'%method-function
)
1920 (%method-function-fast-function mf
))))
1921 (defun safe-method-function (method)
1922 (if (member (class-of method
) **standard-method-classes
** :test
#'eq
)
1923 (clos-slots-ref (std-instance-slots method
) +sm-%function-index
+)
1924 (method-function method
)))
1925 (defun safe-method-qualifiers (method)
1926 (if (member (class-of method
) **standard-method-classes
** :test
#'eq
)
1927 (clos-slots-ref (std-instance-slots method
) +sm-qualifiers-index
+)
1928 (method-qualifiers method
)))
1930 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p
)
1931 (let* ((existing-p (and methods
(cdr methods
) new-method
))
1932 (nreq (length (arg-info-metatypes arg-info
)))
1933 (metatypes (if existing-p
1934 (arg-info-metatypes arg-info
)
1936 (type (if existing-p
1937 (gf-info-simple-accessor-type arg-info
)
1939 (when (arg-info-valid-p arg-info
)
1940 (dolist (method (if new-method
(list new-method
) methods
))
1941 (let* ((specializers (if (or (eq **boot-state
** 'complete
)
1942 (not (consp method
)))
1943 (safe-method-specializers method
)
1944 (early-method-specializers method t
)))
1945 (class (if (or (eq **boot-state
** 'complete
) (not (consp method
)))
1947 (early-method-class method
)))
1950 (or (not (eq **boot-state
** 'complete
))
1951 (eq (generic-function-method-combination gf
)
1952 *standard-method-combination
*)))
1953 (cond ((or (eq class
*the-class-standard-reader-method
*)
1954 (eq class
*the-class-global-reader-method
*))
1956 ((or (eq class
*the-class-standard-writer-method
*)
1957 (eq class
*the-class-global-writer-method
*))
1959 ((or (eq class
*the-class-standard-boundp-method
*)
1960 (eq class
*the-class-global-boundp-method
*))
1962 (setq metatypes
(mapcar #'raise-metatype metatypes specializers
))
1963 (setq type
(cond ((null type
) new-type
)
1964 ((eq type new-type
) type
)
1966 (setf (arg-info-metatypes arg-info
) metatypes
)
1967 (setf (gf-info-simple-accessor-type arg-info
) type
)))
1968 (when (or (not was-valid-p
) first-p
)
1969 (multiple-value-bind (c-a-m-emf std-p
)
1972 (compute-applicable-methods-emf gf
))
1973 (setf (gf-info-static-c-a-m-emf arg-info
) c-a-m-emf
)
1974 (setf (gf-info-c-a-m-emf-std-p arg-info
) std-p
)
1975 (unless (gf-info-c-a-m-emf-std-p arg-info
)
1976 (setf (gf-info-simple-accessor-type arg-info
) t
))))
1978 (let ((name (if (eq **boot-state
** 'complete
)
1979 (generic-function-name gf
)
1980 (!early-gf-name gf
))))
1981 (setf (gf-precompute-dfun-and-emf-p arg-info
)
1985 *internal-pcl-generalized-fun-name-symbols
*))
1987 (t (let* ((symbol (fun-name-block-name name
))
1988 (package (symbol-package symbol
)))
1989 (and (or (eq package
*pcl-package
*)
1990 (memq package
(package-use-list *pcl-package
*)))
1991 (not (eq package
*cl-package
*))
1992 ;; FIXME: this test will eventually be
1993 ;; superseded by the *internal-pcl...* test,
1994 ;; above. While we are in a process of
1995 ;; transition, however, it should probably
1997 (not (find #\Space
(symbol-name symbol
))))))))))
1998 (setf (gf-info-fast-mf-p arg-info
)
1999 (or (not (eq **boot-state
** 'complete
))
2000 (let* ((method-class (generic-function-method-class gf
))
2001 (methods (compute-applicable-methods
2002 #'make-method-lambda
2003 (list gf
(class-prototype method-class
)
2005 (and methods
(null (cdr methods
))
2006 (let ((specls (method-specializers (car methods
))))
2007 (and (classp (car specls
))
2008 (eq 'standard-generic-function
2009 (class-name (car specls
)))
2010 (classp (cadr specls
))
2011 (eq 'standard-method
2012 (class-name (cadr specls
)))))))))
2015 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
2017 ;;; The STATIC-SLOTS field of the funcallable instances used as early
2018 ;;; generic functions is used to store the early methods and early
2019 ;;; discriminator code for the early generic function. The static
2020 ;;; slots field of the fins contains a list whose:
2021 ;;; CAR - a list of the early methods on this early gf
2022 ;;; CADR - the early discriminator code for this method
2023 (defun ensure-generic-function-using-class (existing spec
&rest keys
2024 &key
(lambda-list nil
2026 argument-precedence-order
2030 (declare (ignore keys
))
2031 (cond ((and existing
(early-gf-p existing
))
2033 (set-arg-info existing
:lambda-list lambda-list
))
2035 ((assoc spec
*!generic-function-fixups
* :test
#'equal
)
2037 (make-early-gf spec lambda-list lambda-list-p existing
2038 argument-precedence-order definition-source
2040 (bug "The function ~S is not already defined." spec
)))
2042 (bug "~S should be on the list ~S."
2043 spec
'*!generic-function-fixups
*))
2045 (pushnew spec
*!early-generic-functions
* :test
#'equal
)
2046 (make-early-gf spec lambda-list lambda-list-p nil
2047 argument-precedence-order definition-source
2050 (defun make-early-gf (spec &optional lambda-list lambda-list-p
2051 function argument-precedence-order source-location
2053 (let ((fin (allocate-standard-funcallable-instance
2054 *sgf-wrapper
* *sgf-slots-init
*)))
2055 (set-funcallable-instance-function
2058 (if (eq spec
'print-object
)
2059 #'(lambda (instance stream
)
2060 (print-unreadable-object (instance stream
:identity t
)
2061 (format stream
"std-instance")))
2062 #'(lambda (&rest args
)
2063 (declare (ignore args
))
2064 (error "The function of the funcallable-instance ~S~
2065 has not been set." fin
)))))
2066 (setf (gdefinition spec
) fin
)
2067 (!bootstrap-set-slot
'standard-generic-function fin
'name spec
)
2068 (!bootstrap-set-slot
'standard-generic-function fin
2069 'source source-location
)
2070 (!bootstrap-set-slot
'standard-generic-function fin
2071 '%documentation documentation
)
2072 (let ((arg-info (make-arg-info)))
2073 (setf (early-gf-arg-info fin
) arg-info
)
2075 (setf (info :function
:type spec
)
2077 (ftype-declaration-from-lambda-list lambda-list spec
))
2078 (info :function
:where-from spec
) :defined-method
)
2079 (if argument-precedence-order
2081 :lambda-list lambda-list
2082 :argument-precedence-order argument-precedence-order
)
2083 (set-arg-info fin
:lambda-list lambda-list
))))
2086 (defun safe-gf-dfun-state (generic-function)
2087 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2088 (clos-slots-ref (fsc-instance-slots generic-function
) +sgf-dfun-state-index
+)
2089 (gf-dfun-state generic-function
)))
2090 (defun (setf safe-gf-dfun-state
) (new-value generic-function
)
2091 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2092 (setf (clos-slots-ref (fsc-instance-slots generic-function
)
2093 +sgf-dfun-state-index
+)
2095 (setf (gf-dfun-state generic-function
) new-value
)))
2097 (defun set-dfun (gf &optional dfun cache info
)
2098 (let ((new-state (if (and dfun
(or cache info
))
2099 (list* dfun cache info
)
2102 ((eq **boot-state
** 'complete
)
2103 ;; Check that we are under the lock.
2105 (aver (eq sb-thread
:*current-thread
* (sb-thread:mutex-owner
(gf-lock gf
))))
2106 (setf (safe-gf-dfun-state gf
) new-state
))
2108 (setf (clos-slots-ref (get-slots gf
) +sgf-dfun-state-index
+)
2112 (defun gf-dfun-cache (gf)
2113 (let ((state (if (eq **boot-state
** 'complete
)
2114 (safe-gf-dfun-state gf
)
2115 (clos-slots-ref (get-slots gf
) +sgf-dfun-state-index
+))))
2118 (cons (cadr state
)))))
2120 (defun gf-dfun-info (gf)
2121 (let ((state (if (eq **boot-state
** 'complete
)
2122 (safe-gf-dfun-state gf
)
2123 (clos-slots-ref (get-slots gf
) +sgf-dfun-state-index
+))))
2126 (cons (cddr state
)))))
2128 (defconstant +sgf-name-index
+
2129 (!bootstrap-slot-index
'standard-generic-function
'name
))
2131 (defun !early-gf-name
(gf)
2132 (clos-slots-ref (get-slots gf
) +sgf-name-index
+))
2134 (defun gf-lambda-list (gf)
2135 (let ((arg-info (if (eq **boot-state
** 'complete
)
2137 (early-gf-arg-info gf
))))
2138 (if (eq :no-lambda-list
(arg-info-lambda-list arg-info
))
2139 (let ((methods (if (eq **boot-state
** 'complete
)
2140 (generic-function-methods gf
)
2141 (early-gf-methods gf
))))
2144 (warn "no way to determine the lambda list for ~S" gf
)
2146 (let* ((method (car (last methods
)))
2147 (ll (if (consp method
)
2148 (early-method-lambda-list method
)
2149 (method-lambda-list method
))))
2150 (create-gf-lambda-list ll
))))
2151 (arg-info-lambda-list arg-info
))))
2153 (defmacro real-ensure-gf-internal
(gf-class all-keys env
)
2155 (cond ((symbolp ,gf-class
)
2156 (setq ,gf-class
(find-class ,gf-class t
,env
)))
2157 ((classp ,gf-class
))
2159 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
2160 class nor a symbol that names a class."
2162 (unless (class-finalized-p ,gf-class
)
2163 (if (class-has-a-forward-referenced-superclass-p ,gf-class
)
2164 ;; FIXME: reference MOP documentation -- this is an
2165 ;; additional requirement on our users
2166 (error "The generic function class ~S is not finalizeable" ,gf-class
)
2167 (finalize-inheritance ,gf-class
)))
2168 (remf ,all-keys
:generic-function-class
)
2169 (remf ,all-keys
:environment
)
2170 (let ((combin (getf ,all-keys
:method-combination
)))
2173 (setf (getf ,all-keys
:method-combination
)
2174 (find-method-combination (class-prototype ,gf-class
)
2177 ((or null method-combination
))))
2178 (let ((method-class (getf ,all-keys
:method-class
'.shes-not-there.
)))
2179 (unless (eq method-class
'.shes-not-there.
)
2180 (setf (getf ,all-keys
:method-class
)
2181 (cond ((classp method-class
)
2183 (t (find-class method-class t
,env
))))))))
2185 (defun note-gf-signature (fun-name lambda-list-p lambda-list
)
2186 (unless lambda-list-p
2187 ;; Use the existing lambda-list, if any. It is reasonable to do eg.
2189 ;; (if (fboundp name)
2190 ;; (ensure-generic-function name)
2191 ;; (ensure-generic-function name :lambda-list '(foo)))
2193 ;; in which case we end up here with no lambda-list in the first leg.
2194 (setf (values lambda-list lambda-list-p
)
2196 (values (generic-function-lambda-list (fdefinition fun-name
))
2198 ((or warning error
) ()
2199 (values nil nil
)))))
2203 (ftype-declaration-from-lambda-list lambda-list fun-name
)
2206 ;; FIXME: Ideally we would like to not clobber it, but because generic
2207 ;; functions assert their FTYPEs callers believing the FTYPE are left with
2208 ;; unsafe assumptions. Hence the clobbering. Be quiet when the new type
2209 ;; is a subtype of the old one, though -- even though the type is not
2210 ;; trusted anymore, the warning is still not quite as interesting.
2211 (when (and (eq :declared
(info :function
:where-from fun-name
))
2212 (not (csubtypep gf-type
(setf old-type
(proclaimed-ftype fun-name
)))))
2213 (style-warn "~@<Generic function ~S clobbers an earlier ~S proclamation ~S ~
2214 for the same name with ~S.~:@>"
2216 (type-specifier old-type
)
2217 (type-specifier gf-type
)))
2218 (setf (info :function
:type fun-name
) gf-type
2219 (info :function
:where-from fun-name
) :defined-method
)
2222 (defun real-ensure-gf-using-class--generic-function
2226 &key environment
(lambda-list nil lambda-list-p
)
2227 (generic-function-class 'standard-generic-function
)
2229 (real-ensure-gf-internal generic-function-class all-keys environment
)
2230 ;; KLUDGE: the above macro does SETQ on GENERIC-FUNCTION-CLASS,
2231 ;; which is what makes the next line work
2232 (unless (eq (class-of existing
) generic-function-class
)
2233 (change-class existing generic-function-class
))
2235 (apply #'reinitialize-instance existing all-keys
)
2236 (note-gf-signature fun-name lambda-list-p lambda-list
)))
2238 (defun real-ensure-gf-using-class--null
2242 &key environment
(lambda-list nil lambda-list-p
)
2243 (generic-function-class 'standard-generic-function
)
2245 (declare (ignore existing
))
2246 (real-ensure-gf-internal generic-function-class all-keys environment
)
2248 (setf (gdefinition fun-name
)
2249 (apply #'make-instance generic-function-class
2250 :name fun-name all-keys
))
2251 (note-gf-signature fun-name lambda-list-p lambda-list
)))
2253 (defun safe-gf-arg-info (generic-function)
2254 (if (eq (class-of generic-function
) *the-class-standard-generic-function
*)
2255 (clos-slots-ref (fsc-instance-slots generic-function
)
2256 +sgf-arg-info-index
+)
2257 (gf-arg-info generic-function
)))
2259 ;;; FIXME: this function took on a slightly greater role than it
2260 ;;; previously had around 2005-11-02, when CSR fixed the bug whereby
2261 ;;; having more than one subclass of standard-generic-function caused
2262 ;;; the whole system to die horribly through a metacircle in
2263 ;;; GF-ARG-INFO. The fix is to be slightly more disciplined about
2264 ;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when
2265 ;;; computing discriminating functions, so we need to be careful about
2266 ;;; having a base case for the recursion, and we provide that with the
2267 ;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet)
2268 ;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to
2269 ;;; that stage, where all potentially dangerous cases are enumerated
2270 ;;; and stopped. -- CSR, 2005-11-02.
2271 (defun get-generic-fun-info (gf)
2272 ;; values nreq applyp metatypes nkeys arg-info
2273 (multiple-value-bind (applyp metatypes arg-info
)
2274 (let* ((arg-info (if (early-gf-p gf
)
2275 (early-gf-arg-info gf
)
2276 (safe-gf-arg-info gf
)))
2277 (metatypes (arg-info-metatypes arg-info
)))
2278 (values (arg-info-applyp arg-info
)
2283 (declare (fixnum nreq nkeys
))
2284 (dolist (x metatypes
)
2288 (values nreq applyp metatypes
2292 (defun generic-function-nreq (gf)
2293 (let* ((arg-info (if (early-gf-p gf
)
2294 (early-gf-arg-info gf
)
2295 (safe-gf-arg-info gf
)))
2296 (metatypes (arg-info-metatypes arg-info
)))
2297 (declare (list metatypes
))
2298 (length metatypes
)))
2300 (defun !early-make-a-method
(class qualifiers arglist specializers initargs doc
2301 &key slot-name object-class method-class-function
2305 ;; Figure out whether we got class objects or class names as the
2306 ;; specializers and set parsed and unparsed appropriately. If we
2307 ;; got class objects, then we can compute unparsed, but if we got
2308 ;; class names we don't try to compute parsed.
2310 (aver (notany #'sb-pcl
::eql-specializer-p specializers
))
2311 (if (every #'classp specializers
)
2312 (setq parsed specializers
2313 unparsed
(mapcar (lambda (s)
2314 (if (eq s t
) t
(class-name s
)))
2316 (setq unparsed specializers
2321 (getf initargs
:function
)
2322 (let ((mf (getf initargs
:function
)))
2324 (and (typep mf
'%method-function
)
2325 (%method-function-fast-function mf
)))
2327 ;; the parsed specializers. This is used by
2328 ;; EARLY-METHOD-SPECIALIZERS to cache the parse.
2329 ;; Note that this only comes into play when there is
2330 ;; more than one early method on an early gf.
2333 ;; A list to which REAL-MAKE-A-METHOD can be applied
2334 ;; to make a real method corresponding to this early
2337 (list class qualifiers arglist unparsed
2340 (list :slot-name slot-name
:object-class object-class
2341 :method-class-function method-class-function
))
2342 (list :definition-source definition-source
)))))
2343 (initialize-method-function initargs result
)
2346 (defun real-make-a-method
2347 (class qualifiers lambda-list specializers initargs doc
2348 &rest args
&key slot-name object-class method-class-function
2350 (if method-class-function
2351 (let* ((object-class (if (classp object-class
) object-class
2352 (find-class object-class
)))
2353 (slots (class-direct-slots object-class
))
2354 (slot-definition (find slot-name slots
2355 :key
#'slot-definition-name
)))
2357 (aver slot-definition
)
2358 (let ((initargs (list* :qualifiers qualifiers
:lambda-list lambda-list
2359 :specializers specializers
:documentation doc
2360 :slot-definition slot-definition
2361 :slot-name slot-name initargs
)))
2362 (apply #'make-instance
2363 (apply method-class-function object-class slot-definition
2365 :definition-source definition-source
2367 (apply #'make-instance class
:qualifiers qualifiers
2368 :lambda-list lambda-list
:specializers specializers
2369 :documentation doc
(append args initargs
))))
2371 (defun early-method-function (early-method)
2372 (values (cadr early-method
) (caddr early-method
)))
2374 (defun early-method-class (early-method)
2375 (find-class (car (fifth early-method
))))
2377 (defun early-method-standard-accessor-p (early-method)
2378 (let ((class (first (fifth early-method
))))
2379 (or (eq class
'standard-reader-method
)
2380 (eq class
'standard-writer-method
)
2381 (eq class
'standard-boundp-method
))))
2383 (defun early-method-standard-accessor-slot-name (early-method)
2384 (eighth (fifth early-method
)))
2386 ;;; Fetch the specializers of an early method. This is basically just
2387 ;;; a simple accessor except that when the second argument is t, this
2388 ;;; converts the specializers from symbols into class objects. The
2389 ;;; class objects are cached in the early method, this makes
2390 ;;; bootstrapping faster because the class objects only have to be
2394 ;;; The second argument should only be passed as T by
2395 ;;; early-lookup-method. This is to implement the rule that only when
2396 ;;; there is more than one early method on a generic function is the
2397 ;;; conversion from class names to class objects done. This
2398 ;;; corresponds to the fact that we are only allowed to have one
2399 ;;; method on any generic function up until the time classes exist.
2400 (defun early-method-specializers (early-method &optional objectsp
)
2401 (if (and (listp early-method
)
2402 (eq (car early-method
) :early-method
))
2403 (cond ((eq objectsp t
)
2404 (or (fourth early-method
)
2405 (setf (fourth early-method
)
2406 (mapcar #'find-class
(cadddr (fifth early-method
))))))
2408 (fourth (fifth early-method
))))
2409 (error "~S is not an early-method." early-method
)))
2411 (defun early-method-qualifiers (early-method)
2412 (second (fifth early-method
)))
2414 (defun early-method-lambda-list (early-method)
2415 (third (fifth early-method
)))
2417 (defun early-method-initargs (early-method)
2418 (fifth (fifth early-method
)))
2420 (defun (setf early-method-initargs
) (new-value early-method
)
2421 (setf (fifth (fifth early-method
)) new-value
))
2423 (defun !early-add-named-method
(generic-function-name qualifiers
2424 specializers arglist
&rest initargs
2425 &key documentation definition-source
2427 (let* (;; we don't need to deal with the :generic-function-class
2428 ;; argument here because the default,
2429 ;; STANDARD-GENERIC-FUNCTION, is right for all early generic
2430 ;; functions. (See REAL-ADD-NAMED-METHOD)
2431 (gf (ensure-generic-function generic-function-name
))
2433 (dolist (m (early-gf-methods gf
))
2434 (when (and (equal (early-method-specializers m
) specializers
)
2435 (equal (early-method-qualifiers m
) qualifiers
))
2437 (setf (getf (getf initargs
'plist
) :name
)
2438 (make-method-spec gf qualifiers specializers
))
2439 (let ((new (make-a-method 'standard-method qualifiers arglist
2440 specializers initargs documentation
2441 :definition-source definition-source
)))
2442 (when existing
(remove-method gf existing
))
2443 (add-method gf new
))))
2445 ;;; This is the early version of ADD-METHOD. Later this will become a
2446 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2447 ;;; special knowledge about ADD-METHOD.
2448 (defun add-method (generic-function method
)
2449 (when (not (fsc-instance-p generic-function
))
2450 (error "Early ADD-METHOD didn't get a funcallable instance."))
2451 (when (not (and (listp method
) (eq (car method
) :early-method
)))
2452 (error "Early ADD-METHOD didn't get an early method."))
2453 (push method
(early-gf-methods generic-function
))
2454 (set-arg-info generic-function
:new-method method
)
2455 (unless (assoc (!early-gf-name generic-function
)
2456 *!generic-function-fixups
*
2458 (update-dfun generic-function
)))
2460 ;;; This is the early version of REMOVE-METHOD. See comments on
2461 ;;; the early version of ADD-METHOD.
2462 (defun remove-method (generic-function method
)
2463 (when (not (fsc-instance-p generic-function
))
2464 (error "An early remove-method didn't get a funcallable instance."))
2465 (when (not (and (listp method
) (eq (car method
) :early-method
)))
2466 (error "An early remove-method didn't get an early method."))
2467 (setf (early-gf-methods generic-function
)
2468 (remove method
(early-gf-methods generic-function
)))
2469 (set-arg-info generic-function
)
2470 (unless (assoc (!early-gf-name generic-function
)
2471 *!generic-function-fixups
*
2473 (update-dfun generic-function
)))
2475 ;;; This is the early version of GET-METHOD. See comments on the early
2476 ;;; version of ADD-METHOD.
2477 (defun get-method (generic-function qualifiers specializers
2478 &optional
(errorp t
))
2479 (if (early-gf-p generic-function
)
2480 (or (dolist (m (early-gf-methods generic-function
))
2481 (when (and (or (equal (early-method-specializers m nil
)
2483 (equal (early-method-specializers m t
)
2485 (equal (early-method-qualifiers m
) qualifiers
))
2488 (error "can't get early method")
2490 (real-get-method generic-function qualifiers specializers errorp
)))
2492 ;; minor KLUDGE: a separate code component for this function allows GCing
2493 ;; a few symbols and their associated code that would otherwise be retained:
2494 ;; *!EARLY-{GENERIC-}FUNCTIONS*, *!GENERIC-FUNCTION-FIXUPS*
2495 (defun early-gf-primary-slow-method-fn (fn)
2496 (lambda (args next-methods
)
2497 (declare (ignore next-methods
))
2500 (defun !fix-early-generic-functions
()
2501 (let ((accessors nil
))
2502 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2503 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2504 (dolist (early-gf-spec *!early-generic-functions
*)
2505 (when (every #'early-method-standard-accessor-p
2506 (early-gf-methods (gdefinition early-gf-spec
)))
2507 (push early-gf-spec accessors
)))
2508 (dolist (spec (nconc accessors
2509 '(accessor-method-slot-name
2510 generic-function-methods
2514 slot-definition-location
2515 slot-definition-name
2518 class-precedence-list
2519 slot-boundp-using-class
2520 (setf slot-value-using-class
)
2521 slot-value-using-class
)))
2523 (setq *!early-generic-functions
*
2525 (delete spec
*!early-generic-functions
* :test
#'equal
))))
2527 (dolist (early-gf-spec *!early-generic-functions
*)
2528 (/show early-gf-spec
)
2529 (let* ((gf (gdefinition early-gf-spec
))
2530 (methods (mapcar (lambda (early-method)
2531 (let ((args (copy-list (fifth
2534 (early-method-specializers
2536 (apply #'real-make-a-method args
)))
2537 (early-gf-methods gf
))))
2538 (setf (generic-function-method-class gf
) *the-class-standard-method
*)
2539 (setf (generic-function-method-combination gf
)
2540 *standard-method-combination
*)
2541 (set-methods gf methods
)))
2543 (dolist (fn *!early-functions
*)
2545 (setf (gdefinition (car fn
)) (fdefinition (caddr fn
))))
2547 (dolist (fixup *!generic-function-fixups
*)
2549 (let* ((fspec (car fixup
))
2550 (gf (gdefinition fspec
))
2551 (methods (mapcar (lambda (method)
2552 (let* ((lambda-list (first method
))
2553 (specializers (mapcar #'find-class
(second method
)))
2554 (method-fn-name (third method
))
2555 (fn-name (or method-fn-name fspec
))
2556 (fn (fdefinition fn-name
))
2560 (early-gf-primary-slow-method-fn fn
)
2561 `(call ,fn-name
)))))
2562 (declare (type function fn
))
2563 (make-a-method 'standard-method
2570 (setf (generic-function-method-class gf
) *the-class-standard-method
*)
2571 (setf (generic-function-method-combination gf
)
2572 *standard-method-combination
*)
2573 (set-methods gf methods
))))
2574 (/show
"leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2576 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2577 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2578 ;;; is really implemented.
2579 (defun parse-defmethod (cdr-of-form)
2580 (declare (list cdr-of-form
))
2581 (let ((qualifiers ())
2583 (loop (if (and (car cdr-of-form
) (atom (car cdr-of-form
)))
2584 (push (pop cdr-of-form
) qualifiers
)
2585 (return (setq qualifiers
(nreverse qualifiers
)))))
2586 (setq spec-ll
(pop cdr-of-form
))
2587 (values qualifiers spec-ll cdr-of-form
)))
2589 (defun parse-specializers (generic-function specializers
)
2590 (declare (list specializers
))
2591 (flet ((parse (spec)
2592 (parse-specializer-using-class generic-function spec
)))
2593 (mapcar #'parse specializers
)))
2595 (defun unparse-specializers (generic-function specializers
)
2596 (declare (list specializers
))
2597 (flet ((unparse (spec)
2598 (unparse-specializer-using-class generic-function spec
)))
2599 (mapcar #'unparse specializers
)))
2601 (macrolet ((def (n name
)
2602 `(defun ,name
(lambda-list)
2603 (nth-value ,n
(parse-specialized-lambda-list lambda-list
)))))
2604 ;; We don't need these, but according to the unit tests,
2605 ;; they're mandated by AMOP.
2606 (def 1 extract-lambda-list
)
2607 (def 2 extract-specializer-names
))
2609 (define-condition specialized-lambda-list-error
2610 (reference-condition simple-program-error
)
2612 (:default-initargs
:references
(list '(:ansi-cl
:section
(3 4 3)))))
2615 ;; - the bound variables, without defaults, supplied-p vars, or &AUX vars.
2616 ;; - the lambda list without specializers.
2617 ;; - just the specializers
2618 (defun parse-specialized-lambda-list (arglist)
2619 (multiple-value-bind (llks specialized optional rest key aux
)
2623 :accept
(lambda-list-keyword-mask
2624 '(&optional
&rest
&key
&allow-other-keys
&aux
))
2625 :silent t
; never signal &OPTIONAL + &KEY style-warning
2626 :condition-class
'specialized-lambda-list-error
)
2627 (let ((required (mapcar (lambda (x) (if (listp x
) (car x
) x
)) specialized
)))
2628 (values (append required
2629 (mapcar #'parse-optional-arg-spec optional
)
2631 ;; Preserve keyword-names when given as (:KEYWORD var)
2632 (mapcar (lambda (x) (if (typep x
'(cons cons
))
2634 (parse-key-arg-spec x
))) key
))
2635 (make-lambda-list llks nil required optional rest key aux
)
2636 (mapcar (lambda (x) (if (listp x
) (cadr x
) t
)) specialized
)))))
2638 (setq **boot-state
** 'early
)
2640 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2641 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2642 ;;; walker stuff was only used for implementing stuff like that; maybe
2643 ;;; it's not needed any more? Hunt down what it was used for and see.
2645 (defun extract-the (form)
2646 (cond ((and (consp form
) (eq (car form
) 'the
))
2647 (aver (proper-list-of-length-p form
3))
2652 (defmacro with-slots
(slots instance
&body body
)
2653 (let ((in (gensym)))
2654 `(let ((,in
,instance
))
2655 (declare (ignorable ,in
))
2656 ,@(let ((instance (extract-the instance
)))
2657 (and (symbolp instance
)
2658 `((declare (%variable-rebinding
,in
,instance
)))))
2660 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2662 (if (symbolp slot-entry
)
2666 (if (symbolp slot-entry
)
2668 (cadr slot-entry
))))
2670 (slot-value ,in
',slot-name
))))
2674 (defmacro with-accessors
(slots instance
&body body
)
2675 (let ((in (gensym)))
2676 `(let ((,in
,instance
))
2677 (declare (ignorable ,in
))
2678 ,@(let ((instance (extract-the instance
)))
2679 (and (symbolp instance
)
2680 `((declare (%variable-rebinding
,in
,instance
)))))
2682 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2683 (let ((var-name (car slot-entry
))
2684 (accessor-name (cadr slot-entry
)))
2685 `(,var-name
(,accessor-name
,in
))))