[e90b2f]: src / clos / combin.lsp Maximize Restore History

Download this file

combin.lsp    377 lines (347 with data), 15.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: CLOS -*-
;;;;
;;;; Copyright (c) 1992, Giuseppe Attardi.
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
;;;;
;;;; ECoLisp is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Library General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 2 of the License, or (at your option) any later version.
;;;;
;;;; See file '../Copyright' for full details.
(in-package "CLOS")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; COMPILING EFFECTIVE METHODS
;;;
;;; The following functions take care of transforming the forms
;;; produced by the method combinations into effective methods. In ECL
;;; effective methods are nothing but directly callable functions.
;;; Ideally, this compilation should just produce new compiled
;;; functions. However, we do not want to cons a lot of functions, and
;;; therefore we use closures.
;;;
;;; Formerly we used to keep a list of precompiled effective methods
;;; and made a structural comparison between the current method and
;;; the precompiled ones, so as to save memory. This only causes
;;; improvements in declarative combinations. For standard combinations
;;; it should be enough with a couple of different closures and hence
;;; the structural comparison is a loss of time.
;;;
;;; This is the core routine. It produces effective methods (i.e.
;;; functions) out of the forms generated by the method combinators.
;;; We consider the following cases:
;;; 1) Ordinary methods. The function of the method is extracted.
;;; 2) Functions. They map to themselves. This only happens
;;; when these functions have been generated by previous calls
;;; to EFFECTIVE-METHOD-FUNCTION.
;;; 3) (CALL-METHOD method rest-methods) A closure is
;;; generated that invokes the current method while informing
;;; it about the remaining methods.
;;; 4) (MAKE-METHOD form) A function is created that takes the
;;; list of arguments of the generic function and evaluates
;;; the forms in a null environment. This is the only form
;;; that may lead to consing of new bytecodes objects. Nested
;;; CALL-METHOD are handled via the global macro CALL-METHOD.
;;; 5) Ordinary forms are turned into lambda forms, much like
;;; what happens with the content of MAKE-METHOD.
;;;
(defun effective-method-function (form &optional top-level &aux first)
(cond ((functionp form)
form)
((method-p form)
(method-function form))
((atom form)
(error "Malformed effective method form:~%~A" form))
((eq (setf first (first form)) 'MAKE-METHOD)
(coerce `(lambda (.combined-method-args. *next-methods*)
(declare (special .combined-method-args. *next-methods*))
,(second form))
'function))
((eq first 'CALL-METHOD)
(combine-method-functions
(effective-method-function (second form))
(mapcar #'effective-method-function (third form))))
(top-level
(coerce `(lambda (.combined-method-args. no-next-methods)
(declare (ignorable no-next-methods))
,form)
'function))
(t
(error "Malformed effective method form:~%~A" form))))
;;;
;;; This function is a combinator of effective methods. It creates a
;;; closure that invokes the first method while passing the information
;;; of the remaining methods. The resulting closure (or effective method)
;;; is the equivalent of (CALL-METHOD method rest-methods)
;;;
(defun combine-method-functions (method rest-methods)
(declare (si::c-local))
#'(lambda (.combined-method-args. no-next-methods)
(declare (ignorable no-next-methods))
(funcall method .combined-method-args. rest-methods)))
(defmacro call-method (method &optional rest-methods)
`(funcall ,(effective-method-function method)
.combined-method-args.
',(and rest-methods (mapcar #'effective-method-function rest-methods))))
(defun call-next-method (&rest args)
(declare (special .combined-method-args. *next-methods*))
(unless *next-methods*
(error "No next method."))
(funcall (car *next-methods*) (or args .combined-method-args.) (rest *next-methods*)))
(defun next-method-p ()
(declare (special *next-methods*))
*next-methods*)
(define-compiler-macro call-next-method (&rest args)
`(if *next-methods*
(funcall (car *next-methods*)
,(if args `(list ,@args) '.combined-method-args.)
(rest *next-methods*))
(error "No next method.")))
(define-compiler-macro next-method-p ()
'clos::*next-methods*)
(defun error-qualifier (m qualifier)
(declare (si::c-local))
(error "Standard method combination allows only one qualifier ~
per method, either :BEFORE, :AFTER, or :AROUND; while ~
a method with ~S was found."
m qualifier))
(defun standard-main-effective-method (before primary after)
(declare (si::c-local))
#'(lambda (.combined-method-args. no-next-method)
(declare (ignorable no-next-method))
(dolist (i before)
(funcall i .combined-method-args. nil))
(if after
(multiple-value-prog1
(funcall (first primary) .combined-method-args. (rest primary))
(dolist (i after)
(funcall i .combined-method-args. nil)))
(funcall (first primary) .combined-method-args. (rest primary)))))
(defun standard-compute-effective-method (gf methods)
(with-early-accessors (+standard-method-slots+)
(let* ((before ())
(primary ())
(after ())
(around ()))
(dolist (m methods)
(let* ((qualifiers (method-qualifiers m))
(f (method-function m)))
(cond ((null qualifiers) (push f primary))
((rest qualifiers) (error-qualifier m qualifiers))
((eq (setq qualifiers (first qualifiers)) :BEFORE)
(push f before))
((eq qualifiers :AFTER) (push f after))
((eq qualifiers :AROUND) (push f around))
(t (error-qualifier m qualifiers)))))
;; When there are no primary methods, an error is to be signaled,
;; and we need not care about :AROUND, :AFTER or :BEFORE methods.
(when (null primary)
(return-from standard-compute-effective-method
#'(lambda (&rest args)
(apply 'no-primary-method gf args))))
;; PRIMARY, BEFORE and AROUND are reversed because they have to
;; be on most-specific-first order (ANSI 7.6.6.2), while AFTER
;; may remain as it is because it is least-specific-order.
(setf primary (nreverse primary)
before (nreverse before))
(if around
(let ((main (if (or before after)
(list
(standard-main-effective-method before primary after))
primary)))
(setf around (nreverse around))
(combine-method-functions (first around)
(nconc (rest around) main)))
(if (or before after)
(standard-main-effective-method before primary after)
(combine-method-functions (first primary) (rest primary))))
)))
;; ----------------------------------------------------------------------
;; DEFINE-METHOD-COMBINATION
;;
;; METHOD-COMBINATION objects are just a list
;; (name arg*)
;; where NAME is the name of the method combination type defined with
;; DEFINE-METHOD-COMBINATION, and ARG* is zero or more arguments.
;;
;; For each method combination type there is an associated function,
;; and the list of all known method combination types is kept in
;; *METHOD-COMBINATIONS* in the form of property list:
;; (mc-type-name1 function1 mc-type-name2 function2 ....)
;;
;; FUNCTIONn is the function associated to a method combination. It
;; is of type (FUNCTION (generic-function method-list) FUNCTION),
;; and it outputs an anonymous function which is the effective method.
;;
#+threads
(defparameter *method-combinations-lock* (mp:make-lock :name 'find-method-combination))
(defparameter *method-combinations* (make-hash-table :size 32 :test 'eq))
(defun search-method-combination (name)
(mp:with-lock (*method-combinations-lock*)
(or (gethash name *method-combinations*)
(error "~A does not name a method combination" name))))
(defun install-method-combination (name function)
(mp:with-lock (*method-combinations-lock*)
(setf (gethash name *method-combinations*) function))
name)
(defun make-method-combination (name compiler options)
(with-early-make-instance +method-combination-slots+
(o (find-class 'method-combination)
:name name
:compiler compiler
:options options)
o))
(defun find-method-combination (gf method-combination-type-name method-combination-options)
(make-method-combination method-combination-type-name
(search-method-combination method-combination-type-name)
method-combination-options
))
(defun define-simple-method-combination (name &key documentation
identity-with-one-argument
(operator name))
`(define-method-combination
,name (&optional (order :MOST-SPECIFIC-FIRST))
((around (:AROUND))
(principal (,name) :REQUIRED t))
,documentation
(let ((main-effective-method
`(,',operator ,@(mapcar #'(lambda (x) `(CALL-METHOD ,x NIL))
(if (eql order :MOST-SPECIFIC-LAST)
(reverse principal)
principal)))))
(cond (around
`(call-method ,(first around)
(,@(rest around) (make-method ,main-effective-method))))
(,(if identity-with-one-argument
'(rest principal)
t)
main-effective-method)
(t (second main-effective-method))))))
(defun define-complex-method-combination (form)
(declare (si::c-local))
(flet ((syntax-error ()
(error "~S is not a valid DEFINE-METHOD-COMBINATION form"
form)))
(destructuring-bind (name lambda-list method-groups &rest body &aux
(group-names '())
(group-checks '())
(group-after '())
(generic-function '.generic-function.)
(method-arguments '()))
form
(unless (symbolp name) (syntax-error))
(let ((x (first body)))
(when (and (consp x) (eql (first x) :ARGUMENTS))
(error "Option :ARGUMENTS is not supported in DEFINE-METHOD-COMBINATION.")))
(let ((x (first body)))
(when (and (consp x) (eql (first x) :GENERIC-FUNCTION))
(setf body (rest body))
(unless (symbolp (setf generic-function (second x)))
(syntax-error))))
(dolist (group method-groups)
(destructuring-bind (group-name predicate &key description
(order :most-specific-first) (required nil))
group
(if (symbolp group-name)
(push group-name group-names)
(syntax-error))
(let ((condition
(cond ((eql predicate '*) 'T)
((and predicate (symbolp predicate))
`(,predicate .METHOD-QUALIFIERS.))
((and (listp predicate)
(let* ((q (last predicate 0))
(p (copy-list (butlast predicate 0))))
(when (every #'symbolp p)
(if (eql q '*)
`(every #'equal ',p .METHOD-QUALIFIERS.)
`(equal ',p .METHOD-QUALIFIERS.))))))
(t (syntax-error)))))
(push `(,condition (push .METHOD. ,group-name)) group-checks))
(when required
(push `(unless ,group-name
(error "Method combination: ~S. No methods ~
in required group ~S." ,name ,group-name))
group-after))
(case order
(:most-specific-first
(push `(setf ,group-name (nreverse ,group-name)) group-after))
(:most-specific-last)
(otherwise
(let ((order-var (gensym)))
(setf group-names (append group-names (list (list order-var order)))
group-after (list* `(when (eq ,order-var :most-specific-first)
(setf ,group-name (nreverse ,group-name)))
group-after)))))))
`(install-method-combination ',name
(ext::lambda-block ,name (,generic-function .methods-list. ,@lambda-list)
(let (,@group-names)
(dolist (.method. .methods-list.)
(let ((.method-qualifiers. (method-qualifiers .method.)))
(cond ,@(nreverse group-checks)
(t (invalid-method-error .method.
"Method qualifiers ~S are not allowed in the method~
combination ~S." .method-qualifiers. ,name)))))
,@group-after
(effective-method-function (progn ,@body) t))))
)))
(defmacro define-method-combination (name &body body)
(if (and body (listp (first body)))
(define-complex-method-combination (list* name body))
(apply #'define-simple-method-combination name body)))
(defun method-combination-error (format-control &rest args)
;; FIXME! We should emit a more detailed error!
(error "Method-combination error:~%~S"
(apply #'format nil format-control args)))
(defun invalid-method-error (method format-control &rest args)
(error "Invalid method error for ~A~%~S"
method
(apply #'format nil format-control args)))
;;; ----------------------------------------------------------------------
;;; COMPUTE-EFFECTIVE-METHOD
;;;
(eval-when (compile)
(let* ((class (find-class 'method-combination)))
(define-compiler-macro method-combination-compiler (o)
`(si::instance-ref ,o ,(slot-definition-location (gethash 'compiler (slot-table class)))))
(define-compiler-macro method-combination-options (o)
`(si::instance-ref ,o ,(slot-definition-location (gethash 'options (slot-table class)))))))
(defun std-compute-effective-method (gf method-combination applicable-methods)
(declare (type method-combination method-combination)
(type generic-function gf)
(optimize speed (safety 0)))
(with-early-accessors (+method-combination-slots+)
(let* ((compiler (method-combination-compiler method-combination))
(options (method-combination-options method-combination)))
(if options
(apply compiler gf applicable-methods options)
(funcall compiler gf applicable-methods)))))
(defun compute-effective-method-function (gf method-combination applicable-methods)
;; Cannot be inlined because it will be a method
(declare (notinline compute-effective-method))
(let ((form (compute-effective-method gf method-combination applicable-methods)))
(let ((aux form) f)
(if (and (listp aux)
(eq (pop aux) 'funcall)
(functionp (setf f (pop aux)))
(eq (pop aux) '.combined-method-args.)
(eq (pop aux) '*next-methods*))
f
(effective-method-function form t)))))
(defun compute-effective-method (gf method-combination applicable-methods)
`(funcall ,(std-compute-effective-method gf method-combination applicable-methods)
.combined-method-args. *next-methods*))
;;
;; These method combinations are bytecompiled, for simplicity.
;;
(install-method-combination 'standard 'standard-compute-effective-method)
(eval '(progn
(define-method-combination progn :identity-with-one-argument t)
(define-method-combination and :identity-with-one-argument t)
(define-method-combination max :identity-with-one-argument t)
(define-method-combination + :identity-with-one-argument t)
(define-method-combination nconc :identity-with-one-argument t)
(define-method-combination append :identity-with-one-argument nil)
(define-method-combination list :identity-with-one-argument nil)
(define-method-combination min :identity-with-one-argument t)
(define-method-combination or :identity-with-one-argument t)))