89a4edc56b902830a1d22558808bd897f2ca9bb7
[sbcl.git] / src / code / defstruct.lisp
1 ;;;; that part of DEFSTRUCT implementation which is needed not just 
2 ;;;; in the target Lisp but also in the cross-compilation host
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!KERNEL")
14
15 (/show0 "code/defstruct.lisp 15")
16 \f
17 ;;;; getting LAYOUTs
18
19 ;;; Return the compiler layout for NAME. (The class referred to by
20 ;;; NAME must be a structure-like class.)
21 (defun compiler-layout-or-lose (name)
22   (let ((res (info :type :compiler-layout name)))
23     (cond ((not res)
24            (error "Class is not yet defined or was undefined: ~S" name))
25           ((not (typep (layout-info res) 'defstruct-description))
26            (error "Class is not a structure class: ~S" name))
27           (t res))))
28
29 ;;; Delay looking for compiler-layout until the constructor is being
30 ;;; compiled, since it doesn't exist until after the EVAL-WHEN
31 ;;; (COMPILE) stuff is compiled. (Or, in the oddball case when
32 ;;; DEFSTRUCT is executing in a non-toplevel context, the
33 ;;; compiler-layout still doesn't exist at compilation time, and we
34 ;;; delay still further.)
35 (sb!xc:defmacro %delayed-get-compiler-layout (name)
36   (let ((layout (info :type :compiler-layout name)))
37     (cond (layout
38            ;; ordinary case: When the DEFSTRUCT is at top level,
39            ;; then EVAL-WHEN (COMPILE) stuff will have set up the
40            ;; layout for us to use.
41            (unless (typep (layout-info layout) 'defstruct-description)
42              (error "Class is not a structure class: ~S" name))
43            `,layout)
44           (t
45            ;; KLUDGE: In the case that DEFSTRUCT is not at top-level
46            ;; the layout doesn't exist at compile time. In that case
47            ;; we laboriously look it up at run time. This code will
48            ;; run on every constructor call and will likely be quite
49            ;; slow, so if anyone cares about performance of
50            ;; non-toplevel DEFSTRUCTs, it should be rewritten to be
51            ;; cleverer. -- WHN 2002-10-23
52            (sb!c::compiler-note
53             "implementation limitation: ~
54              Non-toplevel DEFSTRUCT constructors are slow.")
55            (with-unique-names (layout)
56              `(let ((,layout (info :type :compiler-layout ',name)))
57                 (unless (typep (layout-info ,layout) 'defstruct-description)
58                   (error "Class is not a structure class: ~S" ',name))
59                 ,layout))))))
60
61 ;;; Get layout right away.
62 (sb!xc:defmacro compile-time-find-layout (name)
63   (find-layout name))
64
65 ;;; re. %DELAYED-GET-COMPILER-LAYOUT and COMPILE-TIME-FIND-LAYOUT, above..
66 ;;;
67 ;;; FIXME: Perhaps both should be defined with DEFMACRO-MUNDANELY?
68 ;;; FIXME: Do we really need both? If so, their names and implementations
69 ;;; should probably be tweaked to be more parallel.
70 \f
71 ;;;; DEFSTRUCT-DESCRIPTION
72
73 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information
74 ;;; about a structure type.
75 (def!struct (defstruct-description
76              (:conc-name dd-)
77              (:make-load-form-fun just-dump-it-normally)
78              #-sb-xc-host (:pure t)
79              (:constructor make-defstruct-description
80                            (name &aux
81                                  (conc-name (symbolicate name "-"))
82                                  (copier-name (symbolicate "COPY-" name))
83                                  (predicate-name (symbolicate name "-P")))))
84   ;; name of the structure
85   (name (missing-arg) :type symbol :read-only t)
86   ;; documentation on the structure
87   (doc nil :type (or string null))
88   ;; prefix for slot names. If NIL, none.
89   (conc-name nil :type (or symbol null))
90   ;; the name of the primary standard keyword constructor, or NIL if none
91   (default-constructor nil :type (or symbol null))
92   ;; all the explicit :CONSTRUCTOR specs, with name defaulted
93   (constructors () :type list)
94   ;; name of copying function
95   (copier-name nil :type (or symbol null))
96   ;; name of type predicate
97   (predicate-name nil :type (or symbol null))
98   ;; the arguments to the :INCLUDE option, or NIL if no included
99   ;; structure
100   (include nil :type list)
101   ;; properties used to define structure-like classes with an
102   ;; arbitrary superclass and that may not have STRUCTURE-CLASS as the
103   ;; metaclass. Syntax is:
104   ;;    (superclass-name metaclass-name metaclass-constructor)
105   (alternate-metaclass nil :type list)
106   ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
107   ;; (including included ones)
108   (slots () :type list)
109   ;; a list of (NAME . INDEX) pairs for accessors of included structures
110   (inherited-accessor-alist () :type list)
111   ;; number of elements we've allocated (See also RAW-LENGTH.)
112   (length 0 :type index)
113   ;; General kind of implementation.
114   (type 'structure :type (member structure vector list
115                                  funcallable-structure))
116
117   ;; The next three slots are for :TYPE'd structures (which aren't
118   ;; classes, DD-CLASS-P = NIL)
119   ;;
120   ;; vector element type
121   (element-type t)
122   ;; T if :NAMED was explicitly specified, NIL otherwise
123   (named nil :type boolean)
124   ;; any INITIAL-OFFSET option on this direct type
125   (offset nil :type (or index null))
126
127   ;; the argument to the PRINT-FUNCTION option, or NIL if a
128   ;; PRINT-FUNCTION option was given with no argument, or 0 if no
129   ;; PRINT-FUNCTION option was given
130   (print-function 0 :type (or cons symbol (member 0)))
131   ;; the argument to the PRINT-OBJECT option, or NIL if a PRINT-OBJECT
132   ;; option was given with no argument, or 0 if no PRINT-OBJECT option
133   ;; was given
134   (print-object 0 :type (or cons symbol (member 0)))
135   ;; the index of the raw data vector and the number of words in it,
136   ;; or NIL and 0 if not allocated (either because this structure
137   ;; has no raw slots, or because we're still parsing it and haven't
138   ;; run across any raw slots yet)
139   (raw-index nil :type (or index null))
140   (raw-length 0 :type index)
141   ;; the value of the :PURE option, or :UNSPECIFIED. This is only
142   ;; meaningful if DD-CLASS-P = T.
143   (pure :unspecified :type (member t nil :substructure :unspecified)))
144 (def!method print-object ((x defstruct-description) stream)
145   (print-unreadable-object (x stream :type t)
146     (prin1 (dd-name x) stream)))
147
148 ;;; Does DD describe a structure with a class?
149 (defun dd-class-p (dd)
150   (member (dd-type dd)
151           '(structure funcallable-structure)))
152
153 ;;; a type name which can be used when declaring things which operate
154 ;;; on structure instances
155 (defun dd-declarable-type (dd)
156   (if (dd-class-p dd)
157       ;; Native classes are known to the type system, and we can
158       ;; declare them as types.
159       (dd-name dd)
160       ;; Structures layered on :TYPE LIST or :TYPE VECTOR aren't part
161       ;; of the type system, so all we can declare is the underlying
162       ;; LIST or VECTOR type.
163       (dd-type dd)))
164
165 (defun dd-layout-or-lose (dd)
166   (compiler-layout-or-lose (dd-name dd)))
167 \f
168 ;;;; DEFSTRUCT-SLOT-DESCRIPTION
169
170 ;;; A DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about
171 ;;; a structure slot.
172 (def!struct (defstruct-slot-description
173              (:make-load-form-fun just-dump-it-normally)
174              (:conc-name dsd-)
175              (:copier nil)
176              #-sb-xc-host (:pure t))
177   ;; string name of slot
178   %name
179   ;; its position in the implementation sequence
180   (index (missing-arg) :type fixnum)
181   ;; the name of the accessor function
182   ;;
183   ;; (CMU CL had extra complexity here ("..or NIL if this accessor has
184   ;; the same name as an inherited accessor (which we don't want to
185   ;; shadow)") but that behavior doesn't seem to be specified by (or
186   ;; even particularly consistent with) ANSI, so it's gone in SBCL.)
187   (accessor-name nil)
188   default                       ; default value expression
189   (type t)                      ; declared type specifier
190   (safe-p t :type boolean)      ; whether the slot is known to be
191                                 ; always of the specified type
192   ;; If this object does not describe a raw slot, this value is T.
193   ;;
194   ;; If this object describes a raw slot, this value is the type of the
195   ;; value that the raw slot holds. Mostly. (KLUDGE: If the raw slot has
196   ;; type (UNSIGNED-BYTE 32), the value here is UNSIGNED-BYTE, not
197   ;; (UNSIGNED-BYTE 32).)
198   (raw-type t :type (member t single-float double-float
199                             #!+long-float long-float
200                             complex-single-float complex-double-float
201                             #!+long-float complex-long-float
202                             unsigned-byte))
203   (read-only nil :type (member t nil)))
204 (def!method print-object ((x defstruct-slot-description) stream)
205   (print-unreadable-object (x stream :type t)
206     (prin1 (dsd-name x) stream)))
207
208 ;;; Return the name of a defstruct slot as a symbol. We store it as a
209 ;;; string to avoid creating lots of worthless symbols at load time.
210 ;;;
211 ;;; FIXME: This has horrible package issues.  In many ways, it would
212 ;;; be very nice to treat the names of structure slots as strings, but
213 ;;; unfortunately PCL requires slot names to be interned symbols.
214 ;;; Maybe we want to resurrect something like the old
215 ;;; SB-SLOT-ACCESSOR-NAME package?
216 (defun dsd-name (dsd)
217   (intern (dsd-%name dsd)))
218 \f
219 ;;;; typed (non-class) structures
220
221 ;;; Return a type specifier we can use for testing :TYPE'd structures.
222 (defun dd-lisp-type (defstruct)
223   (ecase (dd-type defstruct)
224     (list 'list)
225     (vector `(simple-array ,(dd-element-type defstruct) (*)))))
226 \f
227 ;;;; shared machinery for inline and out-of-line slot accessor functions
228
229 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
230
231   ;; information about how a slot of a given DSD-RAW-TYPE is to be accessed
232   (defstruct raw-slot-data
233     ;; the raw slot type, or T for a non-raw slot
234     ;;
235     ;; (Raw slots are allocated in the raw slots array in a vector which
236     ;; the GC doesn't need to scavenge. Non-raw slots are in the
237     ;; ordinary place you'd expect, directly indexed off the instance
238     ;; pointer.)
239     (raw-type (missing-arg) :type (or symbol cons) :read-only t)
240     ;; What operator is used (on the raw data vector) to access a slot
241     ;; of this type?
242     (accessor-name (missing-arg) :type symbol :read-only t)
243     ;; How many words are each value of this type? (This is used to
244     ;; rescale the offset into the raw data vector.)
245     (n-words (missing-arg) :type (and index (integer 1)) :read-only t))
246
247   (defvar *raw-slot-data-list*
248     (list
249      ;; The compiler thinks that the raw data vector is a vector of
250      ;; word-sized unsigned bytes, so if the slot we want to access
251      ;; actually *is* an unsigned byte, it'll access the slot for us
252      ;; even if we don't lie to it at all, just let it use normal AREF.
253      (make-raw-slot-data :raw-type 'unsigned-byte
254                          :accessor-name 'aref
255                          :n-words 1)
256      ;; In the other cases, we lie to the compiler, making it use
257      ;; some low-level AREFish access in order to pun the hapless
258      ;; bits into some other-than-unsigned-byte meaning.
259      ;;
260      ;; "A lie can travel halfway round the world while the truth is
261      ;; putting on its shoes." -- Mark Twain
262      (make-raw-slot-data :raw-type 'single-float
263                          :accessor-name '%raw-ref-single
264                          :n-words 1)
265      (make-raw-slot-data :raw-type 'double-float
266                          :accessor-name '%raw-ref-double
267                          :n-words 2)
268      (make-raw-slot-data :raw-type 'complex-single-float
269                          :accessor-name '%raw-ref-complex-single
270                          :n-words 2)
271      (make-raw-slot-data :raw-type 'complex-double-float
272                          :accessor-name '%raw-ref-complex-double
273                          :n-words 4)
274      #!+long-float
275      (make-raw-slot-data :raw-type long-float
276                          :accessor-name '%raw-ref-long
277                          :n-words #!+x86 3 #!+sparc 4)
278      #!+long-float
279      (make-raw-slot-data :raw-type complex-long-float
280                          :accessor-name '%raw-ref-complex-long
281                          :n-words #!+x86 6 #!+sparc 8))))
282 \f
283 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
284 ;;;; close personal friend SB!XC:DEFSTRUCT)
285
286 ;;; Return a list of forms to install PRINT and MAKE-LOAD-FORM funs,
287 ;;; mentioning them in the expansion so that they can be compiled.
288 (defun class-method-definitions (defstruct)
289   (let ((name (dd-name defstruct)))
290     `((locally
291         ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
292         ;; class names which creates fast but non-cold-loadable,
293         ;; non-compact code. In this context, we'd rather have
294         ;; compact, cold-loadable code. -- WHN 19990928
295         (declare (notinline find-classoid))
296         ,@(let ((pf (dd-print-function defstruct))
297                 (po (dd-print-object defstruct))
298                 (x (gensym))
299                 (s (gensym)))
300             ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
301             ;; leaves PO or PF equal to NIL. The user-level effect is
302             ;; to generate a PRINT-OBJECT method specialized for the type,
303             ;; implementing the default #S structure-printing behavior.
304             (when (or (eq pf nil) (eq po nil))
305               (setf pf '(default-structure-print)
306                     po 0))
307             (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
308                    ;; option, return the value to pass as an arg to FUNCTION.
309                    (farg (oarg)
310                      (destructuring-bind (fun-name) oarg
311                        fun-name)))
312               (cond ((not (eql pf 0))
313                      `((def!method print-object ((,x ,name) ,s)
314                          (funcall #',(farg pf)
315                                   ,x
316                                   ,s
317                                   *current-level-in-print*))))
318                     ((not (eql po 0))
319                      `((def!method print-object ((,x ,name) ,s)
320                          (funcall #',(farg po) ,x ,s))))
321                     (t nil))))
322         ,@(let ((pure (dd-pure defstruct)))
323             (cond ((eq pure t)
324                    `((setf (layout-pure (classoid-layout
325                                          (find-classoid ',name)))
326                            t)))
327                   ((eq pure :substructure)
328                    `((setf (layout-pure (classoid-layout
329                                          (find-classoid ',name)))
330                            0)))))
331         ,@(let ((def-con (dd-default-constructor defstruct)))
332             (when (and def-con (not (dd-alternate-metaclass defstruct)))
333               `((setf (structure-classoid-constructor (find-classoid ',name))
334                       #',def-con))))))))
335
336 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
337 (defmacro !expander-for-defstruct (name-and-options
338                                    slot-descriptions
339                                    expanding-into-code-for-xc-host-p)
340   `(let ((name-and-options ,name-and-options)
341          (slot-descriptions ,slot-descriptions)
342          (expanding-into-code-for-xc-host-p
343           ,expanding-into-code-for-xc-host-p))
344      (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
345                  name-and-options
346                  slot-descriptions))
347             (name (dd-name dd)))
348        (if (dd-class-p dd)
349            (let ((inherits (inherits-for-structure dd)))
350              `(progn
351                 ;; Note we intentionally call %DEFSTRUCT first, and
352                 ;; especially before %COMPILER-DEFSTRUCT. %DEFSTRUCT
353                 ;; has the tests (and resulting CERROR) for collisions
354                 ;; with LAYOUTs which already exist in the runtime. If
355                 ;; there are any collisions, we want the user's
356                 ;; response to CERROR to control what happens.
357                 ;; Especially, if the user responds to the collision
358                 ;; with ABORT, we don't want %COMPILER-DEFSTRUCT to
359                 ;; modify the definition of the class.
360                 (%defstruct ',dd ',inherits)
361                 (eval-when (:compile-toplevel :load-toplevel :execute)
362                   (%compiler-defstruct ',dd ',inherits))
363                 ,@(unless expanding-into-code-for-xc-host-p
364                     (append ;; FIXME: We've inherited from CMU CL nonparallel
365                             ;; code for creating copiers for typed and untyped
366                             ;; structures. This should be fixed.
367                             ;(copier-definition dd)
368                             (constructor-definitions dd)
369                             (class-method-definitions dd)))
370                 ',name))
371            `(progn
372               (eval-when (:compile-toplevel :load-toplevel :execute)
373                 (setf (info :typed-structure :info ',name) ',dd))
374               ,@(unless expanding-into-code-for-xc-host-p
375                   (append (typed-accessor-definitions dd)
376                           (typed-predicate-definitions dd)
377                           (typed-copier-definitions dd)
378                           (constructor-definitions dd)))
379               ',name)))))
380
381 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
382   #!+sb-doc
383   "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
384    Define the structure type Name. Instances are created by MAKE-<name>, 
385    which takes &KEY arguments allowing initial slot values to the specified.
386    A SETF'able function <name>-<slot> is defined for each slot to read and
387    write slot values. <name>-p is a type predicate.
388
389    Popular DEFSTRUCT options (see manual for others):
390
391    (:CONSTRUCTOR Name)
392    (:PREDICATE Name)
393        Specify the name for the constructor or predicate.
394
395    (:CONSTRUCTOR Name Lambda-List)
396        Specify the name and arguments for a BOA constructor
397        (which is more efficient when keyword syntax isn't necessary.)
398
399    (:INCLUDE Supertype Slot-Spec*)
400        Make this type a subtype of the structure type Supertype. The optional
401        Slot-Specs override inherited slot options.
402
403    Slot options:
404
405    :TYPE Type-Spec
406        Asserts that the value of this slot is always of the specified type.
407
408    :READ-ONLY {T | NIL}
409        If true, no setter function is defined for this slot."
410     (!expander-for-defstruct name-and-options slot-descriptions nil))
411 #+sb-xc-host
412 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
413   #!+sb-doc
414   "Cause information about a target structure to be built into the
415   cross-compiler."
416   (!expander-for-defstruct name-and-options slot-descriptions t))
417 \f
418 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
419
420 ;;; First, a helper to determine whether a name names an inherited
421 ;;; accessor.
422 (defun accessor-inherited-data (name defstruct)
423   (assoc name (dd-inherited-accessor-alist defstruct) :test #'eq))
424
425 ;;; Return a list of forms which create a predicate function for a
426 ;;; typed DEFSTRUCT.
427 (defun typed-predicate-definitions (defstruct)
428   (let ((name (dd-name defstruct))
429         (predicate-name (dd-predicate-name defstruct))
430         (argname (gensym)))
431     (when (and predicate-name (dd-named defstruct))
432       (let ((ltype (dd-lisp-type defstruct))
433             (name-index (cdr (car (last (find-name-indices defstruct))))))
434         `((defun ,predicate-name (,argname)
435             (and (typep ,argname ',ltype)
436                  ,(cond
437                    ((subtypep ltype 'list)
438                     `(consp (nthcdr ,name-index (the ,ltype ,argname))))
439                    ((subtypep ltype 'vector)
440                     `(= (length (the ,ltype ,argname))
441                         ,(dd-length defstruct)))
442                    (t (bug "Uncatered-for lisp type in typed DEFSTRUCT: ~S."
443                            ltype)))
444                  (eq (elt (the ,ltype ,argname)
445                           ,name-index)
446                      ',name))))))))
447
448 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
449 (defun typed-copier-definitions (defstruct)
450   (when (dd-copier-name defstruct)
451     `((setf (fdefinition ',(dd-copier-name defstruct)) #'copy-seq)
452       (declaim (ftype function ,(dd-copier-name defstruct))))))
453
454 ;;; Return a list of function definitions for accessing and setting
455 ;;; the slots of a typed DEFSTRUCT. The functions are proclaimed to be
456 ;;; inline, and the types of their arguments and results are declared
457 ;;; as well. We count on the compiler to do clever things with ELT.
458 (defun typed-accessor-definitions (defstruct)
459   (collect ((stuff))
460     (let ((ltype (dd-lisp-type defstruct)))
461       (dolist (slot (dd-slots defstruct))
462         (let ((name (dsd-accessor-name slot))
463               (index (dsd-index slot))
464               (slot-type `(and ,(dsd-type slot)
465                                ,(dd-element-type defstruct))))
466           (let ((inherited (accessor-inherited-data name defstruct)))
467             (cond
468               ((not inherited)
469                (stuff `(proclaim '(inline ,name (setf ,name))))
470                ;; FIXME: The arguments in the next two DEFUNs should
471                ;; be gensyms. (Otherwise e.g. if NEW-VALUE happened to
472                ;; be the name of a special variable, things could get
473                ;; weird.)
474                (stuff `(defun ,name (structure)
475                         (declare (type ,ltype structure))
476                         (the ,slot-type (elt structure ,index))))
477                (unless (dsd-read-only slot)
478                  (stuff
479                   `(defun (setf ,name) (new-value structure)
480                     (declare (type ,ltype structure) (type ,slot-type new-value))
481                     (setf (elt structure ,index) new-value)))))
482               ((not (= (cdr inherited) index))
483                (style-warn "~@<Non-overwritten accessor ~S does not access ~
484                             slot with name ~S (accessing an inherited slot ~
485                             instead).~:@>" name (dsd-%name slot))))))))
486     (stuff)))
487 \f
488 ;;;; parsing
489
490 (defun require-no-print-options-so-far (defstruct)
491   (unless (and (eql (dd-print-function defstruct) 0)
492                (eql (dd-print-object defstruct) 0))
493     (error "No more than one of the following options may be specified:
494   :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
495
496 ;;; Parse a single DEFSTRUCT option and store the results in DD.
497 (defun parse-1-dd-option (option dd)
498   (let ((args (rest option))
499         (name (dd-name dd)))
500     (case (first option)
501       (:conc-name
502        (destructuring-bind (&optional conc-name) args
503          (setf (dd-conc-name dd)
504                (if (symbolp conc-name)
505                    conc-name
506                    (make-symbol (string conc-name))))))
507       (:constructor
508        (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
509                                       &rest stuff)
510            args
511          (push (cons cname stuff) (dd-constructors dd))))
512       (:copier
513        (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
514            args
515          (setf (dd-copier-name dd) copier)))
516       (:predicate
517        (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
518            args
519          (setf (dd-predicate-name dd) predicate-name)))
520       (:include
521        (when (dd-include dd)
522          (error "more than one :INCLUDE option"))
523        (setf (dd-include dd) args))
524       (:print-function
525        (require-no-print-options-so-far dd)
526        (setf (dd-print-function dd)
527              (the (or symbol cons) args)))
528       (:print-object
529        (require-no-print-options-so-far dd)
530        (setf (dd-print-object dd)
531              (the (or symbol cons) args)))
532       (:type
533        (destructuring-bind (type) args
534          (cond ((member type '(list vector))
535                 (setf (dd-element-type dd) t)
536                 (setf (dd-type dd) type))
537                ((and (consp type) (eq (first type) 'vector))
538                 (destructuring-bind (vector vtype) type
539                   (declare (ignore vector))
540                   (setf (dd-element-type dd) vtype)
541                   (setf (dd-type dd) 'vector)))
542                (t
543                 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
544       (:named
545        (error "The DEFSTRUCT option :NAMED takes no arguments."))
546       (:initial-offset
547        (destructuring-bind (offset) args
548          (setf (dd-offset dd) offset)))
549       (:pure
550        (destructuring-bind (fun) args
551          (setf (dd-pure dd) fun)))
552       (t (error "unknown DEFSTRUCT option:~%  ~S" option)))))
553
554 ;;; Given name and options, return a DD holding that info.
555 (defun parse-defstruct-name-and-options (name-and-options)
556   (destructuring-bind (name &rest options) name-and-options
557     (aver name) ; A null name doesn't seem to make sense here.
558     (let ((dd (make-defstruct-description name)))
559       (dolist (option options)
560         (cond ((eq option :named)
561                (setf (dd-named dd) t))
562               ((consp option)
563                (parse-1-dd-option option dd))
564               ((member option '(:conc-name :constructor :copier :predicate))
565                (parse-1-dd-option (list option) dd))
566               (t
567                (error "unrecognized DEFSTRUCT option: ~S" option))))
568
569       (case (dd-type dd)
570         (structure
571          (when (dd-offset dd)
572            (error ":OFFSET can't be specified unless :TYPE is specified."))
573          (unless (dd-include dd)
574            ;; FIXME: It'd be cleaner to treat no-:INCLUDE as defaulting
575            ;; to :INCLUDE STRUCTURE-OBJECT, and then let the general-case
576            ;; (INCF (DD-LENGTH DD) (DD-LENGTH included-DD)) logic take
577            ;; care of this. (Except that the :TYPE VECTOR and :TYPE
578            ;; LIST cases, with their :NAMED and un-:NAMED flavors,
579            ;; make that messy, alas.)
580            (incf (dd-length dd))))
581         (t
582          (require-no-print-options-so-far dd)
583          (when (dd-named dd)
584            (incf (dd-length dd)))
585          (let ((offset (dd-offset dd)))
586            (when offset (incf (dd-length dd) offset)))))
587
588       (when (dd-include dd)
589         (frob-dd-inclusion-stuff dd))
590
591       dd)))
592
593 ;;; Given name and options and slot descriptions (and possibly doc
594 ;;; string at the head of slot descriptions) return a DD holding that
595 ;;; info.
596 (defun parse-defstruct-name-and-options-and-slot-descriptions
597     (name-and-options slot-descriptions)
598   (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
599                                                       (list name-and-options)
600                                                       name-and-options))))
601     (when (stringp (car slot-descriptions))
602       (setf (dd-doc result) (pop slot-descriptions)))
603     (dolist (slot-description slot-descriptions)
604       (allocate-1-slot result (parse-1-dsd result slot-description)))
605     result))
606 \f
607 ;;;; stuff to parse slot descriptions
608
609 ;;; Parse a slot description for DEFSTRUCT, add it to the description
610 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
611 ;;; that we modify to get the new slot. This is supplied when handling
612 ;;; included slots.
613 (defun parse-1-dsd (defstruct spec &optional
614                     (slot (make-defstruct-slot-description :%name ""
615                                                            :index 0
616                                                            :type t)))
617   (multiple-value-bind (name default default-p type type-p read-only ro-p)
618       (cond
619        ((listp spec)
620         (destructuring-bind
621             (name
622              &optional (default nil default-p)
623              &key (type nil type-p) (read-only nil ro-p))
624             spec
625           (values name
626                   default default-p
627                   (uncross type) type-p
628                   read-only ro-p)))
629        (t
630         (when (keywordp spec)
631           (style-warn "Keyword slot name indicates probable syntax ~
632                        error in DEFSTRUCT: ~S."
633                       spec))
634         spec))
635
636     (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
637       (error 'simple-program-error
638              :format-control "duplicate slot name ~S"
639              :format-arguments (list name)))
640     (setf (dsd-%name slot) (string name))
641     (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
642
643     (let ((accessor-name (if (dd-conc-name defstruct)
644                              (symbolicate (dd-conc-name defstruct) name)
645                              name))
646           (predicate-name (dd-predicate-name defstruct)))
647       (setf (dsd-accessor-name slot) accessor-name)
648       (when (eql accessor-name predicate-name)
649         ;; Some adventurous soul has named a slot so that its accessor
650         ;; collides with the structure type predicate. ANSI doesn't
651         ;; specify what to do in this case. As of 2001-09-04, Martin
652         ;; Atzmueller reports that CLISP and Lispworks both give
653         ;; priority to the slot accessor, so that the predicate is
654         ;; overwritten. We might as well do the same (as well as
655         ;; signalling a warning).
656         (style-warn
657          "~@<The structure accessor name ~S is the same as the name of the ~
658           structure type predicate. ANSI doesn't specify what to do in ~
659           this case. We'll overwrite the type predicate with the slot ~
660           accessor, but you can't rely on this behavior, so it'd be wise to ~
661           remove the ambiguity in your code.~@:>"
662          accessor-name)
663         (setf (dd-predicate-name defstruct) nil))
664       #-sb-xc-host
665       (when (and (fboundp accessor-name)
666                  (not (accessor-inherited-data accessor-name defstruct)))
667         (style-warn "redefining ~S in DEFSTRUCT" accessor-name)))
668
669     (when default-p
670       (setf (dsd-default slot) default))
671     (when type-p
672       (setf (dsd-type slot)
673             (if (eq (dsd-type slot) t)
674                 type
675                 `(and ,(dsd-type slot) ,type))))
676     (when ro-p
677       (if read-only
678           (setf (dsd-read-only slot) t)
679           (when (dsd-read-only slot)
680             (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
681                    name
682                    (dsd-name slot)))))
683     slot))
684
685 ;;; When a value of type TYPE is stored in a structure, should it be
686 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
687 ;;;   RAW? is true if TYPE should be stored in a raw slot.
688 ;;;   RAW-TYPE is the raw slot type, or NIL if no raw slot.
689 ;;;   WORDS is the number of words in the raw slot, or NIL if no raw slot.
690 ;;;
691 ;;; FIXME: This should use the data in *RAW-SLOT-DATA-LIST*.
692 (defun structure-raw-slot-type-and-size (type)
693   (cond #+nil
694         (;; FIXME: For now we suppress raw slots, since there are various
695          ;; issues about the way that the cross-compiler handles them.
696          (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
697          (values nil nil nil))
698         ((and (sb!xc:subtypep type '(unsigned-byte 32))
699               (multiple-value-bind (fixnum? fixnum-certain?)
700                   (sb!xc:subtypep type 'fixnum)
701                 ;; (The extra test for FIXNUM-CERTAIN? here is
702                 ;; intended for bootstrapping the system. In
703                 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
704                 ;; FIXNUM is defined, and so could bogusly end up
705                 ;; putting INDEX-typed values into raw slots if we
706                 ;; didn't test FIXNUM-CERTAIN?.)
707                 (and (not fixnum?) fixnum-certain?)))
708          (values t 'unsigned-byte 1))
709         ((sb!xc:subtypep type 'single-float)
710          (values t 'single-float 1))
711         ((sb!xc:subtypep type 'double-float)
712          (values t 'double-float 2))
713         #!+long-float
714         ((sb!xc:subtypep type 'long-float)
715          (values t 'long-float #!+x86 3 #!+sparc 4))
716         ((sb!xc:subtypep type '(complex single-float))
717          (values t 'complex-single-float 2))
718         ((sb!xc:subtypep type '(complex double-float))
719          (values t 'complex-double-float 4))
720         #!+long-float
721         ((sb!xc:subtypep type '(complex long-float))
722          (values t 'complex-long-float #!+x86 6 #!+sparc 8))
723         (t
724          (values nil nil nil))))
725
726 ;;; Allocate storage for a DSD in DD. This is where we decide whether
727 ;;; a slot is raw or not. If raw, and we haven't allocated a raw-index
728 ;;; yet for the raw data vector, then do it. Raw objects are aligned
729 ;;; on the unit of their size.
730 (defun allocate-1-slot (dd dsd)
731   (multiple-value-bind (raw? raw-type words)
732       (if (eq (dd-type dd) 'structure)
733           (structure-raw-slot-type-and-size (dsd-type dsd))
734           (values nil nil nil))
735     (cond ((not raw?)
736            (setf (dsd-index dsd) (dd-length dd))
737            (incf (dd-length dd)))
738           (t
739            (unless (dd-raw-index dd)
740              (setf (dd-raw-index dd) (dd-length dd))
741              (incf (dd-length dd)))
742            (let ((off (rem (dd-raw-length dd) words)))
743              (unless (zerop off)
744                (incf (dd-raw-length dd) (- words off))))
745            (setf (dsd-raw-type dsd) raw-type)
746            (setf (dsd-index dsd) (dd-raw-length dd))
747            (incf (dd-raw-length dd) words))))
748   (values))
749
750 (defun typed-structure-info-or-lose (name)
751   (or (info :typed-structure :info name)
752       (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
753
754 ;;; Process any included slots pretty much like they were specified.
755 ;;; Also inherit various other attributes.
756 (defun frob-dd-inclusion-stuff (dd)
757   (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
758     (let* ((type (dd-type dd))
759            (included-structure
760             (if (dd-class-p dd)
761                 (layout-info (compiler-layout-or-lose included-name))
762                 (typed-structure-info-or-lose included-name))))
763
764       ;; checks on legality
765       (unless (and (eq type (dd-type included-structure))
766                    (type= (specifier-type (dd-element-type included-structure))
767                           (specifier-type (dd-element-type dd))))
768         (error ":TYPE option mismatch between structures ~S and ~S"
769                (dd-name dd) included-name))
770       (let ((included-classoid (find-classoid included-name nil)))
771         (when included-classoid
772           ;; It's not particularly well-defined to :INCLUDE any of the
773           ;; CMU CL INSTANCE weirdosities like CONDITION or
774           ;; GENERIC-FUNCTION, and it's certainly not ANSI-compliant.
775           (let* ((included-layout (classoid-layout included-classoid))
776                  (included-dd (layout-info included-layout)))
777             (when (and (dd-alternate-metaclass included-dd)
778                        ;; As of sbcl-0.pre7.73, anyway, STRUCTURE-OBJECT
779                        ;; is represented with an ALTERNATE-METACLASS. But
780                        ;; it's specifically OK to :INCLUDE (and PCL does)
781                        ;; so in this one case, it's OK to include
782                        ;; something with :ALTERNATE-METACLASS after all.
783                        (not (eql included-name 'structure-object)))
784               (error "can't :INCLUDE class ~S (has alternate metaclass)"
785                      included-name)))))
786
787       (incf (dd-length dd) (dd-length included-structure))
788       (when (dd-class-p dd)
789         (let ((mc (rest (dd-alternate-metaclass included-structure))))
790           (when (and mc (not (dd-alternate-metaclass dd)))
791             (setf (dd-alternate-metaclass dd)
792                   (cons included-name mc))))
793         (when (eq (dd-pure dd) :unspecified)
794           (setf (dd-pure dd) (dd-pure included-structure)))
795         (setf (dd-raw-index dd) (dd-raw-index included-structure))
796         (setf (dd-raw-length dd) (dd-raw-length included-structure)))
797
798       (setf (dd-inherited-accessor-alist dd)
799             (dd-inherited-accessor-alist included-structure))
800       (dolist (included-slot (dd-slots included-structure))
801         (let* ((included-name (dsd-name included-slot))
802                (modified (or (find included-name modified-slots
803                                    :key (lambda (x) (if (atom x) x (car x)))
804                                    :test #'string=)
805                              `(,included-name))))
806           ;; We stash away an alist of accessors to parents' slots
807           ;; that have already been created to avoid conflicts later
808           ;; so that structures with :INCLUDE and :CONC-NAME (and
809           ;; other edge cases) can work as specified.
810           (when (dsd-accessor-name included-slot)
811             ;; the "oldest" (i.e. highest up the tree of inheritance)
812             ;; will prevail, so don't push new ones on if they
813             ;; conflict.
814             (pushnew (cons (dsd-accessor-name included-slot)
815                            (dsd-index included-slot))
816                      (dd-inherited-accessor-alist dd)
817                      :test #'eq :key #'car))
818           (parse-1-dsd dd
819                        modified
820                        (copy-structure included-slot)))))))
821 \f
822 ;;;; various helper functions for setting up DEFSTRUCTs
823
824 ;;; This function is called at macroexpand time to compute the INHERITS
825 ;;; vector for a structure type definition.
826 (defun inherits-for-structure (info)
827   (declare (type defstruct-description info))
828   (let* ((include (dd-include info))
829          (superclass-opt (dd-alternate-metaclass info))
830          (super
831           (if include
832               (compiler-layout-or-lose (first include))
833               (classoid-layout (find-classoid
834                                 (or (first superclass-opt)
835                                     'structure-object))))))
836     (if (eq (dd-name info) 'ansi-stream)
837         ;; a hack to add the CL:STREAM class as a mixin for ANSI-STREAMs
838         (concatenate 'simple-vector
839                      (layout-inherits super)
840                      (vector super
841                              (classoid-layout (find-classoid 'stream))))
842         (concatenate 'simple-vector
843                      (layout-inherits super)
844                      (vector super)))))
845
846 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
847 ;;; described by DD. Create the class and LAYOUT, checking for
848 ;;; incompatible redefinition. Define those functions which are
849 ;;; sufficiently stereotyped that we can implement them as standard
850 ;;; closures.
851 (defun %defstruct (dd inherits)
852   (declare (type defstruct-description dd))
853
854   ;; We set up LAYOUTs even in the cross-compilation host.
855   (multiple-value-bind (classoid layout old-layout)
856       (ensure-structure-class dd inherits "current" "new")
857     (cond ((not old-layout)
858            (unless (eq (classoid-layout classoid) layout)
859              (register-layout layout)))
860           (t
861            (let ((old-dd (layout-info old-layout)))
862              (when (defstruct-description-p old-dd)
863                (dolist (slot (dd-slots old-dd))
864                  (fmakunbound (dsd-accessor-name slot))
865                  (unless (dsd-read-only slot)
866                    (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
867            (%redefine-defstruct classoid old-layout layout)
868            (setq layout (classoid-layout classoid))))
869     (setf (find-classoid (dd-name dd)) classoid)
870
871     ;; Various other operations only make sense on the target SBCL.
872     #-sb-xc-host
873     (%target-defstruct dd layout))
874
875   (values))
876 \f
877 ;;; Return a form describing the writable place used for this slot
878 ;;; in the instance named INSTANCE-NAME.
879 (defun %accessor-place-form (dd dsd instance-name)
880   (let (;; the operator that we'll use to access a typed slot or, in
881         ;; the case of a raw slot, to read the vector of raw slots
882         (ref (ecase (dd-type dd)
883                (structure '%instance-ref)
884                (list 'nth-but-with-sane-arg-order)
885                (vector 'aref)))
886         (raw-type (dsd-raw-type dsd)))
887     (if (eq raw-type t) ; if not raw slot
888         `(,ref ,instance-name ,(dsd-index dsd))
889         (let* ((raw-slot-data (find raw-type *raw-slot-data-list*
890                                     :key #'raw-slot-data-raw-type
891                                     :test #'equal))
892                (raw-slot-accessor (raw-slot-data-accessor-name raw-slot-data))
893                (raw-n-words (raw-slot-data-n-words raw-slot-data)))
894           (multiple-value-bind (scaled-dsd-index misalignment)
895               (floor (dsd-index dsd) raw-n-words)
896             (aver (zerop misalignment))
897             `(,raw-slot-accessor (,ref ,instance-name ,(dd-raw-index dd))
898                                  ,scaled-dsd-index))))))
899
900 ;;; Return inline expansion designators (i.e. values suitable for
901 ;;; (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR ..)) for the reader
902 ;;; and writer functions of the slot described by DSD.
903 (defun slot-accessor-inline-expansion-designators (dd dsd)
904   (let ((instance-type-decl `(declare (type ,(dd-name dd) instance)))
905         (accessor-place-form (%accessor-place-form dd dsd 'instance))
906         (dsd-type (dsd-type dsd))
907         (value-the (if (dsd-safe-p dsd) 'truly-the 'the)))
908     (values (lambda () `(lambda (instance)
909                           ,instance-type-decl
910                           (,value-the ,dsd-type ,accessor-place-form)))
911             (lambda () `(lambda (new-value instance)
912                           (declare (type ,dsd-type new-value))
913                           ,instance-type-decl
914                           (setf ,accessor-place-form new-value))))))
915
916 ;;; Return a LAMBDA form which can be used to set a slot.
917 (defun slot-setter-lambda-form (dd dsd)
918   (funcall (nth-value 1
919                       (slot-accessor-inline-expansion-designators dd dsd))))
920
921 ;;; core compile-time setup of any class with a LAYOUT, used even by
922 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS weirdosities
923 (defun %compiler-set-up-layout (dd
924                                 &optional
925                                 ;; Several special cases (STRUCTURE-OBJECT
926                                 ;; itself, and structures with alternate
927                                 ;; metaclasses) call this function directly,
928                                 ;; and they're all at the base of the
929                                 ;; instance class structure, so this is
930                                 ;; a handy default.
931                                 (inherits (vector (find-layout t)
932                                                   (find-layout 'instance))))
933
934   (multiple-value-bind (classoid layout old-layout)
935       (multiple-value-bind (clayout clayout-p)
936           (info :type :compiler-layout (dd-name dd))
937         (ensure-structure-class dd
938                                 inherits
939                                 (if clayout-p "previously compiled" "current")
940                                 "compiled"
941                                 :compiler-layout clayout))
942     (cond (old-layout
943            (undefine-structure (layout-classoid old-layout))
944            (when (and (classoid-subclasses classoid)
945                       (not (eq layout old-layout)))
946              (collect ((subs))
947                       (dohash (classoid layout (classoid-subclasses classoid))
948                         (declare (ignore layout))
949                         (undefine-structure classoid)
950                         (subs (classoid-proper-name classoid)))
951                       (when (subs)
952                         (warn "removing old subclasses of ~S:~%  ~S"
953                               (classoid-name classoid)
954                               (subs))))))
955           (t
956            (unless (eq (classoid-layout classoid) layout)
957              (register-layout layout :invalidate nil))
958            (setf (find-classoid (dd-name dd)) classoid)))
959
960     ;; At this point the class should be set up in the INFO database.
961     ;; But the logic that enforces this is a little tangled and
962     ;; scattered, so it's not obvious, so let's check.
963     (aver (find-classoid (dd-name dd) nil))
964
965     (setf (info :type :compiler-layout (dd-name dd)) layout))
966
967   (values))
968
969 ;;; Do (COMPILE LOAD EVAL)-time actions for the normal (not
970 ;;; ALTERNATE-LAYOUT) DEFSTRUCT described by DD.
971 (defun %compiler-defstruct (dd inherits)
972   (declare (type defstruct-description dd))
973
974   (%compiler-set-up-layout dd inherits)
975
976   (let* ((dtype (dd-declarable-type dd)))
977
978     (let ((copier-name (dd-copier-name dd)))
979       (when copier-name
980         (sb!xc:proclaim `(ftype (function (,dtype) ,dtype) ,copier-name))))
981
982     (let ((predicate-name (dd-predicate-name dd)))
983       (when predicate-name
984         (sb!xc:proclaim `(ftype (function (t) t) ,predicate-name))
985         ;; Provide inline expansion (or not).
986         (ecase (dd-type dd)
987           ((structure funcallable-structure)
988            ;; Let the predicate be inlined. 
989            (setf (info :function :inline-expansion-designator predicate-name)
990                  (lambda ()
991                    `(lambda (x)
992                       ;; This dead simple definition works because the
993                       ;; type system knows how to generate inline type
994                       ;; tests for instances.
995                       (typep x ',(dd-name dd))))
996                  (info :function :inlinep predicate-name)
997                  :inline))
998           ((list vector)
999            ;; Just punt. We could provide inline expansions for :TYPE
1000            ;; LIST and :TYPE VECTOR predicates too, but it'd be a
1001            ;; little messier and we don't bother. (Does anyway use
1002            ;; typed DEFSTRUCTs at all, let alone for high
1003            ;; performance?)
1004            ))))
1005
1006     (dolist (dsd (dd-slots dd))
1007       (let* ((accessor-name (dsd-accessor-name dsd))
1008              (dsd-type (dsd-type dsd)))
1009         (when accessor-name
1010           (let ((inherited (accessor-inherited-data accessor-name dd)))
1011             (cond
1012               ((not inherited)
1013                (multiple-value-bind (reader-designator writer-designator)
1014                    (slot-accessor-inline-expansion-designators dd dsd)
1015                  (sb!xc:proclaim `(ftype (function (,dtype) ,dsd-type)
1016                                    ,accessor-name))
1017                  (setf (info :function :inline-expansion-designator
1018                              accessor-name)
1019                        reader-designator
1020                        (info :function :inlinep accessor-name)
1021                        :inline)
1022                  (unless (dsd-read-only dsd)
1023                    (let ((setf-accessor-name `(setf ,accessor-name)))
1024                      (sb!xc:proclaim
1025                       `(ftype (function (,dsd-type ,dtype) ,dsd-type)
1026                         ,setf-accessor-name))
1027                      (setf (info :function
1028                                  :inline-expansion-designator
1029                                  setf-accessor-name)
1030                            writer-designator
1031                            (info :function :inlinep setf-accessor-name)
1032                            :inline)))))
1033               ((not (= (cdr inherited) (dsd-index dsd)))
1034                (style-warn "~@<Non-overwritten accessor ~S does not access ~
1035                             slot with name ~S (accessing an inherited slot ~
1036                             instead).~:@>"
1037                            accessor-name
1038                            (dsd-%name dsd)))))))))
1039   (values))
1040 \f
1041 ;;;; redefinition stuff
1042
1043 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
1044 ;;;   1. Slots which have moved,
1045 ;;;   2. Slots whose type has changed,
1046 ;;;   3. Deleted slots.
1047 (defun compare-slots (old new)
1048   (let* ((oslots (dd-slots old))
1049          (nslots (dd-slots new))
1050          (onames (mapcar #'dsd-name oslots))
1051          (nnames (mapcar #'dsd-name nslots)))
1052     (collect ((moved)
1053               (retyped))
1054       (dolist (name (intersection onames nnames))
1055         (let ((os (find name oslots :key #'dsd-name :test #'string=))
1056               (ns (find name nslots :key #'dsd-name :test #'string=)))
1057           (unless (sb!xc:subtypep (dsd-type ns) (dsd-type os))
1058             (retyped name))
1059           (unless (and (= (dsd-index os) (dsd-index ns))
1060                        (eq (dsd-raw-type os) (dsd-raw-type ns)))
1061             (moved name))))
1062       (values (moved)
1063               (retyped)
1064               (set-difference onames nnames :test #'string=)))))
1065
1066 ;;; If we are redefining a structure with different slots than in the
1067 ;;; currently loaded version, give a warning and return true.
1068 (defun redefine-structure-warning (classoid old new)
1069   (declare (type defstruct-description old new)
1070            (type classoid classoid)
1071            (ignore classoid))
1072   (let ((name (dd-name new)))
1073     (multiple-value-bind (moved retyped deleted) (compare-slots old new)
1074       (when (or moved retyped deleted)
1075         (warn
1076          "incompatibly redefining slots of structure class ~S~@
1077           Make sure any uses of affected accessors are recompiled:~@
1078           ~@[  These slots were moved to new positions:~%    ~S~%~]~
1079           ~@[  These slots have new incompatible types:~%    ~S~%~]~
1080           ~@[  These slots were deleted:~%    ~S~%~]"
1081          name moved retyped deleted)
1082         t))))
1083
1084 ;;; This function is called when we are incompatibly redefining a
1085 ;;; structure CLASS to have the specified NEW-LAYOUT. We signal an
1086 ;;; error with some proceed options and return the layout that should
1087 ;;; be used.
1088 (defun %redefine-defstruct (classoid old-layout new-layout)
1089   (declare (type classoid classoid)
1090            (type layout old-layout new-layout))
1091   (let ((name (classoid-proper-name classoid)))
1092     (restart-case
1093         (error "~@<attempt to redefine the ~S class ~S incompatibly with the current definition~:@>"
1094                'structure-object
1095                name)
1096       (continue ()
1097        :report (lambda (s)
1098                  (format s
1099                          "~@<Use the new definition of ~S, invalidating ~
1100                           already-loaded code and instances.~@:>"
1101                          name))
1102        (register-layout new-layout))
1103       (recklessly-continue ()
1104        :report (lambda (s)
1105                  (format s
1106                          "~@<Use the new definition of ~S as if it were ~
1107                           compatible, allowing old accessors to use new ~
1108                           instances and allowing new accessors to use old ~
1109                           instances.~@:>"
1110                          name))
1111        ;; classic CMU CL warning: "Any old ~S instances will be in a bad way. 
1112        ;; I hope you know what you're doing..."
1113        (register-layout new-layout
1114                         :invalidate nil
1115                         :destruct-layout old-layout))
1116       (clobber-it ()
1117        ;; FIXME: deprecated 2002-10-16, and since it's only interactive
1118        ;; hackery instead of a supported feature, can probably be deleted
1119        ;; in early 2003
1120        :report "(deprecated synonym for RECKLESSLY-CONTINUE)"
1121        (register-layout new-layout
1122                         :invalidate nil
1123                         :destruct-layout old-layout))))
1124   (values))
1125
1126 ;;; This is called when we are about to define a structure class. It
1127 ;;; returns a (possibly new) class object and the layout which should
1128 ;;; be used for the new definition (may be the current layout, and
1129 ;;; also might be an uninstalled forward referenced layout.) The third
1130 ;;; value is true if this is an incompatible redefinition, in which
1131 ;;; case it is the old layout.
1132 (defun ensure-structure-class (info inherits old-context new-context
1133                                     &key compiler-layout)
1134   (multiple-value-bind (class old-layout)
1135       (destructuring-bind
1136           (&optional
1137            name
1138            (class 'structure-classoid)
1139            (constructor 'make-structure-classoid))
1140           (dd-alternate-metaclass info)
1141         (declare (ignore name))
1142         (insured-find-classoid (dd-name info)
1143                                (if (eq class 'structure-classoid)
1144                                    (lambda (x)
1145                                      (sb!xc:typep x 'structure-classoid))
1146                                    (lambda (x)
1147                                      (sb!xc:typep x (find-classoid class))))
1148                                (fdefinition constructor)))
1149     (setf (classoid-direct-superclasses class)
1150           (if (eq (dd-name info) 'ansi-stream)
1151               ;; a hack to add CL:STREAM as a superclass mixin to ANSI-STREAMs
1152               (list (layout-classoid (svref inherits (1- (length inherits))))
1153                     (layout-classoid (svref inherits (- (length inherits) 2))))
1154               (list (layout-classoid
1155                      (svref inherits (1- (length inherits)))))))
1156     (let ((new-layout (make-layout :classoid class
1157                                    :inherits inherits
1158                                    :depthoid (length inherits)
1159                                    :length (dd-length info)
1160                                    :info info))
1161           (old-layout (or compiler-layout old-layout)))
1162       (cond
1163        ((not old-layout)
1164         (values class new-layout nil))
1165        (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1166         ;; of classic CMU CL. I moved it out to here because it was only
1167         ;; exercised in this code path anyway. -- WHN 19990510
1168         (not (eq (layout-classoid new-layout) (layout-classoid old-layout)))
1169         (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1170        ((not *type-system-initialized*)
1171         (setf (layout-info old-layout) info)
1172         (values class old-layout nil))
1173        ((redefine-layout-warning old-context
1174                                  old-layout
1175                                  new-context
1176                                  (layout-length new-layout)
1177                                  (layout-inherits new-layout)
1178                                  (layout-depthoid new-layout))
1179         (values class new-layout old-layout))
1180        (t
1181         (let ((old-info (layout-info old-layout)))
1182           (typecase old-info
1183             ((or defstruct-description)
1184              (cond ((redefine-structure-warning class old-info info)
1185                     (values class new-layout old-layout))
1186                    (t
1187                     (setf (layout-info old-layout) info)
1188                     (values class old-layout nil))))
1189             (null
1190              (setf (layout-info old-layout) info)
1191              (values class old-layout nil))
1192             (t
1193              (error "shouldn't happen! strange thing in LAYOUT-INFO:~%  ~S"
1194                     old-layout)
1195              (values class new-layout old-layout)))))))))
1196
1197 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1198 ;;; over this type, clearing the compiler structure type info, and
1199 ;;; undefining all the associated functions.
1200 (defun undefine-structure (class)
1201   (let ((info (layout-info (classoid-layout class))))
1202     (when (defstruct-description-p info)
1203       (let ((type (dd-name info)))
1204         (remhash type *typecheckfuns*)
1205         (setf (info :type :compiler-layout type) nil)
1206         (undefine-fun-name (dd-copier-name info))
1207         (undefine-fun-name (dd-predicate-name info))
1208         (dolist (slot (dd-slots info))
1209           (let ((fun (dsd-accessor-name slot)))
1210             (unless (accessor-inherited-data fun info)
1211               (undefine-fun-name fun)
1212               (unless (dsd-read-only slot)
1213                 (undefine-fun-name `(setf ,fun)))))))
1214       ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1215       ;; references are unknown types.
1216       (values-specifier-type-cache-clear)))
1217   (values))
1218 \f
1219 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1220 ;;; constructors to find all the names that we have to splice in &
1221 ;;; where. Note that these types don't have a layout, so we can't look
1222 ;;; at LAYOUT-INHERITS.
1223 (defun find-name-indices (defstruct)
1224   (collect ((res))
1225     (let ((infos ()))
1226       (do ((info defstruct
1227                  (typed-structure-info-or-lose (first (dd-include info)))))
1228           ((not (dd-include info))
1229            (push info infos))
1230         (push info infos))
1231
1232       (let ((i 0))
1233         (dolist (info infos)
1234           (incf i (or (dd-offset info) 0))
1235           (when (dd-named info)
1236             (res (cons (dd-name info) i)))
1237           (setq i (dd-length info)))))
1238
1239     (res)))
1240 \f
1241 ;;; These functions are called to actually make a constructor after we
1242 ;;; have processed the arglist. The correct variant (according to the
1243 ;;; DD-TYPE) should be called. The function is defined with the
1244 ;;; specified name and arglist. VARS and TYPES are used for argument
1245 ;;; type declarations. VALUES are the values for the slots (in order.)
1246 ;;;
1247 ;;; This is split three ways because:
1248 ;;;   * LIST & VECTOR structures need "name" symbols stuck in at
1249 ;;;     various weird places, whereas STRUCTURE structures have
1250 ;;;     a LAYOUT slot.
1251 ;;;   * We really want to use LIST to make list structures, instead of
1252 ;;;     MAKE-LIST/(SETF ELT). (We can't in general use VECTOR in an
1253 ;;;     analogous way, since VECTOR makes a SIMPLE-VECTOR and vector-typed
1254 ;;;     structures can have arbitrary subtypes of VECTOR, not necessarily
1255 ;;;     SIMPLE-VECTOR.)
1256 ;;;   * STRUCTURE structures can have raw slots that must also be
1257 ;;;     allocated and indirectly referenced.
1258 (defun create-vector-constructor (dd cons-name arglist vars types values)
1259   (let ((temp (gensym))
1260         (etype (dd-element-type dd)))
1261     `(defun ,cons-name ,arglist
1262        (declare ,@(mapcar (lambda (var type) `(type (and ,type ,etype) ,var))
1263                           vars types))
1264        (let ((,temp (make-array ,(dd-length dd)
1265                                 :element-type ',(dd-element-type dd))))
1266          ,@(mapcar (lambda (x)
1267                      `(setf (aref ,temp ,(cdr x))  ',(car x)))
1268                    (find-name-indices dd))
1269          ,@(mapcar (lambda (dsd value)
1270                      (unless (eq value '.do-not-initialize-slot.)
1271                          `(setf (aref ,temp ,(dsd-index dsd)) ,value)))
1272                    (dd-slots dd) values)
1273          ,temp))))
1274 (defun create-list-constructor (dd cons-name arglist vars types values)
1275   (let ((vals (make-list (dd-length dd) :initial-element nil)))
1276     (dolist (x (find-name-indices dd))
1277       (setf (elt vals (cdr x)) `',(car x)))
1278     (loop for dsd in (dd-slots dd) and val in values do
1279       (setf (elt vals (dsd-index dsd))
1280             (if (eq val '.do-not-initialize-slot.) 0 val)))
1281
1282     `(defun ,cons-name ,arglist
1283        (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1284        (list ,@vals))))
1285 (defun create-structure-constructor (dd cons-name arglist vars types values)
1286   (let* ((instance (gensym "INSTANCE"))
1287          (raw-index (dd-raw-index dd)))
1288     `(defun ,cons-name ,arglist
1289        (declare ,@(mapcar (lambda (var type) `(type ,type ,var))
1290                           vars types))
1291        (let ((,instance (truly-the ,(dd-name dd)
1292                           (%make-instance-with-layout
1293                            (%delayed-get-compiler-layout ,(dd-name dd))))))
1294          ,@(when raw-index
1295              `((setf (%instance-ref ,instance ,raw-index)
1296                      (make-array ,(dd-raw-length dd)
1297                                  :element-type '(unsigned-byte 32)))))
1298          ,@(mapcar (lambda (dsd value)
1299                      ;; (Note that we can't in general use the
1300                      ;; ordinary named slot setter function here
1301                      ;; because the slot might be :READ-ONLY, so we
1302                      ;; whip up new LAMBDA representations of slot
1303                      ;; setters for the occasion.)
1304                      (unless (eq value '.do-not-initialize-slot.)
1305                        `(,(slot-setter-lambda-form dd dsd) ,value ,instance)))
1306                    (dd-slots dd)
1307                    values)
1308          ,instance))))
1309
1310 ;;; Create a default (non-BOA) keyword constructor.
1311 (defun create-keyword-constructor (defstruct creator)
1312   (declare (type function creator))
1313   (collect ((arglist (list '&key))
1314             (types)
1315             (vals))
1316     (dolist (slot (dd-slots defstruct))
1317       (let ((dum (gensym))
1318             (name (dsd-name slot)))
1319         (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1320         (types (dsd-type slot))
1321         (vals dum)))
1322     (funcall creator
1323              defstruct (dd-default-constructor defstruct)
1324              (arglist) (vals) (types) (vals))))
1325
1326 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1327 ;;; the appropriate args to make a constructor.
1328 (defun create-boa-constructor (defstruct boa creator)
1329   (declare (type function creator))
1330   (multiple-value-bind (req opt restp rest keyp keys allowp auxp aux)
1331       (parse-lambda-list (second boa))
1332     (collect ((arglist)
1333               (vars)
1334               (types)
1335               (skipped-vars))
1336       (labels ((get-slot (name)
1337                  (let ((res (find name (dd-slots defstruct)
1338                                   :test #'string=
1339                                   :key #'dsd-name)))
1340                    (if res
1341                        (values (dsd-type res) (dsd-default res))
1342                        (values t nil))))
1343                (do-default (arg)
1344                  (multiple-value-bind (type default) (get-slot arg)
1345                    (arglist `(,arg ,default))
1346                    (vars arg)
1347                    (types type))))
1348         (dolist (arg req)
1349           (arglist arg)
1350           (vars arg)
1351           (types (get-slot arg)))
1352
1353         (when opt
1354           (arglist '&optional)
1355           (dolist (arg opt)
1356             (cond ((consp arg)
1357                    (destructuring-bind
1358                          ;; FIXME: this shares some logic (though not
1359                          ;; code) with the &key case below (and it
1360                          ;; looks confusing) -- factor out the logic
1361                          ;; if possible. - CSR, 2002-04-19
1362                          (name
1363                           &optional
1364                           (def (nth-value 1 (get-slot name)))
1365                           (supplied-test nil supplied-test-p))
1366                        arg
1367                      (arglist `(,name ,def ,@(if supplied-test-p `(,supplied-test) nil)))
1368                      (vars name)
1369                      (types (get-slot name))))
1370                   (t
1371                    (do-default arg)))))
1372
1373         (when restp
1374           (arglist '&rest rest)
1375           (vars rest)
1376           (types 'list))
1377
1378         (when keyp
1379           (arglist '&key)
1380           (dolist (key keys)
1381             (if (consp key)
1382                 (destructuring-bind (wot
1383                                      &optional
1384                                      (def nil def-p)
1385                                      (supplied-test nil supplied-test-p))
1386                     key
1387                   (let ((name (if (consp wot)
1388                                   (destructuring-bind (key var) wot
1389                                     (declare (ignore key))
1390                                     var)
1391                                   wot)))
1392                     (multiple-value-bind (type slot-def)
1393                         (get-slot name)
1394                       (arglist `(,wot ,(if def-p def slot-def)
1395                                  ,@(if supplied-test-p `(,supplied-test) nil)))
1396                       (vars name)
1397                       (types type))))
1398                 (do-default key))))
1399
1400         (when allowp (arglist '&allow-other-keys))
1401
1402         (when auxp
1403           (arglist '&aux)
1404           (dolist (arg aux)
1405             (arglist arg)
1406             (if (proper-list-of-length-p arg 2)
1407               (let ((var (first arg)))
1408                 (vars var)
1409                 (types (get-slot var)))
1410               (skipped-vars (if (consp arg) (first arg) arg))))))
1411
1412       (funcall creator defstruct (first boa)
1413                (arglist) (vars) (types)
1414                (loop for slot in (dd-slots defstruct)
1415                      for name = (dsd-name slot)
1416                      collect (cond ((find name (skipped-vars) :test #'string=)
1417                                     (setf (dsd-safe-p slot) nil)
1418                                     '.do-not-initialize-slot.)
1419                                    ((or (find (dsd-name slot) (vars) :test #'string=)
1420                                         (dsd-default slot)))))))))
1421
1422 ;;; Grovel the constructor options, and decide what constructors (if
1423 ;;; any) to create.
1424 (defun constructor-definitions (defstruct)
1425   (let ((no-constructors nil)
1426         (boas ())
1427         (defaults ())
1428         (creator (ecase (dd-type defstruct)
1429                    (structure #'create-structure-constructor)
1430                    (vector #'create-vector-constructor)
1431                    (list #'create-list-constructor))))
1432     (dolist (constructor (dd-constructors defstruct))
1433       (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1434         (declare (ignore boa-ll))
1435         (cond ((not name) (setq no-constructors t))
1436               (boa-p (push constructor boas))
1437               (t (push name defaults)))))
1438
1439     (when no-constructors
1440       (when (or defaults boas)
1441         (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1442       (return-from constructor-definitions ()))
1443
1444     (unless (or defaults boas)
1445       (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1446
1447     (collect ((res))
1448       (when defaults
1449         (let ((cname (first defaults)))
1450           (setf (dd-default-constructor defstruct) cname)
1451           (res (create-keyword-constructor defstruct creator))
1452           (dolist (other-name (rest defaults))
1453             (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1454             (res `(declaim (ftype function ',other-name))))))
1455
1456       (dolist (boa boas)
1457         (res (create-boa-constructor defstruct boa creator)))
1458
1459       (res))))
1460 \f
1461 ;;;; instances with ALTERNATE-METACLASS
1462 ;;;;
1463 ;;;; The CMU CL support for structures with ALTERNATE-METACLASS was a
1464 ;;;; fairly general extension embedded in the main DEFSTRUCT code, and
1465 ;;;; the result was an fairly impressive mess as ALTERNATE-METACLASS
1466 ;;;; extension mixed with ANSI CL generality (e.g. :TYPE and :INCLUDE)
1467 ;;;; and CMU CL implementation hairiness (esp. raw slots). This SBCL
1468 ;;;; version is much less ambitious, noticing that ALTERNATE-METACLASS
1469 ;;;; is only used to implement CONDITION, STANDARD-INSTANCE, and
1470 ;;;; GENERIC-FUNCTION, and defining a simple specialized
1471 ;;;; separate-from-DEFSTRUCT macro to provide only enough
1472 ;;;; functionality to support those.
1473 ;;;;
1474 ;;;; KLUDGE: The defining macro here is so specialized that it's ugly
1475 ;;;; in its own way. It also violates once-and-only-once by knowing
1476 ;;;; much about structures and layouts that is already known by the
1477 ;;;; main DEFSTRUCT macro. Hopefully it will go away presently
1478 ;;;; (perhaps when CL:CLASS and SB-PCL:CLASS meet) as per FIXME below.
1479 ;;;; -- WHN 2001-10-28
1480 ;;;;
1481 ;;;; FIXME: There seems to be no good reason to shoehorn CONDITION,
1482 ;;;; STANDARD-INSTANCE, and GENERIC-FUNCTION into mutated structures
1483 ;;;; instead of just implementing them as primitive objects. (This
1484 ;;;; reduced-functionality macro seems pretty close to the
1485 ;;;; functionality of DEFINE-PRIMITIVE-OBJECT..)
1486
1487 (defun make-dd-with-alternate-metaclass (&key (class-name (missing-arg))
1488                                               (superclass-name (missing-arg))
1489                                               (metaclass-name (missing-arg))
1490                                               (dd-type (missing-arg))
1491                                               metaclass-constructor
1492                                               slot-names)
1493   (let* ((dd (make-defstruct-description class-name))
1494          (conc-name (concatenate 'string (symbol-name class-name) "-"))
1495          (dd-slots (let ((reversed-result nil)
1496                          ;; The index starts at 1 for ordinary
1497                          ;; named slots because slot 0 is
1498                          ;; magical, used for LAYOUT in
1499                          ;; CONDITIONs or for something (?) in
1500                          ;; funcallable instances.
1501                          (index 1))
1502                      (dolist (slot-name slot-names)
1503                        (push (make-defstruct-slot-description
1504                               :%name (symbol-name slot-name)
1505                               :index index
1506                               :accessor-name (symbolicate conc-name slot-name))
1507                              reversed-result)
1508                        (incf index))
1509                      (nreverse reversed-result))))
1510     (setf (dd-alternate-metaclass dd) (list superclass-name
1511                                             metaclass-name
1512                                             metaclass-constructor)
1513           (dd-slots dd) dd-slots
1514           (dd-length dd) (1+ (length slot-names))
1515           (dd-type dd) dd-type)
1516     dd))
1517
1518 (sb!xc:defmacro !defstruct-with-alternate-metaclass
1519     (class-name &key
1520                 (slot-names (missing-arg))
1521                 (boa-constructor (missing-arg))
1522                 (superclass-name (missing-arg))
1523                 (metaclass-name (missing-arg))
1524                 (metaclass-constructor (missing-arg))
1525                 (dd-type (missing-arg))
1526                 predicate
1527                 (runtime-type-checks-p t))
1528
1529   (declare (type (and list (not null)) slot-names))
1530   (declare (type (and symbol (not null))
1531                  boa-constructor
1532                  superclass-name
1533                  metaclass-name
1534                  metaclass-constructor))
1535   (declare (type symbol predicate))
1536   (declare (type (member structure funcallable-structure) dd-type))
1537
1538   (let* ((dd (make-dd-with-alternate-metaclass
1539               :class-name class-name
1540               :slot-names slot-names
1541               :superclass-name superclass-name
1542               :metaclass-name metaclass-name
1543               :metaclass-constructor metaclass-constructor
1544               :dd-type dd-type))
1545          (dd-slots (dd-slots dd))
1546          (dd-length (1+ (length slot-names)))
1547          (object-gensym (gensym "OBJECT"))
1548          (new-value-gensym (gensym "NEW-VALUE-"))
1549          (delayed-layout-form `(%delayed-get-compiler-layout ,class-name)))
1550     (multiple-value-bind (raw-maker-form raw-reffer-operator)
1551         (ecase dd-type
1552           (structure
1553            (values `(let ((,object-gensym (%make-instance ,dd-length)))
1554                       (setf (%instance-layout ,object-gensym)
1555                             ,delayed-layout-form)
1556                       ,object-gensym)
1557                    '%instance-ref))
1558           (funcallable-structure
1559            (values `(%make-funcallable-instance ,dd-length
1560                                                 ,delayed-layout-form)
1561                    '%funcallable-instance-info)))
1562       `(progn
1563
1564          (eval-when (:compile-toplevel :load-toplevel :execute)
1565            (%compiler-set-up-layout ',dd))
1566
1567          ;; slot readers and writers
1568          (declaim (inline ,@(mapcar #'dsd-accessor-name dd-slots)))
1569          ,@(mapcar (lambda (dsd)
1570                      `(defun ,(dsd-accessor-name dsd) (,object-gensym)
1571                         ,@(when runtime-type-checks-p
1572                             `((declare (type ,class-name ,object-gensym))))
1573                         (,raw-reffer-operator ,object-gensym
1574                                               ,(dsd-index dsd))))
1575                    dd-slots)
1576          (declaim (inline ,@(mapcar (lambda (dsd)
1577                                       `(setf ,(dsd-accessor-name dsd)))
1578                                     dd-slots)))
1579          ,@(mapcar (lambda (dsd)
1580                      `(defun (setf ,(dsd-accessor-name dsd)) (,new-value-gensym
1581                                                               ,object-gensym)
1582                         ,@(when runtime-type-checks-p
1583                             `((declare (type ,class-name ,object-gensym))))
1584                         (setf (,raw-reffer-operator ,object-gensym
1585                                                     ,(dsd-index dsd))
1586                               ,new-value-gensym)))
1587                    dd-slots)
1588
1589          ;; constructor
1590          (defun ,boa-constructor ,slot-names
1591            (let ((,object-gensym ,raw-maker-form))
1592              ,@(mapcar (lambda (slot-name)
1593                          (let ((dsd (find (symbol-name slot-name) dd-slots
1594                                           :key #'dsd-%name
1595                                           :test #'string=)))
1596                            ;; KLUDGE: bug 117 bogowarning.  Neither
1597                            ;; DECLAREing the type nor TRULY-THE cut
1598                            ;; the mustard -- it still gives warnings.
1599                            (enforce-type dsd defstruct-slot-description)
1600                            `(setf (,(dsd-accessor-name dsd) ,object-gensym)
1601                                   ,slot-name)))
1602                        slot-names)
1603              ,object-gensym))
1604
1605          ;; predicate
1606          ,@(when predicate
1607              ;; Just delegate to the compiler's type optimization
1608              ;; code, which knows how to generate inline type tests
1609              ;; for the whole CMU CL INSTANCE menagerie.
1610              `(defun ,predicate (,object-gensym)
1611                 (typep ,object-gensym ',class-name)))))))
1612 \f
1613 ;;;; finalizing bootstrapping
1614
1615 ;;; Set up DD and LAYOUT for STRUCTURE-OBJECT class itself.
1616 ;;;
1617 ;;; Ordinary structure classes effectively :INCLUDE STRUCTURE-OBJECT
1618 ;;; when they have no explicit :INCLUDEs, so (1) it needs to be set up
1619 ;;; before we can define ordinary structure classes, and (2) it's
1620 ;;; special enough (and simple enough) that we just build it by hand
1621 ;;; instead of trying to generalize the ordinary DEFSTRUCT code.
1622 (defun !set-up-structure-object-class ()
1623   (let ((dd (make-defstruct-description 'structure-object)))
1624     (setf
1625      ;; Note: This has an ALTERNATE-METACLASS only because of blind
1626      ;; clueless imitation of the CMU CL code -- dunno if or why it's
1627      ;; needed. -- WHN 
1628      (dd-alternate-metaclass dd) '(instance)
1629      (dd-slots dd) nil
1630      (dd-length dd) 1
1631      (dd-type dd) 'structure)
1632     (%compiler-set-up-layout dd)))
1633 (!set-up-structure-object-class)
1634
1635 ;;; early structure predeclarations: Set up DD and LAYOUT for ordinary
1636 ;;; (non-ALTERNATE-METACLASS) structures which are needed early.
1637 (dolist (args
1638          '#.(sb-cold:read-from-file
1639              "src/code/early-defstruct-args.lisp-expr"))
1640   (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1641               (first args)
1642               (rest args)))
1643          (inherits (inherits-for-structure dd)))
1644     (%compiler-defstruct dd inherits)))
1645
1646 (/show0 "code/defstruct.lisp end of file")