/[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.82 - (show annotations)
Thu Nov 30 15:49:30 2006 UTC (7 years, 4 months ago) by rtoy
Branch: MAIN
CVS Tags: snapshot-2007-09, snapshot-2007-08, snapshot-2008-05, snapshot-2007-05, snapshot-2008-01, snapshot-2008-02, snapshot-2008-03, snapshot-2006-12, snapshot-2007-01, snapshot-2007-02, release-19e, snapshot-2008-04, snapshot-2007-03, snapshot-2007-04, snapshot-2007-07, snapshot-2007-06, release-19e-pre1, release-19e-pre2, unicode-utf16-string-support, release-19e-base, unicode-utf16-base, snapshot-2007-12, snapshot-2007-10, snapshot-2007-11, pre-telent-clx
Branch point for: unicode-utf16-branch, release-19e-branch
Changes since 1.81: +4 -2 lines
Apply Martin Rydstrom's fix for forward-referenced classes.  This
fixes a bug in compiling recent McCLIM versions.
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 (file-comment
29 "$Header: /tiger/var/lib/cvsroots/cmucl/src/pcl/std-class.lisp,v 1.82 2006/11/30 15:49:30 rtoy Exp $")
30
31 (in-package :pcl)
32
33 (defmethod slot-accessor-function ((slotd effective-slot-definition) type)
34 (ecase type
35 (reader (slot-definition-reader-function slotd))
36 (writer (slot-definition-writer-function slotd))
37 (boundp (slot-definition-boundp-function slotd))))
38
39 (defmethod (setf slot-accessor-function) (function
40 (slotd effective-slot-definition) type)
41 (ecase type
42 (reader (setf (slot-definition-reader-function slotd) function))
43 (writer (setf (slot-definition-writer-function slotd) function))
44 (boundp (setf (slot-definition-boundp-function slotd) function))))
45
46 (defconstant *slotd-reader-function-std-p* 1)
47 (defconstant *slotd-writer-function-std-p* 2)
48 (defconstant *slotd-boundp-function-std-p* 4)
49 (defconstant *slotd-all-function-std-p* 7)
50
51 (defmethod slot-accessor-std-p ((slotd effective-slot-definition) type)
52 (let ((flags (slot-value slotd 'accessor-flags)))
53 (declare (type fixnum flags))
54 (if (eq type 'all)
55 (eql *slotd-all-function-std-p* flags)
56 (let ((mask (ecase type
57 (reader *slotd-reader-function-std-p*)
58 (writer *slotd-writer-function-std-p*)
59 (boundp *slotd-boundp-function-std-p*))))
60 (declare (type fixnum mask))
61 (not (zerop (the fixnum (logand mask flags))))))))
62
63 (defmethod (setf slot-accessor-std-p) (value (slotd effective-slot-definition) type)
64 (let ((mask (ecase type
65 (reader *slotd-reader-function-std-p*)
66 (writer *slotd-writer-function-std-p*)
67 (boundp *slotd-boundp-function-std-p*)))
68 (flags (slot-value slotd 'accessor-flags)))
69 (declare (type fixnum mask flags))
70 (setf (slot-value slotd 'accessor-flags)
71 (if value
72 (the fixnum (logior mask flags))
73 (the fixnum (logand (the fixnum (lognot mask)) flags)))))
74 value)
75
76 (defmethod initialize-internal-slot-functions ((slotd effective-slot-definition))
77 (let* ((name (slot-value slotd 'name))
78 (class (slot-value slotd 'class)))
79 (setf (gethash class (slot-name->class-table name)) slotd)
80 (dolist (type '(reader writer boundp))
81 (let* ((gf-name (ecase type
82 (reader 'slot-value-using-class)
83 (writer '(setf slot-value-using-class))
84 (boundp 'slot-boundp-using-class)))
85 (gf (gdefinition gf-name)))
86 (compute-slot-accessor-info slotd type gf)))
87 (initialize-internal-slot-gfs name)))
88
89 ;;;
90 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
91 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
92 ;;; writing/testing effective slot SLOTD.
93 ;;;
94 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
95 ;;; GF. Store the effective method in the effective slot definition
96 ;;; object itself; these GFs have special dispatch functions calling
97 ;;; effective methods directly retrieved from effective slot
98 ;;; definition objects, as an optimization.
99 ;;;
100 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
101 ;;; or some such.
102 ;;;
103 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
104 type gf)
105 (let* ((name (slot-value slotd 'name))
106 (class (slot-value slotd 'class))
107 (old-slotd (find-slot-definition class name))
108 (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
109 (multiple-value-bind (function std-p)
110 (if (eq *boot-state* 'complete)
111 (get-accessor-method-function gf type class slotd)
112 (get-optimized-std-accessor-method-function class slotd type))
113 (setf (slot-accessor-std-p slotd type) std-p)
114 (setf (slot-accessor-function slotd type) function))
115 (when (and old-slotd
116 (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
117 (record-pv-update-info slotd))))
118
119 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
120 :instance)
121
122
123
124 (defmethod shared-initialize :after ((object documentation-mixin)
125 slot-names
126 &key (documentation nil documentation-p))
127 (declare (ignore slot-names))
128 (when documentation-p
129 (setf (documentation object nil) documentation)))
130
131 (defmethod documentation (object doc-type)
132 (declare (ignore object doc-type))
133 nil)
134
135 (defmethod (setf documentation) (new-value object doc-type)
136 (declare (ignore new-value doc-type))
137 (error "~@<Can't change the documentation of ~S.~@:>" object))
138
139 (defmethod documentation ((object documentation-mixin) doc-type)
140 (declare (ignore doc-type))
141 (plist-value object 'documentation))
142
143 (defmethod (setf documentation) (new-value (object documentation-mixin) doc-type)
144 (declare (ignore doc-type))
145 (setf (plist-value object 'documentation) new-value))
146
147 (defmethod documentation ((slotd standard-slot-definition) doc-type)
148 (declare (ignore doc-type))
149 (slot-value slotd 'documentation))
150
151 (defmethod (setf documentation) (new-value (slotd standard-slot-definition) doc-type)
152 (declare (ignore doc-type))
153 (setf (slot-value slotd 'documentation) new-value))
154
155 (defmethod (setf documentation) (doc (gf generic-function) type)
156 (declare (ignore type))
157 (setf (info function documentation (generic-function-name gf))
158 doc))
159
160
161 ;;;
162 ;;; Various class accessors that are a little more complicated than can be
163 ;;; done with automatically generated reader methods.
164 ;;;
165 (defmethod class-prototype ((class std-class))
166 (with-slots (prototype) class
167 (or prototype (setq prototype (allocate-instance class)))))
168
169 (defmethod class-prototype ((class structure-class))
170 (with-slots (prototype wrapper defstruct-constructor) class
171 (or prototype
172 (setq prototype
173 (if defstruct-constructor
174 (allocate-instance class)
175 (allocate-standard-instance wrapper))))))
176
177 (defmethod class-prototype ((class condition-class))
178 (with-slots (prototype) class
179 (or prototype (setf prototype (allocate-instance class)))))
180
181 (defmethod class-direct-default-initargs ((class slot-class))
182 (plist-value class 'direct-default-initargs))
183
184 (defmethod class-default-initargs ((class slot-class))
185 (plist-value class 'default-initargs))
186
187 (defmethod class-constructors ((class slot-class))
188 (plist-value class 'constructors))
189
190 (defmethod class-slot-cells ((class std-class))
191 (plist-value class 'class-slot-cells))
192
193
194 ;;;
195 ;;; Class accessors that are even a little bit more complicated than those
196 ;;; above. These have a protocol for updating them, we must implement that
197 ;;; protocol.
198 ;;;
199
200 ;;;
201 ;;; Maintaining the direct subclasses backpointers. The update methods are
202 ;;; here, the values are read by an automatically generated reader method.
203 ;;;
204 (defmethod add-direct-subclass ((class class) (subclass class))
205 (check-seals class 'add-direct-subclass)
206 (with-slots (direct-subclasses) class
207 (pushnew subclass direct-subclasses)
208 subclass))
209
210 (defmethod remove-direct-subclass ((class class) (subclass class))
211 (check-seals class 'remove-direct-subclass)
212 (with-slots (direct-subclasses) class
213 (setq direct-subclasses (remove subclass direct-subclasses))
214 subclass))
215
216 ;;;
217 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
218 ;;;
219 ;;; There are four generic functions involved, each has one method for the
220 ;;; class case and another method for the damned EQL specializers. All of
221 ;;; these are specified methods and appear in their specified place in the
222 ;;; class graph.
223 ;;;
224 ;;; ADD-DIRECT-METHOD
225 ;;; REMOVE-DIRECT-METHOD
226 ;;; SPECIALIZER-DIRECT-METHODS
227 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
228 ;;;
229 ;;; In each case, we maintain one value which is a cons. The car is the list
230 ;;; methods. The cdr is a list of the generic functions. The cdr is always
231 ;;; computed lazily.
232 ;;;
233
234 (defmethod add-direct-method ((specializer class) (method method))
235 (with-slots (direct-methods) specializer
236 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
237 (cdr direct-methods) ()))
238 method)
239
240 (defmethod remove-direct-method ((specializer class) (method method))
241 (with-slots (direct-methods) specializer
242 (setf (car direct-methods) (remove method (car direct-methods))
243 (cdr direct-methods) ()))
244 (remove-inline-access-method specializer method)
245 method)
246
247 (defmethod specializer-direct-methods ((specializer class))
248 (with-slots (direct-methods) specializer
249 (car direct-methods)))
250
251 (defmethod specializer-direct-generic-functions ((specializer class))
252 (with-slots (direct-methods) specializer
253 (or (cdr direct-methods)
254 (setf (cdr direct-methods)
255 (loop for method in (car direct-methods)
256 for generic-function = (method-generic-function method)
257 unless (member generic-function collected :test #'eq)
258 collect generic-function into collected
259 finally (return collected))))))
260
261
262 ;;;
263 ;;; This hash table is used to store the direct methods and direct generic
264 ;;; functions of EQL specializers. Each value in the table is the cons.
265 ;;;
266 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
267 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
268
269 (defmethod specializer-method-table ((specializer eql-specializer))
270 *eql-specializer-methods*)
271
272 (defmethod specializer-method-table ((specializer class-eq-specializer))
273 *class-eq-specializer-methods*)
274
275 (defmethod add-direct-method ((specializer specializer-with-object) (method method))
276 (let* ((object (specializer-object specializer))
277 (table (specializer-method-table specializer))
278 (entry (gethash object table)))
279 (unless entry
280 (setq entry
281 (setf (gethash object table)
282 (cons nil nil))))
283 (setf (car entry) (adjoin method (car entry))
284 (cdr entry) ())
285 method))
286
287 (defmethod remove-direct-method ((specializer specializer-with-object) (method method))
288 (let* ((object (specializer-object specializer))
289 (entry (gethash object (specializer-method-table specializer))))
290 (when entry
291 (setf (car entry) (remove method (car entry))
292 (cdr entry) ()))
293 method))
294
295 (defmethod specializer-direct-methods ((specializer specializer-with-object))
296 (car (gethash (specializer-object specializer)
297 (specializer-method-table specializer))))
298
299 (defmethod specializer-direct-generic-functions ((specializer specializer-with-object))
300 (let* ((object (specializer-object specializer))
301 (entry (gethash object (specializer-method-table specializer))))
302 (when entry
303 (or (cdr entry)
304 (setf (cdr entry)
305 (loop for method in (car entry)
306 for generic-function = (method-generic-function method)
307 unless (member generic-function collected :test #'eq)
308 collect generic-function into collected
309 finally (return collected)))))))
310
311 (defun map-specializers (function)
312 (map-all-classes (lambda (class)
313 (funcall function (class-eq-specializer class))
314 (funcall function class)))
315 (maphash (lambda (object methods)
316 (declare (ignore methods))
317 (intern-eql-specializer object))
318 *eql-specializer-methods*)
319 (maphash (lambda (object specl)
320 (declare (ignore object))
321 (funcall function specl))
322 *eql-specializer-table*)
323 nil)
324
325 (defun map-all-classes (function &optional (root t))
326 (let ((braid-p (memq *boot-state* '(braid complete)))
327 (root (if (symbolp root) (find-class root) root)))
328 (labels ((map-class (class)
329 (mapc #'map-class
330 (if braid-p
331 (class-direct-subclasses class)
332 (early-class-direct-subclasses class)))
333 (funcall function class)))
334 (map-class root))))
335
336 (defun map-all-generic-functions (function)
337 (let ((all-generic-functions (make-hash-table :test 'eq)))
338 (map-specializers (lambda (specl)
339 (dolist (gf (specializer-direct-generic-functions specl))
340 (unless (gethash gf all-generic-functions)
341 (setf (gethash gf all-generic-functions) t)
342 (funcall function gf))))))
343 nil)
344
345 (defmethod shared-initialize :after ((specl class-eq-specializer) slot-names &key)
346 (declare (ignore slot-names))
347 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
348
349 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
350 (declare (ignore slot-names))
351 (setf (slot-value specl 'type) `(eql ,(specializer-object specl)))
352 ;; Tell the type system about this eql specializer type.
353 (setf (info type translator specl)
354 (constantly (kernel:make-member-type :members (list (specializer-object specl))))))
355
356
357
358 (defun real-load-defclass (name metaclass-name supers slots other)
359 (apply #'ensure-class name :metaclass metaclass-name
360 :direct-superclasses supers
361 :direct-slots slots
362 other))
363
364 (setf (gdefinition 'load-defclass) #'real-load-defclass)
365
366 ;;;
367 ;;; The DEFCLASS spec says that this redefines a class with proper
368 ;;; name NAME, where S is the proper name of class C if S =
369 ;;; (CLASS-NAME C) and (FIND-CLASS S) = C.
370 ;;;
371 (defun ensure-class (name &rest all)
372 (let ((class (find-class name nil)))
373 (when (and class (neq name (class-name class)))
374 (setq class nil))
375 (apply #'ensure-class-using-class class name all)))
376
377 (defmethod ensure-class-using-class ((class null) name &rest args &key)
378 (multiple-value-bind (meta initargs)
379 (ensure-class-values class args)
380 (inform-type-system-about-class (class-prototype meta) name)
381 (let ((class (apply #'make-instance meta :name name initargs)))
382 (setf (find-class name) class)
383 (inform-type-system-about-class class name)
384 class)))
385
386 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
387 (multiple-value-bind (meta initargs)
388 (ensure-class-values class args)
389 (unless (eq (class-of class) meta)
390 (apply #'change-class class meta initargs))
391 (apply #'reinitialize-instance class initargs)
392 (setf (find-class name) class)
393 (inform-type-system-about-class class name)
394 class))
395
396 (defmethod class-predicate-name ((class t))
397 'function-returning-nil)
398
399 (defun ensure-class-values (class args)
400 (let* ((initargs (copy-list args))
401 (unsupplied (list 1))
402 (supplied-meta (getf initargs :metaclass unsupplied))
403 (supplied-supers (getf initargs :direct-superclasses unsupplied))
404 (meta (cond ((neq supplied-meta unsupplied)
405 (if (classp supplied-meta)
406 supplied-meta
407 (find-class supplied-meta)))
408 ((or (null class)
409 (forward-referenced-class-p class))
410 *the-class-standard-class*)
411 (t
412 (class-of class)))))
413 (flet ((fix-super (super)
414 (cond ((classp super)
415 super)
416 ((legal-class-name-p super)
417 (or (find-class super nil)
418 (make-instance 'forward-referenced-class
419 :name super)))
420 (t
421 (simple-program-error
422 "~@<~S is not a class or a legal class name.~@:>"
423 super)))))
424 ;;
425 ;; CLHS: signal PROGRAM-ERROR, if
426 ;; (a) there are any duplicate slot names
427 ;; (b) any of the slot options :ALLOCATION, :INITFORM, :TYPE, or
428 ;; :DOCUMENTATION appears more than one in a single slot description.
429 (loop for (slot . more) on (getf initargs :direct-slots)
430 for slot-name = (getf slot :name)
431 if (some (lambda (s) (eq slot-name (getf s :name))) more) do
432 (simple-program-error
433 "~@<More than one direct slot with name ~S.~@:>"
434 slot-name)
435 else do
436 (loop for (option value . more) on slot by #'cddr
437 if (and (member option '(:allocation :type :initform
438 :documentation))
439 (not (eq unsupplied
440 (getf more option unsupplied)))) do
441 (simple-program-error
442 "~@<Duplicate slot option ~S for slot ~S.~@:>"
443 option slot-name)
444 else if (and (eq option :readers)
445 (notevery #'symbolp value)) do
446 (simple-program-error
447 "~@<Slot ~S: slot reader names must be symbols.~@:>"
448 slot-name)
449 else if (and (eq option :initargs)
450 (notevery #'symbolp value)) do
451 (simple-program-error
452 "~@<Slot ~S: initarg names must be symbols.~@:>"
453 slot-name)))
454 ;;
455 ;; CLHS: signal PROGRAM-ERROR, if an initialization argument name
456 ;; appears more than once in :DEFAULT-INITARGS class option.
457 (loop for (initarg . more) on (getf initargs :direct-default-initargs)
458 for name = (car initarg)
459 when (some (lambda (a) (eq (car a) name)) more) do
460 (simple-program-error
461 "~@<Duplicate initialization argument ~
462 name ~S in ~s of class ~A.~@:>"
463 name :default-initargs class))
464 ;;
465 (loop for (arg value) on initargs by #'cddr
466 count (eq arg :metaclass) into metaclass
467 count (eq arg :direct-default-initargs) into default-initargs
468 when (or (> metaclass 1) (> default-initargs 1)) do
469 (simple-program-error
470 "~@<Class ~S: More than one ~S specified~@:>"
471 class (if (eq arg :direct-default-initargs)
472 :default-initargs arg)))
473 (remf initargs :metaclass)
474 (remf initargs :direct-superclasses)
475 ;;
476 (values meta
477 (nconc (when (neq supplied-supers unsupplied)
478 (list :direct-superclasses
479 (mapcar #'fix-super supplied-supers)))
480 initargs)))))
481
482
483 ;;;
484 ;;;
485 ;;;
486 (defmethod shared-initialize :after
487 ((class std-class)
488 slot-names
489 &key (direct-superclasses nil direct-superclasses-p)
490 (direct-slots nil direct-slots-p)
491 (direct-default-initargs nil direct-default-initargs-p)
492 (predicate-name nil predicate-name-p))
493 (declare (ignore slot-names))
494 (cond ((or direct-superclasses-p
495 (null (slot-value class 'direct-superclasses)))
496 (setq direct-superclasses
497 (or direct-superclasses
498 (list (if (funcallable-standard-class-p class)
499 *the-class-funcallable-standard-object*
500 *the-class-standard-object*))))
501 (dolist (superclass direct-superclasses)
502 (unless (validate-superclass class superclass)
503 (error "~@<The class ~S was specified as a ~
504 super-class of the class ~S, ~
505 but the meta-classes ~S and ~S are incompatible. ~
506 Define a method for ~S to avoid this error.~@:>"
507 superclass class (class-of superclass) (class-of class)
508 'validate-superclass)))
509 (setf (slot-value class 'direct-superclasses) direct-superclasses))
510 (t
511 (setq direct-superclasses (slot-value class 'direct-superclasses))))
512 (setq direct-slots
513 (if direct-slots-p
514 (setf (slot-value class 'direct-slots)
515 (mapcar (lambda (pl) (make-direct-slotd class pl))
516 direct-slots))
517 (slot-value class 'direct-slots)))
518 (if direct-default-initargs-p
519 (setf (plist-value class 'direct-default-initargs)
520 direct-default-initargs)
521 (setq direct-default-initargs
522 (plist-value class 'direct-default-initargs)))
523 ;;
524 ;; Initialize shared slots. A class may inherit initforms for
525 ;; shared slots from superclasses. Such initializations are
526 ;; done in UPDATE-CLASS-SLOT-VALUES.
527 (collect ((cells))
528 (dolist (dslotd direct-slots)
529 (when (eq (slot-definition-allocation dslotd) :class)
530 (let ((initfn (slot-definition-initfunction dslotd)))
531 (cells (cons (slot-definition-name dslotd)
532 (if initfn
533 (funcall initfn)
534 +slot-unbound+))))))
535 (setf (plist-value class 'class-slot-cells) (cells)))
536 ;;
537 (setq predicate-name (if predicate-name-p
538 (setf (slot-value class 'predicate-name)
539 (car predicate-name))
540 (or (slot-value class 'predicate-name)
541 (setf (slot-value class 'predicate-name)
542 (make-class-predicate-name (class-name class))))))
543 (add-direct-subclasses class direct-superclasses)
544 (make-class-predicate class predicate-name)
545 (update-class class nil)
546 (add-slot-accessors class direct-slots)
547 (make-preliminary-layout class))
548
549 (defmethod shared-initialize :after ((class forward-referenced-class)
550 slot-names &key &allow-other-keys)
551 (declare (ignore slot-names))
552 (make-preliminary-layout class))
553
554 ;;;
555 ;;; Give CLASS a preliminary layout, if it doesn't have a layout
556 ;;; already. This is done to make CLASS known to the type system
557 ;;; before the class is finalized, and is a consequence of the class
558 ;;; schizophrenia we are suffering from.
559 ;;;
560 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
561
562 (defun make-preliminary-layout (class)
563 (flet ((compute-preliminary-cpl (root)
564 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
565 (compute-class-precedence-list root))))
566 (unless (class-finalized-p class)
567 (let ((name (class-name class)))
568 (setf (find-class name) class)
569 (inform-type-system-about-class class name)
570 (let ((layout (make-wrapper 0 class))
571 (kernel-class (kernel::find-class name)))
572 (setf (kernel:layout-class layout) kernel-class)
573 (setf (kernel:%class-pcl-class kernel-class) class)
574 (setf (slot-value class 'wrapper) layout)
575 (let ((cpl (compute-preliminary-cpl class)))
576 (setf (kernel:layout-inherits layout)
577 (kernel:order-layout-inherits
578 (map 'simple-vector #'class-wrapper
579 (reverse (rest cpl))))))
580 (kernel:register-layout layout :invalidate t)
581 (setf (kernel:%class-layout kernel-class) layout)
582 (mapc #'make-preliminary-layout (class-direct-subclasses class)))))))
583
584 (defmethod shared-initialize :before ((class class) slot-names &key name)
585 (declare (ignore slot-names name))
586 (setf (slot-value class 'type) `(class ,class))
587 (setf (slot-value class 'class-eq-specializer)
588 (make-instance 'class-eq-specializer :class class)))
589
590 (defmethod reinitialize-instance :before ((class slot-class)
591 &key direct-superclasses)
592 (remove-direct-subclasses class direct-superclasses)
593 (remove-slot-accessors class (class-direct-slots class)))
594
595 (defmethod reinitialize-instance :after ((class slot-class)
596 &rest initargs
597 &key)
598 (map-dependents class
599 (lambda (dependent)
600 (apply #'update-dependent class dependent initargs))))
601
602 (defmethod shared-initialize :after
603 ((class structure-class)
604 slot-names
605 &key (direct-superclasses nil direct-superclasses-p)
606 (direct-slots nil direct-slots-p)
607 direct-default-initargs
608 (predicate-name nil predicate-name-p))
609 (declare (ignore slot-names direct-default-initargs))
610 (if direct-superclasses-p
611 (setf (slot-value class 'direct-superclasses)
612 (or direct-superclasses
613 (setq direct-superclasses
614 (and (not (eq (class-name class) 'structure-object))
615 (list *the-class-structure-object*)))))
616 (setq direct-superclasses (slot-value class 'direct-superclasses)))
617 (let* ((name (class-name class))
618 (from-defclass-p (slot-value class 'from-defclass-p))
619 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
620 (if direct-slots-p
621 (setf (slot-value class 'direct-slots)
622 (setq direct-slots
623 (mapcar (lambda (pl)
624 (when defstruct-p
625 (let* ((slot-name (getf pl :name))
626 (accessor
627 (symbolicate*
628 *package*
629 (if (symbol-package name)
630 (package-name (symbol-package name))
631 "")
632 "::" name " structure class " slot-name)))
633 (setq pl (list* :defstruct-accessor-symbol accessor
634 pl))))
635 (make-direct-slotd class pl))
636 direct-slots)))
637 (setq direct-slots (slot-value class 'direct-slots)))
638 (if defstruct-p
639 (let* ((include (car (slot-value class 'direct-superclasses)))
640 (conc-name (symbolicate* *package*
641 (if (symbol-package name)
642 (package-name (symbol-package name))
643 "")
644 "::" name " structure class "))
645 ;;
646 ;; It's not possible to use a generalized name for the
647 ;; constructor function. It shouldn't matter though, I think,
648 ;; like for the slot names above, because this stuff is not
649 ;; supposed to be used by users directly.
650 (constructor
651 (symbolicate* *package* conc-name " constructor"))
652 (defstruct `(defstruct (,name
653 ,@(when include
654 `((:include ,(class-name include))))
655 (:predicate nil)
656 (:conc-name ,conc-name)
657 (:constructor ,constructor ()))
658 ;;
659 ;; Use a temporary unbound marker that lets
660 ;; SHARED-INITIALIZE recognize if a before-method
661 ;; has written to a slot.
662 ,@(mapcar (lambda (slot)
663 `(,(slot-definition-name slot)
664 '.unbound.))
665 direct-slots)))
666 (reader-names (mapcar (lambda (slotd)
667 (list 'slot-accessor name
668 (slot-definition-name slotd)
669 'reader))
670 direct-slots))
671 (writer-names (mapcar (lambda (slotd)
672 (list 'slot-accessor name
673 (slot-definition-name slotd)
674 'writer))
675 direct-slots))
676 (readers-init
677 (mapcar (lambda (slotd reader-name)
678 (let ((accessor
679 (slot-definition-defstruct-accessor-symbol slotd)))
680 `(defun ,reader-name (obj)
681 (declare (type ,name obj))
682 (,accessor obj))))
683 direct-slots reader-names))
684 (writers-init
685 (mapcar (lambda (slotd writer-name)
686 (let ((accessor
687 (slot-definition-defstruct-accessor-symbol slotd)))
688 `(defun ,writer-name (nv obj)
689 (declare (type ,name obj))
690 (setf (,accessor obj) nv))))
691 direct-slots writer-names))
692 (defstruct-form
693 `(progn
694 ,defstruct
695 ,@readers-init ,@writers-init)))
696 (unless (structure-type-p name) (eval defstruct-form))
697 (mapc (lambda (dslotd reader-name writer-name)
698 (let* ((reader (when (fboundp reader-name)
699 (gdefinition reader-name)))
700 (writer (when (fboundp writer-name)
701 (gdefinition writer-name))))
702 (setf (slot-value dslotd 'internal-reader-function) reader)
703 (setf (slot-value dslotd 'internal-writer-function) writer)))
704 direct-slots reader-names writer-names)
705 (setf (slot-value class 'defstruct-form) defstruct-form)
706 (setf (slot-value class 'defstruct-constructor) constructor))
707 ;;
708 ;; ALLOCATE-INSTANCE is supposed to work with structures
709 ;; defined with DEFSTRUCT.
710 (with-slots (defstruct-constructor) class
711 (setq defstruct-constructor
712 (make-defstruct-allocation-function class)))))
713 ;;
714 (add-direct-subclasses class direct-superclasses)
715 (setf (slot-value class 'class-precedence-list)
716 (compute-class-precedence-list class))
717 (setf (slot-value class 'slots) (compute-slots class))
718 (let ((lclass (kernel::find-class (class-name class))))
719 (setf (kernel:%class-pcl-class lclass) class)
720 (setf (slot-value class 'wrapper) (kernel:%class-layout lclass)))
721 (setf (slot-value class 'finalized-p) t)
722 (update-pv-table-cache-info class)
723 (setq predicate-name (if predicate-name-p
724 (setf (slot-value class 'predicate-name)
725 (car predicate-name))
726 (or (slot-value class 'predicate-name)
727 (setf (slot-value class 'predicate-name)
728 (make-class-predicate-name (class-name class))))))
729 (make-class-predicate class predicate-name)
730 (add-slot-accessors class direct-slots))
731
732 ;;;
733 ;;; Return a closure for allocating an uninitialized structure
734 ;;; instance of class CLASS.
735 ;;;
736 (defun make-defstruct-allocation-function (class)
737 (let ((dd (get-structure-dd (class-name class))))
738 (lambda ()
739 (let ((instance (kernel::%make-instance (kernel::dd-length dd)))
740 (raw-index (kernel::dd-raw-index dd)))
741 (setf (kernel::%instance-layout instance)
742 (kernel::compiler-layout-or-lose (kernel::dd-name dd)))
743 (when raw-index
744 (setf (kernel::%instance-ref instance raw-index)
745 (make-array (kernel::dd-raw-length dd)
746 :element-type '(unsigned-byte 32))))
747 instance))))
748
749 (defmethod direct-slot-definition-class ((class structure-class)
750 &rest initargs)
751 (declare (ignore initargs))
752 (find-class 'structure-direct-slot-definition))
753
754 (defmethod finalize-inheritance ((class structure-class))
755 nil) ; always finalized
756
757 (defun add-slot-accessors (class dslotds)
758 (fix-slot-accessors class dslotds 'add))
759
760 (defun remove-slot-accessors (class dslotds)
761 (fix-slot-accessors class dslotds 'remove))
762
763 (defun fix-slot-accessors (class dslotds add/remove)
764 (flet ((fix (gfspec name r/w)
765 (let ((gf (ensure-generic-function gfspec)))
766 (case r/w
767 (r (if (eq add/remove 'add)
768 (add-reader-method class gf name)
769 (remove-reader-method class gf)))
770 (w (if (eq add/remove 'add)
771 (add-writer-method class gf name)
772 (remove-writer-method class gf)))))))
773 (dolist (dslotd dslotds)
774 (let ((slot-name (slot-definition-name dslotd)))
775 (dolist (r (slot-definition-readers dslotd))
776 (fix r slot-name 'r))
777 (dolist (w (slot-definition-writers dslotd))
778 (fix w slot-name 'w))))))
779
780
781 (defun add-direct-subclasses (class new)
782 (dolist (n new)
783 (unless (memq class (class-direct-subclasses class))
784 (add-direct-subclass n class))))
785
786 (defun remove-direct-subclasses (class new)
787 (let ((old (class-direct-superclasses class)))
788 (dolist (o (set-difference old new))
789 (remove-direct-subclass o class))))
790
791
792 ;;;
793 ;;;
794 ;;;
795 (defmethod finalize-inheritance ((class std-class))
796 (update-class class t))
797
798 (defmethod finalize-inheritance ((class forward-referenced-class))
799 (simple-program-error
800 "~@<Forward-referenced classes cannot be finalized: ~A.~@:>"
801 class))
802
803
804 ;;;
805 ;;; Called by :after shared-initialize whenever a class is initialized or
806 ;;; reinitialized. The class may or may not be finalized.
807 ;;;
808 (defun update-class (class finalizep)
809 ;;
810 ;; Calling UPDATE-SLOTS below sets the class wrapper of CLASS, which
811 ;; makes the class finalized. When UPDATE-CLASS isn't called from
812 ;; FINALIZE-INHERITANCE, make sure that this finalization invokes
813 ;; FINALIZE-INHERITANCE as per AMOP. Note that we can't simply
814 ;; delay the finalization when CLASS has no forward referenced
815 ;; superclasses because that causes bootstrap problems.
816 (when (and (not (or finalizep (class-finalized-p class)))
817 (not (class-has-a-forward-referenced-superclass-p class)))
818 (finalize-inheritance class)
819 (dolist (sub (class-direct-subclasses class))
820 (update-class sub nil))
821 (return-from update-class))
822 ;;
823 (when (or finalizep
824 (class-finalized-p class)
825 (not (class-has-a-forward-referenced-superclass-p class)))
826 (setf (find-class (class-name class)) class)
827 (let ((*allow-forward-referenced-classes-in-cpl-p*
828 (class-has-a-forward-referenced-superclass-p class)))
829 (update-cpl class (compute-class-precedence-list class)))
830 (update-slots class (compute-slots class))
831 (update-gfs-of-class class)
832 (update-inits class (compute-default-initargs class))
833 (update-shared-slot-values class)
834 (update-ctors 'finalize-inheritance :class class))
835 ;;
836 (unless finalizep
837 (dolist (sub (class-direct-subclasses class))
838 (update-class sub nil))))
839
840 ;;;
841 ;;; Set values of shared slots from initforms inherited from
842 ;;; superclasses, which can't be done before the cpl is known.
843 ;;;
844 (defun update-shared-slot-values (class)
845 (dolist (slot (class-slots class))
846 (when (eq (slot-definition-allocation slot) :class)
847 (let ((cell (assq (slot-definition-name slot) (class-slot-cells class))))
848 (when (and cell (eq +slot-unbound+ (cdr cell)))
849 (let ((initfn (slot-definition-initfunction slot)))
850 (when initfn
851 (setf (cdr cell) (funcall initfn)))))))))
852
853 (defun update-cpl (class cpl)
854 (if (class-finalized-p class)
855 (unless (and (equal (class-precedence-list class) cpl)
856 (loop for c in cpl never
857 (loop for s in (class-direct-slots c) thereis
858 (eq (slot-definition-allocation s)
859 :class))))
860 ;; Need to have the cpl setup before update-lisp-class-layout
861 ;; is called on CMUCL.
862 (setf (slot-value class 'class-precedence-list) cpl)
863 (force-cache-flushes class))
864 (setf (slot-value class 'class-precedence-list) cpl))
865 (update-class-can-precede-p cpl))
866
867 (defun update-class-can-precede-p (cpl)
868 (loop for (class . rest) on cpl do
869 (with-slots (can-precede-list) class
870 (setq can-precede-list
871 (union rest can-precede-list :test #'eq)))))
872
873 (defun class-can-precede-p (class1 class2)
874 (member class2 (class-can-precede-list class1)))
875
876 (defun update-slots (class eslotds)
877 (collect ((instance-slots) (class-slots))
878 (dolist (eslotd eslotds)
879 (case (slot-definition-allocation eslotd)
880 (:instance (instance-slots eslotd))
881 (:class (class-slots eslotd))))
882 ;;
883 ;; If there is a change in the shape of the instances then the
884 ;; old class is now obsolete.
885 (let* ((nlayout (mapcar #'slot-definition-name
886 (sort (instance-slots) #'<
887 :key #'slot-definition-location)))
888 (nslots (length nlayout))
889 (nwrapper-class-slots (compute-class-slots (class-slots)))
890 (owrapper (when (class-finalized-p class)
891 (class-wrapper class)))
892 (olayout (when owrapper
893 (wrapper-instance-slots-layout owrapper)))
894 (nwrapper
895 (cond ((null owrapper)
896 (make-wrapper nslots class))
897 ;;
898 ;; We cannot reuse the old wrapper easily when it
899 ;; has class slot cells, even if these cells are
900 ;; EQUAL to the ones used in the new wrapper. The
901 ;; class slot cells of OWRAPPER may be referenced
902 ;; from caches, and if we don't change the wrapper,
903 ;; the caches won't notice that something has
904 ;; changed. We could do something here manually,
905 ;; but I don't think it's worth it.
906 ((and (equal nlayout olayout)
907 (null (wrapper-class-slots owrapper)))
908 owrapper)
909 (t
910 ;;
911 ;; This will initialize the new wrapper to have the same
912 ;; state as the old wrapper. We will then have to change
913 ;; that. This may seem like wasted work (it is), but the
914 ;; spec requires that we call make-instances-obsolete.
915 (make-instances-obsolete class)
916 (class-wrapper class)))))
917
918 (with-slots (wrapper slots finalized-p) class
919 (update-lisp-class-layout class nwrapper)
920 (setf slots eslotds
921 (wrapper-instance-slots-layout nwrapper) nlayout
922 (wrapper-class-slots nwrapper) nwrapper-class-slots
923 (wrapper-no-of-instance-slots nwrapper) nslots
924 wrapper nwrapper
925 finalized-p t))
926
927 (unless (eq owrapper nwrapper)
928 (update-inline-access class)
929 (update-pv-table-cache-info class)
930 (maybe-update-standard-class-locations class)))))
931
932 (defun compute-class-slots (eslotds)
933 (loop for eslotd in eslotds
934 for name = (slot-definition-name eslotd)
935 and class = (slot-definition-class eslotd)
936 collect (assoc name (class-slot-cells class))))
937
938 (defun update-gfs-of-class (class)
939 (when (and (class-finalized-p class)
940 (let ((cpl (class-precedence-list class)))
941 (or (member *the-class-slot-class* cpl)
942 (member *the-class-standard-effective-slot-definition* cpl))))
943 (let ((gf-table (make-hash-table :test 'eq)))
944 (labels ((collect-gfs (class)
945 (dolist (gf (specializer-direct-generic-functions class))
946 (setf (gethash gf gf-table) t))
947 (mapc #'collect-gfs (class-direct-superclasses class))))
948 (collect-gfs class)
949 (maphash (lambda (gf ignore)
950 (declare (ignore ignore))
951 (update-gf-dfun class gf))
952 gf-table)))))
953
954 (defun update-inits (class inits)
955 (setf (plist-value class 'default-initargs) inits))
956
957
958 ;;;
959 ;;;
960 ;;;
961 (defmethod compute-default-initargs ((class slot-class))
962 (let ((initargs (loop for c in (class-precedence-list class)
963 append (class-direct-default-initargs c))))
964 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
965
966 ;;;
967 ;;; Protocols for constructing direct and effective slot definitions.
968 ;;;
969 ;;;
970 ;;;
971 ;;;
972 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
973 (declare (ignore initargs))
974 (find-class 'standard-direct-slot-definition))
975
976 (defun make-direct-slotd (class initargs)
977 (let ((initargs (list* :class class initargs)))
978 (apply #'make-instance
979 (apply #'direct-slot-definition-class class initargs)
980 initargs)))
981
982 ;;;
983 ;;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
984 ;;; for each different slot name we find in our superclasses. Each
985 ;;; call receives the class and a list of the dslotds with that name.
986 ;;; The list is in most-specific-first order.
987 ;;;
988 (defmethod compute-slots ((class std-class))
989 (collect ((names/slots) (effective))
990 (dolist (c (reverse (class-precedence-list class)))
991 (dolist (slot (class-direct-slots c))
992 (let* ((name (slot-definition-name slot))
993 (entry (assq name (names/slots))))
994 (if entry
995 (push slot (cdr entry))
996 (names/slots (list name slot))))))
997 (dolist (e (names/slots) (effective))
998 (effective (compute-effective-slot-definition class (car e) (cdr e))))))
999
1000 ;;;
1001 ;;; These are the specified AMOP methods.
1002 ;;;
1003
1004 (defmethod compute-slots ((class standard-class))
1005 (call-next-method))
1006
1007 (defmethod compute-slots :around ((class standard-class))
1008 (let ((eslotds (call-next-method))
1009 (location -1)
1010 (slot-names ()))
1011 (dolist (eslotd eslotds eslotds)
1012 (let ((allocation (slot-definition-allocation eslotd))
1013 (name (slot-definition-name eslotd)))
1014 ;;
1015 ;; Users are free to override COMPUTE-SLOTS, and they are
1016 ;; arguably free to MAKE-INSTANCE effective slot definition
1017 ;; metaobjects. Such objects won't be initialized completely,
1018 ;; from the standpoint of PCL, so we have to fix them.
1019 ;;
1020 ;; If such an effective slot definition has the same name as
1021 ;; another slot, we certainly lose, at least if it's a class
1022 ;; slot. I'm deliberately ignoring that for now.
1023 ;;
1024 ;; Note that COMPUTE-SLOTS can be called multiple times.
1025 ;; We don't want to create a new class slot cell every time
1026 ;; around.
1027 (when (null (slot-definition-class eslotd))
1028 (setf (slot-definition-class eslotd) class)
1029 (when (eq allocation :class)
1030 (unless (assq name (class-slot-cells class))
1031 (push (cons name +slot-unbound+)
1032 (plist-value class 'class-slot-cells)))
1033 (setf (slot-definition-allocation-class eslotd) class)))
1034 ;;
1035 ;; Assign slot locations.
1036 (setf (slot-definition-location eslotd)
1037 (case allocation
1038 (:instance
1039 (incf location))
1040 (:class
1041 (let* ((from-class (slot-definition-allocation-class eslotd))
1042 (cell (assq name (class-slot-cells from-class))))
1043 (assert (consp cell))
1044 cell))))
1045 (initialize-internal-slot-functions eslotd)))))
1046
1047 (defmethod compute-slots ((class funcallable-standard-class))
1048 (call-next-method))
1049
1050 (defmethod compute-slots :around ((class funcallable-standard-class))
1051 (labels (;;
1052 ;; Return a list of the names of instance slots in SLOTDS.
1053 (instance-slot-names (slotds)
1054 (loop for e in slotds
1055 when (eq (slot-definition-allocation e) :instance)
1056 collect (slot-definition-name e)))
1057 ;;
1058 ;; This sorts slots so that slots of classes later in the CPL
1059 ;; come before slots of other classes. This is crucial for
1060 ;; funcallable instances because it ensures that the slots of
1061 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
1062 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
1063 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
1064 ;; a funcallable instance.
1065 (compute-layout (eslotds)
1066 (loop with first = ()
1067 with names = (instance-slot-names eslotds)
1068 for class in (reverse (class-precedence-list class)) do
1069 (loop for ss in (class-slots class)
1070 as name = (slot-definition-name ss)
1071 when (member name names) do
1072 (push name first)
1073 (setq names (delete name names)))
1074 finally (return (nreverse (nconc names first))))))
1075 ;;
1076 (let ((all-slotds (call-next-method))
1077 (instance-slots ())
1078 (class-slots ()))
1079 (loop for slotd in all-slotds do
1080 (case (slot-definition-allocation slotd)
1081 (:instance (push slotd instance-slots))
1082 (:class (push slotd class-slots))))
1083 (loop with layout = (compute-layout instance-slots)
1084 for slotd in instance-slots do
1085 (setf (slot-definition-location slotd)
1086 (position (slot-definition-name slotd) layout))
1087 (initialize-internal-slot-functions slotd))
1088 (loop for slotd in class-slots
1089 as name = (slot-definition-name slotd)
1090 as from-class = (slot-definition-allocation-class slotd) do
1091 (setf (slot-definition-location slotd)
1092 (assoc name (class-slot-cells from-class)))
1093 (assert (consp (slot-definition-location slotd)))
1094 (initialize-internal-slot-functions slotd))
1095 all-slotds)))
1096
1097 (defmethod compute-slots ((class structure-class))
1098 (mapcan (lambda (superclass)
1099 (mapcar (lambda (dslotd)
1100 (compute-effective-slot-definition
1101 class (slot-definition-name dslotd) (list dslotd)))
1102 (class-direct-slots superclass)))
1103 (reverse (slot-value class 'class-precedence-list))))
1104
1105 (defmethod compute-slots :around ((class structure-class))
1106 (let ((eslotds (call-next-method)))
1107 (mapc #'initialize-internal-slot-functions eslotds)
1108 eslotds))
1109
1110 (defvar *allow-funny-slot-names* nil)
1111
1112 (defmethod initialize-internal-slot-functions :around
1113 ((slotd structure-effective-slot-definition))
1114 (let ((*allow-funny-slot-names* t))
1115 (call-next-method)))
1116
1117 (defmethod compute-effective-slot-definition
1118 ((class slot-class) slot-name dslotds)
1119 (declare (ignore slot-name))
1120 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1121 (class (apply #'effective-slot-definition-class class initargs)))
1122 (apply #'make-instance class initargs)))
1123
1124 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1125 (declare (ignore initargs))
1126 (find-class 'standard-effective-slot-definition))
1127
1128 (defmethod effective-slot-definition-class ((class structure-class)
1129 &rest initargs)
1130 (declare (ignore initargs))
1131 (find-class 'structure-effective-slot-definition))
1132
1133 (defmethod compute-effective-slot-definition-initargs
1134 ((class slot-class) direct-slotds)
1135 (let* ((name nil)
1136 (initfunction nil)
1137 (initform nil)
1138 (initargs nil)
1139 (allocation nil)
1140 (allocation-class nil)
1141 (type t)
1142 (namep nil)
1143 (initp nil)
1144 (allocp nil))
1145
1146 (dolist (slotd direct-slotds)
1147 (when slotd
1148 (unless namep
1149 (setq name (slot-definition-name slotd)
1150 namep t))
1151 (unless initp
1152 (when (slot-definition-initfunction slotd)
1153 (setq initform (slot-definition-initform slotd)
1154 initfunction (slot-definition-initfunction slotd)
1155 initp t)))
1156 (unless allocp
1157 (setq allocation (slot-definition-allocation slotd)
1158 allocation-class (slot-definition-class slotd)
1159 allocp t))
1160 (setq initargs (append (slot-definition-initargs slotd) initargs))
1161 (let ((slotd-type (slot-definition-type slotd)))
1162 (setq type (cond ((eq type t) slotd-type)
1163 ((*subtypep type slotd-type) type)
1164 (t `(and ,type ,slotd-type)))))))
1165 (list :name name
1166 :initform initform
1167 :initfunction initfunction
1168 :initargs initargs
1169 :allocation allocation
1170 :allocation-class allocation-class
1171 :type type
1172 :class class)))
1173
1174 (defmethod compute-effective-slot-definition-initargs :around
1175 ((class structure-class) direct-slotds)
1176 (let ((slotd (car direct-slotds)))
1177 (list* :defstruct-accessor-symbol
1178 (slot-definition-defstruct-accessor-symbol slotd)
1179 :internal-reader-function
1180 (slot-definition-internal-reader-function slotd)
1181 :internal-writer-function
1182 (slot-definition-internal-writer-function slotd)
1183 (call-next-method))))
1184
1185 ;;;
1186 ;;; NOTE: For bootstrapping considerations, these can't use make-instance
1187 ;;; to make the method object. They have to use make-a-method which
1188 ;;; is a specially bootstrapped mechanism for making standard methods.
1189 ;;;
1190 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1191 (declare (ignore direct-slot initargs))
1192 (find-class 'standard-reader-method))
1193
1194 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1195 (add-method generic-function
1196 (make-a-method 'standard-reader-method
1197 ()
1198 (list (or (class-name class) 'object))
1199 (list class)
1200 (make-reader-method-function class slot-name)
1201 "automatically generated reader method"
1202 slot-name)))
1203
1204 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1205 (declare (ignore direct-slot initargs))
1206 (find-class 'standard-writer-method))
1207
1208 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1209 (add-method generic-function
1210 (make-a-method 'standard-writer-method
1211 ()
1212 (list 'new-value (or (class-name class) 'object))
1213 (list *the-class-t* class)
1214 (make-writer-method-function class slot-name)
1215 "automatically generated writer method"
1216 slot-name)))
1217
1218 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1219 (add-method generic-function
1220 (make-a-method 'standard-boundp-method
1221 ()
1222 (list (or (class-name class) 'object))
1223 (list class)
1224 (make-boundp-method-function class slot-name)
1225 "automatically generated boundp method"
1226 slot-name)))
1227
1228 (defmethod remove-reader-method ((class slot-class) generic-function)
1229 (let ((method (get-method generic-function () (list class) nil)))
1230 (when method
1231 (remove-method generic-function method))))
1232
1233 (defmethod remove-writer-method ((class slot-class) generic-function)
1234 (let ((method (get-method generic-function ()
1235 (list *the-class-t* class) nil)))
1236 (when method
1237 (remove-method generic-function method))))
1238
1239 (defmethod remove-boundp-method ((class slot-class) generic-function)
1240 (let ((method (get-method generic-function () (list class) nil)))
1241 (when method
1242 (remove-method generic-function method))))
1243
1244
1245 ;;;
1246 ;;; make-reader-method-function and make-write-method function are NOT part of
1247 ;;; the standard protocol. They are however useful, PCL makes uses makes use
1248 ;;; of them internally and documents them for PCL users.
1249 ;;;
1250 ;;; *** This needs work to make type testing by the writer functions which
1251 ;;; *** do type testing faster. The idea would be to have one constructor
1252 ;;; *** for each possible type test. In order to do this it would be nice
1253 ;;; *** to have help from inform-type-system-about-class and friends.
1254 ;;;
1255 ;;; *** There is a subtle bug here which is going to have to be fixed.
1256 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1257 ;;; *** have to give the optimize-slot-value method the user might have
1258 ;;; *** defined for this metclass a chance to run.
1259 ;;;
1260 (defmethod make-reader-method-function ((class slot-class) slot-name)
1261 (make-std-reader-method-function (class-name class) slot-name))
1262
1263 (defmethod make-writer-method-function ((class slot-class) slot-name)
1264 (make-std-writer-method-function (class-name class) slot-name))
1265
1266 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1267 (make-std-boundp-method-function (class-name class) slot-name))
1268
1269
1270 ;;;; inform-type-system-about-class
1271 ;;;
1272 ;;; This is NOT part of the standard protocol. It is an internal mechanism
1273 ;;; which PCL uses to *try* and tell the type system about class definitions.
1274 ;;; In a more fully integrated implementation of CLOS, the type system would
1275 ;;; know about class objects and class names in a more fundamental way and
1276 ;;; the mechanism used to inform the type system about new classes would be
1277 ;;; different.
1278 ;;;
1279 (defmethod inform-type-system-about-class ((class std-class) name)
1280 ;; Maybe add skeleton lisp:standard-class to avoid undefined-function
1281 ;; compiler warnings. Not otherwise needed in this implementation.
1282 (inform-type-system-about-std-class name)
1283 (set-class-translation class name))
1284
1285 (defmethod inform-type-system-about-class ((class funcallable-standard-class)
1286 name)
1287 (set-class-translation class name))
1288
1289 (defmethod inform-type-system-about-class ((class structure-class) name)
1290 (set-class-translation class name))
1291
1292 (defmethod inform-type-system-about-class ((class condition-class) name)
1293 (set-class-translation class name))
1294
1295 (defmethod inform-type-system-about-class ((class forward-referenced-class)
1296 name)
1297 (inform-type-system-about-std-class name)
1298 (set-class-translation class name))
1299
1300
1301 (defmethod compatible-meta-class-change-p (class proto-new-class)
1302 (eq (class-of class) (class-of proto-new-class)))
1303
1304 (defmethod validate-superclass ((class class) (new-super class))
1305 (or (eq new-super *the-class-t*)
1306 (eq (class-of class) (class-of new-super))))
1307
1308 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1309 (let ((new-super-meta-class (class-of new-super)))
1310 (or (eq new-super-meta-class *the-class-std-class*)
1311 (eq (class-of class) new-super-meta-class))))
1312
1313
1314 ;;;
1315 ;;; Force the flushing of caches by creating a new wrapper for CLASS,
1316 ;;; if necessary.
1317 ;;;
1318 ;;; If the LAYOUT-INVALID slot of CLASS's wrapper is
1319 ;;;
1320 ;;; -- (:FLUSH <new wrapper>) or (:OBSOLETE <new wrapper>), there's
1321 ;;; nothing to do. The new wrapper has already been created.
1322 ;;;
1323 ;;; -- :INVALID, then it has been set to that value by a previous
1324 ;;; call to REGISTER-LAYOUT for a superclass S of CLASS; S's
1325 ;;; wrapper has been invalidated together with its subclasses. In
1326 ;;; this case, CLASS's caches must obviously be flushed, too, like
1327 ;;; S's. So, make a new wrapper for CLASS, and translate kernel's
1328 ;;; :INVALID to PCL (:FLUSH <new wrapper>). UPDATE-SLOTS can later
1329 ;;; decide if it wants to make this (:OBSOLETE ...).
1330 ;;;
1331 ;;; -- NIL, then the wrapper is still valid, in which case we do
1332 ;;; the same as for :INVALID, but for the obviously slightly
1333 ;;; different reason.
1334 ;;;
1335 (defun force-cache-flushes (class)
1336 (let* ((owrapper (class-wrapper class)))
1337 (when (or (not (invalid-wrapper-p owrapper))
1338 (eq :invalid (kernel:layout-invalid owrapper)))
1339 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1340 class)))
1341 (setf (wrapper-instance-slots-layout nwrapper)
1342 (wrapper-instance-slots-layout owrapper))
1343 (setf (wrapper-class-slots nwrapper)
1344 (wrapper-class-slots owrapper))
1345 (with-pcl-lock
1346 (update-lisp-class-layout class nwrapper)
1347 (setf (slot-value class 'wrapper) nwrapper)
1348 (flet ((obsolete-super-p ()
1349 (some (lambda (layout)
1350 (eq (car-safe (kernel:layout-invalid layout))
1351 :obsolete))
1352 (kernel:layout-inherits owrapper))))
1353 (invalidate-wrapper owrapper
1354 (if (obsolete-super-p) :obsolete :flush)
1355 nwrapper)))))))
1356
1357 (defun flush-cache-trap (owrapper nwrapper instance)
1358 (declare (ignore owrapper))
1359 (cond ((std-instance-p instance)
1360 (setf (std-instance-wrapper instance) nwrapper))
1361 ((fsc-instance-p instance)
1362 (setf (fsc-instance-wrapper instance) nwrapper))
1363 (t
1364 (internal-error "Internal error."))))
1365
1366
1367 ;;;
1368 ;;; make-instances-obsolete can be called by user code. It will cause the
1369 ;;; next access to the instance (as defined in 88-002R) to trap through the
1370 ;;; update-instance-for-redefined-class mechanism.
1371 ;;;
1372 (defmethod make-instances-obsolete ((class std-class))
1373 (let* ((owrapper (class-wrapper class))
1374 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1375 class)))
1376 (setf (wrapper-instance-slots-layout nwrapper)
1377 (wrapper-instance-slots-layout owrapper))
1378 (setf (wrapper-class-slots nwrapper)
1379 (wrapper-class-slots owrapper))
1380 (with-pcl-lock
1381 (update-lisp-class-layout class nwrapper)
1382 (setf (slot-value class 'wrapper) nwrapper)
1383 (invalidate-wrapper owrapper :obsolete nwrapper)
1384 class)))
1385
1386 (defmethod make-instances-obsolete ((class symbol))
1387 (make-instances-obsolete (find-class class)))
1388
1389
1390 ;;;
1391 ;;; obsolete-instance-trap is the internal trap that is called when we see
1392 ;;; an obsolete instance. The times when it is called are:
1393 ;;; - when the instance is involved in method lookup
1394 ;;; - when attempting to access a slot of an instance
1395 ;;;
1396 ;;; It is not called by class-of, wrapper-of, or any of the low-level instance
1397 ;;; access macros.
1398 ;;;
1399 ;;; Of course these times when it is called are an internal implementation
1400 ;;; detail of PCL and are not part of the documented description of when the
1401 ;;; obsolete instance update happens. The documented description is as it
1402 ;;; appears in 88-002R.
1403 ;;;
1404 ;;; This has to return the new wrapper, so it counts on all the methods on
1405 ;;; obsolete-instance-trap-internal to return the new wrapper. It also does
1406 ;;; a little internal error checking to make sure that the traps are only
1407 ;;; happening when they should, and that the trap methods are computing
1408 ;;; apropriate new wrappers.
1409 ;;;
1410
1411 ;;; obsolete-instance-trap might be called on structure instances
1412 ;;; after a structure is redefined. In most cases, obsolete-instance-trap
1413 ;;; will not be able to fix the old instance, so it must signal an
1414 ;;; error. The hard part of this is that the error system and debugger
1415 ;;; might cause obsolete-instance-trap to be called again, so in that
1416 ;;; case, we have to return some reasonable wrapper, instead.
1417
1418 (defvar *in-obsolete-instance-trap* nil)
1419
1420 (defvar *the-wrapper-of-structure-object*
1421 (class-wrapper (find-class 'structure-object)))
1422
1423 (define-condition obsolete-structure (error)
1424 ((datum :reader obsolete-structure-datum :initarg :datum))
1425 (:report
1426 (lambda (condition stream)
1427 ;; Don't try to print the structure, since it probably
1428 ;; won't work.
1429 (format stream "~@<Obsolete structure error in ~S ~
1430 for a structure of type ~S.~@:>"
1431 (conditions::condition-function-name condition)
1432 (type-of (obsolete-structure-datum condition))))))
1433
1434 (defun obsolete-instance-trap (owrapper nwrapper instance)
1435 (if (not (pcl-instance-p instance))
1436 (if *in-obsolete-instance-trap*
1437 *the-wrapper-of-structure-object*
1438 (let ((*in-obsolete-instance-trap* t))
1439 (error 'obsolete-structure :datum instance)))
1440 (let* ((class (wrapper-class* nwrapper))
1441 (copy (allocate-instance class)) ;??? allocate-instance ???
1442 (olayout (wrapper-instance-slots-layout owrapper))
1443 (nlayout (wrapper-instance-slots-layout nwrapper))
1444 (oslots (get-slots instance))
1445 (nslots (get-slots copy))
1446 (oclass-slots (wrapper-class-slots owrapper))
1447 (added ())
1448 (discarded ())
1449 (plist ()))
1450 ;;
1451 ;; Collect inherited class slots. Note that LAYOUT-INHERITS is
1452 ;; ordered from most general to most specific.
1453 (loop for layout across (reverse (kernel:layout-inherits owrapper))
1454 when (typep layout 'wrapper) do
1455 (loop for slot in (wrapper-class-slots layout) do
1456 (pushnew slot oclass-slots :key #'car)))
1457 ;;
1458 ;; local --> local transfer
1459 ;; local --> shared discard
1460 ;; local --> -- discard
1461 ;; shared --> local transfer
1462 ;; shared --> shared discard
1463 ;; shared --> -- discard
1464 ;; -- --> local add
1465 ;; -- --> shared --
1466 ;;
1467 ;; Go through all the old local slots.
1468 ;;
1469 (loop for name in olayout and opos from 0
1470 as npos = (posq name nlayout)
1471 if npos do
1472 (setf (slot-ref nslots npos)
1473 (slot-ref oslots opos))
1474 else do
1475 (push name discarded)
1476 (unless (eq (slot-ref oslots opos) +slot-unbound+)
1477 (setf (getf plist name)
1478 (slot-ref oslots opos))))
1479 ;;
1480 ;; Go through all the old shared slots.
1481 ;;
1482 (loop for (name . val) in oclass-slots
1483 for npos = (posq name nlayout)
1484 if npos do
1485 (setf (slot-ref nslots npos) val)
1486 else do
1487 (push name discarded)
1488 (unless (eq val +slot-unbound+)
1489 (setf (getf plist name) val)))
1490 ;;
1491 ;; Go through all the new local slots to compute the added slots.
1492 ;;
1493 (dolist (nlocal nlayout)
1494 (unless (or (memq nlocal olayout)
1495 (assq nlocal oclass-slots))
1496 (push nlocal added)))
1497
1498 (swap-wrappers-and-slots instance copy)
1499
1500 (update-instance-for-redefined-class instance
1501 added
1502 discarded
1503 plist)
1504 nwrapper)))
1505
1506
1507 ;;;
1508 ;;;
1509 ;;;
1510 (defun change-class-internal (instance new-class initargs)
1511 (let* ((old-class (class-of instance))
1512 (copy (allocate-instance new-class))
1513 (new-wrapper (get-wrapper copy))
1514 (old-wrapper (class-wrapper old-class))
1515 (old-layout (wrapper-instance-slots-layout old-wrapper))
1516 (new-layout (wrapper-instance-slots-layout new-wrapper))
1517 (old-slots (get-slots instance))
1518 (new-slots (get-slots copy))
1519 (old-class-slots (wrapper-class-slots old-wrapper)))
1520
1521 ;;
1522 ;; "The values of local slots specified by both the class Cto and
1523 ;; Cfrom are retained. If such a local slot was unbound, it remains
1524 ;; unbound."
1525 ;;
1526 (loop for new-slot in new-layout and new-position from 0
1527 for old-position = (posq new-slot old-layout)
1528 when old-position do
1529 (setf (slot-ref new-slots new-position)
1530 (slot-ref old-slots old-position)))
1531 ;;
1532 ;; "The values of slots specified as shared in the class Cfrom and
1533 ;; as local in the class Cto are retained."
1534 ;;
1535 (loop for (name . val) in old-class-slots
1536 for new-position = (posq name new-layout)
1537 when new-position do
1538 (setf (slot-ref new-slots new-position) val))
1539
1540 ;; Make the copy point to the old instance's storage, and make the
1541 ;; old instance point to the new storage.
1542 (swap-wrappers-and-slots instance copy)
1543
1544 (apply #'update-instance-for-different-class copy instance initargs)
1545 instance))
1546
1547 (defmethod change-class ((instance standard-object)
1548 (new-class standard-class)
1549 &rest initargs &key)
1550 (change-class-internal instance new-class initargs))
1551
1552 (defmethod change-class ((instance funcallable-standard-object)
1553 (new-class funcallable-standard-class)
1554 &rest initargs &key)
1555 (change-class-internal instance new-class initargs))
1556
1557 (defmethod change-class ((instance standard-object)
1558 (new-class funcallable-standard-class)
1559 &rest initargs &key)
1560 (declare (ignore initargs))
1561 (error "~@<Can't change the class of ~S to ~S ~
1562 because it isn't already an instance with metaclass ~S.~@:>"
1563 instance new-class 'standard-class))
1564
1565 (defmethod change-class ((instance funcallable-standard-object)
1566 (new-class standard-class)
1567 &rest initargs &key)
1568 (declare (ignore initargs))
1569 (error "~@<Can't change the class of ~S to ~S ~
1570 because it isn't already an instance with metaclass ~S.~@:>"
1571 instance new-class 'funcallable-standard-class))
1572
1573 (defmethod change-class ((instance t) (new-class-name symbol)
1574 &rest initargs &key)
1575 (apply #'change-class instance (find-class new-class-name) initargs))
1576
1577
1578
1579 ;;;
1580 ;;; The metaclass BUILT-IN-CLASS
1581 ;;;
1582 ;;; This metaclass is something of a weird creature. By this point, all
1583 ;;; instances of it which will exist have been created, and no instance
1584 ;;; is ever created by calling MAKE-INSTANCE.
1585 ;;;
1586 ;;; But, there are other parts of the protcol we must follow and those
1587 ;;; definitions appear here.
1588 ;;;
1589 (defmethod shared-initialize :before
1590 ((class built-in-class) slot-names &rest initargs)
1591 (declare (ignore slot-names initargs))
1592 (error "Attempt to initialize or reinitialize a built-in class."))
1593
1594 (defmethod class-direct-slots ((class built-in-class)) ())
1595 (defmethod class-slots ((class built-in-class)) ())
1596 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1597 (defmethod class-default-initargs ((class built-in-class)) ())
1598
1599 (defmethod validate-superclass ((c class) (s built-in-class))
1600 (or (eq s *the-class-t*)
1601 (eq s *the-class-stream*)))
1602
1603
1604
1605 ;;;
1606 ;;;
1607 ;;;
1608
1609 (macrolet ((frob (method)
1610 `(defmethod ,method ((class forward-referenced-class))
1611 (declare (ignore class))
1612 ())))
1613 (frob class-direct-slots)
1614 (frob class-direct-default-initargs))
1615
1616 (macrolet ((frob (method)
1617 `(defmethod ,method ((class forward-referenced-class))
1618 (error "~@<~S called for forward referenced class ~S.~@:>"
1619 ',method class))))
1620 (frob class-default-initargs)
1621 (frob class-precedence-list)
1622 (frob class-slots))
1623
1624 (defmethod validate-superclass ((c slot-class)
1625 (f forward-referenced-class))
1626 t)
1627
1628
1629 ;;;
1630 ;;;
1631 ;;;
1632
1633 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1634 (pushnew dependent (plist-value metaobject 'dependents)))
1635
1636 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1637 (setf (plist-value metaobject 'dependents)
1638 (delete dependent (plist-value metaobject 'dependents))))
1639
1640 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1641 (dolist (dependent (plist-value metaobject 'dependents))
1642 (funcall function dependent)))
1643
1644 (defmethod update-dependent (metaobject dependent &rest initargs)
1645 (declare (ignore initargs)))
1646
1647
1648 ;;;
1649 ;;; Conditions
1650 ;;;
1651 (defmethod class-default-initargs ((class condition-class)) ())
1652 (defmethod class-direct-default-initargs ((class condition-class)) ())
1653
1654 (defmethod shared-initialize :after ((class condition-class) slot-names
1655 &key direct-slots direct-superclasses)
1656 (declare (ignore slot-names))
1657 (let ((kernel-class (kernel::find-class (class-name class))))
1658 (with-slots (wrapper class-precedence-list prototype predicate-name
1659 (direct-supers direct-superclasses))
1660 class
1661 (setf (slot-value class 'direct-slots)
1662 (mapcar (lambda (pl) (make-direct-slotd class pl))
1663 direct-slots))
1664 (setf (slot-value class 'finalized-p) t)
1665 (setf (kernel:%class-pcl-class kernel-class) class)
1666 (setq direct-supers direct-superclasses)
1667 (setq wrapper (kernel:%class-layout kernel-class))
1668 (setq class-precedence-list (compute-class-precedence-list class))
1669 (add-direct-subclasses class direct-superclasses)
1670 (setq predicate-name (make-class-predicate-name (class-name class)))
1671 (make-class-predicate class predicate-name)
1672 (setf (slot-value class 'slots) (compute-slots class))))
1673 ;;
1674 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
1675 ;; override condition accessors with generic functions. We do this
1676 ;; differently.
1677 (update-pv-table-cache-info class))
1678
1679 (defmethod direct-slot-definition-class
1680 ((class condition-class) &rest initargs)
1681 (declare (ignore initargs))
1682 (find-class 'condition-direct-slot-definition))
1683
1684 (defmethod effective-slot-definition-class
1685 ((class condition-class) &rest initargs)
1686 (declare (ignore initargs))
1687 (find-class 'condition-effective-slot-definition))
1688
1689 (defmethod finalize-inheritance ((class condition-class))
1690 nil)
1691
1692 (defmethod compute-effective-slot-definition
1693 ((class condition-class) slot-name dslotds)
1694 (let ((slotd (call-next-method)))
1695 (setf (slot-definition-reader-function slotd)
1696 (lambda (x)
1697 (handler-case
1698 (conditions::condition-reader-function x slot-name)
1699 (error () (values (slot-unbound class x slot-name))))))
1700 (setf (slot-definition-writer-function slotd)
1701 (lambda (v x)
1702 (conditions::condition-writer-function x v slot-name)))
1703 (setf (slot-definition-boundp-function slotd)
1704 (lambda (x)
1705 (multiple-value-bind (v c)
1706 (ignore-errors
1707 (conditions::condition-reader-function x slot-name))
1708 (declare (ignore v))
1709 (null c))))
1710 slotd))
1711
1712 (defmethod compute-slots ((class condition-class))
1713 (mapcan (lambda (superclass)
1714 (mapcar (lambda (dslotd)
1715 (compute-effective-slot-definition
1716 class (slot-definition-name dslotd) (list dslotd)))
1717 (class-direct-slots superclass)))
1718 (reverse (slot-value class 'class-precedence-list))))
1719
1720 (defmethod compute-slots :around ((class condition-class))
1721 (let ((eslotds (call-next-method)))
1722 (mapc #'initialize-internal-slot-functions eslotds)
1723 eslotds))

  ViewVC Help
Powered by ViewVC 1.1.5