Debian Codegraph @

src/clos/combin.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: CLOS -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1992, Giuseppe Attardi.
6 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 (in-package "CLOS")
11
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 ;;; COMPILING EFFECTIVE METHODS
14 ;;;
15 ;;; The following functions take care of transforming the forms
16 ;;; produced by the method combinations into effective methods. In ECL
17 ;;; effective methods are nothing but directly callable functions.
18 ;;; Ideally, this compilation should just produce new compiled
19 ;;; functions. However, we do not want to cons a lot of functions, and
20 ;;; therefore we use closures.
21 ;;;
22 ;;; Formerly we used to keep a list of precompiled effective methods
23 ;;; and made a structural comparison between the current method and
24 ;;; the precompiled ones, so as to save memory. This only causes
25 ;;; improvements in declarative combinations. For standard combinations
26 ;;; it should be enough with a couple of different closures and hence
27 ;;; the structural comparison is a loss of time.
28 ;;;
29 ;;; This is the core routine. It produces effective methods (i.e.
30 ;;; functions) out of the forms generated by the method combinators.
31 ;;; We consider the following cases:
32 ;;; 1) Ordinary methods. The function of the method is extracted.
33 ;;; 2) Functions. They map to themselves. This only happens
34 ;;; when these functions have been generated by previous calls
35 ;;; to EFFECTIVE-METHOD-FUNCTION.
36 ;;; 3) (CALL-METHOD method rest-methods) A closure is
37 ;;; generated that invokes the current method while informing
38 ;;; it about the remaining methods.
39 ;;; 4) (MAKE-METHOD form) A function is created that takes the
40 ;;; list of arguments of the generic function and evaluates
41 ;;; the forms in a null environment. This is the only form
42 ;;; that may lead to consing of new bytecodes objects. Nested
43 ;;; CALL-METHOD are handled via the global macro CALL-METHOD.
44 ;;; 5) Ordinary forms are turned into lambda forms, much like
45 ;;; what happens with the content of MAKE-METHOD.
46 ;;;
47 (defun effective-method-function (form &optional top-level &aux first)
48 (cond ((functionp form)
49 form)
50 ((method-p form)
51 (method-function form))
52 ((atom form)
53 (error "Malformed effective method form:~%~A" form))
54 ((eq (setf first (first form)) 'MAKE-METHOD)
55 (coerce `(lambda (.combined-method-args. *next-methods*)
56 (declare (special .combined-method-args. *next-methods*))
57 ,(second form))
58 'function))
59 ((eq first 'CALL-METHOD)
60 (combine-method-functions
61 (effective-method-function (second form))
62 (mapcar #'effective-method-function (third form))))
63 (top-level
64 (coerce `(lambda (.combined-method-args. no-next-methods)
65 (declare (ignorable no-next-methods))
66 ,form)
67 'function))
68 (t
69 (error "Malformed effective method form:~%~A" form))))
70
71 ;;;
72 ;;; This function is a combinator of effective methods. It creates a
73 ;;; closure that invokes the first method while passing the information
74 ;;; of the remaining methods. The resulting closure (or effective method)
75 ;;; is the equivalent of (CALL-METHOD method rest-methods)
76 ;;;
77 (defun combine-method-functions (method rest-methods)
78 (declare (si::c-local))
79 #'(lambda (.combined-method-args. no-next-methods)
80 (declare (ignorable no-next-methods))
81 (funcall method .combined-method-args. rest-methods)))
82
83 (defmacro call-method (method &optional rest-methods)
84 `(funcall ,(effective-method-function method)
85 .combined-method-args.
86 ',(and rest-methods (mapcar #'effective-method-function rest-methods))))
87
88 (defun call-next-method (&rest args)
89 (declare (special .combined-method-args. *next-methods*))
90 (unless *next-methods*
91 (error "No next method."))
92 (funcall (car *next-methods*) (or args .combined-method-args.) (rest *next-methods*)))
93
94 (defun next-method-p ()
95 (declare (special *next-methods*))
96 *next-methods*)
97
98 (define-compiler-macro call-next-method (&rest args)
99 `(if *next-methods*
100 (funcall (car *next-methods*)
101 ,(if args `(list ,@args) '.combined-method-args.)
102 (rest *next-methods*))
103 (error "No next method.")))
104
105 (define-compiler-macro next-method-p ()
106 'clos::*next-methods*)
107
108 (defun error-qualifier (m qualifier)
109 (declare (si::c-local))
110 (error "Standard method combination allows only one qualifier ~
111 per method, either :BEFORE, :AFTER, or :AROUND; while ~
112 a method with ~S was found."
113 m qualifier))
114
115 (defun standard-main-effective-method (before primary after)
116 (declare (si::c-local))
117 #'(lambda (.combined-method-args. no-next-method)
118 (declare (ignorable no-next-method))
119 (dolist (i before)
120 (funcall i .combined-method-args. nil))
121 (if after
122 (multiple-value-prog1
123 (funcall (first primary) .combined-method-args. (rest primary))
124 (dolist (i after)
125 (funcall i .combined-method-args. nil)))
126 (funcall (first primary) .combined-method-args. (rest primary)))))
127
128 (defun standard-compute-effective-method (gf methods)
129 (with-early-accessors (+standard-method-slots+)
130 (let* ((before ())
131 (primary ())
132 (after ())
133 (around ()))
134 (dolist (m methods)
135 (let* ((qualifiers (method-qualifiers m))
136 (f (method-function m)))
137 (cond ((null qualifiers) (push f primary))
138 ((rest qualifiers) (error-qualifier m qualifiers))
139 ((eq (setq qualifiers (first qualifiers)) :BEFORE)
140 (push f before))
141 ((eq qualifiers :AFTER) (push f after))
142 ((eq qualifiers :AROUND) (push f around))
143 (t (error-qualifier m qualifiers)))))
144 ;; When there are no primary methods, an error is to be signaled,
145 ;; and we need not care about :AROUND, :AFTER or :BEFORE methods.
146 (when (null primary)
147 (return-from standard-compute-effective-method
148 #'(lambda (&rest args)
149 (apply 'no-primary-method gf args))))
150 ;; PRIMARY, BEFORE and AROUND are reversed because they have to
151 ;; be on most-specific-first order (ANSI 7.6.6.2), while AFTER
152 ;; may remain as it is because it is least-specific-order.
153 (setf primary (nreverse primary)
154 before (nreverse before))
155 (if around
156 (let ((main (if (or before after)
157 (list
158 (standard-main-effective-method before primary after))
159 primary)))
160 (setf around (nreverse around))
161 (combine-method-functions (first around)
162 (nconc (rest around) main)))
163 (if (or before after)
164 (standard-main-effective-method before primary after)
165 (combine-method-functions (first primary) (rest primary)))) )))
166
167 ;;; ----------------------------------------------------------------------
168 ;;; COMPUTE-EFFECTIVE-METHOD
169 ;;;
170
171 (eval-when (:compile-toplevel)
172 (let* ((class (find-class 'method-combination)))
173 (define-compiler-macro method-combination-compiler (o)
174 `(si::instance-ref
175 ,o ,(slot-definition-location (gethash 'compiler (slot-table class)))))
176 (define-compiler-macro method-combination-options (o)
177 `(si::instance-ref
178 ,o ,(slot-definition-location (gethash 'options (slot-table class)))))))
179
180 (defun std-compute-effective-method (gf method-combination applicable-methods)
181 (declare (type method-combination method-combination)
182 (type generic-function gf)
183 (optimize speed (safety 0)))
184 (with-early-accessors (+method-combination-slots+)
185 (let* ((compiler (method-combination-compiler method-combination))
186 (options (method-combination-options method-combination)))
187 (if options
188 (apply compiler gf applicable-methods options)
189 (funcall compiler gf applicable-methods)))))
190
191 (defun compute-effective-method-function (gf method-combination applicable-methods)
192 ;; Cannot be inlined because it will be a method
193 (declare (notinline compute-effective-method))
194 (let ((form (compute-effective-method gf method-combination applicable-methods)))
195 (let ((aux form) f)
196 (if (and (listp aux)
197 (eq (pop aux) 'funcall)
198 (functionp (setf f (pop aux)))
199 (eq (pop aux) '.combined-method-args.)
200 (eq (pop aux) '*next-methods*))
201 f
202 (effective-method-function form t)))))
203
204 (defun compute-effective-method (gf method-combination applicable-methods)
205 `(funcall ,(std-compute-effective-method gf method-combination applicable-methods)
206 .combined-method-args. *next-methods*))