0.8.12.7: Merge package locks, AKA "what can go wrong with a 3783 line patch?"
[sbcl.git] / src / pcl / combin.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3
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.
9
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.
23
24 (in-package "SB-PCL")
25 \f
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 (method-fast-function method)))
34           (let* ((pv-table (and fmf (method-function-pv-table fmf))))
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-cell (when (and pv-table pv-wrappers)
40                                   (pv-table-lookup pv-table pv-wrappers))))
41                   (values mf t fmf pv-cell))
42                 (values
43                  (or mf (if (listp method)
44                             (setf (cadr method)
45                                   (method-function-from-fast-function fmf))
46                             (method-function method)))
47                  t nil nil)))))))
48
49 (defun make-effective-method-function (generic-function form &optional
50                                        method-alist wrappers)
51   (funcall (make-effective-method-function1 generic-function form
52                                             (not (null method-alist))
53                                             (not (null wrappers)))
54            method-alist wrappers))
55
56 (defun make-effective-method-function1 (generic-function form
57                                         method-alist-p wrappers-p)
58   (if (and (listp form)
59            (eq (car form) 'call-method))
60       (make-effective-method-function-simple generic-function form)
61       ;; We have some sort of `real' effective method. Go off and get a
62       ;; compiled function for it. Most of the real hair here is done by
63       ;; the GET-FUN mechanism.
64       (make-effective-method-function-internal generic-function form
65                                                method-alist-p wrappers-p)))
66
67 (defun make-effective-method-fun-type (generic-function
68                                        form
69                                        method-alist-p
70                                        wrappers-p)
71   (if (and (listp form)
72            (eq (car form) 'call-method))
73       (let* ((cm-args (cdr form))
74              (method (car cm-args)))
75         (when method
76           (if (if (listp method)
77                   (eq (car method) :early-method)
78                   (method-p method))
79               (if method-alist-p
80                   t
81                   (multiple-value-bind (mf fmf)
82                       (if (listp method)
83                           (early-method-function method)
84                           (values nil (method-fast-function method)))
85                     (declare (ignore mf))
86                     (let* ((pv-table (and fmf (method-function-pv-table fmf))))
87                       (if (and fmf (or (null pv-table) wrappers-p))
88                           'fast-method-call
89                           'method-call))))
90               (if (and (consp method) (eq (car method) 'make-method))
91                   (make-effective-method-fun-type
92                    generic-function (cadr method) method-alist-p wrappers-p)
93                   (type-of method)))))
94       'fast-method-call))
95
96 (defun make-effective-method-function-simple
97     (generic-function form &optional no-fmf-p)
98   ;; The effective method is just a call to CALL-METHOD. This opens up
99   ;; the possibility of just using the method function of the method as
100   ;; the effective method function.
101   ;;
102   ;; But we have to be careful. If that method function will ask for
103   ;; the next methods we have to provide them. We do not look to see
104   ;; if there are next methods, we look at whether the method function
105   ;; asks about them. If it does, we must tell it whether there are
106   ;; or aren't to prevent the leaky next methods bug.
107   (let* ((cm-args (cdr form))
108          (fmf-p (and (null no-fmf-p)
109                      (or (not (eq *boot-state* 'complete))
110                          (gf-fast-method-function-p generic-function))
111                      (null (cddr cm-args))))
112          (method (car cm-args))
113          (cm-args1 (cdr cm-args)))
114     (lambda (method-alist wrappers)
115       (make-effective-method-function-simple1 generic-function
116                                               method
117                                               cm-args1
118                                               fmf-p
119                                               method-alist
120                                               wrappers))))
121
122 (defun make-emf-from-method
123     (method cm-args &optional gf fmf-p method-alist wrappers)
124   (multiple-value-bind (mf real-mf-p fmf pv-cell)
125       (get-method-function method method-alist wrappers)
126     (if fmf
127         (let* ((next-methods (car cm-args))
128                (next (make-effective-method-function-simple1
129                       gf (car next-methods)
130                       (list* (cdr next-methods) (cdr cm-args))
131                       fmf-p method-alist wrappers))
132                (arg-info (method-function-get fmf :arg-info)))
133           (make-fast-method-call :function fmf
134                                  :pv-cell pv-cell
135                                  :next-method-call next
136                                  :arg-info arg-info))
137         (if real-mf-p
138             (make-method-call :function mf
139                               :call-method-args cm-args)
140             mf))))
141
142 (defun make-effective-method-function-simple1
143     (gf method cm-args fmf-p &optional method-alist wrappers)
144   (when method
145     (if (if (listp method)
146             (eq (car method) :early-method)
147             (method-p method))
148         (make-emf-from-method method cm-args gf fmf-p method-alist wrappers)
149         (if (and (consp method) (eq (car method) 'make-method))
150             (make-effective-method-function gf
151                                             (cadr method)
152                                             method-alist wrappers)
153             method))))
154
155 (defvar *global-effective-method-gensyms* ())
156 (defvar *rebound-effective-method-gensyms*)
157
158 (defun get-effective-method-gensym ()
159   (or (pop *rebound-effective-method-gensyms*)
160       (let ((new (format-symbol *pcl-package*
161                                 "EFFECTIVE-METHOD-GENSYM-~D"
162                                 (length *global-effective-method-gensyms*))))
163         (setq *global-effective-method-gensyms*
164               (append *global-effective-method-gensyms* (list new)))
165         new)))
166
167 (let ((*rebound-effective-method-gensyms* ()))
168   (dotimes-fixnum (i 10) (get-effective-method-gensym)))
169
170 (defun expand-effective-method-function (gf effective-method &optional env)
171   (declare (ignore env))
172   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
173       (get-generic-fun-info gf)
174     (declare (ignore nreq nkeys arg-info))
175     (let ((ll (make-fast-method-call-lambda-list metatypes applyp))
176           (check-applicable-keywords
177            (when (and applyp (gf-requires-emf-keyword-checks gf))
178              '((check-applicable-keywords))))
179           (error-p (or (eq (first effective-method) '%no-primary-method)
180                        (eq (first effective-method) '%invalid-qualifiers)))
181           (mc-args-p
182            (when (eq *boot-state* 'complete)
183              ;; Otherwise the METHOD-COMBINATION slot is not bound.
184              (let ((combin (generic-function-method-combination gf)))
185                (and (long-method-combination-p combin)
186                     (long-method-combination-args-lambda-list combin))))))
187       (cond
188         (error-p
189          `(lambda (.pv-cell. .next-method-call. &rest .args.)
190            (declare (ignore .pv-cell. .next-method-call.))
191            (declare (ignorable .args.))
192            (flet ((%no-primary-method (gf args)
193                     (apply #'no-primary-method gf args))
194                   (%invalid-qualifiers (gf combin method)
195                     (invalid-qualifiers gf combin method)))
196              (declare (ignorable #'%no-primary-method #'%invalid-qualifiers))
197              ,effective-method)))
198         (mc-args-p
199          (let* ((required
200                  ;; FIXME: Ick. Shared idiom, too, with stuff in cache.lisp
201                  (let (req)
202                    (dotimes (i (length metatypes) (nreverse req))
203                      (push (dfun-arg-symbol i) req))))
204                 (gf-args (if applyp
205                              `(list* ,@required .dfun-rest-arg.)
206                              `(list ,@required))))
207            `(lambda ,ll
208              (declare (ignore .pv-cell. .next-method-call.))
209              (let ((.gf-args. ,gf-args))
210                (declare (ignorable .gf-args.))
211                ,@check-applicable-keywords
212                ,effective-method))))
213         (t
214          `(lambda ,ll
215            (declare (ignore ,@(if error-p ll '(.pv-cell. .next-method-call.))))
216            ,@check-applicable-keywords
217            ,effective-method))))))
218
219 (defun expand-emf-call-method (gf form metatypes applyp env)
220   (declare (ignore gf metatypes applyp env))
221   `(call-method ,(cdr form)))
222
223 (defmacro call-method (&rest args)
224   (declare (ignore args))
225   `(error "~S outside of a effective method form" 'call-method))
226
227 (defun make-effective-method-list-fun-type
228     (generic-function form method-alist-p wrappers-p)
229   (if (every (lambda (form)
230                (eq 'fast-method-call
231                    (make-effective-method-fun-type
232                     generic-function form method-alist-p wrappers-p)))
233              (cdr form))
234       'fast-method-call
235       t))
236
237 (defun memf-test-converter (form generic-function method-alist-p wrappers-p)
238   (case (and (consp form) (car form))
239     (call-method
240      (case (make-effective-method-fun-type
241             generic-function form method-alist-p wrappers-p)
242        (fast-method-call '.fast-call-method.)
243        (t '.call-method.)))
244     (call-method-list
245      (case (make-effective-method-list-fun-type
246             generic-function form method-alist-p wrappers-p)
247        (fast-method-call '.fast-call-method-list.)
248        (t '.call-method-list.)))
249     (check-applicable-keywords 'check-applicable-keywords)
250     (t (default-test-converter form))))
251
252 ;;; CMUCL comment (2003-10-15):
253 ;;;
254 ;;;   This function is called via the GET-FUNCTION mechanism on forms
255 ;;;   of an emf lambda.  First value returned replaces FORM in the emf
256 ;;;   lambda.  Second value is a list of variable names that become
257 ;;;   closure variables.
258 (defun memf-code-converter
259     (form generic-function metatypes applyp method-alist-p wrappers-p)
260   (case (and (consp form) (car form))
261     (call-method
262      (let ((gensym (get-effective-method-gensym)))
263        (values (make-emf-call
264                 metatypes applyp gensym
265                 (make-effective-method-fun-type
266                  generic-function form method-alist-p wrappers-p))
267                (list gensym))))
268     (call-method-list
269      (let ((gensym (get-effective-method-gensym))
270            (type (make-effective-method-list-fun-type
271                   generic-function form method-alist-p wrappers-p)))
272        (values `(dolist (emf ,gensym nil)
273                  ,(make-emf-call metatypes applyp 'emf type))
274                (list gensym))))
275     (check-applicable-keywords
276      (values `(check-applicable-keywords
277                .dfun-rest-arg. .keyargs-start. .valid-keys.)
278              '(.keyargs-start. .valid-keys.)))
279     
280     (t
281      (default-code-converter form))))
282
283 (defun memf-constant-converter (form generic-function)
284   (case (and (consp form) (car form))
285     (call-method
286      (list (cons '.meth.
287                  (make-effective-method-function-simple
288                   generic-function form))))
289     (call-method-list
290      (list (cons '.meth-list.
291                  (mapcar (lambda (form)
292                            (make-effective-method-function-simple
293                             generic-function form))
294                          (cdr form)))))
295     (check-applicable-keywords
296      '(.keyargs-start. .valid-keys.))
297     (t
298      (default-constant-converter form))))
299
300 (defvar *applicable-methods*)
301 (defun make-effective-method-function-internal
302     (generic-function effective-method method-alist-p wrappers-p)
303   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
304       (get-generic-fun-info generic-function)
305     (declare (ignore nkeys arg-info))
306     (let* ((*rebound-effective-method-gensyms*
307             *global-effective-method-gensyms*)
308            (name (if (early-gf-p generic-function)
309                      (!early-gf-name generic-function)
310                      (generic-function-name generic-function)))
311            (arg-info (cons nreq applyp))
312            (effective-method-lambda (expand-effective-method-function
313                                      generic-function effective-method)))
314       (multiple-value-bind (cfunction constants)
315           (get-fun1 effective-method-lambda
316                     (lambda (form)
317                       (memf-test-converter form generic-function
318                                            method-alist-p wrappers-p))
319                     (lambda (form)
320                       (memf-code-converter form generic-function
321                                            metatypes applyp
322                                            method-alist-p wrappers-p))
323                     (lambda (form)
324                       (memf-constant-converter form generic-function)))
325         (lambda (method-alist wrappers)
326           (multiple-value-bind (valid-keys keyargs-start)
327               (when (memq '.valid-keys. constants)
328                 (compute-applicable-keywords
329                  generic-function *applicable-methods*))
330             (flet ((compute-constant (constant)
331                      (if (consp constant)
332                          (case (car constant)
333                            (.meth.
334                             (funcall (cdr constant) method-alist wrappers))
335                            (.meth-list.
336                             (mapcar (lambda (fn)
337                                       (funcall fn method-alist wrappers))
338                                     (cdr constant)))
339                            (t constant))
340                          (case constant
341                            (.keyargs-start. keyargs-start)
342                            (.valid-keys. valid-keys)
343                            (t constant)))))
344               (let ((fun (apply cfunction
345                                 (mapcar #'compute-constant constants))))
346                 (set-fun-name fun `(combined-method ,name))
347                 (make-fast-method-call :function fun
348                                        :arg-info arg-info)))))))))
349
350 (defmacro call-method-list (&rest calls)
351   `(progn ,@calls))
352
353 (defun make-call-methods (methods)
354   `(call-method-list
355     ,@(mapcar (lambda (method) `(call-method ,method ())) methods)))
356
357 (defun gf-requires-emf-keyword-checks (generic-function)
358   (member '&key (gf-lambda-list generic-function)))
359
360 (defvar *in-precompute-effective-methods-p* nil)
361
362 (defun standard-compute-effective-method
363     (generic-function combin applicable-methods)
364   (collect ((before) (primary) (after) (around))
365     (flet ((invalid (gf combin m)
366              (if *in-precompute-effective-methods-p*
367                  (return-from standard-compute-effective-method
368                    `(%invalid-qualifiers ',gf ',combin ',m))
369                  (invalid-qualifiers gf combin m))))
370       (dolist (m applicable-methods)
371         (let ((qualifiers (if (listp m)
372                               (early-method-qualifiers m)
373                               (method-qualifiers m))))
374           (cond
375             ((null qualifiers) (primary m))
376             ((cdr qualifiers) (invalid generic-function combin m))
377             ((eq (car qualifiers) :around) (around m))
378             ((eq (car qualifiers) :before) (before m))
379             ((eq (car qualifiers) :after) (after m))
380             (t (invalid generic-function combin m))))))
381     (cond ((null (primary))
382            `(%no-primary-method ',generic-function .args.))
383           ((and (null (before)) (null (after)) (null (around)))
384            ;; By returning a single call-method `form' here we enable
385            ;; an important implementation-specific optimization; that
386            ;; is, we can use the fast method function directly as the
387            ;; effective method function.
388            ;;
389            ;; However, the requirement by ANSI (CLHS 7.6.5) on generic
390            ;; function argument checking inhibits this, as we don't
391            ;; perform this checking in fast-method-functions given
392            ;; that they are not solely used for effective method
393            ;; functions, but also in combination, when they should not
394            ;; perform argument checks.
395            (let ((call-method
396                   `(call-method ,(first (primary)) ,(rest (primary)))))
397              (if (gf-requires-emf-keyword-checks generic-function)
398                  ;; the PROGN inhibits the above optimization
399                  `(progn ,call-method)
400                  call-method)))
401           (t
402            (let ((main-effective-method
403                    (if (or (before) (after))
404                        `(multiple-value-prog1
405                           (progn
406                             ,(make-call-methods (before))
407                             (call-method ,(first (primary))
408                                          ,(rest (primary))))
409                           ,(make-call-methods (reverse (after))))
410                        `(call-method ,(first (primary)) ,(rest (primary))))))
411              (if (around)
412                  `(call-method ,(first (around))
413                                (,@(rest (around))
414                                   (make-method ,main-effective-method)))
415                  main-effective-method))))))
416 \f
417 ;;; helper code for checking keywords in generic function calls.
418 (defun compute-applicable-keywords (gf methods)
419   (let ((any-keyp nil))
420     (flet ((analyze (lambda-list)
421              (multiple-value-bind (nreq nopt keyp restp allowp keys)
422                  (analyze-lambda-list lambda-list)
423                (declare (ignore nreq restp))
424                (when keyp
425                  (setq any-keyp t))
426                (values nopt allowp keys))))
427       (multiple-value-bind (nopt allowp keys)
428           (analyze (generic-function-lambda-list gf))
429         (dolist (method methods)
430           (let ((ll (if (consp method)
431                         (early-method-lambda-list method)
432                         (method-lambda-list method))))
433             (multiple-value-bind (n allowp method-keys)
434                 (analyze ll)
435               (declare (ignore n))
436               (when allowp
437                 (return-from compute-applicable-keywords (values t nopt)))
438               (setq keys (union method-keys keys)))))
439         (aver any-keyp)
440         (values (if allowp t keys) nopt)))))
441
442 (defun check-applicable-keywords (args start valid-keys)
443   (let ((allow-other-keys-seen nil)
444         (allow-other-keys nil)
445         (args (nthcdr start args)))
446     (collect ((invalid))
447       (loop
448        (when (null args)
449          (when (and (invalid) (not allow-other-keys))
450            (error 'simple-program-error
451                   :format-control "~@<invalid keyword argument~P: ~
452                                    ~{~S~^, ~} (valid keys are ~{~S~^, ~}).~@:>"
453                   :format-arguments (list (length (invalid)) (invalid) valid-keys)))
454          (return))
455        (let ((key (pop args)))
456          (cond
457            ((not (symbolp key))
458             (error 'simple-program-error
459                    :format-control "~@<keyword argument not a symbol: ~S.~@:>"
460                    :format-arguments (list key)))
461            ((null args) (sb-c::%odd-key-args-error))
462            ((eq key :allow-other-keys)
463             ;; only the leftmost :ALLOW-OTHER-KEYS has any effect
464             (unless allow-other-keys-seen
465               (setq allow-other-keys-seen t
466                     allow-other-keys (car args))))
467            ((eq t valid-keys))
468            ((not (memq key valid-keys)) (invalid key))))
469        (pop args)))))
470 \f
471 ;;;; the STANDARD method combination type. This is coded by hand
472 ;;;; (rather than with DEFINE-METHOD-COMBINATION) for bootstrapping
473 ;;;; and efficiency reasons. Note that the definition of the
474 ;;;; FIND-METHOD-COMBINATION-METHOD appears in the file
475 ;;;; defcombin.lisp. This is because EQL methods can't appear in the
476 ;;;; bootstrap.
477 ;;;;
478 ;;;; The DEFCLASS for the METHOD-COMBINATION and
479 ;;;; STANDARD-METHOD-COMBINATION classes has to appear here for this
480 ;;;; reason. This code must conform to the code in the file
481 ;;;; defcombin.lisp, look there for more details.
482
483 (defun compute-effective-method (generic-function combin applicable-methods)
484   (standard-compute-effective-method generic-function
485                                      combin
486                                      applicable-methods))
487
488 (defun invalid-method-error (method format-control &rest format-arguments)
489   (let ((sb-debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
490     (error "~@<invalid method error for ~2I~_~S ~I~_method: ~2I~_~?~:>"
491            method
492            format-control
493            format-arguments)))
494
495 (defun method-combination-error (format-control &rest format-arguments)
496   (let ((sb-debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
497     (error "~@<method combination error in CLOS dispatch: ~2I~_~?~:>"
498            format-control
499            format-arguments)))