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