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

Contents of /src/code/defstruct.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.75 - (hide annotations)
Thu Apr 25 21:13:45 2002 UTC (11 years, 11 months ago) by pmai
Branch: MAIN
Changes since 1.74: +26 -19 lines
Fix from SBCL:
0.7.2.17:
	Merged MNA "fix for boa-constructor bug" sbcl-devel 2002-04-16
	... copied the fix to &optional arguments handling
	... also test the &optional handling

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

  ViewVC Help
Powered by ViewVC 1.1.5