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

Contents of /src/code/defstruct.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.77 - (show annotations)
Mon Dec 9 16:52:47 2002 UTC (11 years, 4 months ago) by toy
Branch: MAIN
Changes since 1.76: +5 -3 lines
DEFSTRUCT was not recognizing the option :conc-name or (:conc-name).

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

  ViewVC Help
Powered by ViewVC 1.1.5