Debian Codegraph @

src/clos/defcombin.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 ;;;; Copyright (c) 2023, Daniel Kochmański
8 ;;;; Copyright (c) 2023, Marius Gerbershagen
9 ;;;;
10 ;;;; See file '../Copyright' for full details.
11 ;;;;
12
13 (in-package "CLOS")
14
15 ;; ----------------------------------------------------------------------
16 ;; DEFINE-METHOD-COMBINATION
17 ;;
18 ;; METHOD-COMBINATION objects are just a list
19 ;; (name arg*)
20 ;; where NAME is the name of the method combination type defined with
21 ;; DEFINE-METHOD-COMBINATION, and ARG* is zero or more arguments.
22 ;;
23 ;; For each method combination type there is an associated function,
24 ;; and the list of all known method combination types is kept in
25 ;; *METHOD-COMBINATIONS* in the form of property list:
26 ;; (mc-type-name1 function1 mc-type-name2 function2 ....)
27 ;;
28 ;; FUNCTIONn is the function associated to a method combination. It
29 ;; is of type (FUNCTION (generic-function method-list) FUNCTION),
30 ;; and it outputs an anonymous function which is the effective method.
31 ;;
32
33 #+threads
34 (defparameter *method-combinations-lock* (mp:make-lock :name 'find-method-combination))
35 (defparameter *method-combinations* (make-hash-table :size 32 :test 'eq))
36
37 (defun search-method-combination (name)
38 (mp:with-lock (*method-combinations-lock*)
39 (or (gethash name *method-combinations*)
40 (error "~A does not name a method combination" name))))
41
42 (defun install-method-combination (name function)
43 (mp:with-lock (*method-combinations-lock*)
44 (setf (gethash name *method-combinations*) function))
45 name)
46
47 (defun make-method-combination (name compiler options)
48 (with-early-make-instance +method-combination-slots+
49 (o (find-class 'method-combination)
50 :name name
51 :compiler compiler
52 :options options)
53 o))
54
55 (defun find-method-combination (gf method-combination-type-name method-combination-options)
56 (declare (ignore gf))
57 (make-method-combination method-combination-type-name
58 (search-method-combination method-combination-type-name)
59 method-combination-options
60 ))
61
62 (defun define-simple-method-combination (name &key documentation
63 identity-with-one-argument
64 (operator name))
65 `(define-method-combination ,name (&optional (order :MOST-SPECIFIC-FIRST))
66 ((around (:AROUND))
67 (principal (,name) :REQUIRED t))
68 ,documentation
69 (let ((main-effective-method
70 `(,',operator ,@(mapcar #'(lambda (x) `(CALL-METHOD ,x NIL))
71 (if (eql order :MOST-SPECIFIC-LAST)
72 (reverse principal)
73 principal)))))
74 (cond (around
75 `(call-method ,(first around)
76 (,@(rest around) (make-method ,main-effective-method))))
77 (,(if identity-with-one-argument
78 '(rest principal)
79 t)
80 main-effective-method)
81 (t (second main-effective-method))))))
82
83 (defun method-combination-arguments-reshuffling (mc-whole-arg mc-lambda-list gf-lambda-list body)
84 ;; Implement the reshuffling of arguments to the generic function
85 ;; into arguments for a method-combination arguments lambda list
86 ;; required for the long form of DEFINE-METHOD-COMBINATION. The
87 ;; generic function arguments are stored in the variable
88 ;; .COMBINED-METHOD-ARGS. of type STACK-FRAME. To extract the
89 ;; arguments we apply to this stack frame a function whose
90 ;; lambda-list is built below by reshaping the method-combination
91 ;; lambda-list to fit the form of the generic-function lambda-list.
92 (let ((lambda-list '())
93 (let-statements '())
94 (ignored-vars '())
95 n-gf-requireds n-gf-optionals)
96 (multiple-value-bind (mc-requireds mc-optionals mc-rest mc-key-flag mc-keywords
97 mc-allow-other-keys mc-aux-vars)
98 (si::process-lambda-list mc-lambda-list 'function)
99 (declare (ignore mc-allow-other-keys))
100 (multiple-value-bind (gf-requireds gf-optionals)
101 (si::process-lambda-list gf-lambda-list 'function)
102 (setf n-gf-requireds (first gf-requireds)
103 n-gf-optionals (first gf-optionals))
104 (when mc-whole-arg
105 (push `(,mc-whole-arg (apply #'list .combined-method-args.))
106 let-statements))
107 (loop for r in (rest mc-requireds) by #'cdr
108 for i from 0
109 if (< i n-gf-requireds)
110 do (push r lambda-list)
111 else ; excess required args of the method-combination
112 ; are set to nil
113 do (push `(,r nil) let-statements))
114 ;; excess required args of the generic-function are ignored
115 (loop repeat (- n-gf-requireds (first mc-requireds))
116 for v = (gensym)
117 do (push v lambda-list)
118 (push v ignored-vars))
119 (push '&optional lambda-list)
120 (loop for o on (rest mc-optionals) by #'cdddr
121 for i from 0
122 if (< i n-gf-optionals)
123 do (push (if (third o)
124 `(,(first o) ,(second o) ,(third o))
125 `(,(first o) ,(second o)))
126 lambda-list)
127 else ; excess optional args of the method-combination
128 ; are set to their init forms
129 do (push `(,(first o) ,(second o)) let-statements)
130 (when (third o)
131 (push `(,(third o) nil) let-statements)))
132 ;; excess args of the generic-function are ignored
133 (loop repeat (- n-gf-optionals (first mc-optionals))
134 for v = (gensym)
135 do (push v lambda-list)
136 (push v ignored-vars))
137 (unless mc-rest
138 (setf mc-rest (gensym))
139 (push mc-rest ignored-vars))
140 ;; rest, keyword and aux args are treated as usual
141 (push '&rest lambda-list)
142 (push mc-rest lambda-list)
143 (when mc-key-flag
144 (push '&key lambda-list)
145 (loop for k on (rest mc-keywords) by #'cddddr
146 do (push (if (fourth k)
147 `((,(first k) ,(second k)) ,(third k) ,(fourth k))
148 `((,(first k) ,(second k)) ,(third k)))
149 lambda-list))
150 (push '&allow-other-keys lambda-list))
151 (when mc-aux-vars
152 (push '&aux lambda-list)
153 (loop for a on (rest mc-aux-vars) by #'cddr
154 do (push `(,(first a) ,(second a)) lambda-list)))
155 `(apply #'(lambda ,(nreverse lambda-list)
156 (declare (ignore ,@ignored-vars))
157 (let ,(nreverse let-statements)
158 ,body))
159 .combined-method-args.)))))
160
161 (defun process-define-method-combination-arguments-lambda-list
162 (lambda-list generic-function body)
163 (declare (si::c-local))
164 (when (null lambda-list)
165 (return-from process-define-method-combination-arguments-lambda-list body))
166 (let ((whole (when (eq (first lambda-list) '&whole)
167 (prog1 (second lambda-list)
168 (setf lambda-list (cddr lambda-list))))))
169 (multiple-value-bind (requireds optionals rest key-flag keywords
170 allow-other-keys aux-vars)
171 (si::process-lambda-list lambda-list 'function)
172 (declare (ignore allow-other-keys key-flag))
173 ;; This is a little complicated. We are constructing a form
174 ;; which when evaluated constructs another form that finally
175 ;; implements the desired destructuring of arguments supplied to
176 ;; the generic function.
177 ;;
178 ;; First evaluate the body form containing the grouping of the
179 ;; methods and the user-supplied method-combination body in a
180 ;; context in which all free variables are bound to fresh
181 ;; symbols (we use the name of the free variable itself). This
182 ;; part happens when the method combination is defined.
183 `(let ((result (let ,(mapcar #'(lambda (v) `(,v ',v))
184 (append (when whole (list whole))
185 (rest requireds)
186 (loop for o on (rest optionals) by #'cdddr
187 collect (first o)
188 when (third o) collect (third o))
189 (when rest (list rest))
190 (loop for k on (rest keywords) by #'cddddr
191 collect (second k)
192 when (fourth k) collect (fourth k))
193 (loop for a on (rest aux-vars) by #'cddr
194 collect (first a))))
195 ,body)))
196 ;; Second, construct a form which implements the required
197 ;; complex reshuffling of arguments. This part happens after
198 ;; a generic function using the method combination is
199 ;; defined.
200 (method-combination-arguments-reshuffling
201 ',whole
202 ',lambda-list
203 (generic-function-lambda-list ,generic-function)
204 result)))))
205
206 (defmacro with-method-groups ((&rest method-groups) &body body)
207 (let (group-names matchers cleanup)
208 (flet ((parse-qualifier-pattern (pattern)
209 (cond ((eq pattern '*) 't)
210 ((eq pattern nil) '(null .method-qualifiers.))
211 ((symbolp pattern) `(,pattern .method-qualifiers.))
212 ((listp pattern)
213 `(do ((pattern ',pattern (cdr pattern))
214 (qualifiers .method-qualifiers. (cdr qualifiers)))
215 ((or (eq pattern '*)
216 (and (null pattern)
217 (null qualifiers)))
218 t)
219 (unless (and pattern qualifiers
220 (or (eq (car pattern) '*)
221 (eq (car pattern) (car qualifiers))))
222 (return nil))))
223 (t
224 (error "Invalid method group pattern ~s." pattern)))))
225 (dolist (group method-groups)
226 (destructuring-bind (name pattern &key description
227 (order :most-specific-first)
228 (required nil))
229 group
230 (declare (ignore description))
231 (push name group-names)
232 (push `(,(parse-qualifier-pattern pattern) (push .method. ,name))
233 matchers)
234 (when required
235 (push `(unless ,name
236 (error "No methods in required group ~S." ',name))
237 cleanup))
238 (case order
239 (:most-specific-first
240 (push `(setf ,name (nreverse ,name)) cleanup))
241 (:most-specific-last)
242 (otherwise
243 (push `(when (eq ,order :most-specific-first)
244 (setf ,name (nreverse ,name)))
245 cleanup))))))
246 `(let ,group-names
247 (dolist (.method. .methods-list.)
248 (let ((.method-qualifiers. (method-qualifiers .method.)))
249 (declare (ignorable .method-qualifiers.))
250 (cond ,@(nreverse matchers)
251 ,@(unless (member t matchers :key #'car)
252 `((t (invalid-method-error
253 .method.
254 "Method qualifiers ~s are not allowed." .method-qualifiers.)))))))
255 ,@cleanup
256 ,@body)))
257
258 (defun define-complex-method-combination (form)
259 (declare (si::c-local))
260 (flet ((syntax-error ()
261 (error "~S is not a valid DEFINE-METHOD-COMBINATION form." form)))
262 (destructuring-bind (name lambda-list method-groups &rest body
263 &aux (generic-function '.generic-function.)
264 (arguments nil)
265 decls documentation)
266 form
267 (let ((x (first body)))
268 (when (and (consp x) (eql (first x) :ARGUMENTS))
269 (setf body (rest body))
270 (setf arguments (rest x))))
271 (let ((x (first body)))
272 (when (and (consp x) (eql (first x) :GENERIC-FUNCTION))
273 (setf body (rest body))
274 (setf generic-function (second x))))
275 (unless (and (symbolp name) (symbolp generic-function))
276 (error "~S is not a valid DEFINE-METHOD-COMBINATION form." form))
277 (multiple-value-setq (decls body documentation)
278 (si::find-declarations body t))
279 `(progn
280 ,@(si::expand-set-documentation name 'method-combination documentation)
281 (install-method-combination ',name
282 (lambda (,generic-function .methods-list. ,@lambda-list)
283 (declare (ignorable ,generic-function))
284 ,@decls
285 (block ,name
286 (effective-method-function
287 ,(process-define-method-combination-arguments-lambda-list
288 arguments generic-function
289 `(with-method-groups ,method-groups ,@body))
290 t))))))))
291
292 (defmacro define-method-combination (name &body body)
293 (if (and body (listp (first body)))
294 (define-complex-method-combination (list* name body))
295 (apply #'define-simple-method-combination name body)))
296
297 (defun method-combination-error (format-control &rest args)
298 ;; FIXME! We should emit a more detailed error!
299 (error "Method-combination error:~%~S"
300 (apply #'format nil format-control args)))
301
302 (defun invalid-method-error (method format-control &rest args)
303 (error "Invalid method error for ~A~%~S"
304 method
305 (apply #'format nil format-control args)))
306
307 (install-method-combination 'standard 'standard-compute-effective-method)
308 (progn
309 (define-method-combination progn :identity-with-one-argument t)
310 (define-method-combination and :identity-with-one-argument t)
311 (define-method-combination max :identity-with-one-argument t)
312 (define-method-combination + :identity-with-one-argument t)
313 (define-method-combination nconc :identity-with-one-argument t)
314 (define-method-combination append :identity-with-one-argument nil)
315 (define-method-combination list :identity-with-one-argument nil)
316 (define-method-combination min :identity-with-one-argument t)
317 (define-method-combination or :identity-with-one-argument t))