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