1.0.23.56: special variables cause special cases in CLOS cleverness
[sbcl/tcr.git] / src / pcl / boot.lisp
blob12467fa33bbf0fa9e2e9bd1d9c24da42605fe74c
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
8 ;;;; information.
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
18 ;;;; control laws.
19 ;;;;
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
22 ;;;; specification.
24 (in-package "SB-PCL")
28 The CommonLoops evaluator is meta-circular.
30 Most of the code in PCL is methods on generic functions, including
31 most of the code that actually implements generic functions and method
32 lookup.
34 So, we have a classic bootstrapping problem. The solution to this is
35 to first get a cheap implementation of generic functions running,
36 these are called early generic functions. These early generic
37 functions and the corresponding early methods and early method lookup
38 are used to get enough of the system running that it is possible to
39 create real generic functions and methods and implement real method
40 lookup. At that point (done in the file FIXUP) the function
41 !FIX-EARLY-GENERIC-FUNCTIONS is called to convert all the early generic
42 functions to real generic functions.
44 The cheap generic functions are built using the same
45 FUNCALLABLE-INSTANCE objects that real generic functions are made out of.
46 This means that as PCL is being bootstrapped, the cheap generic
47 function objects which are being created are the same objects which
48 will later be real generic functions. This is good because:
49 - we don't cons garbage structure, and
50 - we can keep pointers to the cheap generic function objects
51 during booting because those pointers will still point to
52 the right object after the generic functions are all fixed up.
54 This file defines the DEFMETHOD macro and the mechanism used to expand
55 it. This includes the mechanism for processing the body of a method.
56 DEFMETHOD basically expands into a call to LOAD-DEFMETHOD, which
57 basically calls ADD-METHOD to add the method to the generic function.
58 These expansions can be loaded either during bootstrapping or when PCL
59 is fully up and running.
61 An important effect of this arrangement is it means we can compile
62 files with DEFMETHOD forms in them in a completely running PCL, but
63 then load those files back in during bootstrapping. This makes
64 development easier. It also means there is only one set of code for
65 processing DEFMETHOD. Bootstrapping works by being sure to have
66 LOAD-METHOD be careful to call only primitives which work during
67 bootstrapping.
71 (declaim (notinline make-a-method add-named-method
72 ensure-generic-function-using-class
73 add-method remove-method))
75 (defvar *!early-functions*
76 '((make-a-method early-make-a-method real-make-a-method)
77 (add-named-method early-add-named-method real-add-named-method)))
79 ;;; For each of the early functions, arrange to have it point to its
80 ;;; early definition. Do this in a way that makes sure that if we
81 ;;; redefine one of the early definitions the redefinition will take
82 ;;; effect. This makes development easier.
83 (dolist (fns *!early-functions*)
84 (let ((name (car fns))
85 (early-name (cadr fns)))
86 (setf (gdefinition name)
87 (set-fun-name
88 (lambda (&rest args)
89 (apply (fdefinition early-name) args))
90 name))))
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.
95 ;;;
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*
99 '((add-method
100 ((generic-function method)
101 (standard-generic-function method)
102 real-add-method))
103 (remove-method
104 ((generic-function method)
105 (standard-generic-function method)
106 real-remove-method))
107 (get-method
108 ((generic-function qualifiers specializers &optional (errorp t))
109 (standard-generic-function t t)
110 real-get-method))
111 (ensure-generic-function-using-class
112 ((generic-function fun-name
113 &key generic-function-class environment
114 &allow-other-keys)
115 (generic-function t)
116 real-ensure-gf-using-class--generic-function)
117 ((generic-function fun-name
118 &key generic-function-class environment
119 &allow-other-keys)
120 (null t)
121 real-ensure-gf-using-class--null))
122 (make-method-lambda
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
140 lambda-expression
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)
156 (let ((initargs ())
157 (methods ()))
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)))
172 (case car-option
173 (declare
174 (when (and
175 (consp (cadr option))
176 (member (first (cadr option))
177 ;; FIXME: this list is slightly weird.
178 ;; ANSI (on the DEFGENERIC page) in one
179 ;; place allows only OPTIMIZE; in
180 ;; another place gives this list of
181 ;; disallowed declaration specifiers.
182 ;; This seems to be the only place where
183 ;; the FUNCTION declaration is
184 ;; mentioned; TYPE seems to be missing.
185 ;; Very strange. -- CSR, 2002-10-21
186 '(declaration ftype function
187 inline notinline special)))
188 (error 'simple-program-error
189 :format-control "The declaration specifier ~S ~
190 is not allowed inside DEFGENERIC."
191 :format-arguments (list (cadr option))))
192 (push (cadr option) (initarg :declarations)))
193 (:method-combination
194 (when (initarg car-option)
195 (duplicate-option car-option))
196 (unless (symbolp (cadr option))
197 (error 'simple-program-error
198 :format-control "METHOD-COMBINATION name not a ~
199 symbol: ~S"
200 :format-arguments (list (cadr option))))
201 (setf (initarg car-option)
202 `',(cdr option)))
203 (:argument-precedence-order
204 (let* ((required (parse-lambda-list lambda-list))
205 (supplied (cdr option)))
206 (unless (= (length required) (length supplied))
207 (error 'simple-program-error
208 :format-control "argument count discrepancy in ~
209 :ARGUMENT-PRECEDENCE-ORDER clause."
210 :format-arguments nil))
211 (when (set-difference required supplied)
212 (error 'simple-program-error
213 :format-control "unequal sets for ~
214 :ARGUMENT-PRECEDENCE-ORDER clause: ~
215 ~S and ~S"
216 :format-arguments (list required supplied)))
217 (setf (initarg car-option)
218 `',(cdr option))))
219 ((:documentation :generic-function-class :method-class)
220 (unless (proper-list-of-length-p option 2)
221 (error "bad list length for ~S" option))
222 (if (initarg car-option)
223 (duplicate-option car-option)
224 (setf (initarg car-option) `',(cadr option))))
225 (:method
226 (push (cdr option) methods))
228 ;; ANSI requires that unsupported things must get a
229 ;; PROGRAM-ERROR.
230 (error 'simple-program-error
231 :format-control "unsupported option ~S"
232 :format-arguments (list option))))))
234 (when (initarg :declarations)
235 (setf (initarg :declarations)
236 `',(initarg :declarations))))
237 `(progn
238 (eval-when (:compile-toplevel :load-toplevel :execute)
239 (compile-or-load-defgeneric ',fun-name))
240 (load-defgeneric ',fun-name ',lambda-list
241 (sb-c:source-location) ,@initargs)
242 ,@(mapcar #'expand-method-definition methods)
243 (fdefinition ',fun-name)))))
245 (defun compile-or-load-defgeneric (fun-name)
246 (proclaim-as-fun-name fun-name)
247 (note-name-defined fun-name :function)
248 (unless (eq (info :function :where-from fun-name) :declared)
249 (setf (info :function :where-from fun-name) :defined)
250 (setf (info :function :type fun-name)
251 (specifier-type 'function))))
253 (defun load-defgeneric (fun-name lambda-list source-location &rest initargs)
254 (when (fboundp fun-name)
255 (let ((fun (fdefinition fun-name)))
256 (warn 'sb-kernel:redefinition-with-defgeneric :name fun-name
257 :old fun :new-location source-location)
258 (when (generic-function-p fun)
259 (loop for method in (generic-function-initial-methods fun)
260 do (remove-method fun method))
261 (setf (generic-function-initial-methods fun) '()))))
262 (apply #'ensure-generic-function
263 fun-name
264 :lambda-list lambda-list
265 :definition-source source-location
266 initargs))
268 (define-condition generic-function-lambda-list-error
269 (reference-condition simple-program-error)
271 (:default-initargs :references (list '(:ansi-cl :section (3 4 2)))))
273 (defun check-gf-lambda-list (lambda-list)
274 (flet ((ensure (arg ok)
275 (unless ok
276 (error 'generic-function-lambda-list-error
277 :format-control
278 "~@<invalid ~S ~_in the generic function lambda list ~S~:>"
279 :format-arguments (list arg lambda-list)))))
280 (multiple-value-bind (required optional restp rest keyp keys allowp
281 auxp aux morep more-context more-count)
282 (parse-lambda-list lambda-list)
283 (declare (ignore required)) ; since they're no different in a gf ll
284 (declare (ignore restp rest)) ; since they're no different in a gf ll
285 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
286 (declare (ignore aux)) ; since we require AUXP=NIL
287 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
288 ;; no defaults allowed for &OPTIONAL arguments
289 (dolist (i optional)
290 (ensure i (or (symbolp i)
291 (and (consp i) (symbolp (car i)) (null (cdr i))))))
292 ;; no defaults allowed for &KEY arguments
293 (when keyp
294 (dolist (i keys)
295 (ensure i (or (symbolp i)
296 (and (consp i)
297 (or (symbolp (car i))
298 (and (consp (car i))
299 (symbolp (caar i))
300 (symbolp (cadar i))
301 (null (cddar i))))
302 (null (cdr i)))))))
303 ;; no &AUX allowed
304 (when auxp
305 (error "&AUX is not allowed in a generic function lambda list: ~S"
306 lambda-list))
307 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
308 ;; the ANSI spec, but the CMU CL &MORE extension does not
309 ;; belong here!
310 (aver (not morep)))))
312 (defmacro defmethod (&rest args)
313 (multiple-value-bind (name qualifiers lambda-list body)
314 (parse-defmethod args)
315 `(progn
316 ;; KLUDGE: this double expansion is quite a monumental
317 ;; workaround: it comes about because of a fantastic interaction
318 ;; between the processing rules of CLHS 3.2.3.1 and the
319 ;; bizarreness of MAKE-METHOD-LAMBDA.
321 ;; MAKE-METHOD-LAMBDA can be called by the user, and if the
322 ;; lambda itself doesn't refer to outside bindings the return
323 ;; value must be compileable in the null lexical environment.
324 ;; However, the function must also refer somehow to the
325 ;; associated method object, so that it can call NO-NEXT-METHOD
326 ;; with the appropriate arguments if there is no next method --
327 ;; but when the function is generated, the method object doesn't
328 ;; exist yet.
330 ;; In order to resolve this issue, we insert a literal cons cell
331 ;; into the body of the method lambda, return the same cons cell
332 ;; as part of the second (initargs) return value of
333 ;; MAKE-METHOD-LAMBDA, and a method on INITIALIZE-INSTANCE fills
334 ;; in the cell when the method is created. However, this
335 ;; strategy depends on having a fresh cons cell for every method
336 ;; lambda, which (without the workaround below) is skewered by
337 ;; the processing in CLHS 3.2.3.1, which permits implementations
338 ;; to macroexpand the bodies of EVAL-WHEN forms with both
339 ;; :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL only once. The
340 ;; expansion below forces the double expansion in those cases,
341 ;; while expanding only once in the common case.
342 (eval-when (:load-toplevel)
343 (%defmethod-expander ,name ,qualifiers ,lambda-list ,body))
344 (eval-when (:execute)
345 (%defmethod-expander ,name ,qualifiers ,lambda-list ,body)))))
347 (defmacro %defmethod-expander
348 (name qualifiers lambda-list body &environment env)
349 (multiple-value-bind (proto-gf proto-method)
350 (prototypes-for-make-method-lambda name)
351 (expand-defmethod name proto-gf proto-method qualifiers
352 lambda-list body env)))
355 (defun prototypes-for-make-method-lambda (name)
356 (if (not (eq *boot-state* 'complete))
357 (values nil nil)
358 (let ((gf? (and (fboundp name)
359 (gdefinition name))))
360 (if (or (null gf?)
361 (not (generic-function-p gf?)))
362 (values (class-prototype (find-class 'standard-generic-function))
363 (class-prototype (find-class 'standard-method)))
364 (values gf?
365 (class-prototype (or (generic-function-method-class gf?)
366 (find-class 'standard-method))))))))
368 ;;; Take a name which is either a generic function name or a list specifying
369 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
370 ;;; the prototype instance of the method-class for that generic function.
372 ;;; If there is no generic function by that name, this returns the
373 ;;; default value, the prototype instance of the class
374 ;;; STANDARD-METHOD. This default value is also returned if the spec
375 ;;; names an ordinary function or even a macro. In effect, this leaves
376 ;;; the signalling of the appropriate error until load time.
378 ;;; Note: During bootstrapping, this function is allowed to return NIL.
379 (defun method-prototype-for-gf (name)
380 (let ((gf? (and (fboundp name)
381 (gdefinition name))))
382 (cond ((neq *boot-state* 'complete) nil)
383 ((or (null gf?)
384 (not (generic-function-p gf?))) ; Someone else MIGHT
385 ; error at load time.
386 (class-prototype (find-class 'standard-method)))
388 (class-prototype (or (generic-function-method-class gf?)
389 (find-class 'standard-method)))))))
391 (defun expand-defmethod (name
392 proto-gf
393 proto-method
394 qualifiers
395 lambda-list
396 body
397 env)
398 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
399 (add-method-declarations name qualifiers lambda-list body env)
400 (multiple-value-bind (method-function-lambda initargs)
401 (make-method-lambda proto-gf proto-method method-lambda env)
402 (let ((initargs-form (make-method-initargs-form
403 proto-gf proto-method method-function-lambda
404 initargs env))
405 (specializers-form (make-method-specializers-form
406 proto-gf proto-method specializers env)))
407 `(progn
408 ;; Note: We could DECLAIM the ftype of the generic function
409 ;; here, since ANSI specifies that we create it if it does
410 ;; not exist. However, I chose not to, because I think it's
411 ;; more useful to support a style of programming where every
412 ;; generic function has an explicit DEFGENERIC and any typos
413 ;; in DEFMETHODs are warned about. Otherwise
415 ;; (DEFGENERIC FOO-BAR-BLETCH (X))
416 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
417 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
418 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
419 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
420 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
422 ;; compiles without raising an error and runs without
423 ;; raising an error (since SIMPLE-VECTOR cases fall through
424 ;; to VECTOR) but still doesn't do what was intended. I hate
425 ;; that kind of bug (code which silently gives the wrong
426 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
427 ,(make-defmethod-form name qualifiers specializers-form
428 unspecialized-lambda-list
429 (if proto-method
430 (class-name (class-of proto-method))
431 'standard-method)
432 initargs-form))))))
434 (defun interned-symbol-p (x)
435 (and (symbolp x) (symbol-package x)))
437 (defun make-defmethod-form
438 (name qualifiers specializers unspecialized-lambda-list
439 method-class-name initargs-form)
440 (let (fn
441 fn-lambda)
442 (if (and (interned-symbol-p (fun-name-block-name name))
443 (every #'interned-symbol-p qualifiers)
444 (every (lambda (s)
445 (if (consp s)
446 (and (eq (car s) 'eql)
447 (constantp (cadr s))
448 (let ((sv (constant-form-value (cadr s))))
449 (or (interned-symbol-p sv)
450 (integerp sv)
451 (and (characterp sv)
452 (standard-char-p sv)))))
453 (interned-symbol-p s)))
454 specializers)
455 (consp initargs-form)
456 (eq (car initargs-form) 'list*)
457 (memq (cadr initargs-form) '(:function))
458 (consp (setq fn (caddr initargs-form)))
459 (eq (car fn) 'function)
460 (consp (setq fn-lambda (cadr fn)))
461 (eq (car fn-lambda) 'lambda)
462 (bug "Really got here"))
463 (let* ((specls (mapcar (lambda (specl)
464 (if (consp specl)
465 ;; CONSTANT-FORM-VALUE? What I
466 ;; kind of want to know, though,
467 ;; is what happens if we don't do
468 ;; this for some slow-method
469 ;; function because of a hairy
470 ;; lexenv -- is the only bad
471 ;; effect that the method
472 ;; function ends up unnamed? If
473 ;; so, couldn't we arrange to
474 ;; name it later?
475 `(,(car specl) ,(eval (cadr specl)))
476 specl))
477 specializers))
478 (mname `(,(if (eq (cadr initargs-form) :function)
479 'slow-method 'fast-method)
480 ,name ,@qualifiers ,specls)))
481 `(progn
482 (defun ,mname ,(cadr fn-lambda)
483 ,@(cddr fn-lambda))
484 ,(make-defmethod-form-internal
485 name qualifiers `',specls
486 unspecialized-lambda-list method-class-name
487 `(list* ,(cadr initargs-form)
488 #',mname
489 ,@(cdddr initargs-form)))))
490 (make-defmethod-form-internal
491 name qualifiers
492 specializers
493 #+nil
494 `(list ,@(mapcar (lambda (specializer)
495 (if (consp specializer)
496 ``(,',(car specializer)
497 ,,(cadr specializer))
498 `',specializer))
499 specializers))
500 unspecialized-lambda-list
501 method-class-name
502 initargs-form))))
504 (defun make-defmethod-form-internal
505 (name qualifiers specializers-form unspecialized-lambda-list
506 method-class-name initargs-form)
507 `(load-defmethod
508 ',method-class-name
509 ',name
510 ',qualifiers
511 ,specializers-form
512 ',unspecialized-lambda-list
513 ,initargs-form
514 (sb-c:source-location)))
516 (defmacro make-method-function (method-lambda &environment env)
517 (multiple-value-bind (proto-gf proto-method)
518 (prototypes-for-make-method-lambda nil)
519 (multiple-value-bind (method-function-lambda initargs)
520 (make-method-lambda proto-gf proto-method method-lambda env)
521 (make-method-initargs-form proto-gf
522 proto-method
523 method-function-lambda
524 initargs
525 env))))
527 (defun add-method-declarations (name qualifiers lambda-list body env)
528 (declare (ignore env))
529 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
530 (parse-specialized-lambda-list lambda-list)
531 (multiple-value-bind (real-body declarations documentation)
532 (parse-body body)
533 (values `(lambda ,unspecialized-lambda-list
534 ,@(when documentation `(,documentation))
535 ;; (Old PCL code used a somewhat different style of
536 ;; list for %METHOD-NAME values. Our names use
537 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
538 ;; method names look more like what you see in a
539 ;; DEFMETHOD form.)
541 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
542 ;; least the code to set up named BLOCKs around the
543 ;; bodies of methods, depends on the function's base
544 ;; name being the first element of the %METHOD-NAME
545 ;; list. It would be good to remove this dependency,
546 ;; perhaps by building the BLOCK here, or by using
547 ;; another declaration (e.g. %BLOCK-NAME), so that
548 ;; our method debug names are free to have any format,
549 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
551 ;; Further, as of sbcl-0.7.9.10, the code to
552 ;; implement NO-NEXT-METHOD is coupled to the form of
553 ;; this declaration; see the definition of
554 ;; CALL-NO-NEXT-METHOD (and the passing of
555 ;; METHOD-NAME-DECLARATION arguments around the
556 ;; various CALL-NEXT-METHOD logic).
557 (declare (%method-name (,name
558 ,@qualifiers
559 ,specializers)))
560 (declare (%method-lambda-list ,@lambda-list))
561 ,@declarations
562 ,@real-body)
563 unspecialized-lambda-list specializers))))
565 (defun real-make-method-initargs-form (proto-gf proto-method
566 method-lambda initargs env)
567 (declare (ignore proto-gf proto-method))
568 (unless (and (consp method-lambda)
569 (eq (car method-lambda) 'lambda))
570 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
571 is not a lambda form."
572 method-lambda))
573 (make-method-initargs-form-internal method-lambda initargs env))
575 (unless (fboundp 'make-method-initargs-form)
576 (setf (gdefinition 'make-method-initargs-form)
577 (symbol-function 'real-make-method-initargs-form)))
579 ;;; When bootstrapping PCL MAKE-METHOD-LAMBDA starts out as a regular
580 ;;; functions: REAL-MAKE-METHOD-LAMBDA set to the fdefinition of
581 ;;; MAKE-METHOD-LAMBDA. Once generic functions are born, the
582 ;;; REAL-MAKE-METHOD lambda is used as the body of the default method.
583 ;;; MAKE-METHOD-LAMBDA-INTERNAL is split out into a separate function
584 ;;; so that changing it in a live image is easy, and changes actually
585 ;;; take effect.
586 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
587 (make-method-lambda-internal proto-gf proto-method method-lambda env))
589 (unless (fboundp 'make-method-lambda)
590 (setf (gdefinition 'make-method-lambda)
591 (symbol-function 'real-make-method-lambda)))
593 (defun declared-specials (declarations)
594 (loop for (declare . specifiers) in declarations
595 append (loop for specifier in specifiers
596 when (eq 'special (car specifier))
597 append (cdr specifier))))
599 (defun make-method-lambda-internal (proto-gf proto-method method-lambda env)
600 (declare (ignore proto-gf proto-method))
601 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
602 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
603 is not a lambda form."
604 method-lambda))
605 (multiple-value-bind (real-body declarations documentation)
606 (parse-body (cddr method-lambda))
607 (let* ((name-decl (get-declaration '%method-name declarations))
608 (sll-decl (get-declaration '%method-lambda-list declarations))
609 (method-name (when (consp name-decl) (car name-decl)))
610 (generic-function-name (when method-name (car method-name)))
611 (specialized-lambda-list (or sll-decl (cadr method-lambda)))
612 ;; the method-cell is a way of communicating what method a
613 ;; method-function implements, for the purpose of
614 ;; NO-NEXT-METHOD. We need something that can be shared
615 ;; between function and initargs, but not something that
616 ;; will be coalesced as a constant (because we are naughty,
617 ;; oh yes) with the expansion of any other methods in the
618 ;; same file. -- CSR, 2007-05-30
619 (method-cell (list (make-symbol "METHOD-CELL"))))
620 (multiple-value-bind (parameters lambda-list specializers)
621 (parse-specialized-lambda-list specialized-lambda-list)
622 (let* ((required-parameters
623 (mapcar (lambda (r s) (declare (ignore s)) r)
624 parameters
625 specializers))
626 (slots (mapcar #'list required-parameters))
627 (class-declarations
628 `(declare
629 ;; These declarations seem to be used by PCL to pass
630 ;; information to itself; when I tried to delete 'em
631 ;; ca. 0.6.10 it didn't work. I'm not sure how
632 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
633 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
634 ,@(remove nil
635 (mapcar (lambda (a s) (and (symbolp s)
636 (neq s t)
637 `(%class ,a ,s)))
638 parameters
639 specializers))
640 ;; These TYPE declarations weren't in the original
641 ;; PCL code, but the Python compiler likes them a
642 ;; lot. (We're telling the compiler about our
643 ;; knowledge of specialized argument types so that
644 ;; it can avoid run-time type dispatch overhead,
645 ;; which can be a huge win for Python.)
647 ;; KLUDGE: when I tried moving these to
648 ;; ADD-METHOD-DECLARATIONS, things broke. No idea
649 ;; why. -- CSR, 2004-06-16
650 ,@(let ((specials (declared-specials declarations)))
651 (mapcar (lambda (par spec)
652 (parameter-specializer-declaration-in-defmethod
653 par spec specials env))
654 parameters
655 specializers))))
656 (method-lambda
657 ;; Remove the documentation string and insert the
658 ;; appropriate class declarations. The documentation
659 ;; string is removed to make it easy for us to insert
660 ;; new declarations later, they will just go after the
661 ;; CADR of the method lambda. The class declarations
662 ;; are inserted to communicate the class of the method's
663 ;; arguments to the code walk.
664 `(lambda ,lambda-list
665 ;; The default ignorability of method parameters
666 ;; doesn't seem to be specified by ANSI. PCL had
667 ;; them basically ignorable but was a little
668 ;; inconsistent. E.g. even though the two
669 ;; method definitions
670 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
671 ;; (DEFMETHOD FOO ((X T) Y) "Z")
672 ;; are otherwise equivalent, PCL treated Y as
673 ;; ignorable in the first definition but not in the
674 ;; second definition. We make all required
675 ;; parameters ignorable as a way of systematizing
676 ;; the old PCL behavior. -- WHN 2000-11-24
677 (declare (ignorable ,@required-parameters))
678 ,class-declarations
679 ,@declarations
680 (block ,(fun-name-block-name generic-function-name)
681 ,@real-body)))
682 (constant-value-p (and (null (cdr real-body))
683 (constantp (car real-body))))
684 (constant-value (and constant-value-p
685 (constant-form-value (car real-body))))
686 (plist (and constant-value-p
687 (or (typep constant-value
688 '(or number character))
689 (and (symbolp constant-value)
690 (symbol-package constant-value)))
691 (list :constant-value constant-value)))
692 (applyp (dolist (p lambda-list nil)
693 (cond ((memq p '(&optional &rest &key))
694 (return t))
695 ((eq p '&aux)
696 (return nil))))))
697 (multiple-value-bind
698 (walked-lambda call-next-method-p closurep
699 next-method-p-p setq-p
700 parameters-setqd)
701 (walk-method-lambda method-lambda
702 required-parameters
704 slots)
705 (multiple-value-bind (walked-lambda-body
706 walked-declarations
707 walked-documentation)
708 (parse-body (cddr walked-lambda))
709 (declare (ignore walked-documentation))
710 (when (some #'cdr slots)
711 (let ((slot-name-lists (slot-name-lists-from-slots slots)))
712 (setq plist
713 `(,@(when slot-name-lists
714 `(:slot-name-lists ,slot-name-lists))
715 ,@plist))
716 (setq walked-lambda-body
717 `((pv-binding (,required-parameters
718 ,slot-name-lists
719 (load-time-value
720 (intern-pv-table
721 :slot-name-lists ',slot-name-lists)))
722 ,@walked-lambda-body)))))
723 (when (and (memq '&key lambda-list)
724 (not (memq '&allow-other-keys lambda-list)))
725 (let ((aux (memq '&aux lambda-list)))
726 (setq lambda-list (nconc (ldiff lambda-list aux)
727 (list '&allow-other-keys)
728 aux))))
729 (values `(lambda (.method-args. .next-methods.)
730 (simple-lexical-method-functions
731 (,lambda-list .method-args. .next-methods.
732 :call-next-method-p
733 ,call-next-method-p
734 :next-method-p-p ,next-method-p-p
735 :setq-p ,setq-p
736 :method-cell ,method-cell
737 :closurep ,closurep
738 :applyp ,applyp)
739 ,@walked-declarations
740 (locally
741 (declare (disable-package-locks
742 %parameter-binding-modified))
743 (symbol-macrolet ((%parameter-binding-modified
744 ',@parameters-setqd))
745 (declare (enable-package-locks
746 %parameter-binding-modified))
747 ,@walked-lambda-body))))
748 `(,@(when call-next-method-p `(method-cell ,method-cell))
749 ,@(when plist `(plist ,plist))
750 ,@(when documentation `(:documentation ,documentation)))))))))))
752 (defun real-make-method-specializers-form
753 (proto-gf proto-method specializer-names env)
754 (declare (ignore env proto-gf proto-method))
755 (flet ((parse (name)
756 (cond
757 ((and (eq *boot-state* 'complete)
758 (specializerp name))
759 name)
760 ((symbolp name) `(find-class ',name))
761 ((consp name) (ecase (car name)
762 ((eql) `(intern-eql-specializer ,(cadr name)))
763 ((class-eq) `(class-eq-specializer (find-class ',(cadr name))))
764 ((prototype) `(fixme))))
765 (t (bug "Foo")))))
766 `(list ,@(mapcar #'parse specializer-names))))
768 (unless (fboundp 'make-method-specializers-form)
769 (setf (gdefinition 'make-method-specializers-form)
770 (symbol-function 'real-make-method-specializers-form)))
772 (defun real-parse-specializer-using-class (generic-function specializer)
773 (let ((result (specializer-from-type specializer)))
774 (if (specializerp result)
775 result
776 (error "~@<~S cannot be parsed as a specializer for ~S.~@:>"
777 specializer generic-function))))
779 (unless (fboundp 'parse-specializer-using-class)
780 (setf (gdefinition 'parse-specializer-using-class)
781 (symbol-function 'real-parse-specializer-using-class)))
783 (defun real-unparse-specializer-using-class (generic-function specializer)
784 (if (specializerp specializer)
785 ;; FIXME: this HANDLER-CASE is a bit of a hammer to crack a nut:
786 ;; the idea is that we want to unparse permissively, so that the
787 ;; lazy (or rather the "portable") specializer extender (who
788 ;; does not define methods on these new SBCL-specific MOP
789 ;; functions) can still subclass specializer and define methods
790 ;; without everything going wrong. Making it cleaner and
791 ;; clearer that that is what we are defending against would be
792 ;; nice. -- CSR, 2007-06-01
793 (handler-case
794 (let ((type (specializer-type specializer)))
795 (if (and (consp type) (eq (car type) 'class))
796 (let* ((class (cadr type))
797 (class-name (class-name class)))
798 (if (eq class (find-class class-name nil))
799 class-name
800 type))
801 type))
802 (error () specializer))
803 (error "~@<~S is not a legal specializer for ~S.~@:>"
804 specializer generic-function)))
806 (unless (fboundp 'unparse-specializer-using-class)
807 (setf (gdefinition 'unparse-specializer-using-class)
808 (symbol-function 'real-unparse-specializer-using-class)))
810 ;;; a helper function for creating Python-friendly type declarations
811 ;;; in DEFMETHOD forms.
813 ;;; We're too lazy to cons up a new environment for this, so we just pass in
814 ;;; the list of locally declared specials in addition to the old environment.
815 (defun parameter-specializer-declaration-in-defmethod
816 (parameter specializer specials env)
817 (cond ((and (consp specializer)
818 (eq (car specializer) 'eql))
819 ;; KLUDGE: ANSI, in its wisdom, says that
820 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
821 ;; DEFMETHOD expansion time. Thus, although one might think
822 ;; that in
823 ;; (DEFMETHOD FOO ((X PACKAGE)
824 ;; (Y (EQL 12))
825 ;; ..))
826 ;; the PACKAGE and (EQL 12) forms are both parallel type
827 ;; names, they're not, as is made clear when you do
828 ;; (DEFMETHOD FOO ((X PACKAGE)
829 ;; (Y (EQL 'BAR)))
830 ;; ..)
831 ;; where Y needs to be a symbol named "BAR", not some cons
832 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
833 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
834 ;; to be of type (EQL X). It'd be easy to transform one to
835 ;; the other, but it'd be somewhat messier to do so while
836 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
837 ;; once. (The new code wouldn't be messy, but it'd require a
838 ;; big transformation of the old code.) So instead we punt.
839 ;; -- WHN 20000610
840 '(ignorable))
841 ((member specializer
842 ;; KLUDGE: For some low-level implementation
843 ;; classes, perhaps because of some problems related
844 ;; to the incomplete integration of PCL into SBCL's
845 ;; type system, some specializer classes can't be
846 ;; declared as argument types. E.g.
847 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
848 ;; (DECLARE (TYPE SLOT-OBJECT X))
849 ;; ..)
850 ;; loses when
851 ;; (DEFSTRUCT BAR A B)
852 ;; (FOO (MAKE-BAR))
853 ;; perhaps because of the way that STRUCTURE-OBJECT
854 ;; inherits both from SLOT-OBJECT and from
855 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
856 ;; problems under the rug, we exclude these problem
857 ;; cases by blacklisting them here. -- WHN 2001-01-19
858 (list 'slot-object #+nil (find-class 'slot-object)))
859 '(ignorable))
860 ((not (eq *boot-state* 'complete))
861 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
862 ;; types which don't match their specializers. (Specifically,
863 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
864 ;; second argument.) Hopefully it only does this kind of
865 ;; weirdness when bootstrapping.. -- WHN 20000610
866 '(ignorable))
867 ((typep specializer 'eql-specializer)
868 `(type (eql ,(eql-specializer-object specializer)) ,parameter))
869 ((or (var-special-p parameter env) (member parameter specials))
870 ;; Don't declare types for special variables -- our rebinding magic
871 ;; for SETQ cases don't work right there as SET, (SETF SYMBOL-VALUE),
872 ;; etc. make things undecidable.
873 '(ignorable))
875 ;; Otherwise, we can usually make Python very happy.
877 ;; KLUDGE: Since INFO doesn't work right for class objects here,
878 ;; and they are valid specializers, see if the specializer is
879 ;; a named class, and use the name in that case -- otherwise
880 ;; the class instance is ok, since info will just return NIL, NIL.
882 ;; We still need to deal with the class case too, but at
883 ;; least #.(find-class 'integer) and integer as equivalent
884 ;; specializers with this.
885 (let* ((specializer-nameoid
886 (if (and (typep specializer 'class)
887 (let ((name (class-name specializer)))
888 (and name (symbolp name)
889 (eq specializer (find-class name nil)))))
890 (class-name specializer)
891 specializer))
892 (kind (info :type :kind specializer-nameoid)))
894 (flet ((specializer-nameoid-class ()
895 (typecase specializer-nameoid
896 (symbol (find-class specializer-nameoid nil))
897 (class specializer-nameoid)
898 (class-eq-specializer
899 (specializer-class specializer-nameoid))
900 (t nil))))
901 (ecase kind
902 ((:primitive) `(type ,specializer-nameoid ,parameter))
903 ((:defined)
904 (let ((class (specializer-nameoid-class)))
905 ;; CLASS can be null here if the user has
906 ;; erroneously tried to use a defined type as a
907 ;; specializer; it can be a non-BUILT-IN-CLASS if
908 ;; the user defines a type and calls (SETF
909 ;; FIND-CLASS) in a consistent way.
910 (when (and class (typep class 'built-in-class))
911 `(type ,specializer-nameoid ,parameter))))
912 ((:instance nil)
913 (let ((class (specializer-nameoid-class)))
914 (cond
915 (class
916 (if (typep class '(or built-in-class structure-class))
917 `(type ,class ,parameter)
918 ;; don't declare CLOS classes as parameters;
919 ;; it's too expensive.
920 '(ignorable)))
922 ;; we can get here, and still not have a failure
923 ;; case, by doing MOP programming like (PROGN
924 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
925 ;; ...)). Best to let the user know we haven't
926 ;; been able to extract enough information:
927 (style-warn
928 "~@<can't find type for specializer ~S in ~S.~@:>"
929 specializer-nameoid
930 'parameter-specializer-declaration-in-defmethod)
931 '(ignorable)))))
932 ((:forthcoming-defclass-type)
933 '(ignorable))))))))
935 ;;; For passing a list (groveled by the walker) of the required
936 ;;; parameters whose bindings are modified in the method body to the
937 ;;; optimized-slot-value* macros.
938 (define-symbol-macro %parameter-binding-modified ())
940 (defmacro simple-lexical-method-functions ((lambda-list
941 method-args
942 next-methods
943 &rest lmf-options)
944 &body body)
945 `(progn
946 ,method-args ,next-methods
947 (bind-simple-lexical-method-functions (,method-args ,next-methods
948 ,lmf-options)
949 (bind-args (,lambda-list ,method-args)
950 ,@body))))
952 (defmacro fast-lexical-method-functions ((lambda-list
953 next-method-call
954 args
955 rest-arg
956 &rest lmf-options)
957 &body body)
958 `(bind-fast-lexical-method-functions (,args ,rest-arg ,next-method-call ,lmf-options)
959 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
960 ,@body)))
962 (defmacro bind-simple-lexical-method-functions
963 ((method-args next-methods (&key call-next-method-p next-method-p-p setq-p
964 closurep applyp method-cell))
965 &body body
966 &environment env)
967 (if (not (or call-next-method-p setq-p closurep next-method-p-p applyp))
968 `(locally
969 ,@body)
970 `(let ((.next-method. (car ,next-methods))
971 (,next-methods (cdr ,next-methods)))
972 (declare (ignorable .next-method. ,next-methods))
973 (flet (,@(and call-next-method-p
974 `((call-next-method
975 (&rest cnm-args)
976 ,@(if (safe-code-p env)
977 `((%check-cnm-args cnm-args
978 ,method-args
979 ',method-cell))
980 nil)
981 (if .next-method.
982 (funcall (if (std-instance-p .next-method.)
983 (method-function .next-method.)
984 .next-method.) ; for early methods
985 (or cnm-args ,method-args)
986 ,next-methods)
987 (apply #'call-no-next-method
988 ',method-cell
989 (or cnm-args ,method-args))))))
990 ,@(and next-method-p-p
991 '((next-method-p ()
992 (not (null .next-method.))))))
993 ,@body))))
995 (defun call-no-next-method (method-cell &rest args)
996 (let ((method (car method-cell)))
997 (aver method)
998 (apply #'no-next-method (method-generic-function method)
999 method args)))
1001 (defstruct (method-call (:copier nil))
1002 (function #'identity :type function)
1003 call-method-args)
1004 (defstruct (constant-method-call (:copier nil) (:include method-call))
1005 value)
1007 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
1009 (defmacro invoke-method-call1 (function args cm-args)
1010 `(let ((.function. ,function)
1011 (.args. ,args)
1012 (.cm-args. ,cm-args))
1013 (if (and .cm-args. (null (cdr .cm-args.)))
1014 (funcall .function. .args. (car .cm-args.))
1015 (apply .function. .args. .cm-args.))))
1017 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
1018 `(invoke-method-call1 (method-call-function ,method-call)
1019 ,(if restp
1020 `(list* ,@required-args+rest-arg)
1021 `(list ,@required-args+rest-arg))
1022 (method-call-call-method-args ,method-call)))
1024 (defstruct (fast-method-call (:copier nil))
1025 (function #'identity :type function)
1027 next-method-call
1028 arg-info)
1029 (defstruct (constant-fast-method-call
1030 (:copier nil) (:include fast-method-call))
1031 value)
1033 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
1035 ;; The two variants of INVOKE-FAST-METHOD-CALL differ in how REST-ARGs
1036 ;; are handled. The first one will get REST-ARG as a single list (as
1037 ;; the last argument), and will thus need to use APPLY. The second one
1038 ;; will get them as a &MORE argument, so we can pass the arguments
1039 ;; directly with MULTIPLE-VALUE-CALL and %MORE-ARG-VALUES.
1041 (defmacro invoke-fast-method-call (method-call restp &rest required-args+rest-arg)
1042 `(,(if restp 'apply 'funcall) (fast-method-call-function ,method-call)
1043 (fast-method-call-pv ,method-call)
1044 (fast-method-call-next-method-call ,method-call)
1045 ,@required-args+rest-arg))
1047 (defmacro invoke-fast-method-call/more (method-call
1048 more-context
1049 more-count
1050 &rest required-args)
1051 (macrolet ((generate-call (n)
1052 ``(funcall (fast-method-call-function ,method-call)
1053 (fast-method-call-pv ,method-call)
1054 (fast-method-call-next-method-call ,method-call)
1055 ,@required-args
1056 ,@(loop for x below ,n
1057 collect `(sb-c::%more-arg ,more-context ,x)))))
1058 ;; The cases with only small amounts of required arguments passed
1059 ;; are probably very common, and special-casing speeds them up by
1060 ;; a factor of 2 with very little effect on the other
1061 ;; cases. Though it'd be nice to have the generic case be equally
1062 ;; fast.
1063 `(case ,more-count
1064 (0 ,(generate-call 0))
1065 (1 ,(generate-call 1))
1066 (t (multiple-value-call (fast-method-call-function ,method-call)
1067 (values (fast-method-call-pv ,method-call))
1068 (values (fast-method-call-next-method-call ,method-call))
1069 ,@required-args
1070 (sb-c::%more-arg-values ,more-context 0 ,more-count))))))
1072 (defstruct (fast-instance-boundp (:copier nil))
1073 (index 0 :type fixnum))
1075 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
1077 (eval-when (:compile-toplevel :load-toplevel :execute)
1078 (defvar *allow-emf-call-tracing-p* nil)
1079 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
1081 ;;;; effective method functions
1083 (defvar *emf-call-trace-size* 200)
1084 (defvar *emf-call-trace* nil)
1085 (defvar *emf-call-trace-index* 0)
1087 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
1088 ;;; without explanation. It appears to be intended for debugging, so
1089 ;;; it might be useful someday, so I haven't deleted it.
1090 ;;; But it isn't documented and isn't used for anything now, so
1091 ;;; I've conditionalized it out of the base system. -- WHN 19991213
1092 #+sb-show
1093 (defun show-emf-call-trace ()
1094 (when *emf-call-trace*
1095 (let ((j *emf-call-trace-index*)
1096 (*enable-emf-call-tracing-p* nil))
1097 (format t "~&(The oldest entries are printed first)~%")
1098 (dotimes-fixnum (i *emf-call-trace-size*)
1099 (let ((ct (aref *emf-call-trace* j)))
1100 (when ct (print ct)))
1101 (incf j)
1102 (when (= j *emf-call-trace-size*)
1103 (setq j 0))))))
1105 (defun trace-emf-call-internal (emf format args)
1106 (unless *emf-call-trace*
1107 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
1108 (setf (aref *emf-call-trace* *emf-call-trace-index*)
1109 (list* emf format args))
1110 (incf *emf-call-trace-index*)
1111 (when (= *emf-call-trace-index* *emf-call-trace-size*)
1112 (setq *emf-call-trace-index* 0)))
1114 (defmacro trace-emf-call (emf format args)
1115 (when *allow-emf-call-tracing-p*
1116 `(when *enable-emf-call-tracing-p*
1117 (trace-emf-call-internal ,emf ,format ,args))))
1119 (defmacro invoke-effective-method-function-fast
1120 (emf restp &key required-args rest-arg more-arg)
1121 `(progn
1122 (trace-emf-call ,emf ,restp (list ,@required-args rest-arg))
1123 ,(if more-arg
1124 `(invoke-fast-method-call/more ,emf
1125 ,@more-arg
1126 ,@required-args)
1127 `(invoke-fast-method-call ,emf
1128 ,restp
1129 ,@required-args
1130 ,@rest-arg))))
1132 (defun effective-method-optimized-slot-access-clause
1133 (emf restp required-args)
1134 ;; "What," you may wonder, "do these next two clauses do?" In that
1135 ;; case, you are not a PCL implementor, for they considered this to
1136 ;; be self-documenting.:-| Or CSR, for that matter, since he can
1137 ;; also figure it out by looking at it without breaking stride. For
1138 ;; the rest of us, though: From what the code is doing with .SLOTS.
1139 ;; and whatnot, evidently it's implementing SLOT-VALUEish and
1140 ;; GET-SLOT-VALUEish things. Then we can reason backwards and
1141 ;; conclude that setting EMF to a FIXNUM is an optimized way to
1142 ;; represent these slot access operations.
1143 (when (not restp)
1144 (let ((length (length required-args)))
1145 (cond ((= 1 length)
1146 `((fixnum
1147 (let* ((.slots. (get-slots-or-nil
1148 ,(car required-args)))
1149 (value (when .slots. (clos-slots-ref .slots. ,emf))))
1150 (if (eq value +slot-unbound+)
1151 (slot-unbound-internal ,(car required-args)
1152 ,emf)
1153 value)))))
1154 ((= 2 length)
1155 `((fixnum
1156 (let ((.new-value. ,(car required-args))
1157 (.slots. (get-slots-or-nil
1158 ,(cadr required-args))))
1159 (when .slots.
1160 (setf (clos-slots-ref .slots. ,emf) .new-value.)))))))
1161 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
1162 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
1163 ;; there was no explanation and presumably the code is 10+
1164 ;; years stale, I simply deleted it. -- WHN)
1167 ;;; Before SBCL 0.9.16.7 instead of
1168 ;;; INVOKE-NARROW-EFFECTIVE-METHOD-FUNCTION we passed a (THE (OR
1169 ;;; FUNCTION METHOD-CALL FAST-METHOD-CALL) EMF) form as the EMF. Now,
1170 ;;; to make less work for the compiler we take a path that doesn't
1171 ;;; involve the slot-accessor clause (where EMF is a FIXNUM) at all.
1172 (macrolet ((def (name &optional narrow)
1173 `(defmacro ,name (emf restp &key required-args rest-arg more-arg)
1174 (unless (constantp restp)
1175 (error "The RESTP argument is not constant."))
1176 (setq restp (constant-form-value restp))
1177 (with-unique-names (emf-n)
1178 `(locally
1179 (declare (optimize (sb-c:insert-step-conditions 0)))
1180 (let ((,emf-n ,emf))
1181 (trace-emf-call ,emf-n ,restp (list ,@required-args ,@rest-arg))
1182 (etypecase ,emf-n
1183 (fast-method-call
1184 ,(if more-arg
1185 `(invoke-fast-method-call/more ,emf-n
1186 ,@more-arg
1187 ,@required-args)
1188 `(invoke-fast-method-call ,emf-n
1189 ,restp
1190 ,@required-args
1191 ,@rest-arg)))
1192 ,@,(unless narrow
1193 `(effective-method-optimized-slot-access-clause
1194 emf-n restp required-args))
1195 (method-call
1196 (invoke-method-call ,emf-n ,restp ,@required-args
1197 ,@rest-arg))
1198 (function
1199 ,(if restp
1200 `(apply ,emf-n ,@required-args ,@rest-arg)
1201 `(funcall ,emf-n ,@required-args
1202 ,@rest-arg))))))))))
1203 (def invoke-effective-method-function nil)
1204 (def invoke-narrow-effective-method-function t))
1206 (defun invoke-emf (emf args)
1207 (trace-emf-call emf t args)
1208 (etypecase emf
1209 (fast-method-call
1210 (let* ((arg-info (fast-method-call-arg-info emf))
1211 (restp (cdr arg-info))
1212 (nreq (car arg-info)))
1213 (if restp
1214 (apply (fast-method-call-function emf)
1215 (fast-method-call-pv emf)
1216 (fast-method-call-next-method-call emf)
1217 args)
1218 (cond ((null args)
1219 (if (eql nreq 0)
1220 (invoke-fast-method-call emf nil)
1221 (error 'simple-program-error
1222 :format-control "invalid number of arguments: 0"
1223 :format-arguments nil)))
1224 ((null (cdr args))
1225 (if (eql nreq 1)
1226 (invoke-fast-method-call emf nil (car args))
1227 (error 'simple-program-error
1228 :format-control "invalid number of arguments: 1"
1229 :format-arguments nil)))
1230 ((null (cddr args))
1231 (if (eql nreq 2)
1232 (invoke-fast-method-call emf nil (car args) (cadr args))
1233 (error 'simple-program-error
1234 :format-control "invalid number of arguments: 2"
1235 :format-arguments nil)))
1237 (apply (fast-method-call-function emf)
1238 (fast-method-call-pv emf)
1239 (fast-method-call-next-method-call emf)
1240 args))))))
1241 (method-call
1242 (apply (method-call-function emf)
1243 args
1244 (method-call-call-method-args emf)))
1245 (fixnum
1246 (cond ((null args)
1247 (error 'simple-program-error
1248 :format-control "invalid number of arguments: 0"
1249 :format-arguments nil))
1250 ((null (cdr args))
1251 (let* ((slots (get-slots (car args)))
1252 (value (clos-slots-ref slots emf)))
1253 (if (eq value +slot-unbound+)
1254 (slot-unbound-internal (car args) emf)
1255 value)))
1256 ((null (cddr args))
1257 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1258 (car args)))
1259 (t (error 'simple-program-error
1260 :format-control "invalid number of arguments"
1261 :format-arguments nil))))
1262 (fast-instance-boundp
1263 (if (or (null args) (cdr args))
1264 (error 'simple-program-error
1265 :format-control "invalid number of arguments"
1266 :format-arguments nil)
1267 (let ((slots (get-slots (car args))))
1268 (not (eq (clos-slots-ref slots (fast-instance-boundp-index emf))
1269 +slot-unbound+)))))
1270 (function
1271 (apply emf args))))
1274 (defmacro fast-call-next-method-body ((args next-method-call rest-arg)
1275 method-cell
1276 cnm-args)
1277 `(if ,next-method-call
1278 ,(let ((call `(invoke-narrow-effective-method-function
1279 ,next-method-call
1280 ,(not (null rest-arg))
1281 :required-args ,args
1282 :rest-arg ,(when rest-arg (list rest-arg)))))
1283 `(if ,cnm-args
1284 (bind-args ((,@args
1285 ,@(when rest-arg
1286 `(&rest ,rest-arg)))
1287 ,cnm-args)
1288 ,call)
1289 ,call))
1290 (call-no-next-method ',method-cell
1291 ,@args
1292 ,@(when rest-arg
1293 `(,rest-arg)))))
1295 (defmacro bind-fast-lexical-method-functions
1296 ((args rest-arg next-method-call (&key
1297 call-next-method-p
1298 setq-p
1299 method-cell
1300 next-method-p-p
1301 closurep
1302 applyp))
1303 &body body
1304 &environment env)
1305 (let* ((all-params (append args (when rest-arg (list rest-arg))))
1306 (rebindings (when (or setq-p call-next-method-p)
1307 (mapcar (lambda (x) (list x x)) all-params))))
1308 (if (not (or call-next-method-p setq-p closurep next-method-p-p applyp))
1309 `(locally
1310 ,@body)
1311 `(flet (,@(when call-next-method-p
1312 `((call-next-method (&rest cnm-args)
1313 (declare (muffle-conditions code-deletion-note)
1314 (optimize (sb-c:insert-step-conditions 0)))
1315 ,@(if (safe-code-p env)
1316 `((%check-cnm-args cnm-args (list ,@args)
1317 ',method-cell))
1318 nil)
1319 (fast-call-next-method-body (,args
1320 ,next-method-call
1321 ,rest-arg)
1322 ,method-cell
1323 cnm-args))))
1324 ,@(when next-method-p-p
1325 `((next-method-p ()
1326 (declare (optimize (sb-c:insert-step-conditions 0)))
1327 (not (null ,next-method-call))))))
1328 (let ,rebindings
1329 ,@(when rebindings `((declare (ignorable ,@all-params))))
1330 ,@body)))))
1332 ;;; CMUCL comment (Gerd Moellmann):
1334 ;;; The standard says it's an error if CALL-NEXT-METHOD is called with
1335 ;;; arguments, and the set of methods applicable to those arguments is
1336 ;;; different from the set of methods applicable to the original
1337 ;;; method arguments. (According to Barry Margolin, this rule was
1338 ;;; probably added to ensure that before and around methods are always
1339 ;;; run before primary methods.)
1341 ;;; This could be optimized for the case that the generic function
1342 ;;; doesn't have hairy methods, does have standard method combination,
1343 ;;; is a standard generic function, there are no methods defined on it
1344 ;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such
1345 ;;; preconditions. That looks hairy and is probably not worth it,
1346 ;;; because this check will never be fast.
1347 (defun %check-cnm-args (cnm-args orig-args method-cell)
1348 (when cnm-args
1349 (let* ((gf (method-generic-function (car method-cell)))
1350 (omethods (compute-applicable-methods gf orig-args))
1351 (nmethods (compute-applicable-methods gf cnm-args)))
1352 (unless (equal omethods nmethods)
1353 (error "~@<The set of methods ~S applicable to argument~P ~
1354 ~{~S~^, ~} to call-next-method is different from ~
1355 the set of methods ~S applicable to the original ~
1356 method argument~P ~{~S~^, ~}.~@:>"
1357 nmethods (length cnm-args) cnm-args omethods
1358 (length orig-args) orig-args)))))
1360 (defmacro bind-args ((lambda-list args) &body body)
1361 (let ((args-tail '.args-tail.)
1362 (key '.key.)
1363 (state 'required))
1364 (flet ((process-var (var)
1365 (if (memq var lambda-list-keywords)
1366 (progn
1367 (case var
1368 (&optional (setq state 'optional))
1369 (&key (setq state 'key))
1370 (&allow-other-keys)
1371 (&rest (setq state 'rest))
1372 (&aux (setq state 'aux))
1373 (otherwise
1374 (error
1375 "encountered the non-standard lambda list keyword ~S"
1376 var)))
1377 nil)
1378 (case state
1379 (required `((,var (pop ,args-tail))))
1380 (optional (cond ((not (consp var))
1381 `((,var (when ,args-tail
1382 (pop ,args-tail)))))
1383 ((null (cddr var))
1384 `((,(car var) (if ,args-tail
1385 (pop ,args-tail)
1386 ,(cadr var)))))
1388 `((,(caddr var) (not (null ,args-tail)))
1389 (,(car var) (if ,args-tail
1390 (pop ,args-tail)
1391 ,(cadr var)))))))
1392 (rest `((,var ,args-tail)))
1393 (key (cond ((not (consp var))
1394 `((,var (car
1395 (get-key-arg-tail ,(keywordicate var)
1396 ,args-tail)))))
1397 ((null (cddr var))
1398 (multiple-value-bind (keyword variable)
1399 (if (consp (car var))
1400 (values (caar var)
1401 (cadar var))
1402 (values (keywordicate (car var))
1403 (car var)))
1404 `((,key (get-key-arg-tail ',keyword
1405 ,args-tail))
1406 (,variable (if ,key
1407 (car ,key)
1408 ,(cadr var))))))
1410 (multiple-value-bind (keyword variable)
1411 (if (consp (car var))
1412 (values (caar var)
1413 (cadar var))
1414 (values (keywordicate (car var))
1415 (car var)))
1416 `((,key (get-key-arg-tail ',keyword
1417 ,args-tail))
1418 (,(caddr var) (not (null,key)))
1419 (,variable (if ,key
1420 (car ,key)
1421 ,(cadr var))))))))
1422 (aux `(,var))))))
1423 (let ((bindings (mapcan #'process-var lambda-list)))
1424 `(let* ((,args-tail ,args)
1425 ,@bindings
1426 (.dummy0.
1427 ,@(when (eq state 'optional)
1428 `((unless (null ,args-tail)
1429 (error 'simple-program-error
1430 :format-control "surplus arguments: ~S"
1431 :format-arguments (list ,args-tail)))))))
1432 (declare (ignorable ,args-tail .dummy0.))
1433 ,@body)))))
1435 (defun get-key-arg-tail (keyword list)
1436 (loop for (key . tail) on list by #'cddr
1437 when (null tail) do
1438 ;; FIXME: Do we want to export this symbol? Or maybe use an
1439 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1440 (sb-c::%odd-key-args-error)
1441 when (eq key keyword)
1442 return tail))
1444 (defun walk-method-lambda (method-lambda required-parameters env slots)
1445 (let (;; flag indicating that CALL-NEXT-METHOD should be in the
1446 ;; method definition
1447 (call-next-method-p nil)
1448 ;; flag indicating that #'CALL-NEXT-METHOD was seen in the
1449 ;; body of a method
1450 (closurep nil)
1451 ;; flag indicating that NEXT-METHOD-P should be in the method
1452 ;; definition
1453 (next-method-p-p nil)
1454 ;; a list of all required parameters whose bindings might be
1455 ;; modified in the method body.
1456 (parameters-setqd nil))
1457 (flet ((walk-function (form context env)
1458 (cond ((not (eq context :eval)) form)
1459 ;; FIXME: Jumping to a conclusion from the way it's used
1460 ;; above, perhaps CONTEXT should be called SITUATION
1461 ;; (after the term used in the ANSI specification of
1462 ;; EVAL-WHEN) and given modern ANSI keyword values
1463 ;; like :LOAD-TOPLEVEL.
1464 ((not (listp form)) form)
1465 ((eq (car form) 'call-next-method)
1466 (setq call-next-method-p t)
1467 form)
1468 ((eq (car form) 'next-method-p)
1469 (setq next-method-p-p t)
1470 form)
1471 ((memq (car form) '(setq multiple-value-setq))
1472 ;; FIXME: this is possibly a little strong as
1473 ;; conditions go. Ideally we would want to detect
1474 ;; which, if any, of the method parameters are
1475 ;; being set, and communicate that information to
1476 ;; e.g. SPLIT-DECLARATIONS. However, the brute
1477 ;; force method doesn't really cost much; a little
1478 ;; loss of discrimination over IGNORED variables
1479 ;; should be all. -- CSR, 2004-07-01
1481 ;; As of 2006-09-18 modified parameter bindings
1482 ;; are now tracked with more granularity than just
1483 ;; one SETQ-P flag, in order to disable SLOT-VALUE
1484 ;; optimizations for parameters that are SETQd.
1485 ;; The old binary SETQ-P flag is still used for
1486 ;; all other purposes, since as noted above, the
1487 ;; extra cost is minimal. -- JES, 2006-09-18
1489 ;; The walker will split (SETQ A 1 B 2) to
1490 ;; separate (SETQ A 1) and (SETQ B 2) forms, so we
1491 ;; only need to handle the simple case of SETQ
1492 ;; here.
1493 (let ((vars (if (eq (car form) 'setq)
1494 (list (second form))
1495 (second form))))
1496 (dolist (var vars)
1497 ;; Note that we don't need to check for
1498 ;; %VARIABLE-REBINDING declarations like is
1499 ;; done in CAN-OPTIMIZE-ACCESS1, since the
1500 ;; bindings that will have that declation will
1501 ;; never be SETQd.
1502 (when (var-declaration '%class var env)
1503 ;; If a parameter binding is shadowed by
1504 ;; another binding it won't have a %CLASS
1505 ;; declaration anymore, and this won't get
1506 ;; executed.
1507 (pushnew var parameters-setqd :test #'eq))))
1508 form)
1509 ((and (eq (car form) 'function)
1510 (cond ((eq (cadr form) 'call-next-method)
1511 (setq call-next-method-p t)
1512 (setq closurep t)
1513 form)
1514 ((eq (cadr form) 'next-method-p)
1515 (setq next-method-p-p t)
1516 (setq closurep t)
1517 form)
1518 (t nil))))
1519 ((and (memq (car form)
1520 '(slot-value set-slot-value slot-boundp))
1521 (constantp (caddr form) env))
1522 (let ((fun (ecase (car form)
1523 (slot-value #'optimize-slot-value)
1524 (set-slot-value #'optimize-set-slot-value)
1525 (slot-boundp #'optimize-slot-boundp))))
1526 (funcall fun form slots required-parameters env)))
1527 (t form))))
1529 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1530 ;;; FIXME: the walker's rewriting of the source code causes
1531 ;;; trouble when doing code coverage. The rewrites should be
1532 ;;; removed, and the same operations done using
1533 ;;; compiler-macros or tranforms.
1534 (values (if (sb-c:policy env (= sb-c:store-coverage-data 0))
1535 walked-lambda
1536 method-lambda)
1537 call-next-method-p
1538 closurep
1539 next-method-p-p
1540 (not (null parameters-setqd))
1541 parameters-setqd)))))
1543 (defun generic-function-name-p (name)
1544 (and (legal-fun-name-p name)
1545 (fboundp name)
1546 (if (eq *boot-state* 'complete)
1547 (standard-generic-function-p (gdefinition name))
1548 (funcallable-instance-p (gdefinition name)))))
1550 (defun method-plist-value (method key &optional default)
1551 (let ((plist (if (consp method)
1552 (getf (early-method-initargs method) 'plist)
1553 (object-plist method))))
1554 (getf plist key default)))
1556 (defun (setf method-plist-value) (new-value method key &optional default)
1557 (if (consp method)
1558 (setf (getf (getf (early-method-initargs method) 'plist) key default)
1559 new-value)
1560 (setf (getf (object-plist method) key default) new-value)))
1562 (defun load-defmethod (class name quals specls ll initargs source-location)
1563 (let ((method-cell (getf initargs 'method-cell)))
1564 (setq initargs (copy-tree initargs))
1565 (when method-cell
1566 (setf (getf initargs 'method-cell) method-cell))
1567 #+nil
1568 (setf (getf (getf initargs 'plist) :name)
1569 (make-method-spec name quals specls))
1570 (load-defmethod-internal class name quals specls
1571 ll initargs source-location)))
1573 (defun load-defmethod-internal
1574 (method-class gf-spec qualifiers specializers lambda-list
1575 initargs source-location)
1576 (when (and (eq *boot-state* 'complete)
1577 (fboundp gf-spec))
1578 (let* ((gf (fdefinition gf-spec))
1579 (method (and (generic-function-p gf)
1580 (generic-function-methods gf)
1581 (find-method gf qualifiers specializers nil))))
1582 (when method
1583 (style-warn 'sb-kernel:redefinition-with-defmethod
1584 :generic-function gf-spec :old-method method
1585 :qualifiers qualifiers :specializers specializers
1586 :new-location source-location))))
1587 (let ((method (apply #'add-named-method
1588 gf-spec qualifiers specializers lambda-list
1589 :definition-source source-location
1590 initargs)))
1591 (unless (or (eq method-class 'standard-method)
1592 (eq (find-class method-class nil) (class-of method)))
1593 ;; FIXME: should be STYLE-WARNING?
1594 (format *error-output*
1595 "~&At the time the method with qualifiers ~:S and~%~
1596 specializers ~:S on the generic function ~S~%~
1597 was compiled, the method-class for that generic function was~%~
1598 ~S. But, the method class is now ~S, this~%~
1599 may mean that this method was compiled improperly.~%"
1600 qualifiers specializers gf-spec
1601 method-class (class-name (class-of method))))
1602 method))
1604 (defun make-method-spec (gf qualifiers specializers)
1605 (let ((name (generic-function-name gf))
1606 (unparsed-specializers (unparse-specializers gf specializers)))
1607 `(slow-method ,name ,@qualifiers ,unparsed-specializers)))
1609 (defun initialize-method-function (initargs method)
1610 (let* ((mf (getf initargs :function))
1611 (mff (and (typep mf '%method-function)
1612 (%method-function-fast-function mf)))
1613 (plist (getf initargs 'plist))
1614 (name (getf plist :name))
1615 (method-cell (getf initargs 'method-cell)))
1616 (when method-cell
1617 (setf (car method-cell) method))
1618 (when name
1619 (when mf
1620 (setq mf (set-fun-name mf name)))
1621 (when (and mff (consp name) (eq (car name) 'slow-method))
1622 (let ((fast-name `(fast-method ,@(cdr name))))
1623 (set-fun-name mff fast-name))))
1624 (when plist
1625 (let ((plist plist))
1626 (let ((snl (getf plist :slot-name-lists)))
1627 (when snl
1628 (setf (method-plist-value method :pv-table)
1629 (intern-pv-table :slot-name-lists snl))))))))
1631 (defun analyze-lambda-list (lambda-list)
1632 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1633 (parse-key-arg (arg)
1634 (if (listp arg)
1635 (if (listp (car arg))
1636 (caar arg)
1637 (keywordicate (car arg)))
1638 (keywordicate arg))))
1639 (let ((nrequired 0)
1640 (noptional 0)
1641 (keysp nil)
1642 (restp nil)
1643 (nrest 0)
1644 (allow-other-keys-p nil)
1645 (keywords ())
1646 (keyword-parameters ())
1647 (state 'required))
1648 (dolist (x lambda-list)
1649 (if (memq x lambda-list-keywords)
1650 (case x
1651 (&optional (setq state 'optional))
1652 (&key (setq keysp t
1653 state 'key))
1654 (&allow-other-keys (setq allow-other-keys-p t))
1655 (&rest (setq restp t
1656 state 'rest))
1657 (&aux (return t))
1658 (otherwise
1659 (error "encountered the non-standard lambda list keyword ~S"
1660 x)))
1661 (ecase state
1662 (required (incf nrequired))
1663 (optional (incf noptional))
1664 (key (push (parse-key-arg x) keywords)
1665 (push x keyword-parameters))
1666 (rest (incf nrest)))))
1667 (when (and restp (zerop nrest))
1668 (error "Error in lambda-list:~%~
1669 After &REST, a DEFGENERIC lambda-list ~
1670 must be followed by at least one variable."))
1671 (values nrequired noptional keysp restp allow-other-keys-p
1672 (reverse keywords)
1673 (reverse keyword-parameters)))))
1675 (defun keyword-spec-name (x)
1676 (let ((key (if (atom x) x (car x))))
1677 (if (atom key)
1678 (keywordicate key)
1679 (car key))))
1681 (defun ftype-declaration-from-lambda-list (lambda-list name)
1682 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1683 keywords keyword-parameters)
1684 (analyze-lambda-list lambda-list)
1685 (declare (ignore keyword-parameters))
1686 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1687 (old-ftype (if (fun-type-p old) old nil))
1688 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1689 (old-keys (and old-ftype
1690 (mapcar #'key-info-name
1691 (fun-type-keywords
1692 old-ftype))))
1693 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1694 (old-allowp (and old-ftype
1695 (fun-type-allowp old-ftype)))
1696 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1697 `(function ,(append (make-list nrequired :initial-element t)
1698 (when (plusp noptional)
1699 (append '(&optional)
1700 (make-list noptional :initial-element t)))
1701 (when (or restp old-restp)
1702 '(&rest t))
1703 (when (or keysp old-keysp)
1704 (append '(&key)
1705 (mapcar (lambda (key)
1706 `(,key t))
1707 keywords)
1708 (when (or allow-other-keys-p old-allowp)
1709 '(&allow-other-keys)))))
1710 *))))
1712 ;;;; early generic function support
1714 (defvar *!early-generic-functions* ())
1716 (defun ensure-generic-function (fun-name
1717 &rest all-keys
1718 &key environment source-location
1719 &allow-other-keys)
1720 (declare (ignore environment))
1721 (let ((existing (and (fboundp fun-name)
1722 (gdefinition fun-name))))
1723 (cond ((and existing
1724 (eq *boot-state* 'complete)
1725 (null (generic-function-p existing)))
1726 (generic-clobbers-function fun-name)
1727 (fmakunbound fun-name)
1728 (apply #'ensure-generic-function fun-name all-keys))
1730 (apply #'ensure-generic-function-using-class
1731 existing fun-name all-keys)))))
1733 (defun generic-clobbers-function (fun-name)
1734 (cerror "Replace the function binding"
1735 'simple-program-error
1736 :format-control "~S already names an ordinary function or a macro."
1737 :format-arguments (list fun-name)))
1739 (defvar *sgf-wrapper*
1740 (boot-make-wrapper (early-class-size 'standard-generic-function)
1741 'standard-generic-function))
1743 (defvar *sgf-slots-init*
1744 (mapcar (lambda (canonical-slot)
1745 (if (memq (getf canonical-slot :name) '(arg-info source))
1746 +slot-unbound+
1747 (let ((initfunction (getf canonical-slot :initfunction)))
1748 (if initfunction
1749 (funcall initfunction)
1750 +slot-unbound+))))
1751 (early-collect-inheritance 'standard-generic-function)))
1753 (defvar *sgf-method-class-index*
1754 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1756 (defun early-gf-p (x)
1757 (and (fsc-instance-p x)
1758 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1759 +slot-unbound+)))
1761 (defvar *sgf-methods-index*
1762 (!bootstrap-slot-index 'standard-generic-function 'methods))
1764 (defmacro early-gf-methods (gf)
1765 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1767 (defun safe-generic-function-methods (generic-function)
1768 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1769 (clos-slots-ref (get-slots generic-function) *sgf-methods-index*)
1770 (generic-function-methods generic-function)))
1772 (defvar *sgf-arg-info-index*
1773 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1775 (defmacro early-gf-arg-info (gf)
1776 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1778 (defvar *sgf-dfun-state-index*
1779 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1781 (defstruct (arg-info
1782 (:conc-name nil)
1783 (:constructor make-arg-info ())
1784 (:copier nil))
1785 (arg-info-lambda-list :no-lambda-list)
1786 arg-info-precedence
1787 arg-info-metatypes
1788 arg-info-number-optional
1789 arg-info-key/rest-p
1790 arg-info-keys ;nil no &KEY or &REST allowed
1791 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1792 ;T must have &KEY or &REST
1794 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1795 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1797 gf-info-static-c-a-m-emf
1798 (gf-info-c-a-m-emf-std-p t)
1799 gf-info-fast-mf-p)
1801 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1803 (defun arg-info-valid-p (arg-info)
1804 (not (null (arg-info-number-optional arg-info))))
1806 (defun arg-info-applyp (arg-info)
1807 (or (plusp (arg-info-number-optional arg-info))
1808 (arg-info-key/rest-p arg-info)))
1810 (defun arg-info-number-required (arg-info)
1811 (length (arg-info-metatypes arg-info)))
1813 (defun arg-info-nkeys (arg-info)
1814 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1816 (defun create-gf-lambda-list (lambda-list)
1817 ;;; Create a gf lambda list from a method lambda list
1818 (loop for x in lambda-list
1819 collect (if (consp x) (list (car x)) x)
1820 if (eq x '&key) do (loop-finish)))
1822 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1823 argument-precedence-order)
1824 (let* ((arg-info (if (eq *boot-state* 'complete)
1825 (gf-arg-info gf)
1826 (early-gf-arg-info gf)))
1827 (methods (if (eq *boot-state* 'complete)
1828 (generic-function-methods gf)
1829 (early-gf-methods gf)))
1830 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1831 (first-p (and new-method (null (cdr methods)))))
1832 (when (and (not lambda-list-p) methods)
1833 (setq lambda-list (gf-lambda-list gf)))
1834 (when (or lambda-list-p
1835 (and first-p
1836 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1837 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1838 (analyze-lambda-list lambda-list)
1839 (when (and methods (not first-p))
1840 (let ((gf-nreq (arg-info-number-required arg-info))
1841 (gf-nopt (arg-info-number-optional arg-info))
1842 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1843 (unless (and (= nreq gf-nreq)
1844 (= nopt gf-nopt)
1845 (eq (or keysp restp) gf-key/rest-p))
1846 (error "The lambda-list ~S is incompatible with ~
1847 existing methods of ~S."
1848 lambda-list gf))))
1849 (setf (arg-info-lambda-list arg-info)
1850 (if lambda-list-p
1851 lambda-list
1852 (create-gf-lambda-list lambda-list)))
1853 (when (or lambda-list-p argument-precedence-order
1854 (null (arg-info-precedence arg-info)))
1855 (setf (arg-info-precedence arg-info)
1856 (compute-precedence lambda-list nreq argument-precedence-order)))
1857 (setf (arg-info-metatypes arg-info) (make-list nreq))
1858 (setf (arg-info-number-optional arg-info) nopt)
1859 (setf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1860 (setf (arg-info-keys arg-info)
1861 (if lambda-list-p
1862 (if allow-other-keys-p t keywords)
1863 (arg-info-key/rest-p arg-info)))))
1864 (when new-method
1865 (check-method-arg-info gf arg-info new-method))
1866 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1867 arg-info))
1869 (defun check-method-arg-info (gf arg-info method)
1870 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1871 (analyze-lambda-list (if (consp method)
1872 (early-method-lambda-list method)
1873 (method-lambda-list method)))
1874 (flet ((lose (string &rest args)
1875 (error 'simple-program-error
1876 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1877 to the generic function~2I~_~S;~I~_~
1878 but ~?~:>"
1879 :format-arguments (list method gf string args)))
1880 (comparison-description (x y)
1881 (if (> x y) "more" "fewer")))
1882 (let ((gf-nreq (arg-info-number-required arg-info))
1883 (gf-nopt (arg-info-number-optional arg-info))
1884 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1885 (gf-keywords (arg-info-keys arg-info)))
1886 (unless (= nreq gf-nreq)
1887 (lose
1888 "the method has ~A required arguments than the generic function."
1889 (comparison-description nreq gf-nreq)))
1890 (unless (= nopt gf-nopt)
1891 (lose
1892 "the method has ~A optional arguments than the generic function."
1893 (comparison-description nopt gf-nopt)))
1894 (unless (eq (or keysp restp) gf-key/rest-p)
1895 (lose
1896 "the method and generic function differ in whether they accept~_~
1897 &REST or &KEY arguments."))
1898 (when (consp gf-keywords)
1899 (unless (or (and restp (not keysp))
1900 allow-other-keys-p
1901 (every (lambda (k) (memq k keywords)) gf-keywords))
1902 (lose "the method does not accept each of the &KEY arguments~2I~_~
1903 ~S."
1904 gf-keywords)))))))
1906 (defvar *sm-specializers-index*
1907 (!bootstrap-slot-index 'standard-method 'specializers))
1908 (defvar *sm-%function-index*
1909 (!bootstrap-slot-index 'standard-method '%function))
1910 (defvar *sm-qualifiers-index*
1911 (!bootstrap-slot-index 'standard-method 'qualifiers))
1912 (defvar *sm-plist-index*
1913 (!bootstrap-slot-index 'standard-method 'plist))
1915 ;;; FIXME: we don't actually need this; we could test for the exact
1916 ;;; class and deal with it as appropriate. In fact we probably don't
1917 ;;; need it anyway because we only use this for METHOD-SPECIALIZERS on
1918 ;;; the standard reader method for METHOD-SPECIALIZERS. Probably.
1919 (dolist (s '(specializers %function plist))
1920 (aver (= (symbol-value (intern (format nil "*SM-~A-INDEX*" s)))
1921 (!bootstrap-slot-index 'standard-reader-method s)
1922 (!bootstrap-slot-index 'standard-writer-method s)
1923 (!bootstrap-slot-index 'standard-boundp-method s)
1924 (!bootstrap-slot-index 'global-reader-method s)
1925 (!bootstrap-slot-index 'global-writer-method s)
1926 (!bootstrap-slot-index 'global-boundp-method s))))
1928 (define-symbol-macro *standard-method-classes*
1929 (list *the-class-standard-method* *the-class-standard-reader-method*
1930 *the-class-standard-writer-method* *the-class-standard-boundp-method*
1931 *the-class-global-reader-method* *the-class-global-writer-method*
1932 *the-class-global-boundp-method*))
1934 (defun safe-method-specializers (method)
1935 (let ((standard-method-classes *standard-method-classes*)
1936 (class (class-of method)))
1937 (if (member class standard-method-classes)
1938 (clos-slots-ref (get-slots method) *sm-specializers-index*)
1939 (method-specializers method))))
1940 (defun safe-method-fast-function (method)
1941 (let ((mf (safe-method-function method)))
1942 (and (typep mf '%method-function)
1943 (%method-function-fast-function mf))))
1944 (defun safe-method-function (method)
1945 (let ((standard-method-classes *standard-method-classes*)
1946 (class (class-of method)))
1947 (if (member class standard-method-classes)
1948 (clos-slots-ref (get-slots method) *sm-%function-index*)
1949 (method-function method))))
1950 (defun safe-method-qualifiers (method)
1951 (let ((standard-method-classes *standard-method-classes*)
1952 (class (class-of method)))
1953 (if (member class standard-method-classes)
1954 (clos-slots-ref (get-slots method) *sm-qualifiers-index*)
1955 (method-qualifiers method))))
1957 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1958 (let* ((existing-p (and methods (cdr methods) new-method))
1959 (nreq (length (arg-info-metatypes arg-info)))
1960 (metatypes (if existing-p
1961 (arg-info-metatypes arg-info)
1962 (make-list nreq)))
1963 (type (if existing-p
1964 (gf-info-simple-accessor-type arg-info)
1965 nil)))
1966 (when (arg-info-valid-p arg-info)
1967 (dolist (method (if new-method (list new-method) methods))
1968 (let* ((specializers (if (or (eq *boot-state* 'complete)
1969 (not (consp method)))
1970 (safe-method-specializers method)
1971 (early-method-specializers method t)))
1972 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1973 (class-of method)
1974 (early-method-class method)))
1975 (new-type
1976 (when (and class
1977 (or (not (eq *boot-state* 'complete))
1978 (eq (generic-function-method-combination gf)
1979 *standard-method-combination*)))
1980 (cond ((or (eq class *the-class-standard-reader-method*)
1981 (eq class *the-class-global-reader-method*))
1982 'reader)
1983 ((or (eq class *the-class-standard-writer-method*)
1984 (eq class *the-class-global-writer-method*))
1985 'writer)
1986 ((or (eq class *the-class-standard-boundp-method*)
1987 (eq class *the-class-global-boundp-method*))
1988 'boundp)))))
1989 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1990 (setq type (cond ((null type) new-type)
1991 ((eq type new-type) type)
1992 (t nil)))))
1993 (setf (arg-info-metatypes arg-info) metatypes)
1994 (setf (gf-info-simple-accessor-type arg-info) type)))
1995 (when (or (not was-valid-p) first-p)
1996 (multiple-value-bind (c-a-m-emf std-p)
1997 (if (early-gf-p gf)
1998 (values t t)
1999 (compute-applicable-methods-emf gf))
2000 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
2001 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
2002 (unless (gf-info-c-a-m-emf-std-p arg-info)
2003 (setf (gf-info-simple-accessor-type arg-info) t))))
2004 (unless was-valid-p
2005 (let ((name (if (eq *boot-state* 'complete)
2006 (generic-function-name gf)
2007 (!early-gf-name gf))))
2008 (setf (gf-precompute-dfun-and-emf-p arg-info)
2009 (cond
2010 ((and (consp name)
2011 (member (car name)
2012 *internal-pcl-generalized-fun-name-symbols*))
2013 nil)
2014 (t (let* ((symbol (fun-name-block-name name))
2015 (package (symbol-package symbol)))
2016 (and (or (eq package *pcl-package*)
2017 (memq package (package-use-list *pcl-package*)))
2018 (not (eq package #.(find-package "CL")))
2019 ;; FIXME: this test will eventually be
2020 ;; superseded by the *internal-pcl...* test,
2021 ;; above. While we are in a process of
2022 ;; transition, however, it should probably
2023 ;; remain.
2024 (not (find #\Space (symbol-name symbol))))))))))
2025 (setf (gf-info-fast-mf-p arg-info)
2026 (or (not (eq *boot-state* 'complete))
2027 (let* ((method-class (generic-function-method-class gf))
2028 (methods (compute-applicable-methods
2029 #'make-method-lambda
2030 (list gf (class-prototype method-class)
2031 '(lambda) nil))))
2032 (and methods (null (cdr methods))
2033 (let ((specls (method-specializers (car methods))))
2034 (and (classp (car specls))
2035 (eq 'standard-generic-function
2036 (class-name (car specls)))
2037 (classp (cadr specls))
2038 (eq 'standard-method
2039 (class-name (cadr specls)))))))))
2040 arg-info)
2042 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
2044 ;;; The STATIC-SLOTS field of the funcallable instances used as early
2045 ;;; generic functions is used to store the early methods and early
2046 ;;; discriminator code for the early generic function. The static
2047 ;;; slots field of the fins contains a list whose:
2048 ;;; CAR - a list of the early methods on this early gf
2049 ;;; CADR - the early discriminator code for this method
2050 (defun ensure-generic-function-using-class (existing spec &rest keys
2051 &key (lambda-list nil
2052 lambda-list-p)
2053 argument-precedence-order
2054 source-location
2055 documentation
2056 &allow-other-keys)
2057 (declare (ignore keys))
2058 (cond ((and existing (early-gf-p existing))
2059 (when lambda-list-p
2060 (set-arg-info existing :lambda-list lambda-list))
2061 existing)
2062 ((assoc spec *!generic-function-fixups* :test #'equal)
2063 (if existing
2064 (make-early-gf spec lambda-list lambda-list-p existing
2065 argument-precedence-order source-location
2066 documentation)
2067 (bug "The function ~S is not already defined." spec)))
2068 (existing
2069 (bug "~S should be on the list ~S."
2070 spec '*!generic-function-fixups*))
2072 (pushnew spec *!early-generic-functions* :test #'equal)
2073 (make-early-gf spec lambda-list lambda-list-p nil
2074 argument-precedence-order source-location
2075 documentation))))
2077 (defun make-early-gf (spec &optional lambda-list lambda-list-p
2078 function argument-precedence-order source-location
2079 documentation)
2080 (let ((fin (allocate-standard-funcallable-instance
2081 *sgf-wrapper* *sgf-slots-init*)))
2082 (set-funcallable-instance-function
2084 (or function
2085 (if (eq spec 'print-object)
2086 #'(lambda (instance stream)
2087 (print-unreadable-object (instance stream :identity t)
2088 (format stream "std-instance")))
2089 #'(lambda (&rest args)
2090 (declare (ignore args))
2091 (error "The function of the funcallable-instance ~S~
2092 has not been set." fin)))))
2093 (setf (gdefinition spec) fin)
2094 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
2095 (!bootstrap-set-slot 'standard-generic-function fin
2096 'source source-location)
2097 (!bootstrap-set-slot 'standard-generic-function fin
2098 '%documentation documentation)
2099 (set-fun-name fin spec)
2100 (let ((arg-info (make-arg-info)))
2101 (setf (early-gf-arg-info fin) arg-info)
2102 (when lambda-list-p
2103 (setf (info :function :type spec)
2104 (specifier-type
2105 (ftype-declaration-from-lambda-list lambda-list spec))
2106 (info :function :where-from spec) :defined-method)
2107 (if argument-precedence-order
2108 (set-arg-info fin
2109 :lambda-list lambda-list
2110 :argument-precedence-order argument-precedence-order)
2111 (set-arg-info fin :lambda-list lambda-list))))
2112 fin))
2114 (defun safe-gf-dfun-state (generic-function)
2115 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2116 (clos-slots-ref (get-slots generic-function) *sgf-dfun-state-index*)
2117 (gf-dfun-state generic-function)))
2118 (defun (setf safe-gf-dfun-state) (new-value generic-function)
2119 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2120 (setf (clos-slots-ref (get-slots generic-function)
2121 *sgf-dfun-state-index*)
2122 new-value)
2123 (setf (gf-dfun-state generic-function) new-value)))
2125 (defun set-dfun (gf &optional dfun cache info)
2126 (let ((new-state (if (and dfun (or cache info))
2127 (list* dfun cache info)
2128 dfun)))
2129 (cond
2130 ((eq *boot-state* 'complete)
2131 ;; Check that we are under the lock.
2132 #+sb-thread
2133 (aver (eq sb-thread:*current-thread* (sb-thread::spinlock-value (gf-lock gf))))
2134 (setf (safe-gf-dfun-state gf) new-state))
2136 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
2137 new-state))))
2138 dfun)
2140 (defun gf-dfun-cache (gf)
2141 (let ((state (if (eq *boot-state* 'complete)
2142 (safe-gf-dfun-state gf)
2143 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
2144 (typecase state
2145 (function nil)
2146 (cons (cadr state)))))
2148 (defun gf-dfun-info (gf)
2149 (let ((state (if (eq *boot-state* 'complete)
2150 (safe-gf-dfun-state gf)
2151 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
2152 (typecase state
2153 (function nil)
2154 (cons (cddr state)))))
2156 (defvar *sgf-name-index*
2157 (!bootstrap-slot-index 'standard-generic-function 'name))
2159 (defun !early-gf-name (gf)
2160 (clos-slots-ref (get-slots gf) *sgf-name-index*))
2162 (defun gf-lambda-list (gf)
2163 (let ((arg-info (if (eq *boot-state* 'complete)
2164 (gf-arg-info gf)
2165 (early-gf-arg-info gf))))
2166 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
2167 (let ((methods (if (eq *boot-state* 'complete)
2168 (generic-function-methods gf)
2169 (early-gf-methods gf))))
2170 (if (null methods)
2171 (progn
2172 (warn "no way to determine the lambda list for ~S" gf)
2173 nil)
2174 (let* ((method (car (last methods)))
2175 (ll (if (consp method)
2176 (early-method-lambda-list method)
2177 (method-lambda-list method))))
2178 (create-gf-lambda-list ll))))
2179 (arg-info-lambda-list arg-info))))
2181 (defmacro real-ensure-gf-internal (gf-class all-keys env)
2182 `(progn
2183 (cond ((symbolp ,gf-class)
2184 (setq ,gf-class (find-class ,gf-class t ,env)))
2185 ((classp ,gf-class))
2187 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
2188 class nor a symbol that names a class."
2189 ,gf-class)))
2190 (unless (class-finalized-p ,gf-class)
2191 (if (class-has-a-forward-referenced-superclass-p ,gf-class)
2192 ;; FIXME: reference MOP documentation -- this is an
2193 ;; additional requirement on our users
2194 (error "The generic function class ~S is not finalizeable" ,gf-class)
2195 (finalize-inheritance ,gf-class)))
2196 (remf ,all-keys :generic-function-class)
2197 (remf ,all-keys :environment)
2198 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
2199 (unless (eq combin '.shes-not-there.)
2200 (setf (getf ,all-keys :method-combination)
2201 (find-method-combination (class-prototype ,gf-class)
2202 (car combin)
2203 (cdr combin)))))
2204 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
2205 (unless (eq method-class '.shes-not-there.)
2206 (setf (getf ,all-keys :method-class)
2207 (cond ((classp method-class)
2208 method-class)
2209 (t (find-class method-class t ,env))))))))
2211 (defun real-ensure-gf-using-class--generic-function
2212 (existing
2213 fun-name
2214 &rest all-keys
2215 &key environment (lambda-list nil lambda-list-p)
2216 (generic-function-class 'standard-generic-function)
2217 &allow-other-keys)
2218 (real-ensure-gf-internal generic-function-class all-keys environment)
2219 ;; KLUDGE: the above macro does SETQ on GENERIC-FUNCTION-CLASS,
2220 ;; which is what makes the next line work
2221 (unless (eq (class-of existing) generic-function-class)
2222 (change-class existing generic-function-class))
2223 (prog1
2224 (apply #'reinitialize-instance existing all-keys)
2225 (when lambda-list-p
2226 (setf (info :function :type fun-name)
2227 (specifier-type
2228 (ftype-declaration-from-lambda-list lambda-list fun-name))
2229 (info :function :where-from fun-name) :defined-method))))
2231 (defun real-ensure-gf-using-class--null
2232 (existing
2233 fun-name
2234 &rest all-keys
2235 &key environment (lambda-list nil lambda-list-p)
2236 (generic-function-class 'standard-generic-function)
2237 &allow-other-keys)
2238 (declare (ignore existing))
2239 (real-ensure-gf-internal generic-function-class all-keys environment)
2240 (prog1
2241 (setf (gdefinition fun-name)
2242 (apply #'make-instance generic-function-class
2243 :name fun-name all-keys))
2244 (when lambda-list-p
2245 (setf (info :function :type fun-name)
2246 (specifier-type
2247 (ftype-declaration-from-lambda-list lambda-list fun-name))
2248 (info :function :where-from fun-name) :defined-method))))
2250 (defun safe-gf-arg-info (generic-function)
2251 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2252 (clos-slots-ref (fsc-instance-slots generic-function)
2253 *sgf-arg-info-index*)
2254 (gf-arg-info generic-function)))
2256 ;;; FIXME: this function took on a slightly greater role than it
2257 ;;; previously had around 2005-11-02, when CSR fixed the bug whereby
2258 ;;; having more than one subclass of standard-generic-function caused
2259 ;;; the whole system to die horribly through a metacircle in
2260 ;;; GF-ARG-INFO. The fix is to be slightly more disciplined about
2261 ;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when
2262 ;;; computing discriminating functions, so we need to be careful about
2263 ;;; having a base case for the recursion, and we provide that with the
2264 ;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet)
2265 ;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to
2266 ;;; that stage, where all potentially dangerous cases are enumerated
2267 ;;; and stopped. -- CSR, 2005-11-02.
2268 (defun get-generic-fun-info (gf)
2269 ;; values nreq applyp metatypes nkeys arg-info
2270 (multiple-value-bind (applyp metatypes arg-info)
2271 (let* ((arg-info (if (early-gf-p gf)
2272 (early-gf-arg-info gf)
2273 (safe-gf-arg-info gf)))
2274 (metatypes (arg-info-metatypes arg-info)))
2275 (values (arg-info-applyp arg-info)
2276 metatypes
2277 arg-info))
2278 (values (length metatypes) applyp metatypes
2279 (count-if (lambda (x) (neq x t)) metatypes)
2280 arg-info)))
2282 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
2283 &key slot-name object-class method-class-function
2284 definition-source)
2285 (let ((parsed ())
2286 (unparsed ()))
2287 ;; Figure out whether we got class objects or class names as the
2288 ;; specializers and set parsed and unparsed appropriately. If we
2289 ;; got class objects, then we can compute unparsed, but if we got
2290 ;; class names we don't try to compute parsed.
2292 ;; Note that the use of not symbolp in this call to every should be
2293 ;; read as 'classp' we can't use classp itself because it doesn't
2294 ;; exist yet.
2295 (if (every (lambda (s) (not (symbolp s))) specializers)
2296 (setq parsed specializers
2297 unparsed (mapcar (lambda (s)
2298 (if (eq s t) t (class-name s)))
2299 specializers))
2300 (setq unparsed specializers
2301 parsed ()))
2302 (let ((result
2303 (list :early-method
2305 (getf initargs :function)
2306 (let ((mf (getf initargs :function)))
2307 (aver mf)
2308 (and (typep mf '%method-function)
2309 (%method-function-fast-function mf)))
2311 ;; the parsed specializers. This is used by
2312 ;; EARLY-METHOD-SPECIALIZERS to cache the parse.
2313 ;; Note that this only comes into play when there is
2314 ;; more than one early method on an early gf.
2315 parsed
2317 ;; A list to which REAL-MAKE-A-METHOD can be applied
2318 ;; to make a real method corresponding to this early
2319 ;; one.
2320 (append
2321 (list class qualifiers arglist unparsed
2322 initargs doc)
2323 (when slot-name
2324 (list :slot-name slot-name :object-class object-class
2325 :method-class-function method-class-function))
2326 (list :definition-source definition-source)))))
2327 (initialize-method-function initargs result)
2328 result)))
2330 (defun real-make-a-method
2331 (class qualifiers lambda-list specializers initargs doc
2332 &rest args &key slot-name object-class method-class-function
2333 definition-source)
2334 (if method-class-function
2335 (let* ((object-class (if (classp object-class) object-class
2336 (find-class object-class)))
2337 (slots (class-direct-slots object-class))
2338 (slot-definition (find slot-name slots
2339 :key #'slot-definition-name)))
2340 (aver slot-name)
2341 (aver slot-definition)
2342 (let ((initargs (list* :qualifiers qualifiers :lambda-list lambda-list
2343 :specializers specializers :documentation doc
2344 :slot-definition slot-definition
2345 :slot-name slot-name initargs)))
2346 (apply #'make-instance
2347 (apply method-class-function object-class slot-definition
2348 initargs)
2349 :definition-source definition-source
2350 initargs)))
2351 (apply #'make-instance class :qualifiers qualifiers
2352 :lambda-list lambda-list :specializers specializers
2353 :documentation doc (append args initargs))))
2355 (defun early-method-function (early-method)
2356 (values (cadr early-method) (caddr early-method)))
2358 (defun early-method-class (early-method)
2359 (find-class (car (fifth early-method))))
2361 (defun early-method-standard-accessor-p (early-method)
2362 (let ((class (first (fifth early-method))))
2363 (or (eq class 'standard-reader-method)
2364 (eq class 'standard-writer-method)
2365 (eq class 'standard-boundp-method))))
2367 (defun early-method-standard-accessor-slot-name (early-method)
2368 (eighth (fifth early-method)))
2370 ;;; Fetch the specializers of an early method. This is basically just
2371 ;;; a simple accessor except that when the second argument is t, this
2372 ;;; converts the specializers from symbols into class objects. The
2373 ;;; class objects are cached in the early method, this makes
2374 ;;; bootstrapping faster because the class objects only have to be
2375 ;;; computed once.
2377 ;;; NOTE:
2378 ;;; The second argument should only be passed as T by
2379 ;;; early-lookup-method. This is to implement the rule that only when
2380 ;;; there is more than one early method on a generic function is the
2381 ;;; conversion from class names to class objects done. This
2382 ;;; corresponds to the fact that we are only allowed to have one
2383 ;;; method on any generic function up until the time classes exist.
2384 (defun early-method-specializers (early-method &optional objectsp)
2385 (if (and (listp early-method)
2386 (eq (car early-method) :early-method))
2387 (cond ((eq objectsp t)
2388 (or (fourth early-method)
2389 (setf (fourth early-method)
2390 (mapcar #'find-class (cadddr (fifth early-method))))))
2392 (fourth (fifth early-method))))
2393 (error "~S is not an early-method." early-method)))
2395 (defun early-method-qualifiers (early-method)
2396 (second (fifth early-method)))
2398 (defun early-method-lambda-list (early-method)
2399 (third (fifth early-method)))
2401 (defun early-method-initargs (early-method)
2402 (fifth (fifth early-method)))
2404 (defun (setf early-method-initargs) (new-value early-method)
2405 (setf (fifth (fifth early-method)) new-value))
2407 (defun early-add-named-method (generic-function-name qualifiers
2408 specializers arglist &rest initargs
2409 &key documentation definition-source
2410 &allow-other-keys)
2411 (let* (;; we don't need to deal with the :generic-function-class
2412 ;; argument here because the default,
2413 ;; STANDARD-GENERIC-FUNCTION, is right for all early generic
2414 ;; functions. (See REAL-ADD-NAMED-METHOD)
2415 (gf (ensure-generic-function generic-function-name))
2416 (existing
2417 (dolist (m (early-gf-methods gf))
2418 (when (and (equal (early-method-specializers m) specializers)
2419 (equal (early-method-qualifiers m) qualifiers))
2420 (return m)))))
2421 (setf (getf (getf initargs 'plist) :name)
2422 (make-method-spec gf qualifiers specializers))
2423 (let ((new (make-a-method 'standard-method qualifiers arglist
2424 specializers initargs documentation
2425 :definition-source definition-source)))
2426 (when existing (remove-method gf existing))
2427 (add-method gf new))))
2429 ;;; This is the early version of ADD-METHOD. Later this will become a
2430 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2431 ;;; special knowledge about ADD-METHOD.
2432 (defun add-method (generic-function method)
2433 (when (not (fsc-instance-p generic-function))
2434 (error "Early ADD-METHOD didn't get a funcallable instance."))
2435 (when (not (and (listp method) (eq (car method) :early-method)))
2436 (error "Early ADD-METHOD didn't get an early method."))
2437 (push method (early-gf-methods generic-function))
2438 (set-arg-info generic-function :new-method method)
2439 (unless (assoc (!early-gf-name generic-function)
2440 *!generic-function-fixups*
2441 :test #'equal)
2442 (update-dfun generic-function)))
2444 ;;; This is the early version of REMOVE-METHOD. See comments on
2445 ;;; the early version of ADD-METHOD.
2446 (defun remove-method (generic-function method)
2447 (when (not (fsc-instance-p generic-function))
2448 (error "An early remove-method didn't get a funcallable instance."))
2449 (when (not (and (listp method) (eq (car method) :early-method)))
2450 (error "An early remove-method didn't get an early method."))
2451 (setf (early-gf-methods generic-function)
2452 (remove method (early-gf-methods generic-function)))
2453 (set-arg-info generic-function)
2454 (unless (assoc (!early-gf-name generic-function)
2455 *!generic-function-fixups*
2456 :test #'equal)
2457 (update-dfun generic-function)))
2459 ;;; This is the early version of GET-METHOD. See comments on the early
2460 ;;; version of ADD-METHOD.
2461 (defun get-method (generic-function qualifiers specializers
2462 &optional (errorp t))
2463 (if (early-gf-p generic-function)
2464 (or (dolist (m (early-gf-methods generic-function))
2465 (when (and (or (equal (early-method-specializers m nil)
2466 specializers)
2467 (equal (early-method-specializers m t)
2468 specializers))
2469 (equal (early-method-qualifiers m) qualifiers))
2470 (return m)))
2471 (if errorp
2472 (error "can't get early method")
2473 nil))
2474 (real-get-method generic-function qualifiers specializers errorp)))
2476 (defun !fix-early-generic-functions ()
2477 (let ((accessors nil))
2478 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2479 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2480 (dolist (early-gf-spec *!early-generic-functions*)
2481 (when (every #'early-method-standard-accessor-p
2482 (early-gf-methods (gdefinition early-gf-spec)))
2483 (push early-gf-spec accessors)))
2484 (dolist (spec (nconc accessors
2485 '(accessor-method-slot-name
2486 generic-function-methods
2487 method-specializers
2488 specializerp
2489 specializer-type
2490 specializer-class
2491 slot-definition-location
2492 slot-definition-name
2493 class-slots
2494 gf-arg-info
2495 class-precedence-list
2496 slot-boundp-using-class
2497 (setf slot-value-using-class)
2498 slot-value-using-class
2499 structure-class-p
2500 standard-class-p
2501 funcallable-standard-class-p
2502 specializerp)))
2503 (/show spec)
2504 (setq *!early-generic-functions*
2505 (cons spec
2506 (delete spec *!early-generic-functions* :test #'equal))))
2508 (dolist (early-gf-spec *!early-generic-functions*)
2509 (/show early-gf-spec)
2510 (let* ((gf (gdefinition early-gf-spec))
2511 (methods (mapcar (lambda (early-method)
2512 (let ((args (copy-list (fifth
2513 early-method))))
2514 (setf (fourth args)
2515 (early-method-specializers
2516 early-method t))
2517 (apply #'real-make-a-method args)))
2518 (early-gf-methods gf))))
2519 (setf (generic-function-method-class gf) *the-class-standard-method*)
2520 (setf (generic-function-method-combination gf)
2521 *standard-method-combination*)
2522 (set-methods gf methods)))
2524 (dolist (fn *!early-functions*)
2525 (/show fn)
2526 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2528 (dolist (fixup *!generic-function-fixups*)
2529 (/show fixup)
2530 (let* ((fspec (car fixup))
2531 (gf (gdefinition fspec))
2532 (methods (mapcar (lambda (method)
2533 (let* ((lambda-list (first method))
2534 (specializers (mapcar #'find-class (second method)))
2535 (method-fn-name (third method))
2536 (fn-name (or method-fn-name fspec))
2537 (fn (fdefinition fn-name))
2538 (initargs
2539 (list :function
2540 (set-fun-name
2541 (lambda (args next-methods)
2542 (declare (ignore
2543 next-methods))
2544 (apply fn args))
2545 `(call ,fn-name)))))
2546 (declare (type function fn))
2547 (make-a-method 'standard-method
2549 lambda-list
2550 specializers
2551 initargs
2552 nil)))
2553 (cdr fixup))))
2554 (setf (generic-function-method-class gf) *the-class-standard-method*)
2555 (setf (generic-function-method-combination gf)
2556 *standard-method-combination*)
2557 (set-methods gf methods))))
2558 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2560 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2561 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2562 ;;; is really implemented.
2563 (defun parse-defmethod (cdr-of-form)
2564 (declare (list cdr-of-form))
2565 (let ((name (pop cdr-of-form))
2566 (qualifiers ())
2567 (spec-ll ()))
2568 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2569 (push (pop cdr-of-form) qualifiers)
2570 (return (setq qualifiers (nreverse qualifiers)))))
2571 (setq spec-ll (pop cdr-of-form))
2572 (values name qualifiers spec-ll cdr-of-form)))
2574 (defun parse-specializers (generic-function specializers)
2575 (declare (list specializers))
2576 (flet ((parse (spec)
2577 (parse-specializer-using-class generic-function spec)))
2578 (mapcar #'parse specializers)))
2580 (defun unparse-specializers (generic-function specializers)
2581 (declare (list specializers))
2582 (flet ((unparse (spec)
2583 (unparse-specializer-using-class generic-function spec)))
2584 (mapcar #'unparse specializers)))
2586 (defun extract-parameters (specialized-lambda-list)
2587 (multiple-value-bind (parameters ignore1 ignore2)
2588 (parse-specialized-lambda-list specialized-lambda-list)
2589 (declare (ignore ignore1 ignore2))
2590 parameters))
2592 (defun extract-lambda-list (specialized-lambda-list)
2593 (multiple-value-bind (ignore1 lambda-list ignore2)
2594 (parse-specialized-lambda-list specialized-lambda-list)
2595 (declare (ignore ignore1 ignore2))
2596 lambda-list))
2598 (defun extract-specializer-names (specialized-lambda-list)
2599 (multiple-value-bind (ignore1 ignore2 specializers)
2600 (parse-specialized-lambda-list specialized-lambda-list)
2601 (declare (ignore ignore1 ignore2))
2602 specializers))
2604 (defun extract-required-parameters (specialized-lambda-list)
2605 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2606 (parse-specialized-lambda-list specialized-lambda-list)
2607 (declare (ignore ignore1 ignore2 ignore3))
2608 required-parameters))
2610 (define-condition specialized-lambda-list-error
2611 (reference-condition simple-program-error)
2613 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2615 (defun parse-specialized-lambda-list
2616 (arglist
2617 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2618 &aux (specialized-lambda-list-keywords
2619 '(&optional &rest &key &allow-other-keys &aux)))
2620 (let ((arg (car arglist)))
2621 (cond ((null arglist) (values nil nil nil nil))
2622 ((eq arg '&aux)
2623 (values nil arglist nil nil))
2624 ((memq arg lambda-list-keywords)
2625 ;; non-standard lambda-list-keywords are errors.
2626 (unless (memq arg specialized-lambda-list-keywords)
2627 (error 'specialized-lambda-list-error
2628 :format-control "unknown specialized-lambda-list ~
2629 keyword ~S~%"
2630 :format-arguments (list arg)))
2631 ;; no multiple &rest x &rest bla specifying
2632 (when (memq arg supplied-keywords)
2633 (error 'specialized-lambda-list-error
2634 :format-control "multiple occurrence of ~
2635 specialized-lambda-list keyword ~S~%"
2636 :format-arguments (list arg)))
2637 ;; And no placing &key in front of &optional, either.
2638 (unless (memq arg allowed-keywords)
2639 (error 'specialized-lambda-list-error
2640 :format-control "misplaced specialized-lambda-list ~
2641 keyword ~S~%"
2642 :format-arguments (list arg)))
2643 ;; When we are at a lambda-list keyword, the parameters
2644 ;; don't include the lambda-list keyword; the lambda-list
2645 ;; does include the lambda-list keyword; and no
2646 ;; specializers are allowed to follow the lambda-list
2647 ;; keywords (at least for now).
2648 (multiple-value-bind (parameters lambda-list)
2649 (parse-specialized-lambda-list (cdr arglist)
2650 (cons arg supplied-keywords)
2651 (if (eq arg '&key)
2652 (cons '&allow-other-keys
2653 (cdr (member arg allowed-keywords)))
2654 (cdr (member arg allowed-keywords))))
2655 (when (and (eq arg '&rest)
2656 (or (null lambda-list)
2657 (memq (car lambda-list)
2658 specialized-lambda-list-keywords)
2659 (not (or (null (cadr lambda-list))
2660 (memq (cadr lambda-list)
2661 specialized-lambda-list-keywords)))))
2662 (error 'specialized-lambda-list-error
2663 :format-control
2664 "in a specialized-lambda-list, excactly one ~
2665 variable must follow &REST.~%"
2666 :format-arguments nil))
2667 (values parameters
2668 (cons arg lambda-list)
2670 ())))
2671 (supplied-keywords
2672 ;; After a lambda-list keyword there can be no specializers.
2673 (multiple-value-bind (parameters lambda-list)
2674 (parse-specialized-lambda-list (cdr arglist)
2675 supplied-keywords
2676 allowed-keywords)
2677 (values (cons (if (listp arg) (car arg) arg) parameters)
2678 (cons arg lambda-list)
2680 ())))
2682 (multiple-value-bind (parameters lambda-list specializers required)
2683 (parse-specialized-lambda-list (cdr arglist))
2684 (values (cons (if (listp arg) (car arg) arg) parameters)
2685 (cons (if (listp arg) (car arg) arg) lambda-list)
2686 (cons (if (listp arg) (cadr arg) t) specializers)
2687 (cons (if (listp arg) (car arg) arg) required)))))))
2689 (setq *boot-state* 'early)
2691 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2692 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2693 ;;; walker stuff was only used for implementing stuff like that; maybe
2694 ;;; it's not needed any more? Hunt down what it was used for and see.
2696 (defun extract-the (form)
2697 (cond ((and (consp form) (eq (car form) 'the))
2698 (aver (proper-list-of-length-p 3))
2699 (third form))
2701 form)))
2703 (defmacro with-slots (slots instance &body body)
2704 (let ((in (gensym)))
2705 `(let ((,in ,instance))
2706 (declare (ignorable ,in))
2707 ,@(let ((instance (extract-the instance)))
2708 (and (symbolp instance)
2709 `((declare (%variable-rebinding ,in ,instance)))))
2711 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2712 (let ((var-name
2713 (if (symbolp slot-entry)
2714 slot-entry
2715 (car slot-entry)))
2716 (slot-name
2717 (if (symbolp slot-entry)
2718 slot-entry
2719 (cadr slot-entry))))
2720 `(,var-name
2721 (slot-value ,in ',slot-name))))
2722 slots)
2723 ,@body))))
2725 (defmacro with-accessors (slots instance &body body)
2726 (let ((in (gensym)))
2727 `(let ((,in ,instance))
2728 (declare (ignorable ,in))
2729 ,@(let ((instance (extract-the instance)))
2730 (and (symbolp instance)
2731 `((declare (%variable-rebinding ,in ,instance)))))
2733 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2734 (let ((var-name (car slot-entry))
2735 (accessor-name (cadr slot-entry)))
2736 `(,var-name (,accessor-name ,in))))
2737 slots)
2738 ,@body))))