Use flatteningization in package-data-list
[sbcl.git] / src / pcl / combin.lisp
blobdf4836bc019133b251e06b53b73aa7585b81214b
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")
26 (defun get-method-function (method &optional method-alist wrappers)
27 (let ((fn (cadr (assoc method method-alist))))
28 (if fn
29 (values fn nil nil nil)
30 (multiple-value-bind (mf fmf)
31 (if (listp method)
32 (early-method-function method)
33 (values nil (safe-method-fast-function method)))
34 (let* ((pv-table (and fmf (method-plist-value method :pv-table))))
35 (if (and fmf (or (null pv-table) wrappers))
36 (let* ((pv-wrappers (when pv-table
37 (pv-wrappers-from-all-wrappers
38 pv-table wrappers)))
39 (pv (when (and pv-table pv-wrappers)
40 (pv-table-lookup pv-table pv-wrappers))))
41 (values mf t fmf pv))
42 (values
43 (or mf (if (listp method)
44 (bug "early method with no method-function")
45 (method-function method)))
46 t nil nil)))))))
48 (defun make-effective-method-function (generic-function form &optional
49 method-alist wrappers)
50 (funcall (make-effective-method-function1 generic-function form
51 (not (null method-alist))
52 (not (null wrappers)))
53 method-alist wrappers))
55 (defun make-effective-method-function1 (generic-function form
56 method-alist-p wrappers-p)
57 (if (and (listp form)
58 (eq (car form) 'call-method))
59 (make-effective-method-function-simple generic-function form)
60 ;; We have some sort of `real' effective method. Go off and get a
61 ;; compiled function for it. Most of the real hair here is done by
62 ;; the GET-FUN mechanism.
63 (make-effective-method-function-internal generic-function form
64 method-alist-p wrappers-p)))
66 (defun make-effective-method-fun-type (generic-function
67 form
68 method-alist-p
69 wrappers-p)
70 (if (and (listp form)
71 (eq (car form) 'call-method))
72 (let* ((cm-args (cdr form))
73 (method (car cm-args)))
74 (when method
75 (if (if (listp method)
76 (eq (car method) :early-method)
77 (method-p method))
78 (if method-alist-p
80 (multiple-value-bind (mf fmf)
81 (if (listp method)
82 (early-method-function method)
83 (values nil (safe-method-fast-function method)))
84 (declare (ignore mf))
85 (let* ((pv-table (and fmf (method-plist-value method :pv-table))))
86 (if (and fmf (or (null pv-table) wrappers-p))
87 'fast-method-call
88 'method-call))))
89 (if (and (consp method) (eq (car method) 'make-method))
90 (make-effective-method-fun-type
91 generic-function (cadr method) method-alist-p wrappers-p)
92 (type-of method)))))
93 'fast-method-call))
95 (defun make-effective-method-function-simple
96 (generic-function form &optional no-fmf-p)
97 ;; The effective method is just a call to CALL-METHOD. This opens up
98 ;; the possibility of just using the method function of the method as
99 ;; the effective method function.
101 ;; But we have to be careful. If that method function will ask for
102 ;; the next methods we have to provide them. We do not look to see
103 ;; if there are next methods, we look at whether the method function
104 ;; asks about them. If it does, we must tell it whether there are
105 ;; or aren't to prevent the leaky next methods bug.
106 (let* ((cm-args (cdr form))
107 (fmf-p (and (null no-fmf-p)
108 (or (not (eq **boot-state** 'complete))
109 (gf-fast-method-function-p generic-function))
110 (null (cddr cm-args))))
111 (method (car cm-args))
112 (cm-args1 (cdr cm-args)))
113 (lambda (method-alist wrappers)
114 (make-effective-method-function-simple1 generic-function
115 method
116 cm-args1
117 fmf-p
118 method-alist
119 wrappers))))
121 (defun make-emf-from-method
122 (method cm-args &optional gf fmf-p method-alist wrappers)
123 ;; Avoid style-warning about compiler-macro being unavailable.
124 (declare (notinline make-instance))
125 (multiple-value-bind (mf real-mf-p fmf pv)
126 (get-method-function method method-alist wrappers)
127 (if fmf
128 (let* ((next-methods (car cm-args))
129 (next (make-effective-method-function-simple1
130 gf (car next-methods)
131 (list* (cdr next-methods) (cdr cm-args))
132 fmf-p method-alist wrappers))
133 (arg-info (method-plist-value method :arg-info))
134 (default (cons nil nil))
135 (value (method-plist-value method :constant-value default)))
136 (if (eq value default)
137 (make-fast-method-call :function fmf :pv pv
138 :next-method-call next :arg-info arg-info)
139 (make-constant-fast-method-call
140 :function fmf :pv pv :next-method-call next
141 :arg-info arg-info :value value)))
142 (if real-mf-p
143 (flet ((frob-cm-arg (arg)
144 (if (if (listp arg)
145 (eq (car arg) :early-method)
146 (method-p arg))
148 (if (and (consp arg) (eq (car arg) 'make-method))
149 (let ((emf (make-effective-method-function
150 gf (cadr arg) method-alist wrappers)))
151 (etypecase emf
152 (method-call
153 (make-instance 'standard-method
154 :specializers nil ; XXX
155 :qualifiers nil ; XXX
156 :function (method-call-function emf)))
157 (fast-method-call
158 (let* ((fmf (fast-method-call-function emf))
159 (fun (method-function-from-fast-method-call emf))
160 (mf (%make-method-function fmf nil)))
161 (set-funcallable-instance-function mf fun)
162 (make-instance 'standard-method
163 :specializers nil ; XXX
164 :qualifiers nil
165 :function mf)))))
166 arg))))
167 (let* ((default (cons nil nil))
168 (value
169 (method-plist-value method :constant-value default))
170 ;; FIXME: this is wrong. Very wrong. It assumes
171 ;; that the only place that can have make-method
172 ;; calls is in the list structure of the second
173 ;; argument to CALL-METHOD, but AMOP says that
174 ;; CALL-METHOD can be more complicated if
175 ;; COMPUTE-EFFECTIVE-METHOD (and presumably
176 ;; MAKE-METHOD-LAMBDA) is adjusted to match.
178 ;; On the other hand, it's a start, because
179 ;; without this calls to MAKE-METHOD in method
180 ;; combination where one of the methods is of a
181 ;; user-defined class don't work at all. -- CSR,
182 ;; 2006-08-05
183 (args (cons (mapcar #'frob-cm-arg (car cm-args))
184 (cdr cm-args))))
185 (if (eq value default)
186 (make-method-call :function mf :call-method-args args)
187 (make-constant-method-call :function mf :value value
188 :call-method-args args))))
189 mf))))
191 (defun make-effective-method-function-simple1
192 (gf method cm-args fmf-p &optional method-alist wrappers)
193 (when method
194 (if (if (listp method)
195 (eq (car method) :early-method)
196 (method-p method))
197 (make-emf-from-method method cm-args gf fmf-p method-alist wrappers)
198 (if (and (consp method) (eq (car method) 'make-method))
199 (make-effective-method-function gf
200 (cadr method)
201 method-alist wrappers)
202 method))))
204 (defvar *global-effective-method-gensyms* ())
205 (defvar *rebound-effective-method-gensyms*)
207 (defun get-effective-method-gensym ()
208 (or (pop *rebound-effective-method-gensyms*)
209 (let ((new (format-symbol *pcl-package*
210 "EFFECTIVE-METHOD-GENSYM-~D"
211 (length *global-effective-method-gensyms*))))
212 (setq *global-effective-method-gensyms*
213 (append *global-effective-method-gensyms* (list new)))
214 new)))
216 (let ((*rebound-effective-method-gensyms* ()))
217 (dotimes-fixnum (i 10) (get-effective-method-gensym)))
219 (defun expand-effective-method-function (gf effective-method &optional env)
220 (declare (ignore env))
221 (multiple-value-bind (nreq applyp)
222 (get-generic-fun-info gf)
223 (let ((ll (make-fast-method-call-lambda-list nreq applyp))
224 (check-applicable-keywords
225 (when (and applyp (gf-requires-emf-keyword-checks gf))
226 '((check-applicable-keywords))))
227 (error-p (or (eq (first effective-method) '%no-primary-method)
228 (eq (first effective-method) '%invalid-qualifiers)))
229 (mc-args-p
230 (when (eq **boot-state** 'complete)
231 ;; Otherwise the METHOD-COMBINATION slot is not bound.
232 (let ((combin (generic-function-method-combination gf)))
233 (and (long-method-combination-p combin)
234 (long-method-combination-args-lambda-list combin)))))
235 (name `(emf ,(generic-function-name gf))))
236 (cond
237 (error-p
238 `(named-lambda ,name (.pv. .next-method-call. &rest .args.)
239 (declare (ignore .pv. .next-method-call.))
240 (declare (ignorable .args.))
241 (flet ((%no-primary-method (gf args)
242 (call-no-primary-method gf args))
243 (%invalid-qualifiers (gf combin method)
244 (invalid-qualifiers gf combin method)))
245 (declare (ignorable #'%no-primary-method #'%invalid-qualifiers))
246 ,effective-method)))
247 (mc-args-p
248 (let* ((required (make-dfun-required-args nreq))
249 (gf-args (if applyp
250 `(list* ,@required
251 (sb-c::%listify-rest-args
252 .dfun-more-context.
253 (the (and unsigned-byte fixnum)
254 .dfun-more-count.)))
255 `(list ,@required))))
256 `(named-lambda ,name ,ll
257 (declare (ignore .pv. .next-method-call.))
258 (let ((.gf-args. ,gf-args))
259 (declare (ignorable .gf-args.))
260 ,@check-applicable-keywords
261 ,effective-method))))
263 `(named-lambda ,name ,ll
264 (declare (ignore ,@(if error-p ll '(.pv. .next-method-call.))))
265 ,@check-applicable-keywords
266 ,effective-method))))))
268 (defun expand-emf-call-method (gf form metatypes applyp env)
269 (declare (ignore gf metatypes applyp env))
270 `(call-method ,(cdr form)))
272 (defmacro call-method (&rest args)
273 (declare (ignore args))
274 ;; the PROGN is here to defend against premature macroexpansion by
275 ;; RESTART-CASE.
276 `(progn (error "~S outside of a effective method form" 'call-method)))
278 (defun make-effective-method-list-fun-type
279 (generic-function form method-alist-p wrappers-p)
280 (if (every (lambda (form)
281 (eq 'fast-method-call
282 (make-effective-method-fun-type
283 generic-function form method-alist-p wrappers-p)))
284 (cdr form))
285 'fast-method-call
288 (defun memf-test-converter (form generic-function method-alist-p wrappers-p)
289 (case (and (consp form) (car form))
290 (call-method
291 (case (make-effective-method-fun-type
292 generic-function form method-alist-p wrappers-p)
293 (fast-method-call '.fast-call-method.)
294 (t '.call-method.)))
295 (call-method-list
296 (case (make-effective-method-list-fun-type
297 generic-function form method-alist-p wrappers-p)
298 (fast-method-call '.fast-call-method-list.)
299 (t '.call-method-list.)))
300 (check-applicable-keywords 'check-applicable-keywords)
301 (t (default-test-converter form))))
303 ;;; CMUCL comment (2003-10-15):
305 ;;; This function is called via the GET-FUNCTION mechanism on forms
306 ;;; of an emf lambda. First value returned replaces FORM in the emf
307 ;;; lambda. Second value is a list of variable names that become
308 ;;; closure variables.
309 (defun memf-code-converter
310 (form generic-function metatypes applyp method-alist-p wrappers-p)
311 (case (and (consp form) (car form))
312 (call-method
313 (let ((gensym (get-effective-method-gensym)))
314 (values (make-emf-call
315 (length metatypes) applyp gensym
316 (make-effective-method-fun-type
317 generic-function form method-alist-p wrappers-p))
318 (list gensym))))
319 (call-method-list
320 (let ((gensym (get-effective-method-gensym))
321 (type (make-effective-method-list-fun-type
322 generic-function form method-alist-p wrappers-p)))
323 (values `(dolist (emf ,gensym nil)
324 ,(make-emf-call (length metatypes) applyp 'emf type))
325 (list gensym))))
326 (check-applicable-keywords
327 (values `(check-applicable-keywords .keyargs-start.
328 .valid-keys.
329 .dfun-more-context.
330 .dfun-more-count.)
331 '(.keyargs-start. .valid-keys.)))
333 (default-code-converter form))))
335 (defun memf-constant-converter (form generic-function)
336 (case (and (consp form) (car form))
337 (call-method
338 (list (cons '.meth.
339 (make-effective-method-function-simple
340 generic-function form))))
341 (call-method-list
342 (list (cons '.meth-list.
343 (mapcar (lambda (form)
344 (make-effective-method-function-simple
345 generic-function form))
346 (cdr form)))))
347 (check-applicable-keywords
348 '(.keyargs-start. .valid-keys.))
350 (default-constant-converter form))))
352 (defvar *applicable-methods*)
353 (defun make-effective-method-function-internal
354 (generic-function effective-method method-alist-p wrappers-p)
355 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
356 (get-generic-fun-info generic-function)
357 (declare (ignore nkeys arg-info))
358 (let* ((*rebound-effective-method-gensyms*
359 *global-effective-method-gensyms*)
360 (name (if (early-gf-p generic-function)
361 (!early-gf-name generic-function)
362 (generic-function-name generic-function)))
363 (arg-info (cons nreq applyp))
364 (effective-method-lambda (expand-effective-method-function
365 generic-function effective-method)))
366 (multiple-value-bind (cfunction constants)
367 (get-fun1 effective-method-lambda
368 (lambda (form)
369 (memf-test-converter form generic-function
370 method-alist-p wrappers-p))
371 (lambda (form)
372 (memf-code-converter form generic-function
373 metatypes applyp
374 method-alist-p wrappers-p))
375 (lambda (form)
376 (memf-constant-converter form generic-function)))
377 (lambda (method-alist wrappers)
378 (multiple-value-bind (valid-keys keyargs-start)
379 (when (memq '.valid-keys. constants)
380 (compute-applicable-keywords
381 generic-function *applicable-methods*))
382 (flet ((compute-constant (constant)
383 (if (consp constant)
384 (case (car constant)
385 (.meth.
386 (funcall (cdr constant) method-alist wrappers))
387 (.meth-list.
388 (mapcar (lambda (fn)
389 (funcall fn method-alist wrappers))
390 (cdr constant)))
391 (t constant))
392 (case constant
393 (.keyargs-start. keyargs-start)
394 (.valid-keys. valid-keys)
395 (t constant)))))
396 (let ((fun (apply cfunction
397 (mapcar #'compute-constant constants))))
398 (set-fun-name fun `(combined-method ,name))
399 (make-fast-method-call :function fun
400 :arg-info arg-info)))))))))
402 (defmacro call-method-list (&rest calls)
403 `(progn ,@calls))
405 (defun make-call-methods (methods)
406 `(call-method-list
407 ,@(mapcar (lambda (method) `(call-method ,method ())) methods)))
409 (defun gf-requires-emf-keyword-checks (generic-function)
410 (member '&key (gf-lambda-list generic-function)))
412 (defun standard-compute-effective-method
413 (generic-function combin applicable-methods)
414 (collect ((before) (primary) (after) (around))
415 (flet ((invalid (gf combin m) (invalid-qualifiers gf combin m)))
416 (dolist (m applicable-methods)
417 (let ((qualifiers (if (listp m)
418 (early-method-qualifiers m)
419 (safe-method-qualifiers m))))
420 (cond
421 ((null qualifiers) (primary m))
422 ((cdr qualifiers) (invalid generic-function combin m))
423 ((eq (car qualifiers) :around) (around m))
424 ((eq (car qualifiers) :before) (before m))
425 ((eq (car qualifiers) :after) (after m))
426 (t (invalid generic-function combin m))))))
427 (cond ((null (primary))
428 `(%no-primary-method ',generic-function .args.))
429 ((and (null (before)) (null (after)) (null (around)))
430 ;; By returning a single call-method `form' here we enable
431 ;; an important implementation-specific optimization; that
432 ;; is, we can use the fast method function directly as the
433 ;; effective method function.
435 ;; However, the requirement by ANSI (CLHS 7.6.5) on generic
436 ;; function argument checking inhibits this, as we don't
437 ;; perform this checking in fast-method-functions given
438 ;; that they are not solely used for effective method
439 ;; functions, but also in combination, when they should not
440 ;; perform argument checks.
441 (let ((call-method
442 `(call-method ,(first (primary)) ,(rest (primary)))))
443 (if (gf-requires-emf-keyword-checks generic-function)
444 ;; the PROGN inhibits the above optimization
445 `(progn ,call-method)
446 call-method)))
448 (let ((main-effective-method
449 (if (or (before) (after))
450 `(multiple-value-prog1
451 (progn
452 ,(make-call-methods (before))
453 (call-method ,(first (primary))
454 ,(rest (primary))))
455 ,(make-call-methods (reverse (after))))
456 `(call-method ,(first (primary)) ,(rest (primary))))))
457 (if (around)
458 `(call-method ,(first (around))
459 (,@(rest (around))
460 (make-method ,main-effective-method)))
461 main-effective-method))))))
463 ;;; helper code for checking keywords in generic function calls.
464 (defun compute-applicable-keywords (gf methods)
465 (let ((any-keyp nil))
466 (flet ((analyze (lambda-list)
467 (multiple-value-bind (llks nreq nopt keys)
468 (analyze-lambda-list lambda-list)
469 (declare (ignore nreq))
470 (when (ll-kwds-keyp llks)
471 (setq any-keyp t))
472 (values nopt (ll-kwds-allowp llks) keys))))
473 (multiple-value-bind (nopt allowp keys)
474 (analyze (generic-function-lambda-list gf))
475 (dolist (method methods)
476 (let ((ll (if (consp method)
477 (early-method-lambda-list method)
478 (method-lambda-list method))))
479 (multiple-value-bind (n allowp method-keys)
480 (analyze ll)
481 (declare (ignore n))
482 (when allowp
483 (return-from compute-applicable-keywords (values t nopt)))
484 (setq keys (union method-keys keys)))))
485 (aver any-keyp)
486 (values (if allowp t keys) nopt)))))
488 (defun check-applicable-keywords (start valid-keys more-context more-count)
489 (let ((allow-other-keys-seen nil)
490 (allow-other-keys nil)
491 (i start))
492 (declare (type index i more-count)
493 (optimize speed))
494 (flet ((current-value ()
495 (sb-c::%more-arg more-context i)))
496 (declare (inline current-value))
497 (collect ((invalid))
498 (loop
499 (when (>= i more-count)
500 (when (and (invalid) (not allow-other-keys))
501 (error 'simple-program-error
502 :format-control "~@<invalid keyword argument~P: ~
503 ~{~S~^, ~} (valid keys are ~{~S~^, ~}).~@:>"
504 :format-arguments (list (length (invalid)) (invalid) valid-keys)))
505 (return))
506 (let ((key (current-value)))
507 (incf i)
508 (cond
509 ((not (symbolp key))
510 (error 'simple-program-error
511 :format-control "~@<keyword argument not a symbol: ~S.~@:>"
512 :format-arguments (list key)))
513 ((= i more-count)
514 (sb-c::%odd-key-args-error))
515 ((eq key :allow-other-keys)
516 ;; only the leftmost :ALLOW-OTHER-KEYS has any effect
517 (unless allow-other-keys-seen
518 (setq allow-other-keys-seen t
519 allow-other-keys (current-value))))
520 ((eq t valid-keys))
521 ((not (memq key valid-keys)) (invalid key))))
522 (incf i))))))
524 ;;;; the STANDARD method combination type. This is coded by hand
525 ;;;; (rather than with DEFINE-METHOD-COMBINATION) for bootstrapping
526 ;;;; and efficiency reasons. Note that the definition of the
527 ;;;; FIND-METHOD-COMBINATION-METHOD appears in the file
528 ;;;; defcombin.lisp. This is because EQL methods can't appear in the
529 ;;;; bootstrap.
530 ;;;;
531 ;;;; The DEFCLASS for the METHOD-COMBINATION and
532 ;;;; STANDARD-METHOD-COMBINATION classes has to appear here for this
533 ;;;; reason. This code must conform to the code in the file
534 ;;;; defcombin.lisp, look there for more details.
536 (defun compute-effective-method (generic-function combin applicable-methods)
537 (standard-compute-effective-method generic-function
538 combin
539 applicable-methods))
541 (defun invalid-method-error (method format-control &rest format-arguments)
542 (let ((sb-debug:*stack-top-hint* (find-caller-frame)))
543 (error "~@<invalid method error for ~2I~_~S ~I~_method: ~2I~_~?~:>"
544 method
545 format-control
546 format-arguments)))
548 (defun method-combination-error (format-control &rest format-arguments)
549 (let ((sb-debug:*stack-top-hint* (find-caller-frame)))
550 (error "~@<method combination error in CLOS dispatch: ~2I~_~?~:>"
551 format-control
552 format-arguments)))