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