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