0.8.3.39:
[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           (error-p (or (eq (first effective-method) '%no-primary-method)
178                        (eq (first effective-method) '%invalid-qualifiers)))
179           (mc-args-p
180            (when (eq *boot-state* 'complete)
181              ;; Otherwise the METHOD-COMBINATION slot is not bound.
182              (let ((combin (generic-function-method-combination gf)))
183                (and (long-method-combination-p combin)
184                     (long-method-combination-args-lambda-list combin))))))
185       (cond
186         (error-p
187          `(lambda (.pv-cell. .next-method-call. &rest .args.)
188            (declare (ignore .pv-cell. .next-method-call.))
189            (declare (ignorable .args.))
190            (flet ((%no-primary-method (gf args)
191                     (apply #'no-primary-method gf args))
192                   (%invalid-qualifiers (gf combin method)
193                     (invalid-qualifiers gf combin method)))
194              (declare (ignorable #'%no-primary-method #'%invalid-qualifiers))
195              ,effective-method)))
196         (mc-args-p
197          (let* ((required
198                  ;; FIXME: Ick. Shared idiom, too, with stuff in cache.lisp
199                  (let (req)
200                    (dotimes (i (length metatypes) (nreverse req))
201                      (push (dfun-arg-symbol i) req))))
202                 (gf-args (if applyp
203                              `(list* ,@required .dfun-rest-arg.)
204                              `(list ,@required))))
205            `(lambda ,ll
206              (declare (ignore .pv-cell. .next-method-call.))
207              (let ((.gf-args. ,gf-args))
208                (declare (ignorable .gf-args.))
209                ,effective-method))))
210         (t
211          `(lambda ,ll
212            (declare (ignore ,@(if error-p ll '(.pv-cell. .next-method-call.))))
213            ,effective-method))))))
214
215 (defun expand-emf-call-method (gf form metatypes applyp env)
216   (declare (ignore gf metatypes applyp env))
217   `(call-method ,(cdr form)))
218
219 (defmacro call-method (&rest args)
220   (declare (ignore args))
221   `(error "~S outside of a effective method form" 'call-method))
222
223 (defun memf-test-converter (form generic-function method-alist-p wrappers-p)
224   (cond ((and (consp form) (eq (car form) 'call-method))
225          (case (make-effective-method-fun-type
226                 generic-function form method-alist-p wrappers-p)
227            (fast-method-call
228             '.fast-call-method.)
229            (t
230             '.call-method.)))
231         ((and (consp form) (eq (car form) 'call-method-list))
232          (case (if (every (lambda (form)
233                             (eq 'fast-method-call
234                                 (make-effective-method-fun-type
235                                  generic-function form
236                                  method-alist-p wrappers-p)))
237                           (cdr form))
238                    'fast-method-call
239                    t)
240            (fast-method-call
241             '.fast-call-method-list.)
242            (t
243             '.call-method-list.)))
244         (t
245          (default-test-converter form))))
246
247 (defun memf-code-converter
248     (form generic-function metatypes applyp method-alist-p wrappers-p)
249   (cond ((and (consp form) (eq (car form) 'call-method))
250          (let ((gensym (get-effective-method-gensym)))
251            (values (make-emf-call metatypes applyp gensym
252                                   (make-effective-method-fun-type
253                                    generic-function form method-alist-p wrappers-p))
254                    (list gensym))))
255         ((and (consp form) (eq (car form) 'call-method-list))
256          (let ((gensym (get-effective-method-gensym))
257                (type (if (every (lambda (form)
258                                   (eq 'fast-method-call
259                                       (make-effective-method-fun-type
260                                        generic-function form
261                                        method-alist-p wrappers-p)))
262                                 (cdr form))
263                          'fast-method-call
264                          t)))
265            (values `(dolist (emf ,gensym nil)
266                       ,(make-emf-call metatypes applyp 'emf type))
267                    (list gensym))))
268         (t
269          (default-code-converter form))))
270
271 (defun memf-constant-converter (form generic-function)
272   (cond ((and (consp form) (eq (car form) 'call-method))
273          (list (cons '.meth.
274                      (make-effective-method-function-simple
275                       generic-function form))))
276         ((and (consp form) (eq (car form) 'call-method-list))
277          (list (cons '.meth-list.
278                      (mapcar (lambda (form)
279                                (make-effective-method-function-simple
280                                 generic-function form))
281                              (cdr form)))))
282         (t
283          (default-constant-converter form))))
284
285 (defun make-effective-method-function-internal
286     (generic-function effective-method method-alist-p wrappers-p)
287   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
288       (get-generic-fun-info generic-function)
289     (declare (ignore nkeys arg-info))
290     (let* ((*rebound-effective-method-gensyms*
291             *global-effective-method-gensyms*)
292            (name (if (early-gf-p generic-function)
293                      (!early-gf-name generic-function)
294                      (generic-function-name generic-function)))
295            (arg-info (cons nreq applyp))
296            (effective-method-lambda (expand-effective-method-function
297                                      generic-function effective-method)))
298       (multiple-value-bind (cfunction constants)
299           (get-fun1 effective-method-lambda
300                     (lambda (form)
301                       (memf-test-converter form generic-function
302                                            method-alist-p wrappers-p))
303                     (lambda (form)
304                       (memf-code-converter form generic-function
305                                            metatypes applyp
306                                            method-alist-p wrappers-p))
307                     (lambda (form)
308                       (memf-constant-converter form generic-function)))
309         (lambda (method-alist wrappers)
310           (let* ((constants
311                   (mapcar (lambda (constant)
312                             (if (consp constant)
313                                 (case (car constant)
314                                   (.meth.
315                                    (funcall (cdr constant)
316                                             method-alist wrappers))
317                                   (.meth-list.
318                                    (mapcar (lambda (fn)
319                                              (funcall fn
320                                                       method-alist
321                                                       wrappers))
322                                            (cdr constant)))
323                                   (t constant))
324                                 constant))
325                           constants))
326                  (function (set-fun-name
327                             (apply cfunction constants)
328                             `(combined-method ,name))))
329             (make-fast-method-call :function function
330                                    :arg-info arg-info)))))))
331
332 (defmacro call-method-list (&rest calls)
333   `(progn ,@calls))
334
335 (defun make-call-methods (methods)
336   `(call-method-list
337     ,@(mapcar (lambda (method) `(call-method ,method ())) methods)))
338
339 (defun standard-compute-effective-method
340     (generic-function combin applicable-methods)
341   (collect ((before) (primary) (after) (around))
342     (flet ((invalid (gf combin m)
343              (if *in-precompute-effective-methods-p*
344                  (return-from standard-compute-effective-method
345                    `(%invalid-qualifiers ',gf ',combin ',m))
346                  (invalid-qualifiers gf combin m))))
347       (dolist (m applicable-methods)
348         (let ((qualifiers (if (listp m)
349                               (early-method-qualifiers m)
350                               (method-qualifiers m))))
351           (cond
352             ((null qualifiers) (primary m))
353             ((cdr qualifiers) (invalid generic-function combin m))
354             ((eq (car qualifiers) :around) (around m))
355             ((eq (car qualifiers) :before) (before m))
356             ((eq (car qualifiers) :after) (after m))
357             (t (invalid generic-function combin m))))))
358     (cond ((null (primary))
359            `(%no-primary-method ',generic-function .args.))
360           ((and (null (before)) (null (after)) (null (around)))
361            ;; By returning a single call-method `form' here we enable
362            ;; an important implementation-specific optimization.
363            `(call-method ,(first (primary)) ,(rest (primary))))
364           (t
365            (let ((main-effective-method
366                    (if (or (before) (after))
367                        `(multiple-value-prog1
368                           (progn
369                             ,(make-call-methods (before))
370                             (call-method ,(first (primary))
371                                          ,(rest (primary))))
372                           ,(make-call-methods (reverse (after))))
373                        `(call-method ,(first (primary)) ,(rest (primary))))))
374              (if (around)
375                  `(call-method ,(first (around))
376                                (,@(rest (around))
377                                   (make-method ,main-effective-method)))
378                  main-effective-method))))))
379 \f
380 ;;;; the STANDARD method combination type. This is coded by hand
381 ;;;; (rather than with DEFINE-METHOD-COMBINATION) for bootstrapping
382 ;;;; and efficiency reasons. Note that the definition of the
383 ;;;; FIND-METHOD-COMBINATION-METHOD appears in the file
384 ;;;; defcombin.lisp. This is because EQL methods can't appear in the
385 ;;;; bootstrap.
386 ;;;;
387 ;;;; The DEFCLASS for the METHOD-COMBINATION and
388 ;;;; STANDARD-METHOD-COMBINATION classes has to appear here for this
389 ;;;; reason. This code must conform to the code in the file
390 ;;;; defcombin.lisp, look there for more details.
391
392 (defun compute-effective-method (generic-function combin applicable-methods)
393   (standard-compute-effective-method generic-function
394                                      combin
395                                      applicable-methods))
396
397 (defun invalid-method-error (method format-control &rest format-arguments)
398   (let ((sb-debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
399     (error "~@<invalid method error for ~2I~_~S ~I~_method: ~2I~_~?~:>"
400            method
401            format-control
402            format-arguments)))
403
404 (defun method-combination-error (format-control &rest format-arguments)
405   (let ((sb-debug:*stack-top-hint* (nth-value 1 (find-caller-name-and-frame))))
406     (error "~@<method combination error in CLOS dispatch: ~2I~_~?~:>"
407            format-control
408            format-arguments)))