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