Debian Codegraph @

src/clos/change.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 ;;; INSTANCE UPDATE PROTOCOL
14 ;;;
15 ;;;
16 ;;; PART 1: CHANGING THE CLASS OF AN INSTANCE
17 ;;;
18 ;;; The method CHANGE-CLASS performs most of the work.
19 ;;;
20 ;;; a) The structure of the instance is changed to match the new
21 ;;; number of local slots.
22 ;;; b) The new local slots are filled with the value of the old
23 ;;; slots. Only the name is used, so that a new local slot may
24 ;;; get the value of old slots that were either local or shared.
25 ;;; c) Finally, UPDATE-INSTANCE-FOR-DIFFERENT-CLASS is invoked
26 ;;; with a copy of the instance as it looked before the change,
27 ;;; the changed instance and enough information to perform any
28 ;;; extra processing.
29 ;;;
30
31 (defmethod update-instance-for-different-class
32 ((old-data standard-object) (new-data standard-object) &rest initargs)
33 (let ((old-local-slotds (si::instance-slotds old-data))
34 (new-local-slotds (remove :instance (si::instance-slotds new-data)
35 :test-not #'eq :key #'slot-definition-allocation))
36 added-slots)
37 (setf added-slots (set-difference (mapcar #'slot-definition-name new-local-slotds)
38 (mapcar #'slot-definition-name old-local-slotds)))
39 (check-initargs (class-of new-data) initargs
40 (valid-keywords-from-methods
41 (compute-applicable-methods
42 #'update-instance-for-different-class
43 (list old-data new-data))
44 (compute-applicable-methods
45 #'shared-initialize (list new-data added-slots))))
46 (apply #'shared-initialize new-data added-slots initargs)))
47
48 (defmethod change-class ((instance standard-object) (new-class std-class)
49 &rest initargs)
50 (let* ((old-instance (si::copy-instance instance))
51 (new-size (class-size new-class))
52 (instance (si::allocate-raw-instance instance new-class new-size)))
53 (si::instance-sig-set instance)
54 ;; "The values of local slots specified by both the class Cto and
55 ;; Cfrom are retained. If such a local slot was unbound, it remains
56 ;; unbound."
57 ;; "The values of slots specified as shared in the class Cfrom and
58 ;; as local in the class Cto are retained."
59 (let* ((new-local-slotds (class-slots (class-of instance))))
60 (dolist (new-slot new-local-slotds)
61 ;; CHANGE-CLASS can only operate on the value of local slots.
62 (when (eq (slot-definition-allocation new-slot) :INSTANCE)
63 (let ((name (slot-definition-name new-slot)))
64 (if (and (slot-exists-p old-instance name)
65 (slot-boundp old-instance name))
66 (setf (slot-value instance name) (slot-value old-instance name))
67 (slot-makunbound instance name))))))
68 (apply #'update-instance-for-different-class old-instance instance
69 initargs)
70 instance))
71
72 (defmethod change-class ((instance class) new-class &rest initargs)
73 (declare (ignore new-class initargs))
74 (if (forward-referenced-class-p instance)
75 (call-next-method)
76 (error "The metaclass of a class metaobject cannot be changed.")))
77
78 ;;;
79 ;;; PART 2: UPDATING AN INSTANCE THAT BECAME OBSOLETE
80 ;;;
81 ;;; Each instance has a field (readable with SI::INSTANCE-SLOTDS),
82 ;;; which contains the list of slot definitions from its class which
83 ;;; are needed to update it when it is obsolete. Generally
84 ;;;
85 ;;; (SI::INSTANCE-OBSOLETE-P x)
86 ;;;
87 ;;; returns T whenever the instance became obsolete. We track that by
88 ;;; comparing the class stamp (which is updated by M-I-O) with a stamp
89 ;;; embedded in the instance. When the instance is being updated its
90 ;;; stamp is corrected to match its class stamp.
91 ;;;
92 ;;; There are two circumstances under which a instance may become
93 ;;; obsolete: either the class has been modified so the list of slots
94 ;;; has changed in an incompatible way (see FINALIZE-INHERITANCE), or
95 ;;; MAKE-INSTANCES-OBSOLETE has been called explicitly.
96 ;;;
97 ;;; The function UPDATE-INSTANCE (hidden to the user) does the job of
98 ;;; updating an instance that has become obsolete.
99 ;;;
100 ;;; a) A copy of the instance is saved to check the old values.
101 ;;; b) The structure of the instance is changed to match the new
102 ;;; number of local slots.
103 ;;; c) The new local slots are filled with the value of the old
104 ;;; local slots.
105 ;;; d) Finally, UPDATE-INSTANCE-FOR-REDEFINED-CLASS is invoked
106 ;;; with enough information to perform any extra initialization,
107 ;;; for instance of new slots.
108 ;;;
109 ;;; It is not clear when the function UPDATE-INSTANCE is invoked. At least
110 ;;; this will happen whenever the functions SLOT-VALUE, (SETF SLOT-VALUE),
111 ;;; SLOT-BOUNDP or SLOT-EXISTS-P are used.
112 ;;;
113
114 (defmethod update-instance-for-redefined-class
115 ((instance standard-object) added-slots discarded-slots property-list
116 &rest initargs)
117 (check-initargs (class-of instance) initargs
118 (valid-keywords-from-methods
119 (compute-applicable-methods
120 #'update-instance-for-redefined-class
121 (list instance added-slots discarded-slots property-list))
122 (compute-applicable-methods
123 #'shared-initialize
124 (list instance added-slots))))
125 (apply #'shared-initialize instance added-slots initargs))
126
127 (defmethod update-instance-for-redefined-class
128 ((instance std-class) added-slots discarded-slots property-list
129 &rest initargs)
130 (declare (ignore initargs))
131 ;; If the metaclass of this class changed, so did probably that of its
132 ;; subclasses. We need those subclasses to be up-to-date. This prevents
133 ;; errors when loading twice the following
134 ;; (defclass metaclas ...)
135 ;; (defclass x () ... (:metaclas metaclas))
136 ;; (defclass y (y) ...)
137 ;; because X might be redefined with Y not being up-to-date on the second
138 ;; pass.
139 (prog1 (call-next-method)
140 (dolist (class (class-direct-subclasses instance))
141 (ensure-up-to-date-instance class))))
142
143 (defun update-instance (instance)
144 (let* ((class (class-of instance))
145 (old-slotds (si::instance-slotds instance))
146 (new-slotds (class-slots class))
147 (old-instance (si::copy-instance instance))
148 (discarded-slots '())
149 (added-slots '())
150 (property-list '()))
151 (setf instance (si::allocate-raw-instance instance class (class-size class)))
152 (si::instance-sig-set instance)
153 (let* ((new-i 0)
154 (old-local-slotds (remove :instance old-slotds :test-not #'eq
155 :key #'slot-definition-allocation))
156 (new-local-slotds (remove :instance new-slotds :test-not #'eq
157 :key #'slot-definition-allocation)))
158 (declare (fixnum new-i))
159 (setq discarded-slots
160 (set-difference (mapcar #'slot-definition-name old-local-slotds)
161 (mapcar #'slot-definition-name new-local-slotds)))
162 (dolist (slot-name discarded-slots)
163 (let* ((ndx (position slot-name old-local-slotds :key #'slot-definition-name)))
164 (setf property-list
165 (list* slot-name (si::instance-ref old-instance ndx) property-list))))
166 (dolist (new-slot new-local-slotds)
167 (let* ((name (slot-definition-name new-slot))
168 (old-i (position name old-local-slotds :key #'slot-definition-name)))
169 (if old-i
170 (si::instance-set instance new-i
171 (si::instance-ref old-instance old-i))
172 (push name added-slots))
173 (incf new-i))))
174 (update-instance-for-redefined-class instance added-slots
175 discarded-slots property-list)))
176
177 ;;; ----------------------------------------------------------------------
178 ;;; CLASS REDEFINITION PROTOCOL
179
180 (ensure-generic-function 'reinitialize-instance
181 :lambda-list '(class &rest initargs))
182
183 (defmethod reinitialize-instance ((class class) &rest initargs
184 &key (direct-superclasses () direct-superclasses-p)
185 (direct-slots nil direct-slots-p))
186 (declare (ignore initargs))
187 (let ((name (class-name class)))
188 (when (member name '(CLASS BUILT-IN-CLASS) :test #'eq)
189 (error "The kernel CLOS class ~S cannot be changed." name)))
190 ;; remove previous defined accessor methods
191 (when (class-finalized-p class)
192 (remove-optional-slot-accessors class))
193 (call-next-method)
194 ;; the list of direct slots is converted to direct-slot-definitions
195 (when direct-slots-p
196 (setf (class-direct-slots class)
197 (loop for s in direct-slots
198 collect (canonical-slot-to-direct-slot class s))))
199 ;; set up inheritance checking that it makes sense
200 (when direct-superclasses-p
201 (setf direct-superclasses
202 (check-direct-superclasses class direct-superclasses))
203 (dolist (l (class-direct-superclasses class))
204 (unless (member l direct-superclasses)
205 (remove-direct-subclass l class)))
206 (dolist (l (setf (class-direct-superclasses class)
207 direct-superclasses))
208 (add-direct-subclass l class)))
209 ;; Per "Reinitialization of Class Metaobjects" we must finalize the
210 ;; inheritance here. Note that this means that already finalized class can't
211 ;; be reinitialized to have a forward-referenced-class as a superclass.
212 (if (class-finalized-p class)
213 (finalize-inheritance class)
214 (finalize-unless-forward class))
215 class)
216
217 (defun slot-definitions-compatible-p (old-slotds new-slotds)
218 (loop for o = (pop old-slotds)
219 for n = (pop new-slotds)
220 while (and o n)
221 do (let ((old-alloc (slot-definition-allocation o))
222 (new-alloc (slot-definition-allocation n))
223 (old-class (class-of o))
224 (new-class (class-of n)))
225 (unless (and (eq old-alloc new-alloc)
226 (eq (slot-definition-name o)
227 (slot-definition-name n))
228 (or (not (eq old-alloc :instance))
229 (= (slot-definition-location o)
230 (slot-definition-location n)))
231 (eq old-class new-class)
232 (or (eq new-class
233 (find-class 'standard-direct-slot-definition))
234 (eq new-class
235 (find-class 'standard-effective-slot-definition))))
236 (return-from slot-definitions-compatible-p nil)))
237 finally
238 (return (and (null o)
239 (null n)
240 (null old-slotds)
241 (null new-slotds)))))
242
243 (defmethod make-instances-obsolete ((class class))
244 (si::instance-new-stamp class)
245 class)
246
247 ;;; Structures can't be redefined in an incompatible way.
248 (defmethod make-instances-obsolete ((class structure-class))
249 class)
250
251 (defun remove-optional-slot-accessors (class)
252 (declare (class class)
253 (optimize (safety 0))
254 (si::c-local))
255 (dolist (slotd (class-slots class))
256 ;; remove previous defined reader methods
257 (dolist (reader (slot-definition-readers slotd))
258 (let* ((gf-object (fdefinition reader))
259 found)
260 ;; primary method
261 (when (setq found
262 (find-method gf-object nil (list class) nil))
263 (remove-method gf-object found))
264 ;; before method
265 (when (setq found
266 (find-method gf-object ':before (list class) nil))
267 (remove-method gf-object found))
268 ;; after method
269 (when (setq found
270 (find-method gf-object ':after (list class) nil))
271 (remove-method gf-object found))))
272
273 ;; remove previous defined writer methods
274 (dolist (writer (slot-definition-writers slotd))
275 (let* ((gf-object (fdefinition writer))
276 found)
277 ;; primary method
278 (when (setq found
279 (find-method gf-object nil (list 'T class) nil))
280 (remove-method gf-object found))
281 ;; before method
282 (when (setq found
283 (find-method gf-object ':before (list 'T class) nil))
284 (remove-method gf-object found))
285 ;; after method
286 (when (setq found
287 (find-method gf-object ':after (list 'T class) nil))
288 (remove-method gf-object found))))))