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

Contents of /src/code/defstruct.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5