0.9.15.29:
[sbcl/lichteblau.git] / src / pcl / boot.lisp
blob672e483a0125dfcf1267202ab41e46cbe9f2f61b
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
72 add-named-method
73 ensure-generic-function-using-class
74 add-method
75 remove-method))
77 (defvar *!early-functions*
78 '((make-a-method early-make-a-method
79 real-make-a-method)
80 (add-named-method early-add-named-method
81 real-add-named-method)
84 ;;; For each of the early functions, arrange to have it point to its
85 ;;; early definition. Do this in a way that makes sure that if we
86 ;;; redefine one of the early definitions the redefinition will take
87 ;;; effect. This makes development easier.
88 (dolist (fns *!early-functions*)
89 (let ((name (car fns))
90 (early-name (cadr fns)))
91 (setf (gdefinition name)
92 (set-fun-name
93 (lambda (&rest args)
94 (apply (fdefinition early-name) args))
95 name))))
97 ;;; *!GENERIC-FUNCTION-FIXUPS* is used by !FIX-EARLY-GENERIC-FUNCTIONS
98 ;;; to convert the few functions in the bootstrap which are supposed
99 ;;; to be generic functions but can't be early on.
100 (defvar *!generic-function-fixups*
101 '((add-method
102 ((generic-function method) ;lambda-list
103 (standard-generic-function method) ;specializers
104 real-add-method)) ;method-function
105 (remove-method
106 ((generic-function method)
107 (standard-generic-function method)
108 real-remove-method))
109 (get-method
110 ((generic-function qualifiers specializers &optional (errorp t))
111 (standard-generic-function t t)
112 real-get-method))
113 (ensure-generic-function-using-class
114 ((generic-function fun-name
115 &key generic-function-class environment
116 &allow-other-keys)
117 (generic-function t)
118 real-ensure-gf-using-class--generic-function)
119 ((generic-function fun-name
120 &key generic-function-class environment
121 &allow-other-keys)
122 (null t)
123 real-ensure-gf-using-class--null))
124 (make-method-lambda
125 ((proto-generic-function proto-method lambda-expression environment)
126 (standard-generic-function standard-method t t)
127 real-make-method-lambda))
128 (make-method-initargs-form
129 ((proto-generic-function proto-method
130 lambda-expression
131 lambda-list environment)
132 (standard-generic-function standard-method t t t)
133 real-make-method-initargs-form))
134 (compute-effective-method
135 ((generic-function combin applicable-methods)
136 (generic-function standard-method-combination t)
137 standard-compute-effective-method))))
139 (defmacro defgeneric (fun-name lambda-list &body options)
140 (declare (type list lambda-list))
141 (unless (legal-fun-name-p fun-name)
142 (error 'simple-program-error
143 :format-control "illegal generic function name ~S"
144 :format-arguments (list fun-name)))
145 (check-gf-lambda-list lambda-list)
146 (let ((initargs ())
147 (methods ()))
148 (flet ((duplicate-option (name)
149 (error 'simple-program-error
150 :format-control "The option ~S appears more than once."
151 :format-arguments (list name)))
152 (expand-method-definition (qab) ; QAB = qualifiers, arglist, body
153 (let* ((arglist-pos (position-if #'listp qab))
154 (arglist (elt qab arglist-pos))
155 (qualifiers (subseq qab 0 arglist-pos))
156 (body (nthcdr (1+ arglist-pos) qab)))
157 `(push (defmethod ,fun-name ,@qualifiers ,arglist ,@body)
158 (generic-function-initial-methods (fdefinition ',fun-name))))))
159 (macrolet ((initarg (key) `(getf initargs ,key)))
160 (dolist (option options)
161 (let ((car-option (car option)))
162 (case car-option
163 (declare
164 (when (and
165 (consp (cadr option))
166 (member (first (cadr option))
167 ;; FIXME: this list is slightly weird.
168 ;; ANSI (on the DEFGENERIC page) in one
169 ;; place allows only OPTIMIZE; in
170 ;; another place gives this list of
171 ;; disallowed declaration specifiers.
172 ;; This seems to be the only place where
173 ;; the FUNCTION declaration is
174 ;; mentioned; TYPE seems to be missing.
175 ;; Very strange. -- CSR, 2002-10-21
176 '(declaration ftype function
177 inline notinline special)))
178 (error 'simple-program-error
179 :format-control "The declaration specifier ~S ~
180 is not allowed inside DEFGENERIC."
181 :format-arguments (list (cadr option))))
182 (push (cadr option) (initarg :declarations)))
183 (:method-combination
184 (when (initarg car-option)
185 (duplicate-option car-option))
186 (unless (symbolp (cadr option))
187 (error 'simple-program-error
188 :format-control "METHOD-COMBINATION name not a ~
189 symbol: ~S"
190 :format-arguments (list (cadr option))))
191 (setf (initarg car-option)
192 `',(cdr option)))
193 (:argument-precedence-order
194 (let* ((required (parse-lambda-list lambda-list))
195 (supplied (cdr option)))
196 (unless (= (length required) (length supplied))
197 (error 'simple-program-error
198 :format-control "argument count discrepancy in ~
199 :ARGUMENT-PRECEDENCE-ORDER clause."
200 :format-arguments nil))
201 (when (set-difference required supplied)
202 (error 'simple-program-error
203 :format-control "unequal sets for ~
204 :ARGUMENT-PRECEDENCE-ORDER clause: ~
205 ~S and ~S"
206 :format-arguments (list required supplied)))
207 (setf (initarg car-option)
208 `',(cdr option))))
209 ((:documentation :generic-function-class :method-class)
210 (unless (proper-list-of-length-p option 2)
211 (error "bad list length for ~S" option))
212 (if (initarg car-option)
213 (duplicate-option car-option)
214 (setf (initarg car-option) `',(cadr option))))
215 (:method
216 (push (cdr option) methods))
218 ;; ANSI requires that unsupported things must get a
219 ;; PROGRAM-ERROR.
220 (error 'simple-program-error
221 :format-control "unsupported option ~S"
222 :format-arguments (list option))))))
224 (when (initarg :declarations)
225 (setf (initarg :declarations)
226 `',(initarg :declarations))))
227 `(progn
228 (eval-when (:compile-toplevel :load-toplevel :execute)
229 (compile-or-load-defgeneric ',fun-name))
230 (load-defgeneric ',fun-name ',lambda-list
231 (sb-c:source-location) ,@initargs)
232 ,@(mapcar #'expand-method-definition methods)
233 (fdefinition ',fun-name)))))
235 (defun compile-or-load-defgeneric (fun-name)
236 (proclaim-as-fun-name fun-name)
237 (note-name-defined fun-name :function)
238 (unless (eq (info :function :where-from fun-name) :declared)
239 (setf (info :function :where-from fun-name) :defined)
240 (setf (info :function :type fun-name)
241 (specifier-type 'function))))
243 (defun load-defgeneric (fun-name lambda-list source-location &rest initargs)
244 (when (fboundp fun-name)
245 (style-warn "redefining ~S in DEFGENERIC" fun-name)
246 (let ((fun (fdefinition fun-name)))
247 (when (generic-function-p fun)
248 (loop for method in (generic-function-initial-methods fun)
249 do (remove-method fun method))
250 (setf (generic-function-initial-methods fun) '()))))
251 (apply #'ensure-generic-function
252 fun-name
253 :lambda-list lambda-list
254 :definition-source source-location
255 initargs))
257 (define-condition generic-function-lambda-list-error
258 (reference-condition simple-program-error)
260 (:default-initargs :references (list '(:ansi-cl :section (3 4 2)))))
262 (defun check-gf-lambda-list (lambda-list)
263 (flet ((ensure (arg ok)
264 (unless ok
265 (error 'generic-function-lambda-list-error
266 :format-control
267 "~@<invalid ~S ~_in the generic function lambda list ~S~:>"
268 :format-arguments (list arg lambda-list)))))
269 (multiple-value-bind (required optional restp rest keyp keys allowp
270 auxp aux morep more-context more-count)
271 (parse-lambda-list lambda-list)
272 (declare (ignore required)) ; since they're no different in a gf ll
273 (declare (ignore restp rest)) ; since they're no different in a gf ll
274 (declare (ignore allowp)) ; since &ALLOW-OTHER-KEYS is fine either way
275 (declare (ignore aux)) ; since we require AUXP=NIL
276 (declare (ignore more-context more-count)) ; safely ignored unless MOREP
277 ;; no defaults allowed for &OPTIONAL arguments
278 (dolist (i optional)
279 (ensure i (or (symbolp i)
280 (and (consp i) (symbolp (car i)) (null (cdr i))))))
281 ;; no defaults allowed for &KEY arguments
282 (when keyp
283 (dolist (i keys)
284 (ensure i (or (symbolp i)
285 (and (consp i)
286 (or (symbolp (car i))
287 (and (consp (car i))
288 (symbolp (caar i))
289 (symbolp (cadar i))
290 (null (cddar i))))
291 (null (cdr i)))))))
292 ;; no &AUX allowed
293 (when auxp
294 (error "&AUX is not allowed in a generic function lambda list: ~S"
295 lambda-list))
296 ;; Oh, *puhlease*... not specifically as per section 3.4.2 of
297 ;; the ANSI spec, but the CMU CL &MORE extension does not
298 ;; belong here!
299 (aver (not morep)))))
301 (defmacro defmethod (&rest args &environment env)
302 (multiple-value-bind (name qualifiers lambda-list body)
303 (parse-defmethod args)
304 (multiple-value-bind (proto-gf proto-method)
305 (prototypes-for-make-method-lambda name)
306 (expand-defmethod name
307 proto-gf
308 proto-method
309 qualifiers
310 lambda-list
311 body
312 env))))
314 (defun prototypes-for-make-method-lambda (name)
315 (if (not (eq *boot-state* 'complete))
316 (values nil nil)
317 (let ((gf? (and (fboundp name)
318 (gdefinition name))))
319 (if (or (null gf?)
320 (not (generic-function-p gf?)))
321 (values (class-prototype (find-class 'standard-generic-function))
322 (class-prototype (find-class 'standard-method)))
323 (values gf?
324 (class-prototype (or (generic-function-method-class gf?)
325 (find-class 'standard-method))))))))
327 ;;; Take a name which is either a generic function name or a list specifying
328 ;;; a SETF generic function (like: (SETF <generic-function-name>)). Return
329 ;;; the prototype instance of the method-class for that generic function.
331 ;;; If there is no generic function by that name, this returns the
332 ;;; default value, the prototype instance of the class
333 ;;; STANDARD-METHOD. This default value is also returned if the spec
334 ;;; names an ordinary function or even a macro. In effect, this leaves
335 ;;; the signalling of the appropriate error until load time.
337 ;;; Note: During bootstrapping, this function is allowed to return NIL.
338 (defun method-prototype-for-gf (name)
339 (let ((gf? (and (fboundp name)
340 (gdefinition name))))
341 (cond ((neq *boot-state* 'complete) nil)
342 ((or (null gf?)
343 (not (generic-function-p gf?))) ; Someone else MIGHT
344 ; error at load time.
345 (class-prototype (find-class 'standard-method)))
347 (class-prototype (or (generic-function-method-class gf?)
348 (find-class 'standard-method)))))))
350 (defun expand-defmethod (name
351 proto-gf
352 proto-method
353 qualifiers
354 lambda-list
355 body
356 env)
357 (multiple-value-bind (method-lambda unspecialized-lambda-list specializers)
358 (add-method-declarations name qualifiers lambda-list body env)
359 (multiple-value-bind (method-function-lambda initargs)
360 (make-method-lambda proto-gf proto-method method-lambda env)
361 (let ((initargs-form (make-method-initargs-form proto-gf
362 proto-method
363 method-function-lambda
364 initargs
365 env)))
366 `(progn
367 ;; Note: We could DECLAIM the ftype of the generic function
368 ;; here, since ANSI specifies that we create it if it does
369 ;; not exist. However, I chose not to, because I think it's
370 ;; more useful to support a style of programming where every
371 ;; generic function has an explicit DEFGENERIC and any typos
372 ;; in DEFMETHODs are warned about. Otherwise
374 ;; (DEFGENERIC FOO-BAR-BLETCH ((X T)))
375 ;; (DEFMETHOD FOO-BAR-BLETCH ((X HASH-TABLE)) ..)
376 ;; (DEFMETHOD FOO-BRA-BLETCH ((X SIMPLE-VECTOR)) ..)
377 ;; (DEFMETHOD FOO-BAR-BLETCH ((X VECTOR)) ..)
378 ;; (DEFMETHOD FOO-BAR-BLETCH ((X ARRAY)) ..)
379 ;; (DEFMETHOD FOO-BAR-BLETCH ((X LIST)) ..)
381 ;; compiles without raising an error and runs without
382 ;; raising an error (since SIMPLE-VECTOR cases fall through
383 ;; to VECTOR) but still doesn't do what was intended. I hate
384 ;; that kind of bug (code which silently gives the wrong
385 ;; answer), so we don't do a DECLAIM here. -- WHN 20000229
386 ,(make-defmethod-form name qualifiers specializers
387 unspecialized-lambda-list
388 (if proto-method
389 (class-name (class-of proto-method))
390 'standard-method)
391 initargs-form))))))
393 (defun interned-symbol-p (x)
394 (and (symbolp x) (symbol-package x)))
396 (defun make-defmethod-form
397 (name qualifiers specializers unspecialized-lambda-list
398 method-class-name initargs-form)
399 (let (fn
400 fn-lambda)
401 (if (and (interned-symbol-p (fun-name-block-name name))
402 (every #'interned-symbol-p qualifiers)
403 (every (lambda (s)
404 (if (consp s)
405 (and (eq (car s) 'eql)
406 (constantp (cadr s))
407 (let ((sv (constant-form-value (cadr s))))
408 (or (interned-symbol-p sv)
409 (integerp sv)
410 (and (characterp sv)
411 (standard-char-p sv)))))
412 (interned-symbol-p s)))
413 specializers)
414 (consp initargs-form)
415 (eq (car initargs-form) 'list*)
416 (memq (cadr initargs-form) '(:function :fast-function))
417 (consp (setq fn (caddr initargs-form)))
418 (eq (car fn) 'function)
419 (consp (setq fn-lambda (cadr fn)))
420 (eq (car fn-lambda) 'lambda))
421 (let* ((specls (mapcar (lambda (specl)
422 (if (consp specl)
423 `(,(car specl) ,(eval (cadr specl)))
424 specl))
425 specializers))
426 (mname `(,(if (eq (cadr initargs-form) :function)
427 'slow-method 'fast-method)
428 ,name ,@qualifiers ,specls)))
429 `(progn
430 (defun ,mname ,(cadr fn-lambda)
431 ,@(cddr fn-lambda))
432 ,(make-defmethod-form-internal
433 name qualifiers `',specls
434 unspecialized-lambda-list method-class-name
435 `(list* ,(cadr initargs-form)
436 #',mname
437 ,@(cdddr initargs-form)))))
438 (make-defmethod-form-internal
439 name qualifiers
440 `(list ,@(mapcar (lambda (specializer)
441 (if (consp specializer)
442 ``(,',(car specializer)
443 ,,(cadr specializer))
444 `',specializer))
445 specializers))
446 unspecialized-lambda-list
447 method-class-name
448 initargs-form))))
450 (defun make-defmethod-form-internal
451 (name qualifiers specializers-form unspecialized-lambda-list
452 method-class-name initargs-form)
453 `(load-defmethod
454 ',method-class-name
455 ',name
456 ',qualifiers
457 ,specializers-form
458 ',unspecialized-lambda-list
459 ,initargs-form
460 (sb-c:source-location)))
462 (defmacro make-method-function (method-lambda &environment env)
463 (make-method-function-internal method-lambda env))
465 (defun make-method-function-internal (method-lambda &optional env)
466 (multiple-value-bind (proto-gf proto-method)
467 (prototypes-for-make-method-lambda nil)
468 (multiple-value-bind (method-function-lambda initargs)
469 (make-method-lambda proto-gf proto-method method-lambda env)
470 (make-method-initargs-form proto-gf
471 proto-method
472 method-function-lambda
473 initargs
474 env))))
476 (defun add-method-declarations (name qualifiers lambda-list body env)
477 (declare (ignore env))
478 (multiple-value-bind (parameters unspecialized-lambda-list specializers)
479 (parse-specialized-lambda-list lambda-list)
480 (multiple-value-bind (real-body declarations documentation)
481 (parse-body body)
482 (values `(lambda ,unspecialized-lambda-list
483 ,@(when documentation `(,documentation))
484 ;; (Old PCL code used a somewhat different style of
485 ;; list for %METHOD-NAME values. Our names use
486 ;; ,@QUALIFIERS instead of ,QUALIFIERS so that the
487 ;; method names look more like what you see in a
488 ;; DEFMETHOD form.)
490 ;; FIXME: As of sbcl-0.7.0.6, code elsewhere, at
491 ;; least the code to set up named BLOCKs around the
492 ;; bodies of methods, depends on the function's base
493 ;; name being the first element of the %METHOD-NAME
494 ;; list. It would be good to remove this dependency,
495 ;; perhaps by building the BLOCK here, or by using
496 ;; another declaration (e.g. %BLOCK-NAME), so that
497 ;; our method debug names are free to have any format,
498 ;; e.g. (:METHOD PRINT-OBJECT :AROUND (CLOWN T)).
500 ;; Further, as of sbcl-0.7.9.10, the code to
501 ;; implement NO-NEXT-METHOD is coupled to the form of
502 ;; this declaration; see the definition of
503 ;; CALL-NO-NEXT-METHOD (and the passing of
504 ;; METHOD-NAME-DECLARATION arguments around the
505 ;; various CALL-NEXT-METHOD logic).
506 (declare (%method-name (,name
507 ,@qualifiers
508 ,specializers)))
509 (declare (%method-lambda-list ,@lambda-list))
510 ,@declarations
511 ,@real-body)
512 unspecialized-lambda-list specializers))))
514 (defun real-make-method-initargs-form (proto-gf proto-method
515 method-lambda initargs env)
516 (declare (ignore proto-gf proto-method))
517 (unless (and (consp method-lambda)
518 (eq (car method-lambda) 'lambda))
519 (error "The METHOD-LAMBDA argument to MAKE-METHOD-FUNCTION, ~S, ~
520 is not a lambda form."
521 method-lambda))
522 (make-method-initargs-form-internal method-lambda initargs env))
524 (unless (fboundp 'make-method-initargs-form)
525 (setf (gdefinition 'make-method-initargs-form)
526 (symbol-function 'real-make-method-initargs-form)))
528 (defun real-make-method-lambda (proto-gf proto-method method-lambda env)
529 (declare (ignore proto-gf proto-method))
530 (make-method-lambda-internal method-lambda env))
532 ;;; a helper function for creating Python-friendly type declarations
533 ;;; in DEFMETHOD forms
534 (defun parameter-specializer-declaration-in-defmethod (parameter specializer)
535 (cond ((and (consp specializer)
536 (eq (car specializer) 'eql))
537 ;; KLUDGE: ANSI, in its wisdom, says that
538 ;; EQL-SPECIALIZER-FORMs in EQL specializers are evaluated at
539 ;; DEFMETHOD expansion time. Thus, although one might think
540 ;; that in
541 ;; (DEFMETHOD FOO ((X PACKAGE)
542 ;; (Y (EQL 12))
543 ;; ..))
544 ;; the PACKAGE and (EQL 12) forms are both parallel type
545 ;; names, they're not, as is made clear when you do
546 ;; (DEFMETHOD FOO ((X PACKAGE)
547 ;; (Y (EQL 'BAR)))
548 ;; ..)
549 ;; where Y needs to be a symbol named "BAR", not some cons
550 ;; made by (CONS 'QUOTE 'BAR). I.e. when the
551 ;; EQL-SPECIALIZER-FORM is (EQL 'X), it requires an argument
552 ;; to be of type (EQL X). It'd be easy to transform one to
553 ;; the other, but it'd be somewhat messier to do so while
554 ;; ensuring that the EQL-SPECIALIZER-FORM is only EVAL'd
555 ;; once. (The new code wouldn't be messy, but it'd require a
556 ;; big transformation of the old code.) So instead we punt.
557 ;; -- WHN 20000610
558 '(ignorable))
559 ((member specializer
560 ;; KLUDGE: For some low-level implementation
561 ;; classes, perhaps because of some problems related
562 ;; to the incomplete integration of PCL into SBCL's
563 ;; type system, some specializer classes can't be
564 ;; declared as argument types. E.g.
565 ;; (DEFMETHOD FOO ((X SLOT-OBJECT))
566 ;; (DECLARE (TYPE SLOT-OBJECT X))
567 ;; ..)
568 ;; loses when
569 ;; (DEFSTRUCT BAR A B)
570 ;; (FOO (MAKE-BAR))
571 ;; perhaps because of the way that STRUCTURE-OBJECT
572 ;; inherits both from SLOT-OBJECT and from
573 ;; SB-KERNEL:INSTANCE. In an effort to sweep such
574 ;; problems under the rug, we exclude these problem
575 ;; cases by blacklisting them here. -- WHN 2001-01-19
576 (list 'slot-object #+nil (find-class 'slot-object)))
577 '(ignorable))
578 ((not (eq *boot-state* 'complete))
579 ;; KLUDGE: PCL, in its wisdom, sometimes calls methods with
580 ;; types which don't match their specializers. (Specifically,
581 ;; it calls ENSURE-CLASS-USING-CLASS (T NULL) with a non-NULL
582 ;; second argument.) Hopefully it only does this kind of
583 ;; weirdness when bootstrapping.. -- WHN 20000610
584 '(ignorable))
585 ((typep specializer 'eql-specializer)
586 `(type (eql ,(eql-specializer-object specializer)) ,parameter))
587 ((var-globally-special-p parameter)
588 ;; KLUDGE: Don't declare types for global special variables
589 ;; -- our rebinding magic for SETQ cases don't work right
590 ;; there.
592 ;; FIXME: It would be better to detect the SETQ earlier and
593 ;; skip declarations for specials only when needed, not
594 ;; always.
596 ;; --NS 2004-10-14
597 '(ignorable))
599 ;; Otherwise, we can usually make Python very happy.
601 ;; KLUDGE: Since INFO doesn't work right for class objects here,
602 ;; and they are valid specializers, see if the specializer is
603 ;; a named class, and use the name in that case -- otherwise
604 ;; the class instance is ok, since info will just return NIL, NIL.
606 ;; We still need to deal with the class case too, but at
607 ;; least #.(find-class 'integer) and integer as equivalent
608 ;; specializers with this.
609 (let* ((specializer (if (and (typep specializer 'class)
610 (let ((name (class-name specializer)))
611 (and name (symbolp name)
612 (eq specializer (find-class name nil)))))
613 (class-name specializer)
614 specializer))
615 (kind (info :type :kind specializer)))
617 (flet ((specializer-class ()
618 (if (typep specializer 'class)
619 specializer
620 (find-class specializer nil))))
621 (ecase kind
622 ((:primitive) `(type ,specializer ,parameter))
623 ((:defined)
624 (let ((class (specializer-class)))
625 ;; CLASS can be null here if the user has erroneously
626 ;; tried to use a defined type as a specializer; it
627 ;; can be a non-BUILT-IN-CLASS if the user defines a
628 ;; type and calls (SETF FIND-CLASS) in a consistent
629 ;; way.
630 (when (and class (typep class 'built-in-class))
631 `(type ,specializer ,parameter))))
632 ((:instance nil)
633 (let ((class (specializer-class)))
634 (cond
635 (class
636 (if (typep class '(or built-in-class structure-class))
637 `(type ,specializer ,parameter)
638 ;; don't declare CLOS classes as parameters;
639 ;; it's too expensive.
640 '(ignorable)))
642 ;; we can get here, and still not have a failure
643 ;; case, by doing MOP programming like (PROGN
644 ;; (ENSURE-CLASS 'FOO) (DEFMETHOD BAR ((X FOO))
645 ;; ...)). Best to let the user know we haven't
646 ;; been able to extract enough information:
647 (style-warn
648 "~@<can't find type for presumed class ~S in ~S.~@:>"
649 specializer
650 'parameter-specializer-declaration-in-defmethod)
651 '(ignorable)))))
652 ((:forthcoming-defclass-type)
653 '(ignorable))))))))
655 (defun make-method-lambda-internal (method-lambda &optional env)
656 (unless (and (consp method-lambda) (eq (car method-lambda) 'lambda))
657 (error "The METHOD-LAMBDA argument to MAKE-METHOD-LAMBDA, ~S, ~
658 is not a lambda form."
659 method-lambda))
660 (multiple-value-bind (real-body declarations documentation)
661 (parse-body (cddr method-lambda))
662 (let* ((name-decl (get-declaration '%method-name declarations))
663 (sll-decl (get-declaration '%method-lambda-list declarations))
664 (method-name (when (consp name-decl) (car name-decl)))
665 (generic-function-name (when method-name (car method-name)))
666 (specialized-lambda-list (or sll-decl (cadr method-lambda))))
667 (multiple-value-bind (parameters lambda-list specializers)
668 (parse-specialized-lambda-list specialized-lambda-list)
669 (let* ((required-parameters
670 (mapcar (lambda (r s) (declare (ignore s)) r)
671 parameters
672 specializers))
673 (slots (mapcar #'list required-parameters))
674 (calls (list nil))
675 (class-declarations
676 `(declare
677 ;; These declarations seem to be used by PCL to pass
678 ;; information to itself; when I tried to delete 'em
679 ;; ca. 0.6.10 it didn't work. I'm not sure how
680 ;; they work, but note the (VAR-DECLARATION '%CLASS ..)
681 ;; expression in CAN-OPTIMIZE-ACCESS1. -- WHN 2000-12-30
682 ,@(remove nil
683 (mapcar (lambda (a s) (and (symbolp s)
684 (neq s t)
685 `(%class ,a ,s)))
686 parameters
687 specializers))
688 ;; These TYPE declarations weren't in the original
689 ;; PCL code, but the Python compiler likes them a
690 ;; lot. (We're telling the compiler about our
691 ;; knowledge of specialized argument types so that
692 ;; it can avoid run-time type dispatch overhead,
693 ;; which can be a huge win for Python.)
695 ;; KLUDGE: when I tried moving these to
696 ;; ADD-METHOD-DECLARATIONS, things broke. No idea
697 ;; why. -- CSR, 2004-06-16
698 ,@(mapcar #'parameter-specializer-declaration-in-defmethod
699 parameters
700 specializers)))
701 (method-lambda
702 ;; Remove the documentation string and insert the
703 ;; appropriate class declarations. The documentation
704 ;; string is removed to make it easy for us to insert
705 ;; new declarations later, they will just go after the
706 ;; CADR of the method lambda. The class declarations
707 ;; are inserted to communicate the class of the method's
708 ;; arguments to the code walk.
709 `(lambda ,lambda-list
710 ;; The default ignorability of method parameters
711 ;; doesn't seem to be specified by ANSI. PCL had
712 ;; them basically ignorable but was a little
713 ;; inconsistent. E.g. even though the two
714 ;; method definitions
715 ;; (DEFMETHOD FOO ((X T) (Y T)) "Z")
716 ;; (DEFMETHOD FOO ((X T) Y) "Z")
717 ;; are otherwise equivalent, PCL treated Y as
718 ;; ignorable in the first definition but not in the
719 ;; second definition. We make all required
720 ;; parameters ignorable as a way of systematizing
721 ;; the old PCL behavior. -- WHN 2000-11-24
722 (declare (ignorable ,@required-parameters))
723 ,class-declarations
724 ,@declarations
725 (block ,(fun-name-block-name generic-function-name)
726 ,@real-body)))
727 (constant-value-p (and (null (cdr real-body))
728 (constantp (car real-body))))
729 (constant-value (and constant-value-p
730 (constant-form-value (car real-body))))
731 (plist (and constant-value-p
732 (or (typep constant-value
733 '(or number character))
734 (and (symbolp constant-value)
735 (symbol-package constant-value)))
736 (list :constant-value constant-value)))
737 (applyp (dolist (p lambda-list nil)
738 (cond ((memq p '(&optional &rest &key))
739 (return t))
740 ((eq p '&aux)
741 (return nil))))))
742 (multiple-value-bind
743 (walked-lambda call-next-method-p closurep
744 next-method-p-p setq-p)
745 (walk-method-lambda method-lambda
746 required-parameters
748 slots
749 calls)
750 (multiple-value-bind (walked-lambda-body
751 walked-declarations
752 walked-documentation)
753 (parse-body (cddr walked-lambda))
754 (declare (ignore walked-documentation))
755 (when (or next-method-p-p call-next-method-p)
756 (setq plist (list* :needs-next-methods-p t plist)))
757 (when (some #'cdr slots)
758 (multiple-value-bind (slot-name-lists call-list)
759 (slot-name-lists-from-slots slots calls)
760 (let ((pv-table-symbol (make-symbol "pv-table")))
761 (setq plist
762 `(,@(when slot-name-lists
763 `(:slot-name-lists ,slot-name-lists))
764 ,@(when call-list
765 `(:call-list ,call-list))
766 :pv-table-symbol ,pv-table-symbol
767 ,@plist))
768 (setq walked-lambda-body
769 `((pv-binding (,required-parameters
770 ,slot-name-lists
771 ,pv-table-symbol)
772 ,@walked-lambda-body))))))
773 (when (and (memq '&key lambda-list)
774 (not (memq '&allow-other-keys lambda-list)))
775 (let ((aux (memq '&aux lambda-list)))
776 (setq lambda-list (nconc (ldiff lambda-list aux)
777 (list '&allow-other-keys)
778 aux))))
779 (values `(lambda (.method-args. .next-methods.)
780 (simple-lexical-method-functions
781 (,lambda-list .method-args. .next-methods.
782 :call-next-method-p
783 ,call-next-method-p
784 :next-method-p-p ,next-method-p-p
785 :setq-p ,setq-p
786 ;; we need to pass this along
787 ;; so that NO-NEXT-METHOD can
788 ;; be given a suitable METHOD
789 ;; argument; we need the
790 ;; QUALIFIERS and SPECIALIZERS
791 ;; inside the declaration to
792 ;; give to FIND-METHOD.
793 :method-name-declaration ,name-decl
794 :closurep ,closurep
795 :applyp ,applyp)
796 ,@walked-declarations
797 ,@walked-lambda-body))
798 `(,@(when plist
799 `(:plist ,plist))
800 ,@(when documentation
801 `(:documentation ,documentation)))))))))))
803 (unless (fboundp 'make-method-lambda)
804 (setf (gdefinition 'make-method-lambda)
805 (symbol-function 'real-make-method-lambda)))
807 (defmacro simple-lexical-method-functions ((lambda-list
808 method-args
809 next-methods
810 &rest lmf-options)
811 &body body)
812 `(progn
813 ,method-args ,next-methods
814 (bind-simple-lexical-method-functions (,method-args ,next-methods
815 ,lmf-options)
816 (bind-args (,lambda-list ,method-args)
817 ,@body))))
819 (defmacro fast-lexical-method-functions ((lambda-list
820 next-method-call
821 args
822 rest-arg
823 &rest lmf-options)
824 &body body)
825 `(bind-fast-lexical-method-functions (,args ,rest-arg ,next-method-call ,lmf-options)
826 (bind-args (,(nthcdr (length args) lambda-list) ,rest-arg)
827 ,@body)))
829 (defmacro bind-simple-lexical-method-functions
830 ((method-args next-methods (&key call-next-method-p next-method-p-p setq-p
831 closurep applyp method-name-declaration))
832 &body body
833 &environment env)
834 (if (not (or call-next-method-p setq-p closurep next-method-p-p applyp))
835 `(locally
836 ,@body)
837 `(let ((.next-method. (car ,next-methods))
838 (,next-methods (cdr ,next-methods)))
839 (declare (ignorable .next-method. ,next-methods))
840 (flet (,@(and call-next-method-p
841 `((call-next-method
842 (&rest cnm-args)
843 ,@(if (safe-code-p env)
844 `((%check-cnm-args cnm-args
845 ,method-args
846 ',method-name-declaration))
847 nil)
848 (if .next-method.
849 (funcall (if (std-instance-p .next-method.)
850 (method-function .next-method.)
851 .next-method.) ; for early methods
852 (or cnm-args ,method-args)
853 ,next-methods)
854 (apply #'call-no-next-method
855 ',method-name-declaration
856 (or cnm-args ,method-args))))))
857 ,@(and next-method-p-p
858 '((next-method-p ()
859 (not (null .next-method.))))))
860 ,@body))))
862 (defun call-no-next-method (method-name-declaration &rest args)
863 (destructuring-bind (name) method-name-declaration
864 (destructuring-bind (name &rest qualifiers-and-specializers) name
865 ;; KLUDGE: inefficient traversal, but hey. This should only
866 ;; happen on the slow error path anyway.
867 (let* ((qualifiers (butlast qualifiers-and-specializers))
868 (specializers (car (last qualifiers-and-specializers)))
869 (method (find-method (gdefinition name) qualifiers specializers)))
870 (apply #'no-next-method
871 (method-generic-function method)
872 method
873 args)))))
875 (defstruct (method-call (:copier nil))
876 (function #'identity :type function)
877 call-method-args)
879 #-sb-fluid (declaim (sb-ext:freeze-type method-call))
881 (defmacro invoke-method-call1 (function args cm-args)
882 `(let ((.function. ,function)
883 (.args. ,args)
884 (.cm-args. ,cm-args))
885 (if (and .cm-args. (null (cdr .cm-args.)))
886 (funcall .function. .args. (car .cm-args.))
887 (apply .function. .args. .cm-args.))))
889 (defmacro invoke-method-call (method-call restp &rest required-args+rest-arg)
890 `(invoke-method-call1 (method-call-function ,method-call)
891 ,(if restp
892 `(list* ,@required-args+rest-arg)
893 `(list ,@required-args+rest-arg))
894 (method-call-call-method-args ,method-call)))
896 (defstruct (fast-method-call (:copier nil))
897 (function #'identity :type function)
898 pv-cell
899 next-method-call
900 arg-info)
902 #-sb-fluid (declaim (sb-ext:freeze-type fast-method-call))
904 (defmacro fmc-funcall (fn pv-cell next-method-call &rest args)
905 `(funcall ,fn ,pv-cell ,next-method-call ,@args))
907 (defmacro invoke-fast-method-call (method-call &rest required-args+rest-arg)
908 `(fmc-funcall (fast-method-call-function ,method-call)
909 (fast-method-call-pv-cell ,method-call)
910 (fast-method-call-next-method-call ,method-call)
911 ,@required-args+rest-arg))
913 (defstruct (fast-instance-boundp (:copier nil))
914 (index 0 :type fixnum))
916 #-sb-fluid (declaim (sb-ext:freeze-type fast-instance-boundp))
918 (eval-when (:compile-toplevel :load-toplevel :execute)
919 (defvar *allow-emf-call-tracing-p* nil)
920 (defvar *enable-emf-call-tracing-p* #-sb-show nil #+sb-show t))
922 ;;;; effective method functions
924 (defvar *emf-call-trace-size* 200)
925 (defvar *emf-call-trace* nil)
926 (defvar *emf-call-trace-index* 0)
928 ;;; This function was in the CMU CL version of PCL (ca Debian 2.4.8)
929 ;;; without explanation. It appears to be intended for debugging, so
930 ;;; it might be useful someday, so I haven't deleted it.
931 ;;; But it isn't documented and isn't used for anything now, so
932 ;;; I've conditionalized it out of the base system. -- WHN 19991213
933 #+sb-show
934 (defun show-emf-call-trace ()
935 (when *emf-call-trace*
936 (let ((j *emf-call-trace-index*)
937 (*enable-emf-call-tracing-p* nil))
938 (format t "~&(The oldest entries are printed first)~%")
939 (dotimes-fixnum (i *emf-call-trace-size*)
940 (let ((ct (aref *emf-call-trace* j)))
941 (when ct (print ct)))
942 (incf j)
943 (when (= j *emf-call-trace-size*)
944 (setq j 0))))))
946 (defun trace-emf-call-internal (emf format args)
947 (unless *emf-call-trace*
948 (setq *emf-call-trace* (make-array *emf-call-trace-size*)))
949 (setf (aref *emf-call-trace* *emf-call-trace-index*)
950 (list* emf format args))
951 (incf *emf-call-trace-index*)
952 (when (= *emf-call-trace-index* *emf-call-trace-size*)
953 (setq *emf-call-trace-index* 0)))
955 (defmacro trace-emf-call (emf format args)
956 (when *allow-emf-call-tracing-p*
957 `(when *enable-emf-call-tracing-p*
958 (trace-emf-call-internal ,emf ,format ,args))))
960 (defmacro invoke-effective-method-function-fast
961 (emf restp &rest required-args+rest-arg)
962 `(progn
963 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
964 (invoke-fast-method-call ,emf ,@required-args+rest-arg)))
966 (defmacro invoke-effective-method-function (emf restp
967 &rest required-args+rest-arg)
968 (unless (constantp restp)
969 (error "The RESTP argument is not constant."))
970 ;; FIXME: The RESTP handling here is confusing and maybe slightly
971 ;; broken if RESTP evaluates to a non-self-evaluating form. E.g. if
972 ;; (INVOKE-EFFECTIVE-METHOD-FUNCTION EMF '(ERROR "gotcha") ...)
973 ;; then TRACE-EMF-CALL-CALL-INTERNAL might die on a gotcha error.
974 (setq restp (constant-form-value restp))
975 `(progn
976 (trace-emf-call ,emf ,restp (list ,@required-args+rest-arg))
977 (cond ((typep ,emf 'fast-method-call)
978 (invoke-fast-method-call ,emf ,@required-args+rest-arg))
979 ;; "What," you may wonder, "do these next two clauses do?"
980 ;; In that case, you are not a PCL implementor, for they
981 ;; considered this to be self-documenting.:-| Or CSR, for
982 ;; that matter, since he can also figure it out by looking
983 ;; at it without breaking stride. For the rest of us,
984 ;; though: From what the code is doing with .SLOTS. and
985 ;; whatnot, evidently it's implementing SLOT-VALUEish and
986 ;; GET-SLOT-VALUEish things. Then we can reason backwards
987 ;; and conclude that setting EMF to a FIXNUM is an
988 ;; optimized way to represent these slot access operations.
989 ,@(when (and (null restp) (= 1 (length required-args+rest-arg)))
990 `(((typep ,emf 'fixnum)
991 (let* ((.slots. (get-slots-or-nil
992 ,(car required-args+rest-arg)))
993 (value (when .slots. (clos-slots-ref .slots. ,emf))))
994 (if (eq value +slot-unbound+)
995 (slot-unbound-internal ,(car required-args+rest-arg)
996 ,emf)
997 value)))))
998 ,@(when (and (null restp) (= 2 (length required-args+rest-arg)))
999 `(((typep ,emf 'fixnum)
1000 (let ((.new-value. ,(car required-args+rest-arg))
1001 (.slots. (get-slots-or-nil
1002 ,(cadr required-args+rest-arg))))
1003 (when .slots.
1004 (setf (clos-slots-ref .slots. ,emf) .new-value.))))))
1005 ;; (In cmucl-2.4.8 there was a commented-out third ,@(WHEN
1006 ;; ...) clause here to handle SLOT-BOUNDish stuff. Since
1007 ;; there was no explanation and presumably the code is 10+
1008 ;; years stale, I simply deleted it. -- WHN)
1010 (etypecase ,emf
1011 (method-call
1012 (invoke-method-call ,emf ,restp ,@required-args+rest-arg))
1013 (function
1014 ,(if restp
1015 `(apply (the function ,emf) ,@required-args+rest-arg)
1016 `(funcall (the function ,emf)
1017 ,@required-args+rest-arg))))))))
1019 (defun invoke-emf (emf args)
1020 (trace-emf-call emf t args)
1021 (etypecase emf
1022 (fast-method-call
1023 (let* ((arg-info (fast-method-call-arg-info emf))
1024 (restp (cdr arg-info))
1025 (nreq (car arg-info)))
1026 (if restp
1027 (let* ((rest-args (nthcdr nreq args))
1028 (req-args (ldiff args rest-args)))
1029 (apply (fast-method-call-function emf)
1030 (fast-method-call-pv-cell emf)
1031 (fast-method-call-next-method-call emf)
1032 (nconc req-args (list rest-args))))
1033 (cond ((null args)
1034 (if (eql nreq 0)
1035 (invoke-fast-method-call emf)
1036 (error 'simple-program-error
1037 :format-control "invalid number of arguments: 0"
1038 :format-arguments nil)))
1039 ((null (cdr args))
1040 (if (eql nreq 1)
1041 (invoke-fast-method-call emf (car args))
1042 (error 'simple-program-error
1043 :format-control "invalid number of arguments: 1"
1044 :format-arguments nil)))
1045 ((null (cddr args))
1046 (if (eql nreq 2)
1047 (invoke-fast-method-call emf (car args) (cadr args))
1048 (error 'simple-program-error
1049 :format-control "invalid number of arguments: 2"
1050 :format-arguments nil)))
1052 (apply (fast-method-call-function emf)
1053 (fast-method-call-pv-cell emf)
1054 (fast-method-call-next-method-call emf)
1055 args))))))
1056 (method-call
1057 (apply (method-call-function emf)
1058 args
1059 (method-call-call-method-args emf)))
1060 (fixnum
1061 (cond ((null args)
1062 (error 'simple-program-error
1063 :format-control "invalid number of arguments: 0"
1064 :format-arguments nil))
1065 ((null (cdr args))
1066 (let* ((slots (get-slots (car args)))
1067 (value (clos-slots-ref slots emf)))
1068 (if (eq value +slot-unbound+)
1069 (slot-unbound-internal (car args) emf)
1070 value)))
1071 ((null (cddr args))
1072 (setf (clos-slots-ref (get-slots (cadr args)) emf)
1073 (car args)))
1074 (t (error 'simple-program-error
1075 :format-control "invalid number of arguments"
1076 :format-arguments nil))))
1077 (fast-instance-boundp
1078 (if (or (null args) (cdr args))
1079 (error 'simple-program-error
1080 :format-control "invalid number of arguments"
1081 :format-arguments nil)
1082 (let ((slots (get-slots (car args))))
1083 (not (eq (clos-slots-ref slots (fast-instance-boundp-index emf))
1084 +slot-unbound+)))))
1085 (function
1086 (apply emf args))))
1089 (defmacro fast-narrowed-emf (emf)
1090 ;; INVOKE-EFFECTIVE-METHOD-FUNCTION has code in it to dispatch on
1091 ;; the possibility that EMF might be of type FIXNUM (as an optimized
1092 ;; representation of a slot accessor). But as far as I (WHN
1093 ;; 2002-06-11) can tell, it's impossible for such a representation
1094 ;; to end up as .NEXT-METHOD-CALL. By reassuring INVOKE-E-M-F that
1095 ;; when called from this context it needn't worry about the FIXNUM
1096 ;; case, we can keep those cases from being compiled, which is good
1097 ;; both because it saves bytes and because it avoids annoying type
1098 ;; mismatch compiler warnings.
1100 ;; KLUDGE: In sbcl-0.7.4.29, the compiler's type system isn't smart
1101 ;; enough about NOT and intersection types to benefit from a (NOT
1102 ;; FIXNUM) declaration here. -- WHN 2002-06-12 (FIXME: maybe it is
1103 ;; now... -- CSR, 2003-06-07)
1105 ;; FIXME: Might the FUNCTION type be omittable here, leaving only
1106 ;; METHOD-CALLs? Failing that, could this be documented somehow?
1107 ;; (It'd be nice if the types involved could be understood without
1108 ;; solving the halting problem.)
1109 `(the (or function method-call fast-method-call)
1110 ,emf))
1112 (defmacro fast-call-next-method-body ((args next-method-call rest-arg)
1113 method-name-declaration
1114 cnm-args)
1115 `(if ,next-method-call
1116 ,(let ((call `(invoke-effective-method-function
1117 (fast-narrowed-emf ,next-method-call)
1118 ,(not (null rest-arg))
1119 ,@args
1120 ,@(when rest-arg `(,rest-arg)))))
1121 `(if ,cnm-args
1122 (bind-args ((,@args
1123 ,@(when rest-arg
1124 `(&rest ,rest-arg)))
1125 ,cnm-args)
1126 ,call)
1127 ,call))
1128 (call-no-next-method ',method-name-declaration
1129 ,@args
1130 ,@(when rest-arg
1131 `(,rest-arg)))))
1133 (defmacro bind-fast-lexical-method-functions
1134 ((args rest-arg next-method-call (&key
1135 call-next-method-p
1136 setq-p
1137 method-name-declaration
1138 next-method-p-p
1139 closurep
1140 applyp))
1141 &body body
1142 &environment env)
1143 (let* ((all-params (append args (when rest-arg (list rest-arg))))
1144 (rebindings (when (or setq-p call-next-method-p)
1145 (mapcar (lambda (x) (list x x)) all-params))))
1146 (if (not (or call-next-method-p setq-p closurep next-method-p-p applyp))
1147 `(locally
1148 ,@body)
1149 `(flet (,@(when call-next-method-p
1150 `((call-next-method (&rest cnm-args)
1151 (declare (muffle-conditions code-deletion-note))
1152 ,@(if (safe-code-p env)
1153 `((%check-cnm-args cnm-args (list ,@args)
1154 ',method-name-declaration))
1155 nil)
1156 (fast-call-next-method-body (,args
1157 ,next-method-call
1158 ,rest-arg)
1159 ,method-name-declaration
1160 cnm-args))))
1161 ,@(when next-method-p-p
1162 `((next-method-p
1164 (not (null ,next-method-call))))))
1165 (let ,rebindings
1166 ,@(when rebindings `((declare (ignorable ,@all-params))))
1167 ,@body)))))
1169 ;;; CMUCL comment (Gerd Moellmann):
1171 ;;; The standard says it's an error if CALL-NEXT-METHOD is called with
1172 ;;; arguments, and the set of methods applicable to those arguments is
1173 ;;; different from the set of methods applicable to the original
1174 ;;; method arguments. (According to Barry Margolin, this rule was
1175 ;;; probably added to ensure that before and around methods are always
1176 ;;; run before primary methods.)
1178 ;;; This could be optimized for the case that the generic function
1179 ;;; doesn't have hairy methods, does have standard method combination,
1180 ;;; is a standard generic function, there are no methods defined on it
1181 ;;; for COMPUTE-APPLICABLE-METHODS and probably a lot more of such
1182 ;;; preconditions. That looks hairy and is probably not worth it,
1183 ;;; because this check will never be fast.
1184 (defun %check-cnm-args (cnm-args orig-args method-name-declaration)
1185 (when cnm-args
1186 (let* ((gf (fdefinition (caar method-name-declaration)))
1187 (omethods (compute-applicable-methods gf orig-args))
1188 (nmethods (compute-applicable-methods gf cnm-args)))
1189 (unless (equal omethods nmethods)
1190 (error "~@<The set of methods ~S applicable to argument~P ~
1191 ~{~S~^, ~} to call-next-method is different from ~
1192 the set of methods ~S applicable to the original ~
1193 method argument~P ~{~S~^, ~}.~@:>"
1194 nmethods (length cnm-args) cnm-args omethods
1195 (length orig-args) orig-args)))))
1197 (defmacro bind-args ((lambda-list args) &body body)
1198 (let ((args-tail '.args-tail.)
1199 (key '.key.)
1200 (state 'required))
1201 (flet ((process-var (var)
1202 (if (memq var lambda-list-keywords)
1203 (progn
1204 (case var
1205 (&optional (setq state 'optional))
1206 (&key (setq state 'key))
1207 (&allow-other-keys)
1208 (&rest (setq state 'rest))
1209 (&aux (setq state 'aux))
1210 (otherwise
1211 (error
1212 "encountered the non-standard lambda list keyword ~S"
1213 var)))
1214 nil)
1215 (case state
1216 (required `((,var (pop ,args-tail))))
1217 (optional (cond ((not (consp var))
1218 `((,var (when ,args-tail
1219 (pop ,args-tail)))))
1220 ((null (cddr var))
1221 `((,(car var) (if ,args-tail
1222 (pop ,args-tail)
1223 ,(cadr var)))))
1225 `((,(caddr var) ,args-tail)
1226 (,(car var) (if ,args-tail
1227 (pop ,args-tail)
1228 ,(cadr var)))))))
1229 (rest `((,var ,args-tail)))
1230 (key (cond ((not (consp var))
1231 `((,var (car
1232 (get-key-arg-tail ,(keywordicate var)
1233 ,args-tail)))))
1234 ((null (cddr var))
1235 (multiple-value-bind (keyword variable)
1236 (if (consp (car var))
1237 (values (caar var)
1238 (cadar var))
1239 (values (keywordicate (car var))
1240 (car var)))
1241 `((,key (get-key-arg-tail ',keyword
1242 ,args-tail))
1243 (,variable (if ,key
1244 (car ,key)
1245 ,(cadr var))))))
1247 (multiple-value-bind (keyword variable)
1248 (if (consp (car var))
1249 (values (caar var)
1250 (cadar var))
1251 (values (keywordicate (car var))
1252 (car var)))
1253 `((,key (get-key-arg-tail ',keyword
1254 ,args-tail))
1255 (,(caddr var) ,key)
1256 (,variable (if ,key
1257 (car ,key)
1258 ,(cadr var))))))))
1259 (aux `(,var))))))
1260 (let ((bindings (mapcan #'process-var lambda-list)))
1261 `(let* ((,args-tail ,args)
1262 ,@bindings
1263 (.dummy0.
1264 ,@(when (eq state 'optional)
1265 `((unless (null ,args-tail)
1266 (error 'simple-program-error
1267 :format-control "surplus arguments: ~S"
1268 :format-arguments (list ,args-tail)))))))
1269 (declare (ignorable ,args-tail .dummy0.))
1270 ,@body)))))
1272 (defun get-key-arg-tail (keyword list)
1273 (loop for (key . tail) on list by #'cddr
1274 when (null tail) do
1275 ;; FIXME: Do we want to export this symbol? Or maybe use an
1276 ;; (ERROR 'SIMPLE-PROGRAM-ERROR) form?
1277 (sb-c::%odd-key-args-error)
1278 when (eq key keyword)
1279 return tail))
1281 (defun walk-method-lambda (method-lambda required-parameters env slots calls)
1282 (let ((call-next-method-p nil) ; flag indicating that CALL-NEXT-METHOD
1283 ; should be in the method definition
1284 (closurep nil) ; flag indicating that #'CALL-NEXT-METHOD
1285 ; was seen in the body of a method
1286 (next-method-p-p nil) ; flag indicating that NEXT-METHOD-P
1287 ; should be in the method definition
1288 (setq-p nil))
1289 (flet ((walk-function (form context env)
1290 (cond ((not (eq context :eval)) form)
1291 ;; FIXME: Jumping to a conclusion from the way it's used
1292 ;; above, perhaps CONTEXT should be called SITUATION
1293 ;; (after the term used in the ANSI specification of
1294 ;; EVAL-WHEN) and given modern ANSI keyword values
1295 ;; like :LOAD-TOPLEVEL.
1296 ((not (listp form)) form)
1297 ((eq (car form) 'call-next-method)
1298 (setq call-next-method-p t)
1299 form)
1300 ((eq (car form) 'next-method-p)
1301 (setq next-method-p-p t)
1302 form)
1303 ((memq (car form) '(setq multiple-value-setq))
1304 ;; FIXME: this is possibly a little strong as
1305 ;; conditions go. Ideally we would want to detect
1306 ;; which, if any, of the method parameters are
1307 ;; being set, and communicate that information to
1308 ;; e.g. SPLIT-DECLARATIONS. However, the brute
1309 ;; force method doesn't really cost much; a little
1310 ;; loss of discrimination over IGNORED variables
1311 ;; should be all. -- CSR, 2004-07-01
1312 (setq setq-p t)
1313 form)
1314 ((and (eq (car form) 'function)
1315 (cond ((eq (cadr form) 'call-next-method)
1316 (setq call-next-method-p t)
1317 (setq closurep t)
1318 form)
1319 ((eq (cadr form) 'next-method-p)
1320 (setq next-method-p-p t)
1321 (setq closurep t)
1322 form)
1323 (t nil))))
1324 ((and (memq (car form)
1325 '(slot-value set-slot-value slot-boundp))
1326 (constantp (caddr form)))
1327 (let ((parameter (can-optimize-access form
1328 required-parameters
1329 env)))
1330 (let ((fun (ecase (car form)
1331 (slot-value #'optimize-slot-value)
1332 (set-slot-value #'optimize-set-slot-value)
1333 (slot-boundp #'optimize-slot-boundp))))
1334 (funcall fun slots parameter form))))
1335 ((and (eq (car form) 'apply)
1336 (consp (cadr form))
1337 (eq (car (cadr form)) 'function)
1338 (generic-function-name-p (cadr (cadr form))))
1339 (optimize-generic-function-call
1340 form required-parameters env slots calls))
1341 ((generic-function-name-p (car form))
1342 (optimize-generic-function-call
1343 form required-parameters env slots calls))
1344 (t form))))
1346 (let ((walked-lambda (walk-form method-lambda env #'walk-function)))
1347 (values walked-lambda
1348 call-next-method-p
1349 closurep
1350 next-method-p-p
1351 setq-p)))))
1353 (defun generic-function-name-p (name)
1354 (and (legal-fun-name-p name)
1355 (fboundp name)
1356 (if (eq *boot-state* 'complete)
1357 (standard-generic-function-p (gdefinition name))
1358 (funcallable-instance-p (gdefinition name)))))
1360 (defvar *method-function-plist* (make-hash-table :test 'eq))
1362 (defun method-function-plist (method-function)
1363 (gethash method-function *method-function-plist*))
1365 (defun (setf method-function-plist) (val method-function)
1366 (setf (gethash method-function *method-function-plist*) val))
1368 (defun method-function-get (method-function key &optional default)
1369 (getf (method-function-plist method-function) key default))
1371 (defun (setf method-function-get)
1372 (val method-function key)
1373 (setf (getf (method-function-plist method-function) key) val))
1375 (defun method-function-pv-table (method-function)
1376 (method-function-get method-function :pv-table))
1378 (defun method-function-method (method-function)
1379 (method-function-get method-function :method))
1381 (defun method-function-needs-next-methods-p (method-function)
1382 (method-function-get method-function :needs-next-methods-p t))
1384 (defmacro method-function-closure-generator (method-function)
1385 `(method-function-get ,method-function 'closure-generator))
1387 (defun load-defmethod
1388 (class name quals specls ll initargs source-location)
1389 (setq initargs (copy-tree initargs))
1390 (let ((method-spec (or (getf initargs :method-spec)
1391 (make-method-spec name quals specls))))
1392 (setf (getf initargs :method-spec) method-spec)
1393 (load-defmethod-internal class name quals specls
1394 ll initargs source-location)))
1396 (defun load-defmethod-internal
1397 (method-class gf-spec qualifiers specializers lambda-list
1398 initargs source-location)
1399 (when (and (eq *boot-state* 'complete)
1400 (fboundp gf-spec))
1401 (let* ((gf (fdefinition gf-spec))
1402 (method (and (generic-function-p gf)
1403 (generic-function-methods gf)
1404 (find-method gf
1405 qualifiers
1406 (parse-specializers specializers)
1407 nil))))
1408 (when method
1409 (style-warn "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1410 gf-spec qualifiers specializers))))
1411 (let ((method (apply #'add-named-method
1412 gf-spec qualifiers specializers lambda-list
1413 :definition-source source-location
1414 initargs)))
1415 (unless (or (eq method-class 'standard-method)
1416 (eq (find-class method-class nil) (class-of method)))
1417 ;; FIXME: should be STYLE-WARNING?
1418 (format *error-output*
1419 "~&At the time the method with qualifiers ~:S and~%~
1420 specializers ~:S on the generic function ~S~%~
1421 was compiled, the method-class for that generic function was~%~
1422 ~S. But, the method class is now ~S, this~%~
1423 may mean that this method was compiled improperly.~%"
1424 qualifiers specializers gf-spec
1425 method-class (class-name (class-of method))))
1426 method))
1428 (defun make-method-spec (gf-spec qualifiers unparsed-specializers)
1429 `(slow-method ,gf-spec ,@qualifiers ,unparsed-specializers))
1431 (defun initialize-method-function (initargs &optional return-function-p method)
1432 (let* ((mf (getf initargs :function))
1433 (method-spec (getf initargs :method-spec))
1434 (plist (getf initargs :plist))
1435 (pv-table-symbol (getf plist :pv-table-symbol))
1436 (pv-table nil)
1437 (mff (getf initargs :fast-function)))
1438 (flet ((set-mf-property (p v)
1439 (when mf
1440 (setf (method-function-get mf p) v))
1441 (when mff
1442 (setf (method-function-get mff p) v))))
1443 (when method-spec
1444 (when mf
1445 (setq mf (set-fun-name mf method-spec)))
1446 (when mff
1447 (let ((name `(fast-method ,@(cdr method-spec))))
1448 (set-fun-name mff name)
1449 (unless mf
1450 (set-mf-property :name name)))))
1451 (when plist
1452 (let ((snl (getf plist :slot-name-lists))
1453 (cl (getf plist :call-list)))
1454 (when (or snl cl)
1455 (setq pv-table (intern-pv-table :slot-name-lists snl
1456 :call-list cl))
1457 (when pv-table (set pv-table-symbol pv-table))
1458 (set-mf-property :pv-table pv-table)))
1459 (loop (when (null plist) (return nil))
1460 (set-mf-property (pop plist) (pop plist)))
1461 (when method
1462 (set-mf-property :method method))
1463 (when return-function-p
1464 (or mf (method-function-from-fast-function mff)))))))
1466 (defun analyze-lambda-list (lambda-list)
1467 (flet (;; FIXME: Is this redundant with SB-C::MAKE-KEYWORD-FOR-ARG?
1468 (parse-key-arg (arg)
1469 (if (listp arg)
1470 (if (listp (car arg))
1471 (caar arg)
1472 (keywordicate (car arg)))
1473 (keywordicate arg))))
1474 (let ((nrequired 0)
1475 (noptional 0)
1476 (keysp nil)
1477 (restp nil)
1478 (nrest 0)
1479 (allow-other-keys-p nil)
1480 (keywords ())
1481 (keyword-parameters ())
1482 (state 'required))
1483 (dolist (x lambda-list)
1484 (if (memq x lambda-list-keywords)
1485 (case x
1486 (&optional (setq state 'optional))
1487 (&key (setq keysp t
1488 state 'key))
1489 (&allow-other-keys (setq allow-other-keys-p t))
1490 (&rest (setq restp t
1491 state 'rest))
1492 (&aux (return t))
1493 (otherwise
1494 (error "encountered the non-standard lambda list keyword ~S"
1495 x)))
1496 (ecase state
1497 (required (incf nrequired))
1498 (optional (incf noptional))
1499 (key (push (parse-key-arg x) keywords)
1500 (push x keyword-parameters))
1501 (rest (incf nrest)))))
1502 (when (and restp (zerop nrest))
1503 (error "Error in lambda-list:~%~
1504 After &REST, a DEFGENERIC lambda-list ~
1505 must be followed by at least one variable."))
1506 (values nrequired noptional keysp restp allow-other-keys-p
1507 (reverse keywords)
1508 (reverse keyword-parameters)))))
1510 (defun keyword-spec-name (x)
1511 (let ((key (if (atom x) x (car x))))
1512 (if (atom key)
1513 (keywordicate key)
1514 (car key))))
1516 (defun ftype-declaration-from-lambda-list (lambda-list name)
1517 (multiple-value-bind (nrequired noptional keysp restp allow-other-keys-p
1518 keywords keyword-parameters)
1519 (analyze-lambda-list lambda-list)
1520 (declare (ignore keyword-parameters))
1521 (let* ((old (info :function :type name)) ;FIXME:FDOCUMENTATION instead?
1522 (old-ftype (if (fun-type-p old) old nil))
1523 (old-restp (and old-ftype (fun-type-rest old-ftype)))
1524 (old-keys (and old-ftype
1525 (mapcar #'key-info-name
1526 (fun-type-keywords
1527 old-ftype))))
1528 (old-keysp (and old-ftype (fun-type-keyp old-ftype)))
1529 (old-allowp (and old-ftype
1530 (fun-type-allowp old-ftype)))
1531 (keywords (union old-keys (mapcar #'keyword-spec-name keywords))))
1532 `(function ,(append (make-list nrequired :initial-element t)
1533 (when (plusp noptional)
1534 (append '(&optional)
1535 (make-list noptional :initial-element t)))
1536 (when (or restp old-restp)
1537 '(&rest t))
1538 (when (or keysp old-keysp)
1539 (append '(&key)
1540 (mapcar (lambda (key)
1541 `(,key t))
1542 keywords)
1543 (when (or allow-other-keys-p old-allowp)
1544 '(&allow-other-keys)))))
1545 *))))
1547 (defun defgeneric-declaration (spec lambda-list)
1548 `(ftype ,(ftype-declaration-from-lambda-list lambda-list spec) ,spec))
1550 ;;;; early generic function support
1552 (defvar *!early-generic-functions* ())
1554 (defun ensure-generic-function (fun-name
1555 &rest all-keys
1556 &key environment source-location
1557 &allow-other-keys)
1558 (declare (ignore environment))
1559 (let ((existing (and (fboundp fun-name)
1560 (gdefinition fun-name))))
1561 (if (and existing
1562 (eq *boot-state* 'complete)
1563 (null (generic-function-p existing)))
1564 (generic-clobbers-function fun-name)
1565 (apply #'ensure-generic-function-using-class
1566 existing fun-name all-keys))))
1568 (defun generic-clobbers-function (fun-name)
1569 (error 'simple-program-error
1570 :format-control "~S already names an ordinary function or a macro."
1571 :format-arguments (list fun-name)))
1573 (defvar *sgf-wrapper*
1574 (boot-make-wrapper (early-class-size 'standard-generic-function)
1575 'standard-generic-function))
1577 (defvar *sgf-slots-init*
1578 (mapcar (lambda (canonical-slot)
1579 (if (memq (getf canonical-slot :name) '(arg-info source))
1580 +slot-unbound+
1581 (let ((initfunction (getf canonical-slot :initfunction)))
1582 (if initfunction
1583 (funcall initfunction)
1584 +slot-unbound+))))
1585 (early-collect-inheritance 'standard-generic-function)))
1587 (defvar *sgf-method-class-index*
1588 (!bootstrap-slot-index 'standard-generic-function 'method-class))
1590 (defun early-gf-p (x)
1591 (and (fsc-instance-p x)
1592 (eq (clos-slots-ref (get-slots x) *sgf-method-class-index*)
1593 +slot-unbound+)))
1595 (defvar *sgf-methods-index*
1596 (!bootstrap-slot-index 'standard-generic-function 'methods))
1598 (defmacro early-gf-methods (gf)
1599 `(clos-slots-ref (get-slots ,gf) *sgf-methods-index*))
1601 (defun safe-generic-function-methods (generic-function)
1602 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1603 (clos-slots-ref (get-slots generic-function) *sgf-methods-index*)
1604 (generic-function-methods generic-function)))
1606 (defvar *sgf-arg-info-index*
1607 (!bootstrap-slot-index 'standard-generic-function 'arg-info))
1609 (defmacro early-gf-arg-info (gf)
1610 `(clos-slots-ref (get-slots ,gf) *sgf-arg-info-index*))
1612 (defvar *sgf-dfun-state-index*
1613 (!bootstrap-slot-index 'standard-generic-function 'dfun-state))
1615 (defstruct (arg-info
1616 (:conc-name nil)
1617 (:constructor make-arg-info ())
1618 (:copier nil))
1619 (arg-info-lambda-list :no-lambda-list)
1620 arg-info-precedence
1621 arg-info-metatypes
1622 arg-info-number-optional
1623 arg-info-key/rest-p
1624 arg-info-keys ;nil no &KEY or &REST allowed
1625 ;(k1 k2 ..) Each method must accept these &KEY arguments.
1626 ;T must have &KEY or &REST
1628 gf-info-simple-accessor-type ; nil, reader, writer, boundp
1629 (gf-precompute-dfun-and-emf-p nil) ; set by set-arg-info
1631 gf-info-static-c-a-m-emf
1632 (gf-info-c-a-m-emf-std-p t)
1633 gf-info-fast-mf-p)
1635 #-sb-fluid (declaim (sb-ext:freeze-type arg-info))
1637 (defun arg-info-valid-p (arg-info)
1638 (not (null (arg-info-number-optional arg-info))))
1640 (defun arg-info-applyp (arg-info)
1641 (or (plusp (arg-info-number-optional arg-info))
1642 (arg-info-key/rest-p arg-info)))
1644 (defun arg-info-number-required (arg-info)
1645 (length (arg-info-metatypes arg-info)))
1647 (defun arg-info-nkeys (arg-info)
1648 (count-if (lambda (x) (neq x t)) (arg-info-metatypes arg-info)))
1650 (defun create-gf-lambda-list (lambda-list)
1651 ;;; Create a gf lambda list from a method lambda list
1652 (loop for x in lambda-list
1653 collect (if (consp x) (list (car x)) x)
1654 if (eq x '&key) do (loop-finish)))
1656 (defun set-arg-info (gf &key new-method (lambda-list nil lambda-list-p)
1657 argument-precedence-order)
1658 (let* ((arg-info (if (eq *boot-state* 'complete)
1659 (gf-arg-info gf)
1660 (early-gf-arg-info gf)))
1661 (methods (if (eq *boot-state* 'complete)
1662 (generic-function-methods gf)
1663 (early-gf-methods gf)))
1664 (was-valid-p (integerp (arg-info-number-optional arg-info)))
1665 (first-p (and new-method (null (cdr methods)))))
1666 (when (and (not lambda-list-p) methods)
1667 (setq lambda-list (gf-lambda-list gf)))
1668 (when (or lambda-list-p
1669 (and first-p
1670 (eq (arg-info-lambda-list arg-info) :no-lambda-list)))
1671 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1672 (analyze-lambda-list lambda-list)
1673 (when (and methods (not first-p))
1674 (let ((gf-nreq (arg-info-number-required arg-info))
1675 (gf-nopt (arg-info-number-optional arg-info))
1676 (gf-key/rest-p (arg-info-key/rest-p arg-info)))
1677 (unless (and (= nreq gf-nreq)
1678 (= nopt gf-nopt)
1679 (eq (or keysp restp) gf-key/rest-p))
1680 (error "The lambda-list ~S is incompatible with ~
1681 existing methods of ~S."
1682 lambda-list gf))))
1683 (setf (arg-info-lambda-list arg-info)
1684 (if lambda-list-p
1685 lambda-list
1686 (create-gf-lambda-list lambda-list)))
1687 (when (or lambda-list-p argument-precedence-order
1688 (null (arg-info-precedence arg-info)))
1689 (setf (arg-info-precedence arg-info)
1690 (compute-precedence lambda-list nreq argument-precedence-order)))
1691 (setf (arg-info-metatypes arg-info) (make-list nreq))
1692 (setf (arg-info-number-optional arg-info) nopt)
1693 (setf (arg-info-key/rest-p arg-info) (not (null (or keysp restp))))
1694 (setf (arg-info-keys arg-info)
1695 (if lambda-list-p
1696 (if allow-other-keys-p t keywords)
1697 (arg-info-key/rest-p arg-info)))))
1698 (when new-method
1699 (check-method-arg-info gf arg-info new-method))
1700 (set-arg-info1 gf arg-info new-method methods was-valid-p first-p)
1701 arg-info))
1703 (defun check-method-arg-info (gf arg-info method)
1704 (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
1705 (analyze-lambda-list (if (consp method)
1706 (early-method-lambda-list method)
1707 (method-lambda-list method)))
1708 (flet ((lose (string &rest args)
1709 (error 'simple-program-error
1710 :format-control "~@<attempt to add the method~2I~_~S~I~_~
1711 to the generic function~2I~_~S;~I~_~
1712 but ~?~:>"
1713 :format-arguments (list method gf string args)))
1714 (comparison-description (x y)
1715 (if (> x y) "more" "fewer")))
1716 (let ((gf-nreq (arg-info-number-required arg-info))
1717 (gf-nopt (arg-info-number-optional arg-info))
1718 (gf-key/rest-p (arg-info-key/rest-p arg-info))
1719 (gf-keywords (arg-info-keys arg-info)))
1720 (unless (= nreq gf-nreq)
1721 (lose
1722 "the method has ~A required arguments than the generic function."
1723 (comparison-description nreq gf-nreq)))
1724 (unless (= nopt gf-nopt)
1725 (lose
1726 "the method has ~A optional arguments than the generic function."
1727 (comparison-description nopt gf-nopt)))
1728 (unless (eq (or keysp restp) gf-key/rest-p)
1729 (lose
1730 "the method and generic function differ in whether they accept~_~
1731 &REST or &KEY arguments."))
1732 (when (consp gf-keywords)
1733 (unless (or (and restp (not keysp))
1734 allow-other-keys-p
1735 (every (lambda (k) (memq k keywords)) gf-keywords))
1736 (lose "the method does not accept each of the &KEY arguments~2I~_~
1737 ~S."
1738 gf-keywords)))))))
1740 (defvar *sm-specializers-index*
1741 (!bootstrap-slot-index 'standard-method 'specializers))
1742 (defvar *sm-fast-function-index*
1743 (!bootstrap-slot-index 'standard-method 'fast-function))
1744 (defvar *sm-%function-index*
1745 (!bootstrap-slot-index 'standard-method '%function))
1746 (defvar *sm-plist-index*
1747 (!bootstrap-slot-index 'standard-method 'plist))
1749 ;;; FIXME: we don't actually need this; we could test for the exact
1750 ;;; class and deal with it as appropriate. In fact we probably don't
1751 ;;; need it anyway because we only use this for METHOD-SPECIALIZERS on
1752 ;;; the standard reader method for METHOD-SPECIALIZERS. Probably.
1753 (dolist (s '(specializers fast-function %function plist))
1754 (aver (= (symbol-value (intern (format nil "*SM-~A-INDEX*" s)))
1755 (!bootstrap-slot-index 'standard-reader-method s)
1756 (!bootstrap-slot-index 'standard-writer-method s)
1757 (!bootstrap-slot-index 'standard-boundp-method s))))
1759 (defun safe-method-specializers (method)
1760 (let ((standard-method-classes
1761 (list *the-class-standard-method*
1762 *the-class-standard-reader-method*
1763 *the-class-standard-writer-method*
1764 *the-class-standard-boundp-method*))
1765 (class (class-of method)))
1766 (if (member class standard-method-classes)
1767 (clos-slots-ref (get-slots method) *sm-specializers-index*)
1768 (method-specializers method))))
1769 (defun safe-method-fast-function (method)
1770 (let ((standard-method-classes
1771 (list *the-class-standard-method*
1772 *the-class-standard-reader-method*
1773 *the-class-standard-writer-method*
1774 *the-class-standard-boundp-method*))
1775 (class (class-of method)))
1776 (if (member class standard-method-classes)
1777 (clos-slots-ref (get-slots method) *sm-fast-function-index*)
1778 (method-fast-function method))))
1779 (defun safe-method-function (method)
1780 (let ((standard-method-classes
1781 (list *the-class-standard-method*
1782 *the-class-standard-reader-method*
1783 *the-class-standard-writer-method*
1784 *the-class-standard-boundp-method*))
1785 (class (class-of method)))
1786 (if (member class standard-method-classes)
1787 (clos-slots-ref (get-slots method) *sm-%function-index*)
1788 (method-function method))))
1789 (defun safe-method-qualifiers (method)
1790 (let ((standard-method-classes
1791 (list *the-class-standard-method*
1792 *the-class-standard-reader-method*
1793 *the-class-standard-writer-method*
1794 *the-class-standard-boundp-method*))
1795 (class (class-of method)))
1796 (if (member class standard-method-classes)
1797 (let ((plist (clos-slots-ref (get-slots method) *sm-plist-index*)))
1798 (getf plist 'qualifiers))
1799 (method-qualifiers method))))
1801 (defun set-arg-info1 (gf arg-info new-method methods was-valid-p first-p)
1802 (let* ((existing-p (and methods (cdr methods) new-method))
1803 (nreq (length (arg-info-metatypes arg-info)))
1804 (metatypes (if existing-p
1805 (arg-info-metatypes arg-info)
1806 (make-list nreq)))
1807 (type (if existing-p
1808 (gf-info-simple-accessor-type arg-info)
1809 nil)))
1810 (when (arg-info-valid-p arg-info)
1811 (dolist (method (if new-method (list new-method) methods))
1812 (let* ((specializers (if (or (eq *boot-state* 'complete)
1813 (not (consp method)))
1814 (safe-method-specializers method)
1815 (early-method-specializers method t)))
1816 (class (if (or (eq *boot-state* 'complete) (not (consp method)))
1817 (class-of method)
1818 (early-method-class method)))
1819 (new-type
1820 (when (and class
1821 (or (not (eq *boot-state* 'complete))
1822 (eq (generic-function-method-combination gf)
1823 *standard-method-combination*)))
1824 (cond ((or (eq class *the-class-standard-reader-method*)
1825 (eq class *the-class-global-reader-method*))
1826 'reader)
1827 ((or (eq class *the-class-standard-writer-method*)
1828 (eq class *the-class-global-writer-method*))
1829 'writer)
1830 ((or (eq class *the-class-standard-boundp-method*)
1831 (eq class *the-class-global-boundp-method*))
1832 'boundp)))))
1833 (setq metatypes (mapcar #'raise-metatype metatypes specializers))
1834 (setq type (cond ((null type) new-type)
1835 ((eq type new-type) type)
1836 (t nil)))))
1837 (setf (arg-info-metatypes arg-info) metatypes)
1838 (setf (gf-info-simple-accessor-type arg-info) type)))
1839 (when (or (not was-valid-p) first-p)
1840 (multiple-value-bind (c-a-m-emf std-p)
1841 (if (early-gf-p gf)
1842 (values t t)
1843 (compute-applicable-methods-emf gf))
1844 (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
1845 (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)
1846 (unless (gf-info-c-a-m-emf-std-p arg-info)
1847 (setf (gf-info-simple-accessor-type arg-info) t))))
1848 (unless was-valid-p
1849 (let ((name (if (eq *boot-state* 'complete)
1850 (generic-function-name gf)
1851 (!early-gf-name gf))))
1852 (setf (gf-precompute-dfun-and-emf-p arg-info)
1853 (cond
1854 ((and (consp name)
1855 (member (car name)
1856 *internal-pcl-generalized-fun-name-symbols*))
1857 nil)
1858 (t (let* ((symbol (fun-name-block-name name))
1859 (package (symbol-package symbol)))
1860 (and (or (eq package *pcl-package*)
1861 (memq package (package-use-list *pcl-package*)))
1862 ;; FIXME: this test will eventually be
1863 ;; superseded by the *internal-pcl...* test,
1864 ;; above. While we are in a process of
1865 ;; transition, however, it should probably
1866 ;; remain.
1867 (not (find #\Space (symbol-name symbol))))))))))
1868 (setf (gf-info-fast-mf-p arg-info)
1869 (or (not (eq *boot-state* 'complete))
1870 (let* ((method-class (generic-function-method-class gf))
1871 (methods (compute-applicable-methods
1872 #'make-method-lambda
1873 (list gf (class-prototype method-class)
1874 '(lambda) nil))))
1875 (and methods (null (cdr methods))
1876 (let ((specls (method-specializers (car methods))))
1877 (and (classp (car specls))
1878 (eq 'standard-generic-function
1879 (class-name (car specls)))
1880 (classp (cadr specls))
1881 (eq 'standard-method
1882 (class-name (cadr specls)))))))))
1883 arg-info)
1885 ;;; This is the early definition of ENSURE-GENERIC-FUNCTION-USING-CLASS.
1887 ;;; The STATIC-SLOTS field of the funcallable instances used as early
1888 ;;; generic functions is used to store the early methods and early
1889 ;;; discriminator code for the early generic function. The static
1890 ;;; slots field of the fins contains a list whose:
1891 ;;; CAR - a list of the early methods on this early gf
1892 ;;; CADR - the early discriminator code for this method
1893 (defun ensure-generic-function-using-class (existing spec &rest keys
1894 &key (lambda-list nil
1895 lambda-list-p)
1896 argument-precedence-order
1897 source-location
1898 &allow-other-keys)
1899 (declare (ignore keys))
1900 (cond ((and existing (early-gf-p existing))
1901 (when lambda-list-p
1902 (set-arg-info existing :lambda-list lambda-list))
1903 existing)
1904 ((assoc spec *!generic-function-fixups* :test #'equal)
1905 (if existing
1906 (make-early-gf spec lambda-list lambda-list-p existing
1907 argument-precedence-order source-location)
1908 (error "The function ~S is not already defined." spec)))
1909 (existing
1910 (error "~S should be on the list ~S."
1911 spec
1912 '*!generic-function-fixups*))
1914 (pushnew spec *!early-generic-functions* :test #'equal)
1915 (make-early-gf spec lambda-list lambda-list-p nil
1916 argument-precedence-order source-location))))
1918 (defun make-early-gf (spec &optional lambda-list lambda-list-p
1919 function argument-precedence-order source-location)
1920 (let ((fin (allocate-standard-funcallable-instance
1921 *sgf-wrapper* *sgf-slots-init*)))
1922 (set-funcallable-instance-function
1924 (or function
1925 (if (eq spec 'print-object)
1926 #'(lambda (instance stream)
1927 (print-unreadable-object (instance stream :identity t)
1928 (format stream "std-instance")))
1929 #'(lambda (&rest args)
1930 (declare (ignore args))
1931 (error "The function of the funcallable-instance ~S~
1932 has not been set." fin)))))
1933 (setf (gdefinition spec) fin)
1934 (!bootstrap-set-slot 'standard-generic-function fin 'name spec)
1935 (!bootstrap-set-slot 'standard-generic-function
1937 'source
1938 source-location)
1939 (set-fun-name fin spec)
1940 (let ((arg-info (make-arg-info)))
1941 (setf (early-gf-arg-info fin) arg-info)
1942 (when lambda-list-p
1943 (proclaim (defgeneric-declaration spec lambda-list))
1944 (if argument-precedence-order
1945 (set-arg-info fin
1946 :lambda-list lambda-list
1947 :argument-precedence-order argument-precedence-order)
1948 (set-arg-info fin :lambda-list lambda-list))))
1949 fin))
1951 (defun safe-gf-dfun-state (generic-function)
1952 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1953 (clos-slots-ref (get-slots generic-function) *sgf-dfun-state-index*)
1954 (gf-dfun-state generic-function)))
1955 (defun (setf safe-gf-dfun-state) (new-value generic-function)
1956 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
1957 (setf (clos-slots-ref (get-slots generic-function)
1958 *sgf-dfun-state-index*)
1959 new-value)
1960 (setf (gf-dfun-state generic-function) new-value)))
1962 (defun set-dfun (gf &optional dfun cache info)
1963 (when cache
1964 (setf (cache-owner cache) gf))
1965 (let ((new-state (if (and dfun (or cache info))
1966 (list* dfun cache info)
1967 dfun)))
1968 (if (eq *boot-state* 'complete)
1969 (setf (safe-gf-dfun-state gf) new-state)
1970 (setf (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*)
1971 new-state)))
1972 dfun)
1974 (defun gf-dfun-cache (gf)
1975 (let ((state (if (eq *boot-state* 'complete)
1976 (safe-gf-dfun-state gf)
1977 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1978 (typecase state
1979 (function nil)
1980 (cons (cadr state)))))
1982 (defun gf-dfun-info (gf)
1983 (let ((state (if (eq *boot-state* 'complete)
1984 (safe-gf-dfun-state gf)
1985 (clos-slots-ref (get-slots gf) *sgf-dfun-state-index*))))
1986 (typecase state
1987 (function nil)
1988 (cons (cddr state)))))
1990 (defvar *sgf-name-index*
1991 (!bootstrap-slot-index 'standard-generic-function 'name))
1993 (defun !early-gf-name (gf)
1994 (clos-slots-ref (get-slots gf) *sgf-name-index*))
1996 (defun gf-lambda-list (gf)
1997 (let ((arg-info (if (eq *boot-state* 'complete)
1998 (gf-arg-info gf)
1999 (early-gf-arg-info gf))))
2000 (if (eq :no-lambda-list (arg-info-lambda-list arg-info))
2001 (let ((methods (if (eq *boot-state* 'complete)
2002 (generic-function-methods gf)
2003 (early-gf-methods gf))))
2004 (if (null methods)
2005 (progn
2006 (warn "no way to determine the lambda list for ~S" gf)
2007 nil)
2008 (let* ((method (car (last methods)))
2009 (ll (if (consp method)
2010 (early-method-lambda-list method)
2011 (method-lambda-list method))))
2012 (create-gf-lambda-list ll))))
2013 (arg-info-lambda-list arg-info))))
2015 (defmacro real-ensure-gf-internal (gf-class all-keys env)
2016 `(progn
2017 (cond ((symbolp ,gf-class)
2018 (setq ,gf-class (find-class ,gf-class t ,env)))
2019 ((classp ,gf-class))
2021 (error "The :GENERIC-FUNCTION-CLASS argument (~S) was neither a~%~
2022 class nor a symbol that names a class."
2023 ,gf-class)))
2024 (unless (class-finalized-p ,gf-class)
2025 (if (class-has-a-forward-referenced-superclass-p ,gf-class)
2026 ;; FIXME: reference MOP documentation -- this is an
2027 ;; additional requirement on our users
2028 (error "The generic function class ~S is not finalizeable" ,gf-class)
2029 (finalize-inheritance ,gf-class)))
2030 (remf ,all-keys :generic-function-class)
2031 (remf ,all-keys :environment)
2032 (let ((combin (getf ,all-keys :method-combination '.shes-not-there.)))
2033 (unless (eq combin '.shes-not-there.)
2034 (setf (getf ,all-keys :method-combination)
2035 (find-method-combination (class-prototype ,gf-class)
2036 (car combin)
2037 (cdr combin)))))
2038 (let ((method-class (getf ,all-keys :method-class '.shes-not-there.)))
2039 (unless (eq method-class '.shes-not-there.)
2040 (setf (getf ,all-keys :method-class)
2041 (cond ((classp method-class)
2042 method-class)
2043 (t (find-class method-class t ,env))))))))
2045 (defun real-ensure-gf-using-class--generic-function
2046 (existing
2047 fun-name
2048 &rest all-keys
2049 &key environment (lambda-list nil lambda-list-p)
2050 (generic-function-class 'standard-generic-function gf-class-p)
2051 &allow-other-keys)
2052 (real-ensure-gf-internal generic-function-class all-keys environment)
2053 (unless (or (null gf-class-p)
2054 (eq (class-of existing) generic-function-class))
2055 (change-class existing generic-function-class))
2056 (prog1
2057 (apply #'reinitialize-instance existing all-keys)
2058 (when lambda-list-p
2059 (proclaim (defgeneric-declaration fun-name lambda-list)))))
2061 (defun real-ensure-gf-using-class--null
2062 (existing
2063 fun-name
2064 &rest all-keys
2065 &key environment (lambda-list nil lambda-list-p)
2066 (generic-function-class 'standard-generic-function)
2067 &allow-other-keys)
2068 (declare (ignore existing))
2069 (real-ensure-gf-internal generic-function-class all-keys environment)
2070 (prog1
2071 (setf (gdefinition fun-name)
2072 (apply #'make-instance generic-function-class
2073 :name fun-name all-keys))
2074 (when lambda-list-p
2075 (proclaim (defgeneric-declaration fun-name lambda-list)))))
2077 (defun safe-gf-arg-info (generic-function)
2078 (if (eq (class-of generic-function) *the-class-standard-generic-function*)
2079 (clos-slots-ref (fsc-instance-slots generic-function)
2080 *sgf-arg-info-index*)
2081 (gf-arg-info generic-function)))
2083 ;;; FIXME: this function took on a slightly greater role than it
2084 ;;; previously had around 2005-11-02, when CSR fixed the bug whereby
2085 ;;; having more than one subclass of standard-generic-function caused
2086 ;;; the whole system to die horribly through a metacircle in
2087 ;;; GF-ARG-INFO. The fix is to be slightly more disciplined about
2088 ;;; calling accessor methods -- we call GET-GENERIC-FUN-INFO when
2089 ;;; computing discriminating functions, so we need to be careful about
2090 ;;; having a base case for the recursion, and we provide that with the
2091 ;;; STANDARD-GENERIC-FUNCTION case below. However, we are not (yet)
2092 ;;; as disciplined as CLISP's CLOS/MOP, and it would be nice to get to
2093 ;;; that stage, where all potentially dangerous cases are enumerated
2094 ;;; and stopped. -- CSR, 2005-11-02.
2095 (defun get-generic-fun-info (gf)
2096 ;; values nreq applyp metatypes nkeys arg-info
2097 (multiple-value-bind (applyp metatypes arg-info)
2098 (let* ((arg-info (if (early-gf-p gf)
2099 (early-gf-arg-info gf)
2100 (safe-gf-arg-info gf)))
2101 (metatypes (arg-info-metatypes arg-info)))
2102 (values (arg-info-applyp arg-info)
2103 metatypes
2104 arg-info))
2105 (values (length metatypes) applyp metatypes
2106 (count-if (lambda (x) (neq x t)) metatypes)
2107 arg-info)))
2109 (defun early-make-a-method (class qualifiers arglist specializers initargs doc
2110 &key slot-name object-class method-class-function)
2111 (initialize-method-function initargs)
2112 (let ((parsed ())
2113 (unparsed ()))
2114 ;; Figure out whether we got class objects or class names as the
2115 ;; specializers and set parsed and unparsed appropriately. If we
2116 ;; got class objects, then we can compute unparsed, but if we got
2117 ;; class names we don't try to compute parsed.
2119 ;; Note that the use of not symbolp in this call to every should be
2120 ;; read as 'classp' we can't use classp itself because it doesn't
2121 ;; exist yet.
2122 (if (every (lambda (s) (not (symbolp s))) specializers)
2123 (setq parsed specializers
2124 unparsed (mapcar (lambda (s)
2125 (if (eq s t) t (class-name s)))
2126 specializers))
2127 (setq unparsed specializers
2128 parsed ()))
2129 (list :early-method ;This is an early method dammit!
2131 (getf initargs :function)
2132 (getf initargs :fast-function)
2134 parsed ;The parsed specializers. This is used
2135 ;by early-method-specializers to cache
2136 ;the parse. Note that this only comes
2137 ;into play when there is more than one
2138 ;early method on an early gf.
2140 (append
2141 (list class ;A list to which real-make-a-method
2142 qualifiers ;can be applied to make a real method
2143 arglist ;corresponding to this early one.
2144 unparsed
2145 initargs
2146 doc)
2147 (when slot-name
2148 (list :slot-name slot-name :object-class object-class
2149 :method-class-function method-class-function))))))
2151 (defun real-make-a-method
2152 (class qualifiers lambda-list specializers initargs doc
2153 &rest args &key slot-name object-class method-class-function)
2154 (setq specializers (parse-specializers specializers))
2155 (if method-class-function
2156 (let* ((object-class (if (classp object-class) object-class
2157 (find-class object-class)))
2158 (slots (class-direct-slots object-class))
2159 (slot-definition (find slot-name slots
2160 :key #'slot-definition-name)))
2161 (aver slot-name)
2162 (aver slot-definition)
2163 (let ((initargs (list* :qualifiers qualifiers :lambda-list lambda-list
2164 :specializers specializers :documentation doc
2165 :slot-definition slot-definition
2166 :slot-name slot-name initargs)))
2167 (apply #'make-instance
2168 (apply method-class-function object-class slot-definition
2169 initargs)
2170 initargs)))
2171 (apply #'make-instance class :qualifiers qualifiers
2172 :lambda-list lambda-list :specializers specializers
2173 :documentation doc (append args initargs))))
2175 (defun early-method-function (early-method)
2176 (values (cadr early-method) (caddr early-method)))
2178 (defun early-method-class (early-method)
2179 (find-class (car (fifth early-method))))
2181 (defun early-method-standard-accessor-p (early-method)
2182 (let ((class (first (fifth early-method))))
2183 (or (eq class 'standard-reader-method)
2184 (eq class 'standard-writer-method)
2185 (eq class 'standard-boundp-method))))
2187 (defun early-method-standard-accessor-slot-name (early-method)
2188 (eighth (fifth early-method)))
2190 ;;; Fetch the specializers of an early method. This is basically just
2191 ;;; a simple accessor except that when the second argument is t, this
2192 ;;; converts the specializers from symbols into class objects. The
2193 ;;; class objects are cached in the early method, this makes
2194 ;;; bootstrapping faster because the class objects only have to be
2195 ;;; computed once.
2197 ;;; NOTE:
2198 ;;; The second argument should only be passed as T by
2199 ;;; early-lookup-method. This is to implement the rule that only when
2200 ;;; there is more than one early method on a generic function is the
2201 ;;; conversion from class names to class objects done. This
2202 ;;; corresponds to the fact that we are only allowed to have one
2203 ;;; method on any generic function up until the time classes exist.
2204 (defun early-method-specializers (early-method &optional objectsp)
2205 (if (and (listp early-method)
2206 (eq (car early-method) :early-method))
2207 (cond ((eq objectsp t)
2208 (or (fourth early-method)
2209 (setf (fourth early-method)
2210 (mapcar #'find-class (cadddr (fifth early-method))))))
2212 (fourth (fifth early-method))))
2213 (error "~S is not an early-method." early-method)))
2215 (defun early-method-qualifiers (early-method)
2216 (second (fifth early-method)))
2218 (defun early-method-lambda-list (early-method)
2219 (third (fifth early-method)))
2221 (defun early-add-named-method (generic-function-name
2222 qualifiers
2223 specializers
2224 arglist
2225 &rest initargs)
2226 (let* ((gf (ensure-generic-function generic-function-name))
2227 (existing
2228 (dolist (m (early-gf-methods gf))
2229 (when (and (equal (early-method-specializers m) specializers)
2230 (equal (early-method-qualifiers m) qualifiers))
2231 (return m))))
2232 (new (make-a-method 'standard-method
2233 qualifiers
2234 arglist
2235 specializers
2236 initargs
2237 ())))
2238 (when existing (remove-method gf existing))
2239 (add-method gf new)))
2241 ;;; This is the early version of ADD-METHOD. Later this will become a
2242 ;;; generic function. See !FIX-EARLY-GENERIC-FUNCTIONS which has
2243 ;;; special knowledge about ADD-METHOD.
2244 (defun add-method (generic-function method)
2245 (when (not (fsc-instance-p generic-function))
2246 (error "Early ADD-METHOD didn't get a funcallable instance."))
2247 (when (not (and (listp method) (eq (car method) :early-method)))
2248 (error "Early ADD-METHOD didn't get an early method."))
2249 (push method (early-gf-methods generic-function))
2250 (set-arg-info generic-function :new-method method)
2251 (unless (assoc (!early-gf-name generic-function)
2252 *!generic-function-fixups*
2253 :test #'equal)
2254 (update-dfun generic-function)))
2256 ;;; This is the early version of REMOVE-METHOD. See comments on
2257 ;;; the early version of ADD-METHOD.
2258 (defun remove-method (generic-function method)
2259 (when (not (fsc-instance-p generic-function))
2260 (error "An early remove-method didn't get a funcallable instance."))
2261 (when (not (and (listp method) (eq (car method) :early-method)))
2262 (error "An early remove-method didn't get an early method."))
2263 (setf (early-gf-methods generic-function)
2264 (remove method (early-gf-methods generic-function)))
2265 (set-arg-info generic-function)
2266 (unless (assoc (!early-gf-name generic-function)
2267 *!generic-function-fixups*
2268 :test #'equal)
2269 (update-dfun generic-function)))
2271 ;;; This is the early version of GET-METHOD. See comments on the early
2272 ;;; version of ADD-METHOD.
2273 (defun get-method (generic-function qualifiers specializers
2274 &optional (errorp t))
2275 (if (early-gf-p generic-function)
2276 (or (dolist (m (early-gf-methods generic-function))
2277 (when (and (or (equal (early-method-specializers m nil)
2278 specializers)
2279 (equal (early-method-specializers m t)
2280 specializers))
2281 (equal (early-method-qualifiers m) qualifiers))
2282 (return m)))
2283 (if errorp
2284 (error "can't get early method")
2285 nil))
2286 (real-get-method generic-function qualifiers specializers errorp)))
2288 (defun !fix-early-generic-functions ()
2289 (let ((accessors nil))
2290 ;; Rearrange *!EARLY-GENERIC-FUNCTIONS* to speed up
2291 ;; FIX-EARLY-GENERIC-FUNCTIONS.
2292 (dolist (early-gf-spec *!early-generic-functions*)
2293 (when (every #'early-method-standard-accessor-p
2294 (early-gf-methods (gdefinition early-gf-spec)))
2295 (push early-gf-spec accessors)))
2296 (dolist (spec (nconc accessors
2297 '(accessor-method-slot-name
2298 generic-function-methods
2299 method-specializers
2300 specializerp
2301 specializer-type
2302 specializer-class
2303 slot-definition-location
2304 slot-definition-name
2305 class-slots
2306 gf-arg-info
2307 class-precedence-list
2308 slot-boundp-using-class
2309 (setf slot-value-using-class)
2310 slot-value-using-class
2311 structure-class-p
2312 standard-class-p
2313 funcallable-standard-class-p
2314 specializerp)))
2315 (/show spec)
2316 (setq *!early-generic-functions*
2317 (cons spec
2318 (delete spec *!early-generic-functions* :test #'equal))))
2320 (dolist (early-gf-spec *!early-generic-functions*)
2321 (/show early-gf-spec)
2322 (let* ((gf (gdefinition early-gf-spec))
2323 (methods (mapcar (lambda (early-method)
2324 (let ((args (copy-list (fifth
2325 early-method))))
2326 (setf (fourth args)
2327 (early-method-specializers
2328 early-method t))
2329 (apply #'real-make-a-method args)))
2330 (early-gf-methods gf))))
2331 (setf (generic-function-method-class gf) *the-class-standard-method*)
2332 (setf (generic-function-method-combination gf)
2333 *standard-method-combination*)
2334 (set-methods gf methods)))
2336 (dolist (fn *!early-functions*)
2337 (/show fn)
2338 (setf (gdefinition (car fn)) (fdefinition (caddr fn))))
2340 (dolist (fixup *!generic-function-fixups*)
2341 (/show fixup)
2342 (let* ((fspec (car fixup))
2343 (gf (gdefinition fspec))
2344 (methods (mapcar (lambda (method)
2345 (let* ((lambda-list (first method))
2346 (specializers (second method))
2347 (method-fn-name (third method))
2348 (fn-name (or method-fn-name fspec))
2349 (fn (fdefinition fn-name))
2350 (initargs
2351 (list :function
2352 (set-fun-name
2353 (lambda (args next-methods)
2354 (declare (ignore
2355 next-methods))
2356 (apply fn args))
2357 `(call ,fn-name)))))
2358 (declare (type function fn))
2359 (make-a-method 'standard-method
2361 lambda-list
2362 specializers
2363 initargs
2364 nil)))
2365 (cdr fixup))))
2366 (setf (generic-function-method-class gf) *the-class-standard-method*)
2367 (setf (generic-function-method-combination gf)
2368 *standard-method-combination*)
2369 (set-methods gf methods))))
2370 (/show "leaving !FIX-EARLY-GENERIC-FUNCTIONS"))
2372 ;;; PARSE-DEFMETHOD is used by DEFMETHOD to parse the &REST argument
2373 ;;; into the 'real' arguments. This is where the syntax of DEFMETHOD
2374 ;;; is really implemented.
2375 (defun parse-defmethod (cdr-of-form)
2376 (declare (list cdr-of-form))
2377 (let ((name (pop cdr-of-form))
2378 (qualifiers ())
2379 (spec-ll ()))
2380 (loop (if (and (car cdr-of-form) (atom (car cdr-of-form)))
2381 (push (pop cdr-of-form) qualifiers)
2382 (return (setq qualifiers (nreverse qualifiers)))))
2383 (setq spec-ll (pop cdr-of-form))
2384 (values name qualifiers spec-ll cdr-of-form)))
2386 (defun parse-specializers (specializers)
2387 (declare (list specializers))
2388 (flet ((parse (spec)
2389 (let ((result (specializer-from-type spec)))
2390 (if (specializerp result)
2391 result
2392 (if (symbolp spec)
2393 (error "~S was used as a specializer,~%~
2394 but is not the name of a class."
2395 spec)
2396 (error "~S is not a legal specializer." spec))))))
2397 (mapcar #'parse specializers)))
2399 (defun unparse-specializers (specializers-or-method)
2400 (if (listp specializers-or-method)
2401 (flet ((unparse (spec)
2402 (if (specializerp spec)
2403 (let ((type (specializer-type spec)))
2404 (if (and (consp type)
2405 (eq (car type) 'class))
2406 (let* ((class (cadr type))
2407 (class-name (class-name class)))
2408 (if (eq class (find-class class-name nil))
2409 class-name
2410 type))
2411 type))
2412 (error "~S is not a legal specializer." spec))))
2413 (mapcar #'unparse specializers-or-method))
2414 (unparse-specializers (method-specializers specializers-or-method))))
2416 (defun parse-method-or-spec (spec &optional (errorp t))
2417 (let (gf method name temp)
2418 (if (method-p spec)
2419 (setq method spec
2420 gf (method-generic-function method)
2421 temp (and gf (generic-function-name gf))
2422 name (if temp
2423 (make-method-spec temp
2424 (method-qualifiers method)
2425 (unparse-specializers
2426 (method-specializers method)))
2427 (make-symbol (format nil "~S" method))))
2428 (multiple-value-bind (gf-spec quals specls)
2429 (parse-defmethod spec)
2430 (and (setq gf (and (or errorp (fboundp gf-spec))
2431 (gdefinition gf-spec)))
2432 (let ((nreq (compute-discriminating-function-arglist-info gf)))
2433 (setq specls (append (parse-specializers specls)
2434 (make-list (- nreq (length specls))
2435 :initial-element
2436 *the-class-t*)))
2437 (and
2438 (setq method (get-method gf quals specls errorp))
2439 (setq name
2440 (make-method-spec
2441 gf-spec quals (unparse-specializers specls))))))))
2442 (values gf method name)))
2444 (defun extract-parameters (specialized-lambda-list)
2445 (multiple-value-bind (parameters ignore1 ignore2)
2446 (parse-specialized-lambda-list specialized-lambda-list)
2447 (declare (ignore ignore1 ignore2))
2448 parameters))
2450 (defun extract-lambda-list (specialized-lambda-list)
2451 (multiple-value-bind (ignore1 lambda-list ignore2)
2452 (parse-specialized-lambda-list specialized-lambda-list)
2453 (declare (ignore ignore1 ignore2))
2454 lambda-list))
2456 (defun extract-specializer-names (specialized-lambda-list)
2457 (multiple-value-bind (ignore1 ignore2 specializers)
2458 (parse-specialized-lambda-list specialized-lambda-list)
2459 (declare (ignore ignore1 ignore2))
2460 specializers))
2462 (defun extract-required-parameters (specialized-lambda-list)
2463 (multiple-value-bind (ignore1 ignore2 ignore3 required-parameters)
2464 (parse-specialized-lambda-list specialized-lambda-list)
2465 (declare (ignore ignore1 ignore2 ignore3))
2466 required-parameters))
2468 (define-condition specialized-lambda-list-error
2469 (reference-condition simple-program-error)
2471 (:default-initargs :references (list '(:ansi-cl :section (3 4 3)))))
2473 (defun parse-specialized-lambda-list
2474 (arglist
2475 &optional supplied-keywords (allowed-keywords '(&optional &rest &key &aux))
2476 &aux (specialized-lambda-list-keywords
2477 '(&optional &rest &key &allow-other-keys &aux)))
2478 (let ((arg (car arglist)))
2479 (cond ((null arglist) (values nil nil nil nil))
2480 ((eq arg '&aux)
2481 (values nil arglist nil nil))
2482 ((memq arg lambda-list-keywords)
2483 ;; non-standard lambda-list-keywords are errors.
2484 (unless (memq arg specialized-lambda-list-keywords)
2485 (error 'specialized-lambda-list-error
2486 :format-control "unknown specialized-lambda-list ~
2487 keyword ~S~%"
2488 :format-arguments (list arg)))
2489 ;; no multiple &rest x &rest bla specifying
2490 (when (memq arg supplied-keywords)
2491 (error 'specialized-lambda-list-error
2492 :format-control "multiple occurrence of ~
2493 specialized-lambda-list keyword ~S~%"
2494 :format-arguments (list arg)))
2495 ;; And no placing &key in front of &optional, either.
2496 (unless (memq arg allowed-keywords)
2497 (error 'specialized-lambda-list-error
2498 :format-control "misplaced specialized-lambda-list ~
2499 keyword ~S~%"
2500 :format-arguments (list arg)))
2501 ;; When we are at a lambda-list keyword, the parameters
2502 ;; don't include the lambda-list keyword; the lambda-list
2503 ;; does include the lambda-list keyword; and no
2504 ;; specializers are allowed to follow the lambda-list
2505 ;; keywords (at least for now).
2506 (multiple-value-bind (parameters lambda-list)
2507 (parse-specialized-lambda-list (cdr arglist)
2508 (cons arg supplied-keywords)
2509 (if (eq arg '&key)
2510 (cons '&allow-other-keys
2511 (cdr (member arg allowed-keywords)))
2512 (cdr (member arg allowed-keywords))))
2513 (when (and (eq arg '&rest)
2514 (or (null lambda-list)
2515 (memq (car lambda-list)
2516 specialized-lambda-list-keywords)
2517 (not (or (null (cadr lambda-list))
2518 (memq (cadr lambda-list)
2519 specialized-lambda-list-keywords)))))
2520 (error 'specialized-lambda-list-error
2521 :format-control
2522 "in a specialized-lambda-list, excactly one ~
2523 variable must follow &REST.~%"
2524 :format-arguments nil))
2525 (values parameters
2526 (cons arg lambda-list)
2528 ())))
2529 (supplied-keywords
2530 ;; After a lambda-list keyword there can be no specializers.
2531 (multiple-value-bind (parameters lambda-list)
2532 (parse-specialized-lambda-list (cdr arglist)
2533 supplied-keywords
2534 allowed-keywords)
2535 (values (cons (if (listp arg) (car arg) arg) parameters)
2536 (cons arg lambda-list)
2538 ())))
2540 (multiple-value-bind (parameters lambda-list specializers required)
2541 (parse-specialized-lambda-list (cdr arglist))
2542 (values (cons (if (listp arg) (car arg) arg) parameters)
2543 (cons (if (listp arg) (car arg) arg) lambda-list)
2544 (cons (if (listp arg) (cadr arg) t) specializers)
2545 (cons (if (listp arg) (car arg) arg) required)))))))
2547 (setq *boot-state* 'early)
2549 ;;; FIXME: In here there was a #-CMU definition of SYMBOL-MACROLET
2550 ;;; which used %WALKER stuff. That suggests to me that maybe the code
2551 ;;; walker stuff was only used for implementing stuff like that; maybe
2552 ;;; it's not needed any more? Hunt down what it was used for and see.
2554 (defmacro with-slots (slots instance &body body)
2555 (let ((in (gensym)))
2556 `(let ((,in ,instance))
2557 (declare (ignorable ,in))
2558 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2559 (third instance)
2560 instance)))
2561 (and (symbolp instance)
2562 `((declare (%variable-rebinding ,in ,instance)))))
2564 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2565 (let ((var-name
2566 (if (symbolp slot-entry)
2567 slot-entry
2568 (car slot-entry)))
2569 (slot-name
2570 (if (symbolp slot-entry)
2571 slot-entry
2572 (cadr slot-entry))))
2573 `(,var-name
2574 (slot-value ,in ',slot-name))))
2575 slots)
2576 ,@body))))
2578 (defmacro with-accessors (slots instance &body body)
2579 (let ((in (gensym)))
2580 `(let ((,in ,instance))
2581 (declare (ignorable ,in))
2582 ,@(let ((instance (if (and (consp instance) (eq (car instance) 'the))
2583 (third instance)
2584 instance)))
2585 (and (symbolp instance)
2586 `((declare (%variable-rebinding ,in ,instance)))))
2588 (symbol-macrolet ,(mapcar (lambda (slot-entry)
2589 (let ((var-name (car slot-entry))
2590 (accessor-name (cadr slot-entry)))
2591 `(,var-name (,accessor-name ,in))))
2592 slots)
2593 ,@body))))