/[cmucl]/src/pcl/std-class.lisp
ViewVC logotype

Contents of /src/pcl/std-class.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (show annotations)
Wed Jan 6 14:30:07 1999 UTC (15 years, 3 months ago) by pw
Branch: MAIN
Changes since 1.19: +3 -2 lines
Backout previous change to ensure-class-values. There is more to
this bug than previously thought.
1 ;;;-*-Mode:LISP; Package:PCL; Base:10; Syntax:Common-lisp -*-
2 ;;;
3 ;;; *************************************************************************
4 ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
5 ;;; All rights reserved.
6 ;;;
7 ;;; Use and copying of this software and preparation of derivative works
8 ;;; based upon this software are permitted. Any distribution of this
9 ;;; software or derivative works must comply with all applicable United
10 ;;; States export control laws.
11 ;;;
12 ;;; This software is made available AS IS, and Xerox Corporation makes no
13 ;;; warranty about the software, its performance or its conformity to any
14 ;;; specification.
15 ;;;
16 ;;; Any person obtaining a copy of this software is requested to send their
17 ;;; name and post office or electronic mail address to:
18 ;;; CommonLoops Coordinator
19 ;;; Xerox PARC
20 ;;; 3333 Coyote Hill Rd.
21 ;;; Palo Alto, CA 94304
22 ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
23 ;;;
24 ;;; Suggestions, comments and requests for improvements are also welcome.
25 ;;; *************************************************************************
26 ;;;
27 #+cmu
28 (ext:file-comment
29 "$Header: /tiger/var/lib/cvsroots/cmucl/src/pcl/std-class.lisp,v 1.20 1999/01/06 14:30:07 pw Exp $")
30 ;;;
31
32 (in-package :pcl)
33
34 (defmethod slot-accessor-function ((slotd effective-slot-definition) type)
35 (ecase type
36 (reader (slot-definition-reader-function slotd))
37 (writer (slot-definition-writer-function slotd))
38 (boundp (slot-definition-boundp-function slotd))))
39
40 (defmethod (setf slot-accessor-function) (function
41 (slotd effective-slot-definition) type)
42 (ecase type
43 (reader (setf (slot-definition-reader-function slotd) function))
44 (writer (setf (slot-definition-writer-function slotd) function))
45 (boundp (setf (slot-definition-boundp-function slotd) function))))
46
47 (defconstant *slotd-reader-function-std-p* 1)
48 (defconstant *slotd-writer-function-std-p* 2)
49 (defconstant *slotd-boundp-function-std-p* 4)
50 (defconstant *slotd-all-function-std-p* 7)
51
52 (defmethod slot-accessor-std-p ((slotd effective-slot-definition) type)
53 (let ((flags (slot-value slotd 'accessor-flags)))
54 (declare (type fixnum flags))
55 (if (eq type 'all)
56 (eql *slotd-all-function-std-p* flags)
57 (let ((mask (ecase type
58 (reader *slotd-reader-function-std-p*)
59 (writer *slotd-writer-function-std-p*)
60 (boundp *slotd-boundp-function-std-p*))))
61 (declare (type fixnum mask))
62 (not (zerop (the fixnum (logand mask flags))))))))
63
64 (defmethod (setf slot-accessor-std-p) (value (slotd effective-slot-definition) type)
65 (let ((mask (ecase type
66 (reader *slotd-reader-function-std-p*)
67 (writer *slotd-writer-function-std-p*)
68 (boundp *slotd-boundp-function-std-p*)))
69 (flags (slot-value slotd 'accessor-flags)))
70 (declare (type fixnum mask flags))
71 (setf (slot-value slotd 'accessor-flags)
72 (if value
73 (the fixnum (logior mask flags))
74 (the fixnum (logand (the fixnum (lognot mask)) flags)))))
75 value)
76
77 (defmethod initialize-internal-slot-functions ((slotd effective-slot-definition))
78 (let* ((name (slot-value slotd 'name))
79 (class (slot-value slotd 'class)))
80 (let ((table (or (gethash name *name->class->slotd-table*)
81 (setf (gethash name *name->class->slotd-table*)
82 (make-hash-table :test 'eq :size 5)))))
83 (setf (gethash class table) slotd))
84 (dolist (type '(reader writer boundp))
85 (let* ((gf-name (ecase type
86 (reader 'slot-value-using-class)
87 (writer '(setf slot-value-using-class))
88 (boundp 'slot-boundp-using-class)))
89 (gf (gdefinition gf-name)))
90 (compute-slot-accessor-info slotd type gf)))
91 (initialize-internal-slot-gfs name)))
92
93 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition) type gf)
94 (let* ((name (slot-value slotd 'name))
95 (class (slot-value slotd 'class))
96 (old-slotd (find-slot-definition class name))
97 (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
98 (multiple-value-bind (function std-p)
99 (if (eq *boot-state* 'complete)
100 (get-accessor-method-function gf type class slotd)
101 (get-optimized-std-accessor-method-function class slotd type))
102 #+kcl (si:turbo-closure function)
103 (setf (slot-accessor-std-p slotd type) std-p)
104 (setf (slot-accessor-function slotd type) function))
105 (when (and old-slotd (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
106 (push (cons class name) *pv-table-cache-update-info*))))
107
108 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
109 :instance)
110
111
112
113 (defmethod shared-initialize :after ((object documentation-mixin)
114 slot-names
115 &key (documentation nil documentation-p))
116 (declare (ignore slot-names))
117 (when documentation-p
118 (setf (plist-value object 'documentation) documentation)))
119
120 (defmethod documentation (object doc-type)
121 (declare (ignore object doc-type))
122 nil)
123
124 (defmethod (setf documentation) (new-value object doc-type)
125 (declare (ignore new-value doc-type))
126 (error "Can't change the documentation of ~S." object))
127
128 (defmethod documentation ((object documentation-mixin) doc-type)
129 (declare (ignore doc-type))
130 (plist-value object 'documentation))
131
132 (defmethod (setf documentation) (new-value (object documentation-mixin) doc-type)
133 (declare (ignore doc-type))
134 (setf (plist-value object 'documentation) new-value))
135
136
137 (defmethod documentation ((slotd standard-slot-definition) doc-type)
138 (declare (ignore doc-type))
139 (slot-value slotd 'documentation))
140
141 (defmethod (setf documentation) (new-value (slotd standard-slot-definition) doc-type)
142 (declare (ignore doc-type))
143 (setf (slot-value slotd 'documentation) new-value))
144
145
146 ;;;
147 ;;; Various class accessors that are a little more complicated than can be
148 ;;; done with automatically generated reader methods.
149 ;;;
150 (defmethod class-finalized-p ((class pcl-class))
151 (with-slots (wrapper) class
152 (not (null wrapper))))
153
154 (defmethod class-prototype ((class std-class))
155 (with-slots (prototype) class
156 (or prototype (setq prototype (allocate-instance class)))))
157
158 (defmethod class-prototype ((class structure-class))
159 (with-slots (prototype wrapper defstruct-constructor) class
160 (or prototype
161 (setq prototype
162 (if #-new-kcl-wrapper defstruct-constructor #+new-kcl-wrapper nil
163 (allocate-instance class)
164 (allocate-standard-instance wrapper))))))
165
166 (defmethod class-direct-default-initargs ((class slot-class))
167 (plist-value class 'direct-default-initargs))
168
169 (defmethod class-default-initargs ((class slot-class))
170 (plist-value class 'default-initargs))
171
172 (defmethod class-constructors ((class slot-class))
173 (plist-value class 'constructors))
174
175 (defmethod class-slot-cells ((class std-class))
176 (plist-value class 'class-slot-cells))
177
178
179 ;;;
180 ;;; Class accessors that are even a little bit more complicated than those
181 ;;; above. These have a protocol for updating them, we must implement that
182 ;;; protocol.
183 ;;;
184
185 ;;;
186 ;;; Maintaining the direct subclasses backpointers. The update methods are
187 ;;; here, the values are read by an automatically generated reader method.
188 ;;;
189 (defmethod add-direct-subclass ((class class) (subclass class))
190 (with-slots (direct-subclasses) class
191 (pushnew subclass direct-subclasses)
192 subclass))
193
194 (defmethod remove-direct-subclass ((class class) (subclass class))
195 (with-slots (direct-subclasses) class
196 (setq direct-subclasses (remove subclass direct-subclasses))
197 subclass))
198
199 ;;;
200 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
201 ;;;
202 ;;; There are four generic functions involved, each has one method for the
203 ;;; class case and another method for the damned EQL specializers. All of
204 ;;; these are specified methods and appear in their specified place in the
205 ;;; class graph.
206 ;;;
207 ;;; ADD-DIRECT-METHOD
208 ;;; REMOVE-DIRECT-METHOD
209 ;;; SPECIALIZER-DIRECT-METHODS
210 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
211 ;;;
212 ;;; In each case, we maintain one value which is a cons. The car is the list
213 ;;; methods. The cdr is a list of the generic functions. The cdr is always
214 ;;; computed lazily.
215 ;;;
216
217 (defmethod add-direct-method ((specializer class) (method method))
218 (with-slots (direct-methods) specializer
219 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
220 (cdr direct-methods) ()))
221 method)
222
223 (defmethod remove-direct-method ((specializer class) (method method))
224 (with-slots (direct-methods) specializer
225 (setf (car direct-methods) (remove method (car direct-methods))
226 (cdr direct-methods) ()))
227 method)
228
229 (defmethod specializer-direct-methods ((specializer class))
230 (with-slots (direct-methods) specializer
231 (car direct-methods)))
232
233 (defmethod specializer-direct-generic-functions ((specializer class))
234 (with-slots (direct-methods) specializer
235 (or (cdr direct-methods)
236 (setf (cdr direct-methods)
237 (gathering1 (collecting-once)
238 (dolist (m (car direct-methods))
239 (gather1 (method-generic-function m))))))))
240
241
242
243 ;;;
244 ;;; This hash table is used to store the direct methods and direct generic
245 ;;; functions of EQL specializers. Each value in the table is the cons.
246 ;;;
247 (defvar *eql-specializer-methods* (make-hash-table :test #'eql))
248 (defvar *class-eq-specializer-methods* (make-hash-table :test #'eq))
249
250 (defmethod specializer-method-table ((specializer eql-specializer))
251 *eql-specializer-methods*)
252
253 (defmethod specializer-method-table ((specializer class-eq-specializer))
254 *class-eq-specializer-methods*)
255
256 (defmethod add-direct-method ((specializer specializer-with-object) (method method))
257 (let* ((object (specializer-object specializer))
258 (table (specializer-method-table specializer))
259 (entry (gethash object table)))
260 (unless entry
261 (setq entry
262 (setf (gethash object table)
263 (cons nil nil))))
264 (setf (car entry) (adjoin method (car entry))
265 (cdr entry) ())
266 method))
267
268 (defmethod remove-direct-method ((specializer specializer-with-object) (method method))
269 (let* ((object (specializer-object specializer))
270 (entry (gethash object (specializer-method-table specializer))))
271 (when entry
272 (setf (car entry) (remove method (car entry))
273 (cdr entry) ()))
274 method))
275
276 (defmethod specializer-direct-methods ((specializer specializer-with-object))
277 (car (gethash (specializer-object specializer)
278 (specializer-method-table specializer))))
279
280 (defmethod specializer-direct-generic-functions ((specializer specializer-with-object))
281 (let* ((object (specializer-object specializer))
282 (entry (gethash object (specializer-method-table specializer))))
283 (when entry
284 (or (cdr entry)
285 (setf (cdr entry)
286 (gathering1 (collecting-once)
287 (dolist (m (car entry))
288 (gather1 (method-generic-function m)))))))))
289
290 (defun map-specializers (function)
291 (map-all-classes #'(lambda (class)
292 (funcall function (class-eq-specializer class))
293 (funcall function class)))
294 (maphash #'(lambda (object methods)
295 (declare (ignore methods))
296 (intern-eql-specializer object))
297 *eql-specializer-methods*)
298 (maphash #'(lambda (object specl)
299 (declare (ignore object))
300 (funcall function specl))
301 *eql-specializer-table*)
302 nil)
303
304 (defun map-all-generic-functions (function)
305 (let ((all-generic-functions (make-hash-table :test 'eq)))
306 (map-specializers #'(lambda (specl)
307 (dolist (gf (specializer-direct-generic-functions specl))
308 (unless (gethash gf all-generic-functions)
309 (setf (gethash gf all-generic-functions) t)
310 (funcall function gf))))))
311 nil)
312
313 (defmethod shared-initialize :after ((specl class-eq-specializer) slot-names &key)
314 (declare (ignore slot-names))
315 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
316
317 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
318 (declare (ignore slot-names))
319 (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
320
321
322
323 (defun real-load-defclass (name metaclass-name supers slots other accessors)
324 (do-standard-defsetfs-for-defclass accessors) ;***
325 (let ((res (apply #'ensure-class name :metaclass metaclass-name
326 :direct-superclasses supers
327 :direct-slots slots
328 :definition-source `((defclass ,name)
329 ,(load-truename))
330 other)))
331 ;; Defclass of a class with a forward-referenced superclass does not
332 ;; have a wrapper. RES is the incomplete PCL class. The Lisp class
333 ;; does not yet exist. Maybe should return NIL in that case as RES
334 ;; is not useful to the user?
335 #+cmu17 (and (class-wrapper res)(kernel:layout-class (class-wrapper res)))
336 #-cmu17 res))
337
338 (setf (gdefinition 'load-defclass) #'real-load-defclass)
339
340 (defun ensure-class (name &rest all)
341 (apply #'ensure-class-using-class name (find-class name nil) all))
342
343 (defmethod ensure-class-using-class (name (class null) &rest args &key)
344 (multiple-value-bind (meta initargs)
345 (ensure-class-values class args)
346 (inform-type-system-about-class (class-prototype meta) name);***
347 (setf class (apply #'make-instance meta :name name initargs)
348 (find-class name) class)
349 (inform-type-system-about-class class name) ;***
350 class))
351
352 (defmethod ensure-class-using-class (name (class pcl-class) &rest args &key)
353 (multiple-value-bind (meta initargs)
354 (ensure-class-values class args)
355 (unless (eq (class-of class) meta) (change-class class meta))
356 (apply #'reinitialize-instance class initargs)
357 (setf (find-class name) class)
358 (inform-type-system-about-class class name) ;***
359 class))
360
361 (defmethod class-predicate-name ((class t))
362 'function-returning-nil)
363
364 (defun ensure-class-values (class args)
365 (let* ((initargs (copy-list args))
366 (unsupplied (list 1))
367 (supplied-meta (getf initargs :metaclass unsupplied))
368 (supplied-supers (getf initargs :direct-superclasses unsupplied))
369 (supplied-slots (getf initargs :direct-slots unsupplied))
370 (meta
371 (cond ((neq supplied-meta unsupplied)
372 (find-class supplied-meta))
373 ((or (null class)
374 (forward-referenced-class-p class))
375 *the-class-standard-class*)
376 (t
377 (class-of class)))))
378 (flet ((fix-super (s)
379 (cond ((classp s) s)
380 ((not (legal-class-name-p s))
381 (error "~S is not a class or a legal class name." s))
382 #||notyet #+cmu17
383 ((eq s 'lisp:standard-class) ; map from lisp to pcl
384 *the-class-standard-class*)
385 ||#
386 (t
387 (or (find-class s nil)
388 (setf (find-class s)
389 (make-instance 'forward-referenced-class
390 :name s)))))))
391 (loop (unless (remf initargs :metaclass) (return)))
392 (loop (unless (remf initargs :direct-superclasses) (return)))
393 (loop (unless (remf initargs :direct-slots) (return)))
394 (values meta
395 (list* :direct-superclasses
396 (and (neq supplied-supers unsupplied)
397 (mapcar #'fix-super supplied-supers))
398 :direct-slots
399 (and (neq supplied-slots unsupplied) supplied-slots)
400 initargs)))))
401
402
403 ;;;
404 ;;;
405 ;;;
406 #|| ; since it doesn't do anything
407 (defmethod shared-initialize :before ((class std-class)
408 slot-names
409 &key direct-superclasses)
410 (declare (ignore slot-names))
411 ;; *** error checking
412 )
413 ||#
414
415 (defmethod shared-initialize :after
416 ((class std-class)
417 slot-names
418 &key (direct-superclasses nil direct-superclasses-p)
419 (direct-slots nil direct-slots-p)
420 (direct-default-initargs nil direct-default-initargs-p)
421 (predicate-name nil predicate-name-p))
422 (declare (ignore slot-names))
423 (cond (direct-superclasses-p
424 (setq direct-superclasses
425 (or direct-superclasses
426 (list (if (funcallable-standard-class-p class)
427 *the-class-funcallable-standard-object*
428 *the-class-standard-object*))))
429 (dolist (superclass direct-superclasses)
430 (unless (validate-superclass class superclass)
431 (error "The class ~S was specified as a~%
432 super-class of the class ~S;~%~
433 but the meta-classes ~S and~%~S are incompatible.~@
434 Define a method for ~S to avoid this error."
435 superclass class (class-of superclass) (class-of class)
436 'validate-superclass)))
437 (setf (slot-value class 'direct-superclasses) direct-superclasses))
438 (t
439 (setq direct-superclasses (slot-value class 'direct-superclasses))))
440 (setq direct-slots
441 (if direct-slots-p
442 (setf (slot-value class 'direct-slots)
443 (mapcar #'(lambda (pl) (make-direct-slotd class pl)) direct-slots))
444 (slot-value class 'direct-slots)))
445 (if direct-default-initargs-p
446 (setf (plist-value class 'direct-default-initargs) direct-default-initargs)
447 (setq direct-default-initargs (plist-value class 'direct-default-initargs)))
448 (setf (plist-value class 'class-slot-cells)
449 (gathering1 (collecting)
450 (dolist (dslotd direct-slots)
451 (when (eq (slot-definition-allocation dslotd) class)
452 (let ((initfunction (slot-definition-initfunction dslotd)))
453 (gather1 (cons (slot-definition-name dslotd)
454 (if initfunction
455 (funcall initfunction)
456 *slot-unbound*))))))))
457 (setq predicate-name (if predicate-name-p
458 (setf (slot-value class 'predicate-name)
459 (car predicate-name))
460 (or (slot-value class 'predicate-name)
461 (setf (slot-value class 'predicate-name)
462 (make-class-predicate-name (class-name class))))))
463 (add-direct-subclasses class direct-superclasses)
464 (update-class class nil)
465 (make-class-predicate class predicate-name)
466 (add-slot-accessors class direct-slots))
467
468 (defmethod shared-initialize :before ((class class) slot-names &key name)
469 (declare (ignore slot-names name))
470 (setf (slot-value class 'type) `(class ,class))
471 (setf (slot-value class 'class-eq-specializer)
472 (make-instance 'class-eq-specializer :class class)))
473
474 (defmethod reinitialize-instance :before ((class slot-class) &key)
475 (remove-direct-subclasses class (class-direct-superclasses class))
476 (remove-slot-accessors class (class-direct-slots class)))
477
478 (defmethod reinitialize-instance :after ((class slot-class)
479 &rest initargs
480 &key)
481 (map-dependents class
482 #'(lambda (dependent)
483 (apply #'update-dependent class dependent initargs))))
484
485 (defmethod shared-initialize :after
486 ((class structure-class)
487 slot-names
488 &key (direct-superclasses nil direct-superclasses-p)
489 (direct-slots nil direct-slots-p)
490 direct-default-initargs
491 (predicate-name nil predicate-name-p))
492 (declare (ignore slot-names direct-default-initargs))
493 (if direct-superclasses-p
494 (setf (slot-value class 'direct-superclasses)
495 (or direct-superclasses
496 (setq direct-superclasses
497 (and (not (eq (class-name class) 'structure-object))
498 (list *the-class-structure-object*)))))
499 (setq direct-superclasses (slot-value class 'direct-superclasses)))
500 (let* ((name (class-name class))
501 (from-defclass-p (slot-value class 'from-defclass-p))
502 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
503 (if direct-slots-p
504 (setf (slot-value class 'direct-slots)
505 (setq direct-slots
506 (mapcar #'(lambda (pl)
507 (when defstruct-p
508 (let* ((slot-name (getf pl :name))
509 (acc-name (format nil "~s structure class ~a"
510 name slot-name))
511 (accessor (intern acc-name)))
512 (setq pl (list* :defstruct-accessor-symbol accessor
513 pl))))
514 (make-direct-slotd class pl))
515 direct-slots)))
516 (setq direct-slots (slot-value class 'direct-slots)))
517 (when defstruct-p
518 (let* ((include (car (slot-value class 'direct-superclasses)))
519 (conc-name (intern (format nil "~s structure class " name)))
520 (constructor (intern (format nil "~a constructor" conc-name)))
521 (defstruct `(defstruct (,name
522 ,@(when include
523 `((:include ,(class-name include))))
524 (:print-function print-std-instance)
525 (:predicate nil)
526 (:conc-name ,conc-name)
527 (:constructor ,constructor ()))
528 ,@(mapcar #'(lambda (slot)
529 `(,(slot-definition-name slot)
530 *slot-unbound*))
531 direct-slots)))
532 (reader-names (mapcar #'(lambda (slotd)
533 (intern (format nil "~A~A reader" conc-name
534 (slot-definition-name slotd))))
535 direct-slots))
536 (writer-names (mapcar #'(lambda (slotd)
537 (intern (format nil "~A~A writer" conc-name
538 (slot-definition-name slotd))))
539 direct-slots))
540 (readers-init
541 (mapcar #'(lambda (slotd reader-name)
542 (let ((accessor
543 (slot-definition-defstruct-accessor-symbol slotd)))
544 `(defun ,reader-name (obj)
545 (declare (type ,name obj))
546 (,accessor obj))))
547 direct-slots reader-names))
548 (writers-init
549 (mapcar #'(lambda (slotd writer-name)
550 (let ((accessor
551 (slot-definition-defstruct-accessor-symbol slotd)))
552 `(defun ,writer-name (nv obj)
553 (declare (type ,name obj))
554 (setf (,accessor obj) nv))))
555 direct-slots writer-names))
556 (defstruct-form
557 `(progn
558 ,defstruct
559 ,@readers-init ,@writers-init
560 (declare-structure ',name nil nil))))
561 (unless (structure-type-p name) (eval defstruct-form))
562 (mapc #'(lambda (dslotd reader-name writer-name)
563 (let* ((reader (gdefinition reader-name))
564 (writer (when (gboundp writer-name)
565 (gdefinition writer-name))))
566 (setf (slot-value dslotd 'internal-reader-function) reader)
567 (setf (slot-value dslotd 'internal-writer-function) writer)))
568 direct-slots reader-names writer-names)
569 (setf (slot-value class 'defstruct-form) defstruct-form)
570 (setf (slot-value class 'defstruct-constructor) constructor))))
571 (add-direct-subclasses class direct-superclasses)
572 (setf (slot-value class 'class-precedence-list)
573 (compute-class-precedence-list class))
574 (setf (slot-value class 'slots) (compute-slots class))
575 #-(or cmu17 new-kcl-wrapper)
576 (unless (slot-value class 'wrapper)
577 (setf (slot-value class 'wrapper) (make-wrapper 0 class)))
578 #+cmu17
579 (let ((lclass (lisp:find-class (class-name class))))
580 (setf (kernel:class-pcl-class lclass) class)
581 (setf (slot-value class 'wrapper) (kernel:class-layout lclass)))
582 #+new-kcl-wrapper
583 (let ((wrapper (get (class-name class) 'si::s-data)))
584 (setf (slot-value class 'wrapper) wrapper)
585 (setf (wrapper-class wrapper) class))
586 (update-pv-table-cache-info class)
587 (setq predicate-name (if predicate-name-p
588 (setf (slot-value class 'predicate-name)
589 (car predicate-name))
590 (or (slot-value class 'predicate-name)
591 (setf (slot-value class 'predicate-name)
592 (make-class-predicate-name (class-name class))))))
593 (make-class-predicate class predicate-name)
594 (add-slot-accessors class direct-slots))
595
596 (defmethod direct-slot-definition-class ((class structure-class) initargs)
597 (declare (ignore initargs))
598 (find-class 'structure-direct-slot-definition))
599
600 (defmethod finalize-inheritance ((class structure-class))
601 nil) ; always finalized
602
603 (defun add-slot-accessors (class dslotds)
604 (fix-slot-accessors class dslotds 'add))
605
606 (defun remove-slot-accessors (class dslotds)
607 (fix-slot-accessors class dslotds 'remove))
608
609 (defun fix-slot-accessors (class dslotds add/remove)
610 (flet ((fix (gfspec name r/w)
611 (let ((gf (ensure-generic-function gfspec)))
612 (case r/w
613 (r (if (eq add/remove 'add)
614 (add-reader-method class gf name)
615 (remove-reader-method class gf)))
616 (w (if (eq add/remove 'add)
617 (add-writer-method class gf name)
618 (remove-writer-method class gf)))))))
619 (dolist (dslotd dslotds)
620 (let ((slot-name (slot-definition-name dslotd)))
621 (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
622 (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
623
624
625 (defun add-direct-subclasses (class new)
626 (dolist (n new)
627 (unless (memq class (class-direct-subclasses class))
628 (add-direct-subclass n class))))
629
630 (defun remove-direct-subclasses (class new)
631 (let ((old (class-direct-superclasses class)))
632 (dolist (o (set-difference old new))
633 (remove-direct-subclass o class))))
634
635
636 ;;;
637 ;;;
638 ;;;
639 (defmethod finalize-inheritance ((class std-class))
640 (update-class class t))
641
642
643 (defun class-has-a-forward-referenced-superclass-p (class)
644 (or (forward-referenced-class-p class)
645 (some #'class-has-a-forward-referenced-superclass-p
646 (class-direct-superclasses class))))
647
648 ;;;
649 ;;; Called by :after shared-initialize whenever a class is initialized or
650 ;;; reinitialized. The class may or may not be finalized.
651 ;;;
652 (defun update-class (class finalizep)
653 (when (or finalizep (class-finalized-p class)
654 (not (class-has-a-forward-referenced-superclass-p class)))
655 (update-cpl class (compute-class-precedence-list class))
656 (update-slots class (compute-slots class))
657 (update-gfs-of-class class)
658 (update-inits class (compute-default-initargs class))
659 (update-make-instance-function-table class))
660 (unless finalizep
661 (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
662
663 (defun update-cpl (class cpl)
664 (if (class-finalized-p class)
665 (unless (equal (class-precedence-list class) cpl)
666 ;; Need to have the cpl setup before update-lisp-class-layout
667 ;; is called on CMUCL.
668 (setf (slot-value class 'class-precedence-list) cpl)
669 (force-cache-flushes class))
670 (setf (slot-value class 'class-precedence-list) cpl))
671 (update-class-can-precede-p cpl))
672
673 (defun update-class-can-precede-p (cpl)
674 (when cpl
675 (let ((first (car cpl)))
676 (dolist (c (cdr cpl))
677 (pushnew c (slot-value first 'can-precede-list))))
678 (update-class-can-precede-p (cdr cpl))))
679
680 (defun class-can-precede-p (class1 class2)
681 (member class2 (class-can-precede-list class1)))
682
683 (defun update-slots (class eslotds)
684 (let ((instance-slots ())
685 (class-slots ()))
686 (dolist (eslotd eslotds)
687 (let ((alloc (slot-definition-allocation eslotd)))
688 (cond ((eq alloc :instance) (push eslotd instance-slots))
689 ((classp alloc) (push eslotd class-slots)))))
690 ;;
691 ;; If there is a change in the shape of the instances then the
692 ;; old class is now obsolete.
693 ;;
694 (let* ((nlayout (mapcar #'slot-definition-name
695 (sort instance-slots #'< :key #'slot-definition-location)))
696 (nslots (length nlayout))
697 (nwrapper-class-slots (compute-class-slots class-slots))
698 (owrapper (class-wrapper class))
699 (olayout (and owrapper (wrapper-instance-slots-layout owrapper)))
700 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
701 (nwrapper
702 (cond ((null owrapper)
703 (make-wrapper nslots class))
704 ((and (equal nlayout olayout)
705 (not
706 (iterate ((o (list-elements owrapper-class-slots))
707 (n (list-elements nwrapper-class-slots)))
708 (unless (eq (car o) (car n)) (return t)))))
709 owrapper)
710 (t
711 ;;
712 ;; This will initialize the new wrapper to have the same
713 ;; state as the old wrapper. We will then have to change
714 ;; that. This may seem like wasted work (it is), but the
715 ;; spec requires that we call make-instances-obsolete.
716 ;;
717 (make-instances-obsolete class)
718 (class-wrapper class)))))
719
720 (with-slots (wrapper slots) class
721 #+new-kcl-wrapper
722 (setf (si::s-data-name nwrapper) (class-name class))
723 #+cmu17
724 (update-lisp-class-layout class nwrapper)
725 (setf slots eslotds
726 (wrapper-instance-slots-layout nwrapper) nlayout
727 (wrapper-class-slots nwrapper) nwrapper-class-slots
728 (wrapper-no-of-instance-slots nwrapper) nslots
729 wrapper nwrapper))
730
731 (unless (eq owrapper nwrapper)
732 (update-pv-table-cache-info class)))))
733
734 (defun compute-class-slots (eslotds)
735 (gathering1 (collecting)
736 (dolist (eslotd eslotds)
737 (gather1
738 (assoc (slot-definition-name eslotd)
739 (class-slot-cells (slot-definition-allocation eslotd)))))))
740
741 (defun compute-layout (cpl instance-eslotds)
742 (let* ((names
743 (gathering1 (collecting)
744 (dolist (eslotd instance-eslotds)
745 (when (eq (slot-definition-allocation eslotd) :instance)
746 (gather1 (slot-definition-name eslotd))))))
747 (order ()))
748 (labels ((rwalk (tail)
749 (when tail
750 (rwalk (cdr tail))
751 (dolist (ss (class-slots (car tail)))
752 (let ((n (slot-definition-name ss)))
753 (when (member n names)
754 (setq order (cons n order)
755 names (remove n names))))))))
756 (rwalk (if (slot-boundp (car cpl) 'slots)
757 cpl
758 (cdr cpl)))
759 (reverse (append names order)))))
760
761 (defun update-gfs-of-class (class)
762 (when (and (class-finalized-p class)
763 (let ((cpl (class-precedence-list class)))
764 (or (member *the-class-slot-class* cpl)
765 (member *the-class-standard-effective-slot-definition* cpl))))
766 (let ((gf-table (make-hash-table :test 'eq)))
767 (labels ((collect-gfs (class)
768 (dolist (gf (specializer-direct-generic-functions class))
769 (setf (gethash gf gf-table) t))
770 (mapc #'collect-gfs (class-direct-superclasses class))))
771 (collect-gfs class)
772 (maphash #'(lambda (gf ignore)
773 (declare (ignore ignore))
774 (update-gf-dfun class gf))
775 gf-table)))))
776
777 (defun update-inits (class inits)
778 (setf (plist-value class 'default-initargs) inits))
779
780
781 ;;;
782 ;;;
783 ;;;
784 (defmethod compute-default-initargs ((class slot-class))
785 (let ((cpl (class-precedence-list class))
786 (direct (class-direct-default-initargs class)))
787 (labels ((walk (tail)
788 (if (null tail)
789 nil
790 (let ((c (pop tail)))
791 (append (if (eq c class)
792 direct
793 (class-direct-default-initargs c))
794 (walk tail))))))
795 (let ((initargs (walk cpl)))
796 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))))
797
798
799 ;;;
800 ;;; Protocols for constructing direct and effective slot definitions.
801 ;;;
802 ;;;
803 ;;;
804 ;;;
805 (defmethod direct-slot-definition-class ((class std-class) initargs)
806 (declare (ignore initargs))
807 (find-class 'standard-direct-slot-definition))
808
809 (defun make-direct-slotd (class initargs)
810 (let ((initargs (list* :class class initargs)))
811 (apply #'make-instance (direct-slot-definition-class class initargs) initargs)))
812
813 ;;;
814 ;;;
815 ;;;
816 (defmethod compute-slots ((class std-class))
817 ;;
818 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
819 ;; for each different slot name we find in our superclasses. Each
820 ;; call receives the class and a list of the dslotds with that name.
821 ;; The list is in most-specific-first order.
822 ;;
823 (let ((name-dslotds-alist ()))
824 (dolist (c (class-precedence-list class))
825 (let ((dslotds (class-direct-slots c)))
826 (dolist (d dslotds)
827 (let* ((name (slot-definition-name d))
828 (entry (assq name name-dslotds-alist)))
829 (if entry
830 (push d (cdr entry))
831 (push (list name d) name-dslotds-alist))))))
832 (mapcar #'(lambda (direct)
833 (compute-effective-slot-definition class
834 (nreverse (cdr direct))))
835 name-dslotds-alist)))
836
837 (defmethod compute-slots :around ((class std-class))
838 (let ((eslotds (call-next-method))
839 (cpl (class-precedence-list class))
840 (instance-slots ())
841 (class-slots ()))
842 (dolist (eslotd eslotds)
843 (let ((alloc (slot-definition-allocation eslotd)))
844 (cond ((eq alloc :instance) (push eslotd instance-slots))
845 ((classp alloc) (push eslotd class-slots)))))
846 (let ((nlayout (compute-layout cpl instance-slots)))
847 (dolist (eslotd instance-slots)
848 (setf (slot-definition-location eslotd)
849 (position (slot-definition-name eslotd) nlayout))))
850 (dolist (eslotd class-slots)
851 (setf (slot-definition-location eslotd)
852 (assoc (slot-definition-name eslotd)
853 (class-slot-cells (slot-definition-allocation eslotd)))))
854 (mapc #'initialize-internal-slot-functions eslotds)
855 eslotds))
856
857 (defmethod compute-slots ((class structure-class))
858 (mapcan #'(lambda (superclass)
859 (mapcar #'(lambda (dslotd)
860 (compute-effective-slot-definition class
861 (list dslotd)))
862 (class-direct-slots superclass)))
863 (reverse (slot-value class 'class-precedence-list))))
864
865 (defmethod compute-slots :around ((class structure-class))
866 (let ((eslotds (call-next-method)))
867 (mapc #'initialize-internal-slot-functions eslotds)
868 eslotds))
869
870 (defmethod compute-effective-slot-definition ((class slot-class) dslotds)
871 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
872 (class (effective-slot-definition-class class initargs)))
873 (apply #'make-instance class initargs)))
874
875 (defmethod effective-slot-definition-class ((class std-class) initargs)
876 (declare (ignore initargs))
877 (find-class 'standard-effective-slot-definition))
878
879 (defmethod effective-slot-definition-class ((class structure-class) initargs)
880 (declare (ignore initargs))
881 (find-class 'structure-effective-slot-definition))
882
883 (defmethod compute-effective-slot-definition-initargs
884 ((class slot-class) direct-slotds)
885 (let* ((name nil)
886 (initfunction nil)
887 (initform nil)
888 (initargs nil)
889 (allocation nil)
890 (type t)
891 (namep nil)
892 (initp nil)
893 (allocp nil))
894
895 (dolist (slotd direct-slotds)
896 (when slotd
897 (unless namep
898 (setq name (slot-definition-name slotd)
899 namep t))
900 (unless initp
901 (when (slot-definition-initfunction slotd)
902 (setq initform (slot-definition-initform slotd)
903 initfunction (slot-definition-initfunction slotd)
904 initp t)))
905 (unless allocp
906 (setq allocation (slot-definition-allocation slotd)
907 allocp t))
908 (setq initargs (append (slot-definition-initargs slotd) initargs))
909 (let ((slotd-type (slot-definition-type slotd)))
910 (setq type (cond ((eq type 't) slotd-type)
911 ((*subtypep type slotd-type) type)
912 (t `(and ,type ,slotd-type)))))))
913 (list :name name
914 :initform initform
915 :initfunction initfunction
916 :initargs initargs
917 :allocation allocation
918 :type type
919 :class class)))
920
921 (defmethod compute-effective-slot-definition-initargs :around
922 ((class structure-class) direct-slotds)
923 (let ((slotd (car direct-slotds)))
924 (list* :defstruct-accessor-symbol (slot-definition-defstruct-accessor-symbol slotd)
925 :internal-reader-function (slot-definition-internal-reader-function slotd)
926 :internal-writer-function (slot-definition-internal-writer-function slotd)
927 (call-next-method))))
928
929 ;;;
930 ;;; NOTE: For bootstrapping considerations, these can't use make-instance
931 ;;; to make the method object. They have to use make-a-method which
932 ;;; is a specially bootstrapped mechanism for making standard methods.
933 ;;;
934 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
935 (declare (ignore direct-slot initargs))
936 (find-class 'standard-reader-method))
937
938 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
939 (add-method generic-function
940 (make-a-method 'standard-reader-method
941 ()
942 (list (or (class-name class) 'object))
943 (list class)
944 (make-reader-method-function class slot-name)
945 "automatically generated reader method"
946 slot-name)))
947
948 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
949 (declare (ignore direct-slot initargs))
950 (find-class 'standard-writer-method))
951
952 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
953 (add-method generic-function
954 (make-a-method 'standard-writer-method
955 ()
956 (list 'new-value (or (class-name class) 'object))
957 (list *the-class-t* class)
958 (make-writer-method-function class slot-name)
959 "automatically generated writer method"
960 slot-name)))
961
962 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
963 (add-method generic-function
964 (make-a-method 'standard-boundp-method
965 ()
966 (list (or (class-name class) 'object))
967 (list class)
968 (make-boundp-method-function class slot-name)
969 "automatically generated boundp method"
970 slot-name)))
971
972 (defmethod remove-reader-method ((class slot-class) generic-function)
973 (let ((method (get-method generic-function () (list class) nil)))
974 (when method (remove-method generic-function method))))
975
976 (defmethod remove-writer-method ((class slot-class) generic-function)
977 (let ((method
978 (get-method generic-function () (list *the-class-t* class) nil)))
979 (when method (remove-method generic-function method))))
980
981 (defmethod remove-boundp-method ((class slot-class) generic-function)
982 (let ((method (get-method generic-function () (list class) nil)))
983 (when method (remove-method generic-function method))))
984
985
986 ;;;
987 ;;; make-reader-method-function and make-write-method function are NOT part of
988 ;;; the standard protocol. They are however useful, PCL makes uses makes use
989 ;;; of them internally and documents them for PCL users.
990 ;;;
991 ;;; *** This needs work to make type testing by the writer functions which
992 ;;; *** do type testing faster. The idea would be to have one constructor
993 ;;; *** for each possible type test. In order to do this it would be nice
994 ;;; *** to have help from inform-type-system-about-class and friends.
995 ;;;
996 ;;; *** There is a subtle bug here which is going to have to be fixed.
997 ;;; *** Namely, the simplistic use of the template has to be fixed. We
998 ;;; *** have to give the optimize-slot-value method the user might have
999 ;;; *** defined for this metclass a chance to run.
1000 ;;;
1001 (defmethod make-reader-method-function ((class slot-class) slot-name)
1002 (make-std-reader-method-function (class-name class) slot-name))
1003
1004 (defmethod make-writer-method-function ((class slot-class) slot-name)
1005 (make-std-writer-method-function (class-name class) slot-name))
1006
1007 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1008 (make-std-boundp-method-function (class-name class) slot-name))
1009
1010
1011 ;;;; inform-type-system-about-class
1012 ;;;; make-type-predicate
1013 ;;;
1014 ;;; These are NOT part of the standard protocol. They are internal mechanism
1015 ;;; which PCL uses to *try* and tell the type system about class definitions.
1016 ;;; In a more fully integrated implementation of CLOS, the type system would
1017 ;;; know about class objects and class names in a more fundamental way and
1018 ;;; the mechanism used to inform the type system about new classes would be
1019 ;;; different.
1020 ;;;
1021 (defmethod inform-type-system-about-class ((class std-class) name)
1022 (inform-type-system-about-std-class name))
1023
1024
1025 (defmethod compatible-meta-class-change-p (class proto-new-class)
1026 (eq (class-of class) (class-of proto-new-class)))
1027
1028 (defmethod validate-superclass ((class class) (new-super class))
1029 (or (eq new-super *the-class-t*)
1030 (eq (class-of class) (class-of new-super))))
1031
1032 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1033 (let ((new-super-meta-class (class-of new-super)))
1034 (or (eq new-super-meta-class *the-class-std-class*)
1035 (eq (class-of class) new-super-meta-class))))
1036
1037
1038 ;;;
1039 ;;;
1040 ;;;
1041 (defun force-cache-flushes (class)
1042 (let* ((owrapper (class-wrapper class))
1043 (state (wrapper-state owrapper)))
1044 ;;
1045 ;; We only need to do something if the state is still T. If the
1046 ;; state isn't T, it will be FLUSH or OBSOLETE, and both of those
1047 ;; will already be doing what we want. In particular, we must be
1048 ;; sure we never change an OBSOLETE into a FLUSH since OBSOLETE
1049 ;; means do what FLUSH does and then some.
1050 ;;
1051 (when (eq state 't)
1052 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1053 class)))
1054 (setf (wrapper-instance-slots-layout nwrapper)
1055 (wrapper-instance-slots-layout owrapper))
1056 (setf (wrapper-class-slots nwrapper)
1057 (wrapper-class-slots owrapper))
1058 (without-interrupts
1059 #+cmu17
1060 (update-lisp-class-layout class nwrapper)
1061 (setf (slot-value class 'wrapper) nwrapper)
1062 (invalidate-wrapper owrapper ':flush nwrapper))))))
1063
1064 (defun flush-cache-trap (owrapper nwrapper instance)
1065 (declare (ignore owrapper))
1066 (set-wrapper instance nwrapper))
1067
1068
1069
1070 ;;;
1071 ;;; make-instances-obsolete can be called by user code. It will cause the
1072 ;;; next access to the instance (as defined in 88-002R) to trap through the
1073 ;;; update-instance-for-redefined-class mechanism.
1074 ;;;
1075 (defmethod make-instances-obsolete ((class std-class))
1076 (let* ((owrapper (class-wrapper class))
1077 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1078 class)))
1079 (setf (wrapper-instance-slots-layout nwrapper)
1080 (wrapper-instance-slots-layout owrapper))
1081 (setf (wrapper-class-slots nwrapper)
1082 (wrapper-class-slots owrapper))
1083 (without-interrupts
1084 #+cmu17
1085 (update-lisp-class-layout class nwrapper)
1086 (setf (slot-value class 'wrapper) nwrapper)
1087 (invalidate-wrapper owrapper ':obsolete nwrapper)
1088 class)))
1089
1090 (defmethod make-instances-obsolete ((class symbol))
1091 (make-instances-obsolete (find-class class)))
1092
1093
1094 ;;;
1095 ;;; obsolete-instance-trap is the internal trap that is called when we see
1096 ;;; an obsolete instance. The times when it is called are:
1097 ;;; - when the instance is involved in method lookup
1098 ;;; - when attempting to access a slot of an instance
1099 ;;;
1100 ;;; It is not called by class-of, wrapper-of, or any of the low-level instance
1101 ;;; access macros.
1102 ;;;
1103 ;;; Of course these times when it is called are an internal implementation
1104 ;;; detail of PCL and are not part of the documented description of when the
1105 ;;; obsolete instance update happens. The documented description is as it
1106 ;;; appears in 88-002R.
1107 ;;;
1108 ;;; This has to return the new wrapper, so it counts on all the methods on
1109 ;;; obsolete-instance-trap-internal to return the new wrapper. It also does
1110 ;;; a little internal error checking to make sure that the traps are only
1111 ;;; happening when they should, and that the trap methods are computing
1112 ;;; apropriate new wrappers.
1113 ;;;
1114
1115 ;;; obsolete-instance-trap might be called on structure instances
1116 ;;; after a structure is redefined. In most cases, obsolete-instance-trap
1117 ;;; will not be able to fix the old instance, so it must signal an
1118 ;;; error. The hard part of this is that the error system and debugger
1119 ;;; might cause obsolete-instance-trap to be called again, so in that
1120 ;;; case, we have to return some reasonable wrapper, instead.
1121
1122 (defvar *in-obsolete-instance-trap* nil)
1123 (defvar *the-wrapper-of-structure-object*
1124 (class-wrapper (find-class 'structure-object)))
1125
1126 #+cmu17
1127 (define-condition obsolete-structure (error)
1128 ((datum :reader obsolete-structure-datum :initarg :datum))
1129 (:report
1130 (lambda (condition stream)
1131 ;; Don't try to print the structure, since it probably
1132 ;; won't work.
1133 (format stream "Obsolete structure error in ~S:~@
1134 For a structure of type: ~S"
1135 (conditions::condition-function-name condition)
1136 (type-of (obsolete-structure-datum condition))))))
1137
1138 (defun obsolete-instance-trap (owrapper nwrapper instance)
1139 (if (not #-(or cmu17 new-kcl-wrapper)
1140 (or (std-instance-p instance) (fsc-instance-p instance))
1141 #+cmu17
1142 (pcl-instance-p instance)
1143 #+new-kcl-wrapper
1144 nil)
1145 (if *in-obsolete-instance-trap*
1146 *the-wrapper-of-structure-object*
1147 (let ((*in-obsolete-instance-trap* t))
1148 #-cmu17
1149 (error "The structure ~S is obsolete." instance)
1150 #+cmu17
1151 (error 'obsolete-structure :datum instance)))
1152 (let* ((class (wrapper-class* nwrapper))
1153 (copy (allocate-instance class)) ;??? allocate-instance ???
1154 (olayout (wrapper-instance-slots-layout owrapper))
1155 (nlayout (wrapper-instance-slots-layout nwrapper))
1156 (oslots (get-slots instance))
1157 (nslots (get-slots copy))
1158 (oclass-slots (wrapper-class-slots owrapper))
1159 (added ())
1160 (discarded ())
1161 (plist ()))
1162 ;; local --> local transfer
1163 ;; local --> shared discard
1164 ;; local --> -- discard
1165 ;; shared --> local transfer
1166 ;; shared --> shared discard
1167 ;; shared --> -- discard
1168 ;; -- --> local add
1169 ;; -- --> shared --
1170 ;;
1171 ;; Go through all the old local slots.
1172 ;;
1173 (iterate ((name (list-elements olayout))
1174 (opos (interval :from 0)))
1175 (let ((npos (posq name nlayout)))
1176 (if npos
1177 (setf (instance-ref nslots npos) (instance-ref oslots opos))
1178 (progn
1179 (push name discarded)
1180 (unless (eq (instance-ref oslots opos) *slot-unbound*)
1181 (setf (getf plist name) (instance-ref oslots opos)))))))
1182 ;;
1183 ;; Go through all the old shared slots.
1184 ;;
1185 (iterate ((oclass-slot-and-val (list-elements oclass-slots)))
1186 (let ((name (car oclass-slot-and-val))
1187 (val (cdr oclass-slot-and-val)))
1188 (let ((npos (posq name nlayout)))
1189 (if npos
1190 (setf (instance-ref nslots npos) (cdr oclass-slot-and-val))
1191 (progn (push name discarded)
1192 (unless (eq val *slot-unbound*)
1193 (setf (getf plist name) val)))))))
1194 ;;
1195 ;; Go through all the new local slots to compute the added slots.
1196 ;;
1197 (dolist (nlocal nlayout)
1198 (unless (or (memq nlocal olayout)
1199 (assq nlocal oclass-slots))
1200 (push nlocal added)))
1201
1202 (swap-wrappers-and-slots instance copy)
1203
1204 (update-instance-for-redefined-class instance
1205 added
1206 discarded
1207 plist)
1208 nwrapper)))
1209
1210
1211 ;;;
1212 ;;;
1213 ;;;
1214 (defmacro copy-instance-internal (instance)
1215 `(#+new-kcl-wrapper if #-new-kcl-wrapper progn
1216 #+new-kcl-wrapper (not (std-instance-p ,instance))
1217 (let* ((class (class-of instance))
1218 (copy (allocate-instance class)))
1219 (if (std-instance-p ,instance)
1220 (setf (std-instance-slots ,instance) (std-instance-slots ,instance))
1221 (setf (fsc-instance-slots ,instance) (fsc-instance-slots ,instance)))
1222 copy)
1223 #+new-kcl-wrapper
1224 (copy-structure-header ,instance)))
1225
1226 (defun change-class-internal (instance new-class)
1227 (let* ((old-class (class-of instance))
1228 (copy (allocate-instance new-class))
1229 (new-wrapper (get-wrapper copy))
1230 (old-wrapper (class-wrapper old-class))
1231 (old-layout (wrapper-instance-slots-layout old-wrapper))
1232 (new-layout (wrapper-instance-slots-layout new-wrapper))
1233 (old-slots (get-slots instance))
1234 (new-slots (get-slots copy))
1235 (old-class-slots (wrapper-class-slots old-wrapper)))
1236
1237 ;;
1238 ;; "The values of local slots specified by both the class Cto and
1239 ;; Cfrom are retained. If such a local slot was unbound, it remains
1240 ;; unbound."
1241 ;;
1242 (iterate ((new-slot (list-elements new-layout))
1243 (new-position (interval :from 0)))
1244 (let ((old-position (posq new-slot old-layout)))
1245 (when old-position
1246 (setf (instance-ref new-slots new-position)
1247 (instance-ref old-slots old-position)))))
1248
1249 ;;
1250 ;; "The values of slots specified as shared in the class Cfrom and
1251 ;; as local in the class Cto are retained."
1252 ;;
1253 (iterate ((slot-and-val (list-elements old-class-slots)))
1254 (let ((position (posq (car slot-and-val) new-layout)))
1255 (when position
1256 (setf (instance-ref new-slots position) (cdr slot-and-val)))))
1257
1258 ;; Make the copy point to the old instance's storage, and make the
1259 ;; old instance point to the new storage.
1260 (swap-wrappers-and-slots instance copy)
1261
1262 (update-instance-for-different-class copy instance)
1263 instance))
1264
1265 (defmethod change-class ((instance standard-object)
1266 (new-class standard-class))
1267 (change-class-internal instance new-class))
1268
1269 (defmethod change-class ((instance funcallable-standard-object)
1270 (new-class funcallable-standard-class))
1271 (change-class-internal instance new-class))
1272
1273 (defmethod change-class ((instance standard-object)
1274 (new-class funcallable-standard-class))
1275 (error "Can't change the class of ~S to ~S~@
1276 because it isn't already an instance with metaclass ~S."
1277 instance new-class 'standard-class))
1278
1279 (defmethod change-class ((instance funcallable-standard-object)
1280 (new-class standard-class))
1281 (error "Can't change the class of ~S to ~S~@
1282 because it isn't already an instance with metaclass ~S."
1283 instance new-class 'funcallable-standard-class))
1284
1285 (defmethod change-class ((instance t) (new-class-name symbol))
1286 (change-class instance (find-class new-class-name)))
1287
1288
1289
1290 ;;;
1291 ;;; The metaclass BUILT-IN-CLASS
1292 ;;;
1293 ;;; This metaclass is something of a weird creature. By this point, all
1294 ;;; instances of it which will exist have been created, and no instance
1295 ;;; is ever created by calling MAKE-INSTANCE.
1296 ;;;
1297 ;;; But, there are other parts of the protcol we must follow and those
1298 ;;; definitions appear here.
1299 ;;;
1300 (defmethod shared-initialize :before
1301 ((class built-in-class) slot-names &rest initargs)
1302 (declare (ignore slot-names initargs))
1303 (error "Attempt to initialize or reinitialize a built in class."))
1304
1305 (defmethod class-direct-slots ((class built-in-class)) ())
1306 (defmethod class-slots ((class built-in-class)) ())
1307 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1308 (defmethod class-default-initargs ((class built-in-class)) ())
1309
1310 (defmethod validate-superclass ((c class) (s built-in-class))
1311 (or (eq s *the-class-t*) #+cmu (eq s *the-class-stream*)))
1312
1313
1314
1315 ;;;
1316 ;;;
1317 ;;;
1318
1319 (defmethod validate-superclass ((c slot-class)
1320 (f forward-referenced-class))
1321 't)
1322
1323
1324 ;;;
1325 ;;;
1326 ;;;
1327
1328 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1329 (pushnew dependent (plist-value metaobject 'dependents)))
1330
1331 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1332 (setf (plist-value metaobject 'dependents)
1333 (delete dependent (plist-value metaobject 'dependents))))
1334
1335 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1336 (dolist (dependent (plist-value metaobject 'dependents))
1337 (funcall function dependent)))
1338

  ViewVC Help
Powered by ViewVC 1.1.5