cfce81ddea4eb0b0488f907ed9ab5aae0a4f0c79
[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 (safe-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 (safe-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             (flet ((frob-cm-arg (arg)
139                      (if (if (listp arg)
140                              (eq (car arg) :early-method)
141                              (method-p arg))
142                          arg
143                          (if (and (consp arg) (eq (car arg) 'make-method))
144                              (make-instance 'standard-method
145                                             :specializers nil ; XXX
146                                             :qualifiers nil
147                                             :fast-function (fast-method-call-function
148                                                             (make-effective-method-function
149                                                              gf (cadr arg) method-alist wrappers)))
150                              arg))))
151               (make-method-call :function mf
152                                 ;; FIXME: this is wrong.  Very wrong.
153                                 ;; It assumes that the only place that
154                                 ;; can have make-method calls is in
155                                 ;; the list structure of the second
156                                 ;; argument to CALL-METHOD, but AMOP
157                                 ;; says that CALL-METHOD can be more
158                                 ;; complicated if
159                                 ;; COMPUTE-EFFECTIVE-METHOD (and
160                                 ;; presumably MAKE-METHOD-LAMBDA) is
161                                 ;; adjusted to match.
162                                 ;;
163                                 ;; On the other hand, it's a start,
164                                 ;; because without this calls to
165                                 ;; MAKE-METHOD in method combination
166                                 ;; where one of the methods is of a
167                                 ;; user-defined class don't work at
168                                 ;; all.  -- CSR, 2006-08-05
169                                 :call-method-args (cons (mapcar #'frob-cm-arg (car cm-args))
170                                                         (cdr cm-args))))
171             mf))))
172
173 (defun make-effective-method-function-simple1
174     (gf method cm-args fmf-p &optional method-alist wrappers)
175   (when method
176     (if (if (listp method)
177             (eq (car method) :early-method)
178             (method-p method))
179         (make-emf-from-method method cm-args gf fmf-p method-alist wrappers)
180         (if (and (consp method) (eq (car method) 'make-method))
181             (make-effective-method-function gf
182                                             (cadr method)
183                                             method-alist wrappers)
184             method))))
185
186 (defvar *global-effective-method-gensyms* ())
187 (defvar *rebound-effective-method-gensyms*)
188
189 (defun get-effective-method-gensym ()
190   (or (pop *rebound-effective-method-gensyms*)
191       (let ((new (format-symbol *pcl-package*
192                                 "EFFECTIVE-METHOD-GENSYM-~D"
193                                 (length *global-effective-method-gensyms*))))
194         (setq *global-effective-method-gensyms*
195               (append *global-effective-method-gensyms* (list new)))
196         new)))
197
198 (let ((*rebound-effective-method-gensyms* ()))
199   (dotimes-fixnum (i 10) (get-effective-method-gensym)))
200
201 (defun expand-effective-method-function (gf effective-method &optional env)
202   (declare (ignore env))
203   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
204       (get-generic-fun-info gf)
205     (declare (ignore nreq nkeys arg-info))
206     (let ((ll (make-fast-method-call-lambda-list metatypes applyp))
207           (check-applicable-keywords
208            (when (and applyp (gf-requires-emf-keyword-checks gf))
209              '((check-applicable-keywords))))
210           (error-p (or (eq (first effective-method) '%no-primary-method)
211                        (eq (first effective-method) '%invalid-qualifiers)))
212           (mc-args-p
213            (when (eq *boot-state* 'complete)
214              ;; Otherwise the METHOD-COMBINATION slot is not bound.
215              (let ((combin (generic-function-method-combination gf)))
216                (and (long-method-combination-p combin)
217                     (long-method-combination-args-lambda-list combin))))))
218       (cond
219         (error-p
220          `(lambda (.pv-cell. .next-method-call. &rest .args.)
221            (declare (ignore .pv-cell. .next-method-call.))
222            (declare (ignorable .args.))
223            (flet ((%no-primary-method (gf args)
224                     (apply #'no-primary-method gf args))
225                   (%invalid-qualifiers (gf combin method)
226                     (invalid-qualifiers gf combin method)))
227              (declare (ignorable #'%no-primary-method #'%invalid-qualifiers))
228              ,effective-method)))
229         (mc-args-p
230          (let* ((required
231                  ;; FIXME: Ick. Shared idiom, too, with stuff in cache.lisp
232                  (let (req)
233                    (dotimes (i (length metatypes) (nreverse req))
234                      (push (dfun-arg-symbol i) req))))
235                 (gf-args (if applyp
236                              `(list* ,@required .dfun-rest-arg.)
237                              `(list ,@required))))
238            `(lambda ,ll
239              (declare (ignore .pv-cell. .next-method-call.))
240              (let ((.gf-args. ,gf-args))
241                (declare (ignorable .gf-args.))
242                ,@check-applicable-keywords
243                ,effective-method))))
244         (t
245          `(lambda ,ll
246            (declare (ignore ,@(if error-p ll '(.pv-cell. .next-method-call.))))
247            ,@check-applicable-keywords
248            ,effective-method))))))
249
250 (defun expand-emf-call-method (gf form metatypes applyp env)
251   (declare (ignore gf metatypes applyp env))
252   `(call-method ,(cdr form)))
253
254 (defmacro call-method (&rest args)
255   (declare (ignore args))
256   ;; the PROGN is here to defend against premature macroexpansion by
257   ;; RESTART-CASE.
258   `(progn (error "~S outside of a effective method form" 'call-method)))
259
260 (defun make-effective-method-list-fun-type
261     (generic-function form method-alist-p wrappers-p)
262   (if (every (lambda (form)
263                (eq 'fast-method-call
264                    (make-effective-method-fun-type
265                     generic-function form method-alist-p wrappers-p)))
266              (cdr form))
267       'fast-method-call
268       t))
269
270 (defun memf-test-converter (form generic-function method-alist-p wrappers-p)
271   (case (and (consp form) (car form))
272     (call-method
273      (case (make-effective-method-fun-type
274             generic-function form method-alist-p wrappers-p)
275        (fast-method-call '.fast-call-method.)
276        (t '.call-method.)))
277     (call-method-list
278      (case (make-effective-method-list-fun-type
279             generic-function form method-alist-p wrappers-p)
280        (fast-method-call '.fast-call-method-list.)
281        (t '.call-method-list.)))
282     (check-applicable-keywords 'check-applicable-keywords)
283     (t (default-test-converter form))))
284
285 ;;; CMUCL comment (2003-10-15):
286 ;;;
287 ;;;   This function is called via the GET-FUNCTION mechanism on forms
288 ;;;   of an emf lambda.  First value returned replaces FORM in the emf
289 ;;;   lambda.  Second value is a list of variable names that become
290 ;;;   closure variables.
291 (defun memf-code-converter
292     (form generic-function metatypes applyp method-alist-p wrappers-p)
293   (case (and (consp form) (car form))
294     (call-method
295      (let ((gensym (get-effective-method-gensym)))
296        (values (make-emf-call
297                 metatypes applyp gensym
298                 (make-effective-method-fun-type
299                  generic-function form method-alist-p wrappers-p))
300                (list gensym))))
301     (call-method-list
302      (let ((gensym (get-effective-method-gensym))
303            (type (make-effective-method-list-fun-type
304                   generic-function form method-alist-p wrappers-p)))
305        (values `(dolist (emf ,gensym nil)
306                  ,(make-emf-call metatypes applyp 'emf type))
307                (list gensym))))
308     (check-applicable-keywords
309      (values `(check-applicable-keywords
310                .dfun-rest-arg. .keyargs-start. .valid-keys.)
311              '(.keyargs-start. .valid-keys.)))
312
313     (t
314      (default-code-converter form))))
315
316 (defun memf-constant-converter (form generic-function)
317   (case (and (consp form) (car form))
318     (call-method
319      (list (cons '.meth.
320                  (make-effective-method-function-simple
321                   generic-function form))))
322     (call-method-list
323      (list (cons '.meth-list.
324                  (mapcar (lambda (form)
325                            (make-effective-method-function-simple
326                             generic-function form))
327                          (cdr form)))))
328     (check-applicable-keywords
329      '(.keyargs-start. .valid-keys.))
330     (t
331      (default-constant-converter form))))
332
333 (defvar *applicable-methods*)
334 (defun make-effective-method-function-internal
335     (generic-function effective-method method-alist-p wrappers-p)
336   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
337       (get-generic-fun-info generic-function)
338     (declare (ignore nkeys arg-info))
339     (let* ((*rebound-effective-method-gensyms*
340             *global-effective-method-gensyms*)
341            (name (if (early-gf-p generic-function)
342                      (!early-gf-name generic-function)
343                      (generic-function-name generic-function)))
344            (arg-info (cons nreq applyp))
345            (effective-method-lambda (expand-effective-method-function
346                                      generic-function effective-method)))
347       (multiple-value-bind (cfunction constants)
348           (get-fun1 effective-method-lambda
349                     (lambda (form)
350                       (memf-test-converter form generic-function
351                                            method-alist-p wrappers-p))
352                     (lambda (form)
353                       (memf-code-converter form generic-function
354                                            metatypes applyp
355                                            method-alist-p wrappers-p))
356                     (lambda (form)
357                       (memf-constant-converter form generic-function)))
358         (lambda (method-alist wrappers)
359           (multiple-value-bind (valid-keys keyargs-start)
360               (when (memq '.valid-keys. constants)
361                 (compute-applicable-keywords
362                  generic-function *applicable-methods*))
363             (flet ((compute-constant (constant)
364                      (if (consp constant)
365                          (case (car constant)
366                            (.meth.
367                             (funcall (cdr constant) method-alist wrappers))
368                            (.meth-list.
369                             (mapcar (lambda (fn)
370                                       (funcall fn method-alist wrappers))
371                                     (cdr constant)))
372                            (t constant))
373                          (case constant
374                            (.keyargs-start. keyargs-start)
375                            (.valid-keys. valid-keys)
376                            (t constant)))))
377               (let ((fun (apply cfunction
378                                 (mapcar #'compute-constant constants))))
379                 (set-fun-name fun `(combined-method ,name))
380                 (make-fast-method-call :function fun
381                                        :arg-info arg-info)))))))))
382
383 (defmacro call-method-list (&rest calls)
384   `(progn ,@calls))
385
386 (defun make-call-methods (methods)
387   `(call-method-list
388     ,@(mapcar (lambda (method) `(call-method ,method ())) methods)))
389
390 (defun gf-requires-emf-keyword-checks (generic-function)
391   (member '&key (gf-lambda-list generic-function)))
392
393 (defvar *in-precompute-effective-methods-p* nil)
394
395 (defun standard-compute-effective-method
396     (generic-function combin applicable-methods)
397   (collect ((before) (primary) (after) (around))
398     (flet ((invalid (gf combin m)
399              (if *in-precompute-effective-methods-p*
400                  (return-from standard-compute-effective-method
401                    `(%invalid-qualifiers ',gf ',combin ',m))
402                  (invalid-qualifiers gf combin m))))
403       (dolist (m applicable-methods)
404         (let ((qualifiers (if (listp m)
405                               (early-method-qualifiers m)
406                               (method-qualifiers m))))
407           (cond
408             ((null qualifiers) (primary m))
409             ((cdr qualifiers) (invalid generic-function combin m))
410             ((eq (car qualifiers) :around) (around m))
411             ((eq (car qualifiers) :before) (before m))
412             ((eq (car qualifiers) :after) (after m))
413             (t (invalid generic-function combin m))))))
414     (cond ((null (primary))
415            `(%no-primary-method ',generic-function .args.))
416           ((and (null (before)) (null (after)) (null (around)))
417            ;; By returning a single call-method `form' here we enable
418            ;; an important implementation-specific optimization; that
419            ;; is, we can use the fast method function directly as the
420            ;; effective method function.
421            ;;
422            ;; However, the requirement by ANSI (CLHS 7.6.5) on generic
423            ;; function argument checking inhibits this, as we don't
424            ;; perform this checking in fast-method-functions given
425            ;; that they are not solely used for effective method
426            ;; functions, but also in combination, when they should not
427            ;; perform argument checks.
428            (let ((call-method
429                   `(call-method ,(first (primary)) ,(rest (primary)))))
430              (if (gf-requires-emf-keyword-checks generic-function)
431                  ;; the PROGN inhibits the above optimization
432                  `(progn ,call-method)
433                  call-method)))
434           (t
435            (let ((main-effective-method
436                    (if (or (before) (after))
437                        `(multiple-value-prog1
438                           (progn
439                             ,(make-call-methods (before))
440                             (call-method ,(first (primary))
441                                          ,(rest (primary))))
442                           ,(make-call-methods (reverse (after))))
443                        `(call-method ,(first (primary)) ,(rest (primary))))))
444              (if (around)
445                  `(call-method ,(first (around))
446                                (,@(rest (around))
447                                   (make-method ,main-effective-method)))
448                  main-effective-method))))))
449 \f
450 ;;; helper code for checking keywords in generic function calls.
451 (defun compute-applicable-keywords (gf methods)
452   (let ((any-keyp nil))
453     (flet ((analyze (lambda-list)
454              (multiple-value-bind (nreq nopt keyp restp allowp keys)
455                  (analyze-lambda-list lambda-list)
456                (declare (ignore nreq restp))
457                (when keyp
458                  (setq any-keyp t))
459                (values nopt allowp keys))))
460       (multiple-value-bind (nopt allowp keys)
461           (analyze (generic-function-lambda-list gf))
462         (dolist (method methods)
463           (let ((ll (if (consp method)
464                         (early-method-lambda-list method)
465                         (method-lambda-list method))))
466             (multiple-value-bind (n allowp method-keys)
467                 (analyze ll)
468               (declare (ignore n))
469               (when allowp
470                 (return-from compute-applicable-keywords (values t nopt)))
471               (setq keys (union method-keys keys)))))
472         (aver any-keyp)
473         (values (if allowp t keys) nopt)))))
474
475 (defun check-applicable-keywords (args start valid-keys)
476   (let ((allow-other-keys-seen nil)
477         (allow-other-keys nil)
478         (args (nthcdr start args)))
479     (collect ((invalid))
480       (loop
481        (when (null args)
482          (when (and (invalid) (not allow-other-keys))
483            (error 'simple-program-error
484                   :format-control "~@<invalid keyword argument~P: ~
485                                    ~{~S~^, ~} (valid keys are ~{~S~^, ~}).~@:>"
486                   :format-arguments (list (length (invalid)) (invalid) valid-keys)))
487          (return))
488        (let ((key (pop args)))
489          (cond
490            ((not (symbolp key))
491             (error 'simple-program-error
492                    :format-control "~@<keyword argument not a symbol: ~S.~@:>"
493                    :format-arguments (list key)))
494            ((null args) (sb-c::%odd-key-args-error))
495            ((eq key :allow-other-keys)
496             ;; only the leftmost :ALLOW-OTHER-KEYS has any effect
497             (unless allow-other-keys-seen
498               (setq allow-other-keys-seen t
499                     allow-other-keys (car args))))
500            ((eq t valid-keys))
501            ((not (memq key valid-keys)) (invalid key))))
502        (pop args)))))
503 \f
504 ;;;; the STANDARD method combination type. This is coded by hand
505 ;;;; (rather than with DEFINE-METHOD-COMBINATION) for bootstrapping
506 ;;;; and efficiency reasons. Note that the definition of the
507 ;;;; FIND-METHOD-COMBINATION-METHOD appears in the file
508 ;;;; defcombin.lisp. This is because EQL methods can't appear in the
509 ;;;; bootstrap.
510 ;;;;
511 ;;;; The DEFCLASS for the METHOD-COMBINATION and
512 ;;;; STANDARD-METHOD-COMBINATION classes has to appear here for this
513 ;;;; reason. This code must conform to the code in the file
514 ;;;; defcombin.lisp, look there for more details.
515
516 (defun compute-effective-method (generic-function combin applicable-methods)
517   (standard-compute-effective-method generic-function
518                                      combin
519                                      applicable-methods))
520
521 (defun invalid-method-error (method format-control &rest format-arguments)
522   (let ((sb-debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
523     (error "~@<invalid method error for ~2I~_~S ~I~_method: ~2I~_~?~:>"
524            method
525            format-control
526            format-arguments)))
527
528 (defun method-combination-error (format-control &rest format-arguments)
529   (let ((sb-debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
530     (error "~@<method combination error in CLOS dispatch: ~2I~_~?~:>"
531            format-control
532            format-arguments)))