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

Contents of /src/code/defstruct.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5