Debian Codegraph @

src/clos/standard.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: CLOS -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1992, Giuseppe Attardi.
6 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 (in-package "CLOS")
11
12 ;;; ----------------------------------------------------------------------
13 ;;; INSTANCES INITIALIZATION AND REINITIALIZATION
14 ;;;
15
16 (defmethod initialize-instance ((instance T) &rest initargs)
17 (apply #'shared-initialize instance 'T initargs))
18
19 (defmethod reinitialize-instance ((instance T) &rest initargs)
20 (check-initargs (class-of instance) initargs
21 (valid-keywords-from-methods
22 (compute-applicable-methods
23 #'reinitialize-instance (list instance))
24 (compute-applicable-methods
25 #'shared-initialize (list instance t))))
26 (apply #'shared-initialize instance '() initargs))
27
28 (defmethod shared-initialize ((instance T) slot-names &rest initargs)
29 ;;
30 ;; initialize the instance's slots is a two step process
31 ;; 1 A slot for which one of the initargs in initargs can set
32 ;; the slot, should be set by that initarg. If more than
33 ;; one initarg in initargs can set the slot, the leftmost
34 ;; one should set it.
35 ;;
36 ;; 2 Any slot not set by step 1, may be set from its initform
37 ;; by step 2. Only those slots specified by the slot-names
38 ;; argument are set. If slot-names is:
39 ;; T
40 ;; any slot not set in step 1 is set from its
41 ;; initform
42 ;; <list of slot names>
43 ;; any slot in the list, and not set in step 1
44 ;; is set from its initform
45 ;;
46 ;; ()
47 ;; no slots are set from initforms
48 ;;
49 (let* ((class (class-of instance)))
50 ;; initialize-instance slots
51 (dolist (slotd (class-slots class))
52 (let* ((slot-initargs (slot-definition-initargs slotd))
53 (slot-name (slot-definition-name slotd)))
54 (or
55 ;; Try to initialize the slot from one of the initargs.
56 (do ((l initargs) initarg val)
57 ((null l) nil)
58 (setf initarg (pop l))
59 (when (endp l)
60 (simple-program-error "Wrong number of keyword arguments for SHARED-INITIALIZE, ~A"
61 initargs))
62 (unless (symbolp initarg)
63 (simple-program-error "Not a valid initarg: ~A" initarg))
64 (setf val (pop l))
65 (when (member initarg slot-initargs :test #'eq)
66 (setf (slot-value instance slot-name) val)
67 (return t)))
68 ;; Try to initialize the slot from its initform.
69 (when (and slot-names
70 (or (eq slot-names 'T)
71 (member slot-name slot-names))
72 (not (slot-boundp instance slot-name)))
73 (let ((initfun (slot-definition-initfunction slotd)))
74 (when initfun
75 (setf (slot-value instance slot-name) (funcall initfun))))))
76 )))
77 instance)
78
79 ;;; ----------------------------------------------------------------------
80 ;;; CLASSES INITIALIZATION AND REINITIALIZATION
81 ;;;
82
83 (defun compute-instance-size (slots)
84 (loop for slotd in slots
85 with last-location = -1
86 with num-slots = 0
87 when (eq (slot-definition-allocation slotd) :instance)
88 do (let ((new-loc (safe-slot-definition-location slotd)))
89 (incf num-slots)
90 (when (and new-loc (> new-loc last-location))
91 (setf last-location new-loc)))
92 finally (return (max num-slots (1+ last-location)))))
93
94 (defmethod allocate-instance ((class class) &rest initargs)
95 (declare (ignore initargs))
96 ;; As pointed out in the CLASP source code (after Dr. Strandh), the class is
97 ;; already finalized, because initargs can't be computed without finalizing
98 ;; the class. -- jd 2021-12-10
99 (assert (class-finalized-p class))
100 (let ((x (si::allocate-raw-instance nil class (class-size class))))
101 (si::instance-sig-set x)
102 x))
103
104 (defmethod make-instance ((class class) &rest initargs)
105 ;; Without finalization we can not find initargs.
106 (unless (class-finalized-p class)
107 (finalize-inheritance class))
108 ;; We add the default-initargs first, because one of these initargs might
109 ;; be (:allow-other-keys t), which disables the checking of the arguments.
110 ;; (Paul Dietz's ANSI test suite, test CLASS-24.4)
111 (setf initargs (add-default-initargs class initargs))
112 (let ((keywords (if (slot-boundp class 'valid-initargs)
113 (class-valid-initargs class)
114 (precompute-valid-initarg-keywords class))))
115 (check-initargs class initargs nil (class-slots class) keywords))
116 (let ((instance (apply #'allocate-instance class initargs)))
117 (apply #'initialize-instance instance initargs)
118 instance))
119
120 (defun delete-keyword (keyword list)
121 (loop until (eq (getf list keyword list) list)
122 do (remf list keyword))
123 list)
124
125 (defun add-default-initargs (class initargs)
126 (declare (si::c-local))
127 ;; Here, for each slot which is not mentioned in the initialization
128 ;; arguments, but which has a value associated with :DEFAULT-INITARGS,
129 ;; we compute the value and add it to the list of initargs.
130 (let ((output '()))
131 (dolist (scan (class-default-initargs class))
132 (let* ((initarg (first scan))
133 (value (third scan))
134 (supplied-value (si::search-keyword initargs initarg)))
135 (when (or (eq supplied-value '+initform-unsupplied+)
136 (eq supplied-value 'si::missing-keyword))
137 (when (eq supplied-value '+initform-unsupplied+)
138 (setf initargs (delete-keyword initarg initargs)))
139 (setf output (list* (funcall value) initarg output)))))
140 (if output
141 (append initargs (nreverse output))
142 initargs)))
143
144 (defmethod direct-slot-definition-class ((class T) &rest canonicalized-slot)
145 (declare (ignore class canonicalized-slot))
146 (find-class 'standard-direct-slot-definition nil))
147
148 (defmethod effective-slot-definition-class ((class T) &rest canonicalized-slot)
149 (declare (ignore class canonicalized-slot))
150 (find-class 'standard-effective-slot-definition nil))
151
152 (defun has-forward-referenced-parents (class)
153 (or (forward-referenced-class-p class)
154 (and (not (class-finalized-p class))
155 (some #'has-forward-referenced-parents
156 (class-direct-superclasses class)))))
157
158 (defun finalize-unless-forward (class)
159 (unless (or (forward-referenced-class-p class)
160 (find-if #'has-forward-referenced-parents (class-direct-superclasses class)))
161 (finalize-inheritance class)))
162
163 (defmethod initialize-instance ((class class) &rest initargs &key direct-slots direct-superclasses)
164 ;; convert the slots from lists to direct slots
165 (apply #'call-next-method class
166 :direct-slots
167 (loop for s in direct-slots
168 collect (canonical-slot-to-direct-slot class s))
169 :direct-superclasses
170 direct-superclasses
171 initargs)
172 (finalize-unless-forward class)
173 class)
174
175 (defmethod shared-initialize ((class class) slot-names &rest initargs
176 &key (direct-superclasses nil direct-superclasses-p))
177 (if direct-superclasses-p
178 ;; verify that the inheritance list makes sense
179 (let* ((class (apply #'call-next-method class slot-names
180 :direct-superclasses
181 (if (slot-boundp class 'direct-superclasses)
182 (slot-value class 'direct-superclasses)
183 nil)
184 initargs))
185 (direct-superclasses (check-direct-superclasses class direct-superclasses)))
186 (loop for c in (class-direct-superclasses class)
187 unless (member c direct-superclasses :test #'eq)
188 do (remove-direct-subclass c class))
189 (setf (class-direct-superclasses class) direct-superclasses)
190 (loop for c in direct-superclasses
191 do (add-direct-subclass c class))
192 class)
193 (apply #'call-next-method class slot-names initargs)))
194
195 (defun precompute-valid-initarg-keywords (class)
196 (setf (class-valid-initargs class)
197 (loop with methods = (nconc
198 (compute-applicable-methods
199 #'allocate-instance (list class))
200 (compute-applicable-methods
201 #'initialize-instance (list (class-prototype class)))
202 (compute-applicable-methods
203 #'shared-initialize (list (class-prototype class) t)))
204 for m in methods
205 for k = (method-keywords m)
206 when (eq k t)
207 return t
208 append k)))
209
210 (defun update-dependents (object initargs)
211 (when *clos-booted*
212 (map-dependents
213 object
214 #'(lambda (dep) (apply #'update-dependent object dep initargs)))))
215
216 (defmethod shared-initialize ((class std-class) slot-names &rest initargs &key
217 (optimize-slot-access (list *optimize-slot-access*))
218 sealedp)
219 (declare (ignore slot-names))
220 (setf (slot-value class 'optimize-slot-access) (first optimize-slot-access)
221 (slot-value class 'sealedp) (and sealedp t))
222 (setf class (call-next-method))
223 (update-dependents class initargs)
224 class)
225
226 (defmethod add-direct-subclass ((parent class) child)
227 (pushnew child (class-direct-subclasses parent)))
228
229 (defmethod remove-direct-subclass ((parent class) child)
230 (setf (class-direct-subclasses parent)
231 (remove child (class-direct-subclasses parent))))
232
233 (defun check-direct-superclasses (class supplied-superclasses)
234 (unless supplied-superclasses
235 (setf supplied-superclasses
236 (typecase class
237 (standard-class
238 (list (find-class 'standard-object)))
239 (structure-class
240 (list (find-class 'structure-object)))
241 (funcallable-standard-class
242 (list (find-class 'funcallable-standard-object)))
243 (forward-referenced-class
244 nil)
245 (otherwise
246 (error "No :DIRECT-SUPERCLASS argument was supplied for the ~
247 metaclass ~S." (class-of class))))))
248 ;; Until we process streams.lsp there are some invalid combinations using
249 ;; built-in-class, which here we simply ignore.
250 (when (eq *clos-booted* T)
251 (loop for superclass in supplied-superclasses
252 unless (validate-superclass class superclass)
253 do (error "Class ~A is not a valid superclass for ~A" superclass class)))
254 ;; FIXME!!! We should check that structures and standard objects are not
255 ;; mixed, and that STANDARD-CLASS, or STANDARD-GENERIC-FUNCTION, etc, are
256 ;; the first classes.
257 supplied-superclasses)
258
259 (defmethod validate-superclass ((class class) (superclass class))
260 (or (eq superclass +the-t-class+)
261 (let ((c1 (class-of class))
262 (c2 (class-of superclass)))
263 (or (eq c1 c2)
264 (and (eq c1 +the-standard-class+) (eq c2 +the-funcallable-standard-class+))
265 (and (eq c2 +the-standard-class+) (eq c1 +the-funcallable-standard-class+))))
266 (or (forward-referenced-class-p class)
267 (forward-referenced-class-p superclass))))
268
269 ;;; ----------------------------------------------------------------------
270 ;;; FINALIZATION OF CLASS INHERITANCE
271 ;;;
272 (defun forward-referenced-class-p (x)
273 (let ((y (find-class 'FORWARD-REFERENCED-CLASS nil)))
274 (and y (si::subclassp (class-of x) y))))
275
276 (defmethod finalize-inheritance ((class class))
277 ;; FINALIZE-INHERITANCE computes the guts of what defines a class: the
278 ;; slots, the list of parent class, etc. It is called when either the
279 ;; class was not finalized before, or when one of the parents has been
280 ;; modified.
281 ;;
282 (let ((cpl (compute-class-precedence-list class)))
283 ;; A class cannot be finalized if any of its parents is either
284 ;; a not yet defined class or it has not yet been finalized.
285 ;; In the first case we can just signal an error...
286 ;;
287 (when-let ((x (find-if #'forward-referenced-class-p (rest cpl))))
288 (error "Cannot finish building the class~% ~A~%~
289 because it contains a reference to the undefined class~% ~A"
290 (class-name class) (class-name x)))
291 ;;
292 ;; ... and in the second case we just finalize the top-most class
293 ;; which is not yet finalized and rely on the fact that this
294 ;; class will also try to finalize all of its children.
295 ;;
296 (when-let ((x (find-if-not #'class-finalized-p cpl :from-end t)))
297 (unless (eq x class)
298 (return-from finalize-inheritance
299 (finalize-inheritance x))))
300
301 (setf (class-precedence-list class) cpl)
302 (let* ((oslotds-p (slot-boundp class 'slots))
303 (oslotds (and oslotds-p (class-slots class)))
304 (nslotds (compute-slots class)))
305 (setf (class-slots class) nslotds
306 (class-size class) (compute-instance-size nslotds)
307 (class-default-initargs class) (compute-default-initargs class)
308 (class-finalized-p class) t)
309 ;;
310 ;; When a class is sealed we rewrite the list of direct slots to fix
311 ;; their locations. This may imply adding _new_ direct slots.
312 ;;
313 (when (class-sealedp class)
314 (let* ((free-slots (delete-duplicates (mapcar #'slot-definition-name
315 (class-slots class))))
316 (all-slots (class-slots class)))
317 ;;
318 ;; We first search all slots that belonged to unsealed classes and which
319 ;; therefore have no fixed position.
320 ;;
321 (loop for c in cpl
322 do (loop for slotd in (class-direct-slots c)
323 when (safe-slot-definition-location slotd)
324 do (setf free-slots (delete (slot-definition-name slotd)
325 free-slots))))
326 ;;
327 ;; We now copy the locations of the effective slots in this class to
328 ;; the class direct slots.
329 ;;
330 (loop for slotd in (class-direct-slots class)
331 do (let* ((name (slot-definition-name slotd))
332 (other-slotd (find name all-slots :key #'slot-definition-name)))
333 (setf (slot-definition-location slotd)
334 (slot-definition-location other-slotd)
335 free-slots (delete name free-slots))))
336 ;;
337 ;; And finally we add one direct slot for each inherited slot that did
338 ;; not have a fixed location.
339 ;;
340 (loop for name in free-slots
341 with direct-slots = (class-direct-slots class)
342 do (let* ((effective-slotd (find name all-slots :key #'slot-definition-name))
343 (def (direct-slot-to-canonical-slot effective-slotd)))
344 (push (apply #'make-instance (direct-slot-definition-class class def)
345 def)
346 direct-slots))
347 finally (setf (class-direct-slots class) direct-slots))))
348 ;;
349 ;; Make all class instances obsolete when slot definitions are
350 ;; not compatible.
351 ;;
352 (cond ((null oslotds-p)
353 ;; Assign the initial class stamp when the class had no slots.
354 ;; That is because for structure class MAKE-INSTANCES-OBSOLETE
355 ;; doesn't update the stamp.
356 (si::instance-new-stamp class))
357 ((not (slot-definitions-compatible-p oslotds nslotds))
358 (make-instances-obsolete class))))
359 ;;
360 ;; Clear the different type caches for type comparisons and so on.
361 ;;
362 (si::subtypep-clear-cache))
363 ;; As mentioned above, when a parent is finalized, it is responsible for
364 ;; invoking FINALIZE-INHERITANCE on all of its children. Obviously,
365 ;; this only makes sense when the class has been defined.
366 (dolist (subclass (reverse (class-direct-subclasses class)))
367 (finalize-unless-forward subclass))
368 ;;
369 ;; We create various caches to more rapidly find the slot locations and
370 ;; slot definitions.
371 (std-create-slots-table class))
372
373 (defmethod finalize-inheritance ((class std-class))
374 (call-next-method)
375 (std-class-generate-accessors class))
376
377 (defmethod finalize-inheritance ((class forward-referenced-class))
378 (error "FINALIZE-INHERITANCE was called on a forward referenced class ~a." class))
379
380 (defmethod compute-class-precedence-list ((class class))
381 (compute-clos-class-precedence-list class (class-direct-superclasses class)))
382
383 (eval-when (:compile-toplevel :execute)
384 (defmacro mapappend (fun &rest args)
385 `(reduce #'append (mapcar ,fun ,@args))))
386
387 (defmethod compute-slots ((class class))
388 ;; INV: for some classes ECL expects that the order of the inherited slots is
389 ;; preserved. The following code ensures that, if C1 is after C2 in the
390 ;; class precedence list, and the slot S1 appears both in C1 and C2,
391 ;; the slot S1 will appear the new class before the slots of C2; and
392 ;; whenever possible, in the same position as in C1.
393 ;;
394 (do* ((all-slots (mapappend #'class-direct-slots (reverse (class-precedence-list class))))
395 (all-names (nreverse (mapcar #'slot-definition-name all-slots)))
396 (output '())
397 (scan all-names (cdr scan)))
398 ((endp scan) output)
399 (let ((name (first scan)))
400 (unless (find name (rest scan))
401 (push (compute-effective-slot-definition
402 class name (delete name (reverse all-slots) :key #'slot-definition-name
403 :test-not #'eq))
404 output)))))
405
406 (defun slot-definition-to-plist (slotd)
407 (list :name (slot-definition-name slotd)
408 :initform (slot-definition-initform slotd)
409 :initfunction (slot-definition-initfunction slotd)
410 :type (slot-definition-type slotd)
411 :allocation (slot-definition-allocation slotd)
412 :initargs (slot-definition-initargs slotd)
413 :readers (slot-definition-readers slotd)
414 :writers (slot-definition-writers slotd)
415 :documentation (slot-definition-documentation slotd)
416 :location (slot-definition-location slotd)))
417
418 (defun safe-slot-definition-location (slotd &optional default)
419 (if (or (listp slotd) (slot-boundp slotd 'location))
420 (slot-definition-location slotd)
421 default))
422
423 (defmethod compute-effective-slot-definition ((class class) name direct-slots)
424 (flet ((direct-to-effective (old-slot)
425 (if (consp old-slot)
426 (copy-list old-slot)
427 (let ((initargs (slot-definition-to-plist old-slot)))
428 (apply #'make-instance
429 (apply #'effective-slot-definition-class class initargs)
430 initargs))))
431 (combine-slotds (new-slotd old-slotd)
432 (let* ((new-type (slot-definition-type new-slotd))
433 (old-type (slot-definition-type old-slotd))
434 (loc1 (safe-slot-definition-location new-slotd))
435 (loc2 (safe-slot-definition-location old-slotd)))
436 (when loc2
437 (if loc1
438 (unless (eql loc1 loc2)
439 (error 'simple-error
440 :format-control "You have specified two conflicting slot locations:~%~D and ~F~%for slot ~A"
441 :format-arguments (list loc1 loc2 name)))
442 (progn
443 #+(or)
444 (format t "~%Assigning a default location ~D for ~A in ~A."
445 loc2 name (class-name class))
446 (setf (slot-definition-location new-slotd) loc2))))
447 (setf (slot-definition-initargs new-slotd)
448 (union (slot-definition-initargs new-slotd)
449 (slot-definition-initargs old-slotd)))
450 (unless (slot-definition-initfunction new-slotd)
451 (setf (slot-definition-initform new-slotd)
452 (slot-definition-initform old-slotd)
453 (slot-definition-initfunction new-slotd)
454 (slot-definition-initfunction old-slotd)))
455 (setf (slot-definition-readers new-slotd)
456 (union (slot-definition-readers new-slotd)
457 (slot-definition-readers old-slotd))
458 (slot-definition-writers new-slotd)
459 (union (slot-definition-writers new-slotd)
460 (slot-definition-writers old-slotd))
461 (slot-definition-type new-slotd)
462 ;; FIXME! we should be more smart then this:
463 (cond ((subtypep new-type old-type) new-type)
464 ((subtypep old-type new-type) old-type)
465 (T `(and ,new-type ,old-type))))
466 new-slotd)))
467 (reduce #'combine-slotds (rest direct-slots)
468 :initial-value (direct-to-effective (first direct-slots)))))
469
470 (defmethod compute-default-initargs ((class class))
471 (let ((all-initargs (mapappend #'class-direct-default-initargs
472 (class-precedence-list class))))
473 ;; We have to use this trick because REMOVE-DUPLICATES on
474 ;; ((:foo x) (:faa y) (:foo z)) would produce ((:faa y) (:foo z))
475 ;; and we want ((:foo x) (:faa y))
476 (nreverse (remove-duplicates (reverse all-initargs) :key #'first))))
477
478 ;;; ======================================================================
479 ;;; STANDARD-CLASS specializations
480 ;;;
481 ;;; IMPORTANT: The following implementation of ENSURE-CLASS-USING-CLASS is
482 ;;; shared by the metaclasses STANDARD-CLASS and STRUCTURE-CLASS.
483 ;;;
484 (defmethod ensure-class-using-class ((class class) name &rest rest
485 &key direct-slots direct-default-initargs)
486 (declare (ignore direct-default-initargs direct-slots))
487 (multiple-value-bind (metaclass direct-superclasses options)
488 (apply #'help-ensure-class rest)
489 (declare (ignore direct-superclasses))
490 (cond ((forward-referenced-class-p class)
491 (change-class class metaclass))
492 ((not (eq (class-of class) metaclass))
493 (error "When redefining a class, the metaclass can not change.")))
494 (setf class (apply #'reinitialize-instance class :name name options))
495 (when name
496 (si:create-type-name name)
497 (setf (find-class name) class))
498 class))
499
500 (defun coerce-to-class (class-or-symbol &optional (fail nil))
501 (cond ((si:instancep class-or-symbol) class-or-symbol)
502 ((not (symbolp class-or-symbol))
503 (error "~a is not a valid class specifier." class-or-symbol))
504 ((find-class class-or-symbol fail))
505 (t
506 (warn 'si::simple-style-warning
507 :format-control "Class ~A has been forward referenced."
508 :format-arguments (list class-or-symbol))
509 (ensure-class class-or-symbol
510 :metaclass 'forward-referenced-class
511 :direct-superclasses (list (find-class 'standard-object))
512 :direct-slots '()))))
513
514 (defun help-ensure-class (&rest options
515 &key (metaclass 'standard-class) direct-superclasses
516 &allow-other-keys)
517 (remf options :metaclass)
518 (remf options :direct-superclasses)
519 (setf metaclass (coerce-to-class metaclass t)
520 direct-superclasses (mapcar #'coerce-to-class direct-superclasses))
521 (values metaclass direct-superclasses
522 (list* :direct-superclasses direct-superclasses options)))
523
524 ;;; ----------------------------------------------------------------------
525 ;;; Around methods for COMPUTE-SLOTS which assign locations to each slot.
526 ;;;
527
528 (defun class-compute-slots (class slots)
529 ;; This an ECL extension. We are allowed to specify the location of
530 ;; a direct slot. Consequently we have to first sort the ones which
531 ;; have been predefined and then assign locations _after_ the last
532 ;; assigned slot. Note the generalized comparison, which pushes all
533 ;; slots without a defined location to the end of the list.
534 (let* ((size (compute-instance-size slots))
535 (instance-slots (remove :instance slots :key #'slot-definition-allocation
536 :test-not #'eq))
537 (numbered-slots (remove-if-not #'safe-slot-definition-location instance-slots))
538 (other-slots (remove-if #'safe-slot-definition-location instance-slots))
539 (aux (make-array size :element-type 't :adjustable nil :initial-element nil)))
540 (loop for i in numbered-slots
541 do (let ((loc (slot-definition-location i)))
542 (when (aref aux loc)
543 (error 'simple-error
544 :format-control "Slots ~A and ~A are said to have the same location in class ~A."
545 :format-ars (list (aref aux loc) i class)))
546 (setf (aref aux loc) i)))
547 (loop for i in other-slots
548 with index = 0
549 do (loop while (aref aux index)
550 do (incf index)
551 finally (setf (aref aux index) i
552 (slot-definition-location i) index)))
553 slots))
554
555 (defmethod compute-slots :around ((class class))
556 (class-compute-slots class (call-next-method)))
557
558 (defun std-class-compute-slots (class slots)
559 (declare (si::c-local))
560 (let* ((direct-slots (class-direct-slots class)))
561 (dolist (slotd slots)
562 (let* ((name (slot-definition-name slotd))
563 (allocation (slot-definition-allocation slotd)))
564 (cond ((not (eq (slot-definition-allocation slotd) :class)))
565 ((find name direct-slots :key #'slot-definition-name) ; new shared slot
566 (let* ((initfunc (slot-definition-initfunction slotd))
567 (value (if initfunc (funcall initfunc) (unbound))))
568 (setf (slot-definition-location slotd) (list value))))
569 (t ; inherited shared slot
570 (dolist (c (class-precedence-list class))
571 (unless (eql c class)
572 (let ((other (find (slot-definition-name slotd)
573 (class-slots c)
574 :key #'slot-definition-name)))
575 (when (and other
576 (eq (slot-definition-allocation other) allocation)
577 (setf (slot-definition-location slotd)
578 (slot-definition-location other)))
579 (return)))))))))
580 slots))
581
582 (defmethod compute-slots :around ((class std-class))
583 (std-class-compute-slots class (call-next-method)))
584
585 ;;; ======================================================================
586 ;;; STANDARD-OBJECT
587 ;;;
588 ;;; Standard-object has no slots and inherits only from t:
589 ;;; (defclass standard-object (t) ())
590
591 (defmethod describe-object ((obj standard-object) (stream t))
592 (let* ((class (si:instance-class obj))
593 (slotds (class-slots class))
594 slotname has-shared-slots)
595 (format stream "~%~S is an instance of class ~A"
596 obj (class-name class))
597 (when slotds
598 ;; print instance slots
599 (format stream "~%it has the following instance slots")
600 (dolist (slot slotds)
601 (setq slotname (slot-definition-name slot))
602 (case (slot-definition-allocation slot)
603 (:INSTANCE
604 (format stream "~%~A:~24,8T~A"
605 slotname
606 (if (slot-boundp obj slotname)
607 (slot-value obj slotname) "Unbound")))
608 ;; :CLASS
609 (T (setq has-shared-slots t))))
610 (when has-shared-slots
611 ;; print class slots
612 (format stream "~%it has the following class slots")
613 (dolist (slot slotds)
614 (setq slotname (slot-definition-name slot))
615 (unless (eq (slot-definition-allocation slot) :INSTANCE)
616 (format stream "~%~A:~24,8T~A"
617 slotname
618 (if (slot-boundp obj slotname)
619 (slot-value obj slotname) "Unbound")))))))
620 obj)
621
622 ;;; ----------------------------------------------------------------------
623 ;;; CHECK INITARGS
624 ;;;
625 ;;; There are different sets of initialization arguments. First we have
626 ;;; those coming from the :INITARG option in the slots. Then we have
627 ;;; all declared initargs which are keyword arguments to methods defined
628 ;;; on SHARED-INITIALIZE, REINITIALIZE-INSTANCE, etc. (See ANSI 7.1.2)
629 ;;;
630
631 (defun valid-keywords-from-methods (&rest method-lists)
632 (loop for methods in method-lists
633 when (member t methods :key #'method-keywords)
634 return t
635 nconc methods))
636
637 (defun check-initargs (class initargs &optional methods
638 (slots (class-slots class))
639 cached-keywords)
640 ;; First get all initargs which have been declared in the given
641 ;; methods, then check the list of initargs declared in the slots
642 ;; of the class.
643 (unless (or (eq methods t) (eq cached-keywords t))
644 (do* ((name-loc initargs (cddr name-loc))
645 (allow-other-keys nil)
646 (allow-other-keys-found nil)
647 (unknown-key-names nil))
648 ((null name-loc)
649 (when (and (not allow-other-keys) unknown-key-names)
650 (simple-program-error "Unknown initialization options ~S for class ~A."
651 (nreverse unknown-key-names) class)))
652 (let ((name (first name-loc)))
653 (cond ((null (cdr name-loc))
654 (simple-program-error "No value supplied for the init-name ~S." name))
655 ;; This check must be here, because :ALLOW-OTHER-KEYS is a valid
656 ;; slot-initarg.
657 ((and (eql name :ALLOW-OTHER-KEYS)
658 (not allow-other-keys-found))
659 (setf allow-other-keys (second name-loc)
660 allow-other-keys-found t))
661 ;; Check if the arguments is associated with a slot
662 ((member name slots :test #'member :key #'slot-definition-initargs))
663 ;; The initialization argument has been declared in some method
664 ((member name cached-keywords))
665 ((and methods (member name methods :test #'member :key #'method-keywords)))
666 (t
667 (push name unknown-key-names)))))))
668
669 ;;; ----------------------------------------------------------------------
670 ;;; Methods
671
672 (defmethod describe-object ((obj std-class) (stream t))
673 (let ((slotds (class-slots (si:instance-class obj))))
674 (format stream "~%~A is an instance of class ~A"
675 obj (class-name (si:instance-class obj)))
676 (do ((scan slotds (cdr scan))
677 (i 0 (1+ i)))
678 ((null scan))
679 (declare (fixnum i))
680 (print (slot-definition-name (car scan)) stream)
681 (princ ": " stream)
682 (case (slot-definition-name (car scan))
683 ((SUPERIORS INFERIORS PRECEDENCE-LIST)
684 (princ "(" stream)
685 (do* ((scan (si:instance-ref obj i) (cdr scan))
686 (e (car scan) (car scan)))
687 ((null scan))
688 (prin1 (class-name e) stream)
689 (when (cdr scan) (princ " " stream)))
690 (princ ")"))
691 (otherwise (prin1 (si:instance-ref obj i) stream)))))
692 obj)