[3fc093]: src / clos / method.lsp Maximize Restore History

Download this file

method.lsp    456 lines (418 with data), 17.4 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: CLOS -*-
;;;;
;;;; Copyright (c) 1992, Giuseppe Attardi.
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
;;;;
;;;; This program 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")
;;; ----------------------------------------------------------------------
(defparameter *method-size* 32) ; Size of methods hash tables
;;; This holds fake methods created during bootstrap.
;;; It is an alist of:
;;; (method-name {method}+)
(defparameter *early-methods* nil)
;;;
;;; This is used by combined methods to communicate the next methods to
;;; the methods they call.
;;;
(defparameter *next-methods* nil)
;;; Add type declarations for the arguments of a METHOD. This implies
;;; copying the method arguments because the arguments may be modified.
(defparameter *add-method-argument-declarations* nil)
;;; ----------------------------------------------------------------------
;;; DEFMETHOD
;;;
(defun generic-function-method-class (generic-function)
(if *clos-booted*
(slot-value generic-function 'method-class)
(find-class 'standard-method)))
(defmacro defmethod (&whole whole name &rest args &environment env)
(let* ((*print-length* 3)
(*print-depth* 2)
(qualifiers (loop while (and args (not (listp (first args))))
collect (pop args)))
(specialized-lambda-list
(if args
(pop args)
(error "Illegal defmethod form: missing lambda list")))
(body args))
(multiple-value-bind (lambda-list required-parameters specializers)
(parse-specialized-lambda-list specialized-lambda-list)
(multiple-value-bind (lambda-form declarations documentation)
(make-raw-lambda name lambda-list required-parameters specializers body env)
(let* ((generic-function (ensure-generic-function name))
(method-class (generic-function-method-class generic-function))
method)
(when *clos-booted*
(when (symbolp method-class)
(setf method-class (find-class method-class nil)))
(if method-class
(setf method (class-prototype method-class))
(error "Cannot determine the method class for generic functions of type ~A"
(type-of generic-function))))
(multiple-value-bind (fn-form options)
(make-method-lambda generic-function method lambda-form env)
(when documentation
(setf options (list* :documentation documentation options)))
(multiple-value-bind (wrapped-lambda wrapped-p)
(simplify-lambda name fn-form)
(unless wrapped-p
(error "Unable to unwrap function"))
(ext:register-with-pde whole
`(install-method ',name ',qualifiers
,(list 'si::quasiquote specializers)
',lambda-list
,(maybe-remove-block wrapped-lambda)
,wrapped-p
,@(mapcar #'si::maybe-quote options))))))))))
(defun maybe-remove-block (method-lambda)
(when (eq (first method-lambda) 'lambda)
(multiple-value-bind (declarations body documentation)
(si::find-declarations (cddr method-lambda))
(let (block)
(when (and (null (rest body))
(listp (setf block (first body)))
(eq (first block) 'block))
(setf method-lambda `(ext:lambda-block ,(second block) ,(second method-lambda)
,@declarations
,@(cddr block)))
))))
method-lambda)
(defun simplify-lambda (method-name fn-form)
(let ((aux fn-form))
(if (and (eq (pop aux) 'lambda)
(equalp (pop aux) '(.combined-method-args. *next-methods*))
(equalp (pop aux) '(declare (special .combined-method-args. *next-methods*)))
(null (rest aux))
(= (length (setf aux (first aux))) 3)
(eq (first aux) 'apply)
(eq (third aux) '.combined-method-args.)
(listp (setf aux (second aux)))
(eq (first aux) 'lambda))
(values aux t)
(values fn-form nil))))
(defun make-raw-lambda (name lambda-list required-parameters specializers body env)
(declare (si::c-local))
(multiple-value-bind (declarations real-body documentation)
(sys::find-declarations body)
;; FIXME!! This deactivates the checking of keyword arguments
;; inside methods. The reason is that this checking must be
;; supplemented the knowledge of the keyword arguments of all
;; applicable methods (X3J13 7.6.5). Therefore, we should insert
;; that check, either in the method itself so that it is done
;; incrementally, or in COMPUTE-EFFECTIVE-METHOD.
(when (and (member '&key lambda-list)
(not (member '&allow-other-keys lambda-list)))
(let ((x (position '&aux lambda-list)))
(setf lambda-list
(append (subseq lambda-list 0 x)
'(&allow-other-keys)
(and x (subseq lambda-list x))
nil))))
(let* ((copied-variables '())
(class-declarations
(nconc (when *add-method-argument-declarations*
(loop for name in required-parameters
for type in specializers
when (and (not (eq type t)) (symbolp type))
do (push `(,name ,name) copied-variables) and
nconc `((type ,type ,name)
(si::no-check-type ,name))))
(cdar declarations)))
(block `(block ,(si::function-block-name name) ,@real-body))
(method-lambda
;; Remove the documentation string and insert the
;; appropriate class declarations. The documentation
;; string is removed to make it easy for us to insert
;; new declarations later, they will just go after the
;; second of the method lambda. The class declarations
;; are inserted to communicate the class of the method's
;; arguments to the code walk.
`(lambda ,lambda-list
,@(and class-declarations `((declare ,@class-declarations)))
,(if copied-variables
`(let* ,copied-variables ,block)
block))))
(values method-lambda declarations documentation))))
(defun make-method-lambda (gf method method-lambda env)
(multiple-value-bind (call-next-method-p next-method-p-p in-closure-p)
(walk-method-lambda method-lambda env)
(values `(lambda (.combined-method-args. *next-methods*)
(declare (special .combined-method-args. *next-methods*))
(apply ,(if in-closure-p
(add-call-next-method-closure method-lambda)
method-lambda)
.combined-method-args.))
nil)))
(defun add-call-next-method-closure (method-lambda)
(multiple-value-bind (declarations real-body documentation)
(si::find-declarations (cddr method-lambda))
`(lambda ,(second method-lambda)
(let* ((.closed-combined-method-args.
(if (listp .combined-method-args.)
.combined-method-args.
(apply #'list .combined-method-args.)))
(.next-methods. *next-methods*))
(flet ((call-next-method (&rest args)
(unless .next-methods.
(error "No next method"))
(funcall (car .next-methods.)
(or args .closed-combined-method-args.)
(rest .next-methods.)))
(next-method-p ()
.next-methods.))
,@real-body)))))
(defun environment-contains-closure (env)
;;
;; As explained in compiler.d (make_lambda()), we use a symbol with
;; name "FUNCTION-BOUNDARY" to mark the beginning of a function. If
;; we find that symbol twice, it is quite likely that this form will
;; end up in a closure.
;;
(let ((counter 0))
(declare (fixnum counter))
(dolist (item (car env))
(when (and (consp item)
(eq (first (the cons item)) 'si::function-boundary)
(> (incf counter) 1))
(return t)))))
(defun walk-method-lambda (method-lambda env)
(declare (si::c-local))
(let ((call-next-method-p nil)
(next-method-p-p nil)
(in-closure-p nil))
(flet ((code-walker (form env)
(unless (atom form)
(let ((name (first form)))
(case name
(CALL-NEXT-METHOD
(setf call-next-method-p
(or call-next-method-p T)
in-closure-p
(or in-closure-p (environment-contains-closure env))))
(NEXT-METHOD-P
(setf next-method-p-p t
in-closure-p (or in-closure-p (environment-contains-closure env))))
(FUNCTION
(when (eq (second form) 'CALL-NEXT-METHOD)
(setf in-closure-p t
call-next-method-p 'FUNCTION))
(when (eq (second form) 'NEXT-METHOD-P)
(setf next-method-p-p 'FUNCTION
in-closure-p t))))))
form))
(let ((si::*code-walker* #'code-walker))
;; Instead of (coerce method-lambda 'function) we use
;; explicitely the bytecodes compiler with an environment, no
;; stepping, compiler-env-p = t and execute = nil, so that the
;; form does not get executed.
(si::eval-with-env method-lambda env nil t t)))
(values call-next-method-p
next-method-p-p
in-closure-p)))
;;; ----------------------------------------------------------------------
;;; parsing
(defun legal-generic-function-name-p (name)
(si::valid-function-name-p name))
(defun parse-defmethod (args)
(declare (si::c-local))
;; This function has to extract the name of the method, a list of
;; possible qualifiers (identified by not being lists), the lambda
;; list of the method (which might be empty!) and the body of the
;; function.
(let* (name)
(unless args
(error "Illegal defmethod form: missing method name"))
(setq name (pop args))
(unless (legal-generic-function-name-p name)
(error "~A cannot be a generic function specifier.~%~
It must be either a non-nil symbol or ~%~
a list whose car is setf and whose second is a non-nil symbol."
name))
(do ((qualifiers '()))
((progn
(when (endp args)
(error "Illegal defmethod form: missing lambda-list"))
(listp (first args)))
(values name (nreverse qualifiers) (first args) (rest args)))
(push (pop args) qualifiers))))
(defun extract-lambda-list (specialized-lambda-list)
(values (parse-specialized-lambda-list specialized-lambda-list)))
(defun extract-specializer-names (specialized-lambda-list)
(nth-value 2 (parse-specialized-lambda-list specialized-lambda-list)))
(defun parse-specialized-lambda-list (specialized-lambda-list)
"This function takes a method lambda list and outputs the list of required
arguments, the list of specializers and a new lambda list where the specializer
have disappeared."
(declare (si::c-local))
;; SI:PROCESS-LAMBDA-LIST will ensure that the lambda list is
;; syntactically correct and will output as a first argument the
;; list of required arguments. We use this list to extract the
;; specializers and build a lambda list without specializers.
(do* ((arglist (rest (si::process-lambda-list specialized-lambda-list 'METHOD))
(rest arglist))
(lambda-list (copy-list specialized-lambda-list))
(ll lambda-list (rest ll))
(required-parameters '())
(specializers '())
arg variable specializer)
((null arglist)
(values lambda-list
(nreverse required-parameters)
(nreverse specializers)))
(setf arg (first arglist))
(cond
;; Just a variable
((atom arg)
(setf variable arg specializer T))
;; List contains more elements than variable and specializer
((not (endp (cddr arg)))
(si::simple-program-error "Syntax error in method specializer ~A" arg))
;; Specializer is NIL
((null (setf variable (first arg)
specializer (second arg)))
(si::simple-program-error
"NIL is not a valid specializer in a method lambda list"))
;; Specializer is a class name
((atom specializer))
;; Specializer is (EQL value)
((and (eql (first specializer) 'EQL)
(endp (cddr specializer)))
(let ((value (second specializer)))
(setf specializer
`(eql ,(if (constantp value)
(eval value)
(list 'si::unquote value))))))
;; Otherwise, syntax error
(t
(si::simple-program-error "Syntax error in method specializer ~A" arg)))
(setf (first ll) variable)
(push variable required-parameters)
(push specializer specializers)))
(defun declaration-specializers (arglist declarations)
(declare (si::c-local))
(do ((argscan arglist (cdr argscan))
(declist (when declarations (cdr declarations))))
((or
(null argscan)
(member (first argscan) '(&OPTIONAL &REST &KEY &ALLOW-OTHER-KEYS &AUX)))
`(DECLARE ,@declist))
(when (listp (first argscan))
(push `(TYPE ,(cadar argscan) ,(caar argscan)) declist))))
;;; ----------------------------------------------------------------------
;;; operations
(defun compute-method-keywords (lambda-list)
(multiple-value-bind (reqs opts rest key-flag keywords allow-other-keys)
(si::process-lambda-list lambda-list t)
(declare (ignore reqs opts rest key-flag))
(if allow-other-keys
't
(loop for k in (rest keywords) by #'cddddr
collect k))))
(defun make-method (method-class qualifiers specializers lambda-list fun options)
(declare (ignore options))
(with-early-make-instance
;; We choose the largest list of slots
+standard-accessor-method-slots+
(method (if (si::instancep method-class)
method-class
(find-class method-class))
:generic-function nil
:lambda-list lambda-list
:function fun
:specializers specializers
:qualifiers qualifiers
:keywords (compute-method-keywords lambda-list))
method))
;;; early version used during bootstrap
(defun method-p (x)
(si::instancep x))
;;; early version used during bootstrap
(defun add-method (gf method)
(with-early-accessors (+standard-method-slots+ +standard-generic-function-slots+ +standard-class-slots+)
(let* ((name (generic-function-name gf))
(method-entry (assoc name *early-methods*)))
(unless method-entry
(setq method-entry (list name))
(push method-entry *early-methods*))
(push method (cdr method-entry))
(push method (generic-function-methods gf))
(setf (method-generic-function method) gf)
(unless (si::sl-boundp (generic-function-lambda-list gf))
(setf (generic-function-lambda-list gf) (method-lambda-list method))
(setf (generic-function-argument-precedence-order gf)
(rest (si::process-lambda-list (method-lambda-list method) t))))
(compute-g-f-spec-list gf)
(set-generic-function-dispatch gf)
method)))
(defun find-method (gf qualifiers specializers &optional (errorp t))
(declare (notinline method-qualifiers))
(flet ((filter-specializer (name)
(cond ((typep name 'specializer)
name)
((atom name)
(let ((class (find-class name nil)))
(unless class
(error "~A is not a valid specializer name" name))
class))
((and (eq (first name) 'EQL)
(null (cddr name)))
(cdr name))
(t
(error "~A is not a valid specializer name" name))))
(specializer= (cons-or-class specializer)
(if (consp cons-or-class)
(and (eql-specializer-flag specializer)
(eql (car cons-or-class)
(eql-specializer-object specializer)))
(eq cons-or-class specializer))))
(when (/= (length specializers)
(length (generic-function-argument-precedence-order gf)))
(error
"The specializers list~%~A~%does not match the number of required arguments in ~A"
specializers (generic-function-name gf)))
(loop with specializers = (mapcar #'filter-specializer specializers)
for method in (generic-function-methods gf)
when (and (equal qualifiers (method-qualifiers method))
(every #'specializer= specializers (method-specializers method)))
do (return-from find-method method))
;; If we did not find any matching method, then the list of
;; specializers might have the wrong size and we must signal
;; an error.
(when errorp
(error "There is no method on the generic function ~S that agrees on qualifiers ~S and specializers ~S"
(generic-function-name gf)
qualifiers specializers)))
nil)
;;; ----------------------------------------------------------------------
;;; with-slots
(defmacro with-slots (slot-entries instance-form &body body)
(let* ((temp (gensym))
(accessors
(do ((scan slot-entries (cdr scan))
(res))
((null scan) (nreverse res))
(if (symbolp (first scan))
(push `(,(first scan) (slot-value ,temp ',(first scan))) res)
(push `(,(caar scan)
(slot-value ,temp ',(cadar scan))) res)))))
`(let ((,temp ,instance-form))
(symbol-macrolet ,accessors ,@body))))
;(with-slots (x (y2 y)) inst (setq x y2))
;;; ----------------------------------------------------------------------
;;; with-accessors
(defmacro with-accessors (slot-accessor-pairs instance-form &body body)
(let* ((temp (gensym))
(accessors (do ((scan slot-accessor-pairs (cdr scan))
(res))
((null scan) (nreverse res))
(push `(,(caar scan) (,(cadar scan) ,temp)) res))))
`(let ((,temp ,instance-form))
(symbol-macrolet ,accessors ,@body))))
;;; Force the compiler into optimizing use of gethash inside methods:
(setf (symbol-function 'SLOT-INDEX) (symbol-function 'GETHASH))