/[cmucl]/src/code/defstruct.lisp
ViewVC logotype

Contents of /src/code/defstruct.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.83 - (show annotations)
Thu Mar 27 19:23:17 2003 UTC (11 years, 1 month ago) by toy
Branch: MAIN
Changes since 1.82: +2 -45 lines
Clean up code.  Remove the bootstrap-conc-name reader conditional
stuff that was introduced to fix the conc-name inheritance bugs.  We
keep the #-bootstrap-conc-name stuff.
1 ;;; -*- Mode: Lisp; Package: KERNEL -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/defstruct.lisp,v 1.83 2003/03/27 19:23:17 toy Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Defstruct structure definition package (Mark III).
13 ;;; Written by Rob MacLachlan, William Lott and Skef Wholey.
14 ;;;
15 (in-package "LISP")
16 (export '(defstruct copy-structure structure-object))
17 (in-package "KERNEL")
18 (export '(default-structure-print make-structure-load-form
19 %compiler-defstruct %%compiler-defstruct
20 %compiler-only-defstruct
21 %make-instance
22 %instance-length %instance-ref %instance-set %instance-layout
23 %set-instance-layout
24 %make-funcallable-instance %funcallable-instance-info
25 %set-funcallable-instance-info
26 funcallable-instance-function funcallable-structure
27 funcallable-instance-p
28 %raw-ref-single %raw-set-single
29 %raw-ref-double %raw-set-double
30 defstruct-description dd-name dd-default-constructor dd-copier
31 dd-predicate dd-slots dd-length dd-type dd-raw-index dd-raw-length
32 defstruct-slot-description dsd-name dsd-%name dsd-accessor dsd-type
33 dsd-index dsd-raw-type dsd-read-only undefine-structure
34 *ansi-defstruct-options-p*))
35
36
37 (defparameter *ANSI-defstruct-options-p* nil
38 "Controls compiling DEFSTRUCT :print-function and :print-method
39 options according to ANSI spec. MUST be NIL to compile CMUCL & PCL")
40
41 ;;;; Structure frobbing primitives.
42
43 (defun %make-instance (length)
44 "Allocate a new instance with LENGTH data slots."
45 (declare (type index length))
46 (%make-instance length))
47
48 (defun %instance-length (instance)
49 "Given an instance, return its length."
50 (declare (type instance instance))
51 (%instance-length instance))
52
53 (defun %instance-ref (instance index)
54 "Return the value from the INDEXth slot of INSTANCE. This is SETFable."
55 (%instance-ref instance index))
56
57 (defun %instance-set (instance index new-value)
58 "Set the INDEXth slot of INSTANCE to NEW-VALUE."
59 (setf (%instance-ref instance index) new-value))
60
61 (defun %raw-ref-single (vec index)
62 (declare (type index index))
63 (%raw-ref-single vec index))
64
65 (defun %raw-ref-double (vec index)
66 (declare (type index index))
67 (%raw-ref-double vec index))
68
69 #+long-float
70 (defun %raw-ref-long (vec index)
71 (declare (type index index))
72 (%raw-ref-long vec index))
73
74 (defun %raw-set-single (vec index val)
75 (declare (type index index))
76 (%raw-set-single vec index val))
77
78 (defun %raw-set-double (vec index val)
79 (declare (type index index))
80 (%raw-set-double vec index val))
81
82 #+long-float
83 (defun %raw-set-long (vec index val)
84 (declare (type index index))
85 (%raw-set-long vec index val))
86
87 (defun %raw-ref-complex-single (vec index)
88 (declare (type index index))
89 (%raw-ref-complex-single vec index))
90
91 (defun %raw-ref-complex-double (vec index)
92 (declare (type index index))
93 (%raw-ref-complex-double vec index))
94
95 #+long-float
96 (defun %raw-ref-complex-long (vec index)
97 (declare (type index index))
98 (%raw-ref-complex-long vec index))
99
100 (defun %raw-set-complex-single (vec index val)
101 (declare (type index index))
102 (%raw-set-complex-single vec index val))
103
104 (defun %raw-set-complex-double (vec index val)
105 (declare (type index index))
106 (%raw-set-complex-double vec index val))
107
108 #+long-float
109 (defun %raw-set-complex-long (vec index val)
110 (declare (type index index))
111 (%raw-set-complex-long vec index val))
112
113 (defun %instance-layout (instance)
114 (%instance-layout instance))
115
116 (defun %set-instance-layout (instance new-value)
117 (%set-instance-layout instance new-value))
118
119 (defun %make-funcallable-instance (len layout)
120 (%make-funcallable-instance len layout))
121
122 (defun funcallable-instance-p (x) (funcallable-instance-p x))
123
124 (defun %funcallable-instance-info (fin i)
125 (%funcallable-instance-info fin i))
126
127 (defun %set-funcallable-instance-info (fin i new-value)
128 (%set-funcallable-instance-info fin i new-value))
129
130 (defun funcallable-instance-function (fin)
131 (%funcallable-instance-lexenv fin))
132
133 ;;; The heart of the magic of funcallable instances. The function for a FIN
134 ;;; must be a magical INSTANCE-LAMBDA form. When called (as with any other
135 ;;; function), we grab the code pointer, and call it, leaving the original
136 ;;; function object in LEXENV (in case it was a closure). If it is actually a
137 ;;; FIN, then we need to do an extra indirection with
138 ;;; funcallable-instance-lexenv to get at any closure environment. This extra
139 ;;; indirection is set up when accessing the closure environment of an
140 ;;; INSTANCE-LAMBDA. Note that the original FIN pointer is lost, so if the
141 ;;; called function wants to get at the original object to do some slot
142 ;;; accesses, it must close over the FIN object.
143 ;;;
144 ;;; If we set the FIN function to be a FIN, we directly copy across both the
145 ;;; code pointer and the lexenv, since that code pointer (for an
146 ;;; instance-lambda) is expecting that lexenv to be accessed. This effectively
147 ;;; pre-flattens what would otherwise be a chain of indirections. Lest this
148 ;;; sound like an excessively obscure case, note that it happens when PCL
149 ;;; dispatch functions are byte-compiled.
150 ;;;
151 ;;; The only loss is that if someone accesses the
152 ;;; funcallable-instance-function, then won't get a FIN back. This probably
153 ;;; doesn't matter, since PCL only sets the FIN function. And the only reason
154 ;;; that interpreted functions are FINs instead of bare closures is for
155 ;;; debuggability.
156 ;;;
157 (defun (setf funcallable-instance-function) (new-value fin)
158 (setf (%funcallable-instance-function fin)
159 (%closure-function new-value))
160 (setf (%funcallable-instance-lexenv fin)
161 (if (funcallable-instance-p new-value)
162 (%funcallable-instance-lexenv new-value)
163 new-value)))
164
165 (defsetf %instance-ref %instance-set)
166 (defsetf %raw-ref-single %raw-set-single)
167 (defsetf %raw-ref-double %raw-set-double)
168 #+long-float
169 (defsetf %raw-ref-long %raw-set-long)
170 (defsetf %raw-ref-complex-single %raw-set-complex-single)
171 (defsetf %raw-ref-complex-double %raw-set-complex-double)
172 #+long-float
173 (defsetf %raw-ref-complex-long %raw-set-complex-long)
174 (defsetf %instance-layout %set-instance-layout)
175 (defsetf %funcallable-instance-info %set-funcallable-instance-info)
176
177
178 ;;; This version of Defstruct is implemented using Defstruct, and is free of
179 ;;; Maclisp compatability nonsense. For bootstrapping, you're on your own.
180
181 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information about a
182 ;;; structure type.
183 ;;;
184 (defstruct (defstruct-description
185 (:conc-name dd-)
186 (:print-function print-defstruct-description)
187 (:make-load-form-fun :just-dump-it-normally)
188 (:pure t)
189 (:constructor make-defstruct-description (name)))
190 ;;
191 ;; name of the structure
192 (name (required-argument) :type symbol)
193 ;;
194 ;; documentation on the structure
195 (doc nil :type (or string null))
196 ;;
197 ;; prefix for slot names. If NIL, none.
198 (conc-name (concat-pnames name '-) :type (or symbol null))
199 ;;
200 ;; The name of the primary standard keyword constructor, or NIL if none.
201 (default-constructor nil :type (or symbol null))
202 ;;
203 ;; All the explicit :CONSTRUCTOR specs, with name defaulted.
204 (constructors () :type list)
205 ;;
206 ;; name of copying function
207 (copier (concat-pnames 'copy- name) :type (or symbol null))
208 ;;
209 ;; Name of type predictate
210 (predicate (concat-pnames name '-p) :type (or symbol null))
211 ;;
212 ;; The arguments to the :INCLUDE option, or NIL if no included structure.
213 (include nil :type list)
214 ;;
215 ;; The arguments to the :ALTERNATE-METACLASS option (an extension used to
216 ;; define structure-like objects with an arbitrary superclass and that may
217 ;; not have STRUCTURE-CLASS as the metaclass.) Syntax is:
218 ;; (superclass-name metaclass-name metaclass-constructor)
219 ;;
220 (alternate-metaclass nil :type list)
221 ;;
222 ;; list of defstruct-slot-description objects for all slots (including
223 ;; included ones.)
224 (slots () :type list)
225 ;;
226 ;; Number of elements we've allocated (see also raw-length.)
227 (length 0 :type index)
228 ;;
229 ;; General kind of implementation.
230 (type 'structure :type (member structure vector list
231 funcallable-structure))
232 ;;
233 ;; The next three slots are for :TYPE'd structures (which aren't classes,
234 ;; CLASS-STRUCTURE-P = NIL)
235 ;;
236 ;; Vector element type.
237 (element-type 't)
238 ;;
239 ;; T if :NAMED was explicitly specified, Nil otherwise.
240 (named nil :type boolean)
241 ;;
242 ;; Any INITIAL-OFFSET option on this direct type.
243 (offset nil :type (or index null))
244 ;;
245 ;; The argument to the PRINT-FUNCTION option, or NIL if none. If we see an
246 ;; explicit (:PRINT-FUNCTION) option, then this is DEFAULT-STRUCTURE-PRINT.
247 ;; See also BASIC-STRUCTURE-CLASS-PRINTER. Only for classed structures.
248 ;;
249 (print-function nil :type (or cons symbol null))
250 ;;
251 ;; The next four slots are only meaningful in real default structures (TYPE =
252 ;; STRUCTURE).
253 ;;
254 ;; Make-load-form function option. See also STRUCTURE-CLASS-LOAD-FORM-MAKER.
255 (make-load-form-fun nil :type (or symbol cons null))
256 ;;
257 ;; The index of the raw data vector and the number of words in it. NIL and 0
258 ;; if not allocated yet.
259 (raw-index nil :type (or index null))
260 (raw-length 0 :type index)
261 ;;
262 ;; Value of the :PURE option, or :UNSPECIFIED. Only meaningful if
263 ;; CLASS-STRUCTURE-P = T.
264 (pure :unspecified :type (member t nil :substructure :unspecified))
265 ;;
266 ;; a list of (NAME . INDEX) pairs for accessors of included structures
267 (inherited-accessor-alist () :type list))
268
269
270 ;;; DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about structure
271 ;;; slots.
272 ;;;
273 (defstruct (defstruct-slot-description
274 (:conc-name dsd-)
275 (:print-function print-defstruct-slot-description)
276 (:pure t)
277 (:make-load-form-fun :just-dump-it-normally))
278 %name ; string name of slot
279 ;;
280 ;; its position in the implementation sequence
281 (index (required-argument) :type fixnum)
282 ;;
283 ;; Name of accessor.
284 (accessor nil)
285 default ; default value expression
286 (type t) ; declared type specifier
287 ;;
288 ;; If a raw slot, what it holds. T means not raw.
289 (raw-type t :type (member t single-float double-float #+long-float long-float
290 complex-single-float complex-double-float
291 #+long-float complex-long-float
292 unsigned-byte))
293 (read-only nil :type (member t nil)))
294
295 (defun print-defstruct-description (structure stream depth)
296 (declare (ignore depth))
297 (format stream "#<Defstruct-Description for ~S>" (dd-name structure)))
298
299
300 ;;; CLASS-STRUCTURE-P -- Internal
301 ;;;
302 ;;; Return true if Defstruct is a structure with a class.
303 ;;;
304 (defun class-structure-p (defstruct)
305 (member (dd-type defstruct) '(structure funcallable-structure)))
306
307
308 ;;; COMPILER-LAYOUT-OR-LOSE -- Internal
309 ;;;
310 ;;; Return the compiler layout for Name. Must be a structure-like class.
311 ;;;
312 (defun compiler-layout-or-lose (name)
313 (let ((res (info type compiler-layout name)))
314 (cond ((not res)
315 (error "Class not yet defined or was undefined: ~S" name))
316 ((not (typep (layout-info res) 'defstruct-description))
317 (error "Class is not a structure class: ~S" name))
318 (t res))))
319
320
321 ;;; DSD-Name -- External
322 ;;;
323 ;;; Return the name of a defstruct slot as a symbol. We store it
324 ;;; as a string to avoid creating lots of worthless symbols at load time.
325 ;;;
326 (defun dsd-name (dsd)
327 (intern (string (dsd-%name dsd))
328 (symbol-package (dsd-accessor dsd))))
329
330 (defun print-defstruct-slot-description (structure stream depth)
331 (declare (ignore depth))
332 (format stream "#<Defstruct-Slot-Description for ~S>" (dsd-name structure)))
333
334 (defun dd-maybe-make-print-method (defstruct)
335 ;; Maybe generate CLOS DEFMETHOD forms for :print-function/:print-object.
336 (let ((print-function-value (dd-print-function defstruct)))
337 (when (consp print-function-value)
338 (let ((kind (car print-function-value))
339 (function (cdr print-function-value)))
340 (unless (eq kind 'lambda)
341 (setf (dd-print-function defstruct) nil)
342 (let* ((class (dd-name defstruct))
343 (func (if (symbolp function) `',function `#',function)))
344 ;; We can only generate this code if CLOS is loaded. Maybe should
345 ;; signal an error instead of quietly ignoring the defmethod?
346 `((when (fboundp 'print-object)
347 (defmethod print-object ((object ,class) stream)
348 (funcall
349 ,func object stream
350 ,@(when (or (eq kind :print-function)
351 (eq function 'default-structure-print))
352 '(*current-level*))))))))))))
353
354
355 ;;; The legendary macro itself.
356
357 ;;; DEFINE-CLASS-METHODS -- Internal
358 ;;;
359 ;;; Return a list of forms to install print and make-load-form funs, mentioning
360 ;;; them in the expansion so that they can be compiled.
361 ;;;
362
363 (defun define-class-methods (defstruct)
364 (let* ((name (dd-name defstruct))
365 (pom (dd-maybe-make-print-method defstruct)))
366 `(,@(let ((pf (dd-print-function defstruct)))
367 (when pf
368 `((setf (basic-structure-class-print-function (find-class ',name))
369 ,(if (symbolp pf)
370 `',pf
371 `#',pf)))))
372 ,@(let ((mlff (dd-make-load-form-fun defstruct)))
373 (when mlff
374 `((setf (structure-class-make-load-form-fun (find-class ',name))
375 ,(if (symbolp mlff)
376 `',mlff
377 `#',mlff)))))
378 ,@(let ((pure (dd-pure defstruct)))
379 (cond ((eq pure 't)
380 `((setf (layout-pure (%class-layout (find-class ',name)))
381 t)))
382 ((eq pure :substructure)
383 `((setf (layout-pure (%class-layout (find-class ',name)))
384 0)))))
385 ,@(let ((def-con (dd-default-constructor defstruct)))
386 (when (and def-con (not (dd-alternate-metaclass defstruct)))
387 `((setf (structure-class-constructor (find-class ',name))
388 #',def-con))))
389 ,@pom)))
390
391 #+ORIGINAL
392 (defun define-class-methods (defstruct)
393 (let ((name (dd-name defstruct)))
394 `(,@(let ((pf (dd-print-function defstruct)))
395 (when pf
396 `((setf (basic-structure-class-print-function (find-class ',name))
397 ,(if (symbolp pf)
398 `',pf
399 `#',pf)))))
400 ,@(let ((mlff (dd-make-load-form-fun defstruct)))
401 (when mlff
402 `((setf (structure-class-make-load-form-fun (find-class ',name))
403 ,(if (symbolp mlff)
404 `',mlff
405 `#',mlff)))))
406 ,@(let ((pure (dd-pure defstruct)))
407 (cond ((eq pure 't)
408 `((setf (layout-pure (%class-layout (find-class ',name)))
409 t)))
410 ((eq pure :substructure)
411 `((setf (layout-pure (%class-layout (find-class ',name)))
412 0)))))
413 ,@(let ((def-con (dd-default-constructor defstruct)))
414 (when (and def-con (not (dd-alternate-metaclass defstruct)))
415 `((setf (structure-class-constructor (find-class ',name))
416 #',def-con)))))))
417
418 (defun accessor-inherited-data (name defstruct)
419 (assoc name (dd-inherited-accessor-alist defstruct) :test #'eq))
420
421
422 ;;; DEFSTRUCT -- Public
423 ;;;
424 (defmacro defstruct (name-and-options &rest slot-descriptions)
425 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
426 Define the structure type Name. Instances are created by MAKE-<name>, which
427 takes keyword arguments allowing initial slot values to the specified.
428 A SETF'able function <name>-<slot> is defined for each slot to read&write
429 slot values. <name>-p is a type predicate.
430
431 Popular DEFSTRUCT options (see manual for others):
432
433 (:CONSTRUCTOR Name)
434 (:PREDICATE Name)
435 Specify an alternate name for the constructor or predicate.
436
437 (:CONSTRUCTOR Name Lambda-List)
438 Explicitly specify the name and arguments to create a BOA constructor
439 (which is more efficient when keyword syntax isn't necessary.)
440
441 (:INCLUDE Supertype Slot-Spec*)
442 Make this type a subtype of the structure type Supertype. The optional
443 Slot-Specs override inherited slot options.
444
445 Slot options:
446
447 :TYPE Type-Spec
448 Asserts that the value of this slot is always of the specified type.
449
450 :READ-ONLY {T | NIL}
451 If true, no setter function is defined for this slot."
452
453 (let* ((defstruct (parse-name-and-options
454 (if (atom name-and-options)
455 (list name-and-options)
456 name-and-options)))
457 (name (dd-name defstruct)))
458 (when (stringp (car slot-descriptions))
459 (setf (dd-doc defstruct) (pop slot-descriptions)))
460 (dolist (slot slot-descriptions)
461 (allocate-1-slot defstruct (parse-1-dsd defstruct slot)))
462 (if (class-structure-p defstruct)
463 (let ((inherits (inherits-for-structure defstruct)))
464 `(progn
465 (%defstruct ',defstruct ',inherits)
466 (%compiler-only-defstruct ',defstruct ',inherits)
467 ,@(when (eq (dd-type defstruct) 'structure)
468 `((%compiler-defstruct ',defstruct)))
469 ,@(define-raw-accessors defstruct)
470 ,@(define-constructors defstruct)
471 ,@(define-class-methods defstruct)
472 ',name))
473 `(progn
474 (eval-when (compile load eval)
475 (setf (info typed-structure info ',name) ',defstruct))
476 ,@(define-constructors defstruct)
477 ,@(define-predicate defstruct)
478 ,@(define-accessors defstruct)
479 ,@(define-copier defstruct)
480 ',name))))
481
482
483 ;;;; Parsing:
484
485 ;;; PARSE-1-OPTION -- Internal
486 ;;;
487 ;;; Parse a single defstruct option and store the results in Defstruct.
488 ;;;
489 (defun parse-1-option (option defstruct)
490 (let ((args (rest option))
491 (name (dd-name defstruct)))
492 (case (first option)
493 (:conc-name
494 (destructuring-bind (&optional conc-name)
495 args
496 (setf (dd-conc-name defstruct)
497 (if (symbolp conc-name)
498 conc-name
499 (make-symbol (string conc-name))))))
500 (:constructor
501 (destructuring-bind (&optional (cname (concat-pnames 'make- name))
502 &rest stuff)
503 args
504 (push (cons cname stuff) (dd-constructors defstruct))))
505 (:copier
506 (destructuring-bind (&optional (copier (concat-pnames 'copy- name)))
507 args
508 (setf (dd-copier defstruct) copier)))
509 (:predicate
510 (destructuring-bind (&optional (pred (concat-pnames name '-p)))
511 args
512 (setf (dd-predicate defstruct) pred)))
513 (:include
514 (when (dd-include defstruct)
515 (error "Can't have more than one :INCLUDE option."))
516 (setf (dd-include defstruct) args))
517 (:alternate-metaclass
518 (setf (dd-alternate-metaclass defstruct) args))
519 ((:print-function :print-object)
520 (destructuring-bind (&optional (fun 'default-structure-print)) args
521 (setf (dd-print-function defstruct)
522 (if *ANSI-defstruct-options-p*
523 (cons (first option) fun)
524 fun))))
525 (:type
526 (destructuring-bind (type) args
527 (cond ((eq type 'funcallable-structure)
528 (setf (dd-type defstruct) type))
529 ((member type '(list vector))
530 (setf (dd-element-type defstruct) 't)
531 (setf (dd-type defstruct) type))
532 ((and (consp type) (eq (first type) 'vector))
533 (destructuring-bind (vector vtype) type
534 (declare (ignore vector))
535 (setf (dd-element-type defstruct) vtype)
536 (setf (dd-type defstruct) 'vector)))
537 (t
538 (error "~S is a bad :TYPE for Defstruct." type)))))
539 (:named
540 (error "The Defstruct option :NAMED takes no arguments."))
541 (:initial-offset
542 (destructuring-bind (offset) args
543 (setf (dd-offset defstruct) offset)))
544 (:make-load-form-fun
545 (destructuring-bind (fun) args
546 (setf (dd-make-load-form-fun defstruct) fun)))
547 (:pure
548 (destructuring-bind (fun) args
549 (setf (dd-pure defstruct) fun)))
550 (t (error "Unknown DEFSTRUCT option~% ~S" option)))))
551
552 #+ORIGINAL
553 (defun parse-1-option (option defstruct)
554 (let ((args (rest option))
555 (name (dd-name defstruct)))
556 (case (first option)
557 (:conc-name
558 (destructuring-bind (conc-name) args
559 (setf (dd-conc-name defstruct)
560 (if (symbolp conc-name)
561 conc-name
562 (make-symbol (string conc-name))))))
563 (:constructor
564 (destructuring-bind (&optional (cname (concat-pnames 'make- name))
565 &rest stuff)
566 args
567 (push (cons cname stuff) (dd-constructors defstruct))))
568 (:copier
569 (destructuring-bind (&optional (copier (concat-pnames 'copy- name)))
570 args
571 (setf (dd-copier defstruct) copier)))
572 (:predicate
573 (destructuring-bind (&optional (pred (concat-pnames name '-p)))
574 args
575 (setf (dd-predicate defstruct) pred)))
576 (:include
577 (when (dd-include defstruct)
578 (error "Can't have more than one :INCLUDE option."))
579 (setf (dd-include defstruct) args))
580 (:alternate-metaclass
581 (setf (dd-alternate-metaclass defstruct) args))
582 (:print-function
583 (destructuring-bind (&optional (fun 'default-structure-print)) args
584 (setf (dd-print-function defstruct) fun)))
585 (:type
586 (destructuring-bind (type) args
587 (cond ((eq type 'funcallable-structure)
588 (setf (dd-type defstruct) type))
589 ((member type '(list vector))
590 (setf (dd-element-type defstruct) 't)
591 (setf (dd-type defstruct) type))
592 ((and (consp type) (eq (first type) 'vector))
593 (destructuring-bind (vector vtype) type
594 (declare (ignore vector))
595 (setf (dd-element-type defstruct) vtype)
596 (setf (dd-type defstruct) 'vector)))
597 (t
598 (error "~S is a bad :TYPE for Defstruct." type)))))
599 (:named
600 (error "The Defstruct option :NAMED takes no arguments."))
601 (:initial-offset
602 (destructuring-bind (offset) args
603 (setf (dd-offset defstruct) offset)))
604 (:make-load-form-fun
605 (destructuring-bind (fun) args
606 (setf (dd-make-load-form-fun defstruct) fun)))
607 (:pure
608 (destructuring-bind (fun) args
609 (setf (dd-pure defstruct) fun)))
610 (t (error "Unknown DEFSTRUCT option~% ~S" option)))))
611
612
613 ;;; PARSE-NAME-AND-OPTIONS -- Internal
614 ;;;
615 ;;; Given name and options, return a DD holding that info.
616 ;;;
617 (defun parse-name-and-options (name-and-options)
618 (destructuring-bind (name &rest options) name-and-options
619 (let ((defstruct (make-defstruct-description name)))
620 (dolist (option options)
621 (cond ((consp option)
622 (parse-1-option option defstruct))
623 ((eq option :named)
624 (setf (dd-named defstruct) t))
625 ((member option '(:constructor :copier :predicate :named
626 :conc-name))
627 (parse-1-option (list option) defstruct))
628 (t
629 (error "Unrecognized DEFSTRUCT option: ~S" option))))
630
631 (case (dd-type defstruct)
632 (structure
633 (when (dd-offset defstruct)
634 (error "Can't specify :OFFSET unless :TYPE is specified."))
635 (unless (dd-include defstruct)
636 (incf (dd-length defstruct))))
637 (funcallable-structure)
638 (t
639 (when (dd-print-function defstruct)
640 (warn "Silly to specify :PRINT-FUNCTION with :TYPE."))
641 (when (dd-make-load-form-fun defstruct)
642 (warn "Silly to specify :MAKE-LOAD-FORM-FUN with :TYPE."))
643 (when (dd-named defstruct) (incf (dd-length defstruct)))
644 (let ((offset (dd-offset defstruct)))
645 (when offset (incf (dd-length defstruct) offset)))))
646
647 (when (dd-include defstruct)
648 (do-inclusion-stuff defstruct))
649
650 defstruct)))
651
652
653 ;;;; Stuff to parse slot descriptions.
654
655 ;;; PARSE-1-DSD -- Internal
656 ;;;
657 ;;; Parse a slot description for DEFSTRUCT, add it to the description and
658 ;;; return it. If supplied, ISLOT is a pre-initialized DSD that we modify to
659 ;;; get the new slot. This is supplied when handling included slots.
660 ;;;
661 (defun parse-1-dsd (defstruct spec &optional
662 (islot (make-defstruct-slot-description
663 :%name "" :index 0 :type t)))
664 (multiple-value-bind
665 (name default default-p type type-p read-only ro-p)
666 (cond
667 ((listp spec)
668 (destructuring-bind (name &optional (default nil default-p)
669 &key (type nil type-p) (read-only nil ro-p))
670 spec
671 (values name default default-p type type-p read-only ro-p)))
672 (t
673 (when (keywordp spec)
674 (warn "Keyword slot name indicates probable syntax ~
675 error in DEFSTRUCT -- ~S."
676 spec))
677 spec))
678 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
679 (error 'simple-program-error
680 :format-control "Duplicate slot name ~S."
681 :format-arguments (list name)))
682 (setf (dsd-%name islot) (string name))
683 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list islot)))
684
685 (setf (dsd-accessor islot) (concat-pnames (dd-conc-name defstruct) name))
686
687 (when default-p
688 (setf (dsd-default islot) default))
689 (when type-p
690 (setf (dsd-type islot)
691 (if (eq (dsd-type islot) 't)
692 type
693 `(and ,(dsd-type islot) ,type))))
694 (when ro-p
695 (if read-only
696 (setf (dsd-read-only islot) t)
697 (when (dsd-read-only islot)
698 (error "Slot ~S must be read-only in subtype ~S." name
699 (dsd-name islot)))))
700 islot))
701
702
703 ;;; ALLOCATE-1-SLOT -- Internal
704 ;;;
705 ;;; Allocate storage for a DSD in Defstruct. This is where we decide if a
706 ;;; slot is raw or not. If raw, and we haven't allocated a raw-index yet for
707 ;;; the raw data vector, then do it. Raw objects are aligned on the unit of
708 ;;; their size.
709 ;;;
710 (defun allocate-1-slot (defstruct dsd)
711 (let ((type (dsd-type dsd)))
712 (multiple-value-bind
713 (raw-type words)
714 (cond ((not (eq (dd-type defstruct) 'structure))
715 (values nil nil))
716 ((and (subtypep type '(unsigned-byte 32))
717 (not (subtypep type 'fixnum)))
718 (values 'unsigned-byte 1))
719 ((subtypep type 'single-float)
720 (values 'single-float 1))
721 ((subtypep type 'double-float)
722 (values 'double-float 2))
723 #+long-float
724 ((subtypep type 'long-float)
725 (values 'long-float #+x86 3 #+sparc 4))
726 ((subtypep type '(complex single-float))
727 (values 'complex-single-float 2))
728 ((subtypep type '(complex double-float))
729 (values 'complex-double-float 4))
730 #+long-float
731 ((subtypep type '(complex long-float))
732 (values 'complex-long-float #+x86 6 #+sparc 8))
733 (t (values nil nil)))
734
735 (cond ((not raw-type)
736 (setf (dsd-index dsd) (dd-length defstruct))
737 (incf (dd-length defstruct)))
738 (t
739 (unless (dd-raw-index defstruct)
740 (setf (dd-raw-index defstruct) (dd-length defstruct))
741 (incf (dd-length defstruct)))
742 (let ((off (rem (dd-raw-length defstruct) words)))
743 (unless (zerop off)
744 (incf (dd-raw-length defstruct) (- words off))))
745 (setf (dsd-raw-type dsd) raw-type)
746 (setf (dsd-index dsd) (dd-raw-length defstruct))
747 (incf (dd-raw-length defstruct) words)))))
748
749 (undefined-value))
750
751
752 ;;; DO-INCLUSION-STUFF -- Internal
753 ;;;
754 ;;; Process any included slots pretty much like they were specified. Also
755 ;;; inherit various other attributes (print function, etc.)
756 ;;;
757 (defun do-inclusion-stuff (defstruct)
758 (destructuring-bind (included-name &rest modified-slots)
759 (dd-include defstruct)
760 (let* ((type (dd-type defstruct))
761 (included-structure
762 (if (class-structure-p defstruct)
763 (layout-info (compiler-layout-or-lose included-name))
764 (typed-structure-info-or-lose included-name))))
765 (unless (and (eq type (dd-type included-structure))
766 (type= (specifier-type (dd-element-type included-structure))
767 (specifier-type (dd-element-type defstruct))))
768 (error ":TYPE option mismatch between structures ~S and ~S."
769 (dd-name defstruct) included-name))
770
771 (incf (dd-length defstruct) (dd-length included-structure))
772 (when (class-structure-p defstruct)
773 (unless (dd-print-function defstruct)
774 (setf (dd-print-function defstruct)
775 (dd-print-function included-structure)))
776 (unless (dd-make-load-form-fun defstruct)
777 (setf (dd-make-load-form-fun defstruct)
778 (dd-make-load-form-fun included-structure)))
779 (let ((mc (rest (dd-alternate-metaclass included-structure))))
780 (when (and mc (not (dd-alternate-metaclass defstruct)))
781 (setf (dd-alternate-metaclass defstruct)
782 (cons included-name mc))))
783 (when (eq (dd-pure defstruct) :unspecified)
784 (setf (dd-pure defstruct) (dd-pure included-structure)))
785 (setf (dd-raw-index defstruct) (dd-raw-index included-structure))
786 (setf (dd-raw-length defstruct) (dd-raw-length included-structure)))
787
788 (setf (dd-inherited-accessor-alist defstruct)
789 (dd-inherited-accessor-alist included-structure))
790
791 (dolist (islot (dd-slots included-structure))
792 (let* ((iname (dsd-name islot))
793 (modified (or (find iname modified-slots
794 :key #'(lambda (x) (if (atom x) x (car x)))
795 :test #'string=)
796 `(,iname))))
797 ;;
798 ;; We stash away an alist of accessors to parents' slots
799 ;; that have already been created to avoid conflicts later
800 ;; so that structures with :INCLUDE and :CONC-NAME (and
801 ;; other edge cases) can work as specified.
802 (when (dsd-accessor islot)
803 ;; the "oldest" (i.e. highest up the tree of inheritance)
804 ;; will prevail, so don't push new ones on if they
805 ;; conflict.
806 (pushnew (cons (dsd-accessor islot) (dsd-index islot))
807 (dd-inherited-accessor-alist defstruct)
808 :test #'eq :key #'car))
809 (parse-1-dsd defstruct modified
810 (copy-defstruct-slot-description islot)))))))
811
812
813
814 ;;;; Constructors:
815
816 (defun typed-structure-info-or-lose (name)
817 (or (info typed-structure info name)
818 (error ":TYPE'd defstruct ~S not found for inclusion." name)))
819
820 ;;; %GET-COMPILER-LAYOUT -- Internal
821 ;;;
822 ;;; Delay looking for compiler-layout until the constructor is being compiled,
823 ;;; since it doesn't exist until after the eval-when (compile) is compiled.
824 ;;;
825 (defmacro %get-compiler-layout (name)
826 `',(compiler-layout-or-lose name))
827
828 ;;; FIND-NAME-INDICES -- Internal
829 ;;;
830 ;;; Returns a list of pairs (name . index). Used for :TYPE'd constructors
831 ;;; to find all the names that we have to splice in & where. Note that these
832 ;;; types don't have a layout, so we can't look at LAYOUT-INHERITS.
833 ;;;
834 (defun find-name-indices (defstruct)
835 (collect ((res))
836 (let ((infos ()))
837 (do ((info defstruct
838 (typed-structure-info-or-lose (first (dd-include info)))))
839 ((not (dd-include info))
840 (push info infos))
841 (push info infos))
842
843 (let ((i 0))
844 (dolist (info infos)
845 (incf i (or (dd-offset info) 0))
846 (when (dd-named info)
847 (res (cons (dd-name info) i)))
848 (setq i (dd-length info)))))
849
850 (res)))
851
852
853 ;;; CREATE-{STRUCTURE,VECTOR,LIST}-CONSTRUCTOR -- Internal
854 ;;;
855 ;;; These functions are called to actually make a constructor after we have
856 ;;; processed the arglist. The correct variant (according to the DD-TYPE)
857 ;;; should be called. The function is defined with the specified name and
858 ;;; arglist. Vars and Types are used for argument type declarations. Values
859 ;;; are the values for the slots (in order.)
860 ;;;
861 ;;; This is split four ways because:
862 ;;; 1] list & vector structures need "name" symbols stuck in at various weird
863 ;;; places, whereas STRUCTURE structures have a LAYOUT slot.
864 ;;; 2] We really want to use LIST to make list structures, instead of
865 ;;; MAKE-LIST/(SETF ELT).
866 ;;; 3] STRUCTURE structures can have raw slots that must also be allocated and
867 ;;; indirectly referenced. We use SLOT-ACCESSOR-FORM to compute how to set
868 ;;; the slots, which deals with raw slots.
869 ;;; 4] funcallable structures are weird.
870 ;;;
871 (defun create-vector-constructor
872 (defstruct cons-name arglist vars types values)
873 (let ((temp (gensym))
874 (etype (dd-element-type defstruct)))
875 `(defun ,cons-name ,arglist
876 (declare ,@(mapcar #'(lambda (var type) `(type (and ,type ,etype) ,var))
877 vars types))
878 (let ((,temp (make-array ,(dd-length defstruct)
879 :element-type ',(dd-element-type defstruct))))
880 ,@(mapcar #'(lambda (x)
881 `(setf (aref ,temp ,(cdr x)) ',(car x)))
882 (find-name-indices defstruct))
883 ,@(mapcar #'(lambda (dsd value)
884 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
885 (dd-slots defstruct) values)
886 ,temp))))
887 ;;;
888 (defun create-list-constructor
889 (defstruct cons-name arglist vars types values)
890 (let ((vals (make-list (dd-length defstruct) :initial-element nil)))
891 (dolist (x (find-name-indices defstruct))
892 (setf (elt vals (cdr x)) `',(car x)))
893 (loop for dsd in (dd-slots defstruct) and val in values do
894 (setf (elt vals (dsd-index dsd)) val))
895
896 `(defun ,cons-name ,arglist
897 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
898 vars types))
899 (list ,@vals))))
900 ;;;
901 (defun create-structure-constructor
902 (defstruct cons-name arglist vars types values)
903 (let* ((temp (gensym))
904 (raw-index (dd-raw-index defstruct))
905 (n-raw-data (when raw-index (gensym))))
906 `(defun ,cons-name ,arglist
907 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
908 vars types))
909 (let ((,temp (truly-the ,(dd-name defstruct)
910 (%make-instance ,(dd-length defstruct))))
911 ,@(when n-raw-data
912 `((,n-raw-data
913 (make-array ,(dd-raw-length defstruct)
914 :element-type '(unsigned-byte 32))))))
915 (setf (%instance-layout ,temp)
916 (%get-compiler-layout ,(dd-name defstruct)))
917 ,@(when n-raw-data
918 `((setf (%instance-ref ,temp ,raw-index) ,n-raw-data)))
919 ,@(mapcar #'(lambda (dsd value)
920 (multiple-value-bind
921 (accessor index data)
922 (slot-accessor-form defstruct dsd temp n-raw-data)
923 `(setf (,accessor ,data ,index) ,value)))
924 (dd-slots defstruct)
925 values)
926 ,temp))))
927 ;;;
928 (defun create-fin-constructor
929 (defstruct cons-name arglist vars types values)
930 (let ((temp (gensym)))
931 `(defun ,cons-name ,arglist
932 (declare ,@(mapcar #'(lambda (var type) `(type ,type ,var))
933 vars types))
934 (let ((,temp (truly-the
935 ,(dd-name defstruct)
936 (%make-funcallable-instance
937 ,(dd-length defstruct)
938 (%get-compiler-layout ,(dd-name defstruct))))))
939 ,@(mapcar #'(lambda (dsd value)
940 `(setf (%funcallable-instance-info
941 ,temp ,(dsd-index dsd))
942 ,value))
943 (dd-slots defstruct) values)
944 ,temp))))
945
946
947 ;;; CREATE-KEYWORD-CONSTRUCTOR -- Internal
948 ;;;
949 ;;; Create a default (non-BOA) keyword constructor.
950 ;;;
951 (defun create-keyword-constructor (defstruct creator)
952 (collect ((arglist (list '&key))
953 (types)
954 (vals))
955 (dolist (slot (dd-slots defstruct))
956 (let ((dum (gensym))
957 (name (dsd-name slot)))
958 (arglist `((,(intern (string name) "KEYWORD") ,dum)
959 ,(dsd-default slot)))
960 (types (dsd-type slot))
961 (vals dum)))
962 (funcall creator
963 defstruct (dd-default-constructor defstruct)
964 (arglist) (vals) (types) (vals))))
965
966
967 ;;; CREATE-BOA-CONSTRUCTOR -- Internal
968 ;;;
969 ;;; Given a structure and a BOA constructor spec, call Creator with the
970 ;;; appropriate args to make a constructor.
971 ;;;
972 (defun create-boa-constructor (defstruct boa creator)
973 (multiple-value-bind (req opt restp rest keyp keys allowp aux)
974 (kernel:parse-lambda-list (second boa))
975 (collect ((arglist)
976 (vars)
977 (types))
978 (labels ((get-slot (name)
979 (let ((res (find name (dd-slots defstruct) :test #'string=
980 :key #'dsd-name)))
981 (if res
982 (values (dsd-type res) (dsd-default res))
983 (values t nil))))
984 (do-default (arg)
985 (multiple-value-bind (type default) (get-slot arg)
986 (arglist `(,arg ,default))
987 (vars arg)
988 (types type))))
989 (dolist (arg req)
990 (arglist arg)
991 (vars arg)
992 (types (get-slot arg)))
993
994 (when opt
995 (arglist '&optional)
996 (dolist (arg opt)
997 (if (consp arg)
998 (destructuring-bind (name &optional
999 (def (nth-value 1 (get-slot name)))
1000 (supplied-test nil supplied-test-p))
1001 arg
1002 (arglist
1003 `(,name ,def ,@(if supplied-test-p `(,supplied-test) nil)))
1004 (vars name)
1005 (types (get-slot name)))
1006 (do-default arg))))
1007
1008 (when restp
1009 (arglist '&rest rest)
1010 (vars rest)
1011 (types 'list))
1012
1013 (when keyp
1014 (arglist '&key)
1015 (dolist (arg keys)
1016 (if (consp arg)
1017 (destructuring-bind
1018 (name-spec &optional
1019 (def nil def-p)
1020 (supplied-test nil supplied-test-p))
1021 arg
1022 (let ((name (if (consp name-spec)
1023 (destructuring-bind (key var) name-spec
1024 (declare (ignore key))
1025 var)
1026 name-spec)))
1027 (multiple-value-bind (type slot-def) (get-slot name)
1028 (arglist
1029 `(,name-spec
1030 ,(if def-p def slot-def)
1031 ,@(if supplied-test-p `(,supplied-test) nil)))
1032 (vars name)
1033 (types type))))
1034 (do-default arg))))
1035
1036 (when allowp (arglist '&allow-other-keys))
1037
1038 (when aux
1039 (arglist '&aux)
1040 (dolist (arg aux)
1041 (let* ((arg (if (consp arg) arg (list arg)))
1042 (var (first arg)))
1043 (arglist arg)
1044 (vars var)
1045 (types (get-slot var))))))
1046
1047 (funcall creator defstruct (first boa)
1048 (arglist) (vars) (types)
1049 (mapcar #'(lambda (slot)
1050 (or (find (dsd-name slot) (vars) :test #'string=)
1051 (dsd-default slot)))
1052 (dd-slots defstruct))))))
1053
1054
1055 ;;; DEFINE-CONSTRUCTORS -- Internal
1056 ;;;
1057 ;;; Grovel the constructor options, and decide what constructors (if any) to
1058 ;;; create.
1059 ;;;
1060 (defun define-constructors (defstruct)
1061 (let ((no-constructors nil)
1062 (boas ())
1063 (defaults ())
1064 (creator (ecase (dd-type defstruct)
1065 (structure #'create-structure-constructor)
1066 (funcallable-structure #'create-fin-constructor)
1067 (vector #'create-vector-constructor)
1068 (list #'create-list-constructor))))
1069 (dolist (constructor (dd-constructors defstruct))
1070 (destructuring-bind (name &optional (boa-ll nil boa-p))
1071 constructor
1072 (declare (ignore boa-ll))
1073 (cond ((not name) (setq no-constructors t))
1074 (boa-p (push constructor boas))
1075 (t (push name defaults)))))
1076
1077 (when no-constructors
1078 (when (or defaults boas)
1079 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs."))
1080 (return-from define-constructors ()))
1081
1082 (unless (or defaults boas)
1083 (push (concat-pnames 'make- (dd-name defstruct)) defaults))
1084
1085 (collect ((res))
1086 (when defaults
1087 (let ((cname (first defaults)))
1088 (setf (dd-default-constructor defstruct) cname)
1089 (res (create-keyword-constructor defstruct creator))
1090 (dolist (other-name (rest defaults))
1091 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1092 (res `(declaim (ftype function ',other-name))))))
1093
1094 (dolist (boa boas)
1095 (res (create-boa-constructor defstruct boa creator)))
1096
1097 (res))))
1098
1099 ;;;; Slot accessors for raw slots:
1100
1101 ;;; SLOT-ACCESSOR-FORM -- Internal
1102 ;;;
1103 ;;; Return info about how to read/write a slot in the value stored in
1104 ;;; Object. This is also used by constructors (we can't use the accessor
1105 ;;; function, since some slots are read-only.) If supplied, Data is a variable
1106 ;;; holding the raw-data vector.
1107 ;;;
1108 ;;; Values:
1109 ;;; 1] Accessor function name (setfable)
1110 ;;; 2] Index to pass to accessor.
1111 ;;; 3] Object form to pass to accessor.
1112 ;;;
1113 (defun slot-accessor-form (defstruct slot &optional (object 'object) data)
1114 (let ((rtype (dsd-raw-type slot)))
1115 (values
1116 (ecase rtype
1117 (single-float '%raw-ref-single)
1118 (double-float '%raw-ref-double)
1119 #+long-float
1120 (long-float '%raw-ref-long)
1121 (complex-single-float '%raw-ref-complex-single)
1122 (complex-double-float '%raw-ref-complex-double)
1123 #+long-float
1124 (complex-long-float '%raw-ref-complex-long)
1125 (unsigned-byte 'aref)
1126 ((t)
1127 (if (eq (dd-type defstruct) 'funcallable-structure)
1128 '%funcallable-instance-info
1129 '%instance-ref)))
1130 (case rtype
1131 #+long-float
1132 (complex-long-float
1133 (truncate (dsd-index slot) #+x86 6 #+sparc 8))
1134 #+long-float
1135 (long-float
1136 (truncate (dsd-index slot) #+x86 3 #+sparc 4))
1137 (double-float
1138 (ash (dsd-index slot) -1))
1139 (complex-double-float
1140 (ash (dsd-index slot) -2))
1141 (complex-single-float
1142 (ash (dsd-index slot) -1))
1143 (t
1144 (dsd-index slot)))
1145 (cond
1146 ((eq rtype 't) object)
1147 (data)
1148 (t
1149 `(truly-the (simple-array (unsigned-byte 32) (*))
1150 (%instance-ref ,object ,(dd-raw-index defstruct))))))))
1151
1152
1153 ;;; dsd-inherited-p -- Internal
1154 ;;;
1155 ;;; True when the defstruct slot has been inherited from an included
1156 ;;; structure.
1157 ;;;
1158 (defun dsd-inherited-p (defstruct slot)
1159 (assoc (dsd-accessor slot) (dd-inherited-accessor-alist defstruct) :test #'eq))
1160
1161 ;;; DEFINE-RAW-ACCESSORS -- Internal
1162 ;;;
1163 ;;; Define readers and writers for raw slots as inline functions. We use
1164 ;;; the special RAW-REF operations to store floats in the raw data vector. We
1165 ;;; also define FIN accessors here.
1166 ;;;
1167 (defun define-raw-accessors (defstruct)
1168 (let ((name (dd-name defstruct)))
1169 (collect ((res))
1170 (dolist (slot (dd-slots defstruct))
1171 (let ((stype (dsd-type slot))
1172 (aname (dsd-accessor slot)))
1173 (multiple-value-bind (accessor offset data)
1174 (slot-accessor-form defstruct slot)
1175 (unless (or (dsd-inherited-p defstruct slot)
1176 (eq accessor '%instance-ref))
1177 (res `(declaim (inline ,aname)))
1178 (res `(declaim (ftype (function (,name) ,stype) ,aname)))
1179 (res `(defun ,aname (object)
1180 (truly-the ,stype (,accessor ,data ,offset))))
1181 (unless (dsd-read-only slot)
1182 (res `(declaim (inline (setf ,aname))))
1183 (res `(declaim (ftype (function (,stype ,name) ,stype)
1184 (setf ,aname))))
1185 (res
1186 `(defun (setf ,aname) (new-value object)
1187 (setf (,accessor ,data ,offset) new-value)
1188 new-value)))))))
1189
1190 (when (eq (dd-type defstruct) 'funcallable-structure)
1191 (let ((pred (dd-predicate defstruct)))
1192 (when pred
1193 (res `(declaim (inline ,pred)))
1194 (res `(defun ,pred (x) (typep x ',name))))))
1195
1196 (res))))
1197
1198
1199 ;;;; Typed (non-class) structures:
1200
1201 ;;; DD-LISP-TYPE -- Internal
1202 ;;;
1203 ;;; Return a type specifier we can use for testing :TYPE'd structures.
1204 ;;;
1205 (defun dd-lisp-type (defstruct)
1206 (ecase (dd-type defstruct)
1207 (list 'list)
1208 (vector `(simple-array ,(dd-element-type defstruct)
1209 (*)))))
1210
1211 ;;; DEFINE-ACCESSORS -- Internal
1212 ;;;
1213 ;;; Returns a list of function definitions for accessing and setting the
1214 ;;; slots of the a typed Defstruct. The functions are proclaimed to be inline,
1215 ;;; and the types of their arguments and results are declared as well. We
1216 ;;; count on the compiler to do clever things with Elt.
1217 ;;;
1218 (defun define-accessors (defstruct)
1219 (collect ((stuff))
1220 (let ((ltype (dd-lisp-type defstruct)))
1221 (dolist (slot (dd-slots defstruct))
1222 (let* ((aname (dsd-accessor slot))
1223 (index (dsd-index slot))
1224 (slot-type `(and ,(dsd-type slot)
1225 ,(dd-element-type defstruct)))
1226 (inherited (accessor-inherited-data aname defstruct)))
1227 (cond ((not inherited)
1228 (stuff `(declaim (inline ,aname (setf ,aname))))
1229 (stuff `(defun ,aname (structure)
1230 (declare (type ,ltype structure))
1231 (the ,slot-type (elt structure ,index))))
1232 (unless (dsd-read-only slot)
1233 (stuff
1234 `(defun (setf ,aname) (new-value structure)
1235 (declare (type ,ltype structure) (type ,slot-type new-value))
1236 (setf (elt structure ,index) new-value)))))
1237 ((not (= (cdr inherited) index))
1238 (warn 'simple-style-warning
1239 :format-control
1240 "~@<Non-overwritten accessor ~S does not access ~
1241 slot with name ~S (accessing an inherited slot ~
1242 instead).~:@>"
1243 :format-arguments (list aname (dsd-%name slot))))))
1244 ))
1245 (stuff)))
1246
1247
1248 ;;; Define-Copier returns the definition for a copier function of a typed
1249 ;;; Defstruct if one is desired.
1250 (defun define-copier (defstruct)
1251 (when (dd-copier defstruct)
1252 `((setf (fdefinition ',(dd-copier defstruct)) #'copy-seq)
1253 (declaim (ftype function ,(dd-copier defstruct))))))
1254
1255
1256 ;;; Define-Predicate returns a definition for a predicate function if one is
1257 ;;; desired. Rather vaguely specified w.r.t. inclusion.
1258 ;;;
1259 (defun define-predicate (defstruct)
1260 (let ((name (dd-name defstruct))
1261 (pred (dd-predicate defstruct)))
1262 (when (and pred (dd-named defstruct))
1263 (let ((ltype (dd-lisp-type defstruct))
1264 (index (cdr (car (last (find-name-indices defstruct))))))
1265 (if (eq ltype 'list)
1266 `((defun ,pred (object)
1267 (and (typep object 'list)
1268 (eq (nth ,index object) ',name))))
1269 `((defun ,pred (object)
1270 (and (typep object 'vector)
1271 (array-in-bounds-p object ,index)
1272 (eq (aref object ,index) ',name)))))))))
1273
1274
1275 ;;;; Load time support for default structures (%DEFSTRUCT)
1276 ;;;
1277 ;;; In the normal case of structures that have a real type (i.e. no :Type
1278 ;;; option was specified), we want to optimize things for space as well as
1279 ;;; speed, since there can be thousands of defined slot accessors.
1280 ;;;
1281 ;;; What we do is defined the accessors and copier as closures over
1282 ;;; general-case code. Since the compiler will normally open-code accessors,
1283 ;;; the (minor) efficiency penalty is not a concern.
1284
1285 ;;; Typep-To-Layout -- Internal
1286 ;;;
1287 ;;; Return true if Obj is an object of the structure type corresponding to
1288 ;;; Layout. This is called by the accessor closures, which have a handle on
1289 ;;; the type's layout.
1290 ;;;
1291 (declaim (inline typep-to-layout))
1292 (defun typep-to-layout (obj layout)
1293 (declare (type layout layout) (optimize (speed 3) (safety 0)))
1294 (when (layout-invalid layout)
1295 (error "Obsolete structure accessor function called."))
1296 (and (%instancep obj)
1297 (let ((depth (layout-inheritance-depth layout))
1298 (obj-layout (%instance-layout obj)))
1299 (cond ((eq obj-layout layout) t)
1300 ((layout-invalid obj-layout)
1301 (error 'layout-invalid :expected-type (layout-class obj-layout)
1302 :datum obj))
1303 (t
1304 (and (> (layout-inheritance-depth obj-layout) depth)
1305 (eq (svref (layout-inherits obj-layout) depth)
1306 layout)))))))
1307
1308
1309 ;;; STRUCTURE-SLOT-SETTER, STRUCTURE-SLOT-ACCESSOR -- Internal
1310 ;;;
1311 ;;; Return closures to do slot access (set), according to Layout and DSD.
1312 ;;; We check types, then do the access. This is only used for normal slots
1313 ;;; (not raw.)
1314 ;;;
1315 (defun structure-slot-accessor (layout dsd)
1316 (let ((class (layout-class layout)))
1317 (if (typep class 'basic-structure-class)
1318 #'(lambda (structure)
1319 (declare (optimize (speed 3) (safety 0)))
1320 (unless (typep-to-layout structure layout)
1321 (error 'simple-type-error
1322 :datum structure
1323 :expected-type class
1324 :format-control "Structure for accessor ~S is not a ~S:~% ~S"
1325 :format-arguments (list (dsd-accessor dsd)
1326 (%class-name class)
1327 structure)))
1328 (%instance-ref structure (dsd-index dsd)))
1329 #'(lambda (structure)
1330 (declare (optimize (speed 3) (safety 0)))
1331 (unless (%typep structure class)
1332 (error 'simple-type-error
1333 :datum structure
1334 :expected-type class
1335 :format-control "Structure for accessor ~S is not a ~S:~% ~S"
1336 :format-arguments (list (dsd-accessor dsd) class
1337 structure)))
1338 (%instance-ref structure (dsd-index dsd))))))
1339 ;;;
1340 (defun structure-slot-setter (layout dsd)
1341 (let ((class (layout-class layout)))
1342 (if (typep class 'basic-structure-class)
1343 #'(lambda (new-value structure)
1344 (declare (optimize (speed 3) (safety 0)))
1345 (unless (typep-to-layout structure layout)
1346 (error 'simple-type-error
1347 :datum structure
1348 :expected-type class
1349 :format-control "Structure for setter ~S is not a ~S:~% ~S"
1350 :format-arguments (list `(setf ,(dsd-accessor dsd))
1351 (%class-name class)
1352 structure)))
1353 (unless (%typep new-value (dsd-type dsd))
1354 (error 'simple-type-error
1355 :datum new-value
1356 :expected-type (dsd-type dsd)
1357 :format-control "New-Value for setter ~S is not a ~S:~% ~S."
1358 :format-arguments (list `(setf ,(dsd-accessor dsd))
1359 (dsd-type dsd)
1360 new-value)))
1361 (setf (%instance-ref structure (dsd-index dsd)) new-value))
1362 #'(lambda (new-value structure)
1363 (declare (optimize (speed 3) (safety 0)))
1364 (unless (%typep structure class)
1365 (error 'simple-type-error
1366 :datum structure
1367 :expected-type class
1368 :format-control "Structure for setter ~S is not a ~S:~% ~S"
1369 :format-arguments (list `(setf ,(dsd-accessor dsd))
1370 (%class-name class)
1371 structure)))
1372 (unless (%typep new-value (dsd-type dsd))
1373 (error 'simple-type-error
1374 :datum new-value
1375 :expected-type (dsd-type dsd)
1376 :format-control "New-Value for setter ~S is not a ~S:~% ~S."
1377 :format-arguments (list `(setf ,(dsd-accessor dsd))
1378 (dsd-type dsd)
1379 new-value)))
1380 (setf (%instance-ref structure (dsd-index dsd)) new-value)))))
1381
1382
1383 ;;; %Defstruct -- Internal
1384 ;;;
1385 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure described by
1386 ;;; Info. Create the class & layout, checking for incompatible redefinition.
1387 ;;; Define setters, accessors, copier, predicate, documentation, instantiate
1388 ;;; definition in load-time env. This is only called for default structures.
1389 ;;;
1390 (defun %defstruct (info inherits)
1391 (declare (type defstruct-description info))
1392 (multiple-value-bind (class layout old-layout)
1393 (ensure-structure-class info inherits "current" "new")
1394 (cond ((not old-layout)
1395 (unless (eq (%class-layout class) layout)
1396 (register-layout layout)))
1397 (t
1398 (let ((old-info (layout-info old-layout)))
1399 (when (defstruct-description-p old-info)
1400 (dolist (slot (dd-slots old-info))
1401 (unless (dsd-inherited-p old-info slot)
1402 (let ((aname (dsd-accessor slot)))
1403 (fmakunbound aname)
1404 (unless (dsd-read-only slot)
1405 (fmakunbound `(setf ,aname))))))))
1406 (%redefine-defstruct class old-layout layout)
1407 (setq layout (%class-layout class))))
1408
1409 (setf (find-class (dd-name info)) class)
1410
1411 (unless (eq (dd-type info) 'funcallable-structure)
1412 (dolist (slot (dd-slots info))
1413 (unless (or (dsd-inherited-p info slot)
1414 (not (eq (dsd-raw-type slot) 't)))
1415 (let* ((aname (dsd-accessor slot))
1416 (inherited (accessor-inherited-data aname info)))
1417 (unless inherited
1418 (setf (symbol-function aname)
1419 (structure-slot-accessor layout slot))
1420 (unless (dsd-read-only slot)
1421 (setf (fdefinition `(setf ,aname))
1422 (structure-slot-setter layout slot)))))
1423
1424 ))
1425
1426 (when (dd-predicate info)
1427 (setf (symbol-function (dd-predicate info))
1428 #'(lambda (object)
1429 (declare (optimize (speed 3) (safety 0)))
1430 (typep-to-layout object layout))))
1431
1432 (when (dd-copier info)
1433 (setf (symbol-function (dd-copier info))
1434 #'(lambda (structure)
1435 (declare (optimize (speed 3) (safety 0)))
1436 (unless (typep-to-layout structure layout)
1437 (error 'simple-type-error
1438 :datum structure
1439 :expected-type class
1440 :format-control "Structure for copier is not a ~S:~% ~S"
1441 :format-arguments (list class structure)))
1442 (copy-structure structure))))))
1443
1444 (when (dd-doc info)
1445 (setf (documentation (dd-name info) 'type) (dd-doc info)))
1446
1447 (undefined-value))
1448
1449
1450 ;;;; Redefinition stuff:
1451
1452 ;;; ENSURE-STRUCTURE-CLASS -- Internal
1453 ;;;
1454 ;;; Called when we are about to define a structure class. Returns a
1455 ;;; (possibly new) class object and the layout which should be used for the new
1456 ;;; definition (may be the current layout, and also might be an uninstalled
1457 ;;; forward referenced layout.) The third value is true if this is an
1458 ;;; incompatible redefinition, in which case it is the old layout.
1459 ;;;
1460 (defun ensure-structure-class (info inherits old-context new-context
1461 &optional compiler-layout)
1462 (multiple-value-bind
1463 (class old-layout)
1464 (destructuring-bind (&optional name (class 'kernel::structure-class)
1465 (constructor 'make-structure-class))
1466 (dd-alternate-metaclass info)
1467 (declare (ignore name))
1468 (insured-find-class (dd-name info)
1469 (if (eq class 'kernel::structure-class)
1470 #'(lambda (x) (typep x 'kernel::structure-class))
1471 #'(lambda (x) (typep x (find-class class))))
1472 (fdefinition constructor)))
1473 (setf (%class-direct-superclasses class)
1474 (if (eq (dd-name info) 'lisp-stream)
1475 ;; Hack to add stream as a superclass mixin to lisp-streams.
1476 (list (layout-class (svref inherits (1- (length inherits))))
1477 (layout-class (svref inherits (- (length inherits) 2))))
1478 (list (layout-class (svref inherits (1- (length inherits)))))))
1479 (let ((new-layout (make-layout :class class
1480 :inherits inherits
1481 :inheritance-depth (length inherits)
1482 :length (dd-length info)
1483 :info info))
1484 (old-layout (or compiler-layout old-layout)))
1485 (cond
1486 ((not old-layout)
1487 (values class new-layout nil))
1488 ((not *type-system-initialized*)
1489 (setf (layout-info old-layout) info)
1490 (values class old-layout nil))
1491 ((redefine-layout-warning old-layout old-context
1492 new-layout new-context)
1493 (values class new-layout old-layout))
1494 (t
1495 (let ((old-info (layout-info old-layout)))
1496 (typecase old-info
1497 ((or defstruct-description)
1498 (cond ((redefine-structure-warning class old-info info)
1499 (values class new-layout old-layout))
1500 (t
1501 (setf (layout-info old-layout) info)
1502 (values class old-layout nil))))
1503 (null
1504 (setf (layout-info old-layout) info)
1505 (values class old-layout nil))
1506 (t
1507 (warn "Shouldn't happen! Some strange thing in LAYOUT-INFO:~
1508 ~% ~S"
1509 old-layout)
1510 (values class new-layout old-layout)))))))))
1511
1512
1513 ;;; COMPARE-SLOTS -- Internal
1514 ;;;
1515 ;;; Compares the slots of Old and New, returning 3 lists of slot names:
1516 ;;; 1] Slots which have moved,
1517 ;;; 2] Slots whose type has changed,
1518 ;;; 3] Deleted slots.
1519 ;;;
1520 (defun compare-slots (old new)
1521 (let* ((oslots (dd-slots old))
1522 (nslots (dd-slots new))
1523 (onames (mapcar #'dsd-name oslots))
1524 (nnames (mapcar #'dsd-name nslots)))
1525 (collect ((moved)
1526 (retyped))
1527 (dolist (name (intersection onames nnames))
1528 (let ((os (find name oslots :key #'dsd-name))
1529 (ns (find name nslots :key #'dsd-name)))
1530 (unless (subtypep (dsd-type ns) (dsd-type os))
1531 (retyped name))
1532 (unless (and (= (dsd-index os) (dsd-index ns))
1533 (eq (dsd-raw-type os) (dsd-raw-type ns)))
1534 (moved name))))
1535 (values (moved)
1536 (retyped)
1537 (set-difference onames nnames)))))
1538
1539
1540 ;;; REDEFINE-STRUCTURE-WARNING -- Internal
1541 ;;;
1542 ;;; Give a warning and return true if we are redefining a structure with
1543 ;;; different slots than in the currently loaded version.
1544 ;;;
1545 (defun redefine-structure-warning (class old new)
1546 (declare (type defstruct-description old new)
1547 (type kernel::class class)
1548 (ignore class))
1549 (let ((name (dd-name new)))
1550 (multiple-value-bind (moved retyped deleted)
1551 (compare-slots old new)
1552 (when (or moved retyped deleted)
1553 (warn
1554 "Incompatibly redefining slots of structure class ~S~@
1555 Make sure any uses of affected accessors are recompiled:~@
1556 ~@[ These slots were moved to new positions:~% ~S~%~]~
1557 ~@[ These slots have new incompatible types:~% ~S~%~]~
1558 ~@[ These slots were deleted:~% ~S~%~]"
1559 name moved retyped deleted)
1560 t))))
1561
1562
1563 ;;; %REDEFINE-DEFSTRUCT -- Internal
1564 ;;;
1565 ;;; This function is called when we are incompatibly redefining a structure
1566 ;;; Class to have the specified New-Layout. We signal an error with some
1567 ;;; proceed options and return the layout that should be used.
1568 ;;;
1569 (defun %redefine-defstruct (class old-layout new-layout)
1570 (declare (type class class) (type layout old-layout new-layout))
1571 (let ((name (class-proper-name class)))
1572 (restart-case
1573 (error "Redefining class ~S incompatibly with the current ~
1574 definition."
1575 name)
1576 (continue ()
1577 :report "Invalidate already loaded code and instances, use new definition."
1578 (warn "Previously loaded ~S accessors will no longer work." name)
1579 (register-layout new-layout))
1580 (clobber-it ()
1581 :report "Assume redefinition is compatible, allow old code and instances."
1582 (warn "Any old ~S instances will be in a bad way.~@
1583 I hope you know what you're doing..."
1584 name)
1585 (register-layout new-layout :invalidate nil
1586 :destruct-layout old-layout))))
1587 (undefined-value))
1588
1589
1590 ;;; UNDEFINE-STRUCTURE -- Interface
1591 ;;;
1592 ;;; Blow away all the compiler info for the structure CLASS.
1593 ;;; Iterate over this type, clearing the compiler structure
1594 ;;; type info, and undefining all the associated functions.
1595 ;;;
1596 (defun undefine-structure (class)
1597 (let ((info (layout-info (%class-layout class))))
1598 (when (defstruct-description-p info)
1599 (let ((type (dd-name info)))
1600 (setf (info type compiler-layout type) nil)
1601 (undefine-function-name (dd-copier info))
1602 (undefine-function-name (dd-predicate info))
1603 (dolist (slot (dd-slots info))
1604 (unless (dsd-inherited-p info slot)
1605 (let ((aname (dsd-accessor slot)))
1606 (unless (accessor-inherited-data aname info)
1607 (undefine-function-name aname)
1608 (unless (dsd-read-only slot)
1609 (undefine-function-name `(setf ,aname))))
1610
1611 ))))
1612 ;;
1613 ;; Clear out the SPECIFIER-TYPE cache so that subsequent references are
1614 ;; unknown types.
1615 (values-specifier-type-cache-clear)))
1616 (undefined-value))
1617
1618
1619 ;;;; Compiler stuff:
1620
1621 ;;; DEFINE-DEFSTRUCT-NAME -- Internal
1622 ;;;
1623 ;;; Like DEFINE-FUNCTION-NAME, but we also set the kind to :DECLARED and
1624 ;;; blow away any ASSUMED-TYPE. Also, if the thing is a slot accessor
1625 ;;; currently, quietly unaccessorize it. And if there are any undefined
1626 ;;; warnings, we nuke them.
1627 ;;;
1628 (defun define-defstruct-name (name)
1629 (when name
1630 (when (info function accessor-for name)
1631 (setf (info function accessor-for name) nil))
1632 (define-function-name name)
1633 (note-name-defined name :function)
1634 (setf (info function where-from name) :declared)
1635 (when (info function assumed-type name)
1636 (setf (info function assumed-type name) nil)))
1637 (undefined-value))
1638
1639
1640 ;;; INHERITS-FOR-STRUCTURE -- Internal
1641 ;;;
1642 ;;; This function is called at macroexpand time to compute the INHERITS
1643 ;;; vector for a structure type definition.
1644 ;;;
1645 (defun inherits-for-structure (info)
1646 (declare (type defstruct-description info))
1647 (let* ((include (dd-include info))
1648 (superclass-opt (dd-alternate-metaclass info))
1649 (super
1650 (if include
1651 (compiler-layout-or-lose (first include))
1652 (%class-layout (find-class (or (first superclass-opt)
1653 'structure-object))))))
1654 (if (eq (dd-name info) 'lisp-stream)
1655 ;; Hack to added the stream class as a mixin for lisp-streams.
1656 (concatenate 'simple-vector (layout-inherits super)
1657 (vector super (%class-layout (find-class 'stream))))
1658 (concatenate 'simple-vector (layout-inherits super) (vector super)))))
1659
1660 ;;; %COMPILER-ONLY-DEFSTRUCT -- Internal
1661 ;;;
1662 ;;; This function is called at compile-time to do the compile-time-only
1663 ;;; actions for defining a structure type. It installs the class in the type
1664 ;;; system in a similar way to %DEFSTRUCT, but is quieter and safer in the case
1665 ;;; of redefinition. Eval-when doesn't do the right thing when nested or
1666 ;;; non-top-level, so this is magically called by the compiler.
1667 ;;;
1668 ;;; Basically, this function avoids trashing the compiler by only actually
1669 ;;; defining the class if there is no current definition. Instead, we just set
1670 ;;; the INFO TYPE COMPILER-LAYOUT.
1671 ;;;
1672 (defun %compiler-only-defstruct (info inherits)
1673 (multiple-value-bind
1674 (class layout old-layout)
1675 (multiple-value-bind
1676 (clayout clayout-p)
1677 (info type compiler-layout (dd-name info))
1678 (ensure-structure-class info inherits
1679 (if clayout-p "previously compiled" "current")
1680 "compiled"
1681 clayout))
1682 (cond
1683 (old-layout
1684 (undefine-structure (layout-class old-layout))
1685 (when (and (%class-subclasses class)
1686 (not (eq layout old-layout)))
1687 (collect ((subs))
1688 (do-hash (class layout (%class-subclasses class))
1689 (declare (ignore layout))
1690 (undefine-structure class)
1691 (subs (class-proper-name class)))
1692 (when (subs)
1693 (warn "Removing old subclasses of ~S:~% ~S"
1694 (%class-name class) (subs))))))
1695 (t
1696 (unless (eq (%class-layout class) layout)
1697 (register-layout layout :invalidate nil))
1698 (setf (find-class (dd-name info)) class)))
1699
1700 (setf (info type compiler-layout (dd-name info)) layout))
1701
1702 (undefined-value))
1703
1704
1705 ;;; %%Compiler-Defstruct -- External
1706 ;;;
1707 ;;; This function does the (compile load eval) time actions for updating the
1708 ;;; compiler's global meta-information to represent the definition of the
1709 ;;; structure described by Info. This primarily amounts to setting up info
1710 ;;; about the accessor and other implicitly defined functions. The
1711 ;;; constructors are explicitly defined by top-level code.
1712 ;;;
1713 (defun %%compiler-defstruct (info)
1714 (declare (type defstruct-description info))
1715 (let* ((name (dd-name info))
1716 (class (find-class name)))
1717 (let ((copier (dd-copier info)))
1718 (when copier
1719 (proclaim `(ftype (function (,name) ,name) ,copier))))
1720
1721 (let ((pred (dd-predicate info)))
1722 (when pred
1723 (define-defstruct-name pred)
1724 (setf (info function inlinep pred) :inline)
1725 (setf (info function inline-expansion pred)
1726 `(lambda (x) (typep x ',name)))))
1727
1728 (dolist (slot (dd-slots info))
1729 (let* ((aname (dsd-accessor slot))
1730 (setf-fun `(setf ,aname))
1731 (inherited (and aname (accessor-inherited-data aname info))))
1732
1733 (cond (inherited
1734 (unless (= (cdr inherited) (dsd-index slot))
1735 (warn 'simple-style-warning
1736 :format-control
1737 "~@<Non-overwritten accessor ~S does not access ~
1738 slot with name ~S (accessing an inherited slot ~
1739 instead).~:@>"
1740 :format-arguments (list aname (dsd-%name slot)))))
1741 (t
1742 (unless (or (dsd-inherited-p info slot)
1743 (not (eq (dsd-raw-type slot) 't)))
1744 (define-defstruct-name aname)
1745 (setf (info function accessor-for aname) class)
1746 (unless (dsd-read-only slot)
1747 (define-defstruct-name setf-fun)
1748 (setf (info function accessor-for setf-fun) class)))))
1749
1750 )))
1751
1752 (undefined-value))
1753
1754 (setf (symbol-function '%compiler-defstruct) #'%%compiler-defstruct)
1755
1756
1757 ;;; COPY-STRUCTURE -- Public
1758 ;;;
1759 ;;; Copy any old kind of structure.
1760 ;;;
1761 (defun copy-structure (structure)
1762 "Return a copy of Structure with the same (EQL) slot values."
1763 (declare (type structure-object structure) (optimize (speed 3) (safety 0)))
1764 (let* ((len (%instance-length structure))
1765 (res (%make-instance len))
1766 (layout (%instance-layout structure)))
1767 (declare (type index len))
1768 (when (layout-invalid layout)
1769 (error "Copying an obsolete structure:~% ~S" structure))
1770
1771 (dotimes (i len)
1772 (declare (type index i))
1773 (setf (%instance-ref res i)
1774 (%instance-ref structure i)))
1775
1776 (let ((raw-index (dd-raw-index (layout-info layout))))
1777 (when raw-index
1778 (let* ((data (%instance-ref structure raw-index))
1779 (raw-len (length data))
1780 (new (make-array raw-len :element-type '(unsigned-byte 32))))
1781 (declare (type (simple-array (unsigned-byte 32) (*)) data))
1782 (setf (%instance-ref res raw-index) new)
1783 (dotimes (i raw-len)
1784 (setf (aref new i) (aref data i))))))
1785
1786 res))
1787
1788
1789 ;;; Default print and make-load-form methods.
1790
1791 (defun default-structure-print (structure stream depth)
1792 (declare (ignore depth))
1793 (if (funcallable-instance-p structure)
1794 (print-unreadable-object (structure stream :identity t :type t)
1795 (write-string "Funcallable Structure" stream))
1796 (let* ((type (%instance-layout structure))
1797 (name (%class-name (layout-class type)))
1798 (dd (layout-info type)))
1799 (if *print-pretty*
1800 (pprint-logical-block (stream nil :prefix "#S(" :suffix ")")
1801 (prin1 name stream)
1802 (let ((slots (dd-slots dd)))
1803 (when slots
1804 (write-char #\space stream)
1805 (pprint-indent :block 2 stream)
1806 (pprint-newline :linear stream)
1807 (loop
1808 (pprint-pop)
1809 (let ((slot (pop slots)))
1810 (write-char #\: stream)
1811 (output-symbol-name (dsd-%name slot) stream)
1812 (write-char #\space stream)
1813 (pprint-newline :miser stream)
1814 (output-object (funcall (fdefinition (dsd-accessor slot))
1815 structure)
1816 stream)
1817 (when (null slots)
1818 (return))
1819 (write-char #\space stream)
1820 (pprint-newline :linear stream))))))
1821 (descend-into (stream)
1822 (write-string "#S(" stream)
1823 (prin1 name stream)
1824 (do ((index 0 (1+ index))
1825 (slots (dd-slots dd) (cdr slots)))
1826 ((or (null slots)
1827 (and (not *print-readably*) (eql *print-length* index)))
1828 (if (null slots)
1829 (write-string ")" stream)
1830 (write-string " ...)" stream)))
1831 (declare (type index index))
1832 (write-char #\space stream)
1833 (write-char #\: stream)
1834 (let ((slot (first slots)))
1835 (output-symbol-name (dsd-%name slot) stream)
1836 (write-char #\space stream)
1837 (output-object (funcall (fdefinition (dsd-accessor slot))
1838 structure)
1839 stream))))))))
1840
1841 (defun make-structure-load-form (structure)
1842 (declare (type structure-object structure))
1843 (let* ((class (layout-class (%instance-layout structure)))
1844 (fun (structure-class-make-load-form-fun class)))
1845 (etypecase fun
1846 ((member :just-dump-it-normally :ignore-it)
1847 fun)
1848 (null
1849 (error "Structures of type ~S cannot be dumped as constants."
1850 (%class-name class)))
1851 (function
1852 (funcall fun structure))
1853 (symbol
1854 (funcall (symbol-function fun) structure)))))

  ViewVC Help
Powered by ViewVC 1.1.5