/[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.39 - (show annotations)
Sun Dec 22 14:09:52 2002 UTC (11 years, 3 months ago) by pmai
Branch: MAIN
Changes since 1.38: +75 -44 lines
Entomotomy Bug: slot-definition-allocation-not-amop-compliant

Added needed second half of patch, from Gerd Moellmann via SBCL, to cure
newly introduced problem with inheritance of class slots.  Also committed
modified version (again via SBCL) of Gerd's COMPUTE-SLOTS revamp for more
modular computation of slot locations.
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.39 2002/12/22 14:09:52 pmai 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 (loop for method in (car direct-methods)
237 for generic-function = (method-generic-function method)
238 unless (member generic-function collected :test #'eq)
239 collect generic-function into collected
240 finally (return collected))))))
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 (loop for method in (car entry)
287 for generic-function = (method-generic-function method)
288 unless (member generic-function collected :test #'eq)
289 collect generic-function into collected
290 finally (return collected)))))))
291
292 (defun map-specializers (function)
293 (map-all-classes (lambda (class)
294 (funcall function (class-eq-specializer class))
295 (funcall function class)))
296 (maphash (lambda (object methods)
297 (declare (ignore methods))
298 (intern-eql-specializer object))
299 *eql-specializer-methods*)
300 (maphash (lambda (object specl)
301 (declare (ignore object))
302 (funcall function specl))
303 *eql-specializer-table*)
304 nil)
305
306 (defun map-all-generic-functions (function)
307 (let ((all-generic-functions (make-hash-table :test 'eq)))
308 (map-specializers (lambda (specl)
309 (dolist (gf (specializer-direct-generic-functions specl))
310 (unless (gethash gf all-generic-functions)
311 (setf (gethash gf all-generic-functions) t)
312 (funcall function gf))))))
313 nil)
314
315 (defmethod shared-initialize :after ((specl class-eq-specializer) slot-names &key)
316 (declare (ignore slot-names))
317 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
318
319 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
320 (declare (ignore slot-names))
321 (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
322
323
324
325 (defun real-load-defclass (name metaclass-name supers slots other)
326 (let ((res (apply #'ensure-class name :metaclass metaclass-name
327 :direct-superclasses supers
328 :direct-slots slots
329 :definition-source `((defclass ,name)
330 ,*load-pathname*)
331 other)))
332 ;; Defclass of a class with a forward-referenced superclass does not
333 ;; have a wrapper. RES is the incomplete PCL class. The Lisp class
334 ;; does not yet exist. Maybe should return NIL in that case as RES
335 ;; is not useful to the user?
336 (and (class-wrapper res)(kernel:layout-class (class-wrapper 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 (meta
370 (cond ((neq supplied-meta unsupplied)
371 (find-class supplied-meta))
372 ((or (null class)
373 (forward-referenced-class-p class))
374 *the-class-standard-class*)
375 (t
376 (class-of class)))))
377 (flet ((fix-super (s)
378 (cond ((classp s) s)
379 ((not (legal-class-name-p s))
380 (simple-program-error
381 "~S is not a class or a legal class name." s))
382 (t
383 (or (find-class s nil)
384 (setf (find-class s)
385 (make-instance 'forward-referenced-class
386 :name s)))))))
387 ;;
388 ;; CLHS: signal PROGRAM-ERROR, if
389 ;; (a) there are any duplicate slot names
390 ;; (b) any of the slot options :ALLOCATION, :INITFORM, :TYPE, or
391 ;; :DOCUMENTATION appears more than one in a single slot description.
392 (loop for (slot . more) on (getf initargs :direct-slots)
393 for slot-name = (getf slot :name)
394 if (some (lambda (s) (eq slot-name (getf s :name))) more) do
395 (simple-program-error
396 "More than one direct slot with name ~S."
397 slot-name)
398 else do
399 (loop for (option value . more) on slot by #'cddr
400 when (and (member option '(:allocation :type :initform
401 :documentation))
402 (not (eq unsupplied
403 (getf more option unsupplied)))) do
404 (simple-program-error
405 "Duplicate slot option ~S for slot ~S."
406 option slot-name)))
407 ;;
408 ;; CLHS: signal PROGRAM-ERROR, if an initialization argument name
409 ;; appears more than once in :DEFAULT-INITARGS class option.
410 (loop for (initarg . more) on (getf initargs :direct-default-initargs)
411 for name = (car initarg)
412 when (some (lambda (a) (eq (car a) name)) more) do
413 (simple-program-error
414 "Duplicate initialization argument ~
415 name ~S in :default-initargs of class ~A."
416 name class))
417 ;;
418 (loop (unless (remf initargs :metaclass) (return)))
419 (loop (unless (remf initargs :direct-superclasses) (return)))
420 (values meta
421 (nconc
422 (when (neq supplied-supers unsupplied)
423 (list
424 :direct-superclasses
425 (mapcar #'fix-super supplied-supers)))
426 initargs)))))
427
428
429 ;;;
430 ;;;
431 ;;;
432 #|| ; since it doesn't do anything
433 (defmethod shared-initialize :before ((class std-class)
434 slot-names
435 &key direct-superclasses)
436 (declare (ignore slot-names))
437 ;; *** error checking
438 )
439 ||#
440
441 (defmethod shared-initialize :after
442 ((class std-class)
443 slot-names
444 &key (direct-superclasses nil direct-superclasses-p)
445 (direct-slots nil direct-slots-p)
446 (direct-default-initargs nil direct-default-initargs-p)
447 (predicate-name nil predicate-name-p))
448 (declare (ignore slot-names))
449 (cond ((or direct-superclasses-p
450 (null (slot-value class 'direct-superclasses)))
451 (setq direct-superclasses
452 (or direct-superclasses
453 (list (if (funcallable-standard-class-p class)
454 *the-class-funcallable-standard-object*
455 *the-class-standard-object*))))
456 (dolist (superclass direct-superclasses)
457 (unless (validate-superclass class superclass)
458 (error "The class ~S was specified as a~%
459 super-class of the class ~S;~%~
460 but the meta-classes ~S and~%~S are incompatible.~@
461 Define a method for ~S to avoid this error."
462 superclass class (class-of superclass) (class-of class)
463 'validate-superclass)))
464 (setf (slot-value class 'direct-superclasses) direct-superclasses))
465 (t
466 (setq direct-superclasses (slot-value class 'direct-superclasses))))
467 (setq direct-slots
468 (if direct-slots-p
469 (setf (slot-value class 'direct-slots)
470 (mapcar (lambda (pl) (make-direct-slotd class pl)) direct-slots))
471 (slot-value class 'direct-slots)))
472 (if direct-default-initargs-p
473 (setf (plist-value class 'direct-default-initargs) direct-default-initargs)
474 (setq direct-default-initargs (plist-value class 'direct-default-initargs)))
475 (setf (plist-value class 'class-slot-cells)
476 (let ((collected ()))
477 (dolist (dslotd direct-slots (nreverse collected))
478 (when (eq (slot-definition-allocation dslotd) :class)
479 (let ((initfunction (slot-definition-initfunction dslotd)))
480 (push (cons (slot-definition-name dslotd)
481 (if initfunction
482 (funcall initfunction)
483 +slot-unbound+))
484 collected))))))
485 (setq predicate-name (if predicate-name-p
486 (setf (slot-value class 'predicate-name)
487 (car predicate-name))
488 (or (slot-value class 'predicate-name)
489 (setf (slot-value class 'predicate-name)
490 (make-class-predicate-name (class-name class))))))
491 (add-direct-subclasses class direct-superclasses)
492 (update-class class nil)
493 (make-class-predicate class predicate-name)
494 (add-slot-accessors class direct-slots))
495
496 (defmethod shared-initialize :before ((class class) slot-names &key name)
497 (declare (ignore slot-names name))
498 (setf (slot-value class 'type) `(class ,class))
499 (setf (slot-value class 'class-eq-specializer)
500 (make-instance 'class-eq-specializer :class class)))
501
502 (defmethod reinitialize-instance :before ((class slot-class)
503 &key direct-superclasses)
504 (remove-direct-subclasses class direct-superclasses)
505 (remove-slot-accessors class (class-direct-slots class)))
506
507 (defmethod reinitialize-instance :after ((class slot-class)
508 &rest initargs
509 &key)
510 (map-dependents class
511 (lambda (dependent)
512 (apply #'update-dependent class dependent initargs))))
513
514 (defmethod shared-initialize :after
515 ((class structure-class)
516 slot-names
517 &key (direct-superclasses nil direct-superclasses-p)
518 (direct-slots nil direct-slots-p)
519 direct-default-initargs
520 (predicate-name nil predicate-name-p))
521 (declare (ignore slot-names direct-default-initargs))
522 (if direct-superclasses-p
523 (setf (slot-value class 'direct-superclasses)
524 (or direct-superclasses
525 (setq direct-superclasses
526 (and (not (eq (class-name class) 'structure-object))
527 (list *the-class-structure-object*)))))
528 (setq direct-superclasses (slot-value class 'direct-superclasses)))
529 (let* ((name (class-name class))
530 (from-defclass-p (slot-value class 'from-defclass-p))
531 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
532 (if direct-slots-p
533 (setf (slot-value class 'direct-slots)
534 (setq direct-slots
535 (mapcar (lambda (pl)
536 (when defstruct-p
537 (let* ((slot-name (getf pl :name))
538 (acc-name (format nil "~s structure class ~a"
539 name slot-name))
540 (accessor (intern acc-name)))
541 (setq pl (list* :defstruct-accessor-symbol accessor
542 pl))))
543 (make-direct-slotd class pl))
544 direct-slots)))
545 (setq direct-slots (slot-value class 'direct-slots)))
546 (when defstruct-p
547 (let* ((include (car (slot-value class 'direct-superclasses)))
548 (conc-name (intern (format nil "~s structure class " name)))
549 (constructor (intern (format nil "~a constructor" conc-name)))
550 (defstruct `(defstruct (,name
551 ,@(when include
552 `((:include ,(class-name include))))
553 (:predicate nil)
554 (:conc-name ,conc-name)
555 (:constructor ,constructor ()))
556 ,@(mapcar (lambda (slot)
557 `(,(slot-definition-name slot)
558 +slot-unbound+))
559 direct-slots)))
560 (reader-names (mapcar (lambda (slotd)
561 (intern (format nil "~A~A reader" conc-name
562 (slot-definition-name slotd))))
563 direct-slots))
564 (writer-names (mapcar (lambda (slotd)
565 (intern (format nil "~A~A writer" conc-name
566 (slot-definition-name slotd))))
567 direct-slots))
568 (readers-init
569 (mapcar (lambda (slotd reader-name)
570 (let ((accessor
571 (slot-definition-defstruct-accessor-symbol slotd)))
572 `(defun ,reader-name (obj)
573 (declare (type ,name obj))
574 (,accessor obj))))
575 direct-slots reader-names))
576 (writers-init
577 (mapcar (lambda (slotd writer-name)
578 (let ((accessor
579 (slot-definition-defstruct-accessor-symbol slotd)))
580 `(defun ,writer-name (nv obj)
581 (declare (type ,name obj))
582 (setf (,accessor obj) nv))))
583 direct-slots writer-names))
584 (defstruct-form
585 `(progn
586 ,defstruct
587 ,@readers-init ,@writers-init)))
588 (unless (structure-type-p name) (eval defstruct-form))
589 (mapc (lambda (dslotd reader-name writer-name)
590 (let* ((reader (gdefinition reader-name))
591 (writer (when (fboundp writer-name)
592 (gdefinition writer-name))))
593 (setf (slot-value dslotd 'internal-reader-function) reader)
594 (setf (slot-value dslotd 'internal-writer-function) writer)))
595 direct-slots reader-names writer-names)
596 (setf (slot-value class 'defstruct-form) defstruct-form)
597 (setf (slot-value class 'defstruct-constructor) constructor))))
598 (add-direct-subclasses class direct-superclasses)
599 (setf (slot-value class 'class-precedence-list)
600 (compute-class-precedence-list class))
601 (setf (slot-value class 'slots) (compute-slots class))
602 (let ((lclass (lisp:find-class (class-name class))))
603 (setf (kernel:class-pcl-class lclass) class)
604 (setf (slot-value class 'wrapper) (kernel:class-layout lclass)))
605 (update-pv-table-cache-info class)
606 (setq predicate-name (if predicate-name-p
607 (setf (slot-value class 'predicate-name)
608 (car predicate-name))
609 (or (slot-value class 'predicate-name)
610 (setf (slot-value class 'predicate-name)
611 (make-class-predicate-name (class-name class))))))
612 (make-class-predicate class predicate-name)
613 (add-slot-accessors class direct-slots))
614
615 (defmethod direct-slot-definition-class ((class structure-class) initargs)
616 (declare (ignore initargs))
617 (find-class 'structure-direct-slot-definition))
618
619 (defmethod finalize-inheritance ((class structure-class))
620 nil) ; always finalized
621
622 (defun add-slot-accessors (class dslotds)
623 (fix-slot-accessors class dslotds 'add))
624
625 (defun remove-slot-accessors (class dslotds)
626 (fix-slot-accessors class dslotds 'remove))
627
628 (defun fix-slot-accessors (class dslotds add/remove)
629 (flet ((fix (gfspec name r/w)
630 (let ((gf (ensure-generic-function gfspec)))
631 (case r/w
632 (r (if (eq add/remove 'add)
633 (add-reader-method class gf name)
634 (remove-reader-method class gf)))
635 (w (if (eq add/remove 'add)
636 (add-writer-method class gf name)
637 (remove-writer-method class gf)))))))
638 (dolist (dslotd dslotds)
639 (let ((slot-name (slot-definition-name dslotd)))
640 (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
641 (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
642
643
644 (defun add-direct-subclasses (class new)
645 (dolist (n new)
646 (unless (memq class (class-direct-subclasses class))
647 (add-direct-subclass n class))))
648
649 (defun remove-direct-subclasses (class new)
650 (let ((old (class-direct-superclasses class)))
651 (dolist (o (set-difference old new))
652 (remove-direct-subclass o class))))
653
654
655 ;;;
656 ;;;
657 ;;;
658 (defmethod finalize-inheritance ((class std-class))
659 (update-class class t))
660
661
662 (defun class-has-a-forward-referenced-superclass-p (class)
663 (or (forward-referenced-class-p class)
664 (some #'class-has-a-forward-referenced-superclass-p
665 (class-direct-superclasses class))))
666
667 ;;;
668 ;;; Called by :after shared-initialize whenever a class is initialized or
669 ;;; reinitialized. The class may or may not be finalized.
670 ;;;
671 (defun update-class (class finalizep)
672 (when (or finalizep (class-finalized-p class)
673 (not (class-has-a-forward-referenced-superclass-p class)))
674 (update-cpl class (compute-class-precedence-list class))
675 (update-slots class (compute-slots class))
676 (update-gfs-of-class class)
677 (update-inits class (compute-default-initargs class))
678 (update-make-instance-function-table class))
679 (unless finalizep
680 (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
681
682 (defun update-cpl (class cpl)
683 (if (class-finalized-p class)
684 (unless (equal (class-precedence-list class) cpl)
685 ;; Need to have the cpl setup before update-lisp-class-layout
686 ;; is called on CMUCL.
687 (setf (slot-value class 'class-precedence-list) cpl)
688 (force-cache-flushes class))
689 (setf (slot-value class 'class-precedence-list) cpl))
690 (update-class-can-precede-p cpl))
691
692 (defun update-class-can-precede-p (cpl)
693 (when cpl
694 (let ((first (car cpl)))
695 (dolist (c (cdr cpl))
696 (pushnew c (slot-value first 'can-precede-list))))
697 (update-class-can-precede-p (cdr cpl))))
698
699 (defun class-can-precede-p (class1 class2)
700 (member class2 (class-can-precede-list class1)))
701
702 (defun update-slots (class eslotds)
703 (let ((instance-slots ())
704 (class-slots ()))
705 (dolist (eslotd eslotds)
706 (ecase (slot-definition-allocation eslotd)
707 (:instance (push eslotd instance-slots))
708 (:class (push eslotd class-slots))))
709 ;;
710 ;; If there is a change in the shape of the instances then the
711 ;; old class is now obsolete.
712 ;;
713 (let* ((nlayout (mapcar #'slot-definition-name
714 (sort instance-slots #'< :key #'slot-definition-location)))
715 (nslots (length nlayout))
716 (nwrapper-class-slots (compute-class-slots class-slots))
717 (owrapper (class-wrapper class))
718 (olayout (and owrapper (wrapper-instance-slots-layout owrapper)))
719 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
720 (nwrapper
721 (cond ((null owrapper)
722 (make-wrapper nslots class))
723 ((and (equal nlayout olayout)
724 (every (lambda (o n) (eq (car o) (car n)))
725 owrapper-class-slots nwrapper-class-slots))
726 owrapper)
727 (t
728 ;;
729 ;; This will initialize the new wrapper to have the same
730 ;; state as the old wrapper. We will then have to change
731 ;; that. This may seem like wasted work (it is), but the
732 ;; spec requires that we call make-instances-obsolete.
733 ;;
734 (make-instances-obsolete class)
735 (class-wrapper class)))))
736
737 (with-slots (wrapper slots) class
738 (update-lisp-class-layout class nwrapper)
739 (setf slots eslotds
740 (wrapper-instance-slots-layout nwrapper) nlayout
741 (wrapper-class-slots nwrapper) nwrapper-class-slots
742 (wrapper-no-of-instance-slots nwrapper) nslots
743 wrapper nwrapper))
744
745 (unless (eq owrapper nwrapper)
746 (update-pv-table-cache-info class)))))
747
748 (defun compute-class-slots (eslotds)
749 (loop for eslotd in eslotds
750 for name = (slot-definition-name eslotd)
751 and class = (slot-definition-class eslotd)
752 collect (assoc name (class-slot-cells class))))
753
754 (defun update-gfs-of-class (class)
755 (when (and (class-finalized-p class)
756 (let ((cpl (class-precedence-list class)))
757 (or (member *the-class-slot-class* cpl)
758 (member *the-class-standard-effective-slot-definition* cpl))))
759 (let ((gf-table (make-hash-table :test 'eq)))
760 (labels ((collect-gfs (class)
761 (dolist (gf (specializer-direct-generic-functions class))
762 (setf (gethash gf gf-table) t))
763 (mapc #'collect-gfs (class-direct-superclasses class))))
764 (collect-gfs class)
765 (maphash (lambda (gf ignore)
766 (declare (ignore ignore))
767 (update-gf-dfun class gf))
768 gf-table)))))
769
770 (defun update-inits (class inits)
771 (setf (plist-value class 'default-initargs) inits))
772
773
774 ;;;
775 ;;;
776 ;;;
777 (defmethod compute-default-initargs ((class slot-class))
778 (let ((cpl (class-precedence-list class))
779 (direct (class-direct-default-initargs class)))
780 (labels ((walk (tail)
781 (if (null tail)
782 nil
783 (let ((c (pop tail)))
784 (append (if (eq c class)
785 direct
786 (class-direct-default-initargs c))
787 (walk tail))))))
788 (let ((initargs (walk cpl)))
789 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))))
790
791
792 ;;;
793 ;;; Protocols for constructing direct and effective slot definitions.
794 ;;;
795 ;;;
796 ;;;
797 ;;;
798 (defmethod direct-slot-definition-class ((class std-class) initargs)
799 (declare (ignore initargs))
800 (find-class 'standard-direct-slot-definition))
801
802 (defun make-direct-slotd (class initargs)
803 (let ((initargs (list* :class class initargs)))
804 (apply #'make-instance (direct-slot-definition-class class initargs) initargs)))
805
806 ;;;
807 ;;;
808 ;;;
809 (defmethod compute-slots ((class std-class))
810 ;;
811 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
812 ;; for each different slot name we find in our superclasses. Each
813 ;; call receives the class and a list of the dslotds with that name.
814 ;; The list is in most-specific-first order.
815 ;;
816 (let ((name-dslotds-alist ()))
817 (dolist (c (class-precedence-list class))
818 (dolist (slot (class-direct-slots c))
819 (let* ((name (slot-definition-name slot))
820 (entry (assq name name-dslotds-alist)))
821 (if entry
822 (push slot (cdr entry))
823 (push (list name slot) name-dslotds-alist)))))
824 (mapcar (lambda (direct)
825 (compute-effective-slot-definition class
826 (nreverse (cdr direct))))
827 name-dslotds-alist)))
828
829 (defmethod compute-slots ((class standard-class))
830 (call-next-method))
831
832 (defmethod compute-slots :around ((class standard-class))
833 (let ((eslotds (call-next-method))
834 (location -1))
835 (dolist (eslotd eslotds eslotds)
836 (setf (slot-definition-location eslotd)
837 (ecase (slot-definition-allocation eslotd)
838 (:instance
839 (incf location))
840 (:class
841 (let* ((name (slot-definition-name eslotd))
842 (from-class (slot-definition-allocation-class eslotd))
843 (cell (assq name (class-slot-cells from-class))))
844 (assert (consp cell))
845 cell))))
846 (initialize-internal-slot-functions eslotd))))
847
848 (defmethod compute-slots ((class funcallable-standard-class))
849 (call-next-method))
850
851 (defmethod compute-slots :around ((class funcallable-standard-class))
852 (labels ((instance-slot-names (slotds)
853 (let (collect)
854 (dolist (slotd slotds (nreverse collect))
855 (when (eq (slot-definition-allocation slotd) :instance)
856 (push (slot-definition-name slotd) collect)))))
857 ;; This sorts slots so that slots of classes later in the CPL
858 ;; come before slots of other classes. This is crucial for
859 ;; funcallable instances because it ensures that the slots of
860 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
861 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
862 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
863 ;; a funcallable instance.
864 (compute-layout (eslotds)
865 (let ((first ())
866 (names (instance-slot-names eslotds)))
867 (dolist (class
868 (reverse (class-precedence-list class))
869 (nreverse (nconc names first)))
870 (dolist (ss (class-slots class))
871 (let ((name (slot-definition-name ss)))
872 (when (member name names)
873 (push name first)
874 (setq names (delete name names)))))))))
875 (let ((all-slotds (call-next-method))
876 (instance-slots ())
877 (class-slots ()))
878 (dolist (slotd all-slotds)
879 (ecase (slot-definition-allocation slotd)
880 (:instance (push slotd instance-slots))
881 (:class (push slotd class-slots))))
882 (let ((layout (compute-layout instance-slots)))
883 (dolist (slotd instance-slots)
884 (setf (slot-definition-location slotd)
885 (position (slot-definition-name slotd) layout))
886 (initialize-internal-slot-functions slotd)))
887 (dolist (slotd class-slots)
888 (let ((name (slot-definition-name slotd))
889 (from-class (slot-definition-allocation-class slotd)))
890 (setf (slot-definition-location slotd)
891 (assoc name (class-slot-cells from-class)))
892 (assert (consp (slot-definition-location slotd)))
893 (initialize-internal-slot-functions slotd)))
894 all-slotds)))
895
896 (defmethod compute-slots ((class structure-class))
897 (mapcan (lambda (superclass)
898 (mapcar (lambda (dslotd)
899 (compute-effective-slot-definition class (list dslotd)))
900 (class-direct-slots superclass)))
901 (reverse (slot-value class 'class-precedence-list))))
902
903 (defmethod compute-slots :around ((class structure-class))
904 (let ((eslotds (call-next-method)))
905 (mapc #'initialize-internal-slot-functions eslotds)
906 eslotds))
907
908 (defmethod compute-effective-slot-definition ((class slot-class) dslotds)
909 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
910 (class (effective-slot-definition-class class initargs)))
911 (apply #'make-instance class initargs)))
912
913 (defmethod effective-slot-definition-class ((class std-class) initargs)
914 (declare (ignore initargs))
915 (find-class 'standard-effective-slot-definition))
916
917 (defmethod effective-slot-definition-class ((class structure-class) initargs)
918 (declare (ignore initargs))
919 (find-class 'structure-effective-slot-definition))
920
921 (defmethod compute-effective-slot-definition-initargs
922 ((class slot-class) direct-slotds)
923 (let* ((name nil)
924 (initfunction nil)
925 (initform nil)
926 (initargs nil)
927 (allocation nil)
928 (allocation-class nil)
929 (type t)
930 (namep nil)
931 (initp nil)
932 (allocp nil))
933
934 (dolist (slotd direct-slotds)
935 (when slotd
936 (unless namep
937 (setq name (slot-definition-name slotd)
938 namep t))
939 (unless initp
940 (when (slot-definition-initfunction slotd)
941 (setq initform (slot-definition-initform slotd)
942 initfunction (slot-definition-initfunction slotd)
943 initp t)))
944 (unless allocp
945 (setq allocation (slot-definition-allocation slotd)
946 allocation-class (slot-definition-class slotd)
947 allocp t))
948 (setq initargs (append (slot-definition-initargs slotd) initargs))
949 (let ((slotd-type (slot-definition-type slotd)))
950 (setq type (cond ((eq type t) slotd-type)
951 ((*subtypep type slotd-type) type)
952 (t `(and ,type ,slotd-type)))))))
953 (list :name name
954 :initform initform
955 :initfunction initfunction
956 :initargs initargs
957 :allocation allocation
958 :allocation-class allocation-class
959 :type type
960 :class class)))
961
962 (defmethod compute-effective-slot-definition-initargs :around
963 ((class structure-class) direct-slotds)
964 (let ((slotd (car direct-slotds)))
965 (list* :defstruct-accessor-symbol (slot-definition-defstruct-accessor-symbol slotd)
966 :internal-reader-function (slot-definition-internal-reader-function slotd)
967 :internal-writer-function (slot-definition-internal-writer-function slotd)
968 (call-next-method))))
969
970 ;;;
971 ;;; NOTE: For bootstrapping considerations, these can't use make-instance
972 ;;; to make the method object. They have to use make-a-method which
973 ;;; is a specially bootstrapped mechanism for making standard methods.
974 ;;;
975 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
976 (declare (ignore direct-slot initargs))
977 (find-class 'standard-reader-method))
978
979 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
980 (add-method generic-function
981 (make-a-method 'standard-reader-method
982 ()
983 (list (or (class-name class) 'object))
984 (list class)
985 (make-reader-method-function class slot-name)
986 "automatically generated reader method"
987 slot-name)))
988
989 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
990 (declare (ignore direct-slot initargs))
991 (find-class 'standard-writer-method))
992
993 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
994 (add-method generic-function
995 (make-a-method 'standard-writer-method
996 ()
997 (list 'new-value (or (class-name class) 'object))
998 (list *the-class-t* class)
999 (make-writer-method-function class slot-name)
1000 "automatically generated writer method"
1001 slot-name)))
1002
1003 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1004 (add-method generic-function
1005 (make-a-method 'standard-boundp-method
1006 ()
1007 (list (or (class-name class) 'object))
1008 (list class)
1009 (make-boundp-method-function class slot-name)
1010 "automatically generated boundp method"
1011 slot-name)))
1012
1013 (defmethod remove-reader-method ((class slot-class) generic-function)
1014 (let ((method (get-method generic-function () (list class) nil)))
1015 (when method (remove-method generic-function method))))
1016
1017 (defmethod remove-writer-method ((class slot-class) generic-function)
1018 (let ((method
1019 (get-method generic-function () (list *the-class-t* class) nil)))
1020 (when method (remove-method generic-function method))))
1021
1022 (defmethod remove-boundp-method ((class slot-class) generic-function)
1023 (let ((method (get-method generic-function () (list class) nil)))
1024 (when method (remove-method generic-function method))))
1025
1026
1027 ;;;
1028 ;;; make-reader-method-function and make-write-method function are NOT part of
1029 ;;; the standard protocol. They are however useful, PCL makes uses makes use
1030 ;;; of them internally and documents them for PCL users.
1031 ;;;
1032 ;;; *** This needs work to make type testing by the writer functions which
1033 ;;; *** do type testing faster. The idea would be to have one constructor
1034 ;;; *** for each possible type test. In order to do this it would be nice
1035 ;;; *** to have help from inform-type-system-about-class and friends.
1036 ;;;
1037 ;;; *** There is a subtle bug here which is going to have to be fixed.
1038 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1039 ;;; *** have to give the optimize-slot-value method the user might have
1040 ;;; *** defined for this metclass a chance to run.
1041 ;;;
1042 (defmethod make-reader-method-function ((class slot-class) slot-name)
1043 (make-std-reader-method-function (class-name class) slot-name))
1044
1045 (defmethod make-writer-method-function ((class slot-class) slot-name)
1046 (make-std-writer-method-function (class-name class) slot-name))
1047
1048 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1049 (make-std-boundp-method-function (class-name class) slot-name))
1050
1051
1052 ;;;; inform-type-system-about-class
1053 ;;;
1054 ;;; This is NOT part of the standard protocol. It is an internal mechanism
1055 ;;; which PCL uses to *try* and tell the type system about class definitions.
1056 ;;; In a more fully integrated implementation of CLOS, the type system would
1057 ;;; know about class objects and class names in a more fundamental way and
1058 ;;; the mechanism used to inform the type system about new classes would be
1059 ;;; different.
1060 ;;;
1061 (defmethod inform-type-system-about-class ((class std-class) name)
1062 ;; Maybe add skeleton lisp:standard-class to avoid undefined-function
1063 ;; compiler warnings. Not otherwise needed in this implementation.
1064 (inform-type-system-about-std-class name))
1065
1066 (defmethod inform-type-system-about-class
1067 ((class funcallable-standard-class) name)
1068 (declare (ignore name))
1069 ;; Avoid load-time warning of changing metaclass.
1070 )
1071
1072
1073
1074 (defmethod compatible-meta-class-change-p (class proto-new-class)
1075 (eq (class-of class) (class-of proto-new-class)))
1076
1077 (defmethod validate-superclass ((class class) (new-super class))
1078 (or (eq new-super *the-class-t*)
1079 (eq (class-of class) (class-of new-super))))
1080
1081 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1082 (let ((new-super-meta-class (class-of new-super)))
1083 (or (eq new-super-meta-class *the-class-std-class*)
1084 (eq (class-of class) new-super-meta-class))))
1085
1086
1087 ;;;
1088 ;;; Force the flushing of caches by creating a new wrapper for CLASS,
1089 ;;; if necessary.
1090 ;;;
1091 ;;; If the LAYOUT-INVALID slot of CLASS's wrapper is
1092 ;;;
1093 ;;; -- (:FLUSH <new wrapper>) or (:OBSOLETE <new wrapper>), there's
1094 ;;; nothing to do. The new wrapper has already been created.
1095 ;;;
1096 ;;; -- :INVALID, then it has been set to that value by a previous
1097 ;;; call to REGISTER-LAYOUT for a superclass S of CLASS; S's
1098 ;;; wrapper has been invalidated together with its subclasses. In
1099 ;;; this case, CLASS's caches must obviously be flushed, too, like
1100 ;;; S's. So, make a new wrapper for CLASS, and translate kernel's
1101 ;;; :INVALID to PCL (:FLUSH <new wrapper>). UPDATE-SLOTS can later
1102 ;;; decide if it wants to make this (:OBSOLETE ...).
1103 ;;;
1104 ;;; -- NIL, then the wrapper is still valid, in which case we do
1105 ;;; the same as for :INVALID, but for the obviously slightly
1106 ;;; different reason.
1107 ;;;
1108 (defun force-cache-flushes (class)
1109 (let* ((owrapper (class-wrapper class)))
1110 ;;
1111 ;; We only need to do something if the wrapper is still valid. If the
1112 ;; wrapper isn't valid, state will be FLUSH or OBSOLETE, and both of those
1113 ;; will already be doing what we want. In particular, we must be
1114 ;; sure we never change an OBSOLETE into a FLUSH since OBSOLETE
1115 ;; means do what FLUSH does and then some.
1116 ;;
1117 (when (or (not (invalid-wrapper-p owrapper))
1118 (eq :invalid (kernel:layout-invalid owrapper)))
1119 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1120 class)))
1121 (setf (wrapper-instance-slots-layout nwrapper)
1122 (wrapper-instance-slots-layout owrapper))
1123 (setf (wrapper-class-slots nwrapper)
1124 (wrapper-class-slots owrapper))
1125 (with-pcl-lock
1126 (update-lisp-class-layout class nwrapper)
1127 (setf (slot-value class 'wrapper) nwrapper)
1128 (invalidate-wrapper owrapper :flush nwrapper))))))
1129
1130 (defun flush-cache-trap (owrapper nwrapper instance)
1131 (declare (ignore owrapper))
1132 (set-wrapper instance nwrapper))
1133
1134
1135
1136 ;;;
1137 ;;; make-instances-obsolete can be called by user code. It will cause the
1138 ;;; next access to the instance (as defined in 88-002R) to trap through the
1139 ;;; update-instance-for-redefined-class mechanism.
1140 ;;;
1141 (defmethod make-instances-obsolete ((class std-class))
1142 (let* ((owrapper (class-wrapper class))
1143 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1144 class)))
1145 (setf (wrapper-instance-slots-layout nwrapper)
1146 (wrapper-instance-slots-layout owrapper))
1147 (setf (wrapper-class-slots nwrapper)
1148 (wrapper-class-slots owrapper))
1149 (with-pcl-lock
1150 (update-lisp-class-layout class nwrapper)
1151 (setf (slot-value class 'wrapper) nwrapper)
1152 (invalidate-wrapper owrapper :obsolete nwrapper)
1153 class)))
1154
1155 (defmethod make-instances-obsolete ((class symbol))
1156 (make-instances-obsolete (find-class class)))
1157
1158
1159 ;;;
1160 ;;; obsolete-instance-trap is the internal trap that is called when we see
1161 ;;; an obsolete instance. The times when it is called are:
1162 ;;; - when the instance is involved in method lookup
1163 ;;; - when attempting to access a slot of an instance
1164 ;;;
1165 ;;; It is not called by class-of, wrapper-of, or any of the low-level instance
1166 ;;; access macros.
1167 ;;;
1168 ;;; Of course these times when it is called are an internal implementation
1169 ;;; detail of PCL and are not part of the documented description of when the
1170 ;;; obsolete instance update happens. The documented description is as it
1171 ;;; appears in 88-002R.
1172 ;;;
1173 ;;; This has to return the new wrapper, so it counts on all the methods on
1174 ;;; obsolete-instance-trap-internal to return the new wrapper. It also does
1175 ;;; a little internal error checking to make sure that the traps are only
1176 ;;; happening when they should, and that the trap methods are computing
1177 ;;; apropriate new wrappers.
1178 ;;;
1179
1180 ;;; obsolete-instance-trap might be called on structure instances
1181 ;;; after a structure is redefined. In most cases, obsolete-instance-trap
1182 ;;; will not be able to fix the old instance, so it must signal an
1183 ;;; error. The hard part of this is that the error system and debugger
1184 ;;; might cause obsolete-instance-trap to be called again, so in that
1185 ;;; case, we have to return some reasonable wrapper, instead.
1186
1187 (defvar *in-obsolete-instance-trap* nil)
1188 (defvar *the-wrapper-of-structure-object*
1189 (class-wrapper (find-class 'structure-object)))
1190
1191 (define-condition obsolete-structure (error)
1192 ((datum :reader obsolete-structure-datum :initarg :datum))
1193 (:report
1194 (lambda (condition stream)
1195 ;; Don't try to print the structure, since it probably
1196 ;; won't work.
1197 (format stream "Obsolete structure error in ~S:~@
1198 For a structure of type: ~S"
1199 (conditions::condition-function-name condition)
1200 (type-of (obsolete-structure-datum condition))))))
1201
1202 (defun obsolete-instance-trap (owrapper nwrapper instance)
1203 (if (not (pcl-instance-p instance))
1204 (if *in-obsolete-instance-trap*
1205 *the-wrapper-of-structure-object*
1206 (let ((*in-obsolete-instance-trap* t))
1207 (error 'obsolete-structure :datum instance)))
1208 (let* ((class (wrapper-class* nwrapper))
1209 (copy (allocate-instance class)) ;??? allocate-instance ???
1210 (olayout (wrapper-instance-slots-layout owrapper))
1211 (nlayout (wrapper-instance-slots-layout nwrapper))
1212 (oslots (get-slots instance))
1213 (nslots (get-slots copy))
1214 (oclass-slots (wrapper-class-slots owrapper))
1215 (added ())
1216 (discarded ())
1217 (plist ()))
1218 ;; local --> local transfer
1219 ;; local --> shared discard
1220 ;; local --> -- discard
1221 ;; shared --> local transfer
1222 ;; shared --> shared discard
1223 ;; shared --> -- discard
1224 ;; -- --> local add
1225 ;; -- --> shared --
1226 ;;
1227 ;; Go through all the old local slots.
1228 ;;
1229 (loop for name in olayout and opos from 0
1230 as npos = (posq name nlayout)
1231 if npos do
1232 (setf (instance-ref nslots npos)
1233 (instance-ref oslots opos))
1234 else do
1235 (push name discarded)
1236 (unless (eq (instance-ref oslots opos) +slot-unbound+)
1237 (setf (getf plist name)
1238 (instance-ref oslots opos))))
1239 ;;
1240 ;; Go through all the old shared slots.
1241 ;;
1242 (loop for (name . val) in oclass-slots
1243 for npos = (posq name nlayout)
1244 if npos do
1245 (setf (instance-ref nslots npos) val)
1246 else do
1247 (push name discarded)
1248 (unless (eq val +slot-unbound+)
1249 (setf (getf plist name) val)))
1250 ;;
1251 ;; Go through all the new local slots to compute the added slots.
1252 ;;
1253 (dolist (nlocal nlayout)
1254 (unless (or (memq nlocal olayout)
1255 (assq nlocal oclass-slots))
1256 (push nlocal added)))
1257
1258 (swap-wrappers-and-slots instance copy)
1259
1260 (update-instance-for-redefined-class instance
1261 added
1262 discarded
1263 plist)
1264 nwrapper)))
1265
1266
1267 ;;;
1268 ;;;
1269 ;;;
1270 (defmacro copy-instance-internal (instance)
1271 `(progn
1272 (let* ((class (class-of instance))
1273 (copy (allocate-instance class)))
1274 (if (std-instance-p ,instance)
1275 (setf (std-instance-slots ,instance) (std-instance-slots ,instance))
1276 (setf (fsc-instance-slots ,instance) (fsc-instance-slots ,instance)))
1277 copy)))
1278
1279 (defun change-class-internal (instance new-class initargs)
1280 (let* ((old-class (class-of instance))
1281 (copy (allocate-instance new-class))
1282 (new-wrapper (get-wrapper copy))
1283 (old-wrapper (class-wrapper old-class))
1284 (old-layout (wrapper-instance-slots-layout old-wrapper))
1285 (new-layout (wrapper-instance-slots-layout new-wrapper))
1286 (old-slots (get-slots instance))
1287 (new-slots (get-slots copy))
1288 (old-class-slots (wrapper-class-slots old-wrapper)))
1289
1290 ;;
1291 ;; "The values of local slots specified by both the class Cto and
1292 ;; Cfrom are retained. If such a local slot was unbound, it remains
1293 ;; unbound."
1294 ;;
1295 (loop for new-slot in new-layout and new-position from 0
1296 for old-position = (posq new-slot old-layout)
1297 when old-position do
1298 (setf (instance-ref new-slots new-position)
1299 (instance-ref old-slots old-position)))
1300 ;;
1301 ;; "The values of slots specified as shared in the class Cfrom and
1302 ;; as local in the class Cto are retained."
1303 ;;
1304 (loop for (name . val) in old-class-slots
1305 for new-position = (posq name new-layout)
1306 when new-position do
1307 (setf (instance-ref new-slots new-position) val))
1308
1309 ;; Make the copy point to the old instance's storage, and make the
1310 ;; old instance point to the new storage.
1311 (swap-wrappers-and-slots instance copy)
1312
1313 (apply #'update-instance-for-different-class copy instance initargs)
1314 instance))
1315
1316 (defmethod change-class ((instance standard-object)
1317 (new-class standard-class)
1318 &rest initargs)
1319 (change-class-internal instance new-class initargs))
1320
1321 (defmethod change-class ((instance funcallable-standard-object)
1322 (new-class funcallable-standard-class)
1323 &rest initargs)
1324 (change-class-internal instance new-class initargs))
1325
1326 (defmethod change-class ((instance standard-object)
1327 (new-class funcallable-standard-class)
1328 &rest initargs)
1329 (declare (ignore initargs))
1330 (error "Can't change the class of ~S to ~S~@
1331 because it isn't already an instance with metaclass ~S."
1332 instance new-class 'standard-class))
1333
1334 (defmethod change-class ((instance funcallable-standard-object)
1335 (new-class standard-class)
1336 &rest initargs)
1337 (declare (ignore initargs))
1338 (error "Can't change the class of ~S to ~S~@
1339 because it isn't already an instance with metaclass ~S."
1340 instance new-class 'funcallable-standard-class))
1341
1342 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1343 (apply #'change-class instance (find-class new-class-name) initargs))
1344
1345
1346
1347 ;;;
1348 ;;; The metaclass BUILT-IN-CLASS
1349 ;;;
1350 ;;; This metaclass is something of a weird creature. By this point, all
1351 ;;; instances of it which will exist have been created, and no instance
1352 ;;; is ever created by calling MAKE-INSTANCE.
1353 ;;;
1354 ;;; But, there are other parts of the protcol we must follow and those
1355 ;;; definitions appear here.
1356 ;;;
1357 (defmethod shared-initialize :before
1358 ((class built-in-class) slot-names &rest initargs)
1359 (declare (ignore slot-names initargs))
1360 (error "Attempt to initialize or reinitialize a built in class."))
1361
1362 (defmethod class-direct-slots ((class built-in-class)) ())
1363 (defmethod class-slots ((class built-in-class)) ())
1364 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1365 (defmethod class-default-initargs ((class built-in-class)) ())
1366
1367 (defmethod validate-superclass ((c class) (s built-in-class))
1368 (or (eq s *the-class-t*) (eq s *the-class-stream*)))
1369
1370
1371
1372 ;;;
1373 ;;;
1374 ;;;
1375
1376 (defmethod validate-superclass ((c slot-class)
1377 (f forward-referenced-class))
1378 t)
1379
1380
1381 ;;;
1382 ;;;
1383 ;;;
1384
1385 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1386 (pushnew dependent (plist-value metaobject 'dependents)))
1387
1388 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1389 (setf (plist-value metaobject 'dependents)
1390 (delete dependent (plist-value metaobject 'dependents))))
1391
1392 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1393 (dolist (dependent (plist-value metaobject 'dependents))
1394 (funcall function dependent)))
1395

  ViewVC Help
Powered by ViewVC 1.1.5