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
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")
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)))
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))
29 ;;; Delay looking for compiler-layout until the constructor is being
30 ;;; compiled, since it doesn't exist until after the EVAL-WHEN (COMPILE)
31 ;;; stuff is compiled.
32 (sb!xc:defmacro %delayed-get-compiler-layout (name)
33 `',(compiler-layout-or-lose name))
35 ;;; Get layout right away.
36 (sb!xc:defmacro compile-time-find-layout (name)
39 ;;; re. %DELAYED-GET-COMPILER-LAYOUT and COMPILE-TIME-FIND-LAYOUT, above..
41 ;;; FIXME: Perhaps both should be defined with DEFMACRO-MUNDANELY?
42 ;;; FIXME: Do we really need both? If so, their names and implementations
43 ;;; should probably be tweaked to be more parallel.
45 ;;;; DEFSTRUCT-DESCRIPTION
47 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information
48 ;;; about a structure type.
49 (def!struct (defstruct-description
51 (:make-load-form-fun just-dump-it-normally)
52 #-sb-xc-host (:pure t)
53 (:constructor make-defstruct-description
55 (conc-name (symbolicate name "-"))
56 (copier-name (symbolicate "COPY-" name))
57 (predicate-name (symbolicate name "-P")))))
58 ;; name of the structure
59 (name (missing-arg) :type symbol :read-only t)
60 ;; documentation on the structure
61 (doc nil :type (or string null))
62 ;; prefix for slot names. If NIL, none.
63 (conc-name nil :type (or symbol null))
64 ;; the name of the primary standard keyword constructor, or NIL if none
65 (default-constructor nil :type (or symbol null))
66 ;; all the explicit :CONSTRUCTOR specs, with name defaulted
67 (constructors () :type list)
68 ;; name of copying function
69 (copier-name nil :type (or symbol null))
70 ;; name of type predicate
71 (predicate-name nil :type (or symbol null))
72 ;; the arguments to the :INCLUDE option, or NIL if no included
74 (include nil :type list)
75 ;; properties used to define structure-like classes with an
76 ;; arbitrary superclass and that may not have STRUCTURE-CLASS as the
77 ;; metaclass. Syntax is:
78 ;; (superclass-name metaclass-name metaclass-constructor)
79 (alternate-metaclass nil :type list)
80 ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
81 ;; (including included ones)
83 ;; number of elements we've allocated (See also RAW-LENGTH.)
84 (length 0 :type index)
85 ;; General kind of implementation.
86 (type 'structure :type (member structure vector list
87 funcallable-structure))
89 ;; The next three slots are for :TYPE'd structures (which aren't
90 ;; classes, DD-CLASS-P = NIL)
92 ;; vector element type
94 ;; T if :NAMED was explicitly specified, NIL otherwise
95 (named nil :type boolean)
96 ;; any INITIAL-OFFSET option on this direct type
97 (offset nil :type (or index null))
99 ;; the argument to the PRINT-FUNCTION option, or NIL if a
100 ;; PRINT-FUNCTION option was given with no argument, or 0 if no
101 ;; PRINT-FUNCTION option was given
102 (print-function 0 :type (or cons symbol (member 0)))
103 ;; the argument to the PRINT-OBJECT option, or NIL if a PRINT-OBJECT
104 ;; option was given with no argument, or 0 if no PRINT-OBJECT option
106 (print-object 0 :type (or cons symbol (member 0)))
107 ;; the index of the raw data vector and the number of words in it,
108 ;; or NIL and 0 if not allocated (either because this structure
109 ;; has no raw slots, or because we're still parsing it and haven't
110 ;; run across any raw slots yet)
111 (raw-index nil :type (or index null))
112 (raw-length 0 :type index)
113 ;; the value of the :PURE option, or :UNSPECIFIED. This is only
114 ;; meaningful if DD-CLASS-P = T.
115 (pure :unspecified :type (member t nil :substructure :unspecified)))
116 (def!method print-object ((x defstruct-description) stream)
117 (print-unreadable-object (x stream :type t)
118 (prin1 (dd-name x) stream)))
120 ;;; Does DD describe a structure with a class?
121 (defun dd-class-p (dd)
123 '(structure funcallable-structure)))
125 ;;; a type name which can be used when declaring things which operate
126 ;;; on structure instances
127 (defun dd-declarable-type (dd)
129 ;; Native classes are known to the type system, and we can
130 ;; declare them as types.
132 ;; Structures layered on :TYPE LIST or :TYPE VECTOR aren't part
133 ;; of the type system, so all we can declare is the underlying
134 ;; LIST or VECTOR type.
137 (defun dd-layout-or-lose (dd)
138 (compiler-layout-or-lose (dd-name dd)))
140 ;;;; DEFSTRUCT-SLOT-DESCRIPTION
142 ;;; A DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about
143 ;;; a structure slot.
144 (def!struct (defstruct-slot-description
145 (:make-load-form-fun just-dump-it-normally)
148 #-sb-xc-host (:pure t))
149 ;; string name of slot
151 ;; its position in the implementation sequence
152 (index (missing-arg) :type fixnum)
153 ;; the name of the accessor function
155 ;; (CMU CL had extra complexity here ("..or NIL if this accessor has
156 ;; the same name as an inherited accessor (which we don't want to
157 ;; shadow)") but that behavior doesn't seem to be specified by (or
158 ;; even particularly consistent with) ANSI, so it's gone in SBCL.)
160 default ; default value expression
161 (type t) ; declared type specifier
162 ;; If this object does not describe a raw slot, this value is T.
164 ;; If this object describes a raw slot, this value is the type of the
165 ;; value that the raw slot holds. Mostly. (KLUDGE: If the raw slot has
166 ;; type (UNSIGNED-BYTE 32), the value here is UNSIGNED-BYTE, not
167 ;; (UNSIGNED-BYTE 32).)
168 (raw-type t :type (member t single-float double-float
169 #!+long-float long-float
170 complex-single-float complex-double-float
171 #!+long-float complex-long-float
173 (read-only nil :type (member t nil)))
174 (def!method print-object ((x defstruct-slot-description) stream)
175 (print-unreadable-object (x stream :type t)
176 (prin1 (dsd-name x) stream)))
178 ;;; Return the name of a defstruct slot as a symbol. We store it as a
179 ;;; string to avoid creating lots of worthless symbols at load time.
180 (defun dsd-name (dsd)
181 (intern (string (dsd-%name dsd))
182 (if (dsd-accessor-name dsd)
183 (symbol-package (dsd-accessor-name dsd))
186 ;;;; typed (non-class) structures
188 ;;; Return a type specifier we can use for testing :TYPE'd structures.
189 (defun dd-lisp-type (defstruct)
190 (ecase (dd-type defstruct)
192 (vector `(simple-array ,(dd-element-type defstruct) (*)))))
194 ;;;; shared machinery for inline and out-of-line slot accessor functions
196 (eval-when (:compile-toplevel :load-toplevel :execute)
198 ;; information about how a slot of a given DSD-RAW-TYPE is to be accessed
199 (defstruct raw-slot-data
200 ;; the raw slot type, or T for a non-raw slot
202 ;; (Raw slots are allocated in the raw slots array in a vector which
203 ;; the GC doesn't need to scavenge. Non-raw slots are in the
204 ;; ordinary place you'd expect, directly indexed off the instance
206 (raw-type (missing-arg) :type (or symbol cons) :read-only t)
207 ;; What operator is used (on the raw data vector) to access a slot
209 (accessor-name (missing-arg) :type symbol :read-only t)
210 ;; How many words are each value of this type? (This is used to
211 ;; rescale the offset into the raw data vector.)
212 (n-words (missing-arg) :type (and index (integer 1)) :read-only t))
214 (defvar *raw-slot-data-list*
216 ;; The compiler thinks that the raw data vector is a vector of
217 ;; word-sized unsigned bytes, so if the slot we want to access
218 ;; actually *is* an unsigned byte, it'll access the slot for us
219 ;; even if we don't lie to it at all, just let it use normal AREF.
220 (make-raw-slot-data :raw-type 'unsigned-byte
223 ;; In the other cases, we lie to the compiler, making it use
224 ;; some low-level AREFish access in order to pun the hapless
225 ;; bits into some other-than-unsigned-byte meaning.
227 ;; "A lie can travel halfway round the world while the truth is
228 ;; putting on its shoes." -- Mark Twain
229 (make-raw-slot-data :raw-type 'single-float
230 :accessor-name '%raw-ref-single
232 (make-raw-slot-data :raw-type 'double-float
233 :accessor-name '%raw-ref-double
235 (make-raw-slot-data :raw-type 'complex-single-float
236 :accessor-name '%raw-ref-complex-single
238 (make-raw-slot-data :raw-type 'complex-double-float
239 :accessor-name '%raw-ref-complex-double
242 (make-raw-slot-data :raw-type long-float
243 :accessor-name '%raw-ref-long
244 :n-words #!+x86 3 #!+sparc 4)
246 (make-raw-slot-data :raw-type complex-long-float
247 :accessor-name '%raw-ref-complex-long
248 :n-words #!+x86 6 #!+sparc 8))))
250 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
251 ;;;; close personal friend SB!XC:DEFSTRUCT)
253 ;;; Return a list of forms to install PRINT and MAKE-LOAD-FORM funs,
254 ;;; mentioning them in the expansion so that they can be compiled.
255 (defun class-method-definitions (defstruct)
256 (let ((name (dd-name defstruct)))
258 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
259 ;; class names which creates fast but non-cold-loadable,
260 ;; non-compact code. In this context, we'd rather have
261 ;; compact, cold-loadable code. -- WHN 19990928
262 (declare (notinline sb!xc:find-class))
263 ,@(let ((pf (dd-print-function defstruct))
264 (po (dd-print-object defstruct))
267 ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
268 ;; leaves PO or PF equal to NIL. The user-level effect is
269 ;; to generate a PRINT-OBJECT method specialized for the type,
270 ;; implementing the default #S structure-printing behavior.
271 (when (or (eq pf nil) (eq po nil))
272 (setf pf '(default-structure-print)
274 (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
275 ;; option, return the value to pass as an arg to FUNCTION.
277 (destructuring-bind (fun-name) oarg
279 (cond ((not (eql pf 0))
280 `((def!method print-object ((,x ,name) ,s)
281 (funcall #',(farg pf)
284 *current-level-in-print*))))
286 `((def!method print-object ((,x ,name) ,s)
287 (funcall #',(farg po) ,x ,s))))
289 ,@(let ((pure (dd-pure defstruct)))
291 `((setf (layout-pure (class-layout
292 (sb!xc:find-class ',name)))
294 ((eq pure :substructure)
295 `((setf (layout-pure (class-layout
296 (sb!xc:find-class ',name)))
298 ,@(let ((def-con (dd-default-constructor defstruct)))
299 (when (and def-con (not (dd-alternate-metaclass defstruct)))
300 `((setf (structure-class-constructor (sb!xc:find-class ',name))
303 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
304 (defmacro !expander-for-defstruct (name-and-options
306 expanding-into-code-for-xc-host-p)
307 `(let ((name-and-options ,name-and-options)
308 (slot-descriptions ,slot-descriptions)
309 (expanding-into-code-for-xc-host-p
310 ,expanding-into-code-for-xc-host-p))
311 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
316 (let ((inherits (inherits-for-structure dd)))
318 (eval-when (:compile-toplevel :load-toplevel :execute)
319 (%compiler-defstruct ',dd ',inherits))
320 (%defstruct ',dd ',inherits)
321 ,@(unless expanding-into-code-for-xc-host-p
322 (append ;; FIXME: We've inherited from CMU CL nonparallel
323 ;; code for creating copiers for typed and untyped
324 ;; structures. This should be fixed.
325 ;(copier-definition dd)
326 (constructor-definitions dd)
327 (class-method-definitions dd)))
330 (eval-when (:compile-toplevel :load-toplevel :execute)
331 (setf (info :typed-structure :info ',name) ',dd))
332 ,@(unless expanding-into-code-for-xc-host-p
333 (append (typed-accessor-definitions dd)
334 (typed-predicate-definitions dd)
335 (typed-copier-definitions dd)
336 (constructor-definitions dd)))
339 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
341 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
342 Define the structure type Name. Instances are created by MAKE-<name>,
343 which takes &KEY arguments allowing initial slot values to the specified.
344 A SETF'able function <name>-<slot> is defined for each slot to read and
345 write slot values. <name>-p is a type predicate.
347 Popular DEFSTRUCT options (see manual for others):
351 Specify the name for the constructor or predicate.
353 (:CONSTRUCTOR Name Lambda-List)
354 Specify the name and arguments for a BOA constructor
355 (which is more efficient when keyword syntax isn't necessary.)
357 (:INCLUDE Supertype Slot-Spec*)
358 Make this type a subtype of the structure type Supertype. The optional
359 Slot-Specs override inherited slot options.
364 Asserts that the value of this slot is always of the specified type.
367 If true, no setter function is defined for this slot."
368 (!expander-for-defstruct name-and-options slot-descriptions nil))
370 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
372 "Cause information about a target structure to be built into the
374 (!expander-for-defstruct name-and-options slot-descriptions t))
376 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
378 ;;; Return a list of forms which create a predicate function for a
380 (defun typed-predicate-definitions (defstruct)
381 (let ((name (dd-name defstruct))
382 (predicate-name (dd-predicate-name defstruct))
384 (when (and predicate-name (dd-named defstruct))
385 (let ((ltype (dd-lisp-type defstruct)))
386 `((defun ,predicate-name (,argname)
387 (and (typep ,argname ',ltype)
388 (eq (elt (the ,ltype ,argname)
389 ,(cdr (car (last (find-name-indices defstruct)))))
392 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
393 (defun typed-copier-definitions (defstruct)
394 (when (dd-copier-name defstruct)
395 `((setf (fdefinition ',(dd-copier-name defstruct)) #'copy-seq)
396 (declaim (ftype function ,(dd-copier-name defstruct))))))
398 ;;; Return a list of function definitions for accessing and setting
399 ;;; the slots of a typed DEFSTRUCT. The functions are proclaimed to be
400 ;;; inline, and the types of their arguments and results are declared
401 ;;; as well. We count on the compiler to do clever things with ELT.
402 (defun typed-accessor-definitions (defstruct)
404 (let ((ltype (dd-lisp-type defstruct)))
405 (dolist (slot (dd-slots defstruct))
406 (let ((name (dsd-accessor-name slot))
407 (index (dsd-index slot))
408 (slot-type `(and ,(dsd-type slot)
409 ,(dd-element-type defstruct))))
410 (stuff `(proclaim '(inline ,name (setf ,name))))
411 ;; FIXME: The arguments in the next two DEFUNs should be
412 ;; gensyms. (Otherwise e.g. if NEW-VALUE happened to be the
413 ;; name of a special variable, things could get weird.)
414 (stuff `(defun ,name (structure)
415 (declare (type ,ltype structure))
416 (the ,slot-type (elt structure ,index))))
417 (unless (dsd-read-only slot)
419 `(defun (setf ,name) (new-value structure)
420 (declare (type ,ltype structure) (type ,slot-type new-value))
421 (setf (elt structure ,index) new-value)))))))
426 (defun require-no-print-options-so-far (defstruct)
427 (unless (and (eql (dd-print-function defstruct) 0)
428 (eql (dd-print-object defstruct) 0))
429 (error "No more than one of the following options may be specified:
430 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
432 ;;; Parse a single DEFSTRUCT option and store the results in DD.
433 (defun parse-1-dd-option (option dd)
434 (let ((args (rest option))
438 (destructuring-bind (conc-name) args
439 (setf (dd-conc-name dd)
440 (if (symbolp conc-name)
442 (make-symbol (string conc-name))))))
444 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
447 (push (cons cname stuff) (dd-constructors dd))))
449 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
451 (setf (dd-copier-name dd) copier)))
453 (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
455 (setf (dd-predicate-name dd) predicate-name)))
457 (when (dd-include dd)
458 (error "more than one :INCLUDE option"))
459 (setf (dd-include dd) args))
461 (require-no-print-options-so-far dd)
462 (setf (dd-print-function dd)
463 (the (or symbol cons) args)))
465 (require-no-print-options-so-far dd)
466 (setf (dd-print-object dd)
467 (the (or symbol cons) args)))
469 (destructuring-bind (type) args
470 (cond ((member type '(list vector))
471 (setf (dd-element-type dd) t)
472 (setf (dd-type dd) type))
473 ((and (consp type) (eq (first type) 'vector))
474 (destructuring-bind (vector vtype) type
475 (declare (ignore vector))
476 (setf (dd-element-type dd) vtype)
477 (setf (dd-type dd) 'vector)))
479 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
481 (error "The DEFSTRUCT option :NAMED takes no arguments."))
483 (destructuring-bind (offset) args
484 (setf (dd-offset dd) offset)))
486 (destructuring-bind (fun) args
487 (setf (dd-pure dd) fun)))
488 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
490 ;;; Given name and options, return a DD holding that info.
491 (defun parse-defstruct-name-and-options (name-and-options)
492 (destructuring-bind (name &rest options) name-and-options
493 (aver name) ; A null name doesn't seem to make sense here.
494 (let ((dd (make-defstruct-description name)))
495 (dolist (option options)
496 (cond ((eq option :named)
497 (setf (dd-named dd) t))
499 (parse-1-dd-option option dd))
500 ((member option '(:conc-name :constructor :copier :predicate))
501 (parse-1-dd-option (list option) dd))
503 (error "unrecognized DEFSTRUCT option: ~S" option))))
508 (error ":OFFSET can't be specified unless :TYPE is specified."))
509 (unless (dd-include dd)
510 ;; FIXME: It'd be cleaner to treat no-:INCLUDE as defaulting
511 ;; to :INCLUDE STRUCTURE-OBJECT, and then let the general-case
512 ;; (INCF (DD-LENGTH DD) (DD-LENGTH included-DD)) logic take
513 ;; care of this. (Except that the :TYPE VECTOR and :TYPE
514 ;; LIST cases, with their :NAMED and un-:NAMED flavors,
515 ;; make that messy, alas.)
516 (incf (dd-length dd))))
518 (require-no-print-options-so-far dd)
520 (incf (dd-length dd)))
521 (let ((offset (dd-offset dd)))
522 (when offset (incf (dd-length dd) offset)))))
524 (when (dd-include dd)
525 (frob-dd-inclusion-stuff dd))
529 ;;; Given name and options and slot descriptions (and possibly doc
530 ;;; string at the head of slot descriptions) return a DD holding that
532 (defun parse-defstruct-name-and-options-and-slot-descriptions
533 (name-and-options slot-descriptions)
534 (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
535 (list name-and-options)
537 (when (stringp (car slot-descriptions))
538 (setf (dd-doc result) (pop slot-descriptions)))
539 (dolist (slot-description slot-descriptions)
540 (allocate-1-slot result (parse-1-dsd result slot-description)))
543 ;;;; stuff to parse slot descriptions
545 ;;; Parse a slot description for DEFSTRUCT, add it to the description
546 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
547 ;;; that we modify to get the new slot. This is supplied when handling
549 (defun parse-1-dsd (defstruct spec &optional
550 (slot (make-defstruct-slot-description :%name ""
553 (multiple-value-bind (name default default-p type type-p read-only ro-p)
558 &optional (default nil default-p)
559 &key (type nil type-p) (read-only nil ro-p))
563 (uncross type) type-p
566 (when (keywordp spec)
567 (style-warn "Keyword slot name indicates probable syntax ~
568 error in DEFSTRUCT: ~S."
572 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
573 (error 'simple-program-error
574 :format-control "duplicate slot name ~S"
575 :format-arguments (list name)))
576 (setf (dsd-%name slot) (string name))
577 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
579 (let ((accessor-name (symbolicate (or (dd-conc-name defstruct) "") name))
580 (predicate-name (dd-predicate-name defstruct)))
581 (setf (dsd-accessor-name slot) accessor-name)
582 (when (eql accessor-name predicate-name)
583 ;; Some adventurous soul has named a slot so that its accessor
584 ;; collides with the structure type predicate. ANSI doesn't
585 ;; specify what to do in this case. As of 2001-09-04, Martin
586 ;; Atzmueller reports that CLISP and Lispworks both give
587 ;; priority to the slot accessor, so that the predicate is
588 ;; overwritten. We might as well do the same (as well as
589 ;; signalling a warning).
591 "~@<The structure accessor name ~S is the same as the name of the ~
592 structure type predicate. ANSI doesn't specify what to do in ~
593 this case. We'll overwrite the type predicate with the slot ~
594 accessor, but you can't rely on this behavior, so it'd be wise to ~
595 remove the ambiguity in your code.~@:>"
597 (setf (dd-predicate-name defstruct) nil)))
600 (setf (dsd-default slot) default))
602 (setf (dsd-type slot)
603 (if (eq (dsd-type slot) t)
605 `(and ,(dsd-type slot) ,type))))
608 (setf (dsd-read-only slot) t)
609 (when (dsd-read-only slot)
610 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
615 ;;; When a value of type TYPE is stored in a structure, should it be
616 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
617 ;;; RAW? is true if TYPE should be stored in a raw slot.
618 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
619 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
621 ;;; FIXME: This should use the data in *RAW-SLOT-DATA-LIST*.
622 (defun structure-raw-slot-type-and-size (type)
624 (;; FIXME: For now we suppress raw slots, since there are various
625 ;; issues about the way that the cross-compiler handles them.
626 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
627 (values nil nil nil))
628 ((and (sb!xc:subtypep type '(unsigned-byte 32))
629 (multiple-value-bind (fixnum? fixnum-certain?)
630 (sb!xc:subtypep type 'fixnum)
631 ;; (The extra test for FIXNUM-CERTAIN? here is
632 ;; intended for bootstrapping the system. In
633 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
634 ;; FIXNUM is defined, and so could bogusly end up
635 ;; putting INDEX-typed values into raw slots if we
636 ;; didn't test FIXNUM-CERTAIN?.)
637 (and (not fixnum?) fixnum-certain?)))
638 (values t 'unsigned-byte 1))
639 ((sb!xc:subtypep type 'single-float)
640 (values t 'single-float 1))
641 ((sb!xc:subtypep type 'double-float)
642 (values t 'double-float 2))
644 ((sb!xc:subtypep type 'long-float)
645 (values t 'long-float #!+x86 3 #!+sparc 4))
646 ((sb!xc:subtypep type '(complex single-float))
647 (values t 'complex-single-float 2))
648 ((sb!xc:subtypep type '(complex double-float))
649 (values t 'complex-double-float 4))
651 ((sb!xc:subtypep type '(complex long-float))
652 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
654 (values nil nil nil))))
656 ;;; Allocate storage for a DSD in DD. This is where we decide whether
657 ;;; a slot is raw or not. If raw, and we haven't allocated a raw-index
658 ;;; yet for the raw data vector, then do it. Raw objects are aligned
659 ;;; on the unit of their size.
660 (defun allocate-1-slot (dd dsd)
661 (multiple-value-bind (raw? raw-type words)
662 (if (eq (dd-type dd) 'structure)
663 (structure-raw-slot-type-and-size (dsd-type dsd))
664 (values nil nil nil))
666 (setf (dsd-index dsd) (dd-length dd))
667 (incf (dd-length dd)))
669 (unless (dd-raw-index dd)
670 (setf (dd-raw-index dd) (dd-length dd))
671 (incf (dd-length dd)))
672 (let ((off (rem (dd-raw-length dd) words)))
674 (incf (dd-raw-length dd) (- words off))))
675 (setf (dsd-raw-type dsd) raw-type)
676 (setf (dsd-index dsd) (dd-raw-length dd))
677 (incf (dd-raw-length dd) words))))
680 (defun typed-structure-info-or-lose (name)
681 (or (info :typed-structure :info name)
682 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
684 ;;; Process any included slots pretty much like they were specified.
685 ;;; Also inherit various other attributes.
686 (defun frob-dd-inclusion-stuff (dd)
687 (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
688 (let* ((type (dd-type dd))
691 (layout-info (compiler-layout-or-lose included-name))
692 (typed-structure-info-or-lose included-name))))
694 ;; checks on legality
695 (unless (and (eq type (dd-type included-structure))
696 (type= (specifier-type (dd-element-type included-structure))
697 (specifier-type (dd-element-type dd))))
698 (error ":TYPE option mismatch between structures ~S and ~S"
699 (dd-name dd) included-name))
700 (let ((included-class (sb!xc:find-class included-name nil)))
702 ;; It's not particularly well-defined to :INCLUDE any of the
703 ;; CMU CL INSTANCE weirdosities like CONDITION or
704 ;; GENERIC-FUNCTION, and it's certainly not ANSI-compliant.
705 (let* ((included-layout (class-layout included-class))
706 (included-dd (layout-info included-layout)))
707 (when (and (dd-alternate-metaclass included-dd)
708 ;; As of sbcl-0.pre7.73, anyway, STRUCTURE-OBJECT
709 ;; is represented with an ALTERNATE-METACLASS. But
710 ;; it's specifically OK to :INCLUDE (and PCL does)
711 ;; so in this one case, it's OK to include
712 ;; something with :ALTERNATE-METACLASS after all.
713 (not (eql included-name 'structure-object)))
714 (error "can't :INCLUDE class ~S (has alternate metaclass)"
717 (incf (dd-length dd) (dd-length included-structure))
718 (when (dd-class-p dd)
719 (let ((mc (rest (dd-alternate-metaclass included-structure))))
720 (when (and mc (not (dd-alternate-metaclass dd)))
721 (setf (dd-alternate-metaclass dd)
722 (cons included-name mc))))
723 (when (eq (dd-pure dd) :unspecified)
724 (setf (dd-pure dd) (dd-pure included-structure)))
725 (setf (dd-raw-index dd) (dd-raw-index included-structure))
726 (setf (dd-raw-length dd) (dd-raw-length included-structure)))
728 (dolist (included-slot (dd-slots included-structure))
729 (let* ((included-name (dsd-name included-slot))
730 (modified (or (find included-name modified-slots
731 :key (lambda (x) (if (atom x) x (car x)))
736 (copy-structure included-slot)))))))
738 ;;;; various helper functions for setting up DEFSTRUCTs
740 ;;; This function is called at macroexpand time to compute the INHERITS
741 ;;; vector for a structure type definition.
742 (defun inherits-for-structure (info)
743 (declare (type defstruct-description info))
744 (let* ((include (dd-include info))
745 (superclass-opt (dd-alternate-metaclass info))
748 (compiler-layout-or-lose (first include))
749 (class-layout (sb!xc:find-class
750 (or (first superclass-opt)
751 'structure-object))))))
752 (if (eq (dd-name info) 'ansi-stream)
753 ;; a hack to add the CL:STREAM class as a mixin for ANSI-STREAMs
754 (concatenate 'simple-vector
755 (layout-inherits super)
757 (class-layout (sb!xc:find-class 'stream))))
758 (concatenate 'simple-vector
759 (layout-inherits super)
762 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
763 ;;; described by DD. Create the class and LAYOUT, checking for
764 ;;; incompatible redefinition. Define those functions which are
765 ;;; sufficiently stereotyped that we can implement them as standard
767 (defun %defstruct (dd inherits)
768 (declare (type defstruct-description dd))
770 ;; We set up LAYOUTs even in the cross-compilation host.
771 (multiple-value-bind (class layout old-layout)
772 (ensure-structure-class dd inherits "current" "new")
773 (cond ((not old-layout)
774 (unless (eq (class-layout class) layout)
775 (register-layout layout)))
777 (let ((old-dd (layout-info old-layout)))
778 (when (defstruct-description-p old-dd)
779 (dolist (slot (dd-slots old-dd))
780 (fmakunbound (dsd-accessor-name slot))
781 (unless (dsd-read-only slot)
782 (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
783 (%redefine-defstruct class old-layout layout)
784 (setq layout (class-layout class))))
785 (setf (sb!xc:find-class (dd-name dd)) class)
787 ;; Various other operations only make sense on the target SBCL.
789 (%target-defstruct dd layout))
793 ;;; Return a form describing the writable place used for this slot
794 ;;; in the instance named INSTANCE-NAME.
795 (defun %accessor-place-form (dd dsd instance-name)
796 (let (;; the operator that we'll use to access a typed slot or, in
797 ;; the case of a raw slot, to read the vector of raw slots
798 (ref (ecase (dd-type dd)
799 (structure '%instance-ref)
800 (list 'nth-but-with-sane-arg-order)
802 (raw-type (dsd-raw-type dsd)))
803 (if (eq raw-type t) ; if not raw slot
804 `(,ref ,instance-name ,(dsd-index dsd))
805 (let* ((raw-slot-data (find raw-type *raw-slot-data-list*
806 :key #'raw-slot-data-raw-type
808 (raw-slot-accessor (raw-slot-data-accessor-name raw-slot-data))
809 (raw-n-words (raw-slot-data-n-words raw-slot-data)))
810 (multiple-value-bind (scaled-dsd-index misalignment)
811 (floor (dsd-index dsd) raw-n-words)
812 (aver (zerop misalignment))
813 `(,raw-slot-accessor (,ref ,instance-name ,(dd-raw-index dd))
814 ,scaled-dsd-index))))))
816 ;;; Return inline expansion designators (i.e. values suitable for
817 ;;; (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR ..)) for the reader
818 ;;; and writer functions of the slot described by DSD.
819 (defun slot-accessor-inline-expansion-designators (dd dsd)
820 (let ((instance-type-decl `(declare (type ,(dd-name dd) instance)))
821 (accessor-place-form (%accessor-place-form dd dsd 'instance))
822 (dsd-type (dsd-type dsd)))
826 (truly-the ,dsd-type ,accessor-place-form)))
828 `(lambda (new-value instance)
829 (declare (type ,dsd-type new-value))
831 (setf ,accessor-place-form new-value))))))
833 ;;; Return a LAMBDA form which can be used to set a slot.
834 (defun slot-setter-lambda-form (dd dsd)
835 (funcall (nth-value 1
836 (slot-accessor-inline-expansion-designators dd dsd))))
838 ;;; core compile-time setup of any class with a LAYOUT, used even by
839 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS weirdosities
840 (defun %compiler-set-up-layout (dd
842 ;; Several special cases (STRUCTURE-OBJECT
843 ;; itself, and structures with alternate
844 ;; metaclasses) call this function directly,
845 ;; and they're all at the base of the
846 ;; instance class structure, so this is
848 (inherits (vector (find-layout t)
849 (find-layout 'instance))))
851 (multiple-value-bind (class layout old-layout)
852 (multiple-value-bind (clayout clayout-p)
853 (info :type :compiler-layout (dd-name dd))
854 (ensure-structure-class dd
856 (if clayout-p "previously compiled" "current")
858 :compiler-layout clayout))
860 (undefine-structure (layout-class old-layout))
861 (when (and (class-subclasses class)
862 (not (eq layout old-layout)))
864 (dohash (class layout (class-subclasses class))
865 (declare (ignore layout))
866 (undefine-structure class)
867 (subs (class-proper-name class)))
869 (warn "removing old subclasses of ~S:~% ~S"
870 (sb!xc:class-name class)
873 (unless (eq (class-layout class) layout)
874 (register-layout layout :invalidate nil))
875 (setf (sb!xc:find-class (dd-name dd)) class)))
877 ;; At this point the class should be set up in the INFO database.
878 ;; But the logic that enforces this is a little tangled and
879 ;; scattered, so it's not obvious, so let's check.
880 (aver (sb!xc:find-class (dd-name dd) nil))
882 (setf (info :type :compiler-layout (dd-name dd)) layout))
886 ;;; Do (COMPILE LOAD EVAL)-time actions for the normal (not
887 ;;; ALTERNATE-LAYOUT) DEFSTRUCT described by DD.
888 (defun %compiler-defstruct (dd inherits)
889 (declare (type defstruct-description dd))
891 (%compiler-set-up-layout dd inherits)
893 (let* ((dtype (dd-declarable-type dd)))
895 (let ((copier-name (dd-copier-name dd)))
897 (sb!xc:proclaim `(ftype (function (,dtype) ,dtype) ,copier-name))))
899 (let ((predicate-name (dd-predicate-name dd)))
901 (sb!xc:proclaim `(ftype (function (t) t) ,predicate-name))
902 ;; Provide inline expansion (or not).
904 ((structure funcallable-structure)
905 ;; Let the predicate be inlined.
906 (setf (info :function :inline-expansion-designator predicate-name)
909 ;; This dead simple definition works because the
910 ;; type system knows how to generate inline type
911 ;; tests for instances.
912 (typep x ',(dd-name dd))))
913 (info :function :inlinep predicate-name)
916 ;; Just punt. We could provide inline expansions for :TYPE
917 ;; LIST and :TYPE VECTOR predicates too, but it'd be a
918 ;; little messier and we don't bother. (Does anyway use
919 ;; typed DEFSTRUCTs at all, let alone for high
923 (dolist (dsd (dd-slots dd))
924 (let* ((accessor-name (dsd-accessor-name dsd))
925 (dsd-type (dsd-type dsd)))
927 (multiple-value-bind (reader-designator writer-designator)
928 (slot-accessor-inline-expansion-designators dd dsd)
929 (sb!xc:proclaim `(ftype (function (,dtype) ,dsd-type)
931 (setf (info :function :inline-expansion-designator accessor-name)
933 (info :function :inlinep accessor-name)
935 (unless (dsd-read-only dsd)
936 (let ((setf-accessor-name `(setf ,accessor-name)))
938 `(ftype (function (,dsd-type ,dtype) ,dsd-type)
939 ,setf-accessor-name))
940 (setf (info :function
941 :inline-expansion-designator
944 (info :function :inlinep setf-accessor-name)
949 ;;;; redefinition stuff
951 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
952 ;;; 1. Slots which have moved,
953 ;;; 2. Slots whose type has changed,
954 ;;; 3. Deleted slots.
955 (defun compare-slots (old new)
956 (let* ((oslots (dd-slots old))
957 (nslots (dd-slots new))
958 (onames (mapcar #'dsd-name oslots))
959 (nnames (mapcar #'dsd-name nslots)))
962 (dolist (name (intersection onames nnames))
963 (let ((os (find name oslots :key #'dsd-name))
964 (ns (find name nslots :key #'dsd-name)))
965 (unless (subtypep (dsd-type ns) (dsd-type os))
967 (unless (and (= (dsd-index os) (dsd-index ns))
968 (eq (dsd-raw-type os) (dsd-raw-type ns)))
972 (set-difference onames nnames)))))
974 ;;; If we are redefining a structure with different slots than in the
975 ;;; currently loaded version, give a warning and return true.
976 (defun redefine-structure-warning (class old new)
977 (declare (type defstruct-description old new)
978 (type sb!xc:class class)
980 (let ((name (dd-name new)))
981 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
982 (when (or moved retyped deleted)
984 "incompatibly redefining slots of structure class ~S~@
985 Make sure any uses of affected accessors are recompiled:~@
986 ~@[ These slots were moved to new positions:~% ~S~%~]~
987 ~@[ These slots have new incompatible types:~% ~S~%~]~
988 ~@[ These slots were deleted:~% ~S~%~]"
989 name moved retyped deleted)
992 ;;; This function is called when we are incompatibly redefining a
993 ;;; structure CLASS to have the specified NEW-LAYOUT. We signal an
994 ;;; error with some proceed options and return the layout that should
996 (defun %redefine-defstruct (class old-layout new-layout)
997 (declare (type sb!xc:class class) (type layout old-layout new-layout))
998 (let ((name (class-proper-name class)))
1000 (error "redefining class ~S incompatibly with the current definition"
1003 :report "Invalidate current definition."
1004 (warn "Previously loaded ~S accessors will no longer work." name)
1005 (register-layout new-layout))
1007 :report "Smash current layout, preserving old code."
1008 (warn "Any old ~S instances will be in a bad way.~@
1009 I hope you know what you're doing..."
1011 (register-layout new-layout :invalidate nil
1012 :destruct-layout old-layout))))
1015 ;;; This is called when we are about to define a structure class. It
1016 ;;; returns a (possibly new) class object and the layout which should
1017 ;;; be used for the new definition (may be the current layout, and
1018 ;;; also might be an uninstalled forward referenced layout.) The third
1019 ;;; value is true if this is an incompatible redefinition, in which
1020 ;;; case it is the old layout.
1021 (defun ensure-structure-class (info inherits old-context new-context
1022 &key compiler-layout)
1023 (multiple-value-bind (class old-layout)
1027 (class 'sb!xc:structure-class)
1028 (constructor 'make-structure-class))
1029 (dd-alternate-metaclass info)
1030 (declare (ignore name))
1031 (insured-find-class (dd-name info)
1032 (if (eq class 'sb!xc:structure-class)
1034 (typep x 'sb!xc:structure-class))
1036 (sb!xc:typep x (sb!xc:find-class class))))
1037 (fdefinition constructor)))
1038 (setf (class-direct-superclasses class)
1039 (if (eq (dd-name info) 'ansi-stream)
1040 ;; a hack to add CL:STREAM as a superclass mixin to ANSI-STREAMs
1041 (list (layout-class (svref inherits (1- (length inherits))))
1042 (layout-class (svref inherits (- (length inherits) 2))))
1043 (list (layout-class (svref inherits (1- (length inherits)))))))
1044 (let ((new-layout (make-layout :class class
1046 :depthoid (length inherits)
1047 :length (dd-length info)
1049 (old-layout (or compiler-layout old-layout)))
1052 (values class new-layout nil))
1053 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1054 ;; of classic CMU CL. I moved it out to here because it was only
1055 ;; exercised in this code path anyway. -- WHN 19990510
1056 (not (eq (layout-class new-layout) (layout-class old-layout)))
1057 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1058 ((not *type-system-initialized*)
1059 (setf (layout-info old-layout) info)
1060 (values class old-layout nil))
1061 ((redefine-layout-warning old-context
1064 (layout-length new-layout)
1065 (layout-inherits new-layout)
1066 (layout-depthoid new-layout))
1067 (values class new-layout old-layout))
1069 (let ((old-info (layout-info old-layout)))
1071 ((or defstruct-description)
1072 (cond ((redefine-structure-warning class old-info info)
1073 (values class new-layout old-layout))
1075 (setf (layout-info old-layout) info)
1076 (values class old-layout nil))))
1078 (setf (layout-info old-layout) info)
1079 (values class old-layout nil))
1081 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1083 (values class new-layout old-layout)))))))))
1085 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1086 ;;; over this type, clearing the compiler structure type info, and
1087 ;;; undefining all the associated functions.
1088 (defun undefine-structure (class)
1089 (let ((info (layout-info (class-layout class))))
1090 (when (defstruct-description-p info)
1091 (let ((type (dd-name info)))
1092 (remhash type *typecheckfuns*)
1093 (setf (info :type :compiler-layout type) nil)
1094 (undefine-fun-name (dd-copier-name info))
1095 (undefine-fun-name (dd-predicate-name info))
1096 (dolist (slot (dd-slots info))
1097 (let ((fun (dsd-accessor-name slot)))
1098 (undefine-fun-name fun)
1099 (unless (dsd-read-only slot)
1100 (undefine-fun-name `(setf ,fun))))))
1101 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1102 ;; references are unknown types.
1103 (values-specifier-type-cache-clear)))
1106 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1107 ;;; constructors to find all the names that we have to splice in &
1108 ;;; where. Note that these types don't have a layout, so we can't look
1109 ;;; at LAYOUT-INHERITS.
1110 (defun find-name-indices (defstruct)
1113 (do ((info defstruct
1114 (typed-structure-info-or-lose (first (dd-include info)))))
1115 ((not (dd-include info))
1120 (dolist (info infos)
1121 (incf i (or (dd-offset info) 0))
1122 (when (dd-named info)
1123 (res (cons (dd-name info) i)))
1124 (setq i (dd-length info)))))
1128 ;;; These functions are called to actually make a constructor after we
1129 ;;; have processed the arglist. The correct variant (according to the
1130 ;;; DD-TYPE) should be called. The function is defined with the
1131 ;;; specified name and arglist. VARS and TYPES are used for argument
1132 ;;; type declarations. VALUES are the values for the slots (in order.)
1134 ;;; This is split three ways because:
1135 ;;; * LIST & VECTOR structures need "name" symbols stuck in at
1136 ;;; various weird places, whereas STRUCTURE structures have
1138 ;;; * We really want to use LIST to make list structures, instead of
1139 ;;; MAKE-LIST/(SETF ELT). (We can't in general use VECTOR in an
1140 ;;; analogous way, since VECTOR makes a SIMPLE-VECTOR and vector-typed
1141 ;;; structures can have arbitrary subtypes of VECTOR, not necessarily
1143 ;;; * STRUCTURE structures can have raw slots that must also be
1144 ;;; allocated and indirectly referenced.
1145 (defun create-vector-constructor (dd cons-name arglist vars types values)
1146 (let ((temp (gensym))
1147 (etype (dd-element-type dd)))
1148 `(defun ,cons-name ,arglist
1149 (declare ,@(mapcar (lambda (var type) `(type (and ,type ,etype) ,var))
1151 (let ((,temp (make-array ,(dd-length dd)
1152 :element-type ',(dd-element-type dd))))
1153 ,@(mapcar (lambda (x)
1154 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1155 (find-name-indices dd))
1156 ,@(mapcar (lambda (dsd value)
1157 `(setf (aref ,temp ,(dsd-index dsd)) ,value))
1158 (dd-slots dd) values)
1160 (defun create-list-constructor (dd cons-name arglist vars types values)
1161 (let ((vals (make-list (dd-length dd) :initial-element nil)))
1162 (dolist (x (find-name-indices dd))
1163 (setf (elt vals (cdr x)) `',(car x)))
1164 (loop for dsd in (dd-slots dd) and val in values do
1165 (setf (elt vals (dsd-index dsd)) val))
1167 `(defun ,cons-name ,arglist
1168 (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1170 (defun create-structure-constructor (dd cons-name arglist vars types values)
1171 (let* ((instance (gensym "INSTANCE"))
1172 (raw-index (dd-raw-index dd)))
1173 `(defun ,cons-name ,arglist
1174 (declare ,@(mapcar (lambda (var type) `(type ,type ,var))
1176 (let ((,instance (truly-the ,(dd-name dd)
1177 (%make-instance-with-layout
1178 (%delayed-get-compiler-layout ,(dd-name dd))))))
1179 (declare (optimize (safety 0))) ; Suppress redundant slot type checks.
1181 `((setf (%instance-ref ,instance ,raw-index)
1182 (make-array ,(dd-raw-length dd)
1183 :element-type '(unsigned-byte 32)))))
1184 ,@(mapcar (lambda (dsd value)
1185 ;; (Note that we can't in general use the
1186 ;; ordinary named slot setter function here
1187 ;; because the slot might be :READ-ONLY, so we
1188 ;; whip up new LAMBDA representations of slot
1189 ;; setters for the occasion.)
1190 `(,(slot-setter-lambda-form dd dsd) ,value ,instance))
1195 ;;; Create a default (non-BOA) keyword constructor.
1196 (defun create-keyword-constructor (defstruct creator)
1197 (collect ((arglist (list '&key))
1200 (dolist (slot (dd-slots defstruct))
1201 (let ((dum (gensym))
1202 (name (dsd-name slot)))
1203 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1204 (types (dsd-type slot))
1207 defstruct (dd-default-constructor defstruct)
1208 (arglist) (vals) (types) (vals))))
1210 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1211 ;;; the appropriate args to make a constructor.
1212 (defun create-boa-constructor (defstruct boa creator)
1213 (multiple-value-bind (req opt restp rest keyp keys allowp auxp aux)
1214 (parse-lambda-list (second boa))
1218 (labels ((get-slot (name)
1219 (let ((res (find name (dd-slots defstruct)
1223 (values (dsd-type res) (dsd-default res))
1226 (multiple-value-bind (type default) (get-slot arg)
1227 (arglist `(,arg ,default))
1233 (types (get-slot arg)))
1236 (arglist '&optional)
1240 ;; FIXME: this shares some logic (though not
1241 ;; code) with the &key case below (and it
1242 ;; looks confusing) -- factor out the logic
1243 ;; if possible. - CSR, 2002-04-19
1246 (def (nth-value 1 (get-slot name)))
1247 (supplied-test nil supplied-test-p))
1249 (arglist `(,name ,def ,@(if supplied-test-p `(,supplied-test) nil)))
1251 (types (get-slot name))))
1253 (do-default arg)))))
1256 (arglist '&rest rest)
1264 (destructuring-bind (wot
1267 (supplied-test nil supplied-test-p))
1269 (let ((name (if (consp wot)
1270 (destructuring-bind (key var) wot
1271 (declare (ignore key))
1274 (multiple-value-bind (type slot-def)
1276 (arglist `(,wot ,(if def-p def slot-def)
1277 ,@(if supplied-test-p `(,supplied-test) nil)))
1282 (when allowp (arglist '&allow-other-keys))
1287 (let* ((arg (if (consp arg) arg (list arg)))
1291 (types (get-slot var))))))
1293 (funcall creator defstruct (first boa)
1294 (arglist) (vars) (types)
1295 (mapcar (lambda (slot)
1296 (or (find (dsd-name slot) (vars) :test #'string=)
1297 (dsd-default slot)))
1298 (dd-slots defstruct))))))
1300 ;;; Grovel the constructor options, and decide what constructors (if
1302 (defun constructor-definitions (defstruct)
1303 (let ((no-constructors nil)
1306 (creator (ecase (dd-type defstruct)
1307 (structure #'create-structure-constructor)
1308 (vector #'create-vector-constructor)
1309 (list #'create-list-constructor))))
1310 (dolist (constructor (dd-constructors defstruct))
1311 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1312 (declare (ignore boa-ll))
1313 (cond ((not name) (setq no-constructors t))
1314 (boa-p (push constructor boas))
1315 (t (push name defaults)))))
1317 (when no-constructors
1318 (when (or defaults boas)
1319 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1320 (return-from constructor-definitions ()))
1322 (unless (or defaults boas)
1323 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1327 (let ((cname (first defaults)))
1328 (setf (dd-default-constructor defstruct) cname)
1329 (res (create-keyword-constructor defstruct creator))
1330 (dolist (other-name (rest defaults))
1331 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1332 (res `(declaim (ftype function ',other-name))))))
1335 (res (create-boa-constructor defstruct boa creator)))
1339 ;;;; instances with ALTERNATE-METACLASS
1341 ;;;; The CMU CL support for structures with ALTERNATE-METACLASS was a
1342 ;;;; fairly general extension embedded in the main DEFSTRUCT code, and
1343 ;;;; the result was an fairly impressive mess as ALTERNATE-METACLASS
1344 ;;;; extension mixed with ANSI CL generality (e.g. :TYPE and :INCLUDE)
1345 ;;;; and CMU CL implementation hairiness (esp. raw slots). This SBCL
1346 ;;;; version is much less ambitious, noticing that ALTERNATE-METACLASS
1347 ;;;; is only used to implement CONDITION, STANDARD-INSTANCE, and
1348 ;;;; GENERIC-FUNCTION, and defining a simple specialized
1349 ;;;; separate-from-DEFSTRUCT macro to provide only enough
1350 ;;;; functionality to support those.
1352 ;;;; KLUDGE: The defining macro here is so specialized that it's ugly
1353 ;;;; in its own way. It also violates once-and-only-once by knowing
1354 ;;;; much about structures and layouts that is already known by the
1355 ;;;; main DEFSTRUCT macro. Hopefully it will go away presently
1356 ;;;; (perhaps when CL:CLASS and SB-PCL:CLASS meet) as per FIXME below.
1357 ;;;; -- WHN 2001-10-28
1359 ;;;; FIXME: There seems to be no good reason to shoehorn CONDITION,
1360 ;;;; STANDARD-INSTANCE, and GENERIC-FUNCTION into mutated structures
1361 ;;;; instead of just implementing them as primitive objects. (This
1362 ;;;; reduced-functionality macro seems pretty close to the
1363 ;;;; functionality of DEFINE-PRIMITIVE-OBJECT..)
1365 (defun make-dd-with-alternate-metaclass (&key (class-name (missing-arg))
1366 (superclass-name (missing-arg))
1367 (metaclass-name (missing-arg))
1368 (dd-type (missing-arg))
1369 metaclass-constructor
1371 (let* ((dd (make-defstruct-description class-name))
1372 (conc-name (concatenate 'string (symbol-name class-name) "-"))
1373 (dd-slots (let ((reversed-result nil)
1374 ;; The index starts at 1 for ordinary
1375 ;; named slots because slot 0 is
1376 ;; magical, used for LAYOUT in
1377 ;; CONDITIONs or for something (?) in
1378 ;; funcallable instances.
1380 (dolist (slot-name slot-names)
1381 (push (make-defstruct-slot-description
1382 :%name (symbol-name slot-name)
1384 :accessor-name (symbolicate conc-name slot-name))
1387 (nreverse reversed-result))))
1388 (setf (dd-alternate-metaclass dd) (list superclass-name
1390 metaclass-constructor)
1391 (dd-slots dd) dd-slots
1392 (dd-length dd) (1+ (length slot-names))
1393 (dd-type dd) dd-type)
1396 (sb!xc:defmacro !defstruct-with-alternate-metaclass
1398 (slot-names (missing-arg))
1399 (boa-constructor (missing-arg))
1400 (superclass-name (missing-arg))
1401 (metaclass-name (missing-arg))
1402 (metaclass-constructor (missing-arg))
1403 (dd-type (missing-arg))
1405 (runtime-type-checks-p t))
1407 (declare (type (and list (not null)) slot-names))
1408 (declare (type (and symbol (not null))
1412 metaclass-constructor))
1413 (declare (type symbol predicate))
1414 (declare (type (member structure funcallable-structure) dd-type))
1416 (let* ((dd (make-dd-with-alternate-metaclass
1417 :class-name class-name
1418 :slot-names slot-names
1419 :superclass-name superclass-name
1420 :metaclass-name metaclass-name
1421 :metaclass-constructor metaclass-constructor
1423 (dd-slots (dd-slots dd))
1424 (dd-length (1+ (length slot-names)))
1425 (object-gensym (gensym "OBJECT"))
1426 (new-value-gensym (gensym "NEW-VALUE-"))
1427 (delayed-layout-form `(%delayed-get-compiler-layout ,class-name)))
1428 (multiple-value-bind (raw-maker-form raw-reffer-operator)
1431 (values `(let ((,object-gensym (%make-instance ,dd-length)))
1432 (setf (%instance-layout ,object-gensym)
1433 ,delayed-layout-form)
1436 (funcallable-structure
1437 (values `(%make-funcallable-instance ,dd-length
1438 ,delayed-layout-form)
1439 '%funcallable-instance-info)))
1442 (eval-when (:compile-toplevel :load-toplevel :execute)
1443 (%compiler-set-up-layout ',dd))
1445 ;; slot readers and writers
1446 (declaim (inline ,@(mapcar #'dsd-accessor-name dd-slots)))
1447 ,@(mapcar (lambda (dsd)
1448 `(defun ,(dsd-accessor-name dsd) (,object-gensym)
1449 ,@(when runtime-type-checks-p
1450 `((declare (type ,class-name ,object-gensym))))
1451 (,raw-reffer-operator ,object-gensym
1454 (declaim (inline ,@(mapcar (lambda (dsd)
1455 `(setf ,(dsd-accessor-name dsd)))
1457 ,@(mapcar (lambda (dsd)
1458 `(defun (setf ,(dsd-accessor-name dsd)) (,new-value-gensym
1460 ,@(when runtime-type-checks-p
1461 `((declare (type ,class-name ,object-gensym))))
1462 (setf (,raw-reffer-operator ,object-gensym
1464 ,new-value-gensym)))
1468 (defun ,boa-constructor ,slot-names
1469 (let ((,object-gensym ,raw-maker-form))
1470 ,@(mapcar (lambda (slot-name)
1471 (let ((dsd (find (symbol-name slot-name) dd-slots
1474 `(setf (,(dsd-accessor-name dsd) ,object-gensym)
1481 ;; Just delegate to the compiler's type optimization
1482 ;; code, which knows how to generate inline type tests
1483 ;; for the whole CMU CL INSTANCE menagerie.
1484 `(defun ,predicate (,object-gensym)
1485 (typep ,object-gensym ',class-name)))))))
1487 ;;;; finalizing bootstrapping
1489 ;;; Set up DD and LAYOUT for STRUCTURE-OBJECT class itself.
1491 ;;; Ordinary structure classes effectively :INCLUDE STRUCTURE-OBJECT
1492 ;;; when they have no explicit :INCLUDEs, so (1) it needs to be set up
1493 ;;; before we can define ordinary structure classes, and (2) it's
1494 ;;; special enough (and simple enough) that we just build it by hand
1495 ;;; instead of trying to generalize the ordinary DEFSTRUCT code.
1496 (defun !set-up-structure-object-class ()
1497 (let ((dd (make-defstruct-description 'structure-object)))
1499 ;; Note: This has an ALTERNATE-METACLASS only because of blind
1500 ;; clueless imitation of the CMU CL code -- dunno if or why it's
1502 (dd-alternate-metaclass dd) '(instance)
1505 (dd-type dd) 'structure)
1506 (%compiler-set-up-layout dd)))
1507 (!set-up-structure-object-class)
1509 ;;; early structure predeclarations: Set up DD and LAYOUT for ordinary
1510 ;;; (non-ALTERNATE-METACLASS) structures which are needed early.
1512 '#.(sb-cold:read-from-file
1513 "src/code/early-defstruct-args.lisp-expr"))
1514 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1517 (inherits (inherits-for-structure dd)))
1518 (%compiler-defstruct dd inherits)))
1520 (/show0 "code/defstruct.lisp end of file")