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