/[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.27 - (show annotations)
Tue Apr 10 22:37:23 2001 UTC (13 years ago) by pw
Branch: MAIN
Changes since 1.26: +2 -3 lines
From Pierre Mai:

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

  ViewVC Help
Powered by ViewVC 1.1.5