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