/[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.66 - (show annotations)
Tue May 20 18:39:36 2003 UTC (10 years, 11 months ago) by gerd
Branch: MAIN
Changes since 1.65: +8 -1 lines
	Store user-specified slot names in DEFSTRUCT-SLOT-DESCRIPTIONs,
	which is necessary for conforming SLOT-EXISTS-P and
	MAKE-LOAD-FORM-SAVING-SLOTS.  To bootstrap, using pmai's
	build scripts:

	1. Copy boot10.lisp to target:bootstrap.lisp and do a full
	   build.  Choose the CLOBBER-IT restart when asked
	   (for an unknown reason, it doesn't work to do this
	   programatically.)

	2. Leave the bootstrap file where it is and do a full build
	   with the Lisp produced in step 1.

	3. Remove the bootstrap file and do another full build.

	* src/bootfiles/18e/boot10.lisp: New file.

	* src-dsd/code/defstruct.lisp (defstruct-slot-description):
	Rename slot %name to name.
	(dsd-%name): New function.
	(dsd-name): Function removed.
	(parse-1-dsd): Set dsd-name to the slot symbol.

	* src-dsd/pcl/std-class.lisp (*allow-keyword-slot-names*): New
	variable.
	(initialize-internal-slot-functions) <around
	structure-effective-slot-definition>: New method binding
	*allow-keyword-slot-names* to true.

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

  ViewVC Help
Powered by ViewVC 1.1.5