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
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)))
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))
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
53 "implementation limitation: ~
54 Non-toplevel DEFSTRUCT constructors are slow.")
55 (let ((layout (gensym "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))
61 ;;; Get layout right away.
62 (sb!xc:defmacro compile-time-find-layout (name)
65 ;;; re. %DELAYED-GET-COMPILER-LAYOUT and COMPILE-TIME-FIND-LAYOUT, above..
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
77 (:make-load-form-fun just-dump-it-normally)
78 #-sb-xc-host (:pure t)
79 (:constructor make-defstruct-description
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
100 (include nil :type list)
101 ;; properties used to define structure-like classes with an
102 ;; arbitrary superclass and that may not have STRUCTURE-CLASS as the
103 ;; metaclass. Syntax is:
104 ;; (superclass-name metaclass-name metaclass-constructor)
105 (alternate-metaclass nil :type list)
106 ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
107 ;; (including included ones)
108 (slots () :type list)
109 ;; a list of (NAME . INDEX) pairs for accessors of included structures
110 (inherited-accessor-alist () :type list)
111 ;; number of elements we've allocated (See also RAW-LENGTH.)
112 (length 0 :type index)
113 ;; General kind of implementation.
114 (type 'structure :type (member structure vector list
115 funcallable-structure))
117 ;; The next three slots are for :TYPE'd structures (which aren't
118 ;; classes, DD-CLASS-P = NIL)
120 ;; vector element type
122 ;; T if :NAMED was explicitly specified, NIL otherwise
123 (named nil :type boolean)
124 ;; any INITIAL-OFFSET option on this direct type
125 (offset nil :type (or index null))
127 ;; the argument to the PRINT-FUNCTION option, or NIL if a
128 ;; PRINT-FUNCTION option was given with no argument, or 0 if no
129 ;; PRINT-FUNCTION option was given
130 (print-function 0 :type (or cons symbol (member 0)))
131 ;; the argument to the PRINT-OBJECT option, or NIL if a PRINT-OBJECT
132 ;; option was given with no argument, or 0 if no PRINT-OBJECT option
134 (print-object 0 :type (or cons symbol (member 0)))
135 ;; the index of the raw data vector and the number of words in it,
136 ;; or NIL and 0 if not allocated (either because this structure
137 ;; has no raw slots, or because we're still parsing it and haven't
138 ;; run across any raw slots yet)
139 (raw-index nil :type (or index null))
140 (raw-length 0 :type index)
141 ;; the value of the :PURE option, or :UNSPECIFIED. This is only
142 ;; meaningful if DD-CLASS-P = T.
143 (pure :unspecified :type (member t nil :substructure :unspecified)))
144 (def!method print-object ((x defstruct-description) stream)
145 (print-unreadable-object (x stream :type t)
146 (prin1 (dd-name x) stream)))
148 ;;; Does DD describe a structure with a class?
149 (defun dd-class-p (dd)
151 '(structure funcallable-structure)))
153 ;;; a type name which can be used when declaring things which operate
154 ;;; on structure instances
155 (defun dd-declarable-type (dd)
157 ;; Native classes are known to the type system, and we can
158 ;; declare them as types.
160 ;; Structures layered on :TYPE LIST or :TYPE VECTOR aren't part
161 ;; of the type system, so all we can declare is the underlying
162 ;; LIST or VECTOR type.
165 (defun dd-layout-or-lose (dd)
166 (compiler-layout-or-lose (dd-name dd)))
168 ;;;; DEFSTRUCT-SLOT-DESCRIPTION
170 ;;; A DEFSTRUCT-SLOT-DESCRIPTION holds compile-time information about
171 ;;; a structure slot.
172 (def!struct (defstruct-slot-description
173 (:make-load-form-fun just-dump-it-normally)
176 #-sb-xc-host (:pure t))
177 ;; string name of slot
179 ;; its position in the implementation sequence
180 (index (missing-arg) :type fixnum)
181 ;; the name of the accessor function
183 ;; (CMU CL had extra complexity here ("..or NIL if this accessor has
184 ;; the same name as an inherited accessor (which we don't want to
185 ;; shadow)") but that behavior doesn't seem to be specified by (or
186 ;; even particularly consistent with) ANSI, so it's gone in SBCL.)
188 default ; default value expression
189 (type t) ; declared type specifier
190 (safe-p t :type boolean) ; whether the slot is known to be
191 ; always of the specified type
192 ;; If this object does not describe a raw slot, this value is T.
194 ;; If this object describes a raw slot, this value is the type of the
195 ;; value that the raw slot holds. Mostly. (KLUDGE: If the raw slot has
196 ;; type (UNSIGNED-BYTE 32), the value here is UNSIGNED-BYTE, not
197 ;; (UNSIGNED-BYTE 32).)
198 (raw-type t :type (member t single-float double-float
199 #!+long-float long-float
200 complex-single-float complex-double-float
201 #!+long-float complex-long-float
203 (read-only nil :type (member t nil)))
204 (def!method print-object ((x defstruct-slot-description) stream)
205 (print-unreadable-object (x stream :type t)
206 (prin1 (dsd-name x) stream)))
208 ;;; Return the name of a defstruct slot as a symbol. We store it as a
209 ;;; string to avoid creating lots of worthless symbols at load time.
210 (defun dsd-name (dsd)
211 (intern (string (dsd-%name dsd))
212 (if (dsd-accessor-name dsd)
213 (symbol-package (dsd-accessor-name dsd))
216 ;;;; typed (non-class) structures
218 ;;; Return a type specifier we can use for testing :TYPE'd structures.
219 (defun dd-lisp-type (defstruct)
220 (ecase (dd-type defstruct)
222 (vector `(simple-array ,(dd-element-type defstruct) (*)))))
224 ;;;; shared machinery for inline and out-of-line slot accessor functions
226 (eval-when (:compile-toplevel :load-toplevel :execute)
228 ;; information about how a slot of a given DSD-RAW-TYPE is to be accessed
229 (defstruct raw-slot-data
230 ;; the raw slot type, or T for a non-raw slot
232 ;; (Raw slots are allocated in the raw slots array in a vector which
233 ;; the GC doesn't need to scavenge. Non-raw slots are in the
234 ;; ordinary place you'd expect, directly indexed off the instance
236 (raw-type (missing-arg) :type (or symbol cons) :read-only t)
237 ;; What operator is used (on the raw data vector) to access a slot
239 (accessor-name (missing-arg) :type symbol :read-only t)
240 ;; How many words are each value of this type? (This is used to
241 ;; rescale the offset into the raw data vector.)
242 (n-words (missing-arg) :type (and index (integer 1)) :read-only t))
244 (defvar *raw-slot-data-list*
246 ;; The compiler thinks that the raw data vector is a vector of
247 ;; word-sized unsigned bytes, so if the slot we want to access
248 ;; actually *is* an unsigned byte, it'll access the slot for us
249 ;; even if we don't lie to it at all, just let it use normal AREF.
250 (make-raw-slot-data :raw-type 'unsigned-byte
253 ;; In the other cases, we lie to the compiler, making it use
254 ;; some low-level AREFish access in order to pun the hapless
255 ;; bits into some other-than-unsigned-byte meaning.
257 ;; "A lie can travel halfway round the world while the truth is
258 ;; putting on its shoes." -- Mark Twain
259 (make-raw-slot-data :raw-type 'single-float
260 :accessor-name '%raw-ref-single
262 (make-raw-slot-data :raw-type 'double-float
263 :accessor-name '%raw-ref-double
265 (make-raw-slot-data :raw-type 'complex-single-float
266 :accessor-name '%raw-ref-complex-single
268 (make-raw-slot-data :raw-type 'complex-double-float
269 :accessor-name '%raw-ref-complex-double
272 (make-raw-slot-data :raw-type long-float
273 :accessor-name '%raw-ref-long
274 :n-words #!+x86 3 #!+sparc 4)
276 (make-raw-slot-data :raw-type complex-long-float
277 :accessor-name '%raw-ref-complex-long
278 :n-words #!+x86 6 #!+sparc 8))))
280 ;;;; the legendary DEFSTRUCT macro itself (both CL:DEFSTRUCT and its
281 ;;;; close personal friend SB!XC:DEFSTRUCT)
283 ;;; Return a list of forms to install PRINT and MAKE-LOAD-FORM funs,
284 ;;; mentioning them in the expansion so that they can be compiled.
285 (defun class-method-definitions (defstruct)
286 (let ((name (dd-name defstruct)))
288 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant
289 ;; class names which creates fast but non-cold-loadable,
290 ;; non-compact code. In this context, we'd rather have
291 ;; compact, cold-loadable code. -- WHN 19990928
292 (declare (notinline find-classoid))
293 ,@(let ((pf (dd-print-function defstruct))
294 (po (dd-print-object defstruct))
297 ;; Giving empty :PRINT-OBJECT or :PRINT-FUNCTION options
298 ;; leaves PO or PF equal to NIL. The user-level effect is
299 ;; to generate a PRINT-OBJECT method specialized for the type,
300 ;; implementing the default #S structure-printing behavior.
301 (when (or (eq pf nil) (eq po nil))
302 (setf pf '(default-structure-print)
304 (flet (;; Given an arg from a :PRINT-OBJECT or :PRINT-FUNCTION
305 ;; option, return the value to pass as an arg to FUNCTION.
307 (destructuring-bind (fun-name) oarg
309 (cond ((not (eql pf 0))
310 `((def!method print-object ((,x ,name) ,s)
311 (funcall #',(farg pf)
314 *current-level-in-print*))))
316 `((def!method print-object ((,x ,name) ,s)
317 (funcall #',(farg po) ,x ,s))))
319 ,@(let ((pure (dd-pure defstruct)))
321 `((setf (layout-pure (classoid-layout
322 (find-classoid ',name)))
324 ((eq pure :substructure)
325 `((setf (layout-pure (classoid-layout
326 (find-classoid ',name)))
328 ,@(let ((def-con (dd-default-constructor defstruct)))
329 (when (and def-con (not (dd-alternate-metaclass defstruct)))
330 `((setf (structure-classoid-constructor (find-classoid ',name))
333 ;;; shared logic for CL:DEFSTRUCT and SB!XC:DEFSTRUCT
334 (defmacro !expander-for-defstruct (name-and-options
336 expanding-into-code-for-xc-host-p)
337 `(let ((name-and-options ,name-and-options)
338 (slot-descriptions ,slot-descriptions)
339 (expanding-into-code-for-xc-host-p
340 ,expanding-into-code-for-xc-host-p))
341 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
346 (let ((inherits (inherits-for-structure dd)))
348 ;; Note we intentionally call %DEFSTRUCT first, and
349 ;; especially before %COMPILER-DEFSTRUCT. %DEFSTRUCT
350 ;; has the tests (and resulting CERROR) for collisions
351 ;; with LAYOUTs which already exist in the runtime. If
352 ;; there are any collisions, we want the user's
353 ;; response to CERROR to control what happens.
354 ;; Especially, if the user responds to the collision
355 ;; with ABORT, we don't want %COMPILER-DEFSTRUCT to
356 ;; modify the definition of the class.
357 (%defstruct ',dd ',inherits)
358 (eval-when (:compile-toplevel :load-toplevel :execute)
359 (%compiler-defstruct ',dd ',inherits))
360 ,@(unless expanding-into-code-for-xc-host-p
361 (append ;; FIXME: We've inherited from CMU CL nonparallel
362 ;; code for creating copiers for typed and untyped
363 ;; structures. This should be fixed.
364 ;(copier-definition dd)
365 (constructor-definitions dd)
366 (class-method-definitions dd)))
369 (eval-when (:compile-toplevel :load-toplevel :execute)
370 (setf (info :typed-structure :info ',name) ',dd))
371 ,@(unless expanding-into-code-for-xc-host-p
372 (append (typed-accessor-definitions dd)
373 (typed-predicate-definitions dd)
374 (typed-copier-definitions dd)
375 (constructor-definitions dd)))
378 (sb!xc:defmacro defstruct (name-and-options &rest slot-descriptions)
380 "DEFSTRUCT {Name | (Name Option*)} {Slot | (Slot [Default] {Key Value}*)}
381 Define the structure type Name. Instances are created by MAKE-<name>,
382 which takes &KEY arguments allowing initial slot values to the specified.
383 A SETF'able function <name>-<slot> is defined for each slot to read and
384 write slot values. <name>-p is a type predicate.
386 Popular DEFSTRUCT options (see manual for others):
390 Specify the name for the constructor or predicate.
392 (:CONSTRUCTOR Name Lambda-List)
393 Specify the name and arguments for a BOA constructor
394 (which is more efficient when keyword syntax isn't necessary.)
396 (:INCLUDE Supertype Slot-Spec*)
397 Make this type a subtype of the structure type Supertype. The optional
398 Slot-Specs override inherited slot options.
403 Asserts that the value of this slot is always of the specified type.
406 If true, no setter function is defined for this slot."
407 (!expander-for-defstruct name-and-options slot-descriptions nil))
409 (defmacro sb!xc:defstruct (name-and-options &rest slot-descriptions)
411 "Cause information about a target structure to be built into the
413 (!expander-for-defstruct name-and-options slot-descriptions t))
415 ;;;; functions to generate code for various parts of DEFSTRUCT definitions
417 ;;; First, a helper to determine whether a name names an inherited
419 (defun accessor-inherited-data (name defstruct)
420 (assoc name (dd-inherited-accessor-alist defstruct) :test #'eq))
422 ;;; Return a list of forms which create a predicate function for a
424 (defun typed-predicate-definitions (defstruct)
425 (let ((name (dd-name defstruct))
426 (predicate-name (dd-predicate-name defstruct))
428 (when (and predicate-name (dd-named defstruct))
429 (let ((ltype (dd-lisp-type defstruct))
430 (name-index (cdr (car (last (find-name-indices defstruct))))))
431 `((defun ,predicate-name (,argname)
432 (and (typep ,argname ',ltype)
434 ((subtypep ltype 'list)
435 `(consp (nthcdr ,name-index (the ,ltype ,argname))))
436 ((subtypep ltype 'vector)
437 `(= (length (the ,ltype ,argname))
438 ,(dd-length defstruct)))
439 (t (bug "Uncatered-for lisp type in typed DEFSTRUCT: ~S."
441 (eq (elt (the ,ltype ,argname)
445 ;;; Return a list of forms to create a copier function of a typed DEFSTRUCT.
446 (defun typed-copier-definitions (defstruct)
447 (when (dd-copier-name defstruct)
448 `((setf (fdefinition ',(dd-copier-name defstruct)) #'copy-seq)
449 (declaim (ftype function ,(dd-copier-name defstruct))))))
451 ;;; Return a list of function definitions for accessing and setting
452 ;;; the slots of a typed DEFSTRUCT. The functions are proclaimed to be
453 ;;; inline, and the types of their arguments and results are declared
454 ;;; as well. We count on the compiler to do clever things with ELT.
455 (defun typed-accessor-definitions (defstruct)
457 (let ((ltype (dd-lisp-type defstruct)))
458 (dolist (slot (dd-slots defstruct))
459 (let ((name (dsd-accessor-name slot))
460 (index (dsd-index slot))
461 (slot-type `(and ,(dsd-type slot)
462 ,(dd-element-type defstruct))))
463 (let ((inherited (accessor-inherited-data name defstruct)))
466 (stuff `(proclaim '(inline ,name (setf ,name))))
467 ;; FIXME: The arguments in the next two DEFUNs should
468 ;; be gensyms. (Otherwise e.g. if NEW-VALUE happened to
469 ;; be the name of a special variable, things could get
471 (stuff `(defun ,name (structure)
472 (declare (type ,ltype structure))
473 (the ,slot-type (elt structure ,index))))
474 (unless (dsd-read-only slot)
476 `(defun (setf ,name) (new-value structure)
477 (declare (type ,ltype structure) (type ,slot-type new-value))
478 (setf (elt structure ,index) new-value)))))
479 ((not (= (cdr inherited) index))
480 (style-warn "~@<Non-overwritten accessor ~S does not access ~
481 slot with name ~S (accessing an inherited slot ~
482 instead).~:@>" name (dsd-%name slot))))))))
487 (defun require-no-print-options-so-far (defstruct)
488 (unless (and (eql (dd-print-function defstruct) 0)
489 (eql (dd-print-object defstruct) 0))
490 (error "No more than one of the following options may be specified:
491 :PRINT-FUNCTION, :PRINT-OBJECT, :TYPE")))
493 ;;; Parse a single DEFSTRUCT option and store the results in DD.
494 (defun parse-1-dd-option (option dd)
495 (let ((args (rest option))
499 (destructuring-bind (&optional conc-name) args
500 (setf (dd-conc-name dd)
501 (if (symbolp conc-name)
503 (make-symbol (string conc-name))))))
505 (destructuring-bind (&optional (cname (symbolicate "MAKE-" name))
508 (push (cons cname stuff) (dd-constructors dd))))
510 (destructuring-bind (&optional (copier (symbolicate "COPY-" name)))
512 (setf (dd-copier-name dd) copier)))
514 (destructuring-bind (&optional (predicate-name (symbolicate name "-P")))
516 (setf (dd-predicate-name dd) predicate-name)))
518 (when (dd-include dd)
519 (error "more than one :INCLUDE option"))
520 (setf (dd-include dd) args))
522 (require-no-print-options-so-far dd)
523 (setf (dd-print-function dd)
524 (the (or symbol cons) args)))
526 (require-no-print-options-so-far dd)
527 (setf (dd-print-object dd)
528 (the (or symbol cons) args)))
530 (destructuring-bind (type) args
531 (cond ((member type '(list vector))
532 (setf (dd-element-type dd) t)
533 (setf (dd-type dd) type))
534 ((and (consp type) (eq (first type) 'vector))
535 (destructuring-bind (vector vtype) type
536 (declare (ignore vector))
537 (setf (dd-element-type dd) vtype)
538 (setf (dd-type dd) 'vector)))
540 (error "~S is a bad :TYPE for DEFSTRUCT." type)))))
542 (error "The DEFSTRUCT option :NAMED takes no arguments."))
544 (destructuring-bind (offset) args
545 (setf (dd-offset dd) offset)))
547 (destructuring-bind (fun) args
548 (setf (dd-pure dd) fun)))
549 (t (error "unknown DEFSTRUCT option:~% ~S" option)))))
551 ;;; Given name and options, return a DD holding that info.
552 (defun parse-defstruct-name-and-options (name-and-options)
553 (destructuring-bind (name &rest options) name-and-options
554 (aver name) ; A null name doesn't seem to make sense here.
555 (let ((dd (make-defstruct-description name)))
556 (dolist (option options)
557 (cond ((eq option :named)
558 (setf (dd-named dd) t))
560 (parse-1-dd-option option dd))
561 ((member option '(:conc-name :constructor :copier :predicate))
562 (parse-1-dd-option (list option) dd))
564 (error "unrecognized DEFSTRUCT option: ~S" option))))
569 (error ":OFFSET can't be specified unless :TYPE is specified."))
570 (unless (dd-include dd)
571 ;; FIXME: It'd be cleaner to treat no-:INCLUDE as defaulting
572 ;; to :INCLUDE STRUCTURE-OBJECT, and then let the general-case
573 ;; (INCF (DD-LENGTH DD) (DD-LENGTH included-DD)) logic take
574 ;; care of this. (Except that the :TYPE VECTOR and :TYPE
575 ;; LIST cases, with their :NAMED and un-:NAMED flavors,
576 ;; make that messy, alas.)
577 (incf (dd-length dd))))
579 (require-no-print-options-so-far dd)
581 (incf (dd-length dd)))
582 (let ((offset (dd-offset dd)))
583 (when offset (incf (dd-length dd) offset)))))
585 (when (dd-include dd)
586 (frob-dd-inclusion-stuff dd))
590 ;;; Given name and options and slot descriptions (and possibly doc
591 ;;; string at the head of slot descriptions) return a DD holding that
593 (defun parse-defstruct-name-and-options-and-slot-descriptions
594 (name-and-options slot-descriptions)
595 (let ((result (parse-defstruct-name-and-options (if (atom name-and-options)
596 (list name-and-options)
598 (when (stringp (car slot-descriptions))
599 (setf (dd-doc result) (pop slot-descriptions)))
600 (dolist (slot-description slot-descriptions)
601 (allocate-1-slot result (parse-1-dsd result slot-description)))
604 ;;;; stuff to parse slot descriptions
606 ;;; Parse a slot description for DEFSTRUCT, add it to the description
607 ;;; and return it. If supplied, SLOT is a pre-initialized DSD
608 ;;; that we modify to get the new slot. This is supplied when handling
610 (defun parse-1-dsd (defstruct spec &optional
611 (slot (make-defstruct-slot-description :%name ""
614 (multiple-value-bind (name default default-p type type-p read-only ro-p)
619 &optional (default nil default-p)
620 &key (type nil type-p) (read-only nil ro-p))
624 (uncross type) type-p
627 (when (keywordp spec)
628 (style-warn "Keyword slot name indicates probable syntax ~
629 error in DEFSTRUCT: ~S."
633 (when (find name (dd-slots defstruct) :test #'string= :key #'dsd-%name)
634 (error 'simple-program-error
635 :format-control "duplicate slot name ~S"
636 :format-arguments (list name)))
637 (setf (dsd-%name slot) (string name))
638 (setf (dd-slots defstruct) (nconc (dd-slots defstruct) (list slot)))
640 (let ((accessor-name (if (dd-conc-name defstruct)
641 (symbolicate (dd-conc-name defstruct) name)
643 (predicate-name (dd-predicate-name defstruct)))
644 (setf (dsd-accessor-name slot) accessor-name)
645 (when (eql accessor-name predicate-name)
646 ;; Some adventurous soul has named a slot so that its accessor
647 ;; collides with the structure type predicate. ANSI doesn't
648 ;; specify what to do in this case. As of 2001-09-04, Martin
649 ;; Atzmueller reports that CLISP and Lispworks both give
650 ;; priority to the slot accessor, so that the predicate is
651 ;; overwritten. We might as well do the same (as well as
652 ;; signalling a warning).
654 "~@<The structure accessor name ~S is the same as the name of the ~
655 structure type predicate. ANSI doesn't specify what to do in ~
656 this case. We'll overwrite the type predicate with the slot ~
657 accessor, but you can't rely on this behavior, so it'd be wise to ~
658 remove the ambiguity in your code.~@:>"
660 (setf (dd-predicate-name defstruct) nil))
662 (when (and (fboundp accessor-name)
663 (not (accessor-inherited-data accessor-name defstruct)))
664 (style-warn "redefining ~S in DEFSTRUCT" accessor-name)))
667 (setf (dsd-default slot) default))
669 (setf (dsd-type slot)
670 (if (eq (dsd-type slot) t)
672 `(and ,(dsd-type slot) ,type))))
675 (setf (dsd-read-only slot) t)
676 (when (dsd-read-only slot)
677 (error "Slot ~S is :READ-ONLY in parent and must be :READ-ONLY in subtype ~S."
682 ;;; When a value of type TYPE is stored in a structure, should it be
683 ;;; stored in a raw slot? Return (VALUES RAW? RAW-TYPE WORDS), where
684 ;;; RAW? is true if TYPE should be stored in a raw slot.
685 ;;; RAW-TYPE is the raw slot type, or NIL if no raw slot.
686 ;;; WORDS is the number of words in the raw slot, or NIL if no raw slot.
688 ;;; FIXME: This should use the data in *RAW-SLOT-DATA-LIST*.
689 (defun structure-raw-slot-type-and-size (type)
691 (;; FIXME: For now we suppress raw slots, since there are various
692 ;; issues about the way that the cross-compiler handles them.
693 (not (boundp '*dummy-placeholder-to-stop-compiler-warnings*))
694 (values nil nil nil))
695 ((and (sb!xc:subtypep type '(unsigned-byte 32))
696 (multiple-value-bind (fixnum? fixnum-certain?)
697 (sb!xc:subtypep type 'fixnum)
698 ;; (The extra test for FIXNUM-CERTAIN? here is
699 ;; intended for bootstrapping the system. In
700 ;; particular, in sbcl-0.6.2, we set up LAYOUT before
701 ;; FIXNUM is defined, and so could bogusly end up
702 ;; putting INDEX-typed values into raw slots if we
703 ;; didn't test FIXNUM-CERTAIN?.)
704 (and (not fixnum?) fixnum-certain?)))
705 (values t 'unsigned-byte 1))
706 ((sb!xc:subtypep type 'single-float)
707 (values t 'single-float 1))
708 ((sb!xc:subtypep type 'double-float)
709 (values t 'double-float 2))
711 ((sb!xc:subtypep type 'long-float)
712 (values t 'long-float #!+x86 3 #!+sparc 4))
713 ((sb!xc:subtypep type '(complex single-float))
714 (values t 'complex-single-float 2))
715 ((sb!xc:subtypep type '(complex double-float))
716 (values t 'complex-double-float 4))
718 ((sb!xc:subtypep type '(complex long-float))
719 (values t 'complex-long-float #!+x86 6 #!+sparc 8))
721 (values nil nil nil))))
723 ;;; Allocate storage for a DSD in DD. This is where we decide whether
724 ;;; a slot is raw or not. If raw, and we haven't allocated a raw-index
725 ;;; yet for the raw data vector, then do it. Raw objects are aligned
726 ;;; on the unit of their size.
727 (defun allocate-1-slot (dd dsd)
728 (multiple-value-bind (raw? raw-type words)
729 (if (eq (dd-type dd) 'structure)
730 (structure-raw-slot-type-and-size (dsd-type dsd))
731 (values nil nil nil))
733 (setf (dsd-index dsd) (dd-length dd))
734 (incf (dd-length dd)))
736 (unless (dd-raw-index dd)
737 (setf (dd-raw-index dd) (dd-length dd))
738 (incf (dd-length dd)))
739 (let ((off (rem (dd-raw-length dd) words)))
741 (incf (dd-raw-length dd) (- words off))))
742 (setf (dsd-raw-type dsd) raw-type)
743 (setf (dsd-index dsd) (dd-raw-length dd))
744 (incf (dd-raw-length dd) words))))
747 (defun typed-structure-info-or-lose (name)
748 (or (info :typed-structure :info name)
749 (error ":TYPE'd DEFSTRUCT ~S not found for inclusion." name)))
751 ;;; Process any included slots pretty much like they were specified.
752 ;;; Also inherit various other attributes.
753 (defun frob-dd-inclusion-stuff (dd)
754 (destructuring-bind (included-name &rest modified-slots) (dd-include dd)
755 (let* ((type (dd-type dd))
758 (layout-info (compiler-layout-or-lose included-name))
759 (typed-structure-info-or-lose included-name))))
761 ;; checks on legality
762 (unless (and (eq type (dd-type included-structure))
763 (type= (specifier-type (dd-element-type included-structure))
764 (specifier-type (dd-element-type dd))))
765 (error ":TYPE option mismatch between structures ~S and ~S"
766 (dd-name dd) included-name))
767 (let ((included-classoid (find-classoid included-name nil)))
768 (when included-classoid
769 ;; It's not particularly well-defined to :INCLUDE any of the
770 ;; CMU CL INSTANCE weirdosities like CONDITION or
771 ;; GENERIC-FUNCTION, and it's certainly not ANSI-compliant.
772 (let* ((included-layout (classoid-layout included-classoid))
773 (included-dd (layout-info included-layout)))
774 (when (and (dd-alternate-metaclass included-dd)
775 ;; As of sbcl-0.pre7.73, anyway, STRUCTURE-OBJECT
776 ;; is represented with an ALTERNATE-METACLASS. But
777 ;; it's specifically OK to :INCLUDE (and PCL does)
778 ;; so in this one case, it's OK to include
779 ;; something with :ALTERNATE-METACLASS after all.
780 (not (eql included-name 'structure-object)))
781 (error "can't :INCLUDE class ~S (has alternate metaclass)"
784 (incf (dd-length dd) (dd-length included-structure))
785 (when (dd-class-p dd)
786 (let ((mc (rest (dd-alternate-metaclass included-structure))))
787 (when (and mc (not (dd-alternate-metaclass dd)))
788 (setf (dd-alternate-metaclass dd)
789 (cons included-name mc))))
790 (when (eq (dd-pure dd) :unspecified)
791 (setf (dd-pure dd) (dd-pure included-structure)))
792 (setf (dd-raw-index dd) (dd-raw-index included-structure))
793 (setf (dd-raw-length dd) (dd-raw-length included-structure)))
795 (setf (dd-inherited-accessor-alist dd)
796 (dd-inherited-accessor-alist included-structure))
797 (dolist (included-slot (dd-slots included-structure))
798 (let* ((included-name (dsd-name included-slot))
799 (modified (or (find included-name modified-slots
800 :key (lambda (x) (if (atom x) x (car x)))
803 ;; We stash away an alist of accessors to parents' slots
804 ;; that have already been created to avoid conflicts later
805 ;; so that structures with :INCLUDE and :CONC-NAME (and
806 ;; other edge cases) can work as specified.
807 (when (dsd-accessor-name included-slot)
808 ;; the "oldest" (i.e. highest up the tree of inheritance)
809 ;; will prevail, so don't push new ones on if they
811 (pushnew (cons (dsd-accessor-name included-slot)
812 (dsd-index included-slot))
813 (dd-inherited-accessor-alist dd)
814 :test #'eq :key #'car))
817 (copy-structure included-slot)))))))
819 ;;;; various helper functions for setting up DEFSTRUCTs
821 ;;; This function is called at macroexpand time to compute the INHERITS
822 ;;; vector for a structure type definition.
823 (defun inherits-for-structure (info)
824 (declare (type defstruct-description info))
825 (let* ((include (dd-include info))
826 (superclass-opt (dd-alternate-metaclass info))
829 (compiler-layout-or-lose (first include))
830 (classoid-layout (find-classoid
831 (or (first superclass-opt)
832 'structure-object))))))
833 (if (eq (dd-name info) 'ansi-stream)
834 ;; a hack to add the CL:STREAM class as a mixin for ANSI-STREAMs
835 (concatenate 'simple-vector
836 (layout-inherits super)
838 (classoid-layout (find-classoid 'stream))))
839 (concatenate 'simple-vector
840 (layout-inherits super)
843 ;;; Do miscellaneous (LOAD EVAL) time actions for the structure
844 ;;; described by DD. Create the class and LAYOUT, checking for
845 ;;; incompatible redefinition. Define those functions which are
846 ;;; sufficiently stereotyped that we can implement them as standard
848 (defun %defstruct (dd inherits)
849 (declare (type defstruct-description dd))
851 ;; We set up LAYOUTs even in the cross-compilation host.
852 (multiple-value-bind (classoid layout old-layout)
853 (ensure-structure-class dd inherits "current" "new")
854 (cond ((not old-layout)
855 (unless (eq (classoid-layout classoid) layout)
856 (register-layout layout)))
858 (let ((old-dd (layout-info old-layout)))
859 (when (defstruct-description-p old-dd)
860 (dolist (slot (dd-slots old-dd))
861 (fmakunbound (dsd-accessor-name slot))
862 (unless (dsd-read-only slot)
863 (fmakunbound `(setf ,(dsd-accessor-name slot)))))))
864 (%redefine-defstruct classoid old-layout layout)
865 (setq layout (classoid-layout classoid))))
866 (setf (find-classoid (dd-name dd)) classoid)
868 ;; Various other operations only make sense on the target SBCL.
870 (%target-defstruct dd layout))
874 ;;; Return a form describing the writable place used for this slot
875 ;;; in the instance named INSTANCE-NAME.
876 (defun %accessor-place-form (dd dsd instance-name)
877 (let (;; the operator that we'll use to access a typed slot or, in
878 ;; the case of a raw slot, to read the vector of raw slots
879 (ref (ecase (dd-type dd)
880 (structure '%instance-ref)
881 (list 'nth-but-with-sane-arg-order)
883 (raw-type (dsd-raw-type dsd)))
884 (if (eq raw-type t) ; if not raw slot
885 `(,ref ,instance-name ,(dsd-index dsd))
886 (let* ((raw-slot-data (find raw-type *raw-slot-data-list*
887 :key #'raw-slot-data-raw-type
889 (raw-slot-accessor (raw-slot-data-accessor-name raw-slot-data))
890 (raw-n-words (raw-slot-data-n-words raw-slot-data)))
891 (multiple-value-bind (scaled-dsd-index misalignment)
892 (floor (dsd-index dsd) raw-n-words)
893 (aver (zerop misalignment))
894 `(,raw-slot-accessor (,ref ,instance-name ,(dd-raw-index dd))
895 ,scaled-dsd-index))))))
897 ;;; Return inline expansion designators (i.e. values suitable for
898 ;;; (INFO :FUNCTION :INLINE-EXPANSION-DESIGNATOR ..)) for the reader
899 ;;; and writer functions of the slot described by DSD.
900 (defun slot-accessor-inline-expansion-designators (dd dsd)
901 (let ((instance-type-decl `(declare (type ,(dd-name dd) instance)))
902 (accessor-place-form (%accessor-place-form dd dsd 'instance))
903 (dsd-type (dsd-type dsd))
904 (value-the (if (dsd-safe-p dsd) 'truly-the 'the)))
905 (values (lambda () `(lambda (instance)
907 (,value-the ,dsd-type ,accessor-place-form)))
908 (lambda () `(lambda (new-value instance)
909 (declare (type ,dsd-type new-value))
911 (setf ,accessor-place-form new-value))))))
913 ;;; Return a LAMBDA form which can be used to set a slot.
914 (defun slot-setter-lambda-form (dd dsd)
915 (funcall (nth-value 1
916 (slot-accessor-inline-expansion-designators dd dsd))))
918 ;;; core compile-time setup of any class with a LAYOUT, used even by
919 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS weirdosities
920 (defun %compiler-set-up-layout (dd
922 ;; Several special cases (STRUCTURE-OBJECT
923 ;; itself, and structures with alternate
924 ;; metaclasses) call this function directly,
925 ;; and they're all at the base of the
926 ;; instance class structure, so this is
928 (inherits (vector (find-layout t)
929 (find-layout 'instance))))
931 (multiple-value-bind (classoid layout old-layout)
932 (multiple-value-bind (clayout clayout-p)
933 (info :type :compiler-layout (dd-name dd))
934 (ensure-structure-class dd
936 (if clayout-p "previously compiled" "current")
938 :compiler-layout clayout))
940 (undefine-structure (layout-classoid old-layout))
941 (when (and (classoid-subclasses classoid)
942 (not (eq layout old-layout)))
944 (dohash (classoid layout (classoid-subclasses classoid))
945 (declare (ignore layout))
946 (undefine-structure classoid)
947 (subs (classoid-proper-name classoid)))
949 (warn "removing old subclasses of ~S:~% ~S"
950 (classoid-name classoid)
953 (unless (eq (classoid-layout classoid) layout)
954 (register-layout layout :invalidate nil))
955 (setf (find-classoid (dd-name dd)) classoid)))
957 ;; At this point the class should be set up in the INFO database.
958 ;; But the logic that enforces this is a little tangled and
959 ;; scattered, so it's not obvious, so let's check.
960 (aver (find-classoid (dd-name dd) nil))
962 (setf (info :type :compiler-layout (dd-name dd)) layout))
966 ;;; Do (COMPILE LOAD EVAL)-time actions for the normal (not
967 ;;; ALTERNATE-LAYOUT) DEFSTRUCT described by DD.
968 (defun %compiler-defstruct (dd inherits)
969 (declare (type defstruct-description dd))
971 (%compiler-set-up-layout dd inherits)
973 (let* ((dtype (dd-declarable-type dd)))
975 (let ((copier-name (dd-copier-name dd)))
977 (sb!xc:proclaim `(ftype (function (,dtype) ,dtype) ,copier-name))))
979 (let ((predicate-name (dd-predicate-name dd)))
981 (sb!xc:proclaim `(ftype (function (t) t) ,predicate-name))
982 ;; Provide inline expansion (or not).
984 ((structure funcallable-structure)
985 ;; Let the predicate be inlined.
986 (setf (info :function :inline-expansion-designator predicate-name)
989 ;; This dead simple definition works because the
990 ;; type system knows how to generate inline type
991 ;; tests for instances.
992 (typep x ',(dd-name dd))))
993 (info :function :inlinep predicate-name)
996 ;; Just punt. We could provide inline expansions for :TYPE
997 ;; LIST and :TYPE VECTOR predicates too, but it'd be a
998 ;; little messier and we don't bother. (Does anyway use
999 ;; typed DEFSTRUCTs at all, let alone for high
1003 (dolist (dsd (dd-slots dd))
1004 (let* ((accessor-name (dsd-accessor-name dsd))
1005 (dsd-type (dsd-type dsd)))
1007 (let ((inherited (accessor-inherited-data accessor-name dd)))
1010 (multiple-value-bind (reader-designator writer-designator)
1011 (slot-accessor-inline-expansion-designators dd dsd)
1012 (sb!xc:proclaim `(ftype (function (,dtype) ,dsd-type)
1014 (setf (info :function :inline-expansion-designator
1017 (info :function :inlinep accessor-name)
1019 (unless (dsd-read-only dsd)
1020 (let ((setf-accessor-name `(setf ,accessor-name)))
1022 `(ftype (function (,dsd-type ,dtype) ,dsd-type)
1023 ,setf-accessor-name))
1024 (setf (info :function
1025 :inline-expansion-designator
1028 (info :function :inlinep setf-accessor-name)
1030 ((not (= (cdr inherited) (dsd-index dsd)))
1031 (style-warn "~@<Non-overwritten accessor ~S does not access ~
1032 slot with name ~S (accessing an inherited slot ~
1035 (dsd-%name dsd)))))))))
1038 ;;;; redefinition stuff
1040 ;;; Compare the slots of OLD and NEW, returning 3 lists of slot names:
1041 ;;; 1. Slots which have moved,
1042 ;;; 2. Slots whose type has changed,
1043 ;;; 3. Deleted slots.
1044 (defun compare-slots (old new)
1045 (let* ((oslots (dd-slots old))
1046 (nslots (dd-slots new))
1047 (onames (mapcar #'dsd-name oslots))
1048 (nnames (mapcar #'dsd-name nslots)))
1051 (dolist (name (intersection onames nnames))
1052 (let ((os (find name oslots :key #'dsd-name))
1053 (ns (find name nslots :key #'dsd-name)))
1054 (unless (subtypep (dsd-type ns) (dsd-type os))
1056 (unless (and (= (dsd-index os) (dsd-index ns))
1057 (eq (dsd-raw-type os) (dsd-raw-type ns)))
1061 (set-difference onames nnames)))))
1063 ;;; If we are redefining a structure with different slots than in the
1064 ;;; currently loaded version, give a warning and return true.
1065 (defun redefine-structure-warning (classoid old new)
1066 (declare (type defstruct-description old new)
1067 (type classoid classoid)
1069 (let ((name (dd-name new)))
1070 (multiple-value-bind (moved retyped deleted) (compare-slots old new)
1071 (when (or moved retyped deleted)
1073 "incompatibly redefining slots of structure class ~S~@
1074 Make sure any uses of affected accessors are recompiled:~@
1075 ~@[ These slots were moved to new positions:~% ~S~%~]~
1076 ~@[ These slots have new incompatible types:~% ~S~%~]~
1077 ~@[ These slots were deleted:~% ~S~%~]"
1078 name moved retyped deleted)
1081 ;;; This function is called when we are incompatibly redefining a
1082 ;;; structure CLASS to have the specified NEW-LAYOUT. We signal an
1083 ;;; error with some proceed options and return the layout that should
1085 (defun %redefine-defstruct (classoid old-layout new-layout)
1086 (declare (type classoid classoid)
1087 (type layout old-layout new-layout))
1088 (let ((name (classoid-proper-name classoid)))
1090 (error "~@<attempt to redefine the ~S class ~S incompatibly with the current definition~:@>"
1096 "~@<Use the new definition of ~S, invalidating ~
1097 already-loaded code and instances.~@:>"
1099 (register-layout new-layout))
1100 (recklessly-continue ()
1103 "~@<Use the new definition of ~S as if it were ~
1104 compatible, allowing old accessors to use new ~
1105 instances and allowing new accessors to use old ~
1108 ;; classic CMU CL warning: "Any old ~S instances will be in a bad way.
1109 ;; I hope you know what you're doing..."
1110 (register-layout new-layout
1112 :destruct-layout old-layout))
1114 ;; FIXME: deprecated 2002-10-16, and since it's only interactive
1115 ;; hackery instead of a supported feature, can probably be deleted
1117 :report "(deprecated synonym for RECKLESSLY-CONTINUE)"
1118 (register-layout new-layout
1120 :destruct-layout old-layout))))
1123 ;;; This is called when we are about to define a structure class. It
1124 ;;; returns a (possibly new) class object and the layout which should
1125 ;;; be used for the new definition (may be the current layout, and
1126 ;;; also might be an uninstalled forward referenced layout.) The third
1127 ;;; value is true if this is an incompatible redefinition, in which
1128 ;;; case it is the old layout.
1129 (defun ensure-structure-class (info inherits old-context new-context
1130 &key compiler-layout)
1131 (multiple-value-bind (class old-layout)
1135 (class 'structure-classoid)
1136 (constructor 'make-structure-classoid))
1137 (dd-alternate-metaclass info)
1138 (declare (ignore name))
1139 (insured-find-classoid (dd-name info)
1140 (if (eq class 'structure-classoid)
1142 (sb!xc:typep x 'structure-classoid))
1144 (sb!xc:typep x (find-classoid class))))
1145 (fdefinition constructor)))
1146 (setf (classoid-direct-superclasses class)
1147 (if (eq (dd-name info) 'ansi-stream)
1148 ;; a hack to add CL:STREAM as a superclass mixin to ANSI-STREAMs
1149 (list (layout-classoid (svref inherits (1- (length inherits))))
1150 (layout-classoid (svref inherits (- (length inherits) 2))))
1151 (list (layout-classoid
1152 (svref inherits (1- (length inherits)))))))
1153 (let ((new-layout (make-layout :classoid class
1155 :depthoid (length inherits)
1156 :length (dd-length info)
1158 (old-layout (or compiler-layout old-layout)))
1161 (values class new-layout nil))
1162 (;; This clause corresponds to an assertion in REDEFINE-LAYOUT-WARNING
1163 ;; of classic CMU CL. I moved it out to here because it was only
1164 ;; exercised in this code path anyway. -- WHN 19990510
1165 (not (eq (layout-classoid new-layout) (layout-classoid old-layout)))
1166 (error "shouldn't happen: weird state of OLD-LAYOUT?"))
1167 ((not *type-system-initialized*)
1168 (setf (layout-info old-layout) info)
1169 (values class old-layout nil))
1170 ((redefine-layout-warning old-context
1173 (layout-length new-layout)
1174 (layout-inherits new-layout)
1175 (layout-depthoid new-layout))
1176 (values class new-layout old-layout))
1178 (let ((old-info (layout-info old-layout)))
1180 ((or defstruct-description)
1181 (cond ((redefine-structure-warning class old-info info)
1182 (values class new-layout old-layout))
1184 (setf (layout-info old-layout) info)
1185 (values class old-layout nil))))
1187 (setf (layout-info old-layout) info)
1188 (values class old-layout nil))
1190 (error "shouldn't happen! strange thing in LAYOUT-INFO:~% ~S"
1192 (values class new-layout old-layout)))))))))
1194 ;;; Blow away all the compiler info for the structure CLASS. Iterate
1195 ;;; over this type, clearing the compiler structure type info, and
1196 ;;; undefining all the associated functions.
1197 (defun undefine-structure (class)
1198 (let ((info (layout-info (classoid-layout class))))
1199 (when (defstruct-description-p info)
1200 (let ((type (dd-name info)))
1201 (remhash type *typecheckfuns*)
1202 (setf (info :type :compiler-layout type) nil)
1203 (undefine-fun-name (dd-copier-name info))
1204 (undefine-fun-name (dd-predicate-name info))
1205 (dolist (slot (dd-slots info))
1206 (let ((fun (dsd-accessor-name slot)))
1207 (unless (accessor-inherited-data fun info)
1208 (undefine-fun-name fun)
1209 (unless (dsd-read-only slot)
1210 (undefine-fun-name `(setf ,fun)))))))
1211 ;; Clear out the SPECIFIER-TYPE cache so that subsequent
1212 ;; references are unknown types.
1213 (values-specifier-type-cache-clear)))
1216 ;;; Return a list of pairs (name . index). Used for :TYPE'd
1217 ;;; constructors to find all the names that we have to splice in &
1218 ;;; where. Note that these types don't have a layout, so we can't look
1219 ;;; at LAYOUT-INHERITS.
1220 (defun find-name-indices (defstruct)
1223 (do ((info defstruct
1224 (typed-structure-info-or-lose (first (dd-include info)))))
1225 ((not (dd-include info))
1230 (dolist (info infos)
1231 (incf i (or (dd-offset info) 0))
1232 (when (dd-named info)
1233 (res (cons (dd-name info) i)))
1234 (setq i (dd-length info)))))
1238 ;;; These functions are called to actually make a constructor after we
1239 ;;; have processed the arglist. The correct variant (according to the
1240 ;;; DD-TYPE) should be called. The function is defined with the
1241 ;;; specified name and arglist. VARS and TYPES are used for argument
1242 ;;; type declarations. VALUES are the values for the slots (in order.)
1244 ;;; This is split three ways because:
1245 ;;; * LIST & VECTOR structures need "name" symbols stuck in at
1246 ;;; various weird places, whereas STRUCTURE structures have
1248 ;;; * We really want to use LIST to make list structures, instead of
1249 ;;; MAKE-LIST/(SETF ELT). (We can't in general use VECTOR in an
1250 ;;; analogous way, since VECTOR makes a SIMPLE-VECTOR and vector-typed
1251 ;;; structures can have arbitrary subtypes of VECTOR, not necessarily
1253 ;;; * STRUCTURE structures can have raw slots that must also be
1254 ;;; allocated and indirectly referenced.
1255 (defun create-vector-constructor (dd cons-name arglist vars types values)
1256 (let ((temp (gensym))
1257 (etype (dd-element-type dd)))
1258 `(defun ,cons-name ,arglist
1259 (declare ,@(mapcar (lambda (var type) `(type (and ,type ,etype) ,var))
1261 (let ((,temp (make-array ,(dd-length dd)
1262 :element-type ',(dd-element-type dd))))
1263 ,@(mapcar (lambda (x)
1264 `(setf (aref ,temp ,(cdr x)) ',(car x)))
1265 (find-name-indices dd))
1266 ,@(mapcar (lambda (dsd value)
1267 (unless (eq value '.do-not-initialize-slot.)
1268 `(setf (aref ,temp ,(dsd-index dsd)) ,value)))
1269 (dd-slots dd) values)
1271 (defun create-list-constructor (dd cons-name arglist vars types values)
1272 (let ((vals (make-list (dd-length dd) :initial-element nil)))
1273 (dolist (x (find-name-indices dd))
1274 (setf (elt vals (cdr x)) `',(car x)))
1275 (loop for dsd in (dd-slots dd) and val in values do
1276 (setf (elt vals (dsd-index dsd))
1277 (if (eq val '.do-not-initialize-slot.) 0 val)))
1279 `(defun ,cons-name ,arglist
1280 (declare ,@(mapcar (lambda (var type) `(type ,type ,var)) vars types))
1282 (defun create-structure-constructor (dd cons-name arglist vars types values)
1283 (let* ((instance (gensym "INSTANCE"))
1284 (raw-index (dd-raw-index dd)))
1285 `(defun ,cons-name ,arglist
1286 (declare ,@(mapcar (lambda (var type) `(type ,type ,var))
1288 (let ((,instance (truly-the ,(dd-name dd)
1289 (%make-instance-with-layout
1290 (%delayed-get-compiler-layout ,(dd-name dd))))))
1292 `((setf (%instance-ref ,instance ,raw-index)
1293 (make-array ,(dd-raw-length dd)
1294 :element-type '(unsigned-byte 32)))))
1295 ,@(mapcar (lambda (dsd value)
1296 ;; (Note that we can't in general use the
1297 ;; ordinary named slot setter function here
1298 ;; because the slot might be :READ-ONLY, so we
1299 ;; whip up new LAMBDA representations of slot
1300 ;; setters for the occasion.)
1301 (unless (eq value '.do-not-initialize-slot.)
1302 `(,(slot-setter-lambda-form dd dsd) ,value ,instance)))
1307 ;;; Create a default (non-BOA) keyword constructor.
1308 (defun create-keyword-constructor (defstruct creator)
1309 (declare (type function creator))
1310 (collect ((arglist (list '&key))
1313 (dolist (slot (dd-slots defstruct))
1314 (let ((dum (gensym))
1315 (name (dsd-name slot)))
1316 (arglist `((,(keywordicate name) ,dum) ,(dsd-default slot)))
1317 (types (dsd-type slot))
1320 defstruct (dd-default-constructor defstruct)
1321 (arglist) (vals) (types) (vals))))
1323 ;;; Given a structure and a BOA constructor spec, call CREATOR with
1324 ;;; the appropriate args to make a constructor.
1325 (defun create-boa-constructor (defstruct boa creator)
1326 (declare (type function creator))
1327 (multiple-value-bind (req opt restp rest keyp keys allowp auxp aux)
1328 (parse-lambda-list (second boa))
1333 (labels ((get-slot (name)
1334 (let ((res (find name (dd-slots defstruct)
1338 (values (dsd-type res) (dsd-default res))
1341 (multiple-value-bind (type default) (get-slot arg)
1342 (arglist `(,arg ,default))
1348 (types (get-slot arg)))
1351 (arglist '&optional)
1355 ;; FIXME: this shares some logic (though not
1356 ;; code) with the &key case below (and it
1357 ;; looks confusing) -- factor out the logic
1358 ;; if possible. - CSR, 2002-04-19
1361 (def (nth-value 1 (get-slot name)))
1362 (supplied-test nil supplied-test-p))
1364 (arglist `(,name ,def ,@(if supplied-test-p `(,supplied-test) nil)))
1366 (types (get-slot name))))
1368 (do-default arg)))))
1371 (arglist '&rest rest)
1379 (destructuring-bind (wot
1382 (supplied-test nil supplied-test-p))
1384 (let ((name (if (consp wot)
1385 (destructuring-bind (key var) wot
1386 (declare (ignore key))
1389 (multiple-value-bind (type slot-def)
1391 (arglist `(,wot ,(if def-p def slot-def)
1392 ,@(if supplied-test-p `(,supplied-test) nil)))
1397 (when allowp (arglist '&allow-other-keys))
1403 (if (proper-list-of-length-p arg 2)
1404 (let ((var (first arg)))
1406 (types (get-slot var)))
1407 (skipped-vars (if (consp arg) (first arg) arg))))))
1409 (funcall creator defstruct (first boa)
1410 (arglist) (vars) (types)
1411 (loop for slot in (dd-slots defstruct)
1412 for name = (dsd-name slot)
1413 collect (cond ((find name (skipped-vars) :test #'string=)
1414 (setf (dsd-safe-p slot) nil)
1415 '.do-not-initialize-slot.)
1416 ((or (find (dsd-name slot) (vars) :test #'string=)
1417 (dsd-default slot)))))))))
1419 ;;; Grovel the constructor options, and decide what constructors (if
1421 (defun constructor-definitions (defstruct)
1422 (let ((no-constructors nil)
1425 (creator (ecase (dd-type defstruct)
1426 (structure #'create-structure-constructor)
1427 (vector #'create-vector-constructor)
1428 (list #'create-list-constructor))))
1429 (dolist (constructor (dd-constructors defstruct))
1430 (destructuring-bind (name &optional (boa-ll nil boa-p)) constructor
1431 (declare (ignore boa-ll))
1432 (cond ((not name) (setq no-constructors t))
1433 (boa-p (push constructor boas))
1434 (t (push name defaults)))))
1436 (when no-constructors
1437 (when (or defaults boas)
1438 (error "(:CONSTRUCTOR NIL) combined with other :CONSTRUCTORs"))
1439 (return-from constructor-definitions ()))
1441 (unless (or defaults boas)
1442 (push (symbolicate "MAKE-" (dd-name defstruct)) defaults))
1446 (let ((cname (first defaults)))
1447 (setf (dd-default-constructor defstruct) cname)
1448 (res (create-keyword-constructor defstruct creator))
1449 (dolist (other-name (rest defaults))
1450 (res `(setf (fdefinition ',other-name) (fdefinition ',cname)))
1451 (res `(declaim (ftype function ',other-name))))))
1454 (res (create-boa-constructor defstruct boa creator)))
1458 ;;;; instances with ALTERNATE-METACLASS
1460 ;;;; The CMU CL support for structures with ALTERNATE-METACLASS was a
1461 ;;;; fairly general extension embedded in the main DEFSTRUCT code, and
1462 ;;;; the result was an fairly impressive mess as ALTERNATE-METACLASS
1463 ;;;; extension mixed with ANSI CL generality (e.g. :TYPE and :INCLUDE)
1464 ;;;; and CMU CL implementation hairiness (esp. raw slots). This SBCL
1465 ;;;; version is much less ambitious, noticing that ALTERNATE-METACLASS
1466 ;;;; is only used to implement CONDITION, STANDARD-INSTANCE, and
1467 ;;;; GENERIC-FUNCTION, and defining a simple specialized
1468 ;;;; separate-from-DEFSTRUCT macro to provide only enough
1469 ;;;; functionality to support those.
1471 ;;;; KLUDGE: The defining macro here is so specialized that it's ugly
1472 ;;;; in its own way. It also violates once-and-only-once by knowing
1473 ;;;; much about structures and layouts that is already known by the
1474 ;;;; main DEFSTRUCT macro. Hopefully it will go away presently
1475 ;;;; (perhaps when CL:CLASS and SB-PCL:CLASS meet) as per FIXME below.
1476 ;;;; -- WHN 2001-10-28
1478 ;;;; FIXME: There seems to be no good reason to shoehorn CONDITION,
1479 ;;;; STANDARD-INSTANCE, and GENERIC-FUNCTION into mutated structures
1480 ;;;; instead of just implementing them as primitive objects. (This
1481 ;;;; reduced-functionality macro seems pretty close to the
1482 ;;;; functionality of DEFINE-PRIMITIVE-OBJECT..)
1484 (defun make-dd-with-alternate-metaclass (&key (class-name (missing-arg))
1485 (superclass-name (missing-arg))
1486 (metaclass-name (missing-arg))
1487 (dd-type (missing-arg))
1488 metaclass-constructor
1490 (let* ((dd (make-defstruct-description class-name))
1491 (conc-name (concatenate 'string (symbol-name class-name) "-"))
1492 (dd-slots (let ((reversed-result nil)
1493 ;; The index starts at 1 for ordinary
1494 ;; named slots because slot 0 is
1495 ;; magical, used for LAYOUT in
1496 ;; CONDITIONs or for something (?) in
1497 ;; funcallable instances.
1499 (dolist (slot-name slot-names)
1500 (push (make-defstruct-slot-description
1501 :%name (symbol-name slot-name)
1503 :accessor-name (symbolicate conc-name slot-name))
1506 (nreverse reversed-result))))
1507 (setf (dd-alternate-metaclass dd) (list superclass-name
1509 metaclass-constructor)
1510 (dd-slots dd) dd-slots
1511 (dd-length dd) (1+ (length slot-names))
1512 (dd-type dd) dd-type)
1515 (sb!xc:defmacro !defstruct-with-alternate-metaclass
1517 (slot-names (missing-arg))
1518 (boa-constructor (missing-arg))
1519 (superclass-name (missing-arg))
1520 (metaclass-name (missing-arg))
1521 (metaclass-constructor (missing-arg))
1522 (dd-type (missing-arg))
1524 (runtime-type-checks-p t))
1526 (declare (type (and list (not null)) slot-names))
1527 (declare (type (and symbol (not null))
1531 metaclass-constructor))
1532 (declare (type symbol predicate))
1533 (declare (type (member structure funcallable-structure) dd-type))
1535 (let* ((dd (make-dd-with-alternate-metaclass
1536 :class-name class-name
1537 :slot-names slot-names
1538 :superclass-name superclass-name
1539 :metaclass-name metaclass-name
1540 :metaclass-constructor metaclass-constructor
1542 (dd-slots (dd-slots dd))
1543 (dd-length (1+ (length slot-names)))
1544 (object-gensym (gensym "OBJECT"))
1545 (new-value-gensym (gensym "NEW-VALUE-"))
1546 (delayed-layout-form `(%delayed-get-compiler-layout ,class-name)))
1547 (multiple-value-bind (raw-maker-form raw-reffer-operator)
1550 (values `(let ((,object-gensym (%make-instance ,dd-length)))
1551 (setf (%instance-layout ,object-gensym)
1552 ,delayed-layout-form)
1555 (funcallable-structure
1556 (values `(%make-funcallable-instance ,dd-length
1557 ,delayed-layout-form)
1558 '%funcallable-instance-info)))
1561 (eval-when (:compile-toplevel :load-toplevel :execute)
1562 (%compiler-set-up-layout ',dd))
1564 ;; slot readers and writers
1565 (declaim (inline ,@(mapcar #'dsd-accessor-name dd-slots)))
1566 ,@(mapcar (lambda (dsd)
1567 `(defun ,(dsd-accessor-name dsd) (,object-gensym)
1568 ,@(when runtime-type-checks-p
1569 `((declare (type ,class-name ,object-gensym))))
1570 (,raw-reffer-operator ,object-gensym
1573 (declaim (inline ,@(mapcar (lambda (dsd)
1574 `(setf ,(dsd-accessor-name dsd)))
1576 ,@(mapcar (lambda (dsd)
1577 `(defun (setf ,(dsd-accessor-name dsd)) (,new-value-gensym
1579 ,@(when runtime-type-checks-p
1580 `((declare (type ,class-name ,object-gensym))))
1581 (setf (,raw-reffer-operator ,object-gensym
1583 ,new-value-gensym)))
1587 (defun ,boa-constructor ,slot-names
1588 (let ((,object-gensym ,raw-maker-form))
1589 ,@(mapcar (lambda (slot-name)
1590 (let ((dsd (find (symbol-name slot-name) dd-slots
1593 ;; KLUDGE: bug 117 bogowarning. Neither
1594 ;; DECLAREing the type nor TRULY-THE cut
1595 ;; the mustard -- it still gives warnings.
1596 (enforce-type dsd defstruct-slot-description)
1597 `(setf (,(dsd-accessor-name dsd) ,object-gensym)
1604 ;; Just delegate to the compiler's type optimization
1605 ;; code, which knows how to generate inline type tests
1606 ;; for the whole CMU CL INSTANCE menagerie.
1607 `(defun ,predicate (,object-gensym)
1608 (typep ,object-gensym ',class-name)))))))
1610 ;;;; finalizing bootstrapping
1612 ;;; Set up DD and LAYOUT for STRUCTURE-OBJECT class itself.
1614 ;;; Ordinary structure classes effectively :INCLUDE STRUCTURE-OBJECT
1615 ;;; when they have no explicit :INCLUDEs, so (1) it needs to be set up
1616 ;;; before we can define ordinary structure classes, and (2) it's
1617 ;;; special enough (and simple enough) that we just build it by hand
1618 ;;; instead of trying to generalize the ordinary DEFSTRUCT code.
1619 (defun !set-up-structure-object-class ()
1620 (let ((dd (make-defstruct-description 'structure-object)))
1622 ;; Note: This has an ALTERNATE-METACLASS only because of blind
1623 ;; clueless imitation of the CMU CL code -- dunno if or why it's
1625 (dd-alternate-metaclass dd) '(instance)
1628 (dd-type dd) 'structure)
1629 (%compiler-set-up-layout dd)))
1630 (!set-up-structure-object-class)
1632 ;;; early structure predeclarations: Set up DD and LAYOUT for ordinary
1633 ;;; (non-ALTERNATE-METACLASS) structures which are needed early.
1635 '#.(sb-cold:read-from-file
1636 "src/code/early-defstruct-args.lisp-expr"))
1637 (let* ((dd (parse-defstruct-name-and-options-and-slot-descriptions
1640 (inherits (inherits-for-structure dd)))
1641 (%compiler-defstruct dd inherits)))
1643 (/show0 "code/defstruct.lisp end of file")