closer-lispworks.lisp 15.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
(in-package :closer-mop)

;; We need a new standard-class for various things.

(defclass standard-class (cl:standard-class) ())
(define-validate-superclass-method standard-class cl:standard-class)

;; We need a new funcallable-standard-class for various things.

(defclass funcallable-standard-class (clos:funcallable-standard-class) ())
(define-validate-superclass-method funcallable-standard-class clos:funcallable-standard-class)

13
#-lispworks4
14
15
16
17
18
19
20
21
(cl:defmethod validate-superclass
           ((class funcallable-standard-class)
            (superclass (eql (find-class 'funcallable-standard-object))))
  t)

;; We also need a new funcallable-standard-object because the default one
;; is not an instance of clos:funcallable-standard-class.

22
#+lispworks4
23
24
25
26
27
28
(defclass funcallable-standard-object (clos:funcallable-standard-object) ()
  (:metaclass clos:funcallable-standard-class))

;; The following code ensures that possibly incorrect lists of direct
;; superclasses are corrected.

29
#+lispworks4
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(defun modify-superclasses (direct-superclasses &optional (standardp t))
  (if (null direct-superclasses)
    (list (if standardp
            (find-class 'standard-object)
            (find-class 'funcallable-standard-object)))
    (let ((standard-object (if standardp
                             (find-class 'standard-object)
                             (find-class 'clos:funcallable-standard-object))))
      (if (eq (car (last direct-superclasses)) standard-object)
        (if standardp
          direct-superclasses
          (append (butlast direct-superclasses)
                  (list (find-class 'funcallable-standard-object))))
        (remove standard-object direct-superclasses)))))

;; During class re/initialization, we take care of the following things:
;; - Optimization of slot accessors is deactivated.
;; - Lists of direct superclasses are corrected.
;; - Removal of direct subclasses.

50
51
52
53
54
55
56
57
58
59
(defun optimize-slot-access-p (class)
  (flet ((applicablep (specializer)
           (if (consp specializer)
             (eql class (eql-specializer-object specializer))
             (subclassp (class-of class) specializer))))
    (and (loop for method in (generic-function-methods #'slot-value-using-class)
               never (applicablep (first (method-specializers method))))
         (loop for method in (generic-function-methods #'(setf slot-value-using-class))
               never (applicablep (second (method-specializers method)))))))

60
61
(cl:defmethod initialize-instance :around
  ((class standard-class) &rest initargs
62
63
   #+lispworks4 &key
   #+lispworks4 (direct-superclasses ()))
64
65
  (declare (dynamic-extent initargs))
  (apply #'call-next-method class
66
67
68
         #+lispworks4 :direct-superclasses
         #+lispworks4 (modify-superclasses direct-superclasses)
         :optimize-slot-access (optimize-slot-access-p class)
69
70
71
72
         initargs))

(cl:defmethod reinitialize-instance :around
  ((class standard-class) &rest initargs
73
74
   #+lispworks4 &key
   #+lispworks4 (direct-superclasses () direct-superclasses-p))
75
  (declare (dynamic-extent initargs))
76
  #+lispworks4
77
78
79
80
81
82
83
84
85
  (progn
    (when direct-superclasses-p
      (setq direct-superclasses (modify-superclasses direct-superclasses))
      (loop for superclass in (copy-list (class-direct-superclasses class))
            unless (member superclass direct-superclasses)
            do (remove-direct-subclass superclass class)))
    (if direct-superclasses-p
      (apply #'call-next-method class
             :direct-superclasses direct-superclasses
86
             :optimize-slot-access (optimize-slot-access-p class)
87
88
             initargs)
      (apply #'call-next-method class
89
             :optimize-slot-access (optimize-slot-access-p class)
90
             initargs)))
91
  #-lispworks4
92
  (apply #'call-next-method class
93
         :optimize-slot-access (optimize-slot-access-p class)
94
95
96
97
         initargs))

(cl:defmethod initialize-instance :around
  ((class funcallable-standard-class) &rest initargs
98
99
   #+lispworks4 &key
   #+lispworks4 (direct-superclasses ()))
100
101
  (declare (dynamic-extent initargs))
  (apply #'call-next-method class
102
103
104
         #+lispworks4 :direct-superclasses
         #+lispworks4 (modify-superclasses direct-superclasses nil)
         :optimize-slot-access (optimize-slot-access-p class)
105
106
107
108
         initargs))

(cl:defmethod reinitialize-instance :around
  ((class funcallable-standard-class) &rest initargs
109
110
   #+lispworks4 &key
   #+lispworks4 (direct-superclasses () direct-superclasses-p))
111
  (declare (dynamic-extent initargs))
112
  #+lispworks4
113
114
115
116
117
118
119
120
121
  (progn
    (when direct-superclasses-p
      (setq direct-superclasses (modify-superclasses direct-superclasses nil))
      (loop for superclass in (copy-list (class-direct-superclasses class))
            unless (member superclass direct-superclasses)
            do (remove-direct-subclass superclass class)))
    (if direct-superclasses-p
      (apply #'call-next-method class
             :direct-superclasses direct-superclasses
122
             :optimize-slot-access (optimize-slot-access-p class)
123
124
             initargs)
      (apply #'call-next-method class
125
             :optimize-slot-access (optimize-slot-access-p class)
126
             initargs)))
127
  #-lispworks4
128
  (apply #'call-next-method class
129
         :optimize-slot-access (optimize-slot-access-p class)
130
131
132
133
134
135
136
         initargs))

;; The following is necessary for forward-referenced-classes.
;; Since we replace the original funcallable-standard-object with
;; a new one, we have to prevent LispWorks from trying to use
;; the original one when forward-ferenced-classes are resolved.

137
#+lispworks4
138
139
140
141
142
143
144
(cl:defmethod change-class :around
  ((class forward-referenced-class)
   (new-class funcallable-standard-class)
   &rest initargs
   &key (direct-superclasses ()))
  (declare (dynamic-extent initargs))
  (apply #'call-next-method class new-class
145
         :optimize-slot-access (optimize-slot-access-p new-class)
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
         :direct-superclasses (modify-superclasses direct-superclasses nil)
         initargs))

;;; In LispWorks, the slot accessors (slot-value-using-class, etc.) are specialized
;;; on slot names instead of effective slot definitions. In order to fix this,
;;; we need to rewire the slot access protocol.

(cl:defmethod slot-value-using-class
           ((class standard-class) object (slot symbol))
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (let ((slotd (find slot (class-slots class)
                     :test #'eq
                     :key #'slot-definition-name)))
    (if slotd
      (slot-value-using-class class object slotd)
      (slot-missing class object slot 'slot-value))))

(cl:defmethod slot-value-using-class
           ((class standard-class) object (slotd standard-effective-slot-definition))
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (slot-value-using-class
   (load-time-value (class-prototype (find-class 'cl:standard-class)))
   object
   (slot-definition-name slotd)))

(cl:defmethod (setf slot-value-using-class)
           (new-value (class standard-class) object (slot symbol))
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (let ((slotd (find slot (class-slots class)
                     :test #'eq
                     :key #'slot-definition-name)))
    (if slotd
      (setf (slot-value-using-class class object slotd)
            new-value)
      (slot-missing class object slot 'setf new-value))))

(cl:defmethod (setf slot-value-using-class)
           (new-value (class standard-class) object (slotd standard-effective-slot-definition))
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (setf (slot-value-using-class
         (load-time-value (class-prototype (find-class 'cl:standard-class)))
         object
         (slot-definition-name slotd))
        new-value))

(cl:defmethod slot-boundp-using-class
           ((class standard-class) object (slot symbol))
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (let ((slotd (find slot (class-slots class)
                     :test #'eq
                     :key #'slot-definition-name)))
    (if slotd
      (slot-boundp-using-class class object slotd)
      (slot-missing class object slot 'slot-boundp))))

(cl:defmethod slot-boundp-using-class
           ((class standard-class) object (slotd standard-effective-slot-definition))
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (slot-boundp-using-class
   (load-time-value (class-prototype (find-class 'cl:standard-class)))
   object
   (slot-definition-name slotd)))

(cl:defmethod slot-makunbound-using-class
           ((class standard-class) object (slot symbol))
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (let ((slotd (find slot (class-slots class)
                     :test #'eq
                     :key #'slot-definition-name)))
    (if slotd
      (slot-makunbound-using-class class object slotd)
      (slot-missing class object slot 'slot-makunbound))))

(cl:defmethod slot-makunbound-using-class
           ((class standard-class) object (slotd standard-effective-slot-definition))
  (declare (optimize (speed 3) (debug 0) (safety 0)
                     (compilation-speed 0)))
  (slot-makunbound-using-class
   (load-time-value (class-prototype (find-class 'cl:standard-class)))
   object
   (slot-definition-name slotd)))

;; In LispWorks, eql specializers are lists. We cannot change this
;; but we can soften some of the incompatibilities.

(deftype eql-specializer ()
  '(or eql-specializer*
       (satisfies clos:eql-specializer-p)))

(cl:defgeneric eql-specializer-object (eql-specializer)
  (:method ((cons cons))
   (if (clos:eql-specializer-p cons)
     (cadr cons)
     (error "~S is not an eql-specializer." cons))))

(defun intern-eql-specializer (object)
  `(eql ,object))

(defclass eql-specializer* (metaobject)
  ((obj :reader eql-specializer-object
        :initarg eso
        :initform (error "Use intern-eql-specializer to create eql-specializers."))
   (direct-methods :reader specializer-direct-methods
                   :accessor es-direct-methods
                   :initform ())))

(defvar *eql-specializers* (make-hash-table))

(defun intern-eql-specializer* (object)
  (or (gethash object *eql-specializers*)
      (sys:with-hash-table-locked *eql-specializers*
        (or (gethash object *eql-specializers*)
            (setf (gethash object *eql-specializers*)
                  (make-instance 'eql-specializer* 'eso object))))))

(cl:defmethod add-direct-method ((specializer eql-specializer*) (method method))
  (pushnew method (es-direct-methods specializer)))

(cl:defmethod remove-direct-method ((specializer eql-specializer*) (method method))
  (removef (es-direct-methods specializer) method))

(cl:defgeneric specializer-direct-generic-functions (specializer)
  (:method ((class class))
   (remove-duplicates
    (mapcar #'method-generic-function
            (specializer-direct-methods class))))
  (:method ((eql-specializer eql-specializer*))
   (remove-duplicates
    (mapcar #'method-generic-function
            (specializer-direct-methods eql-specializer))))
  (:method ((cons cons))
   (specializer-direct-generic-functions
    (intern-eql-specializer*
     (eql-specializer-object cons)))))

;; The following method ensures that remove-method is called.

290
#+lispworks4
291
292
293
294
295
296
297
298
299
300
301
302
303
304
(cl:defmethod add-method :before ((gf standard-generic-function) (method method))
  (when-let (old-method (find-method gf (method-qualifiers method)
                                     (method-specializers method) nil))
    (remove-method gf old-method)))

;; The following two methods ensure that add/remove-direct-method is called,
;; and that the dependent protocol for generic function works.

(cl:defmethod add-method :after ((gf standard-generic-function) (method method))
  (dolist (specializer (method-specializers method))
    (if (consp specializer)
      (add-direct-method (intern-eql-specializer*
                          (eql-specializer-object specializer))
                         method)
305
      #+lispworks4
306
307
308
309
310
311
312
313
314
315
      (add-direct-method specializer method)))
  #+lispworks4.3
  (map-dependents gf (lambda (dep) (update-dependent gf dep 'add-method method))))

(cl:defmethod remove-method :after ((gf standard-generic-function) (method method))
  (dolist (specializer (method-specializers method))
    (if (consp specializer)
      (remove-direct-method (intern-eql-specializer*
                             (eql-specializer-object specializer))
                            method)
316
      #+lispworks4
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
      (remove-direct-method specializer method)))
  #+lispworks4.3
  (map-dependents gf (lambda (dep) (update-dependent gf dep 'remove-method method))))

(cl:defgeneric find-method-combination (gf combi combi-options)
  (:method ((gf generic-function) (combi symbol) combi-options)
   (when combi-options
     (error "This implementation of find-method-combination cannot handle method combination options."))
   (clos::find-a-method-combination-type combi)))

;; "Native" make-method-lambda.

(cl:defmethod make-method-lambda ((gf generic-function) (method standard-method) lambda-expression environment)
  (destructuring-bind
      (lambda (&rest args) &body body)
      lambda-expression
    (declare (ignore lambda))
    (loop with documentation = :unbound
          for (car . cdr) = body then cdr
          while (or (and cdr (stringp car))
                    (and (consp car) (eq (car car) 'declare)))
          if (stringp car)
          do (setf documentation
                   (if (eq documentation :unbound) car
                     (error "Too many documentation strings in lambda expression ~S."
                            lambda-expression)))
          else append (loop for declaration in (cdr car) 
                            if (eq (car declaration) 'ignore)
                            collect `(ignorable ,@(cdr declaration))
                            and collect `(dynamic-extent ,@(cdr declaration))
                            else collect declaration) into declarations
          finally (multiple-value-bind
                      (method-lambda method-args)
                      (clos:make-method-lambda
                       gf method args declarations
                       `(progn ,car ,@cdr)
                       environment)
                    (if (eq documentation :unbound)
                      (return (values method-lambda method-args))
                      (return (values
                               `(lambda ,(cadr method-lambda)
                                  ,documentation
                                  ,@(cddr method-lambda))
                               method-args)))))))

;; Provide standard-instance-access and funcallable-standard-instance-access

(declaim (inline standard-instance-access (setf standard-instance-access)))

(defun standard-instance-access (instance location)
  (clos::fast-standard-instance-access instance location))

(defun (setf standard-instance-access) (new-value instance location)
  (setf (clos::fast-standard-instance-access instance location) new-value))

(declaim (inline funcallable-instance-access))

(defun funcallable-instance-access (instance location &rest args)
  (declare (dynamic-extent args))
  (let* ((class (class-of instance))
         (slot (find location (class-slots class)
                     :key #'slot-definition-location)))
    (if slot
      (apply #'clos::funcallable-instance-access instance (slot-definition-name slot) args)
      (error "There is no slot with location ~S for instance ~S." location instance))))

(defun funcallable-standard-instance-access (instance location)
  (funcallable-instance-access instance location))

(defun (setf funcallable-standard-instance-access) (new-value instance location)
  (funcallable-instance-access instance location new-value))

(eval-when (:compile-toplevel :load-toplevel :execute)
  (pushnew :closer-mop *features*))